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
11 #include "afs/sysincludes.h"
12 #include "afsincludes.h"
13 #include "afs/afs_stats.h" /* statistics */
15 #include "afs/afs_cbqueue.h"
19 #define dv_match(vc, fstat) \
20 ((vc->f.m.DataVersion.low == fstat.DataVersion) && \
21 (vc->f.m.DataVersion.high == fstat.dataVersionHigh))
23 /*! Circular queue of dirty vcaches */
24 struct afs_q afs_disconDirty;
26 /*! Circular queue of vcaches with shadow directories */
27 struct afs_q afs_disconShadow;
29 /*! Locks both of these lists. Must be write locked for anything other than
31 afs_rwlock_t afs_disconDirtyLock;
33 extern afs_int32 *afs_dvhashTbl; /*Data cache hash table */
34 extern afs_int32 *afs_dchashTbl; /*Data cache hash table */
35 extern afs_int32 *afs_dvnextTbl; /*Dcache hash table links */
36 extern afs_int32 *afs_dcnextTbl; /*Dcache hash table links */
37 extern struct dcache **afs_indexTable; /*Pointers to dcache entries */
39 /*! Vnode number. On file creation, use the current value and increment it.
41 afs_uint32 afs_DisconVnode = 2;
43 /*! Conflict policy. */
51 afs_int32 afs_ConflictPolicy = SERVER_WINS;
53 static void afs_DisconDiscardAllShadows(int, afs_ucred_t *);
54 void afs_DbgListDirEntries(struct VenusFid *afid);
58 * Find the first dcache of a file that has the specified fid.
59 * Similar to afs_FindDCache, only that it takes a fid instead
60 * of a vcache and it can get the first dcache.
64 * \return The found dcache or NULL.
67 afs_FindDCacheByFid(struct VenusFid *afid)
70 struct dcache *tdc = NULL;
73 ObtainWriteLock(&afs_xdcache, 758);
74 for (index = afs_dvhashTbl[i]; index != NULLIDX;) {
75 if (afs_indexUnique[index] == afid->Fid.Unique) {
76 tdc = afs_GetDSlot(index, NULL);
77 ReleaseReadLock(&tdc->tlock);
78 if (!FidCmp(&tdc->f.fid, afid)) {
79 break; /* leaving refCount high for caller */
83 index = afs_dvnextTbl[index];
85 ReleaseWriteLock(&afs_xdcache);
93 * Generate a store status from a dirty vcache entry.
95 * \param avc Dirty vcache entry.
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.
101 * \return Mask of operations.
104 afs_GenStoreStatus(struct vcache *avc, struct AFSStoreStatus *astat)
106 if (!avc || !astat || !avc->f.ddirty_flags)
109 /* Clean up store stat. */
110 memset(astat, 0, sizeof(struct AFSStoreStatus));
112 if (avc->f.ddirty_flags & VDisconSetTime) {
113 /* Update timestamp. */
114 astat->ClientModTime = avc->f.m.Date;
115 astat->Mask |= AFS_SETMODTIME;
118 if (avc->f.ddirty_flags & VDisconSetMode) {
119 /* Copy the mode bits. */
120 astat->UnixModeBits = avc->f.m.Mode;
121 astat->Mask |= AFS_SETMODE;
124 /* XXX: more to come... ?*/
130 * Hook for filtering the local dir fid by searching the "." entry.
132 * \param hdata The fid to be filled.
135 get_parent_dir_fid_hook(void *hdata, char *aname, afs_int32 vnode,
138 struct VenusFid *tfid = (struct VenusFid *) hdata;
140 if ((aname[0] == '.') && (aname[1] == '.') && !aname[2]) {
141 tfid->Fid.Vnode = vnode;
142 tfid->Fid.Unique = unique;
150 * Get a the dir's fid by looking in the vcache for simple files and
151 * in the ".." entry for directories.
153 * \param avc The file's vhash entry.
154 * \param afid Put the fid here.
156 * \return 0 on success, -1 on failure
159 afs_GetParentDirFid(struct vcache *avc, struct VenusFid *afid)
163 afid->Cell = avc->f.fid.Cell;
164 afid->Fid.Volume = avc->f.fid.Fid.Volume;
166 switch (vType(avc)) {
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;
174 /* If dir or parent dir created locally*/
175 tdc = afs_FindDCacheByFid(&avc->f.fid);
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);
181 if (afid->Fid.Unique == 0) {
197 struct VenusFid *fid;
203 * Hook that searches a certain fid's name.
205 * \param hdata NameAndFid structure containin a pointer to a fid
206 * and an allocate name. The name will be filled when hit.
209 get_vnode_name_hook(void *hdata, char *aname, afs_int32 vnode,
212 struct NameAndFid *nf = (struct NameAndFid *) hdata;
214 if ((nf->fid->Fid.Vnode == vnode) &&
215 (nf->fid->Fid.Unique == unique)) {
216 nf->name_len = strlen(aname);
217 memcpy(nf->name, aname, nf->name_len);
218 nf->name[nf->name_len] = 0;
227 * Try to get a vnode's name by comparing all parent dir's entries
228 * to the given fid. It can also return the dir's dcache.
230 * \param avc The file's vcache.
231 * \param afid The parent dir's fid.
232 * \param aname A preallocated string for the name.
233 * \param deleted Has this file been deleted? If yes, use the shadow
234 * dir for looking up the name.
237 afs_GetVnodeName(struct vcache *avc, struct VenusFid *afid, char *aname,
242 struct vcache *parent_vc;
243 struct NameAndFid tnf;
244 struct VenusFid parent_fid;
245 struct VenusFid shadow_fid;
247 /* List dir contents and get it's tdc. */
249 /* For deleted files, get the shadow dir's tdc: */
251 /* Get the parent dir's vcache that contains the shadow fid. */
252 parent_fid.Cell = avc->f.fid.Cell;
253 parent_fid.Fid.Volume = avc->f.fid.Fid.Volume;
254 if (avc->f.ddirty_flags & VDisconRename) {
255 /* For renames the old dir fid is needed. */
256 parent_fid.Fid.Vnode = avc->f.oldParent.vnode;
257 parent_fid.Fid.Unique = avc->f.oldParent.unique;
259 parent_fid.Fid.Vnode = afid->Fid.Vnode;
260 parent_fid.Fid.Unique = afid->Fid.Unique;
263 /* Get the parent dir's vcache that contains the shadow fid. */
264 ObtainSharedLock(&afs_xvcache, 755);
265 parent_vc = afs_FindVCache(&parent_fid, 0, 1);
266 ReleaseSharedLock(&afs_xvcache);
271 shadow_fid.Cell = parent_vc->f.fid.Cell;
272 shadow_fid.Fid.Volume = parent_vc->f.fid.Fid.Volume;
273 shadow_fid.Fid.Vnode = parent_vc->f.shadow.vnode;
274 shadow_fid.Fid.Unique = parent_vc->f.shadow.unique;
276 afs_PutVCache(parent_vc);
278 /* Get shadow dir's dcache. */
279 tdc = afs_FindDCacheByFid(&shadow_fid);
283 /* For normal files, look into the current dir's entry. */
284 tdc = afs_FindDCacheByFid(afid);
288 tnf.fid = &avc->f.fid;
291 afs_dir_EnumerateDir(tdc, &get_vnode_name_hook, &tnf);
293 if (tnf.name_len == -1)
296 printf("Directory dcache not found!\n");
303 struct DirtyChildrenCount {
309 * Lookup dirty deleted vnodes in this dir.
312 chk_del_children_hook(void *hdata, char *aname, afs_int32 vnode,
315 struct VenusFid tfid;
316 struct DirtyChildrenCount *v = (struct DirtyChildrenCount *) hdata;
319 if ((aname[0] == '.') && !aname[1])
320 /* Skip processing this dir again.
321 * It would result in an endless loop.
325 if ((aname[0] == '.') && (aname[1] == '.') && !aname[2])
326 /* Don't process parent dir. */
329 /* Get this file's vcache. */
330 tfid.Cell = v->vc->f.fid.Cell;
331 tfid.Fid.Volume = v->vc->f.fid.Fid.Volume;
332 tfid.Fid.Vnode = vnode;
333 tfid.Fid.Unique = unique;
335 ObtainSharedLock(&afs_xvcache, 757);
336 tvc = afs_FindVCache(&tfid, 0, 1);
337 ReleaseSharedLock(&afs_xvcache);
339 /* Count unfinished dirty children. */
341 ObtainReadLock(&tvc->lock);
342 if (tvc->f.ddirty_flags)
344 ReleaseReadLock(&tvc->lock);
353 * Check if entries have been deleted in a vnode's shadow
356 * \return Returns the number of dirty children.
358 * \note afs_DDirtyVCListLock must be write locked.
361 afs_CheckDeletedChildren(struct vcache *avc)
364 struct DirtyChildrenCount dcc;
365 struct VenusFid shadow_fid;
367 if (!avc->f.shadow.vnode)
371 shadow_fid.Cell = avc->f.fid.Cell;
372 shadow_fid.Fid.Volume = avc->f.fid.Fid.Volume;
373 shadow_fid.Fid.Vnode = avc->f.shadow.vnode;
374 shadow_fid.Fid.Unique = avc->f.shadow.unique;
378 /* Get shadow dir's dcache. */
379 tdc = afs_FindDCacheByFid(&shadow_fid);
382 afs_dir_EnumerateDir(tdc, &chk_del_children_hook, &dcc);
390 * Changes a file's parent fid references.
393 fix_children_fids_hook(void *hdata, char *aname, afs_int32 vnode,
396 struct VenusFid tfid;
397 struct VenusFid *afid = (struct VenusFid *) hdata;
399 struct dcache *tdc = NULL;
401 if ((aname[0] == '.') && !aname[1])
404 if ((aname[0] == '.') && (aname[1] == '.') && !aname[2])
407 tfid.Cell = afid->Cell;
408 tfid.Fid.Volume = afid->Fid.Volume;
409 tfid.Fid.Vnode = vnode;
410 tfid.Fid.Unique = unique;
413 /* vnode's parity indicates that it's a file. */
415 /* Get the vcache. */
416 ObtainSharedLock(&afs_xvcache, 759);
417 tvc = afs_FindVCache(&tfid, 0, 1);
418 ReleaseSharedLock(&afs_xvcache);
420 /* Change the fields. */
422 tvc->f.parent.vnode = afid->Fid.Vnode;
423 tvc->f.parent.unique = afid->Fid.Unique;
428 /* It's a dir. Fix this dir's .. entry to contain the new fid. */
429 /* Seek the dir's dcache. */
430 tdc = afs_FindDCacheByFid(&tfid);
432 /* Change the .. entry fid. */
433 afs_dir_ChangeFid(tdc, "..", NULL, &afid->Fid.Vnode);
436 } /* if (!(vnode % 2))*/
442 * Fixes the parentVnode and parentUnique fields of all
443 * files (not dirs) contained in the directory pointed by
444 * old_fid. This is useful on resync, when a locally created dir
445 * get's a new fid and all the children references must be updated
446 * to reflect the new fid.
448 * \note The dir's fid hasn't been changed yet, it is still referenced
451 * \param old_fid The current dir's fid.
452 * \param new_fid The new dir's fid.
455 afs_FixChildrenFids(struct VenusFid *old_fid, struct VenusFid *new_fid)
459 /* Get shadow dir's dcache. */
460 tdc = afs_FindDCacheByFid(old_fid);
461 /* Change the fids. */
463 afs_dir_EnumerateDir(tdc, &fix_children_fids_hook, new_fid);
469 list_dir_hook(void *hdata, char *aname, afs_int32 vnode, afs_int32 unique)
471 printf("list_dir_hook: %s v:%u u:%u\n", aname, vnode, unique);
476 afs_DbgListDirEntries(struct VenusFid *afid)
480 /* Get shadow dir's dcache. */
481 tdc = afs_FindDCacheByFid(afid);
483 afs_dir_EnumerateDir(tdc, &list_dir_hook, NULL);
489 * Find the parent vcache for a given child
491 * \param avc The vcache whose parent is required
492 * \param afid Fid structure in which parent's fid should be stored
493 * \param aname An AFSNAMEMAX sized buffer to hold the parents name
494 * \param adp A pointer to a struct vcache* which will be set to the
497 * \return An error code. 0 indicates success, EAGAIN that the vnode should
498 * be deferred to later in the resync process
502 afs_GetParentVCache(struct vcache *avc, int deleted, struct VenusFid *afid,
503 char *aname, struct vcache **adp)
509 if (afs_GetParentDirFid(avc, afid)) {
510 printf("afs_GetParentVCache: Couldn't find parent dir's FID.\n");
514 code = afs_GetVnodeName(avc, afid, aname, deleted);
516 printf("afs_GetParentVCache: Couldn't find file name\n");
520 ObtainSharedLock(&afs_xvcache, 766);
521 *adp = afs_FindVCache(afid, 0, 1);
522 ReleaseSharedLock(&afs_xvcache);
524 printf("afs_GetParentVCache: Couldn't find parent dir's vcache\n");
529 if ((*adp)->f.ddirty_flags & VDisconCreate) {
530 printf("afs_GetParentVCache: deferring until parent exists\n");
543 * Handles file renaming on reconnection:
544 * - Get the old name from the old dir's shadow dir.
545 * - Get the new name from the current dir.
546 * - Old dir fid and new dir fid are collected along the way.
549 afs_ProcessOpRename(struct vcache *avc, struct vrequest *areq)
551 struct VenusFid old_pdir_fid, new_pdir_fid;
552 char *old_name = NULL, *new_name = NULL;
553 struct AFSFetchStatus OutOldDirStatus, OutNewDirStatus;
554 struct AFSVolSync tsync;
559 /* Get old dir vcache. */
560 old_pdir_fid.Cell = avc->f.fid.Cell;
561 old_pdir_fid.Fid.Volume = avc->f.fid.Fid.Volume;
562 old_pdir_fid.Fid.Vnode = avc->f.oldParent.vnode;
563 old_pdir_fid.Fid.Unique = avc->f.oldParent.unique;
566 old_name = (char *) afs_osi_Alloc(AFSNAMEMAX);
568 printf("afs_ProcessOpRename: Couldn't alloc space for old name.\n");
571 code = afs_GetVnodeName(avc, &old_pdir_fid, old_name, 1);
573 printf("afs_ProcessOpRename: Couldn't find old name.\n");
577 /* Alloc data first. */
578 new_name = (char *) afs_osi_Alloc(AFSNAMEMAX);
580 printf("afs_ProcessOpRename: Couldn't alloc space for new name.\n");
585 if (avc->f.ddirty_flags & VDisconRenameSameDir) {
586 /* If we're in the same dir, don't do the lookups all over again,
587 * just copy fid and vcache from the old dir.
589 memcpy(&new_pdir_fid, &old_pdir_fid, sizeof(struct VenusFid));
591 /* Get parent dir's FID.*/
592 if (afs_GetParentDirFid(avc, &new_pdir_fid)) {
593 printf("afs_ProcessOpRename: Couldn't find new parent dir FID.\n");
599 /* And finally get the new name. */
600 code = afs_GetVnodeName(avc, &new_pdir_fid, new_name, 0);
602 printf("afs_ProcessOpRename: Couldn't find new name.\n");
606 /* Send to data to server. */
608 tc = afs_Conn(&old_pdir_fid, areq, SHARED_LOCK);
610 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_RENAME);
612 code = RXAFS_Rename(tc->id,
613 (struct AFSFid *)&old_pdir_fid.Fid,
615 (struct AFSFid *)&new_pdir_fid.Fid,
625 } while (afs_Analyze(tc,
629 AFS_STATS_FS_RPCIDX_RENAME,
634 printf("afs_ProcessOpRename: server code=%u\n", code);
637 afs_osi_Free(new_name, AFSNAMEMAX);
639 afs_osi_Free(old_name, AFSNAMEMAX);
644 * Handles all the reconnection details:
645 * - Get all the details about the vnode: name, fid, and parent dir fid.
646 * - Send data to server.
648 * - Reorder vhash and dcaches in their hashes, using the newly acquired fid.
651 afs_ProcessOpCreate(struct vcache *avc, struct vrequest *areq,
654 char *tname = NULL, *ttargetName = NULL;
655 struct AFSStoreStatus InStatus;
656 struct AFSFetchStatus OutFidStatus, OutDirStatus;
657 struct VenusFid pdir_fid, newFid;
658 struct AFSCallBack CallBack;
659 struct AFSVolSync tsync;
660 struct vcache *tdp = NULL, *tvc = NULL;
661 struct dcache *tdc = NULL;
663 afs_int32 hash, new_hash, index;
668 tname = afs_osi_Alloc(AFSNAMEMAX);
672 code = afs_GetParentVCache(avc, 0, &pdir_fid, tname, &tdp);
676 /* This data may also be in linkData, but then we have to deal with
677 * the joy of terminating NULLs and . and file modes. So just get
678 * it from the dcache where it won't have been fiddled with.
680 if (vType(avc) == VLNK) {
683 struct osi_file *tfile;
685 tdc = afs_GetDCache(avc, 0, areq, &offset, &tlen, 0);
697 tlen++; /* space for NULL */
698 ttargetName = afs_osi_Alloc(tlen);
703 ObtainReadLock(&tdc->lock);
704 tfile = afs_CFileOpen(&tdc->f.inode);
705 code = afs_CFileRead(tfile, 0, ttargetName, tlen);
706 ttargetName[tlen-1] = '\0';
707 afs_CFileClose(tfile);
708 ReleaseReadLock(&tdc->lock);
713 InStatus.Mask = AFS_SETMODTIME | AFS_SETMODE | AFS_SETGROUP;
714 InStatus.ClientModTime = avc->f.m.Date;
715 InStatus.Owner = avc->f.m.Owner;
716 InStatus.Group = (afs_int32) cr_gid(acred);
717 /* Only care about protection bits. */
718 InStatus.UnixModeBits = avc->f.m.Mode & 0xffff;
721 tc = afs_Conn(&tdp->f.fid, areq, SHARED_LOCK);
723 switch (vType(avc)) {
725 /* Make file on server. */
726 op = AFS_STATS_FS_RPCIDX_CREATEFILE;
727 XSTATS_START_TIME(op);
729 code = RXAFS_CreateFile(tc->id,
730 (struct AFSFid *)&tdp->f.fid.Fid,
732 (struct AFSFid *) &newFid.Fid,
733 &OutFidStatus, &OutDirStatus,
739 /* Make dir on server. */
740 op = AFS_STATS_FS_RPCIDX_MAKEDIR;
741 XSTATS_START_TIME(op);
743 code = RXAFS_MakeDir(tc->id, (struct AFSFid *) &tdp->f.fid.Fid,
745 (struct AFSFid *) &newFid.Fid,
746 &OutFidStatus, &OutDirStatus,
752 /* Make symlink on server. */
753 op = AFS_STATS_FS_RPCIDX_SYMLINK;
754 XSTATS_START_TIME(op);
756 code = RXAFS_Symlink(tc->id,
757 (struct AFSFid *) &tdp->f.fid.Fid,
758 tname, ttargetName, &InStatus,
759 (struct AFSFid *) &newFid.Fid,
760 &OutFidStatus, &OutDirStatus, &tsync);
765 op = AFS_STATS_FS_RPCIDX_CREATEFILE;
771 } while (afs_Analyze(tc, code, &tdp->f.fid, areq, op, SHARED_LOCK, NULL));
773 /* TODO: Handle errors. */
775 printf("afs_ProcessOpCreate: error while creating vnode on server, code=%d .\n", code);
779 /* The rpc doesn't set the cell number. */
780 newFid.Cell = avc->f.fid.Cell;
783 * Change the fid in the dir entry.
786 /* Seek the dir's dcache. */
787 tdc = afs_FindDCacheByFid(&tdp->f.fid);
789 /* And now change the fid in the parent dir entry. */
790 afs_dir_ChangeFid(tdc, tname, &avc->f.fid.Fid.Vnode, &newFid.Fid.Vnode);
794 if (vType(avc) == VDIR) {
795 /* Change fid in the dir for the "." entry. ".." has alredy been
796 * handled by afs_FixChildrenFids when processing the parent dir.
798 tdc = afs_FindDCacheByFid(&avc->f.fid);
800 afs_dir_ChangeFid(tdc, ".", &avc->f.fid.Fid.Vnode,
803 if (avc->f.m.LinkCount >= 2)
804 /* For non empty dirs, fix children's parentVnode and
805 * parentUnique reference.
807 afs_FixChildrenFids(&avc->f.fid, &newFid);
813 /* Recompute hash chain positions for vnode and dcaches.
814 * Then change to the new FID.
817 /* The vcache goes first. */
818 ObtainWriteLock(&afs_xvcache, 735);
821 hash = VCHash(&avc->f.fid);
823 new_hash = VCHash(&newFid);
825 /* Remove hash from old position. */
826 /* XXX: not checking array element contents. It shouldn't be empty.
827 * If it oopses, then something else might be wrong.
829 if (afs_vhashT[hash] == avc) {
830 /* First in hash chain (might be the only one). */
831 afs_vhashT[hash] = avc->hnext;
833 /* More elements in hash chain. */
834 for (tvc = afs_vhashT[hash]; tvc; tvc = tvc->hnext) {
835 if (tvc->hnext == avc) {
836 tvc->hnext = avc->hnext;
840 } /* if (!afs_vhashT[i]->hnext) */
841 QRemove(&avc->vhashq);
843 /* Insert hash in new position. */
844 avc->hnext = afs_vhashT[new_hash];
845 afs_vhashT[new_hash] = avc;
846 QAdd(&afs_vhashTV[VCHashV(&newFid)], &avc->vhashq);
848 ReleaseWriteLock(&afs_xvcache);
850 /* Do the same thing for all dcaches. */
851 hash = DVHash(&avc->f.fid);
852 ObtainWriteLock(&afs_xdcache, 743);
853 for (index = afs_dvhashTbl[hash]; index != NULLIDX; index = hash) {
854 hash = afs_dvnextTbl[index];
855 tdc = afs_GetDSlot(index, NULL);
856 ReleaseReadLock(&tdc->tlock);
857 if (afs_indexUnique[index] == avc->f.fid.Fid.Unique) {
858 if (!FidCmp(&tdc->f.fid, &avc->f.fid)) {
860 /* Safer but slower. */
861 afs_HashOutDCache(tdc, 0);
863 /* Put dcache in new positions in the dchash and dvhash. */
864 new_hash = DCHash(&newFid, tdc->f.chunk);
865 afs_dcnextTbl[tdc->index] = afs_dchashTbl[new_hash];
866 afs_dchashTbl[new_hash] = tdc->index;
868 new_hash = DVHash(&newFid);
869 afs_dvnextTbl[tdc->index] = afs_dvhashTbl[new_hash];
870 afs_dvhashTbl[new_hash] = tdc->index;
872 afs_indexUnique[tdc->index] = newFid.Fid.Unique;
873 memcpy(&tdc->f.fid, &newFid, sizeof(struct VenusFid));
875 } /* if uniquifier match */
878 } /* for all dcaches in this hash bucket */
879 ReleaseWriteLock(&afs_xdcache);
881 /* Now we can set the new fid. */
882 memcpy(&avc->f.fid, &newFid, sizeof(struct VenusFid));
887 afs_osi_Free(tname, AFSNAMEMAX);
889 afs_osi_Free(ttargetName, tlen);
894 * Remove a vnode on the server, be it file or directory.
895 * Not much to do here only get the parent dir's fid and call the
898 * \param avc The deleted vcache
901 * \note The vcache refcount should be dropped because it points to
902 * a deleted vnode and has served it's purpouse, but we drop refcount
903 * on shadow dir deletio (we still need it for that).
905 * \note avc must be write locked.
908 afs_ProcessOpRemove(struct vcache *avc, struct vrequest *areq)
911 struct AFSFetchStatus OutDirStatus;
912 struct VenusFid pdir_fid;
913 struct AFSVolSync tsync;
915 struct vcache *tdp = NULL;
919 tname = afs_osi_Alloc(AFSNAMEMAX);
921 printf("afs_ProcessOpRemove: Couldn't alloc space for file name\n");
925 code = afs_GetParentVCache(avc, 1, &pdir_fid, tname, &tdp);
929 if ((vType(avc) == VDIR) && (afs_CheckDeletedChildren(avc))) {
930 /* Deleted children of this dir remain unsynchronized.
937 if (vType(avc) == VREG || vType(avc) == VLNK) {
938 /* Remove file on server. */
940 tc = afs_Conn(&pdir_fid, areq, SHARED_LOCK);
942 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_REMOVEFILE);
944 code = RXAFS_RemoveFile(tc->id,
954 } while (afs_Analyze(tc,
958 AFS_STATS_FS_RPCIDX_REMOVEFILE,
962 } else if (vType(avc) == VDIR) {
963 /* Remove dir on server. */
965 tc = afs_Conn(&pdir_fid, areq, SHARED_LOCK);
967 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_REMOVEDIR);
969 code = RXAFS_RemoveDir(tc->id,
978 } while (afs_Analyze(tc,
982 AFS_STATS_FS_RPCIDX_REMOVEDIR,
986 } /* if (vType(avc) == VREG) */
989 printf("afs_ProcessOpRemove: server returned code=%u\n", code);
992 afs_osi_Free(tname, AFSNAMEMAX);
997 * Send disconnected file changes to the server.
999 * \note Call with vnode locked both locally and on the server.
1001 * \param avc Vnode that gets synchronized to the server.
1002 * \param areq Used for obtaining a conn struct.
1004 * \return 0 for success. On failure, other error codes.
1007 afs_SendChanges(struct vcache *avc, struct vrequest *areq)
1009 struct afs_conn *tc;
1010 struct AFSStoreStatus sstat;
1011 struct AFSFetchStatus fstat;
1012 struct AFSVolSync tsync;
1017 /* Start multiplexing dirty operations from ddirty_flags field: */
1018 if (avc->f.ddirty_flags & VDisconSetAttrMask) {
1020 /* Turn dirty vc data into a new store status... */
1021 if (afs_GenStoreStatus(avc, &sstat) > 0) {
1023 tc = afs_Conn(&avc->f.fid, areq, SHARED_LOCK);
1025 /* ... and send it. */
1026 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_STORESTATUS);
1028 code = RXAFS_StoreStatus(tc->id,
1029 (struct AFSFid *) &avc->f.fid.Fid,
1039 } while (afs_Analyze(tc,
1043 AFS_STATS_FS_RPCIDX_STORESTATUS,
1047 } /* if (afs_GenStoreStatus() > 0)*/
1048 } /* disconnected SETATTR */
1053 if (avc->f.ddirty_flags &
1057 | VDisconWriteOsiFlush)) {
1061 tc = afs_Conn(&avc->f.fid, areq, SHARED_LOCK);
1063 /* Set storing flags. XXX: A tad inefficient ... */
1064 if (avc->f.ddirty_flags & VDisconWriteClose)
1065 flags |= AFS_LASTSTORE;
1066 if (avc->f.ddirty_flags & VDisconWriteOsiFlush)
1067 flags |= (AFS_SYNC | AFS_LASTSTORE);
1068 if (avc->f.ddirty_flags & VDisconWriteFlush)
1071 /* Try to send store to server. */
1072 /* XXX: AFS_LASTSTORE for writes? Or just AFS_SYNC for all? */
1073 code = afs_StoreAllSegments(avc, areq, flags);
1077 } while (afs_Analyze(tc,
1081 AFS_STATS_FS_RPCIDX_STOREDATA,
1085 } /* disconnected TRUNC | WRITE */
1091 * All files that have been dirty before disconnection are going to
1092 * be replayed back to the server.
1094 * \param areq Request from the user.
1095 * \param acred User credentials.
1097 * \return If all files synchronized succesfully, return 0, otherwise
1100 * \note For now, it's the request from the PDiscon pioctl.
1104 afs_ResyncDisconFiles(struct vrequest *areq, afs_ucred_t *acred)
1106 struct afs_conn *tc;
1108 struct AFSFetchStatus fstat;
1109 struct AFSCallBack callback;
1110 struct AFSVolSync tsync;
1112 afs_int32 start = 0;
1114 /*AFS_STATCNT(afs_ResyncDisconFiles);*/
1116 ObtainWriteLock(&afs_disconDirtyLock, 707);
1118 while (!QEmpty(&afs_disconDirty)) {
1119 tvc = QEntry(QPrev(&afs_disconDirty), struct vcache, dirtyq);
1121 /* Can't lock tvc whilst holding the discon dirty lock */
1122 ReleaseWriteLock(&afs_disconDirtyLock);
1124 /* Get local write lock. */
1125 ObtainWriteLock(&tvc->lock, 705);
1127 if (tvc->f.ddirty_flags & VDisconRemove) {
1128 /* Delete the file on the server and just move on
1129 * to the next file. After all, it has been deleted
1130 * we can't replay any other operation it.
1132 code = afs_ProcessOpRemove(tvc, areq);
1135 } else if (tvc->f.ddirty_flags & VDisconCreate) {
1136 /* For newly created files, we don't need a server lock. */
1137 code = afs_ProcessOpCreate(tvc, areq, acred);
1141 tvc->f.ddirty_flags &= ~VDisconCreate;
1142 tvc->f.ddirty_flags |= VDisconCreated;
1145 /* Get server write lock. */
1147 tc = afs_Conn(&tvc->f.fid, areq, SHARED_LOCK);
1149 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_SETLOCK);
1151 code = RXAFS_SetLock(tc->id,
1152 (struct AFSFid *)&tvc->f.fid.Fid,
1160 } while (afs_Analyze(tc,
1164 AFS_STATS_FS_RPCIDX_SETLOCK,
1171 if (tvc->f.ddirty_flags & VDisconRename) {
1172 /* If we're renaming the file, do so now */
1173 code = afs_ProcessOpRename(tvc, areq);
1175 goto unlock_srv_file;
1178 /* Issue a FetchStatus to get info about DV and callbacks. */
1180 tc = afs_Conn(&tvc->f.fid, areq, SHARED_LOCK);
1182 tvc->callback = tc->srvr->server;
1184 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_FETCHSTATUS);
1186 code = RXAFS_FetchStatus(tc->id,
1187 (struct AFSFid *)&tvc->f.fid.Fid,
1196 } while (afs_Analyze(tc,
1200 AFS_STATS_FS_RPCIDX_FETCHSTATUS,
1205 goto unlock_srv_file;
1208 if ((dv_match(tvc, fstat) && (tvc->f.m.Date == fstat.ServerModTime)) ||
1209 (afs_ConflictPolicy == CLIENT_WINS) ||
1210 (tvc->f.ddirty_flags & VDisconCreated)) {
1212 * Send changes to the server if there's data version match, or
1213 * client wins policy has been selected or file has been created
1214 * but doesn't have it's the contents on to the server yet.
1217 * XXX: Checking server attr changes by timestamp might not the
1218 * most elegant solution, but it's the most viable one that we could find.
1220 afs_UpdateStatus(tvc, &tvc->f.fid, areq, &fstat, &callback, start);
1221 code = afs_SendChanges(tvc, areq);
1223 } else if (afs_ConflictPolicy == SERVER_WINS) {
1224 /* DV mismatch, apply collision resolution policy. */
1225 /* Discard this files chunks and remove from current dir. */
1226 afs_ResetVCache(tvc, acred);
1227 tvc->f.truncPos = AFS_NOTRUNC;
1229 printf("afs_ResyncDisconFiles: no resolution policy selected.\n");
1230 } /* if DV match or client wins policy */
1233 /* Release server write lock. */
1236 tc = afs_Conn(&tvc->f.fid, areq, SHARED_LOCK);
1238 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_RELEASELOCK);
1240 ucode = RXAFS_ReleaseLock(tc->id,
1241 (struct AFSFid *) &tvc->f.fid.Fid,
1247 } while (afs_Analyze(tc,
1251 AFS_STATS_FS_RPCIDX_RELEASELOCK,
1256 ObtainWriteLock(&afs_disconDirtyLock, 710);
1258 /* Replayed successfully - pull the vcache from the
1259 * disconnected list */
1260 tvc->f.ddirty_flags = 0;
1261 QRemove(&tvc->dirtyq);
1264 if (code == EAGAIN) {
1265 /* Operation was deferred. Pull it from the current place in
1266 * the list, and stick it at the end again */
1267 QRemove(&tvc->dirtyq);
1268 QAdd(&afs_disconDirty, &tvc->dirtyq);
1270 /* Failed - keep state as is, and let the user know we died */
1272 ReleaseWriteLock(&tvc->lock);
1277 /* Release local write lock. */
1278 ReleaseWriteLock(&tvc->lock);
1282 ReleaseWriteLock(&afs_disconDirtyLock);
1286 /* Dispose of all of the shadow directories */
1287 afs_DisconDiscardAllShadows(0, acred);
1289 ReleaseWriteLock(&afs_disconDirtyLock);
1294 * Discard all of our shadow directory copies. If squash is true, then
1295 * we also invalidate the vcache holding the shadow directory, to ensure
1296 * that any disconnected changes are deleted
1301 * \note afs_disconDirtyLock must be held on entry. It will be released
1306 afs_DisconDiscardAllShadows(int squash, afs_ucred_t *acred) {
1309 while (!QEmpty(&afs_disconShadow)) {
1310 tvc = QEntry(QNext(&afs_disconShadow), struct vcache, shadowq);
1312 /* Must release the dirty lock to be able to get a vcache lock */
1313 ReleaseWriteLock(&afs_disconDirtyLock);
1314 ObtainWriteLock(&tvc->lock, 706);
1316 afs_DeleteShadowDir(tvc);
1317 tvc->f.shadow.vnode = 0;
1318 tvc->f.shadow.unique = 0;
1321 afs_ResetVCache(tvc, acred);
1323 ReleaseWriteLock(&tvc->lock);
1324 ObtainWriteLock(&afs_disconDirtyLock, 709);
1329 * This function throws away the whole disconnected state, allowing
1330 * the cache manager to reconnect to a server if we get into a state
1331 * where reconiliation is impossible.
1337 afs_DisconDiscardAll(afs_ucred_t *acred) {
1340 ObtainWriteLock(&afs_disconDirtyLock, 717);
1341 while (!QEmpty(&afs_disconDirty)) {
1342 tvc = QEntry(QPrev(&afs_disconDirty), struct vcache, dirtyq);
1343 ReleaseWriteLock(&afs_disconDirtyLock);
1345 ObtainWriteLock(&tvc->lock, 718);
1346 afs_ResetVCache(tvc, acred);
1347 tvc->f.truncPos = AFS_NOTRUNC;
1348 ReleaseWriteLock(&tvc->lock);
1350 ObtainWriteLock(&afs_disconDirtyLock, 719);
1353 afs_DisconDiscardAllShadows(1, acred);
1355 ReleaseWriteLock(&afs_disconDirtyLock);
1359 * Print list of disconnected files.
1361 * \note Call with afs_DDirtyVCListLock read locked.
1364 afs_DbgDisconFiles(void)
1370 printf("List of dirty files: \n");
1372 ObtainReadLock(&afs_disconDirtyLock);
1373 for (q = QPrev(&afs_disconDirty); q != &afs_disconDirty; q = QPrev(q)) {
1374 tvc = QEntry(q, struct vcache, dirtyq);
1376 printf("Cell=%u Volume=%u VNode=%u Unique=%u\n",
1378 tvc->f.fid.Fid.Volume,
1379 tvc->f.fid.Fid.Vnode,
1380 tvc->f.fid.Fid.Unique);
1384 osi_Panic("afs_DbgDisconFiles: loop in dirty list\n");
1386 ReleaseReadLock(&afs_disconDirtyLock);
1390 * Generate a fake fid for a disconnected shadow dir.
1391 * Similar to afs_GenFakeFid, only that it uses the dhash
1392 * to search for a uniquifier because a shadow dir lives only
1397 * \note Don't forget to fill in afid with Cell and Volume.
1400 afs_GenShadowFid(struct VenusFid *afid)
1402 afs_uint32 i, index, max_unique = 1;
1403 struct vcache *tvc = NULL;
1405 /* Try generating a fid that isn't used in the vhash. */
1407 /* Shadow Fids are always directories */
1408 afid->Fid.Vnode = afs_DisconVnode + 1;
1411 ObtainWriteLock(&afs_xdcache, 737);
1412 for (index = afs_dvhashTbl[i]; index != NULLIDX; index = i) {
1413 i = afs_dvnextTbl[index];
1414 if (afs_indexUnique[index] > max_unique)
1415 max_unique = afs_indexUnique[index];
1418 ReleaseWriteLock(&afs_xdcache);
1419 afid->Fid.Unique = max_unique + 1;
1420 afs_DisconVnode += 2;
1421 if (!afs_DisconVnode)
1422 afs_DisconVnode = 2;
1424 /* Is this a used vnode? */
1425 ObtainSharedLock(&afs_xvcache, 762);
1426 tvc = afs_FindVCache(afid, 0, 1);
1427 ReleaseSharedLock(&afs_xvcache);
1434 * Generate a fake fid (vnode and uniquifier) for a vcache
1435 * (either dir or normal file). The vnode is generated via
1436 * afs_DisconVNode and the uniquifier by getting the highest
1437 * uniquifier on a hash chain and incrementing it by one.
1439 * \param afid The fid structre that will be filled.
1440 * \param avtype Vnode type: VDIR/VREG.
1441 * \param lock True indicates that xvcache may be obtained,
1442 * False that it is already held
1444 * \note The cell number must be completed somewhere else.
1447 afs_GenFakeFid(struct VenusFid *afid, afs_uint32 avtype, int lock)
1450 afs_uint32 max_unique = 0, i;
1454 afid->Fid.Vnode = afs_DisconVnode + 1;
1458 afid->Fid.Vnode = afs_DisconVnode;
1463 ObtainWriteLock(&afs_xvcache, 736);
1465 for (tvc = afs_vhashT[i]; tvc; tvc = tvc->hnext) {
1466 if (tvc->f.fid.Fid.Unique > max_unique)
1467 max_unique = tvc->f.fid.Fid.Unique;
1470 ReleaseWriteLock(&afs_xvcache);
1472 afid->Fid.Unique = max_unique + 1;
1473 afs_DisconVnode += 2;
1474 if (!afs_DisconVnode)
1475 afs_DisconVnode = 2;
1479 * Fill in stats for a newly created file/directory.
1481 * \param adp The parent dir's vcache.
1482 * \param avc The created vnode.
1483 * \param afid The new fid.
1486 * \param file_type Specify if file or directory.
1488 * \note Call with avc write locked.
1491 afs_GenDisconStatus(struct vcache *adp, struct vcache *avc,
1492 struct VenusFid *afid, struct vattr *attrs,
1493 struct vrequest *areq, int file_type)
1495 memcpy(&avc->f.fid, afid, sizeof(struct VenusFid));
1496 avc->f.m.Mode = attrs->va_mode;
1498 * avc->f.m.Owner = attrs->va_uid;
1499 * But now we use the parent dir's ownership,
1500 * there's no other way to get a server owner id.
1501 * XXX: Does it really matter?
1503 avc->f.m.Group = adp->f.m.Group;
1504 avc->f.m.Owner = adp->f.m.Owner;
1505 hset64(avc->f.m.DataVersion, 0, 0);
1506 avc->f.m.Length = attrs->va_size;
1507 avc->f.m.Date = osi_Time();
1510 vSetType(avc, VREG);
1511 avc->f.m.Mode |= S_IFREG;
1512 avc->f.m.LinkCount = 1;
1513 avc->f.parent.vnode = adp->f.fid.Fid.Vnode;
1514 avc->f.parent.unique = adp->f.fid.Fid.Unique;
1517 vSetType(avc, VDIR);
1518 avc->f.m.Mode |= S_IFDIR;
1519 avc->f.m.LinkCount = 2;
1522 vSetType(avc, VLNK);
1523 avc->f.m.Mode |= S_IFLNK;
1524 if ((avc->f.m.Mode & 0111) == 0)
1526 avc->f.parent.vnode = adp->f.fid.Fid.Vnode;
1527 avc->f.parent.unique = adp->f.fid.Fid.Unique;
1532 avc->f.anyAccess = adp->f.anyAccess;
1533 afs_AddAxs(avc->Access, areq->uid, adp->Access->axess);
1535 avc->callback = NULL;
1536 avc->f.states |= CStatd;
1537 avc->f.states &= ~CBulkFetching;