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