fix-indent-bug-with-lock-macros-part-three-20040818
[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 (avc->mvstat == 2) {
321 #if defined(AFS_SGI_ENV) || defined(AFS_SUN5_ENV) || defined(AFS_AIX41_ENV) || defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
322                     attrs->va_mtime.tv_nsec += ((++avc->xlatordv) * 1000);
323 #else
324                     attrs->va_mtime.tv_usec += ++avc->xlatordv;
325 #endif
326                 }
327             }
328             if ((au = afs_FindUser(treq.uid, -1, READ_LOCK))) {
329                 register struct afs_exporter *exporter = au->exporter;
330
331                 if (exporter && !(afs_nfsexporter->exp_states & EXP_UNIXMODE)) {
332                     unsigned int ubits;
333                     /*
334                      *  If the remote user wishes to enforce default Unix mode semantics,
335                      *  like in the nfs exporter case, we OR in the user bits
336                      *  into the group and other bits. We need to do this
337                      *  because there is no RFS_ACCESS call and thus nfs
338                      *  clients implement nfs_access by interpreting the 
339                      *  mode bits in the traditional way, which of course
340                      *  loses with afs.
341                      */
342                     ubits = (attrs->va_mode & 0700) >> 6;
343                     attrs->va_mode = attrs->va_mode | ubits | (ubits << 3);
344                     /* If it's the root of AFS, replace the inode number with the
345                      * inode number of the mounted on directory; otherwise this 
346                      * confuses getwd()... */
347 #ifdef AFS_LINUX22_ENV
348                     if (avc == afs_globalVp) {
349                         struct inode *ip = avc->v.i_sb->s_root->d_inode;
350                         attrs->va_nodeid = ip->i_ino;
351                     }
352 #else
353                     if (AFSTOV(avc)->v_flag & VROOT) {
354                         struct vnode *vp = AFSTOV(avc);
355
356                         vp = vp->v_vfsp->vfs_vnodecovered;
357                         if (vp) {       /* Ignore weird failures */
358 #ifdef AFS_SGI62_ENV
359                             attrs->va_nodeid = VnodeToIno(vp);
360 #else
361                             struct inode *ip;
362
363                             ip = (struct inode *)VTOI(vp);
364                             if (ip)     /* Ignore weird failures */
365                                 attrs->va_nodeid = ip->i_number;
366 #endif
367                         }
368                     }
369 #endif /* AFS_LINUX22_ENV */
370                 }
371                 afs_PutUser(au, READ_LOCK);
372             }
373         }
374     }
375     if (!code)
376         return 0;
377     code = afs_CheckCode(code, &treq, 14);
378     return code;
379 }
380
381 /* convert a Unix request into a status store request */
382 int
383 afs_VAttrToAS(register struct vcache *avc, register struct vattr *av,
384               register struct AFSStoreStatus *as)
385 {
386     register int mask;
387     mask = 0;
388     AFS_STATCNT(afs_VAttrToAS);
389 #if     defined(AFS_AIX_ENV)
390 /* Boy, was this machine dependent bogosity hard to swallow????.... */
391     if (av->va_mode != -1) {
392 #else
393 #if     defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV) || defined(AFS_LINUX22_ENV)
394     if (av->va_mask & AT_MODE) {
395 #else
396     if (av->va_mode != ((unsigned short)-1)) {
397 #endif
398 #endif
399         mask |= AFS_SETMODE;
400         as->UnixModeBits = av->va_mode & 0xffff;
401         if (avc->states & CForeign) {
402             ObtainWriteLock(&avc->lock, 127);
403             afs_FreeAllAxs(&(avc->Access));
404             ReleaseWriteLock(&avc->lock);
405         }
406     }
407 #if defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV) || defined(AFS_LINUX22_ENV)
408     if (av->va_mask & AT_GID) {
409 #else
410 #if (defined(AFS_HPUX_ENV) || defined(AFS_SUN_ENV))
411 #if     defined(AFS_HPUX102_ENV)
412     if (av->va_gid != GID_NO_CHANGE) {
413 #else
414     if (av->va_gid != ((unsigned short)-1)) {
415 #endif
416 #else
417     if (av->va_gid != -1) {
418 #endif
419 #endif /* AFS_SUN5_ENV */
420         mask |= AFS_SETGROUP;
421         as->Group = av->va_gid;
422     }
423 #if defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV) || defined(AFS_LINUX22_ENV)
424     if (av->va_mask & AT_UID) {
425 #else
426 #if (defined(AFS_HPUX_ENV) || defined(AFS_SUN_ENV))
427 #if     defined(AFS_HPUX102_ENV)
428     if (av->va_uid != UID_NO_CHANGE) {
429 #else
430     if (av->va_uid != ((unsigned short)-1)) {
431 #endif
432 #else
433     if (av->va_uid != -1) {
434 #endif
435 #endif /* AFS_SUN5_ENV */
436         mask |= AFS_SETOWNER;
437         as->Owner = av->va_uid;
438     }
439 #if     defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV) || defined(AFS_LINUX22_ENV)
440     if (av->va_mask & AT_MTIME) {
441 #else
442     if (av->va_mtime.tv_sec != -1) {
443 #endif
444         mask |= AFS_SETMODTIME;
445 #ifndef AFS_SGI_ENV
446 #if     defined(AFS_SUN5_ENV) || defined(AFS_AIX41_ENV) || defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
447         if (av->va_mtime.tv_nsec == -1)
448 #else
449         if (av->va_mtime.tv_usec == -1)
450 #endif
451             as->ClientModTime = osi_Time();     /* special Sys V compat hack for Suns */
452         else
453 #endif
454             as->ClientModTime = av->va_mtime.tv_sec;
455     }
456     as->Mask = mask;
457     return 0;
458 }
459
460 /* We don't set CDirty bit in avc->states because setattr calls WriteVCache
461  * synchronously, therefore, it's not needed.
462  */
463 #if defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV)
464 int
465 afs_setattr(OSI_VC_DECL(avc), register struct vattr *attrs, int flags,
466             struct AFS_UCRED *acred)
467 #else
468 int
469 afs_setattr(OSI_VC_DECL(avc), register struct vattr *attrs,
470             struct AFS_UCRED *acred)
471 #endif
472 {
473     struct vrequest treq;
474     struct AFSStoreStatus astat;
475     register afs_int32 code;
476     struct afs_fakestat_state fakestate;
477     OSI_VC_CONVERT(avc);
478
479     AFS_STATCNT(afs_setattr);
480 #if defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV) || defined(AFS_LINUX22_ENV)
481     afs_Trace4(afs_iclSetp, CM_TRACE_SETATTR, ICL_TYPE_POINTER, avc,
482                ICL_TYPE_INT32, attrs->va_mask, ICL_TYPE_OFFSET,
483                ICL_HANDLE_OFFSET(attrs->va_size), ICL_TYPE_OFFSET,
484                ICL_HANDLE_OFFSET(avc->m.Length));
485 #else
486     afs_Trace4(afs_iclSetp, CM_TRACE_SETATTR, ICL_TYPE_POINTER, avc,
487                ICL_TYPE_INT32, attrs->va_mode, ICL_TYPE_OFFSET,
488                ICL_HANDLE_OFFSET(attrs->va_size), ICL_TYPE_OFFSET,
489                ICL_HANDLE_OFFSET(avc->m.Length));
490 #endif
491     if ((code = afs_InitReq(&treq, acred)))
492         return code;
493
494     afs_InitFakeStat(&fakestate);
495     code = afs_EvalFakeStat(&avc, &fakestate, &treq);
496     if (code)
497         goto done;
498
499     if (avc->states & CRO) {
500         code = EROFS;
501         goto done;
502     }
503 #if defined(AFS_SGI_ENV)
504     /* ignore ATTR_LAZY calls - they are really only for keeping
505      * the access/mtime of mmaped files up to date
506      */
507     if (flags & ATTR_LAZY)
508         goto done;
509 #endif
510 #ifndef AFS_DEC_ENV
511     /* if file size has changed, we need write access, otherwise (e.g.
512      * chmod) give it a shot; if it fails, we'll discard the status
513      * info.
514      *
515      * Note that Ultrix actually defines ftruncate of a file you have open to
516      * be O.K., and does the proper access checks itself in the truncate
517      * path (unlike BSD or SUNOS), so we skip this check for Ultrix.
518      *
519      */
520 #if     defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV) || defined(AFS_LINUX22_ENV)
521     if (attrs->va_mask & AT_SIZE) {
522 #else
523 #ifdef  AFS_OSF_ENV
524     if (attrs->va_size != VNOVAL) {
525 #else
526 #ifdef  AFS_AIX41_ENV
527     if (attrs->va_size != -1) {
528 #else
529     if (attrs->va_size != ~0) {
530 #endif
531 #endif
532 #endif
533         if (!afs_AccessOK(avc, PRSFS_WRITE, &treq, DONT_CHECK_MODE_BITS)) {
534             code = EACCES;
535             goto done;
536         }
537     }
538 #endif
539
540     afs_VAttrToAS(avc, attrs, &astat);  /* interpret request */
541     code = 0;
542 #if defined(AFS_SUN_ENV) || defined(AFS_ALPHA_ENV) || defined(AFS_SUN5_ENV)
543     afs_BozonLock(&avc->pvnLock, avc);
544 #endif
545 #if     defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV)
546     if (AFS_NFSXLATORREQ(acred)) {
547         avc->execsOrWriters++;
548     }
549 #endif
550
551 #if defined(AFS_SGI_ENV)
552     AFS_RWLOCK((vnode_t *) avc, VRWLOCK_WRITE);
553 #endif
554 #if     defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV) || defined(AFS_LINUX22_ENV)
555     if (attrs->va_mask & AT_SIZE) {
556 #else
557 #if     defined(AFS_OSF_ENV)
558     if (attrs->va_size != VNOVAL) {
559 #else /* AFS_OSF_ENV */
560     if (attrs->va_size != -1) {
561 #endif
562 #endif
563         afs_size_t tsize = attrs->va_size;
564         ObtainWriteLock(&avc->lock, 128);
565         avc->states |= CDirty;
566         code = afs_TruncateAllSegments(avc, tsize, &treq, acred);
567         /* if date not explicitly set by this call, set it ourselves, since we
568          * changed the data */
569         if (!(astat.Mask & AFS_SETMODTIME)) {
570             astat.Mask |= AFS_SETMODTIME;
571             astat.ClientModTime = osi_Time();
572         }
573         if (code == 0) {
574             if (((avc->execsOrWriters <= 0) && (avc->states & CCreating) == 0)
575                 || (avc->execsOrWriters == 1 && AFS_NFSXLATORREQ(acred))) {
576                 code = afs_StoreAllSegments(avc, &treq, AFS_ASYNC);
577                 if (!code)
578                     avc->states &= ~CDirty;
579             }
580         } else
581             avc->states &= ~CDirty;
582
583         ReleaseWriteLock(&avc->lock);
584         hzero(avc->flushDV);
585         osi_FlushText(avc);     /* do this after releasing all locks */
586 #ifdef AFS_DEC_ENV
587         /* in case we changed the size here, propagate it to gp->g_size */
588         afs_gfshack((struct gnode *)avc);
589 #endif
590     }
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->states &= ~CStatd;
600         ReleaseWriteLock(&afs_xcbhash);
601         if (avc->fid.Fid.Vnode & 1 || (vType(avc) == VDIR))
602             osi_dnlc_purgedp(avc);
603         /* error?  erase any changes we made to vcache entry */
604     }
605 #if     defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV)
606     if (AFS_NFSXLATORREQ(acred)) {
607         avc->execsOrWriters--;
608     }
609 #endif
610 #if defined(AFS_SUN_ENV) || defined(AFS_OSF_ENV) || defined(AFS_SUN5_ENV)
611     afs_BozonUnlock(&avc->pvnLock, avc);
612 #endif
613 #if defined(AFS_SGI_ENV)
614     AFS_RWUNLOCK((vnode_t *) avc, VRWLOCK_WRITE);
615 #endif
616   done:
617     afs_PutFakeStat(&fakestate);
618     code = afs_CheckCode(code, &treq, 15);
619     return code;
620 }