afs: remove stats dead code
[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
17 #include "afs/sysincludes.h"    /*Standard vendor system headers */
18 #include "afsincludes.h"        /*AFS-based standard headers */
19 #include "afs/afs_stats.h"      /* statistics */
20 #include "afs/afs_cbqueue.h"
21 #include "afs/afs_osidnlc.h"
22
23 afs_uint32 afs_stampValue = 0;
24
25 /*
26  * afs_StoreMini
27  *
28  * Description:
29  *      Send a truncation request to a FileServer.
30  *
31  * Parameters:
32  *      xxx : description
33  *
34  * Environment:
35  *      We're write-locked upon entry.
36  */
37
38 static int
39 afs_StoreMini(struct vcache *avc, struct vrequest *areq)
40 {
41     struct afs_conn *tc;
42     struct AFSStoreStatus InStatus;
43     struct AFSFetchStatus OutStatus;
44     struct AFSVolSync tsync;
45     afs_int32 code;
46     struct rx_call *tcall;
47     struct rx_connection *rxconn;
48     afs_size_t tlen, xlen = 0;
49     XSTATS_DECLS;
50     AFS_STATCNT(afs_StoreMini);
51     afs_Trace2(afs_iclSetp, CM_TRACE_STOREMINI, ICL_TYPE_POINTER, avc,
52                ICL_TYPE_INT32, avc->f.m.Length);
53     tlen = avc->f.m.Length;
54     if (avc->f.truncPos < tlen)
55         tlen = avc->f.truncPos;
56     avc->f.truncPos = AFS_NOTRUNC;
57     avc->f.states &= ~CExtendedFile;
58     memset(&InStatus, 0, sizeof(InStatus));
59
60     do {
61         tc = afs_Conn(&avc->f.fid, areq, SHARED_LOCK, &rxconn);
62         if (tc) {
63 #ifdef AFS_64BIT_CLIENT
64           retry:
65 #endif
66             RX_AFS_GUNLOCK();
67             tcall = rx_NewCall(rxconn);
68             RX_AFS_GLOCK();
69             /* Set the client mod time since we always want the file
70              * to have the client's mod time and not the server's one
71              * (to avoid problems with make, etc.) It almost always
72              * works fine with standard afs because them server/client
73              * times are in sync and more importantly this storemini
74              * it's a special call that would typically be followed by
75              * the proper store-data or store-status calls.
76              */
77             InStatus.Mask = AFS_SETMODTIME;
78             InStatus.ClientModTime = avc->f.m.Date;
79             XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_STOREDATA);
80             afs_Trace4(afs_iclSetp, CM_TRACE_STOREDATA64, ICL_TYPE_FID,
81                        &avc->f.fid.Fid, ICL_TYPE_OFFSET,
82                        ICL_HANDLE_OFFSET(avc->f.m.Length), ICL_TYPE_OFFSET,
83                        ICL_HANDLE_OFFSET(xlen), ICL_TYPE_OFFSET,
84                        ICL_HANDLE_OFFSET(tlen));
85             RX_AFS_GUNLOCK();
86 #ifdef AFS_64BIT_CLIENT
87             if (!afs_serverHasNo64Bit(tc)) {
88                 code =
89                     StartRXAFS_StoreData64(tcall,
90                                            (struct AFSFid *)&avc->f.fid.Fid,
91                                            &InStatus, avc->f.m.Length,
92                                            (afs_size_t) 0, tlen);
93             } else {
94                 afs_int32 l1, l2;
95                 l1 = avc->f.m.Length;
96                 l2 = tlen;
97                 if ((avc->f.m.Length > 0x7fffffff) ||
98                     (tlen > 0x7fffffff) ||
99                     ((0x7fffffff - tlen) < avc->f.m.Length)) {
100                     code = EFBIG;
101                     goto error;
102                 }
103                 code =
104                     StartRXAFS_StoreData(tcall,
105                                          (struct AFSFid *)&avc->f.fid.Fid,
106                                          &InStatus, l1, 0, l2);
107             }
108 #else /* AFS_64BIT_CLIENT */
109             code =
110                 StartRXAFS_StoreData(tcall, (struct AFSFid *)&avc->f.fid.Fid,
111                                      &InStatus, avc->f.m.Length, 0, tlen);
112 #endif /* AFS_64BIT_CLIENT */
113             if (code == 0) {
114                 code = EndRXAFS_StoreData(tcall, &OutStatus, &tsync);
115             }
116 #ifdef AFS_64BIT_CLIENT
117         error:
118 #endif
119             code = rx_EndCall(tcall, code);
120             RX_AFS_GLOCK();
121             XSTATS_END_TIME;
122 #ifdef AFS_64BIT_CLIENT
123             if (code == RXGEN_OPCODE && !afs_serverHasNo64Bit(tc)) {
124                 afs_serverSetNo64Bit(tc);
125                 goto retry;
126             }
127 #endif /* AFS_64BIT_CLIENT */
128         } else
129             code = -1;
130     } while (afs_Analyze
131              (tc, rxconn, code, &avc->f.fid, areq, AFS_STATS_FS_RPCIDX_STOREDATA,
132               SHARED_LOCK, NULL));
133
134     if (code == 0)
135         afs_ProcessFS(avc, &OutStatus, areq);
136
137     return code;
138 }                               /*afs_StoreMini */
139
140 /*
141  * afs_StoreAllSegments
142  *
143  * Description:
144  *      Stores all modified segments back to server
145  *
146  * Parameters:
147  *      avc  : Pointer to vcache entry.
148  *      areq : Pointer to request structure.
149  *
150  * Environment:
151  *      Called with avc write-locked.
152  */
153 #if defined (AFS_HPUX_ENV)
154 int NCHUNKSATONCE = 3;
155 #else
156 int NCHUNKSATONCE = 64;
157 #endif
158 int afs_dvhack = 0;
159
160
161 int
162 afs_StoreAllSegments(struct vcache *avc, struct vrequest *areq,
163                      int sync)
164 {
165     struct dcache *tdc;
166     afs_int32 code = 0;
167     afs_int32 index;
168     afs_int32 origCBs, foreign = 0;
169     int hash;
170     afs_hyper_t newDV, oldDV;   /* DV when we start, and finish, respectively */
171     struct dcache **dcList;
172     unsigned int i, j, minj, moredata, high, off;
173     afs_size_t maxStoredLength; /* highest offset we've written to server. */
174     int safety, marineronce = 0;
175
176     AFS_STATCNT(afs_StoreAllSegments);
177
178     hash = DVHash(&avc->f.fid);
179     foreign = (avc->f.states & CForeign);
180     dcList = osi_AllocLargeSpace(AFS_LRALLOCSIZ);
181     afs_Trace2(afs_iclSetp, CM_TRACE_STOREALL, ICL_TYPE_POINTER, avc,
182                ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(avc->f.m.Length));
183 #if !defined(AFS_AIX32_ENV) && !defined(AFS_SGI65_ENV)
184     /* In the aix vm implementation we need to do the vm_writep even
185      * on the memcache case since that's we adjust the file's size
186      * and finish flushing partial vm pages.
187      */
188     if ((cacheDiskType != AFS_FCACHE_TYPE_MEM) ||
189         (sync & AFS_VMSYNC_INVAL) || (sync & AFS_VMSYNC) ||
190         (sync & AFS_LASTSTORE))
191 #endif /* !AFS_AIX32_ENV && !AFS_SGI65_ENV */
192     {
193         /* If we're not diskless, reading a file may stress the VM
194          * system enough to cause a pageout, and this vnode would be
195          * locked when the pageout occurs.  We can prevent this problem
196          * by making sure all dirty pages are already flushed.  We don't
197          * do this when diskless because reading a diskless (i.e.
198          * memory-resident) chunk doesn't require using new VM, and we
199          * also don't want to dump more dirty data into a diskless cache,
200          * since they're smaller, and we might exceed its available
201          * space.
202          */
203 #if     defined(AFS_SUN5_ENV)
204         if (sync & AFS_VMSYNC_INVAL)    /* invalidate VM pages */
205             osi_VM_TryToSmush(avc, CRED(), 1);
206         else
207 #endif
208             osi_VM_StoreAllSegments(avc);
209     }
210     if (AFS_IS_DISCONNECTED && !AFS_IN_SYNC) {
211         /* This will probably make someone sad ... */
212         /*printf("Net down in afs_StoreSegments\n");*/
213         return ENETDOWN;
214     }
215
216     /*
217      * Can't do this earlier because osi_VM_StoreAllSegments drops locks
218      * and can indirectly do some stores that increase the DV.
219      */
220     hset(oldDV, avc->f.m.DataVersion);
221     hset(newDV, avc->f.m.DataVersion);
222
223     ConvertWToSLock(&avc->lock);
224
225     /*
226      * Subsequent code expects a sorted list, and it expects all the
227      * chunks in the list to be contiguous, so we need a sort and a
228      * while loop in here, too - but this will work for a first pass...
229      * 92.10.05 - OK, there's a sort in here now.  It's kind of a modified
230      *            bin sort, I guess.  Chunk numbers start with 0
231      *
232      * - Have to get a write lock on xdcache because GetDSlot might need it (if
233      *   the chunk doesn't have a dcache struct).
234      *   This seems like overkill in most cases.
235      * - I'm not sure that it's safe to do "index = .hvNextp", then unlock
236      *   xdcache, then relock xdcache and try to use index.  It is done
237      *   a lot elsewhere in the CM, but I'm not buying that argument.
238      * - should be able to check IFDataMod without doing the GetDSlot (just
239      *   hold afs_xdcache).  That way, it's easy to do this without the
240      *   writelock on afs_xdcache, and we save unneccessary disk
241      *   operations. I don't think that works, 'cuz the next pointers
242      *   are still on disk.
243      */
244     origCBs = afs_allCBs;
245
246     maxStoredLength = 0;
247     minj = 0;
248
249     do {
250         memset(dcList, 0, NCHUNKSATONCE * sizeof(struct dcache *));
251         high = 0;
252         moredata = FALSE;
253
254         /* lock and start over from beginning of hash chain
255          * in order to avoid a race condition. */
256         ObtainWriteLock(&afs_xdcache, 284);
257         index = afs_dvhashTbl[hash];
258
259         for (j = 0; index != NULLIDX;) {
260             if ((afs_indexFlags[index] & IFDataMod)
261                 && (afs_indexUnique[index] == avc->f.fid.Fid.Unique)) {
262                 tdc = afs_GetValidDSlot(index); /* refcount+1. */
263                 if (!tdc) {
264                     ReleaseWriteLock(&afs_xdcache);
265                     code = EIO;
266                     goto done;
267                 }
268                 ReleaseReadLock(&tdc->tlock);
269                 if (!FidCmp(&tdc->f.fid, &avc->f.fid) && tdc->f.chunk >= minj) {
270                     off = tdc->f.chunk - minj;
271                     if (off < NCHUNKSATONCE) {
272                         if (dcList[off])
273                             osi_Panic("dclist slot already in use!");
274                         if (afs_mariner && !marineronce) {
275                             /* first chunk only */
276                             afs_MarinerLog("store$Storing", avc);
277                             marineronce++;
278                         }
279                         dcList[off] = tdc;
280                         if (off > high)
281                             high = off;
282                         j++;
283                         /* DCLOCKXXX: chunkBytes is protected by tdc->lock which we
284                          * can't grab here, due to lock ordering with afs_xdcache.
285                          * So, disable this shortcut for now.  -- kolya 2001-10-13
286                          */
287                         /* shortcut: big win for little files */
288                         /* tlen -= tdc->f.chunkBytes;
289                          * if (tlen <= 0)
290                          *    break;
291                          */
292                     } else {
293                         moredata = TRUE;
294                         afs_PutDCache(tdc);
295                         if (j == NCHUNKSATONCE)
296                             break;
297                     }
298                 } else {
299                     afs_PutDCache(tdc);
300                 }
301             }
302             index = afs_dvnextTbl[index];
303         }
304         ReleaseWriteLock(&afs_xdcache);
305
306         /* this guy writes chunks, puts back dcache structs, and bumps newDV */
307         /* "moredata" just says "there are more dirty chunks yet to come".
308          */
309         if (j) {
310             code =
311                 afs_CacheStoreVCache(dcList, avc, areq, sync,
312                                    minj, high, moredata,
313                                    &newDV, &maxStoredLength);
314             /* Release any zero-length dcache entries in our interval
315              * that we locked but didn't store back above.
316              */
317             for (j = 0; j <= high; j++) {
318                 tdc = dcList[j];
319                 if (tdc) {
320                     osi_Assert(tdc->f.chunkBytes == 0);
321                     ReleaseSharedLock(&tdc->lock);
322                     afs_PutDCache(tdc);
323                 }
324             }
325         }
326         /* if (j) */
327         minj += NCHUNKSATONCE;
328     } while (!code && moredata);
329
330  done:
331     UpgradeSToWLock(&avc->lock, 29);
332
333     /* send a trivial truncation store if did nothing else */
334     if (code == 0) {
335         /*
336          * Call StoreMini if we haven't written enough data to extend the
337          * file at the fileserver to the client's notion of the file length.
338          */
339         if ((avc->f.truncPos != AFS_NOTRUNC)
340             || ((avc->f.states & CExtendedFile)
341                 && (maxStoredLength < avc->f.m.Length))) {
342             code = afs_StoreMini(avc, areq);
343             if (code == 0)
344                 hadd32(newDV, 1);       /* just bumped here, too */
345         }
346         avc->f.states &= ~CExtendedFile;
347     }
348
349     /*
350      * Finally, turn off DWriting, turn on DFEntryMod,
351      * update f.versionNo.
352      * A lot of this could be integrated into the loop above
353      */
354     if (!code) {
355         afs_hyper_t h_unset;
356         hones(h_unset);
357
358         minj = 0;
359
360         do {
361             moredata = FALSE;
362             memset(dcList, 0,
363                    NCHUNKSATONCE * sizeof(struct dcache *));
364
365             /* overkill, but it gets the lock in case GetDSlot needs it */
366             ObtainWriteLock(&afs_xdcache, 285);
367
368             for (j = 0, safety = 0, index = afs_dvhashTbl[hash];
369                  index != NULLIDX && safety < afs_cacheFiles + 2;
370                  index = afs_dvnextTbl[index]) {
371
372                 if (afs_indexUnique[index] == avc->f.fid.Fid.Unique) {
373                     tdc = afs_GetValidDSlot(index);
374                     if (!tdc) {
375                         /* This is okay; since manipulating the dcaches at this
376                          * point is best-effort. We only get a dcache here to
377                          * increment the dv and turn off DWriting. If we were
378                          * supposed to do that for a dcache, but could not
379                          * due to an I/O error, it just means the dv won't
380                          * be updated so we don't be able to use that cached
381                          * chunk in the future. That's inefficient, but not
382                          * an error. */
383                         break;
384                     }
385                     ReleaseReadLock(&tdc->tlock);
386
387                     if (!FidCmp(&tdc->f.fid, &avc->f.fid)
388                         && tdc->f.chunk >= minj) {
389                         off = tdc->f.chunk - minj;
390                         if (off < NCHUNKSATONCE) {
391                             /* this is the file, and the correct chunk range */
392                             if (j >= NCHUNKSATONCE)
393                                 osi_Panic
394                                     ("Too many dcache entries in range\n");
395                             dcList[j++] = tdc;
396                         } else {
397                             moredata = TRUE;
398                             afs_PutDCache(tdc);
399                             if (j == NCHUNKSATONCE)
400                                 break;
401                         }
402                     } else {
403                         afs_PutDCache(tdc);
404                     }
405                 }
406             }
407             ReleaseWriteLock(&afs_xdcache);
408
409             for (i = 0; i < j; i++) {
410                 /* Iterate over the dcache entries we collected above */
411                 tdc = dcList[i];
412                 ObtainSharedLock(&tdc->lock, 677);
413
414                 /* was code here to clear IFDataMod, but it should only be done
415                  * in storedcache and storealldcache.
416                  */
417                 /* Only increase DV if we had up-to-date data to start with.
418                  * Otherwise, we could be falsely upgrading an old chunk
419                  * (that we never read) into one labelled with the current
420                  * DV #.  Also note that we check that no intervening stores
421                  * occurred, otherwise we might mislabel cache information
422                  * for a chunk that we didn't store this time
423                  */
424                 /* Don't update the version number if it's not yet set. */
425                 if (!hsame(tdc->f.versionNo, h_unset)
426                     && hcmp(tdc->f.versionNo, oldDV) >= 0) {
427
428                     if ((!(afs_dvhack || foreign)
429                          && hsame(avc->f.m.DataVersion, newDV))
430                         || ((afs_dvhack || foreign)
431                             && (origCBs == afs_allCBs))) {
432                         /* no error, this is the DV */
433
434                         UpgradeSToWLock(&tdc->lock, 678);
435                         hset(tdc->f.versionNo, avc->f.m.DataVersion);
436                         tdc->dflags |= DFEntryMod;
437                         /* DWriting may not have gotten cleared above, if all
438                          * we did was a StoreMini */
439                         tdc->f.states &= ~DWriting;
440                         ConvertWToSLock(&tdc->lock);
441                     }
442                 }
443
444                 ReleaseSharedLock(&tdc->lock);
445                 afs_PutDCache(tdc);
446             }
447
448             minj += NCHUNKSATONCE;
449
450         } while (moredata);
451     }
452
453     if (code) {
454         /*
455          * Invalidate chunks after an error for ccores files since
456          * afs_inactive won't be called for these and they won't be
457          * invalidated. Also discard data if it's a permanent error from the
458          * fileserver.
459          */
460         if (areq->permWriteError || (avc->f.states & CCore)) {
461             afs_InvalidateAllSegments(avc);
462         }
463     }
464     afs_Trace3(afs_iclSetp, CM_TRACE_STOREALLDONE, ICL_TYPE_POINTER, avc,
465                ICL_TYPE_INT32, avc->f.m.Length, ICL_TYPE_INT32, code);
466     /* would like a Trace5, but it doesn't exist... */
467     afs_Trace3(afs_iclSetp, CM_TRACE_AVCLOCKER, ICL_TYPE_POINTER, avc,
468                ICL_TYPE_INT32, avc->lock.wait_states, ICL_TYPE_INT32,
469                avc->lock.excl_locked);
470     afs_Trace4(afs_iclSetp, CM_TRACE_AVCLOCKEE, ICL_TYPE_POINTER, avc,
471                ICL_TYPE_INT32, avc->lock.wait_states, ICL_TYPE_INT32,
472                avc->lock.readers_reading, ICL_TYPE_INT32,
473                avc->lock.num_waiting);
474
475     /*
476      * Finally, if updated DataVersion matches newDV, we did all of the
477      * stores.  If mapDV indicates that the page cache was flushed up
478      * to when we started the store, then we can relabel them as flushed
479      * as recently as newDV.
480      * Turn off CDirty bit because the stored data is now in sync with server.
481      */
482     if (code == 0 && hcmp(avc->mapDV, oldDV) >= 0) {
483         if ((!(afs_dvhack || foreign) && hsame(avc->f.m.DataVersion, newDV))
484             || ((afs_dvhack || foreign) && (origCBs == afs_allCBs))) {
485             hset(avc->mapDV, newDV);
486             avc->f.states &= ~CDirty;
487         }
488     }
489     osi_FreeLargeSpace(dcList);
490
491     /* If not the final write a temporary error is ok. */
492     if (code && !areq->permWriteError && !(sync & AFS_LASTSTORE))
493         code = 0;
494
495     return code;
496
497 }                               /*afs_StoreAllSegments (new 03/02/94) */
498
499 int
500 afs_InvalidateAllSegments_once(struct vcache *avc)
501 {
502     struct dcache *tdc;
503     afs_int32 hash;
504     afs_int32 index;
505     struct dcache **dcList = NULL;
506     int i, dcListMax, dcListCount = 0;
507
508     AFS_STATCNT(afs_InvalidateAllSegments);
509     afs_Trace2(afs_iclSetp, CM_TRACE_INVALL, ICL_TYPE_POINTER, avc,
510                ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(avc->f.m.Length));
511     hash = DVHash(&avc->f.fid);
512     avc->f.truncPos = AFS_NOTRUNC;      /* don't truncate later */
513     avc->f.states &= ~CExtendedFile;    /* not any more */
514     afs_StaleVCacheFlags(avc, 0, CDirty);
515     /* Blow away pages; for now, only for Solaris */
516 #if     (defined(AFS_SUN5_ENV))
517     if (WriteLocked(&avc->lock))
518         osi_ReleaseVM(avc, (afs_ucred_t *)0);
519 #endif
520     /*
521      * Block out others from screwing with this table; is a read lock
522      * sufficient?
523      */
524     ObtainWriteLock(&afs_xdcache, 286);
525     dcListMax = 0;
526
527     for (index = afs_dvhashTbl[hash]; index != NULLIDX;) {
528         if (afs_indexUnique[index] == avc->f.fid.Fid.Unique) {
529             tdc = afs_GetValidDSlot(index);
530             if (!tdc) {
531                 goto error;
532             }
533             ReleaseReadLock(&tdc->tlock);
534             if (!FidCmp(&tdc->f.fid, &avc->f.fid))
535                 dcListMax++;
536             afs_PutDCache(tdc);
537         }
538         index = afs_dvnextTbl[index];
539     }
540
541     dcList = osi_Alloc(dcListMax * sizeof(struct dcache *));
542
543     for (index = afs_dvhashTbl[hash]; index != NULLIDX;) {
544         if (afs_indexUnique[index] == avc->f.fid.Fid.Unique) {
545             tdc = afs_GetValidDSlot(index);
546             if (!tdc) {
547                 goto error;
548             }
549             ReleaseReadLock(&tdc->tlock);
550             if (!FidCmp(&tdc->f.fid, &avc->f.fid)) {
551                 /* same file? we'll zap it */
552                 if (afs_indexFlags[index] & IFDataMod) {
553                     afs_stats_cmperf.cacheCurrDirtyChunks--;
554                     /* don't write it back */
555                     afs_indexFlags[index] &= ~IFDataMod;
556                 }
557                 afs_indexFlags[index] &= ~IFAnyPages;
558                 if (dcListCount < dcListMax)
559                     dcList[dcListCount++] = tdc;
560                 else
561                     afs_PutDCache(tdc);
562             } else {
563                 afs_PutDCache(tdc);
564             }
565         }
566         index = afs_dvnextTbl[index];
567     }
568     ReleaseWriteLock(&afs_xdcache);
569
570     for (i = 0; i < dcListCount; i++) {
571         tdc = dcList[i];
572
573         ObtainWriteLock(&tdc->lock, 679);
574         ZapDCE(tdc);
575         if (vType(avc) == VDIR)
576             DZap(tdc);
577         ReleaseWriteLock(&tdc->lock);
578         afs_PutDCache(tdc);
579     }
580
581     osi_Free(dcList, dcListMax * sizeof(struct dcache *));
582
583     return 0;
584
585  error:
586     ReleaseWriteLock(&afs_xdcache);
587
588     if (dcList) {
589         for (i = 0; i < dcListCount; i++) {
590             tdc = dcList[i];
591             if (tdc) {
592                 afs_PutDCache(tdc);
593             }
594         }
595         osi_Free(dcList, dcListMax * sizeof(struct dcache *));
596     }
597     return EIO;
598 }
599
600
601 /*
602  * afs_InvalidateAllSegments
603  *
604  * Description:
605  *      Invalidates all chunks for a given file
606  *
607  * Parameters:
608  *      avc      : Pointer to vcache entry.
609  *
610  * Environment:
611  *      For example, called after an error has been detected.  Called
612  *      with avc write-locked, and afs_xdcache unheld.
613  */
614
615 void
616 afs_InvalidateAllSegments(struct vcache *avc)
617 {
618     int code;
619     afs_uint32 last_warn;
620
621     code = afs_InvalidateAllSegments_once(avc);
622     if (code == 0) {
623         /* Success; nothing more to do. */
624         return;
625     }
626
627     /*
628      * If afs_InvalidateAllSegments_once failed, we cannot simply return an
629      * error to our caller. This function is called when we encounter a fatal
630      * error during stores, in which case we MUST invalidate all chunks for the
631      * given file. If we fail to invalidate some chunks, they will be left with
632      * the 'new' dirty/written data that was never successfully stored on the
633      * server, but the DV in the dcache is still the old DV. So, if its left
634      * alone, we may indefinitely serve data to applications that is not
635      * actually in the file on the fileserver.
636      *
637      * So to make sure we never serve userspace bad data after such a failure,
638      * we must keep trying to invalidate the dcaches for the given file. (Note
639      * that we cannot simply set a flag on the vcache to retry the invalidate
640      * later on, because the vcache may go away, but the 'bad' dcaches could
641      * remain.) We do this below, via background daemon requests because in
642      * some scenarios we can always get I/O errors on accessing the cache if we
643      * access via a user pid. (e.g. on LINUX, this can happen if the pid has a
644      * pending SIGKILL.) Doing this via background daemon ops should avoid
645      * that.
646      */
647
648     last_warn = osi_Time();
649     afs_warn("afs: Failed to invalidate cache chunks for fid %d.%d.%d.%d; our "
650              "local disk cache may be throwing errors. We must invalidate "
651              "these chunks to avoid possibly serving incorrect data, so we'll "
652              "retry until we succeed. If AFS access seems to hang, this may "
653              "be why.\n",
654              avc->f.fid.Cell, avc->f.fid.Fid.Volume, avc->f.fid.Fid.Vnode,
655              avc->f.fid.Fid.Unique);
656
657     do {
658         static const afs_uint32 warn_int = 60*60; /* warn once every hour */
659         afs_uint32 now = osi_Time();
660         struct brequest *bp;
661
662         if (now < last_warn || now - last_warn > warn_int) {
663             last_warn = now;
664             afs_warn("afs: Still trying to invalidate cache chunks for fid "
665                      "%d.%d.%d.%d. We will retry until we succeed; if AFS "
666                      "access seems to hang, this may be why.\n",
667                      avc->f.fid.Cell, avc->f.fid.Fid.Volume,
668                      avc->f.fid.Fid.Vnode, avc->f.fid.Fid.Unique);
669         }
670
671         /* Wait 10 seconds between attempts. */
672         afs_osi_Wait(1000 * 10, NULL, 0);
673
674         /*
675          * Ask a background daemon to do this request for us. Note that _we_ hold
676          * the write lock on 'avc', while the background daemon does the work. This
677          * is a little weird, but it helps avoid any issues with lock ordering
678          * or if our caller does not expect avc->lock to be dropped while
679          * running.
680          */
681         bp = afs_BQueue(BOP_INVALIDATE_SEGMENTS, avc, 0, 1, NULL, 0, 0, NULL,
682                         NULL, NULL);
683         while ((bp->flags & BUVALID) == 0) {
684             bp->flags |= BUWAIT;
685             afs_osi_Sleep(bp);
686         }
687         code = bp->code_raw;
688         afs_BRelease(bp);
689     } while (code);
690 }
691
692 /*!
693  *
694  * Extend a cache file
695  *
696  * \param avc pointer to vcache to extend data for
697  * \param alen Length to extend file to
698  * \param areq
699  *
700  * \note avc must be write locked. May release and reobtain avc and GLOCK
701  */
702 int
703 afs_ExtendSegments(struct vcache *avc, afs_size_t alen, struct vrequest *areq)
704 {
705     afs_size_t offset, toAdd;
706     struct osi_file *tfile;
707     afs_int32 code = 0;
708     struct dcache *tdc;
709     void *zeros;
710
711     zeros = afs_osi_Alloc(AFS_PAGESIZE);
712     if (zeros == NULL)
713         return ENOMEM;
714     memset(zeros, 0, AFS_PAGESIZE);
715
716     while (avc->f.m.Length < alen) {
717         tdc = afs_ObtainDCacheForWriting(avc, avc->f.m.Length, alen - avc->f.m.Length, areq, 0);
718         if (!tdc) {
719             code = EIO;
720             break;
721         }
722
723         toAdd = alen - avc->f.m.Length;
724
725         offset = avc->f.m.Length - AFS_CHUNKTOBASE(tdc->f.chunk);
726         if (offset + toAdd > AFS_CHUNKTOSIZE(tdc->f.chunk)) {
727             toAdd = AFS_CHUNKTOSIZE(tdc->f.chunk) - offset;
728         }
729         tfile = afs_CFileOpen(&tdc->f.inode);
730         osi_Assert(tfile);
731         while(tdc->validPos < avc->f.m.Length + toAdd) {
732              afs_size_t towrite;
733
734              towrite = (avc->f.m.Length + toAdd) - tdc->validPos;
735              if (towrite > AFS_PAGESIZE) towrite = AFS_PAGESIZE;
736
737              code = afs_CFileWrite(tfile,
738                                    tdc->validPos - AFS_CHUNKTOBASE(tdc->f.chunk),
739                                    zeros, towrite);
740              tdc->validPos += towrite;
741         }
742         afs_CFileClose(tfile);
743         afs_AdjustSize(tdc, offset + toAdd );
744         avc->f.m.Length += toAdd;
745         ReleaseWriteLock(&tdc->lock);
746         afs_PutDCache(tdc);
747     }
748
749     afs_osi_Free(zeros, AFS_PAGESIZE);
750     return code;
751 }
752
753 /*
754  * afs_TruncateAllSegments
755  *
756  * Description:
757  *      Truncate a cache file.
758  *
759  * Parameters:
760  *      avc  : Ptr to vcache entry to truncate.
761  *      alen : Number of bytes to make the file.
762  *      areq : Ptr to request structure.
763  *
764  * Environment:
765  *      Called with avc write-locked; in VFS40 systems, pvnLock is also
766  *      held.
767  */
768 int
769 afs_TruncateAllSegments(struct vcache *avc, afs_size_t alen,
770                         struct vrequest *areq, afs_ucred_t *acred)
771 {
772     struct dcache *tdc;
773     afs_int32 code;
774     afs_int32 index;
775     afs_size_t newSize;
776
777     int dcCount, dcPos;
778     struct dcache **tdcArray = NULL;
779
780     AFS_STATCNT(afs_TruncateAllSegments);
781     avc->f.m.Date = osi_Time();
782     afs_Trace3(afs_iclSetp, CM_TRACE_TRUNCALL, ICL_TYPE_POINTER, avc,
783                ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(avc->f.m.Length),
784                ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(alen));
785     if (alen >= avc->f.m.Length) {
786         /*
787          * Special speedup since Sun's vm extends the file this way;
788          * we've never written to the file thus we can just set the new
789          * length and avoid the needless calls below.
790          * Also used for ftruncate calls which can extend the file.
791          * To completely minimize the possible extra StoreMini RPC, we really
792          * should keep the ExtendedPos as well and clear this flag if we
793          * truncate below that value before we store the file back.
794          */
795         avc->f.states |= CExtendedFile;
796         avc->f.m.Length = alen;
797         return 0;
798     }
799 #if     (defined(AFS_SUN5_ENV))
800
801     /* Zero unused portion of last page */
802     osi_VM_PreTruncate(avc, alen, acred);
803
804 #endif
805
806 #if     (defined(AFS_SUN5_ENV))
807     ObtainWriteLock(&avc->vlock, 546);
808     avc->activeV++;             /* Block new getpages */
809     ReleaseWriteLock(&avc->vlock);
810 #endif
811
812     ReleaseWriteLock(&avc->lock);
813     AFS_GUNLOCK();
814
815     /* Flush pages beyond end-of-file. */
816     osi_VM_Truncate(avc, alen, acred);
817
818     AFS_GLOCK();
819     ObtainWriteLock(&avc->lock, 79);
820
821     avc->f.m.Length = alen;
822
823     if (alen < avc->f.truncPos)
824         avc->f.truncPos = alen;
825     code = DVHash(&avc->f.fid);
826
827     /* block out others from screwing with this table */
828     ObtainWriteLock(&afs_xdcache, 287);
829
830     dcCount = 0;
831     for (index = afs_dvhashTbl[code]; index != NULLIDX;) {
832         if (afs_indexUnique[index] == avc->f.fid.Fid.Unique) {
833             tdc = afs_GetValidDSlot(index);
834             if (!tdc) {
835                 ReleaseWriteLock(&afs_xdcache);
836                 code = EIO;
837                 goto done;
838             }
839             ReleaseReadLock(&tdc->tlock);
840             if (!FidCmp(&tdc->f.fid, &avc->f.fid))
841                 dcCount++;
842             afs_PutDCache(tdc);
843         }
844         index = afs_dvnextTbl[index];
845     }
846
847     /* Now allocate space where we can save those dcache entries, and
848      * do a second pass over them..  Since we're holding xdcache, it
849      * shouldn't be changing.
850      */
851     tdcArray = osi_Alloc(dcCount * sizeof(struct dcache *));
852     dcPos = 0;
853
854     for (index = afs_dvhashTbl[code]; index != NULLIDX; index = afs_dvnextTbl[index]) {
855         if (afs_indexUnique[index] == avc->f.fid.Fid.Unique) {
856             tdc = afs_GetValidDSlot(index);
857             if (!tdc) {
858                 /* make sure we put back all of the tdcArray members before
859                  * bailing out */
860                 /* remember, the last valid tdc is at dcPos-1, so start at
861                  * dcPos-1, not at dcPos itself. */
862                 for (dcPos = dcPos - 1; dcPos >= 0; dcPos--) {
863                     tdc = tdcArray[dcPos];
864                     afs_PutDCache(tdc);
865                 }
866                 code = EIO;
867                 goto done;
868             }
869             ReleaseReadLock(&tdc->tlock);
870             if (!FidCmp(&tdc->f.fid, &avc->f.fid)) {
871                 /* same file, and modified, we'll store it back */
872                 if (dcPos < dcCount) {
873                     tdcArray[dcPos++] = tdc;
874                 } else {
875                     afs_PutDCache(tdc);
876                 }
877             } else {
878                 afs_PutDCache(tdc);
879             }
880         }
881     }
882
883     ReleaseWriteLock(&afs_xdcache);
884
885     /* Now we loop over the array of dcache entries and truncate them */
886     for (index = 0; index < dcPos; index++) {
887         struct osi_file *tfile;
888
889         tdc = tdcArray[index];
890
891         newSize = alen - AFS_CHUNKTOBASE(tdc->f.chunk);
892         if (newSize < 0)
893             newSize = 0;
894         ObtainSharedLock(&tdc->lock, 672);
895         if (newSize < tdc->f.chunkBytes && newSize < MAX_AFS_UINT32) {
896             UpgradeSToWLock(&tdc->lock, 673);
897             tdc->f.states |= DWriting;
898             tfile = afs_CFileOpen(&tdc->f.inode);
899             osi_Assert(tfile);
900             afs_CFileTruncate(tfile, (afs_int32)newSize);
901             afs_CFileClose(tfile);
902             afs_AdjustSize(tdc, (afs_int32)newSize);
903             if (alen < tdc->validPos) {
904                 if (alen < AFS_CHUNKTOBASE(tdc->f.chunk))
905                     tdc->validPos = 0;
906                 else
907                     tdc->validPos = alen;
908             }
909             ConvertWToSLock(&tdc->lock);
910         }
911         ReleaseSharedLock(&tdc->lock);
912         afs_PutDCache(tdc);
913     }
914
915     code = 0;
916
917  done:
918     if (tdcArray) {
919         osi_Free(tdcArray, dcCount * sizeof(struct dcache *));
920     }
921 #if     (defined(AFS_SUN5_ENV))
922     ObtainWriteLock(&avc->vlock, 547);
923     if (--avc->activeV == 0 && (avc->vstates & VRevokeWait)) {
924         avc->vstates &= ~VRevokeWait;
925         afs_osi_Wakeup((char *)&avc->vstates);
926     }
927     ReleaseWriteLock(&avc->vlock);
928 #endif
929
930     return code;
931 }