cc1fd32193e8a67037562a8a2f9f7dbf0d9c38f8
[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  * afs_CreateAttr
21  * afs_DestroyAttr
22  *
23  */
24
25 #include <afsconfig.h>
26 #include "afs/param.h"
27
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
44 /* copy out attributes from cache entry */
45 int
46 afs_CopyOutAttrs(struct vcache *avc, struct vattr *attrs)
47 {
48     struct volume *tvp;
49     struct cell *tcell;
50 #if defined(AFS_FBSD_ENV) || defined(AFS_DFBSD_ENV)
51     struct vnode *vp = AFSTOV(avc);
52 #endif
53     int fakedir = 0;
54
55     AFS_STATCNT(afs_CopyOutAttrs);
56     if (afs_fakestat_enable && avc->mvstat == AFS_MVSTAT_MTPT)
57         fakedir = 1;
58     attrs->va_type = fakedir ? VDIR : vType(avc);
59 #if defined(AFS_SGI_ENV) || defined(AFS_AIX32_ENV) || defined(AFS_SUN5_ENV) || defined(AFS_DARWIN_ENV)
60     attrs->va_mode = fakedir ? S_IFDIR | 0755 : (mode_t) (avc->f.m.Mode & 0xffff);
61 #else
62     attrs->va_mode = fakedir ? VDIR | 0755 : avc->f.m.Mode;
63 #endif
64
65     if (avc->f.m.Mode & (VSUID | VSGID)) {
66         /* setuid or setgid, make sure we're allowed to run them from this cell */
67         tcell = afs_GetCell(avc->f.fid.Cell, 0);
68         if (tcell && (tcell->states & CNoSUID))
69             attrs->va_mode &= ~(VSUID | VSGID);
70     }
71 #if defined(AFS_DARWIN_ENV)
72     {
73         if (!afs_darwin_realmodes) {
74             /* Mac OS X uses the mode bits to determine whether a file or
75              * directory is accessible, and believes them, even though under
76              * AFS they're almost assuredly wrong, especially if the local uid
77              * does not match the AFS ID.  So we set the mode bits
78              * conservatively.
79              */
80             if (S_ISDIR(attrs->va_mode)) {
81                 /* all access bits need to be set for directories, since even
82                  * a mode 0 directory can still be used normally.
83                  */
84                 attrs->va_mode |= ACCESSPERMS;
85             } else {
86                 /* for other files, replicate the user bits to group and other */
87                 mode_t ubits = (attrs->va_mode & S_IRWXU) >> 6;
88                 attrs->va_mode |= ubits | (ubits << 3);
89             }
90         }
91     }
92 #endif /* AFS_DARWIN_ENV */
93     attrs->va_uid = fakedir ? 0 : avc->f.m.Owner;
94     attrs->va_gid = fakedir ? 0 : avc->f.m.Group;       /* yeah! */
95 #if defined(AFS_SUN5_ENV)
96     attrs->va_fsid = avc->v.v_vfsp->vfs_fsid.val[0];
97 #elif defined(AFS_DARWIN80_ENV)
98     VATTR_RETURN(attrs, va_fsid, vfs_statfs(vnode_mount(AFSTOV(avc)))->f_fsid.val[0]);
99 #elif defined(AFS_DARWIN_ENV)
100     attrs->va_fsid = avc->v->v_mount->mnt_stat.f_fsid.val[0];
101 #else /* ! AFS_DARWIN_ENV */
102     attrs->va_fsid = 1;
103 #endif 
104     if (avc->mvstat == AFS_MVSTAT_ROOT) {
105         tvp = afs_GetVolume(&avc->f.fid, 0, READ_LOCK);
106         /* The mount point's vnode. */
107         if (tvp) {
108             attrs->va_nodeid =
109               afs_calc_inum(tvp->mtpoint.Cell,
110                             tvp->mtpoint.Fid.Volume,
111                             tvp->mtpoint.Fid.Vnode);
112             if (FidCmp(&afs_rootFid, &avc->f.fid) && !attrs->va_nodeid)
113                 attrs->va_nodeid = 2;
114             afs_PutVolume(tvp, READ_LOCK);
115         } else
116             attrs->va_nodeid = 2;
117     } else
118         attrs->va_nodeid = 
119               afs_calc_inum(avc->f.fid.Cell,
120                             avc->f.fid.Fid.Volume,
121                             avc->f.fid.Fid.Vnode);
122     attrs->va_nodeid &= 0x7fffffff;     /* Saber C hates negative inode #s! */
123     attrs->va_nlink = fakedir ? 100 : avc->f.m.LinkCount;
124     attrs->va_size = fakedir ? 4096 : avc->f.m.Length;
125 #if defined(AFS_FBSD_ENV) || defined(AFS_DFBSD_ENV)
126         vnode_pager_setsize(vp, (u_long) attrs->va_size);
127 #endif
128     attrs->va_atime.tv_sec = attrs->va_mtime.tv_sec = attrs->va_ctime.tv_sec =
129         fakedir ? 0 : (int)avc->f.m.Date;
130     /* set microseconds to be dataversion # so that we approximate NFS-style
131      * use of mtime as a dataversion #.  We take it mod 512K because
132      * microseconds *must* be less than a million, and 512K is the biggest
133      * power of 2 less than such.  DataVersions are typically pretty small
134      * anyway, so the difference between 512K and 1000000 shouldn't matter
135      * much, and "&" is a lot faster than "%".
136      */
137 #if defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
138     /* nfs on these systems puts an 0 in nsec and stores the nfs usec (aka 
139      * dataversion) in va_gen */
140
141     attrs->va_atime.tv_nsec = attrs->va_mtime.tv_nsec =
142         attrs->va_ctime.tv_nsec = 0;
143     attrs->va_gen = hgetlo(avc->f.m.DataVersion);
144 #elif defined(AFS_SGI_ENV) || defined(AFS_SUN5_ENV) || defined(AFS_AIX41_ENV) || defined(AFS_OBSD_ENV) || defined(AFS_NBSD_ENV)
145     attrs->va_atime.tv_nsec = attrs->va_mtime.tv_nsec =
146         attrs->va_ctime.tv_nsec =
147         (hgetlo(avc->f.m.DataVersion) & 0x7ffff) * 1000;
148 #else
149     attrs->va_atime.tv_usec = attrs->va_mtime.tv_usec =
150         attrs->va_ctime.tv_usec = (hgetlo(avc->f.m.DataVersion) & 0x7ffff);
151 #endif
152 #if defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
153     attrs->va_flags = 0;
154 #endif
155 #if defined(AFS_SGI_ENV) || defined(AFS_SUN5_ENV)
156     attrs->va_blksize = AFS_BLKSIZE;    /* XXX Was 8192 XXX */
157 #else
158     attrs->va_blocksize = AFS_BLKSIZE;  /* XXX Was 8192 XXX */
159 #endif
160     attrs->va_rdev = 1;
161 #if defined(AFS_HPUX110_ENV)
162     if (afs_globalVFS)
163         attrs->va_fstype = afs_globalVFS->vfs_mtype;
164 #endif
165
166     /*
167      * Below return 0 (and not 1) blocks if the file is zero length. This conforms
168      * better with the other filesystems that do return 0.      
169      */
170 #if defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
171     attrs->va_bytes = (attrs->va_size ? (attrs->va_size + 1023) : 1024);
172 #ifdef  va_bytes_rsv
173     attrs->va_bytes_rsv = -1;
174 #endif
175 #elif defined(AFS_HPUX_ENV)
176     attrs->va_blocks = (attrs->va_size ? ((attrs->va_size + 1023)>>10) : 0);
177 #elif defined(AFS_SGI_ENV)
178     attrs->va_blocks = BTOBB(attrs->va_size);
179 #elif defined(AFS_SUN5_ENV)
180     attrs->va_nblocks = (attrs->va_size ? ((attrs->va_size + 1023)>>10)<<1:0);
181 #else /* everything else */
182     attrs->va_blocks = (attrs->va_size ? ((attrs->va_size + 1023)>>10)<<1:0);
183 #endif
184     return 0;
185 }
186
187
188
189 #if     defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV)
190 int
191 afs_getattr(OSI_VC_DECL(avc), struct vattr *attrs, int flags,
192             afs_ucred_t *acred)
193 #else
194 int
195 afs_getattr(OSI_VC_DECL(avc), struct vattr *attrs, afs_ucred_t *acred)
196 #endif
197 {
198     afs_int32 code;
199     struct vrequest *treq = NULL;
200     struct unixuser *au;
201     int inited = 0;
202     OSI_VC_CONVERT(avc);
203
204     AFS_STATCNT(afs_getattr);
205     afs_Trace2(afs_iclSetp, CM_TRACE_GETATTR, ICL_TYPE_POINTER, avc,
206                ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(avc->f.m.Length));
207
208     if (afs_fakestat_enable && avc->mvstat == AFS_MVSTAT_MTPT) {
209         struct afs_fakestat_state fakestat;
210         struct vrequest *ureq = NULL;
211
212         code = afs_CreateReq(&ureq, acred);
213         if (code) {
214             return code;
215         }
216         afs_InitFakeStat(&fakestat);
217         code = afs_TryEvalFakeStat(&avc, &fakestat, ureq);
218         if (code) {
219             afs_PutFakeStat(&fakestat);
220             afs_DestroyReq(ureq);
221             return code;
222         }
223
224         code = afs_CopyOutAttrs(avc, attrs);
225         afs_PutFakeStat(&fakestat);
226         afs_DestroyReq(ureq);
227         return code;
228     }
229 #if defined(AFS_SUN5_ENV)
230     if (flags & ATTR_HINT) {
231         code = afs_CopyOutAttrs(avc, attrs);
232         return code;
233     }
234 #endif
235 #if defined(AFS_DARWIN_ENV) && !defined(AFS_DARWIN80_ENV)
236     if (avc->f.states & CUBCinit) {
237         code = afs_CopyOutAttrs(avc, attrs);
238         return code;
239     }
240 #endif
241
242     AFS_DISCON_LOCK();
243
244     if (afs_shuttingdown != AFS_RUNNING) {
245         AFS_DISCON_UNLOCK();
246         return EIO;
247     }
248
249     if (!(avc->f.states & CStatd)) {
250         if (!(code = afs_CreateReq(&treq, acred))) {
251             code = afs_VerifyVCache2(avc, treq);
252             inited = 1;
253         }
254     } else
255         code = 0;
256
257 #if defined(AFS_SUN5_ENV)
258     if (code == 0)
259         osi_FlushPages(avc, acred);
260 #endif
261
262     if (code == 0) {
263         osi_FlushText(avc);     /* only needed to flush text if text locked last time */
264         code = afs_CopyOutAttrs(avc, attrs);
265
266         if (afs_nfsexporter) {
267             if (!inited) {
268                 if ((code = afs_CreateReq(&treq, acred))) {
269                     return code;
270                 }
271                 inited = 1;
272             }
273             if (AFS_NFSXLATORREQ(acred)) {
274                 if ((vType(avc) != VDIR)
275                     && !afs_AccessOK(avc, PRSFS_READ, treq,
276                                      CHECK_MODE_BITS |
277                                      CMB_ALLOW_EXEC_AS_READ)) {
278                     afs_DestroyReq(treq);
279                     return EACCES;
280                 }
281             }
282             if ((au = afs_FindUser(treq->uid, -1, READ_LOCK))) {
283                 struct afs_exporter *exporter = au->exporter;
284
285                 if (exporter && !(afs_nfsexporter->exp_states & EXP_UNIXMODE)) {
286                     unsigned int ubits;
287                     /*
288                      *  If the remote user wishes to enforce default Unix mode semantics,
289                      *  like in the nfs exporter case, we OR in the user bits
290                      *  into the group and other bits. We need to do this
291                      *  because there is no RFS_ACCESS call and thus nfs
292                      *  clients implement nfs_access by interpreting the 
293                      *  mode bits in the traditional way, which of course
294                      *  loses with afs.
295                      */
296                     ubits = (attrs->va_mode & 0700) >> 6;
297                     attrs->va_mode = attrs->va_mode | ubits | (ubits << 3);
298                     /* If it's the root of AFS, replace the inode number with the
299                      * inode number of the mounted on directory; otherwise this 
300                      * confuses getwd()... */
301 #ifdef AFS_LINUX22_ENV
302                     if (avc == afs_globalVp) {
303                         struct inode *ip = AFSTOV(avc)->i_sb->s_root->d_inode;
304                         attrs->va_nodeid = ip->i_ino;   /* VTOI()? */
305                     }
306 #else
307                     if (
308 #if defined(AFS_DARWIN_ENV)
309                         vnode_isvroot(AFSTOV(avc))
310 #elif defined(AFS_NBSD50_ENV)
311                         AFSTOV(avc)->v_vflag & VV_ROOT
312 #else
313                         AFSTOV(avc)->v_flag & VROOT
314 #endif
315                         ) {
316                         struct vnode *vp = AFSTOV(avc);
317
318 #ifdef AFS_DARWIN80_ENV
319                         /* XXX vp = vnode_mount(vp)->mnt_vnodecovered; */
320                         vp = 0;
321 #else
322                         vp = vp->v_vfsp->vfs_vnodecovered;
323                         if (vp) {       /* Ignore weird failures */
324 #ifdef AFS_SGI62_ENV
325                             attrs->va_nodeid = VnodeToIno(vp);
326 #else
327                             struct inode *ip;
328
329                             ip = (struct inode *)VTOI(vp);
330                             if (ip)     /* Ignore weird failures */
331                                 attrs->va_nodeid = ip->i_number;
332 #endif
333                         }
334 #endif
335                     }
336 #endif /* AFS_LINUX22_ENV */
337                 }
338                 afs_PutUser(au, READ_LOCK);
339             }
340         }
341     }
342
343     AFS_DISCON_UNLOCK();
344
345     if (!code) {
346         afs_DestroyReq(treq);
347         return 0;
348     }
349     code = afs_CheckCode(code, treq, 14);
350     afs_DestroyReq(treq);
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 = NULL;
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_CreateReq(&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 #if     defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV)
535     if (AFS_NFSXLATORREQ(acred)) {
536         avc->execsOrWriters++;
537     }
538 #endif
539
540 #if defined(AFS_SGI_ENV)
541     AFS_RWLOCK((vnode_t *) avc, VRWLOCK_WRITE);
542 #endif
543 #if     defined(AFS_DARWIN80_ENV)
544     if (VATTR_IS_ACTIVE(attrs, va_data_size)) {
545 #elif   defined(AFS_LINUX22_ENV) || defined(UKERNEL)
546     if (attrs->va_mask & ATTR_SIZE) {
547 #elif   defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV)
548     if (attrs->va_mask & AT_SIZE) {
549 #elif   defined(AFS_XBSD_ENV)
550     if (attrs->va_size != VNOVAL) {
551 #elif   defined(AFS_AIX41_ENV)
552     if (attrs->va_size != -1) {
553 #else
554     if (attrs->va_size != ~0) {
555 #endif
556         afs_size_t tsize = attrs->va_size;
557         ObtainWriteLock(&avc->lock, 128);
558         avc->f.states |= CDirty;
559
560         if (AFS_IS_DISCONNECTED && tsize >=avc->f.m.Length) {
561             /* If we're growing the file, and we're disconnected, we need
562              * to make the relevant dcache chunks appear ourselves. */
563             code = afs_ExtendSegments(avc, tsize, treq);
564         } else {
565             code = afs_TruncateAllSegments(avc, tsize, treq, acred);
566         }
567 #ifdef AFS_LINUX26_ENV
568         /* We must update the Linux kernel's idea of file size as soon as
569          * possible, to avoid racing with delayed writepages delivered by
570          * pdflush */
571         if (code == 0)
572             i_size_write(AFSTOV(avc), tsize);
573 #endif
574 #if defined(AFS_FBSD_ENV) || defined(AFS_DFBSD_ENV)
575         vnode_pager_setsize(vp, (u_long) tsize);
576 #endif
577         /* if date not explicitly set by this call, set it ourselves, since we
578          * changed the data */
579         if (!(astat.Mask & AFS_SETMODTIME)) {
580             astat.Mask |= AFS_SETMODTIME;
581             astat.ClientModTime = osi_Time();
582         }
583
584         if (code == 0) {
585             if (((avc->execsOrWriters <= 0) && (avc->f.states & CCreating) == 0)
586                 || (avc->execsOrWriters == 1 && AFS_NFSXLATORREQ(acred))) {
587
588                 /* Store files now if not disconnected. */
589                 /* XXX: AFS_IS_DISCON_RW handled. */
590                 if (!AFS_IS_DISCONNECTED) {
591                         code = afs_StoreAllSegments(avc, treq, AFS_ASYNC);
592                         if (!code)
593                                 avc->f.states &= ~CDirty;
594                 }
595             }
596         } else
597             avc->f.states &= ~CDirty;
598
599         ReleaseWriteLock(&avc->lock);
600         hzero(avc->flushDV);
601         osi_FlushText(avc);     /* do this after releasing all locks */
602     }
603     
604     if (!AFS_IS_DISCONNECTED) {
605         if (code == 0) {
606             ObtainSharedLock(&avc->lock, 16);   /* lock entry */
607             code = afs_WriteVCache(avc, &astat, treq);  /* send request */
608             ReleaseSharedLock(&avc->lock);      /* release lock */
609         }
610         if (code) {
611             ObtainWriteLock(&afs_xcbhash, 487);
612             afs_DequeueCallback(avc);
613             avc->f.states &= ~CStatd;
614             ReleaseWriteLock(&afs_xcbhash);
615             if (avc->f.fid.Fid.Vnode & 1 || (vType(avc) == VDIR))
616                 osi_dnlc_purgedp(avc);
617             /* error?  erase any changes we made to vcache entry */
618         }
619     } else {
620         ObtainSharedLock(&avc->lock, 712);
621         /* Write changes locally. */
622         code = afs_WriteVCacheDiscon(avc, &astat, attrs);
623         ReleaseSharedLock(&avc->lock);
624     }           /* if (!AFS_IS_DISCONNECTED) */
625
626 #if     defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV)
627     if (AFS_NFSXLATORREQ(acred)) {
628         avc->execsOrWriters--;
629     }
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     afs_DestroyReq(treq);
640     return code;
641 }
642
643 /*!
644  * Allocate a vattr.
645  *
646  * \note The caller must free the allocated vattr with
647  *       afs_DestroyAttr() if this function returns successfully (zero).
648  *
649  * \note The GLOCK must be held on platforms which require the GLOCK
650  *       for osi_AllocSmallSpace() and osi_FreeSmallSpace().
651  *
652  * \param[out] out   address of the vattr pointer
653  * \return     0 on success
654  */
655 int
656 afs_CreateAttr(struct vattr **out)
657 {
658     struct vattr *vattr = NULL;
659
660     if (!out) {
661         return EINVAL;
662     }
663     vattr = osi_AllocSmallSpace(sizeof(struct vattr));
664     if (!vattr) {
665         return ENOMEM;
666     }
667     memset(vattr, 0, sizeof(struct vattr));
668     *out = vattr;
669     return 0;
670 }
671
672 /*!
673  * Deallocate a vattr.
674  *
675  * \note The GLOCK must be held on platforms which require the GLOCK
676  *       for osi_FreeSmallSpace().
677  *
678  * \param[in]  vattr  pointer to the vattr to free; may be NULL
679  */
680 void
681 afs_DestroyAttr(struct vattr *vattr)
682 {
683     if (vattr) {
684         osi_FreeSmallSpace(vattr);
685     }
686 }
687