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