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 "../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"
33 extern afs_rwlock_t afs_xcbhash;
34 struct afs_exporter *afs_nfsexporter;
35 extern struct vcache *afs_globalVp;
37 /* copy out attributes from cache entry */
38 afs_CopyOutAttrs(avc, attrs)
39 register struct vattr *attrs;
40 register struct vcache *avc;
42 register struct volume *tvp;
43 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 : 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_blocksize = PAGESIZE; /* XXX Was 8192 XXX */
115 attrs->va_gen = hgetlo(avc->m.DataVersion);
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 */
125 attrs->va_blksize = PAGESIZE; /* XXX Was 8192 XXX */
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 */
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.
139 if (afs_globalVFS && afs_globalVFS->vfs_data)
140 attrs->va_rdev = ((struct mount *)(afs_globalVFS->vfs_data))->m_dev;
142 attrs->va_rdev = 1; /* better than nothing */
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)
152 attrs->va_size_rsv = 0;
155 /* attrs->va_gen = avc->m.DataVersion;*/
157 #endif /* AFS_OSF_ENV || AFS_DARWIN_ENV */
159 #if !defined(AFS_OSF_ENV) && !defined(AFS_DARWIN_ENV) && !defined(AFS_FBSD_ENV)
160 #if !defined(AFS_HPUX_ENV)
162 attrs->va_nblocks = (attrs->va_size? ((attrs->va_size + 1023)>>10) << 1 : 0);
164 #if defined(AFS_SGI_ENV)
165 attrs->va_blocks = BTOBB(attrs->va_size);
167 attrs->va_blocks = (attrs->va_size? ((attrs->va_size + 1023)>>10) << 1 : 0);
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);
176 attrs->va_bytes_rsv = -1;
178 #endif /* ! AFS_OSF_ENV */
180 #ifdef AFS_LINUX22_ENV
181 /* And linux has it's own stash as well. */
182 vattr2inode(AFSTOV(avc), attrs);
189 #if defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV)
190 afs_getattr(OSI_VC_ARG(avc), attrs, flags, acred)
193 afs_getattr(OSI_VC_ARG(avc), attrs, acred)
197 struct AFS_UCRED *acred;
200 struct vrequest treq;
201 extern struct unixuser *afs_FindUser();
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));
210 if (afs_fakestat_enable && avc->mvstat == 1) {
211 struct afs_fakestat_state fakestat;
213 code = afs_InitReq(&treq, acred);
214 if (code) return code;
215 afs_InitFakeStat(&fakestat);
216 code = afs_TryEvalFakeStat(&avc, &fakestat, &treq);
218 afs_PutFakeStat(&fakestat);
222 code = afs_CopyOutAttrs(avc, attrs);
223 afs_PutFakeStat(&fakestat);
227 #if defined(AFS_SUN5_ENV)
228 if (flags & ATTR_HINT) {
229 code = afs_CopyOutAttrs(avc, attrs);
234 #if defined(AFS_SUN_ENV) || defined(AFS_ALPHA_ENV) || defined(AFS_SUN5_ENV)
235 afs_BozonLock(&avc->pvnLock, avc);
238 if (afs_shuttingdown) return EIO;
240 if (!(avc->states & CStatd)) {
241 if (!(code = afs_InitReq(&treq, acred))) {
242 code = afs_VerifyVCache2(avc, &treq);
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);
255 osi_FlushText(avc); /* only needed to flush text if text locked last time */
256 code = afs_CopyOutAttrs(avc, attrs);
258 if (afs_nfsexporter) {
260 if (code = afs_InitReq(&treq, acred))
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)) {
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);
274 attrs->va_mtime.tv_usec += ++avc->xlatordv;
278 if (au = afs_FindUser(treq.uid, -1, READ_LOCK)) {
279 register struct afs_exporter *exporter = au->exporter;
281 if (exporter && !(afs_nfsexporter->exp_states & EXP_UNIXMODE)) {
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
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;
303 if (AFSTOV(avc)->v_flag & VROOT) {
304 struct vnode *vp = AFSTOV(avc);
306 vp = vp->v_vfsp->vfs_vnodecovered;
307 if (vp) { /* Ignore weird failures */
309 attrs->va_nodeid = VnodeToIno(vp);
313 ip = (struct inode *) VTOI(vp);
314 if (ip) /* Ignore weird failures */
315 attrs->va_nodeid = ip->i_number;
319 #endif /* AFS_LINUX22_ENV */
321 afs_PutUser(au, READ_LOCK);
327 code = afs_CheckCode(code, &treq, 14);
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; {
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) {
343 #if defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV) || defined(AFS_LINUX22_ENV)
344 if (av->va_mask & AT_MODE) {
346 if (av->va_mode != ((unsigned short)-1)) {
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);
358 #if defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV) || defined(AFS_LINUX22_ENV)
359 if (av->va_mask & AT_GID) {
361 #if (defined(AFS_HPUX_ENV) || defined(AFS_SUN_ENV))
362 #if defined(AFS_HPUX102_ENV)
363 if (av->va_gid != GID_NO_CHANGE) {
365 if (av->va_gid != ((unsigned short)-1)) {
368 if (av->va_gid != -1) {
370 #endif /* AFS_SUN5_ENV */
371 mask |= AFS_SETGROUP;
372 as->Group = av->va_gid;
375 #if defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV) || defined(AFS_LINUX22_ENV)
376 if (av->va_mask & AT_UID) {
378 #if (defined(AFS_HPUX_ENV) || defined(AFS_SUN_ENV))
379 #if defined(AFS_HPUX102_ENV)
380 if (av->va_uid != UID_NO_CHANGE) {
382 if (av->va_uid != ((unsigned short)-1)) {
385 if (av->va_uid != -1) {
387 #endif /* AFS_SUN5_ENV */
388 mask |= AFS_SETOWNER;
389 as->Owner = av->va_uid;
391 #if defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV) || defined(AFS_LINUX22_ENV)
392 if (av->va_mask & AT_MTIME) {
394 if (av->va_mtime.tv_sec != -1) {
396 mask |= AFS_SETMODTIME;
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)
401 if (av->va_mtime.tv_usec == -1)
403 as->ClientModTime = osi_Time(); /* special Sys V compat hack for Suns */
406 as->ClientModTime = av->va_mtime.tv_sec;
412 /* We don't set CDirty bit in avc->states because setattr calls WriteVCache
413 * synchronously, therefore, it's not needed.
415 #if defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV)
416 afs_setattr(OSI_VC_ARG(avc), attrs, flags, acred)
419 afs_setattr(avc, attrs, acred)
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;
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;
435 afs_InitFakeStat(&fakestate);
436 code = afs_EvalFakeStat(&avc, &fakestate, &treq);
440 if (avc->states & CRO) {
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
448 if (flags & ATTR_LAZY)
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
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.
461 #if defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV) || defined(AFS_LINUX22_ENV)
462 if (attrs->va_mask & AT_SIZE) {
465 if (attrs->va_size != VNOVAL) {
468 if (attrs->va_size != -1) {
470 if (attrs->va_size != ~0) {
474 if (!afs_AccessOK(avc, PRSFS_WRITE, &treq, DONT_CHECK_MODE_BITS)) {
481 afs_VAttrToAS(avc, attrs, &astat); /* interpret request */
483 #if defined(AFS_SUN_ENV) || defined(AFS_ALPHA_ENV) || defined(AFS_SUN5_ENV)
484 afs_BozonLock(&avc->pvnLock, avc);
486 #if defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV)
487 if (AFS_NFSXLATORREQ(acred)) {
488 avc->execsOrWriters++;
492 #if defined(AFS_SGI_ENV)
493 AFS_RWLOCK((vnode_t *)avc, VRWLOCK_WRITE);
495 #if defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV) || defined(AFS_LINUX22_ENV)
496 if (attrs->va_mask & AT_SIZE) {
498 #if defined(AFS_OSF_ENV)
499 if (attrs->va_size != VNOVAL) {
500 #else /* AFS_OSF_ENV */
501 if (attrs->va_size != -1) {
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();
515 if ( ( (avc->execsOrWriters <= 0 ) && (avc->states & CCreating) == 0)
516 || ( avc->execsOrWriters == 1 && AFS_NFSXLATORREQ(acred)) ) {
517 code = afs_StoreAllSegments(avc, &treq, AFS_ASYNC);
519 avc->states &= ~CDirty;
522 avc->states &= ~CDirty;
524 ReleaseWriteLock(&avc->lock);
526 osi_FlushText(avc); /* do this after releasing all locks */
528 /* in case we changed the size here, propagate it to gp->g_size */
529 afs_gfshack((struct gnode *)avc);
533 ObtainSharedLock(&avc->lock,16); /* lock entry */
534 code = afs_WriteVCache(avc, &astat, &treq); /* send request */
535 ReleaseSharedLock(&avc->lock); /* release lock */
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 */
547 #if defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV)
548 if (AFS_NFSXLATORREQ(acred)) {
549 avc->execsOrWriters--;
552 #if defined(AFS_SUN_ENV) || defined(AFS_OSF_ENV) || defined(AFS_SUN5_ENV)
553 afs_BozonUnlock(&avc->pvnLock, avc);
555 #if defined(AFS_SGI_ENV)
556 AFS_RWUNLOCK((vnode_t *)avc, VRWLOCK_WRITE);
559 afs_PutFakeStat(&fakestate);
560 code = afs_CheckCode(code, &treq, 15);