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(AFSTOV(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 if (tdc) ObtainWriteLock(&tdc->lock, 653);
201 } else if (afs_blocksUsed > (CM_WAITFORDRAINPCT*afs_cacheBlocks)/100) {
202 tdc = afs_FindDCache(avc, filePos);
204 ObtainWriteLock(&tdc->lock, 654);
205 if (!hsame(tdc->f.versionNo, avc->m.DataVersion) ||
206 (tdc->dflags & DFFetching)) {
207 ReleaseWriteLock(&tdc->lock);
213 afs_MaybeWakeupTruncateDaemon();
214 while (afs_blocksUsed >
215 (CM_WAITFORDRAINPCT*afs_cacheBlocks)/100) {
216 ReleaseWriteLock(&avc->lock);
217 if (afs_blocksUsed - afs_blocksDiscarded >
218 (CM_WAITFORDRAINPCT*afs_cacheBlocks)/100) {
219 afs_WaitForCacheDrain = 1;
220 afs_osi_Sleep(&afs_WaitForCacheDrain);
222 afs_MaybeFreeDiscardedDCache();
223 afs_MaybeWakeupTruncateDaemon();
224 ObtainWriteLock(&avc->lock,506);
226 avc->states |= CDirty;
227 tdc = afs_GetDCache(avc, filePos, &treq, &offset, &len, 4);
228 if (tdc) ObtainWriteLock(&tdc->lock, 655);
231 tdc = afs_GetDCache(avc, filePos, &treq, &offset, &len, 4);
232 if (tdc) ObtainWriteLock(&tdc->lock, 656);
238 if (!(afs_indexFlags[tdc->index] & IFDataMod)) {
239 afs_stats_cmperf.cacheCurrDirtyChunks++;
240 afs_indexFlags[tdc->index] |= IFDataMod; /* so it doesn't disappear */
242 if (!(tdc->f.states & DWriting)) {
243 /* don't mark entry as mod if we don't have to */
244 tdc->f.states |= DWriting;
245 tdc->dflags |= DFEntryMod;
247 len = totalLength; /* write this amount by default */
248 offset = filePos - AFS_CHUNKTOBASE(tdc->f.chunk);
249 max = AFS_CHUNKTOSIZE(tdc->f.chunk); /* max size of this chunk */
250 if (max <= len + offset) { /*if we'd go past the end of this chunk */
251 /* it won't all fit in this chunk, so write as much
255 /* mung uio structure to be right for this transfer */
256 afsio_copy(auio, &tuio, tvec);
258 afsio_trim(&tuio, trimlen);
259 tuio.afsio_offset = offset;
261 code = afs_MemWriteUIO(tdc->f.inode, &tuio);
263 void *mep; /* XXX in prototype world is struct memCacheEntry * */
265 ZapDCE(tdc); /* bad data */
266 mep = afs_MemCacheOpen(tdc->f.inode);
267 afs_MemCacheTruncate(mep, 0);
268 afs_MemCacheClose(mep);
269 afs_stats_cmperf.cacheCurrDirtyChunks--;
270 afs_indexFlags[tdc->index] &= ~IFDataMod; /* so it does disappear */
271 ReleaseWriteLock(&tdc->lock);
275 /* otherwise we've written some, fixup length, etc and continue with next seg */
276 len = len - tuio.afsio_resid; /* compute amount really transferred */
278 afsio_skip(auio, tlen); /* advance auio over data written */
279 /* compute new file size */
280 if (offset + len > tdc->f.chunkBytes) {
281 afs_int32 tlength = offset+len;
282 afs_AdjustSize(tdc, tlength);
285 transferLength += len;
287 #if defined(AFS_SGI_ENV)
288 /* afs_xwrite handles setting m.Length */
289 osi_Assert(filePos <= avc->m.Length);
291 if (filePos > avc->m.Length) {
292 afs_Trace4(afs_iclSetp, CM_TRACE_SETLENGTH,
293 ICL_TYPE_STRING, __FILE__,
294 ICL_TYPE_LONG, __LINE__,
295 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(avc->m.Length),
296 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(filePos));
297 avc->m.Length = filePos;
300 ReleaseWriteLock(&tdc->lock);
302 #if !defined(AFS_VM_RDWR_ENV) || defined(AFS_LINUX22_ENV)
304 * If write is implemented via VM, afs_DoPartialWrite() is called from
305 * the high-level write op.
308 code = afs_DoPartialWrite(avc, &treq);
316 #ifndef AFS_VM_RDWR_ENV
317 afs_FakeClose(avc, acred);
319 if (error && !avc->vc_error)
320 avc->vc_error = error;
322 ReleaseWriteLock(&avc->lock);
323 osi_FreeSmallSpace(tvec);
325 /* next, on GFS systems, we update g_size so that lseek's relative to EOF will
326 work. GFS is truly a poorly-designed interface! */
327 afs_gfshack((struct gnode *) avc);
329 error = afs_CheckCode(error, &treq, 6);
334 /* called on writes */
335 afs_UFSWrite(avc, auio, aio, acred, noLock)
336 register struct vcache *avc;
339 struct AFS_UCRED *acred;
341 afs_size_t totalLength;
342 afs_size_t transferLength;
344 afs_size_t offset, len;
349 register struct dcache *tdc;
355 struct iovec *tvec; /* again, should have define */
356 struct osi_file *tfile;
357 register afs_int32 code;
359 struct vrequest treq;
361 AFS_STATCNT(afs_UFSWrite);
363 return avc->vc_error;
365 startDate = osi_Time();
366 if (code = afs_InitReq(&treq, acred)) return code;
367 /* otherwise we read */
368 totalLength = auio->afsio_resid;
369 filePos = auio->afsio_offset;
372 afs_Trace4(afs_iclSetp, CM_TRACE_WRITE, ICL_TYPE_POINTER, avc,
373 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(filePos),
374 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(totalLength),
375 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(avc->m.Length));
377 afs_MaybeWakeupTruncateDaemon();
378 ObtainWriteLock(&avc->lock,556);
380 #if defined(AFS_SGI_ENV)
384 * afs_xwrite handles setting m.Length
385 * and handles APPEND mode.
386 * Since we are called via strategy, we need to trim the write to
387 * the actual size of the file
389 osi_Assert(filePos <= avc->m.Length);
390 diff = avc->m.Length - filePos;
391 auio->afsio_resid = MIN(totalLength, diff);
392 totalLength = auio->afsio_resid;
395 if (aio & IO_APPEND) {
396 /* append mode, start it at the right spot */
397 #if defined(AFS_SUN56_ENV)
398 auio->uio_loffset = 0;
400 filePos = auio->afsio_offset = avc->m.Length;
404 * Note that we use startDate rather than calling osi_Time() here.
405 * This is to avoid counting lock-waiting time in file date (for ranlib).
407 avc->m.Date = startDate;
409 #if defined(AFS_HPUX_ENV) || defined(AFS_GFS_ENV)
410 #if defined(AFS_HPUX101_ENV)
411 if ((totalLength + filePos) >> 9 > p_rlimit(u.u_procp)[RLIMIT_FSIZE].rlim_cur) {
414 if ((totalLength + filePos) >> 9 > u.u_rlimit[RLIMIT_FSIZE].rlim_cur) {
416 if (totalLength + filePos > u.u_rlimit[RLIMIT_FSIZE].rlim_cur) {
420 ReleaseWriteLock(&avc->lock);
424 #ifdef AFS_VM_RDWR_ENV
426 * If write is implemented via VM, afs_FakeOpen() is called from the
427 * high-level write op.
429 if (avc->execsOrWriters <= 0) {
430 printf("WARNING: afs_ufswr vp=%x, exOrW=%d\n", avc, avc->execsOrWriters);
435 avc->states |= CDirty;
436 tvec = (struct iovec *) osi_AllocSmallSpace(sizeof(struct iovec));
437 while (totalLength > 0) {
438 /* read the cached info */
440 tdc = afs_FindDCache(avc, filePos);
441 if (tdc) ObtainWriteLock(&tdc->lock, 657);
442 } else if (afs_blocksUsed > (CM_WAITFORDRAINPCT*afs_cacheBlocks)/100) {
443 tdc = afs_FindDCache(avc, filePos);
445 ObtainWriteLock(&tdc->lock, 658);
446 if (!hsame(tdc->f.versionNo, avc->m.DataVersion) ||
447 (tdc->dflags & DFFetching)) {
448 ReleaseWriteLock(&tdc->lock);
454 afs_MaybeWakeupTruncateDaemon();
455 while (afs_blocksUsed >
456 (CM_WAITFORDRAINPCT*afs_cacheBlocks)/100) {
457 ReleaseWriteLock(&avc->lock);
458 if (afs_blocksUsed - afs_blocksDiscarded >
459 (CM_WAITFORDRAINPCT*afs_cacheBlocks)/100) {
460 afs_WaitForCacheDrain = 1;
461 afs_osi_Sleep(&afs_WaitForCacheDrain);
463 afs_MaybeFreeDiscardedDCache();
464 afs_MaybeWakeupTruncateDaemon();
465 ObtainWriteLock(&avc->lock,509);
467 avc->states |= CDirty;
468 tdc = afs_GetDCache(avc, filePos, &treq, &offset, &len, 4);
469 if (tdc) ObtainWriteLock(&tdc->lock, 659);
472 tdc = afs_GetDCache(avc, filePos, &treq, &offset, &len, 4);
473 if (tdc) ObtainWriteLock(&tdc->lock, 660);
479 if (!(afs_indexFlags[tdc->index] & IFDataMod)) {
480 afs_stats_cmperf.cacheCurrDirtyChunks++;
481 afs_indexFlags[tdc->index] |= IFDataMod; /* so it doesn't disappear */
483 if (!(tdc->f.states & DWriting)) {
484 /* don't mark entry as mod if we don't have to */
485 tdc->f.states |= DWriting;
486 tdc->dflags |= DFEntryMod;
488 tfile = (struct osi_file *)osi_UFSOpen(tdc->f.inode);
489 len = totalLength; /* write this amount by default */
490 offset = filePos - AFS_CHUNKTOBASE(tdc->f.chunk);
491 max = AFS_CHUNKTOSIZE(tdc->f.chunk); /* max size of this chunk */
492 if (max <= len + offset) { /*if we'd go past the end of this chunk */
493 /* it won't all fit in this chunk, so write as much
497 /* mung uio structure to be right for this transfer */
498 afsio_copy(auio, &tuio, tvec);
500 afsio_trim(&tuio, trimlen);
501 tuio.afsio_offset = offset;
505 code = VNOP_RDWR(tfile->vnode, UIO_WRITE, FWRITE, &tuio, NULL, NULL, NULL, &afs_osi_cred);
509 code = VNOP_RDWR(tfile->vnode, UIO_WRITE, FWRITE, &tuio, NULL, NULL);
511 code = VNOP_RDWR(tfile->vnode, UIO_WRITE, FWRITE, (off_t)&offset, &tuio, NULL, NULL, -1);
513 #endif /* AFS_AIX41_ENV */
514 #else /* AFS_AIX_ENV */
517 VOP_RWLOCK(tfile->vnode, 1);
518 code = VOP_WRITE(tfile->vnode, &tuio, 0, &afs_osi_cred);
519 VOP_RWUNLOCK(tfile->vnode, 1);
521 if (code == ENOSPC) afs_warnuser("\n\n\n*** Cache partition is full - decrease cachesize!!! ***\n\n\n");
523 #if defined(AFS_SGI_ENV)
525 avc->states |= CWritingUFS;
526 AFS_VOP_RWLOCK(tfile->vnode, VRWLOCK_WRITE);
527 AFS_VOP_WRITE(tfile->vnode, &tuio, IO_ISLOCKED, &afs_osi_cred, code);
528 AFS_VOP_RWUNLOCK(tfile->vnode, VRWLOCK_WRITE);
529 avc->states &= ~CWritingUFS;
534 struct ucred *tmpcred = u.u_cred;
535 u.u_cred = &afs_osi_cred;
536 tuio.uio_rw = UIO_WRITE;
538 VOP_WRITE(tfile->vnode, &tuio, 0, &afs_osi_cred, code);
542 #else /* AFS_OSF_ENV */
543 #if defined(AFS_HPUX100_ENV)
546 code = VOP_RDWR(tfile->vnode, &tuio, UIO_WRITE, 0, &afs_osi_cred);
551 tuio.uio_fpflags &= ~FSYNCIO; /* don't do sync io */
553 #if defined(AFS_LINUX20_ENV)
555 code = osi_file_uio_rdwr(tfile, &tuio, UIO_WRITE);
558 #if defined(AFS_DARWIN_ENV)
560 VOP_LOCK(tfile->vnode, LK_EXCLUSIVE, current_proc());
561 code = VOP_WRITE(tfile->vnode, &tuio, 0, &afs_osi_cred);
562 VOP_UNLOCK(tfile->vnode, 0, current_proc());
565 #if defined(AFS_FBSD_ENV)
567 VOP_LOCK(tfile->vnode, LK_EXCLUSIVE, curproc);
568 code = VOP_WRITE(tfile->vnode, &tuio, 0, &afs_osi_cred);
569 VOP_UNLOCK(tfile->vnode, 0, curproc);
572 code = VOP_RDWR(tfile->vnode, &tuio, UIO_WRITE, 0, &afs_osi_cred);
573 #endif /* AFS_FBSD_ENV */
574 #endif /* AFS_DARWIN_ENV */
575 #endif /* AFS_LINUX20_ENV */
576 #endif /* AFS_HPUX100_ENV */
577 #endif /* AFS_OSF_ENV */
578 #endif /* AFS_SGI_ENV */
579 #endif /* AFS_SUN5_ENV */
580 #endif /* AFS_AIX41_ENV */
583 ZapDCE(tdc); /* bad data */
584 osi_UFSTruncate(tfile,0); /* fake truncate the segment */
585 afs_AdjustSize(tdc, 0); /* sets f.chunkSize to 0 */
586 afs_stats_cmperf.cacheCurrDirtyChunks--;
587 afs_indexFlags[tdc->index] &= ~IFDataMod; /* so it does disappear */
588 afs_CFileClose(tfile);
589 ReleaseWriteLock(&tdc->lock);
593 /* otherwise we've written some, fixup length, etc and continue with next seg */
594 len = len - tuio.afsio_resid; /* compute amount really transferred */
596 afsio_skip(auio, tlen); /* advance auio over data written */
597 /* compute new file size */
598 if (offset + len > tdc->f.chunkBytes) {
599 afs_int32 tlength = offset+len;
600 afs_AdjustSize(tdc, tlength);
603 transferLength += len;
605 #if defined(AFS_SGI_ENV)
606 /* afs_xwrite handles setting m.Length */
607 osi_Assert(filePos <= avc->m.Length);
609 if (filePos > avc->m.Length) {
610 afs_Trace4(afs_iclSetp, CM_TRACE_SETLENGTH,
611 ICL_TYPE_STRING, __FILE__,
612 ICL_TYPE_LONG, __LINE__,
613 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(avc->m.Length),
614 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(filePos));
615 avc->m.Length = filePos;
619 ReleaseWriteLock(&tdc->lock);
621 #if !defined(AFS_VM_RDWR_ENV) || defined(AFS_LINUX22_ENV)
623 * If write is implemented via VM, afs_DoPartialWrite() is called from
624 * the high-level write op.
627 code = afs_DoPartialWrite(avc, &treq);
635 #ifndef AFS_VM_RDWR_ENV
636 afs_FakeClose(avc, acred);
638 error = afs_CheckCode(error, &treq, 7);
639 /* This set is here so we get the CheckCode. */
640 if (error && !avc->vc_error)
641 avc->vc_error = error;
643 ReleaseWriteLock(&avc->lock);
644 osi_FreeSmallSpace(tvec);
646 /* next, on GFS systems, we update g_size so that lseek's relative to EOF will
647 work. GFS is truly a poorly-designed interface! */
648 afs_gfshack((struct gnode *) avc);
650 #ifndef AFS_VM_RDWR_ENV
652 * If write is implemented via VM, afs_fsync() is called from the high-level
655 #if defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
656 if (noLock && (aio & IO_SYNC)) {
659 /* On hpux on synchronous writes syncio will be set to IO_SYNC. If
660 * we're doing them because the file was opened with O_SYNCIO specified,
661 * we have to look in the u area. No single mechanism here!!
663 if (noLock && ((aio & IO_SYNC) | (auio->uio_fpflags & FSYNCIO))) {
665 if (noLock && (aio & FSYNC)) {
668 if (!AFS_NFSXLATORREQ(acred))
669 afs_fsync(avc, acred);
675 /* do partial write if we're low on unmodified chunks */
676 afs_DoPartialWrite(avc, areq)
677 register struct vcache *avc;
678 struct vrequest *areq; {
679 register afs_int32 code;
681 if (afs_stats_cmperf.cacheCurrDirtyChunks <= afs_stats_cmperf.cacheMaxDirtyChunks)
682 return 0; /* nothing to do */
683 /* otherwise, call afs_StoreDCache (later try to do this async, if possible) */
684 afs_Trace2(afs_iclSetp, CM_TRACE_PARTIALWRITE, ICL_TYPE_POINTER, avc,
685 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(avc->m.Length));
686 #if defined(AFS_SUN5_ENV)
687 code = afs_StoreAllSegments(avc, areq, AFS_ASYNC | AFS_VMSYNC_INVAL);
689 code = afs_StoreAllSegments(avc, areq, AFS_ASYNC);
696 #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)
698 #define vno_close(X) vn_close((X), 0, NOCRED)
699 #elif defined(AFS_DUX40_ENV)
700 #define vno_close vn_close
702 /* We don't need this for AIX since:
703 * (1) aix doesn't use fileops and it call close directly intead
704 * (where the unlocking should be done) and
705 * (2) temporarily, the aix lockf isn't supported yet.
707 * this stupid routine is used to release the flocks held on a
708 * particular file descriptor. Sun doesn't pass file descr. info
709 * through to the vnode layer, and yet we must unlock flocked files
710 * on the *appropriate* (not first, as in System V) close call. Thus
712 * How does this code get invoked? The afs AFS_FLOCK plugs in the new afs
713 * file ops structure into any afs file when it gets flocked.
714 * N.B: Intercepting close syscall doesn't trap aborts or exit system
718 register struct file *afd; {
719 struct vrequest treq;
724 struct afs_fakestat_state fakestat;
726 AFS_STATCNT(afs_closex);
727 /* setup the credentials */
728 if (code = afs_InitReq(&treq, u.u_cred)) return code;
729 afs_InitFakeStat(&fakestat);
732 /* we're the last one. If we're an AFS vnode, clear the flags,
733 * close the file and release the lock when done. Otherwise, just
734 * let the regular close code work. */
735 if (afd->f_type == DTYPE_VNODE) {
736 tvc = VTOAFS(afd->f_data);
737 if (IsAfsVnode(AFSTOV(tvc))) {
738 code = afs_EvalFakeStat(&tvc, &fakestat, &treq);
740 afs_PutFakeStat(&fakestat);
743 VN_HOLD(AFSTOV(tvc));
744 flags = afd->f_flag & (FSHLOCK | FEXLOCK);
745 afd->f_flag &= ~(FSHLOCK | FEXLOCK);
746 code = vno_close(afd);
748 #if defined(AFS_SGI_ENV) || defined(AFS_OSF_ENV) || defined(AFS_SUN_ENV) && !defined(AFS_SUN5_ENV)
749 HandleFlock(tvc, LOCK_UN, &treq,
750 u.u_procp->p_pid, 1/*onlymine*/);
752 HandleFlock(tvc, LOCK_UN, &treq, 0, 1/*onlymine*/);
755 grele((struct gnode *) tvc);
757 AFS_RELE(AFSTOV(tvc));
762 /* now, if close not done, do it */
764 code = vno_close(afd);
766 afs_PutFakeStat(&fakestat);
767 return code; /* return code from vnode layer */
772 /* handle any closing cleanup stuff */
774 afs_close(OSI_VC_ARG(avc), aflags, lastclose,
775 #if !defined(AFS_SGI65_ENV)
779 #if defined(AFS_SGI64_ENV) && !defined(AFS_SGI65_ENV)
783 lastclose_t lastclose;
784 #if !defined(AFS_SGI65_ENV)
786 #if defined(AFS_SGI64_ENV)
791 #if defined(AFS_SUN_ENV) || defined(AFS_SUN5_ENV)
793 afs_close(OSI_VC_ARG(avc), aflags, count, offset, acred)
796 afs_close(OSI_VC_ARG(avc), aflags, count, acred)
800 afs_close(OSI_VC_ARG(avc), aflags, acred)
805 struct AFS_UCRED *acred;
807 register afs_int32 code;
808 register struct brequest *tb;
809 struct vrequest treq;
813 struct afs_fakestat_state fakestat;
816 AFS_STATCNT(afs_close);
817 afs_Trace2(afs_iclSetp, CM_TRACE_CLOSE, ICL_TYPE_POINTER, avc,
818 ICL_TYPE_INT32, aflags);
819 code = afs_InitReq(&treq, acred);
820 if (code) return code;
821 afs_InitFakeStat(&fakestat);
822 code = afs_EvalFakeStat(&avc, &fakestat, &treq);
824 afs_PutFakeStat(&fakestat);
828 if (avc->flockCount) {
829 HandleFlock(avc, LOCK_UN, &treq, 0, 1/*onlymine*/);
832 #if defined(AFS_SGI_ENV)
834 afs_PutFakeStat(&fakestat);
838 #if defined(AFS_SUN_ENV) || defined(AFS_SGI_ENV)
840 /* 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. */
841 afs_PutFakeStat(&fakestat);
847 #if defined(AFS_SGI_ENV)
848 /* unlock any locks for pid - could be wrong for child .. */
849 AFS_RWLOCK((vnode_t *)avc, VRWLOCK_WRITE);
851 get_current_flid(&flid);
852 cleanlocks((vnode_t *)avc, flid.fl_pid, flid.fl_sysid);
853 HandleFlock(avc, LOCK_UN, &treq, flid.fl_pid, 1/*onlymine*/);
856 cleanlocks((vnode_t *)avc, flp);
857 #else /* AFS_SGI64_ENV */
858 cleanlocks((vnode_t *)avc, u.u_procp->p_epid, u.u_procp->p_sysid);
859 #endif /* AFS_SGI64_ENV */
860 HandleFlock(avc, LOCK_UN, &treq, OSI_GET_CURRENT_PID(), 1/*onlymine*/);
861 #endif /* AFS_SGI65_ENV */
862 /* afs_chkpgoob will drop and re-acquire the global lock. */
863 afs_chkpgoob(&avc->v, btoc(avc->m.Length));
865 if (avc->flockCount) { /* Release Lock */
866 #if defined(AFS_OSF_ENV) || defined(AFS_SUN_ENV)
867 HandleFlock(avc, LOCK_UN, &treq, u.u_procp->p_pid, 1/*onlymine*/);
869 HandleFlock(avc, LOCK_UN, &treq, 0, 1/*onlymine*/);
874 if (aflags & (FWRITE | FTRUNC)) {
876 /* do it yourself if daemons are all busy */
877 ObtainWriteLock(&avc->lock,124);
878 code = afs_StoreOnLastReference(avc, &treq);
879 ReleaseWriteLock(&avc->lock);
880 #if defined(AFS_SGI_ENV)
881 AFS_RWUNLOCK((vnode_t *)avc, VRWLOCK_WRITE);
885 #if defined(AFS_SGI_ENV)
886 AFS_RWUNLOCK((vnode_t *)avc, VRWLOCK_WRITE);
888 /* at least one daemon is idle, so ask it to do the store.
889 Also, note that we don't lock it any more... */
890 tb = afs_BQueue(BOP_STORE, avc, 0, 1, acred,
891 (afs_size_t) acred->cr_uid, (afs_size_t) 0,
893 /* sleep waiting for the store to start, then retrieve error code */
894 while ((tb->flags & BUVALID) == 0) {
902 /* VNOVNODE is "acceptable" error code from close, since
903 may happen when deleting a file on another machine while
904 it is open here. We do the same for ENOENT since in afs_CheckCode we map VNOVNODE -> ENOENT */
905 if (code == VNOVNODE || code == ENOENT)
908 /* Ensure last closer gets the error. If another thread caused
909 * DoPartialWrite and this thread does not actually store the data,
910 * it may not see the quota error.
912 ObtainWriteLock(&avc->lock,406);
915 osi_ReleaseVM(avc, acred);
917 code = avc->vc_error;
920 ReleaseWriteLock(&avc->lock);
922 /* some codes merit specific complaint */
924 afs_warnuser("afs: failed to store file (network problems)\n");
927 else if (code == ENOSPC) {
928 afs_warnuser("afs: failed to store file (over quota or partition full)\n");
931 else if (code == ENOSPC) {
932 afs_warnuser("afs: failed to store file (partition full)\n");
934 else if (code == EDQUOT) {
935 afs_warnuser("afs: failed to store file (over quota)\n");
939 afs_warnuser("afs: failed to store file (%d)\n", code);
941 /* finally, we flush any text pages lying around here */
946 #if defined(AFS_SGI_ENV)
947 AFS_RWUNLOCK((vnode_t *)avc, VRWLOCK_WRITE);
948 osi_Assert(avc->opens > 0);
950 /* file open for read */
951 ObtainWriteLock(&avc->lock, 411);
954 osi_ReleaseVM(avc, acred);
956 code = avc->vc_error;
960 ReleaseWriteLock(&avc->lock);
963 if ((VREFCOUNT(avc) <= 2) && (avc->states & CUnlinked)) {
964 afs_remunlink(avc, 1); /* ignore any return code */
967 afs_PutFakeStat(&fakestat);
968 code = afs_CheckCode(code, &treq, 5);
975 afs_fsync(avc, fflags, acred, waitfor)
978 #else /* AFS_OSF_ENV */
979 #if defined(AFS_SGI_ENV) || defined(AFS_SUN53_ENV)
980 afs_fsync(OSI_VC_ARG(avc), flag, acred
986 afs_fsync(avc, acred)
990 struct AFS_UCRED *acred;
991 #if defined(AFS_SGI_ENV) || defined(AFS_SUN53_ENV)
998 register afs_int32 code;
999 struct vrequest treq;
1003 return avc->vc_error;
1005 #if defined(AFS_SUN5_ENV)
1006 /* back out if called from NFS server */
1007 if (curthread->t_flag & T_DONTPEND)
1011 AFS_STATCNT(afs_fsync);
1012 afs_Trace1(afs_iclSetp, CM_TRACE_FSYNC, ICL_TYPE_POINTER, avc);
1013 if (code = afs_InitReq(&treq, acred)) return code;
1015 #if defined(AFS_SGI_ENV)
1016 AFS_RWLOCK((vnode_t *)avc, VRWLOCK_WRITE);
1017 if (flag & FSYNC_INVAL)
1018 osi_VM_FSyncInval(avc);
1019 #endif /* AFS_SGI_ENV */
1021 ObtainSharedLock(&avc->lock,18);
1023 if (avc->execsOrWriters > 0) {
1024 /* put the file back */
1025 UpgradeSToWLock(&avc->lock,41);
1026 code = afs_StoreAllSegments(avc, &treq, AFS_SYNC);
1027 ConvertWToSLock(&avc->lock);
1030 #if defined(AFS_SGI_ENV)
1031 AFS_RWUNLOCK((vnode_t *)avc, VRWLOCK_WRITE);
1032 if (code == VNOVNODE) {
1033 /* syncing an unlinked file! - non-informative to pass an errno
1034 * 102 (== VNOVNODE) to user
1040 code = afs_CheckCode(code, &treq, 33);
1041 ReleaseSharedLock(&avc->lock);