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