uae-translate-create-20061013
[openafs.git] / src / afs / VNOPS / afs_vnop_create.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
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
8  */
9
10 /*
11  * Implements:
12  * afs_create
13  * afs_LocalHero
14  */
15
16 #include <afsconfig.h>
17 #include "afs/param.h"
18
19 RCSID
20     ("$Header$");
21
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"
29
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.
33  */
34 #ifdef AFS_SGI64_ENV
35 int
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 */
39 int
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 */
44 {
45     afs_int32 origCBs, origZaps, finalZaps;
46     struct vrequest treq;
47     register afs_int32 code;
48     register struct conn *tc;
49     struct VenusFid newFid;
50     struct AFSStoreStatus InStatus;
51     struct AFSFetchStatus OutFidStatus, OutDirStatus;
52     struct AFSVolSync tsync;
53     struct AFSCallBack CallBack;
54     afs_int32 now;
55     struct dcache *tdc;
56     afs_size_t offset, len;
57     struct server *hostp = 0;
58     struct vcache *tvc;
59     struct volume *volp = 0;
60     struct afs_fakestat_state fakestate;
61     XSTATS_DECLS;
62     OSI_VC_CONVERT(adp);
63
64
65     AFS_STATCNT(afs_create);
66     if ((code = afs_InitReq(&treq, acred)))
67         goto done2;
68
69     afs_Trace3(afs_iclSetp, CM_TRACE_CREATE, ICL_TYPE_POINTER, adp,
70                ICL_TYPE_STRING, aname, ICL_TYPE_INT32, amode);
71
72     afs_InitFakeStat(&fakestate);
73
74 #ifdef AFS_SGI65_ENV
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.
78      */
79     if (*avcp) {
80         AFS_RELE(AFSTOV(*avcp));
81         *avcp = NULL;
82     }
83 #endif
84
85     if (strlen(aname) > AFSNAMEMAX) {
86         code = ENAMETOOLONG;
87         goto done;
88     }
89
90     if (!afs_ENameOK(aname)) {
91         code = EINVAL;
92         goto done;
93     }
94     switch (attrs->va_type) {
95     case VBLK:
96     case VCHR:
97 #if     !defined(AFS_SUN5_ENV)
98     case VSOCK:
99 #endif
100     case VFIFO:
101         /* We don't support special devices or FIFOs */
102         code = EINVAL;
103         goto done;
104     default:
105         ;
106     }
107     code = afs_EvalFakeStat(&adp, &fakestate, &treq);
108     if (code)
109         goto done;
110   tagain:
111     code = afs_VerifyVCache(adp, &treq);
112     if (code)
113         goto done;
114
115     /** If the volume is read-only, return error without making an RPC to the
116       * fileserver
117       */
118     if (adp->states & CRO) {
119         code = EROFS;
120         goto done;
121     }
122
123     tdc = afs_GetDCache(adp, (afs_size_t) 0, &treq, &offset, &len, 1);
124     ObtainWriteLock(&adp->lock, 135);
125     if (tdc)
126         ObtainSharedLock(&tdc->lock, 630);
127
128     /*
129      * Make sure that the data in the cache is current. We may have
130      * received a callback while we were waiting for the write lock.
131      */
132     if (!(adp->states & CStatd)
133         || (tdc && !hsame(adp->m.DataVersion, tdc->f.versionNo))) {
134         ReleaseWriteLock(&adp->lock);
135         if (tdc) {
136             ReleaseSharedLock(&tdc->lock);
137             afs_PutDCache(tdc);
138         }
139         goto tagain;
140     }
141     if (tdc) {
142         /* see if file already exists.  If it does, we only set 
143          * the size attributes (to handle O_TRUNC) */
144         code = afs_dir_Lookup(tdc, aname, &newFid.Fid); /* use dnlc first xxx */
145         if (code == 0) {
146             ReleaseSharedLock(&tdc->lock);
147             afs_PutDCache(tdc);
148             ReleaseWriteLock(&adp->lock);
149 #ifdef AFS_SGI64_ENV
150             if (flags & VEXCL) {
151 #else
152             if (aexcl != NONEXCL) {
153 #endif
154                 code = EEXIST;  /* file exists in excl mode open */
155                 goto done;
156             }
157             /* found the file, so use it */
158             newFid.Cell = adp->fid.Cell;
159             newFid.Fid.Volume = adp->fid.Fid.Volume;
160             tvc = NULL;
161             if (newFid.Fid.Unique == 0) {
162                 tvc = afs_LookupVCache(&newFid, &treq, NULL, adp, aname);
163             }
164             if (!tvc)           /* lookup failed or wasn't called */
165                 tvc = afs_GetVCache(&newFid, &treq, NULL, NULL);
166
167             if (tvc) {
168                 /* if the thing exists, we need the right access to open it.
169                  * we must check that here, since no other checks are
170                  * made by the open system call */
171                 len = attrs->va_size;   /* only do the truncate */
172                 /*
173                  * We used to check always for READ access before; the
174                  * problem is that we will fail if the existing file
175                  * has mode -w-w-w, which is wrong.
176                  */
177                 if ((amode & VREAD)
178                     && !afs_AccessOK(tvc, PRSFS_READ, &treq, CHECK_MODE_BITS)) {
179                     afs_PutVCache(tvc);
180                     code = EACCES;
181                     goto done;
182                 }
183 #if defined(AFS_DARWIN80_ENV)
184                 if ((amode & VWRITE) || VATTR_IS_ACTIVE(attrs, va_data_size))
185 #elif defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV)
186                 if ((amode & VWRITE) || (attrs->va_mask & AT_SIZE))
187 #else
188                 if ((amode & VWRITE) || len != 0xffffffff)
189 #endif
190                 {
191                     /* needed for write access check */
192                     tvc->parentVnode = adp->fid.Fid.Vnode;
193                     tvc->parentUnique = adp->fid.Fid.Unique;
194                     /* need write mode for these guys */
195                     if (!afs_AccessOK
196                         (tvc, PRSFS_WRITE, &treq, CHECK_MODE_BITS)) {
197                         afs_PutVCache(tvc);
198                         code = EACCES;
199                         goto done;
200                     }
201                 }
202 #if defined(AFS_DARWIN80_ENV)
203                 if (VATTR_IS_ACTIVE(attrs, va_data_size))
204 #elif defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV)
205                 if (attrs->va_mask & AT_SIZE)
206 #else
207                 if (len != 0xffffffff)
208 #endif
209                 {
210                     if (vType(tvc) != VREG) {
211                         afs_PutVCache(tvc);
212                         code = EISDIR;
213                         goto done;
214                     }
215                     /* do a truncate */
216 #if defined(AFS_DARWIN80_ENV)
217                     VATTR_INIT(attrs);
218                     VATTR_SET_SUPPORTED(attrs, va_data_size);
219                     VATTR_SET_ACTIVE(attrs, va_data_size);
220 #elif defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV)
221                     attrs->va_mask = AT_SIZE;
222 #else
223                     VATTR_NULL(attrs);
224 #endif
225                     attrs->va_size = len;
226                     ObtainWriteLock(&tvc->lock, 136);
227                     tvc->states |= CCreating;
228                     ReleaseWriteLock(&tvc->lock);
229 #if defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV)
230 #if defined(AFS_SGI64_ENV)
231                     code =
232                         afs_setattr(VNODE_TO_FIRST_BHV((vnode_t *) tvc),
233                                     attrs, 0, acred);
234 #else
235                     code = afs_setattr(tvc, attrs, 0, acred);
236 #endif /* AFS_SGI64_ENV */
237 #else /* SUN5 || SGI */
238                     code = afs_setattr(tvc, attrs, acred);
239 #endif /* SUN5 || SGI */
240                     ObtainWriteLock(&tvc->lock, 137);
241                     tvc->states &= ~CCreating;
242                     ReleaseWriteLock(&tvc->lock);
243                     if (code) {
244                         afs_PutVCache(tvc);
245                         goto done;
246                     }
247                 }
248                 *avcp = tvc;
249             } else
250                 code = ENOENT;  /* shouldn't get here */
251             /* make sure vrefCount bumped only if code == 0 */
252             goto done;
253         }
254     }
255
256     /* if we create the file, we don't do any access checks, since
257      * that's how O_CREAT is supposed to work */
258     if (adp->states & CForeign) {
259         origCBs = afs_allCBs;
260         origZaps = afs_allZaps;
261     } else {
262         origCBs = afs_evenCBs;  /* if changes, we don't really have a callback */
263         origZaps = afs_evenZaps;        /* number of even numbered vnodes discarded */
264     }
265     InStatus.Mask = AFS_SETMODTIME | AFS_SETMODE | AFS_SETGROUP;
266     InStatus.ClientModTime = osi_Time();
267     InStatus.Group = (afs_int32) acred->cr_gid;
268     if (AFS_NFSXLATORREQ(acred)) {
269         /*
270          * XXX The following is mainly used to fix a bug in the HP-UX
271          * nfs client where they create files with mode of 0 without
272          * doing any setattr later on to fix it.  * XXX
273          */
274 #if     defined(AFS_AIX_ENV)
275         if (attrs->va_mode != -1) {
276 #else
277 #if     defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV)
278         if (attrs->va_mask & AT_MODE) {
279 #else
280         if (attrs->va_mode != ((unsigned short)-1)) {
281 #endif
282 #endif
283             if (!attrs->va_mode)
284                 attrs->va_mode = 0x1b6; /* XXX default mode: rw-rw-rw XXX */
285         }
286     }
287     InStatus.UnixModeBits = attrs->va_mode & 0xffff;    /* only care about protection bits */
288     do {
289         tc = afs_Conn(&adp->fid, &treq, SHARED_LOCK);
290         if (tc) {
291             hostp = tc->srvr->server;   /* remember for callback processing */
292             now = osi_Time();
293             XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_CREATEFILE);
294             RX_AFS_GUNLOCK();
295             code =
296                 RXAFS_CreateFile(tc->id, (struct AFSFid *)&adp->fid.Fid,
297                                  aname, &InStatus, (struct AFSFid *)
298                                  &newFid.Fid, &OutFidStatus, &OutDirStatus,
299                                  &CallBack, &tsync);
300             RX_AFS_GLOCK();
301             XSTATS_END_TIME;
302             CallBack.ExpirationTime += now;
303         } else
304             code = -1;
305     } while (afs_Analyze
306              (tc, code, &adp->fid, &treq, AFS_STATS_FS_RPCIDX_CREATEFILE,
307               SHARED_LOCK, NULL));
308
309     if ((code == EEXIST || code == UAEEXIST) &&
310 #ifdef AFS_SGI64_ENV
311     !(flags & VEXCL)
312 #else /* AFS_SGI64_ENV */
313     aexcl == NONEXCL
314 #endif
315     ) {
316         /* if we get an EEXIST in nonexcl mode, just do a lookup */
317         if (tdc) {
318             ReleaseSharedLock(&tdc->lock);
319             afs_PutDCache(tdc);
320         }
321         ReleaseWriteLock(&adp->lock);
322 #if !(defined(AFS_OSF_ENV) || defined(AFS_DARWIN_ENV))
323 #if defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV)
324 #if defined(AFS_SGI64_ENV)
325         code = afs_lookup(VNODE_TO_FIRST_BHV((vnode_t *) adp), aname, avcp, 
326                           NULL, 0, NULL, acred);
327 #else
328         code = afs_lookup(adp, aname, avcp, NULL, 0, NULL, acred);
329 #endif /* AFS_SGI64_ENV */
330 #else /* SUN5 || SGI */
331         code = afs_lookup(adp, aname, avcp, acred);
332 #endif /* SUN5 || SGI */
333 #endif /* !(AFS_OSF_ENV || AFS_DARWIN_ENV) */
334         goto done;
335     }
336     if (code) {
337         if (code < 0) {
338             ObtainWriteLock(&afs_xcbhash, 488);
339             afs_DequeueCallback(adp);
340             adp->states &= ~CStatd;
341             ReleaseWriteLock(&afs_xcbhash);
342             osi_dnlc_purgedp(adp);
343         }
344         ReleaseWriteLock(&adp->lock);
345         if (tdc) {
346             ReleaseSharedLock(&tdc->lock);
347             afs_PutDCache(tdc);
348         }
349         goto done;
350     }
351     /* otherwise, we should see if we can make the change to the dir locally */
352     if (tdc)
353         UpgradeSToWLock(&tdc->lock, 631);
354     if (afs_LocalHero(adp, tdc, &OutDirStatus, 1)) {
355         /* we can do it locally */
356         code = afs_dir_Create(tdc, aname, &newFid.Fid);
357         if (code) {
358             ZapDCE(tdc);
359             DZap(tdc);
360         }
361     }
362     if (tdc) {
363         ReleaseWriteLock(&tdc->lock);
364         afs_PutDCache(tdc);
365     }
366     newFid.Cell = adp->fid.Cell;
367     newFid.Fid.Volume = adp->fid.Fid.Volume;
368     ReleaseWriteLock(&adp->lock);
369     volp = afs_FindVolume(&newFid, READ_LOCK);
370
371     /* New tricky optimistic callback handling algorithm for file creation works
372      * as follows.  We create the file essentially with no locks set at all.  File
373      * server may thus handle operations from others cache managers as well as from
374      * this very own cache manager that reference the file in question before
375      * we managed to create the cache entry.  However, if anyone else changes
376      * any of the status information for a file, we'll see afs_evenCBs increase
377      * (files always have even fids).  If someone on this workstation manages
378      * to do something to the file, they'll end up having to create a cache
379      * entry for the new file.  Either we'll find it once we've got the afs_xvcache
380      * lock set, or it was also *deleted* the vnode before we got there, in which case
381      * we will find evenZaps has changed, too.  Thus, we only assume we have the right
382      * status information if no callbacks or vnode removals have occurred to even
383      * numbered files from the time the call started until the time that we got the xvcache
384      * lock set.  Of course, this also assumes that any call that modifies a file first
385      * gets a write lock on the file's vnode, but if that weren't true, the whole cache manager
386      * would fail, since no call would be able to update the local vnode status after modifying
387      * a file on a file server. */
388     ObtainWriteLock(&afs_xvcache, 138);
389     if (adp->states & CForeign)
390         finalZaps = afs_allZaps;        /* do this before calling newvcache */
391     else
392         finalZaps = afs_evenZaps;       /* do this before calling newvcache */
393     /* don't need to call RemoveVCB, since only path leaving a callback is the
394      * one where we pass through afs_NewVCache.  Can't have queued a VCB unless
395      * we created and freed an entry between file creation time and here, and the
396      * freeing of the vnode will change evenZaps.  Don't need to update the VLRU
397      * queue, since the find will only succeed in the event of a create race, and 
398      * then the vcache will be at the front of the VLRU queue anyway...  */
399     if (!(tvc = afs_FindVCache(&newFid, 0, DO_STATS))) {
400         tvc = afs_NewVCache(&newFid, hostp);
401         if (tvc) {
402             int finalCBs;
403             ObtainWriteLock(&tvc->lock, 139);
404
405             ObtainWriteLock(&afs_xcbhash, 489);
406             finalCBs = afs_evenCBs;
407             /* add the callback in */
408             if (adp->states & CForeign) {
409                 tvc->states |= CForeign;
410                 finalCBs = afs_allCBs;
411             }
412             if (origCBs == finalCBs && origZaps == finalZaps) {
413                 tvc->states |= CStatd;  /* we've fake entire thing, so don't stat */
414                 tvc->states &= ~CBulkFetching;
415                 tvc->cbExpires = CallBack.ExpirationTime;
416                 afs_QueueCallback(tvc, CBHash(CallBack.ExpirationTime), volp);
417             } else {
418                 afs_DequeueCallback(tvc);
419                 tvc->states &= ~(CStatd | CUnique);
420                 tvc->callback = 0;
421                 if (tvc->fid.Fid.Vnode & 1 || (vType(tvc) == VDIR))
422                     osi_dnlc_purgedp(tvc);
423             }
424             ReleaseWriteLock(&afs_xcbhash);
425             afs_ProcessFS(tvc, &OutFidStatus, &treq);
426             tvc->parentVnode = adp->fid.Fid.Vnode;
427             tvc->parentUnique = adp->fid.Fid.Unique;
428             ReleaseWriteLock(&tvc->lock);
429             *avcp = tvc;
430             code = 0;
431         } else
432             code = ENOENT;
433     } else {
434         /* otherwise cache entry already exists, someone else must
435          * have created it.  Comments used to say:  "don't need write
436          * lock to *clear* these flags" but we should do it anyway.
437          * Code used to clear stat bit and callback, but I don't see 
438          * the point -- we didn't have a create race, somebody else just
439          * snuck into NewVCache before we got here, probably a racing 
440          * lookup.
441          */
442         *avcp = tvc;
443         code = 0;
444     }
445     ReleaseWriteLock(&afs_xvcache);
446
447   done:
448     if (volp)
449         afs_PutVolume(volp, READ_LOCK);
450
451     if (code == 0) {
452         afs_AddMarinerName(aname, *avcp);
453         /* return the new status in vattr */
454         afs_CopyOutAttrs(*avcp, attrs);
455     }
456 #ifdef  AFS_OSF_ENV
457     if (!code && !strcmp(aname, "core"))
458         tvc->states |= CCore1;
459 #endif
460
461     afs_PutFakeStat(&fakestate);
462     code = afs_CheckCode(code, &treq, 20);
463
464   done2:
465     return code;
466 }
467
468
469 /*
470  * Check to see if we can track the change locally: requires that
471  * we have sufficiently recent info in data cache.  If so, we
472  * know the new DataVersion number, and place it correctly in both the
473  * data and stat cache entries.  This routine returns 1 if we should
474  * do the operation locally, and 0 otherwise.
475  *
476  * This routine must be called with the stat cache entry write-locked,
477  * and dcache entry write-locked.
478  */
479 int
480 afs_LocalHero(register struct vcache *avc, register struct dcache *adc,
481               register AFSFetchStatus * astat, register int aincr)
482 {
483     register afs_int32 ok;
484     afs_hyper_t avers;
485
486     AFS_STATCNT(afs_LocalHero);
487     hset64(avers, astat->dataVersionHigh, astat->DataVersion);
488     /* this *is* the version number, no matter what */
489     if (adc) {
490         ok = (hsame(avc->m.DataVersion, adc->f.versionNo) && avc->callback
491               && (avc->states & CStatd) && avc->cbExpires >= osi_Time());
492     } else {
493         ok = 0;
494     }
495 #if defined(AFS_SGI_ENV)
496     osi_Assert(avc->v.v_type == VDIR);
497 #endif
498     /* The bulk status code used the length as a sequence number.  */
499     /* Don't update the vcache entry unless the stats are current. */
500     if (avc->states & CStatd) {
501         hset(avc->m.DataVersion, avers);
502 #ifdef AFS_64BIT_CLIENT
503         FillInt64(avc->m.Length, astat->Length_hi, astat->Length);
504 #else /* AFS_64BIT_CLIENT */
505         avc->m.Length = astat->Length;
506 #endif /* AFS_64BIT_CLIENT */
507         avc->m.Date = astat->ClientModTime;
508     }
509     if (ok) {
510         /* we've been tracking things correctly */
511         adc->dflags |= DFEntryMod;
512         adc->f.versionNo = avers;
513         return 1;
514     } else {
515         if (adc) {
516             ZapDCE(adc);
517             DZap(adc);
518         }
519         if (avc->states & CStatd) {
520             osi_dnlc_purgedp(avc);
521         }
522         return 0;
523     }
524 }