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