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_MemWrite(struct vcache *avc, struct uio *auio, int aio,
99 afs_ucred_t *acred, int noLock)
101 afs_size_t totalLength;
102 afs_size_t transferLength;
104 afs_size_t offset, len;
105 afs_int32 tlen, trimlen;
113 #ifdef AFS_DARWIN80_ENV
117 struct uio *tuiop = &tuio;
118 struct iovec *tvec; /* again, should have define */
121 struct vrequest treq;
123 AFS_STATCNT(afs_MemWrite);
125 return avc->vc_error;
127 startDate = osi_Time();
128 if ((code = afs_InitReq(&treq, acred)))
130 /* otherwise we read */
131 totalLength = AFS_UIO_RESID(auio);
132 filePos = AFS_UIO_OFFSET(auio);
135 afs_Trace4(afs_iclSetp, CM_TRACE_WRITE, ICL_TYPE_POINTER, avc,
136 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(filePos), ICL_TYPE_OFFSET,
137 ICL_HANDLE_OFFSET(totalLength), ICL_TYPE_OFFSET,
138 ICL_HANDLE_OFFSET(avc->f.m.Length));
140 afs_MaybeWakeupTruncateDaemon();
141 ObtainWriteLock(&avc->lock, 126);
143 #if defined(AFS_SGI_ENV)
147 * afs_xwrite handles setting m.Length
148 * and handles APPEND mode.
149 * Since we are called via strategy, we need to trim the write to
150 * the actual size of the file
152 osi_Assert(filePos <= avc->f.m.Length);
153 diff = avc->f.m.Length - filePos;
154 AFS_UIO_SETRESID(auio, MIN(totalLength, diff));
155 totalLength = AFS_UIO_RESID(auio);
158 if (aio & IO_APPEND) {
159 /* append mode, start it at the right spot */
160 #if defined(AFS_SUN56_ENV)
161 auio->uio_loffset = 0;
163 filePos = avc->f.m.Length;
164 AFS_UIO_SETOFFSET(auio, filePos);
168 * Note that we use startDate rather than calling osi_Time() here.
169 * This is to avoid counting lock-waiting time in file date (for ranlib).
171 avc->f.m.Date = startDate;
173 #if defined(AFS_HPUX_ENV)
174 #if defined(AFS_HPUX101_ENV)
175 if ((totalLength + filePos) >> 9 >
176 (p_rlimit(u.u_procp))[RLIMIT_FSIZE].rlim_cur) {
178 if ((totalLength + filePos) >> 9 > u.u_rlimit[RLIMIT_FSIZE].rlim_cur) {
181 ReleaseWriteLock(&avc->lock);
185 #ifdef AFS_VM_RDWR_ENV
187 * If write is implemented via VM, afs_FakeOpen() is called from the
188 * high-level write op.
190 if (avc->execsOrWriters <= 0) {
191 afs_warn("WARNING: afs_ufswr vp=%lx, exOrW=%d\n", (unsigned long)avc,
192 avc->execsOrWriters);
197 avc->f.states |= CDirty;
198 #ifndef AFS_DARWIN80_ENV
199 tvec = (struct iovec *)osi_AllocSmallSpace(sizeof(struct iovec));
201 while (totalLength > 0) {
202 tdc = afs_ObtainDCacheForWriting(avc, filePos, totalLength, &treq,
209 len = totalLength; /* write this amount by default */
210 offset = filePos - AFS_CHUNKTOBASE(tdc->f.chunk);
211 max = AFS_CHUNKTOSIZE(tdc->f.chunk); /* max size of this chunk */
212 if (max <= len + offset) { /*if we'd go past the end of this chunk */
213 /* it won't all fit in this chunk, so write as much
218 #ifdef AFS_DARWIN80_ENV
222 tuiop = afsio_darwin_partialcopy(auio, trimlen);
224 /* mung uio structure to be right for this transfer */
225 afsio_copy(auio, &tuio, tvec);
227 afsio_trim(&tuio, trimlen);
229 AFS_UIO_SETOFFSET(tuiop, offset);
231 code = afs_MemWriteUIO(&tdc->f.inode, tuiop);
233 void *mep; /* XXX in prototype world is struct memCacheEntry * */
235 ZapDCE(tdc); /* bad data */
236 mep = afs_MemCacheOpen(&tdc->f.inode);
237 afs_MemCacheTruncate(mep, 0);
238 afs_MemCacheClose(mep);
239 afs_stats_cmperf.cacheCurrDirtyChunks--;
240 afs_indexFlags[tdc->index] &= ~IFDataMod; /* so it does disappear */
241 ReleaseWriteLock(&tdc->lock);
245 /* otherwise we've written some, fixup length, etc and continue with next seg */
246 len = len - AFS_UIO_RESID(tuiop); /* compute amount really transferred */
248 afsio_skip(auio, tlen); /* advance auio over data written */
249 /* compute new file size */
250 if (offset + len > tdc->f.chunkBytes) {
251 afs_int32 tlength = offset + len;
252 afs_AdjustSize(tdc, tlength);
253 if (tdc->validPos < filePos + len)
254 tdc->validPos = filePos + len;
257 transferLength += len;
259 #if defined(AFS_SGI_ENV)
260 /* afs_xwrite handles setting m.Length */
261 osi_Assert(filePos <= avc->f.m.Length);
263 if (filePos > avc->f.m.Length) {
264 if (AFS_IS_DISCON_RW)
265 afs_PopulateDCache(avc, filePos, &treq);
266 afs_Trace4(afs_iclSetp, CM_TRACE_SETLENGTH, ICL_TYPE_STRING,
267 __FILE__, ICL_TYPE_LONG, __LINE__, ICL_TYPE_OFFSET,
268 ICL_HANDLE_OFFSET(avc->f.m.Length), ICL_TYPE_OFFSET,
269 ICL_HANDLE_OFFSET(filePos));
270 avc->f.m.Length = filePos;
273 ReleaseWriteLock(&tdc->lock);
275 #if !defined(AFS_VM_RDWR_ENV)
277 * If write is implemented via VM, afs_DoPartialWrite() is called from
278 * the high-level write op.
281 code = afs_DoPartialWrite(avc, &treq);
289 #ifndef AFS_VM_RDWR_ENV
290 afs_FakeClose(avc, acred);
292 if (error && !avc->vc_error)
293 avc->vc_error = error;
295 ReleaseWriteLock(&avc->lock);
296 #ifdef AFS_DARWIN80_ENV
299 osi_FreeSmallSpace(tvec);
301 error = afs_CheckCode(error, &treq, 6);
306 /* called on writes */
308 afs_UFSWrite(struct vcache *avc, struct uio *auio, int aio,
309 afs_ucred_t *acred, int noLock)
311 afs_size_t totalLength;
312 afs_size_t transferLength;
314 afs_size_t offset, len;
324 #ifdef AFS_DARWIN80_ENV
328 struct uio *tuiop = &tuio;
329 struct iovec *tvec; /* again, should have define */
331 struct osi_file *tfile;
333 struct vrequest treq;
335 AFS_STATCNT(afs_UFSWrite);
337 return avc->vc_error;
339 if (AFS_IS_DISCONNECTED && !AFS_IS_DISCON_RW)
342 startDate = osi_Time();
343 if ((code = afs_InitReq(&treq, acred)))
345 /* otherwise we read */
346 totalLength = AFS_UIO_RESID(auio);
347 filePos = AFS_UIO_OFFSET(auio);
350 afs_Trace4(afs_iclSetp, CM_TRACE_WRITE, ICL_TYPE_POINTER, avc,
351 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(filePos), ICL_TYPE_OFFSET,
352 ICL_HANDLE_OFFSET(totalLength), ICL_TYPE_OFFSET,
353 ICL_HANDLE_OFFSET(avc->f.m.Length));
355 afs_MaybeWakeupTruncateDaemon();
356 ObtainWriteLock(&avc->lock, 556);
358 #if defined(AFS_SGI_ENV)
362 * afs_xwrite handles setting m.Length
363 * and handles APPEND mode.
364 * Since we are called via strategy, we need to trim the write to
365 * the actual size of the file
367 osi_Assert(filePos <= avc->f.m.Length);
368 diff = avc->f.m.Length - filePos;
369 AFS_UIO_SETRESID(auio, MIN(totalLength, diff));
370 totalLength = AFS_UIO_RESID(auio);
373 if (aio & IO_APPEND) {
374 /* append mode, start it at the right spot */
375 #if defined(AFS_SUN56_ENV)
376 auio->uio_loffset = 0;
378 filePos = avc->f.m.Length;
379 AFS_UIO_SETOFFSET(auio, avc->f.m.Length);
383 * Note that we use startDate rather than calling osi_Time() here.
384 * This is to avoid counting lock-waiting time in file date (for ranlib).
386 avc->f.m.Date = startDate;
388 #if defined(AFS_HPUX_ENV)
389 #if defined(AFS_HPUX101_ENV)
390 if ((totalLength + filePos) >> 9 >
391 p_rlimit(u.u_procp)[RLIMIT_FSIZE].rlim_cur) {
393 if ((totalLength + filePos) >> 9 > u.u_rlimit[RLIMIT_FSIZE].rlim_cur) {
396 ReleaseWriteLock(&avc->lock);
400 #ifdef AFS_VM_RDWR_ENV
402 * If write is implemented via VM, afs_FakeOpen() is called from the
403 * high-level write op.
405 if (avc->execsOrWriters <= 0) {
406 afs_warn("WARNING: afs_ufswr vcp=%lx, exOrW=%d\n", (unsigned long)avc,
407 avc->execsOrWriters);
412 avc->f.states |= CDirty;
413 #ifndef AFS_DARWIN80_ENV
414 tvec = (struct iovec *)osi_AllocSmallSpace(sizeof(struct iovec));
416 while (totalLength > 0) {
417 tdc = afs_ObtainDCacheForWriting(avc, filePos, totalLength, &treq,
423 tfile = (struct osi_file *)osi_UFSOpen(&tdc->f.inode);
424 len = totalLength; /* write this amount by default */
425 offset = filePos - AFS_CHUNKTOBASE(tdc->f.chunk);
426 max = AFS_CHUNKTOSIZE(tdc->f.chunk); /* max size of this chunk */
427 if (max <= len + offset) { /*if we'd go past the end of this chunk */
428 /* it won't all fit in this chunk, so write as much
433 #ifdef AFS_DARWIN80_ENV
437 tuiop = afsio_darwin_partialcopy(auio, trimlen);
439 /* mung uio structure to be right for this transfer */
440 afsio_copy(auio, &tuio, tvec);
442 afsio_trim(&tuio, trimlen);
444 AFS_UIO_SETOFFSET(tuiop, offset);
446 #if defined(AFS_AIX41_ENV)
449 VNOP_RDWR(tfile->vnode, UIO_WRITE, FWRITE, &tuio, NULL, NULL,
450 NULL, afs_osi_credp);
452 #elif defined(AFS_AIX32_ENV)
453 code = VNOP_RDWR(tfile->vnode, UIO_WRITE, FWRITE, &tuio, NULL, NULL);
454 #elif defined(AFS_AIX_ENV)
456 VNOP_RDWR(tfile->vnode, UIO_WRITE, FWRITE, (off_t) & offset,
457 &tuio, NULL, NULL, -1);
458 #elif defined(AFS_SUN5_ENV)
460 #ifdef AFS_SUN510_ENV
464 VOP_RWLOCK(tfile->vnode, 1, &ct);
465 code = VOP_WRITE(tfile->vnode, &tuio, 0, afs_osi_credp, &ct);
466 VOP_RWUNLOCK(tfile->vnode, 1, &ct);
469 VOP_RWLOCK(tfile->vnode, 1);
470 code = VOP_WRITE(tfile->vnode, &tuio, 0, afs_osi_credp);
471 VOP_RWUNLOCK(tfile->vnode, 1);
476 ("\n\n\n*** Cache partition is full - decrease cachesize!!! ***\n\n\n");
477 #elif defined(AFS_SGI_ENV)
479 avc->f.states |= CWritingUFS;
480 AFS_VOP_RWLOCK(tfile->vnode, VRWLOCK_WRITE);
481 AFS_VOP_WRITE(tfile->vnode, &tuio, IO_ISLOCKED, afs_osi_credp, code);
482 AFS_VOP_RWUNLOCK(tfile->vnode, VRWLOCK_WRITE);
483 avc->f.states &= ~CWritingUFS;
485 #elif defined(AFS_HPUX100_ENV)
488 code = VOP_RDWR(tfile->vnode, &tuio, UIO_WRITE, 0, afs_osi_credp);
491 #elif defined(AFS_LINUX20_ENV)
493 code = osi_rdwr(tfile, &tuio, UIO_WRITE);
495 #elif defined(AFS_DARWIN80_ENV)
497 code = VNOP_WRITE(tfile->vnode, tuiop, 0, afs_osi_ctxtp);
499 #elif defined(AFS_DARWIN_ENV)
501 VOP_LOCK(tfile->vnode, LK_EXCLUSIVE, current_proc());
502 code = VOP_WRITE(tfile->vnode, &tuio, 0, afs_osi_credp);
503 VOP_UNLOCK(tfile->vnode, 0, current_proc());
505 #elif defined(AFS_FBSD80_ENV)
507 VOP_LOCK(tfile->vnode, LK_EXCLUSIVE);
508 code = VOP_WRITE(tfile->vnode, &tuio, 0, afs_osi_credp);
509 VOP_UNLOCK(tfile->vnode, 0);
511 #elif defined(AFS_FBSD_ENV)
513 VOP_LOCK(tfile->vnode, LK_EXCLUSIVE, curthread);
514 code = VOP_WRITE(tfile->vnode, &tuio, 0, afs_osi_credp);
515 VOP_UNLOCK(tfile->vnode, 0, curthread);
517 #elif defined(AFS_NBSD_ENV)
519 VOP_LOCK(tfile->vnode, LK_EXCLUSIVE);
520 code = VOP_WRITE(tfile->vnode, &tuio, 0, afs_osi_credp);
521 VOP_UNLOCK(tfile->vnode, 0);
523 #elif defined(AFS_XBSD_ENV)
525 VOP_LOCK(tfile->vnode, LK_EXCLUSIVE, curproc);
526 code = VOP_WRITE(tfile->vnode, &tuio, 0, afs_osi_credp);
527 VOP_UNLOCK(tfile->vnode, 0, curproc);
531 tuio.uio_fpflags &= ~FSYNCIO; /* don't do sync io */
533 code = VOP_RDWR(tfile->vnode, &tuio, UIO_WRITE, 0, afs_osi_credp);
537 ZapDCE(tdc); /* bad data */
538 osi_UFSTruncate(tfile, 0); /* fake truncate the segment */
539 afs_AdjustSize(tdc, 0); /* sets f.chunkSize to 0 */
540 afs_stats_cmperf.cacheCurrDirtyChunks--;
541 afs_indexFlags[tdc->index] &= ~IFDataMod; /* so it does disappear */
542 afs_CFileClose(tfile);
543 ReleaseWriteLock(&tdc->lock);
547 /* otherwise we've written some, fixup length, etc and continue with next seg */
548 len = len - AFS_UIO_RESID(tuiop); /* compute amount really transferred */
550 afsio_skip(auio, tlen); /* advance auio over data written */
551 /* compute new file size */
552 if (offset + len > tdc->f.chunkBytes) {
553 afs_int32 tlength = offset + len;
554 afs_AdjustSize(tdc, tlength);
555 if (tdc->validPos < filePos + len)
556 tdc->validPos = filePos + len;
559 transferLength += len;
561 #if defined(AFS_SGI_ENV)
562 /* afs_xwrite handles setting m.Length */
563 osi_Assert(filePos <= avc->f.m.Length);
565 if (filePos > avc->f.m.Length) {
566 if (AFS_IS_DISCON_RW)
567 afs_PopulateDCache(avc, filePos, &treq);
568 afs_Trace4(afs_iclSetp, CM_TRACE_SETLENGTH, ICL_TYPE_STRING,
569 __FILE__, ICL_TYPE_LONG, __LINE__, ICL_TYPE_OFFSET,
570 ICL_HANDLE_OFFSET(avc->f.m.Length), ICL_TYPE_OFFSET,
571 ICL_HANDLE_OFFSET(filePos));
572 avc->f.m.Length = filePos;
576 ReleaseWriteLock(&tdc->lock);
578 #if !defined(AFS_VM_RDWR_ENV)
580 * If write is implemented via VM, afs_DoPartialWrite() is called from
581 * the high-level write op.
584 code = afs_DoPartialWrite(avc, &treq);
592 #ifndef AFS_VM_RDWR_ENV
593 afs_FakeClose(avc, acred);
595 error = afs_CheckCode(error, &treq, 7);
596 /* This set is here so we get the CheckCode. */
597 if (error && !avc->vc_error)
598 avc->vc_error = error;
600 ReleaseWriteLock(&avc->lock);
601 #ifdef AFS_DARWIN80_ENV
604 osi_FreeSmallSpace(tvec);
606 #ifndef AFS_VM_RDWR_ENV
608 * If write is implemented via VM, afs_fsync() is called from the high-level
611 #if defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
612 if (noLock && (aio & IO_SYNC)) {
615 /* On hpux on synchronous writes syncio will be set to IO_SYNC. If
616 * we're doing them because the file was opened with O_SYNCIO specified,
617 * we have to look in the u area. No single mechanism here!!
619 if (noLock && ((aio & IO_SYNC) | (auio->uio_fpflags & FSYNCIO))) {
621 if (noLock && (aio & FSYNC)) {
624 if (!AFS_NFSXLATORREQ(acred))
625 afs_fsync(avc, acred);
631 /* do partial write if we're low on unmodified chunks */
633 afs_DoPartialWrite(struct vcache *avc, struct vrequest *areq)
637 if (afs_stats_cmperf.cacheCurrDirtyChunks <=
638 afs_stats_cmperf.cacheMaxDirtyChunks
639 || AFS_IS_DISCONNECTED)
640 return 0; /* nothing to do */
641 /* otherwise, call afs_StoreDCache (later try to do this async, if possible) */
642 afs_Trace2(afs_iclSetp, CM_TRACE_PARTIALWRITE, ICL_TYPE_POINTER, avc,
643 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(avc->f.m.Length));
645 #if defined(AFS_SUN5_ENV)
646 code = afs_StoreAllSegments(avc, areq, AFS_ASYNC | AFS_VMSYNC_INVAL);
648 code = afs_StoreAllSegments(avc, areq, AFS_ASYNC);
653 /* handle any closing cleanup stuff */
655 #if defined(AFS_SGI65_ENV)
656 afs_close(OSI_VC_DECL(avc), afs_int32 aflags, lastclose_t lastclose,
658 #elif defined(AFS_SGI64_ENV)
659 afs_close(OSI_VC_DECL(avc), afs_int32 aflags, lastclose_t lastclose,
660 off_t offset, afs_ucred_t *acred, struct flid *flp)
661 #elif defined(AFS_SGI_ENV)
662 afs_close(OSI_VC_DECL(avc), afs_int32 aflags, lastclose_t lastclose
663 off_t offset, afs_ucred_t *acred)
664 #elif defined(AFS_SUN5_ENV)
665 afs_close(OSI_VC_DECL(avc), afs_int32 aflags, int count, offset_t offset,
668 afs_close(OSI_VC_DECL(avc), afs_int32 aflags, afs_ucred_t *acred)
673 struct vrequest treq;
677 struct afs_fakestat_state fakestat;
680 AFS_STATCNT(afs_close);
681 afs_Trace2(afs_iclSetp, CM_TRACE_CLOSE, ICL_TYPE_POINTER, avc,
682 ICL_TYPE_INT32, aflags);
683 code = afs_InitReq(&treq, acred);
686 afs_InitFakeStat(&fakestat);
687 code = afs_EvalFakeStat(&avc, &fakestat, &treq);
689 afs_PutFakeStat(&fakestat);
694 if (avc->flockCount) {
695 HandleFlock(avc, LOCK_UN, &treq, 0, 1 /*onlymine */ );
698 #if defined(AFS_SGI_ENV)
700 afs_PutFakeStat(&fakestat);
704 /* unlock any locks for pid - could be wrong for child .. */
705 AFS_RWLOCK((vnode_t *) avc, VRWLOCK_WRITE);
706 # ifdef AFS_SGI65_ENV
707 get_current_flid(&flid);
708 cleanlocks((vnode_t *) avc, flid.fl_pid, flid.fl_sysid);
709 HandleFlock(avc, LOCK_UN, &treq, flid.fl_pid, 1 /*onlymine */ );
711 # ifdef AFS_SGI64_ENV
712 cleanlocks((vnode_t *) avc, flp);
713 # else /* AFS_SGI64_ENV */
714 cleanlocks((vnode_t *) avc, u.u_procp->p_epid, u.u_procp->p_sysid);
715 # endif /* AFS_SGI64_ENV */
716 HandleFlock(avc, LOCK_UN, &treq, OSI_GET_CURRENT_PID(), 1 /*onlymine */ );
717 # endif /* AFS_SGI65_ENV */
718 /* afs_chkpgoob will drop and re-acquire the global lock. */
719 afs_chkpgoob(&avc->v, btoc(avc->f.m.Length));
720 #elif defined(AFS_SUN5_ENV)
722 /* The vfs layer may call this repeatedly with higher "count"; only
723 * on the last close (i.e. count = 1) we should actually proceed
725 afs_PutFakeStat(&fakestat);
730 if (avc->flockCount) { /* Release Lock */
731 HandleFlock(avc, LOCK_UN, &treq, 0, 1 /*onlymine */ );
734 if (aflags & (FWRITE | FTRUNC)) {
735 if (afs_BBusy() || (AFS_NFSXLATORREQ(acred)) || AFS_IS_DISCONNECTED) {
736 /* do it yourself if daemons are all busy */
737 ObtainWriteLock(&avc->lock, 124);
738 code = afs_StoreOnLastReference(avc, &treq);
739 ReleaseWriteLock(&avc->lock);
740 #if defined(AFS_SGI_ENV)
741 AFS_RWUNLOCK((vnode_t *) avc, VRWLOCK_WRITE);
744 #if defined(AFS_SGI_ENV)
745 AFS_RWUNLOCK((vnode_t *) avc, VRWLOCK_WRITE);
747 /* at least one daemon is idle, so ask it to do the store.
748 * Also, note that we don't lock it any more... */
749 tb = afs_BQueue(BOP_STORE, avc, 0, 1, acred,
750 (afs_size_t) afs_cr_uid(acred), (afs_size_t) 0,
751 (void *)0, (void *)0, (void *)0);
752 /* sleep waiting for the store to start, then retrieve error code */
753 while ((tb->flags & BUVALID) == 0) {
761 /* VNOVNODE is "acceptable" error code from close, since
762 * may happen when deleting a file on another machine while
763 * it is open here. We do the same for ENOENT since in afs_CheckCode we map VNOVNODE -> ENOENT */
764 if (code == VNOVNODE || code == ENOENT)
767 /* Ensure last closer gets the error. If another thread caused
768 * DoPartialWrite and this thread does not actually store the data,
769 * it may not see the quota error.
771 ObtainWriteLock(&avc->lock, 406);
774 osi_ReleaseVM(avc, acred);
776 /* printf("avc->vc_error=%d\n", avc->vc_error); */
777 code = avc->vc_error;
780 ReleaseWriteLock(&avc->lock);
782 /* some codes merit specific complaint */
784 afs_warnuser("afs: failed to store file (network problems)\n");
787 else if (code == ENOSPC) {
789 ("afs: failed to store file (over quota or partition full)\n");
792 else if (code == ENOSPC) {
793 afs_warnuser("afs: failed to store file (partition full)\n");
794 } else if (code == EDQUOT) {
795 afs_warnuser("afs: failed to store file (over quota)\n");
799 afs_warnuser("afs: failed to store file (%d)\n", code);
801 /* finally, we flush any text pages lying around here */
805 #if defined(AFS_SGI_ENV)
806 AFS_RWUNLOCK((vnode_t *) avc, VRWLOCK_WRITE);
807 osi_Assert(avc->opens > 0);
809 /* file open for read */
810 ObtainWriteLock(&avc->lock, 411);
813 osi_ReleaseVM(avc, acred);
815 code = avc->vc_error;
818 #if defined(AFS_FBSD80_ENV)
821 afs_int32 opens, is_free, is_gone, is_doomed, iflag;
822 struct vnode *vp = AFSTOV(avc);
824 is_doomed = vp->v_iflag & VI_DOOMED;
825 is_free = vp->v_iflag & VI_FREE;
826 is_gone = vp->v_iflag & VI_DOINGINACT;
830 afs_warn("afs_close avc %p vp %p opens %d free %d doinginact %d doomed %d iflag %d\n",
831 avc, vp, opens, is_free, is_gone, is_doomed, iflag);
835 ReleaseWriteLock(&avc->lock);
838 afs_PutFakeStat(&fakestat);
839 code = afs_CheckCode(code, &treq, 5);
845 #if defined(AFS_SGI_ENV) || defined(AFS_SUN53_ENV)
846 afs_fsync(OSI_VC_DECL(avc), int flag, afs_ucred_t *acred
847 # ifdef AFS_SGI65_ENV
848 , off_t start, off_t stop
849 # endif /* AFS_SGI65_ENV */
851 #else /* !SUN53 && !SGI */
852 afs_fsync(OSI_VC_DECL(avc), afs_ucred_t *acred)
856 struct vrequest treq;
860 return avc->vc_error;
862 #if defined(AFS_SUN5_ENV)
863 /* back out if called from NFS server */
864 if (curthread->t_flag & T_DONTPEND)
868 AFS_STATCNT(afs_fsync);
869 afs_Trace1(afs_iclSetp, CM_TRACE_FSYNC, ICL_TYPE_POINTER, avc);
870 if ((code = afs_InitReq(&treq, acred)))
873 #if defined(AFS_SGI_ENV)
874 AFS_RWLOCK((vnode_t *) avc, VRWLOCK_WRITE);
875 if (flag & FSYNC_INVAL)
876 osi_VM_FSyncInval(avc);
877 #endif /* AFS_SGI_ENV */
879 ObtainSharedLock(&avc->lock, 18);
881 if (avc->execsOrWriters > 0) {
882 if (!AFS_IS_DISCONNECTED && !AFS_IS_DISCON_RW) {
883 /* Your average flush. */
885 /* put the file back */
886 UpgradeSToWLock(&avc->lock, 41);
887 code = afs_StoreAllSegments(avc, &treq, AFS_SYNC);
888 ConvertWToSLock(&avc->lock);
890 UpgradeSToWLock(&avc->lock, 711);
891 afs_DisconAddDirty(avc, VDisconWriteFlush, 1);
892 ConvertWToSLock(&avc->lock);
893 } /* if not disconnected */
894 } /* if (avc->execsOrWriters > 0) */
896 #if defined(AFS_SGI_ENV)
897 AFS_RWUNLOCK((vnode_t *) avc, VRWLOCK_WRITE);
898 if (code == VNOVNODE) {
899 /* syncing an unlinked file! - non-informative to pass an errno
900 * 102 (== VNOVNODE) to user
906 code = afs_CheckCode(code, &treq, 33);
907 ReleaseSharedLock(&avc->lock);