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