disconnected-dirty-list-keeps-reference-to-vcache-20081130
[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 done3;
88     }
89
90     if (!afs_ENameOK(aname)) {
91         code = EINVAL;
92         goto done3;
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 done3;
104     default:
105         ;
106     }
107     AFS_DISCON_LOCK();
108
109     code = afs_EvalFakeStat(&adp, &fakestate, &treq);
110     if (code)
111         goto done;
112   tagain:
113     code = afs_VerifyVCache(adp, &treq);
114     if (code)
115         goto done;
116
117     /** If the volume is read-only, return error without making an RPC to the
118       * fileserver
119       */
120     if (adp->states & CRO) {
121         code = EROFS;
122         goto done;
123     }
124
125     if (AFS_IS_DISCONNECTED && !AFS_IS_DISCON_RW) {
126         code = ENETDOWN;
127         goto done;
128     }
129
130     tdc = afs_GetDCache(adp, (afs_size_t) 0, &treq, &offset, &len, 1);
131     ObtainWriteLock(&adp->lock, 135);
132     if (tdc)
133         ObtainSharedLock(&tdc->lock, 630);
134
135     /*
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.
138      */
139     if (!(adp->states & CStatd)
140         || (tdc && !hsame(adp->m.DataVersion, tdc->f.versionNo))) {
141         ReleaseWriteLock(&adp->lock);
142         if (tdc) {
143             ReleaseSharedLock(&tdc->lock);
144             afs_PutDCache(tdc);
145         }
146         goto tagain;
147     }
148     if (tdc) {
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 */
152         if (code == 0) {
153             ReleaseSharedLock(&tdc->lock);
154             afs_PutDCache(tdc);
155             ReleaseWriteLock(&adp->lock);
156 #ifdef AFS_SGI64_ENV
157             if (flags & VEXCL) {
158 #else
159             if (aexcl != NONEXCL) {
160 #endif
161                 code = EEXIST;  /* file exists in excl mode open */
162                 goto done;
163             }
164             /* found the file, so use it */
165             newFid.Cell = adp->fid.Cell;
166             newFid.Fid.Volume = adp->fid.Fid.Volume;
167             tvc = NULL;
168             if (newFid.Fid.Unique == 0) {
169                 tvc = afs_LookupVCache(&newFid, &treq, NULL, adp, aname);
170             }
171             if (!tvc)           /* lookup failed or wasn't called */
172                 tvc = afs_GetVCache(&newFid, &treq, NULL, NULL);
173
174             if (tvc) {
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 */
179                 /*
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.
183                  */
184                 if ((amode & VREAD)
185                     && !afs_AccessOK(tvc, PRSFS_READ, &treq, CHECK_MODE_BITS)) {
186                     afs_PutVCache(tvc);
187                     code = EACCES;
188                     goto done;
189                 }
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))
194 #else
195                 if ((amode & VWRITE) || len != 0xffffffff)
196 #endif
197                 {
198                     /* needed for write access check */
199                     tvc->parentVnode = adp->fid.Fid.Vnode;
200                     tvc->parentUnique = adp->fid.Fid.Unique;
201                     /* need write mode for these guys */
202                     if (!afs_AccessOK
203                         (tvc, PRSFS_WRITE, &treq, CHECK_MODE_BITS)) {
204                         afs_PutVCache(tvc);
205                         code = EACCES;
206                         goto done;
207                     }
208                 }
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)
213 #else
214                 if (len != 0xffffffff)
215 #endif
216                 {
217                     if (vType(tvc) != VREG) {
218                         afs_PutVCache(tvc);
219                         code = EISDIR;
220                         goto done;
221                     }
222                     /* do a truncate */
223 #if defined(AFS_DARWIN80_ENV)
224                     VATTR_INIT(attrs);
225                     VATTR_SET_SUPPORTED(attrs, va_data_size);
226                     VATTR_SET_ACTIVE(attrs, va_data_size);
227 #elif defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV)
228                     attrs->va_mask = AT_SIZE;
229 #else
230                     VATTR_NULL(attrs);
231 #endif
232                     attrs->va_size = len;
233                     ObtainWriteLock(&tvc->lock, 136);
234                     tvc->states |= CCreating;
235                     ReleaseWriteLock(&tvc->lock);
236 #if defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV)
237 #if defined(AFS_SGI64_ENV)
238                     code =
239                         afs_setattr(VNODE_TO_FIRST_BHV((vnode_t *) tvc),
240                                     attrs, 0, acred);
241 #else
242                     code = afs_setattr(tvc, attrs, 0, acred);
243 #endif /* AFS_SGI64_ENV */
244 #else /* SUN5 || SGI */
245                     code = afs_setattr(tvc, attrs, acred);
246 #endif /* SUN5 || SGI */
247                     ObtainWriteLock(&tvc->lock, 137);
248                     tvc->states &= ~CCreating;
249                     ReleaseWriteLock(&tvc->lock);
250                     if (code) {
251                         afs_PutVCache(tvc);
252                         goto done;
253                     }
254                 }
255                 *avcp = tvc;
256             } else
257                 code = ENOENT;  /* shouldn't get here */
258             /* make sure vrefCount bumped only if code == 0 */
259             goto done;
260         }
261     }
262     
263     /* if we create the file, we don't do any access checks, since
264      * that's how O_CREAT is supposed to work */
265     if (adp->states & CForeign) {
266         origCBs = afs_allCBs;
267         origZaps = afs_allZaps;
268     } else {
269         origCBs = afs_evenCBs;  /* if changes, we don't really have a callback */
270         origZaps = afs_evenZaps;        /* number of even numbered vnodes discarded */
271     }
272     InStatus.Mask = AFS_SETMODTIME | AFS_SETMODE | AFS_SETGROUP;
273     InStatus.ClientModTime = osi_Time();
274     InStatus.Group = (afs_int32) acred->cr_gid;
275     if (AFS_NFSXLATORREQ(acred)) {
276         /*
277          * XXX The following is mainly used to fix a bug in the HP-UX
278          * nfs client where they create files with mode of 0 without
279          * doing any setattr later on to fix it.  * XXX
280          */
281 #if     defined(AFS_AIX_ENV)
282         if (attrs->va_mode != -1) {
283 #else
284 #if     defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV)
285         if (attrs->va_mask & AT_MODE) {
286 #else
287         if (attrs->va_mode != ((unsigned short)-1)) {
288 #endif
289 #endif
290             if (!attrs->va_mode)
291                 attrs->va_mode = 0x1b6; /* XXX default mode: rw-rw-rw XXX */
292         }
293     }
294
295     if (!AFS_IS_DISCONNECTED) {
296         /* If not disconnected, connect to the server.*/
297
298         InStatus.UnixModeBits = attrs->va_mode & 0xffff;        /* only care about protection bits */
299         do {
300             tc = afs_Conn(&adp->fid, &treq, SHARED_LOCK);
301             if (tc) {
302                 hostp = tc->srvr->server;       /* remember for callback processing */
303                 now = osi_Time();
304                 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_CREATEFILE);
305                 RX_AFS_GUNLOCK();
306                 code =
307                     RXAFS_CreateFile(tc->id, (struct AFSFid *)&adp->fid.Fid,
308                                  aname, &InStatus, (struct AFSFid *)
309                                  &newFid.Fid, &OutFidStatus, &OutDirStatus,
310                                  &CallBack, &tsync);
311                 RX_AFS_GLOCK();
312                 XSTATS_END_TIME;
313                 CallBack.ExpirationTime += now;
314             } else
315                 code = -1;
316         } while (afs_Analyze
317                  (tc, code, &adp->fid, &treq, AFS_STATS_FS_RPCIDX_CREATEFILE,
318                   SHARED_LOCK, NULL));
319
320         if ((code == EEXIST || code == UAEEXIST) &&
321 #ifdef AFS_SGI64_ENV
322         !(flags & VEXCL)
323 #else /* AFS_SGI64_ENV */
324         aexcl == NONEXCL
325 #endif
326         ) {
327             /* if we get an EEXIST in nonexcl mode, just do a lookup */
328             if (tdc) {
329                 ReleaseSharedLock(&tdc->lock);
330                 afs_PutDCache(tdc);
331             }
332             ReleaseWriteLock(&adp->lock);
333 #if !(defined(AFS_OSF_ENV) || defined(AFS_DARWIN_ENV))
334 #if defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV)
335 #if defined(AFS_SGI64_ENV)
336             code = afs_lookup(VNODE_TO_FIRST_BHV((vnode_t *) adp), aname, avcp,
337                                   NULL, 0, NULL, acred);
338 #else
339             code = afs_lookup(adp, aname, avcp, NULL, 0, NULL, acred);
340 #endif /* AFS_SGI64_ENV */
341 #else /* SUN5 || SGI */
342 #if defined(UKERNEL)
343             code = afs_lookup(adp, aname, avcp, acred, 0);
344 #else /* UKERNEL */
345             code = afs_lookup(adp, aname, avcp, acred);
346 #endif /* UKERNEL */
347 #endif /* SUN5 || SGI */
348 #endif /* !(AFS_OSF_ENV || AFS_DARWIN_ENV) */
349         goto done;
350         }
351
352         if (code) {
353             if (code < 0) {
354                 ObtainWriteLock(&afs_xcbhash, 488);
355                 afs_DequeueCallback(adp);
356                 adp->states &= ~CStatd;
357                 ReleaseWriteLock(&afs_xcbhash);
358                 osi_dnlc_purgedp(adp);
359             }
360             ReleaseWriteLock(&adp->lock);
361             if (tdc) {
362                 ReleaseSharedLock(&tdc->lock);
363                 afs_PutDCache(tdc);
364             }
365         goto done;
366         }
367
368     } else {
369 #if defined(AFS_DISCON_ENV)
370         /* Generate a fake FID for disconnected mode. */
371         newFid.Cell = adp->fid.Cell;
372         newFid.Fid.Volume = adp->fid.Fid.Volume;
373         afs_GenFakeFid(&newFid, VREG);
374 #endif
375     }                           /* if (!AFS_IS_DISCON_RW) */
376
377     /* otherwise, we should see if we can make the change to the dir locally */
378     if (tdc)
379         UpgradeSToWLock(&tdc->lock, 631);
380     if (AFS_IS_DISCON_RW || afs_LocalHero(adp, tdc, &OutDirStatus, 1)) {
381         /* we can do it locally */
382         ObtainWriteLock(&afs_xdcache, 291);
383         code = afs_dir_Create(tdc, aname, &newFid.Fid);
384         ReleaseWriteLock(&afs_xdcache);
385         if (code) {
386             ZapDCE(tdc);
387             DZap(tdc);
388         }
389     }
390     if (tdc) {
391         ReleaseWriteLock(&tdc->lock);
392         afs_PutDCache(tdc);
393     }
394     newFid.Cell = adp->fid.Cell;
395     newFid.Fid.Volume = adp->fid.Fid.Volume;
396     ReleaseWriteLock(&adp->lock);
397     volp = afs_FindVolume(&newFid, READ_LOCK);
398
399     /* New tricky optimistic callback handling algorithm for file creation works
400      * as follows.  We create the file essentially with no locks set at all.  File
401      * server may thus handle operations from others cache managers as well as from
402      * this very own cache manager that reference the file in question before
403      * we managed to create the cache entry.  However, if anyone else changes
404      * any of the status information for a file, we'll see afs_evenCBs increase
405      * (files always have even fids).  If someone on this workstation manages
406      * to do something to the file, they'll end up having to create a cache
407      * entry for the new file.  Either we'll find it once we've got the afs_xvcache
408      * lock set, or it was also *deleted* the vnode before we got there, in which case
409      * we will find evenZaps has changed, too.  Thus, we only assume we have the right
410      * status information if no callbacks or vnode removals have occurred to even
411      * numbered files from the time the call started until the time that we got the xvcache
412      * lock set.  Of course, this also assumes that any call that modifies a file first
413      * gets a write lock on the file's vnode, but if that weren't true, the whole cache manager
414      * would fail, since no call would be able to update the local vnode status after modifying
415      * a file on a file server. */
416     ObtainWriteLock(&afs_xvcache, 138);
417     if (adp->states & CForeign)
418         finalZaps = afs_allZaps;        /* do this before calling newvcache */
419     else
420         finalZaps = afs_evenZaps;       /* do this before calling newvcache */
421     /* don't need to call RemoveVCB, since only path leaving a callback is the
422      * one where we pass through afs_NewVCache.  Can't have queued a VCB unless
423      * we created and freed an entry between file creation time and here, and the
424      * freeing of the vnode will change evenZaps.  Don't need to update the VLRU
425      * queue, since the find will only succeed in the event of a create race, and 
426      * then the vcache will be at the front of the VLRU queue anyway...  */
427     if (!(tvc = afs_FindVCache(&newFid, 0, DO_STATS))) {
428         tvc = afs_NewVCache(&newFid, hostp);
429         if (tvc) {
430             int finalCBs;
431             ObtainWriteLock(&tvc->lock, 139);
432
433             ObtainWriteLock(&afs_xcbhash, 489);
434             finalCBs = afs_evenCBs;
435             /* add the callback in */
436             if (adp->states & CForeign) {
437                 tvc->states |= CForeign;
438                 finalCBs = afs_allCBs;
439             }
440             if (origCBs == finalCBs && origZaps == finalZaps) {
441                 tvc->states |= CStatd;  /* we've fake entire thing, so don't stat */
442                 tvc->states &= ~CBulkFetching;
443                 tvc->cbExpires = CallBack.ExpirationTime;
444                 afs_QueueCallback(tvc, CBHash(CallBack.ExpirationTime), volp);
445             } else {
446                 afs_DequeueCallback(tvc);
447                 tvc->states &= ~(CStatd | CUnique);
448                 tvc->callback = 0;
449                 if (tvc->fid.Fid.Vnode & 1 || (vType(tvc) == VDIR))
450                     osi_dnlc_purgedp(tvc);
451             }
452             ReleaseWriteLock(&afs_xcbhash);
453             if (!AFS_IS_DISCON_RW)
454                 afs_ProcessFS(tvc, &OutFidStatus, &treq);
455             tvc->parentVnode = adp->fid.Fid.Vnode;
456             tvc->parentUnique = adp->fid.Fid.Unique;
457             ReleaseWriteLock(&tvc->lock);
458             *avcp = tvc;
459             code = 0;
460
461             if (AFS_IS_DISCON_RW) {
462 #if defined(AFS_DISCON_ENV)
463                 /* After tvc has been created, we can do various ops on it. */
464                 /* Add to dirty list. */
465                 if (!tvc->ddirty_flags ||
466                         (tvc->ddirty_flags == VDisconShadowed)) {
467                     /* Put it in the list only if it's fresh. */
468                     ObtainWriteLock(&afs_DDirtyVCListLock, 729);
469                     AFS_DISCON_ADD_DIRTY(tvc, 0);
470                     ReleaseWriteLock(&afs_DDirtyVCListLock);
471                 }
472
473                 /* Set create flag. */
474                 ObtainWriteLock(&tvc->lock, 730);
475                 afs_GenDisconStatus(adp, tvc, &newFid, attrs, &treq, VREG);
476                 tvc->ddirty_flags |= VDisconCreate;
477                 ReleaseWriteLock(&tvc->lock);
478
479 #endif                          /* #ifdef AFS_DISCON_ENV */
480             }                   /* if (AFS_IS_DISCON_RW) */
481
482
483         } else
484             code = ENOENT;
485     } else {
486         /* otherwise cache entry already exists, someone else must
487          * have created it.  Comments used to say:  "don't need write
488          * lock to *clear* these flags" but we should do it anyway.
489          * Code used to clear stat bit and callback, but I don't see 
490          * the point -- we didn't have a create race, somebody else just
491          * snuck into NewVCache before we got here, probably a racing 
492          * lookup.
493          */
494         *avcp = tvc;
495         code = 0;
496     }
497     ReleaseWriteLock(&afs_xvcache);
498
499   done:
500     AFS_DISCON_UNLOCK();
501
502   done3:
503     if (volp)
504         afs_PutVolume(volp, READ_LOCK);
505
506     if (code == 0) {
507         afs_AddMarinerName(aname, *avcp);
508         /* return the new status in vattr */
509         afs_CopyOutAttrs(*avcp, attrs);
510     }
511 #ifdef  AFS_OSF_ENV
512     if (!code && !strcmp(aname, "core"))
513         tvc->states |= CCore1;
514 #endif
515
516     afs_PutFakeStat(&fakestate);
517     code = afs_CheckCode(code, &treq, 20);
518
519   done2:
520     return code;
521 }
522
523
524 /*
525  * Check to see if we can track the change locally: requires that
526  * we have sufficiently recent info in data cache.  If so, we
527  * know the new DataVersion number, and place it correctly in both the
528  * data and stat cache entries.  This routine returns 1 if we should
529  * do the operation locally, and 0 otherwise.
530  *
531  * This routine must be called with the stat cache entry write-locked,
532  * and dcache entry write-locked.
533  */
534 int
535 afs_LocalHero(register struct vcache *avc, register struct dcache *adc,
536               register AFSFetchStatus * astat, register int aincr)
537 {
538     register afs_int32 ok;
539     afs_hyper_t avers;
540
541     AFS_STATCNT(afs_LocalHero);
542     hset64(avers, astat->dataVersionHigh, astat->DataVersion);
543     /* this *is* the version number, no matter what */
544     if (adc) {
545         ok = (hsame(avc->m.DataVersion, adc->f.versionNo) && avc->callback
546               && (avc->states & CStatd) && avc->cbExpires >= osi_Time());
547     } else {
548         ok = 0;
549     }
550 #if defined(AFS_SGI_ENV)
551     osi_Assert(avc->v.v_type == VDIR);
552 #endif
553     /* The bulk status code used the length as a sequence number.  */
554     /* Don't update the vcache entry unless the stats are current. */
555     if (avc->states & CStatd) {
556         hset(avc->m.DataVersion, avers);
557 #ifdef AFS_64BIT_CLIENT
558         FillInt64(avc->m.Length, astat->Length_hi, astat->Length);
559 #else /* AFS_64BIT_CLIENT */
560         avc->m.Length = astat->Length;
561 #endif /* AFS_64BIT_CLIENT */
562         avc->m.Date = astat->ClientModTime;
563     }
564     if (ok) {
565         /* we've been tracking things correctly */
566         adc->dflags |= DFEntryMod;
567         adc->f.versionNo = avers;
568         return 1;
569     } else {
570         if (adc) {
571             ZapDCE(adc);
572             DZap(adc);
573         }
574         if (avc->states & CStatd) {
575             osi_dnlc_purgedp(avc);
576         }
577         return 0;
578     }
579 }