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