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
11 * afs_vnop_attrs.c - setattr and getattr vnodeops
21 #include <afsconfig.h>
22 #include "afs/param.h"
26 #include "afs/sysincludes.h" /* Standard vendor system headers */
27 #include "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"
33 extern afs_rwlock_t afs_xcbhash;
34 struct afs_exporter *afs_nfsexporter;
35 extern struct vcache *afs_globalVp;
36 #if defined(AFS_HPUX110_ENV)
37 extern struct vfs *afs_globalVFS;
40 /* copy out attributes from cache entry */
41 int afs_CopyOutAttrs(register struct vcache *avc, register struct vattr *attrs)
43 register struct volume *tvp;
44 register struct cell *tcell;
47 AFS_STATCNT(afs_CopyOutAttrs);
48 if (afs_fakestat_enable && avc->mvstat == 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);
57 attrs->va_mode = fakedir ? VDIR | 0755 : avc->m.Mode;
59 #endif /* AFS_MACH_ENV */
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);
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];
73 /* XXX We try making this match the vfs's dev field XXX */
77 attrs->va_fsid = avc->v.v_mount->m_stat.f_fsid.val[0];
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. */
88 tvp->mtpoint.Fid.Vnode + (tvp->mtpoint.Fid.Volume << 16);
89 if (FidCmp(&afs_rootFid, &avc->fid) && !attrs->va_nodeid)
91 afs_PutVolume(tvp, READ_LOCK);
93 else attrs->va_nodeid = 2;
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 "%".
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 */
112 attrs->va_atime.tv_nsec = attrs->va_mtime.tv_nsec =
113 attrs->va_ctime.tv_nsec = 0;
114 attrs->va_gen = hgetlo(avc->m.DataVersion);
115 #elif defined(AFS_SGI_ENV) || defined(AFS_SUN5_ENV) || defined(AFS_AIX41_ENV) || defined(AFS_OBSD_ENV)
116 attrs->va_atime.tv_nsec = attrs->va_mtime.tv_nsec =
117 attrs->va_ctime.tv_nsec = (hgetlo(avc->m.DataVersion) & 0x7ffff) * 1000;
119 attrs->va_atime.tv_usec = attrs->va_mtime.tv_usec =
120 attrs->va_ctime.tv_usec = (hgetlo(avc->m.DataVersion) & 0x7ffff);
122 #if defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV) || defined(AFS_OSF_ENV)
125 #if defined(AFS_SGI_ENV) || defined(AFS_SUN5_ENV)
126 attrs->va_blksize = PAGESIZE; /* XXX Was 8192 XXX */
128 attrs->va_blocksize = PAGESIZE; /* XXX Was 8192 XXX */
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.
135 if (afs_globalVFS && afs_globalVFS->vfs_data)
136 attrs->va_rdev = ((struct mount *)(afs_globalVFS->vfs_data))->m_dev;
138 attrs->va_rdev = 1; /* better than nothing */
142 #if defined(AFS_HPUX110_ENV)
143 if (afs_globalVFS) attrs->va_fstype = afs_globalVFS->vfs_mtype;
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.
150 #if !defined(AFS_OSF_ENV) && !defined(AFS_DARWIN_ENV) && !defined(AFS_XBSD_ENV)
151 #if !defined(AFS_HPUX_ENV)
153 attrs->va_nblocks = (attrs->va_size? ((attrs->va_size + 1023)>>10) << 1 : 0);
154 #elif defined(AFS_SGI_ENV)
155 attrs->va_blocks = BTOBB(attrs->va_size);
157 attrs->va_blocks = (attrs->va_size? ((attrs->va_size + 1023)>>10) << 1 : 0);
159 #else /* !defined(AFS_HPUX_ENV) */
160 attrs->va_blocks = (attrs->va_size? ((attrs->va_size + 1023)>>10) : 0);
161 #endif /* !defined(AFS_HPUX_ENV) */
162 #else /* ! AFS_OSF_ENV && !AFS_DARWIN_ENV && !AFS_XBSD_ENV */
163 attrs->va_bytes = (attrs->va_size? (attrs->va_size + 1023) : 1024);
165 attrs->va_bytes_rsv = -1;
167 #endif /* ! AFS_OSF_ENV && !AFS_DARWIN_ENV && !AFS_XBSD_ENV */
169 #ifdef AFS_LINUX22_ENV
170 /* And linux has its own stash as well. */
171 vattr2inode(AFSTOV(avc), attrs);
175 afs_Trace2(afs_iclSetp, CM_TRACE_STATACLX,
176 ICL_TYPE_POINTER, attrs->va_acl,
177 ICL_TYPE_INT32, attrs->va_aclsiz);
178 if (attrs->va_acl && attrs->va_aclsiz >= 12) {
181 ap = (struct acl *) attrs->va_acl;
183 ap->acl_mode = ACL_MODE;
187 /* temporary fix ? */
188 attrs->va_aclsiz = 1;
196 #if defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV)
197 int afs_getattr(OSI_VC_DECL(avc), struct vattr *attrs, int flags, struct AFS_UCRED *acred)
199 int afs_getattr(OSI_VC_DECL(avc), struct vattr *attrs, struct AFS_UCRED *acred)
203 struct vrequest treq;
204 extern struct unixuser *afs_FindUser();
209 AFS_STATCNT(afs_getattr);
210 afs_Trace2(afs_iclSetp, CM_TRACE_GETATTR, ICL_TYPE_POINTER, avc,
211 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(avc->m.Length));
213 if (afs_fakestat_enable && avc->mvstat == 1) {
214 struct afs_fakestat_state fakestat;
216 code = afs_InitReq(&treq, acred);
217 if (code) return code;
218 afs_InitFakeStat(&fakestat);
219 code = afs_TryEvalFakeStat(&avc, &fakestat, &treq);
221 afs_PutFakeStat(&fakestat);
225 code = afs_CopyOutAttrs(avc, attrs);
226 afs_PutFakeStat(&fakestat);
230 #if defined(AFS_SUN5_ENV)
231 if (flags & ATTR_HINT) {
232 code = afs_CopyOutAttrs(avc, attrs);
236 #if defined(AFS_DARWIN_ENV)
237 if (avc->states & CUBCinit) {
238 code = afs_CopyOutAttrs(avc, attrs);
243 #if defined(AFS_SUN_ENV) || defined(AFS_ALPHA_ENV) || defined(AFS_SUN5_ENV)
244 afs_BozonLock(&avc->pvnLock, avc);
247 if (afs_shuttingdown) return EIO;
249 if (!(avc->states & CStatd)) {
250 if (!(code = afs_InitReq(&treq, acred))) {
251 code = afs_VerifyVCache2(avc, &treq);
257 #if defined(AFS_SUN_ENV) || defined(AFS_ALPHA_ENV) || defined(AFS_SUN5_ENV)
258 if (code == 0) osi_FlushPages(avc, acred);
259 afs_BozonUnlock(&avc->pvnLock, avc);
264 osi_FlushText(avc); /* only needed to flush text if text locked last time */
265 code = afs_CopyOutAttrs(avc, attrs);
267 if (afs_nfsexporter) {
269 if ((code = afs_InitReq(&treq, acred)))
273 if (AFS_NFSXLATORREQ(acred)) {
274 if ((vType(avc) != VDIR) &&
275 !afs_AccessOK(avc, PRSFS_READ, &treq,
276 CHECK_MODE_BITS|CMB_ALLOW_EXEC_AS_READ)) {
279 if (avc->mvstat == 2) {
280 #if defined(AFS_SGI_ENV) || defined(AFS_SUN5_ENV) || defined(AFS_AIX41_ENV) || defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
281 attrs->va_mtime.tv_nsec += ((++avc->xlatordv) * 1000);
283 attrs->va_mtime.tv_usec += ++avc->xlatordv;
287 if ((au = afs_FindUser(treq.uid, -1, READ_LOCK))) {
288 register struct afs_exporter *exporter = au->exporter;
290 if (exporter && !(afs_nfsexporter->exp_states & EXP_UNIXMODE)) {
293 * If the remote user wishes to enforce default Unix mode semantics,
294 * like in the nfs exporter case, we OR in the user bits
295 * into the group and other bits. We need to do this
296 * because there is no RFS_ACCESS call and thus nfs
297 * clients implement nfs_access by interpreting the
298 * mode bits in the traditional way, which of course
301 ubits = (attrs->va_mode & 0700) >> 6;
302 attrs->va_mode = attrs->va_mode | ubits | (ubits << 3);
303 /* If it's the root of AFS, replace the inode number with the
304 * inode number of the mounted on directory; otherwise this
305 * confuses getwd()... */
306 #ifdef AFS_LINUX22_ENV
307 if (avc == afs_globalVp) {
308 struct inode *ip = avc->v.i_sb->s_root->d_inode;
309 attrs->va_nodeid = ip->i_ino;
312 if (AFSTOV(avc)->v_flag & VROOT) {
313 struct vnode *vp = AFSTOV(avc);
315 vp = vp->v_vfsp->vfs_vnodecovered;
316 if (vp) { /* Ignore weird failures */
318 attrs->va_nodeid = VnodeToIno(vp);
322 ip = (struct inode *) VTOI(vp);
323 if (ip) /* Ignore weird failures */
324 attrs->va_nodeid = ip->i_number;
328 #endif /* AFS_LINUX22_ENV */
330 afs_PutUser(au, READ_LOCK);
336 code = afs_CheckCode(code, &treq, 14);
340 /* convert a Unix request into a status store request */
341 int afs_VAttrToAS(register struct vcache *avc, register struct vattr *av,
342 register struct AFSStoreStatus *as)
346 AFS_STATCNT(afs_VAttrToAS);
347 #if defined(AFS_AIX_ENV)
348 /* Boy, was this machine dependent bogosity hard to swallow????.... */
349 if (av->va_mode != -1) {
351 #if defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV) || defined(AFS_LINUX22_ENV)
352 if (av->va_mask & AT_MODE) {
354 if (av->va_mode != ((unsigned short)-1)) {
358 as->UnixModeBits = av->va_mode & 0xffff;
359 if (avc->states & CForeign) {
360 ObtainWriteLock(&avc->lock,127);
361 afs_FreeAllAxs(&(avc->Access));
362 ReleaseWriteLock(&avc->lock);
366 #if defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV) || defined(AFS_LINUX22_ENV)
367 if (av->va_mask & AT_GID) {
369 #if (defined(AFS_HPUX_ENV) || defined(AFS_SUN_ENV))
370 #if defined(AFS_HPUX102_ENV)
371 if (av->va_gid != GID_NO_CHANGE) {
373 if (av->va_gid != ((unsigned short)-1)) {
376 if (av->va_gid != -1) {
378 #endif /* AFS_SUN5_ENV */
379 mask |= AFS_SETGROUP;
380 as->Group = av->va_gid;
383 #if defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV) || defined(AFS_LINUX22_ENV)
384 if (av->va_mask & AT_UID) {
386 #if (defined(AFS_HPUX_ENV) || defined(AFS_SUN_ENV))
387 #if defined(AFS_HPUX102_ENV)
388 if (av->va_uid != UID_NO_CHANGE) {
390 if (av->va_uid != ((unsigned short)-1)) {
393 if (av->va_uid != -1) {
395 #endif /* AFS_SUN5_ENV */
396 mask |= AFS_SETOWNER;
397 as->Owner = av->va_uid;
399 #if defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV) || defined(AFS_LINUX22_ENV)
400 if (av->va_mask & AT_MTIME) {
402 if (av->va_mtime.tv_sec != -1) {
404 mask |= AFS_SETMODTIME;
406 #if defined(AFS_SUN5_ENV) || defined(AFS_AIX41_ENV) || defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
407 if (av->va_mtime.tv_nsec == -1)
409 if (av->va_mtime.tv_usec == -1)
411 as->ClientModTime = osi_Time(); /* special Sys V compat hack for Suns */
414 as->ClientModTime = av->va_mtime.tv_sec;
420 /* We don't set CDirty bit in avc->states because setattr calls WriteVCache
421 * synchronously, therefore, it's not needed.
423 #if defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV)
424 int afs_setattr(OSI_VC_DECL(avc), register struct vattr *attrs, int flags, struct AFS_UCRED *acred)
426 int afs_setattr(OSI_VC_DECL(avc), register struct vattr *attrs, struct AFS_UCRED *acred)
429 struct vrequest treq;
430 struct AFSStoreStatus astat;
431 register afs_int32 code;
432 struct afs_fakestat_state fakestate;
435 AFS_STATCNT(afs_setattr);
436 afs_Trace2(afs_iclSetp, CM_TRACE_SETATTR, ICL_TYPE_POINTER, avc,
437 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(avc->m.Length));
438 if ((code = afs_InitReq(&treq, acred))) return code;
440 afs_InitFakeStat(&fakestate);
441 code = afs_EvalFakeStat(&avc, &fakestate, &treq);
445 if (avc->states & CRO) {
449 #if defined(AFS_SGI_ENV)
450 /* ignore ATTR_LAZY calls - they are really only for keeping
451 * the access/mtime of mmaped files up to date
453 if (flags & ATTR_LAZY)
457 /* if file size has changed, we need write access, otherwise (e.g.
458 * chmod) give it a shot; if it fails, we'll discard the status
461 * Note that Ultrix actually defines ftruncate of a file you have open to
462 * be O.K., and does the proper access checks itself in the truncate
463 * path (unlike BSD or SUNOS), so we skip this check for Ultrix.
466 #if defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV) || defined(AFS_LINUX22_ENV)
467 if (attrs->va_mask & AT_SIZE) {
470 if (attrs->va_size != VNOVAL) {
473 if (attrs->va_size != -1) {
475 if (attrs->va_size != ~0) {
479 if (!afs_AccessOK(avc, PRSFS_WRITE, &treq, DONT_CHECK_MODE_BITS)) {
486 afs_VAttrToAS(avc, attrs, &astat); /* interpret request */
488 #if defined(AFS_SUN_ENV) || defined(AFS_ALPHA_ENV) || defined(AFS_SUN5_ENV)
489 afs_BozonLock(&avc->pvnLock, avc);
491 #if defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV)
492 if (AFS_NFSXLATORREQ(acred)) {
493 avc->execsOrWriters++;
497 #if defined(AFS_SGI_ENV)
498 AFS_RWLOCK((vnode_t *)avc, VRWLOCK_WRITE);
500 #if defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV) || defined(AFS_LINUX22_ENV)
501 if (attrs->va_mask & AT_SIZE) {
503 #if defined(AFS_OSF_ENV)
504 if (attrs->va_size != VNOVAL) {
505 #else /* AFS_OSF_ENV */
506 if (attrs->va_size != -1) {
509 afs_size_t tsize = attrs->va_size;
510 ObtainWriteLock(&avc->lock,128);
511 avc->states |= CDirty;
512 code = afs_TruncateAllSegments(avc, tsize, &treq, acred);
513 /* if date not explicitly set by this call, set it ourselves, since we
514 * changed the data */
515 if (!(astat.Mask & AFS_SETMODTIME)) {
516 astat.Mask |= AFS_SETMODTIME;
517 astat.ClientModTime = osi_Time();
520 if ( ( (avc->execsOrWriters <= 0 ) && (avc->states & CCreating) == 0)
521 || ( avc->execsOrWriters == 1 && AFS_NFSXLATORREQ(acred)) ) {
522 code = afs_StoreAllSegments(avc, &treq, AFS_ASYNC);
524 avc->states &= ~CDirty;
527 avc->states &= ~CDirty;
529 ReleaseWriteLock(&avc->lock);
531 osi_FlushText(avc); /* do this after releasing all locks */
533 /* in case we changed the size here, propagate it to gp->g_size */
534 afs_gfshack((struct gnode *)avc);
538 ObtainSharedLock(&avc->lock,16); /* lock entry */
539 code = afs_WriteVCache(avc, &astat, &treq); /* send request */
540 ReleaseSharedLock(&avc->lock); /* release lock */
543 ObtainWriteLock(&afs_xcbhash, 487);
544 afs_DequeueCallback(avc);
545 avc->states &= ~CStatd;
546 ReleaseWriteLock(&afs_xcbhash);
547 if (avc->fid.Fid.Vnode & 1 || (vType(avc) == VDIR))
548 osi_dnlc_purgedp(avc);
549 /* error? erase any changes we made to vcache entry */
552 #if defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV)
553 if (AFS_NFSXLATORREQ(acred)) {
554 avc->execsOrWriters--;
557 #if defined(AFS_SUN_ENV) || defined(AFS_OSF_ENV) || defined(AFS_SUN5_ENV)
558 afs_BozonUnlock(&avc->pvnLock, avc);
560 #if defined(AFS_SGI_ENV)
561 AFS_RWUNLOCK((vnode_t *)avc, VRWLOCK_WRITE);
564 afs_PutFakeStat(&fakestate);
565 code = afs_CheckCode(code, &treq, 15);