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
20 #include <afsconfig.h>
21 #include "../afs/param.h"
25 #include "../afs/sysincludes.h" /* Standard vendor system headers */
26 #include "../afs/afsincludes.h" /* Afs-based standard headers */
27 #include "../afs/afs_stats.h" /* statistics */
28 #include "../afs/afs_cbqueue.h"
29 #include "../afs/nfsclient.h"
30 #include "../afs/afs_osidnlc.h"
33 extern unsigned char *afs_indexFlags;
35 /* Called by all write-on-close routines: regular afs_close,
36 * store via background daemon and store via the
37 * afs_FlushActiveVCaches routine (when CCORE is on).
38 * avc->lock must be write-locked.
40 afs_StoreOnLastReference(avc, treq)
41 register struct vcache *avc;
42 register struct vrequest *treq;
46 AFS_STATCNT(afs_StoreOnLastReference);
47 /* if CCore flag is set, we clear it and do the extra decrement
48 * ourselves now. If we're called by the CCore clearer, the CCore
49 * flag will already be clear, so we don't have to worry about
50 * clearing it twice. */
51 if (avc->states & CCore) {
52 avc->states &= ~CCore;
53 #if defined(AFS_SGI_ENV)
54 osi_Assert(avc->opens > 0 && avc->execsOrWriters > 0);
56 /* WARNING: Our linux cm code treats the execsOrWriters counter differently
57 * depending on the flags the file was opened with. So, if you make any
58 * changes to the way the execsOrWriters flag is handled check with the
61 avc->execsOrWriters--;
62 AFS_RELE((struct vnode *)avc); /* VN_HOLD at set CCore(afs_FakeClose)*/
63 crfree((struct AFS_UCRED *)avc->linkData); /* "crheld" in afs_FakeClose */
64 avc->linkData = (char *)0;
66 /* Now, send the file back. Used to require 0 writers left, but now do
67 * it on every close for write, since two closes in a row are harmless
68 * since first will clean all chunks, and second will be noop. Note that
69 * this will also save confusion when someone keeps a file open
70 * inadvertently, since with old system, writes to the server would never
73 code = afs_StoreAllSegments(avc, treq, AFS_LASTSTORE/*!sync-to-disk*/);
75 * We have to do these after the above store in done: in some systems like
76 * aix they'll need to flush all the vm dirty pages to the disk via the
77 * strategy routine. During that all procedure (done under no avc locks)
78 * opens, refcounts would be zero, since it didn't reach the afs_{rd,wr}
79 * routines which means the vcache is a perfect candidate for flushing!
81 #if defined(AFS_SGI_ENV)
82 osi_Assert(avc->opens > 0 && avc->execsOrWriters > 0);
85 avc->execsOrWriters--;
91 afs_MemWrite(avc, auio, aio, acred, noLock)
92 register struct vcache *avc;
95 struct AFS_UCRED *acred;
97 afs_size_t totalLength;
98 afs_size_t transferLength;
100 afs_size_t offset, len;
101 afs_int32 tlen, trimlen;
104 register struct dcache *tdc;
110 struct iovec *tvec; /* again, should have define */
112 register afs_int32 code;
113 struct vrequest treq;
115 AFS_STATCNT(afs_MemWrite);
117 return avc->vc_error;
119 startDate = osi_Time();
120 if (code = afs_InitReq(&treq, acred)) return code;
121 /* otherwise we read */
122 totalLength = auio->afsio_resid;
123 filePos = auio->afsio_offset;
126 afs_Trace4(afs_iclSetp, CM_TRACE_WRITE, ICL_TYPE_POINTER, avc,
127 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(filePos),
128 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(totalLength),
129 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(avc->m.Length));
131 afs_MaybeWakeupTruncateDaemon();
132 ObtainWriteLock(&avc->lock,126);
134 #if defined(AFS_SGI_ENV)
138 * afs_xwrite handles setting m.Length
139 * and handles APPEND mode.
140 * Since we are called via strategy, we need to trim the write to
141 * the actual size of the file
143 osi_Assert(filePos <= avc->m.Length);
144 diff = avc->m.Length - filePos;
145 auio->afsio_resid = MIN(totalLength, diff);
146 totalLength = auio->afsio_resid;
149 if (aio & IO_APPEND) {
150 /* append mode, start it at the right spot */
151 #if defined(AFS_SUN56_ENV)
152 auio->uio_loffset = 0;
154 filePos = auio->afsio_offset = avc->m.Length;
158 * Note that we use startDate rather than calling osi_Time() here.
159 * This is to avoid counting lock-waiting time in file date (for ranlib).
161 avc->m.Date = startDate;
163 #if defined(AFS_HPUX_ENV) || defined(AFS_GFS_ENV)
164 #if defined(AFS_HPUX101_ENV)
165 if ((totalLength + filePos) >> 9 > (p_rlimit(u.u_procp))[RLIMIT_FSIZE].rlim_cur) {
168 if ((totalLength + filePos) >> 9 > u.u_rlimit[RLIMIT_FSIZE].rlim_cur) {
170 if (totalLength + filePos > u.u_rlimit[RLIMIT_FSIZE].rlim_cur) {
174 ReleaseWriteLock(&avc->lock);
178 #ifdef AFS_VM_RDWR_ENV
180 * If write is implemented via VM, afs_FakeOpen() is called from the
181 * high-level write op.
183 if (avc->execsOrWriters <= 0) {
184 printf("WARNING: afs_ufswr vp=%x, exOrW=%d\n", avc, avc->execsOrWriters);
189 avc->states |= CDirty;
190 tvec = (struct iovec *) osi_AllocSmallSpace(sizeof(struct iovec));
191 while (totalLength > 0) {
192 /* Read the cached info. If we call GetDCache while the cache
193 * truncate daemon is running we risk overflowing the disk cache.
194 * Instead we check for an existing cache slot. If we cannot
195 * find an existing slot we wait for the cache to drain
196 * before calling GetDCache.
199 tdc = afs_FindDCache(avc, filePos);
200 } else if (afs_blocksUsed > (CM_WAITFORDRAINPCT*afs_cacheBlocks)/100) {
201 tdc = afs_FindDCache(avc, filePos);
203 if (!hsame(tdc->f.versionNo, avc->m.DataVersion) ||
204 (tdc->flags & DFFetching)) {
210 afs_MaybeWakeupTruncateDaemon();
211 while (afs_blocksUsed >
212 (CM_WAITFORDRAINPCT*afs_cacheBlocks)/100) {
213 ReleaseWriteLock(&avc->lock);
214 if (afs_blocksUsed - afs_blocksDiscarded >
215 (CM_WAITFORDRAINPCT*afs_cacheBlocks)/100) {
216 afs_WaitForCacheDrain = 1;
217 afs_osi_Sleep(&afs_WaitForCacheDrain);
219 afs_MaybeFreeDiscardedDCache();
220 afs_MaybeWakeupTruncateDaemon();
221 ObtainWriteLock(&avc->lock,506);
223 avc->states |= CDirty;
224 tdc = afs_GetDCache(avc, filePos, &treq, &offset, &len, 4);
227 tdc = afs_GetDCache(avc, filePos, &treq, &offset, &len, 4);
233 if (!(afs_indexFlags[tdc->index] & IFDataMod)) {
234 afs_stats_cmperf.cacheCurrDirtyChunks++;
235 afs_indexFlags[tdc->index] |= IFDataMod; /* so it doesn't disappear */
237 if (!(tdc->f.states & DWriting)) {
238 /* don't mark entry as mod if we don't have to */
239 tdc->f.states |= DWriting;
240 tdc->flags |= DFEntryMod;
242 len = totalLength; /* write this amount by default */
243 offset = filePos - AFS_CHUNKTOBASE(tdc->f.chunk);
244 max = AFS_CHUNKTOSIZE(tdc->f.chunk); /* max size of this chunk */
245 if (max <= len + offset) { /*if we'd go past the end of this chunk */
246 /* it won't all fit in this chunk, so write as much
250 /* mung uio structure to be right for this transfer */
251 afsio_copy(auio, &tuio, tvec);
253 afsio_trim(&tuio, trimlen);
254 tuio.afsio_offset = offset;
256 code = afs_MemWriteUIO(tdc->f.inode, &tuio);
259 ZapDCE(tdc); /* bad data */
260 afs_MemCacheTruncate(tdc->f.inode, 0);
261 afs_stats_cmperf.cacheCurrDirtyChunks--;
262 afs_indexFlags[tdc->index] &= ~IFDataMod; /* so it does disappear */
266 /* otherwise we've written some, fixup length, etc and continue with next seg */
267 len = len - tuio.afsio_resid; /* compute amount really transferred */
269 afsio_skip(auio, tlen); /* advance auio over data written */
270 /* compute new file size */
271 if (offset + len > tdc->f.chunkBytes) {
272 afs_int32 toffset = offset+len;
273 afs_AdjustSize(tdc, toffset);
276 transferLength += len;
278 #if defined(AFS_SGI_ENV)
279 /* afs_xwrite handles setting m.Length */
280 osi_Assert(filePos <= avc->m.Length);
282 if (filePos > avc->m.Length)
283 avc->m.Length = filePos;
285 #ifndef AFS_VM_RDWR_ENV
287 * If write is implemented via VM, afs_DoPartialWrite() is called from
288 * the high-level write op.
291 code = afs_DoPartialWrite(avc, &treq);
301 #ifndef AFS_VM_RDWR_ENV
302 afs_FakeClose(avc, acred);
304 if (error && !avc->vc_error)
305 avc->vc_error = error;
307 ReleaseWriteLock(&avc->lock);
308 osi_FreeSmallSpace(tvec);
310 /* next, on GFS systems, we update g_size so that lseek's relative to EOF will
311 work. GFS is truly a poorly-designed interface! */
312 afs_gfshack((struct gnode *) avc);
314 error = afs_CheckCode(error, &treq, 6);
319 /* called on writes */
320 afs_UFSWrite(avc, auio, aio, acred, noLock)
321 register struct vcache *avc;
324 struct AFS_UCRED *acred;
326 afs_size_t totalLength;
327 afs_size_t transferLength;
329 afs_size_t offset, len;
334 register struct dcache *tdc;
340 struct iovec *tvec; /* again, should have define */
341 struct osi_file *tfile;
342 register afs_int32 code;
344 struct vrequest treq;
346 AFS_STATCNT(afs_UFSWrite);
348 return avc->vc_error;
350 startDate = osi_Time();
351 if (code = afs_InitReq(&treq, acred)) return code;
352 /* otherwise we read */
353 totalLength = auio->afsio_resid;
354 filePos = auio->afsio_offset;
357 afs_Trace4(afs_iclSetp, CM_TRACE_WRITE, ICL_TYPE_POINTER, avc,
358 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(filePos),
359 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(totalLength),
360 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(avc->m.Length));
362 afs_MaybeWakeupTruncateDaemon();
363 ObtainWriteLock(&avc->lock,556);
365 #if defined(AFS_SGI_ENV)
369 * afs_xwrite handles setting m.Length
370 * and handles APPEND mode.
371 * Since we are called via strategy, we need to trim the write to
372 * the actual size of the file
374 osi_Assert(filePos <= avc->m.Length);
375 diff = avc->m.Length - filePos;
376 auio->afsio_resid = MIN(totalLength, diff);
377 totalLength = auio->afsio_resid;
380 if (aio & IO_APPEND) {
381 /* append mode, start it at the right spot */
382 #if defined(AFS_SUN56_ENV)
383 auio->uio_loffset = 0;
385 filePos = auio->afsio_offset = avc->m.Length;
389 * Note that we use startDate rather than calling osi_Time() here.
390 * This is to avoid counting lock-waiting time in file date (for ranlib).
392 avc->m.Date = startDate;
394 #if defined(AFS_HPUX_ENV) || defined(AFS_GFS_ENV)
395 #if defined(AFS_HPUX101_ENV)
396 if ((totalLength + filePos) >> 9 > p_rlimit(u.u_procp)[RLIMIT_FSIZE].rlim_cur) {
399 if ((totalLength + filePos) >> 9 > u.u_rlimit[RLIMIT_FSIZE].rlim_cur) {
401 if (totalLength + filePos > 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 printf("WARNING: afs_ufswr vp=%x, exOrW=%d\n", avc, avc->execsOrWriters);
420 avc->states |= CDirty;
421 tvec = (struct iovec *) osi_AllocSmallSpace(sizeof(struct iovec));
422 while (totalLength > 0) {
423 /* read the cached info */
425 tdc = afs_FindDCache(avc, filePos);
426 } else if (afs_blocksUsed > (CM_WAITFORDRAINPCT*afs_cacheBlocks)/100) {
427 tdc = afs_FindDCache(avc, filePos);
429 if (!hsame(tdc->f.versionNo, avc->m.DataVersion) ||
430 (tdc->flags & DFFetching)) {
436 afs_MaybeWakeupTruncateDaemon();
437 while (afs_blocksUsed >
438 (CM_WAITFORDRAINPCT*afs_cacheBlocks)/100) {
439 ReleaseWriteLock(&avc->lock);
440 if (afs_blocksUsed - afs_blocksDiscarded >
441 (CM_WAITFORDRAINPCT*afs_cacheBlocks)/100) {
442 afs_WaitForCacheDrain = 1;
443 afs_osi_Sleep(&afs_WaitForCacheDrain);
445 afs_MaybeFreeDiscardedDCache();
446 afs_MaybeWakeupTruncateDaemon();
447 ObtainWriteLock(&avc->lock,509);
449 avc->states |= CDirty;
450 tdc = afs_GetDCache(avc, filePos, &treq, &offset, &len, 4);
453 tdc = afs_GetDCache(avc, filePos, &treq, &offset, &len, 4);
459 if (!(afs_indexFlags[tdc->index] & IFDataMod)) {
460 afs_stats_cmperf.cacheCurrDirtyChunks++;
461 afs_indexFlags[tdc->index] |= IFDataMod; /* so it doesn't disappear */
463 if (!(tdc->f.states & DWriting)) {
464 /* don't mark entry as mod if we don't have to */
465 tdc->f.states |= DWriting;
466 tdc->flags |= DFEntryMod;
468 tfile = (struct osi_file *)osi_UFSOpen(tdc->f.inode);
469 len = totalLength; /* write this amount by default */
470 offset = filePos - AFS_CHUNKTOBASE(tdc->f.chunk);
471 max = AFS_CHUNKTOSIZE(tdc->f.chunk); /* max size of this chunk */
472 if (max <= len + offset) { /*if we'd go past the end of this chunk */
473 /* it won't all fit in this chunk, so write as much
477 /* mung uio structure to be right for this transfer */
478 afsio_copy(auio, &tuio, tvec);
480 afsio_trim(&tuio, trimlen);
481 tuio.afsio_offset = offset;
485 code = VNOP_RDWR(tfile->vnode, UIO_WRITE, FWRITE, &tuio, NULL, NULL, NULL, &afs_osi_cred);
489 code = VNOP_RDWR(tfile->vnode, UIO_WRITE, FWRITE, &tuio, NULL, NULL);
491 code = VNOP_RDWR(tfile->vnode, UIO_WRITE, FWRITE, (off_t)&offset, &tuio, NULL, NULL, -1);
493 #endif /* AFS_AIX41_ENV */
494 #else /* AFS_AIX_ENV */
497 VOP_RWLOCK(tfile->vnode, 1);
498 code = VOP_WRITE(tfile->vnode, &tuio, 0, &afs_osi_cred);
499 VOP_RWUNLOCK(tfile->vnode, 1);
501 if (code == ENOSPC) afs_warnuser("\n\n\n*** Cache partition is full - decrease cachesize!!! ***\n\n\n");
503 #if defined(AFS_SGI_ENV)
505 avc->states |= CWritingUFS;
506 AFS_VOP_RWLOCK(tfile->vnode, VRWLOCK_WRITE);
507 AFS_VOP_WRITE(tfile->vnode, &tuio, IO_ISLOCKED, &afs_osi_cred, code);
508 AFS_VOP_RWUNLOCK(tfile->vnode, VRWLOCK_WRITE);
509 avc->states &= ~CWritingUFS;
514 struct ucred *tmpcred = u.u_cred;
515 u.u_cred = &afs_osi_cred;
516 tuio.uio_rw = UIO_WRITE;
518 VOP_WRITE(tfile->vnode, &tuio, 0, &afs_osi_cred, code);
522 #else /* AFS_OSF_ENV */
523 #if defined(AFS_HPUX100_ENV)
526 code = VOP_RDWR(tfile->vnode, &tuio, UIO_WRITE, 0, &afs_osi_cred);
531 tuio.uio_fpflags &= ~FSYNCIO; /* don't do sync io */
533 #if defined(AFS_LINUX20_ENV)
535 code = osi_file_uio_rdwr(tfile, &tuio, UIO_WRITE);
538 #if defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
540 VOP_LOCK(tfile->vnode, LK_EXCLUSIVE, current_proc());
541 code = VOP_WRITE(tfile->vnode, &tuio, 0, &afs_osi_cred);
542 VOP_UNLOCK(tfile->vnode, 0, current_proc());
545 code = VOP_RDWR(tfile->vnode, &tuio, UIO_WRITE, 0, &afs_osi_cred);
546 #endif /* AFS_DARWIN_ENV || AFS_FBSD_ENV */
547 #endif /* AFS_LINUX20_ENV */
548 #endif /* AFS_HPUX100_ENV */
549 #endif /* AFS_OSF_ENV */
550 #endif /* AFS_SGI_ENV */
551 #endif /* AFS_SUN5_ENV */
552 #endif /* AFS_AIX41_ENV */
555 ZapDCE(tdc); /* bad data */
556 osi_UFSTruncate(tfile,0); /* fake truncate the segment */
557 afs_AdjustSize(tdc, 0); /* sets f.chunkSize to 0 */
558 afs_stats_cmperf.cacheCurrDirtyChunks--;
559 afs_indexFlags[tdc->index] &= ~IFDataMod; /* so it does disappear */
561 afs_CFileClose(tfile);
564 /* otherwise we've written some, fixup length, etc and continue with next seg */
565 len = len - tuio.afsio_resid; /* compute amount really transferred */
567 afsio_skip(auio, tlen); /* advance auio over data written */
568 /* compute new file size */
569 if (offset + len > tdc->f.chunkBytes) {
570 afs_int32 toffset = offset+len;
571 afs_AdjustSize(tdc, toffset);
574 transferLength += len;
576 #if defined(AFS_SGI_ENV)
577 /* afs_xwrite handles setting m.Length */
578 osi_Assert(filePos <= avc->m.Length);
580 if (filePos > avc->m.Length) {
581 avc->m.Length = filePos;
585 #ifndef AFS_VM_RDWR_ENV
587 * If write is implemented via VM, afs_DoPartialWrite() is called from
588 * the high-level write op.
591 code = afs_DoPartialWrite(avc, &treq);
601 #ifndef AFS_VM_RDWR_ENV
602 afs_FakeClose(avc, acred);
604 error = afs_CheckCode(error, &treq, 7);
605 /* This set is here so we get the CheckCode. */
606 if (error && !avc->vc_error)
607 avc->vc_error = error;
609 ReleaseWriteLock(&avc->lock);
610 osi_FreeSmallSpace(tvec);
612 /* next, on GFS systems, we update g_size so that lseek's relative to EOF will
613 work. GFS is truly a poorly-designed interface! */
614 afs_gfshack((struct gnode *) avc);
616 #ifndef AFS_VM_RDWR_ENV
618 * If write is implemented via VM, afs_fsync() is called from the high-level
621 #if defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
622 if (noLock && (aio & IO_SYNC)) {
625 /* On hpux on synchronous writes syncio will be set to IO_SYNC. If
626 * we're doing them because the file was opened with O_SYNCIO specified,
627 * we have to look in the u area. No single mechanism here!!
629 if (noLock && ((aio & IO_SYNC) | (auio->uio_fpflags & FSYNCIO))) {
631 if (noLock && (aio & FSYNC)) {
634 if (!AFS_NFSXLATORREQ(acred))
635 afs_fsync(avc, acred);
641 /* do partial write if we're low on unmodified chunks */
642 afs_DoPartialWrite(avc, areq)
643 register struct vcache *avc;
644 struct vrequest *areq; {
645 register afs_int32 code;
647 if (afs_stats_cmperf.cacheCurrDirtyChunks <= afs_stats_cmperf.cacheMaxDirtyChunks)
648 return 0; /* nothing to do */
649 /* otherwise, call afs_StoreDCache (later try to do this async, if possible) */
650 afs_Trace2(afs_iclSetp, CM_TRACE_PARTIALWRITE, ICL_TYPE_POINTER, avc,
651 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(avc->m.Length));
652 #if defined(AFS_SUN5_ENV)
653 code = afs_StoreAllSegments(avc, areq, AFS_ASYNC | AFS_VMSYNC_INVAL);
655 code = afs_StoreAllSegments(avc, areq, AFS_ASYNC);
662 #if !defined (AFS_AIX_ENV) && !defined (AFS_HPUX_ENV) && !defined (AFS_SUN5_ENV) && !defined(AFS_SGI_ENV) && !defined(AFS_LINUX20_ENV) && !defined(AFS_DARWIN_ENV) && !defined(AFS_FBSD_ENV)
664 #define vno_close(X) vn_close((X), 0, NOCRED)
665 #elif defined(AFS_DUX40_ENV)
666 #define vno_close vn_close
668 /* We don't need this for AIX since:
669 * (1) aix doesn't use fileops and it call close directly intead
670 * (where the unlocking should be done) and
671 * (2) temporarily, the aix lockf isn't supported yet.
673 * this stupid routine is used to release the flocks held on a
674 * particular file descriptor. Sun doesn't pass file descr. info
675 * through to the vnode layer, and yet we must unlock flocked files
676 * on the *appropriate* (not first, as in System V) close call. Thus
678 * How does this code get invoked? The afs AFS_FLOCK plugs in the new afs
679 * file ops structure into any afs file when it gets flocked.
680 * N.B: Intercepting close syscall doesn't trap aborts or exit system
684 register struct file *afd; {
685 struct vrequest treq;
686 register struct vcache *tvc;
691 AFS_STATCNT(afs_closex);
692 /* setup the credentials */
693 if (code = afs_InitReq(&treq, u.u_cred)) return code;
696 /* we're the last one. If we're an AFS vnode, clear the flags,
697 * close the file and release the lock when done. Otherwise, just
698 * let the regular close code work. */
699 if (afd->f_type == DTYPE_VNODE) {
700 tvc = (struct vcache *) afd->f_data;
701 if (IsAfsVnode((struct vnode *)tvc)) {
702 VN_HOLD((struct vnode *) tvc);
703 flags = afd->f_flag & (FSHLOCK | FEXLOCK);
704 afd->f_flag &= ~(FSHLOCK | FEXLOCK);
705 code = vno_close(afd);
707 #if defined(AFS_SGI_ENV) || defined(AFS_OSF_ENV) || defined(AFS_SUN_ENV) && !defined(AFS_SUN5_ENV)
708 HandleFlock(tvc, LOCK_UN, &treq,
709 u.u_procp->p_pid, 1/*onlymine*/);
711 HandleFlock(tvc, LOCK_UN, &treq, 0, 1/*onlymine*/);
714 grele((struct gnode *) tvc);
716 AFS_RELE((struct vnode *) tvc);
721 /* now, if close not done, do it */
723 code = vno_close(afd);
725 return code; /* return code from vnode layer */
730 /* handle any closing cleanup stuff */
732 afs_close(OSI_VC_ARG(avc), aflags, lastclose,
733 #if !defined(AFS_SGI65_ENV)
737 #if defined(AFS_SGI64_ENV) && !defined(AFS_SGI65_ENV)
741 lastclose_t lastclose;
742 #if !defined(AFS_SGI65_ENV)
744 #if defined(AFS_SGI64_ENV)
749 #if defined(AFS_SUN_ENV) || defined(AFS_SUN5_ENV)
751 afs_close(OSI_VC_ARG(avc), aflags, count, offset, acred)
754 afs_close(OSI_VC_ARG(avc), aflags, count, acred)
758 afs_close(OSI_VC_ARG(avc), aflags, acred)
763 struct AFS_UCRED *acred;
765 register afs_int32 code, initreq=0;
766 register struct brequest *tb;
767 struct vrequest treq;
773 AFS_STATCNT(afs_close);
774 afs_Trace2(afs_iclSetp, CM_TRACE_CLOSE, ICL_TYPE_POINTER, avc,
775 ICL_TYPE_INT32, aflags);
777 if (avc->flockCount) {
778 if (code = afs_InitReq(&treq, acred)) return code;
780 HandleFlock(avc, LOCK_UN, &treq, 0, 1/*onlymine*/);
783 #if defined(AFS_SGI_ENV)
787 #if defined(AFS_SUN_ENV) || defined(AFS_SGI_ENV)
789 /* The vfs layer may call this repeatedly with higher "count"; only on the last close (i.e. count = 1) we should actually proceed with the close. */
797 if (code = afs_InitReq(&treq, acred)) return code;
802 #if defined(AFS_SGI_ENV)
803 /* unlock any locks for pid - could be wrong for child .. */
804 AFS_RWLOCK((vnode_t *)avc, VRWLOCK_WRITE);
806 get_current_flid(&flid);
807 cleanlocks((vnode_t *)avc, flid.fl_pid, flid.fl_sysid);
808 HandleFlock(avc, LOCK_UN, &treq, flid.fl_pid, 1/*onlymine*/);
811 cleanlocks((vnode_t *)avc, flp);
812 #else /* AFS_SGI64_ENV */
813 cleanlocks((vnode_t *)avc, u.u_procp->p_epid, u.u_procp->p_sysid);
814 #endif /* AFS_SGI64_ENV */
815 HandleFlock(avc, LOCK_UN, &treq, OSI_GET_CURRENT_PID(), 1/*onlymine*/);
816 #endif /* AFS_SGI65_ENV */
817 /* afs_chkpgoob will drop and re-acquire the global lock. */
818 afs_chkpgoob(&avc->v, btoc(avc->m.Length));
820 if (avc->flockCount) { /* Release Lock */
821 #if defined(AFS_OSF_ENV) || defined(AFS_SUN_ENV)
822 HandleFlock(avc, LOCK_UN, &treq, u.u_procp->p_pid, 1/*onlymine*/);
824 HandleFlock(avc, LOCK_UN, &treq, 0, 1/*onlymine*/);
829 if (aflags & (FWRITE | FTRUNC)) {
831 /* do it yourself if daemons are all busy */
832 ObtainWriteLock(&avc->lock,124);
833 code = afs_StoreOnLastReference(avc, &treq);
834 ReleaseWriteLock(&avc->lock);
835 #if defined(AFS_SGI_ENV)
836 AFS_RWUNLOCK((vnode_t *)avc, VRWLOCK_WRITE);
840 #if defined(AFS_SGI_ENV)
841 AFS_RWUNLOCK((vnode_t *)avc, VRWLOCK_WRITE);
843 /* at least one daemon is idle, so ask it to do the store.
844 Also, note that we don't lock it any more... */
845 tb = afs_BQueue(BOP_STORE, avc, 0, 1, acred,
846 (afs_size_t) acred->cr_uid, (afs_size_t) 0,
847 (afs_size_t) 0, (afs_size_t) 0);
848 /* sleep waiting for the store to start, then retrieve error code */
849 while ((tb->flags & BUVALID) == 0) {
857 /* VNOVNODE is "acceptable" error code from close, since
858 may happen when deleting a file on another machine while
859 it is open here. We do the same for ENOENT since in afs_CheckCode we map VNOVNODE -> ENOENT */
860 if (code == VNOVNODE || code == ENOENT)
863 /* Ensure last closer gets the error. If another thread caused
864 * DoPartialWrite and this thread does not actually store the data,
865 * it may not see the quota error.
867 ObtainWriteLock(&avc->lock,406);
870 osi_ReleaseVM(avc, acred);
872 code = avc->vc_error;
875 ReleaseWriteLock(&avc->lock);
877 /* some codes merit specific complaint */
879 afs_warnuser("afs: failed to store file (network problems)\n");
882 else if (code == ENOSPC) {
883 afs_warnuser("afs: failed to store file (over quota or partition full)\n");
886 else if (code == ENOSPC) {
887 afs_warnuser("afs: failed to store file (partition full)\n");
889 else if (code == EDQUOT) {
890 afs_warnuser("afs: failed to store file (over quota)\n");
894 afs_warnuser("afs: failed to store file (%d)\n", code);
896 /* finally, we flush any text pages lying around here */
901 #if defined(AFS_SGI_ENV)
902 AFS_RWUNLOCK((vnode_t *)avc, VRWLOCK_WRITE);
903 osi_Assert(avc->opens > 0);
905 /* file open for read */
906 ObtainWriteLock(&avc->lock, 411);
909 osi_ReleaseVM(avc, acred);
911 code = avc->vc_error;
915 ReleaseWriteLock(&avc->lock);
918 if ((VREFCOUNT(avc) <= 2) && (avc->states & CUnlinked)) {
919 afs_remunlink(avc, 1); /* ignore any return code */
922 code = afs_CheckCode(code, &treq, 5);
929 afs_fsync(avc, fflags, acred, waitfor)
932 #else /* AFS_OSF_ENV */
933 #if defined(AFS_SGI_ENV) || defined(AFS_SUN53_ENV)
934 afs_fsync(OSI_VC_ARG(avc), flag, acred
940 afs_fsync(avc, acred)
944 struct AFS_UCRED *acred;
945 #if defined(AFS_SGI_ENV) || defined(AFS_SUN53_ENV)
952 register afs_int32 code;
953 struct vrequest treq;
957 return avc->vc_error;
959 #if defined(AFS_SUN5_ENV)
960 /* back out if called from NFS server */
961 if (curthread->t_flag & T_DONTPEND)
965 AFS_STATCNT(afs_fsync);
966 afs_Trace1(afs_iclSetp, CM_TRACE_FSYNC, ICL_TYPE_POINTER, avc);
967 if (code = afs_InitReq(&treq, acred)) return code;
969 #if defined(AFS_SGI_ENV)
970 AFS_RWLOCK((vnode_t *)avc, VRWLOCK_WRITE);
971 if (flag & FSYNC_INVAL)
972 osi_VM_FSyncInval(avc);
973 #endif /* AFS_SGI_ENV */
975 ObtainSharedLock(&avc->lock,18);
977 if (avc->execsOrWriters > 0) {
978 /* put the file back */
979 UpgradeSToWLock(&avc->lock,41);
980 code = afs_StoreAllSegments(avc, &treq, AFS_SYNC);
981 ConvertWToSLock(&avc->lock);
984 #if defined(AFS_SGI_ENV)
985 AFS_RWUNLOCK((vnode_t *)avc, VRWLOCK_WRITE);
986 if (code == VNOVNODE) {
987 /* syncing an unlinked file! - non-informative to pass an errno
988 * 102 (== VNOVNODE) to user
994 code = afs_CheckCode(code, &treq, 33);
995 ReleaseSharedLock(&avc->lock);