Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 2 | * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc. |
| 3 | * All Rights Reserved. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 5 | * This program is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU General Public License as |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | * published by the Free Software Foundation. |
| 8 | * |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 9 | * This program is distributed in the hope that it would be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | * |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write the Free Software Foundation, |
| 16 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include "xfs.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 19 | #include "xfs_fs.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include "xfs_types.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 21 | #include "xfs_bit.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include "xfs_log.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 23 | #include "xfs_inum.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include "xfs_trans.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #include "xfs_sb.h" |
| 26 | #include "xfs_ag.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | #include "xfs_mount.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 28 | #include "xfs_bmap_btree.h" |
| 29 | #include "xfs_alloc_btree.h" |
| 30 | #include "xfs_ialloc_btree.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 31 | #include "xfs_dinode.h" |
| 32 | #include "xfs_inode.h" |
| 33 | #include "xfs_buf_item.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | #include "xfs_trans_priv.h" |
| 35 | #include "xfs_error.h" |
| 36 | #include "xfs_rw.h" |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 37 | #include "xfs_trace.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | |
Christoph Hellwig | 4a5224d | 2010-04-18 00:10:45 +0000 | [diff] [blame] | 39 | /* |
| 40 | * Check to see if a buffer matching the given parameters is already |
| 41 | * a part of the given transaction. |
| 42 | */ |
| 43 | STATIC struct xfs_buf * |
| 44 | xfs_trans_buf_item_match( |
| 45 | struct xfs_trans *tp, |
| 46 | struct xfs_buftarg *target, |
| 47 | xfs_daddr_t blkno, |
| 48 | int len) |
| 49 | { |
| 50 | xfs_log_item_chunk_t *licp; |
| 51 | xfs_log_item_desc_t *lidp; |
| 52 | xfs_buf_log_item_t *blip; |
| 53 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | |
Christoph Hellwig | 4a5224d | 2010-04-18 00:10:45 +0000 | [diff] [blame] | 55 | len = BBTOB(len); |
| 56 | for (licp = &tp->t_items; licp != NULL; licp = licp->lic_next) { |
| 57 | if (xfs_lic_are_all_free(licp)) { |
| 58 | ASSERT(licp == &tp->t_items); |
| 59 | ASSERT(licp->lic_next == NULL); |
| 60 | return NULL; |
| 61 | } |
| 62 | |
| 63 | for (i = 0; i < licp->lic_unused; i++) { |
| 64 | /* |
| 65 | * Skip unoccupied slots. |
| 66 | */ |
| 67 | if (xfs_lic_isfree(licp, i)) |
| 68 | continue; |
| 69 | |
| 70 | lidp = xfs_lic_slot(licp, i); |
| 71 | blip = (xfs_buf_log_item_t *)lidp->lid_item; |
| 72 | if (blip->bli_item.li_type != XFS_LI_BUF) |
| 73 | continue; |
| 74 | |
| 75 | if (XFS_BUF_TARGET(blip->bli_buf) == target && |
| 76 | XFS_BUF_ADDR(blip->bli_buf) == blkno && |
| 77 | XFS_BUF_COUNT(blip->bli_buf) == len) |
| 78 | return blip->bli_buf; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | return NULL; |
| 83 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | |
Christoph Hellwig | d7e84f4 | 2010-02-15 23:35:09 +0000 | [diff] [blame] | 85 | /* |
| 86 | * Add the locked buffer to the transaction. |
| 87 | * |
| 88 | * The buffer must be locked, and it cannot be associated with any |
| 89 | * transaction. |
| 90 | * |
| 91 | * If the buffer does not yet have a buf log item associated with it, |
| 92 | * then allocate one for it. Then add the buf item to the transaction. |
| 93 | */ |
| 94 | STATIC void |
| 95 | _xfs_trans_bjoin( |
| 96 | struct xfs_trans *tp, |
| 97 | struct xfs_buf *bp, |
| 98 | int reset_recur) |
| 99 | { |
| 100 | struct xfs_buf_log_item *bip; |
| 101 | |
| 102 | ASSERT(XFS_BUF_ISBUSY(bp)); |
| 103 | ASSERT(XFS_BUF_FSPRIVATE2(bp, void *) == NULL); |
| 104 | |
| 105 | /* |
| 106 | * The xfs_buf_log_item pointer is stored in b_fsprivate. If |
| 107 | * it doesn't have one yet, then allocate one and initialize it. |
| 108 | * The checks to see if one is there are in xfs_buf_item_init(). |
| 109 | */ |
| 110 | xfs_buf_item_init(bp, tp->t_mountp); |
| 111 | bip = XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t *); |
| 112 | ASSERT(!(bip->bli_flags & XFS_BLI_STALE)); |
Dave Chinner | c115541 | 2010-05-07 11:05:19 +1000 | [diff] [blame] | 113 | ASSERT(!(bip->bli_format.blf_flags & XFS_BLF_CANCEL)); |
Christoph Hellwig | d7e84f4 | 2010-02-15 23:35:09 +0000 | [diff] [blame] | 114 | ASSERT(!(bip->bli_flags & XFS_BLI_LOGGED)); |
| 115 | if (reset_recur) |
| 116 | bip->bli_recur = 0; |
| 117 | |
| 118 | /* |
| 119 | * Take a reference for this transaction on the buf item. |
| 120 | */ |
| 121 | atomic_inc(&bip->bli_refcount); |
| 122 | |
| 123 | /* |
| 124 | * Get a log_item_desc to point at the new item. |
| 125 | */ |
| 126 | (void) xfs_trans_add_item(tp, (xfs_log_item_t *)bip); |
| 127 | |
| 128 | /* |
| 129 | * Initialize b_fsprivate2 so we can find it with incore_match() |
| 130 | * in xfs_trans_get_buf() and friends above. |
| 131 | */ |
| 132 | XFS_BUF_SET_FSPRIVATE2(bp, tp); |
| 133 | |
| 134 | } |
| 135 | |
| 136 | void |
| 137 | xfs_trans_bjoin( |
| 138 | struct xfs_trans *tp, |
| 139 | struct xfs_buf *bp) |
| 140 | { |
| 141 | _xfs_trans_bjoin(tp, bp, 0); |
| 142 | trace_xfs_trans_bjoin(bp->b_fspriv); |
| 143 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | |
| 145 | /* |
| 146 | * Get and lock the buffer for the caller if it is not already |
| 147 | * locked within the given transaction. If it is already locked |
| 148 | * within the transaction, just increment its lock recursion count |
| 149 | * and return a pointer to it. |
| 150 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | * If the transaction pointer is NULL, make this just a normal |
| 152 | * get_buf() call. |
| 153 | */ |
| 154 | xfs_buf_t * |
| 155 | xfs_trans_get_buf(xfs_trans_t *tp, |
| 156 | xfs_buftarg_t *target_dev, |
| 157 | xfs_daddr_t blkno, |
| 158 | int len, |
| 159 | uint flags) |
| 160 | { |
| 161 | xfs_buf_t *bp; |
| 162 | xfs_buf_log_item_t *bip; |
| 163 | |
| 164 | if (flags == 0) |
Christoph Hellwig | 0cadda1 | 2010-01-19 09:56:44 +0000 | [diff] [blame] | 165 | flags = XBF_LOCK | XBF_MAPPED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | |
| 167 | /* |
| 168 | * Default to a normal get_buf() call if the tp is NULL. |
| 169 | */ |
Christoph Hellwig | 6ad112b | 2009-11-24 18:02:23 +0000 | [diff] [blame] | 170 | if (tp == NULL) |
Christoph Hellwig | 0cadda1 | 2010-01-19 09:56:44 +0000 | [diff] [blame] | 171 | return xfs_buf_get(target_dev, blkno, len, |
| 172 | flags | XBF_DONT_BLOCK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | |
| 174 | /* |
| 175 | * If we find the buffer in the cache with this transaction |
| 176 | * pointer in its b_fsprivate2 field, then we know we already |
| 177 | * have it locked. In this case we just increment the lock |
| 178 | * recursion count and return the buffer to the caller. |
| 179 | */ |
Christoph Hellwig | 4a5224d | 2010-04-18 00:10:45 +0000 | [diff] [blame] | 180 | bp = xfs_trans_buf_item_match(tp, target_dev, blkno, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | if (bp != NULL) { |
| 182 | ASSERT(XFS_BUF_VALUSEMA(bp) <= 0); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 183 | if (XFS_FORCED_SHUTDOWN(tp->t_mountp)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | XFS_BUF_SUPER_STALE(bp); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 185 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | /* |
| 187 | * If the buffer is stale then it was binval'ed |
| 188 | * since last read. This doesn't matter since the |
| 189 | * caller isn't allowed to use the data anyway. |
| 190 | */ |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 191 | else if (XFS_BUF_ISSTALE(bp)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | ASSERT(!XFS_BUF_ISDELAYWRITE(bp)); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 193 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | ASSERT(XFS_BUF_FSPRIVATE2(bp, xfs_trans_t *) == tp); |
| 195 | bip = XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t *); |
| 196 | ASSERT(bip != NULL); |
| 197 | ASSERT(atomic_read(&bip->bli_refcount) > 0); |
| 198 | bip->bli_recur++; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 199 | trace_xfs_trans_get_buf_recur(bip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | return (bp); |
| 201 | } |
| 202 | |
| 203 | /* |
Christoph Hellwig | 0cadda1 | 2010-01-19 09:56:44 +0000 | [diff] [blame] | 204 | * We always specify the XBF_DONT_BLOCK flag within a transaction |
| 205 | * so that get_buf does not try to push out a delayed write buffer |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | * which might cause another transaction to take place (if the |
| 207 | * buffer was delayed alloc). Such recursive transactions can |
| 208 | * easily deadlock with our current transaction as well as cause |
| 209 | * us to run out of stack space. |
| 210 | */ |
Christoph Hellwig | 0cadda1 | 2010-01-19 09:56:44 +0000 | [diff] [blame] | 211 | bp = xfs_buf_get(target_dev, blkno, len, flags | XBF_DONT_BLOCK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | if (bp == NULL) { |
| 213 | return NULL; |
| 214 | } |
| 215 | |
| 216 | ASSERT(!XFS_BUF_GETERROR(bp)); |
| 217 | |
Christoph Hellwig | d7e84f4 | 2010-02-15 23:35:09 +0000 | [diff] [blame] | 218 | _xfs_trans_bjoin(tp, bp, 1); |
| 219 | trace_xfs_trans_get_buf(bp->b_fspriv); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | return (bp); |
| 221 | } |
| 222 | |
| 223 | /* |
| 224 | * Get and lock the superblock buffer of this file system for the |
| 225 | * given transaction. |
| 226 | * |
| 227 | * We don't need to use incore_match() here, because the superblock |
| 228 | * buffer is a private buffer which we keep a pointer to in the |
| 229 | * mount structure. |
| 230 | */ |
| 231 | xfs_buf_t * |
| 232 | xfs_trans_getsb(xfs_trans_t *tp, |
| 233 | struct xfs_mount *mp, |
| 234 | int flags) |
| 235 | { |
| 236 | xfs_buf_t *bp; |
| 237 | xfs_buf_log_item_t *bip; |
| 238 | |
| 239 | /* |
| 240 | * Default to just trying to lock the superblock buffer |
| 241 | * if tp is NULL. |
| 242 | */ |
| 243 | if (tp == NULL) { |
| 244 | return (xfs_getsb(mp, flags)); |
| 245 | } |
| 246 | |
| 247 | /* |
| 248 | * If the superblock buffer already has this transaction |
| 249 | * pointer in its b_fsprivate2 field, then we know we already |
| 250 | * have it locked. In this case we just increment the lock |
| 251 | * recursion count and return the buffer to the caller. |
| 252 | */ |
| 253 | bp = mp->m_sb_bp; |
| 254 | if (XFS_BUF_FSPRIVATE2(bp, xfs_trans_t *) == tp) { |
| 255 | bip = XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t*); |
| 256 | ASSERT(bip != NULL); |
| 257 | ASSERT(atomic_read(&bip->bli_refcount) > 0); |
| 258 | bip->bli_recur++; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 259 | trace_xfs_trans_getsb_recur(bip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | return (bp); |
| 261 | } |
| 262 | |
| 263 | bp = xfs_getsb(mp, flags); |
Christoph Hellwig | d7e84f4 | 2010-02-15 23:35:09 +0000 | [diff] [blame] | 264 | if (bp == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | |
Christoph Hellwig | d7e84f4 | 2010-02-15 23:35:09 +0000 | [diff] [blame] | 267 | _xfs_trans_bjoin(tp, bp, 1); |
| 268 | trace_xfs_trans_getsb(bp->b_fspriv); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | return (bp); |
| 270 | } |
| 271 | |
| 272 | #ifdef DEBUG |
| 273 | xfs_buftarg_t *xfs_error_target; |
| 274 | int xfs_do_error; |
| 275 | int xfs_req_num; |
| 276 | int xfs_error_mod = 33; |
| 277 | #endif |
| 278 | |
| 279 | /* |
| 280 | * Get and lock the buffer for the caller if it is not already |
| 281 | * locked within the given transaction. If it has not yet been |
| 282 | * read in, read it from disk. If it is already locked |
| 283 | * within the transaction and already read in, just increment its |
| 284 | * lock recursion count and return a pointer to it. |
| 285 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | * If the transaction pointer is NULL, make this just a normal |
| 287 | * read_buf() call. |
| 288 | */ |
| 289 | int |
| 290 | xfs_trans_read_buf( |
| 291 | xfs_mount_t *mp, |
| 292 | xfs_trans_t *tp, |
| 293 | xfs_buftarg_t *target, |
| 294 | xfs_daddr_t blkno, |
| 295 | int len, |
| 296 | uint flags, |
| 297 | xfs_buf_t **bpp) |
| 298 | { |
| 299 | xfs_buf_t *bp; |
| 300 | xfs_buf_log_item_t *bip; |
| 301 | int error; |
| 302 | |
| 303 | if (flags == 0) |
Christoph Hellwig | 0cadda1 | 2010-01-19 09:56:44 +0000 | [diff] [blame] | 304 | flags = XBF_LOCK | XBF_MAPPED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | |
| 306 | /* |
| 307 | * Default to a normal get_buf() call if the tp is NULL. |
| 308 | */ |
| 309 | if (tp == NULL) { |
Christoph Hellwig | 0cadda1 | 2010-01-19 09:56:44 +0000 | [diff] [blame] | 310 | bp = xfs_buf_read(target, blkno, len, flags | XBF_DONT_BLOCK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | if (!bp) |
Christoph Hellwig | 0cadda1 | 2010-01-19 09:56:44 +0000 | [diff] [blame] | 312 | return (flags & XBF_TRYLOCK) ? |
David Chinner | a3f74ff | 2008-03-06 13:43:42 +1100 | [diff] [blame] | 313 | EAGAIN : XFS_ERROR(ENOMEM); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | |
Julia Lawall | a0f7bfd | 2009-07-27 18:15:25 +0200 | [diff] [blame] | 315 | if (XFS_BUF_GETERROR(bp) != 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | xfs_ioerror_alert("xfs_trans_read_buf", mp, |
| 317 | bp, blkno); |
| 318 | error = XFS_BUF_GETERROR(bp); |
| 319 | xfs_buf_relse(bp); |
| 320 | return error; |
| 321 | } |
| 322 | #ifdef DEBUG |
Julia Lawall | a0f7bfd | 2009-07-27 18:15:25 +0200 | [diff] [blame] | 323 | if (xfs_do_error) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | if (xfs_error_target == target) { |
| 325 | if (((xfs_req_num++) % xfs_error_mod) == 0) { |
| 326 | xfs_buf_relse(bp); |
Nathan Scott | b657452 | 2006-06-09 15:29:40 +1000 | [diff] [blame] | 327 | cmn_err(CE_DEBUG, "Returning error!\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | return XFS_ERROR(EIO); |
| 329 | } |
| 330 | } |
| 331 | } |
| 332 | #endif |
| 333 | if (XFS_FORCED_SHUTDOWN(mp)) |
| 334 | goto shutdown_abort; |
| 335 | *bpp = bp; |
| 336 | return 0; |
| 337 | } |
| 338 | |
| 339 | /* |
| 340 | * If we find the buffer in the cache with this transaction |
| 341 | * pointer in its b_fsprivate2 field, then we know we already |
| 342 | * have it locked. If it is already read in we just increment |
| 343 | * the lock recursion count and return the buffer to the caller. |
| 344 | * If the buffer is not yet read in, then we read it in, increment |
| 345 | * the lock recursion count, and return it to the caller. |
| 346 | */ |
Christoph Hellwig | 4a5224d | 2010-04-18 00:10:45 +0000 | [diff] [blame] | 347 | bp = xfs_trans_buf_item_match(tp, target, blkno, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | if (bp != NULL) { |
| 349 | ASSERT(XFS_BUF_VALUSEMA(bp) <= 0); |
| 350 | ASSERT(XFS_BUF_FSPRIVATE2(bp, xfs_trans_t *) == tp); |
| 351 | ASSERT(XFS_BUF_FSPRIVATE(bp, void *) != NULL); |
| 352 | ASSERT((XFS_BUF_ISERROR(bp)) == 0); |
| 353 | if (!(XFS_BUF_ISDONE(bp))) { |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 354 | trace_xfs_trans_read_buf_io(bp, _RET_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | ASSERT(!XFS_BUF_ISASYNC(bp)); |
| 356 | XFS_BUF_READ(bp); |
| 357 | xfsbdstrat(tp->t_mountp, bp); |
David Chinner | d64e31a | 2008-04-10 12:22:17 +1000 | [diff] [blame] | 358 | error = xfs_iowait(bp); |
| 359 | if (error) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | xfs_ioerror_alert("xfs_trans_read_buf", mp, |
| 361 | bp, blkno); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | xfs_buf_relse(bp); |
| 363 | /* |
David Chinner | d64e31a | 2008-04-10 12:22:17 +1000 | [diff] [blame] | 364 | * We can gracefully recover from most read |
| 365 | * errors. Ones we can't are those that happen |
| 366 | * after the transaction's already dirty. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | */ |
| 368 | if (tp->t_flags & XFS_TRANS_DIRTY) |
| 369 | xfs_force_shutdown(tp->t_mountp, |
Nathan Scott | 7d04a33 | 2006-06-09 14:58:38 +1000 | [diff] [blame] | 370 | SHUTDOWN_META_IO_ERROR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | return error; |
| 372 | } |
| 373 | } |
| 374 | /* |
| 375 | * We never locked this buf ourselves, so we shouldn't |
| 376 | * brelse it either. Just get out. |
| 377 | */ |
| 378 | if (XFS_FORCED_SHUTDOWN(mp)) { |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 379 | trace_xfs_trans_read_buf_shut(bp, _RET_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | *bpp = NULL; |
| 381 | return XFS_ERROR(EIO); |
| 382 | } |
| 383 | |
| 384 | |
| 385 | bip = XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t*); |
| 386 | bip->bli_recur++; |
| 387 | |
| 388 | ASSERT(atomic_read(&bip->bli_refcount) > 0); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 389 | trace_xfs_trans_read_buf_recur(bip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | *bpp = bp; |
| 391 | return 0; |
| 392 | } |
| 393 | |
| 394 | /* |
Christoph Hellwig | 0cadda1 | 2010-01-19 09:56:44 +0000 | [diff] [blame] | 395 | * We always specify the XBF_DONT_BLOCK flag within a transaction |
| 396 | * so that get_buf does not try to push out a delayed write buffer |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | * which might cause another transaction to take place (if the |
| 398 | * buffer was delayed alloc). Such recursive transactions can |
| 399 | * easily deadlock with our current transaction as well as cause |
| 400 | * us to run out of stack space. |
| 401 | */ |
Christoph Hellwig | 0cadda1 | 2010-01-19 09:56:44 +0000 | [diff] [blame] | 402 | bp = xfs_buf_read(target, blkno, len, flags | XBF_DONT_BLOCK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | if (bp == NULL) { |
| 404 | *bpp = NULL; |
| 405 | return 0; |
| 406 | } |
| 407 | if (XFS_BUF_GETERROR(bp) != 0) { |
| 408 | XFS_BUF_SUPER_STALE(bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | error = XFS_BUF_GETERROR(bp); |
| 410 | |
| 411 | xfs_ioerror_alert("xfs_trans_read_buf", mp, |
| 412 | bp, blkno); |
| 413 | if (tp->t_flags & XFS_TRANS_DIRTY) |
Nathan Scott | 7d04a33 | 2006-06-09 14:58:38 +1000 | [diff] [blame] | 414 | xfs_force_shutdown(tp->t_mountp, SHUTDOWN_META_IO_ERROR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | xfs_buf_relse(bp); |
| 416 | return error; |
| 417 | } |
| 418 | #ifdef DEBUG |
| 419 | if (xfs_do_error && !(tp->t_flags & XFS_TRANS_DIRTY)) { |
| 420 | if (xfs_error_target == target) { |
| 421 | if (((xfs_req_num++) % xfs_error_mod) == 0) { |
| 422 | xfs_force_shutdown(tp->t_mountp, |
Nathan Scott | 7d04a33 | 2006-06-09 14:58:38 +1000 | [diff] [blame] | 423 | SHUTDOWN_META_IO_ERROR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | xfs_buf_relse(bp); |
Nathan Scott | b657452 | 2006-06-09 15:29:40 +1000 | [diff] [blame] | 425 | cmn_err(CE_DEBUG, "Returning trans error!\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | return XFS_ERROR(EIO); |
| 427 | } |
| 428 | } |
| 429 | } |
| 430 | #endif |
| 431 | if (XFS_FORCED_SHUTDOWN(mp)) |
| 432 | goto shutdown_abort; |
| 433 | |
Christoph Hellwig | d7e84f4 | 2010-02-15 23:35:09 +0000 | [diff] [blame] | 434 | _xfs_trans_bjoin(tp, bp, 1); |
| 435 | trace_xfs_trans_read_buf(bp->b_fspriv); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 437 | *bpp = bp; |
| 438 | return 0; |
| 439 | |
| 440 | shutdown_abort: |
| 441 | /* |
| 442 | * the theory here is that buffer is good but we're |
| 443 | * bailing out because the filesystem is being forcibly |
| 444 | * shut down. So we should leave the b_flags alone since |
| 445 | * the buffer's not staled and just get out. |
| 446 | */ |
| 447 | #if defined(DEBUG) |
| 448 | if (XFS_BUF_ISSTALE(bp) && XFS_BUF_ISDELAYWRITE(bp)) |
| 449 | cmn_err(CE_NOTE, "about to pop assert, bp == 0x%p", bp); |
| 450 | #endif |
Christoph Hellwig | 0cadda1 | 2010-01-19 09:56:44 +0000 | [diff] [blame] | 451 | ASSERT((XFS_BUF_BFLAGS(bp) & (XBF_STALE|XBF_DELWRI)) != |
| 452 | (XBF_STALE|XBF_DELWRI)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 453 | |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 454 | trace_xfs_trans_read_buf_shut(bp, _RET_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | xfs_buf_relse(bp); |
| 456 | *bpp = NULL; |
| 457 | return XFS_ERROR(EIO); |
| 458 | } |
| 459 | |
| 460 | |
| 461 | /* |
| 462 | * Release the buffer bp which was previously acquired with one of the |
| 463 | * xfs_trans_... buffer allocation routines if the buffer has not |
| 464 | * been modified within this transaction. If the buffer is modified |
| 465 | * within this transaction, do decrement the recursion count but do |
| 466 | * not release the buffer even if the count goes to 0. If the buffer is not |
| 467 | * modified within the transaction, decrement the recursion count and |
| 468 | * release the buffer if the recursion count goes to 0. |
| 469 | * |
| 470 | * If the buffer is to be released and it was not modified before |
| 471 | * this transaction began, then free the buf_log_item associated with it. |
| 472 | * |
| 473 | * If the transaction pointer is NULL, make this just a normal |
| 474 | * brelse() call. |
| 475 | */ |
| 476 | void |
| 477 | xfs_trans_brelse(xfs_trans_t *tp, |
| 478 | xfs_buf_t *bp) |
| 479 | { |
| 480 | xfs_buf_log_item_t *bip; |
| 481 | xfs_log_item_t *lip; |
| 482 | xfs_log_item_desc_t *lidp; |
| 483 | |
| 484 | /* |
| 485 | * Default to a normal brelse() call if the tp is NULL. |
| 486 | */ |
| 487 | if (tp == NULL) { |
| 488 | ASSERT(XFS_BUF_FSPRIVATE2(bp, void *) == NULL); |
| 489 | /* |
| 490 | * If there's a buf log item attached to the buffer, |
| 491 | * then let the AIL know that the buffer is being |
| 492 | * unlocked. |
| 493 | */ |
| 494 | if (XFS_BUF_FSPRIVATE(bp, void *) != NULL) { |
| 495 | lip = XFS_BUF_FSPRIVATE(bp, xfs_log_item_t *); |
| 496 | if (lip->li_type == XFS_LI_BUF) { |
| 497 | bip = XFS_BUF_FSPRIVATE(bp,xfs_buf_log_item_t*); |
David Chinner | 783a2f6 | 2008-10-30 17:39:58 +1100 | [diff] [blame] | 498 | xfs_trans_unlocked_item(bip->bli_item.li_ailp, |
| 499 | lip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | } |
| 501 | } |
| 502 | xfs_buf_relse(bp); |
| 503 | return; |
| 504 | } |
| 505 | |
| 506 | ASSERT(XFS_BUF_FSPRIVATE2(bp, xfs_trans_t *) == tp); |
| 507 | bip = XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t *); |
| 508 | ASSERT(bip->bli_item.li_type == XFS_LI_BUF); |
| 509 | ASSERT(!(bip->bli_flags & XFS_BLI_STALE)); |
Dave Chinner | c115541 | 2010-05-07 11:05:19 +1000 | [diff] [blame] | 510 | ASSERT(!(bip->bli_format.blf_flags & XFS_BLF_CANCEL)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 511 | ASSERT(atomic_read(&bip->bli_refcount) > 0); |
| 512 | |
| 513 | /* |
| 514 | * Find the item descriptor pointing to this buffer's |
| 515 | * log item. It must be there. |
| 516 | */ |
| 517 | lidp = xfs_trans_find_item(tp, (xfs_log_item_t*)bip); |
| 518 | ASSERT(lidp != NULL); |
| 519 | |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 520 | trace_xfs_trans_brelse(bip); |
| 521 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | /* |
| 523 | * If the release is just for a recursive lock, |
| 524 | * then decrement the count and return. |
| 525 | */ |
| 526 | if (bip->bli_recur > 0) { |
| 527 | bip->bli_recur--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 528 | return; |
| 529 | } |
| 530 | |
| 531 | /* |
| 532 | * If the buffer is dirty within this transaction, we can't |
| 533 | * release it until we commit. |
| 534 | */ |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 535 | if (lidp->lid_flags & XFS_LID_DIRTY) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 536 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 537 | |
| 538 | /* |
| 539 | * If the buffer has been invalidated, then we can't release |
| 540 | * it until the transaction commits to disk unless it is re-dirtied |
| 541 | * as part of this transaction. This prevents us from pulling |
| 542 | * the item from the AIL before we should. |
| 543 | */ |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 544 | if (bip->bli_flags & XFS_BLI_STALE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 545 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 546 | |
| 547 | ASSERT(!(bip->bli_flags & XFS_BLI_LOGGED)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 548 | |
| 549 | /* |
| 550 | * Free up the log item descriptor tracking the released item. |
| 551 | */ |
| 552 | xfs_trans_free_item(tp, lidp); |
| 553 | |
| 554 | /* |
| 555 | * Clear the hold flag in the buf log item if it is set. |
| 556 | * We wouldn't want the next user of the buffer to |
| 557 | * get confused. |
| 558 | */ |
| 559 | if (bip->bli_flags & XFS_BLI_HOLD) { |
| 560 | bip->bli_flags &= ~XFS_BLI_HOLD; |
| 561 | } |
| 562 | |
| 563 | /* |
| 564 | * Drop our reference to the buf log item. |
| 565 | */ |
| 566 | atomic_dec(&bip->bli_refcount); |
| 567 | |
| 568 | /* |
| 569 | * If the buf item is not tracking data in the log, then |
| 570 | * we must free it before releasing the buffer back to the |
| 571 | * free pool. Before releasing the buffer to the free pool, |
| 572 | * clear the transaction pointer in b_fsprivate2 to dissolve |
| 573 | * its relation to this transaction. |
| 574 | */ |
| 575 | if (!xfs_buf_item_dirty(bip)) { |
| 576 | /*** |
| 577 | ASSERT(bp->b_pincount == 0); |
| 578 | ***/ |
| 579 | ASSERT(atomic_read(&bip->bli_refcount) == 0); |
| 580 | ASSERT(!(bip->bli_item.li_flags & XFS_LI_IN_AIL)); |
| 581 | ASSERT(!(bip->bli_flags & XFS_BLI_INODE_ALLOC_BUF)); |
| 582 | xfs_buf_item_relse(bp); |
| 583 | bip = NULL; |
| 584 | } |
| 585 | XFS_BUF_SET_FSPRIVATE2(bp, NULL); |
| 586 | |
| 587 | /* |
| 588 | * If we've still got a buf log item on the buffer, then |
| 589 | * tell the AIL that the buffer is being unlocked. |
| 590 | */ |
| 591 | if (bip != NULL) { |
David Chinner | 783a2f6 | 2008-10-30 17:39:58 +1100 | [diff] [blame] | 592 | xfs_trans_unlocked_item(bip->bli_item.li_ailp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 593 | (xfs_log_item_t*)bip); |
| 594 | } |
| 595 | |
| 596 | xfs_buf_relse(bp); |
| 597 | return; |
| 598 | } |
| 599 | |
| 600 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 601 | * Mark the buffer as not needing to be unlocked when the buf item's |
| 602 | * IOP_UNLOCK() routine is called. The buffer must already be locked |
| 603 | * and associated with the given transaction. |
| 604 | */ |
| 605 | /* ARGSUSED */ |
| 606 | void |
| 607 | xfs_trans_bhold(xfs_trans_t *tp, |
| 608 | xfs_buf_t *bp) |
| 609 | { |
| 610 | xfs_buf_log_item_t *bip; |
| 611 | |
| 612 | ASSERT(XFS_BUF_ISBUSY(bp)); |
| 613 | ASSERT(XFS_BUF_FSPRIVATE2(bp, xfs_trans_t *) == tp); |
| 614 | ASSERT(XFS_BUF_FSPRIVATE(bp, void *) != NULL); |
| 615 | |
| 616 | bip = XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t *); |
| 617 | ASSERT(!(bip->bli_flags & XFS_BLI_STALE)); |
Dave Chinner | c115541 | 2010-05-07 11:05:19 +1000 | [diff] [blame] | 618 | ASSERT(!(bip->bli_format.blf_flags & XFS_BLF_CANCEL)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 619 | ASSERT(atomic_read(&bip->bli_refcount) > 0); |
| 620 | bip->bli_flags |= XFS_BLI_HOLD; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 621 | trace_xfs_trans_bhold(bip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 622 | } |
| 623 | |
| 624 | /* |
Tim Shimmin | efa092f | 2005-09-05 08:29:01 +1000 | [diff] [blame] | 625 | * Cancel the previous buffer hold request made on this buffer |
| 626 | * for this transaction. |
| 627 | */ |
| 628 | void |
| 629 | xfs_trans_bhold_release(xfs_trans_t *tp, |
| 630 | xfs_buf_t *bp) |
| 631 | { |
| 632 | xfs_buf_log_item_t *bip; |
| 633 | |
| 634 | ASSERT(XFS_BUF_ISBUSY(bp)); |
| 635 | ASSERT(XFS_BUF_FSPRIVATE2(bp, xfs_trans_t *) == tp); |
| 636 | ASSERT(XFS_BUF_FSPRIVATE(bp, void *) != NULL); |
| 637 | |
| 638 | bip = XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t *); |
| 639 | ASSERT(!(bip->bli_flags & XFS_BLI_STALE)); |
Dave Chinner | c115541 | 2010-05-07 11:05:19 +1000 | [diff] [blame] | 640 | ASSERT(!(bip->bli_format.blf_flags & XFS_BLF_CANCEL)); |
Tim Shimmin | efa092f | 2005-09-05 08:29:01 +1000 | [diff] [blame] | 641 | ASSERT(atomic_read(&bip->bli_refcount) > 0); |
| 642 | ASSERT(bip->bli_flags & XFS_BLI_HOLD); |
| 643 | bip->bli_flags &= ~XFS_BLI_HOLD; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 644 | |
| 645 | trace_xfs_trans_bhold_release(bip); |
Tim Shimmin | efa092f | 2005-09-05 08:29:01 +1000 | [diff] [blame] | 646 | } |
| 647 | |
| 648 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 649 | * This is called to mark bytes first through last inclusive of the given |
| 650 | * buffer as needing to be logged when the transaction is committed. |
| 651 | * The buffer must already be associated with the given transaction. |
| 652 | * |
| 653 | * First and last are numbers relative to the beginning of this buffer, |
| 654 | * so the first byte in the buffer is numbered 0 regardless of the |
| 655 | * value of b_blkno. |
| 656 | */ |
| 657 | void |
| 658 | xfs_trans_log_buf(xfs_trans_t *tp, |
| 659 | xfs_buf_t *bp, |
| 660 | uint first, |
| 661 | uint last) |
| 662 | { |
| 663 | xfs_buf_log_item_t *bip; |
| 664 | xfs_log_item_desc_t *lidp; |
| 665 | |
| 666 | ASSERT(XFS_BUF_ISBUSY(bp)); |
| 667 | ASSERT(XFS_BUF_FSPRIVATE2(bp, xfs_trans_t *) == tp); |
| 668 | ASSERT(XFS_BUF_FSPRIVATE(bp, void *) != NULL); |
| 669 | ASSERT((first <= last) && (last < XFS_BUF_COUNT(bp))); |
| 670 | ASSERT((XFS_BUF_IODONE_FUNC(bp) == NULL) || |
| 671 | (XFS_BUF_IODONE_FUNC(bp) == xfs_buf_iodone_callbacks)); |
| 672 | |
| 673 | /* |
| 674 | * Mark the buffer as needing to be written out eventually, |
| 675 | * and set its iodone function to remove the buffer's buf log |
| 676 | * item from the AIL and free it when the buffer is flushed |
| 677 | * to disk. See xfs_buf_attach_iodone() for more details |
| 678 | * on li_cb and xfs_buf_iodone_callbacks(). |
| 679 | * If we end up aborting this transaction, we trap this buffer |
| 680 | * inside the b_bdstrat callback so that this won't get written to |
| 681 | * disk. |
| 682 | */ |
| 683 | XFS_BUF_DELAYWRITE(bp); |
| 684 | XFS_BUF_DONE(bp); |
| 685 | |
| 686 | bip = XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t *); |
| 687 | ASSERT(atomic_read(&bip->bli_refcount) > 0); |
| 688 | XFS_BUF_SET_IODONE_FUNC(bp, xfs_buf_iodone_callbacks); |
| 689 | bip->bli_item.li_cb = (void(*)(xfs_buf_t*,xfs_log_item_t*))xfs_buf_iodone; |
| 690 | |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 691 | trace_xfs_trans_log_buf(bip); |
| 692 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 693 | /* |
| 694 | * If we invalidated the buffer within this transaction, then |
| 695 | * cancel the invalidation now that we're dirtying the buffer |
| 696 | * again. There are no races with the code in xfs_buf_item_unpin(), |
| 697 | * because we have a reference to the buffer this entire time. |
| 698 | */ |
| 699 | if (bip->bli_flags & XFS_BLI_STALE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 700 | bip->bli_flags &= ~XFS_BLI_STALE; |
| 701 | ASSERT(XFS_BUF_ISSTALE(bp)); |
| 702 | XFS_BUF_UNSTALE(bp); |
Dave Chinner | c115541 | 2010-05-07 11:05:19 +1000 | [diff] [blame] | 703 | bip->bli_format.blf_flags &= ~XFS_BLF_CANCEL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 704 | } |
| 705 | |
| 706 | lidp = xfs_trans_find_item(tp, (xfs_log_item_t*)bip); |
| 707 | ASSERT(lidp != NULL); |
| 708 | |
| 709 | tp->t_flags |= XFS_TRANS_DIRTY; |
| 710 | lidp->lid_flags |= XFS_LID_DIRTY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 711 | bip->bli_flags |= XFS_BLI_LOGGED; |
| 712 | xfs_buf_item_log(bip, first, last); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 713 | } |
| 714 | |
| 715 | |
| 716 | /* |
| 717 | * This called to invalidate a buffer that is being used within |
| 718 | * a transaction. Typically this is because the blocks in the |
| 719 | * buffer are being freed, so we need to prevent it from being |
| 720 | * written out when we're done. Allowing it to be written again |
| 721 | * might overwrite data in the free blocks if they are reallocated |
| 722 | * to a file. |
| 723 | * |
| 724 | * We prevent the buffer from being written out by clearing the |
| 725 | * B_DELWRI flag. We can't always |
| 726 | * get rid of the buf log item at this point, though, because |
| 727 | * the buffer may still be pinned by another transaction. If that |
| 728 | * is the case, then we'll wait until the buffer is committed to |
| 729 | * disk for the last time (we can tell by the ref count) and |
| 730 | * free it in xfs_buf_item_unpin(). Until it is cleaned up we |
| 731 | * will keep the buffer locked so that the buffer and buf log item |
| 732 | * are not reused. |
| 733 | */ |
| 734 | void |
| 735 | xfs_trans_binval( |
| 736 | xfs_trans_t *tp, |
| 737 | xfs_buf_t *bp) |
| 738 | { |
| 739 | xfs_log_item_desc_t *lidp; |
| 740 | xfs_buf_log_item_t *bip; |
| 741 | |
| 742 | ASSERT(XFS_BUF_ISBUSY(bp)); |
| 743 | ASSERT(XFS_BUF_FSPRIVATE2(bp, xfs_trans_t *) == tp); |
| 744 | ASSERT(XFS_BUF_FSPRIVATE(bp, void *) != NULL); |
| 745 | |
| 746 | bip = XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t *); |
| 747 | lidp = xfs_trans_find_item(tp, (xfs_log_item_t*)bip); |
| 748 | ASSERT(lidp != NULL); |
| 749 | ASSERT(atomic_read(&bip->bli_refcount) > 0); |
| 750 | |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 751 | trace_xfs_trans_binval(bip); |
| 752 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 753 | if (bip->bli_flags & XFS_BLI_STALE) { |
| 754 | /* |
| 755 | * If the buffer is already invalidated, then |
| 756 | * just return. |
| 757 | */ |
| 758 | ASSERT(!(XFS_BUF_ISDELAYWRITE(bp))); |
| 759 | ASSERT(XFS_BUF_ISSTALE(bp)); |
| 760 | ASSERT(!(bip->bli_flags & (XFS_BLI_LOGGED | XFS_BLI_DIRTY))); |
Dave Chinner | c115541 | 2010-05-07 11:05:19 +1000 | [diff] [blame] | 761 | ASSERT(!(bip->bli_format.blf_flags & XFS_BLF_INODE_BUF)); |
| 762 | ASSERT(bip->bli_format.blf_flags & XFS_BLF_CANCEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 763 | ASSERT(lidp->lid_flags & XFS_LID_DIRTY); |
| 764 | ASSERT(tp->t_flags & XFS_TRANS_DIRTY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 765 | return; |
| 766 | } |
| 767 | |
| 768 | /* |
| 769 | * Clear the dirty bit in the buffer and set the STALE flag |
| 770 | * in the buf log item. The STALE flag will be used in |
| 771 | * xfs_buf_item_unpin() to determine if it should clean up |
| 772 | * when the last reference to the buf item is given up. |
Dave Chinner | c115541 | 2010-05-07 11:05:19 +1000 | [diff] [blame] | 773 | * We set the XFS_BLF_CANCEL flag in the buf log format structure |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 774 | * and log the buf item. This will be used at recovery time |
| 775 | * to determine that copies of the buffer in the log before |
| 776 | * this should not be replayed. |
| 777 | * We mark the item descriptor and the transaction dirty so |
| 778 | * that we'll hold the buffer until after the commit. |
| 779 | * |
| 780 | * Since we're invalidating the buffer, we also clear the state |
| 781 | * about which parts of the buffer have been logged. We also |
| 782 | * clear the flag indicating that this is an inode buffer since |
| 783 | * the data in the buffer will no longer be valid. |
| 784 | * |
| 785 | * We set the stale bit in the buffer as well since we're getting |
| 786 | * rid of it. |
| 787 | */ |
| 788 | XFS_BUF_UNDELAYWRITE(bp); |
| 789 | XFS_BUF_STALE(bp); |
| 790 | bip->bli_flags |= XFS_BLI_STALE; |
Dave Chinner | ccf7c23 | 2010-05-20 23:19:42 +1000 | [diff] [blame] | 791 | bip->bli_flags &= ~(XFS_BLI_INODE_BUF | XFS_BLI_LOGGED | XFS_BLI_DIRTY); |
Dave Chinner | c115541 | 2010-05-07 11:05:19 +1000 | [diff] [blame] | 792 | bip->bli_format.blf_flags &= ~XFS_BLF_INODE_BUF; |
| 793 | bip->bli_format.blf_flags |= XFS_BLF_CANCEL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 794 | memset((char *)(bip->bli_format.blf_data_map), 0, |
| 795 | (bip->bli_format.blf_map_size * sizeof(uint))); |
Dave Chinner | 8e12385 | 2010-03-08 11:26:03 +1100 | [diff] [blame] | 796 | lidp->lid_flags |= XFS_LID_DIRTY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 797 | tp->t_flags |= XFS_TRANS_DIRTY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 798 | } |
| 799 | |
| 800 | /* |
Dave Chinner | ccf7c23 | 2010-05-20 23:19:42 +1000 | [diff] [blame] | 801 | * This call is used to indicate that the buffer contains on-disk inodes which |
| 802 | * must be handled specially during recovery. They require special handling |
| 803 | * because only the di_next_unlinked from the inodes in the buffer should be |
| 804 | * recovered. The rest of the data in the buffer is logged via the inodes |
| 805 | * themselves. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 806 | * |
Dave Chinner | ccf7c23 | 2010-05-20 23:19:42 +1000 | [diff] [blame] | 807 | * All we do is set the XFS_BLI_INODE_BUF flag in the items flags so it can be |
| 808 | * transferred to the buffer's log format structure so that we'll know what to |
| 809 | * do at recovery time. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 810 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 811 | void |
| 812 | xfs_trans_inode_buf( |
| 813 | xfs_trans_t *tp, |
| 814 | xfs_buf_t *bp) |
| 815 | { |
| 816 | xfs_buf_log_item_t *bip; |
| 817 | |
| 818 | ASSERT(XFS_BUF_ISBUSY(bp)); |
| 819 | ASSERT(XFS_BUF_FSPRIVATE2(bp, xfs_trans_t *) == tp); |
| 820 | ASSERT(XFS_BUF_FSPRIVATE(bp, void *) != NULL); |
| 821 | |
| 822 | bip = XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t *); |
| 823 | ASSERT(atomic_read(&bip->bli_refcount) > 0); |
| 824 | |
Dave Chinner | ccf7c23 | 2010-05-20 23:19:42 +1000 | [diff] [blame] | 825 | bip->bli_flags |= XFS_BLI_INODE_BUF; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 826 | } |
| 827 | |
| 828 | /* |
| 829 | * This call is used to indicate that the buffer is going to |
| 830 | * be staled and was an inode buffer. This means it gets |
| 831 | * special processing during unpin - where any inodes |
| 832 | * associated with the buffer should be removed from ail. |
| 833 | * There is also special processing during recovery, |
| 834 | * any replay of the inodes in the buffer needs to be |
| 835 | * prevented as the buffer may have been reused. |
| 836 | */ |
| 837 | void |
| 838 | xfs_trans_stale_inode_buf( |
| 839 | xfs_trans_t *tp, |
| 840 | xfs_buf_t *bp) |
| 841 | { |
| 842 | xfs_buf_log_item_t *bip; |
| 843 | |
| 844 | ASSERT(XFS_BUF_ISBUSY(bp)); |
| 845 | ASSERT(XFS_BUF_FSPRIVATE2(bp, xfs_trans_t *) == tp); |
| 846 | ASSERT(XFS_BUF_FSPRIVATE(bp, void *) != NULL); |
| 847 | |
| 848 | bip = XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t *); |
| 849 | ASSERT(atomic_read(&bip->bli_refcount) > 0); |
| 850 | |
| 851 | bip->bli_flags |= XFS_BLI_STALE_INODE; |
| 852 | bip->bli_item.li_cb = (void(*)(xfs_buf_t*,xfs_log_item_t*)) |
| 853 | xfs_buf_iodone; |
| 854 | } |
| 855 | |
| 856 | |
| 857 | |
| 858 | /* |
| 859 | * Mark the buffer as being one which contains newly allocated |
| 860 | * inodes. We need to make sure that even if this buffer is |
| 861 | * relogged as an 'inode buf' we still recover all of the inode |
| 862 | * images in the face of a crash. This works in coordination with |
| 863 | * xfs_buf_item_committed() to ensure that the buffer remains in the |
| 864 | * AIL at its original location even after it has been relogged. |
| 865 | */ |
| 866 | /* ARGSUSED */ |
| 867 | void |
| 868 | xfs_trans_inode_alloc_buf( |
| 869 | xfs_trans_t *tp, |
| 870 | xfs_buf_t *bp) |
| 871 | { |
| 872 | xfs_buf_log_item_t *bip; |
| 873 | |
| 874 | ASSERT(XFS_BUF_ISBUSY(bp)); |
| 875 | ASSERT(XFS_BUF_FSPRIVATE2(bp, xfs_trans_t *) == tp); |
| 876 | ASSERT(XFS_BUF_FSPRIVATE(bp, void *) != NULL); |
| 877 | |
| 878 | bip = XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t *); |
| 879 | ASSERT(atomic_read(&bip->bli_refcount) > 0); |
| 880 | |
| 881 | bip->bli_flags |= XFS_BLI_INODE_ALLOC_BUF; |
| 882 | } |
| 883 | |
| 884 | |
| 885 | /* |
| 886 | * Similar to xfs_trans_inode_buf(), this marks the buffer as a cluster of |
| 887 | * dquots. However, unlike in inode buffer recovery, dquot buffers get |
| 888 | * recovered in their entirety. (Hence, no XFS_BLI_DQUOT_ALLOC_BUF flag). |
| 889 | * The only thing that makes dquot buffers different from regular |
| 890 | * buffers is that we must not replay dquot bufs when recovering |
| 891 | * if a _corresponding_ quotaoff has happened. We also have to distinguish |
| 892 | * between usr dquot bufs and grp dquot bufs, because usr and grp quotas |
| 893 | * can be turned off independently. |
| 894 | */ |
| 895 | /* ARGSUSED */ |
| 896 | void |
| 897 | xfs_trans_dquot_buf( |
| 898 | xfs_trans_t *tp, |
| 899 | xfs_buf_t *bp, |
| 900 | uint type) |
| 901 | { |
| 902 | xfs_buf_log_item_t *bip; |
| 903 | |
| 904 | ASSERT(XFS_BUF_ISBUSY(bp)); |
| 905 | ASSERT(XFS_BUF_FSPRIVATE2(bp, xfs_trans_t *) == tp); |
| 906 | ASSERT(XFS_BUF_FSPRIVATE(bp, void *) != NULL); |
Dave Chinner | c115541 | 2010-05-07 11:05:19 +1000 | [diff] [blame] | 907 | ASSERT(type == XFS_BLF_UDQUOT_BUF || |
| 908 | type == XFS_BLF_PDQUOT_BUF || |
| 909 | type == XFS_BLF_GDQUOT_BUF); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 910 | |
| 911 | bip = XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t *); |
| 912 | ASSERT(atomic_read(&bip->bli_refcount) > 0); |
| 913 | |
| 914 | bip->bli_format.blf_flags |= type; |
| 915 | } |