blob: 63d81a22f4fd60c6d6abc93538c37e6480c8fd3b [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"
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"
Nathan Scotta844f452005-11-02 14:38:42 +110023#include "xfs_inum.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include "xfs_trans.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include "xfs_sb.h"
26#include "xfs_ag.h"
Nathan Scotta844f452005-11-02 14:38:42 +110027#include "xfs_dir2.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include "xfs_dmapi.h"
29#include "xfs_mount.h"
Nathan Scotta844f452005-11-02 14:38:42 +110030#include "xfs_bmap_btree.h"
31#include "xfs_alloc_btree.h"
32#include "xfs_ialloc_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110033#include "xfs_dir2_sf.h"
34#include "xfs_attr_sf.h"
35#include "xfs_dinode.h"
36#include "xfs_inode.h"
37#include "xfs_buf_item.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include "xfs_trans_priv.h"
39#include "xfs_error.h"
40#include "xfs_rw.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000041#include "xfs_trace.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Christoph Hellwig4a5224d2010-04-18 00:10:45 +000043/*
44 * Check to see if a buffer matching the given parameters is already
45 * a part of the given transaction.
46 */
47STATIC struct xfs_buf *
48xfs_trans_buf_item_match(
49 struct xfs_trans *tp,
50 struct xfs_buftarg *target,
51 xfs_daddr_t blkno,
52 int len)
53{
54 xfs_log_item_chunk_t *licp;
55 xfs_log_item_desc_t *lidp;
56 xfs_buf_log_item_t *blip;
57 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Christoph Hellwig4a5224d2010-04-18 00:10:45 +000059 len = BBTOB(len);
60 for (licp = &tp->t_items; licp != NULL; licp = licp->lic_next) {
61 if (xfs_lic_are_all_free(licp)) {
62 ASSERT(licp == &tp->t_items);
63 ASSERT(licp->lic_next == NULL);
64 return NULL;
65 }
66
67 for (i = 0; i < licp->lic_unused; i++) {
68 /*
69 * Skip unoccupied slots.
70 */
71 if (xfs_lic_isfree(licp, i))
72 continue;
73
74 lidp = xfs_lic_slot(licp, i);
75 blip = (xfs_buf_log_item_t *)lidp->lid_item;
76 if (blip->bli_item.li_type != XFS_LI_BUF)
77 continue;
78
79 if (XFS_BUF_TARGET(blip->bli_buf) == target &&
80 XFS_BUF_ADDR(blip->bli_buf) == blkno &&
81 XFS_BUF_COUNT(blip->bli_buf) == len)
82 return blip->bli_buf;
83 }
84 }
85
86 return NULL;
87}
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Christoph Hellwigd7e84f42010-02-15 23:35:09 +000089/*
90 * Add the locked buffer to the transaction.
91 *
92 * The buffer must be locked, and it cannot be associated with any
93 * transaction.
94 *
95 * If the buffer does not yet have a buf log item associated with it,
96 * then allocate one for it. Then add the buf item to the transaction.
97 */
98STATIC void
99_xfs_trans_bjoin(
100 struct xfs_trans *tp,
101 struct xfs_buf *bp,
102 int reset_recur)
103{
104 struct xfs_buf_log_item *bip;
105
106 ASSERT(XFS_BUF_ISBUSY(bp));
107 ASSERT(XFS_BUF_FSPRIVATE2(bp, void *) == NULL);
108
109 /*
110 * The xfs_buf_log_item pointer is stored in b_fsprivate. If
111 * it doesn't have one yet, then allocate one and initialize it.
112 * The checks to see if one is there are in xfs_buf_item_init().
113 */
114 xfs_buf_item_init(bp, tp->t_mountp);
115 bip = XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t *);
116 ASSERT(!(bip->bli_flags & XFS_BLI_STALE));
Dave Chinnerc1155412010-05-07 11:05:19 +1000117 ASSERT(!(bip->bli_format.blf_flags & XFS_BLF_CANCEL));
Christoph Hellwigd7e84f42010-02-15 23:35:09 +0000118 ASSERT(!(bip->bli_flags & XFS_BLI_LOGGED));
119 if (reset_recur)
120 bip->bli_recur = 0;
121
122 /*
123 * Take a reference for this transaction on the buf item.
124 */
125 atomic_inc(&bip->bli_refcount);
126
127 /*
128 * Get a log_item_desc to point at the new item.
129 */
130 (void) xfs_trans_add_item(tp, (xfs_log_item_t *)bip);
131
132 /*
133 * Initialize b_fsprivate2 so we can find it with incore_match()
134 * in xfs_trans_get_buf() and friends above.
135 */
136 XFS_BUF_SET_FSPRIVATE2(bp, tp);
137
138}
139
140void
141xfs_trans_bjoin(
142 struct xfs_trans *tp,
143 struct xfs_buf *bp)
144{
145 _xfs_trans_bjoin(tp, bp, 0);
146 trace_xfs_trans_bjoin(bp->b_fspriv);
147}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
149/*
150 * Get and lock the buffer for the caller if it is not already
151 * locked within the given transaction. If it is already locked
152 * within the transaction, just increment its lock recursion count
153 * and return a pointer to it.
154 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 * If the transaction pointer is NULL, make this just a normal
156 * get_buf() call.
157 */
158xfs_buf_t *
159xfs_trans_get_buf(xfs_trans_t *tp,
160 xfs_buftarg_t *target_dev,
161 xfs_daddr_t blkno,
162 int len,
163 uint flags)
164{
165 xfs_buf_t *bp;
166 xfs_buf_log_item_t *bip;
167
168 if (flags == 0)
Christoph Hellwig0cadda12010-01-19 09:56:44 +0000169 flags = XBF_LOCK | XBF_MAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
171 /*
172 * Default to a normal get_buf() call if the tp is NULL.
173 */
Christoph Hellwig6ad112b2009-11-24 18:02:23 +0000174 if (tp == NULL)
Christoph Hellwig0cadda12010-01-19 09:56:44 +0000175 return xfs_buf_get(target_dev, blkno, len,
176 flags | XBF_DONT_BLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
178 /*
179 * If we find the buffer in the cache with this transaction
180 * pointer in its b_fsprivate2 field, then we know we already
181 * have it locked. In this case we just increment the lock
182 * recursion count and return the buffer to the caller.
183 */
Christoph Hellwig4a5224d2010-04-18 00:10:45 +0000184 bp = xfs_trans_buf_item_match(tp, target_dev, blkno, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 if (bp != NULL) {
186 ASSERT(XFS_BUF_VALUSEMA(bp) <= 0);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000187 if (XFS_FORCED_SHUTDOWN(tp->t_mountp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 XFS_BUF_SUPER_STALE(bp);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000189
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 /*
191 * If the buffer is stale then it was binval'ed
192 * since last read. This doesn't matter since the
193 * caller isn't allowed to use the data anyway.
194 */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000195 else if (XFS_BUF_ISSTALE(bp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 ASSERT(!XFS_BUF_ISDELAYWRITE(bp));
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000197
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 ASSERT(XFS_BUF_FSPRIVATE2(bp, xfs_trans_t *) == tp);
199 bip = XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t *);
200 ASSERT(bip != NULL);
201 ASSERT(atomic_read(&bip->bli_refcount) > 0);
202 bip->bli_recur++;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000203 trace_xfs_trans_get_buf_recur(bip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 return (bp);
205 }
206
207 /*
Christoph Hellwig0cadda12010-01-19 09:56:44 +0000208 * We always specify the XBF_DONT_BLOCK flag within a transaction
209 * so that get_buf does not try to push out a delayed write buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 * which might cause another transaction to take place (if the
211 * buffer was delayed alloc). Such recursive transactions can
212 * easily deadlock with our current transaction as well as cause
213 * us to run out of stack space.
214 */
Christoph Hellwig0cadda12010-01-19 09:56:44 +0000215 bp = xfs_buf_get(target_dev, blkno, len, flags | XBF_DONT_BLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 if (bp == NULL) {
217 return NULL;
218 }
219
220 ASSERT(!XFS_BUF_GETERROR(bp));
221
Christoph Hellwigd7e84f42010-02-15 23:35:09 +0000222 _xfs_trans_bjoin(tp, bp, 1);
223 trace_xfs_trans_get_buf(bp->b_fspriv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 return (bp);
225}
226
227/*
228 * Get and lock the superblock buffer of this file system for the
229 * given transaction.
230 *
231 * We don't need to use incore_match() here, because the superblock
232 * buffer is a private buffer which we keep a pointer to in the
233 * mount structure.
234 */
235xfs_buf_t *
236xfs_trans_getsb(xfs_trans_t *tp,
237 struct xfs_mount *mp,
238 int flags)
239{
240 xfs_buf_t *bp;
241 xfs_buf_log_item_t *bip;
242
243 /*
244 * Default to just trying to lock the superblock buffer
245 * if tp is NULL.
246 */
247 if (tp == NULL) {
248 return (xfs_getsb(mp, flags));
249 }
250
251 /*
252 * If the superblock buffer already has this transaction
253 * pointer in its b_fsprivate2 field, then we know we already
254 * have it locked. In this case we just increment the lock
255 * recursion count and return the buffer to the caller.
256 */
257 bp = mp->m_sb_bp;
258 if (XFS_BUF_FSPRIVATE2(bp, xfs_trans_t *) == tp) {
259 bip = XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t*);
260 ASSERT(bip != NULL);
261 ASSERT(atomic_read(&bip->bli_refcount) > 0);
262 bip->bli_recur++;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000263 trace_xfs_trans_getsb_recur(bip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 return (bp);
265 }
266
267 bp = xfs_getsb(mp, flags);
Christoph Hellwigd7e84f42010-02-15 23:35:09 +0000268 if (bp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
Christoph Hellwigd7e84f42010-02-15 23:35:09 +0000271 _xfs_trans_bjoin(tp, bp, 1);
272 trace_xfs_trans_getsb(bp->b_fspriv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 return (bp);
274}
275
276#ifdef DEBUG
277xfs_buftarg_t *xfs_error_target;
278int xfs_do_error;
279int xfs_req_num;
280int xfs_error_mod = 33;
281#endif
282
283/*
284 * Get and lock the buffer for the caller if it is not already
285 * locked within the given transaction. If it has not yet been
286 * read in, read it from disk. If it is already locked
287 * within the transaction and already read in, just increment its
288 * lock recursion count and return a pointer to it.
289 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 * If the transaction pointer is NULL, make this just a normal
291 * read_buf() call.
292 */
293int
294xfs_trans_read_buf(
295 xfs_mount_t *mp,
296 xfs_trans_t *tp,
297 xfs_buftarg_t *target,
298 xfs_daddr_t blkno,
299 int len,
300 uint flags,
301 xfs_buf_t **bpp)
302{
303 xfs_buf_t *bp;
304 xfs_buf_log_item_t *bip;
305 int error;
306
307 if (flags == 0)
Christoph Hellwig0cadda12010-01-19 09:56:44 +0000308 flags = XBF_LOCK | XBF_MAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
310 /*
311 * Default to a normal get_buf() call if the tp is NULL.
312 */
313 if (tp == NULL) {
Christoph Hellwig0cadda12010-01-19 09:56:44 +0000314 bp = xfs_buf_read(target, blkno, len, flags | XBF_DONT_BLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 if (!bp)
Christoph Hellwig0cadda12010-01-19 09:56:44 +0000316 return (flags & XBF_TRYLOCK) ?
David Chinnera3f74ff2008-03-06 13:43:42 +1100317 EAGAIN : XFS_ERROR(ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
Julia Lawalla0f7bfd2009-07-27 18:15:25 +0200319 if (XFS_BUF_GETERROR(bp) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 xfs_ioerror_alert("xfs_trans_read_buf", mp,
321 bp, blkno);
322 error = XFS_BUF_GETERROR(bp);
323 xfs_buf_relse(bp);
324 return error;
325 }
326#ifdef DEBUG
Julia Lawalla0f7bfd2009-07-27 18:15:25 +0200327 if (xfs_do_error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 if (xfs_error_target == target) {
329 if (((xfs_req_num++) % xfs_error_mod) == 0) {
330 xfs_buf_relse(bp);
Nathan Scottb6574522006-06-09 15:29:40 +1000331 cmn_err(CE_DEBUG, "Returning error!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 return XFS_ERROR(EIO);
333 }
334 }
335 }
336#endif
337 if (XFS_FORCED_SHUTDOWN(mp))
338 goto shutdown_abort;
339 *bpp = bp;
340 return 0;
341 }
342
343 /*
344 * If we find the buffer in the cache with this transaction
345 * pointer in its b_fsprivate2 field, then we know we already
346 * have it locked. If it is already read in we just increment
347 * the lock recursion count and return the buffer to the caller.
348 * If the buffer is not yet read in, then we read it in, increment
349 * the lock recursion count, and return it to the caller.
350 */
Christoph Hellwig4a5224d2010-04-18 00:10:45 +0000351 bp = xfs_trans_buf_item_match(tp, target, blkno, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 if (bp != NULL) {
353 ASSERT(XFS_BUF_VALUSEMA(bp) <= 0);
354 ASSERT(XFS_BUF_FSPRIVATE2(bp, xfs_trans_t *) == tp);
355 ASSERT(XFS_BUF_FSPRIVATE(bp, void *) != NULL);
356 ASSERT((XFS_BUF_ISERROR(bp)) == 0);
357 if (!(XFS_BUF_ISDONE(bp))) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000358 trace_xfs_trans_read_buf_io(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 ASSERT(!XFS_BUF_ISASYNC(bp));
360 XFS_BUF_READ(bp);
361 xfsbdstrat(tp->t_mountp, bp);
David Chinnerd64e31a2008-04-10 12:22:17 +1000362 error = xfs_iowait(bp);
363 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 xfs_ioerror_alert("xfs_trans_read_buf", mp,
365 bp, blkno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 xfs_buf_relse(bp);
367 /*
David Chinnerd64e31a2008-04-10 12:22:17 +1000368 * We can gracefully recover from most read
369 * errors. Ones we can't are those that happen
370 * after the transaction's already dirty.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 */
372 if (tp->t_flags & XFS_TRANS_DIRTY)
373 xfs_force_shutdown(tp->t_mountp,
Nathan Scott7d04a332006-06-09 14:58:38 +1000374 SHUTDOWN_META_IO_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 return error;
376 }
377 }
378 /*
379 * We never locked this buf ourselves, so we shouldn't
380 * brelse it either. Just get out.
381 */
382 if (XFS_FORCED_SHUTDOWN(mp)) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000383 trace_xfs_trans_read_buf_shut(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 *bpp = NULL;
385 return XFS_ERROR(EIO);
386 }
387
388
389 bip = XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t*);
390 bip->bli_recur++;
391
392 ASSERT(atomic_read(&bip->bli_refcount) > 0);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000393 trace_xfs_trans_read_buf_recur(bip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 *bpp = bp;
395 return 0;
396 }
397
398 /*
Christoph Hellwig0cadda12010-01-19 09:56:44 +0000399 * We always specify the XBF_DONT_BLOCK flag within a transaction
400 * so that get_buf does not try to push out a delayed write buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 * which might cause another transaction to take place (if the
402 * buffer was delayed alloc). Such recursive transactions can
403 * easily deadlock with our current transaction as well as cause
404 * us to run out of stack space.
405 */
Christoph Hellwig0cadda12010-01-19 09:56:44 +0000406 bp = xfs_buf_read(target, blkno, len, flags | XBF_DONT_BLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 if (bp == NULL) {
408 *bpp = NULL;
409 return 0;
410 }
411 if (XFS_BUF_GETERROR(bp) != 0) {
412 XFS_BUF_SUPER_STALE(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 error = XFS_BUF_GETERROR(bp);
414
415 xfs_ioerror_alert("xfs_trans_read_buf", mp,
416 bp, blkno);
417 if (tp->t_flags & XFS_TRANS_DIRTY)
Nathan Scott7d04a332006-06-09 14:58:38 +1000418 xfs_force_shutdown(tp->t_mountp, SHUTDOWN_META_IO_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 xfs_buf_relse(bp);
420 return error;
421 }
422#ifdef DEBUG
423 if (xfs_do_error && !(tp->t_flags & XFS_TRANS_DIRTY)) {
424 if (xfs_error_target == target) {
425 if (((xfs_req_num++) % xfs_error_mod) == 0) {
426 xfs_force_shutdown(tp->t_mountp,
Nathan Scott7d04a332006-06-09 14:58:38 +1000427 SHUTDOWN_META_IO_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 xfs_buf_relse(bp);
Nathan Scottb6574522006-06-09 15:29:40 +1000429 cmn_err(CE_DEBUG, "Returning trans error!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 return XFS_ERROR(EIO);
431 }
432 }
433 }
434#endif
435 if (XFS_FORCED_SHUTDOWN(mp))
436 goto shutdown_abort;
437
Christoph Hellwigd7e84f42010-02-15 23:35:09 +0000438 _xfs_trans_bjoin(tp, bp, 1);
439 trace_xfs_trans_read_buf(bp->b_fspriv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 *bpp = bp;
442 return 0;
443
444shutdown_abort:
445 /*
446 * the theory here is that buffer is good but we're
447 * bailing out because the filesystem is being forcibly
448 * shut down. So we should leave the b_flags alone since
449 * the buffer's not staled and just get out.
450 */
451#if defined(DEBUG)
452 if (XFS_BUF_ISSTALE(bp) && XFS_BUF_ISDELAYWRITE(bp))
453 cmn_err(CE_NOTE, "about to pop assert, bp == 0x%p", bp);
454#endif
Christoph Hellwig0cadda12010-01-19 09:56:44 +0000455 ASSERT((XFS_BUF_BFLAGS(bp) & (XBF_STALE|XBF_DELWRI)) !=
456 (XBF_STALE|XBF_DELWRI));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000458 trace_xfs_trans_read_buf_shut(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 xfs_buf_relse(bp);
460 *bpp = NULL;
461 return XFS_ERROR(EIO);
462}
463
464
465/*
466 * Release the buffer bp which was previously acquired with one of the
467 * xfs_trans_... buffer allocation routines if the buffer has not
468 * been modified within this transaction. If the buffer is modified
469 * within this transaction, do decrement the recursion count but do
470 * not release the buffer even if the count goes to 0. If the buffer is not
471 * modified within the transaction, decrement the recursion count and
472 * release the buffer if the recursion count goes to 0.
473 *
474 * If the buffer is to be released and it was not modified before
475 * this transaction began, then free the buf_log_item associated with it.
476 *
477 * If the transaction pointer is NULL, make this just a normal
478 * brelse() call.
479 */
480void
481xfs_trans_brelse(xfs_trans_t *tp,
482 xfs_buf_t *bp)
483{
484 xfs_buf_log_item_t *bip;
485 xfs_log_item_t *lip;
486 xfs_log_item_desc_t *lidp;
487
488 /*
489 * Default to a normal brelse() call if the tp is NULL.
490 */
491 if (tp == NULL) {
492 ASSERT(XFS_BUF_FSPRIVATE2(bp, void *) == NULL);
493 /*
494 * If there's a buf log item attached to the buffer,
495 * then let the AIL know that the buffer is being
496 * unlocked.
497 */
498 if (XFS_BUF_FSPRIVATE(bp, void *) != NULL) {
499 lip = XFS_BUF_FSPRIVATE(bp, xfs_log_item_t *);
500 if (lip->li_type == XFS_LI_BUF) {
501 bip = XFS_BUF_FSPRIVATE(bp,xfs_buf_log_item_t*);
David Chinner783a2f62008-10-30 17:39:58 +1100502 xfs_trans_unlocked_item(bip->bli_item.li_ailp,
503 lip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 }
505 }
506 xfs_buf_relse(bp);
507 return;
508 }
509
510 ASSERT(XFS_BUF_FSPRIVATE2(bp, xfs_trans_t *) == tp);
511 bip = XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t *);
512 ASSERT(bip->bli_item.li_type == XFS_LI_BUF);
513 ASSERT(!(bip->bli_flags & XFS_BLI_STALE));
Dave Chinnerc1155412010-05-07 11:05:19 +1000514 ASSERT(!(bip->bli_format.blf_flags & XFS_BLF_CANCEL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 ASSERT(atomic_read(&bip->bli_refcount) > 0);
516
517 /*
518 * Find the item descriptor pointing to this buffer's
519 * log item. It must be there.
520 */
521 lidp = xfs_trans_find_item(tp, (xfs_log_item_t*)bip);
522 ASSERT(lidp != NULL);
523
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000524 trace_xfs_trans_brelse(bip);
525
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 /*
527 * If the release is just for a recursive lock,
528 * then decrement the count and return.
529 */
530 if (bip->bli_recur > 0) {
531 bip->bli_recur--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 return;
533 }
534
535 /*
536 * If the buffer is dirty within this transaction, we can't
537 * release it until we commit.
538 */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000539 if (lidp->lid_flags & XFS_LID_DIRTY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
542 /*
543 * If the buffer has been invalidated, then we can't release
544 * it until the transaction commits to disk unless it is re-dirtied
545 * as part of this transaction. This prevents us from pulling
546 * the item from the AIL before we should.
547 */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000548 if (bip->bli_flags & XFS_BLI_STALE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
551 ASSERT(!(bip->bli_flags & XFS_BLI_LOGGED));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
553 /*
554 * Free up the log item descriptor tracking the released item.
555 */
556 xfs_trans_free_item(tp, lidp);
557
558 /*
559 * Clear the hold flag in the buf log item if it is set.
560 * We wouldn't want the next user of the buffer to
561 * get confused.
562 */
563 if (bip->bli_flags & XFS_BLI_HOLD) {
564 bip->bli_flags &= ~XFS_BLI_HOLD;
565 }
566
567 /*
568 * Drop our reference to the buf log item.
569 */
570 atomic_dec(&bip->bli_refcount);
571
572 /*
573 * If the buf item is not tracking data in the log, then
574 * we must free it before releasing the buffer back to the
575 * free pool. Before releasing the buffer to the free pool,
576 * clear the transaction pointer in b_fsprivate2 to dissolve
577 * its relation to this transaction.
578 */
579 if (!xfs_buf_item_dirty(bip)) {
580/***
581 ASSERT(bp->b_pincount == 0);
582***/
583 ASSERT(atomic_read(&bip->bli_refcount) == 0);
584 ASSERT(!(bip->bli_item.li_flags & XFS_LI_IN_AIL));
585 ASSERT(!(bip->bli_flags & XFS_BLI_INODE_ALLOC_BUF));
586 xfs_buf_item_relse(bp);
587 bip = NULL;
588 }
589 XFS_BUF_SET_FSPRIVATE2(bp, NULL);
590
591 /*
592 * If we've still got a buf log item on the buffer, then
593 * tell the AIL that the buffer is being unlocked.
594 */
595 if (bip != NULL) {
David Chinner783a2f62008-10-30 17:39:58 +1100596 xfs_trans_unlocked_item(bip->bli_item.li_ailp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 (xfs_log_item_t*)bip);
598 }
599
600 xfs_buf_relse(bp);
601 return;
602}
603
604/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 * Mark the buffer as not needing to be unlocked when the buf item's
606 * IOP_UNLOCK() routine is called. The buffer must already be locked
607 * and associated with the given transaction.
608 */
609/* ARGSUSED */
610void
611xfs_trans_bhold(xfs_trans_t *tp,
612 xfs_buf_t *bp)
613{
614 xfs_buf_log_item_t *bip;
615
616 ASSERT(XFS_BUF_ISBUSY(bp));
617 ASSERT(XFS_BUF_FSPRIVATE2(bp, xfs_trans_t *) == tp);
618 ASSERT(XFS_BUF_FSPRIVATE(bp, void *) != NULL);
619
620 bip = XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t *);
621 ASSERT(!(bip->bli_flags & XFS_BLI_STALE));
Dave Chinnerc1155412010-05-07 11:05:19 +1000622 ASSERT(!(bip->bli_format.blf_flags & XFS_BLF_CANCEL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 ASSERT(atomic_read(&bip->bli_refcount) > 0);
624 bip->bli_flags |= XFS_BLI_HOLD;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000625 trace_xfs_trans_bhold(bip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626}
627
628/*
Tim Shimminefa092f2005-09-05 08:29:01 +1000629 * Cancel the previous buffer hold request made on this buffer
630 * for this transaction.
631 */
632void
633xfs_trans_bhold_release(xfs_trans_t *tp,
634 xfs_buf_t *bp)
635{
636 xfs_buf_log_item_t *bip;
637
638 ASSERT(XFS_BUF_ISBUSY(bp));
639 ASSERT(XFS_BUF_FSPRIVATE2(bp, xfs_trans_t *) == tp);
640 ASSERT(XFS_BUF_FSPRIVATE(bp, void *) != NULL);
641
642 bip = XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t *);
643 ASSERT(!(bip->bli_flags & XFS_BLI_STALE));
Dave Chinnerc1155412010-05-07 11:05:19 +1000644 ASSERT(!(bip->bli_format.blf_flags & XFS_BLF_CANCEL));
Tim Shimminefa092f2005-09-05 08:29:01 +1000645 ASSERT(atomic_read(&bip->bli_refcount) > 0);
646 ASSERT(bip->bli_flags & XFS_BLI_HOLD);
647 bip->bli_flags &= ~XFS_BLI_HOLD;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000648
649 trace_xfs_trans_bhold_release(bip);
Tim Shimminefa092f2005-09-05 08:29:01 +1000650}
651
652/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 * This is called to mark bytes first through last inclusive of the given
654 * buffer as needing to be logged when the transaction is committed.
655 * The buffer must already be associated with the given transaction.
656 *
657 * First and last are numbers relative to the beginning of this buffer,
658 * so the first byte in the buffer is numbered 0 regardless of the
659 * value of b_blkno.
660 */
661void
662xfs_trans_log_buf(xfs_trans_t *tp,
663 xfs_buf_t *bp,
664 uint first,
665 uint last)
666{
667 xfs_buf_log_item_t *bip;
668 xfs_log_item_desc_t *lidp;
669
670 ASSERT(XFS_BUF_ISBUSY(bp));
671 ASSERT(XFS_BUF_FSPRIVATE2(bp, xfs_trans_t *) == tp);
672 ASSERT(XFS_BUF_FSPRIVATE(bp, void *) != NULL);
673 ASSERT((first <= last) && (last < XFS_BUF_COUNT(bp)));
674 ASSERT((XFS_BUF_IODONE_FUNC(bp) == NULL) ||
675 (XFS_BUF_IODONE_FUNC(bp) == xfs_buf_iodone_callbacks));
676
677 /*
678 * Mark the buffer as needing to be written out eventually,
679 * and set its iodone function to remove the buffer's buf log
680 * item from the AIL and free it when the buffer is flushed
681 * to disk. See xfs_buf_attach_iodone() for more details
682 * on li_cb and xfs_buf_iodone_callbacks().
683 * If we end up aborting this transaction, we trap this buffer
684 * inside the b_bdstrat callback so that this won't get written to
685 * disk.
686 */
687 XFS_BUF_DELAYWRITE(bp);
688 XFS_BUF_DONE(bp);
689
690 bip = XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t *);
691 ASSERT(atomic_read(&bip->bli_refcount) > 0);
692 XFS_BUF_SET_IODONE_FUNC(bp, xfs_buf_iodone_callbacks);
693 bip->bli_item.li_cb = (void(*)(xfs_buf_t*,xfs_log_item_t*))xfs_buf_iodone;
694
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000695 trace_xfs_trans_log_buf(bip);
696
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 /*
698 * If we invalidated the buffer within this transaction, then
699 * cancel the invalidation now that we're dirtying the buffer
700 * again. There are no races with the code in xfs_buf_item_unpin(),
701 * because we have a reference to the buffer this entire time.
702 */
703 if (bip->bli_flags & XFS_BLI_STALE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 bip->bli_flags &= ~XFS_BLI_STALE;
705 ASSERT(XFS_BUF_ISSTALE(bp));
706 XFS_BUF_UNSTALE(bp);
Dave Chinnerc1155412010-05-07 11:05:19 +1000707 bip->bli_format.blf_flags &= ~XFS_BLF_CANCEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 }
709
710 lidp = xfs_trans_find_item(tp, (xfs_log_item_t*)bip);
711 ASSERT(lidp != NULL);
712
713 tp->t_flags |= XFS_TRANS_DIRTY;
714 lidp->lid_flags |= XFS_LID_DIRTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 bip->bli_flags |= XFS_BLI_LOGGED;
716 xfs_buf_item_log(bip, first, last);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717}
718
719
720/*
721 * This called to invalidate a buffer that is being used within
722 * a transaction. Typically this is because the blocks in the
723 * buffer are being freed, so we need to prevent it from being
724 * written out when we're done. Allowing it to be written again
725 * might overwrite data in the free blocks if they are reallocated
726 * to a file.
727 *
728 * We prevent the buffer from being written out by clearing the
729 * B_DELWRI flag. We can't always
730 * get rid of the buf log item at this point, though, because
731 * the buffer may still be pinned by another transaction. If that
732 * is the case, then we'll wait until the buffer is committed to
733 * disk for the last time (we can tell by the ref count) and
734 * free it in xfs_buf_item_unpin(). Until it is cleaned up we
735 * will keep the buffer locked so that the buffer and buf log item
736 * are not reused.
737 */
738void
739xfs_trans_binval(
740 xfs_trans_t *tp,
741 xfs_buf_t *bp)
742{
743 xfs_log_item_desc_t *lidp;
744 xfs_buf_log_item_t *bip;
745
746 ASSERT(XFS_BUF_ISBUSY(bp));
747 ASSERT(XFS_BUF_FSPRIVATE2(bp, xfs_trans_t *) == tp);
748 ASSERT(XFS_BUF_FSPRIVATE(bp, void *) != NULL);
749
750 bip = XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t *);
751 lidp = xfs_trans_find_item(tp, (xfs_log_item_t*)bip);
752 ASSERT(lidp != NULL);
753 ASSERT(atomic_read(&bip->bli_refcount) > 0);
754
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000755 trace_xfs_trans_binval(bip);
756
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 if (bip->bli_flags & XFS_BLI_STALE) {
758 /*
759 * If the buffer is already invalidated, then
760 * just return.
761 */
762 ASSERT(!(XFS_BUF_ISDELAYWRITE(bp)));
763 ASSERT(XFS_BUF_ISSTALE(bp));
764 ASSERT(!(bip->bli_flags & (XFS_BLI_LOGGED | XFS_BLI_DIRTY)));
Dave Chinnerc1155412010-05-07 11:05:19 +1000765 ASSERT(!(bip->bli_format.blf_flags & XFS_BLF_INODE_BUF));
766 ASSERT(bip->bli_format.blf_flags & XFS_BLF_CANCEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 ASSERT(lidp->lid_flags & XFS_LID_DIRTY);
768 ASSERT(tp->t_flags & XFS_TRANS_DIRTY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 return;
770 }
771
772 /*
773 * Clear the dirty bit in the buffer and set the STALE flag
774 * in the buf log item. The STALE flag will be used in
775 * xfs_buf_item_unpin() to determine if it should clean up
776 * when the last reference to the buf item is given up.
Dave Chinnerc1155412010-05-07 11:05:19 +1000777 * We set the XFS_BLF_CANCEL flag in the buf log format structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 * and log the buf item. This will be used at recovery time
779 * to determine that copies of the buffer in the log before
780 * this should not be replayed.
781 * We mark the item descriptor and the transaction dirty so
782 * that we'll hold the buffer until after the commit.
783 *
784 * Since we're invalidating the buffer, we also clear the state
785 * about which parts of the buffer have been logged. We also
786 * clear the flag indicating that this is an inode buffer since
787 * the data in the buffer will no longer be valid.
788 *
789 * We set the stale bit in the buffer as well since we're getting
790 * rid of it.
791 */
792 XFS_BUF_UNDELAYWRITE(bp);
793 XFS_BUF_STALE(bp);
794 bip->bli_flags |= XFS_BLI_STALE;
Dave Chinnerccf7c232010-05-20 23:19:42 +1000795 bip->bli_flags &= ~(XFS_BLI_INODE_BUF | XFS_BLI_LOGGED | XFS_BLI_DIRTY);
Dave Chinnerc1155412010-05-07 11:05:19 +1000796 bip->bli_format.blf_flags &= ~XFS_BLF_INODE_BUF;
797 bip->bli_format.blf_flags |= XFS_BLF_CANCEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 memset((char *)(bip->bli_format.blf_data_map), 0,
799 (bip->bli_format.blf_map_size * sizeof(uint)));
Dave Chinner8e123852010-03-08 11:26:03 +1100800 lidp->lid_flags |= XFS_LID_DIRTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 tp->t_flags |= XFS_TRANS_DIRTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802}
803
804/*
Dave Chinnerccf7c232010-05-20 23:19:42 +1000805 * This call is used to indicate that the buffer contains on-disk inodes which
806 * must be handled specially during recovery. They require special handling
807 * because only the di_next_unlinked from the inodes in the buffer should be
808 * recovered. The rest of the data in the buffer is logged via the inodes
809 * themselves.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 *
Dave Chinnerccf7c232010-05-20 23:19:42 +1000811 * All we do is set the XFS_BLI_INODE_BUF flag in the items flags so it can be
812 * transferred to the buffer's log format structure so that we'll know what to
813 * do at recovery time.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815void
816xfs_trans_inode_buf(
817 xfs_trans_t *tp,
818 xfs_buf_t *bp)
819{
820 xfs_buf_log_item_t *bip;
821
822 ASSERT(XFS_BUF_ISBUSY(bp));
823 ASSERT(XFS_BUF_FSPRIVATE2(bp, xfs_trans_t *) == tp);
824 ASSERT(XFS_BUF_FSPRIVATE(bp, void *) != NULL);
825
826 bip = XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t *);
827 ASSERT(atomic_read(&bip->bli_refcount) > 0);
828
Dave Chinnerccf7c232010-05-20 23:19:42 +1000829 bip->bli_flags |= XFS_BLI_INODE_BUF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830}
831
832/*
833 * This call is used to indicate that the buffer is going to
834 * be staled and was an inode buffer. This means it gets
835 * special processing during unpin - where any inodes
836 * associated with the buffer should be removed from ail.
837 * There is also special processing during recovery,
838 * any replay of the inodes in the buffer needs to be
839 * prevented as the buffer may have been reused.
840 */
841void
842xfs_trans_stale_inode_buf(
843 xfs_trans_t *tp,
844 xfs_buf_t *bp)
845{
846 xfs_buf_log_item_t *bip;
847
848 ASSERT(XFS_BUF_ISBUSY(bp));
849 ASSERT(XFS_BUF_FSPRIVATE2(bp, xfs_trans_t *) == tp);
850 ASSERT(XFS_BUF_FSPRIVATE(bp, void *) != NULL);
851
852 bip = XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t *);
853 ASSERT(atomic_read(&bip->bli_refcount) > 0);
854
855 bip->bli_flags |= XFS_BLI_STALE_INODE;
856 bip->bli_item.li_cb = (void(*)(xfs_buf_t*,xfs_log_item_t*))
857 xfs_buf_iodone;
858}
859
860
861
862/*
863 * Mark the buffer as being one which contains newly allocated
864 * inodes. We need to make sure that even if this buffer is
865 * relogged as an 'inode buf' we still recover all of the inode
866 * images in the face of a crash. This works in coordination with
867 * xfs_buf_item_committed() to ensure that the buffer remains in the
868 * AIL at its original location even after it has been relogged.
869 */
870/* ARGSUSED */
871void
872xfs_trans_inode_alloc_buf(
873 xfs_trans_t *tp,
874 xfs_buf_t *bp)
875{
876 xfs_buf_log_item_t *bip;
877
878 ASSERT(XFS_BUF_ISBUSY(bp));
879 ASSERT(XFS_BUF_FSPRIVATE2(bp, xfs_trans_t *) == tp);
880 ASSERT(XFS_BUF_FSPRIVATE(bp, void *) != NULL);
881
882 bip = XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t *);
883 ASSERT(atomic_read(&bip->bli_refcount) > 0);
884
885 bip->bli_flags |= XFS_BLI_INODE_ALLOC_BUF;
886}
887
888
889/*
890 * Similar to xfs_trans_inode_buf(), this marks the buffer as a cluster of
891 * dquots. However, unlike in inode buffer recovery, dquot buffers get
892 * recovered in their entirety. (Hence, no XFS_BLI_DQUOT_ALLOC_BUF flag).
893 * The only thing that makes dquot buffers different from regular
894 * buffers is that we must not replay dquot bufs when recovering
895 * if a _corresponding_ quotaoff has happened. We also have to distinguish
896 * between usr dquot bufs and grp dquot bufs, because usr and grp quotas
897 * can be turned off independently.
898 */
899/* ARGSUSED */
900void
901xfs_trans_dquot_buf(
902 xfs_trans_t *tp,
903 xfs_buf_t *bp,
904 uint type)
905{
906 xfs_buf_log_item_t *bip;
907
908 ASSERT(XFS_BUF_ISBUSY(bp));
909 ASSERT(XFS_BUF_FSPRIVATE2(bp, xfs_trans_t *) == tp);
910 ASSERT(XFS_BUF_FSPRIVATE(bp, void *) != NULL);
Dave Chinnerc1155412010-05-07 11:05:19 +1000911 ASSERT(type == XFS_BLF_UDQUOT_BUF ||
912 type == XFS_BLF_PDQUOT_BUF ||
913 type == XFS_BLF_GDQUOT_BUF);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
915 bip = XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t *);
916 ASSERT(atomic_read(&bip->bli_refcount) > 0);
917
918 bip->bli_format.blf_flags |= type;
919}