1 /* Copyright (C) 1995, 1989, 1998 Transarc Corporation - All rights reserved */
4 * For copyright information, see IPL which you accepted in order to
5 * download this software.
10 * afs_vnop_attrs.c - setattr and getattr vnodeops
20 #include "../afs/param.h" /* Should be always first */
21 #include "../afs/sysincludes.h" /* Standard vendor system headers */
22 #include "../afs/afsincludes.h" /* Afs-based standard headers */
23 #include "../afs/afs_stats.h" /* statistics */
24 #include "../afs/afs_cbqueue.h"
25 #include "../afs/nfsclient.h"
26 #include "../afs/afs_osidnlc.h"
28 extern afs_rwlock_t afs_xcbhash;
29 struct afs_exporter *afs_nfsexporter;
30 extern struct vcache *afs_globalVp;
32 /* copy out attributes from cache entry */
33 afs_CopyOutAttrs(avc, attrs)
34 register struct vattr *attrs;
35 register struct vcache *avc; {
36 register struct volume *tvp;
37 register struct cell *tcell;
40 AFS_STATCNT(afs_CopyOutAttrs);
41 #if defined(AFS_MACH_ENV )
42 attrs->va_mode = vType(avc) | (avc->m.Mode&~VFMT);
43 #else /* AFS_MACH_ENV */
44 attrs->va_type = vType(avc);
45 #if defined(AFS_SGI_ENV) || defined(AFS_AIX32_ENV) || defined(AFS_SUN5_ENV)
46 attrs->va_mode = (mode_t)(avc->m.Mode & 0xffff);
48 attrs->va_mode = avc->m.Mode;
50 #endif /* AFS_MACH_ENV */
52 if (avc->m.Mode & (VSUID|VSGID)) {
53 /* setuid or setgid, make sure we're allowed to run them from this cell */
54 tcell = afs_GetCell(avc->fid.Cell, 0);
55 if (tcell && (tcell->states & CNoSUID))
56 attrs->va_mode &= ~(VSUID|VSGID);
58 attrs->va_uid = avc->m.Owner;
59 attrs->va_gid = avc->m.Group; /* yeah! */
60 #if defined(AFS_SUN56_ENV)
61 attrs->va_fsid = avc->v.v_vfsp->vfs_fsid.val[0];
64 /* XXX We try making this match the vfs's dev field XXX */
68 attrs->va_fsid = avc->v.v_mount->m_stat.f_fsid.val[0];
73 #endif /* AFS_SUN56_ENV */
74 if (avc->mvstat == 2) {
75 tvp = afs_GetVolume(&avc->fid, 0, READ_LOCK);
76 /* The mount point's vnode. */
79 tvp->mtpoint.Fid.Vnode + (tvp->mtpoint.Fid.Volume << 16);
80 if (FidCmp(&afs_rootFid, &avc->fid) && !attrs->va_nodeid)
82 afs_PutVolume(tvp, READ_LOCK);
84 else attrs->va_nodeid = 2;
86 else attrs->va_nodeid = avc->fid.Fid.Vnode + (avc->fid.Fid.Volume << 16);
87 attrs->va_nodeid &= 0x7fffffff; /* Saber C hates negative inode #s! */
88 attrs->va_nlink = avc->m.LinkCount;
89 attrs->va_size = avc->m.Length;
90 attrs->va_atime.tv_sec = attrs->va_mtime.tv_sec = attrs->va_ctime.tv_sec =
92 /* set microseconds to be dataversion # so that we approximate NFS-style
93 * use of mtime as a dataversion #. We take it mod 512K because
94 * microseconds *must* be less than a million, and 512K is the biggest
95 * power of 2 less than such. DataVersions are typically pretty small
96 * anyway, so the difference between 512K and 1000000 shouldn't matter
97 * much, and "&" is a lot faster than "%".
99 #if defined(AFS_SGI_ENV) || defined(AFS_SUN5_ENV) || defined(AFS_AIX41_ENV)
100 attrs->va_atime.tv_nsec = attrs->va_mtime.tv_nsec =
101 attrs->va_ctime.tv_nsec =
102 (hgetlo(avc->m.DataVersion) & 0x7ffff) * 1000;
104 attrs->va_blocksize = PAGESIZE; /* XXX Was 8192 XXX */
106 attrs->va_blksize = PAGESIZE; /* XXX Was 8192 XXX */
109 attrs->va_atime.tv_usec = attrs->va_mtime.tv_usec =
110 attrs->va_ctime.tv_usec =
111 (hgetlo(avc->m.DataVersion) & 0x7ffff);
112 attrs->va_blocksize = PAGESIZE; /* XXX Was 8192 XXX */
115 /* Have to use real device #s in Ultrix, since that's how FS type is
116 * encoded. If rdev doesn't match Ultrix equivalent of statfs's rdev, then
117 * "df ." doesn't work.
119 if (afs_globalVFS && afs_globalVFS->vfs_data)
120 attrs->va_rdev = ((struct mount *)(afs_globalVFS->vfs_data))->m_dev;
122 attrs->va_rdev = 1; /* better than nothing */
127 * Below return 0 (and not 1) blocks if the file is zero length. This conforms
128 * better with the other filesystems that do return 0.
132 attrs->va_size_rsv = 0;
135 /* attrs->va_gen = avc->m.DataVersion;*/
137 #endif /* AFS_OSF_ENV */
139 #if !defined(AFS_OSF_ENV)
140 #if !defined(AFS_HPUX_ENV)
142 attrs->va_nblocks = (attrs->va_size? ((attrs->va_size + 1023)>>10) << 1 : 0);
144 #if defined(AFS_SGI_ENV)
145 attrs->va_blocks = BTOBB(attrs->va_size);
147 attrs->va_blocks = (attrs->va_size? ((attrs->va_size + 1023)>>10) << 1 : 0);
150 #else /* !defined(AFS_HPUX_ENV) */
151 attrs->va_blocks = (attrs->va_size? ((attrs->va_size + 1023)>>10) : 0);
152 #endif /* !defined(AFS_HPUX_ENV) */
153 #else /* ! AFS_OSF_ENV */
154 attrs->va_bytes = (attrs->va_size? (attrs->va_size + 1023) : 1024);
156 attrs->va_bytes_rsv = -1;
158 #endif /* ! AFS_OSF_ENV */
160 #ifdef AFS_LINUX22_ENV
161 /* And linux has it's own stash as well. */
162 vattr2inode((struct inode*)avc, attrs);
169 #if defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV)
170 afs_getattr(OSI_VC_ARG(avc), attrs, flags, acred)
173 afs_getattr(OSI_VC_ARG(avc), attrs, acred)
177 struct AFS_UCRED *acred;
180 struct vrequest treq;
181 extern struct unixuser *afs_FindUser();
186 AFS_STATCNT(afs_getattr);
187 afs_Trace2(afs_iclSetp, CM_TRACE_GETATTR, ICL_TYPE_POINTER, avc,
188 ICL_TYPE_INT32, avc->m.Length);
190 #if defined(AFS_SUN_ENV) || defined(AFS_ALPHA_ENV) || defined(AFS_SUN5_ENV)
191 afs_BozonLock(&avc->pvnLock, avc);
194 if (afs_shuttingdown) return EIO;
196 if (!(avc->states & CStatd)) {
197 if (!(code = afs_InitReq(&treq, acred))) {
198 code = afs_VerifyVCache2(avc, &treq);
204 #if defined(AFS_SUN_ENV) || defined(AFS_ALPHA_ENV) || defined(AFS_SUN5_ENV)
205 if (code == 0) osi_FlushPages(avc, acred);
206 afs_BozonUnlock(&avc->pvnLock, avc);
211 osi_FlushText(avc); /* only needed to flush text if text locked last time */
212 code = afs_CopyOutAttrs(avc, attrs);
214 if (afs_nfsexporter) {
216 if (code = afs_InitReq(&treq, acred))
220 if (AFS_NFSXLATORREQ(acred)) {
221 if ((vType(avc) != VDIR) &&
222 !afs_AccessOK(avc, PRSFS_READ, &treq,
223 CHECK_MODE_BITS|CMB_ALLOW_EXEC_AS_READ)) {
226 if (avc->mvstat == 2) {
227 #if defined(AFS_SGI_ENV) || defined(AFS_SUN5_ENV) || defined(AFS_AIX41_ENV)
228 attrs->va_mtime.tv_nsec += ((++avc->xlatordv) * 1000);
230 attrs->va_mtime.tv_usec += ++avc->xlatordv;
234 if (au = afs_FindUser(treq.uid, -1, READ_LOCK)) {
235 register struct afs_exporter *exporter = au->exporter;
237 if (exporter && !(afs_nfsexporter->exp_states & EXP_UNIXMODE)) {
240 * If the remote user wishes to enforce default Unix mode semantics,
241 * like in the nfs exporter case, we OR in the user bits
242 * into the group and other bits. We need to do this
243 * because there is no RFS_ACCESS call and thus nfs
244 * clients implement nfs_access by interpreting the
245 * mode bits in the traditional way, which of course
248 ubits = (attrs->va_mode & 0700) >> 6;
249 attrs->va_mode = attrs->va_mode | ubits | (ubits << 3);
250 /* If it's the root of AFS, replace the inode number with the
251 * inode number of the mounted on directory; otherwise this
252 * confuses getwd()... */
253 #ifdef AFS_LINUX22_ENV
254 if (avc == afs_globalVp) {
255 struct inode *ip = avc->v.i_sb->s_root->d_inode;
256 attrs->va_nodeid = ip->i_ino;
259 if (avc->v.v_flag & VROOT) {
260 struct vnode *vp = (struct vnode *)avc;
262 vp = vp->v_vfsp->vfs_vnodecovered;
263 if (vp) { /* Ignore weird failures */
265 attrs->va_nodeid = VnodeToIno(vp);
269 ip = (struct inode *) VTOI(vp);
270 if (ip) /* Ignore weird failures */
271 attrs->va_nodeid = ip->i_number;
275 #endif /* AFS_LINUX22_ENV */
277 afs_PutUser(au, READ_LOCK);
283 code = afs_CheckCode(code, &treq, 14);
287 /* convert a Unix request into a status store request */
288 afs_VAttrToAS(avc, av, as)
289 register struct vcache *avc;
290 register struct vattr *av;
291 register struct AFSStoreStatus *as; {
294 AFS_STATCNT(afs_VAttrToAS);
295 #if defined(AFS_AIX_ENV)
296 /* Boy, was this machine dependent bogosity hard to swallow????.... */
297 if (av->va_mode != -1) {
299 #if defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV) || defined(AFS_LINUX22_ENV)
300 if (av->va_mask & AT_MODE) {
302 if (av->va_mode != ((unsigned short)-1)) {
306 as->UnixModeBits = av->va_mode & 0xffff;
307 if (avc->states & CForeign) {
308 ObtainWriteLock(&avc->lock,127);
309 afs_FreeAllAxs(&(avc->Access));
310 ReleaseWriteLock(&avc->lock);
314 #if defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV) || defined(AFS_LINUX22_ENV)
315 if (av->va_mask & AT_GID) {
317 #if (defined(AFS_HPUX_ENV) || defined(AFS_SUN_ENV))
318 #if defined(AFS_HPUX102_ENV)
319 if (av->va_gid != GID_NO_CHANGE) {
321 if (av->va_gid != ((unsigned short)-1)) {
324 if (av->va_gid != -1) {
326 #endif /* AFS_SUN5_ENV */
327 mask |= AFS_SETGROUP;
328 as->Group = av->va_gid;
331 #if defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV) || defined(AFS_LINUX22_ENV)
332 if (av->va_mask & AT_UID) {
334 #if (defined(AFS_HPUX_ENV) || defined(AFS_SUN_ENV))
335 #if defined(AFS_HPUX102_ENV)
336 if (av->va_uid != UID_NO_CHANGE) {
338 if (av->va_uid != ((unsigned short)-1)) {
341 if (av->va_uid != -1) {
343 #endif /* AFS_SUN5_ENV */
344 mask |= AFS_SETOWNER;
345 as->Owner = av->va_uid;
347 #if defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV) || defined(AFS_LINUX22_ENV)
348 if (av->va_mask & AT_MTIME) {
350 if (av->va_mtime.tv_sec != -1) {
352 mask |= AFS_SETMODTIME;
354 #if defined(AFS_SUN5_ENV) || defined(AFS_AIX41_ENV)
355 if (av->va_mtime.tv_nsec == -1)
357 if (av->va_mtime.tv_usec == -1)
359 as->ClientModTime = osi_Time(); /* special Sys V compat hack for Suns */
362 as->ClientModTime = av->va_mtime.tv_sec;
368 /* We don't set CDirty bit in avc->states because setattr calls WriteVCache
369 * synchronously, therefore, it's not needed.
371 #if defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV)
372 afs_setattr(OSI_VC_ARG(avc), attrs, flags, acred)
375 afs_setattr(avc, attrs, acred)
378 register struct vattr *attrs;
379 struct AFS_UCRED *acred; {
380 struct vrequest treq;
381 struct AFSStoreStatus astat;
382 register afs_int32 code;
385 AFS_STATCNT(afs_setattr);
386 afs_Trace2(afs_iclSetp, CM_TRACE_SETATTR, ICL_TYPE_POINTER, avc,
387 ICL_TYPE_INT32, avc->m.Length);
388 if (code = afs_InitReq(&treq, acred)) return code;
389 if (avc->states & CRO) {
393 #if defined(AFS_SGI_ENV)
394 /* ignore ATTR_LAZY calls - they are really only for keeping
395 * the access/mtime of mmaped files up to date
397 if (flags & ATTR_LAZY)
401 /* if file size has changed, we need write access, otherwise (e.g.
402 * chmod) give it a shot; if it fails, we'll discard the status
405 * Note that Ultrix actually defines ftruncate of a file you have open to
406 * be O.K., and does the proper access checks itself in the truncate
407 * path (unlike BSD or SUNOS), so we skip this check for Ultrix.
410 #if defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV) || defined(AFS_LINUX22_ENV)
411 if (attrs->va_mask & AT_SIZE) {
414 if (attrs->va_size != VNOVAL) {
417 if (attrs->va_size != -1) {
419 if (attrs->va_size != ~0) {
423 if (!afs_AccessOK(avc, PRSFS_WRITE, &treq, DONT_CHECK_MODE_BITS)) {
430 afs_VAttrToAS(avc, attrs, &astat); /* interpret request */
432 #if defined(AFS_SUN_ENV) || defined(AFS_ALPHA_ENV) || defined(AFS_SUN5_ENV)
433 afs_BozonLock(&avc->pvnLock, avc);
435 #if defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV)
436 if (AFS_NFSXLATORREQ(acred)) {
437 avc->execsOrWriters++;
441 #if defined(AFS_SGI_ENV)
442 AFS_RWLOCK((vnode_t *)avc, VRWLOCK_WRITE);
444 #if defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV) || defined(AFS_LINUX22_ENV)
445 if (attrs->va_mask & AT_SIZE) {
447 #if defined(AFS_OSF_ENV)
448 if (attrs->va_size != VNOVAL) {
449 #else /* AFS_OSF_ENV */
450 if (attrs->va_size != -1) {
453 ObtainWriteLock(&avc->lock,128);
454 avc->states |= CDirty;
455 code = afs_TruncateAllSegments(avc, (afs_int32)attrs->va_size, &treq, acred);
456 /* if date not explicitly set by this call, set it ourselves, since we
457 * changed the data */
458 if (!(astat.Mask & AFS_SETMODTIME)) {
459 astat.Mask |= AFS_SETMODTIME;
460 astat.ClientModTime = osi_Time();
463 if ( ( (avc->execsOrWriters <= 0 ) && (avc->states & CCreating) == 0)
464 || ( avc->execsOrWriters == 1 && AFS_NFSXLATORREQ(acred)) ) {
465 code = afs_StoreAllSegments(avc, &treq, AFS_ASYNC);
467 avc->states &= ~CDirty;
470 avc->states &= ~CDirty;
472 ReleaseWriteLock(&avc->lock);
474 osi_FlushText(avc); /* do this after releasing all locks */
476 /* in case we changed the size here, propagate it to gp->g_size */
477 afs_gfshack((struct gnode *)avc);
481 ObtainSharedLock(&avc->lock,16); /* lock entry */
482 code = afs_WriteVCache(avc, &astat, &treq); /* send request */
483 ReleaseSharedLock(&avc->lock); /* release lock */
486 ObtainWriteLock(&afs_xcbhash, 487);
487 afs_DequeueCallback(avc);
488 avc->states &= ~CStatd;
489 ReleaseWriteLock(&afs_xcbhash);
490 if (avc->fid.Fid.Vnode & 1 || (vType(avc) == VDIR))
491 osi_dnlc_purgedp(avc);
492 /* error? erase any changes we made to vcache entry */
495 #if defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV)
496 if (AFS_NFSXLATORREQ(acred)) {
497 avc->execsOrWriters--;
500 #if defined(AFS_SUN_ENV) || defined(AFS_OSF_ENV) || defined(AFS_SUN5_ENV)
501 afs_BozonUnlock(&avc->pvnLock, avc);
503 #if defined(AFS_SGI_ENV)
504 AFS_RWUNLOCK((vnode_t *)avc, VRWLOCK_WRITE);
507 code = afs_CheckCode(code, &treq, 15);