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"
17 #define dv_match(vc, fstat) \
18 ((vc->f.m.DataVersion.low == fstat.DataVersion) && \
19 (vc->f.m.DataVersion.high == fstat.dataVersionHigh))
21 /*! Circular queue of dirty vcaches */
22 struct afs_q afs_disconDirty;
24 /*! Circular queue of vcaches with shadow directories */
25 struct afs_q afs_disconShadow;
27 /*! Locks both of these lists. Must be write locked for anything other than
29 afs_rwlock_t afs_disconDirtyLock;
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 */
37 /*! Vnode number. On file creation, use the current value and increment it.
39 afs_uint32 afs_DisconVnode = 2;
41 /*! Conflict policy. */
49 afs_int32 afs_ConflictPolicy = SERVER_WINS;
51 static void afs_DisconDiscardAllShadows(int, afs_ucred_t *);
52 void afs_DbgListDirEntries(struct VenusFid *afid);
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.
62 * \return The found dcache or NULL.
65 afs_FindDCacheByFid(struct VenusFid *afid)
68 struct dcache *tdc = NULL;
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 */
81 index = afs_dvnextTbl[index];
83 ReleaseWriteLock(&afs_xdcache);
91 * Generate a store status from a dirty vcache entry.
93 * \param avc Dirty vcache entry.
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.
99 * \return Mask of operations.
102 afs_GenStoreStatus(struct vcache *avc, struct AFSStoreStatus *astat)
104 if (!avc || !astat || !avc->f.ddirty_flags)
107 /* Clean up store stat. */
108 memset(astat, 0, sizeof(struct AFSStoreStatus));
110 if (avc->f.ddirty_flags & VDisconSetTime) {
111 /* Update timestamp. */
112 astat->ClientModTime = avc->f.m.Date;
113 astat->Mask |= AFS_SETMODTIME;
116 if (avc->f.ddirty_flags & VDisconSetMode) {
117 /* Copy the mode bits. */
118 astat->UnixModeBits = avc->f.m.Mode;
119 astat->Mask |= AFS_SETMODE;
122 /* XXX: more to come... ?*/
128 * Hook for filtering the local dir fid by searching the "." entry.
130 * \param hdata The fid to be filled.
133 get_parent_dir_fid_hook(void *hdata, char *aname, afs_int32 vnode,
136 struct VenusFid *tfid = (struct VenusFid *) hdata;
138 if ((aname[0] == '.') && (aname[1] == '.') && !aname[2]) {
139 tfid->Fid.Vnode = vnode;
140 tfid->Fid.Unique = unique;
148 * Get a the dir's fid by looking in the vcache for simple files and
149 * in the ".." entry for directories.
151 * \param avc The file's vhash entry.
152 * \param afid Put the fid here.
154 * \return 0 on success, -1 on failure
157 afs_GetParentDirFid(struct vcache *avc, struct VenusFid *afid)
161 afid->Cell = avc->f.fid.Cell;
162 afid->Fid.Volume = avc->f.fid.Fid.Volume;
164 switch (vType(avc)) {
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;
172 /* If dir or parent dir created locally*/
173 tdc = afs_FindDCacheByFid(&avc->f.fid);
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);
179 if (afid->Fid.Unique == 0) {
194 struct VenusFid *fid;
200 * Hook that searches a certain fid's name.
202 * \param hdata NameAndFid structure containin a pointer to a fid
203 * and an allocate name. The name will be filled when hit.
206 get_vnode_name_hook(void *hdata, char *aname, afs_int32 vnode,
209 struct NameAndFid *nf = (struct NameAndFid *) hdata;
211 if ((nf->fid->Fid.Vnode == vnode) &&
212 (nf->fid->Fid.Unique == unique)) {
213 nf->name_len = strlen(aname);
214 memcpy(nf->name, aname, nf->name_len);
215 nf->name[nf->name_len] = 0;
224 * Try to get a vnode's name by comparing all parent dir's entries
225 * to the given fid. It can also return the dir's dcache.
227 * \param avc The file's vcache.
228 * \param afid The parent dir's fid.
229 * \param aname A preallocated string for the name.
230 * \param deleted Has this file been deleted? If yes, use the shadow
231 * dir for looking up the name.
234 afs_GetVnodeName(struct vcache *avc, struct VenusFid *afid, char *aname,
239 struct vcache *parent_vc;
240 struct NameAndFid tnf;
241 struct VenusFid parent_fid;
242 struct VenusFid shadow_fid;
244 /* List dir contents and get it's tdc. */
246 /* For deleted files, get the shadow dir's tdc: */
248 /* Get the parent dir's vcache that contains the shadow fid. */
249 parent_fid.Cell = avc->f.fid.Cell;
250 parent_fid.Fid.Volume = avc->f.fid.Fid.Volume;
251 if (avc->f.ddirty_flags & VDisconRename) {
252 /* For renames the old dir fid is needed. */
253 parent_fid.Fid.Vnode = avc->f.oldParent.vnode;
254 parent_fid.Fid.Unique = avc->f.oldParent.unique;
256 parent_fid.Fid.Vnode = afid->Fid.Vnode;
257 parent_fid.Fid.Unique = afid->Fid.Unique;
260 /* Get the parent dir's vcache that contains the shadow fid. */
261 ObtainSharedLock(&afs_xvcache, 755);
262 parent_vc = afs_FindVCache(&parent_fid, 0, 1);
263 ReleaseSharedLock(&afs_xvcache);
268 shadow_fid.Cell = parent_vc->f.fid.Cell;
269 shadow_fid.Fid.Volume = parent_vc->f.fid.Fid.Volume;
270 shadow_fid.Fid.Vnode = parent_vc->f.shadow.vnode;
271 shadow_fid.Fid.Unique = parent_vc->f.shadow.unique;
273 afs_PutVCache(parent_vc);
275 /* Get shadow dir's dcache. */
276 tdc = afs_FindDCacheByFid(&shadow_fid);
280 /* For normal files, look into the current dir's entry. */
281 tdc = afs_FindDCacheByFid(afid);
285 tnf.fid = &avc->f.fid;
288 afs_dir_EnumerateDir(tdc, &get_vnode_name_hook, &tnf);
290 if (tnf.name_len == -1)
293 /* printf("Directory dcache not found!\n"); */
300 struct DirtyChildrenCount {
306 * Lookup dirty deleted vnodes in this dir.
309 chk_del_children_hook(void *hdata, char *aname, afs_int32 vnode,
312 struct VenusFid tfid;
313 struct DirtyChildrenCount *v = (struct DirtyChildrenCount *) hdata;
316 if ((aname[0] == '.') && !aname[1])
317 /* Skip processing this dir again.
318 * It would result in an endless loop.
322 if ((aname[0] == '.') && (aname[1] == '.') && !aname[2])
323 /* Don't process parent dir. */
326 /* Get this file's vcache. */
327 tfid.Cell = v->vc->f.fid.Cell;
328 tfid.Fid.Volume = v->vc->f.fid.Fid.Volume;
329 tfid.Fid.Vnode = vnode;
330 tfid.Fid.Unique = unique;
332 ObtainSharedLock(&afs_xvcache, 757);
333 tvc = afs_FindVCache(&tfid, 0, 1);
334 ReleaseSharedLock(&afs_xvcache);
336 /* Count unfinished dirty children. */
338 ObtainReadLock(&tvc->lock);
339 if (tvc->f.ddirty_flags)
341 ReleaseReadLock(&tvc->lock);
350 * Check if entries have been deleted in a vnode's shadow
353 * \return Returns the number of dirty children.
355 * \note afs_DDirtyVCListLock must be write locked.
358 afs_CheckDeletedChildren(struct vcache *avc)
361 struct DirtyChildrenCount dcc;
362 struct VenusFid shadow_fid;
364 if (!avc->f.shadow.vnode)
368 shadow_fid.Cell = avc->f.fid.Cell;
369 shadow_fid.Fid.Volume = avc->f.fid.Fid.Volume;
370 shadow_fid.Fid.Vnode = avc->f.shadow.vnode;
371 shadow_fid.Fid.Unique = avc->f.shadow.unique;
375 /* Get shadow dir's dcache. */
376 tdc = afs_FindDCacheByFid(&shadow_fid);
379 afs_dir_EnumerateDir(tdc, &chk_del_children_hook, &dcc);
387 * Changes a file's parent fid references.
390 fix_children_fids_hook(void *hdata, char *aname, afs_int32 vnode,
393 struct VenusFid tfid;
394 struct VenusFid *afid = (struct VenusFid *) hdata;
396 struct dcache *tdc = NULL;
398 if ((aname[0] == '.') && !aname[1])
401 if ((aname[0] == '.') && (aname[1] == '.') && !aname[2])
404 tfid.Cell = afid->Cell;
405 tfid.Fid.Volume = afid->Fid.Volume;
406 tfid.Fid.Vnode = vnode;
407 tfid.Fid.Unique = unique;
410 /* vnode's parity indicates that it's a file. */
412 /* Get the vcache. */
413 ObtainSharedLock(&afs_xvcache, 759);
414 tvc = afs_FindVCache(&tfid, 0, 1);
415 ReleaseSharedLock(&afs_xvcache);
417 /* Change the fields. */
419 tvc->f.parent.vnode = afid->Fid.Vnode;
420 tvc->f.parent.unique = afid->Fid.Unique;
425 /* It's a dir. Fix this dir's .. entry to contain the new fid. */
426 /* Seek the dir's dcache. */
427 tdc = afs_FindDCacheByFid(&tfid);
429 /* Change the .. entry fid. */
430 afs_dir_ChangeFid(tdc, "..", NULL, &afid->Fid.Vnode);
433 } /* if (!(vnode % 2))*/
439 * Fixes the parentVnode and parentUnique fields of all
440 * files (not dirs) contained in the directory pointed by
441 * old_fid. This is useful on resync, when a locally created dir
442 * get's a new fid and all the children references must be updated
443 * to reflect the new fid.
445 * \note The dir's fid hasn't been changed yet, it is still referenced
448 * \param old_fid The current dir's fid.
449 * \param new_fid The new dir's fid.
452 afs_FixChildrenFids(struct VenusFid *old_fid, struct VenusFid *new_fid)
456 /* Get shadow dir's dcache. */
457 tdc = afs_FindDCacheByFid(old_fid);
458 /* Change the fids. */
460 afs_dir_EnumerateDir(tdc, &fix_children_fids_hook, new_fid);
466 list_dir_hook(void *hdata, char *aname, afs_int32 vnode, afs_int32 unique)
468 /* printf("list_dir_hook: %s v:%u u:%u\n", aname, vnode, unique); */
473 afs_DbgListDirEntries(struct VenusFid *afid)
477 /* Get shadow dir's dcache. */
478 tdc = afs_FindDCacheByFid(afid);
480 afs_dir_EnumerateDir(tdc, &list_dir_hook, NULL);
486 * Find the parent vcache for a given child
488 * \param avc The vcache whose parent is required
489 * \param afid Fid structure in which parent's fid should be stored
490 * \param aname An AFSNAMEMAX sized buffer to hold the parents name
491 * \param adp A pointer to a struct vcache* which will be set to the
494 * \return An error code. 0 indicates success, EAGAIN that the vnode should
495 * be deferred to later in the resync process
499 afs_GetParentVCache(struct vcache *avc, int deleted, struct VenusFid *afid,
500 char *aname, struct vcache **adp)
506 if (afs_GetParentDirFid(avc, afid)) {
507 /* printf("afs_GetParentVCache: Couldn't find parent dir's FID.\n"); */
511 code = afs_GetVnodeName(avc, afid, aname, deleted);
513 /* printf("afs_GetParentVCache: Couldn't find file name\n"); */
517 ObtainSharedLock(&afs_xvcache, 766);
518 *adp = afs_FindVCache(afid, 0, 1);
519 ReleaseSharedLock(&afs_xvcache);
521 /* printf("afs_GetParentVCache: Couldn't find parent dir's vcache\n"); */
526 if ((*adp)->f.ddirty_flags & VDisconCreate) {
527 /* printf("afs_GetParentVCache: deferring until parent exists\n"); */
542 * Handles file renaming on reconnection:
543 * - Get the old name from the old dir's shadow dir.
544 * - Get the new name from the current dir.
545 * - Old dir fid and new dir fid are collected along the way.
548 afs_ProcessOpRename(struct vcache *avc, struct vrequest *areq)
550 struct VenusFid old_pdir_fid, new_pdir_fid;
551 char *old_name = NULL, *new_name = NULL;
552 struct AFSFetchStatus OutOldDirStatus, OutNewDirStatus;
553 struct AFSVolSync tsync;
558 /* Get old dir vcache. */
559 old_pdir_fid.Cell = avc->f.fid.Cell;
560 old_pdir_fid.Fid.Volume = avc->f.fid.Fid.Volume;
561 old_pdir_fid.Fid.Vnode = avc->f.oldParent.vnode;
562 old_pdir_fid.Fid.Unique = avc->f.oldParent.unique;
565 old_name = afs_osi_Alloc(AFSNAMEMAX);
567 /* printf("afs_ProcessOpRename: Couldn't alloc space for old name.\n"); */
570 code = afs_GetVnodeName(avc, &old_pdir_fid, old_name, 1);
572 /* printf("afs_ProcessOpRename: Couldn't find old name.\n"); */
576 /* Alloc data first. */
577 new_name = afs_osi_Alloc(AFSNAMEMAX);
579 /* printf("afs_ProcessOpRename: Couldn't alloc space for new name.\n"); */
584 if (avc->f.ddirty_flags & VDisconRenameSameDir) {
585 /* If we're in the same dir, don't do the lookups all over again,
586 * just copy fid and vcache from the old dir.
588 memcpy(&new_pdir_fid, &old_pdir_fid, sizeof(struct VenusFid));
590 /* Get parent dir's FID.*/
591 if (afs_GetParentDirFid(avc, &new_pdir_fid)) {
592 /* printf("afs_ProcessOpRename: Couldn't find new parent dir FID.\n"); */
598 /* And finally get the new name. */
599 code = afs_GetVnodeName(avc, &new_pdir_fid, new_name, 0);
601 /* printf("afs_ProcessOpRename: Couldn't find new name.\n"); */
605 /* Send to data to server. */
607 tc = afs_Conn(&old_pdir_fid, areq, SHARED_LOCK);
609 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_RENAME);
611 code = RXAFS_Rename(tc->id,
612 (struct AFSFid *)&old_pdir_fid.Fid,
614 (struct AFSFid *)&new_pdir_fid.Fid,
624 } while (afs_Analyze(tc,
628 AFS_STATS_FS_RPCIDX_RENAME,
632 /* if (code) printf("afs_ProcessOpRename: server code=%u\n", code); */
635 afs_osi_Free(new_name, AFSNAMEMAX);
637 afs_osi_Free(old_name, AFSNAMEMAX);
642 * Handles all the reconnection details:
643 * - Get all the details about the vnode: name, fid, and parent dir fid.
644 * - Send data to server.
646 * - Reorder vhash and dcaches in their hashes, using the newly acquired fid.
649 afs_ProcessOpCreate(struct vcache *avc, struct vrequest *areq,
652 char *tname = NULL, *ttargetName = NULL;
653 struct AFSStoreStatus InStatus;
654 struct AFSFetchStatus OutFidStatus, OutDirStatus;
655 struct VenusFid pdir_fid, newFid;
656 struct AFSCallBack CallBack;
657 struct AFSVolSync tsync;
658 struct vcache *tdp = NULL, *tvc = NULL;
659 struct dcache *tdc = NULL;
661 afs_int32 hash, new_hash, index;
666 tname = afs_osi_Alloc(AFSNAMEMAX);
670 code = afs_GetParentVCache(avc, 0, &pdir_fid, tname, &tdp);
674 /* This data may also be in linkData, but then we have to deal with
675 * the joy of terminating NULLs and . and file modes. So just get
676 * it from the dcache where it won't have been fiddled with.
678 if (vType(avc) == VLNK) {
681 struct osi_file *tfile;
683 tdc = afs_GetDCache(avc, 0, areq, &offset, &tlen, 0);
695 tlen++; /* space for NULL */
696 ttargetName = afs_osi_Alloc(tlen);
701 ObtainReadLock(&tdc->lock);
702 tfile = afs_CFileOpen(&tdc->f.inode);
703 code = afs_CFileRead(tfile, 0, ttargetName, tlen);
704 ttargetName[tlen-1] = '\0';
705 afs_CFileClose(tfile);
706 ReleaseReadLock(&tdc->lock);
711 InStatus.Mask = AFS_SETMODTIME | AFS_SETMODE | AFS_SETGROUP;
712 InStatus.ClientModTime = avc->f.m.Date;
713 InStatus.Owner = avc->f.m.Owner;
714 InStatus.Group = (afs_int32) afs_cr_gid(acred);
715 /* Only care about protection bits. */
716 InStatus.UnixModeBits = avc->f.m.Mode & 0xffff;
719 tc = afs_Conn(&tdp->f.fid, areq, SHARED_LOCK);
721 switch (vType(avc)) {
723 /* Make file on server. */
724 op = AFS_STATS_FS_RPCIDX_CREATEFILE;
725 XSTATS_START_TIME(op);
727 code = RXAFS_CreateFile(tc->id,
728 (struct AFSFid *)&tdp->f.fid.Fid,
730 (struct AFSFid *) &newFid.Fid,
731 &OutFidStatus, &OutDirStatus,
737 /* Make dir on server. */
738 op = AFS_STATS_FS_RPCIDX_MAKEDIR;
739 XSTATS_START_TIME(op);
741 code = RXAFS_MakeDir(tc->id, (struct AFSFid *) &tdp->f.fid.Fid,
743 (struct AFSFid *) &newFid.Fid,
744 &OutFidStatus, &OutDirStatus,
750 /* Make symlink on server. */
751 op = AFS_STATS_FS_RPCIDX_SYMLINK;
752 XSTATS_START_TIME(op);
754 code = RXAFS_Symlink(tc->id,
755 (struct AFSFid *) &tdp->f.fid.Fid,
756 tname, ttargetName, &InStatus,
757 (struct AFSFid *) &newFid.Fid,
758 &OutFidStatus, &OutDirStatus, &tsync);
763 op = AFS_STATS_FS_RPCIDX_CREATEFILE;
769 } while (afs_Analyze(tc, code, &tdp->f.fid, areq, op, SHARED_LOCK, NULL));
771 /* TODO: Handle errors. */
773 /* printf("afs_ProcessOpCreate: error while creating vnode on server, code=%d .\n", code); */
777 /* The rpc doesn't set the cell number. */
778 newFid.Cell = avc->f.fid.Cell;
781 * Change the fid in the dir entry.
784 /* Seek the dir's dcache. */
785 tdc = afs_FindDCacheByFid(&tdp->f.fid);
787 /* And now change the fid in the parent dir entry. */
788 afs_dir_ChangeFid(tdc, tname, &avc->f.fid.Fid.Vnode, &newFid.Fid.Vnode);
792 if (vType(avc) == VDIR) {
793 /* Change fid in the dir for the "." entry. ".." has alredy been
794 * handled by afs_FixChildrenFids when processing the parent dir.
796 tdc = afs_FindDCacheByFid(&avc->f.fid);
798 afs_dir_ChangeFid(tdc, ".", &avc->f.fid.Fid.Vnode,
801 if (avc->f.m.LinkCount >= 2)
802 /* For non empty dirs, fix children's parentVnode and
803 * parentUnique reference.
805 afs_FixChildrenFids(&avc->f.fid, &newFid);
811 /* Recompute hash chain positions for vnode and dcaches.
812 * Then change to the new FID.
815 /* The vcache goes first. */
816 ObtainWriteLock(&afs_xvcache, 735);
819 hash = VCHash(&avc->f.fid);
821 new_hash = VCHash(&newFid);
823 /* Remove hash from old position. */
824 /* XXX: not checking array element contents. It shouldn't be empty.
825 * If it oopses, then something else might be wrong.
827 if (afs_vhashT[hash] == avc) {
828 /* First in hash chain (might be the only one). */
829 afs_vhashT[hash] = avc->hnext;
831 /* More elements in hash chain. */
832 for (tvc = afs_vhashT[hash]; tvc; tvc = tvc->hnext) {
833 if (tvc->hnext == avc) {
834 tvc->hnext = avc->hnext;
838 } /* if (!afs_vhashT[i]->hnext) */
839 QRemove(&avc->vhashq);
841 /* Insert hash in new position. */
842 avc->hnext = afs_vhashT[new_hash];
843 afs_vhashT[new_hash] = avc;
844 QAdd(&afs_vhashTV[VCHashV(&newFid)], &avc->vhashq);
846 ReleaseWriteLock(&afs_xvcache);
848 /* Do the same thing for all dcaches. */
849 hash = DVHash(&avc->f.fid);
850 ObtainWriteLock(&afs_xdcache, 743);
851 for (index = afs_dvhashTbl[hash]; index != NULLIDX; index = hash) {
852 hash = afs_dvnextTbl[index];
853 tdc = afs_GetDSlot(index, NULL);
854 ReleaseReadLock(&tdc->tlock);
855 if (afs_indexUnique[index] == avc->f.fid.Fid.Unique) {
856 if (!FidCmp(&tdc->f.fid, &avc->f.fid)) {
858 /* Safer but slower. */
859 afs_HashOutDCache(tdc, 0);
861 /* Put dcache in new positions in the dchash and dvhash. */
862 new_hash = DCHash(&newFid, tdc->f.chunk);
863 afs_dcnextTbl[tdc->index] = afs_dchashTbl[new_hash];
864 afs_dchashTbl[new_hash] = tdc->index;
866 new_hash = DVHash(&newFid);
867 afs_dvnextTbl[tdc->index] = afs_dvhashTbl[new_hash];
868 afs_dvhashTbl[new_hash] = tdc->index;
870 afs_indexUnique[tdc->index] = newFid.Fid.Unique;
871 memcpy(&tdc->f.fid, &newFid, sizeof(struct VenusFid));
873 } /* if uniquifier match */
876 } /* for all dcaches in this hash bucket */
877 ReleaseWriteLock(&afs_xdcache);
879 /* Now we can set the new fid. */
880 memcpy(&avc->f.fid, &newFid, sizeof(struct VenusFid));
885 afs_osi_Free(tname, AFSNAMEMAX);
887 afs_osi_Free(ttargetName, tlen);
892 * Remove a vnode on the server, be it file or directory.
893 * Not much to do here only get the parent dir's fid and call the
896 * \param avc The deleted vcache
899 * \note The vcache refcount should be dropped because it points to
900 * a deleted vnode and has served it's purpouse, but we drop refcount
901 * on shadow dir deletio (we still need it for that).
903 * \note avc must be write locked.
906 afs_ProcessOpRemove(struct vcache *avc, struct vrequest *areq)
909 struct AFSFetchStatus OutDirStatus;
910 struct VenusFid pdir_fid;
911 struct AFSVolSync tsync;
913 struct vcache *tdp = NULL;
917 tname = afs_osi_Alloc(AFSNAMEMAX);
919 /* printf("afs_ProcessOpRemove: Couldn't alloc space for file name\n"); */
923 code = afs_GetParentVCache(avc, 1, &pdir_fid, tname, &tdp);
927 if ((vType(avc) == VDIR) && (afs_CheckDeletedChildren(avc))) {
928 /* Deleted children of this dir remain unsynchronized.
935 if (vType(avc) == VREG || vType(avc) == VLNK) {
936 /* Remove file on server. */
938 tc = afs_Conn(&pdir_fid, areq, SHARED_LOCK);
940 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_REMOVEFILE);
942 code = RXAFS_RemoveFile(tc->id,
952 } while (afs_Analyze(tc,
956 AFS_STATS_FS_RPCIDX_REMOVEFILE,
960 } else if (vType(avc) == VDIR) {
961 /* Remove dir on server. */
963 tc = afs_Conn(&pdir_fid, areq, SHARED_LOCK);
965 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_REMOVEDIR);
967 code = RXAFS_RemoveDir(tc->id,
976 } while (afs_Analyze(tc,
980 AFS_STATS_FS_RPCIDX_REMOVEDIR,
984 } /* if (vType(avc) == VREG) */
986 /* if (code) printf("afs_ProcessOpRemove: server returned code=%u\n", code); */
989 afs_osi_Free(tname, AFSNAMEMAX);
994 * Send disconnected file changes to the server.
996 * \note Call with vnode locked both locally and on the server.
998 * \param avc Vnode that gets synchronized to the server.
999 * \param areq Used for obtaining a conn struct.
1001 * \return 0 for success. On failure, other error codes.
1004 afs_SendChanges(struct vcache *avc, struct vrequest *areq)
1006 struct afs_conn *tc;
1007 struct AFSStoreStatus sstat;
1008 struct AFSFetchStatus fstat;
1009 struct AFSVolSync tsync;
1014 /* Start multiplexing dirty operations from ddirty_flags field: */
1015 if (avc->f.ddirty_flags & VDisconSetAttrMask) {
1017 /* Turn dirty vc data into a new store status... */
1018 if (afs_GenStoreStatus(avc, &sstat) > 0) {
1020 tc = afs_Conn(&avc->f.fid, areq, SHARED_LOCK);
1022 /* ... and send it. */
1023 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_STORESTATUS);
1025 code = RXAFS_StoreStatus(tc->id,
1026 (struct AFSFid *) &avc->f.fid.Fid,
1036 } while (afs_Analyze(tc,
1040 AFS_STATS_FS_RPCIDX_STORESTATUS,
1044 } /* if (afs_GenStoreStatus() > 0)*/
1045 } /* disconnected SETATTR */
1050 if (avc->f.ddirty_flags &
1054 | VDisconWriteOsiFlush)) {
1058 tc = afs_Conn(&avc->f.fid, areq, SHARED_LOCK);
1060 /* Set storing flags. XXX: A tad inefficient ... */
1061 if (avc->f.ddirty_flags & VDisconWriteClose)
1062 flags |= AFS_LASTSTORE;
1063 if (avc->f.ddirty_flags & VDisconWriteOsiFlush)
1064 flags |= (AFS_SYNC | AFS_LASTSTORE);
1065 if (avc->f.ddirty_flags & VDisconWriteFlush)
1068 /* Try to send store to server. */
1069 /* XXX: AFS_LASTSTORE for writes? Or just AFS_SYNC for all? */
1070 code = afs_StoreAllSegments(avc, areq, flags);
1074 } while (afs_Analyze(tc,
1078 AFS_STATS_FS_RPCIDX_STOREDATA,
1082 } /* disconnected TRUNC | WRITE */
1088 * All files that have been dirty before disconnection are going to
1089 * be replayed back to the server.
1091 * \param areq Request from the user.
1092 * \param acred User credentials.
1094 * \return If all files synchronized succesfully, return 0, otherwise
1097 * \note For now, it's the request from the PDiscon pioctl.
1101 afs_ResyncDisconFiles(struct vrequest *areq, afs_ucred_t *acred)
1103 struct afs_conn *tc;
1105 struct AFSFetchStatus fstat;
1106 struct AFSCallBack callback;
1107 struct AFSVolSync tsync;
1109 afs_int32 start = 0;
1111 /*AFS_STATCNT(afs_ResyncDisconFiles);*/
1113 ObtainWriteLock(&afs_disconDirtyLock, 707);
1115 while (!QEmpty(&afs_disconDirty)) {
1116 tvc = QEntry(QPrev(&afs_disconDirty), struct vcache, dirtyq);
1118 /* Can't lock tvc whilst holding the discon dirty lock */
1119 ReleaseWriteLock(&afs_disconDirtyLock);
1121 /* Get local write lock. */
1122 ObtainWriteLock(&tvc->lock, 705);
1124 if (tvc->f.ddirty_flags & VDisconRemove) {
1125 /* Delete the file on the server and just move on
1126 * to the next file. After all, it has been deleted
1127 * we can't replay any other operation it.
1129 code = afs_ProcessOpRemove(tvc, areq);
1132 } else if (tvc->f.ddirty_flags & VDisconCreate) {
1133 /* For newly created files, we don't need a server lock. */
1134 code = afs_ProcessOpCreate(tvc, areq, acred);
1138 tvc->f.ddirty_flags &= ~VDisconCreate;
1139 tvc->f.ddirty_flags |= VDisconCreated;
1142 /* Get server write lock. */
1144 tc = afs_Conn(&tvc->f.fid, areq, SHARED_LOCK);
1146 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_SETLOCK);
1148 code = RXAFS_SetLock(tc->id,
1149 (struct AFSFid *)&tvc->f.fid.Fid,
1157 } while (afs_Analyze(tc,
1161 AFS_STATS_FS_RPCIDX_SETLOCK,
1168 if (tvc->f.ddirty_flags & VDisconRename) {
1169 /* If we're renaming the file, do so now */
1170 code = afs_ProcessOpRename(tvc, areq);
1172 goto unlock_srv_file;
1175 /* Issue a FetchStatus to get info about DV and callbacks. */
1177 tc = afs_Conn(&tvc->f.fid, areq, SHARED_LOCK);
1179 tvc->callback = tc->parent->srvr->server;
1181 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_FETCHSTATUS);
1183 code = RXAFS_FetchStatus(tc->id,
1184 (struct AFSFid *)&tvc->f.fid.Fid,
1193 } while (afs_Analyze(tc,
1197 AFS_STATS_FS_RPCIDX_FETCHSTATUS,
1202 goto unlock_srv_file;
1205 if ((dv_match(tvc, fstat) && (tvc->f.m.Date == fstat.ServerModTime)) ||
1206 (afs_ConflictPolicy == CLIENT_WINS) ||
1207 (tvc->f.ddirty_flags & VDisconCreated)) {
1209 * Send changes to the server if there's data version match, or
1210 * client wins policy has been selected or file has been created
1211 * but doesn't have it's the contents on to the server yet.
1214 * XXX: Checking server attr changes by timestamp might not the
1215 * most elegant solution, but it's the most viable one that we could find.
1217 afs_UpdateStatus(tvc, &tvc->f.fid, areq, &fstat, &callback, start);
1218 code = afs_SendChanges(tvc, areq);
1220 } else if (afs_ConflictPolicy == SERVER_WINS) {
1221 /* DV mismatch, apply collision resolution policy. */
1222 /* Discard this files chunks and remove from current dir. */
1223 afs_ResetVCache(tvc, acred);
1224 tvc->f.truncPos = AFS_NOTRUNC;
1226 /* printf("afs_ResyncDisconFiles: no resolution policy selected.\n"); */
1227 } /* if DV match or client wins policy */
1230 /* Release server write lock. */
1233 tc = afs_Conn(&tvc->f.fid, areq, SHARED_LOCK);
1235 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_RELEASELOCK);
1237 ucode = RXAFS_ReleaseLock(tc->id,
1238 (struct AFSFid *) &tvc->f.fid.Fid,
1244 } while (afs_Analyze(tc,
1248 AFS_STATS_FS_RPCIDX_RELEASELOCK,
1253 ObtainWriteLock(&afs_disconDirtyLock, 710);
1255 /* Replayed successfully - pull the vcache from the
1256 * disconnected list */
1257 tvc->f.ddirty_flags = 0;
1258 QRemove(&tvc->dirtyq);
1261 if (code == EAGAIN) {
1262 /* Operation was deferred. Pull it from the current place in
1263 * the list, and stick it at the end again */
1264 QRemove(&tvc->dirtyq);
1265 QAdd(&afs_disconDirty, &tvc->dirtyq);
1267 /* Failed - keep state as is, and let the user know we died */
1269 ReleaseWriteLock(&tvc->lock);
1274 /* Release local write lock. */
1275 ReleaseWriteLock(&tvc->lock);
1279 ReleaseWriteLock(&afs_disconDirtyLock);
1283 /* Dispose of all of the shadow directories */
1284 afs_DisconDiscardAllShadows(0, acred);
1286 ReleaseWriteLock(&afs_disconDirtyLock);
1291 * Discard all of our shadow directory copies. If squash is true, then
1292 * we also invalidate the vcache holding the shadow directory, to ensure
1293 * that any disconnected changes are deleted
1298 * \note afs_disconDirtyLock must be held on entry. It will be released
1303 afs_DisconDiscardAllShadows(int squash, afs_ucred_t *acred)
1307 while (!QEmpty(&afs_disconShadow)) {
1308 tvc = QEntry(QNext(&afs_disconShadow), struct vcache, shadowq);
1310 /* Must release the dirty lock to be able to get a vcache lock */
1311 ReleaseWriteLock(&afs_disconDirtyLock);
1312 ObtainWriteLock(&tvc->lock, 706);
1315 afs_ResetVCache(tvc, acred);
1317 afs_DeleteShadowDir(tvc);
1319 ReleaseWriteLock(&tvc->lock);
1320 ObtainWriteLock(&afs_disconDirtyLock, 709);
1325 * This function throws away the whole disconnected state, allowing
1326 * the cache manager to reconnect to a server if we get into a state
1327 * where reconiliation is impossible.
1333 afs_DisconDiscardAll(afs_ucred_t *acred)
1337 ObtainWriteLock(&afs_disconDirtyLock, 717);
1338 while (!QEmpty(&afs_disconDirty)) {
1339 tvc = QEntry(QPrev(&afs_disconDirty), struct vcache, dirtyq);
1340 QRemove(&tvc->dirtyq);
1341 ReleaseWriteLock(&afs_disconDirtyLock);
1343 ObtainWriteLock(&tvc->lock, 718);
1344 afs_ResetVCache(tvc, acred);
1345 tvc->f.truncPos = AFS_NOTRUNC;
1346 ReleaseWriteLock(&tvc->lock);
1347 ObtainWriteLock(&afs_disconDirtyLock, 719);
1351 afs_DisconDiscardAllShadows(1, acred);
1353 ReleaseWriteLock(&afs_disconDirtyLock);
1357 * Print list of disconnected files.
1359 * \note Call with afs_DDirtyVCListLock read locked.
1362 afs_DbgDisconFiles(void)
1368 afs_warn("List of dirty files: \n");
1370 ObtainReadLock(&afs_disconDirtyLock);
1371 for (q = QPrev(&afs_disconDirty); q != &afs_disconDirty; q = QPrev(q)) {
1372 tvc = QEntry(q, struct vcache, dirtyq);
1374 afs_warn("Cell=%u Volume=%u VNode=%u Unique=%u\n",
1376 tvc->f.fid.Fid.Volume,
1377 tvc->f.fid.Fid.Vnode,
1378 tvc->f.fid.Fid.Unique);
1382 osi_Panic("afs_DbgDisconFiles: loop in dirty list\n");
1384 ReleaseReadLock(&afs_disconDirtyLock);
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
1395 * \note Don't forget to fill in afid with Cell and Volume.
1398 afs_GenShadowFid(struct VenusFid *afid)
1400 afs_uint32 i, index, max_unique = 1;
1401 struct vcache *tvc = NULL;
1403 /* Try generating a fid that isn't used in the vhash. */
1405 /* Shadow Fids are always directories */
1406 afid->Fid.Vnode = afs_DisconVnode + 1;
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];
1416 ReleaseWriteLock(&afs_xdcache);
1417 afid->Fid.Unique = max_unique + 1;
1418 afs_DisconVnode += 2;
1419 if (!afs_DisconVnode)
1420 afs_DisconVnode = 2;
1422 /* Is this a used vnode? */
1423 ObtainSharedLock(&afs_xvcache, 762);
1424 tvc = afs_FindVCache(afid, 0, 1);
1425 ReleaseSharedLock(&afs_xvcache);
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.
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
1442 * \note The cell number must be completed somewhere else.
1445 afs_GenFakeFid(struct VenusFid *afid, afs_uint32 avtype, int lock)
1448 afs_uint32 max_unique = 0, i;
1452 afid->Fid.Vnode = afs_DisconVnode + 1;
1456 afid->Fid.Vnode = afs_DisconVnode;
1461 ObtainWriteLock(&afs_xvcache, 736);
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;
1468 ReleaseWriteLock(&afs_xvcache);
1470 afid->Fid.Unique = max_unique + 1;
1471 afs_DisconVnode += 2;
1472 if (!afs_DisconVnode)
1473 afs_DisconVnode = 2;
1477 * Fill in stats for a newly created file/directory.
1479 * \param adp The parent dir's vcache.
1480 * \param avc The created vnode.
1481 * \param afid The new fid.
1484 * \param file_type Specify if file or directory.
1486 * \note Call with avc write locked.
1489 afs_GenDisconStatus(struct vcache *adp, struct vcache *avc,
1490 struct VenusFid *afid, struct vattr *attrs,
1491 struct vrequest *areq, int file_type)
1493 memcpy(&avc->f.fid, afid, sizeof(struct VenusFid));
1494 avc->f.m.Mode = attrs->va_mode;
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?
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();
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;
1515 vSetType(avc, VDIR);
1516 avc->f.m.Mode |= S_IFDIR;
1517 avc->f.m.LinkCount = 2;
1520 vSetType(avc, VLNK);
1521 avc->f.m.Mode |= S_IFLNK;
1522 if ((avc->f.m.Mode & 0111) == 0)
1524 avc->f.parent.vnode = adp->f.fid.Fid.Vnode;
1525 avc->f.parent.unique = adp->f.fid.Fid.Unique;
1530 avc->f.anyAccess = adp->f.anyAccess;
1531 afs_AddAxs(avc->Access, areq->uid, adp->Access->axess);
1533 avc->callback = NULL;
1534 avc->f.states |= CStatd;
1535 avc->f.states &= ~CBulkFetching;