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
14 * afs_StoreOnLastReference
19 #include <afsconfig.h>
20 #include "afs/param.h"
23 #include "afs/sysincludes.h" /* Standard vendor system headers */
24 #include "afsincludes.h" /* Afs-based standard headers */
25 #include "afs/afs_stats.h" /* statistics */
26 #include "afs/afs_cbqueue.h"
27 #include "afs/nfsclient.h"
28 #include "afs/afs_osidnlc.h"
31 extern unsigned char *afs_indexFlags;
33 /* Called by all write-on-close routines: regular afs_close,
34 * store via background daemon and store via the
35 * afs_FlushActiveVCaches routine (when CCORE is on).
36 * avc->lock must be write-locked.
39 afs_StoreOnLastReference(struct vcache *avc,
40 struct vrequest *treq)
44 AFS_STATCNT(afs_StoreOnLastReference);
45 /* if CCore flag is set, we clear it and do the extra decrement
46 * ourselves now. If we're called by the CCore clearer, the CCore
47 * flag will already be clear, so we don't have to worry about
48 * clearing it twice. */
49 if (avc->f.states & CCore) {
50 avc->f.states &= ~CCore;
51 #if defined(AFS_SGI_ENV)
52 osi_Assert(avc->opens > 0 && avc->execsOrWriters > 0);
54 /* WARNING: Our linux cm code treats the execsOrWriters counter differently
55 * depending on the flags the file was opened with. So, if you make any
56 * changes to the way the execsOrWriters flag is handled check with the
59 avc->execsOrWriters--;
60 AFS_RELE(AFSTOV(avc)); /* VN_HOLD at set CCore(afs_FakeClose) */
61 crfree((afs_ucred_t *)avc->linkData); /* "crheld" in afs_FakeClose */
65 if (!AFS_IS_DISCONNECTED) {
68 /* Now, send the file back. Used to require 0 writers left, but now do
69 * it on every close for write, since two closes in a row are harmless
70 * since first will clean all chunks, and second will be noop. Note that
71 * this will also save confusion when someone keeps a file open
72 * inadvertently, since with old system, writes to the server would never
75 code = afs_StoreAllSegments(avc, treq, AFS_LASTSTORE /*!sync-to-disk */ );
77 * We have to do these after the above store in done: in some systems
78 * like aix they'll need to flush all the vm dirty pages to the disk via
79 * the strategy routine. During that all procedure (done under no avc
80 * locks) opens, refcounts would be zero, since it didn't reach the
81 * afs_{rd,wr} routines which means the vcache is a perfect candidate
84 } else if (AFS_IS_DISCON_RW) {
85 afs_DisconAddDirty(avc, VDisconWriteClose, 0);
86 } /* if not disconnected */
88 #if defined(AFS_SGI_ENV)
89 osi_Assert(avc->opens > 0 && avc->execsOrWriters > 0);
93 avc->execsOrWriters--;
98 afs_UFSWriteUIO(struct vcache *avc, afs_dcache_id_t *inode, struct uio *tuiop)
100 struct osi_file *tfile;
103 tfile = (struct osi_file *)osi_UFSOpen(inode);
104 #if defined(AFS_AIX41_ENV)
106 code = VNOP_RDWR(tfile->vnode, UIO_WRITE, FWRITE, tuiop, NULL, NULL,
107 NULL, afs_osi_credp);
109 #elif defined(AFS_AIX32_ENV)
110 code = VNOP_RDWR(tfile->vnode, UIO_WRITE, FWRITE, tuiop, NULL, NULL);
111 #elif defined(AFS_AIX_ENV)
112 code = VNOP_RDWR(tfile->vnode, UIO_WRITE, FWRITE, (off_t) &offset,
113 tuiop, NULL, NULL, -1);
114 #elif defined(AFS_SUN5_ENV)
116 # ifdef AFS_SUN510_ENV
120 VOP_RWLOCK(tfile->vnode, 1, &ct);
121 code = VOP_WRITE(tfile->vnode, tuiop, 0, afs_osi_credp, &ct);
122 VOP_RWUNLOCK(tfile->vnode, 1, &ct);
125 VOP_RWLOCK(tfile->vnode, 1);
126 code = VOP_WRITE(tfile->vnode, tuiop, 0, afs_osi_credp);
127 VOP_RWUNLOCK(tfile->vnode, 1);
132 ("\n\n\n*** Cache partition is full - decrease cachesize!!! ***\n\n\n");
133 #elif defined(AFS_SGI_ENV)
135 avc->f.states |= CWritingUFS;
136 AFS_VOP_RWLOCK(tfile->vnode, VRWLOCK_WRITE);
137 AFS_VOP_WRITE(tfile->vnode, tuiop, IO_ISLOCKED, afs_osi_credp, code);
138 AFS_VOP_RWUNLOCK(tfile->vnode, VRWLOCK_WRITE);
139 avc->f.states &= ~CWritingUFS;
141 #elif defined(AFS_HPUX100_ENV)
144 code = VOP_RDWR(tfile->vnode, tuiop, UIO_WRITE, 0, afs_osi_credp);
147 #elif defined(AFS_LINUX20_ENV)
149 code = osi_rdwr(tfile, tuiop, UIO_WRITE);
151 #elif defined(AFS_DARWIN80_ENV)
153 code = VNOP_WRITE(tfile->vnode, tuiop, 0, afs_osi_ctxtp);
155 #elif defined(AFS_DARWIN_ENV)
157 VOP_LOCK(tfile->vnode, LK_EXCLUSIVE, current_proc());
158 code = VOP_WRITE(tfile->vnode, tuiop, 0, afs_osi_credp);
159 VOP_UNLOCK(tfile->vnode, 0, current_proc());
161 #elif defined(AFS_FBSD80_ENV)
163 VOP_LOCK(tfile->vnode, LK_EXCLUSIVE);
164 code = VOP_WRITE(tfile->vnode, tuiop, 0, afs_osi_credp);
165 VOP_UNLOCK(tfile->vnode, 0);
167 #elif defined(AFS_FBSD_ENV)
169 VOP_LOCK(tfile->vnode, LK_EXCLUSIVE, curthread);
170 code = VOP_WRITE(tfile->vnode, tuiop, 0, afs_osi_credp);
171 VOP_UNLOCK(tfile->vnode, 0, curthread);
173 #elif defined(AFS_NBSD_ENV)
175 VOP_LOCK(tfile->vnode, LK_EXCLUSIVE);
176 code = VOP_WRITE(tfile->vnode, tuiop, 0, afs_osi_credp);
177 #if defined(AFS_NBSD60_ENV)
178 VOP_UNLOCK(tfile->vnode);
180 VOP_UNLOCK(tfile->vnode, 0);
183 #elif defined(AFS_XBSD_ENV)
185 VOP_LOCK(tfile->vnode, LK_EXCLUSIVE, curproc);
186 code = VOP_WRITE(tfile->vnode, tuiop, 0, afs_osi_credp);
187 VOP_UNLOCK(tfile->vnode, 0, curproc);
191 tuio.uio_fpflags &= ~FSYNCIO; /* don't do sync io */
193 code = VOP_RDWR(tfile->vnode, tuiop, UIO_WRITE, 0, afs_osi_credp);
200 /* called on writes */
202 afs_write(struct vcache *avc, struct uio *auio, int aio,
203 afs_ucred_t *acred, int noLock)
205 afs_size_t totalLength;
206 afs_size_t transferLength;
208 afs_size_t offset, len;
218 #if defined(AFS_FBSD_ENV) || defined(AFS_DFBSD_ENV)
219 struct vnode *vp = AFSTOV(avc);
221 struct uio *tuiop = NULL;
223 struct vrequest treq;
225 AFS_STATCNT(afs_write);
228 return avc->vc_error;
230 if (AFS_IS_DISCONNECTED && !AFS_IS_DISCON_RW)
233 startDate = osi_Time();
234 if ((code = afs_InitReq(&treq, acred)))
236 /* otherwise we read */
237 totalLength = AFS_UIO_RESID(auio);
238 filePos = AFS_UIO_OFFSET(auio);
241 afs_Trace4(afs_iclSetp, CM_TRACE_WRITE, ICL_TYPE_POINTER, avc,
242 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(filePos), ICL_TYPE_OFFSET,
243 ICL_HANDLE_OFFSET(totalLength), ICL_TYPE_OFFSET,
244 ICL_HANDLE_OFFSET(avc->f.m.Length));
246 afs_MaybeWakeupTruncateDaemon();
247 ObtainWriteLock(&avc->lock, 556);
249 #if defined(AFS_SGI_ENV)
253 * afs_xwrite handles setting m.Length
254 * and handles APPEND mode.
255 * Since we are called via strategy, we need to trim the write to
256 * the actual size of the file
258 osi_Assert(filePos <= avc->f.m.Length);
259 diff = avc->f.m.Length - filePos;
260 AFS_UIO_SETRESID(auio, MIN(totalLength, diff));
261 totalLength = AFS_UIO_RESID(auio);
264 if (aio & IO_APPEND) {
265 /* append mode, start it at the right spot */
266 #if defined(AFS_SUN5_ENV)
267 auio->uio_loffset = 0;
269 filePos = avc->f.m.Length;
270 AFS_UIO_SETOFFSET(auio, avc->f.m.Length);
274 * Note that we use startDate rather than calling osi_Time() here.
275 * This is to avoid counting lock-waiting time in file date (for ranlib).
277 avc->f.m.Date = startDate;
279 #if defined(AFS_HPUX_ENV)
280 #if defined(AFS_HPUX101_ENV)
281 if ((totalLength + filePos) >> 9 >
282 p_rlimit(u.u_procp)[RLIMIT_FSIZE].rlim_cur) {
284 if ((totalLength + filePos) >> 9 > u.u_rlimit[RLIMIT_FSIZE].rlim_cur) {
287 ReleaseWriteLock(&avc->lock);
291 #if defined(AFS_VM_RDWR_ENV) && !defined(AFS_FAKEOPEN_ENV)
293 * If write is implemented via VM, afs_FakeOpen() is called from the
294 * high-level write op.
296 if (avc->execsOrWriters <= 0) {
297 afs_warn("WARNING: afs_ufswr vcp=%lx, exOrW=%d\n", (unsigned long)avc,
298 avc->execsOrWriters);
303 avc->f.states |= CDirty;
305 while (totalLength > 0) {
306 tdc = afs_ObtainDCacheForWriting(avc, filePos, totalLength, &treq,
312 len = totalLength; /* write this amount by default */
313 offset = filePos - AFS_CHUNKTOBASE(tdc->f.chunk);
314 max = AFS_CHUNKTOSIZE(tdc->f.chunk); /* max size of this chunk */
315 if (max <= len + offset) { /*if we'd go past the end of this chunk */
316 /* it won't all fit in this chunk, so write as much
324 tuiop = afsio_partialcopy(auio, trimlen);
325 AFS_UIO_SETOFFSET(tuiop, offset);
327 code = (*(afs_cacheType->vwriteUIO))(avc, &tdc->f.inode, tuiop);
333 ZapDCE(tdc); /* bad data */
334 cfile = afs_CFileOpen(&tdc->f.inode);
335 afs_CFileTruncate(cfile, 0);
336 afs_CFileClose(cfile);
337 afs_AdjustSize(tdc, 0); /* sets f.chunkSize to 0 */
339 afs_stats_cmperf.cacheCurrDirtyChunks--;
340 afs_indexFlags[tdc->index] &= ~IFDataMod; /* so it does disappear */
341 ReleaseWriteLock(&tdc->lock);
345 /* otherwise we've written some, fixup length, etc and continue with next seg */
346 len = len - AFS_UIO_RESID(tuiop); /* compute amount really transferred */
348 afsio_skip(auio, tlen); /* advance auio over data written */
349 /* compute new file size */
350 if (offset + len > tdc->f.chunkBytes) {
351 afs_int32 tlength = offset + len;
352 afs_AdjustSize(tdc, tlength);
353 if (tdc->validPos < filePos + len)
354 tdc->validPos = filePos + len;
357 transferLength += len;
359 #if defined(AFS_SGI_ENV)
360 /* afs_xwrite handles setting m.Length */
361 osi_Assert(filePos <= avc->f.m.Length);
363 if (filePos > avc->f.m.Length) {
364 if (AFS_IS_DISCON_RW)
365 afs_PopulateDCache(avc, filePos, &treq);
366 afs_Trace4(afs_iclSetp, CM_TRACE_SETLENGTH, ICL_TYPE_STRING,
367 __FILE__, ICL_TYPE_LONG, __LINE__, ICL_TYPE_OFFSET,
368 ICL_HANDLE_OFFSET(avc->f.m.Length), ICL_TYPE_OFFSET,
369 ICL_HANDLE_OFFSET(filePos));
370 avc->f.m.Length = filePos;
371 #if defined(AFS_FBSD_ENV) || defined(AFS_DFBSD_ENV)
372 vnode_pager_setsize(vp, filePos);
376 ReleaseWriteLock(&tdc->lock);
378 #if !defined(AFS_VM_RDWR_ENV)
380 * If write is implemented via VM, afs_DoPartialWrite() is called from
381 * the high-level write op.
384 code = afs_DoPartialWrite(avc, &treq);
392 #if !defined(AFS_VM_RDWR_ENV) || defined(AFS_FAKEOPEN_ENV)
393 afs_FakeClose(avc, acred);
395 error = afs_CheckCode(error, &treq, 7);
396 /* This set is here so we get the CheckCode. */
397 if (error && !avc->vc_error)
398 avc->vc_error = error;
400 ReleaseWriteLock(&avc->lock);
404 #ifndef AFS_VM_RDWR_ENV
406 * If write is implemented via VM, afs_fsync() is called from the high-level
409 #if defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
410 if (noLock && (aio & IO_SYNC)) {
413 /* On hpux on synchronous writes syncio will be set to IO_SYNC. If
414 * we're doing them because the file was opened with O_SYNCIO specified,
415 * we have to look in the u area. No single mechanism here!!
417 if (noLock && ((aio & IO_SYNC) | (auio->uio_fpflags & FSYNCIO))) {
419 if (noLock && (aio & FSYNC)) {
422 if (!AFS_NFSXLATORREQ(acred))
423 afs_fsync(avc, acred);
429 /* do partial write if we're low on unmodified chunks */
431 afs_DoPartialWrite(struct vcache *avc, struct vrequest *areq)
435 if (afs_stats_cmperf.cacheCurrDirtyChunks <=
436 afs_stats_cmperf.cacheMaxDirtyChunks
437 || AFS_IS_DISCONNECTED)
438 return 0; /* nothing to do */
439 /* otherwise, call afs_StoreDCache (later try to do this async, if possible) */
440 afs_Trace2(afs_iclSetp, CM_TRACE_PARTIALWRITE, ICL_TYPE_POINTER, avc,
441 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(avc->f.m.Length));
443 #if defined(AFS_SUN5_ENV)
444 code = afs_StoreAllSegments(avc, areq, AFS_ASYNC | AFS_VMSYNC_INVAL);
446 code = afs_StoreAllSegments(avc, areq, AFS_ASYNC);
451 /* handle any closing cleanup stuff */
453 #if defined(AFS_SGI65_ENV)
454 afs_close(OSI_VC_DECL(avc), afs_int32 aflags, lastclose_t lastclose,
456 #elif defined(AFS_SGI64_ENV)
457 afs_close(OSI_VC_DECL(avc), afs_int32 aflags, lastclose_t lastclose,
458 off_t offset, afs_ucred_t *acred, struct flid *flp)
459 #elif defined(AFS_SGI_ENV)
460 afs_close(OSI_VC_DECL(avc), afs_int32 aflags, lastclose_t lastclose
461 off_t offset, afs_ucred_t *acred)
462 #elif defined(AFS_SUN5_ENV)
463 afs_close(OSI_VC_DECL(avc), afs_int32 aflags, int count, offset_t offset,
466 afs_close(OSI_VC_DECL(avc), afs_int32 aflags, afs_ucred_t *acred)
471 struct vrequest treq;
475 struct afs_fakestat_state fakestat;
478 AFS_STATCNT(afs_close);
479 afs_Trace2(afs_iclSetp, CM_TRACE_CLOSE, ICL_TYPE_POINTER, avc,
480 ICL_TYPE_INT32, aflags);
481 code = afs_InitReq(&treq, acred);
484 afs_InitFakeStat(&fakestat);
485 code = afs_EvalFakeStat(&avc, &fakestat, &treq);
487 afs_PutFakeStat(&fakestat);
492 if (avc->flockCount) {
493 HandleFlock(avc, LOCK_UN, &treq, 0, 1 /*onlymine */ );
496 #if defined(AFS_SGI_ENV)
498 afs_PutFakeStat(&fakestat);
502 /* unlock any locks for pid - could be wrong for child .. */
503 AFS_RWLOCK((vnode_t *) avc, VRWLOCK_WRITE);
504 # ifdef AFS_SGI65_ENV
505 get_current_flid(&flid);
506 cleanlocks((vnode_t *) avc, flid.fl_pid, flid.fl_sysid);
507 HandleFlock(avc, LOCK_UN, &treq, flid.fl_pid, 1 /*onlymine */ );
509 # ifdef AFS_SGI64_ENV
510 cleanlocks((vnode_t *) avc, flp);
511 # else /* AFS_SGI64_ENV */
512 cleanlocks((vnode_t *) avc, u.u_procp->p_epid, u.u_procp->p_sysid);
513 # endif /* AFS_SGI64_ENV */
514 HandleFlock(avc, LOCK_UN, &treq, OSI_GET_CURRENT_PID(), 1 /*onlymine */ );
515 # endif /* AFS_SGI65_ENV */
516 /* afs_chkpgoob will drop and re-acquire the global lock. */
517 afs_chkpgoob(&avc->v, btoc(avc->f.m.Length));
518 #elif defined(AFS_SUN5_ENV)
520 /* The vfs layer may call this repeatedly with higher "count"; only
521 * on the last close (i.e. count = 1) we should actually proceed
523 afs_PutFakeStat(&fakestat);
528 if (avc->flockCount) { /* Release Lock */
529 HandleFlock(avc, LOCK_UN, &treq, 0, 1 /*onlymine */ );
532 if (aflags & (FWRITE | FTRUNC)) {
533 if (afs_BBusy() || (AFS_NFSXLATORREQ(acred)) || AFS_IS_DISCONNECTED) {
534 /* do it yourself if daemons are all busy */
535 ObtainWriteLock(&avc->lock, 124);
536 code = afs_StoreOnLastReference(avc, &treq);
537 ReleaseWriteLock(&avc->lock);
538 #if defined(AFS_SGI_ENV)
539 AFS_RWUNLOCK((vnode_t *) avc, VRWLOCK_WRITE);
542 #if defined(AFS_SGI_ENV)
543 AFS_RWUNLOCK((vnode_t *) avc, VRWLOCK_WRITE);
545 /* at least one daemon is idle, so ask it to do the store.
546 * Also, note that we don't lock it any more... */
547 tb = afs_BQueue(BOP_STORE, avc, 0, 1, acred,
548 (afs_size_t) afs_cr_uid(acred), (afs_size_t) 0,
549 (void *)0, (void *)0, (void *)0);
550 /* sleep waiting for the store to start, then retrieve error code */
551 while ((tb->flags & BUVALID) == 0) {
559 /* VNOVNODE is "acceptable" error code from close, since
560 * may happen when deleting a file on another machine while
561 * it is open here. We do the same for ENOENT since in afs_CheckCode we map VNOVNODE -> ENOENT */
562 if (code == VNOVNODE || code == ENOENT)
565 /* Ensure last closer gets the error. If another thread caused
566 * DoPartialWrite and this thread does not actually store the data,
567 * it may not see the quota error.
569 ObtainWriteLock(&avc->lock, 406);
572 osi_ReleaseVM(avc, acred);
574 /* printf("avc->vc_error=%d\n", avc->vc_error); */
575 code = avc->vc_error;
578 ReleaseWriteLock(&avc->lock);
580 /* some codes merit specific complaint */
582 afs_warnuser("afs: failed to store file (network problems)\n");
585 else if (code == ENOSPC) {
587 ("afs: failed to store file (over quota or partition full)\n");
590 else if (code == ENOSPC) {
591 afs_warnuser("afs: failed to store file (partition full)\n");
592 } else if (code == EDQUOT) {
593 afs_warnuser("afs: failed to store file (over quota)\n");
597 afs_warnuser("afs: failed to store file (%d)\n", code);
599 /* finally, we flush any text pages lying around here */
603 #if defined(AFS_SGI_ENV)
604 AFS_RWUNLOCK((vnode_t *) avc, VRWLOCK_WRITE);
605 osi_Assert(avc->opens > 0);
607 /* file open for read */
608 ObtainWriteLock(&avc->lock, 411);
611 osi_ReleaseVM(avc, acred);
613 code = avc->vc_error;
616 #if defined(AFS_FBSD80_ENV)
619 afs_int32 opens, is_free, is_gone, is_doomed, iflag;
620 struct vnode *vp = AFSTOV(avc);
622 is_doomed = vp->v_iflag & VI_DOOMED;
623 is_free = vp->v_iflag & VI_FREE;
624 is_gone = vp->v_iflag & VI_DOINGINACT;
628 afs_warn("afs_close avc %p vp %p opens %d free %d doinginact %d doomed %d iflag %d\n",
629 avc, vp, opens, is_free, is_gone, is_doomed, iflag);
633 ReleaseWriteLock(&avc->lock);
636 afs_PutFakeStat(&fakestat);
637 code = afs_CheckCode(code, &treq, 5);
643 #if defined(AFS_SGI_ENV) || defined(AFS_SUN5_ENV)
644 afs_fsync(OSI_VC_DECL(avc), int flag, afs_ucred_t *acred
645 # ifdef AFS_SGI65_ENV
646 , off_t start, off_t stop
647 # endif /* AFS_SGI65_ENV */
649 #else /* !SUN5 && !SGI */
650 afs_fsync(OSI_VC_DECL(avc), afs_ucred_t *acred)
654 struct vrequest treq;
658 return avc->vc_error;
660 #if defined(AFS_SUN5_ENV)
661 /* back out if called from NFS server */
662 if (curthread->t_flag & T_DONTPEND)
666 AFS_STATCNT(afs_fsync);
667 afs_Trace1(afs_iclSetp, CM_TRACE_FSYNC, ICL_TYPE_POINTER, avc);
668 if ((code = afs_InitReq(&treq, acred)))
671 #if defined(AFS_SGI_ENV)
672 AFS_RWLOCK((vnode_t *) avc, VRWLOCK_WRITE);
673 if (flag & FSYNC_INVAL)
674 osi_VM_FSyncInval(avc);
675 #endif /* AFS_SGI_ENV */
677 ObtainSharedLock(&avc->lock, 18);
679 if (avc->execsOrWriters > 0) {
680 if (!AFS_IS_DISCONNECTED && !AFS_IS_DISCON_RW) {
681 /* Your average flush. */
683 /* put the file back */
684 UpgradeSToWLock(&avc->lock, 41);
685 code = afs_StoreAllSegments(avc, &treq, AFS_SYNC);
686 ConvertWToSLock(&avc->lock);
688 UpgradeSToWLock(&avc->lock, 711);
689 afs_DisconAddDirty(avc, VDisconWriteFlush, 1);
690 ConvertWToSLock(&avc->lock);
691 } /* if not disconnected */
692 } /* if (avc->execsOrWriters > 0) */
694 #if defined(AFS_SGI_ENV)
695 AFS_RWUNLOCK((vnode_t *) avc, VRWLOCK_WRITE);
696 if (code == VNOVNODE) {
697 /* syncing an unlinked file! - non-informative to pass an errno
698 * 102 (== VNOVNODE) to user
704 code = afs_CheckCode(code, &treq, 33);
705 ReleaseSharedLock(&avc->lock);