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; {
41 register struct volume *tvp;
42 register struct cell *tcell;
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);
53 attrs->va_mode = avc->m.Mode;
55 #endif /* AFS_MACH_ENV */
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);
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];
69 /* XXX We try making this match the vfs's dev field XXX */
73 attrs->va_fsid = avc->v.v_mount->m_stat.f_fsid.val[0];
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. */
84 tvp->mtpoint.Fid.Vnode + (tvp->mtpoint.Fid.Volume << 16);
85 if (FidCmp(&afs_rootFid, &avc->fid) && !attrs->va_nodeid)
87 afs_PutVolume(tvp, READ_LOCK);
89 else attrs->va_nodeid = 2;
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 =
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 "%".
104 #if defined(AFS_SGI_ENV) || defined(AFS_SUN5_ENV) || defined(AFS_AIX41_ENV) || defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
105 attrs->va_atime.tv_nsec = attrs->va_mtime.tv_nsec =
106 attrs->va_ctime.tv_nsec =
107 (hgetlo(avc->m.DataVersion) & 0x7ffff) * 1000;
108 #if defined(AFS_AIX41_ENV) || defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
109 attrs->va_blocksize = PAGESIZE; /* XXX Was 8192 XXX */
111 attrs->va_blksize = PAGESIZE; /* XXX Was 8192 XXX */
114 attrs->va_atime.tv_usec = attrs->va_mtime.tv_usec =
115 attrs->va_ctime.tv_usec =
116 (hgetlo(avc->m.DataVersion) & 0x7ffff);
117 attrs->va_blocksize = PAGESIZE; /* XXX Was 8192 XXX */
120 /* Have to use real device #s in Ultrix, since that's how FS type is
121 * encoded. If rdev doesn't match Ultrix equivalent of statfs's rdev, then
122 * "df ." doesn't work.
124 if (afs_globalVFS && afs_globalVFS->vfs_data)
125 attrs->va_rdev = ((struct mount *)(afs_globalVFS->vfs_data))->m_dev;
127 attrs->va_rdev = 1; /* better than nothing */
132 * Below return 0 (and not 1) blocks if the file is zero length. This conforms
133 * better with the other filesystems that do return 0.
135 #if defined(AFS_OSF_ENV) || defined(AFS_DARWIN_ENV)
137 attrs->va_size_rsv = 0;
140 /* attrs->va_gen = avc->m.DataVersion;*/
142 #endif /* AFS_OSF_ENV || AFS_DARWIN_ENV */
144 #if !defined(AFS_OSF_ENV) && !defined(AFS_DARWIN_ENV) && !defined(AFS_FBSD_ENV)
145 #if !defined(AFS_HPUX_ENV)
147 attrs->va_nblocks = (attrs->va_size? ((attrs->va_size + 1023)>>10) << 1 : 0);
149 #if defined(AFS_SGI_ENV)
150 attrs->va_blocks = BTOBB(attrs->va_size);
152 attrs->va_blocks = (attrs->va_size? ((attrs->va_size + 1023)>>10) << 1 : 0);
155 #else /* !defined(AFS_HPUX_ENV) */
156 attrs->va_blocks = (attrs->va_size? ((attrs->va_size + 1023)>>10) : 0);
157 #endif /* !defined(AFS_HPUX_ENV) */
158 #else /* ! AFS_OSF_ENV && !AFS_FBSD_ENV */
159 attrs->va_bytes = (attrs->va_size? (attrs->va_size + 1023) : 1024);
161 attrs->va_bytes_rsv = -1;
163 #endif /* ! AFS_OSF_ENV */
165 #ifdef AFS_LINUX22_ENV
166 /* And linux has it's own stash as well. */
167 vattr2inode((struct inode*)avc, attrs);
174 #if defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV)
175 afs_getattr(OSI_VC_ARG(avc), attrs, flags, acred)
178 afs_getattr(OSI_VC_ARG(avc), attrs, acred)
182 struct AFS_UCRED *acred;
185 struct vrequest treq;
186 extern struct unixuser *afs_FindUser();
191 AFS_STATCNT(afs_getattr);
192 afs_Trace2(afs_iclSetp, CM_TRACE_GETATTR, ICL_TYPE_POINTER, avc,
193 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(avc->m.Length));
195 #if defined(AFS_SUN5_ENV)
196 if (flags & ATTR_HINT) {
197 code = afs_CopyOutAttrs(avc, attrs);
202 #if defined(AFS_SUN_ENV) || defined(AFS_ALPHA_ENV) || defined(AFS_SUN5_ENV)
203 afs_BozonLock(&avc->pvnLock, avc);
206 if (afs_shuttingdown) return EIO;
208 if (!(avc->states & CStatd)) {
209 if (!(code = afs_InitReq(&treq, acred))) {
210 code = afs_VerifyVCache2(avc, &treq);
216 #if defined(AFS_SUN_ENV) || defined(AFS_ALPHA_ENV) || defined(AFS_SUN5_ENV)
217 if (code == 0) osi_FlushPages(avc, acred);
218 afs_BozonUnlock(&avc->pvnLock, avc);
223 osi_FlushText(avc); /* only needed to flush text if text locked last time */
224 code = afs_CopyOutAttrs(avc, attrs);
226 if (afs_nfsexporter) {
228 if (code = afs_InitReq(&treq, acred))
232 if (AFS_NFSXLATORREQ(acred)) {
233 if ((vType(avc) != VDIR) &&
234 !afs_AccessOK(avc, PRSFS_READ, &treq,
235 CHECK_MODE_BITS|CMB_ALLOW_EXEC_AS_READ)) {
238 if (avc->mvstat == 2) {
239 #if defined(AFS_SGI_ENV) || defined(AFS_SUN5_ENV) || defined(AFS_AIX41_ENV) || defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
240 attrs->va_mtime.tv_nsec += ((++avc->xlatordv) * 1000);
242 attrs->va_mtime.tv_usec += ++avc->xlatordv;
246 if (au = afs_FindUser(treq.uid, -1, READ_LOCK)) {
247 register struct afs_exporter *exporter = au->exporter;
249 if (exporter && !(afs_nfsexporter->exp_states & EXP_UNIXMODE)) {
252 * If the remote user wishes to enforce default Unix mode semantics,
253 * like in the nfs exporter case, we OR in the user bits
254 * into the group and other bits. We need to do this
255 * because there is no RFS_ACCESS call and thus nfs
256 * clients implement nfs_access by interpreting the
257 * mode bits in the traditional way, which of course
260 ubits = (attrs->va_mode & 0700) >> 6;
261 attrs->va_mode = attrs->va_mode | ubits | (ubits << 3);
262 /* If it's the root of AFS, replace the inode number with the
263 * inode number of the mounted on directory; otherwise this
264 * confuses getwd()... */
265 #ifdef AFS_LINUX22_ENV
266 if (avc == afs_globalVp) {
267 struct inode *ip = avc->v.i_sb->s_root->d_inode;
268 attrs->va_nodeid = ip->i_ino;
271 if (avc->v.v_flag & VROOT) {
272 struct vnode *vp = (struct vnode *)avc;
274 vp = vp->v_vfsp->vfs_vnodecovered;
275 if (vp) { /* Ignore weird failures */
277 attrs->va_nodeid = VnodeToIno(vp);
281 ip = (struct inode *) VTOI(vp);
282 if (ip) /* Ignore weird failures */
283 attrs->va_nodeid = ip->i_number;
287 #endif /* AFS_LINUX22_ENV */
289 afs_PutUser(au, READ_LOCK);
295 code = afs_CheckCode(code, &treq, 14);
299 /* convert a Unix request into a status store request */
300 afs_VAttrToAS(avc, av, as)
301 register struct vcache *avc;
302 register struct vattr *av;
303 register struct AFSStoreStatus *as; {
306 AFS_STATCNT(afs_VAttrToAS);
307 #if defined(AFS_AIX_ENV)
308 /* Boy, was this machine dependent bogosity hard to swallow????.... */
309 if (av->va_mode != -1) {
311 #if defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV) || defined(AFS_LINUX22_ENV)
312 if (av->va_mask & AT_MODE) {
314 if (av->va_mode != ((unsigned short)-1)) {
318 as->UnixModeBits = av->va_mode & 0xffff;
319 if (avc->states & CForeign) {
320 ObtainWriteLock(&avc->lock,127);
321 afs_FreeAllAxs(&(avc->Access));
322 ReleaseWriteLock(&avc->lock);
326 #if defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV) || defined(AFS_LINUX22_ENV)
327 if (av->va_mask & AT_GID) {
329 #if (defined(AFS_HPUX_ENV) || defined(AFS_SUN_ENV))
330 #if defined(AFS_HPUX102_ENV)
331 if (av->va_gid != GID_NO_CHANGE) {
333 if (av->va_gid != ((unsigned short)-1)) {
336 if (av->va_gid != -1) {
338 #endif /* AFS_SUN5_ENV */
339 mask |= AFS_SETGROUP;
340 as->Group = av->va_gid;
343 #if defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV) || defined(AFS_LINUX22_ENV)
344 if (av->va_mask & AT_UID) {
346 #if (defined(AFS_HPUX_ENV) || defined(AFS_SUN_ENV))
347 #if defined(AFS_HPUX102_ENV)
348 if (av->va_uid != UID_NO_CHANGE) {
350 if (av->va_uid != ((unsigned short)-1)) {
353 if (av->va_uid != -1) {
355 #endif /* AFS_SUN5_ENV */
356 mask |= AFS_SETOWNER;
357 as->Owner = av->va_uid;
359 #if defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV) || defined(AFS_LINUX22_ENV)
360 if (av->va_mask & AT_MTIME) {
362 if (av->va_mtime.tv_sec != -1) {
364 mask |= AFS_SETMODTIME;
366 #if defined(AFS_SUN5_ENV) || defined(AFS_AIX41_ENV) || defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
367 if (av->va_mtime.tv_nsec == -1)
369 if (av->va_mtime.tv_usec == -1)
371 as->ClientModTime = osi_Time(); /* special Sys V compat hack for Suns */
374 as->ClientModTime = av->va_mtime.tv_sec;
380 /* We don't set CDirty bit in avc->states because setattr calls WriteVCache
381 * synchronously, therefore, it's not needed.
383 #if defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV)
384 afs_setattr(OSI_VC_ARG(avc), attrs, flags, acred)
387 afs_setattr(avc, attrs, acred)
390 register struct vattr *attrs;
391 struct AFS_UCRED *acred; {
392 struct vrequest treq;
393 struct AFSStoreStatus astat;
394 register afs_int32 code;
397 AFS_STATCNT(afs_setattr);
398 afs_Trace2(afs_iclSetp, CM_TRACE_SETATTR, ICL_TYPE_POINTER, avc,
399 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(avc->m.Length));
400 if (code = afs_InitReq(&treq, acred)) return code;
401 if (avc->states & CRO) {
405 #if defined(AFS_SGI_ENV)
406 /* ignore ATTR_LAZY calls - they are really only for keeping
407 * the access/mtime of mmaped files up to date
409 if (flags & ATTR_LAZY)
413 /* if file size has changed, we need write access, otherwise (e.g.
414 * chmod) give it a shot; if it fails, we'll discard the status
417 * Note that Ultrix actually defines ftruncate of a file you have open to
418 * be O.K., and does the proper access checks itself in the truncate
419 * path (unlike BSD or SUNOS), so we skip this check for Ultrix.
422 #if defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV) || defined(AFS_LINUX22_ENV)
423 if (attrs->va_mask & AT_SIZE) {
426 if (attrs->va_size != VNOVAL) {
429 if (attrs->va_size != -1) {
431 if (attrs->va_size != ~0) {
435 if (!afs_AccessOK(avc, PRSFS_WRITE, &treq, DONT_CHECK_MODE_BITS)) {
442 afs_VAttrToAS(avc, attrs, &astat); /* interpret request */
444 #if defined(AFS_SUN_ENV) || defined(AFS_ALPHA_ENV) || defined(AFS_SUN5_ENV)
445 afs_BozonLock(&avc->pvnLock, avc);
447 #if defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV)
448 if (AFS_NFSXLATORREQ(acred)) {
449 avc->execsOrWriters++;
453 #if defined(AFS_SGI_ENV)
454 AFS_RWLOCK((vnode_t *)avc, VRWLOCK_WRITE);
456 #if defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV) || defined(AFS_LINUX22_ENV)
457 if (attrs->va_mask & AT_SIZE) {
459 #if defined(AFS_OSF_ENV)
460 if (attrs->va_size != VNOVAL) {
461 #else /* AFS_OSF_ENV */
462 if (attrs->va_size != -1) {
465 afs_size_t tsize = attrs->va_size;
466 ObtainWriteLock(&avc->lock,128);
467 avc->states |= CDirty;
468 code = afs_TruncateAllSegments(avc, tsize, &treq, acred);
469 /* if date not explicitly set by this call, set it ourselves, since we
470 * changed the data */
471 if (!(astat.Mask & AFS_SETMODTIME)) {
472 astat.Mask |= AFS_SETMODTIME;
473 astat.ClientModTime = osi_Time();
476 if ( ( (avc->execsOrWriters <= 0 ) && (avc->states & CCreating) == 0)
477 || ( avc->execsOrWriters == 1 && AFS_NFSXLATORREQ(acred)) ) {
478 code = afs_StoreAllSegments(avc, &treq, AFS_ASYNC);
480 avc->states &= ~CDirty;
483 avc->states &= ~CDirty;
485 ReleaseWriteLock(&avc->lock);
487 osi_FlushText(avc); /* do this after releasing all locks */
489 /* in case we changed the size here, propagate it to gp->g_size */
490 afs_gfshack((struct gnode *)avc);
494 ObtainSharedLock(&avc->lock,16); /* lock entry */
495 code = afs_WriteVCache(avc, &astat, &treq); /* send request */
496 ReleaseSharedLock(&avc->lock); /* release lock */
499 ObtainWriteLock(&afs_xcbhash, 487);
500 afs_DequeueCallback(avc);
501 avc->states &= ~CStatd;
502 ReleaseWriteLock(&afs_xcbhash);
503 if (avc->fid.Fid.Vnode & 1 || (vType(avc) == VDIR))
504 osi_dnlc_purgedp(avc);
505 /* error? erase any changes we made to vcache entry */
508 #if defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV)
509 if (AFS_NFSXLATORREQ(acred)) {
510 avc->execsOrWriters--;
513 #if defined(AFS_SUN_ENV) || defined(AFS_OSF_ENV) || defined(AFS_SUN5_ENV)
514 afs_BozonUnlock(&avc->pvnLock, avc);
516 #if defined(AFS_SGI_ENV)
517 AFS_RWUNLOCK((vnode_t *)avc, VRWLOCK_WRITE);
520 code = afs_CheckCode(code, &treq, 15);