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" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 27 | #include "xfs_dir2.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #include "xfs_dmapi.h" |
| 29 | #include "xfs_mount.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 30 | #include "xfs_bmap_btree.h" |
| 31 | #include "xfs_alloc_btree.h" |
| 32 | #include "xfs_ialloc_btree.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 33 | #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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | #include "xfs_trans_priv.h" |
| 39 | #include "xfs_error.h" |
| 40 | #include "xfs_rw.h" |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 41 | #include "xfs_trace.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | |
Christoph Hellwig | 4a5224d | 2010-04-18 00:10:45 +0000 | [diff] [blame] | 43 | /* |
| 44 | * Check to see if a buffer matching the given parameters is already |
| 45 | * a part of the given transaction. |
| 46 | */ |
| 47 | STATIC struct xfs_buf * |
| 48 | xfs_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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | |
Christoph Hellwig | 4a5224d | 2010-04-18 00:10:45 +0000 | [diff] [blame] | 59 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | |
Christoph Hellwig | d7e84f4 | 2010-02-15 23:35:09 +0000 | [diff] [blame] | 89 | /* |
| 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 | */ |
| 98 | STATIC 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 Chinner | c115541 | 2010-05-07 11:05:19 +1000 | [diff] [blame] | 117 | ASSERT(!(bip->bli_format.blf_flags & XFS_BLF_CANCEL)); |
Christoph Hellwig | d7e84f4 | 2010-02-15 23:35:09 +0000 | [diff] [blame] | 118 | 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 | |
| 140 | void |
| 141 | xfs_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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | * If the transaction pointer is NULL, make this just a normal |
| 156 | * get_buf() call. |
| 157 | */ |
| 158 | xfs_buf_t * |
| 159 | xfs_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 Hellwig | 0cadda1 | 2010-01-19 09:56:44 +0000 | [diff] [blame] | 169 | flags = XBF_LOCK | XBF_MAPPED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | |
| 171 | /* |
| 172 | * Default to a normal get_buf() call if the tp is NULL. |
| 173 | */ |
Christoph Hellwig | 6ad112b | 2009-11-24 18:02:23 +0000 | [diff] [blame] | 174 | if (tp == NULL) |
Christoph Hellwig | 0cadda1 | 2010-01-19 09:56:44 +0000 | [diff] [blame] | 175 | return xfs_buf_get(target_dev, blkno, len, |
| 176 | flags | XBF_DONT_BLOCK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | |
| 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 Hellwig | 4a5224d | 2010-04-18 00:10:45 +0000 | [diff] [blame] | 184 | bp = xfs_trans_buf_item_match(tp, target_dev, blkno, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | if (bp != NULL) { |
| 186 | ASSERT(XFS_BUF_VALUSEMA(bp) <= 0); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 187 | if (XFS_FORCED_SHUTDOWN(tp->t_mountp)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | XFS_BUF_SUPER_STALE(bp); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 189 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | /* |
| 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 Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 195 | else if (XFS_BUF_ISSTALE(bp)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | ASSERT(!XFS_BUF_ISDELAYWRITE(bp)); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 197 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | 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 Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 203 | trace_xfs_trans_get_buf_recur(bip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | return (bp); |
| 205 | } |
| 206 | |
| 207 | /* |
Christoph Hellwig | 0cadda1 | 2010-01-19 09:56:44 +0000 | [diff] [blame] | 208 | * 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | * 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 Hellwig | 0cadda1 | 2010-01-19 09:56:44 +0000 | [diff] [blame] | 215 | bp = xfs_buf_get(target_dev, blkno, len, flags | XBF_DONT_BLOCK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | if (bp == NULL) { |
| 217 | return NULL; |
| 218 | } |
| 219 | |
| 220 | ASSERT(!XFS_BUF_GETERROR(bp)); |
| 221 | |
Christoph Hellwig | d7e84f4 | 2010-02-15 23:35:09 +0000 | [diff] [blame] | 222 | _xfs_trans_bjoin(tp, bp, 1); |
| 223 | trace_xfs_trans_get_buf(bp->b_fspriv); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | 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 | */ |
| 235 | xfs_buf_t * |
| 236 | xfs_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 Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 263 | trace_xfs_trans_getsb_recur(bip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | return (bp); |
| 265 | } |
| 266 | |
| 267 | bp = xfs_getsb(mp, flags); |
Christoph Hellwig | d7e84f4 | 2010-02-15 23:35:09 +0000 | [diff] [blame] | 268 | if (bp == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | |
Christoph Hellwig | d7e84f4 | 2010-02-15 23:35:09 +0000 | [diff] [blame] | 271 | _xfs_trans_bjoin(tp, bp, 1); |
| 272 | trace_xfs_trans_getsb(bp->b_fspriv); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | return (bp); |
| 274 | } |
| 275 | |
| 276 | #ifdef DEBUG |
| 277 | xfs_buftarg_t *xfs_error_target; |
| 278 | int xfs_do_error; |
| 279 | int xfs_req_num; |
| 280 | int 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | * If the transaction pointer is NULL, make this just a normal |
| 291 | * read_buf() call. |
| 292 | */ |
| 293 | int |
| 294 | xfs_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 Hellwig | 0cadda1 | 2010-01-19 09:56:44 +0000 | [diff] [blame] | 308 | flags = XBF_LOCK | XBF_MAPPED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | |
| 310 | /* |
| 311 | * Default to a normal get_buf() call if the tp is NULL. |
| 312 | */ |
| 313 | if (tp == NULL) { |
Christoph Hellwig | 0cadda1 | 2010-01-19 09:56:44 +0000 | [diff] [blame] | 314 | bp = xfs_buf_read(target, blkno, len, flags | XBF_DONT_BLOCK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | if (!bp) |
Christoph Hellwig | 0cadda1 | 2010-01-19 09:56:44 +0000 | [diff] [blame] | 316 | return (flags & XBF_TRYLOCK) ? |
David Chinner | a3f74ff | 2008-03-06 13:43:42 +1100 | [diff] [blame] | 317 | EAGAIN : XFS_ERROR(ENOMEM); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | |
Julia Lawall | a0f7bfd | 2009-07-27 18:15:25 +0200 | [diff] [blame] | 319 | if (XFS_BUF_GETERROR(bp) != 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | 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 Lawall | a0f7bfd | 2009-07-27 18:15:25 +0200 | [diff] [blame] | 327 | if (xfs_do_error) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | if (xfs_error_target == target) { |
| 329 | if (((xfs_req_num++) % xfs_error_mod) == 0) { |
| 330 | xfs_buf_relse(bp); |
Nathan Scott | b657452 | 2006-06-09 15:29:40 +1000 | [diff] [blame] | 331 | cmn_err(CE_DEBUG, "Returning error!\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | 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 Hellwig | 4a5224d | 2010-04-18 00:10:45 +0000 | [diff] [blame] | 351 | bp = xfs_trans_buf_item_match(tp, target, blkno, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | 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 Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 358 | trace_xfs_trans_read_buf_io(bp, _RET_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | ASSERT(!XFS_BUF_ISASYNC(bp)); |
| 360 | XFS_BUF_READ(bp); |
| 361 | xfsbdstrat(tp->t_mountp, bp); |
David Chinner | d64e31a | 2008-04-10 12:22:17 +1000 | [diff] [blame] | 362 | error = xfs_iowait(bp); |
| 363 | if (error) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | xfs_ioerror_alert("xfs_trans_read_buf", mp, |
| 365 | bp, blkno); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | xfs_buf_relse(bp); |
| 367 | /* |
David Chinner | d64e31a | 2008-04-10 12:22:17 +1000 | [diff] [blame] | 368 | * 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | */ |
| 372 | if (tp->t_flags & XFS_TRANS_DIRTY) |
| 373 | xfs_force_shutdown(tp->t_mountp, |
Nathan Scott | 7d04a33 | 2006-06-09 14:58:38 +1000 | [diff] [blame] | 374 | SHUTDOWN_META_IO_ERROR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | 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 Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 383 | trace_xfs_trans_read_buf_shut(bp, _RET_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | *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 Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 393 | trace_xfs_trans_read_buf_recur(bip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | *bpp = bp; |
| 395 | return 0; |
| 396 | } |
| 397 | |
| 398 | /* |
Christoph Hellwig | 0cadda1 | 2010-01-19 09:56:44 +0000 | [diff] [blame] | 399 | * 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | * 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 Hellwig | 0cadda1 | 2010-01-19 09:56:44 +0000 | [diff] [blame] | 406 | bp = xfs_buf_read(target, blkno, len, flags | XBF_DONT_BLOCK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | if (bp == NULL) { |
| 408 | *bpp = NULL; |
| 409 | return 0; |
| 410 | } |
| 411 | if (XFS_BUF_GETERROR(bp) != 0) { |
| 412 | XFS_BUF_SUPER_STALE(bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | 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 Scott | 7d04a33 | 2006-06-09 14:58:38 +1000 | [diff] [blame] | 418 | xfs_force_shutdown(tp->t_mountp, SHUTDOWN_META_IO_ERROR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | 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 Scott | 7d04a33 | 2006-06-09 14:58:38 +1000 | [diff] [blame] | 427 | SHUTDOWN_META_IO_ERROR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 | xfs_buf_relse(bp); |
Nathan Scott | b657452 | 2006-06-09 15:29:40 +1000 | [diff] [blame] | 429 | cmn_err(CE_DEBUG, "Returning trans error!\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | return XFS_ERROR(EIO); |
| 431 | } |
| 432 | } |
| 433 | } |
| 434 | #endif |
| 435 | if (XFS_FORCED_SHUTDOWN(mp)) |
| 436 | goto shutdown_abort; |
| 437 | |
Christoph Hellwig | d7e84f4 | 2010-02-15 23:35:09 +0000 | [diff] [blame] | 438 | _xfs_trans_bjoin(tp, bp, 1); |
| 439 | trace_xfs_trans_read_buf(bp->b_fspriv); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | *bpp = bp; |
| 442 | return 0; |
| 443 | |
| 444 | shutdown_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 Hellwig | 0cadda1 | 2010-01-19 09:56:44 +0000 | [diff] [blame] | 455 | ASSERT((XFS_BUF_BFLAGS(bp) & (XBF_STALE|XBF_DELWRI)) != |
| 456 | (XBF_STALE|XBF_DELWRI)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 458 | trace_xfs_trans_read_buf_shut(bp, _RET_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 | 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 | */ |
| 480 | void |
| 481 | xfs_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 Chinner | 783a2f6 | 2008-10-30 17:39:58 +1100 | [diff] [blame] | 502 | xfs_trans_unlocked_item(bip->bli_item.li_ailp, |
| 503 | lip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 | } |
| 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 Chinner | c115541 | 2010-05-07 11:05:19 +1000 | [diff] [blame] | 514 | ASSERT(!(bip->bli_format.blf_flags & XFS_BLF_CANCEL)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 515 | 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 Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 524 | trace_xfs_trans_brelse(bip); |
| 525 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 526 | /* |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 532 | return; |
| 533 | } |
| 534 | |
| 535 | /* |
| 536 | * If the buffer is dirty within this transaction, we can't |
| 537 | * release it until we commit. |
| 538 | */ |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 539 | if (lidp->lid_flags & XFS_LID_DIRTY) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 540 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 541 | |
| 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 Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 548 | if (bip->bli_flags & XFS_BLI_STALE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 549 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 550 | |
| 551 | ASSERT(!(bip->bli_flags & XFS_BLI_LOGGED)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | |
| 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 Chinner | 783a2f6 | 2008-10-30 17:39:58 +1100 | [diff] [blame] | 596 | xfs_trans_unlocked_item(bip->bli_item.li_ailp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 597 | (xfs_log_item_t*)bip); |
| 598 | } |
| 599 | |
| 600 | xfs_buf_relse(bp); |
| 601 | return; |
| 602 | } |
| 603 | |
| 604 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 605 | * 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 */ |
| 610 | void |
| 611 | xfs_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 Chinner | c115541 | 2010-05-07 11:05:19 +1000 | [diff] [blame] | 622 | ASSERT(!(bip->bli_format.blf_flags & XFS_BLF_CANCEL)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 623 | ASSERT(atomic_read(&bip->bli_refcount) > 0); |
| 624 | bip->bli_flags |= XFS_BLI_HOLD; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 625 | trace_xfs_trans_bhold(bip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 626 | } |
| 627 | |
| 628 | /* |
Tim Shimmin | efa092f | 2005-09-05 08:29:01 +1000 | [diff] [blame] | 629 | * Cancel the previous buffer hold request made on this buffer |
| 630 | * for this transaction. |
| 631 | */ |
| 632 | void |
| 633 | xfs_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 Chinner | c115541 | 2010-05-07 11:05:19 +1000 | [diff] [blame] | 644 | ASSERT(!(bip->bli_format.blf_flags & XFS_BLF_CANCEL)); |
Tim Shimmin | efa092f | 2005-09-05 08:29:01 +1000 | [diff] [blame] | 645 | ASSERT(atomic_read(&bip->bli_refcount) > 0); |
| 646 | ASSERT(bip->bli_flags & XFS_BLI_HOLD); |
| 647 | bip->bli_flags &= ~XFS_BLI_HOLD; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 648 | |
| 649 | trace_xfs_trans_bhold_release(bip); |
Tim Shimmin | efa092f | 2005-09-05 08:29:01 +1000 | [diff] [blame] | 650 | } |
| 651 | |
| 652 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 653 | * 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 | */ |
| 661 | void |
| 662 | xfs_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 Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 695 | trace_xfs_trans_log_buf(bip); |
| 696 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 697 | /* |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 704 | bip->bli_flags &= ~XFS_BLI_STALE; |
| 705 | ASSERT(XFS_BUF_ISSTALE(bp)); |
| 706 | XFS_BUF_UNSTALE(bp); |
Dave Chinner | c115541 | 2010-05-07 11:05:19 +1000 | [diff] [blame] | 707 | bip->bli_format.blf_flags &= ~XFS_BLF_CANCEL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 708 | } |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 715 | bip->bli_flags |= XFS_BLI_LOGGED; |
| 716 | xfs_buf_item_log(bip, first, last); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 717 | } |
| 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 | */ |
| 738 | void |
| 739 | xfs_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 Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 755 | trace_xfs_trans_binval(bip); |
| 756 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 757 | 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 Chinner | c115541 | 2010-05-07 11:05:19 +1000 | [diff] [blame] | 765 | ASSERT(!(bip->bli_format.blf_flags & XFS_BLF_INODE_BUF)); |
| 766 | ASSERT(bip->bli_format.blf_flags & XFS_BLF_CANCEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 767 | ASSERT(lidp->lid_flags & XFS_LID_DIRTY); |
| 768 | ASSERT(tp->t_flags & XFS_TRANS_DIRTY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 769 | 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 Chinner | c115541 | 2010-05-07 11:05:19 +1000 | [diff] [blame] | 777 | * 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] | 778 | * 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 Chinner | ccf7c23 | 2010-05-20 23:19:42 +1000 | [diff] [blame] | 795 | 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] | 796 | bip->bli_format.blf_flags &= ~XFS_BLF_INODE_BUF; |
| 797 | bip->bli_format.blf_flags |= XFS_BLF_CANCEL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 798 | memset((char *)(bip->bli_format.blf_data_map), 0, |
| 799 | (bip->bli_format.blf_map_size * sizeof(uint))); |
Dave Chinner | 8e12385 | 2010-03-08 11:26:03 +1100 | [diff] [blame] | 800 | lidp->lid_flags |= XFS_LID_DIRTY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 801 | tp->t_flags |= XFS_TRANS_DIRTY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 802 | } |
| 803 | |
| 804 | /* |
Dave Chinner | ccf7c23 | 2010-05-20 23:19:42 +1000 | [diff] [blame] | 805 | * 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 810 | * |
Dave Chinner | ccf7c23 | 2010-05-20 23:19:42 +1000 | [diff] [blame] | 811 | * 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 814 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 815 | void |
| 816 | xfs_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 Chinner | ccf7c23 | 2010-05-20 23:19:42 +1000 | [diff] [blame] | 829 | bip->bli_flags |= XFS_BLI_INODE_BUF; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 830 | } |
| 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 | */ |
| 841 | void |
| 842 | xfs_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 */ |
| 871 | void |
| 872 | xfs_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 */ |
| 900 | void |
| 901 | xfs_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 Chinner | c115541 | 2010-05-07 11:05:19 +1000 | [diff] [blame] | 911 | ASSERT(type == XFS_BLF_UDQUOT_BUF || |
| 912 | type == XFS_BLF_PDQUOT_BUF || |
| 913 | type == XFS_BLF_GDQUOT_BUF); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 914 | |
| 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 | } |