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