2 * Copyright 2000, International Business Machines Corporation and others.
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
9 * Portions Copyright (c) 2003 Apple Computer, Inc.
13 * afs_vnop_attrs.c - setattr and getattr vnodeops
23 #include <afsconfig.h>
24 #include "afs/param.h"
29 #include "afs/sysincludes.h" /* Standard vendor system headers */
30 #include "afsincludes.h" /* Afs-based standard headers */
31 #include "afs/afs_stats.h" /* statistics */
32 #include "afs/afs_cbqueue.h"
33 #include "afs/nfsclient.h"
34 #include "afs/afs_osidnlc.h"
36 extern afs_rwlock_t afs_xcbhash;
37 struct afs_exporter *afs_nfsexporter;
38 extern struct vcache *afs_globalVp;
39 #if defined(AFS_HPUX110_ENV)
40 extern struct vfs *afs_globalVFS;
43 /* copy out attributes from cache entry */
45 afs_CopyOutAttrs(register struct vcache *avc, register struct vattr *attrs)
47 register struct volume *tvp;
48 register struct cell *tcell;
51 AFS_STATCNT(afs_CopyOutAttrs);
52 if (afs_fakestat_enable && avc->mvstat == 1)
54 #if defined(AFS_MACH_ENV )
56 fakedir ? VDIR | 0755 : vType(avc) | (avc->m.Mode & ~VFMT);
57 #else /* AFS_MACH_ENV */
58 attrs->va_type = fakedir ? VDIR : vType(avc);
59 #if defined(AFS_SGI_ENV) || defined(AFS_AIX32_ENV) || defined(AFS_SUN5_ENV)
60 attrs->va_mode = fakedir ? 0755 : (mode_t) (avc->m.Mode & 0xffff);
62 attrs->va_mode = fakedir ? VDIR | 0755 : avc->m.Mode;
64 #endif /* AFS_MACH_ENV */
66 if (avc->m.Mode & (VSUID | VSGID)) {
67 /* setuid or setgid, make sure we're allowed to run them from this cell */
68 tcell = afs_GetCell(avc->fid.Cell, 0);
69 if (tcell && (tcell->states & CNoSUID))
70 attrs->va_mode &= ~(VSUID | VSGID);
72 #if defined(AFS_DARWIN_ENV)
74 extern u_int32_t afs_darwin_realmodes;
75 if (!afs_darwin_realmodes) {
76 /* Mac OS X uses the mode bits to determine whether a file or
77 * directory is accessible, and believes them, even though under
78 * AFS they're almost assuredly wrong, especially if the local uid
79 * does not match the AFS ID. So we set the mode bits
82 if (S_ISDIR(attrs->va_mode)) {
83 /* all access bits need to be set for directories, since even
84 * a mode 0 directory can still be used normally.
86 attrs->va_mode |= ACCESSPERMS;
88 /* for other files, replicate the user bits to group and other */
89 mode_t ubits = (attrs->va_mode & S_IRWXU) >> 6;
90 attrs->va_mode |= ubits | (ubits << 3);
94 #endif /* AFS_DARWIN_ENV */
95 attrs->va_uid = fakedir ? 0 : avc->m.Owner;
96 attrs->va_gid = fakedir ? 0 : avc->m.Group; /* yeah! */
97 #if defined(AFS_SUN56_ENV)
98 attrs->va_fsid = avc->v.v_vfsp->vfs_fsid.val[0];
101 /* XXX We try making this match the vfs's dev field XXX */
105 attrs->va_fsid = avc->v.v_mount->m_stat.f_fsid.val[0];
107 #ifdef AFS_DARWIN70_ENV
108 attrs->va_fsid = avc->v.v_mount->mnt_stat.f_fsid.val[0];
109 #else /* ! AFS_DARWIN70_ENV */
111 #endif /* AFS_DARWIN70_ENV */
114 #endif /* AFS_SUN56_ENV */
115 if (avc->mvstat == 2) {
116 tvp = afs_GetVolume(&avc->fid, 0, READ_LOCK);
117 /* The mount point's vnode. */
120 tvp->mtpoint.Fid.Vnode + (tvp->mtpoint.Fid.Volume << 16);
121 if (FidCmp(&afs_rootFid, &avc->fid) && !attrs->va_nodeid)
122 attrs->va_nodeid = 2;
123 afs_PutVolume(tvp, READ_LOCK);
125 attrs->va_nodeid = 2;
127 attrs->va_nodeid = avc->fid.Fid.Vnode + (avc->fid.Fid.Volume << 16);
128 attrs->va_nodeid &= 0x7fffffff; /* Saber C hates negative inode #s! */
129 attrs->va_nlink = fakedir ? 100 : avc->m.LinkCount;
130 attrs->va_size = fakedir ? 4096 : avc->m.Length;
131 attrs->va_atime.tv_sec = attrs->va_mtime.tv_sec = attrs->va_ctime.tv_sec =
132 fakedir ? 0 : (int)avc->m.Date;
133 /* set microseconds to be dataversion # so that we approximate NFS-style
134 * use of mtime as a dataversion #. We take it mod 512K because
135 * microseconds *must* be less than a million, and 512K is the biggest
136 * power of 2 less than such. DataVersions are typically pretty small
137 * anyway, so the difference between 512K and 1000000 shouldn't matter
138 * much, and "&" is a lot faster than "%".
140 #if defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
141 /* nfs on these systems puts an 0 in nsec and stores the nfs usec (aka
142 * dataversion) in va_gen */
144 attrs->va_atime.tv_nsec = attrs->va_mtime.tv_nsec =
145 attrs->va_ctime.tv_nsec = 0;
146 attrs->va_gen = hgetlo(avc->m.DataVersion);
147 #elif defined(AFS_SGI_ENV) || defined(AFS_SUN5_ENV) || defined(AFS_AIX41_ENV) || defined(AFS_OBSD_ENV)
148 attrs->va_atime.tv_nsec = attrs->va_mtime.tv_nsec =
149 attrs->va_ctime.tv_nsec =
150 (hgetlo(avc->m.DataVersion) & 0x7ffff) * 1000;
152 attrs->va_atime.tv_usec = attrs->va_mtime.tv_usec =
153 attrs->va_ctime.tv_usec = (hgetlo(avc->m.DataVersion) & 0x7ffff);
155 #if defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV) || defined(AFS_OSF_ENV)
158 #if defined(AFS_SGI_ENV) || defined(AFS_SUN5_ENV)
159 attrs->va_blksize = PAGESIZE; /* XXX Was 8192 XXX */
161 attrs->va_blocksize = PAGESIZE; /* XXX Was 8192 XXX */
164 /* Have to use real device #s in Ultrix, since that's how FS type is
165 * encoded. If rdev doesn't match Ultrix equivalent of statfs's rdev, then
166 * "df ." doesn't work.
168 if (afs_globalVFS && afs_globalVFS->vfs_data)
169 attrs->va_rdev = ((struct mount *)(afs_globalVFS->vfs_data))->m_dev;
171 attrs->va_rdev = 1; /* better than nothing */
175 #if defined(AFS_HPUX110_ENV)
177 attrs->va_fstype = afs_globalVFS->vfs_mtype;
181 * Below return 0 (and not 1) blocks if the file is zero length. This conforms
182 * better with the other filesystems that do return 0.
184 #if !defined(AFS_OSF_ENV) && !defined(AFS_DARWIN_ENV) && !defined(AFS_XBSD_ENV)
185 #if !defined(AFS_HPUX_ENV)
188 (attrs->va_size ? ((attrs->va_size + 1023) >> 10) << 1 : 0);
189 #elif defined(AFS_SGI_ENV)
190 attrs->va_blocks = BTOBB(attrs->va_size);
193 (attrs->va_size ? ((attrs->va_size + 1023) >> 10) << 1 : 0);
195 #else /* !defined(AFS_HPUX_ENV) */
196 attrs->va_blocks = (attrs->va_size ? ((attrs->va_size + 1023) >> 10) : 0);
197 #endif /* !defined(AFS_HPUX_ENV) */
198 #else /* ! AFS_OSF_ENV && !AFS_DARWIN_ENV && !AFS_XBSD_ENV */
199 attrs->va_bytes = (attrs->va_size ? (attrs->va_size + 1023) : 1024);
201 attrs->va_bytes_rsv = -1;
203 #endif /* ! AFS_OSF_ENV && !AFS_DARWIN_ENV && !AFS_XBSD_ENV */
205 #ifdef AFS_LINUX22_ENV
206 /* And linux has its own stash as well. */
207 vattr2inode(AFSTOV(avc), attrs);
211 afs_Trace2(afs_iclSetp, CM_TRACE_STATACLX, ICL_TYPE_POINTER,
212 attrs->va_acl, ICL_TYPE_INT32, attrs->va_aclsiz);
213 if (attrs->va_acl && attrs->va_aclsiz >= 12) {
216 ap = (struct acl *)attrs->va_acl;
218 ap->acl_mode = ACL_MODE;
222 /* temporary fix ? */
223 attrs->va_aclsiz = 1;
231 #if defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV)
233 afs_getattr(OSI_VC_DECL(avc), struct vattr *attrs, int flags,
234 struct AFS_UCRED *acred)
237 afs_getattr(OSI_VC_DECL(avc), struct vattr *attrs, struct AFS_UCRED *acred)
241 struct vrequest treq;
242 extern struct unixuser *afs_FindUser();
247 AFS_STATCNT(afs_getattr);
248 afs_Trace2(afs_iclSetp, CM_TRACE_GETATTR, ICL_TYPE_POINTER, avc,
249 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(avc->m.Length));
251 if (afs_fakestat_enable && avc->mvstat == 1) {
252 struct afs_fakestat_state fakestat;
254 code = afs_InitReq(&treq, acred);
257 afs_InitFakeStat(&fakestat);
258 code = afs_TryEvalFakeStat(&avc, &fakestat, &treq);
260 afs_PutFakeStat(&fakestat);
264 code = afs_CopyOutAttrs(avc, attrs);
265 afs_PutFakeStat(&fakestat);
268 #if defined(AFS_SUN5_ENV)
269 if (flags & ATTR_HINT) {
270 code = afs_CopyOutAttrs(avc, attrs);
274 #if defined(AFS_DARWIN_ENV)
275 if (avc->states & CUBCinit) {
276 code = afs_CopyOutAttrs(avc, attrs);
281 #if defined(AFS_SUN_ENV) || defined(AFS_ALPHA_ENV) || defined(AFS_SUN5_ENV)
282 afs_BozonLock(&avc->pvnLock, avc);
285 if (afs_shuttingdown)
288 if (!(avc->states & CStatd)) {
289 if (!(code = afs_InitReq(&treq, acred))) {
290 code = afs_VerifyVCache2(avc, &treq);
296 #if defined(AFS_SUN_ENV) || defined(AFS_ALPHA_ENV) || defined(AFS_SUN5_ENV)
298 osi_FlushPages(avc, acred);
299 afs_BozonUnlock(&avc->pvnLock, avc);
304 osi_FlushText(avc); /* only needed to flush text if text locked last time */
305 code = afs_CopyOutAttrs(avc, attrs);
307 if (afs_nfsexporter) {
309 if ((code = afs_InitReq(&treq, acred)))
313 if (AFS_NFSXLATORREQ(acred)) {
314 if ((vType(avc) != VDIR)
315 && !afs_AccessOK(avc, PRSFS_READ, &treq,
317 CMB_ALLOW_EXEC_AS_READ)) {
321 /* The effect of the following is to force the NFS client to refetch the
322 * volume root every time, since the mtime changes. For Solaris 9 NFSv3
323 * clients, this means looping forever, since for some reason (related
324 * to caching?) it wants the mtime to be consistent two reads in a row.
325 * Why are volume roots special???
328 if (avc->mvstat == 2) {
329 #if defined(AFS_SGI_ENV) || defined(AFS_SUN5_ENV) || defined(AFS_AIX41_ENV) || defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
330 attrs->va_mtime.tv_nsec += ((++avc->xlatordv) * 1000);
332 attrs->va_mtime.tv_usec += ++avc->xlatordv;
337 if ((au = afs_FindUser(treq.uid, -1, READ_LOCK))) {
338 register struct afs_exporter *exporter = au->exporter;
340 if (exporter && !(afs_nfsexporter->exp_states & EXP_UNIXMODE)) {
343 * If the remote user wishes to enforce default Unix mode semantics,
344 * like in the nfs exporter case, we OR in the user bits
345 * into the group and other bits. We need to do this
346 * because there is no RFS_ACCESS call and thus nfs
347 * clients implement nfs_access by interpreting the
348 * mode bits in the traditional way, which of course
351 ubits = (attrs->va_mode & 0700) >> 6;
352 attrs->va_mode = attrs->va_mode | ubits | (ubits << 3);
353 /* If it's the root of AFS, replace the inode number with the
354 * inode number of the mounted on directory; otherwise this
355 * confuses getwd()... */
356 #ifdef AFS_LINUX22_ENV
357 if (avc == afs_globalVp) {
358 struct inode *ip = avc->v.i_sb->s_root->d_inode;
359 attrs->va_nodeid = ip->i_ino;
362 if (AFSTOV(avc)->v_flag & VROOT) {
363 struct vnode *vp = AFSTOV(avc);
365 vp = vp->v_vfsp->vfs_vnodecovered;
366 if (vp) { /* Ignore weird failures */
368 attrs->va_nodeid = VnodeToIno(vp);
372 ip = (struct inode *)VTOI(vp);
373 if (ip) /* Ignore weird failures */
374 attrs->va_nodeid = ip->i_number;
378 #endif /* AFS_LINUX22_ENV */
380 afs_PutUser(au, READ_LOCK);
386 code = afs_CheckCode(code, &treq, 14);
390 /* convert a Unix request into a status store request */
392 afs_VAttrToAS(register struct vcache *avc, register struct vattr *av,
393 register struct AFSStoreStatus *as)
397 AFS_STATCNT(afs_VAttrToAS);
398 #if defined(AFS_AIX_ENV)
399 /* Boy, was this machine dependent bogosity hard to swallow????.... */
400 if (av->va_mode != -1) {
402 #if defined(AFS_LINUX22_ENV)
403 if (av->va_mask & ATTR_MODE) {
405 #if defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV)
406 if (av->va_mask & AT_MODE) {
408 if (av->va_mode != ((unsigned short)-1)) {
413 as->UnixModeBits = av->va_mode & 0xffff;
414 if (avc->states & CForeign) {
415 ObtainWriteLock(&avc->lock, 127);
416 afs_FreeAllAxs(&(avc->Access));
417 ReleaseWriteLock(&avc->lock);
420 #if defined(AFS_LINUX22_ENV)
421 if (av->va_mask & ATTR_GID) {
423 #if defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV)
424 if (av->va_mask & AT_GID) {
426 #if (defined(AFS_HPUX_ENV) || defined(AFS_SUN_ENV))
427 #if defined(AFS_HPUX102_ENV)
428 if (av->va_gid != GID_NO_CHANGE) {
430 if (av->va_gid != ((unsigned short)-1)) {
433 if (av->va_gid != -1) {
435 #endif /* AFS_SUN5_ENV */
436 #endif /* AFS_LINUX22_ENV */
437 mask |= AFS_SETGROUP;
438 as->Group = av->va_gid;
440 #if defined(AFS_LINUX22_ENV)
441 if (av->va_mask & ATTR_UID) {
443 #if defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV)
444 if (av->va_mask & AT_UID) {
446 #if (defined(AFS_HPUX_ENV) || defined(AFS_SUN_ENV))
447 #if defined(AFS_HPUX102_ENV)
448 if (av->va_uid != UID_NO_CHANGE) {
450 if (av->va_uid != ((unsigned short)-1)) {
453 if (av->va_uid != -1) {
455 #endif /* AFS_SUN5_ENV */
456 #endif /* AFS_LINUX22_ENV */
457 mask |= AFS_SETOWNER;
458 as->Owner = av->va_uid;
460 #if defined(AFS_LINUX22_ENV)
461 if (av->va_mask & ATTR_MTIME) {
463 #if defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV)
464 if (av->va_mask & AT_MTIME) {
466 if (av->va_mtime.tv_sec != -1) {
469 mask |= AFS_SETMODTIME;
471 #if defined(AFS_SUN5_ENV) || defined(AFS_AIX41_ENV) || defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
472 if (av->va_mtime.tv_nsec == -1)
474 if (av->va_mtime.tv_usec == -1)
476 as->ClientModTime = osi_Time(); /* special Sys V compat hack for Suns */
479 as->ClientModTime = av->va_mtime.tv_sec;
485 /* We don't set CDirty bit in avc->states because setattr calls WriteVCache
486 * synchronously, therefore, it's not needed.
488 #if defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV)
490 afs_setattr(OSI_VC_DECL(avc), register struct vattr *attrs, int flags,
491 struct AFS_UCRED *acred)
494 afs_setattr(OSI_VC_DECL(avc), register struct vattr *attrs,
495 struct AFS_UCRED *acred)
498 struct vrequest treq;
499 struct AFSStoreStatus astat;
500 register afs_int32 code;
501 struct afs_fakestat_state fakestate;
504 AFS_STATCNT(afs_setattr);
505 #if defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV) || defined(AFS_LINUX22_ENV)
506 afs_Trace4(afs_iclSetp, CM_TRACE_SETATTR, ICL_TYPE_POINTER, avc,
507 ICL_TYPE_INT32, attrs->va_mask, ICL_TYPE_OFFSET,
508 ICL_HANDLE_OFFSET(attrs->va_size), ICL_TYPE_OFFSET,
509 ICL_HANDLE_OFFSET(avc->m.Length));
511 afs_Trace4(afs_iclSetp, CM_TRACE_SETATTR, ICL_TYPE_POINTER, avc,
512 ICL_TYPE_INT32, attrs->va_mode, ICL_TYPE_OFFSET,
513 ICL_HANDLE_OFFSET(attrs->va_size), ICL_TYPE_OFFSET,
514 ICL_HANDLE_OFFSET(avc->m.Length));
516 if ((code = afs_InitReq(&treq, acred)))
519 afs_InitFakeStat(&fakestate);
520 code = afs_EvalFakeStat(&avc, &fakestate, &treq);
524 if (avc->states & CRO) {
528 #if defined(AFS_SGI_ENV)
529 /* ignore ATTR_LAZY calls - they are really only for keeping
530 * the access/mtime of mmaped files up to date
532 if (flags & ATTR_LAZY)
536 /* if file size has changed, we need write access, otherwise (e.g.
537 * chmod) give it a shot; if it fails, we'll discard the status
540 * Note that Ultrix actually defines ftruncate of a file you have open to
541 * be O.K., and does the proper access checks itself in the truncate
542 * path (unlike BSD or SUNOS), so we skip this check for Ultrix.
545 #if defined(AFS_LINUX22_ENV)
546 if (attrs->va_mask & ATTR_SIZE) {
548 #if defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV)
549 if (attrs->va_mask & AT_SIZE) {
552 if (attrs->va_size != VNOVAL) {
555 if (attrs->va_size != -1) {
557 if (attrs->va_size != ~0) {
562 if (!afs_AccessOK(avc, PRSFS_WRITE, &treq, DONT_CHECK_MODE_BITS)) {
569 afs_VAttrToAS(avc, attrs, &astat); /* interpret request */
571 #if defined(AFS_SUN_ENV) || defined(AFS_ALPHA_ENV) || defined(AFS_SUN5_ENV)
572 afs_BozonLock(&avc->pvnLock, avc);
574 #if defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV)
575 if (AFS_NFSXLATORREQ(acred)) {
576 avc->execsOrWriters++;
580 #if defined(AFS_SGI_ENV)
581 AFS_RWLOCK((vnode_t *) avc, VRWLOCK_WRITE);
583 #if defined(AFS_LINUX22_ENV)
584 if (attrs->va_mask & ATTR_SIZE) {
586 #if defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV)
587 if (attrs->va_mask & AT_SIZE) {
589 #if defined(AFS_OSF_ENV)
590 if (attrs->va_size != VNOVAL) {
591 #else /* AFS_OSF_ENV */
592 if (attrs->va_size != -1) {
596 afs_size_t tsize = attrs->va_size;
597 ObtainWriteLock(&avc->lock, 128);
598 avc->states |= CDirty;
599 code = afs_TruncateAllSegments(avc, tsize, &treq, acred);
600 /* if date not explicitly set by this call, set it ourselves, since we
601 * changed the data */
602 if (!(astat.Mask & AFS_SETMODTIME)) {
603 astat.Mask |= AFS_SETMODTIME;
604 astat.ClientModTime = osi_Time();
607 if (((avc->execsOrWriters <= 0) && (avc->states & CCreating) == 0)
608 || (avc->execsOrWriters == 1 && AFS_NFSXLATORREQ(acred))) {
609 code = afs_StoreAllSegments(avc, &treq, AFS_ASYNC);
611 avc->states &= ~CDirty;
614 avc->states &= ~CDirty;
616 ReleaseWriteLock(&avc->lock);
618 osi_FlushText(avc); /* do this after releasing all locks */
620 /* in case we changed the size here, propagate it to gp->g_size */
621 afs_gfshack((struct gnode *)avc);
625 ObtainSharedLock(&avc->lock, 16); /* lock entry */
626 code = afs_WriteVCache(avc, &astat, &treq); /* send request */
627 ReleaseSharedLock(&avc->lock); /* release lock */
630 ObtainWriteLock(&afs_xcbhash, 487);
631 afs_DequeueCallback(avc);
632 avc->states &= ~CStatd;
633 ReleaseWriteLock(&afs_xcbhash);
634 if (avc->fid.Fid.Vnode & 1 || (vType(avc) == VDIR))
635 osi_dnlc_purgedp(avc);
636 /* error? erase any changes we made to vcache entry */
638 #if defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV)
639 if (AFS_NFSXLATORREQ(acred)) {
640 avc->execsOrWriters--;
643 #if defined(AFS_SUN_ENV) || defined(AFS_OSF_ENV) || defined(AFS_SUN5_ENV)
644 afs_BozonUnlock(&avc->pvnLock, avc);
646 #if defined(AFS_SGI_ENV)
647 AFS_RWUNLOCK((vnode_t *) avc, VRWLOCK_WRITE);
650 afs_PutFakeStat(&fakestate);
651 code = afs_CheckCode(code, &treq, 15);