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