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 #if defined(AFS_FBSD_ENV) || defined(AFS_DFBSD_ENV)
114 struct vnode *vp = AFSTOV(avc);
116 #ifdef AFS_DARWIN80_ENV
120 struct uio *tuiop = &tuio;
121 struct iovec *tvec; /* again, should have define */
124 struct vrequest treq;
126 AFS_STATCNT(afs_MemWrite);
128 return avc->vc_error;
130 startDate = osi_Time();
131 if ((code = afs_InitReq(&treq, acred)))
133 /* otherwise we read */
134 totalLength = AFS_UIO_RESID(auio);
135 filePos = AFS_UIO_OFFSET(auio);
138 afs_Trace4(afs_iclSetp, CM_TRACE_WRITE, ICL_TYPE_POINTER, avc,
139 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(filePos), ICL_TYPE_OFFSET,
140 ICL_HANDLE_OFFSET(totalLength), ICL_TYPE_OFFSET,
141 ICL_HANDLE_OFFSET(avc->f.m.Length));
143 afs_MaybeWakeupTruncateDaemon();
144 ObtainWriteLock(&avc->lock, 126);
146 #if defined(AFS_SGI_ENV)
150 * afs_xwrite handles setting m.Length
151 * and handles APPEND mode.
152 * Since we are called via strategy, we need to trim the write to
153 * the actual size of the file
155 osi_Assert(filePos <= avc->f.m.Length);
156 diff = avc->f.m.Length - filePos;
157 AFS_UIO_SETRESID(auio, MIN(totalLength, diff));
158 totalLength = AFS_UIO_RESID(auio);
161 if (aio & IO_APPEND) {
162 /* append mode, start it at the right spot */
163 #if defined(AFS_SUN56_ENV)
164 auio->uio_loffset = 0;
166 filePos = avc->f.m.Length;
167 AFS_UIO_SETOFFSET(auio, filePos);
171 * Note that we use startDate rather than calling osi_Time() here.
172 * This is to avoid counting lock-waiting time in file date (for ranlib).
174 avc->f.m.Date = startDate;
176 #if defined(AFS_HPUX_ENV)
177 #if defined(AFS_HPUX101_ENV)
178 if ((totalLength + filePos) >> 9 >
179 (p_rlimit(u.u_procp))[RLIMIT_FSIZE].rlim_cur) {
181 if ((totalLength + filePos) >> 9 > u.u_rlimit[RLIMIT_FSIZE].rlim_cur) {
184 ReleaseWriteLock(&avc->lock);
188 #ifdef AFS_VM_RDWR_ENV
190 * If write is implemented via VM, afs_FakeOpen() is called from the
191 * high-level write op.
193 if (avc->execsOrWriters <= 0) {
194 afs_warn("WARNING: afs_ufswr vp=%lx, exOrW=%d\n", (unsigned long)avc,
195 avc->execsOrWriters);
200 avc->f.states |= CDirty;
201 #ifndef AFS_DARWIN80_ENV
202 tvec = (struct iovec *)osi_AllocSmallSpace(sizeof(struct iovec));
204 while (totalLength > 0) {
205 tdc = afs_ObtainDCacheForWriting(avc, filePos, totalLength, &treq,
212 len = totalLength; /* write this amount by default */
213 offset = filePos - AFS_CHUNKTOBASE(tdc->f.chunk);
214 max = AFS_CHUNKTOSIZE(tdc->f.chunk); /* max size of this chunk */
215 if (max <= len + offset) { /*if we'd go past the end of this chunk */
216 /* it won't all fit in this chunk, so write as much
221 #ifdef AFS_DARWIN80_ENV
225 tuiop = afsio_darwin_partialcopy(auio, trimlen);
227 /* mung uio structure to be right for this transfer */
228 afsio_copy(auio, &tuio, tvec);
230 afsio_trim(&tuio, trimlen);
232 AFS_UIO_SETOFFSET(tuiop, offset);
234 code = afs_MemWriteUIO(&tdc->f.inode, tuiop);
236 void *mep; /* XXX in prototype world is struct memCacheEntry * */
238 ZapDCE(tdc); /* bad data */
239 mep = afs_MemCacheOpen(&tdc->f.inode);
240 afs_MemCacheTruncate(mep, 0);
241 afs_MemCacheClose(mep);
242 afs_stats_cmperf.cacheCurrDirtyChunks--;
243 afs_indexFlags[tdc->index] &= ~IFDataMod; /* so it does disappear */
244 ReleaseWriteLock(&tdc->lock);
248 /* otherwise we've written some, fixup length, etc and continue with next seg */
249 len = len - AFS_UIO_RESID(tuiop); /* compute amount really transferred */
251 afsio_skip(auio, tlen); /* advance auio over data written */
252 /* compute new file size */
253 if (offset + len > tdc->f.chunkBytes) {
254 afs_int32 tlength = offset + len;
255 afs_AdjustSize(tdc, tlength);
256 if (tdc->validPos < filePos + len)
257 tdc->validPos = filePos + len;
260 transferLength += len;
262 #if defined(AFS_SGI_ENV)
263 /* afs_xwrite handles setting m.Length */
264 osi_Assert(filePos <= avc->f.m.Length);
266 if (filePos > avc->f.m.Length) {
267 if (AFS_IS_DISCON_RW)
268 afs_PopulateDCache(avc, filePos, &treq);
269 afs_Trace4(afs_iclSetp, CM_TRACE_SETLENGTH, ICL_TYPE_STRING,
270 __FILE__, ICL_TYPE_LONG, __LINE__, ICL_TYPE_OFFSET,
271 ICL_HANDLE_OFFSET(avc->f.m.Length), ICL_TYPE_OFFSET,
272 ICL_HANDLE_OFFSET(filePos));
273 avc->f.m.Length = filePos;
274 #if defined(AFS_FBSD_ENV) || defined(AFS_DFBSD_ENV)
275 vnode_pager_setsize(vp, filePos);
279 ReleaseWriteLock(&tdc->lock);
281 #if !defined(AFS_VM_RDWR_ENV)
283 * If write is implemented via VM, afs_DoPartialWrite() is called from
284 * the high-level write op.
287 code = afs_DoPartialWrite(avc, &treq);
295 #ifndef AFS_VM_RDWR_ENV
296 afs_FakeClose(avc, acred);
298 if (error && !avc->vc_error)
299 avc->vc_error = error;
301 ReleaseWriteLock(&avc->lock);
302 #ifdef AFS_DARWIN80_ENV
305 osi_FreeSmallSpace(tvec);
307 error = afs_CheckCode(error, &treq, 6);
312 /* called on writes */
314 afs_UFSWrite(struct vcache *avc, struct uio *auio, int aio,
315 afs_ucred_t *acred, int noLock)
317 afs_size_t totalLength;
318 afs_size_t transferLength;
320 afs_size_t offset, len;
330 #if defined(AFS_FBSD_ENV) || defined(AFS_DFBSD_ENV)
331 struct vnode *vp = AFSTOV(avc);
333 #ifdef AFS_DARWIN80_ENV
337 struct uio *tuiop = &tuio;
338 struct iovec *tvec; /* again, should have define */
340 struct osi_file *tfile;
342 struct vrequest treq;
344 AFS_STATCNT(afs_UFSWrite);
346 return avc->vc_error;
348 if (AFS_IS_DISCONNECTED && !AFS_IS_DISCON_RW)
351 startDate = osi_Time();
352 if ((code = afs_InitReq(&treq, acred)))
354 /* otherwise we read */
355 totalLength = AFS_UIO_RESID(auio);
356 filePos = AFS_UIO_OFFSET(auio);
359 afs_Trace4(afs_iclSetp, CM_TRACE_WRITE, ICL_TYPE_POINTER, avc,
360 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(filePos), ICL_TYPE_OFFSET,
361 ICL_HANDLE_OFFSET(totalLength), ICL_TYPE_OFFSET,
362 ICL_HANDLE_OFFSET(avc->f.m.Length));
364 afs_MaybeWakeupTruncateDaemon();
365 ObtainWriteLock(&avc->lock, 556);
367 #if defined(AFS_SGI_ENV)
371 * afs_xwrite handles setting m.Length
372 * and handles APPEND mode.
373 * Since we are called via strategy, we need to trim the write to
374 * the actual size of the file
376 osi_Assert(filePos <= avc->f.m.Length);
377 diff = avc->f.m.Length - filePos;
378 AFS_UIO_SETRESID(auio, MIN(totalLength, diff));
379 totalLength = AFS_UIO_RESID(auio);
382 if (aio & IO_APPEND) {
383 /* append mode, start it at the right spot */
384 #if defined(AFS_SUN56_ENV)
385 auio->uio_loffset = 0;
387 filePos = avc->f.m.Length;
388 AFS_UIO_SETOFFSET(auio, avc->f.m.Length);
392 * Note that we use startDate rather than calling osi_Time() here.
393 * This is to avoid counting lock-waiting time in file date (for ranlib).
395 avc->f.m.Date = startDate;
397 #if defined(AFS_HPUX_ENV)
398 #if defined(AFS_HPUX101_ENV)
399 if ((totalLength + filePos) >> 9 >
400 p_rlimit(u.u_procp)[RLIMIT_FSIZE].rlim_cur) {
402 if ((totalLength + filePos) >> 9 > u.u_rlimit[RLIMIT_FSIZE].rlim_cur) {
405 ReleaseWriteLock(&avc->lock);
409 #ifdef AFS_VM_RDWR_ENV
411 * If write is implemented via VM, afs_FakeOpen() is called from the
412 * high-level write op.
414 if (avc->execsOrWriters <= 0) {
415 afs_warn("WARNING: afs_ufswr vcp=%lx, exOrW=%d\n", (unsigned long)avc,
416 avc->execsOrWriters);
421 avc->f.states |= CDirty;
422 #ifndef AFS_DARWIN80_ENV
423 tvec = (struct iovec *)osi_AllocSmallSpace(sizeof(struct iovec));
425 while (totalLength > 0) {
426 tdc = afs_ObtainDCacheForWriting(avc, filePos, totalLength, &treq,
432 tfile = (struct osi_file *)osi_UFSOpen(&tdc->f.inode);
433 len = totalLength; /* write this amount by default */
434 offset = filePos - AFS_CHUNKTOBASE(tdc->f.chunk);
435 max = AFS_CHUNKTOSIZE(tdc->f.chunk); /* max size of this chunk */
436 if (max <= len + offset) { /*if we'd go past the end of this chunk */
437 /* it won't all fit in this chunk, so write as much
442 #ifdef AFS_DARWIN80_ENV
446 tuiop = afsio_darwin_partialcopy(auio, trimlen);
448 /* mung uio structure to be right for this transfer */
449 afsio_copy(auio, &tuio, tvec);
451 afsio_trim(&tuio, trimlen);
453 AFS_UIO_SETOFFSET(tuiop, offset);
455 #if defined(AFS_AIX41_ENV)
458 VNOP_RDWR(tfile->vnode, UIO_WRITE, FWRITE, &tuio, NULL, NULL,
459 NULL, afs_osi_credp);
461 #elif defined(AFS_AIX32_ENV)
462 code = VNOP_RDWR(tfile->vnode, UIO_WRITE, FWRITE, &tuio, NULL, NULL);
463 #elif defined(AFS_AIX_ENV)
465 VNOP_RDWR(tfile->vnode, UIO_WRITE, FWRITE, (off_t) & offset,
466 &tuio, NULL, NULL, -1);
467 #elif defined(AFS_SUN5_ENV)
469 #ifdef AFS_SUN510_ENV
473 VOP_RWLOCK(tfile->vnode, 1, &ct);
474 code = VOP_WRITE(tfile->vnode, &tuio, 0, afs_osi_credp, &ct);
475 VOP_RWUNLOCK(tfile->vnode, 1, &ct);
478 VOP_RWLOCK(tfile->vnode, 1);
479 code = VOP_WRITE(tfile->vnode, &tuio, 0, afs_osi_credp);
480 VOP_RWUNLOCK(tfile->vnode, 1);
485 ("\n\n\n*** Cache partition is full - decrease cachesize!!! ***\n\n\n");
486 #elif defined(AFS_SGI_ENV)
488 avc->f.states |= CWritingUFS;
489 AFS_VOP_RWLOCK(tfile->vnode, VRWLOCK_WRITE);
490 AFS_VOP_WRITE(tfile->vnode, &tuio, IO_ISLOCKED, afs_osi_credp, code);
491 AFS_VOP_RWUNLOCK(tfile->vnode, VRWLOCK_WRITE);
492 avc->f.states &= ~CWritingUFS;
494 #elif defined(AFS_HPUX100_ENV)
497 code = VOP_RDWR(tfile->vnode, &tuio, UIO_WRITE, 0, afs_osi_credp);
500 #elif defined(AFS_LINUX20_ENV)
502 code = osi_rdwr(tfile, &tuio, UIO_WRITE);
504 #elif defined(AFS_DARWIN80_ENV)
506 code = VNOP_WRITE(tfile->vnode, tuiop, 0, afs_osi_ctxtp);
508 #elif defined(AFS_DARWIN_ENV)
510 VOP_LOCK(tfile->vnode, LK_EXCLUSIVE, current_proc());
511 code = VOP_WRITE(tfile->vnode, &tuio, 0, afs_osi_credp);
512 VOP_UNLOCK(tfile->vnode, 0, current_proc());
514 #elif defined(AFS_FBSD80_ENV)
516 VOP_LOCK(tfile->vnode, LK_EXCLUSIVE);
517 code = VOP_WRITE(tfile->vnode, &tuio, 0, afs_osi_credp);
518 VOP_UNLOCK(tfile->vnode, 0);
520 #elif defined(AFS_FBSD_ENV)
522 VOP_LOCK(tfile->vnode, LK_EXCLUSIVE, curthread);
523 code = VOP_WRITE(tfile->vnode, &tuio, 0, afs_osi_credp);
524 VOP_UNLOCK(tfile->vnode, 0, curthread);
526 #elif defined(AFS_NBSD_ENV)
528 VOP_LOCK(tfile->vnode, LK_EXCLUSIVE);
529 code = VOP_WRITE(tfile->vnode, &tuio, 0, afs_osi_credp);
530 VOP_UNLOCK(tfile->vnode, 0);
532 #elif defined(AFS_XBSD_ENV)
534 VOP_LOCK(tfile->vnode, LK_EXCLUSIVE, curproc);
535 code = VOP_WRITE(tfile->vnode, &tuio, 0, afs_osi_credp);
536 VOP_UNLOCK(tfile->vnode, 0, curproc);
540 tuio.uio_fpflags &= ~FSYNCIO; /* don't do sync io */
542 code = VOP_RDWR(tfile->vnode, &tuio, UIO_WRITE, 0, afs_osi_credp);
546 ZapDCE(tdc); /* bad data */
547 osi_UFSTruncate(tfile, 0); /* fake truncate the segment */
548 afs_AdjustSize(tdc, 0); /* sets f.chunkSize to 0 */
549 afs_stats_cmperf.cacheCurrDirtyChunks--;
550 afs_indexFlags[tdc->index] &= ~IFDataMod; /* so it does disappear */
551 afs_CFileClose(tfile);
552 ReleaseWriteLock(&tdc->lock);
556 /* otherwise we've written some, fixup length, etc and continue with next seg */
557 len = len - AFS_UIO_RESID(tuiop); /* compute amount really transferred */
559 afsio_skip(auio, tlen); /* advance auio over data written */
560 /* compute new file size */
561 if (offset + len > tdc->f.chunkBytes) {
562 afs_int32 tlength = offset + len;
563 afs_AdjustSize(tdc, tlength);
564 if (tdc->validPos < filePos + len)
565 tdc->validPos = filePos + len;
568 transferLength += len;
570 #if defined(AFS_SGI_ENV)
571 /* afs_xwrite handles setting m.Length */
572 osi_Assert(filePos <= avc->f.m.Length);
574 if (filePos > avc->f.m.Length) {
575 if (AFS_IS_DISCON_RW)
576 afs_PopulateDCache(avc, filePos, &treq);
577 afs_Trace4(afs_iclSetp, CM_TRACE_SETLENGTH, ICL_TYPE_STRING,
578 __FILE__, ICL_TYPE_LONG, __LINE__, ICL_TYPE_OFFSET,
579 ICL_HANDLE_OFFSET(avc->f.m.Length), ICL_TYPE_OFFSET,
580 ICL_HANDLE_OFFSET(filePos));
581 avc->f.m.Length = filePos;
582 #if defined(AFS_FBSD_ENV) || defined(AFS_DFBSD_ENV)
583 vnode_pager_setsize(vp, filePos);
588 ReleaseWriteLock(&tdc->lock);
590 #if !defined(AFS_VM_RDWR_ENV)
592 * If write is implemented via VM, afs_DoPartialWrite() is called from
593 * the high-level write op.
596 code = afs_DoPartialWrite(avc, &treq);
604 #ifndef AFS_VM_RDWR_ENV
605 afs_FakeClose(avc, acred);
607 error = afs_CheckCode(error, &treq, 7);
608 /* This set is here so we get the CheckCode. */
609 if (error && !avc->vc_error)
610 avc->vc_error = error;
612 ReleaseWriteLock(&avc->lock);
613 #ifdef AFS_DARWIN80_ENV
616 osi_FreeSmallSpace(tvec);
618 #ifndef AFS_VM_RDWR_ENV
620 * If write is implemented via VM, afs_fsync() is called from the high-level
623 #if defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
624 if (noLock && (aio & IO_SYNC)) {
627 /* On hpux on synchronous writes syncio will be set to IO_SYNC. If
628 * we're doing them because the file was opened with O_SYNCIO specified,
629 * we have to look in the u area. No single mechanism here!!
631 if (noLock && ((aio & IO_SYNC) | (auio->uio_fpflags & FSYNCIO))) {
633 if (noLock && (aio & FSYNC)) {
636 if (!AFS_NFSXLATORREQ(acred))
637 afs_fsync(avc, acred);
643 /* do partial write if we're low on unmodified chunks */
645 afs_DoPartialWrite(struct vcache *avc, struct vrequest *areq)
649 if (afs_stats_cmperf.cacheCurrDirtyChunks <=
650 afs_stats_cmperf.cacheMaxDirtyChunks
651 || AFS_IS_DISCONNECTED)
652 return 0; /* nothing to do */
653 /* otherwise, call afs_StoreDCache (later try to do this async, if possible) */
654 afs_Trace2(afs_iclSetp, CM_TRACE_PARTIALWRITE, ICL_TYPE_POINTER, avc,
655 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(avc->f.m.Length));
657 #if defined(AFS_SUN5_ENV)
658 code = afs_StoreAllSegments(avc, areq, AFS_ASYNC | AFS_VMSYNC_INVAL);
660 code = afs_StoreAllSegments(avc, areq, AFS_ASYNC);
665 /* handle any closing cleanup stuff */
667 #if defined(AFS_SGI65_ENV)
668 afs_close(OSI_VC_DECL(avc), afs_int32 aflags, lastclose_t lastclose,
670 #elif defined(AFS_SGI64_ENV)
671 afs_close(OSI_VC_DECL(avc), afs_int32 aflags, lastclose_t lastclose,
672 off_t offset, afs_ucred_t *acred, struct flid *flp)
673 #elif defined(AFS_SGI_ENV)
674 afs_close(OSI_VC_DECL(avc), afs_int32 aflags, lastclose_t lastclose
675 off_t offset, afs_ucred_t *acred)
676 #elif defined(AFS_SUN5_ENV)
677 afs_close(OSI_VC_DECL(avc), afs_int32 aflags, int count, offset_t offset,
680 afs_close(OSI_VC_DECL(avc), afs_int32 aflags, afs_ucred_t *acred)
685 struct vrequest treq;
689 struct afs_fakestat_state fakestat;
692 AFS_STATCNT(afs_close);
693 afs_Trace2(afs_iclSetp, CM_TRACE_CLOSE, ICL_TYPE_POINTER, avc,
694 ICL_TYPE_INT32, aflags);
695 code = afs_InitReq(&treq, acred);
698 afs_InitFakeStat(&fakestat);
699 code = afs_EvalFakeStat(&avc, &fakestat, &treq);
701 afs_PutFakeStat(&fakestat);
706 if (avc->flockCount) {
707 HandleFlock(avc, LOCK_UN, &treq, 0, 1 /*onlymine */ );
710 #if defined(AFS_SGI_ENV)
712 afs_PutFakeStat(&fakestat);
716 /* unlock any locks for pid - could be wrong for child .. */
717 AFS_RWLOCK((vnode_t *) avc, VRWLOCK_WRITE);
718 # ifdef AFS_SGI65_ENV
719 get_current_flid(&flid);
720 cleanlocks((vnode_t *) avc, flid.fl_pid, flid.fl_sysid);
721 HandleFlock(avc, LOCK_UN, &treq, flid.fl_pid, 1 /*onlymine */ );
723 # ifdef AFS_SGI64_ENV
724 cleanlocks((vnode_t *) avc, flp);
725 # else /* AFS_SGI64_ENV */
726 cleanlocks((vnode_t *) avc, u.u_procp->p_epid, u.u_procp->p_sysid);
727 # endif /* AFS_SGI64_ENV */
728 HandleFlock(avc, LOCK_UN, &treq, OSI_GET_CURRENT_PID(), 1 /*onlymine */ );
729 # endif /* AFS_SGI65_ENV */
730 /* afs_chkpgoob will drop and re-acquire the global lock. */
731 afs_chkpgoob(&avc->v, btoc(avc->f.m.Length));
732 #elif defined(AFS_SUN5_ENV)
734 /* The vfs layer may call this repeatedly with higher "count"; only
735 * on the last close (i.e. count = 1) we should actually proceed
737 afs_PutFakeStat(&fakestat);
742 if (avc->flockCount) { /* Release Lock */
743 HandleFlock(avc, LOCK_UN, &treq, 0, 1 /*onlymine */ );
746 if (aflags & (FWRITE | FTRUNC)) {
747 if (afs_BBusy() || (AFS_NFSXLATORREQ(acred)) || AFS_IS_DISCONNECTED) {
748 /* do it yourself if daemons are all busy */
749 ObtainWriteLock(&avc->lock, 124);
750 code = afs_StoreOnLastReference(avc, &treq);
751 ReleaseWriteLock(&avc->lock);
752 #if defined(AFS_SGI_ENV)
753 AFS_RWUNLOCK((vnode_t *) avc, VRWLOCK_WRITE);
756 #if defined(AFS_SGI_ENV)
757 AFS_RWUNLOCK((vnode_t *) avc, VRWLOCK_WRITE);
759 /* at least one daemon is idle, so ask it to do the store.
760 * Also, note that we don't lock it any more... */
761 tb = afs_BQueue(BOP_STORE, avc, 0, 1, acred,
762 (afs_size_t) afs_cr_uid(acred), (afs_size_t) 0,
763 (void *)0, (void *)0, (void *)0);
764 /* sleep waiting for the store to start, then retrieve error code */
765 while ((tb->flags & BUVALID) == 0) {
773 /* VNOVNODE is "acceptable" error code from close, since
774 * may happen when deleting a file on another machine while
775 * it is open here. We do the same for ENOENT since in afs_CheckCode we map VNOVNODE -> ENOENT */
776 if (code == VNOVNODE || code == ENOENT)
779 /* Ensure last closer gets the error. If another thread caused
780 * DoPartialWrite and this thread does not actually store the data,
781 * it may not see the quota error.
783 ObtainWriteLock(&avc->lock, 406);
786 osi_ReleaseVM(avc, acred);
788 /* printf("avc->vc_error=%d\n", avc->vc_error); */
789 code = avc->vc_error;
792 ReleaseWriteLock(&avc->lock);
794 /* some codes merit specific complaint */
796 afs_warnuser("afs: failed to store file (network problems)\n");
799 else if (code == ENOSPC) {
801 ("afs: failed to store file (over quota or partition full)\n");
804 else if (code == ENOSPC) {
805 afs_warnuser("afs: failed to store file (partition full)\n");
806 } else if (code == EDQUOT) {
807 afs_warnuser("afs: failed to store file (over quota)\n");
811 afs_warnuser("afs: failed to store file (%d)\n", code);
813 /* finally, we flush any text pages lying around here */
817 #if defined(AFS_SGI_ENV)
818 AFS_RWUNLOCK((vnode_t *) avc, VRWLOCK_WRITE);
819 osi_Assert(avc->opens > 0);
821 /* file open for read */
822 ObtainWriteLock(&avc->lock, 411);
825 osi_ReleaseVM(avc, acred);
827 code = avc->vc_error;
830 #if defined(AFS_FBSD80_ENV)
833 afs_int32 opens, is_free, is_gone, is_doomed, iflag;
834 struct vnode *vp = AFSTOV(avc);
836 is_doomed = vp->v_iflag & VI_DOOMED;
837 is_free = vp->v_iflag & VI_FREE;
838 is_gone = vp->v_iflag & VI_DOINGINACT;
842 afs_warn("afs_close avc %p vp %p opens %d free %d doinginact %d doomed %d iflag %d\n",
843 avc, vp, opens, is_free, is_gone, is_doomed, iflag);
847 ReleaseWriteLock(&avc->lock);
850 afs_PutFakeStat(&fakestat);
851 code = afs_CheckCode(code, &treq, 5);
857 #if defined(AFS_SGI_ENV) || defined(AFS_SUN53_ENV)
858 afs_fsync(OSI_VC_DECL(avc), int flag, afs_ucred_t *acred
859 # ifdef AFS_SGI65_ENV
860 , off_t start, off_t stop
861 # endif /* AFS_SGI65_ENV */
863 #else /* !SUN53 && !SGI */
864 afs_fsync(OSI_VC_DECL(avc), afs_ucred_t *acred)
868 struct vrequest treq;
872 return avc->vc_error;
874 #if defined(AFS_SUN5_ENV)
875 /* back out if called from NFS server */
876 if (curthread->t_flag & T_DONTPEND)
880 AFS_STATCNT(afs_fsync);
881 afs_Trace1(afs_iclSetp, CM_TRACE_FSYNC, ICL_TYPE_POINTER, avc);
882 if ((code = afs_InitReq(&treq, acred)))
885 #if defined(AFS_SGI_ENV)
886 AFS_RWLOCK((vnode_t *) avc, VRWLOCK_WRITE);
887 if (flag & FSYNC_INVAL)
888 osi_VM_FSyncInval(avc);
889 #endif /* AFS_SGI_ENV */
891 ObtainSharedLock(&avc->lock, 18);
893 if (avc->execsOrWriters > 0) {
894 if (!AFS_IS_DISCONNECTED && !AFS_IS_DISCON_RW) {
895 /* Your average flush. */
897 /* put the file back */
898 UpgradeSToWLock(&avc->lock, 41);
899 code = afs_StoreAllSegments(avc, &treq, AFS_SYNC);
900 ConvertWToSLock(&avc->lock);
902 UpgradeSToWLock(&avc->lock, 711);
903 afs_DisconAddDirty(avc, VDisconWriteFlush, 1);
904 ConvertWToSLock(&avc->lock);
905 } /* if not disconnected */
906 } /* if (avc->execsOrWriters > 0) */
908 #if defined(AFS_SGI_ENV)
909 AFS_RWUNLOCK((vnode_t *) avc, VRWLOCK_WRITE);
910 if (code == VNOVNODE) {
911 /* syncing an unlinked file! - non-informative to pass an errno
912 * 102 (== VNOVNODE) to user
918 code = afs_CheckCode(code, &treq, 33);
919 ReleaseSharedLock(&avc->lock);