c720f6ab8f46e606c66e035680389ee17ab4bd21
[openafs.git] / src / afs / VNOPS / afs_vnop_write.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
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
8  */
9
10 /*
11  * Implements:
12  * afs_write
13  * afs_UFSWriteUIO
14  * afs_StoreOnLastReference
15  * afs_close
16  * afs_fsync
17  */
18
19 #include <afsconfig.h>
20 #include "afs/param.h"
21
22
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"
29
30
31 extern unsigned char *afs_indexFlags;
32
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.
37  */
38 int
39 afs_StoreOnLastReference(struct vcache *avc,
40                          struct vrequest *treq)
41 {
42     int code = 0;
43
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         afs_ucred_t *cred;
51
52         avc->f.states &= ~CCore;
53 #if defined(AFS_SGI_ENV)
54         osi_Assert(avc->opens > 0 && avc->execsOrWriters > 0);
55 #endif
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 
59          * top level code.  */
60         avc->opens--;
61         avc->execsOrWriters--;
62         AFS_RELE(AFSTOV(avc));  /* VN_HOLD at set CCore(afs_FakeClose) */
63         cred = (afs_ucred_t *)avc->linkData;    /* "crheld" in afs_FakeClose */
64         crfree(cred);
65         avc->linkData = NULL;
66     }
67
68     if (!AFS_IS_DISCONNECTED) {
69         /* Connected. */
70
71         /* Now, send the file back.  Used to require 0 writers left, but now do
72          * it on every close for write, since two closes in a row are harmless
73          * since first will clean all chunks, and second will be noop.  Note that
74          * this will also save confusion when someone keeps a file open
75          * inadvertently, since with old system, writes to the server would never
76          * happen again.
77          */
78         code = afs_StoreAllSegments(avc, treq, AFS_LASTSTORE /*!sync-to-disk */ );
79         /*
80          * We have to do these after the above store in done: in some systems
81          * like aix they'll need to flush all the vm dirty pages to the disk via
82          * the strategy routine. During that all procedure (done under no avc
83          * locks) opens, refcounts would be zero, since it didn't reach the
84          * afs_{rd,wr} routines which means the vcache is a perfect candidate
85          * for flushing!
86          */
87      } else if (AFS_IS_DISCON_RW) {
88         afs_DisconAddDirty(avc, VDisconWriteClose, 0);
89      }          /* if not disconnected */
90
91 #if defined(AFS_SGI_ENV)
92     osi_Assert(avc->opens > 0 && avc->execsOrWriters > 0);
93 #endif
94
95     avc->opens--;
96     avc->execsOrWriters--;
97     return code;
98 }
99
100 int
101 afs_UFSWriteUIO(struct vcache *avc, afs_dcache_id_t *inode, struct uio *tuiop)
102 {
103     struct osi_file *tfile;
104     int code;
105
106     tfile = (struct osi_file *)osi_UFSOpen(inode);
107     if (!tfile)
108         return EIO;
109
110 #if defined(AFS_AIX41_ENV)
111     AFS_GUNLOCK();
112     code = VNOP_RDWR(tfile->vnode, UIO_WRITE, FWRITE, tuiop, NULL, NULL,
113                      NULL, afs_osi_credp);
114     AFS_GLOCK();
115 #elif defined(AFS_AIX32_ENV)
116     code = VNOP_RDWR(tfile->vnode, UIO_WRITE, FWRITE, tuiop, NULL, NULL);
117 #elif defined(AFS_AIX_ENV)
118     code = VNOP_RDWR(tfile->vnode, UIO_WRITE, FWRITE, (off_t) &offset,
119                      tuiop, NULL, NULL, -1);
120 #elif defined(AFS_SUN5_ENV)
121     AFS_GUNLOCK();
122 # ifdef AFS_SUN510_ENV
123     VOP_RWLOCK(tfile->vnode, 1, NULL);
124     code = VOP_WRITE(tfile->vnode, tuiop, 0, afs_osi_credp, NULL);
125     VOP_RWUNLOCK(tfile->vnode, 1, NULL);
126 # else
127     VOP_RWLOCK(tfile->vnode, 1);
128     code = VOP_WRITE(tfile->vnode, tuiop, 0, afs_osi_credp);
129     VOP_RWUNLOCK(tfile->vnode, 1);
130 # endif
131     AFS_GLOCK();
132     if (code == ENOSPC)
133         afs_WarnENOSPC();
134 #elif defined(AFS_SGI_ENV)
135     AFS_GUNLOCK();
136     avc->f.states |= CWritingUFS;
137     AFS_VOP_RWLOCK(tfile->vnode, VRWLOCK_WRITE);
138     AFS_VOP_WRITE(tfile->vnode, tuiop, IO_ISLOCKED, afs_osi_credp, code);
139     AFS_VOP_RWUNLOCK(tfile->vnode, VRWLOCK_WRITE);
140     avc->f.states &= ~CWritingUFS;
141     AFS_GLOCK();
142 #elif defined(AFS_HPUX100_ENV)
143     {
144         AFS_GUNLOCK();
145         code = VOP_RDWR(tfile->vnode, tuiop, UIO_WRITE, 0, afs_osi_credp);
146         AFS_GLOCK();
147     }
148 #elif defined(AFS_LINUX20_ENV)
149     AFS_GUNLOCK();
150     code = osi_rdwr(tfile, tuiop, UIO_WRITE);
151     AFS_GLOCK();
152 #elif defined(AFS_DARWIN80_ENV)
153     AFS_GUNLOCK();
154     code = VNOP_WRITE(tfile->vnode, tuiop, 0, afs_osi_ctxtp);
155     AFS_GLOCK();
156 #elif defined(AFS_DARWIN_ENV)
157     AFS_GUNLOCK();
158     VOP_LOCK(tfile->vnode, LK_EXCLUSIVE, current_proc());
159     code = VOP_WRITE(tfile->vnode, tuiop, 0, afs_osi_credp);
160     VOP_UNLOCK(tfile->vnode, 0, current_proc());
161     AFS_GLOCK();
162 #elif defined(AFS_FBSD80_ENV)
163     AFS_GUNLOCK();
164     VOP_LOCK(tfile->vnode, LK_EXCLUSIVE);
165     code = VOP_WRITE(tfile->vnode, tuiop, 0, afs_osi_credp);
166     VOP_UNLOCK(tfile->vnode, 0);
167     AFS_GLOCK();
168 #elif defined(AFS_FBSD_ENV)
169     AFS_GUNLOCK();
170     VOP_LOCK(tfile->vnode, LK_EXCLUSIVE, curthread);
171     code = VOP_WRITE(tfile->vnode, tuiop, 0, afs_osi_credp);
172     VOP_UNLOCK(tfile->vnode, 0, curthread);
173     AFS_GLOCK();
174 #elif defined(AFS_NBSD_ENV)
175     AFS_GUNLOCK();
176     VOP_LOCK(tfile->vnode, LK_EXCLUSIVE);
177     code = VOP_WRITE(tfile->vnode, tuiop, 0, afs_osi_credp);
178 #if defined(AFS_NBSD60_ENV)
179     VOP_UNLOCK(tfile->vnode);
180 #else
181     VOP_UNLOCK(tfile->vnode, 0);
182 #endif
183     AFS_GLOCK();
184 #elif defined(AFS_XBSD_ENV)
185     AFS_GUNLOCK();
186     VOP_LOCK(tfile->vnode, LK_EXCLUSIVE, curproc);
187     code = VOP_WRITE(tfile->vnode, tuiop, 0, afs_osi_credp);
188     VOP_UNLOCK(tfile->vnode, 0, curproc);
189     AFS_GLOCK();
190 #else
191 # ifdef AFS_HPUX_ENV
192     tuio.uio_fpflags &= ~FSYNCIO;       /* don't do sync io */
193 # endif
194     code = VOP_RDWR(tfile->vnode, tuiop, UIO_WRITE, 0, afs_osi_credp);
195 #endif
196     osi_UFSClose(tfile);
197
198     return code;
199 }
200
201 /* called on writes */
202 int
203 afs_write(struct vcache *avc, struct uio *auio, int aio,
204              afs_ucred_t *acred, int noLock)
205 {
206     afs_size_t totalLength;
207     afs_size_t transferLength;
208     afs_size_t filePos;
209     afs_size_t offset, len;
210     afs_int32 tlen;
211     afs_int32 trimlen;
212     afs_int32 startDate;
213     afs_int32 max;
214     struct dcache *tdc;
215 #ifdef _HIGHC_
216     volatile
217 #endif
218     afs_int32 error;
219 #if defined(AFS_FBSD_ENV) || defined(AFS_DFBSD_ENV)
220     struct vnode *vp = AFSTOV(avc);
221 #endif
222     struct uio *tuiop = NULL;
223     afs_int32 code;
224     struct vrequest *treq = NULL;
225
226     AFS_STATCNT(afs_write);
227
228     if (avc->vc_error)
229         return avc->vc_error;
230
231     if (AFS_IS_DISCONNECTED && !AFS_IS_DISCON_RW)
232         return ENETDOWN;
233     
234     startDate = osi_Time();
235     if ((code = afs_CreateReq(&treq, acred)))
236         return code;
237     /* otherwise we read */
238     totalLength = AFS_UIO_RESID(auio);
239     filePos = AFS_UIO_OFFSET(auio);
240     error = 0;
241     transferLength = 0;
242     afs_Trace4(afs_iclSetp, CM_TRACE_WRITE, ICL_TYPE_POINTER, avc,
243                ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(filePos), ICL_TYPE_OFFSET,
244                ICL_HANDLE_OFFSET(totalLength), ICL_TYPE_OFFSET,
245                ICL_HANDLE_OFFSET(avc->f.m.Length));
246     if (!noLock) {
247         afs_MaybeWakeupTruncateDaemon();
248         ObtainWriteLock(&avc->lock, 556);
249     }
250 #if defined(AFS_SGI_ENV)
251     {
252         off_t diff;
253         /*
254          * afs_xwrite handles setting m.Length
255          * and handles APPEND mode.
256          * Since we are called via strategy, we need to trim the write to
257          * the actual size of the file
258          */
259         osi_Assert(filePos <= avc->f.m.Length);
260         diff = avc->f.m.Length - filePos;
261         AFS_UIO_SETRESID(auio, MIN(totalLength, diff));
262         totalLength = AFS_UIO_RESID(auio);
263     }
264 #else
265     if (aio & IO_APPEND) {
266         /* append mode, start it at the right spot */
267 #if     defined(AFS_SUN5_ENV)
268         auio->uio_loffset = 0;
269 #endif
270         filePos = avc->f.m.Length;
271         AFS_UIO_SETOFFSET(auio, avc->f.m.Length);
272     }
273 #endif
274     /*
275      * Note that we use startDate rather than calling osi_Time() here.
276      * This is to avoid counting lock-waiting time in file date (for ranlib).
277      */
278     avc->f.m.Date = startDate;
279
280 #if     defined(AFS_HPUX_ENV)
281 #if     defined(AFS_HPUX101_ENV)
282     if ((totalLength + filePos) >> 9 >
283         p_rlimit(u.u_procp)[RLIMIT_FSIZE].rlim_cur) {
284 #else
285     if ((totalLength + filePos) >> 9 > u.u_rlimit[RLIMIT_FSIZE].rlim_cur) {
286 #endif
287         if (!noLock)
288             ReleaseWriteLock(&avc->lock);
289         afs_DestroyReq(treq);
290         return (EFBIG);
291     }
292 #endif
293 #if defined(AFS_VM_RDWR_ENV) && !defined(AFS_FAKEOPEN_ENV)
294     /*
295      * If write is implemented via VM, afs_FakeOpen() is called from the
296      * high-level write op.
297      */
298     if (avc->execsOrWriters <= 0) {
299         afs_warn("WARNING: afs_ufswr vcp=%lx, exOrW=%d\n", (unsigned long)avc,
300                avc->execsOrWriters);
301     }
302 #else
303     afs_FakeOpen(avc);
304 #endif
305
306     while (totalLength > 0) {
307         /*
308          * Note that we must set CDirty for every iteration of this loop.
309          * CDirty may get cleared below (such as during afs_DoPartialStore),
310          * but we're still writing to the file, so make sure CDirty is set
311          * here.
312          */
313         avc->f.states |= CDirty;
314
315         tdc = afs_ObtainDCacheForWriting(avc, filePos, totalLength, treq,
316                                          noLock);
317         if (!tdc) {
318             error = EIO;
319             break;
320         }
321         len = totalLength;      /* write this amount by default */
322         offset = filePos - AFS_CHUNKTOBASE(tdc->f.chunk);
323         max = AFS_CHUNKTOSIZE(tdc->f.chunk);    /* max size of this chunk */
324         if (max <= len + offset) {      /*if we'd go past the end of this chunk */
325             /* it won't all fit in this chunk, so write as much
326              * as will fit */
327             len = max - offset;
328         }
329
330         if (tuiop)
331             afsio_free(tuiop);
332         trimlen = len;
333         tuiop = afsio_partialcopy(auio, trimlen);
334         AFS_UIO_SETOFFSET(tuiop, offset);
335
336         code = (*(afs_cacheType->vwriteUIO))(avc, &tdc->f.inode, tuiop);
337
338         if (code) {
339             void *cfile;
340
341             error = code;
342             ZapDCE(tdc);        /* bad data */
343             cfile = afs_CFileOpen(&tdc->f.inode);
344             osi_Assert(cfile);
345             afs_CFileTruncate(cfile, 0);
346             afs_CFileClose(cfile);
347             afs_AdjustSize(tdc, 0);     /* sets f.chunkSize to 0 */
348
349             afs_stats_cmperf.cacheCurrDirtyChunks--;
350             afs_indexFlags[tdc->index] &= ~IFDataMod;   /* so it does disappear */
351             ReleaseWriteLock(&tdc->lock);
352             afs_PutDCache(tdc);
353             break;
354         }
355         /* otherwise we've written some, fixup length, etc and continue with next seg */
356         len = len - AFS_UIO_RESID(tuiop);       /* compute amount really transferred */
357         tlen = len;
358         afsio_skip(auio, tlen); /* advance auio over data written */
359         /* compute new file size */
360         if (offset + len > tdc->f.chunkBytes) {
361             afs_int32 tlength = offset + len;
362             afs_AdjustSize(tdc, tlength);
363             if (tdc->validPos < filePos + len)
364                 tdc->validPos = filePos + len;
365         }
366         totalLength -= len;
367         transferLength += len;
368         filePos += len;
369 #if defined(AFS_SGI_ENV)
370         /* afs_xwrite handles setting m.Length */
371         osi_Assert(filePos <= avc->f.m.Length);
372 #else
373         if (filePos > avc->f.m.Length) {
374             if (AFS_IS_DISCON_RW)
375                 afs_PopulateDCache(avc, filePos, treq);
376             afs_Trace4(afs_iclSetp, CM_TRACE_SETLENGTH, ICL_TYPE_STRING,
377                        __FILE__, ICL_TYPE_LONG, __LINE__, ICL_TYPE_OFFSET,
378                        ICL_HANDLE_OFFSET(avc->f.m.Length), ICL_TYPE_OFFSET,
379                        ICL_HANDLE_OFFSET(filePos));
380             avc->f.m.Length = filePos;
381 #if defined(AFS_FBSD_ENV) || defined(AFS_DFBSD_ENV)
382             vnode_pager_setsize(vp, filePos);
383 #endif
384         }
385 #endif
386         ReleaseWriteLock(&tdc->lock);
387         afs_PutDCache(tdc);
388 #if !defined(AFS_VM_RDWR_ENV)
389         /*
390          * If write is implemented via VM, afs_DoPartialWrite() is called from
391          * the high-level write op.
392          */
393         if (!noLock) {
394             code = afs_DoPartialWrite(avc, treq);
395             if (code) {
396                 error = code;
397                 break;
398             }
399         }
400 #endif
401     }
402 #if !defined(AFS_VM_RDWR_ENV) || defined(AFS_FAKEOPEN_ENV)
403     afs_FakeClose(avc, acred);
404 #endif
405     error = afs_CheckCode(error, treq, 7);
406     /* This set is here so we get the CheckCode. */
407     if (error && !avc->vc_error)
408         avc->vc_error = error;
409     if (!noLock)
410         ReleaseWriteLock(&avc->lock);
411     if (tuiop)
412         afsio_free(tuiop);
413
414 #ifndef AFS_VM_RDWR_ENV
415     /*
416      * If write is implemented via VM, afs_fsync() is called from the high-level
417      * write op.
418      */
419 #if defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
420     if (noLock && (aio & IO_SYNC)) {
421 #else
422 #ifdef  AFS_HPUX_ENV
423     /* On hpux on synchronous writes syncio will be set to IO_SYNC. If
424      * we're doing them because the file was opened with O_SYNCIO specified,
425      * we have to look in the u area. No single mechanism here!!
426      */
427     if (noLock && ((aio & IO_SYNC) | (auio->uio_fpflags & FSYNCIO))) {
428 #else
429     if (noLock && (aio & FSYNC)) {
430 #endif
431 #endif
432         if (!AFS_NFSXLATORREQ(acred))
433             afs_fsync(avc, acred);
434     }
435 #endif
436     afs_DestroyReq(treq);
437     return error;
438 }
439
440 /* do partial write if we're low on unmodified chunks */
441 int
442 afs_DoPartialWrite(struct vcache *avc, struct vrequest *areq)
443 {
444     afs_int32 code;
445
446     if (afs_stats_cmperf.cacheCurrDirtyChunks <=
447         afs_stats_cmperf.cacheMaxDirtyChunks
448         || AFS_IS_DISCONNECTED)
449         return 0;               /* nothing to do */
450     /* otherwise, call afs_StoreDCache (later try to do this async, if possible) */
451     afs_Trace2(afs_iclSetp, CM_TRACE_PARTIALWRITE, ICL_TYPE_POINTER, avc,
452                ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(avc->f.m.Length));
453
454 #if     defined(AFS_SUN5_ENV)
455     code = afs_StoreAllSegments(avc, areq, AFS_ASYNC | AFS_VMSYNC_INVAL);
456 #else
457     code = afs_StoreAllSegments(avc, areq, AFS_ASYNC);
458 #endif
459     return code;
460 }
461
462 /* handle any closing cleanup stuff */
463 int
464 #if defined(AFS_SGI65_ENV)
465 afs_close(OSI_VC_DECL(avc), afs_int32 aflags, lastclose_t lastclose,
466           afs_ucred_t *acred)
467 #elif defined(AFS_SGI64_ENV)
468 afs_close(OSI_VC_DECL(avc), afs_int32 aflags, lastclose_t lastclose,
469           off_t offset, afs_ucred_t *acred, struct flid *flp)
470 #elif defined(AFS_SGI_ENV)
471 afs_close(OSI_VC_DECL(avc), afs_int32 aflags, lastclose_t lastclose
472           off_t offset, afs_ucred_t *acred)
473 #elif defined(AFS_SUN5_ENV)
474 afs_close(OSI_VC_DECL(avc), afs_int32 aflags, int count, offset_t offset, 
475          afs_ucred_t *acred)
476 #else
477 afs_close(OSI_VC_DECL(avc), afs_int32 aflags, afs_ucred_t *acred)
478 #endif
479 {
480     afs_int32 code;
481     afs_int32 code_checkcode = 0;
482     struct brequest *tb;
483     struct vrequest *treq = NULL;
484 #ifdef AFS_SGI65_ENV
485     struct flid flid;
486 #endif
487     struct afs_fakestat_state fakestat;
488     OSI_VC_CONVERT(avc);
489
490     AFS_STATCNT(afs_close);
491     afs_Trace2(afs_iclSetp, CM_TRACE_CLOSE, ICL_TYPE_POINTER, avc,
492                ICL_TYPE_INT32, aflags);
493     code = afs_CreateReq(&treq, acred);
494     if (code)
495         return code;
496     afs_InitFakeStat(&fakestat);
497     code = afs_EvalFakeStat(&avc, &fakestat, treq);
498     if (code) {
499         afs_PutFakeStat(&fakestat);
500         afs_DestroyReq(treq);
501         return code;
502     }
503     AFS_DISCON_LOCK();
504 #ifdef  AFS_SUN5_ENV
505     if (avc->flockCount) {
506         HandleFlock(avc, LOCK_UN, treq, 0, 1 /*onlymine */ );
507     }
508 #endif
509 #if defined(AFS_SGI_ENV)
510     if (!lastclose) {
511         afs_PutFakeStat(&fakestat);
512         AFS_DISCON_UNLOCK();
513         afs_DestroyReq(treq);
514         return 0;
515     }
516     /* unlock any locks for pid - could be wrong for child .. */
517     AFS_RWLOCK((vnode_t *) avc, VRWLOCK_WRITE);
518 # ifdef AFS_SGI65_ENV
519     get_current_flid(&flid);
520     cleanlocks((vnode_t *) avc, flid.fl_pid, flid.fl_sysid);
521     HandleFlock(avc, LOCK_UN, treq, flid.fl_pid, 1 /*onlymine */ );
522 # else
523 #  ifdef AFS_SGI64_ENV
524     cleanlocks((vnode_t *) avc, flp);
525 #  else /* AFS_SGI64_ENV */
526     cleanlocks((vnode_t *) avc, u.u_procp->p_epid, u.u_procp->p_sysid);
527 #  endif /* AFS_SGI64_ENV */
528     HandleFlock(avc, LOCK_UN, treq, OSI_GET_CURRENT_PID(), 1 /*onlymine */ );
529 # endif /* AFS_SGI65_ENV */
530     /* afs_chkpgoob will drop and re-acquire the global lock. */
531     afs_chkpgoob(&avc->v, btoc(avc->f.m.Length));
532 #elif defined(AFS_SUN5_ENV)
533     if (count > 1) {
534         /* The vfs layer may call this repeatedly with higher "count"; only
535          * on the last close (i.e. count = 1) we should actually proceed
536          * with the close. */
537         afs_PutFakeStat(&fakestat);
538         AFS_DISCON_UNLOCK();
539         afs_DestroyReq(treq);
540         return 0;
541     }
542 #else
543     if (avc->flockCount) {      /* Release Lock */
544         HandleFlock(avc, LOCK_UN, treq, 0, 1 /*onlymine */ );
545     }
546 #endif
547     if (aflags & (FWRITE | FTRUNC)) {
548         if (afs_BBusy() || (AFS_NFSXLATORREQ(acred)) || AFS_IS_DISCONNECTED) {
549             /* do it yourself if daemons are all busy */
550             ObtainWriteLock(&avc->lock, 124);
551             code = afs_StoreOnLastReference(avc, treq);
552             ReleaseWriteLock(&avc->lock);
553 #if defined(AFS_SGI_ENV)
554             AFS_RWUNLOCK((vnode_t *) avc, VRWLOCK_WRITE);
555 #endif
556         } else {
557 #if defined(AFS_SGI_ENV)
558             AFS_RWUNLOCK((vnode_t *) avc, VRWLOCK_WRITE);
559 #endif
560             /* at least one daemon is idle, so ask it to do the store.
561              * Also, note that  we don't lock it any more... */
562             tb = afs_BQueue(BOP_STORE, avc, 0, 1, acred,
563                             (afs_size_t) afs_cr_uid(acred), (afs_size_t) 0,
564                             (void *)0, (void *)0, (void *)0);
565             /* sleep waiting for the store to start, then retrieve error code */
566             while ((tb->flags & BUVALID) == 0) {
567                 tb->flags |= BUWAIT;
568                 afs_osi_Sleep(tb);
569             }
570             code = tb->code_raw;
571             code_checkcode = tb->code_checkcode;
572             afs_BRelease(tb);
573         }
574
575         /* VNOVNODE is "acceptable" error code from close, since
576          * may happen when deleting a file on another machine while
577          * it is open here. */
578         if (code == VNOVNODE)
579             code = 0;
580
581         /* Ensure last closer gets the error. If another thread caused
582          * DoPartialWrite and this thread does not actually store the data,
583          * it may not see the quota error.
584          */
585         ObtainWriteLock(&avc->lock, 406);
586         if (avc->vc_error) {
587 #ifdef AFS_AIX32_ENV
588             osi_ReleaseVM(avc, acred);
589 #endif
590             /* We don't know what the original raw error code was, so set
591              * 'code' to 0. But we have the afs_CheckCode-translated error
592              * code, so put that in code_checkcode. We cannot just set code
593              * to avc->vc_error, since vc_error is a checkcode-translated
594              * error code, and 'code' is supposed to be a raw error code. */
595             code = 0;
596             code_checkcode = avc->vc_error;
597             avc->vc_error = 0;
598         }
599         ReleaseWriteLock(&avc->lock);
600
601         /* some codes merit specific complaint */
602         if (code < 0) {
603             afs_warnuser("afs: failed to store file (network problems)\n");
604         }
605 #ifdef  AFS_SUN5_ENV
606         else if (code == ENOSPC || code_checkcode == ENOSPC) {
607             afs_warnuser
608                 ("afs: failed to store file (over quota or partition full)\n");
609         }
610 #else
611         else if (code == ENOSPC || code_checkcode == ENOSPC) {
612             afs_warnuser("afs: failed to store file (partition full)\n");
613         } else if (code == EDQUOT || code_checkcode == EDQUOT) {
614             afs_warnuser("afs: failed to store file (over quota)\n");
615         }
616 #endif
617         else if (code || code_checkcode)
618             afs_warnuser("afs: failed to store file (%d/%d)\n", code, code_checkcode);
619
620         /* finally, we flush any text pages lying around here */
621         hzero(avc->flushDV);
622         osi_FlushText(avc);
623     } else {
624 #if defined(AFS_SGI_ENV)
625         AFS_RWUNLOCK((vnode_t *) avc, VRWLOCK_WRITE);
626         osi_Assert(avc->opens > 0);
627 #endif
628         /* file open for read */
629         ObtainWriteLock(&avc->lock, 411);
630         if (avc->vc_error) {
631 #ifdef AFS_AIX32_ENV
632             osi_ReleaseVM(avc, acred);
633 #endif
634             code = 0;
635             code_checkcode = avc->vc_error;
636             avc->vc_error = 0;
637         }
638 #if defined(AFS_FBSD80_ENV)
639         /* XXX */
640         if (!avc->opens) {
641             afs_int32 opens, is_free, is_gone, is_doomed, iflag;
642             struct vnode *vp = AFSTOV(avc);
643             VI_LOCK(vp);
644             is_doomed =  vp->v_iflag & VI_DOOMED;
645             is_free = vp->v_iflag & VI_FREE;
646             is_gone = vp->v_iflag & VI_DOINGINACT;
647             iflag = vp->v_iflag;
648             VI_UNLOCK(vp);
649             opens = avc->opens;
650             afs_warn("afs_close avc %p vp %p opens %d free %d doinginact %d doomed %d iflag %d\n",
651                      avc, vp, opens, is_free, is_gone, is_doomed, iflag);
652         }
653 #endif
654         avc->opens--;
655         ReleaseWriteLock(&avc->lock);
656     }
657     AFS_DISCON_UNLOCK();
658     afs_PutFakeStat(&fakestat);
659
660     if (code_checkcode) {
661         code = code_checkcode;
662     } else {
663         code = afs_CheckCode(code, treq, 5);
664     }
665     afs_DestroyReq(treq);
666     return code;
667 }
668
669
670 int
671 #if defined(AFS_SGI_ENV) || defined(AFS_SUN5_ENV)
672 afs_fsync(OSI_VC_DECL(avc), int flag, afs_ucred_t *acred
673 # ifdef AFS_SGI65_ENV
674           , off_t start, off_t stop
675 # endif /* AFS_SGI65_ENV */
676     )
677 #else /* !SUN5 && !SGI */
678 afs_fsync(OSI_VC_DECL(avc), afs_ucred_t *acred)
679 #endif 
680 {
681     afs_int32 code;
682     struct vrequest *treq = NULL;
683     OSI_VC_CONVERT(avc);
684
685     if (avc->vc_error)
686         return avc->vc_error;
687
688 #if defined(AFS_SUN5_ENV)
689     /* back out if called from NFS server */
690     if (curthread->t_flag & T_DONTPEND)
691         return 0;
692 #endif
693
694     AFS_STATCNT(afs_fsync);
695     afs_Trace1(afs_iclSetp, CM_TRACE_FSYNC, ICL_TYPE_POINTER, avc);
696     if ((code = afs_CreateReq(&treq, acred)))
697         return code;
698     AFS_DISCON_LOCK();
699 #if defined(AFS_SGI_ENV)
700     AFS_RWLOCK((vnode_t *) avc, VRWLOCK_WRITE);
701     if (flag & FSYNC_INVAL)
702         osi_VM_FSyncInval(avc);
703 #endif /* AFS_SGI_ENV */
704
705     ObtainSharedLock(&avc->lock, 18);
706     code = 0;
707     if (avc->execsOrWriters > 0) {
708         if (!AFS_IS_DISCONNECTED && !AFS_IS_DISCON_RW) {
709             /* Your average flush. */
710             
711             /* put the file back */
712             UpgradeSToWLock(&avc->lock, 41);
713             code = afs_StoreAllSegments(avc, treq, AFS_SYNC);
714             ConvertWToSLock(&avc->lock);
715         } else {
716             UpgradeSToWLock(&avc->lock, 711);
717             afs_DisconAddDirty(avc, VDisconWriteFlush, 1);
718             ConvertWToSLock(&avc->lock);
719         }               /* if not disconnected */
720     }                   /* if (avc->execsOrWriters > 0) */
721
722 #if defined(AFS_SGI_ENV)
723     AFS_RWUNLOCK((vnode_t *) avc, VRWLOCK_WRITE);
724     if (code == VNOVNODE) {
725         /* syncing an unlinked file! - non-informative to pass an errno
726          * 102 (== VNOVNODE) to user
727          */
728         code = ENOENT;
729     }
730 #endif
731     AFS_DISCON_UNLOCK();
732     code = afs_CheckCode(code, treq, 33);
733     afs_DestroyReq(treq);
734     ReleaseSharedLock(&avc->lock);
735     return code;
736 }