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
12 #include "afs/sysincludes.h"
13 #include "afsincludes.h"
14 #include "afs/afs_stats.h" /* statistics */
16 #include "afs/afs_cbqueue.h"
20 #define dv_match(vc, fstat) \
21 ((vc->m.DataVersion.low == fstat.DataVersion) && \
22 (vc->m.DataVersion.high == fstat.dataVersionHigh))
24 /*! Global list of dirty vcaches. */
25 /*! Last added element. */
26 struct vcache *afs_DDirtyVCList = NULL;
28 struct vcache *afs_DDirtyVCListStart = NULL;
29 /*! Previous element in the list. */
30 struct vcache *afs_DDirtyVCListPrev = NULL;
32 /*! Locks list of dirty vcaches. */
33 afs_rwlock_t afs_DDirtyVCListLock;
35 extern afs_int32 *afs_dvhashTbl; /*Data cache hash table */
36 extern afs_int32 *afs_dchashTbl; /*Data cache hash table */
37 extern afs_int32 *afs_dvnextTbl; /*Dcache hash table links */
38 extern afs_int32 *afs_dcnextTbl; /*Dcache hash table links */
39 extern struct dcache **afs_indexTable; /*Pointers to dcache entries */
41 /*! Vnode number. On file creation, use the current
42 * value and increment it.
44 afs_uint32 afs_DisconVnode = 2;
46 /*! Conflict policy. */
54 afs_int32 afs_ConflictPolicy = SERVER_WINS;
57 * Find the first dcache of a file that has the specified fid.
58 * Similar to afs_FindDCache, only that it takes a fid instead
59 * of a vcache and it can get the first dcache.
63 * \return The found dcache or NULL.
65 struct dcache *afs_FindDCacheByFid(register struct VenusFid *afid)
67 register afs_int32 i, index;
68 register 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.
101 int afs_GenStoreStatus(struct vcache *avc, struct AFSStoreStatus *astat)
103 if (!avc || !astat || !avc->ddirty_flags)
106 /* Clean up store stat. */
107 memset(astat, 0, sizeof(struct AFSStoreStatus));
109 if (avc->ddirty_flags & VDisconSetTime) {
110 /* Update timestamp. */
111 astat->ClientModTime = avc->m.Date;
112 astat->Mask |= AFS_SETMODTIME;
115 if (avc->ddirty_flags & VDisconSetMode) {
116 /* Copy the mode bits. */
117 astat->UnixModeBits = avc->m.Mode;
118 astat->Mask |= AFS_SETMODE;
121 /* XXX: more to come... ?*/
127 * Hook for filtering the local dir fid by searching the "." entry.
129 * \param hdata The fid to be filled.
131 int get_parent_dir_fid_hook(void *hdata,
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 int afs_GetParentDirFid(struct vcache *avc, struct VenusFid *afid)
158 afid->Cell = avc->fid.Cell;
159 afid->Fid.Volume = avc->fid.Fid.Volume;
161 if (vType(avc) == VREG) {
162 /* Normal files have the dir fid embedded in the vcache. */
163 afid->Fid.Vnode = avc->parentVnode;
164 afid->Fid.Unique = avc->parentUnique;
166 } else if (vType(avc) == VDIR) {
167 /* If dir or parent dir created locally*/
168 tdc = afs_FindDCacheByFid(&avc->fid);
170 /* Lookup each entry for the fid. It should be the first. */
171 afs_dir_EnumerateDir(tdc, &get_parent_dir_fid_hook, afid);
180 struct VenusFid *fid;
186 * Hook that searches a certain fid's name.
188 * \param hdata NameAndFid structure containin a pointer to a fid
189 * and an allocate name. The name will be filled when hit.
191 int get_vnode_name_hook(void *hdata,
196 struct NameAndFid *nf = (struct NameAndFid *) hdata;
198 if ((nf->fid->Fid.Vnode == vnode) &&
199 (nf->fid->Fid.Unique == unique)) {
200 nf->name_len = strlen(aname);
201 memcpy(nf->name, aname, nf->name_len);
202 nf->name[nf->name_len] = 0;
211 * Try to get a vnode's name by comparing all parent dir's entries
212 * to the given fid. It can also return the dir's dcache.
214 * \param avc The file's vcache.
215 * \param afid The parent dir's fid.
216 * \param aname A preallocated string for the name.
217 * \param deleted Has this file been deleted? If yes, use the shadow
218 * dir for looking up the name.
220 int afs_GetVnodeName(struct vcache *avc,
221 struct VenusFid *afid,
227 struct vcache *parent_vc;
228 struct NameAndFid tnf;
229 struct VenusFid parent_fid;
230 struct VenusFid shadow_fid;
232 /* List dir contents and get it's tdc. */
234 /* For deleted files, get the shadow dir's tdc: */
236 /* Get the parent dir's vcache that contains the shadow fid. */
237 parent_fid.Cell = avc->fid.Cell;
238 parent_fid.Fid.Volume = avc->fid.Fid.Volume;
239 if (avc->ddirty_flags & VDisconRename) {
240 /* For renames the old dir fid is needed. */
241 parent_fid.Fid.Vnode = avc->oldVnode;
242 parent_fid.Fid.Unique = avc->oldUnique;
244 parent_fid.Fid.Vnode = afid->Fid.Vnode;
245 parent_fid.Fid.Unique = afid->Fid.Unique;
248 /* Get the parent dir's vcache that contains the shadow fid. */
249 ObtainSharedLock(&afs_xvcache, 755);
250 parent_vc = afs_FindVCache(&parent_fid, 0, 1);
251 ReleaseSharedLock(&afs_xvcache);
256 shadow_fid.Cell = parent_vc->fid.Cell;
257 shadow_fid.Fid.Volume = parent_vc->fid.Fid.Volume;
258 shadow_fid.Fid.Vnode = parent_vc->shVnode;
259 shadow_fid.Fid.Unique = parent_vc->shUnique;
261 afs_PutVCache(parent_vc);
263 /* Get shadow dir's dcache. */
264 tdc = afs_FindDCacheByFid(&shadow_fid);
268 /* For normal files, look into the current dir's entry. */
269 tdc = afs_FindDCacheByFid(afid);
276 afs_dir_EnumerateDir(tdc, &get_vnode_name_hook, &tnf);
285 struct DirtyChildrenCount {
291 * Lookup dirty deleted vnodes in this dir.
293 int chk_del_children_hook(void *hdata,
298 struct VenusFid tfid;
299 struct DirtyChildrenCount *v = (struct DirtyChildrenCount *) hdata;
302 if ((aname[0] == '.') && !aname[1])
303 /* Skip processing this dir again.
304 * It would result in an endless loop.
308 if ((aname[0] == '.') && (aname[1] == '.') && !aname[2])
309 /* Don't process parent dir. */
312 /* Get this file's vcache. */
313 tfid.Cell = v->vc->fid.Cell;
314 tfid.Fid.Volume = v->vc->fid.Fid.Volume;
315 tfid.Fid.Vnode = vnode;
316 tfid.Fid.Unique = unique;
318 ObtainSharedLock(&afs_xvcache, 757);
319 tvc = afs_FindVCache(&tfid, 0, 1);
320 ReleaseSharedLock(&afs_xvcache);
322 /* Count unfinished dirty children. VDisconShadowed can still be set,
323 * because we need it to remove the shadow dir.
326 if (tvc->ddirty_flags)
336 * Check if entries have been deleted in a vnode's shadow
339 * \return Returns the number of dirty children.
341 * \note afs_DDirtyVCListLock must be write locked.
343 int afs_CheckDeletedChildren(struct vcache *avc)
346 struct DirtyChildrenCount dcc;
347 struct VenusFid shadow_fid;
349 if (!(avc->ddirty_flags & VDisconShadowed))
353 shadow_fid.Cell = avc->fid.Cell;
354 shadow_fid.Fid.Volume = avc->fid.Fid.Volume;
355 shadow_fid.Fid.Vnode = avc->shVnode;
356 shadow_fid.Fid.Unique = avc->shUnique;
360 /* Get shadow dir's dcache. */
361 tdc = afs_FindDCacheByFid(&shadow_fid);
364 afs_dir_EnumerateDir(tdc, &chk_del_children_hook, &dcc);
372 * Changes a file's parent fid references.
374 int fix_children_fids_hook(void *hdata,
379 struct VenusFid tfid;
380 struct VenusFid *afid = (struct VenusFid *) hdata;
382 struct dcache *tdc = NULL;
384 if ((aname[0] == '.') && !aname[1])
387 if ((aname[0] == '.') && (aname[1] == '.') && !aname[2])
390 tfid.Cell = afid->Cell;
391 tfid.Fid.Volume = afid->Fid.Volume;
392 tfid.Fid.Vnode = vnode;
393 tfid.Fid.Unique = unique;
396 /* vnode's parity indicates that it's a file. */
398 /* Get the vcache. */
399 ObtainSharedLock(&afs_xvcache, 759);
400 tvc = afs_FindVCache(&tfid, 0, 1);
401 ReleaseSharedLock(&afs_xvcache);
403 /* Change the fields. */
405 tvc->parentVnode = afid->Fid.Vnode;
406 tvc->parentUnique = afid->Fid.Unique;
411 /* It's a dir. Fix this dir's .. entry to contain the new fid. */
412 /* Seek the dir's dcache. */
413 tdc = afs_FindDCacheByFid(&tfid);
415 /* Change the .. entry fid. */
416 afs_dir_ChangeFid(tdc, "..", NULL, &afid->Fid.Vnode);
419 } /* if (!(vnode % 2))*/
425 * Fixes the parentVnode and parentUnique fields of all
426 * files (not dirs) contained in the directory pointed by
427 * old_fid. This is useful on resync, when a locally created dir
428 * get's a new fid and all the children references must be updated
429 * to reflect the new fid.
431 * \note The dir's fid hasn't been changed yet, it is still referenced
434 * \param old_fid The current dir's fid.
435 * \param new_fid The new dir's fid.
437 void afs_FixChildrenFids(struct VenusFid *old_fid, struct VenusFid *new_fid)
441 /* Get shadow dir's dcache. */
442 tdc = afs_FindDCacheByFid(old_fid);
443 /* Change the fids. */
445 afs_dir_EnumerateDir(tdc, &fix_children_fids_hook, new_fid);
450 int list_dir_hook(void *hdata, char *aname, afs_int32 vnode, afs_int32 unique)
452 printf("list_dir_hook: %s v:%u u:%u\n", aname, vnode, unique);
456 void afs_DbgListDirEntries(struct VenusFid *afid)
460 /* Get shadow dir's dcache. */
461 tdc = afs_FindDCacheByFid(afid);
463 afs_dir_EnumerateDir(tdc, &list_dir_hook, NULL);
469 * Handles file renaming on reconnection:
470 * - Get the old name from the old dir's shadow dir.
471 * - Get the new name from the current dir.
472 * - Old dir fid and new dir fid are collected along the way.
474 int afs_ProcessOpRename(struct vcache *avc, struct vrequest *areq)
476 struct VenusFid old_pdir_fid, new_pdir_fid;
477 char *old_name, *new_name;
478 struct AFSFetchStatus OutOldDirStatus, OutNewDirStatus;
479 struct AFSVolSync tsync;
484 /* Get old dir vcache. */
485 old_pdir_fid.Cell = avc->fid.Cell;
486 old_pdir_fid.Fid.Volume = avc->fid.Fid.Volume;
487 old_pdir_fid.Fid.Vnode = avc->oldVnode;
488 old_pdir_fid.Fid.Unique = avc->oldUnique;
491 old_name = (char *) afs_osi_Alloc(AFSNAMEMAX);
493 printf("afs_ProcessOpRename: Couldn't alloc space for old name.\n");
496 code = afs_GetVnodeName(avc, &old_pdir_fid, old_name, 1);
498 printf("afs_ProcessOpRename: Couldn't find old name.\n");
503 /* Alloc data first. */
504 new_name = (char *) afs_osi_Alloc(AFSNAMEMAX);
506 printf("afs_ProcessOpRename: Couldn't alloc space for new name.\n");
511 if (avc->ddirty_flags & VDisconRenameSameDir) {
512 /* If we're in the same dir, don't do the lookups all over again,
513 * just copy fid and vcache from the old dir.
515 memcpy(&new_pdir_fid, &old_pdir_fid, sizeof(struct VenusFid));
517 /* Get parent dir's FID.*/
518 new_pdir_fid.Fid.Unique = 0;
519 afs_GetParentDirFid(avc, &new_pdir_fid);
520 if (!new_pdir_fid.Fid.Unique) {
521 printf("afs_ProcessOpRename: Couldn't find new parent dir FID.\n");
527 /* And finally get the new name. */
528 code = afs_GetVnodeName(avc, &new_pdir_fid, new_name, 0);
530 printf("afs_ProcessOpRename: Couldn't find new name.\n");
535 /* Send to data to server. */
537 tc = afs_Conn(&old_pdir_fid, areq, SHARED_LOCK);
539 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_RENAME);
541 code = RXAFS_Rename(tc->id,
542 (struct AFSFid *)&old_pdir_fid.Fid,
544 (struct AFSFid *)&new_pdir_fid.Fid,
554 } while (afs_Analyze(tc,
558 AFS_STATS_FS_RPCIDX_RENAME,
563 printf("afs_ProcessOpRename: server code=%u\n", code);
565 afs_osi_Free(new_name, AFSNAMEMAX);
567 afs_osi_Free(old_name, AFSNAMEMAX);
572 * Handles all the reconnection details:
573 * - Get all the details about the vnode: name, fid, and parent dir fid.
574 * - Send data to server.
576 * - Reorder vhash and dcaches in their hashes, using the newly acquired fid.
578 int afs_ProcessOpCreate(struct vcache *avc,
579 struct vrequest *areq,
580 struct AFS_UCRED *acred)
583 struct AFSStoreStatus InStatus;
584 struct AFSFetchStatus OutFidStatus, OutDirStatus;
585 struct VenusFid pdir_fid, newFid;
586 struct server *hostp = NULL;
587 struct AFSCallBack CallBack;
588 struct AFSVolSync tsync;
589 struct vcache *tdp = NULL, *tvc = NULL;
590 struct dcache *tdc = NULL;
592 afs_int32 now, hash, new_hash, index;
596 /* Get parent dir's FID. */
597 pdir_fid.Fid.Unique = 0;
598 afs_GetParentDirFid(avc, &pdir_fid);
599 if (!pdir_fid.Fid.Unique) {
600 printf("afs_ProcessOpCreate: Couldn't find parent dir'sFID.\n");
604 tname = afs_osi_Alloc(AFSNAMEMAX);
606 printf("afs_ProcessOpCreate: Couldn't find file name\n");
610 /* Get vnode's name. */
611 code = afs_GetVnodeName(avc, &pdir_fid, tname, 0);
613 printf("afs_ProcessOpCreate: Couldn't find file name\n");
618 /* Get parent dir vcache. */
619 ObtainSharedLock(&afs_xvcache, 760);
620 tdp = afs_FindVCache(&pdir_fid, 0, 1);
621 ReleaseSharedLock(&afs_xvcache);
623 printf("afs_ProcessOpCreate: Couldn't find parent dir's vcache\n");
628 if (tdp->ddirty_flags & VDisconCreate) {
629 /* If the parent dir has been created locally, defer
630 * this vnode for later by moving it to the end.
632 afs_DDirtyVCList->ddirty_next = avc;
633 afs_DDirtyVCList = avc;
634 printf("afs_ProcessOpRemove: deferring this vcache\n");
640 InStatus.Mask = AFS_SETMODTIME | AFS_SETMODE | AFS_SETGROUP;
641 InStatus.ClientModTime = avc->m.Date;
642 InStatus.Owner = avc->m.Owner;
643 InStatus.Group = (afs_int32) acred->cr_gid;
644 /* Only care about protection bits. */
645 InStatus.UnixModeBits = avc->m.Mode & 0xffff;
647 /* Connect to server. */
648 if (vType(avc) == VREG) {
649 /* Make file on server. */
651 tc = afs_Conn(&tdp->fid, areq, SHARED_LOCK);
653 /* Remember for callback processing. */
654 hostp = tc->srvr->server;
656 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_CREATEFILE);
658 code = RXAFS_CreateFile(tc->id,
659 (struct AFSFid *)&tdp->fid.Fid,
662 (struct AFSFid *) &newFid.Fid,
669 CallBack.ExpirationTime += now;
672 } while (afs_Analyze(tc,
676 AFS_STATS_FS_RPCIDX_CREATEFILE,
680 } else if (vType(avc) == VDIR) {
681 /* Make dir on server. */
683 tc = afs_Conn(&tdp->fid, areq, SHARED_LOCK);
685 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_MAKEDIR);
688 code = RXAFS_MakeDir(tc->id,
689 (struct AFSFid *) &tdp->fid.Fid,
692 (struct AFSFid *) &newFid.Fid,
699 CallBack.ExpirationTime += now;
700 /* DON'T forget to set the callback at some point. */
703 } while (afs_Analyze(tc,
707 AFS_STATS_FS_RPCIDX_MAKEDIR,
710 } /* Do server changes. */
712 /* TODO: Handle errors. */
714 printf("afs_ProcessOpCreate: error while creating vnode on server, code=%d .\n", code);
719 /* The rpc doesn't set the cell number. */
720 newFid.Cell = avc->fid.Cell;
723 * Change the fid in the dir entry.
726 /* Seek the dir's dcache. */
727 tdc = afs_FindDCacheByFid(&tdp->fid);
729 /* And now change the fid in the parent dir entry. */
730 afs_dir_ChangeFid(tdc, tname, &avc->fid.Fid.Vnode, &newFid.Fid.Vnode);
734 if (vType(avc) == VDIR) {
735 /* Change fid in the dir for the "." entry. ".." has alredy been
736 * handled by afs_FixChildrenFids when processing the parent dir.
738 tdc = afs_FindDCacheByFid(&avc->fid);
740 afs_dir_ChangeFid(tdc, ".", &avc->fid.Fid.Vnode, &newFid.Fid.Vnode);
742 if (avc->m.LinkCount >= 2)
743 /* For non empty dirs, fix children's parentVnode and parentUnique
746 afs_FixChildrenFids(&avc->fid, &newFid);
752 /* Recompute hash chain positions for vnode and dcaches.
753 * Then change to the new FID.
756 /* The vcache goes first. */
757 ObtainWriteLock(&afs_xvcache, 735);
760 hash = VCHash(&avc->fid);
762 new_hash = VCHash(&newFid);
764 /* Remove hash from old position. */
765 /* XXX: not checking array element contents. It shouldn't be empty.
766 * If it oopses, then something else might be wrong.
768 if (afs_vhashT[hash] == avc) {
769 /* First in hash chain (might be the only one). */
770 afs_vhashT[hash] = avc->hnext;
772 /* More elements in hash chain. */
773 //for (tvc = afs_vhashT[hash]; tdp; tdp = tdp->hnext) {
774 for (tvc = afs_vhashT[hash]; tvc; tvc = tvc->hnext) {
775 if (tvc->hnext == avc) {
776 tvc->hnext = avc->hnext;
780 } /* if (!afs_vhashT[i]->hnext) */
781 QRemove(&afs_vhashTV[hash]);
783 /* Insert hash in new position. */
784 avc->hnext = afs_vhashT[new_hash];
785 afs_vhashT[new_hash] = avc;
786 QAdd(&afs_vhashTV[new_hash], &avc->vhashq);
788 ReleaseWriteLock(&afs_xvcache);
790 /* Do the same thing for all dcaches. */
791 hash = DVHash(&avc->fid);
792 ObtainWriteLock(&afs_xdcache, 743);
793 for (index = afs_dvhashTbl[hash]; index != NULLIDX; index = hash) {
794 hash = afs_dvnextTbl[index];
795 tdc = afs_GetDSlot(index, NULL);
796 ReleaseReadLock(&tdc->tlock);
797 if (afs_indexUnique[index] == avc->fid.Fid.Unique) {
798 if (!FidCmp(&tdc->f.fid, &avc->fid)) {
800 /* Safer but slower. */
801 afs_HashOutDCache(tdc, 0);
803 /* Put dcache in new positions in the dchash and dvhash. */
804 new_hash = DCHash(&newFid, tdc->f.chunk);
805 afs_dcnextTbl[tdc->index] = afs_dchashTbl[new_hash];
806 afs_dchashTbl[new_hash] = tdc->index;
808 new_hash = DVHash(&newFid);
809 afs_dvnextTbl[tdc->index] = afs_dvhashTbl[new_hash];
810 afs_dvhashTbl[new_hash] = tdc->index;
812 afs_indexUnique[tdc->index] = newFid.Fid.Unique;
813 memcpy(&tdc->f.fid, &newFid, sizeof(struct VenusFid));
814 //afs_MaybeWakeupTruncateDaemon();
816 } /* if uniquifier match */
819 } /* for all dcaches in this hash bucket */
820 ReleaseWriteLock(&afs_xdcache);
822 /* Now we can set the new fid. */
823 memcpy(&avc->fid, &newFid, sizeof(struct VenusFid));
826 /* Unset parent dir CStat flag, so it will get refreshed on next
829 ObtainWriteLock(&tdp->lock, 745);
830 tdp->states &= ~CStatd;
831 ReleaseWriteLock(&tdp->lock);
836 afs_osi_Free(tname, AFSNAMEMAX);
841 * Remove a vnode on the server, be it file or directory.
842 * Not much to do here only get the parent dir's fid and call the
845 * \param avc The deleted vcache
848 * \note The vcache refcount should be dropped because it points to
849 * a deleted vnode and has served it's purpouse, but we drop refcount
850 * on shadow dir deletio (we still need it for that).
852 * \note avc must be write locked.
854 int afs_ProcessOpRemove(struct vcache *avc, struct vrequest *areq)
857 struct AFSFetchStatus OutDirStatus;
858 struct VenusFid pdir_fid;
859 struct AFSVolSync tsync;
861 struct vcache *tdp = NULL;
865 /* Get parent dir's FID. */
866 pdir_fid.Fid.Unique = 0;
867 afs_GetParentDirFid(avc, &pdir_fid);
868 if (!pdir_fid.Fid.Unique) {
869 printf("afs_ProcessOpRemove: Couldn't find parent dir's FID.\n");
873 tname = afs_osi_Alloc(AFSNAMEMAX);
875 printf("afs_ProcessOpRemove: Couldn't find file name\n");
880 code = afs_GetVnodeName(avc, &pdir_fid, tname, 1);
882 printf("afs_ProcessOpRemove: Couldn't find file name\n");
887 if ((vType(avc) == VDIR) && (afs_CheckDeletedChildren(avc))) {
888 /* Deleted children of this dir remain unsynchronized.
891 afs_DDirtyVCList->ddirty_next = avc;
892 afs_DDirtyVCList = avc;
897 if (vType(avc) == VREG) {
898 /* Remove file on server. */
900 tc = afs_Conn(&pdir_fid, areq, SHARED_LOCK);
902 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_REMOVEFILE);
904 code = RXAFS_RemoveFile(tc->id,
914 } while (afs_Analyze(tc,
918 AFS_STATS_FS_RPCIDX_REMOVEFILE,
922 } else if (vType(avc) == VDIR) {
923 /* Remove dir on server. */
925 tc = afs_Conn(&pdir_fid, areq, SHARED_LOCK);
927 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_REMOVEDIR);
929 code = RXAFS_RemoveDir(tc->id,
938 } while (afs_Analyze(tc,
942 AFS_STATS_FS_RPCIDX_REMOVEDIR,
946 } /* if (vType(avc) == VREG) */
949 printf("afs_ProcessOpRemove: server returned code=%u\n", code);
951 /* Remove the statd flag from parent dir's vcache. */
952 ObtainSharedLock(&afs_xvcache, 761);
953 tdp = afs_FindVCache(&pdir_fid, 0, 1);
954 ReleaseSharedLock(&afs_xvcache);
956 ObtainWriteLock(&tdp->lock, 746);
957 tdp->states &= ~CStatd;
958 ReleaseWriteLock(&tdp->lock);
962 afs_osi_Free(tname, AFSNAMEMAX);
967 * Send disconnected file changes to the server.
969 * \note Call with vnode locked both locally and on the server.
971 * \param avc Vnode that gets synchronized to the server.
972 * \param areq Used for obtaining a conn struct.
974 * \return 0 for success. On failure, other error codes.
976 int afs_SendChanges(struct vcache *avc, struct vrequest *areq)
979 struct AFSStoreStatus sstat;
980 struct AFSFetchStatus fstat;
981 struct AFSVolSync tsync;
986 /* Start multiplexing dirty operations from ddirty_flags field: */
987 if (avc->ddirty_flags & VDisconSetAttrMask) {
989 /* Turn dirty vc data into a new store status... */
990 if (afs_GenStoreStatus(avc, &sstat) > 0) {
992 tc = afs_Conn(&avc->fid, areq, SHARED_LOCK);
994 /* ... and send it. */
995 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_STORESTATUS);
997 code = RXAFS_StoreStatus(tc->id,
998 (struct AFSFid *) &avc->fid.Fid,
1008 } while (afs_Analyze(tc,
1012 AFS_STATS_FS_RPCIDX_STORESTATUS,
1016 } /* if (afs_GenStoreStatus() > 0)*/
1017 } /* disconnected SETATTR */
1022 if (avc->ddirty_flags &
1026 | VDisconWriteOsiFlush)) {
1030 tc = afs_Conn(&avc->fid, areq, SHARED_LOCK);
1032 /* Set storing flags. XXX: A tad inefficient ... */
1033 if (avc->ddirty_flags & VDisconWriteClose)
1034 flags |= AFS_LASTSTORE;
1035 if (avc->ddirty_flags & VDisconWriteOsiFlush)
1036 flags |= (AFS_SYNC | AFS_LASTSTORE);
1037 if (avc->ddirty_flags & VDisconWriteFlush)
1040 /* Try to send store to server. */
1041 /* XXX: AFS_LASTSTORE for writes? Or just AFS_SYNC for all? */
1042 code = afs_StoreAllSegments(avc, areq, flags);
1046 } while (afs_Analyze(tc,
1050 AFS_STATS_FS_RPCIDX_STOREDATA,
1054 } /* disconnected TRUNC | WRITE */
1060 * All files that have been dirty before disconnection are going to
1061 * be replayed back to the server.
1063 * \param areq Request from the user.
1064 * \param acred User credentials.
1066 * \return If all files synchronized succesfully, return 0, otherwise
1069 * \note For now, it's the request from the PDiscon pioctl.
1072 int afs_ResyncDisconFiles(struct vrequest *areq, struct AFS_UCRED *acred)
1075 struct vcache *tvc, *tmp;
1076 struct AFSFetchStatus fstat;
1077 struct AFSCallBack callback;
1078 struct AFSVolSync tsync;
1079 struct vcache *shList, *shListStart;
1081 int sync_failed = 0;
1084 afs_int32 start = 0;
1086 //AFS_STATCNT(afs_ResyncDisconFiles);
1088 shList = shListStart = NULL;
1090 ObtainReadLock(&afs_DDirtyVCListLock);
1092 tvc = afs_DDirtyVCListStart;
1095 /* Get local write lock. */
1096 ObtainWriteLock(&tvc->lock, 704);
1099 if ((tvc->ddirty_flags & VDisconRemove) &&
1100 (tvc->ddirty_flags & VDisconCreate)) {
1101 /* Data created and deleted locally. The server doesn't
1102 * need to know about this, so we'll just skip this file
1103 * from the dirty list.
1107 } else if (tvc->ddirty_flags & VDisconRemove) {
1108 /* Delete the file on the server and just move on
1109 * to the next file. After all, it has been deleted
1110 * we can't replay any other operation it.
1112 code = afs_ProcessOpRemove(tvc, areq);
1113 if (code == ENOTEMPTY)
1117 } else if (tvc->ddirty_flags & VDisconCreate) {
1118 /* For newly created files, we don't need a server lock. */
1119 code = afs_ProcessOpCreate(tvc, areq, acred);
1120 if (code == ENOTEMPTY)
1126 /* Get server write lock. */
1128 tc = afs_Conn(&tvc->fid, areq, SHARED_LOCK);
1130 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_SETLOCK);
1132 code = RXAFS_SetLock(tc->id,
1133 (struct AFSFid *)&tvc->fid.Fid,
1141 } while (afs_Analyze(tc,
1145 AFS_STATS_FS_RPCIDX_SETLOCK,
1154 if ((tvc->ddirty_flags & VDisconRename) &&
1155 !(tvc->ddirty_flags & VDisconCreate)) {
1156 /* Rename file only if it hasn't been created locally. */
1157 code = afs_ProcessOpRename(tvc, areq);
1162 /* Issue a FetchStatus to get info about DV and callbacks. */
1164 tc = afs_Conn(&tvc->fid, areq, SHARED_LOCK);
1166 tvc->callback = tc->srvr->server;
1168 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_FETCHSTATUS);
1170 code = RXAFS_FetchStatus(tc->id,
1171 (struct AFSFid *)&tvc->fid.Fid,
1180 } while (afs_Analyze(tc,
1184 AFS_STATS_FS_RPCIDX_FETCHSTATUS,
1190 goto unlock_srv_file;
1193 if ((dv_match(tvc, fstat) && (tvc->m.Date == fstat.ServerModTime)) ||
1194 (afs_ConflictPolicy == CLIENT_WINS) ||
1195 (tvc->ddirty_flags & VDisconCreate)) {
1197 * Send changes to the server if there's data version match, or
1198 * client wins policy has been selected or file has been created
1199 * but doesn't have it's the contents on to the server yet.
1202 * XXX: Checking server attr changes by timestamp might not the
1203 * most elegant solution, but it's the most viable one that we could find.
1205 afs_UpdateStatus(tvc, &tvc->fid, areq, &fstat, &callback, start);
1206 code = afs_SendChanges(tvc, areq);
1208 } else if (afs_ConflictPolicy == SERVER_WINS) {
1209 /* DV mismatch, apply collision resolution policy. */
1210 /* Dequeue whatever callback is on top (XXX: propably none). */
1211 ObtainWriteLock(&afs_xcbhash, 706);
1212 afs_DequeueCallback(tvc);
1213 tvc->callback = NULL;
1214 tvc->states &= ~(CStatd | CDirty | CUnique);
1215 ReleaseWriteLock(&afs_xcbhash);
1217 /* Save metadata. File length gets updated as well because we
1218 * just removed CDirty from the avc.
1220 //afs_ProcessFS(tvc, &fstat, areq);
1222 /* Discard this files chunks and remove from current dir. */
1223 afs_TryToSmush(tvc, acred, 1);
1224 osi_dnlc_purgedp(tvc);
1225 if (tvc->linkData && !(tvc->states & CCore)) {
1226 /* Take care of symlinks. */
1227 afs_osi_Free(tvc->linkData, strlen(tvc->linkData) + 1);
1228 tvc->linkData = NULL;
1231 /* Otherwise file content's won't be synchronized. */
1232 tvc->truncPos = AFS_NOTRUNC;
1235 printf("afs_ResyncDisconFiles: no resolution policy selected.\n");
1236 } /* if DV match or client wins policy */
1240 printf("Sync FAILED.\n");
1244 /* Release server write lock. */
1246 tc = afs_Conn(&tvc->fid, areq, SHARED_LOCK);
1248 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_RELEASELOCK);
1250 code = RXAFS_ReleaseLock(tc->id,
1251 (struct AFSFid *) &tvc->fid.Fid,
1257 } while (afs_Analyze(tc,
1261 AFS_STATS_FS_RPCIDX_RELEASELOCK,
1266 /* Pop this dirty vc out. */
1268 tvc = tvc->ddirty_next;
1271 /* Vnode not deferred. Clean it up. */
1273 if (tmp->ddirty_flags == VDisconShadowed) {
1274 /* Dirs that have only the shadow flag set might still
1275 * be used so keep them in a different list, that gets
1276 * deleted after resync is done.
1279 shListStart = shList = tmp;
1281 shList->ddirty_next = tmp;
1284 } else if (tmp->ddirty_flags & VDisconShadowed)
1285 /* We can discard the shadow dir now. */
1286 afs_DeleteShadowDir(tmp);
1288 /* Drop the refcount on this vnode because it's not in the
1293 /* Only if sync was successfull,
1294 * clear flags and dirty references.
1296 tmp->ddirty_next = NULL;
1297 tmp->ddirty_flags = 0;
1301 tmp->ddirty_next = NULL;
1303 } /* if (!defered) */
1305 /* Release local write lock. */
1306 ReleaseWriteLock(&tmp->lock);
1309 /* Delete the rest of shadow dirs. */
1312 ObtainWriteLock(&tvc->lock, 764);
1314 afs_DeleteShadowDir(tvc);
1319 tvc = tvc->ddirty_next;
1320 tmp->ddirty_next = NULL;
1322 ReleaseWriteLock(&tmp->lock);
1325 if (ret_code == 0) {
1326 /* NULLIFY dirty list only if resync complete. */
1327 afs_DDirtyVCListStart = NULL;
1328 afs_DDirtyVCList = NULL;
1330 ReleaseReadLock(&afs_DDirtyVCListLock);
1336 * Print list of disconnected files.
1338 * \note Call with afs_DDirtyVCListLock read locked.
1340 void afs_DbgDisconFiles()
1345 tvc = afs_DDirtyVCListStart;
1346 printf("List of dirty files: \n");
1348 printf("Cell=%u Volume=%u VNode=%u Unique=%u\n",
1350 tvc->fid.Fid.Volume,
1352 tvc->fid.Fid.Unique);
1353 tvc = tvc->ddirty_next;
1356 osi_Panic("afs_DbgDisconFiles: loop in dirty list\n");
1361 * Generate a fake fid for a disconnected shadow dir.
1362 * Similar to afs_GenFakeFid, only that it uses the dhash
1363 * to search for a uniquifier because a shadow dir lives only
1368 * \note Don't forget to fill in afid with Cell and Volume.
1370 void afs_GenShadowFid(struct VenusFid *afid)
1372 afs_uint32 i, index, max_unique = 1;
1373 struct vcache *tvc = NULL;
1375 /* Try generating a fid that isn't used in the vhash. */
1377 afid->Fid.Vnode = afs_DisconVnode + 1;
1380 ObtainWriteLock(&afs_xdcache, 737);
1381 for (index = afs_dvhashTbl[i]; index != NULLIDX; index = i) {
1382 i = afs_dvnextTbl[index];
1383 if (afs_indexUnique[index] > max_unique)
1384 max_unique = afs_indexUnique[index];
1387 ReleaseWriteLock(&afs_xdcache);
1388 afid->Fid.Unique = max_unique + 1;
1389 afs_DisconVnode += 2;
1390 if (!afs_DisconVnode)
1391 afs_DisconVnode = 2;
1393 /* Is this a used vnode? */
1394 ObtainSharedLock(&afs_xvcache, 762);
1395 tvc = afs_FindVCache(afid, 0, 1);
1396 ReleaseSharedLock(&afs_xvcache);
1403 * Generate a fake fid (vnode and uniquifier) for a vcache
1404 * (either dir or normal file). The vnode is generated via
1405 * afs_DisconVNode and the uniquifier by getting the highest
1406 * uniquifier on a hash chain and incrementing it by one.
1408 * \param avc afid The fid structre that will be filled.
1409 * \param avtype Vnode type: VDIR/VREG.
1411 * \note The cell number must be completed somewhere else.
1413 void afs_GenFakeFid(struct VenusFid *afid, afs_uint32 avtype)
1416 afs_uint32 max_unique = 1, i;
1419 afid->Fid.Vnode = afs_DisconVnode + 1;
1420 else if (avtype == VREG)
1421 afid->Fid.Vnode = afs_DisconVnode;
1423 ObtainWriteLock(&afs_xvcache, 736);
1425 for (tvc = afs_vhashT[i]; tvc; tvc = tvc->hnext) {
1426 if (tvc->fid.Fid.Unique > max_unique)
1427 max_unique = tvc->fid.Fid.Unique;
1429 ReleaseWriteLock(&afs_xvcache);
1431 afid->Fid.Unique = max_unique + 1;
1432 afs_DisconVnode += 2;
1433 if (!afs_DisconVnode)
1434 afs_DisconVnode = 2;
1438 * Fill in stats for a newly created file/directory.
1440 * \param adp The parent dir's vcache.
1441 * \param avc The created vnode.
1442 * \param afid The new fid.
1445 * \param file_type Specify if file or directory.
1447 * \note Call with avc write locked.
1449 void afs_GenDisconStatus(
1452 struct VenusFid *afid,
1453 struct vattr *attrs,
1454 struct vrequest *areq,
1457 memcpy(&avc->fid, afid, sizeof(struct VenusFid));
1458 avc->m.Mode = attrs->va_mode;
1460 * avc->m.Owner = attrs->va_uid;
1461 * But now we use the parent dir's ownership,
1462 * there's no other way to get a server owner id.
1463 * XXX: Does it really matter?
1465 avc->m.Group = adp->m.Group;
1466 avc->m.Owner = adp->m.Owner;
1467 hset64(avc->m.DataVersion, 0, 0);
1468 avc->m.Length = attrs->va_size;
1469 avc->m.Date = osi_Time();
1470 if (file_type == VREG) {
1471 vSetType(avc, VREG);
1472 avc->m.Mode |= S_IFREG;
1473 avc->m.LinkCount = 1;
1474 } else if (file_type == VDIR) {
1475 vSetType(avc, VDIR);
1476 avc->m.Mode |= S_IFDIR;
1477 avc->m.LinkCount = 2;
1480 avc->anyAccess = adp->anyAccess;
1481 afs_AddAxs(avc->Access, areq->uid, adp->Access->axess);
1483 avc->callback = NULL;
1484 avc->states |= CStatd;
1485 avc->states &= ~CBulkFetching;