be81ff32a3de70c5837da0a49c1a07488ccc814e
[openafs.git] / src / afs / VNOPS / afs_vnop_lookup.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 /*
11  * Implements:
12  * afs_lookup
13  * EvalMountPoint
14  * afs_DoBulkStat
15  */
16
17 #include <afsconfig.h>
18 #include "afs/param.h"
19
20 #include "afs/sysincludes.h"    /* Standard vendor system headers */
21 #include "afsincludes.h"        /* Afs-based standard headers */
22 #include "afs/afs_stats.h"      /* statistics */
23 #include "afs/afs_cbqueue.h"
24 #include "afs/nfsclient.h"
25 #include "afs/exporter.h"
26 #include "afs/afs_osidnlc.h"
27 #include "afs/afs_dynroot.h"
28
29 extern struct vcache *afs_globalVp;
30
31 afs_int32 afs_bkvolpref = 0;
32 afs_int32 afs_bulkStatsDone;
33 static int bulkStatCounter = 0; /* counter for bulk stat seq. numbers */
34 int afs_fakestat_enable = 0;    /* 1: fakestat-all, 2: fakestat-crosscell */
35
36
37 /* this would be faster if it did comparison as int32word, but would be 
38  * dependant on byte-order and alignment, and I haven't figured out
39  * what "@sys" is in binary... */
40 #define AFS_EQ_ATSYS(name) (((name)[0]=='@')&&((name)[1]=='s')&&((name)[2]=='y')&&((name)[3]=='s')&&(!(name)[4]))
41
42 /* call under write lock, evaluate mvid field from a mt pt.
43  * avc is the vnode of the mount point object; must be write-locked.
44  * advc is the vnode of the containing directory (optional; if NULL and
45  *   EvalMountPoint succeeds, caller must initialize *avolpp->dotdot)
46  * avolpp is where we return a pointer to the volume named by the mount pt, if success
47  * areq is the identity of the caller.
48  *
49  * NOTE: this function returns a held volume structure in *volpp if it returns 0!
50  */
51 static int
52 EvalMountData(char type, char *data, afs_uint32 states, afs_uint32 cellnum,
53               struct volume **avolpp, register struct vrequest *areq,
54               afs_uint32 *acellidxp, afs_uint32 *avolnump, afs_uint32 *avnoidp)
55 {
56     struct volume *tvp = 0;
57     struct VenusFid tfid;
58     struct cell *tcell;
59     char *cpos, *volnamep, *x;
60     char *buf;
61     afs_int32 prefetch;         /* 1=>None  2=>RO  3=>BK */
62     afs_int32 mtptCell, assocCell = 0, hac = 0;
63     afs_int32 samecell, roname, len;
64     afs_uint32 volid, cellidx, vnoid = 0;
65
66     cpos = afs_strchr(data, ':');       /* if cell name present */
67     if (cpos) {
68         cellnum = 0;
69         volnamep = cpos + 1;
70         *cpos = 0;
71         for (x = data; *x >= '0' && *x <= '9'; x++)
72             cellnum = (cellnum * 10) + (*x - '0');
73         if (cellnum && !*x)
74             tcell = afs_GetCell(cellnum, READ_LOCK);
75         else {
76             tcell = afs_GetCellByName(data, READ_LOCK);
77             cellnum = 0;
78         }
79         *cpos = ':';
80     } else if (cellnum) {
81         volnamep = data;
82         tcell = afs_GetCell(cellnum, READ_LOCK);
83     } else {
84         /*printf("No cellname %s , or cellnum %d , returning ENODEV\n", 
85                data, cellnum);*/
86         return ENODEV;
87     }
88     if (!tcell) {
89         /*printf("Lookup failed, returning ENODEV\n");*/
90         return ENODEV;
91     }
92
93     cellidx = tcell->cellIndex;
94     mtptCell = tcell->cellNum;  /* The cell for the mountpoint */
95     if (tcell->lcellp) {
96         hac = 1;                /* has associated cell */
97         assocCell = tcell->lcellp->cellNum;     /* The associated cell */
98     }
99     afs_PutCell(tcell, READ_LOCK);
100
101     cpos = afs_strrchr(volnamep, ':'); /* if vno present */
102     if (cpos) 
103         *cpos = 0;
104     /* Look for an all-numeric volume ID */
105     volid = 0;
106     for (x = volnamep; *x >= '0' && *x <= '9'; x++)
107         volid = (volid * 10) + (*x - '0');
108     if (cpos) {
109         *cpos = ':';
110         vnoid = 0;
111         if (!*x) /* allow vno with numeric volid only */
112             for (x = (cpos + 1); *x >= '0' && *x <= '9'; x++)
113                 vnoid = (vnoid * 10) + (*x - '0');
114         if (*x)
115             vnoid = 0;
116     }
117
118     /*
119      * If the volume ID was all-numeric, and they didn't ask for a
120      * pointer to the volume structure, then just return the number
121      * as-is.  This is currently only used for handling name lookups
122      * in the dynamic mount directory.
123      */
124     if (!*x && !avolpp) {
125         if (acellidxp)
126             *acellidxp = cellidx;
127         if (avolnump)
128             *avolnump = volid;
129         if (avnoidp)
130             *avnoidp = vnoid;
131         return 0;
132     }
133
134     /*
135      * If the volume ID was all-numeric, and the type was '%', then
136      * assume whoever made the mount point knew what they were doing,
137      * and don't second-guess them by forcing use of a RW volume when
138      * they gave the ID of something else.
139      */
140     if (!*x && type == '%') {
141         tfid.Fid.Volume = volid;        /* remember BK volume */
142         tfid.Cell = mtptCell;
143         tvp = afs_GetVolume(&tfid, areq, WRITE_LOCK);   /* get the new one */
144         if (!tvp) {
145             /*printf("afs_GetVolume failed - returning ENODEV");*/
146             return ENODEV;      /* oops, can't do it */
147         }
148         goto done;
149     }
150
151     /* Is volume name a "<n>.backup" or "<n>.readonly" name */
152     len = strlen(volnamep);
153     roname = ((len > 9) && (strcmp(&volnamep[len - 9], ".readonly") == 0))
154         || ((len > 7) && (strcmp(&volnamep[len - 7], ".backup") == 0));
155
156     /* When we cross mountpoint, do we stay in the same cell */
157     samecell = (cellnum == mtptCell) || (hac && (cellnum == assocCell));
158
159     /* Decide whether to prefetch the BK, or RO.  Also means we want the BK or
160      * RO.
161      * If this is a regular mountpoint with a RW volume name
162      * - If BK preference is enabled AND we remain within the same cell AND
163      *   start from a BK volume, then we will want to prefetch the BK volume.
164      * - If we cross a cell boundary OR start from a RO volume, then we will
165      *   want to prefetch the RO volume.
166      */
167     if ((type == '#') && !roname) {
168         if (afs_bkvolpref && samecell && (states & CBackup))
169             prefetch = 3;       /* Prefetch the BK */
170         else if (!samecell || (states & CRO))
171             prefetch = 2;       /* Prefetch the RO */
172         else
173             prefetch = 1;       /* Do not prefetch */
174     } else {
175         prefetch = 1;           /* Do not prefetch */
176     }
177
178     /* Get the volume struct. Unless this volume name has ".readonly" or
179      * ".backup" in it, this will get the volume struct for the RW volume.
180      * The RO volume will be prefetched if requested (but not returned).
181      */
182     /*printf("Calling GetVolumeByName\n");*/
183     tvp = afs_GetVolumeByName(volnamep, mtptCell, prefetch, areq, WRITE_LOCK);
184
185     /* If no volume was found in this cell, try the associated linked cell */
186     if (!tvp && hac && areq->volumeError) {
187         tvp =
188             afs_GetVolumeByName(volnamep, assocCell, prefetch, areq,
189                                 WRITE_LOCK);
190     }
191
192     /* Still not found. If we are looking for the RO, then perhaps the RW 
193      * doesn't exist? Try adding ".readonly" to volname and look for that.
194      * Don't know why we do this. Would have still found it in above call - jpm.
195      */
196     if (!tvp && (prefetch == 2) && len < AFS_SMALLOCSIZ - 10) {
197         buf = (char *)osi_AllocSmallSpace(len + 10);
198
199         strcpy(buf, volnamep);
200         afs_strcat(buf, ".readonly");
201
202         tvp = afs_GetVolumeByName(buf, mtptCell, 1, areq, WRITE_LOCK);
203
204         /* Try the associated linked cell if failed */
205         if (!tvp && hac && areq->volumeError) {
206             tvp = afs_GetVolumeByName(buf, assocCell, 1, areq, WRITE_LOCK);
207         }
208         osi_FreeSmallSpace(buf);
209     }
210
211     if (!tvp) {
212         /*printf("Couldn't find the volume\n");*/
213         return ENODEV;          /* Couldn't find the volume */
214     }
215
216     /* Don't cross mountpoint from a BK to a BK volume */
217     if ((states & CBackup) && (tvp->states & VBackup)) {
218         afs_PutVolume(tvp, WRITE_LOCK);
219         return ENODEV;
220     }
221
222     /* If we want (prefetched) the BK and it exists, then drop the RW volume
223      * and get the BK.
224      * Otherwise, if we want (prefetched0 the RO and it exists, then drop the
225      * RW volume and get the RO.
226      * Otherwise, go with the RW.
227      */
228     if ((prefetch == 3) && tvp->backVol) {
229         tfid.Fid.Volume = tvp->backVol; /* remember BK volume */
230         tfid.Cell = tvp->cell;
231         afs_PutVolume(tvp, WRITE_LOCK); /* release old volume */
232         tvp = afs_GetVolume(&tfid, areq, WRITE_LOCK);   /* get the new one */
233         if (!tvp)
234             return ENODEV;      /* oops, can't do it */
235     } else if ((prefetch >= 2) && tvp->roVol) {
236         tfid.Fid.Volume = tvp->roVol;   /* remember RO volume */
237         tfid.Cell = tvp->cell;
238         afs_PutVolume(tvp, WRITE_LOCK); /* release old volume */
239         tvp = afs_GetVolume(&tfid, areq, WRITE_LOCK);   /* get the new one */
240         if (!tvp)
241             return ENODEV;      /* oops, can't do it */
242     }
243
244 done:
245     if (acellidxp)
246         *acellidxp = cellidx;
247     if (avolnump)
248         *avolnump = tvp->volume;
249     if (avnoidp)
250         *avnoidp = vnoid;
251     if (avolpp)
252         *avolpp = tvp;
253     else
254         afs_PutVolume(tvp, WRITE_LOCK);
255     return 0;
256 }
257
258 int
259 EvalMountPoint(register struct vcache *avc, struct vcache *advc,
260                struct volume **avolpp, register struct vrequest *areq)
261 {
262     afs_int32 code;
263     afs_uint32 avnoid;
264
265     AFS_STATCNT(EvalMountPoint);
266 #ifdef notdef
267     if (avc->mvid && (avc->f.states & CMValid))
268         return 0;               /* done while racing */
269 #endif
270     *avolpp = NULL;
271     code = afs_HandleLink(avc, areq);
272     if (code)
273         return code;
274
275     /* Determine which cell and volume the mointpoint goes to */
276     code = EvalMountData(avc->linkData[0], avc->linkData + 1,
277                          avc->f.states, avc->f.fid.Cell, avolpp, areq, 0, 0,
278                          &avnoid);
279     if (code) return code;
280
281     if (!avnoid)
282         avnoid = 1;
283
284     if (avc->mvid == 0)
285         avc->mvid =
286             (struct VenusFid *)osi_AllocSmallSpace(sizeof(struct VenusFid));
287     avc->mvid->Cell = (*avolpp)->cell;
288     avc->mvid->Fid.Volume = (*avolpp)->volume;
289     avc->mvid->Fid.Vnode = avnoid;
290     avc->mvid->Fid.Unique = 1;
291     avc->f.states |= CMValid;
292
293     /* Used to: if the mount point is stored within a backup volume,
294      * then we should only update the parent pointer information if
295      * there's none already set, so as to avoid updating a volume's ..
296      * info with something in an OldFiles directory.
297      *
298      * Next two lines used to be under this if:
299      *
300      * if (!(avc->f.states & CBackup) || tvp->dotdot.Fid.Volume == 0)
301      *
302      * Now: update mount point back pointer on every call, so that we handle
303      * multiple mount points better.  This way, when du tries to go back
304      * via chddir(".."), it will end up exactly where it started, yet
305      * cd'ing via a new path to a volume will reset the ".." pointer
306      * to the new path.
307      */
308     (*avolpp)->mtpoint = avc->f.fid;    /* setup back pointer to mtpoint */
309     
310     if (advc)
311         (*avolpp)->dotdot = advc->f.fid;
312
313     return 0;
314 }
315
316 /*
317  * afs_InitFakeStat
318  *
319  * Must be called on an afs_fakestat_state object before calling
320  * afs_EvalFakeStat or afs_PutFakeStat.  Calling afs_PutFakeStat
321  * without calling afs_EvalFakeStat is legal, as long as this
322  * function is called.
323  */
324 void
325 afs_InitFakeStat(struct afs_fakestat_state *state)
326 {
327     if (!afs_fakestat_enable)
328         return;
329
330     state->valid = 1;
331     state->did_eval = 0;
332     state->need_release = 0;
333 }
334
335 /*
336  * afs_EvalFakeStat_int
337  *
338  * The actual implementation of afs_EvalFakeStat and afs_TryEvalFakeStat,
339  * which is called by those wrapper functions.
340  *
341  * Only issues RPCs if canblock is non-zero.
342  */
343 static int
344 afs_EvalFakeStat_int(struct vcache **avcp, struct afs_fakestat_state *state,
345                      struct vrequest *areq, int canblock)
346 {
347     struct vcache *tvc, *root_vp;
348     struct volume *tvolp = NULL;
349     int code = 0;
350
351     if (!afs_fakestat_enable)
352         return 0;
353
354     osi_Assert(state->valid == 1);
355     osi_Assert(state->did_eval == 0);
356     state->did_eval = 1;
357
358     tvc = *avcp;
359     if (tvc->mvstat != 1)
360         return 0;
361
362     /* Is the call to VerifyVCache really necessary? */
363     code = afs_VerifyVCache(tvc, areq);
364     if (code)
365         goto done;
366     if (canblock) {
367         ObtainWriteLock(&tvc->lock, 599);
368         code = EvalMountPoint(tvc, NULL, &tvolp, areq);
369         ReleaseWriteLock(&tvc->lock);
370         if (code)
371             goto done;
372         if (tvolp) {
373             tvolp->dotdot = tvc->f.fid;
374             tvolp->dotdot.Fid.Vnode = tvc->f.parent.vnode;
375             tvolp->dotdot.Fid.Unique = tvc->f.parent.unique;
376         }
377     }
378     if (tvc->mvid && (tvc->f.states & CMValid)) {
379         if (!canblock) {
380             afs_int32 retry;
381
382             do {
383                 retry = 0;
384                 ObtainWriteLock(&afs_xvcache, 597);
385                 root_vp = afs_FindVCache(tvc->mvid, &retry, IS_WLOCK);
386                 if (root_vp && retry) {
387                     ReleaseWriteLock(&afs_xvcache);
388                     afs_PutVCache(root_vp);
389                 }
390             } while (root_vp && retry);
391             ReleaseWriteLock(&afs_xvcache);
392         } else {
393             root_vp = afs_GetVCache(tvc->mvid, areq, NULL, NULL);
394         }
395         if (!root_vp) {
396             code = canblock ? ENOENT : 0;
397             goto done;
398         }
399 #ifdef AFS_DARWIN80_ENV
400         root_vp->f.m.Type = VDIR;
401         AFS_GUNLOCK();
402         code = afs_darwin_finalizevnode(root_vp, NULL, NULL, 0, 0);
403         AFS_GLOCK();
404         if (code) goto done;
405         vnode_ref(AFSTOV(root_vp));
406 #endif
407         if (tvolp && !afs_InReadDir(root_vp)) {
408             /* Is this always kosher?  Perhaps we should instead use
409              * NBObtainWriteLock to avoid potential deadlock.
410              */
411             ObtainWriteLock(&root_vp->lock, 598);
412             if (!root_vp->mvid)
413                 root_vp->mvid = osi_AllocSmallSpace(sizeof(struct VenusFid));
414             *root_vp->mvid = tvolp->dotdot;
415             ReleaseWriteLock(&root_vp->lock);
416         }
417         state->need_release = 1;
418         state->root_vp = root_vp;
419         *avcp = root_vp;
420         code = 0;
421     } else {
422         code = canblock ? ENOENT : 0;
423     }
424
425   done:
426     if (tvolp)
427         afs_PutVolume(tvolp, WRITE_LOCK);
428     return code;
429 }
430
431 /*
432  * afs_EvalFakeStat
433  *
434  * Automatically does the equivalent of EvalMountPoint for vcache entries
435  * which are mount points.  Remembers enough state to properly release
436  * the volume root vcache when afs_PutFakeStat() is called.
437  *
438  * State variable must be initialized by afs_InitFakeState() beforehand.
439  *
440  * Returns 0 when everything succeeds and *avcp points to the vcache entry
441  * that should be used for the real vnode operation.  Returns non-zero if
442  * something goes wrong and the error code should be returned to the user.
443  */
444 int
445 afs_EvalFakeStat(struct vcache **avcp, struct afs_fakestat_state *state,
446                  struct vrequest *areq)
447 {
448     return afs_EvalFakeStat_int(avcp, state, areq, 1);
449 }
450
451 /*
452  * afs_TryEvalFakeStat
453  *
454  * Same as afs_EvalFakeStat, but tries not to talk to remote servers
455  * and only evaluate the mount point if all the data is already in
456  * local caches.
457  *
458  * Returns 0 if everything succeeds and *avcp points to a valid
459  * vcache entry (possibly evaluated).
460  */
461 int
462 afs_TryEvalFakeStat(struct vcache **avcp, struct afs_fakestat_state *state,
463                     struct vrequest *areq)
464 {
465     return afs_EvalFakeStat_int(avcp, state, areq, 0);
466 }
467
468 /*
469  * afs_PutFakeStat
470  *
471  * Perform any necessary cleanup at the end of a vnode op, given that
472  * afs_InitFakeStat was previously called with this state.
473  */
474 void
475 afs_PutFakeStat(struct afs_fakestat_state *state)
476 {
477     if (!afs_fakestat_enable)
478         return;
479
480     osi_Assert(state->valid == 1);
481     if (state->need_release)
482         afs_PutVCache(state->root_vp);
483     state->valid = 0;
484 }
485
486 int
487 afs_ENameOK(register char *aname)
488 {
489     register int tlen;
490
491     AFS_STATCNT(ENameOK);
492     tlen = strlen(aname);
493     if (tlen >= 4 && strcmp(aname + tlen - 4, "@sys") == 0)
494         return 0;
495     return 1;
496 }
497
498 static int
499 afs_getsysname(register struct vrequest *areq, register struct vcache *adp,
500                register char *bufp, int *num, char **sysnamelist[])
501 {
502     register struct unixuser *au;
503     register afs_int32 error;
504
505     AFS_STATCNT(getsysname);
506
507     *sysnamelist = afs_sysnamelist;
508
509     if (!afs_nfsexporter)
510         strcpy(bufp, (*sysnamelist)[0]);
511     else {
512         au = afs_GetUser(areq->uid, adp->f.fid.Cell, 0);
513         if (au->exporter) {
514             error = EXP_SYSNAME(au->exporter, (char *)0, sysnamelist, num, 0);
515             if (error) {
516                 strcpy(bufp, "@sys");
517                 afs_PutUser(au, 0);
518                 return -1;
519             } else {
520                 strcpy(bufp, (*sysnamelist)[0]);
521             }
522         } else
523             strcpy(bufp, afs_sysname);
524         afs_PutUser(au, 0);
525     }
526     return 0;
527 }
528
529 void
530 Check_AtSys(register struct vcache *avc, const char *aname,
531             struct sysname_info *state, struct vrequest *areq)
532 {
533     int num = 0;
534     char **sysnamelist[MAXNUMSYSNAMES];
535
536     if (AFS_EQ_ATSYS(aname)) {
537         state->offset = 0;
538         state->name = (char *)osi_AllocLargeSpace(MAXSYSNAME);
539         state->allocked = 1;
540         state->index =
541             afs_getsysname(areq, avc, state->name, &num, sysnamelist);
542     } else {
543         state->offset = -1;
544         state->allocked = 0;
545         state->index = 0;
546         state->name = (char *)aname;
547     }
548 }
549
550 int
551 Next_AtSys(register struct vcache *avc, struct vrequest *areq,
552            struct sysname_info *state)
553 {
554     int num = afs_sysnamecount;
555     char **sysnamelist[MAXNUMSYSNAMES];
556
557     if (state->index == -1)
558         return 0;               /* No list */
559
560     /* Check for the initial state of aname != "@sys" in Check_AtSys */
561     if (state->offset == -1 && state->allocked == 0) {
562         register char *tname;
563
564         /* Check for .*@sys */
565         for (tname = state->name; *tname; tname++)
566             /*Move to the end of the string */ ;
567
568         if ((tname > state->name + 4) && (AFS_EQ_ATSYS(tname - 4))) {
569             state->offset = (tname - 4) - state->name;
570             tname = (char *)osi_AllocLargeSpace(AFS_LRALLOCSIZ);
571             strncpy(tname, state->name, state->offset);
572             state->name = tname;
573             state->allocked = 1;
574             num = 0;
575             state->index =
576                 afs_getsysname(areq, avc, state->name + state->offset, &num,
577                                sysnamelist);
578             return 1;
579         } else
580             return 0;           /* .*@sys doesn't match either */
581     } else {
582         register struct unixuser *au;
583         register afs_int32 error;
584
585         *sysnamelist = afs_sysnamelist;
586
587         if (afs_nfsexporter) {
588             au = afs_GetUser(areq->uid, avc->f.fid.Cell, 0);
589             if (au->exporter) {
590                 error =
591                     EXP_SYSNAME(au->exporter, (char *)0, sysnamelist, &num, 0);
592                 if (error) {
593                     afs_PutUser(au, 0);
594                     return 0;
595                 }
596             }
597             afs_PutUser(au, 0);
598         }
599         if (++(state->index) >= num || !(*sysnamelist)[(unsigned int)state->index])
600             return 0;           /* end of list */
601     }
602     strcpy(state->name + state->offset, (*sysnamelist)[(unsigned int)state->index]);
603     return 1;
604 }
605
606 extern int BlobScan(struct dcache * afile, afs_int32 ablob);
607
608 /* called with an unlocked directory and directory cookie.  Areqp
609  * describes who is making the call.
610  * Scans the next N (about 30, typically) directory entries, and does
611  * a bulk stat call to stat them all.
612  *
613  * Must be very careful when merging in RPC responses, since we dont
614  * want to overwrite newer info that was added by a file system mutating
615  * call that ran concurrently with our bulk stat call.
616  *
617  * We do that, as described below, by not merging in our info (always
618  * safe to skip the merge) if the status info is valid in the vcache entry.
619  *
620  * If adapt ever implements the bulk stat RPC, then this code will need to
621  * ensure that vcaches created for failed RPC's to older servers have the
622  * CForeign bit set.
623  */
624 static struct vcache *BStvc = NULL;
625
626 int
627 afs_DoBulkStat(struct vcache *adp, long dirCookie, struct vrequest *areqp)
628 {
629     int nentries;               /* # of entries to prefetch */
630     int nskip;                  /* # of slots in the LRU queue to skip */
631 #ifdef AFS_DARWIN80_ENV
632     struct vnode *lruvp;
633 #endif
634     struct vcache *lruvcp;      /* vcache ptr of our goal pos in LRU queue */
635     struct dcache *dcp;         /* chunk containing the dir block */
636     afs_size_t temp;            /* temp for holding chunk length, &c. */
637     struct AFSFid *fidsp;       /* file IDs were collecting */
638     struct AFSCallBack *cbsp;   /* call back pointers */
639     struct AFSCallBack *tcbp;   /* temp callback ptr */
640     struct AFSFetchStatus *statsp;      /* file status info */
641     struct AFSVolSync volSync;  /* vol sync return info */
642     struct vcache *tvcp;        /* temp vcp */
643     struct afs_q *tq;           /* temp queue variable */
644     AFSCBFids fidParm;          /* file ID parm for bulk stat */
645     AFSBulkStats statParm;      /* stat info parm for bulk stat */
646     int fidIndex = 0;           /* which file were stating */
647     struct afs_conn *tcp = 0;   /* conn for call */
648     AFSCBs cbParm;              /* callback parm for bulk stat */
649     struct server *hostp = 0;   /* host we got callback from */
650     long startTime;             /* time we started the call,
651                                  * for callback expiration base
652                                  */
653     afs_size_t statSeqNo = 0;   /* Valued of file size to detect races */
654     int code;                   /* error code */
655     long newIndex;              /* new index in the dir */
656     struct DirEntry *dirEntryp; /* dir entry we are examining */
657     int i;
658     struct VenusFid afid;       /* file ID we are using now */
659     struct VenusFid tfid;       /* another temp. file ID */
660     afs_int32 retry;            /* handle low-level SGI MP race conditions */
661     long volStates;             /* flags from vol structure */
662     struct volume *volp = 0;    /* volume ptr */
663     struct VenusFid dotdot = {0, {0, 0, 0}};
664     int flagIndex = 0;          /* First file with bulk fetch flag set */
665     int inlinebulk = 0;         /* Did we use InlineBulk RPC or not? */
666     XSTATS_DECLS;
667     dotdot.Cell = 0;
668     dotdot.Fid.Unique = 0;
669     dotdot.Fid.Vnode = 0;
670
671     /* first compute some basic parameters.  We dont want to prefetch more
672      * than a fraction of the cache in any given call, and we want to preserve
673      * a portion of the LRU queue in any event, so as to avoid thrashing
674      * the entire stat cache (we will at least leave some of it alone).
675      * presently dont stat more than 1/8 the cache in any one call.      */
676     nentries = afs_cacheStats / 8;
677
678     /* dont bother prefetching more than one calls worth of info */
679     if (nentries > AFSCBMAX)
680         nentries = AFSCBMAX;
681
682     /* heuristic to make sure that things fit in 4K.  This means that
683      * we shouldnt make it any bigger than 47 entries.  I am typically
684      * going to keep it a little lower, since we don't want to load
685      * too much of the stat cache.
686      */
687     if (nentries > 30)
688         nentries = 30;
689
690     /* now, to reduce the stack size, well allocate two 4K blocks,
691      * one for fids and callbacks, and one for stat info.  Well set
692      * up our pointers to the memory from there, too.
693      */
694     statsp = (AFSFetchStatus *) 
695             osi_Alloc(AFSCBMAX * sizeof(AFSFetchStatus));
696     fidsp = (AFSFid *) osi_AllocLargeSpace(nentries * sizeof(AFSFid));
697     cbsp = (AFSCallBack *) 
698             osi_Alloc(AFSCBMAX * sizeof(AFSCallBack));
699
700     /* next, we must iterate over the directory, starting from the specified
701      * cookie offset (dirCookie), and counting out nentries file entries.
702      * We skip files that already have stat cache entries, since we
703      * dont want to bulk stat files that are already in the cache.
704      */
705   tagain:
706     code = afs_VerifyVCache(adp, areqp);
707     if (code)
708         goto done2;
709
710     dcp = afs_GetDCache(adp, (afs_size_t) 0, areqp, &temp, &temp, 1);
711     if (!dcp) {
712         code = ENOENT;
713         goto done2;
714     }
715
716     /* lock the directory cache entry */
717     ObtainReadLock(&adp->lock);
718     ObtainReadLock(&dcp->lock);
719
720     /*
721      * Make sure that the data in the cache is current. There are two
722      * cases we need to worry about:
723      * 1. The cache data is being fetched by another process.
724      * 2. The cache data is no longer valid
725      */
726     while ((adp->f.states & CStatd)
727            && (dcp->dflags & DFFetching)
728            && hsame(adp->f.m.DataVersion, dcp->f.versionNo)) {
729         afs_Trace4(afs_iclSetp, CM_TRACE_DCACHEWAIT, ICL_TYPE_STRING,
730                    __FILE__, ICL_TYPE_INT32, __LINE__, ICL_TYPE_POINTER, dcp,
731                    ICL_TYPE_INT32, dcp->dflags);
732         ReleaseReadLock(&dcp->lock);
733         ReleaseReadLock(&adp->lock);
734         afs_osi_Sleep(&dcp->validPos);
735         ObtainReadLock(&adp->lock);
736         ObtainReadLock(&dcp->lock);
737     }
738     if (!(adp->f.states & CStatd)
739         || !hsame(adp->f.m.DataVersion, dcp->f.versionNo)) {
740         ReleaseReadLock(&dcp->lock);
741         ReleaseReadLock(&adp->lock);
742         afs_PutDCache(dcp);
743         goto tagain;
744     }
745
746     /* Generate a sequence number so we can tell whether we should
747      * store the attributes when processing the response. This number is
748      * stored in the file size when we set the CBulkFetching bit. If the
749      * CBulkFetching is still set and this value hasn't changed, then
750      * we know we were the last to set CBulkFetching bit for this file,
751      * and it is safe to set the status information for this file.
752      */
753     statSeqNo = bulkStatCounter++;
754     /* ensure against wrapping */
755     if (statSeqNo == 0)
756         statSeqNo = bulkStatCounter++;
757
758     /* now we have dir data in the cache, so scan the dir page */
759     fidIndex = 0;
760     flagIndex = 0;
761     while (1) {                 /* Should probably have some constant bound */
762         /* look for first safe entry to examine in the directory.  BlobScan
763          * looks for a the 1st allocated dir after the dirCookie slot.
764          */
765         newIndex = BlobScan(dcp, (dirCookie >> 5));
766         if (newIndex == 0)
767             break;
768
769         /* remember the updated directory cookie */
770         dirCookie = newIndex << 5;
771
772         /* get a ptr to the dir entry */
773         dirEntryp =
774             (struct DirEntry *)afs_dir_GetBlob(dcp, newIndex);
775         if (!dirEntryp)
776             break;
777
778         /* dont copy more than we have room for */
779         if (fidIndex >= nentries) {
780             DRelease(dirEntryp, 0);
781             break;
782         }
783
784         /* now, if the dir entry looks good, copy it out to our list.  Vnode
785          * 0 means deleted, although it should also be free were it deleted.
786          */
787         if (dirEntryp->fid.vnode != 0) {
788             /* dont copy entries we have in our cache.  This check will
789              * also make us skip "." and probably "..", unless it has
790              * disappeared from the cache since we did our namei call.
791              */
792             tfid.Cell = adp->f.fid.Cell;
793             tfid.Fid.Volume = adp->f.fid.Fid.Volume;
794             tfid.Fid.Vnode = ntohl(dirEntryp->fid.vnode);
795             tfid.Fid.Unique = ntohl(dirEntryp->fid.vunique);
796             do {
797                 retry = 0;
798                 ObtainWriteLock(&afs_xvcache, 130);
799                 tvcp = afs_FindVCache(&tfid, &retry, IS_WLOCK /* no stats | LRU */ );
800                 if (tvcp && retry) {
801                     ReleaseWriteLock(&afs_xvcache);
802                     afs_PutVCache(tvcp);
803                 }
804             } while (tvcp && retry);
805             if (!tvcp) {        /* otherwise, create manually */
806                 tvcp = afs_NewBulkVCache(&tfid, hostp, statSeqNo);
807                 if (tvcp)
808                 {
809                         ObtainWriteLock(&tvcp->lock, 505);
810                         ReleaseWriteLock(&afs_xvcache);
811                         afs_RemoveVCB(&tfid);
812                         ReleaseWriteLock(&tvcp->lock);
813                 } else {
814                         ReleaseWriteLock(&afs_xvcache);
815                 }
816             } else {
817                 ReleaseWriteLock(&afs_xvcache);
818             }
819             if (!tvcp)
820             {
821                 DRelease(dirEntryp, 0);
822                 ReleaseReadLock(&dcp->lock);
823                 ReleaseReadLock(&adp->lock);
824                 afs_PutDCache(dcp);
825                 goto done;      /* can happen if afs_NewVCache fails */
826             }
827
828             /* WARNING: afs_DoBulkStat uses the Length field to store a
829              * sequence number for each bulk status request. Under no
830              * circumstances should afs_DoBulkStat store a sequence number
831              * if the new length will be ignored when afs_ProcessFS is
832              * called with new stats. */
833 #ifdef AFS_SGI_ENV
834             if (!(tvcp->f.states & CStatd)
835                 && (!((tvcp->f.states & CBulkFetching) &&
836                       (tvcp->f.m.Length != statSeqNo)))
837                 && (tvcp->execsOrWriters <= 0)
838                 && !afs_DirtyPages(tvcp)
839                 && !AFS_VN_MAPPED((vnode_t *) tvcp))
840 #else
841             if (!(tvcp->f.states & CStatd)
842                 && (!((tvcp->f.states & CBulkFetching) &&
843                       (tvcp->f.m.Length != statSeqNo)))
844                 && (tvcp->execsOrWriters <= 0)
845                 && !afs_DirtyPages(tvcp))
846 #endif
847
848             {
849                 /* this entry doesnt exist in the cache, and is not
850                  * already being fetched by someone else, so add it to the
851                  * list of file IDs to obtain.
852                  *
853                  * We detect a callback breaking race condition by checking the
854                  * CBulkFetching state bit and the value in the file size.
855                  * It is safe to set the status only if the CBulkFetching
856                  * flag is still set and the value in the file size does
857                  * not change. NewBulkVCache sets us up.
858                  *
859                  * Don't fetch status for dirty files. We need to
860                  * preserve the value of the file size. We could
861                  * flush the pages, but it wouldn't be worthwhile.
862                  */
863                 memcpy((char *)(fidsp + fidIndex), (char *)&tfid.Fid,
864                        sizeof(*fidsp));
865                 fidIndex++;
866             }
867             afs_PutVCache(tvcp);
868         }
869
870         /* if dir vnode has non-zero entry */
871         /* move to the next dir entry by adding in the # of entries
872          * used by this dir entry.
873          */
874         temp = afs_dir_NameBlobs(dirEntryp->name) << 5;
875         DRelease(dirEntryp, 0);
876         if (temp <= 0)
877             break;
878         dirCookie += temp;
879     }                           /* while loop over all dir entries */
880
881     /* now release the dir lock and prepare to make the bulk RPC */
882     ReleaseReadLock(&dcp->lock);
883     ReleaseReadLock(&adp->lock);
884
885     /* release the chunk */
886     afs_PutDCache(dcp);
887
888     /* dont make a null call */
889     if (fidIndex == 0)
890         goto done;
891
892     do {
893         /* setup the RPC parm structures */
894         fidParm.AFSCBFids_len = fidIndex;
895         fidParm.AFSCBFids_val = fidsp;
896         statParm.AFSBulkStats_len = fidIndex;
897         statParm.AFSBulkStats_val = statsp;
898         cbParm.AFSCBs_len = fidIndex;
899         cbParm.AFSCBs_val = cbsp;
900
901         /* start the timer; callback expirations are relative to this */
902         startTime = osi_Time();
903
904         tcp = afs_Conn(&adp->f.fid, areqp, SHARED_LOCK);
905         if (tcp) {
906             hostp = tcp->srvr->server;
907             XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_BULKSTATUS);
908             RX_AFS_GUNLOCK();
909
910             if (!(tcp->srvr->server->flags & SNO_INLINEBULK)) {
911                 code =
912                     RXAFS_InlineBulkStatus(tcp->id, &fidParm, &statParm,
913                                            &cbParm, &volSync);
914                 if (code == RXGEN_OPCODE) {
915                     tcp->srvr->server->flags |= SNO_INLINEBULK;
916                     inlinebulk = 0;
917                     code =
918                         RXAFS_BulkStatus(tcp->id, &fidParm, &statParm,
919                                          &cbParm, &volSync);
920                 } else
921                     inlinebulk = 1;
922             } else {
923                 inlinebulk = 0;
924                 code =
925                     RXAFS_BulkStatus(tcp->id, &fidParm, &statParm, &cbParm,
926                                      &volSync);
927             }
928             RX_AFS_GLOCK();
929             XSTATS_END_TIME;
930         } else
931             code = -1;
932     } while (afs_Analyze
933              (tcp, code, &adp->f.fid, areqp, AFS_STATS_FS_RPCIDX_BULKSTATUS,
934               SHARED_LOCK, NULL));
935
936     /* now, if we didnt get the info, bail out. */
937     if (code)
938         goto done;
939
940     /* we need vol flags to create the entries properly */
941     dotdot.Fid.Volume = 0;
942     volp = afs_GetVolume(&adp->f.fid, areqp, READ_LOCK);
943     if (volp) {
944         volStates = volp->states;
945         if (volp->dotdot.Fid.Volume != 0)
946             dotdot = volp->dotdot;
947     } else
948         volStates = 0;
949
950     /* find the place to merge the info into  We do this by skipping
951      * nskip entries in the LRU queue.  The more we skip, the more
952      * we preserve, since the head of the VLRU queue is the most recently
953      * referenced file.
954      */
955   reskip:
956     nskip = afs_cacheStats / 2; /* preserved fraction of the cache */
957     ObtainReadLock(&afs_xvcache);
958     if (QEmpty(&VLRU)) {
959         /* actually a serious error, probably should panic. Probably will 
960          * panic soon, oh well. */
961         ReleaseReadLock(&afs_xvcache);
962         afs_warnuser("afs_DoBulkStat: VLRU empty!");
963         goto done;
964     }
965     if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
966         refpanic("Bulkstat VLRU inconsistent");
967     }
968     for (tq = VLRU.next; tq != &VLRU; tq = QNext(tq)) {
969         if (--nskip <= 0) {
970 #ifdef AFS_DARWIN80_ENV
971             if (!(QTOV(tq)->f.states & CDeadVnode))
972 #endif
973                 break;
974         }
975         if (QNext(QPrev(tq)) != tq) {
976             BStvc = QTOV(tq);
977             refpanic("BulkStat VLRU inconsistent");
978         }
979     }
980     if (tq != &VLRU)
981         lruvcp = QTOV(tq);
982     else
983         lruvcp = QTOV(VLRU.next);
984
985     /* now we have to hold this entry, so that it does not get moved
986      * into the free list while we're running.  It could still get
987      * moved within the lru queue, but hopefully that will be rare; it
988      * doesn't hurt nearly as much.
989      */
990     retry = 0;
991 #ifdef AFS_DARWIN80_ENV
992     lruvp = AFSTOV(lruvcp);
993     if (vnode_get(lruvp))       /* this bumps ref count */
994         retry = 1;
995     else if (vnode_ref(lruvp)) {
996         AFS_GUNLOCK();
997         /* AFSTOV(lruvcp) may be NULL */
998         vnode_put(lruvp);
999         AFS_GLOCK();
1000         retry = 1;
1001     }
1002 #else
1003     osi_vnhold(lruvcp, &retry);
1004 #endif
1005     ReleaseReadLock(&afs_xvcache);      /* could be read lock */
1006     if (retry)
1007         goto reskip;
1008
1009     /* otherwise, merge in the info.  We have to be quite careful here,
1010      * since we need to ensure that we don't merge old info over newer
1011      * stuff in a stat cache entry.  We're very conservative here: we don't
1012      * do the merge at all unless we ourselves create the stat cache
1013      * entry.  That's pretty safe, and should work pretty well, since we
1014      * typically expect to do the stat cache creation ourselves.
1015      *
1016      * We also have to take into account racing token revocations.
1017      */
1018     for (i = 0; i < fidIndex; i++) {
1019         if ((&statsp[i])->errorCode)
1020             continue;
1021         afid.Cell = adp->f.fid.Cell;
1022         afid.Fid.Volume = adp->f.fid.Fid.Volume;
1023         afid.Fid.Vnode = fidsp[i].Vnode;
1024         afid.Fid.Unique = fidsp[i].Unique;
1025         do {
1026             retry = 0;
1027             ObtainReadLock(&afs_xvcache);
1028             tvcp = afs_FindVCache(&afid, &retry, 0 /* !stats&!lru */ );
1029             ReleaseReadLock(&afs_xvcache);
1030         } while (tvcp && retry);
1031
1032         /* The entry may no longer exist */
1033         if (tvcp == NULL) {
1034             continue;
1035         }
1036
1037         /* now we have the entry held, but we need to fill it in */
1038         ObtainWriteLock(&tvcp->lock, 131);
1039
1040         /* if CBulkFetching is not set, or if the file size no longer
1041          * matches the value we placed there when we set the CBulkFetching
1042          * flag, then someone else has done something with this node,
1043          * and we may not have the latest status information for this
1044          * file.  Leave the entry alone.
1045          */
1046         if (!(tvcp->f.states & CBulkFetching) || (tvcp->f.m.Length != statSeqNo)) {
1047             flagIndex++;
1048             ReleaseWriteLock(&tvcp->lock);
1049             afs_PutVCache(tvcp);
1050             continue;
1051         }
1052
1053         /* now copy ".." entry back out of volume structure, if necessary */
1054         if (tvcp->mvstat == 2 && (dotdot.Fid.Volume != 0)) {
1055             if (!tvcp->mvid)
1056                 tvcp->mvid = (struct VenusFid *)
1057                     osi_AllocSmallSpace(sizeof(struct VenusFid));
1058             *tvcp->mvid = dotdot;
1059         }
1060
1061         ObtainWriteLock(&afs_xvcache, 132);
1062         if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
1063             refpanic("Bulkstat VLRU inconsistent2");
1064         }
1065         if ((QNext(QPrev(&tvcp->vlruq)) != &tvcp->vlruq)
1066             || (QPrev(QNext(&tvcp->vlruq)) != &tvcp->vlruq)) {
1067             refpanic("Bulkstat VLRU inconsistent4");
1068         }
1069         if ((QNext(QPrev(&lruvcp->vlruq)) != &lruvcp->vlruq)
1070             || (QPrev(QNext(&lruvcp->vlruq)) != &lruvcp->vlruq)) {
1071             refpanic("Bulkstat VLRU inconsistent5");
1072         }
1073
1074         if (tvcp != lruvcp) {   /* if they are == don't move it, don't corrupt vlru */
1075             QRemove(&tvcp->vlruq);
1076             QAdd(&lruvcp->vlruq, &tvcp->vlruq);
1077         }
1078
1079         if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
1080             refpanic("Bulkstat VLRU inconsistent3");
1081         }
1082         if ((QNext(QPrev(&tvcp->vlruq)) != &tvcp->vlruq)
1083             || (QPrev(QNext(&tvcp->vlruq)) != &tvcp->vlruq)) {
1084             refpanic("Bulkstat VLRU inconsistent5");
1085         }
1086         if ((QNext(QPrev(&lruvcp->vlruq)) != &lruvcp->vlruq)
1087             || (QPrev(QNext(&lruvcp->vlruq)) != &lruvcp->vlruq)) {
1088             refpanic("Bulkstat VLRU inconsistent6");
1089         }
1090         ReleaseWriteLock(&afs_xvcache);
1091
1092         ObtainWriteLock(&afs_xcbhash, 494);
1093
1094         /* We need to check the flags again. We may have missed
1095          * something while we were waiting for a lock.
1096          */
1097         if (!(tvcp->f.states & CBulkFetching) || (tvcp->f.m.Length != statSeqNo)) {
1098             flagIndex++;
1099             ReleaseWriteLock(&tvcp->lock);
1100             ReleaseWriteLock(&afs_xcbhash);
1101             afs_PutVCache(tvcp);
1102             continue;
1103         }
1104
1105         /* now merge in the resulting status back into the vnode.
1106          * We only do this if the entry looks clear.
1107          */
1108         afs_ProcessFS(tvcp, &statsp[i], areqp);
1109 #if defined(AFS_LINUX22_ENV)
1110         afs_fill_inode(AFSTOV(tvcp), NULL);     /* reset inode operations */
1111 #endif
1112
1113         /* do some accounting for bulk stats: mark this entry as
1114          * loaded, so we can tell if we use it before it gets
1115          * recycled.
1116          */
1117         tvcp->f.states |= CBulkStat;
1118         tvcp->f.states &= ~CBulkFetching;
1119         flagIndex++;
1120         afs_bulkStatsDone++;
1121
1122         /* merge in vol info */
1123         if (volStates & VRO)
1124             tvcp->f.states |= CRO;
1125         if (volStates & VBackup)
1126             tvcp->f.states |= CBackup;
1127         if (volStates & VForeign)
1128             tvcp->f.states |= CForeign;
1129
1130         /* merge in the callback info */
1131         tvcp->f.states |= CTruth;
1132
1133         /* get ptr to the callback we are interested in */
1134         tcbp = cbsp + i;
1135
1136         if (tcbp->ExpirationTime != 0) {
1137             tvcp->cbExpires = tcbp->ExpirationTime + startTime;
1138             tvcp->callback = hostp;
1139             tvcp->f.states |= CStatd;
1140             afs_QueueCallback(tvcp, CBHash(tcbp->ExpirationTime), volp);
1141         } else if (tvcp->f.states & CRO) {
1142             /* ordinary callback on a read-only volume -- AFS 3.2 style */
1143             tvcp->cbExpires = 3600 + startTime;
1144             tvcp->callback = hostp;
1145             tvcp->f.states |= CStatd;
1146             afs_QueueCallback(tvcp, CBHash(3600), volp);
1147         } else {
1148             tvcp->callback = 0;
1149             tvcp->f.states &= ~(CStatd | CUnique);
1150             afs_DequeueCallback(tvcp);
1151             if ((tvcp->f.states & CForeign) || (vType(tvcp) == VDIR))
1152                 osi_dnlc_purgedp(tvcp); /* if it (could be) a directory */
1153         }
1154         ReleaseWriteLock(&afs_xcbhash);
1155 #ifdef AFS_DARWIN80_ENV
1156         /* reclaim->FlushVCache will need xcbhash */
1157         if (tvcp->f.states & CDeadVnode) {
1158             /* passing in a parent hangs getting the vnode lock */
1159             code = afs_darwin_finalizevnode(tvcp, NULL, NULL, 0, 1);
1160             if (code) {
1161                 /* It's gonna get recycled - shouldn't happen */
1162                 tvcp->callback = 0;
1163                 tvcp->f.states &= ~(CStatd | CUnique);
1164                 afs_DequeueCallback(tvcp);
1165                 if ((tvcp->f.states & CForeign) || (vType(tvcp) == VDIR))
1166                     osi_dnlc_purgedp(tvcp); /* if it (could be) a directory */
1167             } else
1168                 /* re-acquire the usecount that finalizevnode disposed of */
1169                 vnode_ref(AFSTOV(tvcp));
1170         }
1171 #endif
1172
1173         ReleaseWriteLock(&tvcp->lock);
1174         /* finally, we're done with the entry */
1175         afs_PutVCache(tvcp);
1176     }                           /* for all files we got back */
1177
1178     /* finally return the pointer into the LRU queue */
1179 #ifdef AFS_DARWIN80_ENV
1180     AFS_GUNLOCK();
1181     vnode_put(lruvp);
1182     vnode_rele(lruvp);
1183     AFS_GLOCK();
1184 #else
1185     afs_PutVCache(lruvcp);
1186 #endif
1187
1188   done:
1189     /* Be sure to turn off the CBulkFetching flags */
1190     for (i = flagIndex; i < fidIndex; i++) {
1191         afid.Cell = adp->f.fid.Cell;
1192         afid.Fid.Volume = adp->f.fid.Fid.Volume;
1193         afid.Fid.Vnode = fidsp[i].Vnode;
1194         afid.Fid.Unique = fidsp[i].Unique;
1195         do {
1196             retry = 0;
1197             ObtainReadLock(&afs_xvcache);
1198             tvcp = afs_FindVCache(&afid, &retry, 0 /* !stats&!lru */ );
1199             ReleaseReadLock(&afs_xvcache);
1200         } while (tvcp && retry);
1201         if (tvcp != NULL && (tvcp->f.states & CBulkFetching)
1202             && (tvcp->f.m.Length == statSeqNo)) {
1203             tvcp->f.states &= ~CBulkFetching;
1204         }
1205         if (tvcp != NULL) {
1206             afs_PutVCache(tvcp);
1207         }
1208     }
1209     if (volp)
1210         afs_PutVolume(volp, READ_LOCK);
1211
1212     /* If we did the InlineBulk RPC pull out the return code */
1213     if (inlinebulk && code == 0) {
1214         if ((&statsp[0])->errorCode) {
1215             afs_Analyze(tcp, (&statsp[0])->errorCode, &adp->f.fid, areqp,
1216                         AFS_STATS_FS_RPCIDX_BULKSTATUS, SHARED_LOCK, NULL);
1217             code = (&statsp[0])->errorCode;
1218         }
1219     } else {
1220         code = 0;
1221     }
1222   done2:
1223     osi_FreeLargeSpace((char *)fidsp);
1224     osi_Free((char *)statsp, AFSCBMAX * sizeof(AFSFetchStatus));
1225     osi_Free((char *)cbsp, AFSCBMAX * sizeof(AFSCallBack));
1226     return code;
1227 }
1228
1229 /* was: (AFS_DEC_ENV) || defined(AFS_OSF30_ENV) || defined(AFS_NCR_ENV) */
1230 static int AFSDOBULK = 1;
1231
1232 int
1233 #if defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV)
1234 afs_lookup(OSI_VC_DECL(adp), char *aname, struct vcache **avcp, struct pathname *pnp, int flags, struct vnode *rdir, afs_ucred_t *acred)
1235 #elif defined(UKERNEL)
1236 afs_lookup(OSI_VC_DECL(adp), char *aname, struct vcache **avcp, afs_ucred_t *acred, int flags)
1237 #else
1238 afs_lookup(OSI_VC_DECL(adp), char *aname, struct vcache **avcp, afs_ucred_t *acred)
1239 #endif
1240 {
1241     struct vrequest treq;
1242     char *tname = NULL;
1243     register struct vcache *tvc = 0;
1244     register afs_int32 code;
1245     register afs_int32 bulkcode = 0;
1246     int pass = 0, hit = 0;
1247     int force_eval = afs_fakestat_enable ? 0 : 1;
1248     long dirCookie;
1249     extern afs_int32 afs_mariner;       /*Writing activity to log? */
1250     afs_hyper_t versionNo;
1251     int no_read_access = 0;
1252     struct sysname_info sysState;       /* used only for @sys checking */
1253     int dynrootRetry = 1;
1254     struct afs_fakestat_state fakestate;
1255     int tryEvalOnly = 0;
1256     OSI_VC_CONVERT(adp);
1257
1258     AFS_STATCNT(afs_lookup);
1259     afs_InitFakeStat(&fakestate);
1260
1261     AFS_DISCON_LOCK();
1262     
1263     if ((code = afs_InitReq(&treq, acred)))
1264         goto done;
1265
1266     if (afs_fakestat_enable && adp->mvstat == 1) {
1267        if (strcmp(aname, ".directory") == 0)
1268            tryEvalOnly = 1;
1269     }
1270
1271 #if defined(AFS_DARWIN_ENV)
1272     /* Workaround for MacOSX Finder, which tries to look for
1273      * .DS_Store and Contents under every directory.
1274      */
1275     if (afs_fakestat_enable && adp->mvstat == 1) {
1276         if (strcmp(aname, ".DS_Store") == 0)
1277             tryEvalOnly = 1;
1278         if (strcmp(aname, "Contents") == 0)
1279             tryEvalOnly = 1;
1280     }
1281     if (afs_fakestat_enable && adp->mvstat == 2) {
1282         if (strncmp(aname, "._", 2) == 0)
1283             tryEvalOnly = 1;
1284     }
1285 #endif
1286
1287     if (tryEvalOnly)
1288         code = afs_TryEvalFakeStat(&adp, &fakestate, &treq);
1289     else
1290         code = afs_EvalFakeStat(&adp, &fakestate, &treq);
1291
1292     /*printf("Code is %d\n", code);*/
1293     
1294     if (tryEvalOnly && adp->mvstat == 1)
1295         code = ENOENT;
1296     if (code)
1297         goto done;
1298
1299     *avcp = NULL;               /* Since some callers don't initialize it */
1300
1301     /* come back to here if we encounter a non-existent object in a read-only
1302      * volume's directory */
1303
1304   redo:
1305     *avcp = NULL;               /* Since some callers don't initialize it */
1306     bulkcode = 0;
1307
1308     if (!(adp->f.states & CStatd) && !afs_InReadDir(adp)) {
1309         if ((code = afs_VerifyVCache2(adp, &treq))) {
1310             goto done;
1311         }
1312     } else
1313         code = 0;
1314
1315     /* watch for ".." in a volume root */
1316     if (adp->mvstat == 2 && aname[0] == '.' && aname[1] == '.' && !aname[2]) {
1317         /* looking up ".." in root via special hacks */
1318         if (adp->mvid == (struct VenusFid *)0 || adp->mvid->Fid.Volume == 0) {
1319             code = ENODEV;
1320             goto done;
1321         }
1322         /* otherwise we have the fid here, so we use it */
1323         /*printf("Getting vcache\n");*/
1324         tvc = afs_GetVCache(adp->mvid, &treq, NULL, NULL);
1325         afs_Trace3(afs_iclSetp, CM_TRACE_GETVCDOTDOT, ICL_TYPE_FID, adp->mvid,
1326                    ICL_TYPE_POINTER, tvc, ICL_TYPE_INT32, code);
1327         *avcp = tvc;
1328         code = (tvc ? 0 : ENOENT);
1329         hit = 1;
1330         if (tvc && !VREFCOUNT_GT(tvc, 0)) {
1331             osi_Panic("TT1");
1332         }
1333         if (code) {
1334             /*printf("LOOKUP GETVCDOTDOT -> %d\n", code); */
1335         }
1336         goto done;
1337     }
1338
1339     /* now check the access */
1340     if (treq.uid != adp->last_looker) {
1341         if (!afs_AccessOK(adp, PRSFS_LOOKUP, &treq, CHECK_MODE_BITS)) {
1342             *avcp = NULL;
1343             code = EACCES;
1344             goto done;
1345         } else
1346             adp->last_looker = treq.uid;
1347     }
1348
1349     /* Check for read access as well.  We need read access in order to
1350      * stat files, but not to stat subdirectories. */
1351     if (!afs_AccessOK(adp, PRSFS_READ, &treq, CHECK_MODE_BITS))
1352         no_read_access = 1;
1353
1354     /* special case lookup of ".".  Can we check for it sooner in this code,
1355      * for instance, way up before "redo:" ??
1356      * I'm not fiddling with the LRUQ here, either, perhaps I should, or else 
1357      * invent a lightweight version of GetVCache.
1358      */
1359     if (aname[0] == '.' && !aname[1]) { /* special case */
1360         ObtainReadLock(&afs_xvcache);
1361         osi_vnhold(adp, 0);
1362         ReleaseReadLock(&afs_xvcache);
1363 #ifdef AFS_DARWIN80_ENV
1364         vnode_get(AFSTOV(adp));
1365 #endif
1366         code = 0;
1367         *avcp = tvc = adp;
1368         hit = 1;
1369         if (adp && !VREFCOUNT_GT(adp, 0)) {
1370             osi_Panic("TT2");
1371         }
1372         goto done;
1373     }
1374
1375     /*
1376      * Special case lookup of ".." in the dynamic mount directory.
1377      * The parent of this directory is _always_ the AFS root volume.
1378      */
1379     if (afs_IsDynrootMount(adp) &&
1380         aname[0] == '.' && aname[1] == '.' && !aname[2]) {
1381
1382         ObtainReadLock(&afs_xvcache);
1383         osi_vnhold(afs_globalVp, 0);
1384         ReleaseReadLock(&afs_xvcache);
1385 #ifdef AFS_DARWIN80_ENV
1386         vnode_get(AFSTOV(afs_globalVp));
1387 #endif
1388         code = 0;
1389         *avcp = tvc = afs_globalVp;
1390         hit = 1;
1391         goto done;
1392     }
1393
1394     /*
1395      * Special case lookups in the dynamic mount directory.
1396      * The names here take the form cell:volume, similar to a mount point.
1397      * EvalMountData parses that and returns a cell and volume ID, which
1398      * we use to construct the appropriate dynroot Fid.
1399      */
1400     if (afs_IsDynrootMount(adp)) {
1401         struct VenusFid tfid;
1402         afs_uint32 cellidx, volid, vnoid;
1403
1404         code = EvalMountData('%', aname, 0, 0, NULL, &treq, &cellidx, &volid, &vnoid);
1405         if (code)
1406             goto done;
1407         afs_GetDynrootMountFid(&tfid);
1408         tfid.Fid.Vnode = VNUM_FROM_TYPEID(VN_TYPE_MOUNT, cellidx << 2);
1409         tfid.Fid.Unique = volid;
1410         *avcp = tvc = afs_GetVCache(&tfid, &treq, NULL, NULL);
1411         hit = 1;
1412         goto done;
1413     }
1414
1415 #ifdef AFS_LINUX26_ENV
1416     /*
1417      * Special case of the dynamic mount volume in a static root.
1418      * This is really unfortunate, but we need this for the translator.
1419      */
1420     if (adp == afs_globalVp && !afs_GetDynrootEnable() &&
1421         !strcmp(aname, AFS_DYNROOT_MOUNTNAME)) {
1422         struct VenusFid tfid;
1423
1424         afs_GetDynrootMountFid(&tfid);
1425         *avcp = tvc = afs_GetVCache(&tfid, &treq, NULL, NULL);
1426         code = 0;
1427         hit = 1;
1428         goto done;
1429     }
1430 #endif
1431
1432     Check_AtSys(adp, aname, &sysState, &treq);
1433     tname = sysState.name;
1434
1435     /* 1st Check_AtSys and lookup by tname is required here, for now,
1436      * because the dnlc is *not* told to remove entries for the parent
1437      * dir of file/dir op that afs_LocalHero likes, but dnlc is informed
1438      * if the cached entry for the parent dir is invalidated for a
1439      * non-local change.
1440      * Otherwise, we'd be able to do a dnlc lookup on an entry ending
1441      * w/@sys and know the dnlc was consistent with reality. */
1442     tvc = osi_dnlc_lookup(adp, tname, WRITE_LOCK);
1443     *avcp = tvc;                /* maybe wasn't initialized, but it is now */
1444     if (tvc) {
1445         if (no_read_access && vType(tvc) != VDIR && vType(tvc) != VLNK) {
1446             /* need read access on dir to stat non-directory / non-link */
1447 #ifndef AFS_FBSD80_ENV
1448             afs_PutVCache(tvc);
1449 #endif
1450             *avcp = NULL;
1451             code = EACCES;
1452             goto done;
1453         }
1454 #ifdef AFS_LINUX22_ENV
1455         if (tvc->mvstat == 2) { /* we don't trust the dnlc for root vcaches */
1456             AFS_RELE(AFSTOV(tvc));
1457             *avcp = 0;
1458         } else {
1459             code = 0;
1460             hit = 1;
1461             goto done;
1462         }
1463 #else /* non - LINUX */
1464         code = 0;
1465         hit = 1;
1466         goto done;
1467 #endif /* linux22 */
1468     }
1469
1470     {                           /* sub-block just to reduce stack usage */
1471         register struct dcache *tdc;
1472         afs_size_t dirOffset, dirLen;
1473         struct VenusFid tfid;
1474
1475         /* now we have to lookup the next fid */
1476         if (afs_InReadDir(adp))
1477             tdc = adp->dcreaddir;
1478         else
1479             tdc = afs_GetDCache(adp, (afs_size_t) 0, &treq,
1480                                 &dirOffset, &dirLen, 1);
1481         if (!tdc) {
1482             *avcp = NULL;       /* redundant, but harmless */
1483             code = EIO;
1484             goto done;
1485         }
1486
1487         /* now we will just call dir package with appropriate inode.
1488          * Dirs are always fetched in their entirety for now */
1489         ObtainReadLock(&adp->lock);
1490         ObtainReadLock(&tdc->lock);
1491
1492         /*
1493          * Make sure that the data in the cache is current. There are two
1494          * cases we need to worry about:
1495          * 1. The cache data is being fetched by another process.
1496          * 2. The cache data is no longer valid
1497          *
1498          * If a readdir is in progress _in this thread_, it has a shared
1499          * lock on the vcache and has obtained current data, so we just
1500          * use that.  This eliminates several possible deadlocks.  
1501          */
1502         if (!afs_InReadDir(adp)) {
1503             while ((adp->f.states & CStatd)
1504                    && (tdc->dflags & DFFetching)
1505                    && hsame(adp->f.m.DataVersion, tdc->f.versionNo)) {
1506                 ReleaseReadLock(&tdc->lock);
1507                 ReleaseReadLock(&adp->lock);
1508                 afs_osi_Sleep(&tdc->validPos);
1509                 ObtainReadLock(&adp->lock);
1510                 ObtainReadLock(&tdc->lock);
1511             }
1512             if (!(adp->f.states & CStatd)
1513                 || !hsame(adp->f.m.DataVersion, tdc->f.versionNo)) {
1514                 ReleaseReadLock(&tdc->lock);
1515                 ReleaseReadLock(&adp->lock);
1516                 afs_PutDCache(tdc);
1517                 if (tname && tname != aname)
1518                     osi_FreeLargeSpace(tname);
1519                 goto redo;
1520             }
1521         }
1522
1523         /* Save the version number for when we call osi_dnlc_enter */
1524         hset(versionNo, tdc->f.versionNo);
1525
1526         /*
1527          * check for, and handle "@sys" if it's there.  We should be able
1528          * to avoid the alloc and the strcpy with a little work, but it's
1529          * not pressing.  If there aren't any remote users (ie, via the 
1530          * NFS translator), we have a slightly easier job.
1531          * the faster way to do this is to check for *aname == '@' and if 
1532          * it's there, check for @sys, otherwise, assume there's no @sys 
1533          * then, if the lookup fails, check for .*@sys...
1534          */
1535         /* above now implemented by Check_AtSys and Next_AtSys */
1536
1537         /* lookup the name in the appropriate dir, and return a cache entry
1538          * on the resulting fid */
1539         code =
1540             afs_dir_LookupOffset(tdc, sysState.name, &tfid.Fid,
1541                                  &dirCookie);
1542
1543         /* If the first lookup doesn't succeed, maybe it's got @sys in the name */
1544         while (code == ENOENT && Next_AtSys(adp, &treq, &sysState))
1545             code =
1546                 afs_dir_LookupOffset(tdc, sysState.name, &tfid.Fid,
1547                                      &dirCookie);
1548         tname = sysState.name;
1549
1550         ReleaseReadLock(&tdc->lock);
1551         if (!afs_InReadDir(adp))
1552             afs_PutDCache(tdc);
1553         if (code == ENOENT && afs_IsDynroot(adp) && dynrootRetry && !tryEvalOnly) {
1554             ReleaseReadLock(&adp->lock);
1555             dynrootRetry = 0;
1556             if (tname[0] == '.')
1557                 afs_LookupAFSDB(tname + 1);
1558             else
1559                 afs_LookupAFSDB(tname);
1560             if (tname && tname != aname)
1561                 osi_FreeLargeSpace(tname);
1562             goto redo;
1563         } else {
1564             ReleaseReadLock(&adp->lock);
1565         }
1566
1567         /* new fid has same cell and volume */
1568         tfid.Cell = adp->f.fid.Cell;
1569         tfid.Fid.Volume = adp->f.fid.Fid.Volume;
1570         afs_Trace4(afs_iclSetp, CM_TRACE_LOOKUP, ICL_TYPE_POINTER, adp,
1571                    ICL_TYPE_STRING, tname, ICL_TYPE_FID, &tfid,
1572                    ICL_TYPE_INT32, code);
1573
1574         if (code) {
1575             if (code != ENOENT) {
1576                 /*printf("LOOKUP dirLookupOff -> %d\n", code);*/
1577             }
1578             goto done;
1579         }
1580
1581         /* prefetch some entries, if the dir is currently open.  The variable
1582          * dirCookie tells us where to start prefetching from.
1583          */
1584         if (!AFS_IS_DISCONNECTED && 
1585             AFSDOBULK && adp->opens > 0 && !(adp->f.states & CForeign)
1586             && !afs_IsDynroot(adp) && !afs_InReadDir(adp)) {
1587             afs_int32 retry;
1588             /* if the entry is not in the cache, or is in the cache,
1589              * but hasn't been statd, then do a bulk stat operation.
1590              */
1591             do {
1592                 retry = 0;
1593                 ObtainReadLock(&afs_xvcache);
1594                 tvc = afs_FindVCache(&tfid, &retry, 0 /* !stats,!lru */ );
1595                 ReleaseReadLock(&afs_xvcache);
1596             } while (tvc && retry);
1597
1598             if (!tvc || !(tvc->f.states & CStatd))
1599                 bulkcode = afs_DoBulkStat(adp, dirCookie, &treq);
1600             else
1601                 bulkcode = 0;
1602
1603             /* if the vcache isn't usable, release it */
1604             if (tvc && !(tvc->f.states & CStatd)) {
1605 #ifndef  AFS_FBSD80_ENV
1606               afs_PutVCache(tvc);
1607 #endif
1608                 tvc = NULL;
1609             }
1610         } else {
1611             tvc = NULL;
1612             bulkcode = 0;
1613         }
1614
1615         /* now get the status info, if we don't already have it */
1616         /* This is kind of weird, but we might wind up accidentally calling
1617          * RXAFS_Lookup because we happened upon a file which legitimately
1618          * has a 0 uniquifier. That is the result of allowing unique to wrap
1619          * to 0. This was fixed in AFS 3.4. For CForeign, Unique == 0 means that
1620          * the file has not yet been looked up.
1621          */
1622         if (!tvc) {
1623             afs_int32 cached = 0;
1624             if (!tfid.Fid.Unique && (adp->f.states & CForeign)) {
1625                 tvc = afs_LookupVCache(&tfid, &treq, &cached, adp, tname);
1626             }
1627             if (!tvc && !bulkcode) {    /* lookup failed or wasn't called */
1628                 tvc = afs_GetVCache(&tfid, &treq, &cached, NULL);
1629             }
1630         }                       /* if !tvc */
1631     }                           /* sub-block just to reduce stack usage */
1632
1633     if (tvc) {
1634         if (adp->f.states & CForeign)
1635             tvc->f.states |= CForeign;
1636         tvc->f.parent.vnode = adp->f.fid.Fid.Vnode;
1637         tvc->f.parent.unique = adp->f.fid.Fid.Unique;
1638         tvc->f.states &= ~CBulkStat;
1639
1640         if (afs_fakestat_enable == 2 && tvc->mvstat == 1) {
1641             ObtainSharedLock(&tvc->lock, 680);
1642             if (!tvc->linkData) {
1643                 UpgradeSToWLock(&tvc->lock, 681);
1644                 code = afs_HandleLink(tvc, &treq);
1645                 ConvertWToRLock(&tvc->lock);
1646             } else {
1647                 ConvertSToRLock(&tvc->lock);
1648                 code = 0;
1649             }
1650             if (!code && !afs_strchr(tvc->linkData, ':'))
1651                 force_eval = 1;
1652             ReleaseReadLock(&tvc->lock);
1653         }
1654         if (tvc->mvstat == 1 && (tvc->f.states & CMValid) && tvc->mvid != NULL)
1655           force_eval = 1; /* This is now almost for free, get it correct */
1656
1657 #if defined(UKERNEL) && defined(AFS_WEB_ENHANCEMENTS)
1658         if (!(flags & AFS_LOOKUP_NOEVAL))
1659             /* don't eval mount points */
1660 #endif /* UKERNEL && AFS_WEB_ENHANCEMENTS */
1661             if (tvc->mvstat == 1 && force_eval) {
1662                 /* a mt point, possibly unevaluated */
1663                 struct volume *tvolp;
1664
1665                 ObtainWriteLock(&tvc->lock, 133);
1666                 code = EvalMountPoint(tvc, adp, &tvolp, &treq);
1667                 ReleaseWriteLock(&tvc->lock);
1668
1669                 if (code) {
1670 #ifndef AFS_FBSD80_ENV
1671                     afs_PutVCache(tvc);
1672 #endif
1673                     if (tvolp)
1674                         afs_PutVolume(tvolp, WRITE_LOCK);
1675                     goto done;
1676                 }
1677
1678                 /* next, we want to continue using the target of the mt point */
1679                 if (tvc->mvid && (tvc->f.states & CMValid)) {
1680                     struct vcache *uvc;
1681                     /* now lookup target, to set .. pointer */
1682                     afs_Trace2(afs_iclSetp, CM_TRACE_LOOKUP1,
1683                                ICL_TYPE_POINTER, tvc, ICL_TYPE_FID,
1684                                &tvc->f.fid);
1685                     uvc = tvc;  /* remember for later */
1686
1687                     if (tvolp && (tvolp->states & VForeign)) {
1688                         /* XXXX tvolp has ref cnt on but not locked! XXX */
1689                         tvc =
1690                             afs_GetRootVCache(tvc->mvid, &treq, NULL, tvolp);
1691                     } else {
1692                         tvc = afs_GetVCache(tvc->mvid, &treq, NULL, NULL);
1693                     }
1694 #ifndef AFS_FBSD80_ENV
1695                     afs_PutVCache(uvc); /* we're done with it */
1696 #endif
1697
1698                     if (!tvc) {
1699                         code = ENOENT;
1700                         if (tvolp) {
1701                             afs_PutVolume(tvolp, WRITE_LOCK);
1702                         }
1703                         goto done;
1704                     }
1705
1706                     /* now, if we came via a new mt pt (say because of a new
1707                      * release of a R/O volume), we must reevaluate the ..
1708                      * ptr to point back to the appropriate place */
1709                     if (tvolp) {
1710                         ObtainWriteLock(&tvc->lock, 134);
1711                         if (tvc->mvid == NULL) {
1712                             tvc->mvid = (struct VenusFid *)
1713                                 osi_AllocSmallSpace(sizeof(struct VenusFid));
1714                         }
1715                         /* setup backpointer */
1716                         *tvc->mvid = tvolp->dotdot;
1717                         ReleaseWriteLock(&tvc->lock);
1718                         afs_PutVolume(tvolp, WRITE_LOCK);
1719                     }
1720                 } else {
1721 #ifndef AFS_FBSD80_ENV
1722                     afs_PutVCache(tvc);
1723 #endif
1724                     code = ENOENT;
1725                     if (tvolp)
1726                         afs_PutVolume(tvolp, WRITE_LOCK);
1727                     goto done;
1728                 }
1729             }
1730         *avcp = tvc;
1731         if (tvc && !VREFCOUNT_GT(tvc, 0)) {
1732             osi_Panic("TT3");
1733         }
1734         code = 0;
1735     } else {
1736         /* if we get here, we found something in a directory that couldn't
1737          * be located (a Multics "connection failure").  If the volume is
1738          * read-only, we try flushing this entry from the cache and trying
1739          * again. */
1740         if (!AFS_IS_DISCONNECTED) {
1741             if (pass == 0) {
1742                 struct volume *tv;
1743                 tv = afs_GetVolume(&adp->f.fid, &treq, READ_LOCK);
1744                 if (tv) {
1745                     if (tv->states & VRO) {
1746                         pass = 1;       /* try this *once* */
1747                         ObtainWriteLock(&afs_xcbhash, 495);
1748                         afs_DequeueCallback(adp);
1749                         /* re-stat to get later version */
1750                         adp->f.states &= ~CStatd;
1751                         ReleaseWriteLock(&afs_xcbhash);
1752                         osi_dnlc_purgedp(adp);
1753                         afs_PutVolume(tv, READ_LOCK);
1754                         goto redo;
1755                     }
1756                     afs_PutVolume(tv, READ_LOCK);
1757                 }
1758             }
1759             code = ENOENT;
1760         } else {
1761             code = ENETDOWN;
1762         }
1763     }
1764
1765   done:
1766     /* put the network buffer back, if need be */
1767     if (tname != aname && tname)
1768         osi_FreeLargeSpace(tname);
1769     if (code == 0) {
1770
1771         if (afs_mariner)
1772             afs_AddMarinerName(aname, tvc);
1773
1774 #if defined(UKERNEL) && defined(AFS_WEB_ENHANCEMENTS)
1775         if (!(flags & AFS_LOOKUP_NOEVAL)) {
1776             /* Here we don't enter the name into the DNLC because we want the
1777              * evaluated mount dir to be there (the vcache for the mounted
1778              * volume) rather than the vc of the mount point itself.  We can
1779              * still find the mount point's vc in the vcache by its fid. */
1780 #endif /* UKERNEL && AFS_WEB_ENHANCEMENTS */
1781             if (!hit && force_eval) {
1782                 osi_dnlc_enter(adp, aname, tvc, &versionNo);
1783             } else {
1784 #ifdef AFS_LINUX20_ENV
1785                 /* So Linux inode cache is up to date. */
1786                 code = afs_VerifyVCache(tvc, &treq);
1787 #else
1788                 afs_PutFakeStat(&fakestate);
1789                 AFS_DISCON_UNLOCK();
1790                 return 0;       /* can't have been any errors if hit and !code */
1791 #endif
1792             }
1793 #if defined(UKERNEL) && defined(AFS_WEB_ENHANCEMENTS)
1794         }
1795 #endif
1796     }
1797     if (bulkcode)
1798         code = bulkcode;
1799
1800     code = afs_CheckCode(code, &treq, 19);
1801     if (code) {
1802         /* If there is an error, make sure *avcp is null.
1803          * Alphas panic otherwise - defect 10719.
1804          */
1805         *avcp = NULL;
1806     }
1807
1808     afs_PutFakeStat(&fakestate);
1809     AFS_DISCON_UNLOCK();
1810     return code;
1811 }