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