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