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)
101 attrs->va_atime.tv_nsec = attrs->va_mtime.tv_nsec =
102 attrs->va_ctime.tv_nsec =
103 (hgetlo(avc->m.DataVersion) & 0x7ffff) * 1000;
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)
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_SUN_ENV) || defined(AFS_ALPHA_ENV) || defined(AFS_SUN5_ENV)
192 afs_BozonLock(&avc->pvnLock, avc);
195 if (afs_shuttingdown) return EIO;
197 if (!(avc->states & CStatd)) {
198 if (!(code = afs_InitReq(&treq, acred))) {
199 code = afs_VerifyVCache2(avc, &treq);
205 #if defined(AFS_SUN_ENV) || defined(AFS_ALPHA_ENV) || defined(AFS_SUN5_ENV)
206 if (code == 0) osi_FlushPages(avc, acred);
207 afs_BozonUnlock(&avc->pvnLock, avc);
212 osi_FlushText(avc); /* only needed to flush text if text locked last time */
213 code = afs_CopyOutAttrs(avc, attrs);
215 if (afs_nfsexporter) {
217 if (code = afs_InitReq(&treq, acred))
221 if (AFS_NFSXLATORREQ(acred)) {
222 if ((vType(avc) != VDIR) &&
223 !afs_AccessOK(avc, PRSFS_READ, &treq,
224 CHECK_MODE_BITS|CMB_ALLOW_EXEC_AS_READ)) {
227 if (avc->mvstat == 2) {
228 #if defined(AFS_SGI_ENV) || defined(AFS_SUN5_ENV) || defined(AFS_AIX41_ENV)
229 attrs->va_mtime.tv_nsec += ((++avc->xlatordv) * 1000);
231 attrs->va_mtime.tv_usec += ++avc->xlatordv;
235 if (au = afs_FindUser(treq.uid, -1, READ_LOCK)) {
236 register struct afs_exporter *exporter = au->exporter;
238 if (exporter && !(afs_nfsexporter->exp_states & EXP_UNIXMODE)) {
241 * If the remote user wishes to enforce default Unix mode semantics,
242 * like in the nfs exporter case, we OR in the user bits
243 * into the group and other bits. We need to do this
244 * because there is no RFS_ACCESS call and thus nfs
245 * clients implement nfs_access by interpreting the
246 * mode bits in the traditional way, which of course
249 ubits = (attrs->va_mode & 0700) >> 6;
250 attrs->va_mode = attrs->va_mode | ubits | (ubits << 3);
251 /* If it's the root of AFS, replace the inode number with the
252 * inode number of the mounted on directory; otherwise this
253 * confuses getwd()... */
254 #ifdef AFS_LINUX22_ENV
255 if (avc == afs_globalVp) {
256 struct inode *ip = avc->v.i_sb->s_root->d_inode;
257 attrs->va_nodeid = ip->i_ino;
260 if (avc->v.v_flag & VROOT) {
261 struct vnode *vp = (struct vnode *)avc;
263 vp = vp->v_vfsp->vfs_vnodecovered;
264 if (vp) { /* Ignore weird failures */
266 attrs->va_nodeid = VnodeToIno(vp);
270 ip = (struct inode *) VTOI(vp);
271 if (ip) /* Ignore weird failures */
272 attrs->va_nodeid = ip->i_number;
276 #endif /* AFS_LINUX22_ENV */
278 afs_PutUser(au, READ_LOCK);
284 code = afs_CheckCode(code, &treq, 14);
288 /* convert a Unix request into a status store request */
289 afs_VAttrToAS(avc, av, as)
290 register struct vcache *avc;
291 register struct vattr *av;
292 register struct AFSStoreStatus *as; {
295 AFS_STATCNT(afs_VAttrToAS);
296 #if defined(AFS_AIX_ENV)
297 /* Boy, was this machine dependent bogosity hard to swallow????.... */
298 if (av->va_mode != -1) {
300 #if defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV) || defined(AFS_LINUX22_ENV)
301 if (av->va_mask & AT_MODE) {
303 if (av->va_mode != ((unsigned short)-1)) {
307 as->UnixModeBits = av->va_mode & 0xffff;
308 if (avc->states & CForeign) {
309 ObtainWriteLock(&avc->lock,127);
310 afs_FreeAllAxs(&(avc->Access));
311 ReleaseWriteLock(&avc->lock);
315 #if defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV) || defined(AFS_LINUX22_ENV)
316 if (av->va_mask & AT_GID) {
318 #if (defined(AFS_HPUX_ENV) || defined(AFS_SUN_ENV))
319 #if defined(AFS_HPUX102_ENV)
320 if (av->va_gid != GID_NO_CHANGE) {
322 if (av->va_gid != ((unsigned short)-1)) {
325 if (av->va_gid != -1) {
327 #endif /* AFS_SUN5_ENV */
328 mask |= AFS_SETGROUP;
329 as->Group = av->va_gid;
332 #if defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV) || defined(AFS_LINUX22_ENV)
333 if (av->va_mask & AT_UID) {
335 #if (defined(AFS_HPUX_ENV) || defined(AFS_SUN_ENV))
336 #if defined(AFS_HPUX102_ENV)
337 if (av->va_uid != UID_NO_CHANGE) {
339 if (av->va_uid != ((unsigned short)-1)) {
342 if (av->va_uid != -1) {
344 #endif /* AFS_SUN5_ENV */
345 mask |= AFS_SETOWNER;
346 as->Owner = av->va_uid;
348 #if defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV) || defined(AFS_LINUX22_ENV)
349 if (av->va_mask & AT_MTIME) {
351 if (av->va_mtime.tv_sec != -1) {
353 mask |= AFS_SETMODTIME;
355 #if defined(AFS_SUN5_ENV) || defined(AFS_AIX41_ENV)
356 if (av->va_mtime.tv_nsec == -1)
358 if (av->va_mtime.tv_usec == -1)
360 as->ClientModTime = osi_Time(); /* special Sys V compat hack for Suns */
363 as->ClientModTime = av->va_mtime.tv_sec;
369 /* We don't set CDirty bit in avc->states because setattr calls WriteVCache
370 * synchronously, therefore, it's not needed.
372 #if defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV)
373 afs_setattr(OSI_VC_ARG(avc), attrs, flags, acred)
376 afs_setattr(avc, attrs, acred)
379 register struct vattr *attrs;
380 struct AFS_UCRED *acred; {
381 struct vrequest treq;
382 struct AFSStoreStatus astat;
383 register afs_int32 code;
386 AFS_STATCNT(afs_setattr);
387 afs_Trace2(afs_iclSetp, CM_TRACE_SETATTR, ICL_TYPE_POINTER, avc,
388 ICL_TYPE_INT32, avc->m.Length);
389 if (code = afs_InitReq(&treq, acred)) return code;
390 if (avc->states & CRO) {
394 #if defined(AFS_SGI_ENV)
395 /* ignore ATTR_LAZY calls - they are really only for keeping
396 * the access/mtime of mmaped files up to date
398 if (flags & ATTR_LAZY)
402 /* if file size has changed, we need write access, otherwise (e.g.
403 * chmod) give it a shot; if it fails, we'll discard the status
406 * Note that Ultrix actually defines ftruncate of a file you have open to
407 * be O.K., and does the proper access checks itself in the truncate
408 * path (unlike BSD or SUNOS), so we skip this check for Ultrix.
411 #if defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV) || defined(AFS_LINUX22_ENV)
412 if (attrs->va_mask & AT_SIZE) {
415 if (attrs->va_size != VNOVAL) {
418 if (attrs->va_size != -1) {
420 if (attrs->va_size != ~0) {
424 if (!afs_AccessOK(avc, PRSFS_WRITE, &treq, DONT_CHECK_MODE_BITS)) {
431 afs_VAttrToAS(avc, attrs, &astat); /* interpret request */
433 #if defined(AFS_SUN_ENV) || defined(AFS_ALPHA_ENV) || defined(AFS_SUN5_ENV)
434 afs_BozonLock(&avc->pvnLock, avc);
436 #if defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV)
437 if (AFS_NFSXLATORREQ(acred)) {
438 avc->execsOrWriters++;
442 #if defined(AFS_SGI_ENV)
443 AFS_RWLOCK((vnode_t *)avc, VRWLOCK_WRITE);
445 #if defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV) || defined(AFS_LINUX22_ENV)
446 if (attrs->va_mask & AT_SIZE) {
448 #if defined(AFS_OSF_ENV)
449 if (attrs->va_size != VNOVAL) {
450 #else /* AFS_OSF_ENV */
451 if (attrs->va_size != -1) {
454 ObtainWriteLock(&avc->lock,128);
455 avc->states |= CDirty;
456 code = afs_TruncateAllSegments(avc, (afs_int32)attrs->va_size, &treq, acred);
457 /* if date not explicitly set by this call, set it ourselves, since we
458 * changed the data */
459 if (!(astat.Mask & AFS_SETMODTIME)) {
460 astat.Mask |= AFS_SETMODTIME;
461 astat.ClientModTime = osi_Time();
464 if ( ( (avc->execsOrWriters <= 0 ) && (avc->states & CCreating) == 0)
465 || ( avc->execsOrWriters == 1 && AFS_NFSXLATORREQ(acred)) ) {
466 code = afs_StoreAllSegments(avc, &treq, AFS_ASYNC);
468 avc->states &= ~CDirty;
471 avc->states &= ~CDirty;
473 ReleaseWriteLock(&avc->lock);
475 osi_FlushText(avc); /* do this after releasing all locks */
477 /* in case we changed the size here, propagate it to gp->g_size */
478 afs_gfshack((struct gnode *)avc);
482 ObtainSharedLock(&avc->lock,16); /* lock entry */
483 code = afs_WriteVCache(avc, &astat, &treq); /* send request */
484 ReleaseSharedLock(&avc->lock); /* release lock */
487 ObtainWriteLock(&afs_xcbhash, 487);
488 afs_DequeueCallback(avc);
489 avc->states &= ~CStatd;
490 ReleaseWriteLock(&afs_xcbhash);
491 if (avc->fid.Fid.Vnode & 1 || (vType(avc) == VDIR))
492 osi_dnlc_purgedp(avc);
493 /* error? erase any changes we made to vcache entry */
496 #if defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV)
497 if (AFS_NFSXLATORREQ(acred)) {
498 avc->execsOrWriters--;
501 #if defined(AFS_SUN_ENV) || defined(AFS_OSF_ENV) || defined(AFS_SUN5_ENV)
502 afs_BozonUnlock(&avc->pvnLock, avc);
504 #if defined(AFS_SGI_ENV)
505 AFS_RWUNLOCK((vnode_t *)avc, VRWLOCK_WRITE);
508 code = afs_CheckCode(code, &treq, 15);