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 "../afs/param.h" /* Should be always first */
22 #include "../afs/sysincludes.h" /* Standard vendor system headers */
23 #include "../afs/afsincludes.h" /* Afs-based standard headers */
24 #include "../afs/afs_stats.h" /* statistics */
25 #include "../afs/afs_cbqueue.h"
26 #include "../afs/nfsclient.h"
27 #include "../afs/afs_osidnlc.h"
29 extern afs_rwlock_t afs_xcbhash;
30 struct afs_exporter *afs_nfsexporter;
31 extern struct vcache *afs_globalVp;
33 /* copy out attributes from cache entry */
34 afs_CopyOutAttrs(avc, attrs)
35 register struct vattr *attrs;
36 register struct vcache *avc; {
37 register struct volume *tvp;
38 register struct cell *tcell;
41 AFS_STATCNT(afs_CopyOutAttrs);
42 #if defined(AFS_MACH_ENV )
43 attrs->va_mode = vType(avc) | (avc->m.Mode&~VFMT);
44 #else /* AFS_MACH_ENV */
45 attrs->va_type = vType(avc);
46 #if defined(AFS_SGI_ENV) || defined(AFS_AIX32_ENV) || defined(AFS_SUN5_ENV)
47 attrs->va_mode = (mode_t)(avc->m.Mode & 0xffff);
49 attrs->va_mode = avc->m.Mode;
51 #endif /* AFS_MACH_ENV */
53 if (avc->m.Mode & (VSUID|VSGID)) {
54 /* setuid or setgid, make sure we're allowed to run them from this cell */
55 tcell = afs_GetCell(avc->fid.Cell, 0);
56 if (tcell && (tcell->states & CNoSUID))
57 attrs->va_mode &= ~(VSUID|VSGID);
59 attrs->va_uid = avc->m.Owner;
60 attrs->va_gid = avc->m.Group; /* yeah! */
61 #if defined(AFS_SUN56_ENV)
62 attrs->va_fsid = avc->v.v_vfsp->vfs_fsid.val[0];
65 /* XXX We try making this match the vfs's dev field XXX */
69 attrs->va_fsid = avc->v.v_mount->m_stat.f_fsid.val[0];
74 #endif /* AFS_SUN56_ENV */
75 if (avc->mvstat == 2) {
76 tvp = afs_GetVolume(&avc->fid, 0, READ_LOCK);
77 /* The mount point's vnode. */
80 tvp->mtpoint.Fid.Vnode + (tvp->mtpoint.Fid.Volume << 16);
81 if (FidCmp(&afs_rootFid, &avc->fid) && !attrs->va_nodeid)
83 afs_PutVolume(tvp, READ_LOCK);
85 else attrs->va_nodeid = 2;
87 else attrs->va_nodeid = avc->fid.Fid.Vnode + (avc->fid.Fid.Volume << 16);
88 attrs->va_nodeid &= 0x7fffffff; /* Saber C hates negative inode #s! */
89 attrs->va_nlink = avc->m.LinkCount;
90 attrs->va_size = avc->m.Length;
91 attrs->va_atime.tv_sec = attrs->va_mtime.tv_sec = attrs->va_ctime.tv_sec =
93 /* set microseconds to be dataversion # so that we approximate NFS-style
94 * use of mtime as a dataversion #. We take it mod 512K because
95 * microseconds *must* be less than a million, and 512K is the biggest
96 * power of 2 less than such. DataVersions are typically pretty small
97 * anyway, so the difference between 512K and 1000000 shouldn't matter
98 * much, and "&" is a lot faster than "%".
100 #if defined(AFS_SGI_ENV) || defined(AFS_SUN5_ENV) || defined(AFS_AIX41_ENV) || defined(AFS_DARWIN_ENV)
101 attrs->va_atime.tv_nsec = attrs->va_mtime.tv_nsec =
102 attrs->va_ctime.tv_nsec =
103 (hgetlo(avc->m.DataVersion) & 0x7ffff) * 1000;
104 #if defined(AFS_AIX41_ENV) || defined(AFS_DARWIN_ENV)
105 attrs->va_blocksize = PAGESIZE; /* XXX Was 8192 XXX */
107 attrs->va_blksize = PAGESIZE; /* XXX Was 8192 XXX */
110 attrs->va_atime.tv_usec = attrs->va_mtime.tv_usec =
111 attrs->va_ctime.tv_usec =
112 (hgetlo(avc->m.DataVersion) & 0x7ffff);
113 attrs->va_blocksize = PAGESIZE; /* XXX Was 8192 XXX */
116 /* Have to use real device #s in Ultrix, since that's how FS type is
117 * encoded. If rdev doesn't match Ultrix equivalent of statfs's rdev, then
118 * "df ." doesn't work.
120 if (afs_globalVFS && afs_globalVFS->vfs_data)
121 attrs->va_rdev = ((struct mount *)(afs_globalVFS->vfs_data))->m_dev;
123 attrs->va_rdev = 1; /* better than nothing */
128 * Below return 0 (and not 1) blocks if the file is zero length. This conforms
129 * better with the other filesystems that do return 0.
133 attrs->va_size_rsv = 0;
136 /* attrs->va_gen = avc->m.DataVersion;*/
138 #endif /* AFS_OSF_ENV */
140 #if !defined(AFS_OSF_ENV) && !defined(AFS_DARWIN_ENV)
141 #if !defined(AFS_HPUX_ENV)
143 attrs->va_nblocks = (attrs->va_size? ((attrs->va_size + 1023)>>10) << 1 : 0);
145 #if defined(AFS_SGI_ENV)
146 attrs->va_blocks = BTOBB(attrs->va_size);
148 attrs->va_blocks = (attrs->va_size? ((attrs->va_size + 1023)>>10) << 1 : 0);
151 #else /* !defined(AFS_HPUX_ENV) */
152 attrs->va_blocks = (attrs->va_size? ((attrs->va_size + 1023)>>10) : 0);
153 #endif /* !defined(AFS_HPUX_ENV) */
154 #else /* ! AFS_OSF_ENV */
155 attrs->va_bytes = (attrs->va_size? (attrs->va_size + 1023) : 1024);
157 attrs->va_bytes_rsv = -1;
159 #endif /* ! AFS_OSF_ENV */
161 #ifdef AFS_LINUX22_ENV
162 /* And linux has it's own stash as well. */
163 vattr2inode((struct inode*)avc, attrs);
170 #if defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV)
171 afs_getattr(OSI_VC_ARG(avc), attrs, flags, acred)
174 afs_getattr(OSI_VC_ARG(avc), attrs, acred)
178 struct AFS_UCRED *acred;
181 struct vrequest treq;
182 extern struct unixuser *afs_FindUser();
187 AFS_STATCNT(afs_getattr);
188 afs_Trace2(afs_iclSetp, CM_TRACE_GETATTR, ICL_TYPE_POINTER, avc,
189 ICL_TYPE_INT32, avc->m.Length);
191 #if defined(AFS_SUN5_ENV)
192 if (flags & ATTR_HINT) {
193 code = afs_CopyOutAttrs(avc, attrs);
198 #if defined(AFS_SUN_ENV) || defined(AFS_ALPHA_ENV) || defined(AFS_SUN5_ENV)
199 afs_BozonLock(&avc->pvnLock, avc);
202 if (afs_shuttingdown) return EIO;
204 if (!(avc->states & CStatd)) {
205 if (!(code = afs_InitReq(&treq, acred))) {
206 code = afs_VerifyVCache2(avc, &treq);
212 #if defined(AFS_SUN_ENV) || defined(AFS_ALPHA_ENV) || defined(AFS_SUN5_ENV)
213 if (code == 0) osi_FlushPages(avc, acred);
214 afs_BozonUnlock(&avc->pvnLock, avc);
219 osi_FlushText(avc); /* only needed to flush text if text locked last time */
220 code = afs_CopyOutAttrs(avc, attrs);
222 if (afs_nfsexporter) {
224 if (code = afs_InitReq(&treq, acred))
228 if (AFS_NFSXLATORREQ(acred)) {
229 if ((vType(avc) != VDIR) &&
230 !afs_AccessOK(avc, PRSFS_READ, &treq,
231 CHECK_MODE_BITS|CMB_ALLOW_EXEC_AS_READ)) {
234 if (avc->mvstat == 2) {
235 #if defined(AFS_SGI_ENV) || defined(AFS_SUN5_ENV) || defined(AFS_AIX41_ENV) || defined(AFS_DARWIN_ENV)
236 attrs->va_mtime.tv_nsec += ((++avc->xlatordv) * 1000);
238 attrs->va_mtime.tv_usec += ++avc->xlatordv;
242 if (au = afs_FindUser(treq.uid, -1, READ_LOCK)) {
243 register struct afs_exporter *exporter = au->exporter;
245 if (exporter && !(afs_nfsexporter->exp_states & EXP_UNIXMODE)) {
248 * If the remote user wishes to enforce default Unix mode semantics,
249 * like in the nfs exporter case, we OR in the user bits
250 * into the group and other bits. We need to do this
251 * because there is no RFS_ACCESS call and thus nfs
252 * clients implement nfs_access by interpreting the
253 * mode bits in the traditional way, which of course
256 ubits = (attrs->va_mode & 0700) >> 6;
257 attrs->va_mode = attrs->va_mode | ubits | (ubits << 3);
258 /* If it's the root of AFS, replace the inode number with the
259 * inode number of the mounted on directory; otherwise this
260 * confuses getwd()... */
261 #ifdef AFS_LINUX22_ENV
262 if (avc == afs_globalVp) {
263 struct inode *ip = avc->v.i_sb->s_root->d_inode;
264 attrs->va_nodeid = ip->i_ino;
267 if (avc->v.v_flag & VROOT) {
268 struct vnode *vp = (struct vnode *)avc;
270 vp = vp->v_vfsp->vfs_vnodecovered;
271 if (vp) { /* Ignore weird failures */
273 attrs->va_nodeid = VnodeToIno(vp);
277 ip = (struct inode *) VTOI(vp);
278 if (ip) /* Ignore weird failures */
279 attrs->va_nodeid = ip->i_number;
283 #endif /* AFS_LINUX22_ENV */
285 afs_PutUser(au, READ_LOCK);
291 code = afs_CheckCode(code, &treq, 14);
295 /* convert a Unix request into a status store request */
296 afs_VAttrToAS(avc, av, as)
297 register struct vcache *avc;
298 register struct vattr *av;
299 register struct AFSStoreStatus *as; {
302 AFS_STATCNT(afs_VAttrToAS);
303 #if defined(AFS_AIX_ENV)
304 /* Boy, was this machine dependent bogosity hard to swallow????.... */
305 if (av->va_mode != -1) {
307 #if defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV) || defined(AFS_LINUX22_ENV)
308 if (av->va_mask & AT_MODE) {
310 if (av->va_mode != ((unsigned short)-1)) {
314 as->UnixModeBits = av->va_mode & 0xffff;
315 if (avc->states & CForeign) {
316 ObtainWriteLock(&avc->lock,127);
317 afs_FreeAllAxs(&(avc->Access));
318 ReleaseWriteLock(&avc->lock);
322 #if defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV) || defined(AFS_LINUX22_ENV)
323 if (av->va_mask & AT_GID) {
325 #if (defined(AFS_HPUX_ENV) || defined(AFS_SUN_ENV))
326 #if defined(AFS_HPUX102_ENV)
327 if (av->va_gid != GID_NO_CHANGE) {
329 if (av->va_gid != ((unsigned short)-1)) {
332 if (av->va_gid != -1) {
334 #endif /* AFS_SUN5_ENV */
335 mask |= AFS_SETGROUP;
336 as->Group = av->va_gid;
339 #if defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV) || defined(AFS_LINUX22_ENV)
340 if (av->va_mask & AT_UID) {
342 #if (defined(AFS_HPUX_ENV) || defined(AFS_SUN_ENV))
343 #if defined(AFS_HPUX102_ENV)
344 if (av->va_uid != UID_NO_CHANGE) {
346 if (av->va_uid != ((unsigned short)-1)) {
349 if (av->va_uid != -1) {
351 #endif /* AFS_SUN5_ENV */
352 mask |= AFS_SETOWNER;
353 as->Owner = av->va_uid;
355 #if defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV) || defined(AFS_LINUX22_ENV)
356 if (av->va_mask & AT_MTIME) {
358 if (av->va_mtime.tv_sec != -1) {
360 mask |= AFS_SETMODTIME;
362 #if defined(AFS_SUN5_ENV) || defined(AFS_AIX41_ENV) || defined(AFS_DARWIN_ENV)
363 if (av->va_mtime.tv_nsec == -1)
365 if (av->va_mtime.tv_usec == -1)
367 as->ClientModTime = osi_Time(); /* special Sys V compat hack for Suns */
370 as->ClientModTime = av->va_mtime.tv_sec;
376 /* We don't set CDirty bit in avc->states because setattr calls WriteVCache
377 * synchronously, therefore, it's not needed.
379 #if defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV)
380 afs_setattr(OSI_VC_ARG(avc), attrs, flags, acred)
383 afs_setattr(avc, attrs, acred)
386 register struct vattr *attrs;
387 struct AFS_UCRED *acred; {
388 struct vrequest treq;
389 struct AFSStoreStatus astat;
390 register afs_int32 code;
393 AFS_STATCNT(afs_setattr);
394 afs_Trace2(afs_iclSetp, CM_TRACE_SETATTR, ICL_TYPE_POINTER, avc,
395 ICL_TYPE_INT32, avc->m.Length);
396 if (code = afs_InitReq(&treq, acred)) return code;
397 if (avc->states & CRO) {
401 #if defined(AFS_SGI_ENV)
402 /* ignore ATTR_LAZY calls - they are really only for keeping
403 * the access/mtime of mmaped files up to date
405 if (flags & ATTR_LAZY)
409 /* if file size has changed, we need write access, otherwise (e.g.
410 * chmod) give it a shot; if it fails, we'll discard the status
413 * Note that Ultrix actually defines ftruncate of a file you have open to
414 * be O.K., and does the proper access checks itself in the truncate
415 * path (unlike BSD or SUNOS), so we skip this check for Ultrix.
418 #if defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV) || defined(AFS_LINUX22_ENV)
419 if (attrs->va_mask & AT_SIZE) {
422 if (attrs->va_size != VNOVAL) {
425 if (attrs->va_size != -1) {
427 if (attrs->va_size != ~0) {
431 if (!afs_AccessOK(avc, PRSFS_WRITE, &treq, DONT_CHECK_MODE_BITS)) {
438 afs_VAttrToAS(avc, attrs, &astat); /* interpret request */
440 #if defined(AFS_SUN_ENV) || defined(AFS_ALPHA_ENV) || defined(AFS_SUN5_ENV)
441 afs_BozonLock(&avc->pvnLock, avc);
443 #if defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV)
444 if (AFS_NFSXLATORREQ(acred)) {
445 avc->execsOrWriters++;
449 #if defined(AFS_SGI_ENV)
450 AFS_RWLOCK((vnode_t *)avc, VRWLOCK_WRITE);
452 #if defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV) || defined(AFS_LINUX22_ENV)
453 if (attrs->va_mask & AT_SIZE) {
455 #if defined(AFS_OSF_ENV)
456 if (attrs->va_size != VNOVAL) {
457 #else /* AFS_OSF_ENV */
458 if (attrs->va_size != -1) {
461 ObtainWriteLock(&avc->lock,128);
462 avc->states |= CDirty;
463 code = afs_TruncateAllSegments(avc, (afs_int32)attrs->va_size, &treq, acred);
464 /* if date not explicitly set by this call, set it ourselves, since we
465 * changed the data */
466 if (!(astat.Mask & AFS_SETMODTIME)) {
467 astat.Mask |= AFS_SETMODTIME;
468 astat.ClientModTime = osi_Time();
471 if ( ( (avc->execsOrWriters <= 0 ) && (avc->states & CCreating) == 0)
472 || ( avc->execsOrWriters == 1 && AFS_NFSXLATORREQ(acred)) ) {
473 code = afs_StoreAllSegments(avc, &treq, AFS_ASYNC);
475 avc->states &= ~CDirty;
478 avc->states &= ~CDirty;
480 ReleaseWriteLock(&avc->lock);
482 osi_FlushText(avc); /* do this after releasing all locks */
484 /* in case we changed the size here, propagate it to gp->g_size */
485 afs_gfshack((struct gnode *)avc);
489 ObtainSharedLock(&avc->lock,16); /* lock entry */
490 code = afs_WriteVCache(avc, &astat, &treq); /* send request */
491 ReleaseSharedLock(&avc->lock); /* release lock */
494 ObtainWriteLock(&afs_xcbhash, 487);
495 afs_DequeueCallback(avc);
496 avc->states &= ~CStatd;
497 ReleaseWriteLock(&afs_xcbhash);
498 if (avc->fid.Fid.Vnode & 1 || (vType(avc) == VDIR))
499 osi_dnlc_purgedp(avc);
500 /* error? erase any changes we made to vcache entry */
503 #if defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV)
504 if (AFS_NFSXLATORREQ(acred)) {
505 avc->execsOrWriters--;
508 #if defined(AFS_SUN_ENV) || defined(AFS_OSF_ENV) || defined(AFS_SUN5_ENV)
509 afs_BozonUnlock(&avc->pvnLock, avc);
511 #if defined(AFS_SGI_ENV)
512 AFS_RWUNLOCK((vnode_t *)avc, VRWLOCK_WRITE);
515 code = afs_CheckCode(code, &treq, 15);