]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - fs/xfs/xfs_buf_item.c
52cd8f89ee72b4ba403def680f2ac99585c48aee
[karo-tx-linux.git] / fs / xfs / xfs_buf_item.c
1 /*
2  * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3  * All Rights Reserved.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it would be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write the Free Software Foundation,
16  * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17  */
18 #include "xfs.h"
19 #include "xfs_fs.h"
20 #include "xfs_types.h"
21 #include "xfs_bit.h"
22 #include "xfs_log.h"
23 #include "xfs_trans.h"
24 #include "xfs_sb.h"
25 #include "xfs_ag.h"
26 #include "xfs_mount.h"
27 #include "xfs_buf_item.h"
28 #include "xfs_trans_priv.h"
29 #include "xfs_error.h"
30 #include "xfs_trace.h"
31
32
33 kmem_zone_t     *xfs_buf_item_zone;
34
35 static inline struct xfs_buf_log_item *BUF_ITEM(struct xfs_log_item *lip)
36 {
37         return container_of(lip, struct xfs_buf_log_item, bli_item);
38 }
39
40
41 #ifdef XFS_TRANS_DEBUG
42 /*
43  * This function uses an alternate strategy for tracking the bytes
44  * that the user requests to be logged.  This can then be used
45  * in conjunction with the bli_orig array in the buf log item to
46  * catch bugs in our callers' code.
47  *
48  * We also double check the bits set in xfs_buf_item_log using a
49  * simple algorithm to check that every byte is accounted for.
50  */
51 STATIC void
52 xfs_buf_item_log_debug(
53         xfs_buf_log_item_t      *bip,
54         uint                    first,
55         uint                    last)
56 {
57         uint    x;
58         uint    byte;
59         uint    nbytes;
60         uint    chunk_num;
61         uint    word_num;
62         uint    bit_num;
63         uint    bit_set;
64         uint    *wordp;
65
66         ASSERT(bip->bli_logged != NULL);
67         byte = first;
68         nbytes = last - first + 1;
69         bfset(bip->bli_logged, first, nbytes);
70         for (x = 0; x < nbytes; x++) {
71                 chunk_num = byte >> XFS_BLF_SHIFT;
72                 word_num = chunk_num >> BIT_TO_WORD_SHIFT;
73                 bit_num = chunk_num & (NBWORD - 1);
74                 wordp = &(bip->bli_format.blf_data_map[word_num]);
75                 bit_set = *wordp & (1 << bit_num);
76                 ASSERT(bit_set);
77                 byte++;
78         }
79 }
80
81 /*
82  * This function is called when we flush something into a buffer without
83  * logging it.  This happens for things like inodes which are logged
84  * separately from the buffer.
85  */
86 void
87 xfs_buf_item_flush_log_debug(
88         xfs_buf_t       *bp,
89         uint            first,
90         uint            last)
91 {
92         xfs_buf_log_item_t      *bip = bp->b_fspriv;
93         uint                    nbytes;
94
95         if (bip == NULL || (bip->bli_item.li_type != XFS_LI_BUF))
96                 return;
97
98         ASSERT(bip->bli_logged != NULL);
99         nbytes = last - first + 1;
100         bfset(bip->bli_logged, first, nbytes);
101 }
102
103 /*
104  * This function is called to verify that our callers have logged
105  * all the bytes that they changed.
106  *
107  * It does this by comparing the original copy of the buffer stored in
108  * the buf log item's bli_orig array to the current copy of the buffer
109  * and ensuring that all bytes which mismatch are set in the bli_logged
110  * array of the buf log item.
111  */
112 STATIC void
113 xfs_buf_item_log_check(
114         xfs_buf_log_item_t      *bip)
115 {
116         char            *orig;
117         char            *buffer;
118         int             x;
119         xfs_buf_t       *bp;
120
121         ASSERT(bip->bli_orig != NULL);
122         ASSERT(bip->bli_logged != NULL);
123
124         bp = bip->bli_buf;
125         ASSERT(bp->b_length > 0);
126         ASSERT(bp->b_addr != NULL);
127         orig = bip->bli_orig;
128         buffer = bp->b_addr;
129         for (x = 0; x < BBTOB(bp->b_length); x++) {
130                 if (orig[x] != buffer[x] && !btst(bip->bli_logged, x)) {
131                         xfs_emerg(bp->b_mount,
132                                 "%s: bip %x buffer %x orig %x index %d",
133                                 __func__, bip, bp, orig, x);
134                         ASSERT(0);
135                 }
136         }
137 }
138 #else
139 #define         xfs_buf_item_log_debug(x,y,z)
140 #define         xfs_buf_item_log_check(x)
141 #endif
142
143 STATIC void     xfs_buf_do_callbacks(struct xfs_buf *bp);
144
145 /*
146  * This returns the number of log iovecs needed to log the
147  * given buf log item.
148  *
149  * It calculates this as 1 iovec for the buf log format structure
150  * and 1 for each stretch of non-contiguous chunks to be logged.
151  * Contiguous chunks are logged in a single iovec.
152  *
153  * If the XFS_BLI_STALE flag has been set, then log nothing.
154  */
155 STATIC uint
156 xfs_buf_item_size(
157         struct xfs_log_item     *lip)
158 {
159         struct xfs_buf_log_item *bip = BUF_ITEM(lip);
160         struct xfs_buf          *bp = bip->bli_buf;
161         uint                    nvecs;
162         int                     next_bit;
163         int                     last_bit;
164
165         ASSERT(atomic_read(&bip->bli_refcount) > 0);
166         if (bip->bli_flags & XFS_BLI_STALE) {
167                 /*
168                  * The buffer is stale, so all we need to log
169                  * is the buf log format structure with the
170                  * cancel flag in it.
171                  */
172                 trace_xfs_buf_item_size_stale(bip);
173                 ASSERT(bip->bli_format.blf_flags & XFS_BLF_CANCEL);
174                 return 1;
175         }
176
177         ASSERT(bip->bli_flags & XFS_BLI_LOGGED);
178         nvecs = 1;
179         last_bit = xfs_next_bit(bip->bli_format.blf_data_map,
180                                          bip->bli_format.blf_map_size, 0);
181         ASSERT(last_bit != -1);
182         nvecs++;
183         while (last_bit != -1) {
184                 /*
185                  * This takes the bit number to start looking from and
186                  * returns the next set bit from there.  It returns -1
187                  * if there are no more bits set or the start bit is
188                  * beyond the end of the bitmap.
189                  */
190                 next_bit = xfs_next_bit(bip->bli_format.blf_data_map,
191                                                  bip->bli_format.blf_map_size,
192                                                  last_bit + 1);
193                 /*
194                  * If we run out of bits, leave the loop,
195                  * else if we find a new set of bits bump the number of vecs,
196                  * else keep scanning the current set of bits.
197                  */
198                 if (next_bit == -1) {
199                         last_bit = -1;
200                 } else if (next_bit != last_bit + 1) {
201                         last_bit = next_bit;
202                         nvecs++;
203                 } else if (xfs_buf_offset(bp, next_bit * XFS_BLF_CHUNK) !=
204                            (xfs_buf_offset(bp, last_bit * XFS_BLF_CHUNK) +
205                             XFS_BLF_CHUNK)) {
206                         last_bit = next_bit;
207                         nvecs++;
208                 } else {
209                         last_bit++;
210                 }
211         }
212
213         trace_xfs_buf_item_size(bip);
214         return nvecs;
215 }
216
217 /*
218  * This is called to fill in the vector of log iovecs for the
219  * given log buf item.  It fills the first entry with a buf log
220  * format structure, and the rest point to contiguous chunks
221  * within the buffer.
222  */
223 STATIC void
224 xfs_buf_item_format(
225         struct xfs_log_item     *lip,
226         struct xfs_log_iovec    *vecp)
227 {
228         struct xfs_buf_log_item *bip = BUF_ITEM(lip);
229         struct xfs_buf  *bp = bip->bli_buf;
230         uint            base_size;
231         uint            nvecs;
232         int             first_bit;
233         int             last_bit;
234         int             next_bit;
235         uint            nbits;
236         uint            buffer_offset;
237
238         ASSERT(atomic_read(&bip->bli_refcount) > 0);
239         ASSERT((bip->bli_flags & XFS_BLI_LOGGED) ||
240                (bip->bli_flags & XFS_BLI_STALE));
241
242         /*
243          * Base size is the actual size of the ondisk structure - it reflects
244          * the actual size of the dirty bitmap rather than the size of the in
245          * memory structure.
246          */
247         base_size = offsetof(struct xfs_buf_log_format, blf_data_map) +
248                         (bip->bli_format.blf_map_size *
249                                 sizeof(bip->bli_format.blf_data_map[0]));
250         vecp->i_addr = &bip->bli_format;
251         vecp->i_len = base_size;
252         vecp->i_type = XLOG_REG_TYPE_BFORMAT;
253         vecp++;
254         nvecs = 1;
255
256         /*
257          * If it is an inode buffer, transfer the in-memory state to the
258          * format flags and clear the in-memory state. We do not transfer
259          * this state if the inode buffer allocation has not yet been committed
260          * to the log as setting the XFS_BLI_INODE_BUF flag will prevent
261          * correct replay of the inode allocation.
262          */
263         if (bip->bli_flags & XFS_BLI_INODE_BUF) {
264                 if (!((bip->bli_flags & XFS_BLI_INODE_ALLOC_BUF) &&
265                       xfs_log_item_in_current_chkpt(lip)))
266                         bip->bli_format.blf_flags |= XFS_BLF_INODE_BUF;
267                 bip->bli_flags &= ~XFS_BLI_INODE_BUF;
268         }
269
270         if (bip->bli_flags & XFS_BLI_STALE) {
271                 /*
272                  * The buffer is stale, so all we need to log
273                  * is the buf log format structure with the
274                  * cancel flag in it.
275                  */
276                 trace_xfs_buf_item_format_stale(bip);
277                 ASSERT(bip->bli_format.blf_flags & XFS_BLF_CANCEL);
278                 bip->bli_format.blf_size = nvecs;
279                 return;
280         }
281
282         /*
283          * Fill in an iovec for each set of contiguous chunks.
284          */
285         first_bit = xfs_next_bit(bip->bli_format.blf_data_map,
286                                          bip->bli_format.blf_map_size, 0);
287         ASSERT(first_bit != -1);
288         last_bit = first_bit;
289         nbits = 1;
290         for (;;) {
291                 /*
292                  * This takes the bit number to start looking from and
293                  * returns the next set bit from there.  It returns -1
294                  * if there are no more bits set or the start bit is
295                  * beyond the end of the bitmap.
296                  */
297                 next_bit = xfs_next_bit(bip->bli_format.blf_data_map,
298                                                  bip->bli_format.blf_map_size,
299                                                  (uint)last_bit + 1);
300                 /*
301                  * If we run out of bits fill in the last iovec and get
302                  * out of the loop.
303                  * Else if we start a new set of bits then fill in the
304                  * iovec for the series we were looking at and start
305                  * counting the bits in the new one.
306                  * Else we're still in the same set of bits so just
307                  * keep counting and scanning.
308                  */
309                 if (next_bit == -1) {
310                         buffer_offset = first_bit * XFS_BLF_CHUNK;
311                         vecp->i_addr = xfs_buf_offset(bp, buffer_offset);
312                         vecp->i_len = nbits * XFS_BLF_CHUNK;
313                         vecp->i_type = XLOG_REG_TYPE_BCHUNK;
314                         nvecs++;
315                         break;
316                 } else if (next_bit != last_bit + 1) {
317                         buffer_offset = first_bit * XFS_BLF_CHUNK;
318                         vecp->i_addr = xfs_buf_offset(bp, buffer_offset);
319                         vecp->i_len = nbits * XFS_BLF_CHUNK;
320                         vecp->i_type = XLOG_REG_TYPE_BCHUNK;
321                         nvecs++;
322                         vecp++;
323                         first_bit = next_bit;
324                         last_bit = next_bit;
325                         nbits = 1;
326                 } else if (xfs_buf_offset(bp, next_bit << XFS_BLF_SHIFT) !=
327                            (xfs_buf_offset(bp, last_bit << XFS_BLF_SHIFT) +
328                             XFS_BLF_CHUNK)) {
329                         buffer_offset = first_bit * XFS_BLF_CHUNK;
330                         vecp->i_addr = xfs_buf_offset(bp, buffer_offset);
331                         vecp->i_len = nbits * XFS_BLF_CHUNK;
332                         vecp->i_type = XLOG_REG_TYPE_BCHUNK;
333 /* You would think we need to bump the nvecs here too, but we do not
334  * this number is used by recovery, and it gets confused by the boundary
335  * split here
336  *                      nvecs++;
337  */
338                         vecp++;
339                         first_bit = next_bit;
340                         last_bit = next_bit;
341                         nbits = 1;
342                 } else {
343                         last_bit++;
344                         nbits++;
345                 }
346         }
347         bip->bli_format.blf_size = nvecs;
348
349         /*
350          * Check to make sure everything is consistent.
351          */
352         trace_xfs_buf_item_format(bip);
353         xfs_buf_item_log_check(bip);
354 }
355
356 /*
357  * This is called to pin the buffer associated with the buf log item in memory
358  * so it cannot be written out.
359  *
360  * We also always take a reference to the buffer log item here so that the bli
361  * is held while the item is pinned in memory. This means that we can
362  * unconditionally drop the reference count a transaction holds when the
363  * transaction is completed.
364  */
365 STATIC void
366 xfs_buf_item_pin(
367         struct xfs_log_item     *lip)
368 {
369         struct xfs_buf_log_item *bip = BUF_ITEM(lip);
370
371         ASSERT(atomic_read(&bip->bli_refcount) > 0);
372         ASSERT((bip->bli_flags & XFS_BLI_LOGGED) ||
373                (bip->bli_flags & XFS_BLI_STALE));
374
375         trace_xfs_buf_item_pin(bip);
376
377         atomic_inc(&bip->bli_refcount);
378         atomic_inc(&bip->bli_buf->b_pin_count);
379 }
380
381 /*
382  * This is called to unpin the buffer associated with the buf log
383  * item which was previously pinned with a call to xfs_buf_item_pin().
384  *
385  * Also drop the reference to the buf item for the current transaction.
386  * If the XFS_BLI_STALE flag is set and we are the last reference,
387  * then free up the buf log item and unlock the buffer.
388  *
389  * If the remove flag is set we are called from uncommit in the
390  * forced-shutdown path.  If that is true and the reference count on
391  * the log item is going to drop to zero we need to free the item's
392  * descriptor in the transaction.
393  */
394 STATIC void
395 xfs_buf_item_unpin(
396         struct xfs_log_item     *lip,
397         int                     remove)
398 {
399         struct xfs_buf_log_item *bip = BUF_ITEM(lip);
400         xfs_buf_t       *bp = bip->bli_buf;
401         struct xfs_ail  *ailp = lip->li_ailp;
402         int             stale = bip->bli_flags & XFS_BLI_STALE;
403         int             freed;
404
405         ASSERT(bp->b_fspriv == bip);
406         ASSERT(atomic_read(&bip->bli_refcount) > 0);
407
408         trace_xfs_buf_item_unpin(bip);
409
410         freed = atomic_dec_and_test(&bip->bli_refcount);
411
412         if (atomic_dec_and_test(&bp->b_pin_count))
413                 wake_up_all(&bp->b_waiters);
414
415         if (freed && stale) {
416                 ASSERT(bip->bli_flags & XFS_BLI_STALE);
417                 ASSERT(xfs_buf_islocked(bp));
418                 ASSERT(XFS_BUF_ISSTALE(bp));
419                 ASSERT(bip->bli_format.blf_flags & XFS_BLF_CANCEL);
420
421                 trace_xfs_buf_item_unpin_stale(bip);
422
423                 if (remove) {
424                         /*
425                          * If we are in a transaction context, we have to
426                          * remove the log item from the transaction as we are
427                          * about to release our reference to the buffer.  If we
428                          * don't, the unlock that occurs later in
429                          * xfs_trans_uncommit() will try to reference the
430                          * buffer which we no longer have a hold on.
431                          */
432                         if (lip->li_desc)
433                                 xfs_trans_del_item(lip);
434
435                         /*
436                          * Since the transaction no longer refers to the buffer,
437                          * the buffer should no longer refer to the transaction.
438                          */
439                         bp->b_transp = NULL;
440                 }
441
442                 /*
443                  * If we get called here because of an IO error, we may
444                  * or may not have the item on the AIL. xfs_trans_ail_delete()
445                  * will take care of that situation.
446                  * xfs_trans_ail_delete() drops the AIL lock.
447                  */
448                 if (bip->bli_flags & XFS_BLI_STALE_INODE) {
449                         xfs_buf_do_callbacks(bp);
450                         bp->b_fspriv = NULL;
451                         bp->b_iodone = NULL;
452                 } else {
453                         spin_lock(&ailp->xa_lock);
454                         xfs_trans_ail_delete(ailp, lip, SHUTDOWN_LOG_IO_ERROR);
455                         xfs_buf_item_relse(bp);
456                         ASSERT(bp->b_fspriv == NULL);
457                 }
458                 xfs_buf_relse(bp);
459         } else if (freed && remove) {
460                 xfs_buf_lock(bp);
461                 xfs_buf_ioerror(bp, EIO);
462                 XFS_BUF_UNDONE(bp);
463                 xfs_buf_stale(bp);
464                 xfs_buf_ioend(bp, 0);
465         }
466 }
467
468 STATIC uint
469 xfs_buf_item_push(
470         struct xfs_log_item     *lip,
471         struct list_head        *buffer_list)
472 {
473         struct xfs_buf_log_item *bip = BUF_ITEM(lip);
474         struct xfs_buf          *bp = bip->bli_buf;
475         uint                    rval = XFS_ITEM_SUCCESS;
476
477         if (xfs_buf_ispinned(bp))
478                 return XFS_ITEM_PINNED;
479         if (!xfs_buf_trylock(bp))
480                 return XFS_ITEM_LOCKED;
481
482         ASSERT(!(bip->bli_flags & XFS_BLI_STALE));
483
484         trace_xfs_buf_item_push(bip);
485
486         if (!xfs_buf_delwri_queue(bp, buffer_list))
487                 rval = XFS_ITEM_FLUSHING;
488         xfs_buf_unlock(bp);
489         return rval;
490 }
491
492 /*
493  * Release the buffer associated with the buf log item.  If there is no dirty
494  * logged data associated with the buffer recorded in the buf log item, then
495  * free the buf log item and remove the reference to it in the buffer.
496  *
497  * This call ignores the recursion count.  It is only called when the buffer
498  * should REALLY be unlocked, regardless of the recursion count.
499  *
500  * We unconditionally drop the transaction's reference to the log item. If the
501  * item was logged, then another reference was taken when it was pinned, so we
502  * can safely drop the transaction reference now.  This also allows us to avoid
503  * potential races with the unpin code freeing the bli by not referencing the
504  * bli after we've dropped the reference count.
505  *
506  * If the XFS_BLI_HOLD flag is set in the buf log item, then free the log item
507  * if necessary but do not unlock the buffer.  This is for support of
508  * xfs_trans_bhold(). Make sure the XFS_BLI_HOLD field is cleared if we don't
509  * free the item.
510  */
511 STATIC void
512 xfs_buf_item_unlock(
513         struct xfs_log_item     *lip)
514 {
515         struct xfs_buf_log_item *bip = BUF_ITEM(lip);
516         struct xfs_buf          *bp = bip->bli_buf;
517         int                     aborted;
518         uint                    hold;
519
520         /* Clear the buffer's association with this transaction. */
521         bp->b_transp = NULL;
522
523         /*
524          * If this is a transaction abort, don't return early.  Instead, allow
525          * the brelse to happen.  Normally it would be done for stale
526          * (cancelled) buffers at unpin time, but we'll never go through the
527          * pin/unpin cycle if we abort inside commit.
528          */
529         aborted = (lip->li_flags & XFS_LI_ABORTED) != 0;
530
531         /*
532          * Before possibly freeing the buf item, determine if we should
533          * release the buffer at the end of this routine.
534          */
535         hold = bip->bli_flags & XFS_BLI_HOLD;
536
537         /* Clear the per transaction state. */
538         bip->bli_flags &= ~(XFS_BLI_LOGGED | XFS_BLI_HOLD);
539
540         /*
541          * If the buf item is marked stale, then don't do anything.  We'll
542          * unlock the buffer and free the buf item when the buffer is unpinned
543          * for the last time.
544          */
545         if (bip->bli_flags & XFS_BLI_STALE) {
546                 trace_xfs_buf_item_unlock_stale(bip);
547                 ASSERT(bip->bli_format.blf_flags & XFS_BLF_CANCEL);
548                 if (!aborted) {
549                         atomic_dec(&bip->bli_refcount);
550                         return;
551                 }
552         }
553
554         trace_xfs_buf_item_unlock(bip);
555
556         /*
557          * If the buf item isn't tracking any data, free it, otherwise drop the
558          * reference we hold to it.
559          */
560         if (xfs_bitmap_empty(bip->bli_format.blf_data_map,
561                              bip->bli_format.blf_map_size))
562                 xfs_buf_item_relse(bp);
563         else
564                 atomic_dec(&bip->bli_refcount);
565
566         if (!hold)
567                 xfs_buf_relse(bp);
568 }
569
570 /*
571  * This is called to find out where the oldest active copy of the
572  * buf log item in the on disk log resides now that the last log
573  * write of it completed at the given lsn.
574  * We always re-log all the dirty data in a buffer, so usually the
575  * latest copy in the on disk log is the only one that matters.  For
576  * those cases we simply return the given lsn.
577  *
578  * The one exception to this is for buffers full of newly allocated
579  * inodes.  These buffers are only relogged with the XFS_BLI_INODE_BUF
580  * flag set, indicating that only the di_next_unlinked fields from the
581  * inodes in the buffers will be replayed during recovery.  If the
582  * original newly allocated inode images have not yet been flushed
583  * when the buffer is so relogged, then we need to make sure that we
584  * keep the old images in the 'active' portion of the log.  We do this
585  * by returning the original lsn of that transaction here rather than
586  * the current one.
587  */
588 STATIC xfs_lsn_t
589 xfs_buf_item_committed(
590         struct xfs_log_item     *lip,
591         xfs_lsn_t               lsn)
592 {
593         struct xfs_buf_log_item *bip = BUF_ITEM(lip);
594
595         trace_xfs_buf_item_committed(bip);
596
597         if ((bip->bli_flags & XFS_BLI_INODE_ALLOC_BUF) && lip->li_lsn != 0)
598                 return lip->li_lsn;
599         return lsn;
600 }
601
602 STATIC void
603 xfs_buf_item_committing(
604         struct xfs_log_item     *lip,
605         xfs_lsn_t               commit_lsn)
606 {
607 }
608
609 /*
610  * This is the ops vector shared by all buf log items.
611  */
612 static const struct xfs_item_ops xfs_buf_item_ops = {
613         .iop_size       = xfs_buf_item_size,
614         .iop_format     = xfs_buf_item_format,
615         .iop_pin        = xfs_buf_item_pin,
616         .iop_unpin      = xfs_buf_item_unpin,
617         .iop_unlock     = xfs_buf_item_unlock,
618         .iop_committed  = xfs_buf_item_committed,
619         .iop_push       = xfs_buf_item_push,
620         .iop_committing = xfs_buf_item_committing
621 };
622
623
624 /*
625  * Allocate a new buf log item to go with the given buffer.
626  * Set the buffer's b_fsprivate field to point to the new
627  * buf log item.  If there are other item's attached to the
628  * buffer (see xfs_buf_attach_iodone() below), then put the
629  * buf log item at the front.
630  */
631 void
632 xfs_buf_item_init(
633         xfs_buf_t       *bp,
634         xfs_mount_t     *mp)
635 {
636         xfs_log_item_t          *lip = bp->b_fspriv;
637         xfs_buf_log_item_t      *bip;
638         int                     chunks;
639         int                     map_size;
640
641         /*
642          * Check to see if there is already a buf log item for
643          * this buffer.  If there is, it is guaranteed to be
644          * the first.  If we do already have one, there is
645          * nothing to do here so return.
646          */
647         ASSERT(bp->b_target->bt_mount == mp);
648         if (lip != NULL && lip->li_type == XFS_LI_BUF)
649                 return;
650
651         /*
652          * chunks is the number of XFS_BLF_CHUNK size pieces
653          * the buffer can be divided into. Make sure not to
654          * truncate any pieces.  map_size is the size of the
655          * bitmap needed to describe the chunks of the buffer.
656          */
657         chunks = (int)((BBTOB(bp->b_length) + (XFS_BLF_CHUNK - 1)) >>
658                                                                 XFS_BLF_SHIFT);
659         map_size = (int)((chunks + NBWORD) >> BIT_TO_WORD_SHIFT);
660
661         bip = (xfs_buf_log_item_t*)kmem_zone_zalloc(xfs_buf_item_zone,
662                                                     KM_SLEEP);
663         xfs_log_item_init(mp, &bip->bli_item, XFS_LI_BUF, &xfs_buf_item_ops);
664         bip->bli_buf = bp;
665         xfs_buf_hold(bp);
666         bip->bli_format.blf_type = XFS_LI_BUF;
667         bip->bli_format.blf_blkno = (__int64_t)XFS_BUF_ADDR(bp);
668         bip->bli_format.blf_len = (ushort)bp->b_length;
669         bip->bli_format.blf_map_size = map_size;
670
671 #ifdef XFS_TRANS_DEBUG
672         /*
673          * Allocate the arrays for tracking what needs to be logged
674          * and what our callers request to be logged.  bli_orig
675          * holds a copy of the original, clean buffer for comparison
676          * against, and bli_logged keeps a 1 bit flag per byte in
677          * the buffer to indicate which bytes the callers have asked
678          * to have logged.
679          */
680         bip->bli_orig = kmem_alloc(BBTOB(bp->b_length), KM_SLEEP);
681         memcpy(bip->bli_orig, bp->b_addr, BBTOB(bp->b_length));
682         bip->bli_logged = kmem_zalloc(BBTOB(bp->b_length) / NBBY, KM_SLEEP);
683 #endif
684
685         /*
686          * Put the buf item into the list of items attached to the
687          * buffer at the front.
688          */
689         if (bp->b_fspriv)
690                 bip->bli_item.li_bio_list = bp->b_fspriv;
691         bp->b_fspriv = bip;
692 }
693
694
695 /*
696  * Mark bytes first through last inclusive as dirty in the buf
697  * item's bitmap.
698  */
699 void
700 xfs_buf_item_log(
701         xfs_buf_log_item_t      *bip,
702         uint                    first,
703         uint                    last)
704 {
705         uint            first_bit;
706         uint            last_bit;
707         uint            bits_to_set;
708         uint            bits_set;
709         uint            word_num;
710         uint            *wordp;
711         uint            bit;
712         uint            end_bit;
713         uint            mask;
714
715         /*
716          * Mark the item as having some dirty data for
717          * quick reference in xfs_buf_item_dirty.
718          */
719         bip->bli_flags |= XFS_BLI_DIRTY;
720
721         /*
722          * Convert byte offsets to bit numbers.
723          */
724         first_bit = first >> XFS_BLF_SHIFT;
725         last_bit = last >> XFS_BLF_SHIFT;
726
727         /*
728          * Calculate the total number of bits to be set.
729          */
730         bits_to_set = last_bit - first_bit + 1;
731
732         /*
733          * Get a pointer to the first word in the bitmap
734          * to set a bit in.
735          */
736         word_num = first_bit >> BIT_TO_WORD_SHIFT;
737         wordp = &(bip->bli_format.blf_data_map[word_num]);
738
739         /*
740          * Calculate the starting bit in the first word.
741          */
742         bit = first_bit & (uint)(NBWORD - 1);
743
744         /*
745          * First set any bits in the first word of our range.
746          * If it starts at bit 0 of the word, it will be
747          * set below rather than here.  That is what the variable
748          * bit tells us. The variable bits_set tracks the number
749          * of bits that have been set so far.  End_bit is the number
750          * of the last bit to be set in this word plus one.
751          */
752         if (bit) {
753                 end_bit = MIN(bit + bits_to_set, (uint)NBWORD);
754                 mask = ((1 << (end_bit - bit)) - 1) << bit;
755                 *wordp |= mask;
756                 wordp++;
757                 bits_set = end_bit - bit;
758         } else {
759                 bits_set = 0;
760         }
761
762         /*
763          * Now set bits a whole word at a time that are between
764          * first_bit and last_bit.
765          */
766         while ((bits_to_set - bits_set) >= NBWORD) {
767                 *wordp |= 0xffffffff;
768                 bits_set += NBWORD;
769                 wordp++;
770         }
771
772         /*
773          * Finally, set any bits left to be set in one last partial word.
774          */
775         end_bit = bits_to_set - bits_set;
776         if (end_bit) {
777                 mask = (1 << end_bit) - 1;
778                 *wordp |= mask;
779         }
780
781         xfs_buf_item_log_debug(bip, first, last);
782 }
783
784
785 /*
786  * Return 1 if the buffer has some data that has been logged (at any
787  * point, not just the current transaction) and 0 if not.
788  */
789 uint
790 xfs_buf_item_dirty(
791         xfs_buf_log_item_t      *bip)
792 {
793         return (bip->bli_flags & XFS_BLI_DIRTY);
794 }
795
796 STATIC void
797 xfs_buf_item_free(
798         xfs_buf_log_item_t      *bip)
799 {
800 #ifdef XFS_TRANS_DEBUG
801         kmem_free(bip->bli_orig);
802         kmem_free(bip->bli_logged);
803 #endif /* XFS_TRANS_DEBUG */
804
805         kmem_zone_free(xfs_buf_item_zone, bip);
806 }
807
808 /*
809  * This is called when the buf log item is no longer needed.  It should
810  * free the buf log item associated with the given buffer and clear
811  * the buffer's pointer to the buf log item.  If there are no more
812  * items in the list, clear the b_iodone field of the buffer (see
813  * xfs_buf_attach_iodone() below).
814  */
815 void
816 xfs_buf_item_relse(
817         xfs_buf_t       *bp)
818 {
819         xfs_buf_log_item_t      *bip;
820
821         trace_xfs_buf_item_relse(bp, _RET_IP_);
822
823         bip = bp->b_fspriv;
824         bp->b_fspriv = bip->bli_item.li_bio_list;
825         if (bp->b_fspriv == NULL)
826                 bp->b_iodone = NULL;
827
828         xfs_buf_rele(bp);
829         xfs_buf_item_free(bip);
830 }
831
832
833 /*
834  * Add the given log item with its callback to the list of callbacks
835  * to be called when the buffer's I/O completes.  If it is not set
836  * already, set the buffer's b_iodone() routine to be
837  * xfs_buf_iodone_callbacks() and link the log item into the list of
838  * items rooted at b_fsprivate.  Items are always added as the second
839  * entry in the list if there is a first, because the buf item code
840  * assumes that the buf log item is first.
841  */
842 void
843 xfs_buf_attach_iodone(
844         xfs_buf_t       *bp,
845         void            (*cb)(xfs_buf_t *, xfs_log_item_t *),
846         xfs_log_item_t  *lip)
847 {
848         xfs_log_item_t  *head_lip;
849
850         ASSERT(xfs_buf_islocked(bp));
851
852         lip->li_cb = cb;
853         head_lip = bp->b_fspriv;
854         if (head_lip) {
855                 lip->li_bio_list = head_lip->li_bio_list;
856                 head_lip->li_bio_list = lip;
857         } else {
858                 bp->b_fspriv = lip;
859         }
860
861         ASSERT(bp->b_iodone == NULL ||
862                bp->b_iodone == xfs_buf_iodone_callbacks);
863         bp->b_iodone = xfs_buf_iodone_callbacks;
864 }
865
866 /*
867  * We can have many callbacks on a buffer. Running the callbacks individually
868  * can cause a lot of contention on the AIL lock, so we allow for a single
869  * callback to be able to scan the remaining lip->li_bio_list for other items
870  * of the same type and callback to be processed in the first call.
871  *
872  * As a result, the loop walking the callback list below will also modify the
873  * list. it removes the first item from the list and then runs the callback.
874  * The loop then restarts from the new head of the list. This allows the
875  * callback to scan and modify the list attached to the buffer and we don't
876  * have to care about maintaining a next item pointer.
877  */
878 STATIC void
879 xfs_buf_do_callbacks(
880         struct xfs_buf          *bp)
881 {
882         struct xfs_log_item     *lip;
883
884         while ((lip = bp->b_fspriv) != NULL) {
885                 bp->b_fspriv = lip->li_bio_list;
886                 ASSERT(lip->li_cb != NULL);
887                 /*
888                  * Clear the next pointer so we don't have any
889                  * confusion if the item is added to another buf.
890                  * Don't touch the log item after calling its
891                  * callback, because it could have freed itself.
892                  */
893                 lip->li_bio_list = NULL;
894                 lip->li_cb(bp, lip);
895         }
896 }
897
898 /*
899  * This is the iodone() function for buffers which have had callbacks
900  * attached to them by xfs_buf_attach_iodone().  It should remove each
901  * log item from the buffer's list and call the callback of each in turn.
902  * When done, the buffer's fsprivate field is set to NULL and the buffer
903  * is unlocked with a call to iodone().
904  */
905 void
906 xfs_buf_iodone_callbacks(
907         struct xfs_buf          *bp)
908 {
909         struct xfs_log_item     *lip = bp->b_fspriv;
910         struct xfs_mount        *mp = lip->li_mountp;
911         static ulong            lasttime;
912         static xfs_buftarg_t    *lasttarg;
913
914         if (likely(!xfs_buf_geterror(bp)))
915                 goto do_callbacks;
916
917         /*
918          * If we've already decided to shutdown the filesystem because of
919          * I/O errors, there's no point in giving this a retry.
920          */
921         if (XFS_FORCED_SHUTDOWN(mp)) {
922                 xfs_buf_stale(bp);
923                 XFS_BUF_DONE(bp);
924                 trace_xfs_buf_item_iodone(bp, _RET_IP_);
925                 goto do_callbacks;
926         }
927
928         if (bp->b_target != lasttarg ||
929             time_after(jiffies, (lasttime + 5*HZ))) {
930                 lasttime = jiffies;
931                 xfs_buf_ioerror_alert(bp, __func__);
932         }
933         lasttarg = bp->b_target;
934
935         /*
936          * If the write was asynchronous then no one will be looking for the
937          * error.  Clear the error state and write the buffer out again.
938          *
939          * XXX: This helps against transient write errors, but we need to find
940          * a way to shut the filesystem down if the writes keep failing.
941          *
942          * In practice we'll shut the filesystem down soon as non-transient
943          * erorrs tend to affect the whole device and a failing log write
944          * will make us give up.  But we really ought to do better here.
945          */
946         if (XFS_BUF_ISASYNC(bp)) {
947                 ASSERT(bp->b_iodone != NULL);
948
949                 trace_xfs_buf_item_iodone_async(bp, _RET_IP_);
950
951                 xfs_buf_ioerror(bp, 0); /* errno of 0 unsets the flag */
952
953                 if (!XFS_BUF_ISSTALE(bp)) {
954                         bp->b_flags |= XBF_WRITE | XBF_ASYNC | XBF_DONE;
955                         xfs_bdstrat_cb(bp);
956                 } else {
957                         xfs_buf_relse(bp);
958                 }
959
960                 return;
961         }
962
963         /*
964          * If the write of the buffer was synchronous, we want to make
965          * sure to return the error to the caller of xfs_bwrite().
966          */
967         xfs_buf_stale(bp);
968         XFS_BUF_DONE(bp);
969
970         trace_xfs_buf_error_relse(bp, _RET_IP_);
971
972 do_callbacks:
973         xfs_buf_do_callbacks(bp);
974         bp->b_fspriv = NULL;
975         bp->b_iodone = NULL;
976         xfs_buf_ioend(bp, 0);
977 }
978
979 /*
980  * This is the iodone() function for buffers which have been
981  * logged.  It is called when they are eventually flushed out.
982  * It should remove the buf item from the AIL, and free the buf item.
983  * It is called by xfs_buf_iodone_callbacks() above which will take
984  * care of cleaning up the buffer itself.
985  */
986 void
987 xfs_buf_iodone(
988         struct xfs_buf          *bp,
989         struct xfs_log_item     *lip)
990 {
991         struct xfs_ail          *ailp = lip->li_ailp;
992
993         ASSERT(BUF_ITEM(lip)->bli_buf == bp);
994
995         xfs_buf_rele(bp);
996
997         /*
998          * If we are forcibly shutting down, this may well be
999          * off the AIL already. That's because we simulate the
1000          * log-committed callbacks to unpin these buffers. Or we may never
1001          * have put this item on AIL because of the transaction was
1002          * aborted forcibly. xfs_trans_ail_delete() takes care of these.
1003          *
1004          * Either way, AIL is useless if we're forcing a shutdown.
1005          */
1006         spin_lock(&ailp->xa_lock);
1007         xfs_trans_ail_delete(ailp, lip, SHUTDOWN_CORRUPT_INCORE);
1008         xfs_buf_item_free(BUF_ITEM(lip));
1009 }