PageRenderTime 87ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/fs/xfs/xfs_bmap.c

https://bitbucket.org/digetx/picasso-kernel
C | 6314 lines | 4572 code | 491 blank | 1251 comment | 1101 complexity | da7ec63f1de34212f526fcbac107a93d MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0, LGPL-2.0
  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_types.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_dir2.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_mount.h"
  36. #include "xfs_itable.h"
  37. #include "xfs_inode_item.h"
  38. #include "xfs_extfree_item.h"
  39. #include "xfs_alloc.h"
  40. #include "xfs_bmap.h"
  41. #include "xfs_rtalloc.h"
  42. #include "xfs_error.h"
  43. #include "xfs_attr_leaf.h"
  44. #include "xfs_quota.h"
  45. #include "xfs_trans_space.h"
  46. #include "xfs_buf_item.h"
  47. #include "xfs_filestream.h"
  48. #include "xfs_vnodeops.h"
  49. #include "xfs_trace.h"
  50. kmem_zone_t *xfs_bmap_free_item_zone;
  51. /*
  52. * Prototypes for internal bmap routines.
  53. */
  54. #ifdef DEBUG
  55. STATIC void
  56. xfs_bmap_check_leaf_extents(
  57. struct xfs_btree_cur *cur,
  58. struct xfs_inode *ip,
  59. int whichfork);
  60. #else
  61. #define xfs_bmap_check_leaf_extents(cur, ip, whichfork) do { } while (0)
  62. #endif
  63. /*
  64. * Called from xfs_bmap_add_attrfork to handle extents format files.
  65. */
  66. STATIC int /* error */
  67. xfs_bmap_add_attrfork_extents(
  68. xfs_trans_t *tp, /* transaction pointer */
  69. xfs_inode_t *ip, /* incore inode pointer */
  70. xfs_fsblock_t *firstblock, /* first block allocated */
  71. xfs_bmap_free_t *flist, /* blocks to free at commit */
  72. int *flags); /* inode logging flags */
  73. /*
  74. * Called from xfs_bmap_add_attrfork to handle local format files.
  75. */
  76. STATIC int /* error */
  77. xfs_bmap_add_attrfork_local(
  78. xfs_trans_t *tp, /* transaction pointer */
  79. xfs_inode_t *ip, /* incore inode pointer */
  80. xfs_fsblock_t *firstblock, /* first block allocated */
  81. xfs_bmap_free_t *flist, /* blocks to free at commit */
  82. int *flags); /* inode logging flags */
  83. /*
  84. * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
  85. * It figures out where to ask the underlying allocator to put the new extent.
  86. */
  87. STATIC int /* error */
  88. xfs_bmap_alloc(
  89. xfs_bmalloca_t *ap); /* bmap alloc argument struct */
  90. /*
  91. * Transform a btree format file with only one leaf node, where the
  92. * extents list will fit in the inode, into an extents format file.
  93. * Since the file extents are already in-core, all we have to do is
  94. * give up the space for the btree root and pitch the leaf block.
  95. */
  96. STATIC int /* error */
  97. xfs_bmap_btree_to_extents(
  98. xfs_trans_t *tp, /* transaction pointer */
  99. xfs_inode_t *ip, /* incore inode pointer */
  100. xfs_btree_cur_t *cur, /* btree cursor */
  101. int *logflagsp, /* inode logging flags */
  102. int whichfork); /* data or attr fork */
  103. /*
  104. * Remove the entry "free" from the free item list. Prev points to the
  105. * previous entry, unless "free" is the head of the list.
  106. */
  107. STATIC void
  108. xfs_bmap_del_free(
  109. xfs_bmap_free_t *flist, /* free item list header */
  110. xfs_bmap_free_item_t *prev, /* previous item on list, if any */
  111. xfs_bmap_free_item_t *free); /* list item to be freed */
  112. /*
  113. * Convert an extents-format file into a btree-format file.
  114. * The new file will have a root block (in the inode) and a single child block.
  115. */
  116. STATIC int /* error */
  117. xfs_bmap_extents_to_btree(
  118. xfs_trans_t *tp, /* transaction pointer */
  119. xfs_inode_t *ip, /* incore inode pointer */
  120. xfs_fsblock_t *firstblock, /* first-block-allocated */
  121. xfs_bmap_free_t *flist, /* blocks freed in xaction */
  122. xfs_btree_cur_t **curp, /* cursor returned to caller */
  123. int wasdel, /* converting a delayed alloc */
  124. int *logflagsp, /* inode logging flags */
  125. int whichfork); /* data or attr fork */
  126. /*
  127. * Convert a local file to an extents file.
  128. * This code is sort of bogus, since the file data needs to get
  129. * logged so it won't be lost. The bmap-level manipulations are ok, though.
  130. */
  131. STATIC int /* error */
  132. xfs_bmap_local_to_extents(
  133. xfs_trans_t *tp, /* transaction pointer */
  134. xfs_inode_t *ip, /* incore inode pointer */
  135. xfs_fsblock_t *firstblock, /* first block allocated in xaction */
  136. xfs_extlen_t total, /* total blocks needed by transaction */
  137. int *logflagsp, /* inode logging flags */
  138. int whichfork, /* data or attr fork */
  139. void (*init_fn)(struct xfs_buf *bp,
  140. struct xfs_inode *ip,
  141. struct xfs_ifork *ifp));
  142. /*
  143. * Search the extents list for the inode, for the extent containing bno.
  144. * If bno lies in a hole, point to the next entry. If bno lies past eof,
  145. * *eofp will be set, and *prevp will contain the last entry (null if none).
  146. * Else, *lastxp will be set to the index of the found
  147. * entry; *gotp will contain the entry.
  148. */
  149. STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */
  150. xfs_bmap_search_extents(
  151. xfs_inode_t *ip, /* incore inode pointer */
  152. xfs_fileoff_t bno, /* block number searched for */
  153. int whichfork, /* data or attr fork */
  154. int *eofp, /* out: end of file found */
  155. xfs_extnum_t *lastxp, /* out: last extent index */
  156. xfs_bmbt_irec_t *gotp, /* out: extent entry found */
  157. xfs_bmbt_irec_t *prevp); /* out: previous extent entry found */
  158. /*
  159. * Compute the worst-case number of indirect blocks that will be used
  160. * for ip's delayed extent of length "len".
  161. */
  162. STATIC xfs_filblks_t
  163. xfs_bmap_worst_indlen(
  164. xfs_inode_t *ip, /* incore inode pointer */
  165. xfs_filblks_t len); /* delayed extent length */
  166. #ifdef DEBUG
  167. /*
  168. * Perform various validation checks on the values being returned
  169. * from xfs_bmapi().
  170. */
  171. STATIC void
  172. xfs_bmap_validate_ret(
  173. xfs_fileoff_t bno,
  174. xfs_filblks_t len,
  175. int flags,
  176. xfs_bmbt_irec_t *mval,
  177. int nmap,
  178. int ret_nmap);
  179. #else
  180. #define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap)
  181. #endif /* DEBUG */
  182. STATIC int
  183. xfs_bmap_count_tree(
  184. xfs_mount_t *mp,
  185. xfs_trans_t *tp,
  186. xfs_ifork_t *ifp,
  187. xfs_fsblock_t blockno,
  188. int levelin,
  189. int *count);
  190. STATIC void
  191. xfs_bmap_count_leaves(
  192. xfs_ifork_t *ifp,
  193. xfs_extnum_t idx,
  194. int numrecs,
  195. int *count);
  196. STATIC void
  197. xfs_bmap_disk_count_leaves(
  198. struct xfs_mount *mp,
  199. struct xfs_btree_block *block,
  200. int numrecs,
  201. int *count);
  202. /*
  203. * Bmap internal routines.
  204. */
  205. STATIC int /* error */
  206. xfs_bmbt_lookup_eq(
  207. struct xfs_btree_cur *cur,
  208. xfs_fileoff_t off,
  209. xfs_fsblock_t bno,
  210. xfs_filblks_t len,
  211. int *stat) /* success/failure */
  212. {
  213. cur->bc_rec.b.br_startoff = off;
  214. cur->bc_rec.b.br_startblock = bno;
  215. cur->bc_rec.b.br_blockcount = len;
  216. return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat);
  217. }
  218. STATIC int /* error */
  219. xfs_bmbt_lookup_ge(
  220. struct xfs_btree_cur *cur,
  221. xfs_fileoff_t off,
  222. xfs_fsblock_t bno,
  223. xfs_filblks_t len,
  224. int *stat) /* success/failure */
  225. {
  226. cur->bc_rec.b.br_startoff = off;
  227. cur->bc_rec.b.br_startblock = bno;
  228. cur->bc_rec.b.br_blockcount = len;
  229. return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat);
  230. }
  231. /*
  232. * Check if the inode needs to be converted to btree format.
  233. */
  234. static inline bool xfs_bmap_needs_btree(struct xfs_inode *ip, int whichfork)
  235. {
  236. return XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
  237. XFS_IFORK_NEXTENTS(ip, whichfork) >
  238. XFS_IFORK_MAXEXT(ip, whichfork);
  239. }
  240. /*
  241. * Check if the inode should be converted to extent format.
  242. */
  243. static inline bool xfs_bmap_wants_extents(struct xfs_inode *ip, int whichfork)
  244. {
  245. return XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE &&
  246. XFS_IFORK_NEXTENTS(ip, whichfork) <=
  247. XFS_IFORK_MAXEXT(ip, whichfork);
  248. }
  249. /*
  250. * Update the record referred to by cur to the value given
  251. * by [off, bno, len, state].
  252. * This either works (return 0) or gets an EFSCORRUPTED error.
  253. */
  254. STATIC int
  255. xfs_bmbt_update(
  256. struct xfs_btree_cur *cur,
  257. xfs_fileoff_t off,
  258. xfs_fsblock_t bno,
  259. xfs_filblks_t len,
  260. xfs_exntst_t state)
  261. {
  262. union xfs_btree_rec rec;
  263. xfs_bmbt_disk_set_allf(&rec.bmbt, off, bno, len, state);
  264. return xfs_btree_update(cur, &rec);
  265. }
  266. /*
  267. * Called from xfs_bmap_add_attrfork to handle btree format files.
  268. */
  269. STATIC int /* error */
  270. xfs_bmap_add_attrfork_btree(
  271. xfs_trans_t *tp, /* transaction pointer */
  272. xfs_inode_t *ip, /* incore inode pointer */
  273. xfs_fsblock_t *firstblock, /* first block allocated */
  274. xfs_bmap_free_t *flist, /* blocks to free at commit */
  275. int *flags) /* inode logging flags */
  276. {
  277. xfs_btree_cur_t *cur; /* btree cursor */
  278. int error; /* error return value */
  279. xfs_mount_t *mp; /* file system mount struct */
  280. int stat; /* newroot status */
  281. mp = ip->i_mount;
  282. if (ip->i_df.if_broot_bytes <= XFS_IFORK_DSIZE(ip))
  283. *flags |= XFS_ILOG_DBROOT;
  284. else {
  285. cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK);
  286. cur->bc_private.b.flist = flist;
  287. cur->bc_private.b.firstblock = *firstblock;
  288. if ((error = xfs_bmbt_lookup_ge(cur, 0, 0, 0, &stat)))
  289. goto error0;
  290. /* must be at least one entry */
  291. XFS_WANT_CORRUPTED_GOTO(stat == 1, error0);
  292. if ((error = xfs_btree_new_iroot(cur, flags, &stat)))
  293. goto error0;
  294. if (stat == 0) {
  295. xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
  296. return XFS_ERROR(ENOSPC);
  297. }
  298. *firstblock = cur->bc_private.b.firstblock;
  299. cur->bc_private.b.allocated = 0;
  300. xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
  301. }
  302. return 0;
  303. error0:
  304. xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
  305. return error;
  306. }
  307. /*
  308. * Called from xfs_bmap_add_attrfork to handle extents format files.
  309. */
  310. STATIC int /* error */
  311. xfs_bmap_add_attrfork_extents(
  312. xfs_trans_t *tp, /* transaction pointer */
  313. xfs_inode_t *ip, /* incore inode pointer */
  314. xfs_fsblock_t *firstblock, /* first block allocated */
  315. xfs_bmap_free_t *flist, /* blocks to free at commit */
  316. int *flags) /* inode logging flags */
  317. {
  318. xfs_btree_cur_t *cur; /* bmap btree cursor */
  319. int error; /* error return value */
  320. if (ip->i_d.di_nextents * sizeof(xfs_bmbt_rec_t) <= XFS_IFORK_DSIZE(ip))
  321. return 0;
  322. cur = NULL;
  323. error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist, &cur, 0,
  324. flags, XFS_DATA_FORK);
  325. if (cur) {
  326. cur->bc_private.b.allocated = 0;
  327. xfs_btree_del_cursor(cur,
  328. error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
  329. }
  330. return error;
  331. }
  332. /*
  333. * Block initialisation functions for local to extent format conversion.
  334. * As these get more complex, they will be moved to the relevant files,
  335. * but for now they are too simple to worry about.
  336. */
  337. STATIC void
  338. xfs_bmap_local_to_extents_init_fn(
  339. struct xfs_buf *bp,
  340. struct xfs_inode *ip,
  341. struct xfs_ifork *ifp)
  342. {
  343. bp->b_ops = &xfs_bmbt_buf_ops;
  344. memcpy(bp->b_addr, ifp->if_u1.if_data, ifp->if_bytes);
  345. }
  346. STATIC void
  347. xfs_symlink_local_to_remote(
  348. struct xfs_buf *bp,
  349. struct xfs_inode *ip,
  350. struct xfs_ifork *ifp)
  351. {
  352. /* remote symlink blocks are not verifiable until CRCs come along */
  353. bp->b_ops = NULL;
  354. memcpy(bp->b_addr, ifp->if_u1.if_data, ifp->if_bytes);
  355. }
  356. /*
  357. * Called from xfs_bmap_add_attrfork to handle local format files. Each
  358. * different data fork content type needs a different callout to do the
  359. * conversion. Some are basic and only require special block initialisation
  360. * callouts for the data formating, others (directories) are so specialised they
  361. * handle everything themselves.
  362. *
  363. * XXX (dgc): investigate whether directory conversion can use the generic
  364. * formatting callout. It should be possible - it's just a very complex
  365. * formatter. it would also require passing the transaction through to the init
  366. * function.
  367. */
  368. STATIC int /* error */
  369. xfs_bmap_add_attrfork_local(
  370. xfs_trans_t *tp, /* transaction pointer */
  371. xfs_inode_t *ip, /* incore inode pointer */
  372. xfs_fsblock_t *firstblock, /* first block allocated */
  373. xfs_bmap_free_t *flist, /* blocks to free at commit */
  374. int *flags) /* inode logging flags */
  375. {
  376. xfs_da_args_t dargs; /* args for dir/attr code */
  377. if (ip->i_df.if_bytes <= XFS_IFORK_DSIZE(ip))
  378. return 0;
  379. if (S_ISDIR(ip->i_d.di_mode)) {
  380. memset(&dargs, 0, sizeof(dargs));
  381. dargs.dp = ip;
  382. dargs.firstblock = firstblock;
  383. dargs.flist = flist;
  384. dargs.total = ip->i_mount->m_dirblkfsbs;
  385. dargs.whichfork = XFS_DATA_FORK;
  386. dargs.trans = tp;
  387. return xfs_dir2_sf_to_block(&dargs);
  388. }
  389. if (S_ISLNK(ip->i_d.di_mode))
  390. return xfs_bmap_local_to_extents(tp, ip, firstblock, 1,
  391. flags, XFS_DATA_FORK,
  392. xfs_symlink_local_to_remote);
  393. return xfs_bmap_local_to_extents(tp, ip, firstblock, 1, flags,
  394. XFS_DATA_FORK,
  395. xfs_bmap_local_to_extents_init_fn);
  396. }
  397. /*
  398. * Convert a delayed allocation to a real allocation.
  399. */
  400. STATIC int /* error */
  401. xfs_bmap_add_extent_delay_real(
  402. struct xfs_bmalloca *bma)
  403. {
  404. struct xfs_bmbt_irec *new = &bma->got;
  405. int diff; /* temp value */
  406. xfs_bmbt_rec_host_t *ep; /* extent entry for idx */
  407. int error; /* error return value */
  408. int i; /* temp state */
  409. xfs_ifork_t *ifp; /* inode fork pointer */
  410. xfs_fileoff_t new_endoff; /* end offset of new entry */
  411. xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
  412. /* left is 0, right is 1, prev is 2 */
  413. int rval=0; /* return value (logging flags) */
  414. int state = 0;/* state bits, accessed thru macros */
  415. xfs_filblks_t da_new; /* new count del alloc blocks used */
  416. xfs_filblks_t da_old; /* old count del alloc blocks used */
  417. xfs_filblks_t temp=0; /* value for da_new calculations */
  418. xfs_filblks_t temp2=0;/* value for da_new calculations */
  419. int tmp_rval; /* partial logging flags */
  420. ifp = XFS_IFORK_PTR(bma->ip, XFS_DATA_FORK);
  421. ASSERT(bma->idx >= 0);
  422. ASSERT(bma->idx <= ifp->if_bytes / sizeof(struct xfs_bmbt_rec));
  423. ASSERT(!isnullstartblock(new->br_startblock));
  424. ASSERT(!bma->cur ||
  425. (bma->cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL));
  426. XFS_STATS_INC(xs_add_exlist);
  427. #define LEFT r[0]
  428. #define RIGHT r[1]
  429. #define PREV r[2]
  430. /*
  431. * Set up a bunch of variables to make the tests simpler.
  432. */
  433. ep = xfs_iext_get_ext(ifp, bma->idx);
  434. xfs_bmbt_get_all(ep, &PREV);
  435. new_endoff = new->br_startoff + new->br_blockcount;
  436. ASSERT(PREV.br_startoff <= new->br_startoff);
  437. ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
  438. da_old = startblockval(PREV.br_startblock);
  439. da_new = 0;
  440. /*
  441. * Set flags determining what part of the previous delayed allocation
  442. * extent is being replaced by a real allocation.
  443. */
  444. if (PREV.br_startoff == new->br_startoff)
  445. state |= BMAP_LEFT_FILLING;
  446. if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
  447. state |= BMAP_RIGHT_FILLING;
  448. /*
  449. * Check and set flags if this segment has a left neighbor.
  450. * Don't set contiguous if the combined extent would be too large.
  451. */
  452. if (bma->idx > 0) {
  453. state |= BMAP_LEFT_VALID;
  454. xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx - 1), &LEFT);
  455. if (isnullstartblock(LEFT.br_startblock))
  456. state |= BMAP_LEFT_DELAY;
  457. }
  458. if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
  459. LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
  460. LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
  461. LEFT.br_state == new->br_state &&
  462. LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
  463. state |= BMAP_LEFT_CONTIG;
  464. /*
  465. * Check and set flags if this segment has a right neighbor.
  466. * Don't set contiguous if the combined extent would be too large.
  467. * Also check for all-three-contiguous being too large.
  468. */
  469. if (bma->idx < bma->ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1) {
  470. state |= BMAP_RIGHT_VALID;
  471. xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx + 1), &RIGHT);
  472. if (isnullstartblock(RIGHT.br_startblock))
  473. state |= BMAP_RIGHT_DELAY;
  474. }
  475. if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
  476. new_endoff == RIGHT.br_startoff &&
  477. new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
  478. new->br_state == RIGHT.br_state &&
  479. new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
  480. ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
  481. BMAP_RIGHT_FILLING)) !=
  482. (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
  483. BMAP_RIGHT_FILLING) ||
  484. LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
  485. <= MAXEXTLEN))
  486. state |= BMAP_RIGHT_CONTIG;
  487. error = 0;
  488. /*
  489. * Switch out based on the FILLING and CONTIG state bits.
  490. */
  491. switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
  492. BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
  493. case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
  494. BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
  495. /*
  496. * Filling in all of a previously delayed allocation extent.
  497. * The left and right neighbors are both contiguous with new.
  498. */
  499. bma->idx--;
  500. trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
  501. xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
  502. LEFT.br_blockcount + PREV.br_blockcount +
  503. RIGHT.br_blockcount);
  504. trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
  505. xfs_iext_remove(bma->ip, bma->idx + 1, 2, state);
  506. bma->ip->i_d.di_nextents--;
  507. if (bma->cur == NULL)
  508. rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
  509. else {
  510. rval = XFS_ILOG_CORE;
  511. error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff,
  512. RIGHT.br_startblock,
  513. RIGHT.br_blockcount, &i);
  514. if (error)
  515. goto done;
  516. XFS_WANT_CORRUPTED_GOTO(i == 1, done);
  517. error = xfs_btree_delete(bma->cur, &i);
  518. if (error)
  519. goto done;
  520. XFS_WANT_CORRUPTED_GOTO(i == 1, done);
  521. error = xfs_btree_decrement(bma->cur, 0, &i);
  522. if (error)
  523. goto done;
  524. XFS_WANT_CORRUPTED_GOTO(i == 1, done);
  525. error = xfs_bmbt_update(bma->cur, LEFT.br_startoff,
  526. LEFT.br_startblock,
  527. LEFT.br_blockcount +
  528. PREV.br_blockcount +
  529. RIGHT.br_blockcount, LEFT.br_state);
  530. if (error)
  531. goto done;
  532. }
  533. break;
  534. case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
  535. /*
  536. * Filling in all of a previously delayed allocation extent.
  537. * The left neighbor is contiguous, the right is not.
  538. */
  539. bma->idx--;
  540. trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
  541. xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
  542. LEFT.br_blockcount + PREV.br_blockcount);
  543. trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
  544. xfs_iext_remove(bma->ip, bma->idx + 1, 1, state);
  545. if (bma->cur == NULL)
  546. rval = XFS_ILOG_DEXT;
  547. else {
  548. rval = 0;
  549. error = xfs_bmbt_lookup_eq(bma->cur, LEFT.br_startoff,
  550. LEFT.br_startblock, LEFT.br_blockcount,
  551. &i);
  552. if (error)
  553. goto done;
  554. XFS_WANT_CORRUPTED_GOTO(i == 1, done);
  555. error = xfs_bmbt_update(bma->cur, LEFT.br_startoff,
  556. LEFT.br_startblock,
  557. LEFT.br_blockcount +
  558. PREV.br_blockcount, LEFT.br_state);
  559. if (error)
  560. goto done;
  561. }
  562. break;
  563. case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
  564. /*
  565. * Filling in all of a previously delayed allocation extent.
  566. * The right neighbor is contiguous, the left is not.
  567. */
  568. trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
  569. xfs_bmbt_set_startblock(ep, new->br_startblock);
  570. xfs_bmbt_set_blockcount(ep,
  571. PREV.br_blockcount + RIGHT.br_blockcount);
  572. trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
  573. xfs_iext_remove(bma->ip, bma->idx + 1, 1, state);
  574. if (bma->cur == NULL)
  575. rval = XFS_ILOG_DEXT;
  576. else {
  577. rval = 0;
  578. error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff,
  579. RIGHT.br_startblock,
  580. RIGHT.br_blockcount, &i);
  581. if (error)
  582. goto done;
  583. XFS_WANT_CORRUPTED_GOTO(i == 1, done);
  584. error = xfs_bmbt_update(bma->cur, PREV.br_startoff,
  585. new->br_startblock,
  586. PREV.br_blockcount +
  587. RIGHT.br_blockcount, PREV.br_state);
  588. if (error)
  589. goto done;
  590. }
  591. break;
  592. case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
  593. /*
  594. * Filling in all of a previously delayed allocation extent.
  595. * Neither the left nor right neighbors are contiguous with
  596. * the new one.
  597. */
  598. trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
  599. xfs_bmbt_set_startblock(ep, new->br_startblock);
  600. trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
  601. bma->ip->i_d.di_nextents++;
  602. if (bma->cur == NULL)
  603. rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
  604. else {
  605. rval = XFS_ILOG_CORE;
  606. error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
  607. new->br_startblock, new->br_blockcount,
  608. &i);
  609. if (error)
  610. goto done;
  611. XFS_WANT_CORRUPTED_GOTO(i == 0, done);
  612. bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
  613. error = xfs_btree_insert(bma->cur, &i);
  614. if (error)
  615. goto done;
  616. XFS_WANT_CORRUPTED_GOTO(i == 1, done);
  617. }
  618. break;
  619. case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
  620. /*
  621. * Filling in the first part of a previous delayed allocation.
  622. * The left neighbor is contiguous.
  623. */
  624. trace_xfs_bmap_pre_update(bma->ip, bma->idx - 1, state, _THIS_IP_);
  625. xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx - 1),
  626. LEFT.br_blockcount + new->br_blockcount);
  627. xfs_bmbt_set_startoff(ep,
  628. PREV.br_startoff + new->br_blockcount);
  629. trace_xfs_bmap_post_update(bma->ip, bma->idx - 1, state, _THIS_IP_);
  630. temp = PREV.br_blockcount - new->br_blockcount;
  631. trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
  632. xfs_bmbt_set_blockcount(ep, temp);
  633. if (bma->cur == NULL)
  634. rval = XFS_ILOG_DEXT;
  635. else {
  636. rval = 0;
  637. error = xfs_bmbt_lookup_eq(bma->cur, LEFT.br_startoff,
  638. LEFT.br_startblock, LEFT.br_blockcount,
  639. &i);
  640. if (error)
  641. goto done;
  642. XFS_WANT_CORRUPTED_GOTO(i == 1, done);
  643. error = xfs_bmbt_update(bma->cur, LEFT.br_startoff,
  644. LEFT.br_startblock,
  645. LEFT.br_blockcount +
  646. new->br_blockcount,
  647. LEFT.br_state);
  648. if (error)
  649. goto done;
  650. }
  651. da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
  652. startblockval(PREV.br_startblock));
  653. xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
  654. trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
  655. bma->idx--;
  656. break;
  657. case BMAP_LEFT_FILLING:
  658. /*
  659. * Filling in the first part of a previous delayed allocation.
  660. * The left neighbor is not contiguous.
  661. */
  662. trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
  663. xfs_bmbt_set_startoff(ep, new_endoff);
  664. temp = PREV.br_blockcount - new->br_blockcount;
  665. xfs_bmbt_set_blockcount(ep, temp);
  666. xfs_iext_insert(bma->ip, bma->idx, 1, new, state);
  667. bma->ip->i_d.di_nextents++;
  668. if (bma->cur == NULL)
  669. rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
  670. else {
  671. rval = XFS_ILOG_CORE;
  672. error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
  673. new->br_startblock, new->br_blockcount,
  674. &i);
  675. if (error)
  676. goto done;
  677. XFS_WANT_CORRUPTED_GOTO(i == 0, done);
  678. bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
  679. error = xfs_btree_insert(bma->cur, &i);
  680. if (error)
  681. goto done;
  682. XFS_WANT_CORRUPTED_GOTO(i == 1, done);
  683. }
  684. if (xfs_bmap_needs_btree(bma->ip, XFS_DATA_FORK)) {
  685. error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
  686. bma->firstblock, bma->flist,
  687. &bma->cur, 1, &tmp_rval, XFS_DATA_FORK);
  688. rval |= tmp_rval;
  689. if (error)
  690. goto done;
  691. }
  692. da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
  693. startblockval(PREV.br_startblock) -
  694. (bma->cur ? bma->cur->bc_private.b.allocated : 0));
  695. ep = xfs_iext_get_ext(ifp, bma->idx + 1);
  696. xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
  697. trace_xfs_bmap_post_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
  698. break;
  699. case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
  700. /*
  701. * Filling in the last part of a previous delayed allocation.
  702. * The right neighbor is contiguous with the new allocation.
  703. */
  704. temp = PREV.br_blockcount - new->br_blockcount;
  705. trace_xfs_bmap_pre_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
  706. xfs_bmbt_set_blockcount(ep, temp);
  707. xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, bma->idx + 1),
  708. new->br_startoff, new->br_startblock,
  709. new->br_blockcount + RIGHT.br_blockcount,
  710. RIGHT.br_state);
  711. trace_xfs_bmap_post_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
  712. if (bma->cur == NULL)
  713. rval = XFS_ILOG_DEXT;
  714. else {
  715. rval = 0;
  716. error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff,
  717. RIGHT.br_startblock,
  718. RIGHT.br_blockcount, &i);
  719. if (error)
  720. goto done;
  721. XFS_WANT_CORRUPTED_GOTO(i == 1, done);
  722. error = xfs_bmbt_update(bma->cur, new->br_startoff,
  723. new->br_startblock,
  724. new->br_blockcount +
  725. RIGHT.br_blockcount,
  726. RIGHT.br_state);
  727. if (error)
  728. goto done;
  729. }
  730. da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
  731. startblockval(PREV.br_startblock));
  732. trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
  733. xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
  734. trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
  735. bma->idx++;
  736. break;
  737. case BMAP_RIGHT_FILLING:
  738. /*
  739. * Filling in the last part of a previous delayed allocation.
  740. * The right neighbor is not contiguous.
  741. */
  742. temp = PREV.br_blockcount - new->br_blockcount;
  743. trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
  744. xfs_bmbt_set_blockcount(ep, temp);
  745. xfs_iext_insert(bma->ip, bma->idx + 1, 1, new, state);
  746. bma->ip->i_d.di_nextents++;
  747. if (bma->cur == NULL)
  748. rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
  749. else {
  750. rval = XFS_ILOG_CORE;
  751. error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
  752. new->br_startblock, new->br_blockcount,
  753. &i);
  754. if (error)
  755. goto done;
  756. XFS_WANT_CORRUPTED_GOTO(i == 0, done);
  757. bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
  758. error = xfs_btree_insert(bma->cur, &i);
  759. if (error)
  760. goto done;
  761. XFS_WANT_CORRUPTED_GOTO(i == 1, done);
  762. }
  763. if (xfs_bmap_needs_btree(bma->ip, XFS_DATA_FORK)) {
  764. error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
  765. bma->firstblock, bma->flist, &bma->cur, 1,
  766. &tmp_rval, XFS_DATA_FORK);
  767. rval |= tmp_rval;
  768. if (error)
  769. goto done;
  770. }
  771. da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
  772. startblockval(PREV.br_startblock) -
  773. (bma->cur ? bma->cur->bc_private.b.allocated : 0));
  774. ep = xfs_iext_get_ext(ifp, bma->idx);
  775. xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
  776. trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
  777. bma->idx++;
  778. break;
  779. case 0:
  780. /*
  781. * Filling in the middle part of a previous delayed allocation.
  782. * Contiguity is impossible here.
  783. * This case is avoided almost all the time.
  784. *
  785. * We start with a delayed allocation:
  786. *
  787. * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+
  788. * PREV @ idx
  789. *
  790. * and we are allocating:
  791. * +rrrrrrrrrrrrrrrrr+
  792. * new
  793. *
  794. * and we set it up for insertion as:
  795. * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+
  796. * new
  797. * PREV @ idx LEFT RIGHT
  798. * inserted at idx + 1
  799. */
  800. temp = new->br_startoff - PREV.br_startoff;
  801. temp2 = PREV.br_startoff + PREV.br_blockcount - new_endoff;
  802. trace_xfs_bmap_pre_update(bma->ip, bma->idx, 0, _THIS_IP_);
  803. xfs_bmbt_set_blockcount(ep, temp); /* truncate PREV */
  804. LEFT = *new;
  805. RIGHT.br_state = PREV.br_state;
  806. RIGHT.br_startblock = nullstartblock(
  807. (int)xfs_bmap_worst_indlen(bma->ip, temp2));
  808. RIGHT.br_startoff = new_endoff;
  809. RIGHT.br_blockcount = temp2;
  810. /* insert LEFT (r[0]) and RIGHT (r[1]) at the same time */
  811. xfs_iext_insert(bma->ip, bma->idx + 1, 2, &LEFT, state);
  812. bma->ip->i_d.di_nextents++;
  813. if (bma->cur == NULL)
  814. rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
  815. else {
  816. rval = XFS_ILOG_CORE;
  817. error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
  818. new->br_startblock, new->br_blockcount,
  819. &i);
  820. if (error)
  821. goto done;
  822. XFS_WANT_CORRUPTED_GOTO(i == 0, done);
  823. bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
  824. error = xfs_btree_insert(bma->cur, &i);
  825. if (error)
  826. goto done;
  827. XFS_WANT_CORRUPTED_GOTO(i == 1, done);
  828. }
  829. if (xfs_bmap_needs_btree(bma->ip, XFS_DATA_FORK)) {
  830. error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
  831. bma->firstblock, bma->flist, &bma->cur,
  832. 1, &tmp_rval, XFS_DATA_FORK);
  833. rval |= tmp_rval;
  834. if (error)
  835. goto done;
  836. }
  837. temp = xfs_bmap_worst_indlen(bma->ip, temp);
  838. temp2 = xfs_bmap_worst_indlen(bma->ip, temp2);
  839. diff = (int)(temp + temp2 - startblockval(PREV.br_startblock) -
  840. (bma->cur ? bma->cur->bc_private.b.allocated : 0));
  841. if (diff > 0) {
  842. error = xfs_icsb_modify_counters(bma->ip->i_mount,
  843. XFS_SBS_FDBLOCKS,
  844. -((int64_t)diff), 0);
  845. ASSERT(!error);
  846. if (error)
  847. goto done;
  848. }
  849. ep = xfs_iext_get_ext(ifp, bma->idx);
  850. xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
  851. trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
  852. trace_xfs_bmap_pre_update(bma->ip, bma->idx + 2, state, _THIS_IP_);
  853. xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, bma->idx + 2),
  854. nullstartblock((int)temp2));
  855. trace_xfs_bmap_post_update(bma->ip, bma->idx + 2, state, _THIS_IP_);
  856. bma->idx++;
  857. da_new = temp + temp2;
  858. break;
  859. case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
  860. case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
  861. case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
  862. case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
  863. case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
  864. case BMAP_LEFT_CONTIG:
  865. case BMAP_RIGHT_CONTIG:
  866. /*
  867. * These cases are all impossible.
  868. */
  869. ASSERT(0);
  870. }
  871. /* convert to a btree if necessary */
  872. if (xfs_bmap_needs_btree(bma->ip, XFS_DATA_FORK)) {
  873. int tmp_logflags; /* partial log flag return val */
  874. ASSERT(bma->cur == NULL);
  875. error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
  876. bma->firstblock, bma->flist, &bma->cur,
  877. da_old > 0, &tmp_logflags, XFS_DATA_FORK);
  878. bma->logflags |= tmp_logflags;
  879. if (error)
  880. goto done;
  881. }
  882. /* adjust for changes in reserved delayed indirect blocks */
  883. if (da_old || da_new) {
  884. temp = da_new;
  885. if (bma->cur)
  886. temp += bma->cur->bc_private.b.allocated;
  887. ASSERT(temp <= da_old);
  888. if (temp < da_old)
  889. xfs_icsb_modify_counters(bma->ip->i_mount,
  890. XFS_SBS_FDBLOCKS,
  891. (int64_t)(da_old - temp), 0);
  892. }
  893. /* clear out the allocated field, done with it now in any case. */
  894. if (bma->cur)
  895. bma->cur->bc_private.b.allocated = 0;
  896. xfs_bmap_check_leaf_extents(bma->cur, bma->ip, XFS_DATA_FORK);
  897. done:
  898. bma->logflags |= rval;
  899. return error;
  900. #undef LEFT
  901. #undef RIGHT
  902. #undef PREV
  903. }
  904. /*
  905. * Convert an unwritten allocation to a real allocation or vice versa.
  906. */
  907. STATIC int /* error */
  908. xfs_bmap_add_extent_unwritten_real(
  909. struct xfs_trans *tp,
  910. xfs_inode_t *ip, /* incore inode pointer */
  911. xfs_extnum_t *idx, /* extent number to update/insert */
  912. xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
  913. xfs_bmbt_irec_t *new, /* new data to add to file extents */
  914. xfs_fsblock_t *first, /* pointer to firstblock variable */
  915. xfs_bmap_free_t *flist, /* list of extents to be freed */
  916. int *logflagsp) /* inode logging flags */
  917. {
  918. xfs_btree_cur_t *cur; /* btree cursor */
  919. xfs_bmbt_rec_host_t *ep; /* extent entry for idx */
  920. int error; /* error return value */
  921. int i; /* temp state */
  922. xfs_ifork_t *ifp; /* inode fork pointer */
  923. xfs_fileoff_t new_endoff; /* end offset of new entry */
  924. xfs_exntst_t newext; /* new extent state */
  925. xfs_exntst_t oldext; /* old extent state */
  926. xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
  927. /* left is 0, right is 1, prev is 2 */
  928. int rval=0; /* return value (logging flags) */
  929. int state = 0;/* state bits, accessed thru macros */
  930. *logflagsp = 0;
  931. cur = *curp;
  932. ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
  933. ASSERT(*idx >= 0);
  934. ASSERT(*idx <= ifp->if_bytes / sizeof(struct xfs_bmbt_rec));
  935. ASSERT(!isnullstartblock(new->br_startblock));
  936. XFS_STATS_INC(xs_add_exlist);
  937. #define LEFT r[0]
  938. #define RIGHT r[1]
  939. #define PREV r[2]
  940. /*
  941. * Set up a bunch of variables to make the tests simpler.
  942. */
  943. error = 0;
  944. ep = xfs_iext_get_ext(ifp, *idx);
  945. xfs_bmbt_get_all(ep, &PREV);
  946. newext = new->br_state;
  947. oldext = (newext == XFS_EXT_UNWRITTEN) ?
  948. XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
  949. ASSERT(PREV.br_state == oldext);
  950. new_endoff = new->br_startoff + new->br_blockcount;
  951. ASSERT(PREV.br_startoff <= new->br_startoff);
  952. ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
  953. /*
  954. * Set flags determining what part of the previous oldext allocation
  955. * extent is being replaced by a newext allocation.
  956. */
  957. if (PREV.br_startoff == new->br_startoff)
  958. state |= BMAP_LEFT_FILLING;
  959. if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
  960. state |= BMAP_RIGHT_FILLING;
  961. /*
  962. * Check and set flags if this segment has a left neighbor.
  963. * Don't set contiguous if the combined extent would be too large.
  964. */
  965. if (*idx > 0) {
  966. state |= BMAP_LEFT_VALID;
  967. xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &LEFT);
  968. if (isnullstartblock(LEFT.br_startblock))
  969. state |= BMAP_LEFT_DELAY;
  970. }
  971. if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
  972. LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
  973. LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
  974. LEFT.br_state == newext &&
  975. LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
  976. state |= BMAP_LEFT_CONTIG;
  977. /*
  978. * Check and set flags if this segment has a right neighbor.
  979. * Don't set contiguous if the combined extent would be too large.
  980. * Also check for all-three-contiguous being too large.
  981. */
  982. if (*idx < ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1) {
  983. state |= BMAP_RIGHT_VALID;
  984. xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx + 1), &RIGHT);
  985. if (isnullstartblock(RIGHT.br_startblock))
  986. state |= BMAP_RIGHT_DELAY;
  987. }
  988. if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
  989. new_endoff == RIGHT.br_startoff &&
  990. new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
  991. newext == RIGHT.br_state &&
  992. new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
  993. ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
  994. BMAP_RIGHT_FILLING)) !=
  995. (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
  996. BMAP_RIGHT_FILLING) ||
  997. LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
  998. <= MAXEXTLEN))
  999. state |= BMAP_RIGHT_CONTIG;
  1000. /*
  1001. * Switch out based on the FILLING and CONTIG state bits.
  1002. */
  1003. switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
  1004. BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
  1005. case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
  1006. BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
  1007. /*
  1008. * Setting all of a previous oldext extent to newext.
  1009. * The left and right neighbors are both contiguous with new.
  1010. */
  1011. --*idx;
  1012. trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
  1013. xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
  1014. LEFT.br_blockcount + PREV.br_blockcount +
  1015. RIGHT.br_blockcount);
  1016. trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
  1017. xfs_iext_remove(ip, *idx + 1, 2, state);
  1018. ip->i_d.di_nextents -= 2;
  1019. if (cur == NULL)
  1020. rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
  1021. else {
  1022. rval = XFS_ILOG_CORE;
  1023. if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
  1024. RIGHT.br_startblock,
  1025. RIGHT.br_blockcount, &i)))
  1026. goto done;
  1027. XFS_WANT_CORRUPTED_GOTO(i == 1, done);
  1028. if ((error = xfs_btree_delete(cur, &i)))
  1029. goto done;
  1030. XFS_WANT_CORRUPTED_GOTO(i == 1, done);
  1031. if ((error = xfs_btree_decrement(cur, 0, &i)))
  1032. goto done;
  1033. XFS_WANT_CORRUPTED_GOTO(i == 1, done);
  1034. if ((error = xfs_btree_delete(cur, &i)))
  1035. goto done;
  1036. XFS_WANT_CORRUPTED_GOTO(i == 1, done);
  1037. if ((error = xfs_btree_decrement(cur, 0, &i)))
  1038. goto done;
  1039. XFS_WANT_CORRUPTED_GOTO(i == 1, done);
  1040. if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
  1041. LEFT.br_startblock,
  1042. LEFT.br_blockcount + PREV.br_blockcount +
  1043. RIGHT.br_blockcount, LEFT.br_state)))
  1044. goto done;
  1045. }
  1046. break;
  1047. case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
  1048. /*
  1049. * Setting all of a previous oldext extent to newext.
  1050. * The left neighbor is contiguous, the right is not.
  1051. */
  1052. --*idx;
  1053. trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
  1054. xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
  1055. LEFT.br_blockcount + PREV.br_blockcount);
  1056. trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
  1057. xfs_iext_remove(ip, *idx + 1, 1, state);
  1058. ip->i_d.di_nextents--;
  1059. if (cur == NULL)
  1060. rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
  1061. else {
  1062. rval = XFS_ILOG_CORE;
  1063. if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
  1064. PREV.br_startblock, PREV.br_blockcount,
  1065. &i)))
  1066. goto done;
  1067. XFS_WANT_CORRUPTED_GOTO(i == 1, done);
  1068. if ((error = xfs_btree_delete(cur, &i)))
  1069. goto done;
  1070. XFS_WANT_CORRUPTED_GOTO(i == 1, done);
  1071. if ((error = xfs_btree_decrement(cur, 0, &i)))
  1072. goto done;
  1073. XFS_WANT_CORRUPTED_GOTO(i == 1, done);
  1074. if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
  1075. LEFT.br_startblock,
  1076. LEFT.br_blockcount + PREV.br_blockcount,
  1077. LEFT.br_state)))
  1078. goto done;
  1079. }
  1080. break;
  1081. case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
  1082. /*
  1083. * Setting all of a previous oldext extent to newext.
  1084. * The right neighbor is contiguous, the left is not.
  1085. */
  1086. trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
  1087. xfs_bmbt_set_blockcount(ep,
  1088. PREV.br_blockcount + RIGHT.br_blockcount);
  1089. xfs_bmbt_set_state(ep, newext);
  1090. trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
  1091. xfs_iext_remove(ip, *idx + 1, 1, state);
  1092. ip->i_d.di_nextents--;
  1093. if (cur == NULL)
  1094. rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
  1095. else {
  1096. rval = XFS_ILOG_CORE;
  1097. if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
  1098. RIGHT.br_startblock,
  1099. RIGHT.br_blockcount, &i)))
  1100. goto done;
  1101. XFS_WANT_CORRUPTED_GOTO(i == 1, done);
  1102. if ((error = xfs_btree_delete(cur, &i)))
  1103. goto done;
  1104. XFS_WANT_CORRUPTED_GOTO(i == 1, done);
  1105. if ((error = xfs_btree_decrement(cur, 0, &i)))
  1106. goto done;
  1107. XFS_WANT_CORRUPTED_GOTO(i == 1, done);
  1108. if ((error = xfs_bmbt_update(cur, new->br_startoff,
  1109. new->br_startblock,
  1110. new->br_blockcount + RIGHT.br_blockcount,
  1111. newext)))
  1112. goto done;
  1113. }
  1114. break;
  1115. case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
  1116. /*
  1117. * Setting all of a previous oldext extent to newext.
  1118. * Neither the left nor right neighbors are contiguous with
  1119. * the new one.
  1120. */
  1121. trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
  1122. xfs_bmbt_set_state(ep, newext);
  1123. trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
  1124. if (cur == NULL)
  1125. rval = XFS_ILOG_DEXT;
  1126. else {
  1127. rval = 0;
  1128. if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
  1129. new->br_startblock, new->br_blockcount,
  1130. &i)))
  1131. goto done;
  1132. XFS_WANT_CORRUPTED_GOTO(i == 1, done);
  1133. if ((error = xfs_bmbt_update(cur, new->br_startoff,
  1134. new->br_startblock, new->br_blockcount,
  1135. newext)))
  1136. goto done;
  1137. }
  1138. break;
  1139. case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
  1140. /*
  1141. * Setting the first part of a previous oldext extent to newext.
  1142. * The left neighbor is contiguous.
  1143. */
  1144. trace_xfs_bmap_pre_update(ip, *idx - 1, state, _THIS_IP_);
  1145. xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx - 1),
  1146. LEFT.br_blockcount + new->br_blockcount);
  1147. xfs_bmbt_set_startoff(ep,
  1148. PREV.br_startoff + new->br_blockcount);
  1149. trace_xfs_bmap_post_update(ip, *idx - 1, state, _THIS_IP_);
  1150. trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
  1151. xfs_bmbt_set_startblock(ep,
  1152. new->br_startblock + new->br_blockcount);
  1153. xfs_bmbt_set_blockcount(ep,
  1154. PREV.br_blockcount - new->br_blockcount);
  1155. trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
  1156. --*idx;
  1157. if (cur == NULL)
  1158. rval = XFS_ILOG_DEXT;
  1159. else {
  1160. rval = 0;
  1161. if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
  1162. PREV.br_startblock, PREV.br_blockcount,
  1163. &i)))
  1164. goto done;
  1165. XFS_WANT_CORRUPTED_GOTO(i == 1, done);
  1166. if ((error = xfs_bmbt_update(cur,
  1167. PREV.br_startoff + new->br_blockcount,
  1168. PREV.br_startblock + new->br_blockcount,
  1169. PREV.br_blockcount - new->br_blockcount,
  1170. oldext)))
  1171. goto done;
  1172. if ((error = xfs_btree_decrement(cur, 0, &i)))
  1173. goto done;
  1174. error = xfs_bmbt_update(cur, LEFT.br_startoff,
  1175. LEFT.br_startblock,
  1176. LEFT.br_blockcount + new->br_blockcount,
  1177. LEFT.br_state);
  1178. if (error)
  1179. goto done;
  1180. }
  1181. break;
  1182. case BMAP_LEFT_FILLING:
  1183. /*
  1184. * Setting the first part of a previous oldext extent to newext.
  1185. * The left neighbor is not contiguous.
  1186. */
  1187. trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
  1188. ASSERT(ep && xfs_bmbt_get_state(ep) == oldext);
  1189. xfs_bmbt_set_startoff(ep, new_endoff);
  1190. xfs_bmbt_set_blockcount(ep,
  1191. PREV.br_blockcount - new->br_blockcount);
  1192. xfs_bmbt_set_startblock(ep,
  1193. new->br_startblock + new->br_blockcount);
  1194. trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
  1195. xfs_iext_insert(ip, *idx, 1, new, state);
  1196. ip->i_d.di_nextents++;
  1197. if (cur == NULL)
  1198. rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
  1199. else {
  1200. rval = XFS_ILOG_CORE;
  1201. if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
  1202. PREV.br_startblock, PREV.br_blockcount,
  1203. &i)))
  1204. goto done;
  1205. XFS_WANT_CORRUPTED_GOTO(i == 1, done);
  1206. if ((error = xfs_bmbt_update(cur,
  1207. PREV.br_startoff + new->br_blockcount,
  1208. PREV.br_startblock + new->br_blockcount,
  1209. PREV.br_blockcount - new->br_blockcount,
  1210. oldext)))
  1211. goto done;
  1212. cur->bc_rec.b = *new;
  1213. if ((error = xfs_btree_insert(cur, &i)))
  1214. goto done;
  1215. XFS_WANT_CORRUPTED_GOTO(i == 1, done);
  1216. }
  1217. break;
  1218. case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
  1219. /*
  1220. * Setting the last part of a previous oldext extent to newext.
  1221. * The right neighbor is contiguous with the new allocation.
  1222. */
  1223. trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
  1224. xfs_bmbt_set_blockcount(ep,
  1225. PREV.br_blockcount - new->br_blockcount);
  1226. trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
  1227. ++*idx;
  1228. trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
  1229. xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx),
  1230. new->br_startoff, new->br_startblock,
  1231. new->br_blockcount + RIGHT.br_blockcount, newext);
  1232. trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
  1233. if (cur == NULL)
  1234. rval = XFS_ILOG_DEXT;
  1235. else {
  1236. rval = 0;
  1237. if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
  1238. PREV.br_startblock,
  1239. PREV.br_blockcount, &i)))
  1240. goto done;
  1241. XFS_WANT_CORRUPTED_GOTO(i == 1, done);
  1242. if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
  1243. PREV.br_startblock,
  1244. PREV.br_blockcount - new->br_blockcount,
  1245. oldext)))
  1246. goto done;
  1247. if ((error = xfs_btree_increment(cur, 0, &i)))
  1248. goto done;
  1249. if ((error = xfs_bmbt_update(cur, new->br_startoff,
  1250. new->br_startblock,
  1251. new->br_blockcount + RIGHT.br_blockcount,
  1252. newext)))
  1253. goto done;
  1254. }
  1255. break;
  1256. case BMAP_RIGHT_FILLING:
  1257. /*
  1258. * Setting the last part of a previous oldext extent to newext.
  1259. * The right neighbor is not contiguous.
  1260. */
  1261. trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
  1262. xfs_bmbt_set_blockcount(ep,
  1263. PREV.br_blockcount - new->br_blockcount);
  1264. trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
  1265. ++*idx;
  1266. xfs_iext_insert(ip, *idx, 1, new, state);
  1267. ip->i_d.di_nextents++;
  1268. if (cur == NULL)
  1269. rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
  1270. else {
  1271. rval = XFS_ILOG_CORE;
  1272. if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
  1273. PREV.br_startblock, PREV.br_blockcount,
  1274. &i)))
  1275. goto done;
  1276. XFS_WANT_CORRUPTED_GOTO(i == 1, done);
  1277. if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
  1278. PREV.br_startblock,
  1279. PREV.br_blockcount - new->br_blockcount,
  1280. oldext)))
  1281. goto done;
  1282. if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
  1283. new->br_startblock, new->br_blockcount,
  1284. &i)))
  1285. goto done;
  1286. XFS_WANT_CORRUPTED_GOTO(i == 0, done);
  1287. cur->bc_rec.b.br_state = XFS_EXT_NORM;
  1288. if ((error = xfs_btree_insert(cur, &i)))
  1289. goto done;
  1290. XFS_WANT_CORRUPTED_GOTO(i == 1, done);
  1291. }
  1292. break;
  1293. case 0:
  1294. /*
  1295. * Setting the middle part of a previous oldext extent to
  1296. * newext. Contiguity is impossible here.
  1297. * One extent becomes three extents.
  1298. */
  1299. trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
  1300. xfs_bmbt_set_blockcount(ep,
  1301. new->br_startoff - PREV.br_startoff);
  1302. trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
  1303. r[0] = *new;
  1304. r[1].br_startoff = new_endoff;
  1305. r[1].br_blockcount =
  1306. PREV.br_startoff + PREV.br_blockcount - new_endoff;
  1307. r[1].br_startblock = new->br_startblock + new->br_blockcount;
  1308. r[1].br_state = oldext;
  1309. ++*idx;
  1310. xfs_iext_insert(ip, *idx, 2, &r[0], state);
  1311. ip->i_d.di_nextents += 2;
  1312. if (cur == NULL)
  1313. rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
  1314. else {
  1315. rval = XFS_ILOG_CORE;
  1316. if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
  1317. PREV.br_startblock, PREV.br_blockcount,
  1318. &i)))
  1319. goto done;
  1320. XFS_WANT_CORRUPTED_GOTO(i == 1, done);
  1321. /* new right extent - oldext */
  1322. if ((error = xfs_bmbt_update(cur, r[1].br_startoff,
  1323. r[1].br_startblock, r[1].br_blockcount,
  1324. r[1].br_state)))
  1325. goto done;
  1326. /* new left extent - oldext */
  1327. cur->bc_rec.b = PREV;
  1328. cur->bc_rec.b.br_blockcount =
  1329. new->br_startoff - PREV.br_startoff;
  1330. if ((error = xfs_btree_insert(cur, &i)))
  1331. goto done;
  1332. XFS_WANT_CORRUPTED_GOTO(i == 1, done);
  1333. /*
  1334. * Reset the cursor to the position of the new extent
  1335. * we are about to insert as we can't trust it after
  1336. * the previous insert.
  1337. */
  1338. if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
  1339. new->br_startblock, new->br_blockcount,
  1340. &i)))
  1341. goto done;
  1342. XFS_WANT_CORRUPTED_GOTO(i == 0, done);
  1343. /* new middle extent - newext */
  1344. cur->bc_rec.b.br_state = new->br_state;
  1345. if ((error = xfs_btree_insert(cur, &i)))
  1346. goto done;
  1347. XFS_WANT_CORRUPTED_GOTO(i == 1, done);
  1348. }
  1349. break;
  1350. case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
  1351. case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
  1352. case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
  1353. case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
  1354. case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
  1355. case BMAP_LEFT_CONTIG:
  1356. case BMAP_RIGHT_CONTIG:
  1357. /*
  1358. * These cases are all impossible.
  1359. */
  1360. ASSERT(0);
  1361. }
  1362. /* convert to a btree if necessary */
  1363. if (xfs_bmap_needs_btree(ip, XFS_DATA_FORK)) {
  1364. int tmp_logflags; /* partial log flag return val */
  1365. ASSERT(cur == NULL);
  1366. error = xfs_bmap_extents_to_btree(tp, ip, first, flist, &cur,
  1367. 0, &tmp_logflags, XFS_DATA_FORK);
  1368. *logflagsp |= tmp_logflags;
  1369. if (error)
  1370. goto done;
  1371. }
  1372. /* clear out the allocated field, done with it now in any case. */
  1373. if (cur) {
  1374. cur->bc_private.b.allocated = 0;
  1375. *curp = cur;
  1376. }
  1377. xfs_bmap_check_leaf_extents(*curp, ip, XFS_DATA_FORK);
  1378. done:
  1379. *logflagsp |= rval;
  1380. return error;
  1381. #undef LEFT
  1382. #undef RIGHT
  1383. #undef PREV
  1384. }
  1385. /*
  1386. * Convert a hole to a delayed allocation.
  1387. */
  1388. STATIC void
  1389. xfs_bmap_add_extent_hole_delay(
  1390. xfs_inode_t *ip, /* incore inode pointer */
  1391. xfs_extnum_t *idx, /* extent number to update/insert */
  1392. xfs_bmbt_irec_t *new) /* new data to add to file extents */
  1393. {
  1394. xfs_ifork_t *ifp; /* inode fork pointer */
  1395. xfs_bmbt_irec_t left; /* left neighbor extent entry */
  1396. xfs_filblks_t newlen=0; /* new indirect size */
  1397. xfs_filblks_t oldlen=0; /* old indirect size */
  1398. xfs_bmbt_irec_t right; /* right neighbor extent entry */
  1399. int state; /* state bits, accessed thru macros */
  1400. xfs_filblks_t temp=0; /* temp for indirect calculations */
  1401. ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
  1402. state = 0;
  1403. ASSERT(isnullstartblock(new->br_startblock));
  1404. /*
  1405. * Check and set flags if this segment has a left neighbor
  1406. */
  1407. if (*idx > 0) {
  1408. state |= BMAP_LEFT_VALID;
  1409. xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &left);
  1410. if (isnullstartblock(left.br_startblock))
  1411. state |= BMAP_LEFT_DELAY;
  1412. }
  1413. /*
  1414. * Check and set flags if the current (right) segment exists.
  1415. * If it doesn't exist, we're converting the hole at end-of-file.
  1416. */
  1417. if (*idx < ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t)) {
  1418. state |= BMAP_RIGHT_VALID;
  1419. xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx), &right);
  1420. if (isnullstartblock(right.br_startblock))
  1421. state |= BMAP_RIGHT_DELAY;
  1422. }
  1423. /*
  1424. * Set contiguity flags on the left and right neighbors.
  1425. * Don't let extents get too large, even if the pieces are contiguous.
  1426. */
  1427. if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) &&
  1428. left.br_startoff + left.br_blockcount == new->br_startoff &&
  1429. left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
  1430. state |= BMAP_LEFT_CONTIG;
  1431. if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) &&
  1432. new->br_startoff + new->br_blockcount == right.br_startoff &&
  1433. new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
  1434. (!(state & BMAP_LEFT_CONTIG) ||
  1435. (left.br_blockcount + new->br_blockcount +
  1436. right.br_blockcount <= MAXEXTLEN)))
  1437. state |= BMAP_RIGHT_CONTIG;
  1438. /*
  1439. * Switch out based on the contiguity flags.
  1440. */
  1441. switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
  1442. case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
  1443. /*
  1444. * New allocation is contiguous with delayed allocations
  1445. * on the left and on the right.
  1446. * Merge all three into a single extent record.
  1447. */
  1448. --*idx;
  1449. temp = left.br_blockcount + new->br_blockcount +
  1450. right.br_blockcount;
  1451. trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
  1452. xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), temp);
  1453. oldlen = startblockval(left.br_startblock) +
  1454. startblockval(new->br_startblock) +
  1455. startblockval(right.br_startblock);
  1456. newlen = xfs_bmap_worst_indlen(ip, temp);
  1457. xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, *idx),
  1458. nullstartblock((int)newlen));
  1459. trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
  1460. xfs_iext_remove(ip, *idx + 1, 1, state);
  1461. break;
  1462. case BMAP_LEFT_CONTIG:
  1463. /*
  1464. * New allocation is contiguous with a delayed allocation
  1465. * on the left.
  1466. * Merge the new allocation with the left neighbor.
  1467. */
  1468. --*idx;
  1469. temp = left.br_blockcount + new->br_blockcount;
  1470. trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
  1471. xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), temp);
  1472. oldlen = startblockval(left.br_startblock) +
  1473. startblockval(new->br_startblock);
  1474. newlen = xfs_bmap_worst_indlen(ip, temp);
  1475. xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, *idx),
  1476. nullstartblock((int)newlen));
  1477. trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
  1478. break;
  1479. case BMAP_RIGHT_CONTIG:
  1480. /*
  1481. * New allocation is contiguous with a delayed allocation
  1482. * on the right.
  1483. * Merge the new allocation with the right neighbor.
  1484. */
  1485. trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
  1486. temp = new->br_blockcount + right.br_blockcount;
  1487. oldlen = startblockval(new->br_startblock) +
  1488. startblockval(right.br_startblock);
  1489. newlen = xfs_bmap_worst_indlen(ip, temp);
  1490. xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx),
  1491. new->br_startoff,
  1492. nullstartblock((int)newlen), temp, right.br_state);
  1493. trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
  1494. break;
  1495. case 0:
  1496. /*
  1497. * New allocation is not contiguous with another
  1498. * delayed allocation.
  1499. * Insert a new entry.
  1500. */
  1501. oldlen = newlen = 0;
  1502. xfs_iext_insert(ip, *idx, 1, new, state);
  1503. break;
  1504. }
  1505. if (oldlen != newlen) {
  1506. ASSERT(oldlen > newlen);
  1507. xfs_icsb_modify_counters(ip->i_mount, XFS_SBS_FDBLOCKS,
  1508. (int64_t)(oldlen - newlen), 0);
  1509. /*
  1510. * Nothing to do for disk quota accounting here.
  1511. */
  1512. }
  1513. }
  1514. /*
  1515. * Convert a hole to a real allocation.
  1516. */
  1517. STATIC int /* error */
  1518. xfs_bmap_add_extent_hole_real(
  1519. struct xfs_bmalloca *bma,
  1520. int whichfork)
  1521. {
  1522. struct xfs_bmbt_irec *new = &bma->got;
  1523. int error; /* error return value */
  1524. int i; /* temp state */
  1525. xfs_ifork_t *ifp; /* inode fork pointer */
  1526. xfs_bmbt_irec_t left; /* left neighbor extent entry */
  1527. xfs_bmbt_irec_t right; /* right neighbor extent entry */
  1528. int rval=0; /* return value (logging flags) */
  1529. int state; /* state bits, accessed thru macros */
  1530. ifp = XFS_IFORK_PTR(bma->ip, whichfork);
  1531. ASSERT(bma->idx >= 0);
  1532. ASSERT(bma->idx <= ifp->if_bytes / sizeof(struct xfs_bmbt_rec));
  1533. ASSERT(!isnullstartblock(new->br_startblock));
  1534. ASSERT(!bma->cur ||
  1535. !(bma->cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL));
  1536. XFS_STATS_INC(xs_add_exlist);
  1537. state = 0;
  1538. if (whichfork == XFS_ATTR_FORK)
  1539. state |= BMAP_ATTRFORK;
  1540. /*
  1541. * Check and set flags if this segment has a left neighbor.
  1542. */
  1543. if (bma->idx > 0) {
  1544. state |= BMAP_LEFT_VALID;
  1545. xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx - 1), &left);
  1546. if (isnullstartblock(left.br_startblock))
  1547. state |= BMAP_LEFT_DELAY;
  1548. }
  1549. /*
  1550. * Check and set flags if this segment has a current value.
  1551. * Not true if we're inserting into the "hole" at eof.
  1552. */
  1553. if (bma->idx < ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)) {
  1554. state |= BMAP_RIGHT_VALID;
  1555. xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx), &right);
  1556. if (isnullstartblock(right.br_startblock))
  1557. state |= BMAP_RIGHT_DELAY;
  1558. }
  1559. /*
  1560. * We're inserting a real allocation between "left" and "right".
  1561. * Set the contiguity flags. Don't let extents get too large.
  1562. */
  1563. if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
  1564. left.br_startoff + left.br_blockcount == new->br_startoff &&
  1565. left.br_startblock + left.br_blockcount == new->br_startblock &&
  1566. left.br_state == new->br_state &&
  1567. left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
  1568. state |= BMAP_LEFT_CONTIG;
  1569. if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
  1570. new->br_startoff + new->br_blockcount == right.br_startoff &&
  1571. new->br_startblock + new->br_blockcount == right.br_startblock &&
  1572. new->br_state == right.br_state &&
  1573. new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
  1574. (!(state & BMAP_LEFT_CONTIG) ||
  1575. left.br_blockcount + new->br_blockcount +
  1576. right.br_blockcount <= MAXEXTLEN))
  1577. state |= BMAP_RIGHT_CONTIG;
  1578. error = 0;
  1579. /*
  1580. * Select which case we're in here, and implement it.
  1581. */
  1582. switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
  1583. case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
  1584. /*
  1585. * New allocation is contiguous with real allocations on the
  1586. * left and on the right.
  1587. * Merge all three into a single extent record.
  1588. */
  1589. --bma->idx;
  1590. trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
  1591. xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
  1592. left.br_blockcount + new->br_blockcount +
  1593. right.br_blockcount);
  1594. trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
  1595. xfs_iext_remove(bma->ip, bma->idx + 1, 1, state);
  1596. XFS_IFORK_NEXT_SET(bma->ip, whichfork,
  1597. XFS_IFORK_NEXTENTS(bma->ip, whichfork) - 1);
  1598. if (bma->cur == NULL) {
  1599. rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
  1600. } else {
  1601. rval = XFS_ILOG_CORE;
  1602. error = xfs_bmbt_lookup_eq(bma->cur, right.br_startoff,
  1603. right.br_startblock, right.br_blockcount,
  1604. &i);
  1605. if (error)
  1606. goto done;
  1607. XFS_WANT_CORRUPTED_GOTO(i == 1, done);
  1608. error = xfs_btree_delete(bma->cur, &i);
  1609. if (error)
  1610. goto done;
  1611. XFS_WANT_CORRUPTED_GOTO(i == 1, done);
  1612. error = xfs_btree_decrement(bma->cur, 0, &i);
  1613. if (error)
  1614. goto done;
  1615. XFS_WANT_CORRUPTED_GOTO(i == 1, done);
  1616. error = xfs_bmbt_update(bma->cur, left.br_startoff,
  1617. left.br_startblock,
  1618. left.br_blockcount +
  1619. new->br_blockcount +
  1620. right.br_blockcount,
  1621. left.br_state);
  1622. if (error)
  1623. goto done;
  1624. }
  1625. break;
  1626. case BMAP_LEFT_CONTIG:
  1627. /*
  1628. * New allocation is contiguous with a real allocation
  1629. * on the left.
  1630. * Merge the new allocation with the left neighbor.
  1631. */
  1632. --bma->idx;
  1633. trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
  1634. xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
  1635. left.br_blockcount + new->br_blockcount);
  1636. trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
  1637. if (bma->cur == NULL) {
  1638. rval = xfs_ilog_fext(whichfork);
  1639. } else {
  1640. rval = 0;
  1641. error = xfs_bmbt_lookup_eq(bma->cur, left.br_startoff,
  1642. left.br_startblock, left.br_blockcount,
  1643. &i);
  1644. if (error)
  1645. goto done;
  1646. XFS_WANT_CORRUPTED_GOTO(i == 1, done);
  1647. error = xfs_bmbt_update(bma->cur, left.br_startoff,
  1648. left.br_startblock,
  1649. left.br_blockcount +
  1650. new->br_blockcount,
  1651. left.br_state);
  1652. if (error)
  1653. goto done;
  1654. }
  1655. break;
  1656. case BMAP_RIGHT_CONTIG:
  1657. /*
  1658. * New allocation is contiguous with a real allocation
  1659. * on the right.
  1660. * Merge the new allocation with the right neighbor.
  1661. */
  1662. trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
  1663. xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, bma->idx),
  1664. new->br_startoff, new->br_startblock,
  1665. new->br_blockcount + right.br_blockcount,
  1666. right.br_state);
  1667. trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
  1668. if (bma->cur == NULL) {
  1669. rval = xfs_ilog_fext(whichfork);
  1670. } else {
  1671. rval = 0;
  1672. error = xfs_bmbt_lookup_eq(bma->cur,
  1673. right.br_startoff,
  1674. right.br_startblock,
  1675. right.br_blockcount, &i);
  1676. if (error)
  1677. goto done;
  1678. XFS_WANT_CORRUPTED_GOTO(i == 1, done);
  1679. error = xfs_bmbt_update(bma->cur, new->br_startoff,
  1680. new->br_startblock,
  1681. new->br_blockcount +
  1682. right.br_blockcount,
  1683. right.br_state);
  1684. if (error)
  1685. goto done;
  1686. }
  1687. break;
  1688. case 0:
  1689. /*
  1690. * New allocation is not contiguous with another
  1691. * real allocation.
  1692. * Insert a new entry.
  1693. */
  1694. xfs_iext_insert(bma->ip, bma->idx, 1, new, state);
  1695. XFS_IFORK_NEXT_SET(bma->ip, whichfork,
  1696. XFS_IFORK_NEXTENTS(bma->ip, whichfork) + 1);
  1697. if (bma->cur == NULL) {
  1698. rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
  1699. } else {
  1700. rval = XFS_ILOG_CORE;
  1701. error = xfs_bmbt_lookup_eq(bma->cur,
  1702. new->br_startoff,
  1703. new->br_startblock,
  1704. new->br_blockcount, &i);
  1705. if (error)
  1706. goto done;
  1707. XFS_WANT_CORRUPTED_GOTO(i == 0, done);
  1708. bma->cur->bc_rec.b.br_state = new->br_state;
  1709. error = xfs_btree_insert(bma->cur, &i);
  1710. if (error)
  1711. goto done;
  1712. XFS_WANT_CORRUPTED_GOTO(i == 1, done);
  1713. }
  1714. break;
  1715. }
  1716. /* convert to a btree if necessary */
  1717. if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
  1718. int tmp_logflags; /* partial log flag return val */
  1719. ASSERT(bma->cur == NULL);
  1720. error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
  1721. bma->firstblock, bma->flist, &bma->cur,
  1722. 0, &tmp_logflags, whichfork);
  1723. bma->logflags |= tmp_logflags;
  1724. if (error)
  1725. goto done;
  1726. }
  1727. /* clear out the allocated field, done with it now in any case. */
  1728. if (bma->cur)
  1729. bma->cur->bc_private.b.allocated = 0;
  1730. xfs_bmap_check_leaf_extents(bma->cur, bma->ip, whichfork);
  1731. done:
  1732. bma->logflags |= rval;
  1733. return error;
  1734. }
  1735. /*
  1736. * Adjust the size of the new extent based on di_extsize and rt extsize.
  1737. */
  1738. STATIC int
  1739. xfs_bmap_extsize_align(
  1740. xfs_mount_t *mp,
  1741. xfs_bmbt_irec_t *gotp, /* next extent pointer */
  1742. xfs_bmbt_irec_t *prevp, /* previous extent pointer */
  1743. xfs_extlen_t extsz, /* align to this extent size */
  1744. int rt, /* is this a realtime inode? */
  1745. int eof, /* is extent at end-of-file? */
  1746. int delay, /* creating delalloc extent? */
  1747. int convert, /* overwriting unwritten extent? */
  1748. xfs_fileoff_t *offp, /* in/out: aligned offset */
  1749. xfs_extlen_t *lenp) /* in/out: aligned length */
  1750. {
  1751. xfs_fileoff_t orig_off; /* original offset */
  1752. xfs_extlen_t orig_alen; /* original length */
  1753. xfs_fileoff_t orig_end; /* original off+len */
  1754. xfs_fileoff_t nexto; /* next file offset */
  1755. xfs_fileoff_t prevo; /* previous file offset */
  1756. xfs_fileoff_t align_off; /* temp for offset */
  1757. xfs_extlen_t align_alen; /* temp for length */
  1758. xfs_extlen_t temp; /* temp for calculations */
  1759. if (convert)
  1760. return 0;
  1761. orig_off = align_off = *offp;
  1762. orig_alen = align_alen = *lenp;
  1763. orig_end = orig_off + orig_alen;
  1764. /*
  1765. * If this request overlaps an existing extent, then don't
  1766. * attempt to perform any additional alignment.
  1767. */
  1768. if (!delay && !eof &&
  1769. (orig_off >= gotp->br_startoff) &&
  1770. (orig_end <= gotp->br_startoff + gotp->br_blockcount)) {
  1771. return 0;
  1772. }
  1773. /*
  1774. * If the file offset is unaligned vs. the extent size
  1775. * we need to align it. This will be possible unless
  1776. * the file was previously written with a kernel that didn't
  1777. * perform this alignment, or if a truncate shot us in the
  1778. * foot.
  1779. */
  1780. temp = do_mod(orig_off, extsz);
  1781. if (temp) {
  1782. align_alen += temp;
  1783. align_off -= temp;
  1784. }
  1785. /*
  1786. * Same adjustment for the end of the requested area.
  1787. */
  1788. if ((temp = (align_alen % extsz))) {
  1789. align_alen += extsz - temp;
  1790. }
  1791. /*
  1792. * If the previous block overlaps with this proposed allocation
  1793. * then move the start forward without adjusting the length.
  1794. */
  1795. if (prevp->br_startoff != NULLFILEOFF) {
  1796. if (prevp->br_startblock == HOLESTARTBLOCK)
  1797. prevo = prevp->br_startoff;
  1798. else
  1799. prevo = prevp->br_startoff + prevp->br_blockcount;
  1800. } else
  1801. prevo = 0;
  1802. if (align_off != orig_off && align_off < prevo)
  1803. align_off = prevo;
  1804. /*
  1805. * If the next block overlaps with this proposed allocation
  1806. * then move the start back without adjusting the length,
  1807. * but not before offset 0.
  1808. * This may of course make the start overlap previous block,
  1809. * and if we hit the offset 0 limit then the next block
  1810. * can still overlap too.
  1811. */
  1812. if (!eof && gotp->br_startoff != NULLFILEOFF) {
  1813. if ((delay && gotp->br_startblock == HOLESTARTBLOCK) ||
  1814. (!delay && gotp->br_startblock == DELAYSTARTBLOCK))
  1815. nexto = gotp->br_startoff + gotp->br_blockcount;
  1816. else
  1817. nexto = gotp->br_startoff;
  1818. } else
  1819. nexto = NULLFILEOFF;
  1820. if (!eof &&
  1821. align_off + align_alen != orig_end &&
  1822. align_off + align_alen > nexto)
  1823. align_off = nexto > align_alen ? nexto - align_alen : 0;
  1824. /*
  1825. * If we're now overlapping the next or previous extent that
  1826. * means we can't fit an extsz piece in this hole. Just move
  1827. * the start forward to the first valid spot and set
  1828. * the length so we hit the end.
  1829. */
  1830. if (align_off != orig_off && align_off < prevo)
  1831. align_off = prevo;
  1832. if (align_off + align_alen != orig_end &&
  1833. align_off + align_alen > nexto &&
  1834. nexto != NULLFILEOFF) {
  1835. ASSERT(nexto > prevo);
  1836. align_alen = nexto - align_off;
  1837. }
  1838. /*
  1839. * If realtime, and the result isn't a multiple of the realtime
  1840. * extent size we need to remove blocks until it is.
  1841. */
  1842. if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) {
  1843. /*
  1844. * We're not covering the original request, or
  1845. * we won't be able to once we fix the length.
  1846. */
  1847. if (orig_off < align_off ||
  1848. orig_end > align_off + align_alen ||
  1849. align_alen - temp < orig_alen)
  1850. return XFS_ERROR(EINVAL);
  1851. /*
  1852. * Try to fix it by moving the start up.
  1853. */
  1854. if (align_off + temp <= orig_off) {
  1855. align_alen -= temp;
  1856. align_off += temp;
  1857. }
  1858. /*
  1859. * Try to fix it by moving the end in.
  1860. */
  1861. else if (align_off + align_alen - temp >= orig_end)
  1862. align_alen -= temp;
  1863. /*
  1864. * Set the start to the minimum then trim the length.
  1865. */
  1866. else {
  1867. align_alen -= orig_off - align_off;
  1868. align_off = orig_off;
  1869. align_alen -= align_alen % mp->m_sb.sb_rextsize;
  1870. }
  1871. /*
  1872. * Result doesn't cover the request, fail it.
  1873. */
  1874. if (orig_off < align_off || orig_end > align_off + align_alen)
  1875. return XFS_ERROR(EINVAL);
  1876. } else {
  1877. ASSERT(orig_off >= align_off);
  1878. ASSERT(orig_end <= align_off + align_alen);
  1879. }
  1880. #ifdef DEBUG
  1881. if (!eof && gotp->br_startoff != NULLFILEOFF)
  1882. ASSERT(align_off + align_alen <= gotp->br_startoff);
  1883. if (prevp->br_startoff != NULLFILEOFF)
  1884. ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount);
  1885. #endif
  1886. *lenp = align_alen;
  1887. *offp = align_off;
  1888. return 0;
  1889. }
  1890. #define XFS_ALLOC_GAP_UNITS 4
  1891. STATIC void
  1892. xfs_bmap_adjacent(
  1893. xfs_bmalloca_t *ap) /* bmap alloc argument struct */
  1894. {
  1895. xfs_fsblock_t adjust; /* adjustment to block numbers */
  1896. xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
  1897. xfs_mount_t *mp; /* mount point structure */
  1898. int nullfb; /* true if ap->firstblock isn't set */
  1899. int rt; /* true if inode is realtime */
  1900. #define ISVALID(x,y) \
  1901. (rt ? \
  1902. (x) < mp->m_sb.sb_rblocks : \
  1903. XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
  1904. XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
  1905. XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
  1906. mp = ap->ip->i_mount;
  1907. nullfb = *ap->firstblock == NULLFSBLOCK;
  1908. rt = XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata;
  1909. fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock);
  1910. /*
  1911. * If allocating at eof, and there's a previous real block,
  1912. * try to use its last block as our starting point.
  1913. */
  1914. if (ap->eof && ap->prev.br_startoff != NULLFILEOFF &&
  1915. !isnullstartblock(ap->prev.br_startblock) &&
  1916. ISVALID(ap->prev.br_startblock + ap->prev.br_blockcount,
  1917. ap->prev.br_startblock)) {
  1918. ap->blkno = ap->prev.br_startblock + ap->prev.br_blockcount;
  1919. /*
  1920. * Adjust for the gap between prevp and us.
  1921. */
  1922. adjust = ap->offset -
  1923. (ap->prev.br_startoff + ap->prev.br_blockcount);
  1924. if (adjust &&
  1925. ISVALID(ap->blkno + adjust, ap->prev.br_startblock))
  1926. ap->blkno += adjust;
  1927. }
  1928. /*
  1929. * If not at eof, then compare the two neighbor blocks.
  1930. * Figure out whether either one gives us a good starting point,
  1931. * and pick the better one.
  1932. */
  1933. else if (!ap->eof) {
  1934. xfs_fsblock_t gotbno; /* right side block number */
  1935. xfs_fsblock_t gotdiff=0; /* right side difference */
  1936. xfs_fsblock_t prevbno; /* left side block number */
  1937. xfs_fsblock_t prevdiff=0; /* left side difference */
  1938. /*
  1939. * If there's a previous (left) block, select a requested
  1940. * start block based on it.
  1941. */
  1942. if (ap->prev.br_startoff != NULLFILEOFF &&
  1943. !isnullstartblock(ap->prev.br_startblock) &&
  1944. (prevbno = ap->prev.br_startblock +
  1945. ap->prev.br_blockcount) &&
  1946. ISVALID(prevbno, ap->prev.br_startblock)) {
  1947. /*
  1948. * Calculate gap to end of previous block.
  1949. */
  1950. adjust = prevdiff = ap->offset -
  1951. (ap->prev.br_startoff +
  1952. ap->prev.br_blockcount);
  1953. /*
  1954. * Figure the startblock based on the previous block's
  1955. * end and the gap size.
  1956. * Heuristic!
  1957. * If the gap is large relative to the piece we're
  1958. * allocating, or using it gives us an invalid block
  1959. * number, then just use the end of the previous block.
  1960. */
  1961. if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
  1962. ISVALID(prevbno + prevdiff,
  1963. ap->prev.br_startblock))
  1964. prevbno += adjust;
  1965. else
  1966. prevdiff += adjust;
  1967. /*
  1968. * If the firstblock forbids it, can't use it,
  1969. * must use default.
  1970. */
  1971. if (!rt && !nullfb &&
  1972. XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno)
  1973. prevbno = NULLFSBLOCK;
  1974. }
  1975. /*
  1976. * No previous block or can't follow it, just default.
  1977. */
  1978. else
  1979. prevbno = NULLFSBLOCK;
  1980. /*
  1981. * If there's a following (right) block, select a requested
  1982. * start block based on it.
  1983. */
  1984. if (!isnullstartblock(ap->got.br_startblock)) {
  1985. /*
  1986. * Calculate gap to start of next block.
  1987. */
  1988. adjust = gotdiff = ap->got.br_startoff - ap->offset;
  1989. /*
  1990. * Figure the startblock based on the next block's
  1991. * start and the gap size.
  1992. */
  1993. gotbno = ap->got.br_startblock;
  1994. /*
  1995. * Heuristic!
  1996. * If the gap is large relative to the piece we're
  1997. * allocating, or using it gives us an invalid block
  1998. * number, then just use the start of the next block
  1999. * offset by our length.
  2000. */
  2001. if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
  2002. ISVALID(gotbno - gotdiff, gotbno))
  2003. gotbno -= adjust;
  2004. else if (ISVALID(gotbno - ap->length, gotbno)) {
  2005. gotbno -= ap->length;
  2006. gotdiff += adjust - ap->length;
  2007. } else
  2008. gotdiff += adjust;
  2009. /*
  2010. * If the firstblock forbids it, can't use it,
  2011. * must use default.
  2012. */
  2013. if (!rt && !nullfb &&
  2014. XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno)
  2015. gotbno = NULLFSBLOCK;
  2016. }
  2017. /*
  2018. * No next block, just default.
  2019. */
  2020. else
  2021. gotbno = NULLFSBLOCK;
  2022. /*
  2023. * If both valid, pick the better one, else the only good
  2024. * one, else ap->blkno is already set (to 0 or the inode block).
  2025. */
  2026. if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK)
  2027. ap->blkno = prevdiff <= gotdiff ? prevbno : gotbno;
  2028. else if (prevbno != NULLFSBLOCK)
  2029. ap->blkno = prevbno;
  2030. else if (gotbno != NULLFSBLOCK)
  2031. ap->blkno = gotbno;
  2032. }
  2033. #undef ISVALID
  2034. }
  2035. STATIC int
  2036. xfs_bmap_rtalloc(
  2037. xfs_bmalloca_t *ap) /* bmap alloc argument struct */
  2038. {
  2039. xfs_alloctype_t atype = 0; /* type for allocation routines */
  2040. int error; /* error return value */
  2041. xfs_mount_t *mp; /* mount point structure */
  2042. xfs_extlen_t prod = 0; /* product factor for allocators */
  2043. xfs_extlen_t ralen = 0; /* realtime allocation length */
  2044. xfs_extlen_t align; /* minimum allocation alignment */
  2045. xfs_rtblock_t rtb;
  2046. mp = ap->ip->i_mount;
  2047. align = xfs_get_extsz_hint(ap->ip);
  2048. prod = align / mp->m_sb.sb_rextsize;
  2049. error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev,
  2050. align, 1, ap->eof, 0,
  2051. ap->conv, &ap->offset, &ap->length);
  2052. if (error)
  2053. return error;
  2054. ASSERT(ap->length);
  2055. ASSERT(ap->length % mp->m_sb.sb_rextsize == 0);
  2056. /*
  2057. * If the offset & length are not perfectly aligned
  2058. * then kill prod, it will just get us in trouble.
  2059. */
  2060. if (do_mod(ap->offset, align) || ap->length % align)
  2061. prod = 1;
  2062. /*
  2063. * Set ralen to be the actual requested length in rtextents.
  2064. */
  2065. ralen = ap->length / mp->m_sb.sb_rextsize;
  2066. /*
  2067. * If the old value was close enough to MAXEXTLEN that
  2068. * we rounded up to it, cut it back so it's valid again.
  2069. * Note that if it's a really large request (bigger than
  2070. * MAXEXTLEN), we don't hear about that number, and can't
  2071. * adjust the starting point to match it.
  2072. */
  2073. if (ralen * mp->m_sb.sb_rextsize >= MAXEXTLEN)
  2074. ralen = MAXEXTLEN / mp->m_sb.sb_rextsize;
  2075. /*
  2076. * Lock out other modifications to the RT bitmap inode.
  2077. */
  2078. xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL);
  2079. xfs_trans_ijoin(ap->tp, mp->m_rbmip, XFS_ILOCK_EXCL);
  2080. /*
  2081. * If it's an allocation to an empty file at offset 0,
  2082. * pick an extent that will space things out in the rt area.
  2083. */
  2084. if (ap->eof && ap->offset == 0) {
  2085. xfs_rtblock_t uninitialized_var(rtx); /* realtime extent no */
  2086. error = xfs_rtpick_extent(mp, ap->tp, ralen, &rtx);
  2087. if (error)
  2088. return error;
  2089. ap->blkno = rtx * mp->m_sb.sb_rextsize;
  2090. } else {
  2091. ap->blkno = 0;
  2092. }
  2093. xfs_bmap_adjacent(ap);
  2094. /*
  2095. * Realtime allocation, done through xfs_rtallocate_extent.
  2096. */
  2097. atype = ap->blkno == 0 ? XFS_ALLOCTYPE_ANY_AG : XFS_ALLOCTYPE_NEAR_BNO;
  2098. do_div(ap->blkno, mp->m_sb.sb_rextsize);
  2099. rtb = ap->blkno;
  2100. ap->length = ralen;
  2101. if ((error = xfs_rtallocate_extent(ap->tp, ap->blkno, 1, ap->length,
  2102. &ralen, atype, ap->wasdel, prod, &rtb)))
  2103. return error;
  2104. if (rtb == NULLFSBLOCK && prod > 1 &&
  2105. (error = xfs_rtallocate_extent(ap->tp, ap->blkno, 1,
  2106. ap->length, &ralen, atype,
  2107. ap->wasdel, 1, &rtb)))
  2108. return error;
  2109. ap->blkno = rtb;
  2110. if (ap->blkno != NULLFSBLOCK) {
  2111. ap->blkno *= mp->m_sb.sb_rextsize;
  2112. ralen *= mp->m_sb.sb_rextsize;
  2113. ap->length = ralen;
  2114. ap->ip->i_d.di_nblocks += ralen;
  2115. xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
  2116. if (ap->wasdel)
  2117. ap->ip->i_delayed_blks -= ralen;
  2118. /*
  2119. * Adjust the disk quota also. This was reserved
  2120. * earlier.
  2121. */
  2122. xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
  2123. ap->wasdel ? XFS_TRANS_DQ_DELRTBCOUNT :
  2124. XFS_TRANS_DQ_RTBCOUNT, (long) ralen);
  2125. } else {
  2126. ap->length = 0;
  2127. }
  2128. return 0;
  2129. }
  2130. STATIC int
  2131. xfs_bmap_btalloc_nullfb(
  2132. struct xfs_bmalloca *ap,
  2133. struct xfs_alloc_arg *args,
  2134. xfs_extlen_t *blen)
  2135. {
  2136. struct xfs_mount *mp = ap->ip->i_mount;
  2137. struct xfs_perag *pag;
  2138. xfs_agnumber_t ag, startag;
  2139. int notinit = 0;
  2140. int error;
  2141. if (ap->userdata && xfs_inode_is_filestream(ap->ip))
  2142. args->type = XFS_ALLOCTYPE_NEAR_BNO;
  2143. else
  2144. args->type = XFS_ALLOCTYPE_START_BNO;
  2145. args->total = ap->total;
  2146. /*
  2147. * Search for an allocation group with a single extent large enough
  2148. * for the request. If one isn't found, then adjust the minimum
  2149. * allocation size to the largest space found.
  2150. */
  2151. startag = ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
  2152. if (startag == NULLAGNUMBER)
  2153. startag = ag = 0;
  2154. pag = xfs_perag_get(mp, ag);
  2155. while (*blen < args->maxlen) {
  2156. if (!pag->pagf_init) {
  2157. error = xfs_alloc_pagf_init(mp, args->tp, ag,
  2158. XFS_ALLOC_FLAG_TRYLOCK);
  2159. if (error) {
  2160. xfs_perag_put(pag);
  2161. return error;
  2162. }
  2163. }
  2164. /*
  2165. * See xfs_alloc_fix_freelist...
  2166. */
  2167. if (pag->pagf_init) {
  2168. xfs_extlen_t longest;
  2169. longest = xfs_alloc_longest_free_extent(mp, pag);
  2170. if (*blen < longest)
  2171. *blen = longest;
  2172. } else
  2173. notinit = 1;
  2174. if (xfs_inode_is_filestream(ap->ip)) {
  2175. if (*blen >= args->maxlen)
  2176. break;
  2177. if (ap->userdata) {
  2178. /*
  2179. * If startag is an invalid AG, we've
  2180. * come here once before and
  2181. * xfs_filestream_new_ag picked the
  2182. * best currently available.
  2183. *
  2184. * Don't continue looping, since we
  2185. * could loop forever.
  2186. */
  2187. if (startag == NULLAGNUMBER)
  2188. break;
  2189. error = xfs_filestream_new_ag(ap, &ag);
  2190. xfs_perag_put(pag);
  2191. if (error)
  2192. return error;
  2193. /* loop again to set 'blen'*/
  2194. startag = NULLAGNUMBER;
  2195. pag = xfs_perag_get(mp, ag);
  2196. continue;
  2197. }
  2198. }
  2199. if (++ag == mp->m_sb.sb_agcount)
  2200. ag = 0;
  2201. if (ag == startag)
  2202. break;
  2203. xfs_perag_put(pag);
  2204. pag = xfs_perag_get(mp, ag);
  2205. }
  2206. xfs_perag_put(pag);
  2207. /*
  2208. * Since the above loop did a BUF_TRYLOCK, it is
  2209. * possible that there is space for this request.
  2210. */
  2211. if (notinit || *blen < ap->minlen)
  2212. args->minlen = ap->minlen;
  2213. /*
  2214. * If the best seen length is less than the request
  2215. * length, use the best as the minimum.
  2216. */
  2217. else if (*blen < args->maxlen)
  2218. args->minlen = *blen;
  2219. /*
  2220. * Otherwise we've seen an extent as big as maxlen,
  2221. * use that as the minimum.
  2222. */
  2223. else
  2224. args->minlen = args->maxlen;
  2225. /*
  2226. * set the failure fallback case to look in the selected
  2227. * AG as the stream may have moved.
  2228. */
  2229. if (xfs_inode_is_filestream(ap->ip))
  2230. ap->blkno = args->fsbno = XFS_AGB_TO_FSB(mp, ag, 0);
  2231. return 0;
  2232. }
  2233. STATIC int
  2234. xfs_bmap_btalloc(
  2235. xfs_bmalloca_t *ap) /* bmap alloc argument struct */
  2236. {
  2237. xfs_mount_t *mp; /* mount point structure */
  2238. xfs_alloctype_t atype = 0; /* type for allocation routines */
  2239. xfs_extlen_t align; /* minimum allocation alignment */
  2240. xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
  2241. xfs_agnumber_t ag;
  2242. xfs_alloc_arg_t args;
  2243. xfs_extlen_t blen;
  2244. xfs_extlen_t nextminlen = 0;
  2245. int nullfb; /* true if ap->firstblock isn't set */
  2246. int isaligned;
  2247. int tryagain;
  2248. int error;
  2249. ASSERT(ap->length);
  2250. mp = ap->ip->i_mount;
  2251. align = ap->userdata ? xfs_get_extsz_hint(ap->ip) : 0;
  2252. if (unlikely(align)) {
  2253. error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev,
  2254. align, 0, ap->eof, 0, ap->conv,
  2255. &ap->offset, &ap->length);
  2256. ASSERT(!error);
  2257. ASSERT(ap->length);
  2258. }
  2259. nullfb = *ap->firstblock == NULLFSBLOCK;
  2260. fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock);
  2261. if (nullfb) {
  2262. if (ap->userdata && xfs_inode_is_filestream(ap->ip)) {
  2263. ag = xfs_filestream_lookup_ag(ap->ip);
  2264. ag = (ag != NULLAGNUMBER) ? ag : 0;
  2265. ap->blkno = XFS_AGB_TO_FSB(mp, ag, 0);
  2266. } else {
  2267. ap->blkno = XFS_INO_TO_FSB(mp, ap->ip->i_ino);
  2268. }
  2269. } else
  2270. ap->blkno = *ap->firstblock;
  2271. xfs_bmap_adjacent(ap);
  2272. /*
  2273. * If allowed, use ap->blkno; otherwise must use firstblock since
  2274. * it's in the right allocation group.
  2275. */
  2276. if (nullfb || XFS_FSB_TO_AGNO(mp, ap->blkno) == fb_agno)
  2277. ;
  2278. else
  2279. ap->blkno = *ap->firstblock;
  2280. /*
  2281. * Normal allocation, done through xfs_alloc_vextent.
  2282. */
  2283. tryagain = isaligned = 0;
  2284. memset(&args, 0, sizeof(args));
  2285. args.tp = ap->tp;
  2286. args.mp = mp;
  2287. args.fsbno = ap->blkno;
  2288. /* Trim the allocation back to the maximum an AG can fit. */
  2289. args.maxlen = MIN(ap->length, XFS_ALLOC_AG_MAX_USABLE(mp));
  2290. args.firstblock = *ap->firstblock;
  2291. blen = 0;
  2292. if (nullfb) {
  2293. error = xfs_bmap_btalloc_nullfb(ap, &args, &blen);
  2294. if (error)
  2295. return error;
  2296. } else if (ap->flist->xbf_low) {
  2297. if (xfs_inode_is_filestream(ap->ip))
  2298. args.type = XFS_ALLOCTYPE_FIRST_AG;
  2299. else
  2300. args.type = XFS_ALLOCTYPE_START_BNO;
  2301. args.total = args.minlen = ap->minlen;
  2302. } else {
  2303. args.type = XFS_ALLOCTYPE_NEAR_BNO;
  2304. args.total = ap->total;
  2305. args.minlen = ap->minlen;
  2306. }
  2307. /* apply extent size hints if obtained earlier */
  2308. if (unlikely(align)) {
  2309. args.prod = align;
  2310. if ((args.mod = (xfs_extlen_t)do_mod(ap->offset, args.prod)))
  2311. args.mod = (xfs_extlen_t)(args.prod - args.mod);
  2312. } else if (mp->m_sb.sb_blocksize >= PAGE_CACHE_SIZE) {
  2313. args.prod = 1;
  2314. args.mod = 0;
  2315. } else {
  2316. args.prod = PAGE_CACHE_SIZE >> mp->m_sb.sb_blocklog;
  2317. if ((args.mod = (xfs_extlen_t)(do_mod(ap->offset, args.prod))))
  2318. args.mod = (xfs_extlen_t)(args.prod - args.mod);
  2319. }
  2320. /*
  2321. * If we are not low on available data blocks, and the
  2322. * underlying logical volume manager is a stripe, and
  2323. * the file offset is zero then try to allocate data
  2324. * blocks on stripe unit boundary.
  2325. * NOTE: ap->aeof is only set if the allocation length
  2326. * is >= the stripe unit and the allocation offset is
  2327. * at the end of file.
  2328. */
  2329. if (!ap->flist->xbf_low && ap->aeof) {
  2330. if (!ap->offset) {
  2331. args.alignment = mp->m_dalign;
  2332. atype = args.type;
  2333. isaligned = 1;
  2334. /*
  2335. * Adjust for alignment
  2336. */
  2337. if (blen > args.alignment && blen <= args.maxlen)
  2338. args.minlen = blen - args.alignment;
  2339. args.minalignslop = 0;
  2340. } else {
  2341. /*
  2342. * First try an exact bno allocation.
  2343. * If it fails then do a near or start bno
  2344. * allocation with alignment turned on.
  2345. */
  2346. atype = args.type;
  2347. tryagain = 1;
  2348. args.type = XFS_ALLOCTYPE_THIS_BNO;
  2349. args.alignment = 1;
  2350. /*
  2351. * Compute the minlen+alignment for the
  2352. * next case. Set slop so that the value
  2353. * of minlen+alignment+slop doesn't go up
  2354. * between the calls.
  2355. */
  2356. if (blen > mp->m_dalign && blen <= args.maxlen)
  2357. nextminlen = blen - mp->m_dalign;
  2358. else
  2359. nextminlen = args.minlen;
  2360. if (nextminlen + mp->m_dalign > args.minlen + 1)
  2361. args.minalignslop =
  2362. nextminlen + mp->m_dalign -
  2363. args.minlen - 1;
  2364. else
  2365. args.minalignslop = 0;
  2366. }
  2367. } else {
  2368. args.alignment = 1;
  2369. args.minalignslop = 0;
  2370. }
  2371. args.minleft = ap->minleft;
  2372. args.wasdel = ap->wasdel;
  2373. args.isfl = 0;
  2374. args.userdata = ap->userdata;
  2375. if ((error = xfs_alloc_vextent(&args)))
  2376. return error;
  2377. if (tryagain && args.fsbno == NULLFSBLOCK) {
  2378. /*
  2379. * Exact allocation failed. Now try with alignment
  2380. * turned on.
  2381. */
  2382. args.type = atype;
  2383. args.fsbno = ap->blkno;
  2384. args.alignment = mp->m_dalign;
  2385. args.minlen = nextminlen;
  2386. args.minalignslop = 0;
  2387. isaligned = 1;
  2388. if ((error = xfs_alloc_vextent(&args)))
  2389. return error;
  2390. }
  2391. if (isaligned && args.fsbno == NULLFSBLOCK) {
  2392. /*
  2393. * allocation failed, so turn off alignment and
  2394. * try again.
  2395. */
  2396. args.type = atype;
  2397. args.fsbno = ap->blkno;
  2398. args.alignment = 0;
  2399. if ((error = xfs_alloc_vextent(&args)))
  2400. return error;
  2401. }
  2402. if (args.fsbno == NULLFSBLOCK && nullfb &&
  2403. args.minlen > ap->minlen) {
  2404. args.minlen = ap->minlen;
  2405. args.type = XFS_ALLOCTYPE_START_BNO;
  2406. args.fsbno = ap->blkno;
  2407. if ((error = xfs_alloc_vextent(&args)))
  2408. return error;
  2409. }
  2410. if (args.fsbno == NULLFSBLOCK && nullfb) {
  2411. args.fsbno = 0;
  2412. args.type = XFS_ALLOCTYPE_FIRST_AG;
  2413. args.total = ap->minlen;
  2414. args.minleft = 0;
  2415. if ((error = xfs_alloc_vextent(&args)))
  2416. return error;
  2417. ap->flist->xbf_low = 1;
  2418. }
  2419. if (args.fsbno != NULLFSBLOCK) {
  2420. /*
  2421. * check the allocation happened at the same or higher AG than
  2422. * the first block that was allocated.
  2423. */
  2424. ASSERT(*ap->firstblock == NULLFSBLOCK ||
  2425. XFS_FSB_TO_AGNO(mp, *ap->firstblock) ==
  2426. XFS_FSB_TO_AGNO(mp, args.fsbno) ||
  2427. (ap->flist->xbf_low &&
  2428. XFS_FSB_TO_AGNO(mp, *ap->firstblock) <
  2429. XFS_FSB_TO_AGNO(mp, args.fsbno)));
  2430. ap->blkno = args.fsbno;
  2431. if (*ap->firstblock == NULLFSBLOCK)
  2432. *ap->firstblock = args.fsbno;
  2433. ASSERT(nullfb || fb_agno == args.agno ||
  2434. (ap->flist->xbf_low && fb_agno < args.agno));
  2435. ap->length = args.len;
  2436. ap->ip->i_d.di_nblocks += args.len;
  2437. xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
  2438. if (ap->wasdel)
  2439. ap->ip->i_delayed_blks -= args.len;
  2440. /*
  2441. * Adjust the disk quota also. This was reserved
  2442. * earlier.
  2443. */
  2444. xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
  2445. ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT :
  2446. XFS_TRANS_DQ_BCOUNT,
  2447. (long) args.len);
  2448. } else {
  2449. ap->blkno = NULLFSBLOCK;
  2450. ap->length = 0;
  2451. }
  2452. return 0;
  2453. }
  2454. /*
  2455. * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
  2456. * It figures out where to ask the underlying allocator to put the new extent.
  2457. */
  2458. STATIC int
  2459. xfs_bmap_alloc(
  2460. xfs_bmalloca_t *ap) /* bmap alloc argument struct */
  2461. {
  2462. if (XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata)
  2463. return xfs_bmap_rtalloc(ap);
  2464. return xfs_bmap_btalloc(ap);
  2465. }
  2466. /*
  2467. * Transform a btree format file with only one leaf node, where the
  2468. * extents list will fit in the inode, into an extents format file.
  2469. * Since the file extents are already in-core, all we have to do is
  2470. * give up the space for the btree root and pitch the leaf block.
  2471. */
  2472. STATIC int /* error */
  2473. xfs_bmap_btree_to_extents(
  2474. xfs_trans_t *tp, /* transaction pointer */
  2475. xfs_inode_t *ip, /* incore inode pointer */
  2476. xfs_btree_cur_t *cur, /* btree cursor */
  2477. int *logflagsp, /* inode logging flags */
  2478. int whichfork) /* data or attr fork */
  2479. {
  2480. /* REFERENCED */
  2481. struct xfs_btree_block *cblock;/* child btree block */
  2482. xfs_fsblock_t cbno; /* child block number */
  2483. xfs_buf_t *cbp; /* child block's buffer */
  2484. int error; /* error return value */
  2485. xfs_ifork_t *ifp; /* inode fork data */
  2486. xfs_mount_t *mp; /* mount point structure */
  2487. __be64 *pp; /* ptr to block address */
  2488. struct xfs_btree_block *rblock;/* root btree block */
  2489. mp = ip->i_mount;
  2490. ifp = XFS_IFORK_PTR(ip, whichfork);
  2491. ASSERT(ifp->if_flags & XFS_IFEXTENTS);
  2492. ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
  2493. rblock = ifp->if_broot;
  2494. ASSERT(be16_to_cpu(rblock->bb_level) == 1);
  2495. ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1);
  2496. ASSERT(xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0) == 1);
  2497. pp = XFS_BMAP_BROOT_PTR_ADDR(mp, rblock, 1, ifp->if_broot_bytes);
  2498. cbno = be64_to_cpu(*pp);
  2499. *logflagsp = 0;
  2500. #ifdef DEBUG
  2501. if ((error = xfs_btree_check_lptr(cur, cbno, 1)))
  2502. return error;
  2503. #endif
  2504. error = xfs_btree_read_bufl(mp, tp, cbno, 0, &cbp, XFS_BMAP_BTREE_REF,
  2505. &xfs_bmbt_buf_ops);
  2506. if (error)
  2507. return error;
  2508. cblock = XFS_BUF_TO_BLOCK(cbp);
  2509. if ((error = xfs_btree_check_block(cur, cblock, 0, cbp)))
  2510. return error;
  2511. xfs_bmap_add_free(cbno, 1, cur->bc_private.b.flist, mp);
  2512. ip->i_d.di_nblocks--;
  2513. xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
  2514. xfs_trans_binval(tp, cbp);
  2515. if (cur->bc_bufs[0] == cbp)
  2516. cur->bc_bufs[0] = NULL;
  2517. xfs_iroot_realloc(ip, -1, whichfork);
  2518. ASSERT(ifp->if_broot == NULL);
  2519. ASSERT((ifp->if_flags & XFS_IFBROOT) == 0);
  2520. XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
  2521. *logflagsp = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
  2522. return 0;
  2523. }
  2524. /*
  2525. * Called by xfs_bmapi to update file extent records and the btree
  2526. * after removing space (or undoing a delayed allocation).
  2527. */
  2528. STATIC int /* error */
  2529. xfs_bmap_del_extent(
  2530. xfs_inode_t *ip, /* incore inode pointer */
  2531. xfs_trans_t *tp, /* current transaction pointer */
  2532. xfs_extnum_t *idx, /* extent number to update/delete */
  2533. xfs_bmap_free_t *flist, /* list of extents to be freed */
  2534. xfs_btree_cur_t *cur, /* if null, not a btree */
  2535. xfs_bmbt_irec_t *del, /* data to remove from extents */
  2536. int *logflagsp, /* inode logging flags */
  2537. int whichfork) /* data or attr fork */
  2538. {
  2539. xfs_filblks_t da_new; /* new delay-alloc indirect blocks */
  2540. xfs_filblks_t da_old; /* old delay-alloc indirect blocks */
  2541. xfs_fsblock_t del_endblock=0; /* first block past del */
  2542. xfs_fileoff_t del_endoff; /* first offset past del */
  2543. int delay; /* current block is delayed allocated */
  2544. int do_fx; /* free extent at end of routine */
  2545. xfs_bmbt_rec_host_t *ep; /* current extent entry pointer */
  2546. int error; /* error return value */
  2547. int flags; /* inode logging flags */
  2548. xfs_bmbt_irec_t got; /* current extent entry */
  2549. xfs_fileoff_t got_endoff; /* first offset past got */
  2550. int i; /* temp state */
  2551. xfs_ifork_t *ifp; /* inode fork pointer */
  2552. xfs_mount_t *mp; /* mount structure */
  2553. xfs_filblks_t nblks; /* quota/sb block count */
  2554. xfs_bmbt_irec_t new; /* new record to be inserted */
  2555. /* REFERENCED */
  2556. uint qfield; /* quota field to update */
  2557. xfs_filblks_t temp; /* for indirect length calculations */
  2558. xfs_filblks_t temp2; /* for indirect length calculations */
  2559. int state = 0;
  2560. XFS_STATS_INC(xs_del_exlist);
  2561. if (whichfork == XFS_ATTR_FORK)
  2562. state |= BMAP_ATTRFORK;
  2563. mp = ip->i_mount;
  2564. ifp = XFS_IFORK_PTR(ip, whichfork);
  2565. ASSERT((*idx >= 0) && (*idx < ifp->if_bytes /
  2566. (uint)sizeof(xfs_bmbt_rec_t)));
  2567. ASSERT(del->br_blockcount > 0);
  2568. ep = xfs_iext_get_ext(ifp, *idx);
  2569. xfs_bmbt_get_all(ep, &got);
  2570. ASSERT(got.br_startoff <= del->br_startoff);
  2571. del_endoff = del->br_startoff + del->br_blockcount;
  2572. got_endoff = got.br_startoff + got.br_blockcount;
  2573. ASSERT(got_endoff >= del_endoff);
  2574. delay = isnullstartblock(got.br_startblock);
  2575. ASSERT(isnullstartblock(del->br_startblock) == delay);
  2576. flags = 0;
  2577. qfield = 0;
  2578. error = 0;
  2579. /*
  2580. * If deleting a real allocation, must free up the disk space.
  2581. */
  2582. if (!delay) {
  2583. flags = XFS_ILOG_CORE;
  2584. /*
  2585. * Realtime allocation. Free it and record di_nblocks update.
  2586. */
  2587. if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) {
  2588. xfs_fsblock_t bno;
  2589. xfs_filblks_t len;
  2590. ASSERT(do_mod(del->br_blockcount,
  2591. mp->m_sb.sb_rextsize) == 0);
  2592. ASSERT(do_mod(del->br_startblock,
  2593. mp->m_sb.sb_rextsize) == 0);
  2594. bno = del->br_startblock;
  2595. len = del->br_blockcount;
  2596. do_div(bno, mp->m_sb.sb_rextsize);
  2597. do_div(len, mp->m_sb.sb_rextsize);
  2598. error = xfs_rtfree_extent(tp, bno, (xfs_extlen_t)len);
  2599. if (error)
  2600. goto done;
  2601. do_fx = 0;
  2602. nblks = len * mp->m_sb.sb_rextsize;
  2603. qfield = XFS_TRANS_DQ_RTBCOUNT;
  2604. }
  2605. /*
  2606. * Ordinary allocation.
  2607. */
  2608. else {
  2609. do_fx = 1;
  2610. nblks = del->br_blockcount;
  2611. qfield = XFS_TRANS_DQ_BCOUNT;
  2612. }
  2613. /*
  2614. * Set up del_endblock and cur for later.
  2615. */
  2616. del_endblock = del->br_startblock + del->br_blockcount;
  2617. if (cur) {
  2618. if ((error = xfs_bmbt_lookup_eq(cur, got.br_startoff,
  2619. got.br_startblock, got.br_blockcount,
  2620. &i)))
  2621. goto done;
  2622. XFS_WANT_CORRUPTED_GOTO(i == 1, done);
  2623. }
  2624. da_old = da_new = 0;
  2625. } else {
  2626. da_old = startblockval(got.br_startblock);
  2627. da_new = 0;
  2628. nblks = 0;
  2629. do_fx = 0;
  2630. }
  2631. /*
  2632. * Set flag value to use in switch statement.
  2633. * Left-contig is 2, right-contig is 1.
  2634. */
  2635. switch (((got.br_startoff == del->br_startoff) << 1) |
  2636. (got_endoff == del_endoff)) {
  2637. case 3:
  2638. /*
  2639. * Matches the whole extent. Delete the entry.
  2640. */
  2641. xfs_iext_remove(ip, *idx, 1,
  2642. whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0);
  2643. --*idx;
  2644. if (delay)
  2645. break;
  2646. XFS_IFORK_NEXT_SET(ip, whichfork,
  2647. XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
  2648. flags |= XFS_ILOG_CORE;
  2649. if (!cur) {
  2650. flags |= xfs_ilog_fext(whichfork);
  2651. break;
  2652. }
  2653. if ((error = xfs_btree_delete(cur, &i)))
  2654. goto done;
  2655. XFS_WANT_CORRUPTED_GOTO(i == 1, done);
  2656. break;
  2657. case 2:
  2658. /*
  2659. * Deleting the first part of the extent.
  2660. */
  2661. trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
  2662. xfs_bmbt_set_startoff(ep, del_endoff);
  2663. temp = got.br_blockcount - del->br_blockcount;
  2664. xfs_bmbt_set_blockcount(ep, temp);
  2665. if (delay) {
  2666. temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
  2667. da_old);
  2668. xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
  2669. trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
  2670. da_new = temp;
  2671. break;
  2672. }
  2673. xfs_bmbt_set_startblock(ep, del_endblock);
  2674. trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
  2675. if (!cur) {
  2676. flags |= xfs_ilog_fext(whichfork);
  2677. break;
  2678. }
  2679. if ((error = xfs_bmbt_update(cur, del_endoff, del_endblock,
  2680. got.br_blockcount - del->br_blockcount,
  2681. got.br_state)))
  2682. goto done;
  2683. break;
  2684. case 1:
  2685. /*
  2686. * Deleting the last part of the extent.
  2687. */
  2688. temp = got.br_blockcount - del->br_blockcount;
  2689. trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
  2690. xfs_bmbt_set_blockcount(ep, temp);
  2691. if (delay) {
  2692. temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
  2693. da_old);
  2694. xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
  2695. trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
  2696. da_new = temp;
  2697. break;
  2698. }
  2699. trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
  2700. if (!cur) {
  2701. flags |= xfs_ilog_fext(whichfork);
  2702. break;
  2703. }
  2704. if ((error = xfs_bmbt_update(cur, got.br_startoff,
  2705. got.br_startblock,
  2706. got.br_blockcount - del->br_blockcount,
  2707. got.br_state)))
  2708. goto done;
  2709. break;
  2710. case 0:
  2711. /*
  2712. * Deleting the middle of the extent.
  2713. */
  2714. temp = del->br_startoff - got.br_startoff;
  2715. trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
  2716. xfs_bmbt_set_blockcount(ep, temp);
  2717. new.br_startoff = del_endoff;
  2718. temp2 = got_endoff - del_endoff;
  2719. new.br_blockcount = temp2;
  2720. new.br_state = got.br_state;
  2721. if (!delay) {
  2722. new.br_startblock = del_endblock;
  2723. flags |= XFS_ILOG_CORE;
  2724. if (cur) {
  2725. if ((error = xfs_bmbt_update(cur,
  2726. got.br_startoff,
  2727. got.br_startblock, temp,
  2728. got.br_state)))
  2729. goto done;
  2730. if ((error = xfs_btree_increment(cur, 0, &i)))
  2731. goto done;
  2732. cur->bc_rec.b = new;
  2733. error = xfs_btree_insert(cur, &i);
  2734. if (error && error != ENOSPC)
  2735. goto done;
  2736. /*
  2737. * If get no-space back from btree insert,
  2738. * it tried a split, and we have a zero
  2739. * block reservation.
  2740. * Fix up our state and return the error.
  2741. */
  2742. if (error == ENOSPC) {
  2743. /*
  2744. * Reset the cursor, don't trust
  2745. * it after any insert operation.
  2746. */
  2747. if ((error = xfs_bmbt_lookup_eq(cur,
  2748. got.br_startoff,
  2749. got.br_startblock,
  2750. temp, &i)))
  2751. goto done;
  2752. XFS_WANT_CORRUPTED_GOTO(i == 1, done);
  2753. /*
  2754. * Update the btree record back
  2755. * to the original value.
  2756. */
  2757. if ((error = xfs_bmbt_update(cur,
  2758. got.br_startoff,
  2759. got.br_startblock,
  2760. got.br_blockcount,
  2761. got.br_state)))
  2762. goto done;
  2763. /*
  2764. * Reset the extent record back
  2765. * to the original value.
  2766. */
  2767. xfs_bmbt_set_blockcount(ep,
  2768. got.br_blockcount);
  2769. flags = 0;
  2770. error = XFS_ERROR(ENOSPC);
  2771. goto done;
  2772. }
  2773. XFS_WANT_CORRUPTED_GOTO(i == 1, done);
  2774. } else
  2775. flags |= xfs_ilog_fext(whichfork);
  2776. XFS_IFORK_NEXT_SET(ip, whichfork,
  2777. XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
  2778. } else {
  2779. ASSERT(whichfork == XFS_DATA_FORK);
  2780. temp = xfs_bmap_worst_indlen(ip, temp);
  2781. xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
  2782. temp2 = xfs_bmap_worst_indlen(ip, temp2);
  2783. new.br_startblock = nullstartblock((int)temp2);
  2784. da_new = temp + temp2;
  2785. while (da_new > da_old) {
  2786. if (temp) {
  2787. temp--;
  2788. da_new--;
  2789. xfs_bmbt_set_startblock(ep,
  2790. nullstartblock((int)temp));
  2791. }
  2792. if (da_new == da_old)
  2793. break;
  2794. if (temp2) {
  2795. temp2--;
  2796. da_new--;
  2797. new.br_startblock =
  2798. nullstartblock((int)temp2);
  2799. }
  2800. }
  2801. }
  2802. trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
  2803. xfs_iext_insert(ip, *idx + 1, 1, &new, state);
  2804. ++*idx;
  2805. break;
  2806. }
  2807. /*
  2808. * If we need to, add to list of extents to delete.
  2809. */
  2810. if (do_fx)
  2811. xfs_bmap_add_free(del->br_startblock, del->br_blockcount, flist,
  2812. mp);
  2813. /*
  2814. * Adjust inode # blocks in the file.
  2815. */
  2816. if (nblks)
  2817. ip->i_d.di_nblocks -= nblks;
  2818. /*
  2819. * Adjust quota data.
  2820. */
  2821. if (qfield)
  2822. xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks);
  2823. /*
  2824. * Account for change in delayed indirect blocks.
  2825. * Nothing to do for disk quota accounting here.
  2826. */
  2827. ASSERT(da_old >= da_new);
  2828. if (da_old > da_new) {
  2829. xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS,
  2830. (int64_t)(da_old - da_new), 0);
  2831. }
  2832. done:
  2833. *logflagsp = flags;
  2834. return error;
  2835. }
  2836. /*
  2837. * Remove the entry "free" from the free item list. Prev points to the
  2838. * previous entry, unless "free" is the head of the list.
  2839. */
  2840. STATIC void
  2841. xfs_bmap_del_free(
  2842. xfs_bmap_free_t *flist, /* free item list header */
  2843. xfs_bmap_free_item_t *prev, /* previous item on list, if any */
  2844. xfs_bmap_free_item_t *free) /* list item to be freed */
  2845. {
  2846. if (prev)
  2847. prev->xbfi_next = free->xbfi_next;
  2848. else
  2849. flist->xbf_first = free->xbfi_next;
  2850. flist->xbf_count--;
  2851. kmem_zone_free(xfs_bmap_free_item_zone, free);
  2852. }
  2853. /*
  2854. * Convert an extents-format file into a btree-format file.
  2855. * The new file will have a root block (in the inode) and a single child block.
  2856. */
  2857. STATIC int /* error */
  2858. xfs_bmap_extents_to_btree(
  2859. xfs_trans_t *tp, /* transaction pointer */
  2860. xfs_inode_t *ip, /* incore inode pointer */
  2861. xfs_fsblock_t *firstblock, /* first-block-allocated */
  2862. xfs_bmap_free_t *flist, /* blocks freed in xaction */
  2863. xfs_btree_cur_t **curp, /* cursor returned to caller */
  2864. int wasdel, /* converting a delayed alloc */
  2865. int *logflagsp, /* inode logging flags */
  2866. int whichfork) /* data or attr fork */
  2867. {
  2868. struct xfs_btree_block *ablock; /* allocated (child) bt block */
  2869. xfs_buf_t *abp; /* buffer for ablock */
  2870. xfs_alloc_arg_t args; /* allocation arguments */
  2871. xfs_bmbt_rec_t *arp; /* child record pointer */
  2872. struct xfs_btree_block *block; /* btree root block */
  2873. xfs_btree_cur_t *cur; /* bmap btree cursor */
  2874. xfs_bmbt_rec_host_t *ep; /* extent record pointer */
  2875. int error; /* error return value */
  2876. xfs_extnum_t i, cnt; /* extent record index */
  2877. xfs_ifork_t *ifp; /* inode fork pointer */
  2878. xfs_bmbt_key_t *kp; /* root block key pointer */
  2879. xfs_mount_t *mp; /* mount structure */
  2880. xfs_extnum_t nextents; /* number of file extents */
  2881. xfs_bmbt_ptr_t *pp; /* root block address pointer */
  2882. ifp = XFS_IFORK_PTR(ip, whichfork);
  2883. ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS);
  2884. /*
  2885. * Make space in the inode incore.
  2886. */
  2887. xfs_iroot_realloc(ip, 1, whichfork);
  2888. ifp->if_flags |= XFS_IFBROOT;
  2889. /*
  2890. * Fill in the root.
  2891. */
  2892. block = ifp->if_broot;
  2893. block->bb_magic = cpu_to_be32(XFS_BMAP_MAGIC);
  2894. block->bb_level = cpu_to_be16(1);
  2895. block->bb_numrecs = cpu_to_be16(1);
  2896. block->bb_u.l.bb_leftsib = cpu_to_be64(NULLDFSBNO);
  2897. block->bb_u.l.bb_rightsib = cpu_to_be64(NULLDFSBNO);
  2898. /*
  2899. * Need a cursor. Can't allocate until bb_level is filled in.
  2900. */
  2901. mp = ip->i_mount;
  2902. cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
  2903. cur->bc_private.b.firstblock = *firstblock;
  2904. cur->bc_private.b.flist = flist;
  2905. cur->bc_private.b.flags = wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
  2906. /*
  2907. * Convert to a btree with two levels, one record in root.
  2908. */
  2909. XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_BTREE);
  2910. memset(&args, 0, sizeof(args));
  2911. args.tp = tp;
  2912. args.mp = mp;
  2913. args.firstblock = *firstblock;
  2914. if (*firstblock == NULLFSBLOCK) {
  2915. args.type = XFS_ALLOCTYPE_START_BNO;
  2916. args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino);
  2917. } else if (flist->xbf_low) {
  2918. args.type = XFS_ALLOCTYPE_START_BNO;
  2919. args.fsbno = *firstblock;
  2920. } else {
  2921. args.type = XFS_ALLOCTYPE_NEAR_BNO;
  2922. args.fsbno = *firstblock;
  2923. }
  2924. args.minlen = args.maxlen = args.prod = 1;
  2925. args.total = args.minleft = args.alignment = args.mod = args.isfl =
  2926. args.minalignslop = 0;
  2927. args.wasdel = wasdel;
  2928. *logflagsp = 0;
  2929. if ((error = xfs_alloc_vextent(&args))) {
  2930. xfs_iroot_realloc(ip, -1, whichfork);
  2931. xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
  2932. return error;
  2933. }
  2934. /*
  2935. * Allocation can't fail, the space was reserved.
  2936. */
  2937. ASSERT(args.fsbno != NULLFSBLOCK);
  2938. ASSERT(*firstblock == NULLFSBLOCK ||
  2939. args.agno == XFS_FSB_TO_AGNO(mp, *firstblock) ||
  2940. (flist->xbf_low &&
  2941. args.agno > XFS_FSB_TO_AGNO(mp, *firstblock)));
  2942. *firstblock = cur->bc_private.b.firstblock = args.fsbno;
  2943. cur->bc_private.b.allocated++;
  2944. ip->i_d.di_nblocks++;
  2945. xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L);
  2946. abp = xfs_btree_get_bufl(mp, tp, args.fsbno, 0);
  2947. /*
  2948. * Fill in the child block.
  2949. */
  2950. abp->b_ops = &xfs_bmbt_buf_ops;
  2951. ablock = XFS_BUF_TO_BLOCK(abp);
  2952. ablock->bb_magic = cpu_to_be32(XFS_BMAP_MAGIC);
  2953. ablock->bb_level = 0;
  2954. ablock->bb_u.l.bb_leftsib = cpu_to_be64(NULLDFSBNO);
  2955. ablock->bb_u.l.bb_rightsib = cpu_to_be64(NULLDFSBNO);
  2956. arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
  2957. nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  2958. for (cnt = i = 0; i < nextents; i++) {
  2959. ep = xfs_iext_get_ext(ifp, i);
  2960. if (!isnullstartblock(xfs_bmbt_get_startblock(ep))) {
  2961. arp->l0 = cpu_to_be64(ep->l0);
  2962. arp->l1 = cpu_to_be64(ep->l1);
  2963. arp++; cnt++;
  2964. }
  2965. }
  2966. ASSERT(cnt == XFS_IFORK_NEXTENTS(ip, whichfork));
  2967. xfs_btree_set_numrecs(ablock, cnt);
  2968. /*
  2969. * Fill in the root key and pointer.
  2970. */
  2971. kp = XFS_BMBT_KEY_ADDR(mp, block, 1);
  2972. arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
  2973. kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp));
  2974. pp = XFS_BMBT_PTR_ADDR(mp, block, 1, xfs_bmbt_get_maxrecs(cur,
  2975. be16_to_cpu(block->bb_level)));
  2976. *pp = cpu_to_be64(args.fsbno);
  2977. /*
  2978. * Do all this logging at the end so that
  2979. * the root is at the right level.
  2980. */
  2981. xfs_btree_log_block(cur, abp, XFS_BB_ALL_BITS);
  2982. xfs_btree_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs));
  2983. ASSERT(*curp == NULL);
  2984. *curp = cur;
  2985. *logflagsp = XFS_ILOG_CORE | xfs_ilog_fbroot(whichfork);
  2986. return 0;
  2987. }
  2988. /*
  2989. * Calculate the default attribute fork offset for newly created inodes.
  2990. */
  2991. uint
  2992. xfs_default_attroffset(
  2993. struct xfs_inode *ip)
  2994. {
  2995. struct xfs_mount *mp = ip->i_mount;
  2996. uint offset;
  2997. if (mp->m_sb.sb_inodesize == 256) {
  2998. offset = XFS_LITINO(mp) -
  2999. XFS_BMDR_SPACE_CALC(MINABTPTRS);
  3000. } else {
  3001. offset = XFS_BMDR_SPACE_CALC(6 * MINABTPTRS);
  3002. }
  3003. ASSERT(offset < XFS_LITINO(mp));
  3004. return offset;
  3005. }
  3006. /*
  3007. * Helper routine to reset inode di_forkoff field when switching
  3008. * attribute fork from local to extent format - we reset it where
  3009. * possible to make space available for inline data fork extents.
  3010. */
  3011. STATIC void
  3012. xfs_bmap_forkoff_reset(
  3013. xfs_mount_t *mp,
  3014. xfs_inode_t *ip,
  3015. int whichfork)
  3016. {
  3017. if (whichfork == XFS_ATTR_FORK &&
  3018. ip->i_d.di_format != XFS_DINODE_FMT_DEV &&
  3019. ip->i_d.di_format != XFS_DINODE_FMT_UUID &&
  3020. ip->i_d.di_format != XFS_DINODE_FMT_BTREE) {
  3021. uint dfl_forkoff = xfs_default_attroffset(ip) >> 3;
  3022. if (dfl_forkoff > ip->i_d.di_forkoff)
  3023. ip->i_d.di_forkoff = dfl_forkoff;
  3024. }
  3025. }
  3026. /*
  3027. * Convert a local file to an extents file.
  3028. * This code is out of bounds for data forks of regular files,
  3029. * since the file data needs to get logged so things will stay consistent.
  3030. * (The bmap-level manipulations are ok, though).
  3031. */
  3032. STATIC int /* error */
  3033. xfs_bmap_local_to_extents(
  3034. xfs_trans_t *tp, /* transaction pointer */
  3035. xfs_inode_t *ip, /* incore inode pointer */
  3036. xfs_fsblock_t *firstblock, /* first block allocated in xaction */
  3037. xfs_extlen_t total, /* total blocks needed by transaction */
  3038. int *logflagsp, /* inode logging flags */
  3039. int whichfork,
  3040. void (*init_fn)(struct xfs_buf *bp,
  3041. struct xfs_inode *ip,
  3042. struct xfs_ifork *ifp))
  3043. {
  3044. int error; /* error return value */
  3045. int flags; /* logging flags returned */
  3046. xfs_ifork_t *ifp; /* inode fork pointer */
  3047. /*
  3048. * We don't want to deal with the case of keeping inode data inline yet.
  3049. * So sending the data fork of a regular inode is invalid.
  3050. */
  3051. ASSERT(!(S_ISREG(ip->i_d.di_mode) && whichfork == XFS_DATA_FORK));
  3052. ifp = XFS_IFORK_PTR(ip, whichfork);
  3053. ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
  3054. flags = 0;
  3055. error = 0;
  3056. if (ifp->if_bytes) {
  3057. xfs_alloc_arg_t args; /* allocation arguments */
  3058. xfs_buf_t *bp; /* buffer for extent block */
  3059. xfs_bmbt_rec_host_t *ep;/* extent record pointer */
  3060. ASSERT((ifp->if_flags &
  3061. (XFS_IFINLINE|XFS_IFEXTENTS|XFS_IFEXTIREC)) == XFS_IFINLINE);
  3062. memset(&args, 0, sizeof(args));
  3063. args.tp = tp;
  3064. args.mp = ip->i_mount;
  3065. args.firstblock = *firstblock;
  3066. /*
  3067. * Allocate a block. We know we need only one, since the
  3068. * file currently fits in an inode.
  3069. */
  3070. if (*firstblock == NULLFSBLOCK) {
  3071. args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino);
  3072. args.type = XFS_ALLOCTYPE_START_BNO;
  3073. } else {
  3074. args.fsbno = *firstblock;
  3075. args.type = XFS_ALLOCTYPE_NEAR_BNO;
  3076. }
  3077. args.total = total;
  3078. args.mod = args.minleft = args.alignment = args.wasdel =
  3079. args.isfl = args.minalignslop = 0;
  3080. args.minlen = args.maxlen = args.prod = 1;
  3081. error = xfs_alloc_vextent(&args);
  3082. if (error)
  3083. goto done;
  3084. /* Can't fail, the space was reserved. */
  3085. ASSERT(args.fsbno != NULLFSBLOCK);
  3086. ASSERT(args.len == 1);
  3087. *firstblock = args.fsbno;
  3088. bp = xfs_btree_get_bufl(args.mp, tp, args.fsbno, 0);
  3089. /* initialise the block and copy the data */
  3090. init_fn(bp, ip, ifp);
  3091. /* account for the change in fork size and log everything */
  3092. xfs_trans_log_buf(tp, bp, 0, ifp->if_bytes - 1);
  3093. xfs_bmap_forkoff_reset(args.mp, ip, whichfork);
  3094. xfs_idata_realloc(ip, -ifp->if_bytes, whichfork);
  3095. xfs_iext_add(ifp, 0, 1);
  3096. ep = xfs_iext_get_ext(ifp, 0);
  3097. xfs_bmbt_set_allf(ep, 0, args.fsbno, 1, XFS_EXT_NORM);
  3098. trace_xfs_bmap_post_update(ip, 0,
  3099. whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0,
  3100. _THIS_IP_);
  3101. XFS_IFORK_NEXT_SET(ip, whichfork, 1);
  3102. ip->i_d.di_nblocks = 1;
  3103. xfs_trans_mod_dquot_byino(tp, ip,
  3104. XFS_TRANS_DQ_BCOUNT, 1L);
  3105. flags |= xfs_ilog_fext(whichfork);
  3106. } else {
  3107. ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) == 0);
  3108. xfs_bmap_forkoff_reset(ip->i_mount, ip, whichfork);
  3109. }
  3110. ifp->if_flags &= ~XFS_IFINLINE;
  3111. ifp->if_flags |= XFS_IFEXTENTS;
  3112. XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
  3113. flags |= XFS_ILOG_CORE;
  3114. done:
  3115. *logflagsp = flags;
  3116. return error;
  3117. }
  3118. /*
  3119. * Search the extent records for the entry containing block bno.
  3120. * If bno lies in a hole, point to the next entry. If bno lies
  3121. * past eof, *eofp will be set, and *prevp will contain the last
  3122. * entry (null if none). Else, *lastxp will be set to the index
  3123. * of the found entry; *gotp will contain the entry.
  3124. */
  3125. STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */
  3126. xfs_bmap_search_multi_extents(
  3127. xfs_ifork_t *ifp, /* inode fork pointer */
  3128. xfs_fileoff_t bno, /* block number searched for */
  3129. int *eofp, /* out: end of file found */
  3130. xfs_extnum_t *lastxp, /* out: last extent index */
  3131. xfs_bmbt_irec_t *gotp, /* out: extent entry found */
  3132. xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
  3133. {
  3134. xfs_bmbt_rec_host_t *ep; /* extent record pointer */
  3135. xfs_extnum_t lastx; /* last extent index */
  3136. /*
  3137. * Initialize the extent entry structure to catch access to
  3138. * uninitialized br_startblock field.
  3139. */
  3140. gotp->br_startoff = 0xffa5a5a5a5a5a5a5LL;
  3141. gotp->br_blockcount = 0xa55a5a5a5a5a5a5aLL;
  3142. gotp->br_state = XFS_EXT_INVALID;
  3143. #if XFS_BIG_BLKNOS
  3144. gotp->br_startblock = 0xffffa5a5a5a5a5a5LL;
  3145. #else
  3146. gotp->br_startblock = 0xffffa5a5;
  3147. #endif
  3148. prevp->br_startoff = NULLFILEOFF;
  3149. ep = xfs_iext_bno_to_ext(ifp, bno, &lastx);
  3150. if (lastx > 0) {
  3151. xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx - 1), prevp);
  3152. }
  3153. if (lastx < (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))) {
  3154. xfs_bmbt_get_all(ep, gotp);
  3155. *eofp = 0;
  3156. } else {
  3157. if (lastx > 0) {
  3158. *gotp = *prevp;
  3159. }
  3160. *eofp = 1;
  3161. ep = NULL;
  3162. }
  3163. *lastxp = lastx;
  3164. return ep;
  3165. }
  3166. /*
  3167. * Search the extents list for the inode, for the extent containing bno.
  3168. * If bno lies in a hole, point to the next entry. If bno lies past eof,
  3169. * *eofp will be set, and *prevp will contain the last entry (null if none).
  3170. * Else, *lastxp will be set to the index of the found
  3171. * entry; *gotp will contain the entry.
  3172. */
  3173. STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */
  3174. xfs_bmap_search_extents(
  3175. xfs_inode_t *ip, /* incore inode pointer */
  3176. xfs_fileoff_t bno, /* block number searched for */
  3177. int fork, /* data or attr fork */
  3178. int *eofp, /* out: end of file found */
  3179. xfs_extnum_t *lastxp, /* out: last extent index */
  3180. xfs_bmbt_irec_t *gotp, /* out: extent entry found */
  3181. xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
  3182. {
  3183. xfs_ifork_t *ifp; /* inode fork pointer */
  3184. xfs_bmbt_rec_host_t *ep; /* extent record pointer */
  3185. XFS_STATS_INC(xs_look_exlist);
  3186. ifp = XFS_IFORK_PTR(ip, fork);
  3187. ep = xfs_bmap_search_multi_extents(ifp, bno, eofp, lastxp, gotp, prevp);
  3188. if (unlikely(!(gotp->br_startblock) && (*lastxp != NULLEXTNUM) &&
  3189. !(XFS_IS_REALTIME_INODE(ip) && fork == XFS_DATA_FORK))) {
  3190. xfs_alert_tag(ip->i_mount, XFS_PTAG_FSBLOCK_ZERO,
  3191. "Access to block zero in inode %llu "
  3192. "start_block: %llx start_off: %llx "
  3193. "blkcnt: %llx extent-state: %x lastx: %x\n",
  3194. (unsigned long long)ip->i_ino,
  3195. (unsigned long long)gotp->br_startblock,
  3196. (unsigned long long)gotp->br_startoff,
  3197. (unsigned long long)gotp->br_blockcount,
  3198. gotp->br_state, *lastxp);
  3199. *lastxp = NULLEXTNUM;
  3200. *eofp = 1;
  3201. return NULL;
  3202. }
  3203. return ep;
  3204. }
  3205. /*
  3206. * Compute the worst-case number of indirect blocks that will be used
  3207. * for ip's delayed extent of length "len".
  3208. */
  3209. STATIC xfs_filblks_t
  3210. xfs_bmap_worst_indlen(
  3211. xfs_inode_t *ip, /* incore inode pointer */
  3212. xfs_filblks_t len) /* delayed extent length */
  3213. {
  3214. int level; /* btree level number */
  3215. int maxrecs; /* maximum record count at this level */
  3216. xfs_mount_t *mp; /* mount structure */
  3217. xfs_filblks_t rval; /* return value */
  3218. mp = ip->i_mount;
  3219. maxrecs = mp->m_bmap_dmxr[0];
  3220. for (level = 0, rval = 0;
  3221. level < XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK);
  3222. level++) {
  3223. len += maxrecs - 1;
  3224. do_div(len, maxrecs);
  3225. rval += len;
  3226. if (len == 1)
  3227. return rval + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) -
  3228. level - 1;
  3229. if (level == 0)
  3230. maxrecs = mp->m_bmap_dmxr[1];
  3231. }
  3232. return rval;
  3233. }
  3234. /*
  3235. * Convert inode from non-attributed to attributed.
  3236. * Must not be in a transaction, ip must not be locked.
  3237. */
  3238. int /* error code */
  3239. xfs_bmap_add_attrfork(
  3240. xfs_inode_t *ip, /* incore inode pointer */
  3241. int size, /* space new attribute needs */
  3242. int rsvd) /* xact may use reserved blks */
  3243. {
  3244. xfs_fsblock_t firstblock; /* 1st block/ag allocated */
  3245. xfs_bmap_free_t flist; /* freed extent records */
  3246. xfs_mount_t *mp; /* mount structure */
  3247. xfs_trans_t *tp; /* transaction pointer */
  3248. int blks; /* space reservation */
  3249. int version = 1; /* superblock attr version */
  3250. int committed; /* xaction was committed */
  3251. int logflags; /* logging flags */
  3252. int error; /* error return value */
  3253. ASSERT(XFS_IFORK_Q(ip) == 0);
  3254. mp = ip->i_mount;
  3255. ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
  3256. tp = xfs_trans_alloc(mp, XFS_TRANS_ADDAFORK);
  3257. blks = XFS_ADDAFORK_SPACE_RES(mp);
  3258. if (rsvd)
  3259. tp->t_flags |= XFS_TRANS_RESERVE;
  3260. if ((error = xfs_trans_reserve(tp, blks, XFS_ADDAFORK_LOG_RES(mp), 0,
  3261. XFS_TRANS_PERM_LOG_RES, XFS_ADDAFORK_LOG_COUNT)))
  3262. goto error0;
  3263. xfs_ilock(ip, XFS_ILOCK_EXCL);
  3264. error = xfs_trans_reserve_quota_nblks(tp, ip, blks, 0, rsvd ?
  3265. XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
  3266. XFS_QMOPT_RES_REGBLKS);
  3267. if (error) {
  3268. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  3269. xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES);
  3270. return error;
  3271. }
  3272. if (XFS_IFORK_Q(ip))
  3273. goto error1;
  3274. if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS) {
  3275. /*
  3276. * For inodes coming from pre-6.2 filesystems.
  3277. */
  3278. ASSERT(ip->i_d.di_aformat == 0);
  3279. ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
  3280. }
  3281. ASSERT(ip->i_d.di_anextents == 0);
  3282. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  3283. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  3284. switch (ip->i_d.di_format) {
  3285. case XFS_DINODE_FMT_DEV:
  3286. ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3;
  3287. break;
  3288. case XFS_DINODE_FMT_UUID:
  3289. ip->i_d.di_forkoff = roundup(sizeof(uuid_t), 8) >> 3;
  3290. break;
  3291. case XFS_DINODE_FMT_LOCAL:
  3292. case XFS_DINODE_FMT_EXTENTS:
  3293. case XFS_DINODE_FMT_BTREE:
  3294. ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size);
  3295. if (!ip->i_d.di_forkoff)
  3296. ip->i_d.di_forkoff = xfs_default_attroffset(ip) >> 3;
  3297. else if (mp->m_flags & XFS_MOUNT_ATTR2)
  3298. version = 2;
  3299. break;
  3300. default:
  3301. ASSERT(0);
  3302. error = XFS_ERROR(EINVAL);
  3303. goto error1;
  3304. }
  3305. ASSERT(ip->i_afp == NULL);
  3306. ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP);
  3307. ip->i_afp->if_flags = XFS_IFEXTENTS;
  3308. logflags = 0;
  3309. xfs_bmap_init(&flist, &firstblock);
  3310. switch (ip->i_d.di_format) {
  3311. case XFS_DINODE_FMT_LOCAL:
  3312. error = xfs_bmap_add_attrfork_local(tp, ip, &firstblock, &flist,
  3313. &logflags);
  3314. break;
  3315. case XFS_DINODE_FMT_EXTENTS:
  3316. error = xfs_bmap_add_attrfork_extents(tp, ip, &firstblock,
  3317. &flist, &logflags);
  3318. break;
  3319. case XFS_DINODE_FMT_BTREE:
  3320. error = xfs_bmap_add_attrfork_btree(tp, ip, &firstblock, &flist,
  3321. &logflags);
  3322. break;
  3323. default:
  3324. error = 0;
  3325. break;
  3326. }
  3327. if (logflags)
  3328. xfs_trans_log_inode(tp, ip, logflags);
  3329. if (error)
  3330. goto error2;
  3331. if (!xfs_sb_version_hasattr(&mp->m_sb) ||
  3332. (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2)) {
  3333. __int64_t sbfields = 0;
  3334. spin_lock(&mp->m_sb_lock);
  3335. if (!xfs_sb_version_hasattr(&mp->m_sb)) {
  3336. xfs_sb_version_addattr(&mp->m_sb);
  3337. sbfields |= XFS_SB_VERSIONNUM;
  3338. }
  3339. if (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2) {
  3340. xfs_sb_version_addattr2(&mp->m_sb);
  3341. sbfields |= (XFS_SB_VERSIONNUM | XFS_SB_FEATURES2);
  3342. }
  3343. if (sbfields) {
  3344. spin_unlock(&mp->m_sb_lock);
  3345. xfs_mod_sb(tp, sbfields);
  3346. } else
  3347. spin_unlock(&mp->m_sb_lock);
  3348. }
  3349. error = xfs_bmap_finish(&tp, &flist, &committed);
  3350. if (error)
  3351. goto error2;
  3352. return xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  3353. error2:
  3354. xfs_bmap_cancel(&flist);
  3355. error1:
  3356. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  3357. error0:
  3358. xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
  3359. return error;
  3360. }
  3361. /*
  3362. * Add the extent to the list of extents to be free at transaction end.
  3363. * The list is maintained sorted (by block number).
  3364. */
  3365. /* ARGSUSED */
  3366. void
  3367. xfs_bmap_add_free(
  3368. xfs_fsblock_t bno, /* fs block number of extent */
  3369. xfs_filblks_t len, /* length of extent */
  3370. xfs_bmap_free_t *flist, /* list of extents */
  3371. xfs_mount_t *mp) /* mount point structure */
  3372. {
  3373. xfs_bmap_free_item_t *cur; /* current (next) element */
  3374. xfs_bmap_free_item_t *new; /* new element */
  3375. xfs_bmap_free_item_t *prev; /* previous element */
  3376. #ifdef DEBUG
  3377. xfs_agnumber_t agno;
  3378. xfs_agblock_t agbno;
  3379. ASSERT(bno != NULLFSBLOCK);
  3380. ASSERT(len > 0);
  3381. ASSERT(len <= MAXEXTLEN);
  3382. ASSERT(!isnullstartblock(bno));
  3383. agno = XFS_FSB_TO_AGNO(mp, bno);
  3384. agbno = XFS_FSB_TO_AGBNO(mp, bno);
  3385. ASSERT(agno < mp->m_sb.sb_agcount);
  3386. ASSERT(agbno < mp->m_sb.sb_agblocks);
  3387. ASSERT(len < mp->m_sb.sb_agblocks);
  3388. ASSERT(agbno + len <= mp->m_sb.sb_agblocks);
  3389. #endif
  3390. ASSERT(xfs_bmap_free_item_zone != NULL);
  3391. new = kmem_zone_alloc(xfs_bmap_free_item_zone, KM_SLEEP);
  3392. new->xbfi_startblock = bno;
  3393. new->xbfi_blockcount = (xfs_extlen_t)len;
  3394. for (prev = NULL, cur = flist->xbf_first;
  3395. cur != NULL;
  3396. prev = cur, cur = cur->xbfi_next) {
  3397. if (cur->xbfi_startblock >= bno)
  3398. break;
  3399. }
  3400. if (prev)
  3401. prev->xbfi_next = new;
  3402. else
  3403. flist->xbf_first = new;
  3404. new->xbfi_next = cur;
  3405. flist->xbf_count++;
  3406. }
  3407. /*
  3408. * Compute and fill in the value of the maximum depth of a bmap btree
  3409. * in this filesystem. Done once, during mount.
  3410. */
  3411. void
  3412. xfs_bmap_compute_maxlevels(
  3413. xfs_mount_t *mp, /* file system mount structure */
  3414. int whichfork) /* data or attr fork */
  3415. {
  3416. int level; /* btree level */
  3417. uint maxblocks; /* max blocks at this level */
  3418. uint maxleafents; /* max leaf entries possible */
  3419. int maxrootrecs; /* max records in root block */
  3420. int minleafrecs; /* min records in leaf block */
  3421. int minnoderecs; /* min records in node block */
  3422. int sz; /* root block size */
  3423. /*
  3424. * The maximum number of extents in a file, hence the maximum
  3425. * number of leaf entries, is controlled by the type of di_nextents
  3426. * (a signed 32-bit number, xfs_extnum_t), or by di_anextents
  3427. * (a signed 16-bit number, xfs_aextnum_t).
  3428. *
  3429. * Note that we can no longer assume that if we are in ATTR1 that
  3430. * the fork offset of all the inodes will be
  3431. * (xfs_default_attroffset(ip) >> 3) because we could have mounted
  3432. * with ATTR2 and then mounted back with ATTR1, keeping the
  3433. * di_forkoff's fixed but probably at various positions. Therefore,
  3434. * for both ATTR1 and ATTR2 we have to assume the worst case scenario
  3435. * of a minimum size available.
  3436. */
  3437. if (whichfork == XFS_DATA_FORK) {
  3438. maxleafents = MAXEXTNUM;
  3439. sz = XFS_BMDR_SPACE_CALC(MINDBTPTRS);
  3440. } else {
  3441. maxleafents = MAXAEXTNUM;
  3442. sz = XFS_BMDR_SPACE_CALC(MINABTPTRS);
  3443. }
  3444. maxrootrecs = xfs_bmdr_maxrecs(mp, sz, 0);
  3445. minleafrecs = mp->m_bmap_dmnr[0];
  3446. minnoderecs = mp->m_bmap_dmnr[1];
  3447. maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
  3448. for (level = 1; maxblocks > 1; level++) {
  3449. if (maxblocks <= maxrootrecs)
  3450. maxblocks = 1;
  3451. else
  3452. maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
  3453. }
  3454. mp->m_bm_maxlevels[whichfork] = level;
  3455. }
  3456. /*
  3457. * Routine to be called at transaction's end by xfs_bmapi, xfs_bunmapi
  3458. * caller. Frees all the extents that need freeing, which must be done
  3459. * last due to locking considerations. We never free any extents in
  3460. * the first transaction.
  3461. *
  3462. * Return 1 if the given transaction was committed and a new one
  3463. * started, and 0 otherwise in the committed parameter.
  3464. */
  3465. int /* error */
  3466. xfs_bmap_finish(
  3467. xfs_trans_t **tp, /* transaction pointer addr */
  3468. xfs_bmap_free_t *flist, /* i/o: list extents to free */
  3469. int *committed) /* xact committed or not */
  3470. {
  3471. xfs_efd_log_item_t *efd; /* extent free data */
  3472. xfs_efi_log_item_t *efi; /* extent free intention */
  3473. int error; /* error return value */
  3474. xfs_bmap_free_item_t *free; /* free extent item */
  3475. unsigned int logres; /* new log reservation */
  3476. unsigned int logcount; /* new log count */
  3477. xfs_mount_t *mp; /* filesystem mount structure */
  3478. xfs_bmap_free_item_t *next; /* next item on free list */
  3479. xfs_trans_t *ntp; /* new transaction pointer */
  3480. ASSERT((*tp)->t_flags & XFS_TRANS_PERM_LOG_RES);
  3481. if (flist->xbf_count == 0) {
  3482. *committed = 0;
  3483. return 0;
  3484. }
  3485. ntp = *tp;
  3486. efi = xfs_trans_get_efi(ntp, flist->xbf_count);
  3487. for (free = flist->xbf_first; free; free = free->xbfi_next)
  3488. xfs_trans_log_efi_extent(ntp, efi, free->xbfi_startblock,
  3489. free->xbfi_blockcount);
  3490. logres = ntp->t_log_res;
  3491. logcount = ntp->t_log_count;
  3492. ntp = xfs_trans_dup(*tp);
  3493. error = xfs_trans_commit(*tp, 0);
  3494. *tp = ntp;
  3495. *committed = 1;
  3496. /*
  3497. * We have a new transaction, so we should return committed=1,
  3498. * even though we're returning an error.
  3499. */
  3500. if (error)
  3501. return error;
  3502. /*
  3503. * transaction commit worked ok so we can drop the extra ticket
  3504. * reference that we gained in xfs_trans_dup()
  3505. */
  3506. xfs_log_ticket_put(ntp->t_ticket);
  3507. if ((error = xfs_trans_reserve(ntp, 0, logres, 0, XFS_TRANS_PERM_LOG_RES,
  3508. logcount)))
  3509. return error;
  3510. efd = xfs_trans_get_efd(ntp, efi, flist->xbf_count);
  3511. for (free = flist->xbf_first; free != NULL; free = next) {
  3512. next = free->xbfi_next;
  3513. if ((error = xfs_free_extent(ntp, free->xbfi_startblock,
  3514. free->xbfi_blockcount))) {
  3515. /*
  3516. * The bmap free list will be cleaned up at a
  3517. * higher level. The EFI will be canceled when
  3518. * this transaction is aborted.
  3519. * Need to force shutdown here to make sure it
  3520. * happens, since this transaction may not be
  3521. * dirty yet.
  3522. */
  3523. mp = ntp->t_mountp;
  3524. if (!XFS_FORCED_SHUTDOWN(mp))
  3525. xfs_force_shutdown(mp,
  3526. (error == EFSCORRUPTED) ?
  3527. SHUTDOWN_CORRUPT_INCORE :
  3528. SHUTDOWN_META_IO_ERROR);
  3529. return error;
  3530. }
  3531. xfs_trans_log_efd_extent(ntp, efd, free->xbfi_startblock,
  3532. free->xbfi_blockcount);
  3533. xfs_bmap_del_free(flist, NULL, free);
  3534. }
  3535. return 0;
  3536. }
  3537. /*
  3538. * Free up any items left in the list.
  3539. */
  3540. void
  3541. xfs_bmap_cancel(
  3542. xfs_bmap_free_t *flist) /* list of bmap_free_items */
  3543. {
  3544. xfs_bmap_free_item_t *free; /* free list item */
  3545. xfs_bmap_free_item_t *next;
  3546. if (flist->xbf_count == 0)
  3547. return;
  3548. ASSERT(flist->xbf_first != NULL);
  3549. for (free = flist->xbf_first; free; free = next) {
  3550. next = free->xbfi_next;
  3551. xfs_bmap_del_free(flist, NULL, free);
  3552. }
  3553. ASSERT(flist->xbf_count == 0);
  3554. }
  3555. /*
  3556. * Returns the file-relative block number of the first unused block(s)
  3557. * in the file with at least "len" logically contiguous blocks free.
  3558. * This is the lowest-address hole if the file has holes, else the first block
  3559. * past the end of file.
  3560. * Return 0 if the file is currently local (in-inode).
  3561. */
  3562. int /* error */
  3563. xfs_bmap_first_unused(
  3564. xfs_trans_t *tp, /* transaction pointer */
  3565. xfs_inode_t *ip, /* incore inode */
  3566. xfs_extlen_t len, /* size of hole to find */
  3567. xfs_fileoff_t *first_unused, /* unused block */
  3568. int whichfork) /* data or attr fork */
  3569. {
  3570. int error; /* error return value */
  3571. int idx; /* extent record index */
  3572. xfs_ifork_t *ifp; /* inode fork pointer */
  3573. xfs_fileoff_t lastaddr; /* last block number seen */
  3574. xfs_fileoff_t lowest; /* lowest useful block */
  3575. xfs_fileoff_t max; /* starting useful block */
  3576. xfs_fileoff_t off; /* offset for this block */
  3577. xfs_extnum_t nextents; /* number of extent entries */
  3578. ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE ||
  3579. XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ||
  3580. XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
  3581. if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
  3582. *first_unused = 0;
  3583. return 0;
  3584. }
  3585. ifp = XFS_IFORK_PTR(ip, whichfork);
  3586. if (!(ifp->if_flags & XFS_IFEXTENTS) &&
  3587. (error = xfs_iread_extents(tp, ip, whichfork)))
  3588. return error;
  3589. lowest = *first_unused;
  3590. nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  3591. for (idx = 0, lastaddr = 0, max = lowest; idx < nextents; idx++) {
  3592. xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, idx);
  3593. off = xfs_bmbt_get_startoff(ep);
  3594. /*
  3595. * See if the hole before this extent will work.
  3596. */
  3597. if (off >= lowest + len && off - max >= len) {
  3598. *first_unused = max;
  3599. return 0;
  3600. }
  3601. lastaddr = off + xfs_bmbt_get_blockcount(ep);
  3602. max = XFS_FILEOFF_MAX(lastaddr, lowest);
  3603. }
  3604. *first_unused = max;
  3605. return 0;
  3606. }
  3607. /*
  3608. * Returns the file-relative block number of the last block + 1 before
  3609. * last_block (input value) in the file.
  3610. * This is not based on i_size, it is based on the extent records.
  3611. * Returns 0 for local files, as they do not have extent records.
  3612. */
  3613. int /* error */
  3614. xfs_bmap_last_before(
  3615. xfs_trans_t *tp, /* transaction pointer */
  3616. xfs_inode_t *ip, /* incore inode */
  3617. xfs_fileoff_t *last_block, /* last block */
  3618. int whichfork) /* data or attr fork */
  3619. {
  3620. xfs_fileoff_t bno; /* input file offset */
  3621. int eof; /* hit end of file */
  3622. xfs_bmbt_rec_host_t *ep; /* pointer to last extent */
  3623. int error; /* error return value */
  3624. xfs_bmbt_irec_t got; /* current extent value */
  3625. xfs_ifork_t *ifp; /* inode fork pointer */
  3626. xfs_extnum_t lastx; /* last extent used */
  3627. xfs_bmbt_irec_t prev; /* previous extent value */
  3628. if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
  3629. XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
  3630. XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL)
  3631. return XFS_ERROR(EIO);
  3632. if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
  3633. *last_block = 0;
  3634. return 0;
  3635. }
  3636. ifp = XFS_IFORK_PTR(ip, whichfork);
  3637. if (!(ifp->if_flags & XFS_IFEXTENTS) &&
  3638. (error = xfs_iread_extents(tp, ip, whichfork)))
  3639. return error;
  3640. bno = *last_block - 1;
  3641. ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
  3642. &prev);
  3643. if (eof || xfs_bmbt_get_startoff(ep) > bno) {
  3644. if (prev.br_startoff == NULLFILEOFF)
  3645. *last_block = 0;
  3646. else
  3647. *last_block = prev.br_startoff + prev.br_blockcount;
  3648. }
  3649. /*
  3650. * Otherwise *last_block is already the right answer.
  3651. */
  3652. return 0;
  3653. }
  3654. STATIC int
  3655. xfs_bmap_last_extent(
  3656. struct xfs_trans *tp,
  3657. struct xfs_inode *ip,
  3658. int whichfork,
  3659. struct xfs_bmbt_irec *rec,
  3660. int *is_empty)
  3661. {
  3662. struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
  3663. int error;
  3664. int nextents;
  3665. if (!(ifp->if_flags & XFS_IFEXTENTS)) {
  3666. error = xfs_iread_extents(tp, ip, whichfork);
  3667. if (error)
  3668. return error;
  3669. }
  3670. nextents = ifp->if_bytes / sizeof(xfs_bmbt_rec_t);
  3671. if (nextents == 0) {
  3672. *is_empty = 1;
  3673. return 0;
  3674. }
  3675. xfs_bmbt_get_all(xfs_iext_get_ext(ifp, nextents - 1), rec);
  3676. *is_empty = 0;
  3677. return 0;
  3678. }
  3679. /*
  3680. * Check the last inode extent to determine whether this allocation will result
  3681. * in blocks being allocated at the end of the file. When we allocate new data
  3682. * blocks at the end of the file which do not start at the previous data block,
  3683. * we will try to align the new blocks at stripe unit boundaries.
  3684. *
  3685. * Returns 0 in bma->aeof if the file (fork) is empty as any new write will be
  3686. * at, or past the EOF.
  3687. */
  3688. STATIC int
  3689. xfs_bmap_isaeof(
  3690. struct xfs_bmalloca *bma,
  3691. int whichfork)
  3692. {
  3693. struct xfs_bmbt_irec rec;
  3694. int is_empty;
  3695. int error;
  3696. bma->aeof = 0;
  3697. error = xfs_bmap_last_extent(NULL, bma->ip, whichfork, &rec,
  3698. &is_empty);
  3699. if (error || is_empty)
  3700. return error;
  3701. /*
  3702. * Check if we are allocation or past the last extent, or at least into
  3703. * the last delayed allocated extent.
  3704. */
  3705. bma->aeof = bma->offset >= rec.br_startoff + rec.br_blockcount ||
  3706. (bma->offset >= rec.br_startoff &&
  3707. isnullstartblock(rec.br_startblock));
  3708. return 0;
  3709. }
  3710. /*
  3711. * Check if the endoff is outside the last extent. If so the caller will grow
  3712. * the allocation to a stripe unit boundary. All offsets are considered outside
  3713. * the end of file for an empty fork, so 1 is returned in *eof in that case.
  3714. */
  3715. int
  3716. xfs_bmap_eof(
  3717. struct xfs_inode *ip,
  3718. xfs_fileoff_t endoff,
  3719. int whichfork,
  3720. int *eof)
  3721. {
  3722. struct xfs_bmbt_irec rec;
  3723. int error;
  3724. error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, eof);
  3725. if (error || *eof)
  3726. return error;
  3727. *eof = endoff >= rec.br_startoff + rec.br_blockcount;
  3728. return 0;
  3729. }
  3730. /*
  3731. * Returns the file-relative block number of the first block past eof in
  3732. * the file. This is not based on i_size, it is based on the extent records.
  3733. * Returns 0 for local files, as they do not have extent records.
  3734. */
  3735. int
  3736. xfs_bmap_last_offset(
  3737. struct xfs_trans *tp,
  3738. struct xfs_inode *ip,
  3739. xfs_fileoff_t *last_block,
  3740. int whichfork)
  3741. {
  3742. struct xfs_bmbt_irec rec;
  3743. int is_empty;
  3744. int error;
  3745. *last_block = 0;
  3746. if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL)
  3747. return 0;
  3748. if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
  3749. XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
  3750. return XFS_ERROR(EIO);
  3751. error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, &is_empty);
  3752. if (error || is_empty)
  3753. return error;
  3754. *last_block = rec.br_startoff + rec.br_blockcount;
  3755. return 0;
  3756. }
  3757. /*
  3758. * Returns whether the selected fork of the inode has exactly one
  3759. * block or not. For the data fork we check this matches di_size,
  3760. * implying the file's range is 0..bsize-1.
  3761. */
  3762. int /* 1=>1 block, 0=>otherwise */
  3763. xfs_bmap_one_block(
  3764. xfs_inode_t *ip, /* incore inode */
  3765. int whichfork) /* data or attr fork */
  3766. {
  3767. xfs_bmbt_rec_host_t *ep; /* ptr to fork's extent */
  3768. xfs_ifork_t *ifp; /* inode fork pointer */
  3769. int rval; /* return value */
  3770. xfs_bmbt_irec_t s; /* internal version of extent */
  3771. #ifndef DEBUG
  3772. if (whichfork == XFS_DATA_FORK)
  3773. return XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize;
  3774. #endif /* !DEBUG */
  3775. if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1)
  3776. return 0;
  3777. if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
  3778. return 0;
  3779. ifp = XFS_IFORK_PTR(ip, whichfork);
  3780. ASSERT(ifp->if_flags & XFS_IFEXTENTS);
  3781. ep = xfs_iext_get_ext(ifp, 0);
  3782. xfs_bmbt_get_all(ep, &s);
  3783. rval = s.br_startoff == 0 && s.br_blockcount == 1;
  3784. if (rval && whichfork == XFS_DATA_FORK)
  3785. ASSERT(XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize);
  3786. return rval;
  3787. }
  3788. STATIC int
  3789. xfs_bmap_sanity_check(
  3790. struct xfs_mount *mp,
  3791. struct xfs_buf *bp,
  3792. int level)
  3793. {
  3794. struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
  3795. if (block->bb_magic != cpu_to_be32(XFS_BMAP_MAGIC) ||
  3796. be16_to_cpu(block->bb_level) != level ||
  3797. be16_to_cpu(block->bb_numrecs) == 0 ||
  3798. be16_to_cpu(block->bb_numrecs) > mp->m_bmap_dmxr[level != 0])
  3799. return 0;
  3800. return 1;
  3801. }
  3802. /*
  3803. * Read in the extents to if_extents.
  3804. * All inode fields are set up by caller, we just traverse the btree
  3805. * and copy the records in. If the file system cannot contain unwritten
  3806. * extents, the records are checked for no "state" flags.
  3807. */
  3808. int /* error */
  3809. xfs_bmap_read_extents(
  3810. xfs_trans_t *tp, /* transaction pointer */
  3811. xfs_inode_t *ip, /* incore inode */
  3812. int whichfork) /* data or attr fork */
  3813. {
  3814. struct xfs_btree_block *block; /* current btree block */
  3815. xfs_fsblock_t bno; /* block # of "block" */
  3816. xfs_buf_t *bp; /* buffer for "block" */
  3817. int error; /* error return value */
  3818. xfs_exntfmt_t exntf; /* XFS_EXTFMT_NOSTATE, if checking */
  3819. xfs_extnum_t i, j; /* index into the extents list */
  3820. xfs_ifork_t *ifp; /* fork structure */
  3821. int level; /* btree level, for checking */
  3822. xfs_mount_t *mp; /* file system mount structure */
  3823. __be64 *pp; /* pointer to block address */
  3824. /* REFERENCED */
  3825. xfs_extnum_t room; /* number of entries there's room for */
  3826. bno = NULLFSBLOCK;
  3827. mp = ip->i_mount;
  3828. ifp = XFS_IFORK_PTR(ip, whichfork);
  3829. exntf = (whichfork != XFS_DATA_FORK) ? XFS_EXTFMT_NOSTATE :
  3830. XFS_EXTFMT_INODE(ip);
  3831. block = ifp->if_broot;
  3832. /*
  3833. * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
  3834. */
  3835. level = be16_to_cpu(block->bb_level);
  3836. ASSERT(level > 0);
  3837. pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
  3838. bno = be64_to_cpu(*pp);
  3839. ASSERT(bno != NULLDFSBNO);
  3840. ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
  3841. ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
  3842. /*
  3843. * Go down the tree until leaf level is reached, following the first
  3844. * pointer (leftmost) at each level.
  3845. */
  3846. while (level-- > 0) {
  3847. error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
  3848. XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops);
  3849. if (error)
  3850. return error;
  3851. block = XFS_BUF_TO_BLOCK(bp);
  3852. XFS_WANT_CORRUPTED_GOTO(
  3853. xfs_bmap_sanity_check(mp, bp, level),
  3854. error0);
  3855. if (level == 0)
  3856. break;
  3857. pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
  3858. bno = be64_to_cpu(*pp);
  3859. XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp, bno), error0);
  3860. xfs_trans_brelse(tp, bp);
  3861. }
  3862. /*
  3863. * Here with bp and block set to the leftmost leaf node in the tree.
  3864. */
  3865. room = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  3866. i = 0;
  3867. /*
  3868. * Loop over all leaf nodes. Copy information to the extent records.
  3869. */
  3870. for (;;) {
  3871. xfs_bmbt_rec_t *frp;
  3872. xfs_fsblock_t nextbno;
  3873. xfs_extnum_t num_recs;
  3874. xfs_extnum_t start;
  3875. num_recs = xfs_btree_get_numrecs(block);
  3876. if (unlikely(i + num_recs > room)) {
  3877. ASSERT(i + num_recs <= room);
  3878. xfs_warn(ip->i_mount,
  3879. "corrupt dinode %Lu, (btree extents).",
  3880. (unsigned long long) ip->i_ino);
  3881. XFS_CORRUPTION_ERROR("xfs_bmap_read_extents(1)",
  3882. XFS_ERRLEVEL_LOW, ip->i_mount, block);
  3883. goto error0;
  3884. }
  3885. XFS_WANT_CORRUPTED_GOTO(
  3886. xfs_bmap_sanity_check(mp, bp, 0),
  3887. error0);
  3888. /*
  3889. * Read-ahead the next leaf block, if any.
  3890. */
  3891. nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
  3892. if (nextbno != NULLFSBLOCK)
  3893. xfs_btree_reada_bufl(mp, nextbno, 1,
  3894. &xfs_bmbt_buf_ops);
  3895. /*
  3896. * Copy records into the extent records.
  3897. */
  3898. frp = XFS_BMBT_REC_ADDR(mp, block, 1);
  3899. start = i;
  3900. for (j = 0; j < num_recs; j++, i++, frp++) {
  3901. xfs_bmbt_rec_host_t *trp = xfs_iext_get_ext(ifp, i);
  3902. trp->l0 = be64_to_cpu(frp->l0);
  3903. trp->l1 = be64_to_cpu(frp->l1);
  3904. }
  3905. if (exntf == XFS_EXTFMT_NOSTATE) {
  3906. /*
  3907. * Check all attribute bmap btree records and
  3908. * any "older" data bmap btree records for a
  3909. * set bit in the "extent flag" position.
  3910. */
  3911. if (unlikely(xfs_check_nostate_extents(ifp,
  3912. start, num_recs))) {
  3913. XFS_ERROR_REPORT("xfs_bmap_read_extents(2)",
  3914. XFS_ERRLEVEL_LOW,
  3915. ip->i_mount);
  3916. goto error0;
  3917. }
  3918. }
  3919. xfs_trans_brelse(tp, bp);
  3920. bno = nextbno;
  3921. /*
  3922. * If we've reached the end, stop.
  3923. */
  3924. if (bno == NULLFSBLOCK)
  3925. break;
  3926. error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
  3927. XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops);
  3928. if (error)
  3929. return error;
  3930. block = XFS_BUF_TO_BLOCK(bp);
  3931. }
  3932. ASSERT(i == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)));
  3933. ASSERT(i == XFS_IFORK_NEXTENTS(ip, whichfork));
  3934. XFS_BMAP_TRACE_EXLIST(ip, i, whichfork);
  3935. return 0;
  3936. error0:
  3937. xfs_trans_brelse(tp, bp);
  3938. return XFS_ERROR(EFSCORRUPTED);
  3939. }
  3940. #ifdef DEBUG
  3941. /*
  3942. * Add bmap trace insert entries for all the contents of the extent records.
  3943. */
  3944. void
  3945. xfs_bmap_trace_exlist(
  3946. xfs_inode_t *ip, /* incore inode pointer */
  3947. xfs_extnum_t cnt, /* count of entries in the list */
  3948. int whichfork, /* data or attr fork */
  3949. unsigned long caller_ip)
  3950. {
  3951. xfs_extnum_t idx; /* extent record index */
  3952. xfs_ifork_t *ifp; /* inode fork pointer */
  3953. int state = 0;
  3954. if (whichfork == XFS_ATTR_FORK)
  3955. state |= BMAP_ATTRFORK;
  3956. ifp = XFS_IFORK_PTR(ip, whichfork);
  3957. ASSERT(cnt == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)));
  3958. for (idx = 0; idx < cnt; idx++)
  3959. trace_xfs_extlist(ip, idx, whichfork, caller_ip);
  3960. }
  3961. /*
  3962. * Validate that the bmbt_irecs being returned from bmapi are valid
  3963. * given the callers original parameters. Specifically check the
  3964. * ranges of the returned irecs to ensure that they only extent beyond
  3965. * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
  3966. */
  3967. STATIC void
  3968. xfs_bmap_validate_ret(
  3969. xfs_fileoff_t bno,
  3970. xfs_filblks_t len,
  3971. int flags,
  3972. xfs_bmbt_irec_t *mval,
  3973. int nmap,
  3974. int ret_nmap)
  3975. {
  3976. int i; /* index to map values */
  3977. ASSERT(ret_nmap <= nmap);
  3978. for (i = 0; i < ret_nmap; i++) {
  3979. ASSERT(mval[i].br_blockcount > 0);
  3980. if (!(flags & XFS_BMAPI_ENTIRE)) {
  3981. ASSERT(mval[i].br_startoff >= bno);
  3982. ASSERT(mval[i].br_blockcount <= len);
  3983. ASSERT(mval[i].br_startoff + mval[i].br_blockcount <=
  3984. bno + len);
  3985. } else {
  3986. ASSERT(mval[i].br_startoff < bno + len);
  3987. ASSERT(mval[i].br_startoff + mval[i].br_blockcount >
  3988. bno);
  3989. }
  3990. ASSERT(i == 0 ||
  3991. mval[i - 1].br_startoff + mval[i - 1].br_blockcount ==
  3992. mval[i].br_startoff);
  3993. ASSERT(mval[i].br_startblock != DELAYSTARTBLOCK &&
  3994. mval[i].br_startblock != HOLESTARTBLOCK);
  3995. ASSERT(mval[i].br_state == XFS_EXT_NORM ||
  3996. mval[i].br_state == XFS_EXT_UNWRITTEN);
  3997. }
  3998. }
  3999. #endif /* DEBUG */
  4000. /*
  4001. * Trim the returned map to the required bounds
  4002. */
  4003. STATIC void
  4004. xfs_bmapi_trim_map(
  4005. struct xfs_bmbt_irec *mval,
  4006. struct xfs_bmbt_irec *got,
  4007. xfs_fileoff_t *bno,
  4008. xfs_filblks_t len,
  4009. xfs_fileoff_t obno,
  4010. xfs_fileoff_t end,
  4011. int n,
  4012. int flags)
  4013. {
  4014. if ((flags & XFS_BMAPI_ENTIRE) ||
  4015. got->br_startoff + got->br_blockcount <= obno) {
  4016. *mval = *got;
  4017. if (isnullstartblock(got->br_startblock))
  4018. mval->br_startblock = DELAYSTARTBLOCK;
  4019. return;
  4020. }
  4021. if (obno > *bno)
  4022. *bno = obno;
  4023. ASSERT((*bno >= obno) || (n == 0));
  4024. ASSERT(*bno < end);
  4025. mval->br_startoff = *bno;
  4026. if (isnullstartblock(got->br_startblock))
  4027. mval->br_startblock = DELAYSTARTBLOCK;
  4028. else
  4029. mval->br_startblock = got->br_startblock +
  4030. (*bno - got->br_startoff);
  4031. /*
  4032. * Return the minimum of what we got and what we asked for for
  4033. * the length. We can use the len variable here because it is
  4034. * modified below and we could have been there before coming
  4035. * here if the first part of the allocation didn't overlap what
  4036. * was asked for.
  4037. */
  4038. mval->br_blockcount = XFS_FILBLKS_MIN(end - *bno,
  4039. got->br_blockcount - (*bno - got->br_startoff));
  4040. mval->br_state = got->br_state;
  4041. ASSERT(mval->br_blockcount <= len);
  4042. return;
  4043. }
  4044. /*
  4045. * Update and validate the extent map to return
  4046. */
  4047. STATIC void
  4048. xfs_bmapi_update_map(
  4049. struct xfs_bmbt_irec **map,
  4050. xfs_fileoff_t *bno,
  4051. xfs_filblks_t *len,
  4052. xfs_fileoff_t obno,
  4053. xfs_fileoff_t end,
  4054. int *n,
  4055. int flags)
  4056. {
  4057. xfs_bmbt_irec_t *mval = *map;
  4058. ASSERT((flags & XFS_BMAPI_ENTIRE) ||
  4059. ((mval->br_startoff + mval->br_blockcount) <= end));
  4060. ASSERT((flags & XFS_BMAPI_ENTIRE) || (mval->br_blockcount <= *len) ||
  4061. (mval->br_startoff < obno));
  4062. *bno = mval->br_startoff + mval->br_blockcount;
  4063. *len = end - *bno;
  4064. if (*n > 0 && mval->br_startoff == mval[-1].br_startoff) {
  4065. /* update previous map with new information */
  4066. ASSERT(mval->br_startblock == mval[-1].br_startblock);
  4067. ASSERT(mval->br_blockcount > mval[-1].br_blockcount);
  4068. ASSERT(mval->br_state == mval[-1].br_state);
  4069. mval[-1].br_blockcount = mval->br_blockcount;
  4070. mval[-1].br_state = mval->br_state;
  4071. } else if (*n > 0 && mval->br_startblock != DELAYSTARTBLOCK &&
  4072. mval[-1].br_startblock != DELAYSTARTBLOCK &&
  4073. mval[-1].br_startblock != HOLESTARTBLOCK &&
  4074. mval->br_startblock == mval[-1].br_startblock +
  4075. mval[-1].br_blockcount &&
  4076. ((flags & XFS_BMAPI_IGSTATE) ||
  4077. mval[-1].br_state == mval->br_state)) {
  4078. ASSERT(mval->br_startoff ==
  4079. mval[-1].br_startoff + mval[-1].br_blockcount);
  4080. mval[-1].br_blockcount += mval->br_blockcount;
  4081. } else if (*n > 0 &&
  4082. mval->br_startblock == DELAYSTARTBLOCK &&
  4083. mval[-1].br_startblock == DELAYSTARTBLOCK &&
  4084. mval->br_startoff ==
  4085. mval[-1].br_startoff + mval[-1].br_blockcount) {
  4086. mval[-1].br_blockcount += mval->br_blockcount;
  4087. mval[-1].br_state = mval->br_state;
  4088. } else if (!((*n == 0) &&
  4089. ((mval->br_startoff + mval->br_blockcount) <=
  4090. obno))) {
  4091. mval++;
  4092. (*n)++;
  4093. }
  4094. *map = mval;
  4095. }
  4096. /*
  4097. * Map file blocks to filesystem blocks without allocation.
  4098. */
  4099. int
  4100. xfs_bmapi_read(
  4101. struct xfs_inode *ip,
  4102. xfs_fileoff_t bno,
  4103. xfs_filblks_t len,
  4104. struct xfs_bmbt_irec *mval,
  4105. int *nmap,
  4106. int flags)
  4107. {
  4108. struct xfs_mount *mp = ip->i_mount;
  4109. struct xfs_ifork *ifp;
  4110. struct xfs_bmbt_irec got;
  4111. struct xfs_bmbt_irec prev;
  4112. xfs_fileoff_t obno;
  4113. xfs_fileoff_t end;
  4114. xfs_extnum_t lastx;
  4115. int error;
  4116. int eof;
  4117. int n = 0;
  4118. int whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
  4119. XFS_ATTR_FORK : XFS_DATA_FORK;
  4120. ASSERT(*nmap >= 1);
  4121. ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK|XFS_BMAPI_ENTIRE|
  4122. XFS_BMAPI_IGSTATE)));
  4123. if (unlikely(XFS_TEST_ERROR(
  4124. (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
  4125. XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
  4126. mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
  4127. XFS_ERROR_REPORT("xfs_bmapi_read", XFS_ERRLEVEL_LOW, mp);
  4128. return XFS_ERROR(EFSCORRUPTED);
  4129. }
  4130. if (XFS_FORCED_SHUTDOWN(mp))
  4131. return XFS_ERROR(EIO);
  4132. XFS_STATS_INC(xs_blk_mapr);
  4133. ifp = XFS_IFORK_PTR(ip, whichfork);
  4134. if (!(ifp->if_flags & XFS_IFEXTENTS)) {
  4135. error = xfs_iread_extents(NULL, ip, whichfork);
  4136. if (error)
  4137. return error;
  4138. }
  4139. xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got, &prev);
  4140. end = bno + len;
  4141. obno = bno;
  4142. while (bno < end && n < *nmap) {
  4143. /* Reading past eof, act as though there's a hole up to end. */
  4144. if (eof)
  4145. got.br_startoff = end;
  4146. if (got.br_startoff > bno) {
  4147. /* Reading in a hole. */
  4148. mval->br_startoff = bno;
  4149. mval->br_startblock = HOLESTARTBLOCK;
  4150. mval->br_blockcount =
  4151. XFS_FILBLKS_MIN(len, got.br_startoff - bno);
  4152. mval->br_state = XFS_EXT_NORM;
  4153. bno += mval->br_blockcount;
  4154. len -= mval->br_blockcount;
  4155. mval++;
  4156. n++;
  4157. continue;
  4158. }
  4159. /* set up the extent map to return. */
  4160. xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags);
  4161. xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
  4162. /* If we're done, stop now. */
  4163. if (bno >= end || n >= *nmap)
  4164. break;
  4165. /* Else go on to the next record. */
  4166. if (++lastx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t))
  4167. xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx), &got);
  4168. else
  4169. eof = 1;
  4170. }
  4171. *nmap = n;
  4172. return 0;
  4173. }
  4174. STATIC int
  4175. xfs_bmapi_reserve_delalloc(
  4176. struct xfs_inode *ip,
  4177. xfs_fileoff_t aoff,
  4178. xfs_filblks_t len,
  4179. struct xfs_bmbt_irec *got,
  4180. struct xfs_bmbt_irec *prev,
  4181. xfs_extnum_t *lastx,
  4182. int eof)
  4183. {
  4184. struct xfs_mount *mp = ip->i_mount;
  4185. struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
  4186. xfs_extlen_t alen;
  4187. xfs_extlen_t indlen;
  4188. char rt = XFS_IS_REALTIME_INODE(ip);
  4189. xfs_extlen_t extsz;
  4190. int error;
  4191. alen = XFS_FILBLKS_MIN(len, MAXEXTLEN);
  4192. if (!eof)
  4193. alen = XFS_FILBLKS_MIN(alen, got->br_startoff - aoff);
  4194. /* Figure out the extent size, adjust alen */
  4195. extsz = xfs_get_extsz_hint(ip);
  4196. if (extsz) {
  4197. /*
  4198. * Make sure we don't exceed a single extent length when we
  4199. * align the extent by reducing length we are going to
  4200. * allocate by the maximum amount extent size aligment may
  4201. * require.
  4202. */
  4203. alen = XFS_FILBLKS_MIN(len, MAXEXTLEN - (2 * extsz - 1));
  4204. error = xfs_bmap_extsize_align(mp, got, prev, extsz, rt, eof,
  4205. 1, 0, &aoff, &alen);
  4206. ASSERT(!error);
  4207. }
  4208. if (rt)
  4209. extsz = alen / mp->m_sb.sb_rextsize;
  4210. /*
  4211. * Make a transaction-less quota reservation for delayed allocation
  4212. * blocks. This number gets adjusted later. We return if we haven't
  4213. * allocated blocks already inside this loop.
  4214. */
  4215. error = xfs_trans_reserve_quota_nblks(NULL, ip, (long)alen, 0,
  4216. rt ? XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
  4217. if (error)
  4218. return error;
  4219. /*
  4220. * Split changing sb for alen and indlen since they could be coming
  4221. * from different places.
  4222. */
  4223. indlen = (xfs_extlen_t)xfs_bmap_worst_indlen(ip, alen);
  4224. ASSERT(indlen > 0);
  4225. if (rt) {
  4226. error = xfs_mod_incore_sb(mp, XFS_SBS_FREXTENTS,
  4227. -((int64_t)extsz), 0);
  4228. } else {
  4229. error = xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS,
  4230. -((int64_t)alen), 0);
  4231. }
  4232. if (error)
  4233. goto out_unreserve_quota;
  4234. error = xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS,
  4235. -((int64_t)indlen), 0);
  4236. if (error)
  4237. goto out_unreserve_blocks;
  4238. ip->i_delayed_blks += alen;
  4239. got->br_startoff = aoff;
  4240. got->br_startblock = nullstartblock(indlen);
  4241. got->br_blockcount = alen;
  4242. got->br_state = XFS_EXT_NORM;
  4243. xfs_bmap_add_extent_hole_delay(ip, lastx, got);
  4244. /*
  4245. * Update our extent pointer, given that xfs_bmap_add_extent_hole_delay
  4246. * might have merged it into one of the neighbouring ones.
  4247. */
  4248. xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *lastx), got);
  4249. ASSERT(got->br_startoff <= aoff);
  4250. ASSERT(got->br_startoff + got->br_blockcount >= aoff + alen);
  4251. ASSERT(isnullstartblock(got->br_startblock));
  4252. ASSERT(got->br_state == XFS_EXT_NORM);
  4253. return 0;
  4254. out_unreserve_blocks:
  4255. if (rt)
  4256. xfs_mod_incore_sb(mp, XFS_SBS_FREXTENTS, extsz, 0);
  4257. else
  4258. xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS, alen, 0);
  4259. out_unreserve_quota:
  4260. if (XFS_IS_QUOTA_ON(mp))
  4261. xfs_trans_unreserve_quota_nblks(NULL, ip, (long)alen, 0, rt ?
  4262. XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
  4263. return error;
  4264. }
  4265. /*
  4266. * Map file blocks to filesystem blocks, adding delayed allocations as needed.
  4267. */
  4268. int
  4269. xfs_bmapi_delay(
  4270. struct xfs_inode *ip, /* incore inode */
  4271. xfs_fileoff_t bno, /* starting file offs. mapped */
  4272. xfs_filblks_t len, /* length to map in file */
  4273. struct xfs_bmbt_irec *mval, /* output: map values */
  4274. int *nmap, /* i/o: mval size/count */
  4275. int flags) /* XFS_BMAPI_... */
  4276. {
  4277. struct xfs_mount *mp = ip->i_mount;
  4278. struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
  4279. struct xfs_bmbt_irec got; /* current file extent record */
  4280. struct xfs_bmbt_irec prev; /* previous file extent record */
  4281. xfs_fileoff_t obno; /* old block number (offset) */
  4282. xfs_fileoff_t end; /* end of mapped file region */
  4283. xfs_extnum_t lastx; /* last useful extent number */
  4284. int eof; /* we've hit the end of extents */
  4285. int n = 0; /* current extent index */
  4286. int error = 0;
  4287. ASSERT(*nmap >= 1);
  4288. ASSERT(*nmap <= XFS_BMAP_MAX_NMAP);
  4289. ASSERT(!(flags & ~XFS_BMAPI_ENTIRE));
  4290. if (unlikely(XFS_TEST_ERROR(
  4291. (XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_EXTENTS &&
  4292. XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_BTREE),
  4293. mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
  4294. XFS_ERROR_REPORT("xfs_bmapi_delay", XFS_ERRLEVEL_LOW, mp);
  4295. return XFS_ERROR(EFSCORRUPTED);
  4296. }
  4297. if (XFS_FORCED_SHUTDOWN(mp))
  4298. return XFS_ERROR(EIO);
  4299. XFS_STATS_INC(xs_blk_mapw);
  4300. if (!(ifp->if_flags & XFS_IFEXTENTS)) {
  4301. error = xfs_iread_extents(NULL, ip, XFS_DATA_FORK);
  4302. if (error)
  4303. return error;
  4304. }
  4305. xfs_bmap_search_extents(ip, bno, XFS_DATA_FORK, &eof, &lastx, &got, &prev);
  4306. end = bno + len;
  4307. obno = bno;
  4308. while (bno < end && n < *nmap) {
  4309. if (eof || got.br_startoff > bno) {
  4310. error = xfs_bmapi_reserve_delalloc(ip, bno, len, &got,
  4311. &prev, &lastx, eof);
  4312. if (error) {
  4313. if (n == 0) {
  4314. *nmap = 0;
  4315. return error;
  4316. }
  4317. break;
  4318. }
  4319. }
  4320. /* set up the extent map to return. */
  4321. xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags);
  4322. xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
  4323. /* If we're done, stop now. */
  4324. if (bno >= end || n >= *nmap)
  4325. break;
  4326. /* Else go on to the next record. */
  4327. prev = got;
  4328. if (++lastx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t))
  4329. xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx), &got);
  4330. else
  4331. eof = 1;
  4332. }
  4333. *nmap = n;
  4334. return 0;
  4335. }
  4336. STATIC int
  4337. __xfs_bmapi_allocate(
  4338. struct xfs_bmalloca *bma)
  4339. {
  4340. struct xfs_mount *mp = bma->ip->i_mount;
  4341. int whichfork = (bma->flags & XFS_BMAPI_ATTRFORK) ?
  4342. XFS_ATTR_FORK : XFS_DATA_FORK;
  4343. struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
  4344. int tmp_logflags = 0;
  4345. int error;
  4346. int rt;
  4347. ASSERT(bma->length > 0);
  4348. rt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(bma->ip);
  4349. /*
  4350. * For the wasdelay case, we could also just allocate the stuff asked
  4351. * for in this bmap call but that wouldn't be as good.
  4352. */
  4353. if (bma->wasdel) {
  4354. bma->length = (xfs_extlen_t)bma->got.br_blockcount;
  4355. bma->offset = bma->got.br_startoff;
  4356. if (bma->idx != NULLEXTNUM && bma->idx) {
  4357. xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx - 1),
  4358. &bma->prev);
  4359. }
  4360. } else {
  4361. bma->length = XFS_FILBLKS_MIN(bma->length, MAXEXTLEN);
  4362. if (!bma->eof)
  4363. bma->length = XFS_FILBLKS_MIN(bma->length,
  4364. bma->got.br_startoff - bma->offset);
  4365. }
  4366. /*
  4367. * Indicate if this is the first user data in the file, or just any
  4368. * user data.
  4369. */
  4370. if (!(bma->flags & XFS_BMAPI_METADATA)) {
  4371. bma->userdata = (bma->offset == 0) ?
  4372. XFS_ALLOC_INITIAL_USER_DATA : XFS_ALLOC_USERDATA;
  4373. }
  4374. bma->minlen = (bma->flags & XFS_BMAPI_CONTIG) ? bma->length : 1;
  4375. /*
  4376. * Only want to do the alignment at the eof if it is userdata and
  4377. * allocation length is larger than a stripe unit.
  4378. */
  4379. if (mp->m_dalign && bma->length >= mp->m_dalign &&
  4380. !(bma->flags & XFS_BMAPI_METADATA) && whichfork == XFS_DATA_FORK) {
  4381. error = xfs_bmap_isaeof(bma, whichfork);
  4382. if (error)
  4383. return error;
  4384. }
  4385. error = xfs_bmap_alloc(bma);
  4386. if (error)
  4387. return error;
  4388. if (bma->flist->xbf_low)
  4389. bma->minleft = 0;
  4390. if (bma->cur)
  4391. bma->cur->bc_private.b.firstblock = *bma->firstblock;
  4392. if (bma->blkno == NULLFSBLOCK)
  4393. return 0;
  4394. if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) {
  4395. bma->cur = xfs_bmbt_init_cursor(mp, bma->tp, bma->ip, whichfork);
  4396. bma->cur->bc_private.b.firstblock = *bma->firstblock;
  4397. bma->cur->bc_private.b.flist = bma->flist;
  4398. }
  4399. /*
  4400. * Bump the number of extents we've allocated
  4401. * in this call.
  4402. */
  4403. bma->nallocs++;
  4404. if (bma->cur)
  4405. bma->cur->bc_private.b.flags =
  4406. bma->wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
  4407. bma->got.br_startoff = bma->offset;
  4408. bma->got.br_startblock = bma->blkno;
  4409. bma->got.br_blockcount = bma->length;
  4410. bma->got.br_state = XFS_EXT_NORM;
  4411. /*
  4412. * A wasdelay extent has been initialized, so shouldn't be flagged
  4413. * as unwritten.
  4414. */
  4415. if (!bma->wasdel && (bma->flags & XFS_BMAPI_PREALLOC) &&
  4416. xfs_sb_version_hasextflgbit(&mp->m_sb))
  4417. bma->got.br_state = XFS_EXT_UNWRITTEN;
  4418. if (bma->wasdel)
  4419. error = xfs_bmap_add_extent_delay_real(bma);
  4420. else
  4421. error = xfs_bmap_add_extent_hole_real(bma, whichfork);
  4422. bma->logflags |= tmp_logflags;
  4423. if (error)
  4424. return error;
  4425. /*
  4426. * Update our extent pointer, given that xfs_bmap_add_extent_delay_real
  4427. * or xfs_bmap_add_extent_hole_real might have merged it into one of
  4428. * the neighbouring ones.
  4429. */
  4430. xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx), &bma->got);
  4431. ASSERT(bma->got.br_startoff <= bma->offset);
  4432. ASSERT(bma->got.br_startoff + bma->got.br_blockcount >=
  4433. bma->offset + bma->length);
  4434. ASSERT(bma->got.br_state == XFS_EXT_NORM ||
  4435. bma->got.br_state == XFS_EXT_UNWRITTEN);
  4436. return 0;
  4437. }
  4438. static void
  4439. xfs_bmapi_allocate_worker(
  4440. struct work_struct *work)
  4441. {
  4442. struct xfs_bmalloca *args = container_of(work,
  4443. struct xfs_bmalloca, work);
  4444. unsigned long pflags;
  4445. /* we are in a transaction context here */
  4446. current_set_flags_nested(&pflags, PF_FSTRANS);
  4447. args->result = __xfs_bmapi_allocate(args);
  4448. complete(args->done);
  4449. current_restore_flags_nested(&pflags, PF_FSTRANS);
  4450. }
  4451. /*
  4452. * Some allocation requests often come in with little stack to work on. Push
  4453. * them off to a worker thread so there is lots of stack to use. Otherwise just
  4454. * call directly to avoid the context switch overhead here.
  4455. */
  4456. int
  4457. xfs_bmapi_allocate(
  4458. struct xfs_bmalloca *args)
  4459. {
  4460. DECLARE_COMPLETION_ONSTACK(done);
  4461. if (!args->stack_switch)
  4462. return __xfs_bmapi_allocate(args);
  4463. args->done = &done;
  4464. INIT_WORK_ONSTACK(&args->work, xfs_bmapi_allocate_worker);
  4465. queue_work(xfs_alloc_wq, &args->work);
  4466. wait_for_completion(&done);
  4467. return args->result;
  4468. }
  4469. STATIC int
  4470. xfs_bmapi_convert_unwritten(
  4471. struct xfs_bmalloca *bma,
  4472. struct xfs_bmbt_irec *mval,
  4473. xfs_filblks_t len,
  4474. int flags)
  4475. {
  4476. int whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
  4477. XFS_ATTR_FORK : XFS_DATA_FORK;
  4478. struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
  4479. int tmp_logflags = 0;
  4480. int error;
  4481. /* check if we need to do unwritten->real conversion */
  4482. if (mval->br_state == XFS_EXT_UNWRITTEN &&
  4483. (flags & XFS_BMAPI_PREALLOC))
  4484. return 0;
  4485. /* check if we need to do real->unwritten conversion */
  4486. if (mval->br_state == XFS_EXT_NORM &&
  4487. (flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) !=
  4488. (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT))
  4489. return 0;
  4490. /*
  4491. * Modify (by adding) the state flag, if writing.
  4492. */
  4493. ASSERT(mval->br_blockcount <= len);
  4494. if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) {
  4495. bma->cur = xfs_bmbt_init_cursor(bma->ip->i_mount, bma->tp,
  4496. bma->ip, whichfork);
  4497. bma->cur->bc_private.b.firstblock = *bma->firstblock;
  4498. bma->cur->bc_private.b.flist = bma->flist;
  4499. }
  4500. mval->br_state = (mval->br_state == XFS_EXT_UNWRITTEN)
  4501. ? XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
  4502. error = xfs_bmap_add_extent_unwritten_real(bma->tp, bma->ip, &bma->idx,
  4503. &bma->cur, mval, bma->firstblock, bma->flist,
  4504. &tmp_logflags);
  4505. bma->logflags |= tmp_logflags;
  4506. if (error)
  4507. return error;
  4508. /*
  4509. * Update our extent pointer, given that
  4510. * xfs_bmap_add_extent_unwritten_real might have merged it into one
  4511. * of the neighbouring ones.
  4512. */
  4513. xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx), &bma->got);
  4514. /*
  4515. * We may have combined previously unwritten space with written space,
  4516. * so generate another request.
  4517. */
  4518. if (mval->br_blockcount < len)
  4519. return EAGAIN;
  4520. return 0;
  4521. }
  4522. /*
  4523. * Map file blocks to filesystem blocks, and allocate blocks or convert the
  4524. * extent state if necessary. Details behaviour is controlled by the flags
  4525. * parameter. Only allocates blocks from a single allocation group, to avoid
  4526. * locking problems.
  4527. *
  4528. * The returned value in "firstblock" from the first call in a transaction
  4529. * must be remembered and presented to subsequent calls in "firstblock".
  4530. * An upper bound for the number of blocks to be allocated is supplied to
  4531. * the first call in "total"; if no allocation group has that many free
  4532. * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
  4533. */
  4534. int
  4535. xfs_bmapi_write(
  4536. struct xfs_trans *tp, /* transaction pointer */
  4537. struct xfs_inode *ip, /* incore inode */
  4538. xfs_fileoff_t bno, /* starting file offs. mapped */
  4539. xfs_filblks_t len, /* length to map in file */
  4540. int flags, /* XFS_BMAPI_... */
  4541. xfs_fsblock_t *firstblock, /* first allocated block
  4542. controls a.g. for allocs */
  4543. xfs_extlen_t total, /* total blocks needed */
  4544. struct xfs_bmbt_irec *mval, /* output: map values */
  4545. int *nmap, /* i/o: mval size/count */
  4546. struct xfs_bmap_free *flist) /* i/o: list extents to free */
  4547. {
  4548. struct xfs_mount *mp = ip->i_mount;
  4549. struct xfs_ifork *ifp;
  4550. struct xfs_bmalloca bma = { 0 }; /* args for xfs_bmap_alloc */
  4551. xfs_fileoff_t end; /* end of mapped file region */
  4552. int eof; /* after the end of extents */
  4553. int error; /* error return */
  4554. int n; /* current extent index */
  4555. xfs_fileoff_t obno; /* old block number (offset) */
  4556. int whichfork; /* data or attr fork */
  4557. char inhole; /* current location is hole in file */
  4558. char wasdelay; /* old extent was delayed */
  4559. #ifdef DEBUG
  4560. xfs_fileoff_t orig_bno; /* original block number value */
  4561. int orig_flags; /* original flags arg value */
  4562. xfs_filblks_t orig_len; /* original value of len arg */
  4563. struct xfs_bmbt_irec *orig_mval; /* original value of mval */
  4564. int orig_nmap; /* original value of *nmap */
  4565. orig_bno = bno;
  4566. orig_len = len;
  4567. orig_flags = flags;
  4568. orig_mval = mval;
  4569. orig_nmap = *nmap;
  4570. #endif
  4571. ASSERT(*nmap >= 1);
  4572. ASSERT(*nmap <= XFS_BMAP_MAX_NMAP);
  4573. ASSERT(!(flags & XFS_BMAPI_IGSTATE));
  4574. ASSERT(tp != NULL);
  4575. ASSERT(len > 0);
  4576. whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
  4577. XFS_ATTR_FORK : XFS_DATA_FORK;
  4578. if (unlikely(XFS_TEST_ERROR(
  4579. (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
  4580. XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
  4581. XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL),
  4582. mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
  4583. XFS_ERROR_REPORT("xfs_bmapi_write", XFS_ERRLEVEL_LOW, mp);
  4584. return XFS_ERROR(EFSCORRUPTED);
  4585. }
  4586. if (XFS_FORCED_SHUTDOWN(mp))
  4587. return XFS_ERROR(EIO);
  4588. ifp = XFS_IFORK_PTR(ip, whichfork);
  4589. XFS_STATS_INC(xs_blk_mapw);
  4590. if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
  4591. /*
  4592. * XXX (dgc): This assumes we are only called for inodes that
  4593. * contain content neutral data in local format. Anything that
  4594. * contains caller-specific data in local format that needs
  4595. * transformation to move to a block format needs to do the
  4596. * conversion to extent format itself.
  4597. *
  4598. * Directory data forks and attribute forks handle this
  4599. * themselves, but with the addition of metadata verifiers every
  4600. * data fork in local format now contains caller specific data
  4601. * and as such conversion through this function is likely to be
  4602. * broken.
  4603. *
  4604. * The only likely user of this branch is for remote symlinks,
  4605. * but we cannot overwrite the data fork contents of the symlink
  4606. * (EEXIST occurs higher up the stack) and so it will never go
  4607. * from local format to extent format here. Hence I don't think
  4608. * this branch is ever executed intentionally and we should
  4609. * consider removing it and asserting that xfs_bmapi_write()
  4610. * cannot be called directly on local format forks. i.e. callers
  4611. * are completely responsible for local to extent format
  4612. * conversion, not xfs_bmapi_write().
  4613. */
  4614. error = xfs_bmap_local_to_extents(tp, ip, firstblock, total,
  4615. &bma.logflags, whichfork,
  4616. xfs_bmap_local_to_extents_init_fn);
  4617. if (error)
  4618. goto error0;
  4619. }
  4620. if (*firstblock == NULLFSBLOCK) {
  4621. if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE)
  4622. bma.minleft = be16_to_cpu(ifp->if_broot->bb_level) + 1;
  4623. else
  4624. bma.minleft = 1;
  4625. } else {
  4626. bma.minleft = 0;
  4627. }
  4628. if (!(ifp->if_flags & XFS_IFEXTENTS)) {
  4629. error = xfs_iread_extents(tp, ip, whichfork);
  4630. if (error)
  4631. goto error0;
  4632. }
  4633. xfs_bmap_search_extents(ip, bno, whichfork, &eof, &bma.idx, &bma.got,
  4634. &bma.prev);
  4635. n = 0;
  4636. end = bno + len;
  4637. obno = bno;
  4638. bma.tp = tp;
  4639. bma.ip = ip;
  4640. bma.total = total;
  4641. bma.userdata = 0;
  4642. bma.flist = flist;
  4643. bma.firstblock = firstblock;
  4644. if (flags & XFS_BMAPI_STACK_SWITCH)
  4645. bma.stack_switch = 1;
  4646. while (bno < end && n < *nmap) {
  4647. inhole = eof || bma.got.br_startoff > bno;
  4648. wasdelay = !inhole && isnullstartblock(bma.got.br_startblock);
  4649. /*
  4650. * First, deal with the hole before the allocated space
  4651. * that we found, if any.
  4652. */
  4653. if (inhole || wasdelay) {
  4654. bma.eof = eof;
  4655. bma.conv = !!(flags & XFS_BMAPI_CONVERT);
  4656. bma.wasdel = wasdelay;
  4657. bma.offset = bno;
  4658. bma.flags = flags;
  4659. /*
  4660. * There's a 32/64 bit type mismatch between the
  4661. * allocation length request (which can be 64 bits in
  4662. * length) and the bma length request, which is
  4663. * xfs_extlen_t and therefore 32 bits. Hence we have to
  4664. * check for 32-bit overflows and handle them here.
  4665. */
  4666. if (len > (xfs_filblks_t)MAXEXTLEN)
  4667. bma.length = MAXEXTLEN;
  4668. else
  4669. bma.length = len;
  4670. ASSERT(len > 0);
  4671. ASSERT(bma.length > 0);
  4672. error = xfs_bmapi_allocate(&bma);
  4673. if (error)
  4674. goto error0;
  4675. if (bma.blkno == NULLFSBLOCK)
  4676. break;
  4677. }
  4678. /* Deal with the allocated space we found. */
  4679. xfs_bmapi_trim_map(mval, &bma.got, &bno, len, obno,
  4680. end, n, flags);
  4681. /* Execute unwritten extent conversion if necessary */
  4682. error = xfs_bmapi_convert_unwritten(&bma, mval, len, flags);
  4683. if (error == EAGAIN)
  4684. continue;
  4685. if (error)
  4686. goto error0;
  4687. /* update the extent map to return */
  4688. xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
  4689. /*
  4690. * If we're done, stop now. Stop when we've allocated
  4691. * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
  4692. * the transaction may get too big.
  4693. */
  4694. if (bno >= end || n >= *nmap || bma.nallocs >= *nmap)
  4695. break;
  4696. /* Else go on to the next record. */
  4697. bma.prev = bma.got;
  4698. if (++bma.idx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t)) {
  4699. xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma.idx),
  4700. &bma.got);
  4701. } else
  4702. eof = 1;
  4703. }
  4704. *nmap = n;
  4705. /*
  4706. * Transform from btree to extents, give it cur.
  4707. */
  4708. if (xfs_bmap_wants_extents(ip, whichfork)) {
  4709. int tmp_logflags = 0;
  4710. ASSERT(bma.cur);
  4711. error = xfs_bmap_btree_to_extents(tp, ip, bma.cur,
  4712. &tmp_logflags, whichfork);
  4713. bma.logflags |= tmp_logflags;
  4714. if (error)
  4715. goto error0;
  4716. }
  4717. ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE ||
  4718. XFS_IFORK_NEXTENTS(ip, whichfork) >
  4719. XFS_IFORK_MAXEXT(ip, whichfork));
  4720. error = 0;
  4721. error0:
  4722. /*
  4723. * Log everything. Do this after conversion, there's no point in
  4724. * logging the extent records if we've converted to btree format.
  4725. */
  4726. if ((bma.logflags & xfs_ilog_fext(whichfork)) &&
  4727. XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
  4728. bma.logflags &= ~xfs_ilog_fext(whichfork);
  4729. else if ((bma.logflags & xfs_ilog_fbroot(whichfork)) &&
  4730. XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
  4731. bma.logflags &= ~xfs_ilog_fbroot(whichfork);
  4732. /*
  4733. * Log whatever the flags say, even if error. Otherwise we might miss
  4734. * detecting a case where the data is changed, there's an error,
  4735. * and it's not logged so we don't shutdown when we should.
  4736. */
  4737. if (bma.logflags)
  4738. xfs_trans_log_inode(tp, ip, bma.logflags);
  4739. if (bma.cur) {
  4740. if (!error) {
  4741. ASSERT(*firstblock == NULLFSBLOCK ||
  4742. XFS_FSB_TO_AGNO(mp, *firstblock) ==
  4743. XFS_FSB_TO_AGNO(mp,
  4744. bma.cur->bc_private.b.firstblock) ||
  4745. (flist->xbf_low &&
  4746. XFS_FSB_TO_AGNO(mp, *firstblock) <
  4747. XFS_FSB_TO_AGNO(mp,
  4748. bma.cur->bc_private.b.firstblock)));
  4749. *firstblock = bma.cur->bc_private.b.firstblock;
  4750. }
  4751. xfs_btree_del_cursor(bma.cur,
  4752. error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
  4753. }
  4754. if (!error)
  4755. xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval,
  4756. orig_nmap, *nmap);
  4757. return error;
  4758. }
  4759. /*
  4760. * Unmap (remove) blocks from a file.
  4761. * If nexts is nonzero then the number of extents to remove is limited to
  4762. * that value. If not all extents in the block range can be removed then
  4763. * *done is set.
  4764. */
  4765. int /* error */
  4766. xfs_bunmapi(
  4767. xfs_trans_t *tp, /* transaction pointer */
  4768. struct xfs_inode *ip, /* incore inode */
  4769. xfs_fileoff_t bno, /* starting offset to unmap */
  4770. xfs_filblks_t len, /* length to unmap in file */
  4771. int flags, /* misc flags */
  4772. xfs_extnum_t nexts, /* number of extents max */
  4773. xfs_fsblock_t *firstblock, /* first allocated block
  4774. controls a.g. for allocs */
  4775. xfs_bmap_free_t *flist, /* i/o: list extents to free */
  4776. int *done) /* set if not done yet */
  4777. {
  4778. xfs_btree_cur_t *cur; /* bmap btree cursor */
  4779. xfs_bmbt_irec_t del; /* extent being deleted */
  4780. int eof; /* is deleting at eof */
  4781. xfs_bmbt_rec_host_t *ep; /* extent record pointer */
  4782. int error; /* error return value */
  4783. xfs_extnum_t extno; /* extent number in list */
  4784. xfs_bmbt_irec_t got; /* current extent record */
  4785. xfs_ifork_t *ifp; /* inode fork pointer */
  4786. int isrt; /* freeing in rt area */
  4787. xfs_extnum_t lastx; /* last extent index used */
  4788. int logflags; /* transaction logging flags */
  4789. xfs_extlen_t mod; /* rt extent offset */
  4790. xfs_mount_t *mp; /* mount structure */
  4791. xfs_extnum_t nextents; /* number of file extents */
  4792. xfs_bmbt_irec_t prev; /* previous extent record */
  4793. xfs_fileoff_t start; /* first file offset deleted */
  4794. int tmp_logflags; /* partial logging flags */
  4795. int wasdel; /* was a delayed alloc extent */
  4796. int whichfork; /* data or attribute fork */
  4797. xfs_fsblock_t sum;
  4798. trace_xfs_bunmap(ip, bno, len, flags, _RET_IP_);
  4799. whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
  4800. XFS_ATTR_FORK : XFS_DATA_FORK;
  4801. ifp = XFS_IFORK_PTR(ip, whichfork);
  4802. if (unlikely(
  4803. XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
  4804. XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
  4805. XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW,
  4806. ip->i_mount);
  4807. return XFS_ERROR(EFSCORRUPTED);
  4808. }
  4809. mp = ip->i_mount;
  4810. if (XFS_FORCED_SHUTDOWN(mp))
  4811. return XFS_ERROR(EIO);
  4812. ASSERT(len > 0);
  4813. ASSERT(nexts >= 0);
  4814. if (!(ifp->if_flags & XFS_IFEXTENTS) &&
  4815. (error = xfs_iread_extents(tp, ip, whichfork)))
  4816. return error;
  4817. nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  4818. if (nextents == 0) {
  4819. *done = 1;
  4820. return 0;
  4821. }
  4822. XFS_STATS_INC(xs_blk_unmap);
  4823. isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
  4824. start = bno;
  4825. bno = start + len - 1;
  4826. ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
  4827. &prev);
  4828. /*
  4829. * Check to see if the given block number is past the end of the
  4830. * file, back up to the last block if so...
  4831. */
  4832. if (eof) {
  4833. ep = xfs_iext_get_ext(ifp, --lastx);
  4834. xfs_bmbt_get_all(ep, &got);
  4835. bno = got.br_startoff + got.br_blockcount - 1;
  4836. }
  4837. logflags = 0;
  4838. if (ifp->if_flags & XFS_IFBROOT) {
  4839. ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
  4840. cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
  4841. cur->bc_private.b.firstblock = *firstblock;
  4842. cur->bc_private.b.flist = flist;
  4843. cur->bc_private.b.flags = 0;
  4844. } else
  4845. cur = NULL;
  4846. if (isrt) {
  4847. /*
  4848. * Synchronize by locking the bitmap inode.
  4849. */
  4850. xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL);
  4851. xfs_trans_ijoin(tp, mp->m_rbmip, XFS_ILOCK_EXCL);
  4852. }
  4853. extno = 0;
  4854. while (bno != (xfs_fileoff_t)-1 && bno >= start && lastx >= 0 &&
  4855. (nexts == 0 || extno < nexts)) {
  4856. /*
  4857. * Is the found extent after a hole in which bno lives?
  4858. * Just back up to the previous extent, if so.
  4859. */
  4860. if (got.br_startoff > bno) {
  4861. if (--lastx < 0)
  4862. break;
  4863. ep = xfs_iext_get_ext(ifp, lastx);
  4864. xfs_bmbt_get_all(ep, &got);
  4865. }
  4866. /*
  4867. * Is the last block of this extent before the range
  4868. * we're supposed to delete? If so, we're done.
  4869. */
  4870. bno = XFS_FILEOFF_MIN(bno,
  4871. got.br_startoff + got.br_blockcount - 1);
  4872. if (bno < start)
  4873. break;
  4874. /*
  4875. * Then deal with the (possibly delayed) allocated space
  4876. * we found.
  4877. */
  4878. ASSERT(ep != NULL);
  4879. del = got;
  4880. wasdel = isnullstartblock(del.br_startblock);
  4881. if (got.br_startoff < start) {
  4882. del.br_startoff = start;
  4883. del.br_blockcount -= start - got.br_startoff;
  4884. if (!wasdel)
  4885. del.br_startblock += start - got.br_startoff;
  4886. }
  4887. if (del.br_startoff + del.br_blockcount > bno + 1)
  4888. del.br_blockcount = bno + 1 - del.br_startoff;
  4889. sum = del.br_startblock + del.br_blockcount;
  4890. if (isrt &&
  4891. (mod = do_mod(sum, mp->m_sb.sb_rextsize))) {
  4892. /*
  4893. * Realtime extent not lined up at the end.
  4894. * The extent could have been split into written
  4895. * and unwritten pieces, or we could just be
  4896. * unmapping part of it. But we can't really
  4897. * get rid of part of a realtime extent.
  4898. */
  4899. if (del.br_state == XFS_EXT_UNWRITTEN ||
  4900. !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
  4901. /*
  4902. * This piece is unwritten, or we're not
  4903. * using unwritten extents. Skip over it.
  4904. */
  4905. ASSERT(bno >= mod);
  4906. bno -= mod > del.br_blockcount ?
  4907. del.br_blockcount : mod;
  4908. if (bno < got.br_startoff) {
  4909. if (--lastx >= 0)
  4910. xfs_bmbt_get_all(xfs_iext_get_ext(
  4911. ifp, lastx), &got);
  4912. }
  4913. continue;
  4914. }
  4915. /*
  4916. * It's written, turn it unwritten.
  4917. * This is better than zeroing it.
  4918. */
  4919. ASSERT(del.br_state == XFS_EXT_NORM);
  4920. ASSERT(xfs_trans_get_block_res(tp) > 0);
  4921. /*
  4922. * If this spans a realtime extent boundary,
  4923. * chop it back to the start of the one we end at.
  4924. */
  4925. if (del.br_blockcount > mod) {
  4926. del.br_startoff += del.br_blockcount - mod;
  4927. del.br_startblock += del.br_blockcount - mod;
  4928. del.br_blockcount = mod;
  4929. }
  4930. del.br_state = XFS_EXT_UNWRITTEN;
  4931. error = xfs_bmap_add_extent_unwritten_real(tp, ip,
  4932. &lastx, &cur, &del, firstblock, flist,
  4933. &logflags);
  4934. if (error)
  4935. goto error0;
  4936. goto nodelete;
  4937. }
  4938. if (isrt && (mod = do_mod(del.br_startblock, mp->m_sb.sb_rextsize))) {
  4939. /*
  4940. * Realtime extent is lined up at the end but not
  4941. * at the front. We'll get rid of full extents if
  4942. * we can.
  4943. */
  4944. mod = mp->m_sb.sb_rextsize - mod;
  4945. if (del.br_blockcount > mod) {
  4946. del.br_blockcount -= mod;
  4947. del.br_startoff += mod;
  4948. del.br_startblock += mod;
  4949. } else if ((del.br_startoff == start &&
  4950. (del.br_state == XFS_EXT_UNWRITTEN ||
  4951. xfs_trans_get_block_res(tp) == 0)) ||
  4952. !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
  4953. /*
  4954. * Can't make it unwritten. There isn't
  4955. * a full extent here so just skip it.
  4956. */
  4957. ASSERT(bno >= del.br_blockcount);
  4958. bno -= del.br_blockcount;
  4959. if (got.br_startoff > bno) {
  4960. if (--lastx >= 0) {
  4961. ep = xfs_iext_get_ext(ifp,
  4962. lastx);
  4963. xfs_bmbt_get_all(ep, &got);
  4964. }
  4965. }
  4966. continue;
  4967. } else if (del.br_state == XFS_EXT_UNWRITTEN) {
  4968. /*
  4969. * This one is already unwritten.
  4970. * It must have a written left neighbor.
  4971. * Unwrite the killed part of that one and
  4972. * try again.
  4973. */
  4974. ASSERT(lastx > 0);
  4975. xfs_bmbt_get_all(xfs_iext_get_ext(ifp,
  4976. lastx - 1), &prev);
  4977. ASSERT(prev.br_state == XFS_EXT_NORM);
  4978. ASSERT(!isnullstartblock(prev.br_startblock));
  4979. ASSERT(del.br_startblock ==
  4980. prev.br_startblock + prev.br_blockcount);
  4981. if (prev.br_startoff < start) {
  4982. mod = start - prev.br_startoff;
  4983. prev.br_blockcount -= mod;
  4984. prev.br_startblock += mod;
  4985. prev.br_startoff = start;
  4986. }
  4987. prev.br_state = XFS_EXT_UNWRITTEN;
  4988. lastx--;
  4989. error = xfs_bmap_add_extent_unwritten_real(tp,
  4990. ip, &lastx, &cur, &prev,
  4991. firstblock, flist, &logflags);
  4992. if (error)
  4993. goto error0;
  4994. goto nodelete;
  4995. } else {
  4996. ASSERT(del.br_state == XFS_EXT_NORM);
  4997. del.br_state = XFS_EXT_UNWRITTEN;
  4998. error = xfs_bmap_add_extent_unwritten_real(tp,
  4999. ip, &lastx, &cur, &del,
  5000. firstblock, flist, &logflags);
  5001. if (error)
  5002. goto error0;
  5003. goto nodelete;
  5004. }
  5005. }
  5006. if (wasdel) {
  5007. ASSERT(startblockval(del.br_startblock) > 0);
  5008. /* Update realtime/data freespace, unreserve quota */
  5009. if (isrt) {
  5010. xfs_filblks_t rtexts;
  5011. rtexts = XFS_FSB_TO_B(mp, del.br_blockcount);
  5012. do_div(rtexts, mp->m_sb.sb_rextsize);
  5013. xfs_mod_incore_sb(mp, XFS_SBS_FREXTENTS,
  5014. (int64_t)rtexts, 0);
  5015. (void)xfs_trans_reserve_quota_nblks(NULL,
  5016. ip, -((long)del.br_blockcount), 0,
  5017. XFS_QMOPT_RES_RTBLKS);
  5018. } else {
  5019. xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS,
  5020. (int64_t)del.br_blockcount, 0);
  5021. (void)xfs_trans_reserve_quota_nblks(NULL,
  5022. ip, -((long)del.br_blockcount), 0,
  5023. XFS_QMOPT_RES_REGBLKS);
  5024. }
  5025. ip->i_delayed_blks -= del.br_blockcount;
  5026. if (cur)
  5027. cur->bc_private.b.flags |=
  5028. XFS_BTCUR_BPRV_WASDEL;
  5029. } else if (cur)
  5030. cur->bc_private.b.flags &= ~XFS_BTCUR_BPRV_WASDEL;
  5031. /*
  5032. * If it's the case where the directory code is running
  5033. * with no block reservation, and the deleted block is in
  5034. * the middle of its extent, and the resulting insert
  5035. * of an extent would cause transformation to btree format,
  5036. * then reject it. The calling code will then swap
  5037. * blocks around instead.
  5038. * We have to do this now, rather than waiting for the
  5039. * conversion to btree format, since the transaction
  5040. * will be dirty.
  5041. */
  5042. if (!wasdel && xfs_trans_get_block_res(tp) == 0 &&
  5043. XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
  5044. XFS_IFORK_NEXTENTS(ip, whichfork) >= /* Note the >= */
  5045. XFS_IFORK_MAXEXT(ip, whichfork) &&
  5046. del.br_startoff > got.br_startoff &&
  5047. del.br_startoff + del.br_blockcount <
  5048. got.br_startoff + got.br_blockcount) {
  5049. error = XFS_ERROR(ENOSPC);
  5050. goto error0;
  5051. }
  5052. error = xfs_bmap_del_extent(ip, tp, &lastx, flist, cur, &del,
  5053. &tmp_logflags, whichfork);
  5054. logflags |= tmp_logflags;
  5055. if (error)
  5056. goto error0;
  5057. bno = del.br_startoff - 1;
  5058. nodelete:
  5059. /*
  5060. * If not done go on to the next (previous) record.
  5061. */
  5062. if (bno != (xfs_fileoff_t)-1 && bno >= start) {
  5063. if (lastx >= 0) {
  5064. ep = xfs_iext_get_ext(ifp, lastx);
  5065. if (xfs_bmbt_get_startoff(ep) > bno) {
  5066. if (--lastx >= 0)
  5067. ep = xfs_iext_get_ext(ifp,
  5068. lastx);
  5069. }
  5070. xfs_bmbt_get_all(ep, &got);
  5071. }
  5072. extno++;
  5073. }
  5074. }
  5075. *done = bno == (xfs_fileoff_t)-1 || bno < start || lastx < 0;
  5076. /*
  5077. * Convert to a btree if necessary.
  5078. */
  5079. if (xfs_bmap_needs_btree(ip, whichfork)) {
  5080. ASSERT(cur == NULL);
  5081. error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist,
  5082. &cur, 0, &tmp_logflags, whichfork);
  5083. logflags |= tmp_logflags;
  5084. if (error)
  5085. goto error0;
  5086. }
  5087. /*
  5088. * transform from btree to extents, give it cur
  5089. */
  5090. else if (xfs_bmap_wants_extents(ip, whichfork)) {
  5091. ASSERT(cur != NULL);
  5092. error = xfs_bmap_btree_to_extents(tp, ip, cur, &tmp_logflags,
  5093. whichfork);
  5094. logflags |= tmp_logflags;
  5095. if (error)
  5096. goto error0;
  5097. }
  5098. /*
  5099. * transform from extents to local?
  5100. */
  5101. error = 0;
  5102. error0:
  5103. /*
  5104. * Log everything. Do this after conversion, there's no point in
  5105. * logging the extent records if we've converted to btree format.
  5106. */
  5107. if ((logflags & xfs_ilog_fext(whichfork)) &&
  5108. XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
  5109. logflags &= ~xfs_ilog_fext(whichfork);
  5110. else if ((logflags & xfs_ilog_fbroot(whichfork)) &&
  5111. XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
  5112. logflags &= ~xfs_ilog_fbroot(whichfork);
  5113. /*
  5114. * Log inode even in the error case, if the transaction
  5115. * is dirty we'll need to shut down the filesystem.
  5116. */
  5117. if (logflags)
  5118. xfs_trans_log_inode(tp, ip, logflags);
  5119. if (cur) {
  5120. if (!error) {
  5121. *firstblock = cur->bc_private.b.firstblock;
  5122. cur->bc_private.b.allocated = 0;
  5123. }
  5124. xfs_btree_del_cursor(cur,
  5125. error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
  5126. }
  5127. return error;
  5128. }
  5129. /*
  5130. * returns 1 for success, 0 if we failed to map the extent.
  5131. */
  5132. STATIC int
  5133. xfs_getbmapx_fix_eof_hole(
  5134. xfs_inode_t *ip, /* xfs incore inode pointer */
  5135. struct getbmapx *out, /* output structure */
  5136. int prealloced, /* this is a file with
  5137. * preallocated data space */
  5138. __int64_t end, /* last block requested */
  5139. xfs_fsblock_t startblock)
  5140. {
  5141. __int64_t fixlen;
  5142. xfs_mount_t *mp; /* file system mount point */
  5143. xfs_ifork_t *ifp; /* inode fork pointer */
  5144. xfs_extnum_t lastx; /* last extent pointer */
  5145. xfs_fileoff_t fileblock;
  5146. if (startblock == HOLESTARTBLOCK) {
  5147. mp = ip->i_mount;
  5148. out->bmv_block = -1;
  5149. fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, XFS_ISIZE(ip)));
  5150. fixlen -= out->bmv_offset;
  5151. if (prealloced && out->bmv_offset + out->bmv_length == end) {
  5152. /* Came to hole at EOF. Trim it. */
  5153. if (fixlen <= 0)
  5154. return 0;
  5155. out->bmv_length = fixlen;
  5156. }
  5157. } else {
  5158. if (startblock == DELAYSTARTBLOCK)
  5159. out->bmv_block = -2;
  5160. else
  5161. out->bmv_block = xfs_fsb_to_db(ip, startblock);
  5162. fileblock = XFS_BB_TO_FSB(ip->i_mount, out->bmv_offset);
  5163. ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
  5164. if (xfs_iext_bno_to_ext(ifp, fileblock, &lastx) &&
  5165. (lastx == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))-1))
  5166. out->bmv_oflags |= BMV_OF_LAST;
  5167. }
  5168. return 1;
  5169. }
  5170. /*
  5171. * Get inode's extents as described in bmv, and format for output.
  5172. * Calls formatter to fill the user's buffer until all extents
  5173. * are mapped, until the passed-in bmv->bmv_count slots have
  5174. * been filled, or until the formatter short-circuits the loop,
  5175. * if it is tracking filled-in extents on its own.
  5176. */
  5177. int /* error code */
  5178. xfs_getbmap(
  5179. xfs_inode_t *ip,
  5180. struct getbmapx *bmv, /* user bmap structure */
  5181. xfs_bmap_format_t formatter, /* format to user */
  5182. void *arg) /* formatter arg */
  5183. {
  5184. __int64_t bmvend; /* last block requested */
  5185. int error = 0; /* return value */
  5186. __int64_t fixlen; /* length for -1 case */
  5187. int i; /* extent number */
  5188. int lock; /* lock state */
  5189. xfs_bmbt_irec_t *map; /* buffer for user's data */
  5190. xfs_mount_t *mp; /* file system mount point */
  5191. int nex; /* # of user extents can do */
  5192. int nexleft; /* # of user extents left */
  5193. int subnex; /* # of bmapi's can do */
  5194. int nmap; /* number of map entries */
  5195. struct getbmapx *out; /* output structure */
  5196. int whichfork; /* data or attr fork */
  5197. int prealloced; /* this is a file with
  5198. * preallocated data space */
  5199. int iflags; /* interface flags */
  5200. int bmapi_flags; /* flags for xfs_bmapi */
  5201. int cur_ext = 0;
  5202. mp = ip->i_mount;
  5203. iflags = bmv->bmv_iflags;
  5204. whichfork = iflags & BMV_IF_ATTRFORK ? XFS_ATTR_FORK : XFS_DATA_FORK;
  5205. if (whichfork == XFS_ATTR_FORK) {
  5206. if (XFS_IFORK_Q(ip)) {
  5207. if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS &&
  5208. ip->i_d.di_aformat != XFS_DINODE_FMT_BTREE &&
  5209. ip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL)
  5210. return XFS_ERROR(EINVAL);
  5211. } else if (unlikely(
  5212. ip->i_d.di_aformat != 0 &&
  5213. ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS)) {
  5214. XFS_ERROR_REPORT("xfs_getbmap", XFS_ERRLEVEL_LOW,
  5215. ip->i_mount);
  5216. return XFS_ERROR(EFSCORRUPTED);
  5217. }
  5218. prealloced = 0;
  5219. fixlen = 1LL << 32;
  5220. } else {
  5221. if (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS &&
  5222. ip->i_d.di_format != XFS_DINODE_FMT_BTREE &&
  5223. ip->i_d.di_format != XFS_DINODE_FMT_LOCAL)
  5224. return XFS_ERROR(EINVAL);
  5225. if (xfs_get_extsz_hint(ip) ||
  5226. ip->i_d.di_flags & (XFS_DIFLAG_PREALLOC|XFS_DIFLAG_APPEND)){
  5227. prealloced = 1;
  5228. fixlen = mp->m_super->s_maxbytes;
  5229. } else {
  5230. prealloced = 0;
  5231. fixlen = XFS_ISIZE(ip);
  5232. }
  5233. }
  5234. if (bmv->bmv_length == -1) {
  5235. fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, fixlen));
  5236. bmv->bmv_length =
  5237. max_t(__int64_t, fixlen - bmv->bmv_offset, 0);
  5238. } else if (bmv->bmv_length == 0) {
  5239. bmv->bmv_entries = 0;
  5240. return 0;
  5241. } else if (bmv->bmv_length < 0) {
  5242. return XFS_ERROR(EINVAL);
  5243. }
  5244. nex = bmv->bmv_count - 1;
  5245. if (nex <= 0)
  5246. return XFS_ERROR(EINVAL);
  5247. bmvend = bmv->bmv_offset + bmv->bmv_length;
  5248. if (bmv->bmv_count > ULONG_MAX / sizeof(struct getbmapx))
  5249. return XFS_ERROR(ENOMEM);
  5250. out = kmem_zalloc(bmv->bmv_count * sizeof(struct getbmapx), KM_MAYFAIL);
  5251. if (!out) {
  5252. out = kmem_zalloc_large(bmv->bmv_count *
  5253. sizeof(struct getbmapx));
  5254. if (!out)
  5255. return XFS_ERROR(ENOMEM);
  5256. }
  5257. xfs_ilock(ip, XFS_IOLOCK_SHARED);
  5258. if (whichfork == XFS_DATA_FORK && !(iflags & BMV_IF_DELALLOC)) {
  5259. if (ip->i_delayed_blks || XFS_ISIZE(ip) > ip->i_d.di_size) {
  5260. error = -filemap_write_and_wait(VFS_I(ip)->i_mapping);
  5261. if (error)
  5262. goto out_unlock_iolock;
  5263. }
  5264. /*
  5265. * even after flushing the inode, there can still be delalloc
  5266. * blocks on the inode beyond EOF due to speculative
  5267. * preallocation. These are not removed until the release
  5268. * function is called or the inode is inactivated. Hence we
  5269. * cannot assert here that ip->i_delayed_blks == 0.
  5270. */
  5271. }
  5272. lock = xfs_ilock_map_shared(ip);
  5273. /*
  5274. * Don't let nex be bigger than the number of extents
  5275. * we can have assuming alternating holes and real extents.
  5276. */
  5277. if (nex > XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1)
  5278. nex = XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1;
  5279. bmapi_flags = xfs_bmapi_aflag(whichfork);
  5280. if (!(iflags & BMV_IF_PREALLOC))
  5281. bmapi_flags |= XFS_BMAPI_IGSTATE;
  5282. /*
  5283. * Allocate enough space to handle "subnex" maps at a time.
  5284. */
  5285. error = ENOMEM;
  5286. subnex = 16;
  5287. map = kmem_alloc(subnex * sizeof(*map), KM_MAYFAIL | KM_NOFS);
  5288. if (!map)
  5289. goto out_unlock_ilock;
  5290. bmv->bmv_entries = 0;
  5291. if (XFS_IFORK_NEXTENTS(ip, whichfork) == 0 &&
  5292. (whichfork == XFS_ATTR_FORK || !(iflags & BMV_IF_DELALLOC))) {
  5293. error = 0;
  5294. goto out_free_map;
  5295. }
  5296. nexleft = nex;
  5297. do {
  5298. nmap = (nexleft > subnex) ? subnex : nexleft;
  5299. error = xfs_bmapi_read(ip, XFS_BB_TO_FSBT(mp, bmv->bmv_offset),
  5300. XFS_BB_TO_FSB(mp, bmv->bmv_length),
  5301. map, &nmap, bmapi_flags);
  5302. if (error)
  5303. goto out_free_map;
  5304. ASSERT(nmap <= subnex);
  5305. for (i = 0; i < nmap && nexleft && bmv->bmv_length; i++) {
  5306. out[cur_ext].bmv_oflags = 0;
  5307. if (map[i].br_state == XFS_EXT_UNWRITTEN)
  5308. out[cur_ext].bmv_oflags |= BMV_OF_PREALLOC;
  5309. else if (map[i].br_startblock == DELAYSTARTBLOCK)
  5310. out[cur_ext].bmv_oflags |= BMV_OF_DELALLOC;
  5311. out[cur_ext].bmv_offset =
  5312. XFS_FSB_TO_BB(mp, map[i].br_startoff);
  5313. out[cur_ext].bmv_length =
  5314. XFS_FSB_TO_BB(mp, map[i].br_blockcount);
  5315. out[cur_ext].bmv_unused1 = 0;
  5316. out[cur_ext].bmv_unused2 = 0;
  5317. /*
  5318. * delayed allocation extents that start beyond EOF can
  5319. * occur due to speculative EOF allocation when the
  5320. * delalloc extent is larger than the largest freespace
  5321. * extent at conversion time. These extents cannot be
  5322. * converted by data writeback, so can exist here even
  5323. * if we are not supposed to be finding delalloc
  5324. * extents.
  5325. */
  5326. if (map[i].br_startblock == DELAYSTARTBLOCK &&
  5327. map[i].br_startoff <= XFS_B_TO_FSB(mp, XFS_ISIZE(ip)))
  5328. ASSERT((iflags & BMV_IF_DELALLOC) != 0);
  5329. if (map[i].br_startblock == HOLESTARTBLOCK &&
  5330. whichfork == XFS_ATTR_FORK) {
  5331. /* came to the end of attribute fork */
  5332. out[cur_ext].bmv_oflags |= BMV_OF_LAST;
  5333. goto out_free_map;
  5334. }
  5335. if (!xfs_getbmapx_fix_eof_hole(ip, &out[cur_ext],
  5336. prealloced, bmvend,
  5337. map[i].br_startblock))
  5338. goto out_free_map;
  5339. bmv->bmv_offset =
  5340. out[cur_ext].bmv_offset +
  5341. out[cur_ext].bmv_length;
  5342. bmv->bmv_length =
  5343. max_t(__int64_t, 0, bmvend - bmv->bmv_offset);
  5344. /*
  5345. * In case we don't want to return the hole,
  5346. * don't increase cur_ext so that we can reuse
  5347. * it in the next loop.
  5348. */
  5349. if ((iflags & BMV_IF_NO_HOLES) &&
  5350. map[i].br_startblock == HOLESTARTBLOCK) {
  5351. memset(&out[cur_ext], 0, sizeof(out[cur_ext]));
  5352. continue;
  5353. }
  5354. nexleft--;
  5355. bmv->bmv_entries++;
  5356. cur_ext++;
  5357. }
  5358. } while (nmap && nexleft && bmv->bmv_length);
  5359. out_free_map:
  5360. kmem_free(map);
  5361. out_unlock_ilock:
  5362. xfs_iunlock_map_shared(ip, lock);
  5363. out_unlock_iolock:
  5364. xfs_iunlock(ip, XFS_IOLOCK_SHARED);
  5365. for (i = 0; i < cur_ext; i++) {
  5366. int full = 0; /* user array is full */
  5367. /* format results & advance arg */
  5368. error = formatter(&arg, &out[i], &full);
  5369. if (error || full)
  5370. break;
  5371. }
  5372. if (is_vmalloc_addr(out))
  5373. kmem_free_large(out);
  5374. else
  5375. kmem_free(out);
  5376. return error;
  5377. }
  5378. #ifdef DEBUG
  5379. STATIC struct xfs_buf *
  5380. xfs_bmap_get_bp(
  5381. struct xfs_btree_cur *cur,
  5382. xfs_fsblock_t bno)
  5383. {
  5384. struct xfs_log_item_desc *lidp;
  5385. int i;
  5386. if (!cur)
  5387. return NULL;
  5388. for (i = 0; i < XFS_BTREE_MAXLEVELS; i++) {
  5389. if (!cur->bc_bufs[i])
  5390. break;
  5391. if (XFS_BUF_ADDR(cur->bc_bufs[i]) == bno)
  5392. return cur->bc_bufs[i];
  5393. }
  5394. /* Chase down all the log items to see if the bp is there */
  5395. list_for_each_entry(lidp, &cur->bc_tp->t_items, lid_trans) {
  5396. struct xfs_buf_log_item *bip;
  5397. bip = (struct xfs_buf_log_item *)lidp->lid_item;
  5398. if (bip->bli_item.li_type == XFS_LI_BUF &&
  5399. XFS_BUF_ADDR(bip->bli_buf) == bno)
  5400. return bip->bli_buf;
  5401. }
  5402. return NULL;
  5403. }
  5404. STATIC void
  5405. xfs_check_block(
  5406. struct xfs_btree_block *block,
  5407. xfs_mount_t *mp,
  5408. int root,
  5409. short sz)
  5410. {
  5411. int i, j, dmxr;
  5412. __be64 *pp, *thispa; /* pointer to block address */
  5413. xfs_bmbt_key_t *prevp, *keyp;
  5414. ASSERT(be16_to_cpu(block->bb_level) > 0);
  5415. prevp = NULL;
  5416. for( i = 1; i <= xfs_btree_get_numrecs(block); i++) {
  5417. dmxr = mp->m_bmap_dmxr[0];
  5418. keyp = XFS_BMBT_KEY_ADDR(mp, block, i);
  5419. if (prevp) {
  5420. ASSERT(be64_to_cpu(prevp->br_startoff) <
  5421. be64_to_cpu(keyp->br_startoff));
  5422. }
  5423. prevp = keyp;
  5424. /*
  5425. * Compare the block numbers to see if there are dups.
  5426. */
  5427. if (root)
  5428. pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, i, sz);
  5429. else
  5430. pp = XFS_BMBT_PTR_ADDR(mp, block, i, dmxr);
  5431. for (j = i+1; j <= be16_to_cpu(block->bb_numrecs); j++) {
  5432. if (root)
  5433. thispa = XFS_BMAP_BROOT_PTR_ADDR(mp, block, j, sz);
  5434. else
  5435. thispa = XFS_BMBT_PTR_ADDR(mp, block, j, dmxr);
  5436. if (*thispa == *pp) {
  5437. xfs_warn(mp, "%s: thispa(%d) == pp(%d) %Ld",
  5438. __func__, j, i,
  5439. (unsigned long long)be64_to_cpu(*thispa));
  5440. panic("%s: ptrs are equal in node\n",
  5441. __func__);
  5442. }
  5443. }
  5444. }
  5445. }
  5446. /*
  5447. * Check that the extents for the inode ip are in the right order in all
  5448. * btree leaves.
  5449. */
  5450. STATIC void
  5451. xfs_bmap_check_leaf_extents(
  5452. xfs_btree_cur_t *cur, /* btree cursor or null */
  5453. xfs_inode_t *ip, /* incore inode pointer */
  5454. int whichfork) /* data or attr fork */
  5455. {
  5456. struct xfs_btree_block *block; /* current btree block */
  5457. xfs_fsblock_t bno; /* block # of "block" */
  5458. xfs_buf_t *bp; /* buffer for "block" */
  5459. int error; /* error return value */
  5460. xfs_extnum_t i=0, j; /* index into the extents list */
  5461. xfs_ifork_t *ifp; /* fork structure */
  5462. int level; /* btree level, for checking */
  5463. xfs_mount_t *mp; /* file system mount structure */
  5464. __be64 *pp; /* pointer to block address */
  5465. xfs_bmbt_rec_t *ep; /* pointer to current extent */
  5466. xfs_bmbt_rec_t last = {0, 0}; /* last extent in prev block */
  5467. xfs_bmbt_rec_t *nextp; /* pointer to next extent */
  5468. int bp_release = 0;
  5469. if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) {
  5470. return;
  5471. }
  5472. bno = NULLFSBLOCK;
  5473. mp = ip->i_mount;
  5474. ifp = XFS_IFORK_PTR(ip, whichfork);
  5475. block = ifp->if_broot;
  5476. /*
  5477. * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
  5478. */
  5479. level = be16_to_cpu(block->bb_level);
  5480. ASSERT(level > 0);
  5481. xfs_check_block(block, mp, 1, ifp->if_broot_bytes);
  5482. pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
  5483. bno = be64_to_cpu(*pp);
  5484. ASSERT(bno != NULLDFSBNO);
  5485. ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
  5486. ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
  5487. /*
  5488. * Go down the tree until leaf level is reached, following the first
  5489. * pointer (leftmost) at each level.
  5490. */
  5491. while (level-- > 0) {
  5492. /* See if buf is in cur first */
  5493. bp_release = 0;
  5494. bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
  5495. if (!bp) {
  5496. bp_release = 1;
  5497. error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
  5498. XFS_BMAP_BTREE_REF,
  5499. &xfs_bmbt_buf_ops);
  5500. if (error)
  5501. goto error_norelse;
  5502. }
  5503. block = XFS_BUF_TO_BLOCK(bp);
  5504. XFS_WANT_CORRUPTED_GOTO(
  5505. xfs_bmap_sanity_check(mp, bp, level),
  5506. error0);
  5507. if (level == 0)
  5508. break;
  5509. /*
  5510. * Check this block for basic sanity (increasing keys and
  5511. * no duplicate blocks).
  5512. */
  5513. xfs_check_block(block, mp, 0, 0);
  5514. pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
  5515. bno = be64_to_cpu(*pp);
  5516. XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp, bno), error0);
  5517. if (bp_release) {
  5518. bp_release = 0;
  5519. xfs_trans_brelse(NULL, bp);
  5520. }
  5521. }
  5522. /*
  5523. * Here with bp and block set to the leftmost leaf node in the tree.
  5524. */
  5525. i = 0;
  5526. /*
  5527. * Loop over all leaf nodes checking that all extents are in the right order.
  5528. */
  5529. for (;;) {
  5530. xfs_fsblock_t nextbno;
  5531. xfs_extnum_t num_recs;
  5532. num_recs = xfs_btree_get_numrecs(block);
  5533. /*
  5534. * Read-ahead the next leaf block, if any.
  5535. */
  5536. nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
  5537. /*
  5538. * Check all the extents to make sure they are OK.
  5539. * If we had a previous block, the last entry should
  5540. * conform with the first entry in this one.
  5541. */
  5542. ep = XFS_BMBT_REC_ADDR(mp, block, 1);
  5543. if (i) {
  5544. ASSERT(xfs_bmbt_disk_get_startoff(&last) +
  5545. xfs_bmbt_disk_get_blockcount(&last) <=
  5546. xfs_bmbt_disk_get_startoff(ep));
  5547. }
  5548. for (j = 1; j < num_recs; j++) {
  5549. nextp = XFS_BMBT_REC_ADDR(mp, block, j + 1);
  5550. ASSERT(xfs_bmbt_disk_get_startoff(ep) +
  5551. xfs_bmbt_disk_get_blockcount(ep) <=
  5552. xfs_bmbt_disk_get_startoff(nextp));
  5553. ep = nextp;
  5554. }
  5555. last = *ep;
  5556. i += num_recs;
  5557. if (bp_release) {
  5558. bp_release = 0;
  5559. xfs_trans_brelse(NULL, bp);
  5560. }
  5561. bno = nextbno;
  5562. /*
  5563. * If we've reached the end, stop.
  5564. */
  5565. if (bno == NULLFSBLOCK)
  5566. break;
  5567. bp_release = 0;
  5568. bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
  5569. if (!bp) {
  5570. bp_release = 1;
  5571. error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
  5572. XFS_BMAP_BTREE_REF,
  5573. &xfs_bmbt_buf_ops);
  5574. if (error)
  5575. goto error_norelse;
  5576. }
  5577. block = XFS_BUF_TO_BLOCK(bp);
  5578. }
  5579. if (bp_release) {
  5580. bp_release = 0;
  5581. xfs_trans_brelse(NULL, bp);
  5582. }
  5583. return;
  5584. error0:
  5585. xfs_warn(mp, "%s: at error0", __func__);
  5586. if (bp_release)
  5587. xfs_trans_brelse(NULL, bp);
  5588. error_norelse:
  5589. xfs_warn(mp, "%s: BAD after btree leaves for %d extents",
  5590. __func__, i);
  5591. panic("%s: CORRUPTED BTREE OR SOMETHING", __func__);
  5592. return;
  5593. }
  5594. #endif
  5595. /*
  5596. * Count fsblocks of the given fork.
  5597. */
  5598. int /* error */
  5599. xfs_bmap_count_blocks(
  5600. xfs_trans_t *tp, /* transaction pointer */
  5601. xfs_inode_t *ip, /* incore inode */
  5602. int whichfork, /* data or attr fork */
  5603. int *count) /* out: count of blocks */
  5604. {
  5605. struct xfs_btree_block *block; /* current btree block */
  5606. xfs_fsblock_t bno; /* block # of "block" */
  5607. xfs_ifork_t *ifp; /* fork structure */
  5608. int level; /* btree level, for checking */
  5609. xfs_mount_t *mp; /* file system mount structure */
  5610. __be64 *pp; /* pointer to block address */
  5611. bno = NULLFSBLOCK;
  5612. mp = ip->i_mount;
  5613. ifp = XFS_IFORK_PTR(ip, whichfork);
  5614. if ( XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ) {
  5615. xfs_bmap_count_leaves(ifp, 0,
  5616. ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t),
  5617. count);
  5618. return 0;
  5619. }
  5620. /*
  5621. * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
  5622. */
  5623. block = ifp->if_broot;
  5624. level = be16_to_cpu(block->bb_level);
  5625. ASSERT(level > 0);
  5626. pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
  5627. bno = be64_to_cpu(*pp);
  5628. ASSERT(bno != NULLDFSBNO);
  5629. ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
  5630. ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
  5631. if (unlikely(xfs_bmap_count_tree(mp, tp, ifp, bno, level, count) < 0)) {
  5632. XFS_ERROR_REPORT("xfs_bmap_count_blocks(2)", XFS_ERRLEVEL_LOW,
  5633. mp);
  5634. return XFS_ERROR(EFSCORRUPTED);
  5635. }
  5636. return 0;
  5637. }
  5638. /*
  5639. * Recursively walks each level of a btree
  5640. * to count total fsblocks is use.
  5641. */
  5642. STATIC int /* error */
  5643. xfs_bmap_count_tree(
  5644. xfs_mount_t *mp, /* file system mount point */
  5645. xfs_trans_t *tp, /* transaction pointer */
  5646. xfs_ifork_t *ifp, /* inode fork pointer */
  5647. xfs_fsblock_t blockno, /* file system block number */
  5648. int levelin, /* level in btree */
  5649. int *count) /* Count of blocks */
  5650. {
  5651. int error;
  5652. xfs_buf_t *bp, *nbp;
  5653. int level = levelin;
  5654. __be64 *pp;
  5655. xfs_fsblock_t bno = blockno;
  5656. xfs_fsblock_t nextbno;
  5657. struct xfs_btree_block *block, *nextblock;
  5658. int numrecs;
  5659. error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, XFS_BMAP_BTREE_REF,
  5660. &xfs_bmbt_buf_ops);
  5661. if (error)
  5662. return error;
  5663. *count += 1;
  5664. block = XFS_BUF_TO_BLOCK(bp);
  5665. if (--level) {
  5666. /* Not at node above leaves, count this level of nodes */
  5667. nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
  5668. while (nextbno != NULLFSBLOCK) {
  5669. error = xfs_btree_read_bufl(mp, tp, nextbno, 0, &nbp,
  5670. XFS_BMAP_BTREE_REF,
  5671. &xfs_bmbt_buf_ops);
  5672. if (error)
  5673. return error;
  5674. *count += 1;
  5675. nextblock = XFS_BUF_TO_BLOCK(nbp);
  5676. nextbno = be64_to_cpu(nextblock->bb_u.l.bb_rightsib);
  5677. xfs_trans_brelse(tp, nbp);
  5678. }
  5679. /* Dive to the next level */
  5680. pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
  5681. bno = be64_to_cpu(*pp);
  5682. if (unlikely((error =
  5683. xfs_bmap_count_tree(mp, tp, ifp, bno, level, count)) < 0)) {
  5684. xfs_trans_brelse(tp, bp);
  5685. XFS_ERROR_REPORT("xfs_bmap_count_tree(1)",
  5686. XFS_ERRLEVEL_LOW, mp);
  5687. return XFS_ERROR(EFSCORRUPTED);
  5688. }
  5689. xfs_trans_brelse(tp, bp);
  5690. } else {
  5691. /* count all level 1 nodes and their leaves */
  5692. for (;;) {
  5693. nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
  5694. numrecs = be16_to_cpu(block->bb_numrecs);
  5695. xfs_bmap_disk_count_leaves(mp, block, numrecs, count);
  5696. xfs_trans_brelse(tp, bp);
  5697. if (nextbno == NULLFSBLOCK)
  5698. break;
  5699. bno = nextbno;
  5700. error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
  5701. XFS_BMAP_BTREE_REF,
  5702. &xfs_bmbt_buf_ops);
  5703. if (error)
  5704. return error;
  5705. *count += 1;
  5706. block = XFS_BUF_TO_BLOCK(bp);
  5707. }
  5708. }
  5709. return 0;
  5710. }
  5711. /*
  5712. * Count leaf blocks given a range of extent records.
  5713. */
  5714. STATIC void
  5715. xfs_bmap_count_leaves(
  5716. xfs_ifork_t *ifp,
  5717. xfs_extnum_t idx,
  5718. int numrecs,
  5719. int *count)
  5720. {
  5721. int b;
  5722. for (b = 0; b < numrecs; b++) {
  5723. xfs_bmbt_rec_host_t *frp = xfs_iext_get_ext(ifp, idx + b);
  5724. *count += xfs_bmbt_get_blockcount(frp);
  5725. }
  5726. }
  5727. /*
  5728. * Count leaf blocks given a range of extent records originally
  5729. * in btree format.
  5730. */
  5731. STATIC void
  5732. xfs_bmap_disk_count_leaves(
  5733. struct xfs_mount *mp,
  5734. struct xfs_btree_block *block,
  5735. int numrecs,
  5736. int *count)
  5737. {
  5738. int b;
  5739. xfs_bmbt_rec_t *frp;
  5740. for (b = 1; b <= numrecs; b++) {
  5741. frp = XFS_BMBT_REC_ADDR(mp, block, b);
  5742. *count += xfs_bmbt_disk_get_blockcount(frp);
  5743. }
  5744. }
  5745. /*
  5746. * dead simple method of punching delalyed allocation blocks from a range in
  5747. * the inode. Walks a block at a time so will be slow, but is only executed in
  5748. * rare error cases so the overhead is not critical. This will alays punch out
  5749. * both the start and end blocks, even if the ranges only partially overlap
  5750. * them, so it is up to the caller to ensure that partial blocks are not
  5751. * passed in.
  5752. */
  5753. int
  5754. xfs_bmap_punch_delalloc_range(
  5755. struct xfs_inode *ip,
  5756. xfs_fileoff_t start_fsb,
  5757. xfs_fileoff_t length)
  5758. {
  5759. xfs_fileoff_t remaining = length;
  5760. int error = 0;
  5761. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
  5762. do {
  5763. int done;
  5764. xfs_bmbt_irec_t imap;
  5765. int nimaps = 1;
  5766. xfs_fsblock_t firstblock;
  5767. xfs_bmap_free_t flist;
  5768. /*
  5769. * Map the range first and check that it is a delalloc extent
  5770. * before trying to unmap the range. Otherwise we will be
  5771. * trying to remove a real extent (which requires a
  5772. * transaction) or a hole, which is probably a bad idea...
  5773. */
  5774. error = xfs_bmapi_read(ip, start_fsb, 1, &imap, &nimaps,
  5775. XFS_BMAPI_ENTIRE);
  5776. if (error) {
  5777. /* something screwed, just bail */
  5778. if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) {
  5779. xfs_alert(ip->i_mount,
  5780. "Failed delalloc mapping lookup ino %lld fsb %lld.",
  5781. ip->i_ino, start_fsb);
  5782. }
  5783. break;
  5784. }
  5785. if (!nimaps) {
  5786. /* nothing there */
  5787. goto next_block;
  5788. }
  5789. if (imap.br_startblock != DELAYSTARTBLOCK) {
  5790. /* been converted, ignore */
  5791. goto next_block;
  5792. }
  5793. WARN_ON(imap.br_blockcount == 0);
  5794. /*
  5795. * Note: while we initialise the firstblock/flist pair, they
  5796. * should never be used because blocks should never be
  5797. * allocated or freed for a delalloc extent and hence we need
  5798. * don't cancel or finish them after the xfs_bunmapi() call.
  5799. */
  5800. xfs_bmap_init(&flist, &firstblock);
  5801. error = xfs_bunmapi(NULL, ip, start_fsb, 1, 0, 1, &firstblock,
  5802. &flist, &done);
  5803. if (error)
  5804. break;
  5805. ASSERT(!flist.xbf_count && !flist.xbf_first);
  5806. next_block:
  5807. start_fsb++;
  5808. remaining--;
  5809. } while(remaining > 0);
  5810. return error;
  5811. }
  5812. /*
  5813. * Convert the given file system block to a disk block. We have to treat it
  5814. * differently based on whether the file is a real time file or not, because the
  5815. * bmap code does.
  5816. */
  5817. xfs_daddr_t
  5818. xfs_fsb_to_db(struct xfs_inode *ip, xfs_fsblock_t fsb)
  5819. {
  5820. return (XFS_IS_REALTIME_INODE(ip) ? \
  5821. (xfs_daddr_t)XFS_FSB_TO_BB((ip)->i_mount, (fsb)) : \
  5822. XFS_FSB_TO_DADDR((ip)->i_mount, (fsb)));
  5823. }