linux-truncate-race-20090109
[openafs.git] / src / afs / VNOPS / afs_vnop_attrs.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  *
9  * Portions Copyright (c) 2003 Apple Computer, Inc.
10  */
11
12 /*
13  * afs_vnop_attrs.c - setattr and getattr vnodeops
14  *
15  * Implements:
16  * afs_CopyOutAttrs
17  * afs_getattr
18  * afs_VAttrToAS
19  * afs_setattr
20  *
21  */
22
23 #include <afsconfig.h>
24 #include "afs/param.h"
25
26 RCSID
27     ("$Header$");
28
29 #include "afs/sysincludes.h"    /* Standard vendor system headers */
30 #include "afsincludes.h"        /* Afs-based standard headers */
31 #include "afs/afs_stats.h"      /* statistics */
32 #include "afs/afs_cbqueue.h"
33 #include "afs/nfsclient.h"
34 #include "afs/afs_osidnlc.h"
35
36 extern afs_rwlock_t afs_xcbhash;
37 struct afs_exporter *afs_nfsexporter;
38 extern struct vcache *afs_globalVp;
39 #if defined(AFS_HPUX110_ENV)
40 extern struct vfs *afs_globalVFS;
41 #endif
42
43
44 /* copy out attributes from cache entry */
45 int
46 afs_CopyOutAttrs(register struct vcache *avc, register struct vattr *attrs)
47 {
48     register struct volume *tvp;
49     register struct cell *tcell;
50     int fakedir = 0;
51
52     AFS_STATCNT(afs_CopyOutAttrs);
53     if (afs_fakestat_enable && avc->mvstat == 1)
54         fakedir = 1;
55     attrs->va_type = fakedir ? VDIR : vType(avc);
56 #if defined(AFS_SGI_ENV) || defined(AFS_AIX32_ENV) || defined(AFS_SUN5_ENV)
57     attrs->va_mode = fakedir ? 0755 : (mode_t) (avc->m.Mode & 0xffff);
58 #else
59     attrs->va_mode = fakedir ? VDIR | 0755 : avc->m.Mode;
60 #endif
61
62     if (avc->m.Mode & (VSUID | VSGID)) {
63         /* setuid or setgid, make sure we're allowed to run them from this cell */
64         tcell = afs_GetCell(avc->fid.Cell, 0);
65         if (tcell && (tcell->states & CNoSUID))
66             attrs->va_mode &= ~(VSUID | VSGID);
67     }
68 #if defined(AFS_DARWIN_ENV)
69     {
70         extern u_int32_t afs_darwin_realmodes;
71         if (!afs_darwin_realmodes) {
72             /* Mac OS X uses the mode bits to determine whether a file or
73              * directory is accessible, and believes them, even though under
74              * AFS they're almost assuredly wrong, especially if the local uid
75              * does not match the AFS ID.  So we set the mode bits
76              * conservatively.
77              */
78             if (S_ISDIR(attrs->va_mode)) {
79                 /* all access bits need to be set for directories, since even
80                  * a mode 0 directory can still be used normally.
81                  */
82                 attrs->va_mode |= ACCESSPERMS;
83             } else {
84                 /* for other files, replicate the user bits to group and other */
85                 mode_t ubits = (attrs->va_mode & S_IRWXU) >> 6;
86                 attrs->va_mode |= ubits | (ubits << 3);
87             }
88         }
89     }
90 #endif /* AFS_DARWIN_ENV */
91     attrs->va_uid = fakedir ? 0 : avc->m.Owner;
92     attrs->va_gid = fakedir ? 0 : avc->m.Group; /* yeah! */
93 #if defined(AFS_SUN56_ENV)
94     attrs->va_fsid = avc->v.v_vfsp->vfs_fsid.val[0];
95 #elif defined(AFS_OSF_ENV)
96     attrs->va_fsid = avc->v.v_mount->m_stat.f_fsid.val[0];
97 #elif defined(AFS_DARWIN80_ENV)
98     VATTR_RETURN(attrs, va_fsid, vfs_statfs(vnode_mount(AFSTOV(avc)))->f_fsid.val[0]);
99 #elif defined(AFS_DARWIN70_ENV)
100     attrs->va_fsid = avc->v->v_mount->mnt_stat.f_fsid.val[0];
101 #else /* ! AFS_DARWIN70_ENV */
102     attrs->va_fsid = 1;
103 #endif 
104     if (avc->mvstat == 2) {
105         tvp = afs_GetVolume(&avc->fid, 0, READ_LOCK);
106         /* The mount point's vnode. */
107         if (tvp) {
108             attrs->va_nodeid =
109               afs_calc_inum (tvp->mtpoint.Fid.Volume,
110                               tvp->mtpoint.Fid.Vnode);
111             if (FidCmp(&afs_rootFid, &avc->fid) && !attrs->va_nodeid)
112                 attrs->va_nodeid = 2;
113             afs_PutVolume(tvp, READ_LOCK);
114         } else
115             attrs->va_nodeid = 2;
116     } else
117         attrs->va_nodeid = 
118               afs_calc_inum (avc->fid.Fid.Volume,
119                               avc->fid.Fid.Vnode);
120     attrs->va_nodeid &= 0x7fffffff;     /* Saber C hates negative inode #s! */
121     attrs->va_nlink = fakedir ? 100 : avc->m.LinkCount;
122     attrs->va_size = fakedir ? 4096 : avc->m.Length;
123     attrs->va_atime.tv_sec = attrs->va_mtime.tv_sec = attrs->va_ctime.tv_sec =
124         fakedir ? 0 : (int)avc->m.Date;
125     /* set microseconds to be dataversion # so that we approximate NFS-style
126      * use of mtime as a dataversion #.  We take it mod 512K because
127      * microseconds *must* be less than a million, and 512K is the biggest
128      * power of 2 less than such.  DataVersions are typically pretty small
129      * anyway, so the difference between 512K and 1000000 shouldn't matter
130      * much, and "&" is a lot faster than "%".
131      */
132 #if defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
133     /* nfs on these systems puts an 0 in nsec and stores the nfs usec (aka 
134      * dataversion) in va_gen */
135
136     attrs->va_atime.tv_nsec = attrs->va_mtime.tv_nsec =
137         attrs->va_ctime.tv_nsec = 0;
138     attrs->va_gen = hgetlo(avc->m.DataVersion);
139 #elif defined(AFS_SGI_ENV) || defined(AFS_SUN5_ENV) || defined(AFS_AIX41_ENV) || defined(AFS_OBSD_ENV)
140     attrs->va_atime.tv_nsec = attrs->va_mtime.tv_nsec =
141         attrs->va_ctime.tv_nsec =
142         (hgetlo(avc->m.DataVersion) & 0x7ffff) * 1000;
143 #else
144     attrs->va_atime.tv_usec = attrs->va_mtime.tv_usec =
145         attrs->va_ctime.tv_usec = (hgetlo(avc->m.DataVersion) & 0x7ffff);
146 #endif
147 #if defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV) || defined(AFS_OSF_ENV)
148     attrs->va_flags = 0;
149 #endif
150 #if defined(AFS_SGI_ENV) || defined(AFS_SUN5_ENV)
151     attrs->va_blksize = AFS_BLKSIZE;    /* XXX Was 8192 XXX */
152 #else
153     attrs->va_blocksize = AFS_BLKSIZE;  /* XXX Was 8192 XXX */
154 #endif
155     attrs->va_rdev = 1;
156 #if defined(AFS_HPUX110_ENV)
157     if (afs_globalVFS)
158         attrs->va_fstype = afs_globalVFS->vfs_mtype;
159 #endif
160
161     /*
162      * Below return 0 (and not 1) blocks if the file is zero length. This conforms
163      * better with the other filesystems that do return 0.      
164      */
165 #if defined(AFS_OSF_ENV) || defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
166     attrs->va_bytes = (attrs->va_size ? (attrs->va_size + 1023) : 1024);
167 #ifdef  va_bytes_rsv
168     attrs->va_bytes_rsv = -1;
169 #endif
170 #elif defined(AFS_HPUX_ENV)
171     attrs->va_blocks = (attrs->va_size ? ((attrs->va_size + 1023)>>10) : 0);
172 #elif defined(AFS_SGI_ENV)
173     attrs->va_blocks = BTOBB(attrs->va_size);
174 #elif defined(AFS_SUN5_ENV)
175     attrs->va_nblocks = (attrs->va_size ? ((attrs->va_size + 1023)>>10)<<1:0);
176 #else /* everything else */
177     attrs->va_blocks = (attrs->va_size ? ((attrs->va_size + 1023)>>10)<<1:0);
178 #endif
179     return 0;
180 }
181
182
183
184 #if     defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV)
185 int
186 afs_getattr(OSI_VC_DECL(avc), struct vattr *attrs, int flags,
187             struct AFS_UCRED *acred)
188 #else
189 int
190 afs_getattr(OSI_VC_DECL(avc), struct vattr *attrs, struct AFS_UCRED *acred)
191 #endif
192 {
193     afs_int32 code;
194     struct vrequest treq;
195     struct unixuser *au;
196     int inited = 0;
197     OSI_VC_CONVERT(avc);
198
199     AFS_STATCNT(afs_getattr);
200     afs_Trace2(afs_iclSetp, CM_TRACE_GETATTR, ICL_TYPE_POINTER, avc,
201                ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(avc->m.Length));
202
203     if (afs_fakestat_enable && avc->mvstat == 1) {
204         struct afs_fakestat_state fakestat;
205
206         code = afs_InitReq(&treq, acred);
207         if (code)
208             return code;
209         afs_InitFakeStat(&fakestat);
210         code = afs_TryEvalFakeStat(&avc, &fakestat, &treq);
211         if (code) {
212             afs_PutFakeStat(&fakestat);
213             return code;
214         }
215
216         code = afs_CopyOutAttrs(avc, attrs);
217         afs_PutFakeStat(&fakestat);
218         return code;
219     }
220 #if defined(AFS_SUN5_ENV)
221     if (flags & ATTR_HINT) {
222         code = afs_CopyOutAttrs(avc, attrs);
223         return code;
224     }
225 #endif
226 #if defined(AFS_DARWIN_ENV) && !defined(AFS_DARWIN80_ENV)
227     if (avc->states & CUBCinit) {
228         code = afs_CopyOutAttrs(avc, attrs);
229         return code;
230     }
231 #endif
232
233     AFS_DISCON_LOCK();
234
235 #ifdef AFS_BOZONLOCK_ENV
236     afs_BozonLock(&avc->pvnLock, avc);
237 #endif
238
239     if (afs_shuttingdown)
240         return EIO;
241
242     if (!(avc->states & CStatd)) {
243         if (!(code = afs_InitReq(&treq, acred))) {
244             code = afs_VerifyVCache2(avc, &treq);
245             inited = 1;
246         }
247     } else
248         code = 0;
249
250 #ifdef AFS_BOZONLOCK_ENV
251     if (code == 0)
252         osi_FlushPages(avc, acred);
253     afs_BozonUnlock(&avc->pvnLock, avc);
254 #endif
255
256
257     if (code == 0) {
258         osi_FlushText(avc);     /* only needed to flush text if text locked last time */
259         code = afs_CopyOutAttrs(avc, attrs);
260
261         if (afs_nfsexporter) {
262             if (!inited) {
263                 if ((code = afs_InitReq(&treq, acred)))
264                     return code;
265                 inited = 1;
266             }
267             if (AFS_NFSXLATORREQ(acred)) {
268                 if ((vType(avc) != VDIR)
269                     && !afs_AccessOK(avc, PRSFS_READ, &treq,
270                                      CHECK_MODE_BITS |
271                                      CMB_ALLOW_EXEC_AS_READ)) {
272                     return EACCES;
273                 }
274             }
275             if ((au = afs_FindUser(treq.uid, -1, READ_LOCK))) {
276                 register struct afs_exporter *exporter = au->exporter;
277
278                 if (exporter && !(afs_nfsexporter->exp_states & EXP_UNIXMODE)) {
279                     unsigned int ubits;
280                     /*
281                      *  If the remote user wishes to enforce default Unix mode semantics,
282                      *  like in the nfs exporter case, we OR in the user bits
283                      *  into the group and other bits. We need to do this
284                      *  because there is no RFS_ACCESS call and thus nfs
285                      *  clients implement nfs_access by interpreting the 
286                      *  mode bits in the traditional way, which of course
287                      *  loses with afs.
288                      */
289                     ubits = (attrs->va_mode & 0700) >> 6;
290                     attrs->va_mode = attrs->va_mode | ubits | (ubits << 3);
291                     /* If it's the root of AFS, replace the inode number with the
292                      * inode number of the mounted on directory; otherwise this 
293                      * confuses getwd()... */
294 #ifdef AFS_LINUX22_ENV
295                     if (avc == afs_globalVp) {
296                         struct inode *ip = AFSTOV(avc)->i_sb->s_root->d_inode;
297                         attrs->va_nodeid = ip->i_ino;   /* VTOI()? */
298                     }
299 #else
300                     if (
301 #ifdef AFS_DARWIN_ENV               
302                         vnode_isvroot(AFSTOV(avc))
303 #else
304                         AFSTOV(avc)->v_flag & VROOT
305 #endif
306                         ) {
307                         struct vnode *vp = AFSTOV(avc);
308
309 #ifdef AFS_DARWIN80_ENV
310                         /* XXX vp = vnode_mount(vp)->mnt_vnodecovered; */
311                         vp = 0;
312 #else
313                         vp = vp->v_vfsp->vfs_vnodecovered;
314                         if (vp) {       /* Ignore weird failures */
315 #ifdef AFS_SGI62_ENV
316                             attrs->va_nodeid = VnodeToIno(vp);
317 #else
318                             struct inode *ip;
319
320                             ip = (struct inode *)VTOI(vp);
321                             if (ip)     /* Ignore weird failures */
322                                 attrs->va_nodeid = ip->i_number;
323 #endif
324                         }
325 #endif
326                     }
327 #endif /* AFS_LINUX22_ENV */
328                 }
329                 afs_PutUser(au, READ_LOCK);
330             }
331         }
332     }
333
334     AFS_DISCON_UNLOCK();
335
336     if (!code)
337         return 0;
338     code = afs_CheckCode(code, &treq, 14);
339     return code;
340 }
341
342 /* convert a Unix request into a status store request */
343 int
344 afs_VAttrToAS(register struct vcache *avc, register struct vattr *av,
345               register struct AFSStoreStatus *as)
346 {
347     register int mask;
348     mask = 0;
349     AFS_STATCNT(afs_VAttrToAS);
350 #if     defined(AFS_DARWIN80_ENV)
351     if (VATTR_IS_ACTIVE(av, va_mode)) {
352 #elif   defined(AFS_AIX_ENV)
353 /* Boy, was this machine dependent bogosity hard to swallow????.... */
354     if (av->va_mode != -1) {
355 #elif   defined(AFS_LINUX22_ENV) || defined(UKERNEL)
356     if (av->va_mask & ATTR_MODE) {
357 #elif   defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV)
358     if (av->va_mask & AT_MODE) {
359 #elif   defined(AFS_XBSD_ENV)
360     if (av->va_mode != (mode_t)VNOVAL) {
361 #else
362     if (av->va_mode != ((unsigned short)-1)) {
363 #endif
364         mask |= AFS_SETMODE;
365         as->UnixModeBits = av->va_mode & 0xffff;
366         if (avc->states & CForeign) {
367             ObtainWriteLock(&avc->lock, 127);
368             afs_FreeAllAxs(&(avc->Access));
369             ReleaseWriteLock(&avc->lock);
370         }
371     }
372 #if     defined(AFS_DARWIN80_ENV)
373     if (VATTR_IS_ACTIVE(av, va_gid)) {
374 #elif defined(AFS_LINUX22_ENV) || defined(UKERNEL)
375     if (av->va_mask & ATTR_GID) {
376 #elif defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV)
377     if (av->va_mask & AT_GID) {
378 #elif defined(AFS_HPUX_ENV)
379 #if     defined(AFS_HPUX102_ENV)
380     if (av->va_gid != GID_NO_CHANGE) {
381 #else
382     if (av->va_gid != ((unsigned short)-1)) {
383 #endif
384 #elif   defined(AFS_XBSD_ENV)
385     if (av->va_gid != (gid_t)VNOVAL) {
386 #else
387     if (av->va_gid != -1) {
388 #endif /* AFS_LINUX22_ENV */
389         mask |= AFS_SETGROUP;
390         as->Group = av->va_gid;
391     }
392 #if     defined(AFS_DARWIN80_ENV)
393     if (VATTR_IS_ACTIVE(av, va_uid)) {
394 #elif defined(AFS_LINUX22_ENV) || defined(UKERNEL)
395     if (av->va_mask & ATTR_UID) {
396 #elif defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV)
397     if (av->va_mask & AT_UID) {
398 #elif defined(AFS_HPUX_ENV)
399 #if     defined(AFS_HPUX102_ENV)
400     if (av->va_uid != UID_NO_CHANGE) {
401 #elif   defined(AFS_XBSD_ENV)
402     if (av->va_uid != (uid_t)VNOVAL) {
403 #else
404     if (av->va_uid != ((unsigned short)-1)) {
405 #endif
406 #else
407     if (av->va_uid != -1) {
408 #endif /* AFS_LINUX22_ENV */
409         mask |= AFS_SETOWNER;
410         as->Owner = av->va_uid;
411     }
412 #if     defined(AFS_DARWIN80_ENV)
413     if (VATTR_IS_ACTIVE(av, va_modify_time)) {
414 #elif   defined(AFS_LINUX22_ENV) || defined(UKERNEL)
415     if (av->va_mask & ATTR_MTIME) {
416 #elif   defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV)
417     if (av->va_mask & AT_MTIME) {
418 #else
419     if (av->va_mtime.tv_sec != -1) {
420 #endif
421         mask |= AFS_SETMODTIME;
422 #ifndef AFS_SGI_ENV
423 #if     defined(AFS_SUN5_ENV) || defined(AFS_AIX41_ENV) || defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
424         if (av->va_mtime.tv_nsec == -1)
425 #else
426         if (av->va_mtime.tv_usec == -1)
427 #endif
428             as->ClientModTime = osi_Time();     /* special Sys V compat hack for Suns */
429         else
430 #endif
431             as->ClientModTime = av->va_mtime.tv_sec;
432     }
433     as->Mask = mask;
434     return 0;
435 }
436
437 /* We don't set CDirty bit in avc->states because setattr calls WriteVCache
438  * synchronously, therefore, it's not needed.
439  */
440 #if defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV)
441 int
442 afs_setattr(OSI_VC_DECL(avc), register struct vattr *attrs, int flags,
443             struct AFS_UCRED *acred)
444 #else
445 int
446 afs_setattr(OSI_VC_DECL(avc), register struct vattr *attrs,
447             struct AFS_UCRED *acred)
448 #endif
449 {
450     struct vrequest treq;
451     struct AFSStoreStatus astat;
452     register afs_int32 code;
453     struct afs_fakestat_state fakestate;
454     OSI_VC_CONVERT(avc);
455
456     AFS_STATCNT(afs_setattr);
457 #if defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV) || defined(AFS_LINUX22_ENV)
458     afs_Trace4(afs_iclSetp, CM_TRACE_SETATTR, ICL_TYPE_POINTER, avc,
459                ICL_TYPE_INT32, attrs->va_mask, ICL_TYPE_OFFSET,
460                ICL_HANDLE_OFFSET(attrs->va_size), ICL_TYPE_OFFSET,
461                ICL_HANDLE_OFFSET(avc->m.Length));
462 #else
463     afs_Trace4(afs_iclSetp, CM_TRACE_SETATTR, ICL_TYPE_POINTER, avc,
464                ICL_TYPE_INT32, attrs->va_mode, ICL_TYPE_OFFSET,
465                ICL_HANDLE_OFFSET(attrs->va_size), ICL_TYPE_OFFSET,
466                ICL_HANDLE_OFFSET(avc->m.Length));
467 #endif
468     if ((code = afs_InitReq(&treq, acred)))
469         return code;
470
471     AFS_DISCON_LOCK();
472
473     afs_InitFakeStat(&fakestate);
474     code = afs_EvalFakeStat(&avc, &fakestate, &treq);
475     if (code)
476         goto done;
477
478     if (avc->states & CRO) {
479         code = EROFS;
480         goto done;
481     }
482 #if defined(AFS_SGI_ENV)
483     /* ignore ATTR_LAZY calls - they are really only for keeping
484      * the access/mtime of mmaped files up to date
485      */
486     if (flags & ATTR_LAZY)
487         goto done;
488 #endif
489     /* if file size has changed, we need write access, otherwise (e.g.
490      * chmod) give it a shot; if it fails, we'll discard the status
491      * info.
492      */
493 #if     defined(AFS_DARWIN80_ENV)
494     if (VATTR_IS_ACTIVE(attrs, va_data_size)) {
495 #elif   defined(AFS_LINUX22_ENV) || defined(UKERNEL)
496     if (attrs->va_mask & ATTR_SIZE) {
497 #elif   defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV)
498     if (attrs->va_mask & AT_SIZE) {
499 #elif   defined(AFS_OSF_ENV) || defined(AFS_XBSD_ENV)
500     if (attrs->va_size != VNOVAL) {
501 #elif   defined(AFS_AIX41_ENV)
502     if (attrs->va_size != -1) {
503 #else
504     if (attrs->va_size != ~0) {
505 #endif
506         if (!afs_AccessOK(avc, PRSFS_WRITE, &treq, DONT_CHECK_MODE_BITS)) {
507             code = EACCES;
508             goto done;
509         }
510     }
511
512     if (AFS_IS_DISCONNECTED && !AFS_IS_DISCON_RW) {
513         code = ENETDOWN;
514         goto done;
515     }
516
517     afs_VAttrToAS(avc, attrs, &astat);  /* interpret request */
518     code = 0;
519 #ifdef AFS_BOZONLOCK_ENV
520     afs_BozonLock(&avc->pvnLock, avc);
521 #endif
522 #if     defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV)
523     if (AFS_NFSXLATORREQ(acred)) {
524         avc->execsOrWriters++;
525     }
526 #endif
527
528 #if defined(AFS_SGI_ENV)
529     AFS_RWLOCK((vnode_t *) avc, VRWLOCK_WRITE);
530 #endif
531 #if     defined(AFS_DARWIN80_ENV)
532     if (VATTR_IS_ACTIVE(attrs, va_data_size)) {
533 #elif   defined(AFS_LINUX22_ENV) || defined(UKERNEL)
534     if (attrs->va_mask & ATTR_SIZE) {
535 #elif   defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV)
536     if (attrs->va_mask & AT_SIZE) {
537 #elif   defined(AFS_OSF_ENV) || defined(AFS_XBSD_ENV)
538     if (attrs->va_size != VNOVAL) {
539 #elif   defined(AFS_AIX41_ENV)
540     if (attrs->va_size != -1) {
541 #else
542     if (attrs->va_size != ~0) {
543 #endif
544         afs_size_t tsize = attrs->va_size;
545         ObtainWriteLock(&avc->lock, 128);
546         avc->states |= CDirty;
547
548         code = afs_TruncateAllSegments(avc, tsize, &treq, acred);
549 #ifdef AFS_LINUX_26_ENV
550         /* We must update the Linux kernel's idea of file size as soon as
551          * possible, to avoid racing with delayed writepages delivered by
552          * pdflush */
553         if (code == 0) 
554             i_size_write(AFSTOV(avc), tsize);
555 #endif
556         /* if date not explicitly set by this call, set it ourselves, since we
557          * changed the data */
558         if (!(astat.Mask & AFS_SETMODTIME)) {
559             astat.Mask |= AFS_SETMODTIME;
560             astat.ClientModTime = osi_Time();
561         }
562
563         if (code == 0) {
564             if (((avc->execsOrWriters <= 0) && (avc->states & CCreating) == 0)
565                 || (avc->execsOrWriters == 1 && AFS_NFSXLATORREQ(acred))) {
566
567                 /* Store files now if not disconnected. */
568                 /* XXX: AFS_IS_DISCON_RW handled. */
569                 if (!AFS_IS_DISCONNECTED) {
570                         code = afs_StoreAllSegments(avc, &treq, AFS_ASYNC);
571                         if (!code)
572                                 avc->states &= ~CDirty;
573                 }
574             }
575         } else
576             avc->states &= ~CDirty;
577
578         ReleaseWriteLock(&avc->lock);
579         hzero(avc->flushDV);
580         osi_FlushText(avc);     /* do this after releasing all locks */
581     }
582     
583     if (!AFS_IS_DISCONNECTED) {
584         if (code == 0) {
585             ObtainSharedLock(&avc->lock, 16);   /* lock entry */
586             code = afs_WriteVCache(avc, &astat, &treq); /* send request */
587             ReleaseSharedLock(&avc->lock);      /* release lock */
588         }
589         if (code) {
590             ObtainWriteLock(&afs_xcbhash, 487);
591             afs_DequeueCallback(avc);
592             avc->states &= ~CStatd;
593             ReleaseWriteLock(&afs_xcbhash);
594             if (avc->fid.Fid.Vnode & 1 || (vType(avc) == VDIR))
595                 osi_dnlc_purgedp(avc);
596             /* error?  erase any changes we made to vcache entry */
597         }
598
599 #if defined(AFS_DISCON_ENV)
600     } else {
601
602         ObtainSharedLock(&avc->lock, 712);
603         /* Write changes locally. */
604         code = afs_WriteVCacheDiscon(avc, &astat, attrs);
605         ReleaseSharedLock(&avc->lock);
606 #endif
607     }           /* if (!AFS_IS_DISCONNECTED) */
608
609 #if     defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV)
610     if (AFS_NFSXLATORREQ(acred)) {
611         avc->execsOrWriters--;
612     }
613 #endif
614 #ifdef AFS_BOZONLOCK_ENV
615     afs_BozonUnlock(&avc->pvnLock, avc);
616 #endif
617 #if defined(AFS_SGI_ENV)
618     AFS_RWUNLOCK((vnode_t *) avc, VRWLOCK_WRITE);
619 #endif
620   done:
621     afs_PutFakeStat(&fakestate);
622
623     AFS_DISCON_UNLOCK();
624     code = afs_CheckCode(code, &treq, 15);
625     return code;
626 }