blob: f9192b03d57049d276030ff0207d512c6d3a839f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Nathan Scott7b718762005-11-02 14:58:39 +11002 * Copyright (c) 2000-2002,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"
Dave Chinner70a98832013-10-23 10:36:05 +110020#include "xfs_shared.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110021#include "xfs_format.h"
Dave Chinner239880e2013-10-23 10:50:10 +110022#include "xfs_log_format.h"
23#include "xfs_trans_resv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include "xfs_sb.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include "xfs_mount.h"
Nathan Scotta844f452005-11-02 14:38:42 +110026#include "xfs_inode.h"
Dave Chinner239880e2013-10-23 10:50:10 +110027#include "xfs_trans.h"
Nathan Scotta844f452005-11-02 14:38:42 +110028#include "xfs_buf_item.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include "xfs_trans_priv.h"
30#include "xfs_error.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000031#include "xfs_trace.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
Christoph Hellwig4a5224d2010-04-18 00:10:45 +000033/*
34 * Check to see if a buffer matching the given parameters is already
35 * a part of the given transaction.
36 */
37STATIC struct xfs_buf *
38xfs_trans_buf_item_match(
39 struct xfs_trans *tp,
40 struct xfs_buftarg *target,
Dave Chinnerde2a4f52012-06-22 18:50:11 +100041 struct xfs_buf_map *map,
42 int nmaps)
Christoph Hellwig4a5224d2010-04-18 00:10:45 +000043{
Christoph Hellwige98c4142010-06-23 18:11:15 +100044 struct xfs_log_item_desc *lidp;
45 struct xfs_buf_log_item *blip;
Dave Chinnerde2a4f52012-06-22 18:50:11 +100046 int len = 0;
47 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Dave Chinnerde2a4f52012-06-22 18:50:11 +100049 for (i = 0; i < nmaps; i++)
50 len += map[i].bm_len;
51
Christoph Hellwige98c4142010-06-23 18:11:15 +100052 list_for_each_entry(lidp, &tp->t_items, lid_trans) {
53 blip = (struct xfs_buf_log_item *)lidp->lid_item;
54 if (blip->bli_item.li_type == XFS_LI_BUF &&
Chandra Seetharaman49074c02011-07-22 23:40:40 +000055 blip->bli_buf->b_target == target &&
Dave Chinnerde2a4f52012-06-22 18:50:11 +100056 XFS_BUF_ADDR(blip->bli_buf) == map[0].bm_bn &&
57 blip->bli_buf->b_length == len) {
58 ASSERT(blip->bli_buf->b_map_count == nmaps);
Christoph Hellwige98c4142010-06-23 18:11:15 +100059 return blip->bli_buf;
Dave Chinnerde2a4f52012-06-22 18:50:11 +100060 }
Christoph Hellwig4a5224d2010-04-18 00:10:45 +000061 }
62
63 return NULL;
64}
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Christoph Hellwigd7e84f42010-02-15 23:35:09 +000066/*
67 * Add the locked buffer to the transaction.
68 *
69 * The buffer must be locked, and it cannot be associated with any
70 * transaction.
71 *
72 * If the buffer does not yet have a buf log item associated with it,
73 * then allocate one for it. Then add the buf item to the transaction.
74 */
75STATIC void
76_xfs_trans_bjoin(
77 struct xfs_trans *tp,
78 struct xfs_buf *bp,
79 int reset_recur)
80{
81 struct xfs_buf_log_item *bip;
82
Christoph Hellwigbf9d9012011-07-13 13:43:49 +020083 ASSERT(bp->b_transp == NULL);
Christoph Hellwigd7e84f42010-02-15 23:35:09 +000084
85 /*
86 * The xfs_buf_log_item pointer is stored in b_fsprivate. If
87 * it doesn't have one yet, then allocate one and initialize it.
88 * The checks to see if one is there are in xfs_buf_item_init().
89 */
90 xfs_buf_item_init(bp, tp->t_mountp);
Christoph Hellwigadadbee2011-07-13 13:43:49 +020091 bip = bp->b_fspriv;
Christoph Hellwigd7e84f42010-02-15 23:35:09 +000092 ASSERT(!(bip->bli_flags & XFS_BLI_STALE));
Mark Tinguely0f22f9d2012-12-04 17:18:03 -060093 ASSERT(!(bip->__bli_format.blf_flags & XFS_BLF_CANCEL));
Christoph Hellwigd7e84f42010-02-15 23:35:09 +000094 ASSERT(!(bip->bli_flags & XFS_BLI_LOGGED));
95 if (reset_recur)
96 bip->bli_recur = 0;
97
98 /*
99 * Take a reference for this transaction on the buf item.
100 */
101 atomic_inc(&bip->bli_refcount);
102
103 /*
104 * Get a log_item_desc to point at the new item.
105 */
Christoph Hellwige98c4142010-06-23 18:11:15 +1000106 xfs_trans_add_item(tp, &bip->bli_item);
Christoph Hellwigd7e84f42010-02-15 23:35:09 +0000107
108 /*
109 * Initialize b_fsprivate2 so we can find it with incore_match()
110 * in xfs_trans_get_buf() and friends above.
111 */
Christoph Hellwigbf9d9012011-07-13 13:43:49 +0200112 bp->b_transp = tp;
Christoph Hellwigd7e84f42010-02-15 23:35:09 +0000113
114}
115
116void
117xfs_trans_bjoin(
118 struct xfs_trans *tp,
119 struct xfs_buf *bp)
120{
121 _xfs_trans_bjoin(tp, bp, 0);
122 trace_xfs_trans_bjoin(bp->b_fspriv);
123}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
125/*
126 * Get and lock the buffer for the caller if it is not already
127 * locked within the given transaction. If it is already locked
128 * within the transaction, just increment its lock recursion count
129 * and return a pointer to it.
130 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 * If the transaction pointer is NULL, make this just a normal
132 * get_buf() call.
133 */
Dave Chinnerde2a4f52012-06-22 18:50:11 +1000134struct xfs_buf *
135xfs_trans_get_buf_map(
136 struct xfs_trans *tp,
137 struct xfs_buftarg *target,
138 struct xfs_buf_map *map,
139 int nmaps,
140 xfs_buf_flags_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141{
142 xfs_buf_t *bp;
143 xfs_buf_log_item_t *bip;
144
Dave Chinnerde2a4f52012-06-22 18:50:11 +1000145 if (!tp)
146 return xfs_buf_get_map(target, map, nmaps, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
148 /*
149 * If we find the buffer in the cache with this transaction
150 * pointer in its b_fsprivate2 field, then we know we already
151 * have it locked. In this case we just increment the lock
152 * recursion count and return the buffer to the caller.
153 */
Dave Chinnerde2a4f52012-06-22 18:50:11 +1000154 bp = xfs_trans_buf_item_match(tp, target, map, nmaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 if (bp != NULL) {
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200156 ASSERT(xfs_buf_islocked(bp));
Christoph Hellwigc867cb62011-10-10 16:52:46 +0000157 if (XFS_FORCED_SHUTDOWN(tp->t_mountp)) {
158 xfs_buf_stale(bp);
Christoph Hellwigc867cb62011-10-10 16:52:46 +0000159 XFS_BUF_DONE(bp);
160 }
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000161
Christoph Hellwigbf9d9012011-07-13 13:43:49 +0200162 ASSERT(bp->b_transp == tp);
Christoph Hellwigadadbee2011-07-13 13:43:49 +0200163 bip = bp->b_fspriv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 ASSERT(bip != NULL);
165 ASSERT(atomic_read(&bip->bli_refcount) > 0);
166 bip->bli_recur++;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000167 trace_xfs_trans_get_buf_recur(bip);
Eric Sandeend99831f2014-06-22 15:03:54 +1000168 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 }
170
Dave Chinnerde2a4f52012-06-22 18:50:11 +1000171 bp = xfs_buf_get_map(target, map, nmaps, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 if (bp == NULL) {
173 return NULL;
174 }
175
Chandra Seetharaman5a52c2a582011-07-22 23:39:51 +0000176 ASSERT(!bp->b_error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
Christoph Hellwigd7e84f42010-02-15 23:35:09 +0000178 _xfs_trans_bjoin(tp, bp, 1);
179 trace_xfs_trans_get_buf(bp->b_fspriv);
Eric Sandeend99831f2014-06-22 15:03:54 +1000180 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181}
182
183/*
184 * Get and lock the superblock buffer of this file system for the
185 * given transaction.
186 *
187 * We don't need to use incore_match() here, because the superblock
188 * buffer is a private buffer which we keep a pointer to in the
189 * mount structure.
190 */
191xfs_buf_t *
192xfs_trans_getsb(xfs_trans_t *tp,
193 struct xfs_mount *mp,
194 int flags)
195{
196 xfs_buf_t *bp;
197 xfs_buf_log_item_t *bip;
198
199 /*
200 * Default to just trying to lock the superblock buffer
201 * if tp is NULL.
202 */
Eric Sandeend99831f2014-06-22 15:03:54 +1000203 if (tp == NULL)
204 return xfs_getsb(mp, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
206 /*
207 * If the superblock buffer already has this transaction
208 * pointer in its b_fsprivate2 field, then we know we already
209 * have it locked. In this case we just increment the lock
210 * recursion count and return the buffer to the caller.
211 */
212 bp = mp->m_sb_bp;
Christoph Hellwigbf9d9012011-07-13 13:43:49 +0200213 if (bp->b_transp == tp) {
Christoph Hellwigadadbee2011-07-13 13:43:49 +0200214 bip = bp->b_fspriv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 ASSERT(bip != NULL);
216 ASSERT(atomic_read(&bip->bli_refcount) > 0);
217 bip->bli_recur++;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000218 trace_xfs_trans_getsb_recur(bip);
Eric Sandeend99831f2014-06-22 15:03:54 +1000219 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 }
221
222 bp = xfs_getsb(mp, flags);
Christoph Hellwigd7e84f42010-02-15 23:35:09 +0000223 if (bp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
Christoph Hellwigd7e84f42010-02-15 23:35:09 +0000226 _xfs_trans_bjoin(tp, bp, 1);
227 trace_xfs_trans_getsb(bp->b_fspriv);
Eric Sandeend99831f2014-06-22 15:03:54 +1000228 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229}
230
231#ifdef DEBUG
232xfs_buftarg_t *xfs_error_target;
233int xfs_do_error;
234int xfs_req_num;
235int xfs_error_mod = 33;
236#endif
237
238/*
239 * Get and lock the buffer for the caller if it is not already
240 * locked within the given transaction. If it has not yet been
241 * read in, read it from disk. If it is already locked
242 * within the transaction and already read in, just increment its
243 * lock recursion count and return a pointer to it.
244 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 * If the transaction pointer is NULL, make this just a normal
246 * read_buf() call.
247 */
248int
Dave Chinnerde2a4f52012-06-22 18:50:11 +1000249xfs_trans_read_buf_map(
250 struct xfs_mount *mp,
251 struct xfs_trans *tp,
252 struct xfs_buftarg *target,
253 struct xfs_buf_map *map,
254 int nmaps,
255 xfs_buf_flags_t flags,
Dave Chinnerc3f8fc72012-11-12 22:54:01 +1100256 struct xfs_buf **bpp,
Dave Chinner1813dd62012-11-14 17:54:40 +1100257 const struct xfs_buf_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258{
259 xfs_buf_t *bp;
260 xfs_buf_log_item_t *bip;
261 int error;
262
Dave Chinner7ca790a2012-04-23 15:58:55 +1000263 *bpp = NULL;
Dave Chinnerde2a4f52012-06-22 18:50:11 +1000264 if (!tp) {
Dave Chinner1813dd62012-11-14 17:54:40 +1100265 bp = xfs_buf_read_map(target, map, nmaps, flags, ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 if (!bp)
Christoph Hellwig0cadda12010-01-19 09:56:44 +0000267 return (flags & XBF_TRYLOCK) ?
Dave Chinner24513372014-06-25 14:58:08 +1000268 -EAGAIN : -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
Chandra Seetharaman5a52c2a582011-07-22 23:39:51 +0000270 if (bp->b_error) {
271 error = bp->b_error;
Christoph Hellwig901796a2011-10-10 16:52:49 +0000272 xfs_buf_ioerror_alert(bp, __func__);
Dave Chinner7ca790a2012-04-23 15:58:55 +1000273 XFS_BUF_UNDONE(bp);
274 xfs_buf_stale(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 xfs_buf_relse(bp);
Dave Chinnerac75a1f2014-03-07 16:19:14 +1100276
277 /* bad CRC means corrupted metadata */
Dave Chinner24513372014-06-25 14:58:08 +1000278 if (error == -EFSBADCRC)
279 error = -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 return error;
281 }
282#ifdef DEBUG
Julia Lawalla0f7bfd2009-07-27 18:15:25 +0200283 if (xfs_do_error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 if (xfs_error_target == target) {
285 if (((xfs_req_num++) % xfs_error_mod) == 0) {
286 xfs_buf_relse(bp);
Dave Chinner0b932cc2011-03-07 10:08:35 +1100287 xfs_debug(mp, "Returning error!");
Dave Chinner24513372014-06-25 14:58:08 +1000288 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 }
290 }
291 }
292#endif
293 if (XFS_FORCED_SHUTDOWN(mp))
294 goto shutdown_abort;
295 *bpp = bp;
296 return 0;
297 }
298
299 /*
300 * If we find the buffer in the cache with this transaction
301 * pointer in its b_fsprivate2 field, then we know we already
302 * have it locked. If it is already read in we just increment
303 * the lock recursion count and return the buffer to the caller.
304 * If the buffer is not yet read in, then we read it in, increment
305 * the lock recursion count, and return it to the caller.
306 */
Dave Chinnerde2a4f52012-06-22 18:50:11 +1000307 bp = xfs_trans_buf_item_match(tp, target, map, nmaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 if (bp != NULL) {
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200309 ASSERT(xfs_buf_islocked(bp));
Christoph Hellwigbf9d9012011-07-13 13:43:49 +0200310 ASSERT(bp->b_transp == tp);
Christoph Hellwigadadbee2011-07-13 13:43:49 +0200311 ASSERT(bp->b_fspriv != NULL);
Chandra Seetharaman5a52c2a582011-07-22 23:39:51 +0000312 ASSERT(!bp->b_error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 if (!(XFS_BUF_ISDONE(bp))) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000314 trace_xfs_trans_read_buf_io(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 ASSERT(!XFS_BUF_ISASYNC(bp));
Dave Chinnerc3f8fc72012-11-12 22:54:01 +1100316 ASSERT(bp->b_iodone == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 XFS_BUF_READ(bp);
Dave Chinner1813dd62012-11-14 17:54:40 +1100318 bp->b_ops = ops;
Christoph Hellwig83a0adc2013-12-17 00:03:52 -0800319
Dave Chinner595bff72014-10-02 09:05:14 +1000320 error = xfs_buf_submit_wait(bp);
David Chinnerd64e31a2008-04-10 12:22:17 +1000321 if (error) {
Dave Chinner595bff72014-10-02 09:05:14 +1000322 if (!XFS_FORCED_SHUTDOWN(mp))
323 xfs_buf_ioerror_alert(bp, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 xfs_buf_relse(bp);
325 /*
David Chinnerd64e31a2008-04-10 12:22:17 +1000326 * We can gracefully recover from most read
327 * errors. Ones we can't are those that happen
328 * after the transaction's already dirty.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 */
330 if (tp->t_flags & XFS_TRANS_DIRTY)
331 xfs_force_shutdown(tp->t_mountp,
Nathan Scott7d04a332006-06-09 14:58:38 +1000332 SHUTDOWN_META_IO_ERROR);
Dave Chinnerac75a1f2014-03-07 16:19:14 +1100333 /* bad CRC means corrupted metadata */
Dave Chinner24513372014-06-25 14:58:08 +1000334 if (error == -EFSBADCRC)
335 error = -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 return error;
337 }
338 }
339 /*
340 * We never locked this buf ourselves, so we shouldn't
341 * brelse it either. Just get out.
342 */
343 if (XFS_FORCED_SHUTDOWN(mp)) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000344 trace_xfs_trans_read_buf_shut(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 *bpp = NULL;
Dave Chinner24513372014-06-25 14:58:08 +1000346 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 }
348
349
Christoph Hellwigadadbee2011-07-13 13:43:49 +0200350 bip = bp->b_fspriv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 bip->bli_recur++;
352
353 ASSERT(atomic_read(&bip->bli_refcount) > 0);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000354 trace_xfs_trans_read_buf_recur(bip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 *bpp = bp;
356 return 0;
357 }
358
Dave Chinner1813dd62012-11-14 17:54:40 +1100359 bp = xfs_buf_read_map(target, map, nmaps, flags, ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 if (bp == NULL) {
361 *bpp = NULL;
Dave Chinner7401aaf2011-03-26 09:14:44 +1100362 return (flags & XBF_TRYLOCK) ?
Dave Chinner24513372014-06-25 14:58:08 +1000363 0 : -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 }
Chandra Seetharaman5a52c2a582011-07-22 23:39:51 +0000365 if (bp->b_error) {
366 error = bp->b_error;
Christoph Hellwigc867cb62011-10-10 16:52:46 +0000367 xfs_buf_stale(bp);
Christoph Hellwigc867cb62011-10-10 16:52:46 +0000368 XFS_BUF_DONE(bp);
Christoph Hellwig901796a2011-10-10 16:52:49 +0000369 xfs_buf_ioerror_alert(bp, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 if (tp->t_flags & XFS_TRANS_DIRTY)
Nathan Scott7d04a332006-06-09 14:58:38 +1000371 xfs_force_shutdown(tp->t_mountp, SHUTDOWN_META_IO_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 xfs_buf_relse(bp);
Dave Chinnerac75a1f2014-03-07 16:19:14 +1100373
374 /* bad CRC means corrupted metadata */
Dave Chinner24513372014-06-25 14:58:08 +1000375 if (error == -EFSBADCRC)
376 error = -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 return error;
378 }
379#ifdef DEBUG
380 if (xfs_do_error && !(tp->t_flags & XFS_TRANS_DIRTY)) {
381 if (xfs_error_target == target) {
382 if (((xfs_req_num++) % xfs_error_mod) == 0) {
383 xfs_force_shutdown(tp->t_mountp,
Nathan Scott7d04a332006-06-09 14:58:38 +1000384 SHUTDOWN_META_IO_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 xfs_buf_relse(bp);
Dave Chinner0b932cc2011-03-07 10:08:35 +1100386 xfs_debug(mp, "Returning trans error!");
Dave Chinner24513372014-06-25 14:58:08 +1000387 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 }
389 }
390 }
391#endif
392 if (XFS_FORCED_SHUTDOWN(mp))
393 goto shutdown_abort;
394
Christoph Hellwigd7e84f42010-02-15 23:35:09 +0000395 _xfs_trans_bjoin(tp, bp, 1);
396 trace_xfs_trans_read_buf(bp->b_fspriv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 *bpp = bp;
399 return 0;
400
401shutdown_abort:
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000402 trace_xfs_trans_read_buf_shut(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 xfs_buf_relse(bp);
404 *bpp = NULL;
Dave Chinner24513372014-06-25 14:58:08 +1000405 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406}
407
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408/*
409 * Release the buffer bp which was previously acquired with one of the
410 * xfs_trans_... buffer allocation routines if the buffer has not
411 * been modified within this transaction. If the buffer is modified
412 * within this transaction, do decrement the recursion count but do
413 * not release the buffer even if the count goes to 0. If the buffer is not
414 * modified within the transaction, decrement the recursion count and
415 * release the buffer if the recursion count goes to 0.
416 *
417 * If the buffer is to be released and it was not modified before
418 * this transaction began, then free the buf_log_item associated with it.
419 *
420 * If the transaction pointer is NULL, make this just a normal
421 * brelse() call.
422 */
423void
424xfs_trans_brelse(xfs_trans_t *tp,
425 xfs_buf_t *bp)
426{
427 xfs_buf_log_item_t *bip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
429 /*
430 * Default to a normal brelse() call if the tp is NULL.
431 */
432 if (tp == NULL) {
Christoph Hellwigbf9d9012011-07-13 13:43:49 +0200433 ASSERT(bp->b_transp == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 xfs_buf_relse(bp);
435 return;
436 }
437
Christoph Hellwigbf9d9012011-07-13 13:43:49 +0200438 ASSERT(bp->b_transp == tp);
Christoph Hellwigadadbee2011-07-13 13:43:49 +0200439 bip = bp->b_fspriv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 ASSERT(bip->bli_item.li_type == XFS_LI_BUF);
441 ASSERT(!(bip->bli_flags & XFS_BLI_STALE));
Mark Tinguely0f22f9d2012-12-04 17:18:03 -0600442 ASSERT(!(bip->__bli_format.blf_flags & XFS_BLF_CANCEL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 ASSERT(atomic_read(&bip->bli_refcount) > 0);
444
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000445 trace_xfs_trans_brelse(bip);
446
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 /*
448 * If the release is just for a recursive lock,
449 * then decrement the count and return.
450 */
451 if (bip->bli_recur > 0) {
452 bip->bli_recur--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 return;
454 }
455
456 /*
457 * If the buffer is dirty within this transaction, we can't
458 * release it until we commit.
459 */
Christoph Hellwige98c4142010-06-23 18:11:15 +1000460 if (bip->bli_item.li_desc->lid_flags & XFS_LID_DIRTY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
463 /*
464 * If the buffer has been invalidated, then we can't release
465 * it until the transaction commits to disk unless it is re-dirtied
466 * as part of this transaction. This prevents us from pulling
467 * the item from the AIL before we should.
468 */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000469 if (bip->bli_flags & XFS_BLI_STALE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
472 ASSERT(!(bip->bli_flags & XFS_BLI_LOGGED));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
474 /*
475 * Free up the log item descriptor tracking the released item.
476 */
Christoph Hellwige98c4142010-06-23 18:11:15 +1000477 xfs_trans_del_item(&bip->bli_item);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
479 /*
480 * Clear the hold flag in the buf log item if it is set.
481 * We wouldn't want the next user of the buffer to
482 * get confused.
483 */
484 if (bip->bli_flags & XFS_BLI_HOLD) {
485 bip->bli_flags &= ~XFS_BLI_HOLD;
486 }
487
488 /*
489 * Drop our reference to the buf log item.
490 */
491 atomic_dec(&bip->bli_refcount);
492
493 /*
494 * If the buf item is not tracking data in the log, then
495 * we must free it before releasing the buffer back to the
496 * free pool. Before releasing the buffer to the free pool,
497 * clear the transaction pointer in b_fsprivate2 to dissolve
498 * its relation to this transaction.
499 */
500 if (!xfs_buf_item_dirty(bip)) {
501/***
502 ASSERT(bp->b_pincount == 0);
503***/
504 ASSERT(atomic_read(&bip->bli_refcount) == 0);
505 ASSERT(!(bip->bli_item.li_flags & XFS_LI_IN_AIL));
506 ASSERT(!(bip->bli_flags & XFS_BLI_INODE_ALLOC_BUF));
507 xfs_buf_item_relse(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 }
Christoph Hellwig5b03ff12012-02-20 02:31:22 +0000509
Christoph Hellwigbf9d9012011-07-13 13:43:49 +0200510 bp->b_transp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 xfs_buf_relse(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512}
513
514/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 * Mark the buffer as not needing to be unlocked when the buf item's
Dave Chinner904c17e2013-08-28 21:12:03 +1000516 * iop_unlock() routine is called. The buffer must already be locked
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 * and associated with the given transaction.
518 */
519/* ARGSUSED */
520void
521xfs_trans_bhold(xfs_trans_t *tp,
522 xfs_buf_t *bp)
523{
Christoph Hellwigadadbee2011-07-13 13:43:49 +0200524 xfs_buf_log_item_t *bip = bp->b_fspriv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
Christoph Hellwigbf9d9012011-07-13 13:43:49 +0200526 ASSERT(bp->b_transp == tp);
Christoph Hellwigadadbee2011-07-13 13:43:49 +0200527 ASSERT(bip != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 ASSERT(!(bip->bli_flags & XFS_BLI_STALE));
Mark Tinguely0f22f9d2012-12-04 17:18:03 -0600529 ASSERT(!(bip->__bli_format.blf_flags & XFS_BLF_CANCEL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 ASSERT(atomic_read(&bip->bli_refcount) > 0);
Christoph Hellwigadadbee2011-07-13 13:43:49 +0200531
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 bip->bli_flags |= XFS_BLI_HOLD;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000533 trace_xfs_trans_bhold(bip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534}
535
536/*
Tim Shimminefa092f2005-09-05 08:29:01 +1000537 * Cancel the previous buffer hold request made on this buffer
538 * for this transaction.
539 */
540void
541xfs_trans_bhold_release(xfs_trans_t *tp,
542 xfs_buf_t *bp)
543{
Christoph Hellwigadadbee2011-07-13 13:43:49 +0200544 xfs_buf_log_item_t *bip = bp->b_fspriv;
Tim Shimminefa092f2005-09-05 08:29:01 +1000545
Christoph Hellwigbf9d9012011-07-13 13:43:49 +0200546 ASSERT(bp->b_transp == tp);
Christoph Hellwigadadbee2011-07-13 13:43:49 +0200547 ASSERT(bip != NULL);
Tim Shimminefa092f2005-09-05 08:29:01 +1000548 ASSERT(!(bip->bli_flags & XFS_BLI_STALE));
Mark Tinguely0f22f9d2012-12-04 17:18:03 -0600549 ASSERT(!(bip->__bli_format.blf_flags & XFS_BLF_CANCEL));
Tim Shimminefa092f2005-09-05 08:29:01 +1000550 ASSERT(atomic_read(&bip->bli_refcount) > 0);
551 ASSERT(bip->bli_flags & XFS_BLI_HOLD);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000552
Christoph Hellwigadadbee2011-07-13 13:43:49 +0200553 bip->bli_flags &= ~XFS_BLI_HOLD;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000554 trace_xfs_trans_bhold_release(bip);
Tim Shimminefa092f2005-09-05 08:29:01 +1000555}
556
557/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 * This is called to mark bytes first through last inclusive of the given
559 * buffer as needing to be logged when the transaction is committed.
560 * The buffer must already be associated with the given transaction.
561 *
562 * First and last are numbers relative to the beginning of this buffer,
563 * so the first byte in the buffer is numbered 0 regardless of the
564 * value of b_blkno.
565 */
566void
567xfs_trans_log_buf(xfs_trans_t *tp,
568 xfs_buf_t *bp,
569 uint first,
570 uint last)
571{
Christoph Hellwigadadbee2011-07-13 13:43:49 +0200572 xfs_buf_log_item_t *bip = bp->b_fspriv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
Christoph Hellwigbf9d9012011-07-13 13:43:49 +0200574 ASSERT(bp->b_transp == tp);
Christoph Hellwigadadbee2011-07-13 13:43:49 +0200575 ASSERT(bip != NULL);
Dave Chinneraa0e8832012-04-23 15:58:52 +1000576 ASSERT(first <= last && last < BBTOB(bp->b_length));
Christoph Hellwigcb669ca2011-07-13 13:43:49 +0200577 ASSERT(bp->b_iodone == NULL ||
578 bp->b_iodone == xfs_buf_iodone_callbacks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
580 /*
581 * Mark the buffer as needing to be written out eventually,
582 * and set its iodone function to remove the buffer's buf log
583 * item from the AIL and free it when the buffer is flushed
584 * to disk. See xfs_buf_attach_iodone() for more details
585 * on li_cb and xfs_buf_iodone_callbacks().
586 * If we end up aborting this transaction, we trap this buffer
587 * inside the b_bdstrat callback so that this won't get written to
588 * disk.
589 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 XFS_BUF_DONE(bp);
591
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 ASSERT(atomic_read(&bip->bli_refcount) > 0);
Christoph Hellwigcb669ca2011-07-13 13:43:49 +0200593 bp->b_iodone = xfs_buf_iodone_callbacks;
Christoph Hellwigca30b2a2010-06-23 18:11:15 +1000594 bip->bli_item.li_cb = xfs_buf_iodone;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000596 trace_xfs_trans_log_buf(bip);
597
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 /*
599 * If we invalidated the buffer within this transaction, then
600 * cancel the invalidation now that we're dirtying the buffer
601 * again. There are no races with the code in xfs_buf_item_unpin(),
602 * because we have a reference to the buffer this entire time.
603 */
604 if (bip->bli_flags & XFS_BLI_STALE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 bip->bli_flags &= ~XFS_BLI_STALE;
606 ASSERT(XFS_BUF_ISSTALE(bp));
607 XFS_BUF_UNSTALE(bp);
Mark Tinguely0f22f9d2012-12-04 17:18:03 -0600608 bip->__bli_format.blf_flags &= ~XFS_BLF_CANCEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 }
610
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 tp->t_flags |= XFS_TRANS_DIRTY;
Christoph Hellwige98c4142010-06-23 18:11:15 +1000612 bip->bli_item.li_desc->lid_flags |= XFS_LID_DIRTY;
Dave Chinner5f6bed72013-06-27 16:04:52 +1000613
614 /*
615 * If we have an ordered buffer we are not logging any dirty range but
616 * it still needs to be marked dirty and that it has been logged.
617 */
618 bip->bli_flags |= XFS_BLI_DIRTY | XFS_BLI_LOGGED;
619 if (!(bip->bli_flags & XFS_BLI_ORDERED))
620 xfs_buf_item_log(bip, first, last);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621}
622
623
624/*
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000625 * Invalidate a buffer that is being used within a transaction.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 *
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000627 * Typically this is because the blocks in the buffer are being freed, so we
628 * need to prevent it from being written out when we're done. Allowing it
629 * to be written again might overwrite data in the free blocks if they are
630 * reallocated to a file.
631 *
632 * We prevent the buffer from being written out by marking it stale. We can't
633 * get rid of the buf log item at this point because the buffer may still be
634 * pinned by another transaction. If that is the case, then we'll wait until
635 * the buffer is committed to disk for the last time (we can tell by the ref
636 * count) and free it in xfs_buf_item_unpin(). Until that happens we will
637 * keep the buffer locked so that the buffer and buf log item are not reused.
638 *
639 * We also set the XFS_BLF_CANCEL flag in the buf log format structure and log
640 * the buf item. This will be used at recovery time to determine that copies
641 * of the buffer in the log before this should not be replayed.
642 *
643 * We mark the item descriptor and the transaction dirty so that we'll hold
644 * the buffer until after the commit.
645 *
646 * Since we're invalidating the buffer, we also clear the state about which
647 * parts of the buffer have been logged. We also clear the flag indicating
648 * that this is an inode buffer since the data in the buffer will no longer
649 * be valid.
650 *
651 * We set the stale bit in the buffer as well since we're getting rid of it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 */
653void
654xfs_trans_binval(
655 xfs_trans_t *tp,
656 xfs_buf_t *bp)
657{
Christoph Hellwigadadbee2011-07-13 13:43:49 +0200658 xfs_buf_log_item_t *bip = bp->b_fspriv;
Mark Tinguely91e4bac2012-12-04 17:18:05 -0600659 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
Christoph Hellwigbf9d9012011-07-13 13:43:49 +0200661 ASSERT(bp->b_transp == tp);
Christoph Hellwigadadbee2011-07-13 13:43:49 +0200662 ASSERT(bip != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 ASSERT(atomic_read(&bip->bli_refcount) > 0);
664
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000665 trace_xfs_trans_binval(bip);
666
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 if (bip->bli_flags & XFS_BLI_STALE) {
668 /*
669 * If the buffer is already invalidated, then
670 * just return.
671 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 ASSERT(XFS_BUF_ISSTALE(bp));
673 ASSERT(!(bip->bli_flags & (XFS_BLI_LOGGED | XFS_BLI_DIRTY)));
Mark Tinguely0f22f9d2012-12-04 17:18:03 -0600674 ASSERT(!(bip->__bli_format.blf_flags & XFS_BLF_INODE_BUF));
Dave Chinner61fe1352013-04-03 16:11:30 +1100675 ASSERT(!(bip->__bli_format.blf_flags & XFS_BLFT_MASK));
Mark Tinguely0f22f9d2012-12-04 17:18:03 -0600676 ASSERT(bip->__bli_format.blf_flags & XFS_BLF_CANCEL);
Christoph Hellwige98c4142010-06-23 18:11:15 +1000677 ASSERT(bip->bli_item.li_desc->lid_flags & XFS_LID_DIRTY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 ASSERT(tp->t_flags & XFS_TRANS_DIRTY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 return;
680 }
681
Christoph Hellwigc867cb62011-10-10 16:52:46 +0000682 xfs_buf_stale(bp);
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000683
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 bip->bli_flags |= XFS_BLI_STALE;
Dave Chinnerccf7c232010-05-20 23:19:42 +1000685 bip->bli_flags &= ~(XFS_BLI_INODE_BUF | XFS_BLI_LOGGED | XFS_BLI_DIRTY);
Mark Tinguely0f22f9d2012-12-04 17:18:03 -0600686 bip->__bli_format.blf_flags &= ~XFS_BLF_INODE_BUF;
687 bip->__bli_format.blf_flags |= XFS_BLF_CANCEL;
Dave Chinner61fe1352013-04-03 16:11:30 +1100688 bip->__bli_format.blf_flags &= ~XFS_BLFT_MASK;
Mark Tinguely91e4bac2012-12-04 17:18:05 -0600689 for (i = 0; i < bip->bli_format_count; i++) {
690 memset(bip->bli_formats[i].blf_data_map, 0,
691 (bip->bli_formats[i].blf_map_size * sizeof(uint)));
692 }
Christoph Hellwige98c4142010-06-23 18:11:15 +1000693 bip->bli_item.li_desc->lid_flags |= XFS_LID_DIRTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 tp->t_flags |= XFS_TRANS_DIRTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695}
696
697/*
Dave Chinnerccf7c232010-05-20 23:19:42 +1000698 * This call is used to indicate that the buffer contains on-disk inodes which
699 * must be handled specially during recovery. They require special handling
700 * because only the di_next_unlinked from the inodes in the buffer should be
701 * recovered. The rest of the data in the buffer is logged via the inodes
702 * themselves.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 *
Dave Chinnerccf7c232010-05-20 23:19:42 +1000704 * All we do is set the XFS_BLI_INODE_BUF flag in the items flags so it can be
705 * transferred to the buffer's log format structure so that we'll know what to
706 * do at recovery time.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708void
709xfs_trans_inode_buf(
710 xfs_trans_t *tp,
711 xfs_buf_t *bp)
712{
Christoph Hellwigadadbee2011-07-13 13:43:49 +0200713 xfs_buf_log_item_t *bip = bp->b_fspriv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
Christoph Hellwigbf9d9012011-07-13 13:43:49 +0200715 ASSERT(bp->b_transp == tp);
Christoph Hellwigadadbee2011-07-13 13:43:49 +0200716 ASSERT(bip != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 ASSERT(atomic_read(&bip->bli_refcount) > 0);
718
Dave Chinnerccf7c232010-05-20 23:19:42 +1000719 bip->bli_flags |= XFS_BLI_INODE_BUF;
Dave Chinner61fe1352013-04-03 16:11:30 +1100720 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DINO_BUF);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721}
722
723/*
724 * This call is used to indicate that the buffer is going to
725 * be staled and was an inode buffer. This means it gets
Christoph Hellwig93848a92013-04-03 16:11:17 +1100726 * special processing during unpin - where any inodes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 * associated with the buffer should be removed from ail.
728 * There is also special processing during recovery,
729 * any replay of the inodes in the buffer needs to be
730 * prevented as the buffer may have been reused.
731 */
732void
733xfs_trans_stale_inode_buf(
734 xfs_trans_t *tp,
735 xfs_buf_t *bp)
736{
Christoph Hellwigadadbee2011-07-13 13:43:49 +0200737 xfs_buf_log_item_t *bip = bp->b_fspriv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
Christoph Hellwigbf9d9012011-07-13 13:43:49 +0200739 ASSERT(bp->b_transp == tp);
Christoph Hellwigadadbee2011-07-13 13:43:49 +0200740 ASSERT(bip != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 ASSERT(atomic_read(&bip->bli_refcount) > 0);
742
743 bip->bli_flags |= XFS_BLI_STALE_INODE;
Christoph Hellwigca30b2a2010-06-23 18:11:15 +1000744 bip->bli_item.li_cb = xfs_buf_iodone;
Dave Chinner61fe1352013-04-03 16:11:30 +1100745 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DINO_BUF);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746}
747
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748/*
749 * Mark the buffer as being one which contains newly allocated
750 * inodes. We need to make sure that even if this buffer is
751 * relogged as an 'inode buf' we still recover all of the inode
752 * images in the face of a crash. This works in coordination with
753 * xfs_buf_item_committed() to ensure that the buffer remains in the
754 * AIL at its original location even after it has been relogged.
755 */
756/* ARGSUSED */
757void
758xfs_trans_inode_alloc_buf(
759 xfs_trans_t *tp,
760 xfs_buf_t *bp)
761{
Christoph Hellwigadadbee2011-07-13 13:43:49 +0200762 xfs_buf_log_item_t *bip = bp->b_fspriv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
Christoph Hellwigbf9d9012011-07-13 13:43:49 +0200764 ASSERT(bp->b_transp == tp);
Christoph Hellwigadadbee2011-07-13 13:43:49 +0200765 ASSERT(bip != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 ASSERT(atomic_read(&bip->bli_refcount) > 0);
767
768 bip->bli_flags |= XFS_BLI_INODE_ALLOC_BUF;
Dave Chinner61fe1352013-04-03 16:11:30 +1100769 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DINO_BUF);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770}
771
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500772/*
Dave Chinner5f6bed72013-06-27 16:04:52 +1000773 * Mark the buffer as ordered for this transaction. This means
774 * that the contents of the buffer are not recorded in the transaction
775 * but it is tracked in the AIL as though it was. This allows us
776 * to record logical changes in transactions rather than the physical
777 * changes we make to the buffer without changing writeback ordering
778 * constraints of metadata buffers.
779 */
780void
781xfs_trans_ordered_buf(
782 struct xfs_trans *tp,
783 struct xfs_buf *bp)
784{
785 struct xfs_buf_log_item *bip = bp->b_fspriv;
786
787 ASSERT(bp->b_transp == tp);
788 ASSERT(bip != NULL);
789 ASSERT(atomic_read(&bip->bli_refcount) > 0);
790
791 bip->bli_flags |= XFS_BLI_ORDERED;
792 trace_xfs_buf_item_ordered(bip);
793}
794
795/*
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500796 * Set the type of the buffer for log recovery so that it can correctly identify
797 * and hence attach the correct buffer ops to the buffer after replay.
798 */
799void
800xfs_trans_buf_set_type(
801 struct xfs_trans *tp,
802 struct xfs_buf *bp,
Dave Chinner61fe1352013-04-03 16:11:30 +1100803 enum xfs_blft type)
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500804{
805 struct xfs_buf_log_item *bip = bp->b_fspriv;
806
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100807 if (!tp)
808 return;
809
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500810 ASSERT(bp->b_transp == tp);
811 ASSERT(bip != NULL);
812 ASSERT(atomic_read(&bip->bli_refcount) > 0);
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500813
Dave Chinner61fe1352013-04-03 16:11:30 +1100814 xfs_blft_to_flags(&bip->__bli_format, type);
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500815}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100817void
818xfs_trans_buf_copy_type(
819 struct xfs_buf *dst_bp,
820 struct xfs_buf *src_bp)
821{
822 struct xfs_buf_log_item *sbip = src_bp->b_fspriv;
823 struct xfs_buf_log_item *dbip = dst_bp->b_fspriv;
Dave Chinner61fe1352013-04-03 16:11:30 +1100824 enum xfs_blft type;
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100825
Dave Chinner61fe1352013-04-03 16:11:30 +1100826 type = xfs_blft_from_flags(&sbip->__bli_format);
827 xfs_blft_to_flags(&dbip->__bli_format, type);
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100828}
829
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830/*
831 * Similar to xfs_trans_inode_buf(), this marks the buffer as a cluster of
832 * dquots. However, unlike in inode buffer recovery, dquot buffers get
833 * recovered in their entirety. (Hence, no XFS_BLI_DQUOT_ALLOC_BUF flag).
834 * The only thing that makes dquot buffers different from regular
835 * buffers is that we must not replay dquot bufs when recovering
836 * if a _corresponding_ quotaoff has happened. We also have to distinguish
837 * between usr dquot bufs and grp dquot bufs, because usr and grp quotas
838 * can be turned off independently.
839 */
840/* ARGSUSED */
841void
842xfs_trans_dquot_buf(
843 xfs_trans_t *tp,
844 xfs_buf_t *bp,
845 uint type)
846{
Dave Chinner61fe1352013-04-03 16:11:30 +1100847 struct xfs_buf_log_item *bip = bp->b_fspriv;
848
Dave Chinnerc1155412010-05-07 11:05:19 +1000849 ASSERT(type == XFS_BLF_UDQUOT_BUF ||
850 type == XFS_BLF_PDQUOT_BUF ||
851 type == XFS_BLF_GDQUOT_BUF);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
Dave Chinner61fe1352013-04-03 16:11:30 +1100853 bip->__bli_format.blf_flags |= type;
854
855 switch (type) {
856 case XFS_BLF_UDQUOT_BUF:
857 type = XFS_BLFT_UDQUOT_BUF;
858 break;
859 case XFS_BLF_PDQUOT_BUF:
860 type = XFS_BLFT_PDQUOT_BUF;
861 break;
862 case XFS_BLF_GDQUOT_BUF:
863 type = XFS_BLFT_GDQUOT_BUF;
864 break;
865 default:
866 type = XFS_BLFT_UNKNOWN_BUF;
867 break;
868 }
869
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500870 xfs_trans_buf_set_type(tp, bp, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871}