patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / fs / xfs / xfs_buf_item.c
1 /*
2  * Copyright (c) 2000-2004 Silicon Graphics, Inc.  All Rights Reserved.
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of version 2 of the GNU General Public License as
6  * published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it would be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11  *
12  * Further, this software is distributed without any warranty that it is
13  * free of the rightful claim of any third person regarding infringement
14  * or the like.  Any license provided herein, whether implied or
15  * otherwise, applies only to this software file.  Patent licenses, if
16  * any, provided herein do not apply to combinations of this program with
17  * other software, or any other product whatsoever.
18  *
19  * You should have received a copy of the GNU General Public License along
20  * with this program; if not, write the Free Software Foundation, Inc., 59
21  * Temple Place - Suite 330, Boston MA 02111-1307, USA.
22  *
23  * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24  * Mountain View, CA  94043, or:
25  *
26  * http://www.sgi.com
27  *
28  * For further information regarding this notice, see:
29  *
30  * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
31  */
32
33 /*
34  * This file contains the implementation of the xfs_buf_log_item.
35  * It contains the item operations used to manipulate the buf log
36  * items as well as utility routines used by the buffer specific
37  * transaction routines.
38  */
39
40 #include "xfs.h"
41
42 #include "xfs_macros.h"
43 #include "xfs_types.h"
44 #include "xfs_inum.h"
45 #include "xfs_log.h"
46 #include "xfs_trans.h"
47 #include "xfs_buf_item.h"
48 #include "xfs_sb.h"
49 #include "xfs_dir.h"
50 #include "xfs_dmapi.h"
51 #include "xfs_mount.h"
52 #include "xfs_trans_priv.h"
53 #include "xfs_rw.h"
54 #include "xfs_bit.h"
55 #include "xfs_error.h"
56
57
58 #define ROUNDUPNBWORD(x)        (((x) + (NBWORD - 1)) & ~(NBWORD - 1))
59
60 kmem_zone_t     *xfs_buf_item_zone;
61
62 #ifdef XFS_TRANS_DEBUG
63 /*
64  * This function uses an alternate strategy for tracking the bytes
65  * that the user requests to be logged.  This can then be used
66  * in conjunction with the bli_orig array in the buf log item to
67  * catch bugs in our callers' code.
68  *
69  * We also double check the bits set in xfs_buf_item_log using a
70  * simple algorithm to check that every byte is accounted for.
71  */
72 STATIC void
73 xfs_buf_item_log_debug(
74         xfs_buf_log_item_t      *bip,
75         uint                    first,
76         uint                    last)
77 {
78         uint    x;
79         uint    byte;
80         uint    nbytes;
81         uint    chunk_num;
82         uint    word_num;
83         uint    bit_num;
84         uint    bit_set;
85         uint    *wordp;
86
87         ASSERT(bip->bli_logged != NULL);
88         byte = first;
89         nbytes = last - first + 1;
90         bfset(bip->bli_logged, first, nbytes);
91         for (x = 0; x < nbytes; x++) {
92                 chunk_num = byte >> XFS_BLI_SHIFT;
93                 word_num = chunk_num >> BIT_TO_WORD_SHIFT;
94                 bit_num = chunk_num & (NBWORD - 1);
95                 wordp = &(bip->bli_format.blf_data_map[word_num]);
96                 bit_set = *wordp & (1 << bit_num);
97                 ASSERT(bit_set);
98                 byte++;
99         }
100 }
101
102 /*
103  * This function is called when we flush something into a buffer without
104  * logging it.  This happens for things like inodes which are logged
105  * separately from the buffer.
106  */
107 void
108 xfs_buf_item_flush_log_debug(
109         xfs_buf_t       *bp,
110         uint            first,
111         uint            last)
112 {
113         xfs_buf_log_item_t      *bip;
114         uint                    nbytes;
115
116         bip = XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t*);
117         if ((bip == NULL) || (bip->bli_item.li_type != XFS_LI_BUF)) {
118                 return;
119         }
120
121         ASSERT(bip->bli_logged != NULL);
122         nbytes = last - first + 1;
123         bfset(bip->bli_logged, first, nbytes);
124 }
125
126 /*
127  * This function is called to verify that our caller's have logged
128  * all the bytes that they changed.
129  *
130  * It does this by comparing the original copy of the buffer stored in
131  * the buf log item's bli_orig array to the current copy of the buffer
132  * and ensuring that all bytes which miscompare are set in the bli_logged
133  * array of the buf log item.
134  */
135 STATIC void
136 xfs_buf_item_log_check(
137         xfs_buf_log_item_t      *bip)
138 {
139         char            *orig;
140         char            *buffer;
141         int             x;
142         xfs_buf_t       *bp;
143
144         ASSERT(bip->bli_orig != NULL);
145         ASSERT(bip->bli_logged != NULL);
146
147         bp = bip->bli_buf;
148         ASSERT(XFS_BUF_COUNT(bp) > 0);
149         ASSERT(XFS_BUF_PTR(bp) != NULL);
150         orig = bip->bli_orig;
151         buffer = XFS_BUF_PTR(bp);
152         for (x = 0; x < XFS_BUF_COUNT(bp); x++) {
153                 if (orig[x] != buffer[x] && !btst(bip->bli_logged, x))
154                         cmn_err(CE_PANIC,
155         "xfs_buf_item_log_check bip %x buffer %x orig %x index %d",
156                                 bip, bp, orig, x);
157         }
158 }
159 #else
160 #define         xfs_buf_item_log_debug(x,y,z)
161 #define         xfs_buf_item_log_check(x)
162 #endif
163
164 STATIC void     xfs_buf_error_relse(xfs_buf_t *bp);
165 STATIC void     xfs_buf_do_callbacks(xfs_buf_t *bp, xfs_log_item_t *lip);
166
167 /*
168  * This returns the number of log iovecs needed to log the
169  * given buf log item.
170  *
171  * It calculates this as 1 iovec for the buf log format structure
172  * and 1 for each stretch of non-contiguous chunks to be logged.
173  * Contiguous chunks are logged in a single iovec.
174  *
175  * If the XFS_BLI_STALE flag has been set, then log nothing.
176  */
177 uint
178 xfs_buf_item_size(
179         xfs_buf_log_item_t      *bip)
180 {
181         uint            nvecs;
182         int             next_bit;
183         int             last_bit;
184         xfs_buf_t       *bp;
185
186         ASSERT(atomic_read(&bip->bli_refcount) > 0);
187         if (bip->bli_flags & XFS_BLI_STALE) {
188                 /*
189                  * The buffer is stale, so all we need to log
190                  * is the buf log format structure with the
191                  * cancel flag in it.
192                  */
193                 xfs_buf_item_trace("SIZE STALE", bip);
194                 ASSERT(bip->bli_format.blf_flags & XFS_BLI_CANCEL);
195                 return 1;
196         }
197
198         bp = bip->bli_buf;
199         ASSERT(bip->bli_flags & XFS_BLI_LOGGED);
200         nvecs = 1;
201         last_bit = xfs_next_bit(bip->bli_format.blf_data_map,
202                                          bip->bli_format.blf_map_size, 0);
203         ASSERT(last_bit != -1);
204         nvecs++;
205         while (last_bit != -1) {
206                 /*
207                  * This takes the bit number to start looking from and
208                  * returns the next set bit from there.  It returns -1
209                  * if there are no more bits set or the start bit is
210                  * beyond the end of the bitmap.
211                  */
212                 next_bit = xfs_next_bit(bip->bli_format.blf_data_map,
213                                                  bip->bli_format.blf_map_size,
214                                                  last_bit + 1);
215                 /*
216                  * If we run out of bits, leave the loop,
217                  * else if we find a new set of bits bump the number of vecs,
218                  * else keep scanning the current set of bits.
219                  */
220                 if (next_bit == -1) {
221                         last_bit = -1;
222                 } else if (next_bit != last_bit + 1) {
223                         last_bit = next_bit;
224                         nvecs++;
225                 } else if (xfs_buf_offset(bp, next_bit * XFS_BLI_CHUNK) !=
226                            (xfs_buf_offset(bp, last_bit * XFS_BLI_CHUNK) +
227                             XFS_BLI_CHUNK)) {
228                         last_bit = next_bit;
229                         nvecs++;
230                 } else {
231                         last_bit++;
232                 }
233         }
234
235         xfs_buf_item_trace("SIZE NORM", bip);
236         return nvecs;
237 }
238
239 /*
240  * This is called to fill in the vector of log iovecs for the
241  * given log buf item.  It fills the first entry with a buf log
242  * format structure, and the rest point to contiguous chunks
243  * within the buffer.
244  */
245 void
246 xfs_buf_item_format(
247         xfs_buf_log_item_t      *bip,
248         xfs_log_iovec_t         *log_vector)
249 {
250         uint            base_size;
251         uint            nvecs;
252         xfs_log_iovec_t *vecp;
253         xfs_buf_t       *bp;
254         int             first_bit;
255         int             last_bit;
256         int             next_bit;
257         uint            nbits;
258         uint            buffer_offset;
259
260         ASSERT(atomic_read(&bip->bli_refcount) > 0);
261         ASSERT((bip->bli_flags & XFS_BLI_LOGGED) ||
262                (bip->bli_flags & XFS_BLI_STALE));
263         bp = bip->bli_buf;
264         ASSERT(XFS_BUF_BP_ISMAPPED(bp));
265         vecp = log_vector;
266
267         /*
268          * The size of the base structure is the size of the
269          * declared structure plus the space for the extra words
270          * of the bitmap.  We subtract one from the map size, because
271          * the first element of the bitmap is accounted for in the
272          * size of the base structure.
273          */
274         base_size =
275                 (uint)(sizeof(xfs_buf_log_format_t) +
276                        ((bip->bli_format.blf_map_size - 1) * sizeof(uint)));
277         vecp->i_addr = (xfs_caddr_t)&bip->bli_format;
278         vecp->i_len = base_size;
279         vecp++;
280         nvecs = 1;
281
282         if (bip->bli_flags & XFS_BLI_STALE) {
283                 /*
284                  * The buffer is stale, so all we need to log
285                  * is the buf log format structure with the
286                  * cancel flag in it.
287                  */
288                 xfs_buf_item_trace("FORMAT STALE", bip);
289                 ASSERT(bip->bli_format.blf_flags & XFS_BLI_CANCEL);
290                 bip->bli_format.blf_size = nvecs;
291                 return;
292         }
293
294         /*
295          * Fill in an iovec for each set of contiguous chunks.
296          */
297         first_bit = xfs_next_bit(bip->bli_format.blf_data_map,
298                                          bip->bli_format.blf_map_size, 0);
299         ASSERT(first_bit != -1);
300         last_bit = first_bit;
301         nbits = 1;
302         for (;;) {
303                 /*
304                  * This takes the bit number to start looking from and
305                  * returns the next set bit from there.  It returns -1
306                  * if there are no more bits set or the start bit is
307                  * beyond the end of the bitmap.
308                  */
309                 next_bit = xfs_next_bit(bip->bli_format.blf_data_map,
310                                                  bip->bli_format.blf_map_size,
311                                                  (uint)last_bit + 1);
312                 /*
313                  * If we run out of bits fill in the last iovec and get
314                  * out of the loop.
315                  * Else if we start a new set of bits then fill in the
316                  * iovec for the series we were looking at and start
317                  * counting the bits in the new one.
318                  * Else we're still in the same set of bits so just
319                  * keep counting and scanning.
320                  */
321                 if (next_bit == -1) {
322                         buffer_offset = first_bit * XFS_BLI_CHUNK;
323                         vecp->i_addr = xfs_buf_offset(bp, buffer_offset);
324                         vecp->i_len = nbits * XFS_BLI_CHUNK;
325                         nvecs++;
326                         break;
327                 } else if (next_bit != last_bit + 1) {
328                         buffer_offset = first_bit * XFS_BLI_CHUNK;
329                         vecp->i_addr = xfs_buf_offset(bp, buffer_offset);
330                         vecp->i_len = nbits * XFS_BLI_CHUNK;
331                         nvecs++;
332                         vecp++;
333                         first_bit = next_bit;
334                         last_bit = next_bit;
335                         nbits = 1;
336                 } else if (xfs_buf_offset(bp, next_bit << XFS_BLI_SHIFT) !=
337                            (xfs_buf_offset(bp, last_bit << XFS_BLI_SHIFT) +
338                             XFS_BLI_CHUNK)) {
339                         buffer_offset = first_bit * XFS_BLI_CHUNK;
340                         vecp->i_addr = xfs_buf_offset(bp, buffer_offset);
341                         vecp->i_len = nbits * XFS_BLI_CHUNK;
342 /* You would think we need to bump the nvecs here too, but we do not
343  * this number is used by recovery, and it gets confused by the boundary
344  * split here
345  *                      nvecs++;
346  */
347                         vecp++;
348                         first_bit = next_bit;
349                         last_bit = next_bit;
350                         nbits = 1;
351                 } else {
352                         last_bit++;
353                         nbits++;
354                 }
355         }
356         bip->bli_format.blf_size = nvecs;
357
358         /*
359          * Check to make sure everything is consistent.
360          */
361         xfs_buf_item_trace("FORMAT NORM", bip);
362         xfs_buf_item_log_check(bip);
363 }
364
365 /*
366  * This is called to pin the buffer associated with the buf log
367  * item in memory so it cannot be written out.  Simply call bpin()
368  * on the buffer to do this.
369  */
370 void
371 xfs_buf_item_pin(
372         xfs_buf_log_item_t      *bip)
373 {
374         xfs_buf_t       *bp;
375
376         bp = bip->bli_buf;
377         ASSERT(XFS_BUF_ISBUSY(bp));
378         ASSERT(atomic_read(&bip->bli_refcount) > 0);
379         ASSERT((bip->bli_flags & XFS_BLI_LOGGED) ||
380                (bip->bli_flags & XFS_BLI_STALE));
381         xfs_buf_item_trace("PIN", bip);
382         xfs_buftrace("XFS_PIN", bp);
383         xfs_bpin(bp);
384 }
385
386
387 /*
388  * This is called to unpin the buffer associated with the buf log
389  * item which was previously pinned with a call to xfs_buf_item_pin().
390  * Just call bunpin() on the buffer to do this.
391  *
392  * Also drop the reference to the buf item for the current transaction.
393  * If the XFS_BLI_STALE flag is set and we are the last reference,
394  * then free up the buf log item and unlock the buffer.
395  */
396 void
397 xfs_buf_item_unpin(
398         xfs_buf_log_item_t      *bip,
399         int                     stale)
400 {
401         xfs_mount_t     *mp;
402         xfs_buf_t       *bp;
403         int             freed;
404         SPLDECL(s);
405
406         bp = bip->bli_buf;
407         ASSERT(bp != NULL);
408         ASSERT(XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t *) == bip);
409         ASSERT(atomic_read(&bip->bli_refcount) > 0);
410         xfs_buf_item_trace("UNPIN", bip);
411         xfs_buftrace("XFS_UNPIN", bp);
412
413         freed = atomic_dec_and_test(&bip->bli_refcount);
414         mp = bip->bli_item.li_mountp;
415         xfs_bunpin(bp);
416         if (freed && stale) {
417                 ASSERT(bip->bli_flags & XFS_BLI_STALE);
418                 ASSERT(XFS_BUF_VALUSEMA(bp) <= 0);
419                 ASSERT(!(XFS_BUF_ISDELAYWRITE(bp)));
420                 ASSERT(XFS_BUF_ISSTALE(bp));
421                 ASSERT(bip->bli_format.blf_flags & XFS_BLI_CANCEL);
422                 xfs_buf_item_trace("UNPIN STALE", bip);
423                 xfs_buftrace("XFS_UNPIN STALE", bp);
424                 /*
425                  * If we get called here because of an IO error, we may
426                  * or may not have the item on the AIL. xfs_trans_delete_ail()
427                  * will take care of that situation.
428                  * xfs_trans_delete_ail() drops the AIL lock.
429                  */
430                 if (bip->bli_flags & XFS_BLI_STALE_INODE) {
431                         xfs_buf_do_callbacks(bp, (xfs_log_item_t *)bip);
432                         XFS_BUF_SET_FSPRIVATE(bp, NULL);
433                         XFS_BUF_CLR_IODONE_FUNC(bp);
434                 } else {
435                         AIL_LOCK(mp,s);
436                         xfs_trans_delete_ail(mp, (xfs_log_item_t *)bip, s);
437                         xfs_buf_item_relse(bp);
438                         ASSERT(XFS_BUF_FSPRIVATE(bp, void *) == NULL);
439                 }
440                 xfs_buf_relse(bp);
441         }
442 }
443
444 /*
445  * this is called from uncommit in the forced-shutdown path.
446  * we need to check to see if the reference count on the log item
447  * is going to drop to zero.  If so, unpin will free the log item
448  * so we need to free the item's descriptor (that points to the item)
449  * in the transaction.
450  */
451 void
452 xfs_buf_item_unpin_remove(
453         xfs_buf_log_item_t      *bip,
454         xfs_trans_t             *tp)
455 {
456         xfs_buf_t               *bp;
457         xfs_log_item_desc_t     *lidp;
458         int                     stale = 0;
459
460         bp = bip->bli_buf;
461         /*
462          * will xfs_buf_item_unpin() call xfs_buf_item_relse()?
463          */
464         if ((atomic_read(&bip->bli_refcount) == 1) &&
465             (bip->bli_flags & XFS_BLI_STALE)) {
466                 ASSERT(XFS_BUF_VALUSEMA(bip->bli_buf) <= 0);
467                 xfs_buf_item_trace("UNPIN REMOVE", bip);
468                 xfs_buftrace("XFS_UNPIN_REMOVE", bp);
469                 /*
470                  * yes -- clear the xaction descriptor in-use flag
471                  * and free the chunk if required.  We can safely
472                  * do some work here and then call buf_item_unpin
473                  * to do the rest because if the if is true, then
474                  * we are holding the buffer locked so no one else
475                  * will be able to bump up the refcount.
476                  */
477                 lidp = xfs_trans_find_item(tp, (xfs_log_item_t *) bip);
478                 stale = lidp->lid_flags & XFS_LID_BUF_STALE;
479                 xfs_trans_free_item(tp, lidp);
480                 /*
481                  * Since the transaction no longer refers to the buffer,
482                  * the buffer should no longer refer to the transaction.
483                  */
484                 XFS_BUF_SET_FSPRIVATE2(bp, NULL);
485         }
486
487         xfs_buf_item_unpin(bip, stale);
488
489         return;
490 }
491
492 /*
493  * This is called to attempt to lock the buffer associated with this
494  * buf log item.  Don't sleep on the buffer lock.  If we can't get
495  * the lock right away, return 0.  If we can get the lock, pull the
496  * buffer from the free list, mark it busy, and return 1.
497  */
498 uint
499 xfs_buf_item_trylock(
500         xfs_buf_log_item_t      *bip)
501 {
502         xfs_buf_t       *bp;
503
504         bp = bip->bli_buf;
505
506         if (XFS_BUF_ISPINNED(bp)) {
507                 return XFS_ITEM_PINNED;
508         }
509
510         if (!XFS_BUF_CPSEMA(bp)) {
511                 return XFS_ITEM_LOCKED;
512         }
513
514         /*
515          * Remove the buffer from the free list.  Only do this
516          * if it's on the free list.  Private buffers like the
517          * superblock buffer are not.
518          */
519         XFS_BUF_HOLD(bp);
520
521         ASSERT(!(bip->bli_flags & XFS_BLI_STALE));
522         xfs_buf_item_trace("TRYLOCK SUCCESS", bip);
523         return XFS_ITEM_SUCCESS;
524 }
525
526 /*
527  * Release the buffer associated with the buf log item.
528  * If there is no dirty logged data associated with the
529  * buffer recorded in the buf log item, then free the
530  * buf log item and remove the reference to it in the
531  * buffer.
532  *
533  * This call ignores the recursion count.  It is only called
534  * when the buffer should REALLY be unlocked, regardless
535  * of the recursion count.
536  *
537  * If the XFS_BLI_HOLD flag is set in the buf log item, then
538  * free the log item if necessary but do not unlock the buffer.
539  * This is for support of xfs_trans_bhold(). Make sure the
540  * XFS_BLI_HOLD field is cleared if we don't free the item.
541  */
542 void
543 xfs_buf_item_unlock(
544         xfs_buf_log_item_t      *bip)
545 {
546         int             aborted;
547         xfs_buf_t       *bp;
548         uint            hold;
549
550         bp = bip->bli_buf;
551         xfs_buftrace("XFS_UNLOCK", bp);
552
553         /*
554          * Clear the buffer's association with this transaction.
555          */
556         XFS_BUF_SET_FSPRIVATE2(bp, NULL);
557
558         /*
559          * If this is a transaction abort, don't return early.
560          * Instead, allow the brelse to happen.
561          * Normally it would be done for stale (cancelled) buffers
562          * at unpin time, but we'll never go through the pin/unpin
563          * cycle if we abort inside commit.
564          */
565         aborted = (bip->bli_item.li_flags & XFS_LI_ABORTED) != 0;
566
567         /*
568          * If the buf item is marked stale, then don't do anything.
569          * We'll unlock the buffer and free the buf item when the
570          * buffer is unpinned for the last time.
571          */
572         if (bip->bli_flags & XFS_BLI_STALE) {
573                 bip->bli_flags &= ~XFS_BLI_LOGGED;
574                 xfs_buf_item_trace("UNLOCK STALE", bip);
575                 ASSERT(bip->bli_format.blf_flags & XFS_BLI_CANCEL);
576                 if (!aborted)
577                         return;
578         }
579
580         /*
581          * Drop the transaction's reference to the log item if
582          * it was not logged as part of the transaction.  Otherwise
583          * we'll drop the reference in xfs_buf_item_unpin() when
584          * the transaction is really through with the buffer.
585          */
586         if (!(bip->bli_flags & XFS_BLI_LOGGED)) {
587                 atomic_dec(&bip->bli_refcount);
588         } else {
589                 /*
590                  * Clear the logged flag since this is per
591                  * transaction state.
592                  */
593                 bip->bli_flags &= ~XFS_BLI_LOGGED;
594         }
595
596         /*
597          * Before possibly freeing the buf item, determine if we should
598          * release the buffer at the end of this routine.
599          */
600         hold = bip->bli_flags & XFS_BLI_HOLD;
601         xfs_buf_item_trace("UNLOCK", bip);
602
603         /*
604          * If the buf item isn't tracking any data, free it.
605          * Otherwise, if XFS_BLI_HOLD is set clear it.
606          */
607         if (xfs_count_bits(bip->bli_format.blf_data_map,
608                               bip->bli_format.blf_map_size, 0) == 0) {
609                 xfs_buf_item_relse(bp);
610         } else if (hold) {
611                 bip->bli_flags &= ~XFS_BLI_HOLD;
612         }
613
614         /*
615          * Release the buffer if XFS_BLI_HOLD was not set.
616          */
617         if (!hold) {
618                 xfs_buf_relse(bp);
619         }
620 }
621
622 /*
623  * This is called to find out where the oldest active copy of the
624  * buf log item in the on disk log resides now that the last log
625  * write of it completed at the given lsn.
626  * We always re-log all the dirty data in a buffer, so usually the
627  * latest copy in the on disk log is the only one that matters.  For
628  * those cases we simply return the given lsn.
629  *
630  * The one exception to this is for buffers full of newly allocated
631  * inodes.  These buffers are only relogged with the XFS_BLI_INODE_BUF
632  * flag set, indicating that only the di_next_unlinked fields from the
633  * inodes in the buffers will be replayed during recovery.  If the
634  * original newly allocated inode images have not yet been flushed
635  * when the buffer is so relogged, then we need to make sure that we
636  * keep the old images in the 'active' portion of the log.  We do this
637  * by returning the original lsn of that transaction here rather than
638  * the current one.
639  */
640 xfs_lsn_t
641 xfs_buf_item_committed(
642         xfs_buf_log_item_t      *bip,
643         xfs_lsn_t               lsn)
644 {
645         xfs_buf_item_trace("COMMITTED", bip);
646         if ((bip->bli_flags & XFS_BLI_INODE_ALLOC_BUF) &&
647             (bip->bli_item.li_lsn != 0)) {
648                 return bip->bli_item.li_lsn;
649         }
650         return (lsn);
651 }
652
653 /*
654  * This is called when the transaction holding the buffer is aborted.
655  * Just behave as if the transaction had been cancelled. If we're shutting down
656  * and have aborted this transaction, we'll trap this buffer when it tries to
657  * get written out.
658  */
659 void
660 xfs_buf_item_abort(
661         xfs_buf_log_item_t      *bip)
662 {
663         xfs_buf_t       *bp;
664
665         bp = bip->bli_buf;
666         xfs_buftrace("XFS_ABORT", bp);
667         XFS_BUF_SUPER_STALE(bp);
668         xfs_buf_item_unlock(bip);
669         return;
670 }
671
672 /*
673  * This is called to asynchronously write the buffer associated with this
674  * buf log item out to disk. The buffer will already have been locked by
675  * a successful call to xfs_buf_item_trylock().  If the buffer still has
676  * B_DELWRI set, then get it going out to disk with a call to bawrite().
677  * If not, then just release the buffer.
678  */
679 void
680 xfs_buf_item_push(
681         xfs_buf_log_item_t      *bip)
682 {
683         xfs_buf_t       *bp;
684
685         ASSERT(!(bip->bli_flags & XFS_BLI_STALE));
686         xfs_buf_item_trace("PUSH", bip);
687
688         bp = bip->bli_buf;
689
690         if (XFS_BUF_ISDELAYWRITE(bp)) {
691                 xfs_bawrite(bip->bli_item.li_mountp, bp);
692         } else {
693                 xfs_buf_relse(bp);
694         }
695 }
696
697 /* ARGSUSED */
698 void
699 xfs_buf_item_committing(xfs_buf_log_item_t *bip, xfs_lsn_t commit_lsn)
700 {
701 }
702
703 /*
704  * This is the ops vector shared by all buf log items.
705  */
706 struct xfs_item_ops xfs_buf_item_ops = {
707         .iop_size       = (uint(*)(xfs_log_item_t*))xfs_buf_item_size,
708         .iop_format     = (void(*)(xfs_log_item_t*, xfs_log_iovec_t*))
709                                         xfs_buf_item_format,
710         .iop_pin        = (void(*)(xfs_log_item_t*))xfs_buf_item_pin,
711         .iop_unpin      = (void(*)(xfs_log_item_t*, int))xfs_buf_item_unpin,
712         .iop_unpin_remove = (void(*)(xfs_log_item_t*, xfs_trans_t *))
713                                         xfs_buf_item_unpin_remove,
714         .iop_trylock    = (uint(*)(xfs_log_item_t*))xfs_buf_item_trylock,
715         .iop_unlock     = (void(*)(xfs_log_item_t*))xfs_buf_item_unlock,
716         .iop_committed  = (xfs_lsn_t(*)(xfs_log_item_t*, xfs_lsn_t))
717                                         xfs_buf_item_committed,
718         .iop_push       = (void(*)(xfs_log_item_t*))xfs_buf_item_push,
719         .iop_abort      = (void(*)(xfs_log_item_t*))xfs_buf_item_abort,
720         .iop_pushbuf    = NULL,
721         .iop_committing = (void(*)(xfs_log_item_t*, xfs_lsn_t))
722                                         xfs_buf_item_committing
723 };
724
725
726 /*
727  * Allocate a new buf log item to go with the given buffer.
728  * Set the buffer's b_fsprivate field to point to the new
729  * buf log item.  If there are other item's attached to the
730  * buffer (see xfs_buf_attach_iodone() below), then put the
731  * buf log item at the front.
732  */
733 void
734 xfs_buf_item_init(
735         xfs_buf_t       *bp,
736         xfs_mount_t     *mp)
737 {
738         xfs_log_item_t          *lip;
739         xfs_buf_log_item_t      *bip;
740         int                     chunks;
741         int                     map_size;
742
743         /*
744          * Check to see if there is already a buf log item for
745          * this buffer.  If there is, it is guaranteed to be
746          * the first.  If we do already have one, there is
747          * nothing to do here so return.
748          */
749         if (XFS_BUF_FSPRIVATE3(bp, xfs_mount_t *) != mp)
750                 XFS_BUF_SET_FSPRIVATE3(bp, mp);
751         XFS_BUF_SET_BDSTRAT_FUNC(bp, xfs_bdstrat_cb);
752         if (XFS_BUF_FSPRIVATE(bp, void *) != NULL) {
753                 lip = XFS_BUF_FSPRIVATE(bp, xfs_log_item_t *);
754                 if (lip->li_type == XFS_LI_BUF) {
755                         return;
756                 }
757         }
758
759         /*
760          * chunks is the number of XFS_BLI_CHUNK size pieces
761          * the buffer can be divided into. Make sure not to
762          * truncate any pieces.  map_size is the size of the
763          * bitmap needed to describe the chunks of the buffer.
764          */
765         chunks = (int)((XFS_BUF_COUNT(bp) + (XFS_BLI_CHUNK - 1)) >> XFS_BLI_SHIFT);
766         map_size = (int)((chunks + NBWORD) >> BIT_TO_WORD_SHIFT);
767
768         bip = (xfs_buf_log_item_t*)kmem_zone_zalloc(xfs_buf_item_zone,
769                                                     KM_SLEEP);
770         bip->bli_item.li_type = XFS_LI_BUF;
771         bip->bli_item.li_ops = &xfs_buf_item_ops;
772         bip->bli_item.li_mountp = mp;
773         bip->bli_buf = bp;
774         bip->bli_format.blf_type = XFS_LI_BUF;
775         bip->bli_format.blf_blkno = (__int64_t)XFS_BUF_ADDR(bp);
776         bip->bli_format.blf_len = (ushort)BTOBB(XFS_BUF_COUNT(bp));
777         bip->bli_format.blf_map_size = map_size;
778 #ifdef XFS_BLI_TRACE
779         bip->bli_trace = ktrace_alloc(XFS_BLI_TRACE_SIZE, KM_SLEEP);
780 #endif
781
782 #ifdef XFS_TRANS_DEBUG
783         /*
784          * Allocate the arrays for tracking what needs to be logged
785          * and what our callers request to be logged.  bli_orig
786          * holds a copy of the original, clean buffer for comparison
787          * against, and bli_logged keeps a 1 bit flag per byte in
788          * the buffer to indicate which bytes the callers have asked
789          * to have logged.
790          */
791         bip->bli_orig = (char *)kmem_alloc(XFS_BUF_COUNT(bp), KM_SLEEP);
792         memcpy(bip->bli_orig, XFS_BUF_PTR(bp), XFS_BUF_COUNT(bp));
793         bip->bli_logged = (char *)kmem_zalloc(XFS_BUF_COUNT(bp) / NBBY, KM_SLEEP);
794 #endif
795
796         /*
797          * Put the buf item into the list of items attached to the
798          * buffer at the front.
799          */
800         if (XFS_BUF_FSPRIVATE(bp, void *) != NULL) {
801                 bip->bli_item.li_bio_list =
802                                 XFS_BUF_FSPRIVATE(bp, xfs_log_item_t *);
803         }
804         XFS_BUF_SET_FSPRIVATE(bp, bip);
805 }
806
807
808 /*
809  * Mark bytes first through last inclusive as dirty in the buf
810  * item's bitmap.
811  */
812 void
813 xfs_buf_item_log(
814         xfs_buf_log_item_t      *bip,
815         uint                    first,
816         uint                    last)
817 {
818         uint            first_bit;
819         uint            last_bit;
820         uint            bits_to_set;
821         uint            bits_set;
822         uint            word_num;
823         uint            *wordp;
824         uint            bit;
825         uint            end_bit;
826         uint            mask;
827
828         /*
829          * Mark the item as having some dirty data for
830          * quick reference in xfs_buf_item_dirty.
831          */
832         bip->bli_flags |= XFS_BLI_DIRTY;
833
834         /*
835          * Convert byte offsets to bit numbers.
836          */
837         first_bit = first >> XFS_BLI_SHIFT;
838         last_bit = last >> XFS_BLI_SHIFT;
839
840         /*
841          * Calculate the total number of bits to be set.
842          */
843         bits_to_set = last_bit - first_bit + 1;
844
845         /*
846          * Get a pointer to the first word in the bitmap
847          * to set a bit in.
848          */
849         word_num = first_bit >> BIT_TO_WORD_SHIFT;
850         wordp = &(bip->bli_format.blf_data_map[word_num]);
851
852         /*
853          * Calculate the starting bit in the first word.
854          */
855         bit = first_bit & (uint)(NBWORD - 1);
856
857         /*
858          * First set any bits in the first word of our range.
859          * If it starts at bit 0 of the word, it will be
860          * set below rather than here.  That is what the variable
861          * bit tells us. The variable bits_set tracks the number
862          * of bits that have been set so far.  End_bit is the number
863          * of the last bit to be set in this word plus one.
864          */
865         if (bit) {
866                 end_bit = MIN(bit + bits_to_set, (uint)NBWORD);
867                 mask = ((1 << (end_bit - bit)) - 1) << bit;
868                 *wordp |= mask;
869                 wordp++;
870                 bits_set = end_bit - bit;
871         } else {
872                 bits_set = 0;
873         }
874
875         /*
876          * Now set bits a whole word at a time that are between
877          * first_bit and last_bit.
878          */
879         while ((bits_to_set - bits_set) >= NBWORD) {
880                 *wordp |= 0xffffffff;
881                 bits_set += NBWORD;
882                 wordp++;
883         }
884
885         /*
886          * Finally, set any bits left to be set in one last partial word.
887          */
888         end_bit = bits_to_set - bits_set;
889         if (end_bit) {
890                 mask = (1 << end_bit) - 1;
891                 *wordp |= mask;
892         }
893
894         xfs_buf_item_log_debug(bip, first, last);
895 }
896
897
898 /*
899  * Return 1 if the buffer has some data that has been logged (at any
900  * point, not just the current transaction) and 0 if not.
901  */
902 uint
903 xfs_buf_item_dirty(
904         xfs_buf_log_item_t      *bip)
905 {
906         return (bip->bli_flags & XFS_BLI_DIRTY);
907 }
908
909 /*
910  * This is called when the buf log item is no longer needed.  It should
911  * free the buf log item associated with the given buffer and clear
912  * the buffer's pointer to the buf log item.  If there are no more
913  * items in the list, clear the b_iodone field of the buffer (see
914  * xfs_buf_attach_iodone() below).
915  */
916 void
917 xfs_buf_item_relse(
918         xfs_buf_t       *bp)
919 {
920         xfs_buf_log_item_t      *bip;
921
922         xfs_buftrace("XFS_RELSE", bp);
923         bip = XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t*);
924         XFS_BUF_SET_FSPRIVATE(bp, bip->bli_item.li_bio_list);
925         if ((XFS_BUF_FSPRIVATE(bp, void *) == NULL) &&
926             (XFS_BUF_IODONE_FUNC(bp) != NULL)) {
927                 ASSERT((XFS_BUF_ISUNINITIAL(bp)) == 0);
928                 XFS_BUF_CLR_IODONE_FUNC(bp);
929         }
930
931 #ifdef XFS_TRANS_DEBUG
932         kmem_free(bip->bli_orig, XFS_BUF_COUNT(bp));
933         bip->bli_orig = NULL;
934         kmem_free(bip->bli_logged, XFS_BUF_COUNT(bp) / NBBY);
935         bip->bli_logged = NULL;
936 #endif /* XFS_TRANS_DEBUG */
937
938 #ifdef XFS_BLI_TRACE
939         ktrace_free(bip->bli_trace);
940 #endif
941         kmem_zone_free(xfs_buf_item_zone, bip);
942 }
943
944
945 /*
946  * Add the given log item with its callback to the list of callbacks
947  * to be called when the buffer's I/O completes.  If it is not set
948  * already, set the buffer's b_iodone() routine to be
949  * xfs_buf_iodone_callbacks() and link the log item into the list of
950  * items rooted at b_fsprivate.  Items are always added as the second
951  * entry in the list if there is a first, because the buf item code
952  * assumes that the buf log item is first.
953  */
954 void
955 xfs_buf_attach_iodone(
956         xfs_buf_t       *bp,
957         void            (*cb)(xfs_buf_t *, xfs_log_item_t *),
958         xfs_log_item_t  *lip)
959 {
960         xfs_log_item_t  *head_lip;
961
962         ASSERT(XFS_BUF_ISBUSY(bp));
963         ASSERT(XFS_BUF_VALUSEMA(bp) <= 0);
964
965         lip->li_cb = cb;
966         if (XFS_BUF_FSPRIVATE(bp, void *) != NULL) {
967                 head_lip = XFS_BUF_FSPRIVATE(bp, xfs_log_item_t *);
968                 lip->li_bio_list = head_lip->li_bio_list;
969                 head_lip->li_bio_list = lip;
970         } else {
971                 XFS_BUF_SET_FSPRIVATE(bp, lip);
972         }
973
974         ASSERT((XFS_BUF_IODONE_FUNC(bp) == xfs_buf_iodone_callbacks) ||
975                (XFS_BUF_IODONE_FUNC(bp) == NULL));
976         XFS_BUF_SET_IODONE_FUNC(bp, xfs_buf_iodone_callbacks);
977 }
978
979 STATIC void
980 xfs_buf_do_callbacks(
981         xfs_buf_t       *bp,
982         xfs_log_item_t  *lip)
983 {
984         xfs_log_item_t  *nlip;
985
986         while (lip != NULL) {
987                 nlip = lip->li_bio_list;
988                 ASSERT(lip->li_cb != NULL);
989                 /*
990                  * Clear the next pointer so we don't have any
991                  * confusion if the item is added to another buf.
992                  * Don't touch the log item after calling its
993                  * callback, because it could have freed itself.
994                  */
995                 lip->li_bio_list = NULL;
996                 lip->li_cb(bp, lip);
997                 lip = nlip;
998         }
999 }
1000
1001 /*
1002  * This is the iodone() function for buffers which have had callbacks
1003  * attached to them by xfs_buf_attach_iodone().  It should remove each
1004  * log item from the buffer's list and call the callback of each in turn.
1005  * When done, the buffer's fsprivate field is set to NULL and the buffer
1006  * is unlocked with a call to iodone().
1007  */
1008 void
1009 xfs_buf_iodone_callbacks(
1010         xfs_buf_t       *bp)
1011 {
1012         xfs_log_item_t  *lip;
1013         static ulong    lasttime;
1014         static xfs_buftarg_t *lasttarg;
1015         xfs_mount_t     *mp;
1016
1017         ASSERT(XFS_BUF_FSPRIVATE(bp, void *) != NULL);
1018         lip = XFS_BUF_FSPRIVATE(bp, xfs_log_item_t *);
1019
1020         if (XFS_BUF_GETERROR(bp) != 0) {
1021                 /*
1022                  * If we've already decided to shutdown the filesystem
1023                  * because of IO errors, there's no point in giving this
1024                  * a retry.
1025                  */
1026                 mp = lip->li_mountp;
1027                 if (XFS_FORCED_SHUTDOWN(mp)) {
1028                         ASSERT(XFS_BUF_TARGET(bp) == mp->m_ddev_targp);
1029                         XFS_BUF_SUPER_STALE(bp);
1030                         xfs_buftrace("BUF_IODONE_CB", bp);
1031                         xfs_buf_do_callbacks(bp, lip);
1032                         XFS_BUF_SET_FSPRIVATE(bp, NULL);
1033                         XFS_BUF_CLR_IODONE_FUNC(bp);
1034
1035                         /*
1036                          * XFS_SHUT flag gets set when we go thru the
1037                          * entire buffer cache and deliberately start
1038                          * throwing away delayed write buffers.
1039                          * Since there's no biowait done on those,
1040                          * we should just brelse them.
1041                          */
1042                         if (XFS_BUF_ISSHUT(bp)) {
1043                             XFS_BUF_UNSHUT(bp);
1044                                 xfs_buf_relse(bp);
1045                         } else {
1046                                 xfs_biodone(bp);
1047                         }
1048
1049                         return;
1050                 }
1051
1052                 if ((XFS_BUF_TARGET(bp) != lasttarg) ||
1053                     (time_after(jiffies, (lasttime + 5*HZ)))) {
1054                         lasttime = jiffies;
1055                         prdev("XFS write error in file system meta-data "
1056                               "block 0x%llx in %s",
1057                               XFS_BUF_TARGET(bp),
1058                               (__uint64_t)XFS_BUF_ADDR(bp), mp->m_fsname);
1059                 }
1060                 lasttarg = XFS_BUF_TARGET(bp);
1061
1062                 if (XFS_BUF_ISASYNC(bp)) {
1063                         /*
1064                          * If the write was asynchronous then noone will be
1065                          * looking for the error.  Clear the error state
1066                          * and write the buffer out again delayed write.
1067                          *
1068                          * XXXsup This is OK, so long as we catch these
1069                          * before we start the umount; we don't want these
1070                          * DELWRI metadata bufs to be hanging around.
1071                          */
1072                         XFS_BUF_ERROR(bp,0); /* errno of 0 unsets the flag */
1073
1074                         if (!(XFS_BUF_ISSTALE(bp))) {
1075                                 XFS_BUF_DELAYWRITE(bp);
1076                                 XFS_BUF_DONE(bp);
1077                                 XFS_BUF_SET_START(bp);
1078                         }
1079                         ASSERT(XFS_BUF_IODONE_FUNC(bp));
1080                         xfs_buftrace("BUF_IODONE ASYNC", bp);
1081                         xfs_buf_relse(bp);
1082                 } else {
1083                         /*
1084                          * If the write of the buffer was not asynchronous,
1085                          * then we want to make sure to return the error
1086                          * to the caller of bwrite().  Because of this we
1087                          * cannot clear the B_ERROR state at this point.
1088                          * Instead we install a callback function that
1089                          * will be called when the buffer is released, and
1090                          * that routine will clear the error state and
1091                          * set the buffer to be written out again after
1092                          * some delay.
1093                          */
1094                         /* We actually overwrite the existing b-relse
1095                            function at times, but we're gonna be shutting down
1096                            anyway. */
1097                         XFS_BUF_SET_BRELSE_FUNC(bp,xfs_buf_error_relse);
1098                         XFS_BUF_DONE(bp);
1099                         XFS_BUF_V_IODONESEMA(bp);
1100                 }
1101                 return;
1102         }
1103 #ifdef XFSERRORDEBUG
1104         xfs_buftrace("XFS BUFCB NOERR", bp);
1105 #endif
1106         xfs_buf_do_callbacks(bp, lip);
1107         XFS_BUF_SET_FSPRIVATE(bp, NULL);
1108         XFS_BUF_CLR_IODONE_FUNC(bp);
1109         xfs_biodone(bp);
1110 }
1111
1112 /*
1113  * This is a callback routine attached to a buffer which gets an error
1114  * when being written out synchronously.
1115  */
1116 STATIC void
1117 xfs_buf_error_relse(
1118         xfs_buf_t       *bp)
1119 {
1120         xfs_log_item_t  *lip;
1121         xfs_mount_t     *mp;
1122
1123         lip = XFS_BUF_FSPRIVATE(bp, xfs_log_item_t *);
1124         mp = (xfs_mount_t *)lip->li_mountp;
1125         ASSERT(XFS_BUF_TARGET(bp) == mp->m_ddev_targp);
1126
1127         XFS_BUF_STALE(bp);
1128         XFS_BUF_DONE(bp);
1129         XFS_BUF_UNDELAYWRITE(bp);
1130         XFS_BUF_ERROR(bp,0);
1131         xfs_buftrace("BUF_ERROR_RELSE", bp);
1132         if (! XFS_FORCED_SHUTDOWN(mp))
1133                 xfs_force_shutdown(mp, XFS_METADATA_IO_ERROR);
1134         /*
1135          * We have to unpin the pinned buffers so do the
1136          * callbacks.
1137          */
1138         xfs_buf_do_callbacks(bp, lip);
1139         XFS_BUF_SET_FSPRIVATE(bp, NULL);
1140         XFS_BUF_CLR_IODONE_FUNC(bp);
1141         XFS_BUF_SET_BRELSE_FUNC(bp,NULL);
1142         xfs_buf_relse(bp);
1143 }
1144
1145
1146 /*
1147  * This is the iodone() function for buffers which have been
1148  * logged.  It is called when they are eventually flushed out.
1149  * It should remove the buf item from the AIL, and free the buf item.
1150  * It is called by xfs_buf_iodone_callbacks() above which will take
1151  * care of cleaning up the buffer itself.
1152  */
1153 /* ARGSUSED */
1154 void
1155 xfs_buf_iodone(
1156         xfs_buf_t               *bp,
1157         xfs_buf_log_item_t      *bip)
1158 {
1159         struct xfs_mount        *mp;
1160         SPLDECL(s);
1161
1162         ASSERT(bip->bli_buf == bp);
1163
1164         mp = bip->bli_item.li_mountp;
1165
1166         /*
1167          * If we are forcibly shutting down, this may well be
1168          * off the AIL already. That's because we simulate the
1169          * log-committed callbacks to unpin these buffers. Or we may never
1170          * have put this item on AIL because of the transaction was
1171          * aborted forcibly. xfs_trans_delete_ail() takes care of these.
1172          *
1173          * Either way, AIL is useless if we're forcing a shutdown.
1174          */
1175         AIL_LOCK(mp,s);
1176         /*
1177          * xfs_trans_delete_ail() drops the AIL lock.
1178          */
1179         xfs_trans_delete_ail(mp, (xfs_log_item_t *)bip, s);
1180
1181 #ifdef XFS_TRANS_DEBUG
1182         kmem_free(bip->bli_orig, XFS_BUF_COUNT(bp));
1183         bip->bli_orig = NULL;
1184         kmem_free(bip->bli_logged, XFS_BUF_COUNT(bp) / NBBY);
1185         bip->bli_logged = NULL;
1186 #endif /* XFS_TRANS_DEBUG */
1187
1188 #ifdef XFS_BLI_TRACE
1189         ktrace_free(bip->bli_trace);
1190 #endif
1191         kmem_zone_free(xfs_buf_item_zone, bip);
1192 }
1193
1194 #if defined(XFS_BLI_TRACE)
1195 void
1196 xfs_buf_item_trace(
1197         char                    *id,
1198         xfs_buf_log_item_t      *bip)
1199 {
1200         xfs_buf_t               *bp;
1201         ASSERT(bip->bli_trace != NULL);
1202
1203         bp = bip->bli_buf;
1204         ktrace_enter(bip->bli_trace,
1205                      (void *)id,
1206                      (void *)bip->bli_buf,
1207                      (void *)((unsigned long)bip->bli_flags),
1208                      (void *)((unsigned long)bip->bli_recur),
1209                      (void *)((unsigned long)atomic_read(&bip->bli_refcount)),
1210                      (void *)((unsigned long)
1211                                 (0xFFFFFFFF & XFS_BUF_ADDR(bp) >> 32)),
1212                      (void *)((unsigned long)(0xFFFFFFFF & XFS_BUF_ADDR(bp))),
1213                      (void *)((unsigned long)XFS_BUF_COUNT(bp)),
1214                      (void *)((unsigned long)XFS_BUF_BFLAGS(bp)),
1215                      XFS_BUF_FSPRIVATE(bp, void *),
1216                      XFS_BUF_FSPRIVATE2(bp, void *),
1217                      (void *)(unsigned long)XFS_BUF_ISPINNED(bp),
1218                      (void *)XFS_BUF_IODONE_FUNC(bp),
1219                      (void *)((unsigned long)(XFS_BUF_VALUSEMA(bp))),
1220                      (void *)bip->bli_item.li_desc,
1221                      (void *)((unsigned long)bip->bli_item.li_flags));
1222 }
1223 #endif /* XFS_BLI_TRACE */