bulkstat-changes-20001202
[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
10 /*
11  * afs_vnop_attrs.c - setattr and getattr vnodeops
12  *
13  * Implements:
14  * afs_CopyOutAttrs
15  * afs_getattr
16  * afs_VAttrToAS
17  * afs_setattr
18  *
19  */
20
21 #include "../afs/param.h"       /* Should be always first */
22 #include "../afs/sysincludes.h" /* Standard vendor system headers */
23 #include "../afs/afsincludes.h" /* Afs-based standard headers */
24 #include "../afs/afs_stats.h" /* statistics */
25 #include "../afs/afs_cbqueue.h"
26 #include "../afs/nfsclient.h"
27 #include "../afs/afs_osidnlc.h"
28
29 extern afs_rwlock_t afs_xcbhash;
30 struct afs_exporter *afs_nfsexporter;
31 extern struct vcache *afs_globalVp;
32
33 /* copy out attributes from cache entry */
34 afs_CopyOutAttrs(avc, attrs)
35     register struct vattr *attrs;
36     register struct vcache *avc; {
37     register struct volume *tvp;
38     register struct cell *tcell;
39     register afs_int32 i;
40
41     AFS_STATCNT(afs_CopyOutAttrs);
42 #if     defined(AFS_MACH_ENV )
43     attrs->va_mode = vType(avc) | (avc->m.Mode&~VFMT);
44 #else /* AFS_MACH_ENV */
45     attrs->va_type = vType(avc);
46 #if defined(AFS_SGI_ENV) || defined(AFS_AIX32_ENV) || defined(AFS_SUN5_ENV)
47     attrs->va_mode = (mode_t)(avc->m.Mode & 0xffff);
48 #else
49     attrs->va_mode = avc->m.Mode;
50 #endif
51 #endif /* AFS_MACH_ENV */
52
53     if (avc->m.Mode & (VSUID|VSGID)) {
54         /* setuid or setgid, make sure we're allowed to run them from this cell */
55         tcell = afs_GetCell(avc->fid.Cell, 0);
56         if (tcell && (tcell->states & CNoSUID))
57             attrs->va_mode &= ~(VSUID|VSGID);
58     }
59     attrs->va_uid = avc->m.Owner;
60     attrs->va_gid = avc->m.Group;   /* yeah! */
61 #if     defined(AFS_SUN56_ENV)
62     attrs->va_fsid = avc->v.v_vfsp->vfs_fsid.val[0];
63 #else
64 #ifdef  AFS_SUN5_ENV
65     /* XXX We try making this match the vfs's dev field  XXX */
66     attrs->va_fsid = 1;
67 #else
68 #ifdef AFS_OSF_ENV
69     attrs->va_fsid = avc->v.v_mount->m_stat.f_fsid.val[0]; 
70 #else
71     attrs->va_fsid = 1;
72 #endif
73 #endif
74 #endif /* AFS_SUN56_ENV */
75     if (avc->mvstat == 2) {
76         tvp = afs_GetVolume(&avc->fid, 0, READ_LOCK);
77         /* The mount point's vnode. */
78         if (tvp) {
79             attrs->va_nodeid =
80                 tvp->mtpoint.Fid.Vnode + (tvp->mtpoint.Fid.Volume << 16);
81             if (FidCmp(&afs_rootFid, &avc->fid) && !attrs->va_nodeid)
82                 attrs->va_nodeid = 2;
83             afs_PutVolume(tvp, READ_LOCK);
84         }
85         else attrs->va_nodeid = 2;
86     }
87     else attrs->va_nodeid = avc->fid.Fid.Vnode + (avc->fid.Fid.Volume << 16);
88     attrs->va_nodeid &= 0x7fffffff;     /* Saber C hates negative inode #s! */
89     attrs->va_nlink = avc->m.LinkCount;
90     attrs->va_size = avc->m.Length;
91     attrs->va_atime.tv_sec = attrs->va_mtime.tv_sec = attrs->va_ctime.tv_sec =
92         avc->m.Date;
93     /* set microseconds to be dataversion # so that we approximate NFS-style
94      * use of mtime as a dataversion #.  We take it mod 512K because
95      * microseconds *must* be less than a million, and 512K is the biggest
96      * power of 2 less than such.  DataVersions are typically pretty small
97      * anyway, so the difference between 512K and 1000000 shouldn't matter
98      * much, and "&" is a lot faster than "%".
99      */
100 #if defined(AFS_SGI_ENV) || defined(AFS_SUN5_ENV) || defined(AFS_AIX41_ENV)
101     attrs->va_atime.tv_nsec = attrs->va_mtime.tv_nsec =
102         attrs->va_ctime.tv_nsec =
103             (hgetlo(avc->m.DataVersion) & 0x7ffff) * 1000;
104 #ifdef  AFS_AIX41_ENV
105     attrs->va_blocksize = PAGESIZE;             /* XXX Was 8192 XXX */
106 #else
107     attrs->va_blksize = PAGESIZE;               /* XXX Was 8192 XXX */
108 #endif
109 #else
110     attrs->va_atime.tv_usec = attrs->va_mtime.tv_usec =
111         attrs->va_ctime.tv_usec =
112             (hgetlo(avc->m.DataVersion) & 0x7ffff);
113     attrs->va_blocksize = PAGESIZE;             /* XXX Was 8192 XXX */
114 #endif
115 #ifdef AFS_DEC_ENV
116     /* Have to use real device #s in Ultrix, since that's how FS type is
117      * encoded.  If rdev doesn't match Ultrix equivalent of statfs's rdev, then
118      * "df ." doesn't work.
119      */
120     if (afs_globalVFS && afs_globalVFS->vfs_data)
121         attrs->va_rdev = ((struct mount *)(afs_globalVFS->vfs_data))->m_dev;
122     else
123         attrs->va_rdev = 1;     /* better than nothing */
124 #else 
125     attrs->va_rdev = 1;
126 #endif
127     /*
128      * Below return 0 (and not 1) blocks if the file is zero length. This conforms
129      * better with the other filesystems that do return 0.      
130      */
131 #ifdef  AFS_OSF_ENV
132 #ifdef  va_size_rsv
133     attrs->va_size_rsv = 0;
134 #endif
135 /* XXX do this */
136 /*    attrs->va_gen = avc->m.DataVersion;*/
137     attrs->va_flags = 0;
138 #endif  /* AFS_OSF_ENV */
139
140 #if !defined(AFS_OSF_ENV)
141 #if !defined(AFS_HPUX_ENV)
142 #ifdef  AFS_SUN5_ENV
143     attrs->va_nblocks = (attrs->va_size? ((attrs->va_size + 1023)>>10) << 1 : 0);
144 #else
145 #if defined(AFS_SGI_ENV)
146     attrs->va_blocks = BTOBB(attrs->va_size);
147 #else
148     attrs->va_blocks = (attrs->va_size? ((attrs->va_size + 1023)>>10) << 1 : 0);
149 #endif
150 #endif
151 #else /* !defined(AFS_HPUX_ENV) */
152     attrs->va_blocks = (attrs->va_size? ((attrs->va_size + 1023)>>10) : 0);
153 #endif /* !defined(AFS_HPUX_ENV) */
154 #else   /* ! AFS_OSF_ENV */
155     attrs->va_bytes = (attrs->va_size? (attrs->va_size + 1023) : 1024);
156 #ifdef  va_bytes_rsv
157     attrs->va_bytes_rsv = -1;
158 #endif
159 #endif  /* ! AFS_OSF_ENV */
160
161 #ifdef AFS_LINUX22_ENV
162     /* And linux has it's own stash as well. */
163     vattr2inode((struct inode*)avc, attrs);
164 #endif
165     return 0;
166 }
167
168
169
170 #if     defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV)
171 afs_getattr(OSI_VC_ARG(avc), attrs, flags, acred)
172     int flags;
173 #else
174 afs_getattr(OSI_VC_ARG(avc), attrs, acred)
175 #endif
176     OSI_VC_DECL(avc);
177     struct vattr *attrs;
178     struct AFS_UCRED *acred; 
179 {
180     afs_int32 code;
181     struct vrequest treq;
182     extern struct unixuser *afs_FindUser();
183     struct unixuser *au;
184     int inited = 0;
185     OSI_VC_CONVERT(avc)
186
187     AFS_STATCNT(afs_getattr);
188     afs_Trace2(afs_iclSetp, CM_TRACE_GETATTR, ICL_TYPE_POINTER, avc, 
189                ICL_TYPE_INT32, avc->m.Length);
190
191 #if defined(AFS_SUN_ENV) || defined(AFS_ALPHA_ENV) || defined(AFS_SUN5_ENV)
192     afs_BozonLock(&avc->pvnLock, avc);
193 #endif
194
195     if (afs_shuttingdown) return EIO;
196
197     if (!(avc->states & CStatd)) {
198       if (!(code = afs_InitReq(&treq, acred))) {
199         code = afs_VerifyVCache2(avc, &treq);
200         inited = 1;
201       }
202     }
203     else code = 0;
204
205 #if defined(AFS_SUN_ENV) || defined(AFS_ALPHA_ENV) || defined(AFS_SUN5_ENV)
206     if (code == 0) osi_FlushPages(avc, acred);
207     afs_BozonUnlock(&avc->pvnLock, avc);
208 #endif
209
210
211     if (code == 0) {
212         osi_FlushText(avc); /* only needed to flush text if text locked last time */
213         code = afs_CopyOutAttrs(avc, attrs);
214
215         if (afs_nfsexporter) {
216           if (!inited) {
217             if (code = afs_InitReq(&treq, acred))
218               return code;
219              inited = 1;
220           }
221           if (AFS_NFSXLATORREQ(acred)) {
222               if ((vType(avc) != VDIR) &&
223                   !afs_AccessOK(avc, PRSFS_READ, &treq,
224                                 CHECK_MODE_BITS|CMB_ALLOW_EXEC_AS_READ)) {
225                   return EACCES;
226               }
227               if (avc->mvstat == 2) {
228 #if defined(AFS_SGI_ENV) || defined(AFS_SUN5_ENV) || defined(AFS_AIX41_ENV)
229                   attrs->va_mtime.tv_nsec += ((++avc->xlatordv) * 1000); 
230 #else
231                   attrs->va_mtime.tv_usec += ++avc->xlatordv; 
232 #endif
233               }
234           }
235           if (au = afs_FindUser(treq.uid, -1, READ_LOCK)) {
236             register struct afs_exporter *exporter = au->exporter;
237
238             if (exporter && !(afs_nfsexporter->exp_states & EXP_UNIXMODE)) {
239                 unsigned int ubits;
240                 /*
241                  *  If the remote user wishes to enforce default Unix mode semantics,
242                  *  like in the nfs exporter case, we OR in the user bits
243                  *  into the group and other bits. We need to do this
244                  *  because there is no RFS_ACCESS call and thus nfs
245                  *  clients implement nfs_access by interpreting the 
246                  *  mode bits in the traditional way, which of course
247                  *  loses with afs.
248                  */
249                 ubits = (attrs->va_mode & 0700) >> 6;
250                 attrs->va_mode = attrs->va_mode | ubits | (ubits << 3);
251                 /* If it's the root of AFS, replace the inode number with the
252                  * inode number of the mounted on directory; otherwise this 
253                  * confuses getwd()... */
254 #ifdef AFS_LINUX22_ENV
255                 if (avc == afs_globalVp) {
256                     struct inode *ip = avc->v.i_sb->s_root->d_inode;
257                     attrs->va_nodeid = ip->i_ino;
258                 }
259 #else
260                 if (avc->v.v_flag & VROOT) {
261                     struct vnode *vp = (struct vnode *)avc;
262
263                     vp = vp->v_vfsp->vfs_vnodecovered;
264                     if (vp) {   /* Ignore weird failures */
265 #ifdef AFS_SGI62_ENV
266                         attrs->va_nodeid = VnodeToIno(vp);
267 #else
268                         struct inode *ip;
269                         
270                         ip = (struct inode *) VTOI(vp);
271                         if (ip) /* Ignore weird failures */
272                             attrs->va_nodeid = ip->i_number;
273 #endif
274                     }
275                 }
276 #endif /* AFS_LINUX22_ENV */
277               }
278             afs_PutUser(au, READ_LOCK);
279           }
280         }
281     }
282     if (!code)
283       return 0;
284     code = afs_CheckCode(code, &treq, 14);
285     return code;
286 }
287
288 /* convert a Unix request into a status store request */
289 afs_VAttrToAS(avc, av, as)
290 register struct vcache *avc;
291 register struct vattr *av;
292 register struct AFSStoreStatus *as; {
293     register int mask;
294     mask = 0;
295     AFS_STATCNT(afs_VAttrToAS);
296 #if     defined(AFS_AIX_ENV)
297 /* Boy, was this machine dependent bogosity hard to swallow????.... */
298     if (av->va_mode != -1) {
299 #else
300 #if     defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV) || defined(AFS_LINUX22_ENV)
301     if (av->va_mask & AT_MODE) {
302 #else
303     if (av->va_mode != ((unsigned short)-1)) {
304 #endif
305 #endif
306         mask |= AFS_SETMODE;
307         as->UnixModeBits = av->va_mode & 0xffff;
308         if (avc->states & CForeign) {
309             ObtainWriteLock(&avc->lock,127);
310             afs_FreeAllAxs(&(avc->Access));
311             ReleaseWriteLock(&avc->lock);
312         }
313     }
314
315 #if defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV) || defined(AFS_LINUX22_ENV)
316     if (av->va_mask & AT_GID) {
317 #else
318 #if (defined(AFS_HPUX_ENV) || defined(AFS_SUN_ENV))
319 #if     defined(AFS_HPUX102_ENV)
320     if (av->va_gid != GID_NO_CHANGE) {
321 #else
322     if (av->va_gid != ((unsigned short)-1)) {
323 #endif
324 #else
325     if (av->va_gid != -1) {
326 #endif
327 #endif /* AFS_SUN5_ENV */
328         mask |= AFS_SETGROUP;
329         as->Group = av->va_gid;
330     }
331
332 #if defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV) || defined(AFS_LINUX22_ENV)
333     if (av->va_mask & AT_UID) {
334 #else
335 #if (defined(AFS_HPUX_ENV) || defined(AFS_SUN_ENV))
336 #if     defined(AFS_HPUX102_ENV)
337     if (av->va_uid != UID_NO_CHANGE) {
338 #else
339     if (av->va_uid != ((unsigned short)-1)) {
340 #endif
341 #else
342     if (av->va_uid != -1) {
343 #endif
344 #endif /* AFS_SUN5_ENV */
345         mask |= AFS_SETOWNER;
346         as->Owner = av->va_uid;
347     }
348 #if     defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV) || defined(AFS_LINUX22_ENV)
349     if (av->va_mask & AT_MTIME) {
350 #else
351     if (av->va_mtime.tv_sec != -1) {
352 #endif
353         mask |= AFS_SETMODTIME;
354 #ifndef AFS_SGI_ENV
355 #if     defined(AFS_SUN5_ENV) || defined(AFS_AIX41_ENV)
356         if (av->va_mtime.tv_nsec == -1)
357 #else
358         if (av->va_mtime.tv_usec == -1)
359 #endif
360             as->ClientModTime = osi_Time();     /* special Sys V compat hack for Suns */
361         else
362 #endif
363             as->ClientModTime = av->va_mtime.tv_sec;
364     }
365     as->Mask = mask;
366     return 0;
367 }
368
369 /* We don't set CDirty bit in avc->states because setattr calls WriteVCache
370  * synchronously, therefore, it's not needed.
371  */
372 #if     defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV)
373 afs_setattr(OSI_VC_ARG(avc), attrs, flags, acred)
374     int flags;
375 #else
376 afs_setattr(avc, attrs, acred)
377 #endif
378     OSI_VC_DECL(avc);
379     register struct vattr *attrs;
380     struct AFS_UCRED *acred; {
381     struct vrequest treq;
382     struct AFSStoreStatus astat;
383     register afs_int32 code;
384     OSI_VC_CONVERT(avc)
385
386     AFS_STATCNT(afs_setattr);
387     afs_Trace2(afs_iclSetp, CM_TRACE_SETATTR, ICL_TYPE_POINTER, avc, 
388                ICL_TYPE_INT32, avc->m.Length);
389     if (code = afs_InitReq(&treq, acred)) return code;
390     if (avc->states & CRO) {
391         code=EROFS;
392         goto done;
393     }
394 #if defined(AFS_SGI_ENV)
395     /* ignore ATTR_LAZY calls - they are really only for keeping
396      * the access/mtime of mmaped files up to date
397      */
398     if (flags & ATTR_LAZY)
399         goto done;
400 #endif
401 #ifndef AFS_DEC_ENV
402     /* if file size has changed, we need write access, otherwise (e.g.
403      * chmod) give it a shot; if it fails, we'll discard the status
404      * info.
405      *
406      * Note that Ultrix actually defines ftruncate of a file you have open to
407      * be O.K., and does the proper access checks itself in the truncate
408      * path (unlike BSD or SUNOS), so we skip this check for Ultrix.
409      *
410      */
411 #if     defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV) || defined(AFS_LINUX22_ENV)
412     if (attrs->va_mask & AT_SIZE) {
413 #else
414 #ifdef  AFS_OSF_ENV
415     if (attrs->va_size != VNOVAL) {
416 #else
417 #ifdef  AFS_AIX41_ENV
418     if (attrs->va_size != -1) {
419 #else
420     if (attrs->va_size != ~0) {
421 #endif
422 #endif
423 #endif
424         if (!afs_AccessOK(avc, PRSFS_WRITE, &treq, DONT_CHECK_MODE_BITS)) {
425             code = EACCES;
426             goto done;
427         }
428     }
429 #endif
430
431     afs_VAttrToAS(avc, attrs, &astat);  /* interpret request */
432     code = 0;
433 #if defined(AFS_SUN_ENV) || defined(AFS_ALPHA_ENV) || defined(AFS_SUN5_ENV)
434     afs_BozonLock(&avc->pvnLock, avc);
435 #endif
436 #if     defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV)
437     if (AFS_NFSXLATORREQ(acred)) {
438         avc->execsOrWriters++;
439     }
440 #endif
441
442 #if defined(AFS_SGI_ENV)
443     AFS_RWLOCK((vnode_t *)avc, VRWLOCK_WRITE);
444 #endif
445 #if     defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV) || defined(AFS_LINUX22_ENV)
446     if (attrs->va_mask & AT_SIZE) {
447 #else
448 #if     defined(AFS_OSF_ENV)
449     if (attrs->va_size != VNOVAL) {
450 #else   /* AFS_OSF_ENV */
451     if (attrs->va_size != -1) {
452 #endif
453 #endif
454         ObtainWriteLock(&avc->lock,128);
455         avc->states |= CDirty;
456         code = afs_TruncateAllSegments(avc, (afs_int32)attrs->va_size, &treq, acred);
457         /* if date not explicitly set by this call, set it ourselves, since we
458          * changed the data */
459         if (!(astat.Mask & AFS_SETMODTIME)) {
460             astat.Mask |= AFS_SETMODTIME;
461             astat.ClientModTime = osi_Time();
462         }
463         if (code == 0) {
464           if ( ( (avc->execsOrWriters <= 0 ) && (avc->states & CCreating) == 0)
465                 || (  avc->execsOrWriters == 1 && AFS_NFSXLATORREQ(acred)) ) {
466             code = afs_StoreAllSegments(avc, &treq, AFS_ASYNC);
467             if (!code)
468               avc->states &= ~CDirty;
469           }
470         } else
471           avc->states &= ~CDirty;
472
473         ReleaseWriteLock(&avc->lock);
474         hzero(avc->flushDV);
475         osi_FlushText(avc);     /* do this after releasing all locks */
476 #ifdef AFS_DEC_ENV
477         /* in case we changed the size here, propagate it to gp->g_size */
478         afs_gfshack((struct gnode *)avc);
479 #endif
480     }
481     if (code == 0) {
482         ObtainSharedLock(&avc->lock,16);        /* lock entry */
483         code = afs_WriteVCache(avc, &astat, &treq);    /* send request */
484         ReleaseSharedLock(&avc->lock);  /* release lock */
485     }
486     if (code) {
487        ObtainWriteLock(&afs_xcbhash, 487);
488        afs_DequeueCallback(avc);
489        avc->states &= ~CStatd;      
490        ReleaseWriteLock(&afs_xcbhash);
491        if (avc->fid.Fid.Vnode & 1 || (vType(avc) == VDIR))
492            osi_dnlc_purgedp(avc);
493        /* error?  erase any changes we made to vcache entry */
494      }
495
496 #if     defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV)
497     if (AFS_NFSXLATORREQ(acred)) {
498         avc->execsOrWriters--;
499     }
500 #endif
501 #if defined(AFS_SUN_ENV) || defined(AFS_OSF_ENV) || defined(AFS_SUN5_ENV)
502     afs_BozonUnlock(&avc->pvnLock, avc);
503 #endif
504 #if defined(AFS_SGI_ENV)
505     AFS_RWUNLOCK((vnode_t *)avc, VRWLOCK_WRITE);
506 #endif
507 done:
508     code = afs_CheckCode(code, &treq, 15);
509     return code;
510 }