blob: 52cd8f89ee72b4ba403def680f2ac99585c48aee [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Nathan Scott7b718762005-11-02 14:58:39 +11002 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott7b718762005-11-02 14:58:39 +11005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott7b718762005-11-02 14:58:39 +11009 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott7b718762005-11-02 14:58:39 +110014 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110019#include "xfs_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include "xfs_types.h"
Nathan Scotta844f452005-11-02 14:38:42 +110021#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include "xfs_log.h"
23#include "xfs_trans.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include "xfs_sb.h"
David Chinnerda353b02007-08-28 14:00:13 +100025#include "xfs_ag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include "xfs_mount.h"
Nathan Scotta844f452005-11-02 14:38:42 +110027#include "xfs_buf_item.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include "xfs_trans_priv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include "xfs_error.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000030#include "xfs_trace.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
32
33kmem_zone_t *xfs_buf_item_zone;
34
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +100035static 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#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 */
51STATIC void
52xfs_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++) {
Dave Chinnerc1155412010-05-07 11:05:19 +100071 chunk_num = byte >> XFS_BLF_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 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 */
86void
87xfs_buf_item_flush_log_debug(
88 xfs_buf_t *bp,
89 uint first,
90 uint last)
91{
Christoph Hellwigadadbee2011-07-13 13:43:49 +020092 xfs_buf_log_item_t *bip = bp->b_fspriv;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 uint nbytes;
94
Christoph Hellwigadadbee2011-07-13 13:43:49 +020095 if (bip == NULL || (bip->bli_item.li_type != XFS_LI_BUF))
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
98 ASSERT(bip->bli_logged != NULL);
99 nbytes = last - first + 1;
100 bfset(bip->bli_logged, first, nbytes);
101}
102
103/*
Nathan Scottc41564b2006-03-29 08:55:14 +1000104 * This function is called to verify that our callers have logged
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 * 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
Nathan Scottc41564b2006-03-29 08:55:14 +1000109 * and ensuring that all bytes which mismatch are set in the bli_logged
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 * array of the buf log item.
111 */
112STATIC void
113xfs_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;
Dave Chinneraa0e8832012-04-23 15:58:52 +1000125 ASSERT(bp->b_length > 0);
Chandra Seetharaman62926042011-07-22 23:40:15 +0000126 ASSERT(bp->b_addr != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 orig = bip->bli_orig;
Chandra Seetharaman62926042011-07-22 23:40:15 +0000128 buffer = bp->b_addr;
Dave Chinneraa0e8832012-04-23 15:58:52 +1000129 for (x = 0; x < BBTOB(bp->b_length); x++) {
Dave Chinner0b932cc2011-03-07 10:08:35 +1100130 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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 }
137}
138#else
139#define xfs_buf_item_log_debug(x,y,z)
140#define xfs_buf_item_log_check(x)
141#endif
142
Dave Chinnerc90821a2010-12-03 17:00:52 +1100143STATIC void xfs_buf_do_callbacks(struct xfs_buf *bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
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 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +1000155STATIC uint
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156xfs_buf_item_size(
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000157 struct xfs_log_item *lip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158{
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000159 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
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 */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000172 trace_xfs_buf_item_size_stale(bip);
Dave Chinnerc1155412010-05-07 11:05:19 +1000173 ASSERT(bip->bli_format.blf_flags & XFS_BLF_CANCEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 return 1;
175 }
176
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 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++;
Dave Chinnerc1155412010-05-07 11:05:19 +1000203 } 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)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 last_bit = next_bit;
207 nvecs++;
208 } else {
209 last_bit++;
210 }
211 }
212
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000213 trace_xfs_buf_item_size(bip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 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 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +1000223STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224xfs_buf_item_format(
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000225 struct xfs_log_item *lip,
226 struct xfs_log_iovec *vecp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227{
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000228 struct xfs_buf_log_item *bip = BUF_ITEM(lip);
229 struct xfs_buf *bp = bip->bli_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 uint base_size;
231 uint nvecs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 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));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
242 /*
Dave Chinner77c1a082012-06-22 18:50:07 +1000243 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 */
Dave Chinner77c1a082012-06-22 18:50:07 +1000247 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]));
Christoph Hellwig4e0d5f92010-06-23 18:11:15 +1000250 vecp->i_addr = &bip->bli_format;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 vecp->i_len = base_size;
Christoph Hellwig4139b3b2010-01-19 09:56:45 +0000252 vecp->i_type = XLOG_REG_TYPE_BFORMAT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 vecp++;
254 nvecs = 1;
255
Dave Chinnerccf7c232010-05-20 23:19:42 +1000256 /*
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) &&
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000265 xfs_log_item_in_current_chkpt(lip)))
Dave Chinnerccf7c232010-05-20 23:19:42 +1000266 bip->bli_format.blf_flags |= XFS_BLF_INODE_BUF;
267 bip->bli_flags &= ~XFS_BLI_INODE_BUF;
268 }
269
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 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 */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000276 trace_xfs_buf_item_format_stale(bip);
Dave Chinnerc1155412010-05-07 11:05:19 +1000277 ASSERT(bip->bli_format.blf_flags & XFS_BLF_CANCEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 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) {
Dave Chinnerc1155412010-05-07 11:05:19 +1000310 buffer_offset = first_bit * XFS_BLF_CHUNK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 vecp->i_addr = xfs_buf_offset(bp, buffer_offset);
Dave Chinnerc1155412010-05-07 11:05:19 +1000312 vecp->i_len = nbits * XFS_BLF_CHUNK;
Christoph Hellwig4139b3b2010-01-19 09:56:45 +0000313 vecp->i_type = XLOG_REG_TYPE_BCHUNK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 nvecs++;
315 break;
316 } else if (next_bit != last_bit + 1) {
Dave Chinnerc1155412010-05-07 11:05:19 +1000317 buffer_offset = first_bit * XFS_BLF_CHUNK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 vecp->i_addr = xfs_buf_offset(bp, buffer_offset);
Dave Chinnerc1155412010-05-07 11:05:19 +1000319 vecp->i_len = nbits * XFS_BLF_CHUNK;
Christoph Hellwig4139b3b2010-01-19 09:56:45 +0000320 vecp->i_type = XLOG_REG_TYPE_BCHUNK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 nvecs++;
322 vecp++;
323 first_bit = next_bit;
324 last_bit = next_bit;
325 nbits = 1;
Dave Chinnerc1155412010-05-07 11:05:19 +1000326 } 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 vecp->i_addr = xfs_buf_offset(bp, buffer_offset);
Dave Chinnerc1155412010-05-07 11:05:19 +1000331 vecp->i_len = nbits * XFS_BLF_CHUNK;
Christoph Hellwig4139b3b2010-01-19 09:56:45 +0000332 vecp->i_type = XLOG_REG_TYPE_BCHUNK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333/* 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 */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000352 trace_xfs_buf_item_format(bip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 xfs_buf_item_log_check(bip);
354}
355
356/*
Dave Chinner64fc35d2010-05-07 11:04:34 +1000357 * This is called to pin the buffer associated with the buf log item in memory
Christoph Hellwig4d16e922010-06-23 18:11:15 +1000358 * so it cannot be written out.
Dave Chinner64fc35d2010-05-07 11:04:34 +1000359 *
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.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +1000365STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366xfs_buf_item_pin(
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000367 struct xfs_log_item *lip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368{
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000369 struct xfs_buf_log_item *bip = BUF_ITEM(lip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 ASSERT(atomic_read(&bip->bli_refcount) > 0);
372 ASSERT((bip->bli_flags & XFS_BLI_LOGGED) ||
373 (bip->bli_flags & XFS_BLI_STALE));
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000374
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000375 trace_xfs_buf_item_pin(bip);
Christoph Hellwig4d16e922010-06-23 18:11:15 +1000376
377 atomic_inc(&bip->bli_refcount);
378 atomic_inc(&bip->bli_buf->b_pin_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379}
380
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381/*
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().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 *
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.
Christoph Hellwig9412e312010-06-23 18:11:15 +1000388 *
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.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +1000394STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395xfs_buf_item_unpin(
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000396 struct xfs_log_item *lip,
Christoph Hellwig9412e312010-06-23 18:11:15 +1000397 int remove)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398{
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000399 struct xfs_buf_log_item *bip = BUF_ITEM(lip);
Christoph Hellwig9412e312010-06-23 18:11:15 +1000400 xfs_buf_t *bp = bip->bli_buf;
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000401 struct xfs_ail *ailp = lip->li_ailp;
Dave Chinner8e123852010-03-08 11:26:03 +1100402 int stale = bip->bli_flags & XFS_BLI_STALE;
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000403 int freed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
Christoph Hellwigadadbee2011-07-13 13:43:49 +0200405 ASSERT(bp->b_fspriv == bip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 ASSERT(atomic_read(&bip->bli_refcount) > 0);
Christoph Hellwig9412e312010-06-23 18:11:15 +1000407
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000408 trace_xfs_buf_item_unpin(bip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
410 freed = atomic_dec_and_test(&bip->bli_refcount);
Christoph Hellwig4d16e922010-06-23 18:11:15 +1000411
412 if (atomic_dec_and_test(&bp->b_pin_count))
413 wake_up_all(&bp->b_waiters);
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000414
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 if (freed && stale) {
416 ASSERT(bip->bli_flags & XFS_BLI_STALE);
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200417 ASSERT(xfs_buf_islocked(bp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 ASSERT(XFS_BUF_ISSTALE(bp));
Dave Chinnerc1155412010-05-07 11:05:19 +1000419 ASSERT(bip->bli_format.blf_flags & XFS_BLF_CANCEL);
Christoph Hellwig9412e312010-06-23 18:11:15 +1000420
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000421 trace_xfs_buf_item_unpin_stale(bip);
422
Christoph Hellwig9412e312010-06-23 18:11:15 +1000423 if (remove) {
424 /*
Dave Chinnere34a3142011-01-27 12:13:35 +1100425 * 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
Christoph Hellwig9412e312010-06-23 18:11:15 +1000430 * buffer which we no longer have a hold on.
431 */
Dave Chinnere34a3142011-01-27 12:13:35 +1100432 if (lip->li_desc)
433 xfs_trans_del_item(lip);
Christoph Hellwig9412e312010-06-23 18:11:15 +1000434
435 /*
436 * Since the transaction no longer refers to the buffer,
437 * the buffer should no longer refer to the transaction.
438 */
Christoph Hellwigbf9d9012011-07-13 13:43:49 +0200439 bp->b_transp = NULL;
Christoph Hellwig9412e312010-06-23 18:11:15 +1000440 }
441
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 /*
443 * If we get called here because of an IO error, we may
David Chinner783a2f62008-10-30 17:39:58 +1100444 * or may not have the item on the AIL. xfs_trans_ail_delete()
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 * will take care of that situation.
David Chinner783a2f62008-10-30 17:39:58 +1100446 * xfs_trans_ail_delete() drops the AIL lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 */
448 if (bip->bli_flags & XFS_BLI_STALE_INODE) {
Dave Chinnerc90821a2010-12-03 17:00:52 +1100449 xfs_buf_do_callbacks(bp);
Christoph Hellwigadadbee2011-07-13 13:43:49 +0200450 bp->b_fspriv = NULL;
Christoph Hellwigcb669ca2011-07-13 13:43:49 +0200451 bp->b_iodone = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 } else {
David Chinner783a2f62008-10-30 17:39:58 +1100453 spin_lock(&ailp->xa_lock);
Dave Chinner04913fd2012-04-23 15:58:41 +1000454 xfs_trans_ail_delete(ailp, lip, SHUTDOWN_LOG_IO_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 xfs_buf_item_relse(bp);
Christoph Hellwigadadbee2011-07-13 13:43:49 +0200456 ASSERT(bp->b_fspriv == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 }
458 xfs_buf_relse(bp);
Christoph Hellwig960c60a2012-04-23 15:58:38 +1000459 } 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 }
466}
467
Christoph Hellwigba0f32d2005-06-21 15:36:52 +1000468STATIC uint
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000469xfs_buf_item_push(
470 struct xfs_log_item *lip,
471 struct list_head *buffer_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472{
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000473 struct xfs_buf_log_item *bip = BUF_ITEM(lip);
474 struct xfs_buf *bp = bip->bli_buf;
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000475 uint rval = XFS_ITEM_SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
Chandra Seetharaman811e64c2011-07-22 23:40:27 +0000477 if (xfs_buf_ispinned(bp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 return XFS_ITEM_PINNED;
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200479 if (!xfs_buf_trylock(bp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 return XFS_ITEM_LOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 ASSERT(!(bip->bli_flags & XFS_BLI_STALE));
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000483
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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490}
491
492/*
Dave Chinner64fc35d2010-05-07 11:04:34 +1000493 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 *
Dave Chinner64fc35d2010-05-07 11:04:34 +1000497 * This call ignores the recursion count. It is only called when the buffer
498 * should REALLY be unlocked, regardless of the recursion count.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 *
Dave Chinner64fc35d2010-05-07 11:04:34 +1000500 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +1000511STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512xfs_buf_item_unlock(
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000513 struct xfs_log_item *lip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514{
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000515 struct xfs_buf_log_item *bip = BUF_ITEM(lip);
516 struct xfs_buf *bp = bip->bli_buf;
517 int aborted;
518 uint hold;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
Dave Chinner64fc35d2010-05-07 11:04:34 +1000520 /* Clear the buffer's association with this transaction. */
Christoph Hellwigbf9d9012011-07-13 13:43:49 +0200521 bp->b_transp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
523 /*
Dave Chinner64fc35d2010-05-07 11:04:34 +1000524 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 */
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000529 aborted = (lip->li_flags & XFS_LI_ABORTED) != 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
531 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 * 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;
Dave Chinner64fc35d2010-05-07 11:04:34 +1000536
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);
Dave Chinnerc1155412010-05-07 11:05:19 +1000547 ASSERT(bip->bli_format.blf_flags & XFS_BLF_CANCEL);
Dave Chinner64fc35d2010-05-07 11:04:34 +1000548 if (!aborted) {
549 atomic_dec(&bip->bli_refcount);
550 return;
551 }
552 }
553
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000554 trace_xfs_buf_item_unlock(bip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
556 /*
Dave Chinner64fc35d2010-05-07 11:04:34 +1000557 * If the buf item isn't tracking any data, free it, otherwise drop the
558 * reference we hold to it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 */
Eric Sandeen24ad33f2007-06-28 16:43:30 +1000560 if (xfs_bitmap_empty(bip->bli_format.blf_data_map,
Dave Chinner64fc35d2010-05-07 11:04:34 +1000561 bip->bli_format.blf_map_size))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 xfs_buf_item_relse(bp);
Dave Chinner64fc35d2010-05-07 11:04:34 +1000563 else
564 atomic_dec(&bip->bli_refcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
Dave Chinner64fc35d2010-05-07 11:04:34 +1000566 if (!hold)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 xfs_buf_relse(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568}
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 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +1000588STATIC xfs_lsn_t
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589xfs_buf_item_committed(
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000590 struct xfs_log_item *lip,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 xfs_lsn_t lsn)
592{
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000593 struct xfs_buf_log_item *bip = BUF_ITEM(lip);
594
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000595 trace_xfs_buf_item_committed(bip);
596
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000597 if ((bip->bli_flags & XFS_BLI_INODE_ALLOC_BUF) && lip->li_lsn != 0)
598 return lip->li_lsn;
599 return lsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600}
601
Christoph Hellwigba0f32d2005-06-21 15:36:52 +1000602STATIC void
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000603xfs_buf_item_committing(
604 struct xfs_log_item *lip,
605 xfs_lsn_t commit_lsn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606{
607}
608
609/*
610 * This is the ops vector shared by all buf log items.
611 */
Christoph Hellwig272e42b2011-10-28 09:54:24 +0000612static const struct xfs_item_ops xfs_buf_item_ops = {
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000613 .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,
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000617 .iop_unlock = xfs_buf_item_unlock,
618 .iop_committed = xfs_buf_item_committed,
619 .iop_push = xfs_buf_item_push,
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000620 .iop_committing = xfs_buf_item_committing
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621};
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 */
631void
632xfs_buf_item_init(
633 xfs_buf_t *bp,
634 xfs_mount_t *mp)
635{
Christoph Hellwigadadbee2011-07-13 13:43:49 +0200636 xfs_log_item_t *lip = bp->b_fspriv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 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 */
Dave Chinnerebad8612010-09-22 10:47:20 +1000647 ASSERT(bp->b_target->bt_mount == mp);
Christoph Hellwigadadbee2011-07-13 13:43:49 +0200648 if (lip != NULL && lip->li_type == XFS_LI_BUF)
649 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
651 /*
Dave Chinnerc1155412010-05-07 11:05:19 +1000652 * chunks is the number of XFS_BLF_CHUNK size pieces
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 * 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 */
Dave Chinneraa0e8832012-04-23 15:58:52 +1000657 chunks = (int)((BBTOB(bp->b_length) + (XFS_BLF_CHUNK - 1)) >>
658 XFS_BLF_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 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);
Dave Chinner43f5efc2010-03-23 10:10:00 +1100663 xfs_log_item_init(mp, &bip->bli_item, XFS_LI_BUF, &xfs_buf_item_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 bip->bli_buf = bp;
Lachlan McIlroye1f5dbd2008-09-17 16:52:13 +1000665 xfs_buf_hold(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 bip->bli_format.blf_type = XFS_LI_BUF;
667 bip->bli_format.blf_blkno = (__int64_t)XFS_BUF_ADDR(bp);
Dave Chinneraa0e8832012-04-23 15:58:52 +1000668 bip->bli_format.blf_len = (ushort)bp->b_length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 bip->bli_format.blf_map_size = map_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
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 */
Dave Chinneraa0e8832012-04-23 15:58:52 +1000680 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683#endif
684
685 /*
686 * Put the buf item into the list of items attached to the
687 * buffer at the front.
688 */
Christoph Hellwigadadbee2011-07-13 13:43:49 +0200689 if (bp->b_fspriv)
690 bip->bli_item.li_bio_list = bp->b_fspriv;
691 bp->b_fspriv = bip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692}
693
694
695/*
696 * Mark bytes first through last inclusive as dirty in the buf
697 * item's bitmap.
698 */
699void
700xfs_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 */
Dave Chinnerc1155412010-05-07 11:05:19 +1000724 first_bit = first >> XFS_BLF_SHIFT;
725 last_bit = last >> XFS_BLF_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
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 */
789uint
790xfs_buf_item_dirty(
791 xfs_buf_log_item_t *bip)
792{
793 return (bip->bli_flags & XFS_BLI_DIRTY);
794}
795
Lachlan McIlroye1f5dbd2008-09-17 16:52:13 +1000796STATIC void
797xfs_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
Lachlan McIlroye1f5dbd2008-09-17 16:52:13 +1000805 kmem_zone_free(xfs_buf_item_zone, bip);
806}
807
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808/*
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 */
815void
816xfs_buf_item_relse(
817 xfs_buf_t *bp)
818{
819 xfs_buf_log_item_t *bip;
820
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000821 trace_xfs_buf_item_relse(bp, _RET_IP_);
822
Christoph Hellwigadadbee2011-07-13 13:43:49 +0200823 bip = bp->b_fspriv;
824 bp->b_fspriv = bip->bli_item.li_bio_list;
Christoph Hellwigcb669ca2011-07-13 13:43:49 +0200825 if (bp->b_fspriv == NULL)
826 bp->b_iodone = NULL;
Christoph Hellwigadadbee2011-07-13 13:43:49 +0200827
Lachlan McIlroye1f5dbd2008-09-17 16:52:13 +1000828 xfs_buf_rele(bp);
829 xfs_buf_item_free(bip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830}
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 */
842void
843xfs_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
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200850 ASSERT(xfs_buf_islocked(bp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
852 lip->li_cb = cb;
Christoph Hellwigadadbee2011-07-13 13:43:49 +0200853 head_lip = bp->b_fspriv;
854 if (head_lip) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 lip->li_bio_list = head_lip->li_bio_list;
856 head_lip->li_bio_list = lip;
857 } else {
Christoph Hellwigadadbee2011-07-13 13:43:49 +0200858 bp->b_fspriv = lip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 }
860
Christoph Hellwigcb669ca2011-07-13 13:43:49 +0200861 ASSERT(bp->b_iodone == NULL ||
862 bp->b_iodone == xfs_buf_iodone_callbacks);
863 bp->b_iodone = xfs_buf_iodone_callbacks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864}
865
Dave Chinnerc90821a2010-12-03 17:00:52 +1100866/*
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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878STATIC void
879xfs_buf_do_callbacks(
Dave Chinnerc90821a2010-12-03 17:00:52 +1100880 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881{
Dave Chinnerc90821a2010-12-03 17:00:52 +1100882 struct xfs_log_item *lip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883
Christoph Hellwigadadbee2011-07-13 13:43:49 +0200884 while ((lip = bp->b_fspriv) != NULL) {
885 bp->b_fspriv = lip->li_bio_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 }
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 */
905void
906xfs_buf_iodone_callbacks(
Christoph Hellwigbfc60172011-01-07 13:02:23 +0000907 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908{
Christoph Hellwigbfc60172011-01-07 13:02:23 +0000909 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
Chandra Seetharaman5a52c2a582011-07-22 23:39:51 +0000914 if (likely(!xfs_buf_geterror(bp)))
Christoph Hellwigbfc60172011-01-07 13:02:23 +0000915 goto do_callbacks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916
Christoph Hellwigbfc60172011-01-07 13:02:23 +0000917 /*
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)) {
Christoph Hellwigc867cb62011-10-10 16:52:46 +0000922 xfs_buf_stale(bp);
Christoph Hellwigc867cb62011-10-10 16:52:46 +0000923 XFS_BUF_DONE(bp);
Christoph Hellwigbfc60172011-01-07 13:02:23 +0000924 trace_xfs_buf_item_iodone(bp, _RET_IP_);
925 goto do_callbacks;
926 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
Chandra Seetharaman49074c02011-07-22 23:40:40 +0000928 if (bp->b_target != lasttarg ||
Christoph Hellwigbfc60172011-01-07 13:02:23 +0000929 time_after(jiffies, (lasttime + 5*HZ))) {
930 lasttime = jiffies;
Christoph Hellwigb38505b2011-10-10 16:52:50 +0000931 xfs_buf_ioerror_alert(bp, __func__);
Christoph Hellwigbfc60172011-01-07 13:02:23 +0000932 }
Chandra Seetharaman49074c02011-07-22 23:40:40 +0000933 lasttarg = bp->b_target;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
Christoph Hellwigbfc60172011-01-07 13:02:23 +0000935 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300936 * If the write was asynchronous then no one will be looking for the
Christoph Hellwigbfc60172011-01-07 13:02:23 +0000937 * error. Clear the error state and write the buffer out again.
938 *
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000939 * 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.
Christoph Hellwigbfc60172011-01-07 13:02:23 +0000945 */
946 if (XFS_BUF_ISASYNC(bp)) {
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000947 ASSERT(bp->b_iodone != NULL);
948
949 trace_xfs_buf_item_iodone_async(bp, _RET_IP_);
950
Chandra Seetharaman5a52c2a582011-07-22 23:39:51 +0000951 xfs_buf_ioerror(bp, 0); /* errno of 0 unsets the flag */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952
Christoph Hellwigbfc60172011-01-07 13:02:23 +0000953 if (!XFS_BUF_ISSTALE(bp)) {
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000954 bp->b_flags |= XBF_WRITE | XBF_ASYNC | XBF_DONE;
955 xfs_bdstrat_cb(bp);
956 } else {
957 xfs_buf_relse(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 }
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000959
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 return;
961 }
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000962
Christoph Hellwigbfc60172011-01-07 13:02:23 +0000963 /*
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 */
Christoph Hellwigc867cb62011-10-10 16:52:46 +0000967 xfs_buf_stale(bp);
Christoph Hellwigbfc60172011-01-07 13:02:23 +0000968 XFS_BUF_DONE(bp);
Christoph Hellwigbfc60172011-01-07 13:02:23 +0000969
970 trace_xfs_buf_error_relse(bp, _RET_IP_);
Christoph Hellwigbfc60172011-01-07 13:02:23 +0000971
972do_callbacks:
Dave Chinnerc90821a2010-12-03 17:00:52 +1100973 xfs_buf_do_callbacks(bp);
Christoph Hellwigadadbee2011-07-13 13:43:49 +0200974 bp->b_fspriv = NULL;
Christoph Hellwigcb669ca2011-07-13 13:43:49 +0200975 bp->b_iodone = NULL;
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +0000976 xfs_buf_ioend(bp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977}
978
979/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 * 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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986void
987xfs_buf_iodone(
Christoph Hellwigca30b2a2010-06-23 18:11:15 +1000988 struct xfs_buf *bp,
989 struct xfs_log_item *lip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990{
Christoph Hellwigca30b2a2010-06-23 18:11:15 +1000991 struct xfs_ail *ailp = lip->li_ailp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992
Christoph Hellwigca30b2a2010-06-23 18:11:15 +1000993 ASSERT(BUF_ITEM(lip)->bli_buf == bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994
Lachlan McIlroye1f5dbd2008-09-17 16:52:13 +1000995 xfs_buf_rele(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996
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
David Chinner783a2f62008-10-30 17:39:58 +11001002 * aborted forcibly. xfs_trans_ail_delete() takes care of these.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 *
1004 * Either way, AIL is useless if we're forcing a shutdown.
1005 */
David Chinnerfc1829f2008-10-30 17:39:46 +11001006 spin_lock(&ailp->xa_lock);
Dave Chinner04913fd2012-04-23 15:58:41 +10001007 xfs_trans_ail_delete(ailp, lip, SHUTDOWN_CORRUPT_INCORE);
Christoph Hellwigca30b2a2010-06-23 18:11:15 +10001008 xfs_buf_item_free(BUF_ITEM(lip));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009}