afs: Do not supply bogus poll vnodeops for FBSD
[openafs.git] / src / afs / afs_disconnected.c
1 /*
2  * This software has been released under the terms of the IBM Public
3  * License.  For details, see the LICENSE file in the top-level source
4  * directory or online at http://www.openafs.org/dl/license10.html
5  */
6
7 #include <afsconfig.h>
8 #include "afs/param.h"
9
10
11 #include "afs/sysincludes.h"
12 #include "afsincludes.h"
13 #include "afs/afs_stats.h"      /* statistics */
14 #include "afs/lock.h"
15 #include "afs/afs_cbqueue.h"
16
17 #define dv_match(vc, fstat)                              \
18         ((vc->f.m.DataVersion.low == fstat.DataVersion) && \
19         (vc->f.m.DataVersion.high == fstat.dataVersionHigh))
20
21 /*! Circular queue of dirty vcaches */
22 struct afs_q afs_disconDirty;
23
24 /*! Circular queue of vcaches with shadow directories */
25 struct afs_q afs_disconShadow;
26
27 /*! Locks both of these lists. Must be write locked for anything other than
28  *  list traversal */
29 afs_rwlock_t afs_disconDirtyLock;
30
31 extern afs_int32 *afs_dvhashTbl;        /*Data cache hash table */
32 extern afs_int32 *afs_dchashTbl;        /*Data cache hash table */
33 extern afs_int32 *afs_dvnextTbl;        /*Dcache hash table links */
34 extern afs_int32 *afs_dcnextTbl;        /*Dcache hash table links */
35 extern struct dcache **afs_indexTable;  /*Pointers to dcache entries */
36
37 /*! Vnode number. On file creation, use the current value and increment it.
38  */
39 afs_uint32 afs_DisconVnode = 2;
40
41 /*! Conflict policy. */
42 enum {
43         CLIENT_WINS = 0,
44         SERVER_WINS,
45         LAST_CLOSER_WINS,
46         ASK
47 };
48
49 afs_int32 afs_ConflictPolicy = SERVER_WINS;
50
51 static void afs_DisconDiscardAllShadows(int, afs_ucred_t *);
52 void afs_DbgListDirEntries(struct VenusFid *afid);
53
54
55 /*!
56  * Find the first dcache of a file that has the specified fid.
57  * Similar to afs_FindDCache, only that it takes a fid instead
58  * of a vcache and it can get the first dcache.
59  *
60  * \param afid
61  *
62  * \return The found dcache or NULL.
63  */
64 struct dcache *
65 afs_FindDCacheByFid(struct VenusFid *afid)
66 {
67     afs_int32 i, index;
68     struct dcache *tdc = NULL;
69
70     i = DVHash(afid);
71     ObtainWriteLock(&afs_xdcache, 758);
72     for (index = afs_dvhashTbl[i]; index != NULLIDX;) {
73         if (afs_indexUnique[index] == afid->Fid.Unique) {
74             tdc = afs_GetValidDSlot(index);
75             if (tdc) {
76                 ReleaseReadLock(&tdc->tlock);
77                 if (!FidCmp(&tdc->f.fid, afid)) {
78                     break;              /* leaving refCount high for caller */
79                 }
80                 afs_PutDCache(tdc);
81             }
82         }
83         index = afs_dvnextTbl[index];
84     }
85     ReleaseWriteLock(&afs_xdcache);
86
87     if (index == NULLIDX)
88         tdc = NULL;
89     return tdc;
90 }
91
92 /*!
93  * Generate a store status from a dirty vcache entry.
94  *
95  * \param avc Dirty vcache entry.
96  * \param astat
97  *
98  * \note The vnode must be share locked. It is called only on resync,
99  * where the vnode is write locked locally and and the server.
100  *
101  * \return Mask of operations.
102  */
103 int
104 afs_GenStoreStatus(struct vcache *avc, struct AFSStoreStatus *astat)
105 {
106     if (!avc || !astat || !avc->f.ddirty_flags)
107         return 0;
108
109     /* Clean up store stat. */
110     memset(astat, 0, sizeof(struct AFSStoreStatus));
111
112     if (avc->f.ddirty_flags & VDisconSetTime) {
113         /* Update timestamp. */
114         astat->ClientModTime = avc->f.m.Date;
115         astat->Mask |= AFS_SETMODTIME;
116     }
117
118     if (avc->f.ddirty_flags & VDisconSetMode) {
119         /* Copy the mode bits. */
120         astat->UnixModeBits = avc->f.m.Mode;
121         astat->Mask |= AFS_SETMODE;
122    }
123
124    /* XXX: more to come... ?*/
125
126    return astat->Mask;
127 }
128
129 /*!
130  * Hook for filtering the local dir fid by searching the "." entry.
131  *
132  * \param hdata The fid to be filled.
133  */
134 static int
135 get_parent_dir_fid_hook(void *hdata, char *aname, afs_int32 vnode,
136                         afs_int32 unique)
137 {
138     struct VenusFid *tfid = (struct VenusFid *) hdata;
139
140     if ((aname[0] == '.') && (aname[1] == '.') && !aname[2]) {
141         tfid->Fid.Vnode = vnode;
142         tfid->Fid.Unique = unique;
143         return 1;
144     }
145
146     return 0;
147 }
148
149 /*!
150  * Get a the dir's fid by looking in the vcache for simple files and
151  * in the ".." entry for directories.
152  *
153  * \param avc The file's vhash entry.
154  * \param afid Put the fid here.
155  *
156  * \return 0 on success, -1 on failure
157  */
158 int
159 afs_GetParentDirFid(struct vcache *avc, struct VenusFid *afid)
160 {
161     struct dcache *tdc;
162
163     afid->Cell = avc->f.fid.Cell;
164     afid->Fid.Volume = avc->f.fid.Fid.Volume;
165
166     switch (vType(avc)) {
167     case VREG:
168     case VLNK:
169         /* Normal files have the dir fid embedded in the vcache. */
170         afid->Fid.Vnode = avc->f.parent.vnode;
171         afid->Fid.Unique = avc->f.parent.unique;
172         break;
173     case VDIR:
174         /* If dir or parent dir created locally*/
175         tdc = afs_FindDCacheByFid(&avc->f.fid);
176         if (tdc) {
177             afid->Fid.Unique = 0;
178             /* Lookup each entry for the fid. It should be the first. */
179             afs_dir_EnumerateDir(tdc, &get_parent_dir_fid_hook, afid);
180             afs_PutDCache(tdc);
181             if (afid->Fid.Unique == 0) {
182                 return -1;
183             }
184         } else {
185             return -1;
186         }
187         break;
188     default:
189         return -1;
190     }
191
192     return 0;
193 }
194
195 struct NameAndFid {
196     struct VenusFid *fid;
197     char *name;
198     int name_len;
199 };
200
201 /*!
202  * Hook that searches a certain fid's name.
203  *
204  * \param hdata NameAndFid structure containin a pointer to a fid
205  * and an allocate name. The name will be filled when hit.
206  */
207 static int
208 get_vnode_name_hook(void *hdata, char *aname, afs_int32 vnode,
209                     afs_int32 unique)
210 {
211     struct NameAndFid *nf = (struct NameAndFid *) hdata;
212
213     if ((nf->fid->Fid.Vnode == vnode) &&
214         (nf->fid->Fid.Unique == unique)) {
215         nf->name_len = strlen(aname);
216         memcpy(nf->name, aname, nf->name_len);
217         nf->name[nf->name_len] = 0;
218
219         return 1;
220     }
221
222     return 0;
223 }
224
225 /*!
226  * Try to get a vnode's name by comparing all parent dir's entries
227  * to the given fid. It can also return the dir's dcache.
228  *
229  * \param avc The file's vcache.
230  * \param afid The parent dir's fid.
231  * \param aname A preallocated string for the name.
232  * \param deleted Has this file been deleted? If yes, use the shadow
233  * dir for looking up the name.
234  */
235 int
236 afs_GetVnodeName(struct vcache *avc, struct VenusFid *afid, char *aname,
237                  int deleted)
238 {
239     int code = 0;
240     struct dcache *tdc;
241     struct vcache *parent_vc;
242     struct NameAndFid tnf;
243     struct VenusFid parent_fid;
244     struct VenusFid shadow_fid;
245
246     /* List dir contents and get it's tdc. */
247     if (deleted) {
248         /* For deleted files, get the shadow dir's tdc: */
249
250         /* Get the parent dir's vcache that contains the shadow fid. */
251         parent_fid.Cell = avc->f.fid.Cell;
252         parent_fid.Fid.Volume = avc->f.fid.Fid.Volume;
253         if (avc->f.ddirty_flags & VDisconRename) {
254             /* For renames the old dir fid is needed. */
255             parent_fid.Fid.Vnode = avc->f.oldParent.vnode;
256             parent_fid.Fid.Unique = avc->f.oldParent.unique;
257         } else {
258             parent_fid.Fid.Vnode = afid->Fid.Vnode;
259             parent_fid.Fid.Unique = afid->Fid.Unique;
260         }
261
262         /* Get the parent dir's vcache that contains the shadow fid. */
263         ObtainSharedLock(&afs_xvcache, 755);
264         parent_vc = afs_FindVCache(&parent_fid, 0, 1);
265         ReleaseSharedLock(&afs_xvcache);
266         if (!parent_vc) {
267             return ENOENT;
268         }
269
270         shadow_fid.Cell = parent_vc->f.fid.Cell;
271         shadow_fid.Fid.Volume = parent_vc->f.fid.Fid.Volume;
272         shadow_fid.Fid.Vnode = parent_vc->f.shadow.vnode;
273         shadow_fid.Fid.Unique = parent_vc->f.shadow.unique;
274
275         afs_PutVCache(parent_vc);
276
277         /* Get shadow dir's dcache. */
278         tdc = afs_FindDCacheByFid(&shadow_fid);
279
280     } else {
281
282         /* For normal files, look into the current dir's entry. */
283         tdc = afs_FindDCacheByFid(afid);
284     }                   /* if (deleted) */
285
286     if (tdc) {
287         tnf.fid = &avc->f.fid;
288         tnf.name_len = -1;
289         tnf.name = aname;
290         afs_dir_EnumerateDir(tdc, &get_vnode_name_hook, &tnf);
291         afs_PutDCache(tdc);
292         if (tnf.name_len == -1)
293             code = ENOENT;
294     } else {
295         /* printf("Directory dcache not found!\n"); */
296         code = ENOENT;
297     }
298
299     return code;
300 }
301
302 struct DirtyChildrenCount {
303     struct vcache *vc;
304     afs_uint32 count;
305 };
306
307 /*!
308  * Lookup dirty deleted vnodes in this dir.
309  */
310 static int
311 chk_del_children_hook(void *hdata, char *aname, afs_int32 vnode,
312                       afs_int32 unique)
313 {
314     struct VenusFid tfid;
315     struct DirtyChildrenCount *v = (struct DirtyChildrenCount *) hdata;
316     struct vcache *tvc;
317
318     if ((aname[0] == '.') && !aname[1])
319         /* Skip processing this dir again.
320          * It would result in an endless loop.
321          */
322         return 0;
323
324     if ((aname[0] == '.') && (aname[1] == '.') && !aname[2])
325         /* Don't process parent dir. */
326         return 0;
327
328     /* Get this file's vcache. */
329     tfid.Cell = v->vc->f.fid.Cell;
330     tfid.Fid.Volume = v->vc->f.fid.Fid.Volume;
331     tfid.Fid.Vnode = vnode;
332     tfid.Fid.Unique = unique;
333
334     ObtainSharedLock(&afs_xvcache, 757);
335     tvc = afs_FindVCache(&tfid, 0, 1);
336     ReleaseSharedLock(&afs_xvcache);
337
338     /* Count unfinished dirty children. */
339     if (tvc) {
340         ObtainReadLock(&tvc->lock);
341         if (tvc->f.ddirty_flags)
342             v->count++;
343         ReleaseReadLock(&tvc->lock);
344
345         afs_PutVCache(tvc);
346     }
347
348     return 0;
349 }
350
351 /*!
352  * Check if entries have been deleted in a vnode's shadow
353  * dir.
354  *
355  * \return Returns the number of dirty children.
356  *
357  * \note afs_DDirtyVCListLock must be write locked.
358  */
359 int
360 afs_CheckDeletedChildren(struct vcache *avc)
361 {
362     struct dcache *tdc;
363     struct DirtyChildrenCount dcc;
364     struct VenusFid shadow_fid;
365
366     if (!avc->f.shadow.vnode)
367         /* Empty dir. */
368         return 0;
369
370     shadow_fid.Cell = avc->f.fid.Cell;
371     shadow_fid.Fid.Volume = avc->f.fid.Fid.Volume;
372     shadow_fid.Fid.Vnode = avc->f.shadow.vnode;
373     shadow_fid.Fid.Unique = avc->f.shadow.unique;
374
375     dcc.count = 0;
376
377     /* Get shadow dir's dcache. */
378     tdc = afs_FindDCacheByFid(&shadow_fid);
379     if (tdc) {
380         dcc.vc = avc;
381         afs_dir_EnumerateDir(tdc, &chk_del_children_hook, &dcc);
382         afs_PutDCache(tdc);
383     }
384
385     return dcc.count;
386 }
387
388 /*!
389  * Changes a file's parent fid references.
390  */
391 static int
392 fix_children_fids_hook(void *hdata, char *aname, afs_int32 vnode,
393                        afs_int32 unique)
394 {
395     struct VenusFid tfid;
396     struct VenusFid *afid = (struct VenusFid *) hdata;
397     struct vcache *tvc;
398     struct dcache *tdc = NULL;
399
400     if ((aname[0] == '.') && !aname[1])
401         return 0;
402
403     if ((aname[0] == '.') && (aname[1] == '.') && !aname[2])
404         return 0;
405
406     tfid.Cell = afid->Cell;
407     tfid.Fid.Volume = afid->Fid.Volume;
408     tfid.Fid.Vnode = vnode;
409     tfid.Fid.Unique = unique;
410
411     if (!(vnode % 2)) {
412         /* vnode's parity indicates that it's a file. */
413
414         /* Get the vcache. */
415         ObtainSharedLock(&afs_xvcache, 759);
416         tvc = afs_FindVCache(&tfid, 0, 1);
417         ReleaseSharedLock(&afs_xvcache);
418
419         /* Change the fields. */
420         if (tvc) {
421             tvc->f.parent.vnode = afid->Fid.Vnode;
422             tvc->f.parent.unique = afid->Fid.Unique;
423
424             afs_PutVCache(tvc);
425         }
426     } else {
427         /* It's a dir. Fix this dir's .. entry to contain the new fid. */
428         /* Seek the dir's dcache. */
429         tdc = afs_FindDCacheByFid(&tfid);
430         if (tdc) {
431             /* Change the .. entry fid. */
432             afs_dir_ChangeFid(tdc, "..", NULL, &afid->Fid.Vnode);
433             afs_PutDCache(tdc);
434         }
435     }                   /* if (!(vnode % 2))*/
436
437     return 0;
438 }
439
440 /*!
441  * Fixes the parentVnode and parentUnique fields of all
442  * files (not dirs) contained in the directory pointed by
443  * old_fid. This is useful on resync, when a locally created dir
444  * get's a new fid and all the children references must be updated
445  * to reflect the new fid.
446  *
447  * \note The dir's fid hasn't been changed yet, it is still referenced
448  * with the old fid.
449  *
450  * \param old_fid The current dir's fid.
451  * \param new_fid The new dir's fid.
452  */
453 void
454 afs_FixChildrenFids(struct VenusFid *old_fid, struct VenusFid *new_fid)
455 {
456     struct dcache *tdc;
457
458     /* Get shadow dir's dcache. */
459     tdc = afs_FindDCacheByFid(old_fid);
460     /* Change the fids. */
461     if (tdc) {
462         afs_dir_EnumerateDir(tdc, &fix_children_fids_hook, new_fid);
463         afs_PutDCache(tdc);
464     }
465 }
466
467 static int
468 list_dir_hook(void *hdata, char *aname, afs_int32 vnode, afs_int32 unique)
469 {
470     /* printf("list_dir_hook: %s v:%u u:%u\n", aname, vnode, unique); */
471     return 0;
472 }
473
474 void
475 afs_DbgListDirEntries(struct VenusFid *afid)
476 {
477     struct dcache *tdc;
478
479     /* Get shadow dir's dcache. */
480     tdc = afs_FindDCacheByFid(afid);
481     if (tdc) {
482         afs_dir_EnumerateDir(tdc, &list_dir_hook, NULL);
483         afs_PutDCache(tdc);
484     }
485 }
486
487 /*!
488  * Find the parent vcache for a given child
489  *
490  * \param avc   The vcache whose parent is required
491  * \param afid  Fid structure in which parent's fid should be stored
492  * \param aname An AFSNAMEMAX sized buffer to hold the parents name
493  * \param adp   A pointer to a struct vcache* which will be set to the
494  *              parent vcache
495  *
496  * \return An error code. 0 indicates success, EAGAIN that the vnode should
497  *         be deferred to later in the resync process
498  */
499
500 int
501 afs_GetParentVCache(struct vcache *avc, int deleted, struct VenusFid *afid,
502                     char *aname, struct vcache **adp)
503 {
504     int code;
505
506     *adp = NULL;
507
508     if (afs_GetParentDirFid(avc, afid)) {
509         /* printf("afs_GetParentVCache: Couldn't find parent dir's FID.\n"); */
510         return ENOENT;
511     }
512
513     code = afs_GetVnodeName(avc, afid, aname, deleted);
514     if (code) {
515         /* printf("afs_GetParentVCache: Couldn't find file name\n"); */
516         goto end;
517     }
518
519     ObtainSharedLock(&afs_xvcache, 766);
520     *adp = afs_FindVCache(afid, 0, 1);
521     ReleaseSharedLock(&afs_xvcache);
522     if (!*adp) {
523         /* printf("afs_GetParentVCache: Couldn't find parent dir's vcache\n"); */
524         code = ENOENT;
525         goto end;
526     }
527
528     if ((*adp)->f.ddirty_flags & VDisconCreate) {
529         /* printf("afs_GetParentVCache: deferring until parent exists\n"); */
530         code = EAGAIN;
531         goto end;
532     }
533
534 end:
535     if (code && *adp) {
536         afs_PutVCache(*adp);
537         *adp = NULL;
538     }
539     return code;
540 }
541
542
543 /*!
544  * Handles file renaming on reconnection:
545  * - Get the old name from the old dir's shadow dir.
546  * - Get the new name from the current dir.
547  * - Old dir fid and new dir fid are collected along the way.
548  * */
549 int
550 afs_ProcessOpRename(struct vcache *avc, struct vrequest *areq)
551 {
552     struct VenusFid old_pdir_fid, new_pdir_fid;
553     char *old_name = NULL, *new_name = NULL;
554     struct AFSFetchStatus OutOldDirStatus, OutNewDirStatus;
555     struct AFSVolSync tsync;
556     struct afs_conn *tc;
557     struct rx_connection *rxconn;
558     afs_uint32 code = 0;
559     XSTATS_DECLS;
560
561     /* Get old dir vcache. */
562     old_pdir_fid.Cell = avc->f.fid.Cell;
563     old_pdir_fid.Fid.Volume = avc->f.fid.Fid.Volume;
564     old_pdir_fid.Fid.Vnode = avc->f.oldParent.vnode;
565     old_pdir_fid.Fid.Unique = avc->f.oldParent.unique;
566
567     /* Get old name. */
568     old_name = afs_osi_Alloc(AFSNAMEMAX);
569     if (!old_name) {
570         /* printf("afs_ProcessOpRename: Couldn't alloc space for old name.\n"); */
571         return ENOMEM;
572     }
573     code = afs_GetVnodeName(avc, &old_pdir_fid, old_name, 1);
574     if (code) {
575         /* printf("afs_ProcessOpRename: Couldn't find old name.\n"); */
576         goto done;
577     }
578
579     /* Alloc data first. */
580     new_name = afs_osi_Alloc(AFSNAMEMAX);
581     if (!new_name) {
582         /* printf("afs_ProcessOpRename: Couldn't alloc space for new name.\n"); */
583         code = ENOMEM;
584         goto done;
585     }
586
587     if (avc->f.ddirty_flags & VDisconRenameSameDir) {
588         /* If we're in the same dir, don't do the lookups all over again,
589          * just copy fid and vcache from the old dir.
590          */
591         memcpy(&new_pdir_fid, &old_pdir_fid, sizeof(struct VenusFid));
592     } else {
593         /* Get parent dir's FID.*/
594         if (afs_GetParentDirFid(avc, &new_pdir_fid)) {
595             /* printf("afs_ProcessOpRename: Couldn't find new parent dir FID.\n"); */
596             code = ENOENT;
597             goto done;
598         }
599     }
600
601     /* And finally get the new name. */
602     code = afs_GetVnodeName(avc, &new_pdir_fid, new_name, 0);
603     if (code) {
604         /* printf("afs_ProcessOpRename: Couldn't find new name.\n"); */
605         goto done;
606     }
607
608     /* Send to data to server. */
609     do {
610         tc = afs_Conn(&old_pdir_fid, areq, SHARED_LOCK, &rxconn);
611         if (tc) {
612             XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_RENAME);
613             RX_AFS_GUNLOCK();
614             code = RXAFS_Rename(rxconn,
615                 (struct AFSFid *)&old_pdir_fid.Fid,
616                 old_name,
617                 (struct AFSFid *)&new_pdir_fid.Fid,
618                 new_name,
619                 &OutOldDirStatus,
620                 &OutNewDirStatus,
621                 &tsync);
622             RX_AFS_GLOCK();
623             XSTATS_END_TIME;
624         } else
625             code = -1;
626
627     } while (afs_Analyze(tc,
628                 rxconn,
629                 code,
630                 &new_pdir_fid,
631                 areq,
632                 AFS_STATS_FS_RPCIDX_RENAME,
633                 SHARED_LOCK,
634                 NULL));
635
636     /* if (code) printf("afs_ProcessOpRename: server code=%u\n", code); */
637 done:
638     if (new_name)
639         afs_osi_Free(new_name, AFSNAMEMAX);
640     if (old_name)
641         afs_osi_Free(old_name, AFSNAMEMAX);
642     return code;
643 }
644
645 /*!
646  * Handles all the reconnection details:
647  * - Get all the details about the vnode: name, fid, and parent dir fid.
648  * - Send data to server.
649  * - Handle errors.
650  * - Reorder vhash and dcaches in their hashes, using the newly acquired fid.
651  */
652 int
653 afs_ProcessOpCreate(struct vcache *avc, struct vrequest *areq,
654                     afs_ucred_t *acred)
655 {
656     char *tname = NULL, *ttargetName = NULL;
657     struct AFSStoreStatus InStatus;
658     struct AFSFetchStatus OutFidStatus, OutDirStatus;
659     struct VenusFid pdir_fid, newFid;
660     struct AFSCallBack CallBack;
661     struct AFSVolSync tsync;
662     struct vcache *tdp = NULL, *tvc = NULL;
663     struct dcache *tdc = NULL;
664     struct afs_conn *tc;
665     struct rx_connection *rxconn;
666     afs_int32 hash, new_hash, index;
667     afs_size_t tlen;
668     int code, op = 0;
669     XSTATS_DECLS;
670
671     tname = afs_osi_Alloc(AFSNAMEMAX);
672     if (!tname)
673         return ENOMEM;
674
675     code = afs_GetParentVCache(avc, 0, &pdir_fid, tname, &tdp);
676     if (code)
677         goto end;
678
679     /* This data may also be in linkData, but then we have to deal with
680      * the joy of terminating NULLs and . and file modes. So just get
681      * it from the dcache where it won't have been fiddled with.
682      */
683     if (vType(avc) == VLNK) {
684         afs_size_t offset;
685         struct dcache *tdc;
686         struct osi_file *tfile;
687
688         tdc = afs_GetDCache(avc, 0, areq, &offset, &tlen, 0);
689         if (!tdc) {
690             code = ENOENT;
691             goto end;
692         }
693
694         if (tlen > 1024) {
695             afs_PutDCache(tdc);
696             code = EFAULT;
697             goto end;
698         }
699
700         tlen++; /* space for NULL */
701         ttargetName = afs_osi_Alloc(tlen);
702         if (!ttargetName) {
703             afs_PutDCache(tdc);
704             return ENOMEM;
705         }
706         ObtainReadLock(&tdc->lock);
707         tfile = afs_CFileOpen(&tdc->f.inode);
708         code = afs_CFileRead(tfile, 0, ttargetName, tlen);
709         ttargetName[tlen-1] = '\0';
710         afs_CFileClose(tfile);
711         ReleaseReadLock(&tdc->lock);
712         afs_PutDCache(tdc);
713     }
714
715     /* Set status. */
716     InStatus.Mask = AFS_SETMODTIME | AFS_SETMODE | AFS_SETGROUP;
717     InStatus.ClientModTime = avc->f.m.Date;
718     InStatus.Owner = avc->f.m.Owner;
719     InStatus.Group = (afs_int32) afs_cr_gid(acred);
720     /* Only care about protection bits. */
721     InStatus.UnixModeBits = avc->f.m.Mode & 0xffff;
722
723     do {
724         tc = afs_Conn(&tdp->f.fid, areq, SHARED_LOCK, &rxconn);
725         if (tc) {
726             switch (vType(avc)) {
727             case VREG:
728                 /* Make file on server. */
729                 op = AFS_STATS_FS_RPCIDX_CREATEFILE;
730                 XSTATS_START_TIME(op);
731                 RX_AFS_GUNLOCK();
732                 code = RXAFS_CreateFile(rxconn,
733                                         (struct AFSFid *)&tdp->f.fid.Fid,
734                                         tname, &InStatus,
735                                         (struct AFSFid *) &newFid.Fid,
736                                         &OutFidStatus, &OutDirStatus,
737                                         &CallBack, &tsync);
738                 RX_AFS_GLOCK();
739                 XSTATS_END_TIME;
740                 break;
741             case VDIR:
742                 /* Make dir on server. */
743                 op = AFS_STATS_FS_RPCIDX_MAKEDIR;
744                 XSTATS_START_TIME(op);
745                 RX_AFS_GUNLOCK();
746                 code = RXAFS_MakeDir(rxconn, (struct AFSFid *) &tdp->f.fid.Fid,
747                                      tname, &InStatus,
748                                      (struct AFSFid *) &newFid.Fid,
749                                      &OutFidStatus, &OutDirStatus,
750                                      &CallBack, &tsync);
751                 RX_AFS_GLOCK();
752                 XSTATS_END_TIME;
753                 break;
754             case VLNK:
755                 /* Make symlink on server. */
756                 op = AFS_STATS_FS_RPCIDX_SYMLINK;
757                 XSTATS_START_TIME(op);
758                 RX_AFS_GUNLOCK();
759                 code = RXAFS_Symlink(rxconn,
760                                 (struct AFSFid *) &tdp->f.fid.Fid,
761                                 tname, ttargetName, &InStatus,
762                                 (struct AFSFid *) &newFid.Fid,
763                                 &OutFidStatus, &OutDirStatus, &tsync);
764                 RX_AFS_GLOCK();
765                 XSTATS_END_TIME;
766                 break;
767             default:
768                 op = AFS_STATS_FS_RPCIDX_CREATEFILE;
769                 code = 1;
770                 break;
771             }
772         } else
773             code = -1;
774     } while (afs_Analyze(tc, rxconn, code, &tdp->f.fid, areq, op, SHARED_LOCK, NULL));
775
776     /* TODO: Handle errors. */
777     if (code) {
778         /* printf("afs_ProcessOpCreate: error while creating vnode on server, code=%d .\n", code); */
779         goto end;
780     }
781
782     /* The rpc doesn't set the cell number. */
783     newFid.Cell = avc->f.fid.Cell;
784
785     /*
786      * Change the fid in the dir entry.
787      */
788
789     /* Seek the dir's dcache. */
790     tdc = afs_FindDCacheByFid(&tdp->f.fid);
791     if (tdc) {
792         /* And now change the fid in the parent dir entry. */
793         afs_dir_ChangeFid(tdc, tname, &avc->f.fid.Fid.Vnode, &newFid.Fid.Vnode);
794         afs_PutDCache(tdc);
795     }
796
797     if (vType(avc) == VDIR) {
798         /* Change fid in the dir for the "." entry. ".." has alredy been
799          * handled by afs_FixChildrenFids when processing the parent dir.
800          */
801         tdc = afs_FindDCacheByFid(&avc->f.fid);
802         if (tdc) {
803             afs_dir_ChangeFid(tdc, ".", &avc->f.fid.Fid.Vnode,
804                               &newFid.Fid.Vnode);
805
806             if (avc->f.m.LinkCount >= 2)
807                 /* For non empty dirs, fix children's parentVnode and
808                  * parentUnique reference.
809                  */
810                 afs_FixChildrenFids(&avc->f.fid, &newFid);
811
812             afs_PutDCache(tdc);
813         }
814     }
815
816     /* Recompute hash chain positions for vnode and dcaches.
817      * Then change to the new FID.
818      */
819
820     /* The vcache goes first. */
821     ObtainWriteLock(&afs_xvcache, 735);
822
823     /* Old fid hash. */
824     hash = VCHash(&avc->f.fid);
825     /* New fid hash. */
826     new_hash = VCHash(&newFid);
827
828     /* Remove hash from old position. */
829     /* XXX: not checking array element contents. It shouldn't be empty.
830      * If it oopses, then something else might be wrong.
831      */
832     if (afs_vhashT[hash] == avc) {
833         /* First in hash chain (might be the only one). */
834         afs_vhashT[hash] = avc->hnext;
835     } else {
836         /* More elements in hash chain. */
837         for (tvc = afs_vhashT[hash]; tvc; tvc = tvc->hnext) {
838             if (tvc->hnext == avc) {
839                 tvc->hnext = avc->hnext;
840                 break;
841             }
842         }
843     }                           /* if (!afs_vhashT[i]->hnext) */
844     QRemove(&avc->vhashq);
845
846     /* Insert hash in new position. */
847     avc->hnext = afs_vhashT[new_hash];
848     afs_vhashT[new_hash] = avc;
849     QAdd(&afs_vhashTV[VCHashV(&newFid)], &avc->vhashq);
850
851     ReleaseWriteLock(&afs_xvcache);
852
853     /* Do the same thing for all dcaches. */
854     hash = DVHash(&avc->f.fid);
855     ObtainWriteLock(&afs_xdcache, 743);
856     for (index = afs_dvhashTbl[hash]; index != NULLIDX; index = hash) {
857         hash = afs_dvnextTbl[index];
858         tdc = afs_GetValidDSlot(index);
859         ReleaseReadLock(&tdc->tlock);
860         if (afs_indexUnique[index] == avc->f.fid.Fid.Unique) {
861             if (!FidCmp(&tdc->f.fid, &avc->f.fid)) {
862
863                 /* Safer but slower. */
864                 afs_HashOutDCache(tdc, 0);
865
866                 /* Put dcache in new positions in the dchash and dvhash. */
867                 new_hash = DCHash(&newFid, tdc->f.chunk);
868                 afs_dcnextTbl[tdc->index] = afs_dchashTbl[new_hash];
869                 afs_dchashTbl[new_hash] = tdc->index;
870
871                 new_hash = DVHash(&newFid);
872                 afs_dvnextTbl[tdc->index] = afs_dvhashTbl[new_hash];
873                 afs_dvhashTbl[new_hash] = tdc->index;
874
875                 afs_indexUnique[tdc->index] = newFid.Fid.Unique;
876                 memcpy(&tdc->f.fid, &newFid, sizeof(struct VenusFid));
877            }                   /* if fid match */
878         }                       /* if uniquifier match */
879         if (tdc)
880             afs_PutDCache(tdc);
881     }                           /* for all dcaches in this hash bucket */
882     ReleaseWriteLock(&afs_xdcache);
883
884     /* Now we can set the new fid. */
885     memcpy(&avc->f.fid, &newFid, sizeof(struct VenusFid));
886
887 end:
888     if (tdp)
889         afs_PutVCache(tdp);
890     afs_osi_Free(tname, AFSNAMEMAX);
891     if (ttargetName)
892         afs_osi_Free(ttargetName, tlen);
893     return code;
894 }
895
896 /*!
897  * Remove a vnode on the server, be it file or directory.
898  * Not much to do here only get the parent dir's fid and call the
899  * removal rpc.
900  *
901  * \param avc The deleted vcache
902  * \param areq
903  *
904  * \note The vcache refcount should be dropped because it points to
905  * a deleted vnode and has served it's purpouse, but we drop refcount
906  * on shadow dir deletio (we still need it for that).
907  *
908  * \note avc must be write locked.
909  */
910 int
911 afs_ProcessOpRemove(struct vcache *avc, struct vrequest *areq)
912 {
913     char *tname = NULL;
914     struct AFSFetchStatus OutDirStatus;
915     struct VenusFid pdir_fid;
916     struct AFSVolSync tsync;
917     struct afs_conn *tc;
918     struct rx_connection *rxconn;
919     struct vcache *tdp = NULL;
920     int code = 0;
921     XSTATS_DECLS;
922
923     tname = afs_osi_Alloc(AFSNAMEMAX);
924     if (!tname) {
925         /* printf("afs_ProcessOpRemove: Couldn't alloc space for file name\n"); */
926         return ENOMEM;
927     }
928
929     code = afs_GetParentVCache(avc, 1, &pdir_fid, tname, &tdp);
930     if (code)
931         goto end;
932
933     if ((vType(avc) == VDIR) && (afs_CheckDeletedChildren(avc))) {
934         /* Deleted children of this dir remain unsynchronized.
935          * Defer this vcache.
936          */
937         code = EAGAIN;
938         goto end;
939     }
940
941     if (vType(avc) == VREG || vType(avc) == VLNK) {
942         /* Remove file on server. */
943         do {
944             tc = afs_Conn(&pdir_fid, areq, SHARED_LOCK, &rxconn);
945             if (tc) {
946                 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_REMOVEFILE);
947                 RX_AFS_GUNLOCK();
948                 code = RXAFS_RemoveFile(rxconn,
949                                 &pdir_fid.Fid,
950                                 tname,
951                                 &OutDirStatus,
952                                 &tsync);
953
954                 RX_AFS_GLOCK();
955                 XSTATS_END_TIME;
956             } else
957                 code = -1;
958         } while (afs_Analyze(tc,
959                         rxconn,
960                         code,
961                         &pdir_fid,
962                         areq,
963                         AFS_STATS_FS_RPCIDX_REMOVEFILE,
964                         SHARED_LOCK,
965                         NULL));
966
967     } else if (vType(avc) == VDIR) {
968         /* Remove dir on server. */
969         do {
970             tc = afs_Conn(&pdir_fid, areq, SHARED_LOCK, &rxconn);
971             if (tc) {
972                 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_REMOVEDIR);
973                 RX_AFS_GUNLOCK();
974                 code = RXAFS_RemoveDir(rxconn,
975                                 &pdir_fid.Fid,
976                                 tname,
977                                 &OutDirStatus,
978                                 &tsync);
979                 RX_AFS_GLOCK();
980                 XSTATS_END_TIME;
981            } else
982                 code = -1;
983         } while (afs_Analyze(tc,
984                         rxconn,
985                         code,
986                         &pdir_fid,
987                         areq,
988                         AFS_STATS_FS_RPCIDX_REMOVEDIR,
989                         SHARED_LOCK,
990                         NULL));
991
992     }                           /* if (vType(avc) == VREG) */
993
994     /* if (code) printf("afs_ProcessOpRemove: server returned code=%u\n", code); */
995
996 end:
997     afs_osi_Free(tname, AFSNAMEMAX);
998     return code;
999 }
1000
1001 /*!
1002  * Send disconnected file changes to the server.
1003  *
1004  * \note Call with vnode locked both locally and on the server.
1005  *
1006  * \param avc Vnode that gets synchronized to the server.
1007  * \param areq Used for obtaining a conn struct.
1008  *
1009  * \return 0 for success. On failure, other error codes.
1010  */
1011 int
1012 afs_SendChanges(struct vcache *avc, struct vrequest *areq)
1013 {
1014     struct afs_conn *tc;
1015     struct rx_connection *rxconn;
1016     struct AFSStoreStatus sstat;
1017     struct AFSFetchStatus fstat;
1018     struct AFSVolSync tsync;
1019     int code = 0;
1020     int flags = 0;
1021     XSTATS_DECLS;
1022
1023     /* Start multiplexing dirty operations from ddirty_flags field: */
1024     if (avc->f.ddirty_flags & VDisconSetAttrMask) {
1025         /* Setattr OPS: */
1026         /* Turn dirty vc data into a new store status... */
1027         if (afs_GenStoreStatus(avc, &sstat) > 0) {
1028             do {
1029                 tc = afs_Conn(&avc->f.fid, areq, SHARED_LOCK, &rxconn);
1030                 if (tc) {
1031                     /* ... and send it. */
1032                     XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_STORESTATUS);
1033                     RX_AFS_GUNLOCK();
1034                     code = RXAFS_StoreStatus(rxconn,
1035                                 (struct AFSFid *) &avc->f.fid.Fid,
1036                                 &sstat,
1037                                 &fstat,
1038                                 &tsync);
1039
1040                     RX_AFS_GLOCK();
1041                     XSTATS_END_TIME;
1042                 } else
1043                     code = -1;
1044
1045         } while (afs_Analyze(tc,
1046                         rxconn,
1047                         code,
1048                         &avc->f.fid,
1049                         areq,
1050                         AFS_STATS_FS_RPCIDX_STORESTATUS,
1051                         SHARED_LOCK,
1052                         NULL));
1053
1054         }               /* if (afs_GenStoreStatus() > 0)*/
1055     }                   /* disconnected SETATTR */
1056
1057     if (code)
1058         return code;
1059
1060     if (avc->f.ddirty_flags &
1061         (VDisconTrunc
1062         | VDisconWriteClose
1063         | VDisconWriteFlush
1064         | VDisconWriteOsiFlush)) {
1065
1066         /* Truncate OP: */
1067         do {
1068             tc = afs_Conn(&avc->f.fid, areq, SHARED_LOCK, &rxconn);
1069             if (tc) {
1070                 /* Set storing flags. XXX: A tad inefficient ... */
1071                 if (avc->f.ddirty_flags & VDisconWriteClose)
1072                     flags |= AFS_LASTSTORE;
1073                 if (avc->f.ddirty_flags & VDisconWriteOsiFlush)
1074                     flags |= (AFS_SYNC | AFS_LASTSTORE);
1075                 if (avc->f.ddirty_flags & VDisconWriteFlush)
1076                     flags |= AFS_SYNC;
1077
1078                 /* Try to send store to server. */
1079                 /* XXX: AFS_LASTSTORE for writes? Or just AFS_SYNC for all? */
1080                 code = afs_StoreAllSegments(avc, areq, flags);
1081             } else
1082                 code = -1;
1083
1084         } while (afs_Analyze(tc,
1085                         rxconn,
1086                         code,
1087                         &avc->f.fid,
1088                         areq,
1089                         AFS_STATS_FS_RPCIDX_STOREDATA,
1090                         SHARED_LOCK,
1091                         NULL));
1092
1093     }                   /* disconnected TRUNC | WRITE */
1094
1095     return code;
1096 }
1097
1098 /*!
1099  * All files that have been dirty before disconnection are going to
1100  * be replayed back to the server.
1101  *
1102  * \param areq Request from the user.
1103  * \param acred User credentials.
1104  *
1105  * \return If all files synchronized succesfully, return 0, otherwise
1106  * return error code
1107  *
1108  * \note For now, it's the request from the PDiscon pioctl.
1109  *
1110  */
1111 int
1112 afs_ResyncDisconFiles(struct vrequest *areq, afs_ucred_t *acred)
1113 {
1114     struct afs_conn *tc;
1115     struct rx_connection *rxconn;
1116     struct vcache *tvc;
1117     struct AFSFetchStatus fstat;
1118     struct AFSCallBack callback;
1119     struct AFSVolSync tsync;
1120     int code = 0;
1121     afs_int32 start = 0;
1122     XSTATS_DECLS;
1123     /*AFS_STATCNT(afs_ResyncDisconFiles);*/
1124
1125     ObtainWriteLock(&afs_disconDirtyLock, 707);
1126
1127     while (!QEmpty(&afs_disconDirty)) {
1128         tvc = QEntry(QPrev(&afs_disconDirty), struct vcache, dirtyq);
1129
1130         /* Can't lock tvc whilst holding the discon dirty lock */
1131         ReleaseWriteLock(&afs_disconDirtyLock);
1132
1133         /* Get local write lock. */
1134         ObtainWriteLock(&tvc->lock, 705);
1135
1136         if (tvc->f.ddirty_flags & VDisconRemove) {
1137             /* Delete the file on the server and just move on
1138              * to the next file. After all, it has been deleted
1139              * we can't replay any other operation it.
1140              */
1141             code = afs_ProcessOpRemove(tvc, areq);
1142             goto next_file;
1143
1144         } else if (tvc->f.ddirty_flags & VDisconCreate) {
1145             /* For newly created files, we don't need a server lock. */
1146             code = afs_ProcessOpCreate(tvc, areq, acred);
1147             if (code)
1148                 goto next_file;
1149
1150             tvc->f.ddirty_flags &= ~VDisconCreate;
1151             tvc->f.ddirty_flags |= VDisconCreated;
1152         }
1153 #if 0
1154         /* Get server write lock. */
1155         do {
1156             tc = afs_Conn(&tvc->f.fid, areq, SHARED_LOCK, &rxconn);
1157             if (tc) {
1158                 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_SETLOCK);
1159                 RX_AFS_GUNLOCK();
1160                 code = RXAFS_SetLock(rxconn,
1161                                         (struct AFSFid *)&tvc->f.fid.Fid,
1162                                         LockWrite,
1163                                         &tsync);
1164                 RX_AFS_GLOCK();
1165                 XSTATS_END_TIME;
1166            } else
1167                 code = -1;
1168
1169         } while (afs_Analyze(tc,
1170                         rxconn,
1171                         code,
1172                         &tvc->f.fid,
1173                         areq,
1174                         AFS_STATS_FS_RPCIDX_SETLOCK,
1175                         SHARED_LOCK,
1176                         NULL));
1177
1178         if (code)
1179             goto next_file;
1180 #endif
1181         if (tvc->f.ddirty_flags & VDisconRename) {
1182             /* If we're renaming the file, do so now */
1183             code = afs_ProcessOpRename(tvc, areq);
1184             if (code)
1185                 goto unlock_srv_file;
1186         }
1187
1188         /* Issue a FetchStatus to get info about DV and callbacks. */
1189         do {
1190             tc = afs_Conn(&tvc->f.fid, areq, SHARED_LOCK, &rxconn);
1191             if (tc) {
1192                 tvc->callback = tc->parent->srvr->server;
1193                 start = osi_Time();
1194                 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_FETCHSTATUS);
1195                 RX_AFS_GUNLOCK();
1196                 code = RXAFS_FetchStatus(rxconn,
1197                                 (struct AFSFid *)&tvc->f.fid.Fid,
1198                                 &fstat,
1199                                 &callback,
1200                                 &tsync);
1201                 RX_AFS_GLOCK();
1202                 XSTATS_END_TIME;
1203             } else
1204                 code = -1;
1205
1206         } while (afs_Analyze(tc,
1207                         rxconn,
1208                         code,
1209                         &tvc->f.fid,
1210                         areq,
1211                         AFS_STATS_FS_RPCIDX_FETCHSTATUS,
1212                         SHARED_LOCK,
1213                         NULL));
1214
1215         if (code) {
1216             goto unlock_srv_file;
1217         }
1218
1219         if ((dv_match(tvc, fstat) && (tvc->f.m.Date == fstat.ServerModTime)) ||
1220                 (afs_ConflictPolicy == CLIENT_WINS) ||
1221                 (tvc->f.ddirty_flags & VDisconCreated)) {
1222             /*
1223              * Send changes to the server if there's data version match, or
1224              * client wins policy has been selected or file has been created
1225              * but doesn't have it's the contents on to the server yet.
1226              */
1227            /*
1228             * XXX: Checking server attr changes by timestamp might not the
1229             * most elegant solution, but it's the most viable one that we could find.
1230             */
1231             afs_UpdateStatus(tvc, &tvc->f.fid, areq, &fstat, &callback, start);
1232             code = afs_SendChanges(tvc, areq);
1233
1234         } else if (afs_ConflictPolicy == SERVER_WINS) {
1235             /* DV mismatch, apply collision resolution policy. */
1236             /* Discard this files chunks and remove from current dir. */
1237             afs_ResetVCache(tvc, acred, 0);
1238             tvc->f.truncPos = AFS_NOTRUNC;
1239         } else {
1240             /* printf("afs_ResyncDisconFiles: no resolution policy selected.\n"); */
1241         }               /* if DV match or client wins policy */
1242
1243 unlock_srv_file:
1244         /* Release server write lock. */
1245 #if 0
1246         do {
1247             tc = afs_Conn(&tvc->f.fid, areq, SHARED_LOCK, &rxconn);
1248             if (tc) {
1249                 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_RELEASELOCK);
1250                 RX_AFS_GUNLOCK();
1251                 ucode = RXAFS_ReleaseLock(rxconn,
1252                                 (struct AFSFid *) &tvc->f.fid.Fid,
1253                                 &tsync);
1254                 RX_AFS_GLOCK();
1255                 XSTATS_END_TIME;
1256             } else
1257                 ucode = -1;
1258         } while (afs_Analyze(tc,
1259                         rxconn,
1260                         ucode,
1261                         &tvc->f.fid,
1262                         areq,
1263                         AFS_STATS_FS_RPCIDX_RELEASELOCK,
1264                         SHARED_LOCK,
1265                         NULL));
1266 #endif
1267 next_file:
1268         ObtainWriteLock(&afs_disconDirtyLock, 710);
1269         if (code == 0) {
1270             /* Replayed successfully - pull the vcache from the
1271              * disconnected list */
1272             tvc->f.ddirty_flags = 0;
1273             QRemove(&tvc->dirtyq);
1274             afs_PutVCache(tvc);
1275         } else {
1276             if (code == EAGAIN) {
1277                 /* Operation was deferred. Pull it from the current place in
1278                  * the list, and stick it at the end again */
1279                 QRemove(&tvc->dirtyq);
1280                 QAdd(&afs_disconDirty, &tvc->dirtyq);
1281             } else {
1282                 /* Failed - keep state as is, and let the user know we died */
1283
1284                 ReleaseWriteLock(&tvc->lock);
1285                 break;
1286             }
1287         }
1288
1289         /* Release local write lock. */
1290         ReleaseWriteLock(&tvc->lock);
1291     }                   /* while (tvc) */
1292
1293     if (code) {
1294         ReleaseWriteLock(&afs_disconDirtyLock);
1295         return code;
1296     }
1297
1298     /* Dispose of all of the shadow directories */
1299     afs_DisconDiscardAllShadows(0, acred);
1300
1301     ReleaseWriteLock(&afs_disconDirtyLock);
1302     return code;
1303 }
1304
1305 /*!
1306  * Discard all of our shadow directory copies. If squash is true, then
1307  * we also invalidate the vcache holding the shadow directory, to ensure
1308  * that any disconnected changes are deleted
1309  *
1310  * \param squash
1311  * \param acred
1312  *
1313  * \note afs_disconDirtyLock must be held on entry. It will be released
1314  * and reobtained
1315  */
1316
1317 static void
1318 afs_DisconDiscardAllShadows(int squash, afs_ucred_t *acred)
1319 {
1320    struct vcache *tvc;
1321
1322    while (!QEmpty(&afs_disconShadow)) {
1323         tvc = QEntry(QNext(&afs_disconShadow), struct vcache, shadowq);
1324
1325         /* Must release the dirty lock to be able to get a vcache lock */
1326         ReleaseWriteLock(&afs_disconDirtyLock);
1327         ObtainWriteLock(&tvc->lock, 706);
1328
1329         if (squash)
1330            afs_ResetVCache(tvc, acred, 0);
1331
1332         afs_DeleteShadowDir(tvc);
1333
1334         ReleaseWriteLock(&tvc->lock);
1335         ObtainWriteLock(&afs_disconDirtyLock, 709);
1336     }                           /* while (tvc) */
1337 }
1338
1339 /*!
1340  * This function throws away the whole disconnected state, allowing
1341  * the cache manager to reconnect to a server if we get into a state
1342  * where reconiliation is impossible.
1343  *
1344  * \param acred
1345  *
1346  */
1347 void
1348 afs_DisconDiscardAll(afs_ucred_t *acred)
1349 {
1350     struct vcache *tvc;
1351
1352     ObtainWriteLock(&afs_disconDirtyLock, 717);
1353     while (!QEmpty(&afs_disconDirty)) {
1354         tvc = QEntry(QPrev(&afs_disconDirty), struct vcache, dirtyq);
1355         QRemove(&tvc->dirtyq);
1356         ReleaseWriteLock(&afs_disconDirtyLock);
1357
1358         ObtainWriteLock(&tvc->lock, 718);
1359         afs_ResetVCache(tvc, acred, 0);
1360         tvc->f.truncPos = AFS_NOTRUNC;
1361         ReleaseWriteLock(&tvc->lock);
1362         ObtainWriteLock(&afs_disconDirtyLock, 719);
1363         afs_PutVCache(tvc);
1364     }
1365
1366     afs_DisconDiscardAllShadows(1, acred);
1367
1368     ReleaseWriteLock(&afs_disconDirtyLock);
1369 }
1370
1371 /*!
1372  * Print list of disconnected files.
1373  *
1374  * \note Call with afs_DDirtyVCListLock read locked.
1375  */
1376 void
1377 afs_DbgDisconFiles(void)
1378 {
1379     struct vcache *tvc;
1380     struct afs_q *q;
1381     int i = 0;
1382
1383     afs_warn("List of dirty files: \n");
1384
1385     ObtainReadLock(&afs_disconDirtyLock);
1386     for (q = QPrev(&afs_disconDirty); q != &afs_disconDirty; q = QPrev(q)) {
1387         tvc = QEntry(q, struct vcache, dirtyq);
1388
1389         afs_warn("Cell=%u Volume=%u VNode=%u Unique=%u\n",
1390                 tvc->f.fid.Cell,
1391                 tvc->f.fid.Fid.Volume,
1392                 tvc->f.fid.Fid.Vnode,
1393                 tvc->f.fid.Fid.Unique);
1394
1395         i++;
1396         if (i >= 30)
1397             osi_Panic("afs_DbgDisconFiles: loop in dirty list\n");
1398     }
1399     ReleaseReadLock(&afs_disconDirtyLock);
1400 }
1401
1402 /*!
1403  * Generate a fake fid for a disconnected shadow dir.
1404  * Similar to afs_GenFakeFid, only that it uses the dhash
1405  * to search for a uniquifier because a shadow dir lives only
1406  * in the dcache.
1407  *
1408  * \param afid
1409  *
1410  * \note Don't forget to fill in afid with Cell and Volume.
1411  */
1412 void
1413 afs_GenShadowFid(struct VenusFid *afid)
1414 {
1415     afs_uint32 i, index, max_unique = 1;
1416     struct vcache *tvc = NULL;
1417
1418     /* Try generating a fid that isn't used in the vhash. */
1419     do {
1420         /* Shadow Fids are always directories */
1421         afid->Fid.Vnode = afs_DisconVnode + 1;
1422
1423         i = DVHash(afid);
1424         ObtainWriteLock(&afs_xdcache, 737);
1425         for (index = afs_dvhashTbl[i]; index != NULLIDX; index = i) {
1426             i = afs_dvnextTbl[index];
1427             if (afs_indexUnique[index] > max_unique)
1428                 max_unique = afs_indexUnique[index];
1429         }
1430
1431         ReleaseWriteLock(&afs_xdcache);
1432         afid->Fid.Unique = max_unique + 1;
1433         afs_DisconVnode += 2;
1434         if (!afs_DisconVnode)
1435             afs_DisconVnode = 2;
1436
1437         /* Is this a used vnode? */
1438         ObtainSharedLock(&afs_xvcache, 762);
1439         tvc = afs_FindVCache(afid, 0, 1);
1440         ReleaseSharedLock(&afs_xvcache);
1441         if (tvc)
1442             afs_PutVCache(tvc);
1443     } while (tvc);
1444 }
1445
1446 /*!
1447  * Generate a fake fid (vnode and uniquifier) for a vcache
1448  * (either dir or normal file). The vnode is generated via
1449  * afs_DisconVNode and the uniquifier by getting the highest
1450  * uniquifier on a hash chain and incrementing it by one.
1451  *
1452  * \param afid   The fid structre that will be filled.
1453  * \param avtype Vnode type: VDIR/VREG.
1454  * \param lock   True indicates that xvcache may be obtained,
1455  *               False that it is already held
1456  *
1457  * \note The cell number must be completed somewhere else.
1458  */
1459 void
1460 afs_GenFakeFid(struct VenusFid *afid, afs_uint32 avtype, int lock)
1461 {
1462     struct vcache *tvc;
1463     afs_uint32 max_unique = 0, i;
1464
1465     switch (avtype) {
1466     case VDIR:
1467         afid->Fid.Vnode = afs_DisconVnode + 1;
1468         break;
1469     case VREG:
1470     case VLNK:
1471         afid->Fid.Vnode = afs_DisconVnode;
1472         break;
1473     }
1474
1475     if (lock)
1476         ObtainWriteLock(&afs_xvcache, 736);
1477     i = VCHash(afid);
1478     for (tvc = afs_vhashT[i]; tvc; tvc = tvc->hnext) {
1479         if (tvc->f.fid.Fid.Unique > max_unique)
1480             max_unique = tvc->f.fid.Fid.Unique;
1481     }
1482     if (lock)
1483         ReleaseWriteLock(&afs_xvcache);
1484
1485     afid->Fid.Unique = max_unique + 1;
1486     afs_DisconVnode += 2;
1487     if (!afs_DisconVnode)
1488         afs_DisconVnode = 2;
1489 }
1490
1491 /*!
1492  * Fill in stats for a newly created file/directory.
1493  *
1494  * \param adp The parent dir's vcache.
1495  * \param avc The created vnode.
1496  * \param afid The new fid.
1497  * \param attrs
1498  * \param areq
1499  * \param file_type Specify if file or directory.
1500  *
1501  * \note Call with avc write locked.
1502  */
1503 void
1504 afs_GenDisconStatus(struct vcache *adp, struct vcache *avc,
1505                     struct VenusFid *afid, struct vattr *attrs,
1506                     struct vrequest *areq, int file_type)
1507 {
1508     memcpy(&avc->f.fid, afid, sizeof(struct VenusFid));
1509     avc->f.m.Mode = attrs->va_mode;
1510     /* Used to do this:
1511      * avc->f.m.Owner = attrs->va_uid;
1512      * But now we use the parent dir's ownership,
1513      * there's no other way to get a server owner id.
1514      * XXX: Does it really matter?
1515      */
1516     avc->f.m.Group = adp->f.m.Group;
1517     avc->f.m.Owner = adp->f.m.Owner;
1518     hset64(avc->f.m.DataVersion, 0, 0);
1519     avc->f.m.Length = attrs->va_size;
1520     avc->f.m.Date = osi_Time();
1521     switch(file_type) {
1522       case VREG:
1523         vSetType(avc, VREG);
1524         avc->f.m.Mode |= S_IFREG;
1525         avc->f.m.LinkCount = 1;
1526         avc->f.parent.vnode = adp->f.fid.Fid.Vnode;
1527         avc->f.parent.unique = adp->f.fid.Fid.Unique;
1528         break;
1529       case VDIR:
1530         vSetType(avc, VDIR);
1531         avc->f.m.Mode |= S_IFDIR;
1532         avc->f.m.LinkCount = 2;
1533         break;
1534       case VLNK:
1535         vSetType(avc, VLNK);
1536         avc->f.m.Mode |= S_IFLNK;
1537         if ((avc->f.m.Mode & 0111) == 0)
1538             avc->mvstat = 1;
1539         avc->f.parent.vnode = adp->f.fid.Fid.Vnode;
1540         avc->f.parent.unique = adp->f.fid.Fid.Unique;
1541         break;
1542       default:
1543         break;
1544     }
1545     avc->f.anyAccess = adp->f.anyAccess;
1546     afs_AddAxs(avc->Access, areq->uid, adp->Access->axess);
1547
1548     avc->callback = NULL;
1549     avc->f.states |= CStatd;
1550     avc->f.states &= ~CBulkFetching;
1551 }