Merge master.kernel.org:/pub/scm/linux/kernel/git/davem/net-2.6
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / xfs / xfs_buf_item.c
bloba4aa53974f7650668204ea2ce22f93de28adfa9c
1 /*
2 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
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.
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.
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
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_inum.h"
24 #include "xfs_trans.h"
25 #include "xfs_sb.h"
26 #include "xfs_dmapi.h"
27 #include "xfs_mount.h"
28 #include "xfs_buf_item.h"
29 #include "xfs_trans_priv.h"
30 #include "xfs_error.h"
33 kmem_zone_t *xfs_buf_item_zone;
35 #ifdef XFS_TRANS_DEBUG
37 * This function uses an alternate strategy for tracking the bytes
38 * that the user requests to be logged. This can then be used
39 * in conjunction with the bli_orig array in the buf log item to
40 * catch bugs in our callers' code.
42 * We also double check the bits set in xfs_buf_item_log using a
43 * simple algorithm to check that every byte is accounted for.
45 STATIC void
46 xfs_buf_item_log_debug(
47 xfs_buf_log_item_t *bip,
48 uint first,
49 uint last)
51 uint x;
52 uint byte;
53 uint nbytes;
54 uint chunk_num;
55 uint word_num;
56 uint bit_num;
57 uint bit_set;
58 uint *wordp;
60 ASSERT(bip->bli_logged != NULL);
61 byte = first;
62 nbytes = last - first + 1;
63 bfset(bip->bli_logged, first, nbytes);
64 for (x = 0; x < nbytes; x++) {
65 chunk_num = byte >> XFS_BLI_SHIFT;
66 word_num = chunk_num >> BIT_TO_WORD_SHIFT;
67 bit_num = chunk_num & (NBWORD - 1);
68 wordp = &(bip->bli_format.blf_data_map[word_num]);
69 bit_set = *wordp & (1 << bit_num);
70 ASSERT(bit_set);
71 byte++;
76 * This function is called when we flush something into a buffer without
77 * logging it. This happens for things like inodes which are logged
78 * separately from the buffer.
80 void
81 xfs_buf_item_flush_log_debug(
82 xfs_buf_t *bp,
83 uint first,
84 uint last)
86 xfs_buf_log_item_t *bip;
87 uint nbytes;
89 bip = XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t*);
90 if ((bip == NULL) || (bip->bli_item.li_type != XFS_LI_BUF)) {
91 return;
94 ASSERT(bip->bli_logged != NULL);
95 nbytes = last - first + 1;
96 bfset(bip->bli_logged, first, nbytes);
100 * This function is called to verify that our callers have logged
101 * all the bytes that they changed.
103 * It does this by comparing the original copy of the buffer stored in
104 * the buf log item's bli_orig array to the current copy of the buffer
105 * and ensuring that all bytes which mismatch are set in the bli_logged
106 * array of the buf log item.
108 STATIC void
109 xfs_buf_item_log_check(
110 xfs_buf_log_item_t *bip)
112 char *orig;
113 char *buffer;
114 int x;
115 xfs_buf_t *bp;
117 ASSERT(bip->bli_orig != NULL);
118 ASSERT(bip->bli_logged != NULL);
120 bp = bip->bli_buf;
121 ASSERT(XFS_BUF_COUNT(bp) > 0);
122 ASSERT(XFS_BUF_PTR(bp) != NULL);
123 orig = bip->bli_orig;
124 buffer = XFS_BUF_PTR(bp);
125 for (x = 0; x < XFS_BUF_COUNT(bp); x++) {
126 if (orig[x] != buffer[x] && !btst(bip->bli_logged, x))
127 cmn_err(CE_PANIC,
128 "xfs_buf_item_log_check bip %x buffer %x orig %x index %d",
129 bip, bp, orig, x);
132 #else
133 #define xfs_buf_item_log_debug(x,y,z)
134 #define xfs_buf_item_log_check(x)
135 #endif
137 STATIC void xfs_buf_error_relse(xfs_buf_t *bp);
138 STATIC void xfs_buf_do_callbacks(xfs_buf_t *bp, xfs_log_item_t *lip);
141 * This returns the number of log iovecs needed to log the
142 * given buf log item.
144 * It calculates this as 1 iovec for the buf log format structure
145 * and 1 for each stretch of non-contiguous chunks to be logged.
146 * Contiguous chunks are logged in a single iovec.
148 * If the XFS_BLI_STALE flag has been set, then log nothing.
150 STATIC uint
151 xfs_buf_item_size(
152 xfs_buf_log_item_t *bip)
154 uint nvecs;
155 int next_bit;
156 int last_bit;
157 xfs_buf_t *bp;
159 ASSERT(atomic_read(&bip->bli_refcount) > 0);
160 if (bip->bli_flags & XFS_BLI_STALE) {
162 * The buffer is stale, so all we need to log
163 * is the buf log format structure with the
164 * cancel flag in it.
166 xfs_buf_item_trace("SIZE STALE", bip);
167 ASSERT(bip->bli_format.blf_flags & XFS_BLI_CANCEL);
168 return 1;
171 bp = bip->bli_buf;
172 ASSERT(bip->bli_flags & XFS_BLI_LOGGED);
173 nvecs = 1;
174 last_bit = xfs_next_bit(bip->bli_format.blf_data_map,
175 bip->bli_format.blf_map_size, 0);
176 ASSERT(last_bit != -1);
177 nvecs++;
178 while (last_bit != -1) {
180 * This takes the bit number to start looking from and
181 * returns the next set bit from there. It returns -1
182 * if there are no more bits set or the start bit is
183 * beyond the end of the bitmap.
185 next_bit = xfs_next_bit(bip->bli_format.blf_data_map,
186 bip->bli_format.blf_map_size,
187 last_bit + 1);
189 * If we run out of bits, leave the loop,
190 * else if we find a new set of bits bump the number of vecs,
191 * else keep scanning the current set of bits.
193 if (next_bit == -1) {
194 last_bit = -1;
195 } else if (next_bit != last_bit + 1) {
196 last_bit = next_bit;
197 nvecs++;
198 } else if (xfs_buf_offset(bp, next_bit * XFS_BLI_CHUNK) !=
199 (xfs_buf_offset(bp, last_bit * XFS_BLI_CHUNK) +
200 XFS_BLI_CHUNK)) {
201 last_bit = next_bit;
202 nvecs++;
203 } else {
204 last_bit++;
208 xfs_buf_item_trace("SIZE NORM", bip);
209 return nvecs;
213 * This is called to fill in the vector of log iovecs for the
214 * given log buf item. It fills the first entry with a buf log
215 * format structure, and the rest point to contiguous chunks
216 * within the buffer.
218 STATIC void
219 xfs_buf_item_format(
220 xfs_buf_log_item_t *bip,
221 xfs_log_iovec_t *log_vector)
223 uint base_size;
224 uint nvecs;
225 xfs_log_iovec_t *vecp;
226 xfs_buf_t *bp;
227 int first_bit;
228 int last_bit;
229 int next_bit;
230 uint nbits;
231 uint buffer_offset;
233 ASSERT(atomic_read(&bip->bli_refcount) > 0);
234 ASSERT((bip->bli_flags & XFS_BLI_LOGGED) ||
235 (bip->bli_flags & XFS_BLI_STALE));
236 bp = bip->bli_buf;
237 ASSERT(XFS_BUF_BP_ISMAPPED(bp));
238 vecp = log_vector;
241 * The size of the base structure is the size of the
242 * declared structure plus the space for the extra words
243 * of the bitmap. We subtract one from the map size, because
244 * the first element of the bitmap is accounted for in the
245 * size of the base structure.
247 base_size =
248 (uint)(sizeof(xfs_buf_log_format_t) +
249 ((bip->bli_format.blf_map_size - 1) * sizeof(uint)));
250 vecp->i_addr = (xfs_caddr_t)&bip->bli_format;
251 vecp->i_len = base_size;
252 XLOG_VEC_SET_TYPE(vecp, XLOG_REG_TYPE_BFORMAT);
253 vecp++;
254 nvecs = 1;
256 if (bip->bli_flags & XFS_BLI_STALE) {
258 * The buffer is stale, so all we need to log
259 * is the buf log format structure with the
260 * cancel flag in it.
262 xfs_buf_item_trace("FORMAT STALE", bip);
263 ASSERT(bip->bli_format.blf_flags & XFS_BLI_CANCEL);
264 bip->bli_format.blf_size = nvecs;
265 return;
269 * Fill in an iovec for each set of contiguous chunks.
271 first_bit = xfs_next_bit(bip->bli_format.blf_data_map,
272 bip->bli_format.blf_map_size, 0);
273 ASSERT(first_bit != -1);
274 last_bit = first_bit;
275 nbits = 1;
276 for (;;) {
278 * This takes the bit number to start looking from and
279 * returns the next set bit from there. It returns -1
280 * if there are no more bits set or the start bit is
281 * beyond the end of the bitmap.
283 next_bit = xfs_next_bit(bip->bli_format.blf_data_map,
284 bip->bli_format.blf_map_size,
285 (uint)last_bit + 1);
287 * If we run out of bits fill in the last iovec and get
288 * out of the loop.
289 * Else if we start a new set of bits then fill in the
290 * iovec for the series we were looking at and start
291 * counting the bits in the new one.
292 * Else we're still in the same set of bits so just
293 * keep counting and scanning.
295 if (next_bit == -1) {
296 buffer_offset = first_bit * XFS_BLI_CHUNK;
297 vecp->i_addr = xfs_buf_offset(bp, buffer_offset);
298 vecp->i_len = nbits * XFS_BLI_CHUNK;
299 XLOG_VEC_SET_TYPE(vecp, XLOG_REG_TYPE_BCHUNK);
300 nvecs++;
301 break;
302 } else if (next_bit != last_bit + 1) {
303 buffer_offset = first_bit * XFS_BLI_CHUNK;
304 vecp->i_addr = xfs_buf_offset(bp, buffer_offset);
305 vecp->i_len = nbits * XFS_BLI_CHUNK;
306 XLOG_VEC_SET_TYPE(vecp, XLOG_REG_TYPE_BCHUNK);
307 nvecs++;
308 vecp++;
309 first_bit = next_bit;
310 last_bit = next_bit;
311 nbits = 1;
312 } else if (xfs_buf_offset(bp, next_bit << XFS_BLI_SHIFT) !=
313 (xfs_buf_offset(bp, last_bit << XFS_BLI_SHIFT) +
314 XFS_BLI_CHUNK)) {
315 buffer_offset = first_bit * XFS_BLI_CHUNK;
316 vecp->i_addr = xfs_buf_offset(bp, buffer_offset);
317 vecp->i_len = nbits * XFS_BLI_CHUNK;
318 XLOG_VEC_SET_TYPE(vecp, XLOG_REG_TYPE_BCHUNK);
319 /* You would think we need to bump the nvecs here too, but we do not
320 * this number is used by recovery, and it gets confused by the boundary
321 * split here
322 * nvecs++;
324 vecp++;
325 first_bit = next_bit;
326 last_bit = next_bit;
327 nbits = 1;
328 } else {
329 last_bit++;
330 nbits++;
333 bip->bli_format.blf_size = nvecs;
336 * Check to make sure everything is consistent.
338 xfs_buf_item_trace("FORMAT NORM", bip);
339 xfs_buf_item_log_check(bip);
343 * This is called to pin the buffer associated with the buf log
344 * item in memory so it cannot be written out. Simply call bpin()
345 * on the buffer to do this.
347 STATIC void
348 xfs_buf_item_pin(
349 xfs_buf_log_item_t *bip)
351 xfs_buf_t *bp;
353 bp = bip->bli_buf;
354 ASSERT(XFS_BUF_ISBUSY(bp));
355 ASSERT(atomic_read(&bip->bli_refcount) > 0);
356 ASSERT((bip->bli_flags & XFS_BLI_LOGGED) ||
357 (bip->bli_flags & XFS_BLI_STALE));
358 xfs_buf_item_trace("PIN", bip);
359 xfs_buftrace("XFS_PIN", bp);
360 xfs_bpin(bp);
365 * This is called to unpin the buffer associated with the buf log
366 * item which was previously pinned with a call to xfs_buf_item_pin().
367 * Just call bunpin() on the buffer to do this.
369 * Also drop the reference to the buf item for the current transaction.
370 * If the XFS_BLI_STALE flag is set and we are the last reference,
371 * then free up the buf log item and unlock the buffer.
373 STATIC void
374 xfs_buf_item_unpin(
375 xfs_buf_log_item_t *bip,
376 int stale)
378 xfs_mount_t *mp;
379 xfs_buf_t *bp;
380 int freed;
381 SPLDECL(s);
383 bp = bip->bli_buf;
384 ASSERT(bp != NULL);
385 ASSERT(XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t *) == bip);
386 ASSERT(atomic_read(&bip->bli_refcount) > 0);
387 xfs_buf_item_trace("UNPIN", bip);
388 xfs_buftrace("XFS_UNPIN", bp);
390 freed = atomic_dec_and_test(&bip->bli_refcount);
391 mp = bip->bli_item.li_mountp;
392 xfs_bunpin(bp);
393 if (freed && stale) {
394 ASSERT(bip->bli_flags & XFS_BLI_STALE);
395 ASSERT(XFS_BUF_VALUSEMA(bp) <= 0);
396 ASSERT(!(XFS_BUF_ISDELAYWRITE(bp)));
397 ASSERT(XFS_BUF_ISSTALE(bp));
398 ASSERT(bip->bli_format.blf_flags & XFS_BLI_CANCEL);
399 xfs_buf_item_trace("UNPIN STALE", bip);
400 xfs_buftrace("XFS_UNPIN STALE", bp);
402 * If we get called here because of an IO error, we may
403 * or may not have the item on the AIL. xfs_trans_delete_ail()
404 * will take care of that situation.
405 * xfs_trans_delete_ail() drops the AIL lock.
407 if (bip->bli_flags & XFS_BLI_STALE_INODE) {
408 xfs_buf_do_callbacks(bp, (xfs_log_item_t *)bip);
409 XFS_BUF_SET_FSPRIVATE(bp, NULL);
410 XFS_BUF_CLR_IODONE_FUNC(bp);
411 } else {
412 AIL_LOCK(mp,s);
413 xfs_trans_delete_ail(mp, (xfs_log_item_t *)bip, s);
414 xfs_buf_item_relse(bp);
415 ASSERT(XFS_BUF_FSPRIVATE(bp, void *) == NULL);
417 xfs_buf_relse(bp);
422 * this is called from uncommit in the forced-shutdown path.
423 * we need to check to see if the reference count on the log item
424 * is going to drop to zero. If so, unpin will free the log item
425 * so we need to free the item's descriptor (that points to the item)
426 * in the transaction.
428 STATIC void
429 xfs_buf_item_unpin_remove(
430 xfs_buf_log_item_t *bip,
431 xfs_trans_t *tp)
433 xfs_buf_t *bp;
434 xfs_log_item_desc_t *lidp;
435 int stale = 0;
437 bp = bip->bli_buf;
439 * will xfs_buf_item_unpin() call xfs_buf_item_relse()?
441 if ((atomic_read(&bip->bli_refcount) == 1) &&
442 (bip->bli_flags & XFS_BLI_STALE)) {
443 ASSERT(XFS_BUF_VALUSEMA(bip->bli_buf) <= 0);
444 xfs_buf_item_trace("UNPIN REMOVE", bip);
445 xfs_buftrace("XFS_UNPIN_REMOVE", bp);
447 * yes -- clear the xaction descriptor in-use flag
448 * and free the chunk if required. We can safely
449 * do some work here and then call buf_item_unpin
450 * to do the rest because if the if is true, then
451 * we are holding the buffer locked so no one else
452 * will be able to bump up the refcount.
454 lidp = xfs_trans_find_item(tp, (xfs_log_item_t *) bip);
455 stale = lidp->lid_flags & XFS_LID_BUF_STALE;
456 xfs_trans_free_item(tp, lidp);
458 * Since the transaction no longer refers to the buffer,
459 * the buffer should no longer refer to the transaction.
461 XFS_BUF_SET_FSPRIVATE2(bp, NULL);
464 xfs_buf_item_unpin(bip, stale);
466 return;
470 * This is called to attempt to lock the buffer associated with this
471 * buf log item. Don't sleep on the buffer lock. If we can't get
472 * the lock right away, return 0. If we can get the lock, pull the
473 * buffer from the free list, mark it busy, and return 1.
475 STATIC uint
476 xfs_buf_item_trylock(
477 xfs_buf_log_item_t *bip)
479 xfs_buf_t *bp;
481 bp = bip->bli_buf;
483 if (XFS_BUF_ISPINNED(bp)) {
484 return XFS_ITEM_PINNED;
487 if (!XFS_BUF_CPSEMA(bp)) {
488 return XFS_ITEM_LOCKED;
492 * Remove the buffer from the free list. Only do this
493 * if it's on the free list. Private buffers like the
494 * superblock buffer are not.
496 XFS_BUF_HOLD(bp);
498 ASSERT(!(bip->bli_flags & XFS_BLI_STALE));
499 xfs_buf_item_trace("TRYLOCK SUCCESS", bip);
500 return XFS_ITEM_SUCCESS;
504 * Release the buffer associated with the buf log item.
505 * If there is no dirty logged data associated with the
506 * buffer recorded in the buf log item, then free the
507 * buf log item and remove the reference to it in the
508 * buffer.
510 * This call ignores the recursion count. It is only called
511 * when the buffer should REALLY be unlocked, regardless
512 * of the recursion count.
514 * If the XFS_BLI_HOLD flag is set in the buf log item, then
515 * free the log item if necessary but do not unlock the buffer.
516 * This is for support of xfs_trans_bhold(). Make sure the
517 * XFS_BLI_HOLD field is cleared if we don't free the item.
519 STATIC void
520 xfs_buf_item_unlock(
521 xfs_buf_log_item_t *bip)
523 int aborted;
524 xfs_buf_t *bp;
525 uint hold;
527 bp = bip->bli_buf;
528 xfs_buftrace("XFS_UNLOCK", bp);
531 * Clear the buffer's association with this transaction.
533 XFS_BUF_SET_FSPRIVATE2(bp, NULL);
536 * If this is a transaction abort, don't return early.
537 * Instead, allow the brelse to happen.
538 * Normally it would be done for stale (cancelled) buffers
539 * at unpin time, but we'll never go through the pin/unpin
540 * cycle if we abort inside commit.
542 aborted = (bip->bli_item.li_flags & XFS_LI_ABORTED) != 0;
545 * If the buf item is marked stale, then don't do anything.
546 * We'll unlock the buffer and free the buf item when the
547 * buffer is unpinned for the last time.
549 if (bip->bli_flags & XFS_BLI_STALE) {
550 bip->bli_flags &= ~XFS_BLI_LOGGED;
551 xfs_buf_item_trace("UNLOCK STALE", bip);
552 ASSERT(bip->bli_format.blf_flags & XFS_BLI_CANCEL);
553 if (!aborted)
554 return;
558 * Drop the transaction's reference to the log item if
559 * it was not logged as part of the transaction. Otherwise
560 * we'll drop the reference in xfs_buf_item_unpin() when
561 * the transaction is really through with the buffer.
563 if (!(bip->bli_flags & XFS_BLI_LOGGED)) {
564 atomic_dec(&bip->bli_refcount);
565 } else {
567 * Clear the logged flag since this is per
568 * transaction state.
570 bip->bli_flags &= ~XFS_BLI_LOGGED;
574 * Before possibly freeing the buf item, determine if we should
575 * release the buffer at the end of this routine.
577 hold = bip->bli_flags & XFS_BLI_HOLD;
578 xfs_buf_item_trace("UNLOCK", bip);
581 * If the buf item isn't tracking any data, free it.
582 * Otherwise, if XFS_BLI_HOLD is set clear it.
584 if (xfs_count_bits(bip->bli_format.blf_data_map,
585 bip->bli_format.blf_map_size, 0) == 0) {
586 xfs_buf_item_relse(bp);
587 } else if (hold) {
588 bip->bli_flags &= ~XFS_BLI_HOLD;
592 * Release the buffer if XFS_BLI_HOLD was not set.
594 if (!hold) {
595 xfs_buf_relse(bp);
600 * This is called to find out where the oldest active copy of the
601 * buf log item in the on disk log resides now that the last log
602 * write of it completed at the given lsn.
603 * We always re-log all the dirty data in a buffer, so usually the
604 * latest copy in the on disk log is the only one that matters. For
605 * those cases we simply return the given lsn.
607 * The one exception to this is for buffers full of newly allocated
608 * inodes. These buffers are only relogged with the XFS_BLI_INODE_BUF
609 * flag set, indicating that only the di_next_unlinked fields from the
610 * inodes in the buffers will be replayed during recovery. If the
611 * original newly allocated inode images have not yet been flushed
612 * when the buffer is so relogged, then we need to make sure that we
613 * keep the old images in the 'active' portion of the log. We do this
614 * by returning the original lsn of that transaction here rather than
615 * the current one.
617 STATIC xfs_lsn_t
618 xfs_buf_item_committed(
619 xfs_buf_log_item_t *bip,
620 xfs_lsn_t lsn)
622 xfs_buf_item_trace("COMMITTED", bip);
623 if ((bip->bli_flags & XFS_BLI_INODE_ALLOC_BUF) &&
624 (bip->bli_item.li_lsn != 0)) {
625 return bip->bli_item.li_lsn;
627 return (lsn);
631 * This is called when the transaction holding the buffer is aborted.
632 * Just behave as if the transaction had been cancelled. If we're shutting down
633 * and have aborted this transaction, we'll trap this buffer when it tries to
634 * get written out.
636 STATIC void
637 xfs_buf_item_abort(
638 xfs_buf_log_item_t *bip)
640 xfs_buf_t *bp;
642 bp = bip->bli_buf;
643 xfs_buftrace("XFS_ABORT", bp);
644 XFS_BUF_SUPER_STALE(bp);
645 xfs_buf_item_unlock(bip);
646 return;
650 * This is called to asynchronously write the buffer associated with this
651 * buf log item out to disk. The buffer will already have been locked by
652 * a successful call to xfs_buf_item_trylock(). If the buffer still has
653 * B_DELWRI set, then get it going out to disk with a call to bawrite().
654 * If not, then just release the buffer.
656 STATIC void
657 xfs_buf_item_push(
658 xfs_buf_log_item_t *bip)
660 xfs_buf_t *bp;
662 ASSERT(!(bip->bli_flags & XFS_BLI_STALE));
663 xfs_buf_item_trace("PUSH", bip);
665 bp = bip->bli_buf;
667 if (XFS_BUF_ISDELAYWRITE(bp)) {
668 xfs_bawrite(bip->bli_item.li_mountp, bp);
669 } else {
670 xfs_buf_relse(bp);
674 /* ARGSUSED */
675 STATIC void
676 xfs_buf_item_committing(xfs_buf_log_item_t *bip, xfs_lsn_t commit_lsn)
681 * This is the ops vector shared by all buf log items.
683 STATIC struct xfs_item_ops xfs_buf_item_ops = {
684 .iop_size = (uint(*)(xfs_log_item_t*))xfs_buf_item_size,
685 .iop_format = (void(*)(xfs_log_item_t*, xfs_log_iovec_t*))
686 xfs_buf_item_format,
687 .iop_pin = (void(*)(xfs_log_item_t*))xfs_buf_item_pin,
688 .iop_unpin = (void(*)(xfs_log_item_t*, int))xfs_buf_item_unpin,
689 .iop_unpin_remove = (void(*)(xfs_log_item_t*, xfs_trans_t *))
690 xfs_buf_item_unpin_remove,
691 .iop_trylock = (uint(*)(xfs_log_item_t*))xfs_buf_item_trylock,
692 .iop_unlock = (void(*)(xfs_log_item_t*))xfs_buf_item_unlock,
693 .iop_committed = (xfs_lsn_t(*)(xfs_log_item_t*, xfs_lsn_t))
694 xfs_buf_item_committed,
695 .iop_push = (void(*)(xfs_log_item_t*))xfs_buf_item_push,
696 .iop_abort = (void(*)(xfs_log_item_t*))xfs_buf_item_abort,
697 .iop_pushbuf = NULL,
698 .iop_committing = (void(*)(xfs_log_item_t*, xfs_lsn_t))
699 xfs_buf_item_committing
704 * Allocate a new buf log item to go with the given buffer.
705 * Set the buffer's b_fsprivate field to point to the new
706 * buf log item. If there are other item's attached to the
707 * buffer (see xfs_buf_attach_iodone() below), then put the
708 * buf log item at the front.
710 void
711 xfs_buf_item_init(
712 xfs_buf_t *bp,
713 xfs_mount_t *mp)
715 xfs_log_item_t *lip;
716 xfs_buf_log_item_t *bip;
717 int chunks;
718 int map_size;
721 * Check to see if there is already a buf log item for
722 * this buffer. If there is, it is guaranteed to be
723 * the first. If we do already have one, there is
724 * nothing to do here so return.
726 if (XFS_BUF_FSPRIVATE3(bp, xfs_mount_t *) != mp)
727 XFS_BUF_SET_FSPRIVATE3(bp, mp);
728 XFS_BUF_SET_BDSTRAT_FUNC(bp, xfs_bdstrat_cb);
729 if (XFS_BUF_FSPRIVATE(bp, void *) != NULL) {
730 lip = XFS_BUF_FSPRIVATE(bp, xfs_log_item_t *);
731 if (lip->li_type == XFS_LI_BUF) {
732 return;
737 * chunks is the number of XFS_BLI_CHUNK size pieces
738 * the buffer can be divided into. Make sure not to
739 * truncate any pieces. map_size is the size of the
740 * bitmap needed to describe the chunks of the buffer.
742 chunks = (int)((XFS_BUF_COUNT(bp) + (XFS_BLI_CHUNK - 1)) >> XFS_BLI_SHIFT);
743 map_size = (int)((chunks + NBWORD) >> BIT_TO_WORD_SHIFT);
745 bip = (xfs_buf_log_item_t*)kmem_zone_zalloc(xfs_buf_item_zone,
746 KM_SLEEP);
747 bip->bli_item.li_type = XFS_LI_BUF;
748 bip->bli_item.li_ops = &xfs_buf_item_ops;
749 bip->bli_item.li_mountp = mp;
750 bip->bli_buf = bp;
751 bip->bli_format.blf_type = XFS_LI_BUF;
752 bip->bli_format.blf_blkno = (__int64_t)XFS_BUF_ADDR(bp);
753 bip->bli_format.blf_len = (ushort)BTOBB(XFS_BUF_COUNT(bp));
754 bip->bli_format.blf_map_size = map_size;
755 #ifdef XFS_BLI_TRACE
756 bip->bli_trace = ktrace_alloc(XFS_BLI_TRACE_SIZE, KM_SLEEP);
757 #endif
759 #ifdef XFS_TRANS_DEBUG
761 * Allocate the arrays for tracking what needs to be logged
762 * and what our callers request to be logged. bli_orig
763 * holds a copy of the original, clean buffer for comparison
764 * against, and bli_logged keeps a 1 bit flag per byte in
765 * the buffer to indicate which bytes the callers have asked
766 * to have logged.
768 bip->bli_orig = (char *)kmem_alloc(XFS_BUF_COUNT(bp), KM_SLEEP);
769 memcpy(bip->bli_orig, XFS_BUF_PTR(bp), XFS_BUF_COUNT(bp));
770 bip->bli_logged = (char *)kmem_zalloc(XFS_BUF_COUNT(bp) / NBBY, KM_SLEEP);
771 #endif
774 * Put the buf item into the list of items attached to the
775 * buffer at the front.
777 if (XFS_BUF_FSPRIVATE(bp, void *) != NULL) {
778 bip->bli_item.li_bio_list =
779 XFS_BUF_FSPRIVATE(bp, xfs_log_item_t *);
781 XFS_BUF_SET_FSPRIVATE(bp, bip);
786 * Mark bytes first through last inclusive as dirty in the buf
787 * item's bitmap.
789 void
790 xfs_buf_item_log(
791 xfs_buf_log_item_t *bip,
792 uint first,
793 uint last)
795 uint first_bit;
796 uint last_bit;
797 uint bits_to_set;
798 uint bits_set;
799 uint word_num;
800 uint *wordp;
801 uint bit;
802 uint end_bit;
803 uint mask;
806 * Mark the item as having some dirty data for
807 * quick reference in xfs_buf_item_dirty.
809 bip->bli_flags |= XFS_BLI_DIRTY;
812 * Convert byte offsets to bit numbers.
814 first_bit = first >> XFS_BLI_SHIFT;
815 last_bit = last >> XFS_BLI_SHIFT;
818 * Calculate the total number of bits to be set.
820 bits_to_set = last_bit - first_bit + 1;
823 * Get a pointer to the first word in the bitmap
824 * to set a bit in.
826 word_num = first_bit >> BIT_TO_WORD_SHIFT;
827 wordp = &(bip->bli_format.blf_data_map[word_num]);
830 * Calculate the starting bit in the first word.
832 bit = first_bit & (uint)(NBWORD - 1);
835 * First set any bits in the first word of our range.
836 * If it starts at bit 0 of the word, it will be
837 * set below rather than here. That is what the variable
838 * bit tells us. The variable bits_set tracks the number
839 * of bits that have been set so far. End_bit is the number
840 * of the last bit to be set in this word plus one.
842 if (bit) {
843 end_bit = MIN(bit + bits_to_set, (uint)NBWORD);
844 mask = ((1 << (end_bit - bit)) - 1) << bit;
845 *wordp |= mask;
846 wordp++;
847 bits_set = end_bit - bit;
848 } else {
849 bits_set = 0;
853 * Now set bits a whole word at a time that are between
854 * first_bit and last_bit.
856 while ((bits_to_set - bits_set) >= NBWORD) {
857 *wordp |= 0xffffffff;
858 bits_set += NBWORD;
859 wordp++;
863 * Finally, set any bits left to be set in one last partial word.
865 end_bit = bits_to_set - bits_set;
866 if (end_bit) {
867 mask = (1 << end_bit) - 1;
868 *wordp |= mask;
871 xfs_buf_item_log_debug(bip, first, last);
876 * Return 1 if the buffer has some data that has been logged (at any
877 * point, not just the current transaction) and 0 if not.
879 uint
880 xfs_buf_item_dirty(
881 xfs_buf_log_item_t *bip)
883 return (bip->bli_flags & XFS_BLI_DIRTY);
887 * This is called when the buf log item is no longer needed. It should
888 * free the buf log item associated with the given buffer and clear
889 * the buffer's pointer to the buf log item. If there are no more
890 * items in the list, clear the b_iodone field of the buffer (see
891 * xfs_buf_attach_iodone() below).
893 void
894 xfs_buf_item_relse(
895 xfs_buf_t *bp)
897 xfs_buf_log_item_t *bip;
899 xfs_buftrace("XFS_RELSE", bp);
900 bip = XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t*);
901 XFS_BUF_SET_FSPRIVATE(bp, bip->bli_item.li_bio_list);
902 if ((XFS_BUF_FSPRIVATE(bp, void *) == NULL) &&
903 (XFS_BUF_IODONE_FUNC(bp) != NULL)) {
904 ASSERT((XFS_BUF_ISUNINITIAL(bp)) == 0);
905 XFS_BUF_CLR_IODONE_FUNC(bp);
908 #ifdef XFS_TRANS_DEBUG
909 kmem_free(bip->bli_orig, XFS_BUF_COUNT(bp));
910 bip->bli_orig = NULL;
911 kmem_free(bip->bli_logged, XFS_BUF_COUNT(bp) / NBBY);
912 bip->bli_logged = NULL;
913 #endif /* XFS_TRANS_DEBUG */
915 #ifdef XFS_BLI_TRACE
916 ktrace_free(bip->bli_trace);
917 #endif
918 kmem_zone_free(xfs_buf_item_zone, bip);
923 * Add the given log item with its callback to the list of callbacks
924 * to be called when the buffer's I/O completes. If it is not set
925 * already, set the buffer's b_iodone() routine to be
926 * xfs_buf_iodone_callbacks() and link the log item into the list of
927 * items rooted at b_fsprivate. Items are always added as the second
928 * entry in the list if there is a first, because the buf item code
929 * assumes that the buf log item is first.
931 void
932 xfs_buf_attach_iodone(
933 xfs_buf_t *bp,
934 void (*cb)(xfs_buf_t *, xfs_log_item_t *),
935 xfs_log_item_t *lip)
937 xfs_log_item_t *head_lip;
939 ASSERT(XFS_BUF_ISBUSY(bp));
940 ASSERT(XFS_BUF_VALUSEMA(bp) <= 0);
942 lip->li_cb = cb;
943 if (XFS_BUF_FSPRIVATE(bp, void *) != NULL) {
944 head_lip = XFS_BUF_FSPRIVATE(bp, xfs_log_item_t *);
945 lip->li_bio_list = head_lip->li_bio_list;
946 head_lip->li_bio_list = lip;
947 } else {
948 XFS_BUF_SET_FSPRIVATE(bp, lip);
951 ASSERT((XFS_BUF_IODONE_FUNC(bp) == xfs_buf_iodone_callbacks) ||
952 (XFS_BUF_IODONE_FUNC(bp) == NULL));
953 XFS_BUF_SET_IODONE_FUNC(bp, xfs_buf_iodone_callbacks);
956 STATIC void
957 xfs_buf_do_callbacks(
958 xfs_buf_t *bp,
959 xfs_log_item_t *lip)
961 xfs_log_item_t *nlip;
963 while (lip != NULL) {
964 nlip = lip->li_bio_list;
965 ASSERT(lip->li_cb != NULL);
967 * Clear the next pointer so we don't have any
968 * confusion if the item is added to another buf.
969 * Don't touch the log item after calling its
970 * callback, because it could have freed itself.
972 lip->li_bio_list = NULL;
973 lip->li_cb(bp, lip);
974 lip = nlip;
979 * This is the iodone() function for buffers which have had callbacks
980 * attached to them by xfs_buf_attach_iodone(). It should remove each
981 * log item from the buffer's list and call the callback of each in turn.
982 * When done, the buffer's fsprivate field is set to NULL and the buffer
983 * is unlocked with a call to iodone().
985 void
986 xfs_buf_iodone_callbacks(
987 xfs_buf_t *bp)
989 xfs_log_item_t *lip;
990 static ulong lasttime;
991 static xfs_buftarg_t *lasttarg;
992 xfs_mount_t *mp;
994 ASSERT(XFS_BUF_FSPRIVATE(bp, void *) != NULL);
995 lip = XFS_BUF_FSPRIVATE(bp, xfs_log_item_t *);
997 if (XFS_BUF_GETERROR(bp) != 0) {
999 * If we've already decided to shutdown the filesystem
1000 * because of IO errors, there's no point in giving this
1001 * a retry.
1003 mp = lip->li_mountp;
1004 if (XFS_FORCED_SHUTDOWN(mp)) {
1005 ASSERT(XFS_BUF_TARGET(bp) == mp->m_ddev_targp);
1006 XFS_BUF_SUPER_STALE(bp);
1007 xfs_buftrace("BUF_IODONE_CB", bp);
1008 xfs_buf_do_callbacks(bp, lip);
1009 XFS_BUF_SET_FSPRIVATE(bp, NULL);
1010 XFS_BUF_CLR_IODONE_FUNC(bp);
1013 * XFS_SHUT flag gets set when we go thru the
1014 * entire buffer cache and deliberately start
1015 * throwing away delayed write buffers.
1016 * Since there's no biowait done on those,
1017 * we should just brelse them.
1019 if (XFS_BUF_ISSHUT(bp)) {
1020 XFS_BUF_UNSHUT(bp);
1021 xfs_buf_relse(bp);
1022 } else {
1023 xfs_biodone(bp);
1026 return;
1029 if ((XFS_BUF_TARGET(bp) != lasttarg) ||
1030 (time_after(jiffies, (lasttime + 5*HZ)))) {
1031 lasttime = jiffies;
1032 cmn_err(CE_ALERT, "Device %s, XFS metadata write error"
1033 " block 0x%llx in %s",
1034 XFS_BUFTARG_NAME(XFS_BUF_TARGET(bp)),
1035 (__uint64_t)XFS_BUF_ADDR(bp), mp->m_fsname);
1037 lasttarg = XFS_BUF_TARGET(bp);
1039 if (XFS_BUF_ISASYNC(bp)) {
1041 * If the write was asynchronous then noone will be
1042 * looking for the error. Clear the error state
1043 * and write the buffer out again delayed write.
1045 * XXXsup This is OK, so long as we catch these
1046 * before we start the umount; we don't want these
1047 * DELWRI metadata bufs to be hanging around.
1049 XFS_BUF_ERROR(bp,0); /* errno of 0 unsets the flag */
1051 if (!(XFS_BUF_ISSTALE(bp))) {
1052 XFS_BUF_DELAYWRITE(bp);
1053 XFS_BUF_DONE(bp);
1054 XFS_BUF_SET_START(bp);
1056 ASSERT(XFS_BUF_IODONE_FUNC(bp));
1057 xfs_buftrace("BUF_IODONE ASYNC", bp);
1058 xfs_buf_relse(bp);
1059 } else {
1061 * If the write of the buffer was not asynchronous,
1062 * then we want to make sure to return the error
1063 * to the caller of bwrite(). Because of this we
1064 * cannot clear the B_ERROR state at this point.
1065 * Instead we install a callback function that
1066 * will be called when the buffer is released, and
1067 * that routine will clear the error state and
1068 * set the buffer to be written out again after
1069 * some delay.
1071 /* We actually overwrite the existing b-relse
1072 function at times, but we're gonna be shutting down
1073 anyway. */
1074 XFS_BUF_SET_BRELSE_FUNC(bp,xfs_buf_error_relse);
1075 XFS_BUF_DONE(bp);
1076 XFS_BUF_V_IODONESEMA(bp);
1078 return;
1080 #ifdef XFSERRORDEBUG
1081 xfs_buftrace("XFS BUFCB NOERR", bp);
1082 #endif
1083 xfs_buf_do_callbacks(bp, lip);
1084 XFS_BUF_SET_FSPRIVATE(bp, NULL);
1085 XFS_BUF_CLR_IODONE_FUNC(bp);
1086 xfs_biodone(bp);
1090 * This is a callback routine attached to a buffer which gets an error
1091 * when being written out synchronously.
1093 STATIC void
1094 xfs_buf_error_relse(
1095 xfs_buf_t *bp)
1097 xfs_log_item_t *lip;
1098 xfs_mount_t *mp;
1100 lip = XFS_BUF_FSPRIVATE(bp, xfs_log_item_t *);
1101 mp = (xfs_mount_t *)lip->li_mountp;
1102 ASSERT(XFS_BUF_TARGET(bp) == mp->m_ddev_targp);
1104 XFS_BUF_STALE(bp);
1105 XFS_BUF_DONE(bp);
1106 XFS_BUF_UNDELAYWRITE(bp);
1107 XFS_BUF_ERROR(bp,0);
1108 xfs_buftrace("BUF_ERROR_RELSE", bp);
1109 if (! XFS_FORCED_SHUTDOWN(mp))
1110 xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR);
1112 * We have to unpin the pinned buffers so do the
1113 * callbacks.
1115 xfs_buf_do_callbacks(bp, lip);
1116 XFS_BUF_SET_FSPRIVATE(bp, NULL);
1117 XFS_BUF_CLR_IODONE_FUNC(bp);
1118 XFS_BUF_SET_BRELSE_FUNC(bp,NULL);
1119 xfs_buf_relse(bp);
1124 * This is the iodone() function for buffers which have been
1125 * logged. It is called when they are eventually flushed out.
1126 * It should remove the buf item from the AIL, and free the buf item.
1127 * It is called by xfs_buf_iodone_callbacks() above which will take
1128 * care of cleaning up the buffer itself.
1130 /* ARGSUSED */
1131 void
1132 xfs_buf_iodone(
1133 xfs_buf_t *bp,
1134 xfs_buf_log_item_t *bip)
1136 struct xfs_mount *mp;
1137 SPLDECL(s);
1139 ASSERT(bip->bli_buf == bp);
1141 mp = bip->bli_item.li_mountp;
1144 * If we are forcibly shutting down, this may well be
1145 * off the AIL already. That's because we simulate the
1146 * log-committed callbacks to unpin these buffers. Or we may never
1147 * have put this item on AIL because of the transaction was
1148 * aborted forcibly. xfs_trans_delete_ail() takes care of these.
1150 * Either way, AIL is useless if we're forcing a shutdown.
1152 AIL_LOCK(mp,s);
1154 * xfs_trans_delete_ail() drops the AIL lock.
1156 xfs_trans_delete_ail(mp, (xfs_log_item_t *)bip, s);
1158 #ifdef XFS_TRANS_DEBUG
1159 kmem_free(bip->bli_orig, XFS_BUF_COUNT(bp));
1160 bip->bli_orig = NULL;
1161 kmem_free(bip->bli_logged, XFS_BUF_COUNT(bp) / NBBY);
1162 bip->bli_logged = NULL;
1163 #endif /* XFS_TRANS_DEBUG */
1165 #ifdef XFS_BLI_TRACE
1166 ktrace_free(bip->bli_trace);
1167 #endif
1168 kmem_zone_free(xfs_buf_item_zone, bip);
1171 #if defined(XFS_BLI_TRACE)
1172 void
1173 xfs_buf_item_trace(
1174 char *id,
1175 xfs_buf_log_item_t *bip)
1177 xfs_buf_t *bp;
1178 ASSERT(bip->bli_trace != NULL);
1180 bp = bip->bli_buf;
1181 ktrace_enter(bip->bli_trace,
1182 (void *)id,
1183 (void *)bip->bli_buf,
1184 (void *)((unsigned long)bip->bli_flags),
1185 (void *)((unsigned long)bip->bli_recur),
1186 (void *)((unsigned long)atomic_read(&bip->bli_refcount)),
1187 (void *)((unsigned long)
1188 (0xFFFFFFFF & XFS_BUF_ADDR(bp) >> 32)),
1189 (void *)((unsigned long)(0xFFFFFFFF & XFS_BUF_ADDR(bp))),
1190 (void *)((unsigned long)XFS_BUF_COUNT(bp)),
1191 (void *)((unsigned long)XFS_BUF_BFLAGS(bp)),
1192 XFS_BUF_FSPRIVATE(bp, void *),
1193 XFS_BUF_FSPRIVATE2(bp, void *),
1194 (void *)(unsigned long)XFS_BUF_ISPINNED(bp),
1195 (void *)XFS_BUF_IODONE_FUNC(bp),
1196 (void *)((unsigned long)(XFS_BUF_VALUSEMA(bp))),
1197 (void *)bip->bli_item.li_desc,
1198 (void *)((unsigned long)bip->bli_item.li_flags));
1200 #endif /* XFS_BLI_TRACE */