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_GetValidDSlot(index);
76 ReleaseReadLock(&tdc->tlock);
77 if (!FidCmp(&tdc->f.fid, afid)) {
78 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) {
196 struct VenusFid *fid;
202 * Hook that searches a certain fid's name.
204 * \param hdata NameAndFid structure containin a pointer to a fid
205 * and an allocate name. The name will be filled when hit.
208 get_vnode_name_hook(void *hdata, char *aname, afs_int32 vnode,
211 struct NameAndFid *nf = (struct NameAndFid *) hdata;
213 if ((nf->fid->Fid.Vnode == vnode) &&
214 (nf->fid->Fid.Unique == unique)) {
215 nf->name_len = strlen(aname);
216 memcpy(nf->name, aname, nf->name_len);
217 nf->name[nf->name_len] = 0;
226 * Try to get a vnode's name by comparing all parent dir's entries
227 * to the given fid. It can also return the dir's dcache.
229 * \param avc The file's vcache.
230 * \param afid The parent dir's fid.
231 * \param aname A preallocated string for the name.
232 * \param deleted Has this file been deleted? If yes, use the shadow
233 * dir for looking up the name.
236 afs_GetVnodeName(struct vcache *avc, struct VenusFid *afid, char *aname,
241 struct vcache *parent_vc;
242 struct NameAndFid tnf;
243 struct VenusFid parent_fid;
244 struct VenusFid shadow_fid;
246 /* List dir contents and get it's tdc. */
248 /* For deleted files, get the shadow dir's tdc: */
250 /* Get the parent dir's vcache that contains the shadow fid. */
251 parent_fid.Cell = avc->f.fid.Cell;
252 parent_fid.Fid.Volume = avc->f.fid.Fid.Volume;
253 if (avc->f.ddirty_flags & VDisconRename) {
254 /* For renames the old dir fid is needed. */
255 parent_fid.Fid.Vnode = avc->f.oldParent.vnode;
256 parent_fid.Fid.Unique = avc->f.oldParent.unique;
258 parent_fid.Fid.Vnode = afid->Fid.Vnode;
259 parent_fid.Fid.Unique = afid->Fid.Unique;
262 /* Get the parent dir's vcache that contains the shadow fid. */
263 ObtainSharedLock(&afs_xvcache, 755);
264 parent_vc = afs_FindVCache(&parent_fid, 0, 1);
265 ReleaseSharedLock(&afs_xvcache);
270 shadow_fid.Cell = parent_vc->f.fid.Cell;
271 shadow_fid.Fid.Volume = parent_vc->f.fid.Fid.Volume;
272 shadow_fid.Fid.Vnode = parent_vc->f.shadow.vnode;
273 shadow_fid.Fid.Unique = parent_vc->f.shadow.unique;
275 afs_PutVCache(parent_vc);
277 /* Get shadow dir's dcache. */
278 tdc = afs_FindDCacheByFid(&shadow_fid);
282 /* For normal files, look into the current dir's entry. */
283 tdc = afs_FindDCacheByFid(afid);
287 tnf.fid = &avc->f.fid;
290 afs_dir_EnumerateDir(tdc, &get_vnode_name_hook, &tnf);
292 if (tnf.name_len == -1)
295 /* printf("Directory dcache not found!\n"); */
302 struct DirtyChildrenCount {
308 * Lookup dirty deleted vnodes in this dir.
311 chk_del_children_hook(void *hdata, char *aname, afs_int32 vnode,
314 struct VenusFid tfid;
315 struct DirtyChildrenCount *v = (struct DirtyChildrenCount *) hdata;
318 if ((aname[0] == '.') && !aname[1])
319 /* Skip processing this dir again.
320 * It would result in an endless loop.
324 if ((aname[0] == '.') && (aname[1] == '.') && !aname[2])
325 /* Don't process parent dir. */
328 /* Get this file's vcache. */
329 tfid.Cell = v->vc->f.fid.Cell;
330 tfid.Fid.Volume = v->vc->f.fid.Fid.Volume;
331 tfid.Fid.Vnode = vnode;
332 tfid.Fid.Unique = unique;
334 ObtainSharedLock(&afs_xvcache, 757);
335 tvc = afs_FindVCache(&tfid, 0, 1);
336 ReleaseSharedLock(&afs_xvcache);
338 /* Count unfinished dirty children. */
340 ObtainReadLock(&tvc->lock);
341 if (tvc->f.ddirty_flags)
343 ReleaseReadLock(&tvc->lock);
352 * Check if entries have been deleted in a vnode's shadow
355 * \return Returns the number of dirty children.
357 * \note afs_DDirtyVCListLock must be write locked.
360 afs_CheckDeletedChildren(struct vcache *avc)
363 struct DirtyChildrenCount dcc;
364 struct VenusFid shadow_fid;
366 if (!avc->f.shadow.vnode)
370 shadow_fid.Cell = avc->f.fid.Cell;
371 shadow_fid.Fid.Volume = avc->f.fid.Fid.Volume;
372 shadow_fid.Fid.Vnode = avc->f.shadow.vnode;
373 shadow_fid.Fid.Unique = avc->f.shadow.unique;
377 /* Get shadow dir's dcache. */
378 tdc = afs_FindDCacheByFid(&shadow_fid);
381 afs_dir_EnumerateDir(tdc, &chk_del_children_hook, &dcc);
389 * Changes a file's parent fid references.
392 fix_children_fids_hook(void *hdata, char *aname, afs_int32 vnode,
395 struct VenusFid tfid;
396 struct VenusFid *afid = (struct VenusFid *) hdata;
398 struct dcache *tdc = NULL;
400 if ((aname[0] == '.') && !aname[1])
403 if ((aname[0] == '.') && (aname[1] == '.') && !aname[2])
406 tfid.Cell = afid->Cell;
407 tfid.Fid.Volume = afid->Fid.Volume;
408 tfid.Fid.Vnode = vnode;
409 tfid.Fid.Unique = unique;
412 /* vnode's parity indicates that it's a file. */
414 /* Get the vcache. */
415 ObtainSharedLock(&afs_xvcache, 759);
416 tvc = afs_FindVCache(&tfid, 0, 1);
417 ReleaseSharedLock(&afs_xvcache);
419 /* Change the fields. */
421 tvc->f.parent.vnode = afid->Fid.Vnode;
422 tvc->f.parent.unique = afid->Fid.Unique;
427 /* It's a dir. Fix this dir's .. entry to contain the new fid. */
428 /* Seek the dir's dcache. */
429 tdc = afs_FindDCacheByFid(&tfid);
431 /* Change the .. entry fid. */
432 afs_dir_ChangeFid(tdc, "..", NULL, &afid->Fid.Vnode);
435 } /* if (!(vnode % 2))*/
441 * Fixes the parentVnode and parentUnique fields of all
442 * files (not dirs) contained in the directory pointed by
443 * old_fid. This is useful on resync, when a locally created dir
444 * get's a new fid and all the children references must be updated
445 * to reflect the new fid.
447 * \note The dir's fid hasn't been changed yet, it is still referenced
450 * \param old_fid The current dir's fid.
451 * \param new_fid The new dir's fid.
454 afs_FixChildrenFids(struct VenusFid *old_fid, struct VenusFid *new_fid)
458 /* Get shadow dir's dcache. */
459 tdc = afs_FindDCacheByFid(old_fid);
460 /* Change the fids. */
462 afs_dir_EnumerateDir(tdc, &fix_children_fids_hook, new_fid);
468 list_dir_hook(void *hdata, char *aname, afs_int32 vnode, afs_int32 unique)
470 /* printf("list_dir_hook: %s v:%u u:%u\n", aname, vnode, unique); */
475 afs_DbgListDirEntries(struct VenusFid *afid)
479 /* Get shadow dir's dcache. */
480 tdc = afs_FindDCacheByFid(afid);
482 afs_dir_EnumerateDir(tdc, &list_dir_hook, NULL);
488 * Find the parent vcache for a given child
490 * \param avc The vcache whose parent is required
491 * \param afid Fid structure in which parent's fid should be stored
492 * \param aname An AFSNAMEMAX sized buffer to hold the parents name
493 * \param adp A pointer to a struct vcache* which will be set to the
496 * \return An error code. 0 indicates success, EAGAIN that the vnode should
497 * be deferred to later in the resync process
501 afs_GetParentVCache(struct vcache *avc, int deleted, struct VenusFid *afid,
502 char *aname, struct vcache **adp)
508 if (afs_GetParentDirFid(avc, afid)) {
509 /* printf("afs_GetParentVCache: Couldn't find parent dir's FID.\n"); */
513 code = afs_GetVnodeName(avc, afid, aname, deleted);
515 /* printf("afs_GetParentVCache: Couldn't find file name\n"); */
519 ObtainSharedLock(&afs_xvcache, 766);
520 *adp = afs_FindVCache(afid, 0, 1);
521 ReleaseSharedLock(&afs_xvcache);
523 /* printf("afs_GetParentVCache: Couldn't find parent dir's vcache\n"); */
528 if ((*adp)->f.ddirty_flags & VDisconCreate) {
529 /* printf("afs_GetParentVCache: deferring until parent exists\n"); */
544 * Handles file renaming on reconnection:
545 * - Get the old name from the old dir's shadow dir.
546 * - Get the new name from the current dir.
547 * - Old dir fid and new dir fid are collected along the way.
550 afs_ProcessOpRename(struct vcache *avc, struct vrequest *areq)
552 struct VenusFid old_pdir_fid, new_pdir_fid;
553 char *old_name = NULL, *new_name = NULL;
554 struct AFSFetchStatus OutOldDirStatus, OutNewDirStatus;
555 struct AFSVolSync tsync;
557 struct rx_connection *rxconn;
561 /* Get old dir vcache. */
562 old_pdir_fid.Cell = avc->f.fid.Cell;
563 old_pdir_fid.Fid.Volume = avc->f.fid.Fid.Volume;
564 old_pdir_fid.Fid.Vnode = avc->f.oldParent.vnode;
565 old_pdir_fid.Fid.Unique = avc->f.oldParent.unique;
568 old_name = afs_osi_Alloc(AFSNAMEMAX);
570 /* printf("afs_ProcessOpRename: Couldn't alloc space for old name.\n"); */
573 code = afs_GetVnodeName(avc, &old_pdir_fid, old_name, 1);
575 /* printf("afs_ProcessOpRename: Couldn't find old name.\n"); */
579 /* Alloc data first. */
580 new_name = afs_osi_Alloc(AFSNAMEMAX);
582 /* printf("afs_ProcessOpRename: Couldn't alloc space for new name.\n"); */
587 if (avc->f.ddirty_flags & VDisconRenameSameDir) {
588 /* If we're in the same dir, don't do the lookups all over again,
589 * just copy fid and vcache from the old dir.
591 memcpy(&new_pdir_fid, &old_pdir_fid, sizeof(struct VenusFid));
593 /* Get parent dir's FID.*/
594 if (afs_GetParentDirFid(avc, &new_pdir_fid)) {
595 /* printf("afs_ProcessOpRename: Couldn't find new parent dir FID.\n"); */
601 /* And finally get the new name. */
602 code = afs_GetVnodeName(avc, &new_pdir_fid, new_name, 0);
604 /* printf("afs_ProcessOpRename: Couldn't find new name.\n"); */
608 /* Send to data to server. */
610 tc = afs_Conn(&old_pdir_fid, areq, SHARED_LOCK, &rxconn);
612 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_RENAME);
614 code = RXAFS_Rename(rxconn,
615 (struct AFSFid *)&old_pdir_fid.Fid,
617 (struct AFSFid *)&new_pdir_fid.Fid,
627 } while (afs_Analyze(tc,
632 AFS_STATS_FS_RPCIDX_RENAME,
636 /* if (code) printf("afs_ProcessOpRename: server code=%u\n", code); */
639 afs_osi_Free(new_name, AFSNAMEMAX);
641 afs_osi_Free(old_name, AFSNAMEMAX);
646 * Handles all the reconnection details:
647 * - Get all the details about the vnode: name, fid, and parent dir fid.
648 * - Send data to server.
650 * - Reorder vhash and dcaches in their hashes, using the newly acquired fid.
653 afs_ProcessOpCreate(struct vcache *avc, struct vrequest *areq,
656 char *tname = NULL, *ttargetName = NULL;
657 struct AFSStoreStatus InStatus;
658 struct AFSFetchStatus OutFidStatus, OutDirStatus;
659 struct VenusFid pdir_fid, newFid;
660 struct AFSCallBack CallBack;
661 struct AFSVolSync tsync;
662 struct vcache *tdp = NULL, *tvc = NULL;
663 struct dcache *tdc = NULL;
665 struct rx_connection *rxconn;
666 afs_int32 hash, new_hash, index;
671 tname = afs_osi_Alloc(AFSNAMEMAX);
674 memset(&InStatus, 0, sizeof(InStatus));
676 code = afs_GetParentVCache(avc, 0, &pdir_fid, tname, &tdp);
680 /* This data may also be in linkData, but then we have to deal with
681 * the joy of terminating NULLs and . and file modes. So just get
682 * it from the dcache where it won't have been fiddled with.
684 if (vType(avc) == VLNK) {
687 struct osi_file *tfile;
689 tdc = afs_GetDCache(avc, 0, areq, &offset, &tlen, 0);
701 tlen++; /* space for NULL */
702 ttargetName = afs_osi_Alloc(tlen);
708 ObtainReadLock(&tdc->lock);
709 tfile = afs_CFileOpen(&tdc->f.inode);
710 code = afs_CFileRead(tfile, 0, ttargetName, tlen);
711 ttargetName[tlen-1] = '\0';
712 afs_CFileClose(tfile);
713 ReleaseReadLock(&tdc->lock);
718 InStatus.Mask = AFS_SETMODTIME | AFS_SETMODE | AFS_SETGROUP;
719 InStatus.ClientModTime = avc->f.m.Date;
720 InStatus.Owner = avc->f.m.Owner;
721 InStatus.Group = (afs_int32) afs_cr_gid(acred);
722 /* Only care about protection bits. */
723 InStatus.UnixModeBits = avc->f.m.Mode & 0xffff;
726 tc = afs_Conn(&tdp->f.fid, areq, SHARED_LOCK, &rxconn);
728 switch (vType(avc)) {
730 /* Make file on server. */
731 op = AFS_STATS_FS_RPCIDX_CREATEFILE;
732 XSTATS_START_TIME(op);
734 code = RXAFS_CreateFile(rxconn,
735 (struct AFSFid *)&tdp->f.fid.Fid,
737 (struct AFSFid *) &newFid.Fid,
738 &OutFidStatus, &OutDirStatus,
744 /* Make dir on server. */
745 op = AFS_STATS_FS_RPCIDX_MAKEDIR;
746 XSTATS_START_TIME(op);
748 code = RXAFS_MakeDir(rxconn, (struct AFSFid *) &tdp->f.fid.Fid,
750 (struct AFSFid *) &newFid.Fid,
751 &OutFidStatus, &OutDirStatus,
757 /* Make symlink on server. */
758 op = AFS_STATS_FS_RPCIDX_SYMLINK;
759 XSTATS_START_TIME(op);
761 code = RXAFS_Symlink(rxconn,
762 (struct AFSFid *) &tdp->f.fid.Fid,
763 tname, ttargetName, &InStatus,
764 (struct AFSFid *) &newFid.Fid,
765 &OutFidStatus, &OutDirStatus, &tsync);
770 op = AFS_STATS_FS_RPCIDX_CREATEFILE;
776 } while (afs_Analyze(tc, rxconn, code, &tdp->f.fid, areq, op, SHARED_LOCK, NULL));
778 /* TODO: Handle errors. */
780 /* printf("afs_ProcessOpCreate: error while creating vnode on server, code=%d .\n", code); */
784 /* The rpc doesn't set the cell number. */
785 newFid.Cell = avc->f.fid.Cell;
788 * Change the fid in the dir entry.
791 /* Seek the dir's dcache. */
792 tdc = afs_FindDCacheByFid(&tdp->f.fid);
794 /* And now change the fid in the parent dir entry. */
795 afs_dir_ChangeFid(tdc, tname, &avc->f.fid.Fid.Vnode, &newFid.Fid.Vnode);
799 if (vType(avc) == VDIR) {
800 /* Change fid in the dir for the "." entry. ".." has alredy been
801 * handled by afs_FixChildrenFids when processing the parent dir.
803 tdc = afs_FindDCacheByFid(&avc->f.fid);
805 afs_dir_ChangeFid(tdc, ".", &avc->f.fid.Fid.Vnode,
808 if (avc->f.m.LinkCount >= 2)
809 /* For non empty dirs, fix children's parentVnode and
810 * parentUnique reference.
812 afs_FixChildrenFids(&avc->f.fid, &newFid);
818 /* Recompute hash chain positions for vnode and dcaches.
819 * Then change to the new FID.
822 /* The vcache goes first. */
823 ObtainWriteLock(&afs_xvcache, 735);
826 hash = VCHash(&avc->f.fid);
828 new_hash = VCHash(&newFid);
830 /* Remove hash from old position. */
831 /* XXX: not checking array element contents. It shouldn't be empty.
832 * If it oopses, then something else might be wrong.
834 if (afs_vhashT[hash] == avc) {
835 /* First in hash chain (might be the only one). */
836 afs_vhashT[hash] = avc->hnext;
838 /* More elements in hash chain. */
839 for (tvc = afs_vhashT[hash]; tvc; tvc = tvc->hnext) {
840 if (tvc->hnext == avc) {
841 tvc->hnext = avc->hnext;
845 } /* if (!afs_vhashT[i]->hnext) */
846 QRemove(&avc->vhashq);
848 /* Insert hash in new position. */
849 avc->hnext = afs_vhashT[new_hash];
850 afs_vhashT[new_hash] = avc;
851 QAdd(&afs_vhashTV[VCHashV(&newFid)], &avc->vhashq);
853 ReleaseWriteLock(&afs_xvcache);
855 /* Do the same thing for all dcaches. */
856 hash = DVHash(&avc->f.fid);
857 ObtainWriteLock(&afs_xdcache, 743);
858 for (index = afs_dvhashTbl[hash]; index != NULLIDX; index = hash) {
859 hash = afs_dvnextTbl[index];
860 tdc = afs_GetValidDSlot(index);
861 ReleaseReadLock(&tdc->tlock);
862 if (afs_indexUnique[index] == avc->f.fid.Fid.Unique) {
863 if (!FidCmp(&tdc->f.fid, &avc->f.fid)) {
865 /* Safer but slower. */
866 afs_HashOutDCache(tdc, 0);
868 /* Put dcache in new positions in the dchash and dvhash. */
869 new_hash = DCHash(&newFid, tdc->f.chunk);
870 afs_dcnextTbl[tdc->index] = afs_dchashTbl[new_hash];
871 afs_dchashTbl[new_hash] = tdc->index;
873 new_hash = DVHash(&newFid);
874 afs_dvnextTbl[tdc->index] = afs_dvhashTbl[new_hash];
875 afs_dvhashTbl[new_hash] = tdc->index;
877 afs_indexUnique[tdc->index] = newFid.Fid.Unique;
878 memcpy(&tdc->f.fid, &newFid, sizeof(struct VenusFid));
880 } /* if uniquifier match */
883 } /* for all dcaches in this hash bucket */
884 ReleaseWriteLock(&afs_xdcache);
886 /* Now we can set the new fid. */
887 memcpy(&avc->f.fid, &newFid, sizeof(struct VenusFid));
892 afs_osi_Free(tname, AFSNAMEMAX);
894 afs_osi_Free(ttargetName, tlen);
899 * Remove a vnode on the server, be it file or directory.
900 * Not much to do here only get the parent dir's fid and call the
903 * \param avc The deleted vcache
906 * \note The vcache refcount should be dropped because it points to
907 * a deleted vnode and has served it's purpouse, but we drop refcount
908 * on shadow dir deletio (we still need it for that).
910 * \note avc must be write locked.
913 afs_ProcessOpRemove(struct vcache *avc, struct vrequest *areq)
916 struct AFSFetchStatus OutDirStatus;
917 struct VenusFid pdir_fid;
918 struct AFSVolSync tsync;
920 struct rx_connection *rxconn;
921 struct vcache *tdp = NULL;
925 tname = afs_osi_Alloc(AFSNAMEMAX);
927 /* printf("afs_ProcessOpRemove: Couldn't alloc space for file name\n"); */
931 code = afs_GetParentVCache(avc, 1, &pdir_fid, tname, &tdp);
935 if ((vType(avc) == VDIR) && (afs_CheckDeletedChildren(avc))) {
936 /* Deleted children of this dir remain unsynchronized.
943 if (vType(avc) == VREG || vType(avc) == VLNK) {
944 /* Remove file on server. */
946 tc = afs_Conn(&pdir_fid, areq, SHARED_LOCK, &rxconn);
948 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_REMOVEFILE);
950 code = RXAFS_RemoveFile(rxconn,
960 } while (afs_Analyze(tc,
965 AFS_STATS_FS_RPCIDX_REMOVEFILE,
969 } else if (vType(avc) == VDIR) {
970 /* Remove dir on server. */
972 tc = afs_Conn(&pdir_fid, areq, SHARED_LOCK, &rxconn);
974 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_REMOVEDIR);
976 code = RXAFS_RemoveDir(rxconn,
985 } while (afs_Analyze(tc,
990 AFS_STATS_FS_RPCIDX_REMOVEDIR,
994 } /* if (vType(avc) == VREG) */
996 /* if (code) printf("afs_ProcessOpRemove: server returned code=%u\n", code); */
999 afs_osi_Free(tname, AFSNAMEMAX);
1004 * Send disconnected file changes to the server.
1006 * \note Call with vnode locked both locally and on the server.
1008 * \param avc Vnode that gets synchronized to the server.
1009 * \param areq Used for obtaining a conn struct.
1011 * \return 0 for success. On failure, other error codes.
1014 afs_SendChanges(struct vcache *avc, struct vrequest *areq)
1016 struct afs_conn *tc;
1017 struct rx_connection *rxconn;
1018 struct AFSStoreStatus sstat;
1019 struct AFSFetchStatus fstat;
1020 struct AFSVolSync tsync;
1025 /* Start multiplexing dirty operations from ddirty_flags field: */
1026 if (avc->f.ddirty_flags & VDisconSetAttrMask) {
1028 /* Turn dirty vc data into a new store status... */
1029 if (afs_GenStoreStatus(avc, &sstat) > 0) {
1031 tc = afs_Conn(&avc->f.fid, areq, SHARED_LOCK, &rxconn);
1033 /* ... and send it. */
1034 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_STORESTATUS);
1036 code = RXAFS_StoreStatus(rxconn,
1037 (struct AFSFid *) &avc->f.fid.Fid,
1047 } while (afs_Analyze(tc,
1052 AFS_STATS_FS_RPCIDX_STORESTATUS,
1056 } /* if (afs_GenStoreStatus() > 0)*/
1057 } /* disconnected SETATTR */
1062 if (avc->f.ddirty_flags &
1066 | VDisconWriteOsiFlush)) {
1070 tc = afs_Conn(&avc->f.fid, areq, SHARED_LOCK, &rxconn);
1072 /* Set storing flags. XXX: A tad inefficient ... */
1073 if (avc->f.ddirty_flags & VDisconWriteClose)
1074 flags |= AFS_LASTSTORE;
1075 if (avc->f.ddirty_flags & VDisconWriteOsiFlush)
1076 flags |= (AFS_SYNC | AFS_LASTSTORE);
1077 if (avc->f.ddirty_flags & VDisconWriteFlush)
1080 /* Try to send store to server. */
1081 /* XXX: AFS_LASTSTORE for writes? Or just AFS_SYNC for all? */
1082 code = afs_StoreAllSegments(avc, areq, flags);
1086 } while (afs_Analyze(tc,
1091 AFS_STATS_FS_RPCIDX_STOREDATA,
1095 } /* disconnected TRUNC | WRITE */
1101 * All files that have been dirty before disconnection are going to
1102 * be replayed back to the server.
1104 * \param areq Request from the user.
1105 * \param acred User credentials.
1107 * \return If all files synchronized succesfully, return 0, otherwise
1110 * \note For now, it's the request from the PDiscon pioctl.
1114 afs_ResyncDisconFiles(struct vrequest *areq, afs_ucred_t *acred)
1116 struct afs_conn *tc;
1117 struct rx_connection *rxconn;
1119 struct AFSFetchStatus fstat;
1120 struct AFSCallBack callback;
1121 struct AFSVolSync tsync;
1123 afs_int32 start = 0;
1125 /*AFS_STATCNT(afs_ResyncDisconFiles);*/
1127 ObtainWriteLock(&afs_disconDirtyLock, 707);
1129 while (!QEmpty(&afs_disconDirty)) {
1130 tvc = QEntry(QPrev(&afs_disconDirty), struct vcache, dirtyq);
1132 /* Can't lock tvc whilst holding the discon dirty lock */
1133 ReleaseWriteLock(&afs_disconDirtyLock);
1135 /* Get local write lock. */
1136 ObtainWriteLock(&tvc->lock, 705);
1138 if (tvc->f.ddirty_flags & VDisconRemove) {
1139 /* Delete the file on the server and just move on
1140 * to the next file. After all, it has been deleted
1141 * we can't replay any other operation it.
1143 code = afs_ProcessOpRemove(tvc, areq);
1146 } else if (tvc->f.ddirty_flags & VDisconCreate) {
1147 /* For newly created files, we don't need a server lock. */
1148 code = afs_ProcessOpCreate(tvc, areq, acred);
1152 tvc->f.ddirty_flags &= ~VDisconCreate;
1153 tvc->f.ddirty_flags |= VDisconCreated;
1156 /* Get server write lock. */
1158 tc = afs_Conn(&tvc->f.fid, areq, SHARED_LOCK, &rxconn);
1160 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_SETLOCK);
1162 code = RXAFS_SetLock(rxconn,
1163 (struct AFSFid *)&tvc->f.fid.Fid,
1171 } while (afs_Analyze(tc,
1176 AFS_STATS_FS_RPCIDX_SETLOCK,
1183 if (tvc->f.ddirty_flags & VDisconRename) {
1184 /* If we're renaming the file, do so now */
1185 code = afs_ProcessOpRename(tvc, areq);
1187 goto unlock_srv_file;
1190 /* Issue a FetchStatus to get info about DV and callbacks. */
1192 tc = afs_Conn(&tvc->f.fid, areq, SHARED_LOCK, &rxconn);
1194 tvc->callback = tc->parent->srvr->server;
1196 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_FETCHSTATUS);
1198 code = RXAFS_FetchStatus(rxconn,
1199 (struct AFSFid *)&tvc->f.fid.Fid,
1208 } while (afs_Analyze(tc,
1213 AFS_STATS_FS_RPCIDX_FETCHSTATUS,
1218 goto unlock_srv_file;
1221 if ((dv_match(tvc, fstat) && (tvc->f.m.Date == fstat.ServerModTime)) ||
1222 (afs_ConflictPolicy == CLIENT_WINS) ||
1223 (tvc->f.ddirty_flags & VDisconCreated)) {
1225 * Send changes to the server if there's data version match, or
1226 * client wins policy has been selected or file has been created
1227 * but doesn't have it's the contents on to the server yet.
1230 * XXX: Checking server attr changes by timestamp might not the
1231 * most elegant solution, but it's the most viable one that we could find.
1233 afs_UpdateStatus(tvc, &tvc->f.fid, areq, &fstat, &callback, start);
1234 code = afs_SendChanges(tvc, areq);
1236 } else if (afs_ConflictPolicy == SERVER_WINS) {
1237 /* DV mismatch, apply collision resolution policy. */
1238 /* Discard this files chunks and remove from current dir. */
1239 afs_ResetVCache(tvc, acred, 0);
1240 tvc->f.truncPos = AFS_NOTRUNC;
1242 /* printf("afs_ResyncDisconFiles: no resolution policy selected.\n"); */
1243 } /* if DV match or client wins policy */
1246 /* Release server write lock. */
1249 tc = afs_Conn(&tvc->f.fid, areq, SHARED_LOCK, &rxconn);
1251 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_RELEASELOCK);
1253 ucode = RXAFS_ReleaseLock(rxconn,
1254 (struct AFSFid *) &tvc->f.fid.Fid,
1260 } while (afs_Analyze(tc,
1265 AFS_STATS_FS_RPCIDX_RELEASELOCK,
1270 ObtainWriteLock(&afs_disconDirtyLock, 710);
1272 /* Replayed successfully - pull the vcache from the
1273 * disconnected list */
1274 tvc->f.ddirty_flags = 0;
1275 QRemove(&tvc->dirtyq);
1278 if (code == EAGAIN) {
1279 /* Operation was deferred. Pull it from the current place in
1280 * the list, and stick it at the end again */
1281 QRemove(&tvc->dirtyq);
1282 QAdd(&afs_disconDirty, &tvc->dirtyq);
1284 /* Failed - keep state as is, and let the user know we died */
1286 ReleaseWriteLock(&tvc->lock);
1291 /* Release local write lock. */
1292 ReleaseWriteLock(&tvc->lock);
1296 ReleaseWriteLock(&afs_disconDirtyLock);
1300 /* Dispose of all of the shadow directories */
1301 afs_DisconDiscardAllShadows(0, acred);
1303 ReleaseWriteLock(&afs_disconDirtyLock);
1308 * Discard all of our shadow directory copies. If squash is true, then
1309 * we also invalidate the vcache holding the shadow directory, to ensure
1310 * that any disconnected changes are deleted
1315 * \note afs_disconDirtyLock must be held on entry. It will be released
1320 afs_DisconDiscardAllShadows(int squash, afs_ucred_t *acred)
1324 while (!QEmpty(&afs_disconShadow)) {
1325 tvc = QEntry(QNext(&afs_disconShadow), struct vcache, shadowq);
1327 /* Must release the dirty lock to be able to get a vcache lock */
1328 ReleaseWriteLock(&afs_disconDirtyLock);
1329 ObtainWriteLock(&tvc->lock, 706);
1332 afs_ResetVCache(tvc, acred, 0);
1334 afs_DeleteShadowDir(tvc);
1336 ReleaseWriteLock(&tvc->lock);
1337 ObtainWriteLock(&afs_disconDirtyLock, 709);
1342 * This function throws away the whole disconnected state, allowing
1343 * the cache manager to reconnect to a server if we get into a state
1344 * where reconiliation is impossible.
1350 afs_DisconDiscardAll(afs_ucred_t *acred)
1354 ObtainWriteLock(&afs_disconDirtyLock, 717);
1355 while (!QEmpty(&afs_disconDirty)) {
1356 tvc = QEntry(QPrev(&afs_disconDirty), struct vcache, dirtyq);
1357 QRemove(&tvc->dirtyq);
1358 ReleaseWriteLock(&afs_disconDirtyLock);
1360 ObtainWriteLock(&tvc->lock, 718);
1361 afs_ResetVCache(tvc, acred, 0);
1362 tvc->f.truncPos = AFS_NOTRUNC;
1363 ReleaseWriteLock(&tvc->lock);
1364 ObtainWriteLock(&afs_disconDirtyLock, 719);
1368 afs_DisconDiscardAllShadows(1, acred);
1370 ReleaseWriteLock(&afs_disconDirtyLock);
1374 * Print list of disconnected files.
1376 * \note Call with afs_DDirtyVCListLock read locked.
1379 afs_DbgDisconFiles(void)
1385 afs_warn("List of dirty files: \n");
1387 ObtainReadLock(&afs_disconDirtyLock);
1388 for (q = QPrev(&afs_disconDirty); q != &afs_disconDirty; q = QPrev(q)) {
1389 tvc = QEntry(q, struct vcache, dirtyq);
1391 afs_warn("Cell=%u Volume=%u VNode=%u Unique=%u\n",
1393 tvc->f.fid.Fid.Volume,
1394 tvc->f.fid.Fid.Vnode,
1395 tvc->f.fid.Fid.Unique);
1399 osi_Panic("afs_DbgDisconFiles: loop in dirty list\n");
1401 ReleaseReadLock(&afs_disconDirtyLock);
1405 * Generate a fake fid for a disconnected shadow dir.
1406 * Similar to afs_GenFakeFid, only that it uses the dhash
1407 * to search for a uniquifier because a shadow dir lives only
1412 * \note Don't forget to fill in afid with Cell and Volume.
1415 afs_GenShadowFid(struct VenusFid *afid)
1417 afs_uint32 i, index, max_unique = 1;
1418 struct vcache *tvc = NULL;
1420 /* Try generating a fid that isn't used in the vhash. */
1422 /* Shadow Fids are always directories */
1423 afid->Fid.Vnode = afs_DisconVnode + 1;
1426 ObtainWriteLock(&afs_xdcache, 737);
1427 for (index = afs_dvhashTbl[i]; index != NULLIDX; index = i) {
1428 i = afs_dvnextTbl[index];
1429 if (afs_indexUnique[index] > max_unique)
1430 max_unique = afs_indexUnique[index];
1433 ReleaseWriteLock(&afs_xdcache);
1434 afid->Fid.Unique = max_unique + 1;
1435 afs_DisconVnode += 2;
1436 if (!afs_DisconVnode)
1437 afs_DisconVnode = 2;
1439 /* Is this a used vnode? */
1440 ObtainSharedLock(&afs_xvcache, 762);
1441 tvc = afs_FindVCache(afid, 0, 1);
1442 ReleaseSharedLock(&afs_xvcache);
1449 * Generate a fake fid (vnode and uniquifier) for a vcache
1450 * (either dir or normal file). The vnode is generated via
1451 * afs_DisconVNode and the uniquifier by getting the highest
1452 * uniquifier on a hash chain and incrementing it by one.
1454 * \param afid The fid structre that will be filled.
1455 * \param avtype Vnode type: VDIR/VREG.
1456 * \param lock True indicates that xvcache may be obtained,
1457 * False that it is already held
1459 * \note The cell number must be completed somewhere else.
1462 afs_GenFakeFid(struct VenusFid *afid, afs_uint32 avtype, int lock)
1465 afs_uint32 max_unique = 0, i;
1469 afid->Fid.Vnode = afs_DisconVnode + 1;
1473 afid->Fid.Vnode = afs_DisconVnode;
1478 ObtainWriteLock(&afs_xvcache, 736);
1480 for (tvc = afs_vhashT[i]; tvc; tvc = tvc->hnext) {
1481 if (tvc->f.fid.Fid.Unique > max_unique)
1482 max_unique = tvc->f.fid.Fid.Unique;
1485 ReleaseWriteLock(&afs_xvcache);
1487 afid->Fid.Unique = max_unique + 1;
1488 afs_DisconVnode += 2;
1489 if (!afs_DisconVnode)
1490 afs_DisconVnode = 2;
1494 * Fill in stats for a newly created file/directory.
1496 * \param adp The parent dir's vcache.
1497 * \param avc The created vnode.
1498 * \param afid The new fid.
1501 * \param file_type Specify if file or directory.
1503 * \note Call with avc write locked.
1506 afs_GenDisconStatus(struct vcache *adp, struct vcache *avc,
1507 struct VenusFid *afid, struct vattr *attrs,
1508 struct vrequest *areq, int file_type)
1511 memcpy(&avc->f.fid, afid, sizeof(struct VenusFid));
1512 avc->f.m.Mode = attrs->va_mode;
1514 * avc->f.m.Owner = attrs->va_uid;
1515 * But now we use the parent dir's ownership,
1516 * there's no other way to get a server owner id.
1517 * XXX: Does it really matter?
1519 avc->f.m.Group = adp->f.m.Group;
1520 avc->f.m.Owner = adp->f.m.Owner;
1522 afs_SetDataVersion(avc, &zero);
1523 avc->f.m.Length = attrs->va_size;
1524 avc->f.m.Date = osi_Time();
1527 vSetType(avc, VREG);
1528 avc->f.m.Mode |= S_IFREG;
1529 avc->f.m.LinkCount = 1;
1530 avc->f.parent.vnode = adp->f.fid.Fid.Vnode;
1531 avc->f.parent.unique = adp->f.fid.Fid.Unique;
1534 vSetType(avc, VDIR);
1535 avc->f.m.Mode |= S_IFDIR;
1536 avc->f.m.LinkCount = 2;
1539 vSetType(avc, VLNK);
1540 avc->f.m.Mode |= S_IFLNK;
1541 if ((avc->f.m.Mode & 0111) == 0)
1542 avc->mvstat = AFS_MVSTAT_MTPT;
1543 avc->f.parent.vnode = adp->f.fid.Fid.Vnode;
1544 avc->f.parent.unique = adp->f.fid.Fid.Unique;
1549 avc->f.anyAccess = adp->f.anyAccess;
1550 afs_AddAxs(avc->Access, areq->uid, adp->Access->axess);
1552 avc->callback = NULL;
1553 avc->f.states |= CStatd;
1554 avc->f.states &= ~CBulkFetching;