linux-fh-based-cache-20081108
[openafs.git] / src / afs / afs_segments.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  * --------------------- Required definitions ---------------------
12  */
13 #include <afsconfig.h>
14 #include "afs/param.h"
15
16 RCSID
17     ("$Header$");
18
19 #include "afs/sysincludes.h"    /*Standard vendor system headers */
20 #include "afsincludes.h"        /*AFS-based standard headers */
21 #include "afs/afs_stats.h"      /* statistics */
22 #include "afs/afs_cbqueue.h"
23 #include "afs/afs_osidnlc.h"
24
25 afs_uint32 afs_stampValue = 0;
26
27 /*
28  * afs_StoreMini
29  *
30  * Description:
31  *      Send a truncation request to a FileServer.
32  *
33  * Parameters:
34  *      xxx : description
35  *
36  * Environment:
37  *      We're write-locked upon entry.
38  */
39
40 int
41 afs_StoreMini(register struct vcache *avc, struct vrequest *areq)
42 {
43     register struct conn *tc;
44     struct AFSStoreStatus InStatus;
45     struct AFSFetchStatus OutStatus;
46     struct AFSVolSync tsync;
47     register afs_int32 code;
48     register struct rx_call *tcall;
49     afs_size_t tlen, xlen = 0;
50     XSTATS_DECLS;
51     AFS_STATCNT(afs_StoreMini);
52     afs_Trace2(afs_iclSetp, CM_TRACE_STOREMINI, ICL_TYPE_POINTER, avc,
53                ICL_TYPE_INT32, avc->m.Length);
54     tlen = avc->m.Length;
55     if (avc->truncPos < tlen)
56         tlen = avc->truncPos;
57     avc->truncPos = AFS_NOTRUNC;
58     avc->states &= ~CExtendedFile;
59
60     do {
61         tc = afs_Conn(&avc->fid, areq, SHARED_LOCK);
62         if (tc) {
63           retry:
64             RX_AFS_GUNLOCK();
65             tcall = rx_NewCall(tc->id);
66             RX_AFS_GLOCK();
67             /* Set the client mod time since we always want the file
68              * to have the client's mod time and not the server's one
69              * (to avoid problems with make, etc.) It almost always
70              * works fine with standard afs because them server/client
71              * times are in sync and more importantly this storemini
72              * it's a special call that would typically be followed by
73              * the proper store-data or store-status calls.
74              */
75             InStatus.Mask = AFS_SETMODTIME;
76             InStatus.ClientModTime = avc->m.Date;
77             XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_STOREDATA);
78             afs_Trace4(afs_iclSetp, CM_TRACE_STOREDATA64, ICL_TYPE_FID,
79                        &avc->fid.Fid, ICL_TYPE_OFFSET,
80                        ICL_HANDLE_OFFSET(avc->m.Length), ICL_TYPE_OFFSET,
81                        ICL_HANDLE_OFFSET(xlen), ICL_TYPE_OFFSET,
82                        ICL_HANDLE_OFFSET(tlen));
83             RX_AFS_GUNLOCK();
84 #ifdef AFS_64BIT_CLIENT
85             if (!afs_serverHasNo64Bit(tc)) {
86                 code =
87                     StartRXAFS_StoreData64(tcall,
88                                            (struct AFSFid *)&avc->fid.Fid,
89                                            &InStatus, avc->m.Length,
90                                            (afs_size_t) 0, tlen);
91             } else {
92                 afs_int32 l1, l2;
93                 l1 = avc->m.Length;
94                 l2 = tlen;
95                 if ((avc->m.Length > 0x7fffffff) ||
96                     (tlen > 0x7fffffff) ||
97                     ((0x7fffffff - tlen) < avc->m.Length))
98                     return EFBIG;
99                 code =
100                     StartRXAFS_StoreData(tcall,
101                                          (struct AFSFid *)&avc->fid.Fid,
102                                          &InStatus, l1, 0, l2);
103             }
104 #else /* AFS_64BIT_CLIENT */
105             code =
106                 StartRXAFS_StoreData(tcall, (struct AFSFid *)&avc->fid.Fid,
107                                      &InStatus, avc->m.Length, 0, tlen);
108 #endif /* AFS_64BIT_CLIENT */
109             if (code == 0) {
110                 code = EndRXAFS_StoreData(tcall, &OutStatus, &tsync);
111             }
112             code = rx_EndCall(tcall, code);
113             RX_AFS_GLOCK();
114             XSTATS_END_TIME;
115 #ifdef AFS_64BIT_CLIENT
116             if (code == RXGEN_OPCODE && !afs_serverHasNo64Bit(tc)) {
117                 afs_serverSetNo64Bit(tc);
118                 goto retry;
119             }
120 #endif /* AFS_64BIT_CLIENT */
121         } else
122             code = -1;
123     } while (afs_Analyze
124              (tc, code, &avc->fid, areq, AFS_STATS_FS_RPCIDX_STOREDATA,
125               SHARED_LOCK, NULL));
126
127     if (code == 0) {
128         afs_ProcessFS(avc, &OutStatus, areq);
129     } else {
130         /* blew it away */
131         afs_InvalidateAllSegments(avc);
132     }
133     return code;
134
135 }                               /*afs_StoreMini */
136
137 unsigned int storeallmissing = 0;
138 #define lmin(a,b) (((a) < (b)) ? (a) : (b))
139 /*
140  * afs_StoreAllSegments
141  *
142  * Description:
143  *      Stores all modified segments back to server
144  *
145  * Parameters:
146  *      avc  : Pointer to vcache entry.
147  *      areq : Pointer to request structure.
148  *
149  * Environment:
150  *      Called with avc write-locked.
151  */
152 #if defined (AFS_HPUX_ENV)
153 int NCHUNKSATONCE = 3;
154 #else
155 int NCHUNKSATONCE = 64;
156 #endif
157 int afs_dvhack = 0;
158
159
160 int
161 afs_StoreAllSegments(register struct vcache *avc, struct vrequest *areq,
162                      int sync)
163 {
164     register struct dcache *tdc;
165     register afs_int32 code = 0;
166     register afs_int32 index;
167     register afs_int32 origCBs, foreign = 0;
168     int hash, stored;
169     afs_hyper_t newDV, oldDV;   /* DV when we start, and finish, respectively */
170     struct dcache **dcList, **dclist;
171     unsigned int i, j, minj, moredata, high, off;
172     afs_size_t tlen;
173     afs_size_t maxStoredLength; /* highest offset we've written to server. */
174     int safety;
175 #ifndef AFS_NOSTATS
176     struct afs_stats_xferData *xferP;   /* Ptr to this op's xfer struct */
177     osi_timeval_t xferStartTime,        /*FS xfer start time */
178       xferStopTime;             /*FS xfer stop time */
179     afs_size_t bytesToXfer;     /* # bytes to xfer */
180     afs_size_t bytesXferred;    /* # bytes actually xferred */
181 #endif /* AFS_NOSTATS */
182
183
184     AFS_STATCNT(afs_StoreAllSegments);
185
186     hset(oldDV, avc->m.DataVersion);
187     hset(newDV, avc->m.DataVersion);
188     hash = DVHash(&avc->fid);
189     foreign = (avc->states & CForeign);
190     dcList = (struct dcache **)osi_AllocLargeSpace(AFS_LRALLOCSIZ);
191     afs_Trace2(afs_iclSetp, CM_TRACE_STOREALL, ICL_TYPE_POINTER, avc,
192                ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(avc->m.Length));
193 #if !defined(AFS_AIX32_ENV) && !defined(AFS_SGI65_ENV)
194     /* In the aix vm implementation we need to do the vm_writep even
195      * on the memcache case since that's we adjust the file's size
196      * and finish flushing partial vm pages.
197      */
198     if (cacheDiskType != AFS_FCACHE_TYPE_MEM)
199 #endif /* !AFS_AIX32_ENV && !AFS_SGI65_ENV */
200     {
201         /* If we're not diskless, reading a file may stress the VM
202          * system enough to cause a pageout, and this vnode would be
203          * locked when the pageout occurs.  We can prevent this problem
204          * by making sure all dirty pages are already flushed.  We don't
205          * do this when diskless because reading a diskless (i.e.
206          * memory-resident) chunk doesn't require using new VM, and we
207          * also don't want to dump more dirty data into a diskless cache,
208          * since they're smaller, and we might exceed its available
209          * space.
210          */
211 #if     defined(AFS_SUN5_ENV)
212         if (sync & AFS_VMSYNC_INVAL)    /* invalidate VM pages */
213             osi_VM_TryToSmush(avc, CRED(), 1);
214         else
215 #endif
216             osi_VM_StoreAllSegments(avc);
217     }
218     if (AFS_IS_DISCONNECTED && !AFS_IN_SYNC) {
219         if (!AFS_IS_LOGGING) {
220             /* This will probably make someone sad ... */
221             /*printf("Net down in afs_StoreSegments\n");*/
222             return ENETDOWN;
223         }
224     }
225     ConvertWToSLock(&avc->lock);
226
227     /*
228      * Subsequent code expects a sorted list, and it expects all the
229      * chunks in the list to be contiguous, so we need a sort and a
230      * while loop in here, too - but this will work for a first pass...
231      * 92.10.05 - OK, there's a sort in here now.  It's kind of a modified
232      *            bin sort, I guess.  Chunk numbers start with 0
233      *
234      * - Have to get a write lock on xdcache because GetDSlot might need it (if
235      *   the chunk doesn't have a dcache struct).
236      *   This seems like overkill in most cases.
237      * - I'm not sure that it's safe to do "index = .hvNextp", then unlock 
238      *   xdcache, then relock xdcache and try to use index.  It is done
239      *   a lot elsewhere in the CM, but I'm not buying that argument.
240      * - should be able to check IFDataMod without doing the GetDSlot (just
241      *   hold afs_xdcache).  That way, it's easy to do this without the
242      *   writelock on afs_xdcache, and we save unneccessary disk
243      *   operations. I don't think that works, 'cuz the next pointers 
244      *   are still on disk.
245      */
246     origCBs = afs_allCBs;
247
248     maxStoredLength = 0;
249     tlen = avc->m.Length;
250     minj = 0;
251
252     do {
253         memset((char *)dcList, 0, NCHUNKSATONCE * sizeof(struct dcache *));
254         high = 0;
255         moredata = FALSE;
256
257         /* lock and start over from beginning of hash chain 
258          * in order to avoid a race condition. */
259         MObtainWriteLock(&afs_xdcache, 284);
260         index = afs_dvhashTbl[hash];
261
262         for (j = 0; index != NULLIDX;) {
263             if ((afs_indexFlags[index] & IFDataMod)
264                 && (afs_indexUnique[index] == avc->fid.Fid.Unique)) {
265                 tdc = afs_GetDSlot(index, 0);   /* refcount+1. */
266                 ReleaseReadLock(&tdc->tlock);
267                 if (!FidCmp(&tdc->f.fid, &avc->fid) && tdc->f.chunk >= minj) {
268                     off = tdc->f.chunk - minj;
269                     if (off < NCHUNKSATONCE) {
270                         if (dcList[off])
271                             osi_Panic("dclist slot already in use!");
272                         dcList[off] = tdc;
273                         if (off > high)
274                             high = off;
275                         j++;
276                         /* DCLOCKXXX: chunkBytes is protected by tdc->lock which we
277                          * can't grab here, due to lock ordering with afs_xdcache.
278                          * So, disable this shortcut for now.  -- kolya 2001-10-13
279                          */
280                         /* shortcut: big win for little files */
281                         /* tlen -= tdc->f.chunkBytes;
282                          * if (tlen <= 0)
283                          *    break;
284                          */
285                     } else {
286                         moredata = TRUE;
287                         afs_PutDCache(tdc);
288                         if (j == NCHUNKSATONCE)
289                             break;
290                     }
291                 } else {
292                     afs_PutDCache(tdc);
293                 }
294             }
295             index = afs_dvnextTbl[index];
296         }
297         MReleaseWriteLock(&afs_xdcache);
298
299         /* this guy writes chunks, puts back dcache structs, and bumps newDV */
300         /* "moredata" just says "there are more dirty chunks yet to come".
301          */
302         if (j) {
303 #ifdef AFS_NOSTATS
304             static afs_uint32 lp1 = 10000, lp2 = 10000;
305 #endif
306             struct AFSStoreStatus InStatus;
307             struct AFSFetchStatus OutStatus;
308             int doProcessFS = 0;
309             afs_size_t base, bytes;
310             afs_uint32 nchunks;
311             int nomore;
312             unsigned int first = 0;
313             int *shouldwake;
314             struct conn *tc;
315             struct osi_file *tfile;
316             struct rx_call *tcall;
317             XSTATS_DECLS;
318             for (bytes = 0, j = 0; !code && j <= high; j++) {
319                 if (dcList[j]) {
320                     ObtainSharedLock(&(dcList[j]->lock), 629);
321                     if (!bytes)
322                         first = j;
323                     bytes += dcList[j]->f.chunkBytes;
324                     if ((dcList[j]->f.chunkBytes < afs_OtherCSize)
325                         && (dcList[j]->f.chunk - minj < high)
326                         && dcList[j + 1]) {
327                         int sbytes = afs_OtherCSize - dcList[j]->f.chunkBytes;
328                         bytes += sbytes;
329                     }
330                 }
331                 if (bytes && (j == high || !dcList[j + 1])) {
332                     /* base = AFS_CHUNKTOBASE(dcList[first]->f.chunk); */
333                     base = AFS_CHUNKTOBASE(first + minj);
334                     /*
335                      * 
336                      * take a list of dcache structs and send them all off to the server
337                      * the list must be in order, and the chunks contiguous.
338                      * Note - there is no locking done by this code currently.  For
339                      * safety's sake, xdcache could be locked over the entire call.
340                      * However, that pretty well ties up all the threads.  Meantime, all
341                      * the chunks _MUST_ have their refcounts bumped.
342                      * The writes done before a store back will clear setuid-ness
343                      * in cache file.
344                      * We can permit CacheStoreProc to wake up the user process IFF we 
345                      * are doing the last RPC for this close, ie, storing back the last 
346                      * set of contiguous chunks of a file.
347                      */
348
349                     dclist = &dcList[first];
350                     nchunks = 1 + j - first;
351                     nomore = !(moredata || (j != high));
352                     InStatus.ClientModTime = avc->m.Date;
353                     InStatus.Mask = AFS_SETMODTIME;
354                     if (sync & AFS_SYNC) {
355                         InStatus.Mask |= AFS_FSYNC;
356                     }
357                     tlen = lmin(avc->m.Length, avc->truncPos);
358                     afs_Trace4(afs_iclSetp, CM_TRACE_STOREDATA64,
359                                ICL_TYPE_FID, &avc->fid.Fid, ICL_TYPE_OFFSET,
360                                ICL_HANDLE_OFFSET(base), ICL_TYPE_OFFSET,
361                                ICL_HANDLE_OFFSET(bytes), ICL_TYPE_OFFSET,
362                                ICL_HANDLE_OFFSET(tlen));
363
364                     do {
365                         stored = 0;
366                         tc = afs_Conn(&avc->fid, areq, 0);
367                         if (tc) {
368                           restart:
369                             RX_AFS_GUNLOCK();
370                             tcall = rx_NewCall(tc->id);
371 #ifdef AFS_64BIT_CLIENT
372                             if (!afs_serverHasNo64Bit(tc)) {
373                                 code =
374                                     StartRXAFS_StoreData64(tcall,
375                                                            (struct AFSFid *)
376                                                            &avc->fid.Fid,
377                                                            &InStatus, base,
378                                                            bytes, tlen);
379                             } else {
380                                 if (tlen > 0xFFFFFFFF) {
381                                     code = EFBIG;
382                                 } else {
383                                     afs_int32 t1, t2, t3;
384                                     t1 = base;
385                                     t2 = bytes;
386                                     t3 = tlen;
387                                     code =
388                                         StartRXAFS_StoreData(tcall,
389                                                              (struct AFSFid *)
390                                                              &avc->fid.Fid,
391                                                              &InStatus, t1,
392                                                              t2, t3);
393                                 }
394                             }
395 #else /* AFS_64BIT_CLIENT */
396                             code =
397                                 StartRXAFS_StoreData(tcall,
398                                                      (struct AFSFid *)&avc->
399                                                      fid.Fid, &InStatus, base,
400                                                      bytes, tlen);
401 #endif /* AFS_64BIT_CLIENT */
402                             RX_AFS_GLOCK();
403                         } else {
404                             code = -1;
405                             tcall = NULL;
406                         }
407                         if (!code) {
408                             XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_STOREDATA);
409                             avc->truncPos = AFS_NOTRUNC;
410                         }
411                         for (i = 0; i < nchunks && !code; i++) {
412                             tdc = dclist[i];
413                             if (!tdc) {
414                                 afs_warn("afs: missing dcache!\n");
415                                 storeallmissing++;
416                                 continue;       /* panic? */
417                             }
418                             afs_Trace4(afs_iclSetp, CM_TRACE_STOREALL2,
419                                        ICL_TYPE_POINTER, avc, ICL_TYPE_INT32,
420                                        tdc->f.chunk, ICL_TYPE_INT32,
421                                        tdc->index, ICL_TYPE_INT32,
422                                        tdc->f.inode);
423                             shouldwake = 0;
424                             if (nomore) {
425                                 if (avc->asynchrony == -1) {
426                                     if (afs_defaultAsynchrony >
427                                         (bytes - stored)) {
428                                         shouldwake = &nomore;
429                                     }
430                                 } else if ((afs_uint32) avc->asynchrony >=
431                                            (bytes - stored)) {
432                                     shouldwake = &nomore;
433                                 }
434                             }
435 #if defined(LINUX_USE_FH)
436                             tfile = afs_CFileOpen(&tdc->f.fh, tdc->f.fh_type);
437 #else
438                             tfile = afs_CFileOpen(tdc->f.inode);
439 #endif
440 #ifndef AFS_NOSTATS
441                             xferP =
442                                 &(afs_stats_cmfullperf.rpc.
443                                   fsXferTimes
444                                   [AFS_STATS_FS_XFERIDX_STOREDATA]);
445                             osi_GetuTime(&xferStartTime);
446
447                             code =
448                                 afs_CacheStoreProc(tcall, tfile,
449                                                    tdc->f.chunkBytes, avc,
450                                                    shouldwake, &bytesToXfer,
451                                                    &bytesXferred);
452
453                             osi_GetuTime(&xferStopTime);
454                             (xferP->numXfers)++;
455                             if (!code) {
456                                 (xferP->numSuccesses)++;
457                                 afs_stats_XferSumBytes
458                                     [AFS_STATS_FS_XFERIDX_STOREDATA] +=
459                                     bytesXferred;
460                                 (xferP->sumBytes) +=
461                                     (afs_stats_XferSumBytes
462                                      [AFS_STATS_FS_XFERIDX_STOREDATA] >> 10);
463                                 afs_stats_XferSumBytes
464                                     [AFS_STATS_FS_XFERIDX_STOREDATA] &= 0x3FF;
465                                 if (bytesXferred < xferP->minBytes)
466                                     xferP->minBytes = bytesXferred;
467                                 if (bytesXferred > xferP->maxBytes)
468                                     xferP->maxBytes = bytesXferred;
469
470                                 /*
471                                  * Tally the size of the object.  Note: we tally the actual size,
472                                  * NOT the number of bytes that made it out over the wire.
473                                  */
474                                 if (bytesToXfer <= AFS_STATS_MAXBYTES_BUCKET0)
475                                     (xferP->count[0])++;
476                                 else if (bytesToXfer <=
477                                          AFS_STATS_MAXBYTES_BUCKET1)
478                                     (xferP->count[1])++;
479                                 else if (bytesToXfer <=
480                                          AFS_STATS_MAXBYTES_BUCKET2)
481                                     (xferP->count[2])++;
482                                 else if (bytesToXfer <=
483                                          AFS_STATS_MAXBYTES_BUCKET3)
484                                     (xferP->count[3])++;
485                                 else if (bytesToXfer <=
486                                          AFS_STATS_MAXBYTES_BUCKET4)
487                                     (xferP->count[4])++;
488                                 else if (bytesToXfer <=
489                                          AFS_STATS_MAXBYTES_BUCKET5)
490                                     (xferP->count[5])++;
491                                 else if (bytesToXfer <=
492                                          AFS_STATS_MAXBYTES_BUCKET6)
493                                     (xferP->count[6])++;
494                                 else if (bytesToXfer <=
495                                          AFS_STATS_MAXBYTES_BUCKET7)
496                                     (xferP->count[7])++;
497                                 else
498                                     (xferP->count[8])++;
499
500                                 afs_stats_GetDiff(elapsedTime, xferStartTime,
501                                                   xferStopTime);
502                                 afs_stats_AddTo((xferP->sumTime),
503                                                 elapsedTime);
504                                 afs_stats_SquareAddTo((xferP->sqrTime),
505                                                       elapsedTime);
506                                 if (afs_stats_TimeLessThan
507                                     (elapsedTime, (xferP->minTime))) {
508                                     afs_stats_TimeAssign((xferP->minTime),
509                                                          elapsedTime);
510                                 }
511                                 if (afs_stats_TimeGreaterThan
512                                     (elapsedTime, (xferP->maxTime))) {
513                                     afs_stats_TimeAssign((xferP->maxTime),
514                                                          elapsedTime);
515                                 }
516                             }
517 #else
518                             code =
519                                 afs_CacheStoreProc(tcall, tfile,
520                                                    tdc->f.chunkBytes, avc,
521                                                    shouldwake, &lp1, &lp2);
522 #endif /* AFS_NOSTATS */
523                             afs_CFileClose(tfile);
524                             if ((tdc->f.chunkBytes < afs_OtherCSize)
525                                 && (i < (nchunks - 1)) && code == 0) {
526                                 int bsent, tlen, sbytes =
527                                     afs_OtherCSize - tdc->f.chunkBytes;
528                                 char *tbuffer =
529                                     osi_AllocLargeSpace(AFS_LRALLOCSIZ);
530
531                                 while (sbytes > 0) {
532                                     tlen =
533                                         (sbytes >
534                                          AFS_LRALLOCSIZ ? AFS_LRALLOCSIZ :
535                                          sbytes);
536                                     memset(tbuffer, 0, tlen);
537                                     RX_AFS_GUNLOCK();
538                                     bsent = rx_Write(tcall, tbuffer, tlen);
539                                     RX_AFS_GLOCK();
540
541                                     if (bsent != tlen) {
542                                         code = -33;     /* XXX */
543                                         break;
544                                     }
545                                     sbytes -= tlen;
546                                 }
547                                 osi_FreeLargeSpace(tbuffer);
548                             }
549                             stored += tdc->f.chunkBytes;
550
551                             /* ideally, I'd like to unlock the dcache and turn
552                              * off the writing bit here, but that would
553                              * require being able to retry StoreAllSegments in
554                              * the event of a failure. It only really matters
555                              * if user can't read from a 'locked' dcache or
556                              * one which has the writing bit turned on. */
557                         }
558                         if (!code) {
559                             struct AFSVolSync tsync;
560                             RX_AFS_GUNLOCK();
561                             code =
562                                 EndRXAFS_StoreData(tcall, &OutStatus, &tsync);
563                             RX_AFS_GLOCK();
564                             hadd32(newDV, 1);
565                             XSTATS_END_TIME;
566                             if (!code)
567                                 doProcessFS = 1;        /* Flag to run afs_ProcessFS() later on */
568                         }
569                         if (tcall) {
570                             afs_int32 code2;
571                             RX_AFS_GUNLOCK();
572                             code2 = rx_EndCall(tcall, code);
573                             RX_AFS_GLOCK();
574                             if (code2)
575                                 code = code2;
576                         }
577 #ifdef AFS_64BIT_CLIENT
578                         if (code == RXGEN_OPCODE && !afs_serverHasNo64Bit(tc)) {
579                             afs_serverSetNo64Bit(tc);
580                             goto restart;
581                         }
582 #endif /* AFS_64BIT_CLIENT */
583                     } while (afs_Analyze
584                              (tc, code, &avc->fid, areq,
585                               AFS_STATS_FS_RPCIDX_STOREDATA, SHARED_LOCK,
586                               NULL));
587
588                     /* put back all remaining locked dcache entries */
589                     for (i = 0; i < nchunks; i++) {
590                         tdc = dclist[i];
591                         if (!code) {
592                             if (afs_indexFlags[tdc->index] & IFDataMod) {
593                                 /*
594                                  * LOCKXXX -- should hold afs_xdcache(W) when
595                                  * modifying afs_indexFlags.
596                                  */
597                                 afs_indexFlags[tdc->index] &= ~IFDataMod;
598                                 afs_stats_cmperf.cacheCurrDirtyChunks--;
599                                 afs_indexFlags[tdc->index] &= ~IFDirtyPages;
600                                 if (sync & AFS_VMSYNC_INVAL) {
601                                     /* since we have invalidated all the pages of this
602                                      ** vnode by calling osi_VM_TryToSmush, we can
603                                      ** safely mark this dcache entry as not having
604                                      ** any pages. This vnode now becomes eligible for
605                                      ** reclamation by getDownD.
606                                      */
607                                     afs_indexFlags[tdc->index] &= ~IFAnyPages;
608                                 }
609                             }
610                         }
611                         UpgradeSToWLock(&tdc->lock, 628);
612                         tdc->f.states &= ~DWriting;     /* correct? */
613                         tdc->dflags |= DFEntryMod;
614                         ReleaseWriteLock(&tdc->lock);
615                         afs_PutDCache(tdc);
616                         /* Mark the entry as released */
617                         dclist[i] = NULL;
618                     }
619
620                     if (doProcessFS) {
621                         /* Now copy out return params */
622                         UpgradeSToWLock(&avc->lock, 28);        /* keep out others for a while */
623                         afs_ProcessFS(avc, &OutStatus, areq);
624                         /* Keep last (max) size of file on server to see if
625                          * we need to call afs_StoreMini to extend the file.
626                          */
627                         if (!moredata)
628                             maxStoredLength = OutStatus.Length;
629                         ConvertWToSLock(&avc->lock);
630                         doProcessFS = 0;
631                     }
632
633                     if (code) {
634                         for (j++; j <= high; j++) {
635                             if (dcList[j]) {
636                                 ReleaseSharedLock(&(dcList[j]->lock));
637                                 afs_PutDCache(dcList[j]);
638                                 /* Releasing entry */
639                                 dcList[j] = NULL;
640                             }
641                         }
642                     }
643
644                     afs_Trace2(afs_iclSetp, CM_TRACE_STOREALLDCDONE,
645                                ICL_TYPE_POINTER, avc, ICL_TYPE_INT32, code);
646                     bytes = 0;
647                 }
648             }
649
650             /* Release any zero-length dcache entries in our interval
651              * that we locked but didn't store back above.
652              */
653             for (j = 0; j <= high; j++) {
654                 tdc = dcList[j];
655                 if (tdc) {
656                     osi_Assert(tdc->f.chunkBytes == 0);
657                     ReleaseSharedLock(&tdc->lock);
658                     afs_PutDCache(tdc);
659                 }
660             }
661         }
662         /* if (j) */
663         minj += NCHUNKSATONCE;
664     } while (!code && moredata);
665
666     UpgradeSToWLock(&avc->lock, 29);
667
668     /* send a trivial truncation store if did nothing else */
669     if (code == 0) {
670         /*
671          * Call StoreMini if we haven't written enough data to extend the
672          * file at the fileserver to the client's notion of the file length.
673          */
674         if ((avc->truncPos != AFS_NOTRUNC) || ((avc->states & CExtendedFile)
675                                                && (maxStoredLength <
676                                                    avc->m.Length))) {
677             code = afs_StoreMini(avc, areq);
678             if (code == 0)
679                 hadd32(newDV, 1);       /* just bumped here, too */
680         }
681         avc->states &= ~CExtendedFile;
682     }
683
684     /*
685      * Finally, turn off DWriting, turn on DFEntryMod,
686      * update f.versionNo.
687      * A lot of this could be integrated into the loop above 
688      */
689     if (!code) {
690         afs_hyper_t h_unset;
691         hones(h_unset);
692
693         minj = 0;
694
695         do {
696             moredata = FALSE;
697             memset((char *)dcList, 0,
698                    NCHUNKSATONCE * sizeof(struct dcache *));
699
700             /* overkill, but it gets the lock in case GetDSlot needs it */
701             MObtainWriteLock(&afs_xdcache, 285);
702
703             for (j = 0, safety = 0, index = afs_dvhashTbl[hash];
704                  index != NULLIDX && safety < afs_cacheFiles + 2;) {
705
706                 if (afs_indexUnique[index] == avc->fid.Fid.Unique) {
707                     tdc = afs_GetDSlot(index, 0);
708                     ReleaseReadLock(&tdc->tlock);
709
710                     if (!FidCmp(&tdc->f.fid, &avc->fid)
711                         && tdc->f.chunk >= minj) {
712                         off = tdc->f.chunk - minj;
713                         if (off < NCHUNKSATONCE) {
714                             /* this is the file, and the correct chunk range */
715                             if (j >= NCHUNKSATONCE)
716                                 osi_Panic
717                                     ("Too many dcache entries in range\n");
718                             dcList[j++] = tdc;
719                         } else {
720                             moredata = TRUE;
721                             afs_PutDCache(tdc);
722                             if (j == NCHUNKSATONCE)
723                                 break;
724                         }
725                     } else {
726                         afs_PutDCache(tdc);
727                     }
728                 }
729
730                 index = afs_dvnextTbl[index];
731             }
732             MReleaseWriteLock(&afs_xdcache);
733
734             for (i = 0; i < j; i++) {
735                 /* Iterate over the dcache entries we collected above */
736                 tdc = dcList[i];
737                 ObtainSharedLock(&tdc->lock, 677);
738
739                 /* was code here to clear IFDataMod, but it should only be done
740                  * in storedcache and storealldcache.
741                  */
742                 /* Only increase DV if we had up-to-date data to start with.
743                  * Otherwise, we could be falsely upgrading an old chunk
744                  * (that we never read) into one labelled with the current
745                  * DV #.  Also note that we check that no intervening stores
746                  * occurred, otherwise we might mislabel cache information
747                  * for a chunk that we didn't store this time
748                  */
749                 /* Don't update the version number if it's not yet set. */
750                 if (!hsame(tdc->f.versionNo, h_unset)
751                     && hcmp(tdc->f.versionNo, oldDV) >= 0) {
752
753                     if ((!(afs_dvhack || foreign)
754                          && hsame(avc->m.DataVersion, newDV))
755                         || ((afs_dvhack || foreign)
756                             && (origCBs == afs_allCBs))) {
757                         /* no error, this is the DV */
758
759                         UpgradeSToWLock(&tdc->lock, 678);
760                         hset(tdc->f.versionNo, avc->m.DataVersion);
761                         tdc->dflags |= DFEntryMod;
762                         ConvertWToSLock(&tdc->lock);
763                     }
764                 }
765
766                 ReleaseSharedLock(&tdc->lock);
767                 afs_PutDCache(tdc);
768             }
769
770             minj += NCHUNKSATONCE;
771
772         } while (moredata);
773     }
774
775     if (code) {
776         /*
777          * Invalidate chunks after an error for ccores files since
778          * afs_inactive won't be called for these and they won't be
779          * invalidated. Also discard data if it's a permanent error from the
780          * fileserver.
781          */
782         if (areq->permWriteError || (avc->states & (CCore1 | CCore))) {
783             afs_InvalidateAllSegments(avc);
784         }
785     }
786     afs_Trace3(afs_iclSetp, CM_TRACE_STOREALLDONE, ICL_TYPE_POINTER, avc,
787                ICL_TYPE_INT32, avc->m.Length, ICL_TYPE_INT32, code);
788     /* would like a Trace5, but it doesn't exist... */
789     afs_Trace3(afs_iclSetp, CM_TRACE_AVCLOCKER, ICL_TYPE_POINTER, avc,
790                ICL_TYPE_INT32, avc->lock.wait_states, ICL_TYPE_INT32,
791                avc->lock.excl_locked);
792     afs_Trace4(afs_iclSetp, CM_TRACE_AVCLOCKEE, ICL_TYPE_POINTER, avc,
793                ICL_TYPE_INT32, avc->lock.wait_states, ICL_TYPE_INT32,
794                avc->lock.readers_reading, ICL_TYPE_INT32,
795                avc->lock.num_waiting);
796
797     /*
798      * Finally, if updated DataVersion matches newDV, we did all of the
799      * stores.  If mapDV indicates that the page cache was flushed up
800      * to when we started the store, then we can relabel them as flushed
801      * as recently as newDV.
802      * Turn off CDirty bit because the stored data is now in sync with server.
803      */
804     if (code == 0 && hcmp(avc->mapDV, oldDV) >= 0) {
805         if ((!(afs_dvhack || foreign) && hsame(avc->m.DataVersion, newDV))
806             || ((afs_dvhack || foreign) && (origCBs == afs_allCBs))) {
807             hset(avc->mapDV, newDV);
808             avc->states &= ~CDirty;
809         }
810     }
811     osi_FreeLargeSpace(dcList);
812
813     /* If not the final write a temporary error is ok. */
814     if (code && !areq->permWriteError && !(sync & AFS_LASTSTORE))
815         code = 0;
816
817     return code;
818
819 }                               /*afs_StoreAllSegments (new 03/02/94) */
820
821
822 /*
823  * afs_InvalidateAllSegments
824  *
825  * Description:
826  *      Invalidates all chunks for a given file
827  *
828  * Parameters:
829  *      avc      : Pointer to vcache entry.
830  *
831  * Environment:
832  *      For example, called after an error has been detected.  Called
833  *      with avc write-locked, and afs_xdcache unheld.
834  */
835
836 int
837 afs_InvalidateAllSegments(struct vcache *avc)
838 {
839     struct dcache *tdc;
840     afs_int32 hash;
841     afs_int32 index;
842     struct dcache **dcList;
843     int i, dcListMax, dcListCount;
844
845     AFS_STATCNT(afs_InvalidateAllSegments);
846     afs_Trace2(afs_iclSetp, CM_TRACE_INVALL, ICL_TYPE_POINTER, avc,
847                ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(avc->m.Length));
848     hash = DVHash(&avc->fid);
849     avc->truncPos = AFS_NOTRUNC;        /* don't truncate later */
850     avc->states &= ~CExtendedFile;      /* not any more */
851     ObtainWriteLock(&afs_xcbhash, 459);
852     afs_DequeueCallback(avc);
853     avc->states &= ~(CStatd | CDirty);  /* mark status information as bad, too */
854     ReleaseWriteLock(&afs_xcbhash);
855     if (avc->fid.Fid.Vnode & 1 || (vType(avc) == VDIR))
856         osi_dnlc_purgedp(avc);
857     /* Blow away pages; for now, only for Solaris */
858 #if     (defined(AFS_SUN5_ENV))
859     if (WriteLocked(&avc->lock))
860         osi_ReleaseVM(avc, (struct AFS_UCRED *)0);
861 #endif
862     /*
863      * Block out others from screwing with this table; is a read lock
864      * sufficient?
865      */
866     MObtainWriteLock(&afs_xdcache, 286);
867     dcListMax = 0;
868
869     for (index = afs_dvhashTbl[hash]; index != NULLIDX;) {
870         if (afs_indexUnique[index] == avc->fid.Fid.Unique) {
871             tdc = afs_GetDSlot(index, 0);
872             ReleaseReadLock(&tdc->tlock);
873             if (!FidCmp(&tdc->f.fid, &avc->fid))
874                 dcListMax++;
875             afs_PutDCache(tdc);
876         }
877         index = afs_dvnextTbl[index];
878     }
879
880     dcList = osi_Alloc(dcListMax * sizeof(struct dcache *));
881     dcListCount = 0;
882
883     for (index = afs_dvhashTbl[hash]; index != NULLIDX;) {
884         if (afs_indexUnique[index] == avc->fid.Fid.Unique) {
885             tdc = afs_GetDSlot(index, 0);
886             ReleaseReadLock(&tdc->tlock);
887             if (!FidCmp(&tdc->f.fid, &avc->fid)) {
888                 /* same file? we'll zap it */
889                 if (afs_indexFlags[index] & IFDataMod) {
890                     afs_stats_cmperf.cacheCurrDirtyChunks--;
891                     /* don't write it back */
892                     afs_indexFlags[index] &= ~IFDataMod;
893                 }
894                 afs_indexFlags[index] &= ~IFAnyPages;
895                 if (dcListCount < dcListMax)
896                     dcList[dcListCount++] = tdc;
897                 else
898                     afs_PutDCache(tdc);
899             } else {
900                 afs_PutDCache(tdc);
901             }
902         }
903         index = afs_dvnextTbl[index];
904     }
905     MReleaseWriteLock(&afs_xdcache);
906
907     for (i = 0; i < dcListCount; i++) {
908         tdc = dcList[i];
909
910         ObtainWriteLock(&tdc->lock, 679);
911         ZapDCE(tdc);
912         if (vType(avc) == VDIR)
913             DZap(tdc);
914         ReleaseWriteLock(&tdc->lock);
915         afs_PutDCache(tdc);
916     }
917
918     osi_Free(dcList, dcListMax * sizeof(struct dcache *));
919
920     return 0;
921 }
922
923
924 /*
925  * afs_TruncateAllSegments
926  *
927  * Description:
928  *      Truncate a cache file.
929  *
930  * Parameters:
931  *      avc  : Ptr to vcache entry to truncate.
932  *      alen : Number of bytes to make the file.
933  *      areq : Ptr to request structure.
934  *
935  * Environment:
936  *      Called with avc write-locked; in VFS40 systems, pvnLock is also
937  *      held.
938  */
939 int
940 afs_TruncateAllSegments(register struct vcache *avc, afs_size_t alen,
941                         struct vrequest *areq, struct AFS_UCRED *acred)
942 {
943     register struct dcache *tdc;
944     register afs_int32 code;
945     register afs_int32 index;
946     afs_int32 newSize;
947
948     int dcCount, dcPos;
949     struct dcache **tdcArray;
950
951     AFS_STATCNT(afs_TruncateAllSegments);
952     avc->m.Date = osi_Time();
953     afs_Trace3(afs_iclSetp, CM_TRACE_TRUNCALL, ICL_TYPE_POINTER, avc,
954                ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(avc->m.Length),
955                ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(alen));
956     if (alen >= avc->m.Length) {
957         /*
958          * Special speedup since Sun's vm extends the file this way;
959          * we've never written to the file thus we can just set the new
960          * length and avoid the needless calls below.
961          * Also used for ftruncate calls which can extend the file.
962          * To completely minimize the possible extra StoreMini RPC, we really
963          * should keep the ExtendedPos as well and clear this flag if we
964          * truncate below that value before we store the file back.
965          */
966         avc->states |= CExtendedFile;
967         avc->m.Length = alen;
968         return 0;
969     }
970 #if     (defined(AFS_SUN5_ENV))
971
972     /* Zero unused portion of last page */
973     osi_VM_PreTruncate(avc, alen, acred);
974
975 #endif
976
977 #if     (defined(AFS_SUN5_ENV))
978     ObtainWriteLock(&avc->vlock, 546);
979     avc->activeV++;             /* Block new getpages */
980     ReleaseWriteLock(&avc->vlock);
981 #endif
982
983     ReleaseWriteLock(&avc->lock);
984     AFS_GUNLOCK();
985
986     /* Flush pages beyond end-of-file. */
987     osi_VM_Truncate(avc, alen, acred);
988
989     AFS_GLOCK();
990     ObtainWriteLock(&avc->lock, 79);
991
992     avc->m.Length = alen;
993
994     if (alen < avc->truncPos)
995         avc->truncPos = alen;
996     code = DVHash(&avc->fid);
997
998     /* block out others from screwing with this table */
999     MObtainWriteLock(&afs_xdcache, 287);
1000
1001     dcCount = 0;
1002     for (index = afs_dvhashTbl[code]; index != NULLIDX;) {
1003         if (afs_indexUnique[index] == avc->fid.Fid.Unique) {
1004             tdc = afs_GetDSlot(index, 0);
1005             ReleaseReadLock(&tdc->tlock);
1006             if (!FidCmp(&tdc->f.fid, &avc->fid))
1007                 dcCount++;
1008             afs_PutDCache(tdc);
1009         }
1010         index = afs_dvnextTbl[index];
1011     }
1012
1013     /* Now allocate space where we can save those dcache entries, and
1014      * do a second pass over them..  Since we're holding xdcache, it
1015      * shouldn't be changing.
1016      */
1017     tdcArray = osi_Alloc(dcCount * sizeof(struct dcache *));
1018     dcPos = 0;
1019
1020     for (index = afs_dvhashTbl[code]; index != NULLIDX;) {
1021         if (afs_indexUnique[index] == avc->fid.Fid.Unique) {
1022             tdc = afs_GetDSlot(index, 0);
1023             ReleaseReadLock(&tdc->tlock);
1024             if (!FidCmp(&tdc->f.fid, &avc->fid)) {
1025                 /* same file, and modified, we'll store it back */
1026                 if (dcPos < dcCount) {
1027                     tdcArray[dcPos++] = tdc;
1028                 } else {
1029                     afs_PutDCache(tdc);
1030                 }
1031             } else {
1032                 afs_PutDCache(tdc);
1033             }
1034         }
1035         index = afs_dvnextTbl[index];
1036     }
1037
1038     MReleaseWriteLock(&afs_xdcache);
1039
1040     /* Now we loop over the array of dcache entries and truncate them */
1041     for (index = 0; index < dcPos; index++) {
1042         struct osi_file *tfile;
1043
1044         tdc = tdcArray[index];
1045
1046         newSize = alen - AFS_CHUNKTOBASE(tdc->f.chunk);
1047         if (newSize < 0)
1048             newSize = 0;
1049         ObtainSharedLock(&tdc->lock, 672);
1050         if (newSize < tdc->f.chunkBytes) {
1051             UpgradeSToWLock(&tdc->lock, 673);
1052 #if defined(LINUX_USE_FH)
1053             tfile = afs_CFileOpen(&tdc->f.fh, tdc->f.fh_type);
1054 #else
1055             tfile = afs_CFileOpen(tdc->f.inode);
1056 #endif
1057             afs_CFileTruncate(tfile, newSize);
1058             afs_CFileClose(tfile);
1059             afs_AdjustSize(tdc, newSize);
1060             if (alen < tdc->validPos) {
1061                 if (alen < AFS_CHUNKTOBASE(tdc->f.chunk))
1062                     tdc->validPos = 0;
1063                 else
1064                     tdc->validPos = alen;
1065             }
1066             ConvertWToSLock(&tdc->lock);
1067         }
1068         ReleaseSharedLock(&tdc->lock);
1069         afs_PutDCache(tdc);
1070     }
1071
1072     osi_Free(tdcArray, dcCount * sizeof(struct dcache *));
1073
1074 #if     (defined(AFS_SUN5_ENV))
1075     ObtainWriteLock(&avc->vlock, 547);
1076     if (--avc->activeV == 0 && (avc->vstates & VRevokeWait)) {
1077         avc->vstates &= ~VRevokeWait;
1078         afs_osi_Wakeup((char *)&avc->vstates);
1079     }
1080     ReleaseWriteLock(&avc->vlock);
1081 #endif
1082     return 0;
1083 }