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