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
16 #include <afsconfig.h>
17 #include "afs/param.h"
22 #include "afs/sysincludes.h" /* Standard vendor system headers */
23 #include "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"
28 #include "afs/unified_afs.h"
30 /* question: does afs_create need to set CDirty in the adp or the avc?
31 * I think we can get away without it, but I'm not sure. Note that
32 * afs_setattr is called in here for truncation.
36 afs_create(OSI_VC_DECL(adp), char *aname, struct vattr *attrs, int flags,
37 int amode, struct vcache **avcp, struct AFS_UCRED *acred)
38 #else /* AFS_SGI64_ENV */
40 afs_create(OSI_VC_DECL(adp), char *aname, struct vattr *attrs,
41 enum vcexcl aexcl, int amode, struct vcache **avcp,
42 struct AFS_UCRED *acred)
43 #endif /* AFS_SGI64_ENV */
45 afs_int32 origCBs, origZaps, finalZaps;
47 register afs_int32 code;
48 register struct afs_conn *tc;
49 struct VenusFid newFid;
50 struct AFSStoreStatus InStatus;
51 struct AFSFetchStatus OutFidStatus, OutDirStatus;
52 struct AFSVolSync tsync;
53 struct AFSCallBack CallBack;
56 afs_size_t offset, len;
57 struct server *hostp = 0;
59 struct volume *volp = 0;
60 struct afs_fakestat_state fakestate;
65 AFS_STATCNT(afs_create);
66 if ((code = afs_InitReq(&treq, acred)))
69 afs_Trace3(afs_iclSetp, CM_TRACE_CREATE, ICL_TYPE_POINTER, adp,
70 ICL_TYPE_STRING, aname, ICL_TYPE_INT32, amode);
72 afs_InitFakeStat(&fakestate);
75 /* If avcp is passed not null, it's the old reference to this file.
76 * We can use this to avoid create races. For now, just decrement
77 * the reference count on it.
80 AFS_RELE(AFSTOV(*avcp));
85 if (strlen(aname) > AFSNAMEMAX) {
90 if (!afs_ENameOK(aname)) {
94 switch (attrs->va_type) {
97 #if !defined(AFS_SUN5_ENV)
101 /* We don't support special devices or FIFOs */
109 code = afs_EvalFakeStat(&adp, &fakestate, &treq);
113 code = afs_VerifyVCache(adp, &treq);
117 /** If the volume is read-only, return error without making an RPC to the
120 if (adp->f.states & CRO) {
125 if (AFS_IS_DISCONNECTED && !AFS_IS_DISCON_RW) {
130 tdc = afs_GetDCache(adp, (afs_size_t) 0, &treq, &offset, &len, 1);
131 ObtainWriteLock(&adp->lock, 135);
133 ObtainSharedLock(&tdc->lock, 630);
136 * Make sure that the data in the cache is current. We may have
137 * received a callback while we were waiting for the write lock.
139 if (!(adp->f.states & CStatd)
140 || (tdc && !hsame(adp->f.m.DataVersion, tdc->f.versionNo))) {
141 ReleaseWriteLock(&adp->lock);
143 ReleaseSharedLock(&tdc->lock);
149 /* see if file already exists. If it does, we only set
150 * the size attributes (to handle O_TRUNC) */
151 code = afs_dir_Lookup(tdc, aname, &newFid.Fid); /* use dnlc first xxx */
153 ReleaseSharedLock(&tdc->lock);
155 ReleaseWriteLock(&adp->lock);
159 if (aexcl != NONEXCL) {
161 code = EEXIST; /* file exists in excl mode open */
164 /* found the file, so use it */
165 newFid.Cell = adp->f.fid.Cell;
166 newFid.Fid.Volume = adp->f.fid.Fid.Volume;
168 if (newFid.Fid.Unique == 0) {
169 tvc = afs_LookupVCache(&newFid, &treq, NULL, adp, aname);
171 if (!tvc) /* lookup failed or wasn't called */
172 tvc = afs_GetVCache(&newFid, &treq, NULL, NULL);
175 /* if the thing exists, we need the right access to open it.
176 * we must check that here, since no other checks are
177 * made by the open system call */
178 len = attrs->va_size; /* only do the truncate */
180 * We used to check always for READ access before; the
181 * problem is that we will fail if the existing file
182 * has mode -w-w-w, which is wrong.
185 && !afs_AccessOK(tvc, PRSFS_READ, &treq, CHECK_MODE_BITS)) {
190 #if defined(AFS_DARWIN80_ENV)
191 if ((amode & VWRITE) || VATTR_IS_ACTIVE(attrs, va_data_size))
192 #elif defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV)
193 if ((amode & VWRITE) || (attrs->va_mask & AT_SIZE))
195 if ((amode & VWRITE) || len != 0xffffffff)
198 /* needed for write access check */
199 tvc->f.parent.vnode = adp->f.fid.Fid.Vnode;
200 tvc->f.parent.unique = adp->f.fid.Fid.Unique;
201 /* need write mode for these guys */
203 (tvc, PRSFS_WRITE, &treq, CHECK_MODE_BITS)) {
209 #if defined(AFS_DARWIN80_ENV)
210 if (VATTR_IS_ACTIVE(attrs, va_data_size))
211 #elif defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV)
212 if (attrs->va_mask & AT_SIZE)
214 if (len != 0xffffffff)
217 if (vType(tvc) != VREG) {
223 #if defined(AFS_DARWIN80_ENV)
225 VATTR_SET_SUPPORTED(attrs, va_data_size);
226 VATTR_SET_ACTIVE(attrs, va_data_size);
227 #elif defined(UKERNEL)
228 attrs->va_mask = ATTR_SIZE;
229 #elif defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV)
230 attrs->va_mask = AT_SIZE;
234 attrs->va_size = len;
235 ObtainWriteLock(&tvc->lock, 136);
236 tvc->f.states |= CCreating;
237 ReleaseWriteLock(&tvc->lock);
238 #if defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV)
239 #if defined(AFS_SGI64_ENV)
241 afs_setattr(VNODE_TO_FIRST_BHV((vnode_t *) tvc),
244 code = afs_setattr(tvc, attrs, 0, acred);
245 #endif /* AFS_SGI64_ENV */
246 #else /* SUN5 || SGI */
247 code = afs_setattr(tvc, attrs, acred);
248 #endif /* SUN5 || SGI */
249 ObtainWriteLock(&tvc->lock, 137);
250 tvc->f.states &= ~CCreating;
251 ReleaseWriteLock(&tvc->lock);
259 code = ENOENT; /* shouldn't get here */
260 /* make sure vrefCount bumped only if code == 0 */
265 /* if we create the file, we don't do any access checks, since
266 * that's how O_CREAT is supposed to work */
267 if (adp->f.states & CForeign) {
268 origCBs = afs_allCBs;
269 origZaps = afs_allZaps;
271 origCBs = afs_evenCBs; /* if changes, we don't really have a callback */
272 origZaps = afs_evenZaps; /* number of even numbered vnodes discarded */
274 InStatus.Mask = AFS_SETMODTIME | AFS_SETMODE | AFS_SETGROUP;
275 InStatus.ClientModTime = osi_Time();
276 InStatus.Group = (afs_int32) acred->cr_gid;
277 if (AFS_NFSXLATORREQ(acred)) {
279 * XXX The following is mainly used to fix a bug in the HP-UX
280 * nfs client where they create files with mode of 0 without
281 * doing any setattr later on to fix it. * XXX
283 #if defined(AFS_AIX_ENV)
284 if (attrs->va_mode != -1) {
286 #if defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV)
287 if (attrs->va_mask & AT_MODE) {
289 if (attrs->va_mode != ((unsigned short)-1)) {
293 attrs->va_mode = 0x1b6; /* XXX default mode: rw-rw-rw XXX */
297 if (!AFS_IS_DISCONNECTED) {
298 /* If not disconnected, connect to the server.*/
300 InStatus.UnixModeBits = attrs->va_mode & 0xffff; /* only care about protection bits */
302 tc = afs_Conn(&adp->f.fid, &treq, SHARED_LOCK);
304 hostp = tc->srvr->server; /* remember for callback processing */
306 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_CREATEFILE);
309 RXAFS_CreateFile(tc->id, (struct AFSFid *)&adp->f.fid.Fid,
310 aname, &InStatus, (struct AFSFid *)
311 &newFid.Fid, &OutFidStatus, &OutDirStatus,
315 CallBack.ExpirationTime += now;
319 (tc, code, &adp->f.fid, &treq, AFS_STATS_FS_RPCIDX_CREATEFILE,
322 if ((code == EEXIST || code == UAEEXIST) &&
325 #else /* AFS_SGI64_ENV */
329 /* if we get an EEXIST in nonexcl mode, just do a lookup */
331 ReleaseSharedLock(&tdc->lock);
334 ReleaseWriteLock(&adp->lock);
335 #if !(defined(AFS_OSF_ENV) || defined(AFS_DARWIN_ENV))
336 #if defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV)
337 #if defined(AFS_SGI64_ENV)
338 code = afs_lookup(VNODE_TO_FIRST_BHV((vnode_t *) adp), aname, avcp,
339 NULL, 0, NULL, acred);
341 code = afs_lookup(adp, aname, avcp, NULL, 0, NULL, acred);
342 #endif /* AFS_SGI64_ENV */
343 #else /* SUN5 || SGI */
345 code = afs_lookup(adp, aname, avcp, acred, 0);
347 code = afs_lookup(adp, aname, avcp, acred);
349 #endif /* SUN5 || SGI */
350 #endif /* !(AFS_OSF_ENV || AFS_DARWIN_ENV) */
356 ObtainWriteLock(&afs_xcbhash, 488);
357 afs_DequeueCallback(adp);
358 adp->f.states &= ~CStatd;
359 ReleaseWriteLock(&afs_xcbhash);
360 osi_dnlc_purgedp(adp);
362 ReleaseWriteLock(&adp->lock);
364 ReleaseSharedLock(&tdc->lock);
371 #if defined(AFS_DISCON_ENV)
372 /* Generate a fake FID for disconnected mode. */
373 newFid.Cell = adp->f.fid.Cell;
374 newFid.Fid.Volume = adp->f.fid.Fid.Volume;
375 afs_GenFakeFid(&newFid, VREG, 1);
377 } /* if (!AFS_IS_DISCON_RW) */
379 /* otherwise, we should see if we can make the change to the dir locally */
381 UpgradeSToWLock(&tdc->lock, 631);
382 if (AFS_IS_DISCON_RW || afs_LocalHero(adp, tdc, &OutDirStatus, 1)) {
383 /* we can do it locally */
384 ObtainWriteLock(&afs_xdcache, 291);
385 code = afs_dir_Create(tdc, aname, &newFid.Fid);
386 ReleaseWriteLock(&afs_xdcache);
393 ReleaseWriteLock(&tdc->lock);
396 if (AFS_IS_DISCON_RW)
397 adp->f.m.LinkCount++;
399 newFid.Cell = adp->f.fid.Cell;
400 newFid.Fid.Volume = adp->f.fid.Fid.Volume;
401 ReleaseWriteLock(&adp->lock);
402 volp = afs_FindVolume(&newFid, READ_LOCK);
404 /* New tricky optimistic callback handling algorithm for file creation works
405 * as follows. We create the file essentially with no locks set at all. File
406 * server may thus handle operations from others cache managers as well as from
407 * this very own cache manager that reference the file in question before
408 * we managed to create the cache entry. However, if anyone else changes
409 * any of the status information for a file, we'll see afs_evenCBs increase
410 * (files always have even fids). If someone on this workstation manages
411 * to do something to the file, they'll end up having to create a cache
412 * entry for the new file. Either we'll find it once we've got the afs_xvcache
413 * lock set, or it was also *deleted* the vnode before we got there, in which case
414 * we will find evenZaps has changed, too. Thus, we only assume we have the right
415 * status information if no callbacks or vnode removals have occurred to even
416 * numbered files from the time the call started until the time that we got the xvcache
417 * lock set. Of course, this also assumes that any call that modifies a file first
418 * gets a write lock on the file's vnode, but if that weren't true, the whole cache manager
419 * would fail, since no call would be able to update the local vnode status after modifying
420 * a file on a file server. */
421 ObtainWriteLock(&afs_xvcache, 138);
422 if (adp->f.states & CForeign)
423 finalZaps = afs_allZaps; /* do this before calling newvcache */
425 finalZaps = afs_evenZaps; /* do this before calling newvcache */
426 /* don't need to call RemoveVCB, since only path leaving a callback is the
427 * one where we pass through afs_NewVCache. Can't have queued a VCB unless
428 * we created and freed an entry between file creation time and here, and the
429 * freeing of the vnode will change evenZaps. Don't need to update the VLRU
430 * queue, since the find will only succeed in the event of a create race, and
431 * then the vcache will be at the front of the VLRU queue anyway... */
432 if (!(tvc = afs_FindVCache(&newFid, 0, DO_STATS))) {
433 tvc = afs_NewVCache(&newFid, hostp);
436 ObtainWriteLock(&tvc->lock, 139);
438 ObtainWriteLock(&afs_xcbhash, 489);
439 finalCBs = afs_evenCBs;
440 /* add the callback in */
441 if (adp->f.states & CForeign) {
442 tvc->f.states |= CForeign;
443 finalCBs = afs_allCBs;
445 if (origCBs == finalCBs && origZaps == finalZaps) {
446 tvc->f.states |= CStatd; /* we've fake entire thing, so don't stat */
447 tvc->f.states &= ~CBulkFetching;
448 if (!AFS_IS_DISCON_RW) {
449 tvc->cbExpires = CallBack.ExpirationTime;
450 afs_QueueCallback(tvc, CBHash(CallBack.ExpirationTime), volp);
453 afs_DequeueCallback(tvc);
454 tvc->f.states &= ~(CStatd | CUnique);
456 if (tvc->f.fid.Fid.Vnode & 1 || (vType(tvc) == VDIR))
457 osi_dnlc_purgedp(tvc);
459 ReleaseWriteLock(&afs_xcbhash);
460 if (AFS_IS_DISCON_RW) {
461 #if defined(AFS_DISCON_ENV)
462 afs_DisconAddDirty(tvc, VDisconCreate, 0);
463 afs_GenDisconStatus(adp, tvc, &newFid, attrs, &treq, VREG);
466 afs_ProcessFS(tvc, &OutFidStatus, &treq);
469 tvc->f.parent.vnode = adp->f.fid.Fid.Vnode;
470 tvc->f.parent.unique = adp->f.fid.Fid.Unique;
471 ReleaseWriteLock(&tvc->lock);
477 /* otherwise cache entry already exists, someone else must
478 * have created it. Comments used to say: "don't need write
479 * lock to *clear* these flags" but we should do it anyway.
480 * Code used to clear stat bit and callback, but I don't see
481 * the point -- we didn't have a create race, somebody else just
482 * snuck into NewVCache before we got here, probably a racing
488 ReleaseWriteLock(&afs_xvcache);
495 afs_PutVolume(volp, READ_LOCK);
498 afs_AddMarinerName(aname, *avcp);
499 /* return the new status in vattr */
500 afs_CopyOutAttrs(*avcp, attrs);
503 if (!code && !strcmp(aname, "core"))
504 tvc->f.states |= CCore1;
507 afs_PutFakeStat(&fakestate);
508 code = afs_CheckCode(code, &treq, 20);
516 * Check to see if we can track the change locally: requires that
517 * we have sufficiently recent info in data cache. If so, we
518 * know the new DataVersion number, and place it correctly in both the
519 * data and stat cache entries. This routine returns 1 if we should
520 * do the operation locally, and 0 otherwise.
522 * This routine must be called with the stat cache entry write-locked,
523 * and dcache entry write-locked.
526 afs_LocalHero(register struct vcache *avc, register struct dcache *adc,
527 register AFSFetchStatus * astat, register int aincr)
529 register afs_int32 ok;
532 AFS_STATCNT(afs_LocalHero);
533 hset64(avers, astat->dataVersionHigh, astat->DataVersion);
534 /* this *is* the version number, no matter what */
536 ok = (hsame(avc->f.m.DataVersion, adc->f.versionNo) && avc->callback
537 && (avc->f.states & CStatd) && avc->cbExpires >= osi_Time());
541 #if defined(AFS_SGI_ENV)
542 osi_Assert(avc->v.v_type == VDIR);
544 /* The bulk status code used the length as a sequence number. */
545 /* Don't update the vcache entry unless the stats are current. */
546 if (avc->f.states & CStatd) {
547 hset(avc->f.m.DataVersion, avers);
548 #ifdef AFS_64BIT_CLIENT
549 FillInt64(avc->f.m.Length, astat->Length_hi, astat->Length);
550 #else /* AFS_64BIT_CLIENT */
551 avc->f.m.Length = astat->Length;
552 #endif /* AFS_64BIT_CLIENT */
553 avc->f.m.Date = astat->ClientModTime;
556 /* we've been tracking things correctly */
557 adc->dflags |= DFEntryMod;
558 adc->f.versionNo = avers;
565 if (avc->f.states & CStatd) {
566 osi_dnlc_purgedp(avc);