Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (c) 2000-2006 Silicon Graphics, Inc. |
| 3 | * All Rights Reserved. |
| 4 | * |
| 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 |
| 7 | * published by the Free Software Foundation. |
| 8 | * |
| 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. |
| 13 | * |
| 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 |
| 17 | */ |
| 18 | #include "xfs.h" |
| 19 | #include "xfs_fs.h" |
| 20 | #include "xfs_format.h" |
| 21 | #include "xfs_bit.h" |
| 22 | #include "xfs_log.h" |
| 23 | #include "xfs_inum.h" |
| 24 | #include "xfs_trans.h" |
| 25 | #include "xfs_sb.h" |
| 26 | #include "xfs_ag.h" |
| 27 | #include "xfs_mount.h" |
| 28 | #include "xfs_da_btree.h" |
| 29 | #include "xfs_bmap_btree.h" |
| 30 | #include "xfs_alloc_btree.h" |
| 31 | #include "xfs_ialloc_btree.h" |
| 32 | #include "xfs_dinode.h" |
| 33 | #include "xfs_inode.h" |
| 34 | #include "xfs_btree.h" |
| 35 | #include "xfs_extfree_item.h" |
| 36 | #include "xfs_alloc.h" |
| 37 | #include "xfs_bmap.h" |
| 38 | #include "xfs_bmap_util.h" |
| 39 | #include "xfs_rtalloc.h" |
| 40 | #include "xfs_error.h" |
| 41 | #include "xfs_quota.h" |
| 42 | #include "xfs_trans_space.h" |
| 43 | #include "xfs_trace.h" |
| 44 | |
| 45 | /* Kernel only BMAP related definitions and functions */ |
| 46 | |
| 47 | /* |
| 48 | * Convert the given file system block to a disk block. We have to treat it |
| 49 | * differently based on whether the file is a real time file or not, because the |
| 50 | * bmap code does. |
| 51 | */ |
| 52 | xfs_daddr_t |
| 53 | xfs_fsb_to_db(struct xfs_inode *ip, xfs_fsblock_t fsb) |
| 54 | { |
| 55 | return (XFS_IS_REALTIME_INODE(ip) ? \ |
| 56 | (xfs_daddr_t)XFS_FSB_TO_BB((ip)->i_mount, (fsb)) : \ |
| 57 | XFS_FSB_TO_DADDR((ip)->i_mount, (fsb))); |
| 58 | } |
| 59 | |
| 60 | /* |
| 61 | * Routine to be called at transaction's end by xfs_bmapi, xfs_bunmapi |
| 62 | * caller. Frees all the extents that need freeing, which must be done |
| 63 | * last due to locking considerations. We never free any extents in |
| 64 | * the first transaction. |
| 65 | * |
| 66 | * Return 1 if the given transaction was committed and a new one |
| 67 | * started, and 0 otherwise in the committed parameter. |
| 68 | */ |
| 69 | int /* error */ |
| 70 | xfs_bmap_finish( |
| 71 | xfs_trans_t **tp, /* transaction pointer addr */ |
| 72 | xfs_bmap_free_t *flist, /* i/o: list extents to free */ |
| 73 | int *committed) /* xact committed or not */ |
| 74 | { |
| 75 | xfs_efd_log_item_t *efd; /* extent free data */ |
| 76 | xfs_efi_log_item_t *efi; /* extent free intention */ |
| 77 | int error; /* error return value */ |
| 78 | xfs_bmap_free_item_t *free; /* free extent item */ |
| 79 | unsigned int logres; /* new log reservation */ |
| 80 | unsigned int logcount; /* new log count */ |
| 81 | xfs_mount_t *mp; /* filesystem mount structure */ |
| 82 | xfs_bmap_free_item_t *next; /* next item on free list */ |
| 83 | xfs_trans_t *ntp; /* new transaction pointer */ |
| 84 | |
| 85 | ASSERT((*tp)->t_flags & XFS_TRANS_PERM_LOG_RES); |
| 86 | if (flist->xbf_count == 0) { |
| 87 | *committed = 0; |
| 88 | return 0; |
| 89 | } |
| 90 | ntp = *tp; |
| 91 | efi = xfs_trans_get_efi(ntp, flist->xbf_count); |
| 92 | for (free = flist->xbf_first; free; free = free->xbfi_next) |
| 93 | xfs_trans_log_efi_extent(ntp, efi, free->xbfi_startblock, |
| 94 | free->xbfi_blockcount); |
| 95 | logres = ntp->t_log_res; |
| 96 | logcount = ntp->t_log_count; |
| 97 | ntp = xfs_trans_dup(*tp); |
| 98 | error = xfs_trans_commit(*tp, 0); |
| 99 | *tp = ntp; |
| 100 | *committed = 1; |
| 101 | /* |
| 102 | * We have a new transaction, so we should return committed=1, |
| 103 | * even though we're returning an error. |
| 104 | */ |
| 105 | if (error) |
| 106 | return error; |
| 107 | |
| 108 | /* |
| 109 | * transaction commit worked ok so we can drop the extra ticket |
| 110 | * reference that we gained in xfs_trans_dup() |
| 111 | */ |
| 112 | xfs_log_ticket_put(ntp->t_ticket); |
| 113 | |
| 114 | if ((error = xfs_trans_reserve(ntp, 0, logres, 0, XFS_TRANS_PERM_LOG_RES, |
| 115 | logcount))) |
| 116 | return error; |
| 117 | efd = xfs_trans_get_efd(ntp, efi, flist->xbf_count); |
| 118 | for (free = flist->xbf_first; free != NULL; free = next) { |
| 119 | next = free->xbfi_next; |
| 120 | if ((error = xfs_free_extent(ntp, free->xbfi_startblock, |
| 121 | free->xbfi_blockcount))) { |
| 122 | /* |
| 123 | * The bmap free list will be cleaned up at a |
| 124 | * higher level. The EFI will be canceled when |
| 125 | * this transaction is aborted. |
| 126 | * Need to force shutdown here to make sure it |
| 127 | * happens, since this transaction may not be |
| 128 | * dirty yet. |
| 129 | */ |
| 130 | mp = ntp->t_mountp; |
| 131 | if (!XFS_FORCED_SHUTDOWN(mp)) |
| 132 | xfs_force_shutdown(mp, |
| 133 | (error == EFSCORRUPTED) ? |
| 134 | SHUTDOWN_CORRUPT_INCORE : |
| 135 | SHUTDOWN_META_IO_ERROR); |
| 136 | return error; |
| 137 | } |
| 138 | xfs_trans_log_efd_extent(ntp, efd, free->xbfi_startblock, |
| 139 | free->xbfi_blockcount); |
| 140 | xfs_bmap_del_free(flist, NULL, free); |
| 141 | } |
| 142 | return 0; |
| 143 | } |
| 144 | |
| 145 | int |
| 146 | xfs_bmap_rtalloc( |
| 147 | struct xfs_bmalloca *ap) /* bmap alloc argument struct */ |
| 148 | { |
| 149 | xfs_alloctype_t atype = 0; /* type for allocation routines */ |
| 150 | int error; /* error return value */ |
| 151 | xfs_mount_t *mp; /* mount point structure */ |
| 152 | xfs_extlen_t prod = 0; /* product factor for allocators */ |
| 153 | xfs_extlen_t ralen = 0; /* realtime allocation length */ |
| 154 | xfs_extlen_t align; /* minimum allocation alignment */ |
| 155 | xfs_rtblock_t rtb; |
| 156 | |
| 157 | mp = ap->ip->i_mount; |
| 158 | align = xfs_get_extsz_hint(ap->ip); |
| 159 | prod = align / mp->m_sb.sb_rextsize; |
| 160 | error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev, |
| 161 | align, 1, ap->eof, 0, |
| 162 | ap->conv, &ap->offset, &ap->length); |
| 163 | if (error) |
| 164 | return error; |
| 165 | ASSERT(ap->length); |
| 166 | ASSERT(ap->length % mp->m_sb.sb_rextsize == 0); |
| 167 | |
| 168 | /* |
| 169 | * If the offset & length are not perfectly aligned |
| 170 | * then kill prod, it will just get us in trouble. |
| 171 | */ |
| 172 | if (do_mod(ap->offset, align) || ap->length % align) |
| 173 | prod = 1; |
| 174 | /* |
| 175 | * Set ralen to be the actual requested length in rtextents. |
| 176 | */ |
| 177 | ralen = ap->length / mp->m_sb.sb_rextsize; |
| 178 | /* |
| 179 | * If the old value was close enough to MAXEXTLEN that |
| 180 | * we rounded up to it, cut it back so it's valid again. |
| 181 | * Note that if it's a really large request (bigger than |
| 182 | * MAXEXTLEN), we don't hear about that number, and can't |
| 183 | * adjust the starting point to match it. |
| 184 | */ |
| 185 | if (ralen * mp->m_sb.sb_rextsize >= MAXEXTLEN) |
| 186 | ralen = MAXEXTLEN / mp->m_sb.sb_rextsize; |
| 187 | |
| 188 | /* |
| 189 | * Lock out other modifications to the RT bitmap inode. |
| 190 | */ |
| 191 | xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL); |
| 192 | xfs_trans_ijoin(ap->tp, mp->m_rbmip, XFS_ILOCK_EXCL); |
| 193 | |
| 194 | /* |
| 195 | * If it's an allocation to an empty file at offset 0, |
| 196 | * pick an extent that will space things out in the rt area. |
| 197 | */ |
| 198 | if (ap->eof && ap->offset == 0) { |
| 199 | xfs_rtblock_t uninitialized_var(rtx); /* realtime extent no */ |
| 200 | |
| 201 | error = xfs_rtpick_extent(mp, ap->tp, ralen, &rtx); |
| 202 | if (error) |
| 203 | return error; |
| 204 | ap->blkno = rtx * mp->m_sb.sb_rextsize; |
| 205 | } else { |
| 206 | ap->blkno = 0; |
| 207 | } |
| 208 | |
| 209 | xfs_bmap_adjacent(ap); |
| 210 | |
| 211 | /* |
| 212 | * Realtime allocation, done through xfs_rtallocate_extent. |
| 213 | */ |
| 214 | atype = ap->blkno == 0 ? XFS_ALLOCTYPE_ANY_AG : XFS_ALLOCTYPE_NEAR_BNO; |
| 215 | do_div(ap->blkno, mp->m_sb.sb_rextsize); |
| 216 | rtb = ap->blkno; |
| 217 | ap->length = ralen; |
| 218 | if ((error = xfs_rtallocate_extent(ap->tp, ap->blkno, 1, ap->length, |
| 219 | &ralen, atype, ap->wasdel, prod, &rtb))) |
| 220 | return error; |
| 221 | if (rtb == NULLFSBLOCK && prod > 1 && |
| 222 | (error = xfs_rtallocate_extent(ap->tp, ap->blkno, 1, |
| 223 | ap->length, &ralen, atype, |
| 224 | ap->wasdel, 1, &rtb))) |
| 225 | return error; |
| 226 | ap->blkno = rtb; |
| 227 | if (ap->blkno != NULLFSBLOCK) { |
| 228 | ap->blkno *= mp->m_sb.sb_rextsize; |
| 229 | ralen *= mp->m_sb.sb_rextsize; |
| 230 | ap->length = ralen; |
| 231 | ap->ip->i_d.di_nblocks += ralen; |
| 232 | xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE); |
| 233 | if (ap->wasdel) |
| 234 | ap->ip->i_delayed_blks -= ralen; |
| 235 | /* |
| 236 | * Adjust the disk quota also. This was reserved |
| 237 | * earlier. |
| 238 | */ |
| 239 | xfs_trans_mod_dquot_byino(ap->tp, ap->ip, |
| 240 | ap->wasdel ? XFS_TRANS_DQ_DELRTBCOUNT : |
| 241 | XFS_TRANS_DQ_RTBCOUNT, (long) ralen); |
| 242 | } else { |
| 243 | ap->length = 0; |
| 244 | } |
| 245 | return 0; |
| 246 | } |
| 247 | |
| 248 | /* |
| 249 | * Stack switching interfaces for allocation |
| 250 | */ |
| 251 | static void |
| 252 | xfs_bmapi_allocate_worker( |
| 253 | struct work_struct *work) |
| 254 | { |
| 255 | struct xfs_bmalloca *args = container_of(work, |
| 256 | struct xfs_bmalloca, work); |
| 257 | unsigned long pflags; |
| 258 | |
| 259 | /* we are in a transaction context here */ |
| 260 | current_set_flags_nested(&pflags, PF_FSTRANS); |
| 261 | |
| 262 | args->result = __xfs_bmapi_allocate(args); |
| 263 | complete(args->done); |
| 264 | |
| 265 | current_restore_flags_nested(&pflags, PF_FSTRANS); |
| 266 | } |
| 267 | |
| 268 | /* |
| 269 | * Some allocation requests often come in with little stack to work on. Push |
| 270 | * them off to a worker thread so there is lots of stack to use. Otherwise just |
| 271 | * call directly to avoid the context switch overhead here. |
| 272 | */ |
| 273 | int |
| 274 | xfs_bmapi_allocate( |
| 275 | struct xfs_bmalloca *args) |
| 276 | { |
| 277 | DECLARE_COMPLETION_ONSTACK(done); |
| 278 | |
| 279 | if (!args->stack_switch) |
| 280 | return __xfs_bmapi_allocate(args); |
| 281 | |
| 282 | |
| 283 | args->done = &done; |
| 284 | INIT_WORK_ONSTACK(&args->work, xfs_bmapi_allocate_worker); |
| 285 | queue_work(xfs_alloc_wq, &args->work); |
| 286 | wait_for_completion(&done); |
| 287 | return args->result; |
| 288 | } |
| 289 | |
| 290 | /* |
| 291 | * Check if the endoff is outside the last extent. If so the caller will grow |
| 292 | * the allocation to a stripe unit boundary. All offsets are considered outside |
| 293 | * the end of file for an empty fork, so 1 is returned in *eof in that case. |
| 294 | */ |
| 295 | int |
| 296 | xfs_bmap_eof( |
| 297 | struct xfs_inode *ip, |
| 298 | xfs_fileoff_t endoff, |
| 299 | int whichfork, |
| 300 | int *eof) |
| 301 | { |
| 302 | struct xfs_bmbt_irec rec; |
| 303 | int error; |
| 304 | |
| 305 | error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, eof); |
| 306 | if (error || *eof) |
| 307 | return error; |
| 308 | |
| 309 | *eof = endoff >= rec.br_startoff + rec.br_blockcount; |
| 310 | return 0; |
| 311 | } |
| 312 | |
| 313 | /* |
| 314 | * Extent tree block counting routines. |
| 315 | */ |
| 316 | |
| 317 | /* |
| 318 | * Count leaf blocks given a range of extent records. |
| 319 | */ |
| 320 | STATIC void |
| 321 | xfs_bmap_count_leaves( |
| 322 | xfs_ifork_t *ifp, |
| 323 | xfs_extnum_t idx, |
| 324 | int numrecs, |
| 325 | int *count) |
| 326 | { |
| 327 | int b; |
| 328 | |
| 329 | for (b = 0; b < numrecs; b++) { |
| 330 | xfs_bmbt_rec_host_t *frp = xfs_iext_get_ext(ifp, idx + b); |
| 331 | *count += xfs_bmbt_get_blockcount(frp); |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | /* |
| 336 | * Count leaf blocks given a range of extent records originally |
| 337 | * in btree format. |
| 338 | */ |
| 339 | STATIC void |
| 340 | xfs_bmap_disk_count_leaves( |
| 341 | struct xfs_mount *mp, |
| 342 | struct xfs_btree_block *block, |
| 343 | int numrecs, |
| 344 | int *count) |
| 345 | { |
| 346 | int b; |
| 347 | xfs_bmbt_rec_t *frp; |
| 348 | |
| 349 | for (b = 1; b <= numrecs; b++) { |
| 350 | frp = XFS_BMBT_REC_ADDR(mp, block, b); |
| 351 | *count += xfs_bmbt_disk_get_blockcount(frp); |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | /* |
| 356 | * Recursively walks each level of a btree |
| 357 | * to count total fsblocks is use. |
| 358 | */ |
| 359 | STATIC int /* error */ |
| 360 | xfs_bmap_count_tree( |
| 361 | xfs_mount_t *mp, /* file system mount point */ |
| 362 | xfs_trans_t *tp, /* transaction pointer */ |
| 363 | xfs_ifork_t *ifp, /* inode fork pointer */ |
| 364 | xfs_fsblock_t blockno, /* file system block number */ |
| 365 | int levelin, /* level in btree */ |
| 366 | int *count) /* Count of blocks */ |
| 367 | { |
| 368 | int error; |
| 369 | xfs_buf_t *bp, *nbp; |
| 370 | int level = levelin; |
| 371 | __be64 *pp; |
| 372 | xfs_fsblock_t bno = blockno; |
| 373 | xfs_fsblock_t nextbno; |
| 374 | struct xfs_btree_block *block, *nextblock; |
| 375 | int numrecs; |
| 376 | |
| 377 | error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, XFS_BMAP_BTREE_REF, |
| 378 | &xfs_bmbt_buf_ops); |
| 379 | if (error) |
| 380 | return error; |
| 381 | *count += 1; |
| 382 | block = XFS_BUF_TO_BLOCK(bp); |
| 383 | |
| 384 | if (--level) { |
| 385 | /* Not at node above leaves, count this level of nodes */ |
| 386 | nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib); |
| 387 | while (nextbno != NULLFSBLOCK) { |
| 388 | error = xfs_btree_read_bufl(mp, tp, nextbno, 0, &nbp, |
| 389 | XFS_BMAP_BTREE_REF, |
| 390 | &xfs_bmbt_buf_ops); |
| 391 | if (error) |
| 392 | return error; |
| 393 | *count += 1; |
| 394 | nextblock = XFS_BUF_TO_BLOCK(nbp); |
| 395 | nextbno = be64_to_cpu(nextblock->bb_u.l.bb_rightsib); |
| 396 | xfs_trans_brelse(tp, nbp); |
| 397 | } |
| 398 | |
| 399 | /* Dive to the next level */ |
| 400 | pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]); |
| 401 | bno = be64_to_cpu(*pp); |
| 402 | if (unlikely((error = |
| 403 | xfs_bmap_count_tree(mp, tp, ifp, bno, level, count)) < 0)) { |
| 404 | xfs_trans_brelse(tp, bp); |
| 405 | XFS_ERROR_REPORT("xfs_bmap_count_tree(1)", |
| 406 | XFS_ERRLEVEL_LOW, mp); |
| 407 | return XFS_ERROR(EFSCORRUPTED); |
| 408 | } |
| 409 | xfs_trans_brelse(tp, bp); |
| 410 | } else { |
| 411 | /* count all level 1 nodes and their leaves */ |
| 412 | for (;;) { |
| 413 | nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib); |
| 414 | numrecs = be16_to_cpu(block->bb_numrecs); |
| 415 | xfs_bmap_disk_count_leaves(mp, block, numrecs, count); |
| 416 | xfs_trans_brelse(tp, bp); |
| 417 | if (nextbno == NULLFSBLOCK) |
| 418 | break; |
| 419 | bno = nextbno; |
| 420 | error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, |
| 421 | XFS_BMAP_BTREE_REF, |
| 422 | &xfs_bmbt_buf_ops); |
| 423 | if (error) |
| 424 | return error; |
| 425 | *count += 1; |
| 426 | block = XFS_BUF_TO_BLOCK(bp); |
| 427 | } |
| 428 | } |
| 429 | return 0; |
| 430 | } |
| 431 | |
| 432 | /* |
| 433 | * Count fsblocks of the given fork. |
| 434 | */ |
| 435 | int /* error */ |
| 436 | xfs_bmap_count_blocks( |
| 437 | xfs_trans_t *tp, /* transaction pointer */ |
| 438 | xfs_inode_t *ip, /* incore inode */ |
| 439 | int whichfork, /* data or attr fork */ |
| 440 | int *count) /* out: count of blocks */ |
| 441 | { |
| 442 | struct xfs_btree_block *block; /* current btree block */ |
| 443 | xfs_fsblock_t bno; /* block # of "block" */ |
| 444 | xfs_ifork_t *ifp; /* fork structure */ |
| 445 | int level; /* btree level, for checking */ |
| 446 | xfs_mount_t *mp; /* file system mount structure */ |
| 447 | __be64 *pp; /* pointer to block address */ |
| 448 | |
| 449 | bno = NULLFSBLOCK; |
| 450 | mp = ip->i_mount; |
| 451 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 452 | if ( XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ) { |
| 453 | xfs_bmap_count_leaves(ifp, 0, |
| 454 | ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t), |
| 455 | count); |
| 456 | return 0; |
| 457 | } |
| 458 | |
| 459 | /* |
| 460 | * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out. |
| 461 | */ |
| 462 | block = ifp->if_broot; |
| 463 | level = be16_to_cpu(block->bb_level); |
| 464 | ASSERT(level > 0); |
| 465 | pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes); |
| 466 | bno = be64_to_cpu(*pp); |
| 467 | ASSERT(bno != NULLDFSBNO); |
| 468 | ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount); |
| 469 | ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks); |
| 470 | |
| 471 | if (unlikely(xfs_bmap_count_tree(mp, tp, ifp, bno, level, count) < 0)) { |
| 472 | XFS_ERROR_REPORT("xfs_bmap_count_blocks(2)", XFS_ERRLEVEL_LOW, |
| 473 | mp); |
| 474 | return XFS_ERROR(EFSCORRUPTED); |
| 475 | } |
| 476 | |
| 477 | return 0; |
| 478 | } |
| 479 | |
| 480 | /* |
| 481 | * returns 1 for success, 0 if we failed to map the extent. |
| 482 | */ |
| 483 | STATIC int |
| 484 | xfs_getbmapx_fix_eof_hole( |
| 485 | xfs_inode_t *ip, /* xfs incore inode pointer */ |
| 486 | struct getbmapx *out, /* output structure */ |
| 487 | int prealloced, /* this is a file with |
| 488 | * preallocated data space */ |
| 489 | __int64_t end, /* last block requested */ |
| 490 | xfs_fsblock_t startblock) |
| 491 | { |
| 492 | __int64_t fixlen; |
| 493 | xfs_mount_t *mp; /* file system mount point */ |
| 494 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 495 | xfs_extnum_t lastx; /* last extent pointer */ |
| 496 | xfs_fileoff_t fileblock; |
| 497 | |
| 498 | if (startblock == HOLESTARTBLOCK) { |
| 499 | mp = ip->i_mount; |
| 500 | out->bmv_block = -1; |
| 501 | fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, XFS_ISIZE(ip))); |
| 502 | fixlen -= out->bmv_offset; |
| 503 | if (prealloced && out->bmv_offset + out->bmv_length == end) { |
| 504 | /* Came to hole at EOF. Trim it. */ |
| 505 | if (fixlen <= 0) |
| 506 | return 0; |
| 507 | out->bmv_length = fixlen; |
| 508 | } |
| 509 | } else { |
| 510 | if (startblock == DELAYSTARTBLOCK) |
| 511 | out->bmv_block = -2; |
| 512 | else |
| 513 | out->bmv_block = xfs_fsb_to_db(ip, startblock); |
| 514 | fileblock = XFS_BB_TO_FSB(ip->i_mount, out->bmv_offset); |
| 515 | ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK); |
| 516 | if (xfs_iext_bno_to_ext(ifp, fileblock, &lastx) && |
| 517 | (lastx == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))-1)) |
| 518 | out->bmv_oflags |= BMV_OF_LAST; |
| 519 | } |
| 520 | |
| 521 | return 1; |
| 522 | } |
| 523 | |
| 524 | /* |
| 525 | * Get inode's extents as described in bmv, and format for output. |
| 526 | * Calls formatter to fill the user's buffer until all extents |
| 527 | * are mapped, until the passed-in bmv->bmv_count slots have |
| 528 | * been filled, or until the formatter short-circuits the loop, |
| 529 | * if it is tracking filled-in extents on its own. |
| 530 | */ |
| 531 | int /* error code */ |
| 532 | xfs_getbmap( |
| 533 | xfs_inode_t *ip, |
| 534 | struct getbmapx *bmv, /* user bmap structure */ |
| 535 | xfs_bmap_format_t formatter, /* format to user */ |
| 536 | void *arg) /* formatter arg */ |
| 537 | { |
| 538 | __int64_t bmvend; /* last block requested */ |
| 539 | int error = 0; /* return value */ |
| 540 | __int64_t fixlen; /* length for -1 case */ |
| 541 | int i; /* extent number */ |
| 542 | int lock; /* lock state */ |
| 543 | xfs_bmbt_irec_t *map; /* buffer for user's data */ |
| 544 | xfs_mount_t *mp; /* file system mount point */ |
| 545 | int nex; /* # of user extents can do */ |
| 546 | int nexleft; /* # of user extents left */ |
| 547 | int subnex; /* # of bmapi's can do */ |
| 548 | int nmap; /* number of map entries */ |
| 549 | struct getbmapx *out; /* output structure */ |
| 550 | int whichfork; /* data or attr fork */ |
| 551 | int prealloced; /* this is a file with |
| 552 | * preallocated data space */ |
| 553 | int iflags; /* interface flags */ |
| 554 | int bmapi_flags; /* flags for xfs_bmapi */ |
| 555 | int cur_ext = 0; |
| 556 | |
| 557 | mp = ip->i_mount; |
| 558 | iflags = bmv->bmv_iflags; |
| 559 | whichfork = iflags & BMV_IF_ATTRFORK ? XFS_ATTR_FORK : XFS_DATA_FORK; |
| 560 | |
| 561 | if (whichfork == XFS_ATTR_FORK) { |
| 562 | if (XFS_IFORK_Q(ip)) { |
| 563 | if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS && |
| 564 | ip->i_d.di_aformat != XFS_DINODE_FMT_BTREE && |
| 565 | ip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL) |
| 566 | return XFS_ERROR(EINVAL); |
| 567 | } else if (unlikely( |
| 568 | ip->i_d.di_aformat != 0 && |
| 569 | ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS)) { |
| 570 | XFS_ERROR_REPORT("xfs_getbmap", XFS_ERRLEVEL_LOW, |
| 571 | ip->i_mount); |
| 572 | return XFS_ERROR(EFSCORRUPTED); |
| 573 | } |
| 574 | |
| 575 | prealloced = 0; |
| 576 | fixlen = 1LL << 32; |
| 577 | } else { |
| 578 | if (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS && |
| 579 | ip->i_d.di_format != XFS_DINODE_FMT_BTREE && |
| 580 | ip->i_d.di_format != XFS_DINODE_FMT_LOCAL) |
| 581 | return XFS_ERROR(EINVAL); |
| 582 | |
| 583 | if (xfs_get_extsz_hint(ip) || |
| 584 | ip->i_d.di_flags & (XFS_DIFLAG_PREALLOC|XFS_DIFLAG_APPEND)){ |
| 585 | prealloced = 1; |
| 586 | fixlen = mp->m_super->s_maxbytes; |
| 587 | } else { |
| 588 | prealloced = 0; |
| 589 | fixlen = XFS_ISIZE(ip); |
| 590 | } |
| 591 | } |
| 592 | |
| 593 | if (bmv->bmv_length == -1) { |
| 594 | fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, fixlen)); |
| 595 | bmv->bmv_length = |
| 596 | max_t(__int64_t, fixlen - bmv->bmv_offset, 0); |
| 597 | } else if (bmv->bmv_length == 0) { |
| 598 | bmv->bmv_entries = 0; |
| 599 | return 0; |
| 600 | } else if (bmv->bmv_length < 0) { |
| 601 | return XFS_ERROR(EINVAL); |
| 602 | } |
| 603 | |
| 604 | nex = bmv->bmv_count - 1; |
| 605 | if (nex <= 0) |
| 606 | return XFS_ERROR(EINVAL); |
| 607 | bmvend = bmv->bmv_offset + bmv->bmv_length; |
| 608 | |
| 609 | |
| 610 | if (bmv->bmv_count > ULONG_MAX / sizeof(struct getbmapx)) |
| 611 | return XFS_ERROR(ENOMEM); |
| 612 | out = kmem_zalloc(bmv->bmv_count * sizeof(struct getbmapx), KM_MAYFAIL); |
| 613 | if (!out) { |
| 614 | out = kmem_zalloc_large(bmv->bmv_count * |
| 615 | sizeof(struct getbmapx)); |
| 616 | if (!out) |
| 617 | return XFS_ERROR(ENOMEM); |
| 618 | } |
| 619 | |
| 620 | xfs_ilock(ip, XFS_IOLOCK_SHARED); |
| 621 | if (whichfork == XFS_DATA_FORK && !(iflags & BMV_IF_DELALLOC)) { |
| 622 | if (ip->i_delayed_blks || XFS_ISIZE(ip) > ip->i_d.di_size) { |
| 623 | error = -filemap_write_and_wait(VFS_I(ip)->i_mapping); |
| 624 | if (error) |
| 625 | goto out_unlock_iolock; |
| 626 | } |
| 627 | /* |
| 628 | * even after flushing the inode, there can still be delalloc |
| 629 | * blocks on the inode beyond EOF due to speculative |
| 630 | * preallocation. These are not removed until the release |
| 631 | * function is called or the inode is inactivated. Hence we |
| 632 | * cannot assert here that ip->i_delayed_blks == 0. |
| 633 | */ |
| 634 | } |
| 635 | |
| 636 | lock = xfs_ilock_map_shared(ip); |
| 637 | |
| 638 | /* |
| 639 | * Don't let nex be bigger than the number of extents |
| 640 | * we can have assuming alternating holes and real extents. |
| 641 | */ |
| 642 | if (nex > XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1) |
| 643 | nex = XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1; |
| 644 | |
| 645 | bmapi_flags = xfs_bmapi_aflag(whichfork); |
| 646 | if (!(iflags & BMV_IF_PREALLOC)) |
| 647 | bmapi_flags |= XFS_BMAPI_IGSTATE; |
| 648 | |
| 649 | /* |
| 650 | * Allocate enough space to handle "subnex" maps at a time. |
| 651 | */ |
| 652 | error = ENOMEM; |
| 653 | subnex = 16; |
| 654 | map = kmem_alloc(subnex * sizeof(*map), KM_MAYFAIL | KM_NOFS); |
| 655 | if (!map) |
| 656 | goto out_unlock_ilock; |
| 657 | |
| 658 | bmv->bmv_entries = 0; |
| 659 | |
| 660 | if (XFS_IFORK_NEXTENTS(ip, whichfork) == 0 && |
| 661 | (whichfork == XFS_ATTR_FORK || !(iflags & BMV_IF_DELALLOC))) { |
| 662 | error = 0; |
| 663 | goto out_free_map; |
| 664 | } |
| 665 | |
| 666 | nexleft = nex; |
| 667 | |
| 668 | do { |
| 669 | nmap = (nexleft > subnex) ? subnex : nexleft; |
| 670 | error = xfs_bmapi_read(ip, XFS_BB_TO_FSBT(mp, bmv->bmv_offset), |
| 671 | XFS_BB_TO_FSB(mp, bmv->bmv_length), |
| 672 | map, &nmap, bmapi_flags); |
| 673 | if (error) |
| 674 | goto out_free_map; |
| 675 | ASSERT(nmap <= subnex); |
| 676 | |
| 677 | for (i = 0; i < nmap && nexleft && bmv->bmv_length; i++) { |
| 678 | out[cur_ext].bmv_oflags = 0; |
| 679 | if (map[i].br_state == XFS_EXT_UNWRITTEN) |
| 680 | out[cur_ext].bmv_oflags |= BMV_OF_PREALLOC; |
| 681 | else if (map[i].br_startblock == DELAYSTARTBLOCK) |
| 682 | out[cur_ext].bmv_oflags |= BMV_OF_DELALLOC; |
| 683 | out[cur_ext].bmv_offset = |
| 684 | XFS_FSB_TO_BB(mp, map[i].br_startoff); |
| 685 | out[cur_ext].bmv_length = |
| 686 | XFS_FSB_TO_BB(mp, map[i].br_blockcount); |
| 687 | out[cur_ext].bmv_unused1 = 0; |
| 688 | out[cur_ext].bmv_unused2 = 0; |
| 689 | |
| 690 | /* |
| 691 | * delayed allocation extents that start beyond EOF can |
| 692 | * occur due to speculative EOF allocation when the |
| 693 | * delalloc extent is larger than the largest freespace |
| 694 | * extent at conversion time. These extents cannot be |
| 695 | * converted by data writeback, so can exist here even |
| 696 | * if we are not supposed to be finding delalloc |
| 697 | * extents. |
| 698 | */ |
| 699 | if (map[i].br_startblock == DELAYSTARTBLOCK && |
| 700 | map[i].br_startoff <= XFS_B_TO_FSB(mp, XFS_ISIZE(ip))) |
| 701 | ASSERT((iflags & BMV_IF_DELALLOC) != 0); |
| 702 | |
| 703 | if (map[i].br_startblock == HOLESTARTBLOCK && |
| 704 | whichfork == XFS_ATTR_FORK) { |
| 705 | /* came to the end of attribute fork */ |
| 706 | out[cur_ext].bmv_oflags |= BMV_OF_LAST; |
| 707 | goto out_free_map; |
| 708 | } |
| 709 | |
| 710 | if (!xfs_getbmapx_fix_eof_hole(ip, &out[cur_ext], |
| 711 | prealloced, bmvend, |
| 712 | map[i].br_startblock)) |
| 713 | goto out_free_map; |
| 714 | |
| 715 | bmv->bmv_offset = |
| 716 | out[cur_ext].bmv_offset + |
| 717 | out[cur_ext].bmv_length; |
| 718 | bmv->bmv_length = |
| 719 | max_t(__int64_t, 0, bmvend - bmv->bmv_offset); |
| 720 | |
| 721 | /* |
| 722 | * In case we don't want to return the hole, |
| 723 | * don't increase cur_ext so that we can reuse |
| 724 | * it in the next loop. |
| 725 | */ |
| 726 | if ((iflags & BMV_IF_NO_HOLES) && |
| 727 | map[i].br_startblock == HOLESTARTBLOCK) { |
| 728 | memset(&out[cur_ext], 0, sizeof(out[cur_ext])); |
| 729 | continue; |
| 730 | } |
| 731 | |
| 732 | nexleft--; |
| 733 | bmv->bmv_entries++; |
| 734 | cur_ext++; |
| 735 | } |
| 736 | } while (nmap && nexleft && bmv->bmv_length); |
| 737 | |
| 738 | out_free_map: |
| 739 | kmem_free(map); |
| 740 | out_unlock_ilock: |
| 741 | xfs_iunlock_map_shared(ip, lock); |
| 742 | out_unlock_iolock: |
| 743 | xfs_iunlock(ip, XFS_IOLOCK_SHARED); |
| 744 | |
| 745 | for (i = 0; i < cur_ext; i++) { |
| 746 | int full = 0; /* user array is full */ |
| 747 | |
| 748 | /* format results & advance arg */ |
| 749 | error = formatter(&arg, &out[i], &full); |
| 750 | if (error || full) |
| 751 | break; |
| 752 | } |
| 753 | |
| 754 | if (is_vmalloc_addr(out)) |
| 755 | kmem_free_large(out); |
| 756 | else |
| 757 | kmem_free(out); |
| 758 | return error; |
| 759 | } |
| 760 | |
| 761 | /* |
| 762 | * dead simple method of punching delalyed allocation blocks from a range in |
| 763 | * the inode. Walks a block at a time so will be slow, but is only executed in |
| 764 | * rare error cases so the overhead is not critical. This will alays punch out |
| 765 | * both the start and end blocks, even if the ranges only partially overlap |
| 766 | * them, so it is up to the caller to ensure that partial blocks are not |
| 767 | * passed in. |
| 768 | */ |
| 769 | int |
| 770 | xfs_bmap_punch_delalloc_range( |
| 771 | struct xfs_inode *ip, |
| 772 | xfs_fileoff_t start_fsb, |
| 773 | xfs_fileoff_t length) |
| 774 | { |
| 775 | xfs_fileoff_t remaining = length; |
| 776 | int error = 0; |
| 777 | |
| 778 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
| 779 | |
| 780 | do { |
| 781 | int done; |
| 782 | xfs_bmbt_irec_t imap; |
| 783 | int nimaps = 1; |
| 784 | xfs_fsblock_t firstblock; |
| 785 | xfs_bmap_free_t flist; |
| 786 | |
| 787 | /* |
| 788 | * Map the range first and check that it is a delalloc extent |
| 789 | * before trying to unmap the range. Otherwise we will be |
| 790 | * trying to remove a real extent (which requires a |
| 791 | * transaction) or a hole, which is probably a bad idea... |
| 792 | */ |
| 793 | error = xfs_bmapi_read(ip, start_fsb, 1, &imap, &nimaps, |
| 794 | XFS_BMAPI_ENTIRE); |
| 795 | |
| 796 | if (error) { |
| 797 | /* something screwed, just bail */ |
| 798 | if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) { |
| 799 | xfs_alert(ip->i_mount, |
| 800 | "Failed delalloc mapping lookup ino %lld fsb %lld.", |
| 801 | ip->i_ino, start_fsb); |
| 802 | } |
| 803 | break; |
| 804 | } |
| 805 | if (!nimaps) { |
| 806 | /* nothing there */ |
| 807 | goto next_block; |
| 808 | } |
| 809 | if (imap.br_startblock != DELAYSTARTBLOCK) { |
| 810 | /* been converted, ignore */ |
| 811 | goto next_block; |
| 812 | } |
| 813 | WARN_ON(imap.br_blockcount == 0); |
| 814 | |
| 815 | /* |
| 816 | * Note: while we initialise the firstblock/flist pair, they |
| 817 | * should never be used because blocks should never be |
| 818 | * allocated or freed for a delalloc extent and hence we need |
| 819 | * don't cancel or finish them after the xfs_bunmapi() call. |
| 820 | */ |
| 821 | xfs_bmap_init(&flist, &firstblock); |
| 822 | error = xfs_bunmapi(NULL, ip, start_fsb, 1, 0, 1, &firstblock, |
| 823 | &flist, &done); |
| 824 | if (error) |
| 825 | break; |
| 826 | |
| 827 | ASSERT(!flist.xbf_count && !flist.xbf_first); |
| 828 | next_block: |
| 829 | start_fsb++; |
| 830 | remaining--; |
| 831 | } while(remaining > 0); |
| 832 | |
| 833 | return error; |
| 834 | } |