ceaab48048ee9f82b69648d285aca4d0d6316c87
[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_UFSWrite
13  * afs_MemWrite
14  * afs_StoreOnLastReference
15  * afs_close
16  * afs_closex
17  * afs_fsync
18  */
19
20 #include <afsconfig.h>
21 #include "../afs/param.h"
22
23 RCSID("$Header$");
24
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"
31
32
33 extern unsigned char *afs_indexFlags;
34
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.
39  */
40 int afs_StoreOnLastReference(register struct vcache *avc, register 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->states & CCore) {
50         avc->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((struct AFS_UCRED *)avc->linkData);      /* "crheld" in afs_FakeClose */
62         avc->linkData = NULL;
63     }
64     /* Now, send the file back.  Used to require 0 writers left, but now do
65      * it on every close for write, since two closes in a row are harmless
66      * since first will clean all chunks, and second will be noop.  Note that
67      * this will also save confusion when someone keeps a file open 
68      * inadvertently, since with old system, writes to the server would never
69      * happen again. 
70      */
71     code = afs_StoreAllSegments(avc, treq, AFS_LASTSTORE/*!sync-to-disk*/);
72     /*
73      * We have to do these after the above store in done: in some systems like
74      * aix they'll need to flush all the vm dirty pages to the disk via the
75      * strategy routine. During that all procedure (done under no avc locks)
76      * opens, refcounts would be zero, since it didn't reach the afs_{rd,wr}
77      * routines which means the vcache is a perfect candidate for flushing!
78      */
79 #if defined(AFS_SGI_ENV)
80     osi_Assert(avc->opens > 0 && avc->execsOrWriters > 0);
81 #endif
82     avc->opens--;
83     avc->execsOrWriters--;
84     return code;
85 }
86
87
88
89 int afs_MemWrite(register struct vcache *avc, struct uio *auio, int aio, 
90         struct AFS_UCRED *acred, int noLock)
91 {
92     afs_size_t totalLength;
93     afs_size_t transferLength;
94     afs_size_t filePos;
95     afs_size_t offset, len;
96     afs_int32 tlen, trimlen;
97     afs_int32 startDate;
98     afs_int32 max;
99     register struct dcache *tdc;
100 #ifdef _HIGHC_
101     volatile
102 #endif
103     afs_int32 error;
104     struct uio tuio;
105     struct iovec *tvec;  /* again, should have define */
106     char *tfile;
107     register afs_int32 code;
108     struct vrequest treq;
109
110     AFS_STATCNT(afs_MemWrite);
111     if (avc->vc_error)
112         return avc->vc_error;
113
114     startDate = osi_Time();
115     if ((code = afs_InitReq(&treq, acred))) return code;
116     /* otherwise we read */
117     totalLength = auio->afsio_resid;
118     filePos = auio->afsio_offset;
119     error = 0;
120     transferLength = 0;
121     afs_Trace4(afs_iclSetp, CM_TRACE_WRITE, ICL_TYPE_POINTER, avc, 
122                         ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(filePos),
123                         ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(totalLength),
124                         ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(avc->m.Length));
125     if (!noLock) {
126         afs_MaybeWakeupTruncateDaemon();
127         ObtainWriteLock(&avc->lock,126);
128     }
129 #if defined(AFS_SGI_ENV)
130     {
131     off_t diff;
132     /*
133      * afs_xwrite handles setting m.Length
134      * and handles APPEND mode.
135      * Since we are called via strategy, we need to trim the write to
136      * the actual size of the file
137      */
138     osi_Assert(filePos <= avc->m.Length);
139     diff = avc->m.Length - filePos;
140     auio->afsio_resid = MIN(totalLength, diff);
141     totalLength = auio->afsio_resid;
142     }
143 #else
144     if (aio & IO_APPEND) {
145         /* append mode, start it at the right spot */
146 #if     defined(AFS_SUN56_ENV)
147         auio->uio_loffset = 0;
148 #endif
149         filePos = auio->afsio_offset = avc->m.Length;
150     }
151 #endif
152     /*
153      * Note that we use startDate rather than calling osi_Time() here.
154      * This is to avoid counting lock-waiting time in file date (for ranlib).
155      */
156     avc->m.Date = startDate;
157
158 #if     defined(AFS_HPUX_ENV) || defined(AFS_GFS_ENV)
159 #if     defined(AFS_HPUX101_ENV)
160     if ((totalLength + filePos) >> 9 > (p_rlimit(u.u_procp))[RLIMIT_FSIZE].rlim_cur) {
161 #else
162 #ifdef  AFS_HPUX_ENV
163     if ((totalLength + filePos) >> 9 > u.u_rlimit[RLIMIT_FSIZE].rlim_cur) {
164 #else
165     if (totalLength + filePos > u.u_rlimit[RLIMIT_FSIZE].rlim_cur) {
166 #endif
167 #endif
168         if (!noLock)
169             ReleaseWriteLock(&avc->lock);
170         return (EFBIG);
171     }
172 #endif
173 #ifdef AFS_VM_RDWR_ENV
174     /*
175      * If write is implemented via VM, afs_FakeOpen() is called from the
176      * high-level write op.
177      */
178     if (avc->execsOrWriters <= 0) {
179         printf("WARNING: afs_ufswr vp=%x, exOrW=%d\n", avc, avc->execsOrWriters);
180     }
181 #else
182     afs_FakeOpen(avc);
183 #endif
184     avc->states |= CDirty;
185     tvec = (struct iovec *) osi_AllocSmallSpace(sizeof(struct iovec));
186     while (totalLength > 0) {
187         /* Read the cached info. If we call GetDCache while the cache
188          * truncate daemon is running we risk overflowing the disk cache.
189          * Instead we check for an existing cache slot. If we cannot
190          * find an existing slot we wait for the cache to drain
191          * before calling GetDCache.
192          */
193         if (noLock) {
194             tdc = afs_FindDCache(avc, filePos);
195             if (tdc) ObtainWriteLock(&tdc->lock, 653);
196         } else if (afs_blocksUsed > (CM_WAITFORDRAINPCT*afs_cacheBlocks)/100) {
197             tdc = afs_FindDCache(avc, filePos);
198             if (tdc) {
199                 ObtainWriteLock(&tdc->lock, 654);
200                 if (!hsame(tdc->f.versionNo, avc->m.DataVersion) ||
201                     (tdc->dflags & DFFetching)) {
202                     ReleaseWriteLock(&tdc->lock);
203                     afs_PutDCache(tdc);
204                     tdc = NULL;
205                 }
206             }
207             if (!tdc) {
208                 afs_MaybeWakeupTruncateDaemon();
209                 while (afs_blocksUsed >
210                        (CM_WAITFORDRAINPCT*afs_cacheBlocks)/100) {
211                     ReleaseWriteLock(&avc->lock);
212                     if (afs_blocksUsed - afs_blocksDiscarded >
213                         (CM_WAITFORDRAINPCT*afs_cacheBlocks)/100) {
214                         afs_WaitForCacheDrain = 1;
215                         afs_osi_Sleep(&afs_WaitForCacheDrain);
216                     }
217                     afs_MaybeFreeDiscardedDCache();
218                     afs_MaybeWakeupTruncateDaemon();
219                     ObtainWriteLock(&avc->lock,506);
220                 }
221                 avc->states |= CDirty;
222                 tdc = afs_GetDCache(avc, filePos, &treq, &offset, &len, 4);
223                 if (tdc) ObtainWriteLock(&tdc->lock, 655);
224             }
225         } else {
226             tdc = afs_GetDCache(avc, filePos, &treq, &offset, &len, 4);
227             if (tdc) ObtainWriteLock(&tdc->lock, 656);
228         }
229         if (!tdc) {
230             error = EIO;
231             break;
232         }
233         if (!(afs_indexFlags[tdc->index] & IFDataMod)) {
234           afs_stats_cmperf.cacheCurrDirtyChunks++;
235           afs_indexFlags[tdc->index] |= IFDataMod;    /* so it doesn't disappear */
236         }
237         if (!(tdc->f.states & DWriting)) {
238             /* don't mark entry as mod if we don't have to */
239             tdc->f.states |= DWriting;
240             tdc->dflags |= DFEntryMod;
241         }
242         len = totalLength;      /* write this amount by default */
243         offset = filePos - AFS_CHUNKTOBASE(tdc->f.chunk);
244         max = AFS_CHUNKTOSIZE(tdc->f.chunk);    /* max size of this chunk */
245         if (max <= len + offset)        {   /*if we'd go past the end of this chunk */
246             /* it won't all fit in this chunk, so write as much
247                 as will fit */
248             len = max - offset;
249         }
250         /* mung uio structure to be right for this transfer */
251         afsio_copy(auio, &tuio, tvec);
252         trimlen = len;
253         afsio_trim(&tuio, trimlen);
254         tuio.afsio_offset = offset;
255
256         code = afs_MemWriteUIO(tdc->f.inode, &tuio);
257         if (code) {
258             void *mep; /* XXX in prototype world is struct memCacheEntry * */
259             error = code;
260             ZapDCE(tdc);                /* bad data */
261             mep = afs_MemCacheOpen(tdc->f.inode);
262             afs_MemCacheTruncate(mep, 0);
263             afs_MemCacheClose(mep);
264             afs_stats_cmperf.cacheCurrDirtyChunks--;
265             afs_indexFlags[tdc->index] &= ~IFDataMod;    /* so it does disappear */
266             ReleaseWriteLock(&tdc->lock);
267             afs_PutDCache(tdc);
268             break;
269         }
270         /* otherwise we've written some, fixup length, etc and continue with next seg */
271         len = len - tuio.afsio_resid; /* compute amount really transferred */
272         tlen = len;
273         afsio_skip(auio, tlen);     /* advance auio over data written */
274         /* compute new file size */
275         if (offset + len > tdc->f.chunkBytes) {
276             afs_int32 tlength = offset+len;
277             afs_AdjustSize(tdc, tlength);
278         }
279         totalLength -= len;
280         transferLength += len;
281         filePos += len;
282 #if defined(AFS_SGI_ENV)
283         /* afs_xwrite handles setting m.Length */
284         osi_Assert(filePos <= avc->m.Length);
285 #else
286         if (filePos > avc->m.Length) {
287             afs_Trace4(afs_iclSetp, CM_TRACE_SETLENGTH,
288                 ICL_TYPE_STRING, __FILE__,
289                 ICL_TYPE_LONG, __LINE__,
290                 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(avc->m.Length),
291                 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(filePos));
292             avc->m.Length = filePos;
293         }
294 #endif
295         ReleaseWriteLock(&tdc->lock);
296         afs_PutDCache(tdc);
297 #if !defined(AFS_VM_RDWR_ENV)
298         /*
299          * If write is implemented via VM, afs_DoPartialWrite() is called from
300          * the high-level write op.
301          */
302         if (!noLock) {
303             code = afs_DoPartialWrite(avc, &treq);
304             if (code) {
305                 error = code;
306                 break;
307             }
308         }
309 #endif
310     }
311 #ifndef AFS_VM_RDWR_ENV
312     afs_FakeClose(avc, acred);
313 #endif
314     if (error && !avc->vc_error)
315         avc->vc_error = error;
316     if (!noLock)
317         ReleaseWriteLock(&avc->lock);
318     osi_FreeSmallSpace(tvec);
319 #ifdef AFS_DEC_ENV
320     /* next, on GFS systems, we update g_size so that lseek's relative to EOF will
321        work.  GFS is truly a poorly-designed interface!  */
322     afs_gfshack((struct gnode *) avc);
323 #endif
324     error = afs_CheckCode(error, &treq, 6);
325     return error;
326 }
327
328
329 /* called on writes */
330 int afs_UFSWrite(register struct vcache *avc, struct uio *auio, 
331         int aio, struct AFS_UCRED *acred, int noLock)
332 {
333     afs_size_t totalLength;
334     afs_size_t transferLength;
335     afs_size_t filePos;
336     afs_size_t offset, len;
337     afs_int32  tlen;
338     afs_int32  trimlen;
339     afs_int32 startDate;
340     afs_int32 max;
341     register struct dcache *tdc;
342 #ifdef _HIGHC_
343     volatile
344 #endif
345     afs_int32 error;
346     struct uio tuio;
347     struct iovec *tvec;  /* again, should have define */
348     struct osi_file *tfile;
349     register afs_int32 code;
350     struct vnode *vp;
351     struct vrequest treq;
352
353     AFS_STATCNT(afs_UFSWrite);
354     if (avc->vc_error)
355         return avc->vc_error;
356
357     startDate = osi_Time();
358     if ((code = afs_InitReq(&treq, acred))) return code;
359     /* otherwise we read */
360     totalLength = auio->afsio_resid;
361     filePos = auio->afsio_offset;
362     error = 0;
363     transferLength = 0;
364     afs_Trace4(afs_iclSetp, CM_TRACE_WRITE, ICL_TYPE_POINTER, avc, 
365                         ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(filePos),
366                         ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(totalLength),
367                         ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(avc->m.Length));
368     if (!noLock) {
369         afs_MaybeWakeupTruncateDaemon();
370         ObtainWriteLock(&avc->lock,556);
371     }
372 #if defined(AFS_SGI_ENV)
373     {
374     off_t diff;
375     /*
376      * afs_xwrite handles setting m.Length
377      * and handles APPEND mode.
378      * Since we are called via strategy, we need to trim the write to
379      * the actual size of the file
380      */
381     osi_Assert(filePos <= avc->m.Length);
382     diff = avc->m.Length - filePos;
383     auio->afsio_resid = MIN(totalLength, diff);
384     totalLength = auio->afsio_resid;
385     }
386 #else
387     if (aio & IO_APPEND) {
388         /* append mode, start it at the right spot */
389 #if     defined(AFS_SUN56_ENV)
390         auio->uio_loffset = 0;
391 #endif
392         filePos = auio->afsio_offset = avc->m.Length;
393     }
394 #endif
395     /*
396      * Note that we use startDate rather than calling osi_Time() here.
397      * This is to avoid counting lock-waiting time in file date (for ranlib).
398      */
399     avc->m.Date = startDate;
400
401 #if     defined(AFS_HPUX_ENV) || defined(AFS_GFS_ENV)
402 #if     defined(AFS_HPUX101_ENV)
403     if ((totalLength + filePos) >> 9 > p_rlimit(u.u_procp)[RLIMIT_FSIZE].rlim_cur) {
404 #else
405 #ifdef  AFS_HPUX_ENV
406     if ((totalLength + filePos) >> 9 > u.u_rlimit[RLIMIT_FSIZE].rlim_cur) {
407 #else
408     if (totalLength + filePos > u.u_rlimit[RLIMIT_FSIZE].rlim_cur) {
409 #endif
410 #endif
411         if (!noLock)
412             ReleaseWriteLock(&avc->lock);
413         return (EFBIG);
414     }
415 #endif
416 #ifdef  AFS_VM_RDWR_ENV
417     /*
418      * If write is implemented via VM, afs_FakeOpen() is called from the
419      * high-level write op.
420      */
421     if (avc->execsOrWriters <= 0) {
422         printf("WARNING: afs_ufswr vp=%x, exOrW=%d\n", avc, avc->execsOrWriters);
423     }
424 #else
425     afs_FakeOpen(avc);
426 #endif
427     avc->states |= CDirty;
428     tvec = (struct iovec *) osi_AllocSmallSpace(sizeof(struct iovec));
429     while (totalLength > 0) {
430 #if 0 /* Can't call without tdc. Can't call later since GetDCache can be 
431          called with !tdc. Leaving it out for now. */
432         /* 
433          *  The following lines are necessary because afs_GetDCache with
434          *  flag == 4 expects the length field to be filled. It decides
435          *  from this whether it's necessary to fetch data into the chunk
436          *  before writing or not (when the whole chunk is overwritten!).
437          */
438         len = totalLength;      /* write this amount by default */
439         offset = filePos - AFS_CHUNKTOBASE(tdc->f.chunk);
440         max = AFS_CHUNKTOSIZE(tdc->f.chunk);    /* max size of this chunk */
441         if (max <= len + offset)        {   /*if we'd go past the end of this chunk */
442             /* it won't all fit in this chunk, so write as much
443                 as will fit */
444             len = max - offset;
445         }
446 #endif
447         /* read the cached info */
448         if (noLock) {
449             tdc = afs_FindDCache(avc, filePos);
450             if (tdc) ObtainWriteLock(&tdc->lock, 657);
451         } else if (afs_blocksUsed > (CM_WAITFORDRAINPCT*afs_cacheBlocks)/100) {
452             tdc = afs_FindDCache(avc, filePos);
453             if (tdc) {
454                 ObtainWriteLock(&tdc->lock, 658);
455                 if (!hsame(tdc->f.versionNo, avc->m.DataVersion) ||
456                     (tdc->dflags & DFFetching)) {
457                     ReleaseWriteLock(&tdc->lock);
458                     afs_PutDCache(tdc);
459                     tdc = NULL;
460                 }
461             }
462             if (!tdc) {
463                 afs_MaybeWakeupTruncateDaemon();
464                 while (afs_blocksUsed >
465                        (CM_WAITFORDRAINPCT*afs_cacheBlocks)/100) {
466                     ReleaseWriteLock(&avc->lock);
467                     if (afs_blocksUsed - afs_blocksDiscarded >
468                         (CM_WAITFORDRAINPCT*afs_cacheBlocks)/100) {
469                         afs_WaitForCacheDrain = 1;
470                         afs_osi_Sleep(&afs_WaitForCacheDrain);
471                     }
472                     afs_MaybeFreeDiscardedDCache();
473                     afs_MaybeWakeupTruncateDaemon();
474                     ObtainWriteLock(&avc->lock,509);
475                 }
476                 avc->states |= CDirty;
477                 tdc = afs_GetDCache(avc, filePos, &treq, &offset, &len, 4);
478                 if (tdc) ObtainWriteLock(&tdc->lock, 659);
479             }
480         } else {
481             tdc = afs_GetDCache(avc, filePos, &treq, &offset, &len, 4);
482             if (tdc) ObtainWriteLock(&tdc->lock, 660);
483         }
484         if (!tdc) {
485             error = EIO;
486             break;
487         }
488         if (!(afs_indexFlags[tdc->index] & IFDataMod)) {
489           afs_stats_cmperf.cacheCurrDirtyChunks++;
490           afs_indexFlags[tdc->index] |= IFDataMod;    /* so it doesn't disappear */
491         }
492         if (!(tdc->f.states & DWriting)) {
493             /* don't mark entry as mod if we don't have to */
494             tdc->f.states |= DWriting;
495             tdc->dflags |= DFEntryMod;
496         }
497         tfile = (struct osi_file *)osi_UFSOpen(tdc->f.inode);
498         len = totalLength;      /* write this amount by default */
499         offset = filePos - AFS_CHUNKTOBASE(tdc->f.chunk);
500         max = AFS_CHUNKTOSIZE(tdc->f.chunk);    /* max size of this chunk */
501         if (max <= len + offset)        {   /*if we'd go past the end of this chunk */
502             /* it won't all fit in this chunk, so write as much
503                 as will fit */
504             len = max - offset;
505         }
506         /* mung uio structure to be right for this transfer */
507         afsio_copy(auio, &tuio, tvec);
508         trimlen = len;
509         afsio_trim(&tuio, trimlen);
510         tuio.afsio_offset = offset;
511 #ifdef  AFS_AIX_ENV
512 #ifdef  AFS_AIX41_ENV
513         AFS_GUNLOCK();
514         code = VNOP_RDWR(tfile->vnode, UIO_WRITE, FWRITE, &tuio, NULL, NULL, NULL, &afs_osi_cred);
515         AFS_GLOCK();
516 #else
517 #ifdef AFS_AIX32_ENV
518         code = VNOP_RDWR(tfile->vnode, UIO_WRITE, FWRITE, &tuio, NULL, NULL);
519 #else
520         code = VNOP_RDWR(tfile->vnode, UIO_WRITE, FWRITE, (off_t)&offset, &tuio, NULL, NULL, -1);
521 #endif
522 #endif /* AFS_AIX41_ENV */
523 #else /* AFS_AIX_ENV */
524 #ifdef  AFS_SUN5_ENV
525         AFS_GUNLOCK();
526         VOP_RWLOCK(tfile->vnode, 1);
527         code = VOP_WRITE(tfile->vnode, &tuio, 0, &afs_osi_cred);
528         VOP_RWUNLOCK(tfile->vnode, 1);
529         AFS_GLOCK();
530         if (code == ENOSPC) afs_warnuser("\n\n\n*** Cache partition is full - decrease cachesize!!! ***\n\n\n"); 
531 #else
532 #if defined(AFS_SGI_ENV)
533         AFS_GUNLOCK();
534         avc->states |= CWritingUFS;
535         AFS_VOP_RWLOCK(tfile->vnode, VRWLOCK_WRITE);
536         AFS_VOP_WRITE(tfile->vnode, &tuio, IO_ISLOCKED, &afs_osi_cred, code);
537         AFS_VOP_RWUNLOCK(tfile->vnode, VRWLOCK_WRITE);
538         avc->states &= ~CWritingUFS;
539         AFS_GLOCK();
540 #else
541 #ifdef  AFS_OSF_ENV
542     {
543         struct ucred *tmpcred = u.u_cred;
544         u.u_cred = &afs_osi_cred;
545         tuio.uio_rw = UIO_WRITE;
546         AFS_GUNLOCK();
547         VOP_WRITE(tfile->vnode, &tuio, 0, &afs_osi_cred, code);
548         AFS_GLOCK();
549         u.u_cred = tmpcred;
550     }
551 #else   /* AFS_OSF_ENV */
552 #if defined(AFS_HPUX100_ENV)
553     {
554         AFS_GUNLOCK();
555         code = VOP_RDWR(tfile->vnode, &tuio, UIO_WRITE, 0, &afs_osi_cred);
556         AFS_GLOCK();
557     }
558 #else
559 #ifdef  AFS_HPUX_ENV
560         tuio.uio_fpflags &= ~FSYNCIO;   /* don't do sync io */
561 #endif
562 #if defined(AFS_LINUX20_ENV)
563         AFS_GUNLOCK();
564         code = osi_file_uio_rdwr(tfile, &tuio, UIO_WRITE);
565         AFS_GLOCK();
566 #else
567 #if defined(AFS_DARWIN_ENV)
568         AFS_GUNLOCK();
569         VOP_LOCK(tfile->vnode, LK_EXCLUSIVE, current_proc());
570         code = VOP_WRITE(tfile->vnode, &tuio, 0, &afs_osi_cred);
571         VOP_UNLOCK(tfile->vnode, 0, current_proc());
572         AFS_GLOCK();
573 #else
574 #if defined(AFS_FBSD_ENV)
575         AFS_GUNLOCK();
576         VOP_LOCK(tfile->vnode, LK_EXCLUSIVE, curproc);
577         code = VOP_WRITE(tfile->vnode, &tuio, 0, &afs_osi_cred);
578         VOP_UNLOCK(tfile->vnode, 0, curproc);
579         AFS_GLOCK();
580 #else
581         code = VOP_RDWR(tfile->vnode, &tuio, UIO_WRITE, 0, &afs_osi_cred);
582 #endif /* AFS_FBSD_ENV */
583 #endif /* AFS_DARWIN_ENV */
584 #endif /* AFS_LINUX20_ENV */
585 #endif /* AFS_HPUX100_ENV */
586 #endif /* AFS_OSF_ENV */
587 #endif /* AFS_SGI_ENV */
588 #endif /* AFS_SUN5_ENV */
589 #endif /* AFS_AIX41_ENV */
590         if (code) {
591             error = code;
592             ZapDCE(tdc);                /* bad data */
593             osi_UFSTruncate(tfile,0);   /* fake truncate the segment */
594             afs_AdjustSize(tdc, 0);     /* sets f.chunkSize to 0 */
595             afs_stats_cmperf.cacheCurrDirtyChunks--;
596             afs_indexFlags[tdc->index] &= ~IFDataMod;    /* so it does disappear */
597             afs_CFileClose(tfile);
598             ReleaseWriteLock(&tdc->lock);
599             afs_PutDCache(tdc);
600             break;
601         }
602         /* otherwise we've written some, fixup length, etc and continue with next seg */
603         len = len - tuio.afsio_resid; /* compute amount really transferred */
604         tlen = len;
605         afsio_skip(auio, tlen);     /* advance auio over data written */
606         /* compute new file size */
607         if (offset + len > tdc->f.chunkBytes) {
608             afs_int32 tlength = offset+len;
609             afs_AdjustSize(tdc, tlength);
610         }
611         totalLength -= len;
612         transferLength += len;
613         filePos += len;
614 #if defined(AFS_SGI_ENV)
615         /* afs_xwrite handles setting m.Length */
616         osi_Assert(filePos <= avc->m.Length);
617 #else
618         if (filePos > avc->m.Length) {
619             afs_Trace4(afs_iclSetp, CM_TRACE_SETLENGTH,
620                 ICL_TYPE_STRING, __FILE__,
621                 ICL_TYPE_LONG, __LINE__,
622                 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(avc->m.Length),
623                 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(filePos));
624             avc->m.Length = filePos;
625         }
626 #endif
627         osi_UFSClose(tfile);
628         ReleaseWriteLock(&tdc->lock);
629         afs_PutDCache(tdc);
630 #if !defined(AFS_VM_RDWR_ENV)
631         /*
632          * If write is implemented via VM, afs_DoPartialWrite() is called from
633          * the high-level write op.
634          */
635         if (!noLock) {
636             code = afs_DoPartialWrite(avc, &treq);
637             if (code) {
638                 error = code;
639                 break;
640             }
641         }
642 #endif
643     }
644 #ifndef AFS_VM_RDWR_ENV
645     afs_FakeClose(avc, acred);
646 #endif
647     error = afs_CheckCode(error, &treq, 7);
648     /* This set is here so we get the CheckCode. */
649     if (error && !avc->vc_error)
650         avc->vc_error = error;
651     if (!noLock)
652         ReleaseWriteLock(&avc->lock);
653     osi_FreeSmallSpace(tvec);
654 #ifdef AFS_DEC_ENV
655     /* next, on GFS systems, we update g_size so that lseek's relative to EOF will
656        work.  GFS is truly a poorly-designed interface!  */
657     afs_gfshack((struct gnode *) avc);
658 #endif
659 #ifndef AFS_VM_RDWR_ENV
660     /*
661      * If write is implemented via VM, afs_fsync() is called from the high-level
662      * write op.
663      */
664 #if defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
665      if (noLock && (aio & IO_SYNC)) {
666 #else 
667 #ifdef  AFS_HPUX_ENV
668     /* On hpux on synchronous writes syncio will be set to IO_SYNC. If
669      * we're doing them because the file was opened with O_SYNCIO specified,
670      * we have to look in the u area. No single mechanism here!!
671      */
672     if (noLock && ((aio & IO_SYNC) | (auio->uio_fpflags & FSYNCIO))) {    
673 #else
674     if (noLock && (aio & FSYNC)) {
675 #endif
676 #endif
677         if (!AFS_NFSXLATORREQ(acred))
678             afs_fsync(avc, acred);
679     }
680 #endif
681     return error;
682 }
683
684 /* do partial write if we're low on unmodified chunks */
685 int afs_DoPartialWrite(register struct vcache *avc, struct vrequest *areq)
686 {
687     register afs_int32 code;
688
689     if (afs_stats_cmperf.cacheCurrDirtyChunks <= afs_stats_cmperf.cacheMaxDirtyChunks) 
690         return 0;       /* nothing to do */
691     /* otherwise, call afs_StoreDCache (later try to do this async, if possible) */
692     afs_Trace2(afs_iclSetp, CM_TRACE_PARTIALWRITE, ICL_TYPE_POINTER, avc,
693                 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(avc->m.Length));
694 #if     defined(AFS_SUN5_ENV)
695     code = afs_StoreAllSegments(avc, areq, AFS_ASYNC | AFS_VMSYNC_INVAL);
696 #else
697     code = afs_StoreAllSegments(avc, areq, AFS_ASYNC);
698 #endif
699     return code;
700 }
701
702
703
704 #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)
705 #ifdef AFS_DUX50_ENV
706 #define vno_close(X) vn_close((X), 0, NOCRED)
707 #elif defined(AFS_DUX40_ENV)
708 #define      vno_close       vn_close
709 #endif
710 /* We don't need this for AIX since: 
711  * (1) aix doesn't use fileops and it call close directly intead
712  * (where the unlocking should be done) and 
713  * (2) temporarily, the aix lockf isn't supported yet.
714  *
715  *  this stupid routine is used to release the flocks held on a
716  *  particular file descriptor.  Sun doesn't pass file descr. info
717  *  through to the vnode layer, and yet we must unlock flocked files
718  *  on the *appropriate* (not first, as in System V) close call.  Thus
719  *  this code.
720  * How does this code get invoked? The afs AFS_FLOCK plugs in the new afs
721  * file ops structure into any afs file when it gets flocked. 
722  * N.B: Intercepting close syscall doesn't trap aborts or exit system
723  * calls.
724 */
725 int afs_closex(register struct file *afd)
726 {
727     struct vrequest treq;
728     struct vcache *tvc;
729     afs_int32 flags;
730     int closeDone;
731     afs_int32 code = 0;
732     struct afs_fakestat_state fakestat;
733
734     AFS_STATCNT(afs_closex);
735     /* setup the credentials */
736     if ((code = afs_InitReq(&treq, u.u_cred))) return code;
737     afs_InitFakeStat(&fakestat);
738
739     closeDone = 0;
740     /* we're the last one.  If we're an AFS vnode, clear the flags,
741      * close the file and release the lock when done.  Otherwise, just
742      * let the regular close code work.      */
743     if (afd->f_type == DTYPE_VNODE) {
744         tvc = VTOAFS(afd->f_data);
745         if (IsAfsVnode(AFSTOV(tvc))) {
746             code = afs_EvalFakeStat(&tvc, &fakestat, &treq);
747             if (code) {
748                 afs_PutFakeStat(&fakestat);
749                 return code;
750             }
751             VN_HOLD(AFSTOV(tvc));
752             flags = afd->f_flag & (FSHLOCK | FEXLOCK);
753             afd->f_flag &= ~(FSHLOCK | FEXLOCK);
754             code = vno_close(afd);
755             if (flags) 
756 #if defined(AFS_SGI_ENV) || defined(AFS_OSF_ENV) || defined(AFS_SUN_ENV) && !defined(AFS_SUN5_ENV)
757                 HandleFlock(tvc, LOCK_UN, &treq,
758                             u.u_procp->p_pid, 1/*onlymine*/);
759 #else
760                 HandleFlock(tvc, LOCK_UN, &treq, 0, 1/*onlymine*/);
761 #endif
762 #ifdef  AFS_DEC_ENV
763             grele((struct gnode *) tvc);
764 #else
765             AFS_RELE(AFSTOV(tvc));
766 #endif
767             closeDone = 1;
768         }
769     }
770     /* now, if close not done, do it */
771     if (!closeDone) {
772         code = vno_close(afd);
773     }
774     afs_PutFakeStat(&fakestat);
775     return code;        /* return code from vnode layer */
776 }
777 #endif
778
779
780 /* handle any closing cleanup stuff */
781 #ifdef  AFS_SGI_ENV
782 afs_close(OSI_VC_ARG(avc), aflags, lastclose,
783 #if !defined(AFS_SGI65_ENV)
784           offset,
785 #endif
786           acred
787 #if defined(AFS_SGI64_ENV) && !defined(AFS_SGI65_ENV)
788           , flp
789 #endif
790           )
791 lastclose_t lastclose;
792 #if !defined(AFS_SGI65_ENV)
793 off_t offset;
794 #if defined(AFS_SGI64_ENV)
795 struct flid *flp;
796 #endif
797 #endif
798 #else /* SGI */
799 #if     defined(AFS_SUN_ENV) || defined(AFS_SUN5_ENV)
800 #ifdef  AFS_SUN5_ENV
801 afs_close(OSI_VC_ARG(avc), aflags, count, offset, acred)
802     offset_t offset;
803 #else
804 afs_close(OSI_VC_ARG(avc), aflags, count, acred)
805 #endif
806 int count;
807 #else
808 afs_close(OSI_VC_ARG(avc), aflags, acred)
809 #endif
810 #endif
811     OSI_VC_DECL(avc);
812     afs_int32 aflags;
813     struct AFS_UCRED *acred; 
814 {
815     register afs_int32 code;
816     register struct brequest *tb;
817     struct vrequest treq;
818 #ifdef AFS_SGI65_ENV
819     struct flid flid;
820 #endif
821     struct afs_fakestat_state fakestat;
822     OSI_VC_CONVERT(avc)
823
824     AFS_STATCNT(afs_close);
825     afs_Trace2(afs_iclSetp, CM_TRACE_CLOSE, ICL_TYPE_POINTER, avc,
826                ICL_TYPE_INT32, aflags);
827     code = afs_InitReq(&treq, acred);
828     if (code) return code;
829     afs_InitFakeStat(&fakestat);
830     code = afs_EvalFakeStat(&avc, &fakestat, &treq);
831     if (code) {
832         afs_PutFakeStat(&fakestat);
833         return code;
834     }
835 #ifdef  AFS_SUN5_ENV
836     if (avc->flockCount) {
837         HandleFlock(avc, LOCK_UN, &treq, 0, 1/*onlymine*/);
838     }
839 #endif
840 #if defined(AFS_SGI_ENV)
841     if (!lastclose) {
842         afs_PutFakeStat(&fakestat);
843         return 0;
844     }
845 #else
846 #if     defined(AFS_SUN_ENV) || defined(AFS_SGI_ENV)
847     if (count > 1) {
848         /* 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. */
849         afs_PutFakeStat(&fakestat);
850         return 0;
851     }
852 #endif
853 #endif
854 #ifndef AFS_SUN5_ENV
855 #if defined(AFS_SGI_ENV)
856     /* unlock any locks for pid - could be wrong for child .. */
857     AFS_RWLOCK((vnode_t *)avc, VRWLOCK_WRITE);
858 #ifdef AFS_SGI65_ENV
859     get_current_flid(&flid);
860     cleanlocks((vnode_t *)avc, flid.fl_pid, flid.fl_sysid);
861     HandleFlock(avc, LOCK_UN, &treq, flid.fl_pid, 1/*onlymine*/);
862 #else
863 #ifdef AFS_SGI64_ENV
864     cleanlocks((vnode_t *)avc, flp);
865 #else /* AFS_SGI64_ENV */
866     cleanlocks((vnode_t *)avc, u.u_procp->p_epid, u.u_procp->p_sysid);
867 #endif /* AFS_SGI64_ENV */
868     HandleFlock(avc, LOCK_UN, &treq, OSI_GET_CURRENT_PID(), 1/*onlymine*/);
869 #endif /* AFS_SGI65_ENV */
870     /* afs_chkpgoob will drop and re-acquire the global lock. */
871     afs_chkpgoob(&avc->v, btoc(avc->m.Length));
872 #else
873     if (avc->flockCount) {              /* Release Lock */
874 #if     defined(AFS_OSF_ENV) || defined(AFS_SUN_ENV)
875         HandleFlock(avc, LOCK_UN, &treq, u.u_procp->p_pid, 1/*onlymine*/);
876 #else
877         HandleFlock(avc, LOCK_UN, &treq, 0, 1/*onlymine*/);
878 #endif
879     }
880 #endif
881 #endif
882     if (aflags & (FWRITE | FTRUNC)) {
883         if (afs_BBusy()) {
884             /* do it yourself if daemons are all busy */
885             ObtainWriteLock(&avc->lock,124);
886             code = afs_StoreOnLastReference(avc, &treq);
887             ReleaseWriteLock(&avc->lock);
888 #if defined(AFS_SGI_ENV)
889             AFS_RWUNLOCK((vnode_t *)avc, VRWLOCK_WRITE);
890 #endif
891         }
892         else {
893 #if defined(AFS_SGI_ENV)
894             AFS_RWUNLOCK((vnode_t *)avc, VRWLOCK_WRITE);
895 #endif
896             /* at least one daemon is idle, so ask it to do the store.
897                 Also, note that  we don't lock it any more... */
898             tb = afs_BQueue(BOP_STORE, avc, 0, 1, acred,
899                                 (afs_size_t) acred->cr_uid, (afs_size_t) 0,
900                                 (void *) 0);
901             /* sleep waiting for the store to start, then retrieve error code */
902             while ((tb->flags & BUVALID) == 0) {
903                 tb->flags |= BUWAIT;
904                 afs_osi_Sleep(tb);
905             }
906             code = tb->code;
907             afs_BRelease(tb);
908         }
909
910         /* VNOVNODE is "acceptable" error code from close, since
911             may happen when deleting a file on another machine while
912             it is open here. We do the same for ENOENT since in afs_CheckCode we map VNOVNODE -> ENOENT */
913         if (code == VNOVNODE || code == ENOENT)
914             code = 0;
915         
916         /* Ensure last closer gets the error. If another thread caused
917          * DoPartialWrite and this thread does not actually store the data,
918          * it may not see the quota error.
919          */
920         ObtainWriteLock(&avc->lock,406);
921         if (avc->vc_error) {
922 #ifdef AFS_AIX32_ENV
923             osi_ReleaseVM(avc, acred);
924 #endif
925             code = avc->vc_error;
926             avc->vc_error = 0;
927         }
928         ReleaseWriteLock(&avc->lock);
929
930         /* some codes merit specific complaint */
931         if (code < 0) {
932             afs_warnuser("afs: failed to store file (network problems)\n");
933         }
934 #ifdef  AFS_SUN5_ENV
935         else if (code == ENOSPC) {
936             afs_warnuser("afs: failed to store file (over quota or partition full)\n");
937         }
938 #else
939         else if (code == ENOSPC) {
940             afs_warnuser("afs: failed to store file (partition full)\n");
941         }
942         else if (code == EDQUOT) {
943             afs_warnuser("afs: failed to store file (over quota)\n");
944         }
945 #endif
946         else if (code != 0)
947             afs_warnuser("afs: failed to store file (%d)\n", code);
948
949         /* finally, we flush any text pages lying around here */
950         hzero(avc->flushDV);
951         osi_FlushText(avc);
952     }
953     else {
954 #if defined(AFS_SGI_ENV)
955         AFS_RWUNLOCK((vnode_t *)avc, VRWLOCK_WRITE);
956         osi_Assert(avc->opens > 0);
957 #endif
958         /* file open for read */
959         ObtainWriteLock(&avc->lock, 411);
960         if (avc->vc_error) {
961 #ifdef AFS_AIX32_ENV
962             osi_ReleaseVM(avc, acred);
963 #endif
964             code = avc->vc_error;
965             avc->vc_error = 0;
966         }
967         avc->opens--;
968         ReleaseWriteLock(&avc->lock);
969     }
970 #ifdef  AFS_OSF_ENV
971     if ((VREFCOUNT(avc) <= 2) && (avc->states & CUnlinked)) {
972         afs_remunlink(avc, 1);  /* ignore any return code */
973     }
974 #endif
975     afs_PutFakeStat(&fakestat);
976     code = afs_CheckCode(code, &treq, 5);
977     return code;
978 }
979
980
981
982 #ifdef  AFS_OSF_ENV
983 afs_fsync(avc, fflags, acred, waitfor)
984 int fflags;
985 int waitfor;
986 #else   /* AFS_OSF_ENV */
987 #if defined(AFS_SGI_ENV) || defined(AFS_SUN53_ENV)
988 afs_fsync(OSI_VC_ARG(avc), flag, acred
989 #ifdef AFS_SGI65_ENV
990           , start, stop
991 #endif
992           )
993 #else
994 afs_fsync(avc, acred)
995 #endif
996 #endif
997     OSI_VC_DECL(avc);
998      struct AFS_UCRED *acred;
999 #if defined(AFS_SGI_ENV) || defined(AFS_SUN53_ENV)
1000 int flag;
1001 #ifdef AFS_SGI65_ENV
1002 off_t start, stop;
1003 #endif
1004 #endif
1005 {
1006     register afs_int32 code;
1007     struct vrequest treq;
1008     OSI_VC_CONVERT(avc)
1009
1010     if (avc->vc_error)
1011         return avc->vc_error;
1012
1013 #if defined(AFS_SUN5_ENV)
1014      /* back out if called from NFS server */
1015     if (curthread->t_flag & T_DONTPEND)
1016         return 0;
1017 #endif
1018
1019     AFS_STATCNT(afs_fsync);
1020     afs_Trace1(afs_iclSetp, CM_TRACE_FSYNC, ICL_TYPE_POINTER, avc);
1021     if ((code = afs_InitReq(&treq, acred))) return code;
1022
1023 #if defined(AFS_SGI_ENV)
1024     AFS_RWLOCK((vnode_t *)avc, VRWLOCK_WRITE);
1025     if (flag & FSYNC_INVAL)
1026         osi_VM_FSyncInval(avc);
1027 #endif /* AFS_SGI_ENV */
1028
1029     ObtainSharedLock(&avc->lock,18);
1030     code = 0;
1031     if (avc->execsOrWriters > 0) {
1032         /* put the file back */
1033         UpgradeSToWLock(&avc->lock,41);
1034         code = afs_StoreAllSegments(avc, &treq, AFS_SYNC);
1035         ConvertWToSLock(&avc->lock);
1036     }
1037
1038 #if defined(AFS_SGI_ENV)
1039     AFS_RWUNLOCK((vnode_t *)avc, VRWLOCK_WRITE);
1040     if (code == VNOVNODE) {
1041         /* syncing an unlinked file! - non-informative to pass an errno
1042          * 102 (== VNOVNODE) to user
1043          */
1044         code =  ENOENT;
1045     }
1046 #endif
1047
1048     code = afs_CheckCode(code, &treq, 33);
1049     ReleaseSharedLock(&avc->lock);
1050     return code;
1051 }