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