initial-darwin-support-20010327
[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) || defined(AFS_DARWIN_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 #if defined(AFS_AIX41_ENV) || defined(AFS_DARWIN_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) && !defined(AFS_DARWIN_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_SUN5_ENV)
192     if (flags & ATTR_HINT) {
193        code = afs_CopyOutAttrs(avc, attrs);
194        return code;
195     }
196 #endif
197
198 #if defined(AFS_SUN_ENV) || defined(AFS_ALPHA_ENV) || defined(AFS_SUN5_ENV)
199     afs_BozonLock(&avc->pvnLock, avc);
200 #endif
201
202     if (afs_shuttingdown) return EIO;
203
204     if (!(avc->states & CStatd)) {
205       if (!(code = afs_InitReq(&treq, acred))) {
206         code = afs_VerifyVCache2(avc, &treq);
207         inited = 1;
208       }
209     }
210     else code = 0;
211
212 #if defined(AFS_SUN_ENV) || defined(AFS_ALPHA_ENV) || defined(AFS_SUN5_ENV)
213     if (code == 0) osi_FlushPages(avc, acred);
214     afs_BozonUnlock(&avc->pvnLock, avc);
215 #endif
216
217
218     if (code == 0) {
219         osi_FlushText(avc); /* only needed to flush text if text locked last time */
220         code = afs_CopyOutAttrs(avc, attrs);
221
222         if (afs_nfsexporter) {
223           if (!inited) {
224             if (code = afs_InitReq(&treq, acred))
225               return code;
226              inited = 1;
227           }
228           if (AFS_NFSXLATORREQ(acred)) {
229               if ((vType(avc) != VDIR) &&
230                   !afs_AccessOK(avc, PRSFS_READ, &treq,
231                                 CHECK_MODE_BITS|CMB_ALLOW_EXEC_AS_READ)) {
232                   return EACCES;
233               }
234               if (avc->mvstat == 2) {
235 #if defined(AFS_SGI_ENV) || defined(AFS_SUN5_ENV) || defined(AFS_AIX41_ENV) || defined(AFS_DARWIN_ENV)
236                   attrs->va_mtime.tv_nsec += ((++avc->xlatordv) * 1000); 
237 #else
238                   attrs->va_mtime.tv_usec += ++avc->xlatordv; 
239 #endif
240               }
241           }
242           if (au = afs_FindUser(treq.uid, -1, READ_LOCK)) {
243             register struct afs_exporter *exporter = au->exporter;
244
245             if (exporter && !(afs_nfsexporter->exp_states & EXP_UNIXMODE)) {
246                 unsigned int ubits;
247                 /*
248                  *  If the remote user wishes to enforce default Unix mode semantics,
249                  *  like in the nfs exporter case, we OR in the user bits
250                  *  into the group and other bits. We need to do this
251                  *  because there is no RFS_ACCESS call and thus nfs
252                  *  clients implement nfs_access by interpreting the 
253                  *  mode bits in the traditional way, which of course
254                  *  loses with afs.
255                  */
256                 ubits = (attrs->va_mode & 0700) >> 6;
257                 attrs->va_mode = attrs->va_mode | ubits | (ubits << 3);
258                 /* If it's the root of AFS, replace the inode number with the
259                  * inode number of the mounted on directory; otherwise this 
260                  * confuses getwd()... */
261 #ifdef AFS_LINUX22_ENV
262                 if (avc == afs_globalVp) {
263                     struct inode *ip = avc->v.i_sb->s_root->d_inode;
264                     attrs->va_nodeid = ip->i_ino;
265                 }
266 #else
267                 if (avc->v.v_flag & VROOT) {
268                     struct vnode *vp = (struct vnode *)avc;
269
270                     vp = vp->v_vfsp->vfs_vnodecovered;
271                     if (vp) {   /* Ignore weird failures */
272 #ifdef AFS_SGI62_ENV
273                         attrs->va_nodeid = VnodeToIno(vp);
274 #else
275                         struct inode *ip;
276                         
277                         ip = (struct inode *) VTOI(vp);
278                         if (ip) /* Ignore weird failures */
279                             attrs->va_nodeid = ip->i_number;
280 #endif
281                     }
282                 }
283 #endif /* AFS_LINUX22_ENV */
284               }
285             afs_PutUser(au, READ_LOCK);
286           }
287         }
288     }
289     if (!code)
290       return 0;
291     code = afs_CheckCode(code, &treq, 14);
292     return code;
293 }
294
295 /* convert a Unix request into a status store request */
296 afs_VAttrToAS(avc, av, as)
297 register struct vcache *avc;
298 register struct vattr *av;
299 register struct AFSStoreStatus *as; {
300     register int mask;
301     mask = 0;
302     AFS_STATCNT(afs_VAttrToAS);
303 #if     defined(AFS_AIX_ENV)
304 /* Boy, was this machine dependent bogosity hard to swallow????.... */
305     if (av->va_mode != -1) {
306 #else
307 #if     defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV) || defined(AFS_LINUX22_ENV)
308     if (av->va_mask & AT_MODE) {
309 #else
310     if (av->va_mode != ((unsigned short)-1)) {
311 #endif
312 #endif
313         mask |= AFS_SETMODE;
314         as->UnixModeBits = av->va_mode & 0xffff;
315         if (avc->states & CForeign) {
316             ObtainWriteLock(&avc->lock,127);
317             afs_FreeAllAxs(&(avc->Access));
318             ReleaseWriteLock(&avc->lock);
319         }
320     }
321
322 #if defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV) || defined(AFS_LINUX22_ENV)
323     if (av->va_mask & AT_GID) {
324 #else
325 #if (defined(AFS_HPUX_ENV) || defined(AFS_SUN_ENV))
326 #if     defined(AFS_HPUX102_ENV)
327     if (av->va_gid != GID_NO_CHANGE) {
328 #else
329     if (av->va_gid != ((unsigned short)-1)) {
330 #endif
331 #else
332     if (av->va_gid != -1) {
333 #endif
334 #endif /* AFS_SUN5_ENV */
335         mask |= AFS_SETGROUP;
336         as->Group = av->va_gid;
337     }
338
339 #if defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV) || defined(AFS_LINUX22_ENV)
340     if (av->va_mask & AT_UID) {
341 #else
342 #if (defined(AFS_HPUX_ENV) || defined(AFS_SUN_ENV))
343 #if     defined(AFS_HPUX102_ENV)
344     if (av->va_uid != UID_NO_CHANGE) {
345 #else
346     if (av->va_uid != ((unsigned short)-1)) {
347 #endif
348 #else
349     if (av->va_uid != -1) {
350 #endif
351 #endif /* AFS_SUN5_ENV */
352         mask |= AFS_SETOWNER;
353         as->Owner = av->va_uid;
354     }
355 #if     defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV) || defined(AFS_LINUX22_ENV)
356     if (av->va_mask & AT_MTIME) {
357 #else
358     if (av->va_mtime.tv_sec != -1) {
359 #endif
360         mask |= AFS_SETMODTIME;
361 #ifndef AFS_SGI_ENV
362 #if     defined(AFS_SUN5_ENV) || defined(AFS_AIX41_ENV) || defined(AFS_DARWIN_ENV)
363         if (av->va_mtime.tv_nsec == -1)
364 #else
365         if (av->va_mtime.tv_usec == -1)
366 #endif
367             as->ClientModTime = osi_Time();     /* special Sys V compat hack for Suns */
368         else
369 #endif
370             as->ClientModTime = av->va_mtime.tv_sec;
371     }
372     as->Mask = mask;
373     return 0;
374 }
375
376 /* We don't set CDirty bit in avc->states because setattr calls WriteVCache
377  * synchronously, therefore, it's not needed.
378  */
379 #if     defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV)
380 afs_setattr(OSI_VC_ARG(avc), attrs, flags, acred)
381     int flags;
382 #else
383 afs_setattr(avc, attrs, acred)
384 #endif
385     OSI_VC_DECL(avc);
386     register struct vattr *attrs;
387     struct AFS_UCRED *acred; {
388     struct vrequest treq;
389     struct AFSStoreStatus astat;
390     register afs_int32 code;
391     OSI_VC_CONVERT(avc)
392
393     AFS_STATCNT(afs_setattr);
394     afs_Trace2(afs_iclSetp, CM_TRACE_SETATTR, ICL_TYPE_POINTER, avc, 
395                ICL_TYPE_INT32, avc->m.Length);
396     if (code = afs_InitReq(&treq, acred)) return code;
397     if (avc->states & CRO) {
398         code=EROFS;
399         goto done;
400     }
401 #if defined(AFS_SGI_ENV)
402     /* ignore ATTR_LAZY calls - they are really only for keeping
403      * the access/mtime of mmaped files up to date
404      */
405     if (flags & ATTR_LAZY)
406         goto done;
407 #endif
408 #ifndef AFS_DEC_ENV
409     /* if file size has changed, we need write access, otherwise (e.g.
410      * chmod) give it a shot; if it fails, we'll discard the status
411      * info.
412      *
413      * Note that Ultrix actually defines ftruncate of a file you have open to
414      * be O.K., and does the proper access checks itself in the truncate
415      * path (unlike BSD or SUNOS), so we skip this check for Ultrix.
416      *
417      */
418 #if     defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV) || defined(AFS_LINUX22_ENV)
419     if (attrs->va_mask & AT_SIZE) {
420 #else
421 #ifdef  AFS_OSF_ENV
422     if (attrs->va_size != VNOVAL) {
423 #else
424 #ifdef  AFS_AIX41_ENV
425     if (attrs->va_size != -1) {
426 #else
427     if (attrs->va_size != ~0) {
428 #endif
429 #endif
430 #endif
431         if (!afs_AccessOK(avc, PRSFS_WRITE, &treq, DONT_CHECK_MODE_BITS)) {
432             code = EACCES;
433             goto done;
434         }
435     }
436 #endif
437
438     afs_VAttrToAS(avc, attrs, &astat);  /* interpret request */
439     code = 0;
440 #if defined(AFS_SUN_ENV) || defined(AFS_ALPHA_ENV) || defined(AFS_SUN5_ENV)
441     afs_BozonLock(&avc->pvnLock, avc);
442 #endif
443 #if     defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV)
444     if (AFS_NFSXLATORREQ(acred)) {
445         avc->execsOrWriters++;
446     }
447 #endif
448
449 #if defined(AFS_SGI_ENV)
450     AFS_RWLOCK((vnode_t *)avc, VRWLOCK_WRITE);
451 #endif
452 #if     defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV) || defined(AFS_LINUX22_ENV)
453     if (attrs->va_mask & AT_SIZE) {
454 #else
455 #if     defined(AFS_OSF_ENV)
456     if (attrs->va_size != VNOVAL) {
457 #else   /* AFS_OSF_ENV */
458     if (attrs->va_size != -1) {
459 #endif
460 #endif
461         ObtainWriteLock(&avc->lock,128);
462         avc->states |= CDirty;
463         code = afs_TruncateAllSegments(avc, (afs_int32)attrs->va_size, &treq, acred);
464         /* if date not explicitly set by this call, set it ourselves, since we
465          * changed the data */
466         if (!(astat.Mask & AFS_SETMODTIME)) {
467             astat.Mask |= AFS_SETMODTIME;
468             astat.ClientModTime = osi_Time();
469         }
470         if (code == 0) {
471           if ( ( (avc->execsOrWriters <= 0 ) && (avc->states & CCreating) == 0)
472                 || (  avc->execsOrWriters == 1 && AFS_NFSXLATORREQ(acred)) ) {
473             code = afs_StoreAllSegments(avc, &treq, AFS_ASYNC);
474             if (!code)
475               avc->states &= ~CDirty;
476           }
477         } else
478           avc->states &= ~CDirty;
479
480         ReleaseWriteLock(&avc->lock);
481         hzero(avc->flushDV);
482         osi_FlushText(avc);     /* do this after releasing all locks */
483 #ifdef AFS_DEC_ENV
484         /* in case we changed the size here, propagate it to gp->g_size */
485         afs_gfshack((struct gnode *)avc);
486 #endif
487     }
488     if (code == 0) {
489         ObtainSharedLock(&avc->lock,16);        /* lock entry */
490         code = afs_WriteVCache(avc, &astat, &treq);    /* send request */
491         ReleaseSharedLock(&avc->lock);  /* release lock */
492     }
493     if (code) {
494        ObtainWriteLock(&afs_xcbhash, 487);
495        afs_DequeueCallback(avc);
496        avc->states &= ~CStatd;      
497        ReleaseWriteLock(&afs_xcbhash);
498        if (avc->fid.Fid.Vnode & 1 || (vType(avc) == VDIR))
499            osi_dnlc_purgedp(avc);
500        /* error?  erase any changes we made to vcache entry */
501      }
502
503 #if     defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV)
504     if (AFS_NFSXLATORREQ(acred)) {
505         avc->execsOrWriters--;
506     }
507 #endif
508 #if defined(AFS_SUN_ENV) || defined(AFS_OSF_ENV) || defined(AFS_SUN5_ENV)
509     afs_BozonUnlock(&avc->pvnLock, avc);
510 #endif
511 #if defined(AFS_SGI_ENV)
512     AFS_RWUNLOCK((vnode_t *)avc, VRWLOCK_WRITE);
513 #endif
514 done:
515     code = afs_CheckCode(code, &treq, 15);
516     return code;
517 }