xfs_iomap.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2000-2006 Silicon Graphics, Inc.
  4. * Copyright (c) 2016 Christoph Hellwig.
  5. * All Rights Reserved.
  6. */
  7. #include <linux/iomap.h>
  8. #include "xfs.h"
  9. #include "xfs_fs.h"
  10. #include "xfs_shared.h"
  11. #include "xfs_format.h"
  12. #include "xfs_log_format.h"
  13. #include "xfs_trans_resv.h"
  14. #include "xfs_mount.h"
  15. #include "xfs_defer.h"
  16. #include "xfs_inode.h"
  17. #include "xfs_btree.h"
  18. #include "xfs_bmap_btree.h"
  19. #include "xfs_bmap.h"
  20. #include "xfs_bmap_util.h"
  21. #include "xfs_errortag.h"
  22. #include "xfs_error.h"
  23. #include "xfs_trans.h"
  24. #include "xfs_trans_space.h"
  25. #include "xfs_inode_item.h"
  26. #include "xfs_iomap.h"
  27. #include "xfs_trace.h"
  28. #include "xfs_icache.h"
  29. #include "xfs_quota.h"
  30. #include "xfs_dquot_item.h"
  31. #include "xfs_dquot.h"
  32. #include "xfs_reflink.h"
  33. #define XFS_WRITEIO_ALIGN(mp,off) (((off) >> mp->m_writeio_log) \
  34. << mp->m_writeio_log)
  35. void
  36. xfs_bmbt_to_iomap(
  37. struct xfs_inode *ip,
  38. struct iomap *iomap,
  39. struct xfs_bmbt_irec *imap)
  40. {
  41. struct xfs_mount *mp = ip->i_mount;
  42. if (imap->br_startblock == HOLESTARTBLOCK) {
  43. iomap->addr = IOMAP_NULL_ADDR;
  44. iomap->type = IOMAP_HOLE;
  45. } else if (imap->br_startblock == DELAYSTARTBLOCK) {
  46. iomap->addr = IOMAP_NULL_ADDR;
  47. iomap->type = IOMAP_DELALLOC;
  48. } else {
  49. iomap->addr = BBTOB(xfs_fsb_to_db(ip, imap->br_startblock));
  50. if (imap->br_state == XFS_EXT_UNWRITTEN)
  51. iomap->type = IOMAP_UNWRITTEN;
  52. else
  53. iomap->type = IOMAP_MAPPED;
  54. }
  55. iomap->offset = XFS_FSB_TO_B(mp, imap->br_startoff);
  56. iomap->length = XFS_FSB_TO_B(mp, imap->br_blockcount);
  57. iomap->bdev = xfs_find_bdev_for_inode(VFS_I(ip));
  58. iomap->dax_dev = xfs_find_daxdev_for_inode(VFS_I(ip));
  59. }
  60. xfs_extlen_t
  61. xfs_eof_alignment(
  62. struct xfs_inode *ip,
  63. xfs_extlen_t extsize)
  64. {
  65. struct xfs_mount *mp = ip->i_mount;
  66. xfs_extlen_t align = 0;
  67. if (!XFS_IS_REALTIME_INODE(ip)) {
  68. /*
  69. * Round up the allocation request to a stripe unit
  70. * (m_dalign) boundary if the file size is >= stripe unit
  71. * size, and we are allocating past the allocation eof.
  72. *
  73. * If mounted with the "-o swalloc" option the alignment is
  74. * increased from the strip unit size to the stripe width.
  75. */
  76. if (mp->m_swidth && (mp->m_flags & XFS_MOUNT_SWALLOC))
  77. align = mp->m_swidth;
  78. else if (mp->m_dalign)
  79. align = mp->m_dalign;
  80. if (align && XFS_ISIZE(ip) < XFS_FSB_TO_B(mp, align))
  81. align = 0;
  82. }
  83. /*
  84. * Always round up the allocation request to an extent boundary
  85. * (when file on a real-time subvolume or has di_extsize hint).
  86. */
  87. if (extsize) {
  88. if (align)
  89. align = roundup_64(align, extsize);
  90. else
  91. align = extsize;
  92. }
  93. return align;
  94. }
  95. STATIC int
  96. xfs_iomap_eof_align_last_fsb(
  97. struct xfs_inode *ip,
  98. xfs_extlen_t extsize,
  99. xfs_fileoff_t *last_fsb)
  100. {
  101. xfs_extlen_t align = xfs_eof_alignment(ip, extsize);
  102. if (align) {
  103. xfs_fileoff_t new_last_fsb = roundup_64(*last_fsb, align);
  104. int eof, error;
  105. error = xfs_bmap_eof(ip, new_last_fsb, XFS_DATA_FORK, &eof);
  106. if (error)
  107. return error;
  108. if (eof)
  109. *last_fsb = new_last_fsb;
  110. }
  111. return 0;
  112. }
  113. STATIC int
  114. xfs_alert_fsblock_zero(
  115. xfs_inode_t *ip,
  116. xfs_bmbt_irec_t *imap)
  117. {
  118. xfs_alert_tag(ip->i_mount, XFS_PTAG_FSBLOCK_ZERO,
  119. "Access to block zero in inode %llu "
  120. "start_block: %llx start_off: %llx "
  121. "blkcnt: %llx extent-state: %x",
  122. (unsigned long long)ip->i_ino,
  123. (unsigned long long)imap->br_startblock,
  124. (unsigned long long)imap->br_startoff,
  125. (unsigned long long)imap->br_blockcount,
  126. imap->br_state);
  127. return -EFSCORRUPTED;
  128. }
  129. int
  130. xfs_iomap_write_direct(
  131. xfs_inode_t *ip,
  132. xfs_off_t offset,
  133. size_t count,
  134. xfs_bmbt_irec_t *imap,
  135. int nmaps)
  136. {
  137. xfs_mount_t *mp = ip->i_mount;
  138. xfs_fileoff_t offset_fsb;
  139. xfs_fileoff_t last_fsb;
  140. xfs_filblks_t count_fsb, resaligned;
  141. xfs_fsblock_t firstfsb;
  142. xfs_extlen_t extsz;
  143. int nimaps;
  144. int quota_flag;
  145. int rt;
  146. xfs_trans_t *tp;
  147. struct xfs_defer_ops dfops;
  148. uint qblocks, resblks, resrtextents;
  149. int error;
  150. int lockmode;
  151. int bmapi_flags = XFS_BMAPI_PREALLOC;
  152. uint tflags = 0;
  153. rt = XFS_IS_REALTIME_INODE(ip);
  154. extsz = xfs_get_extsz_hint(ip);
  155. lockmode = XFS_ILOCK_SHARED; /* locked by caller */
  156. ASSERT(xfs_isilocked(ip, lockmode));
  157. offset_fsb = XFS_B_TO_FSBT(mp, offset);
  158. last_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)(offset + count)));
  159. if ((offset + count) > XFS_ISIZE(ip)) {
  160. /*
  161. * Assert that the in-core extent list is present since this can
  162. * call xfs_iread_extents() and we only have the ilock shared.
  163. * This should be safe because the lock was held around a bmapi
  164. * call in the caller and we only need it to access the in-core
  165. * list.
  166. */
  167. ASSERT(XFS_IFORK_PTR(ip, XFS_DATA_FORK)->if_flags &
  168. XFS_IFEXTENTS);
  169. error = xfs_iomap_eof_align_last_fsb(ip, extsz, &last_fsb);
  170. if (error)
  171. goto out_unlock;
  172. } else {
  173. if (nmaps && (imap->br_startblock == HOLESTARTBLOCK))
  174. last_fsb = min(last_fsb, (xfs_fileoff_t)
  175. imap->br_blockcount +
  176. imap->br_startoff);
  177. }
  178. count_fsb = last_fsb - offset_fsb;
  179. ASSERT(count_fsb > 0);
  180. resaligned = xfs_aligned_fsb_count(offset_fsb, count_fsb, extsz);
  181. if (unlikely(rt)) {
  182. resrtextents = qblocks = resaligned;
  183. resrtextents /= mp->m_sb.sb_rextsize;
  184. resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
  185. quota_flag = XFS_QMOPT_RES_RTBLKS;
  186. } else {
  187. resrtextents = 0;
  188. resblks = qblocks = XFS_DIOSTRAT_SPACE_RES(mp, resaligned);
  189. quota_flag = XFS_QMOPT_RES_REGBLKS;
  190. }
  191. /*
  192. * Drop the shared lock acquired by the caller, attach the dquot if
  193. * necessary and move on to transaction setup.
  194. */
  195. xfs_iunlock(ip, lockmode);
  196. error = xfs_qm_dqattach(ip);
  197. if (error)
  198. return error;
  199. /*
  200. * For DAX, we do not allocate unwritten extents, but instead we zero
  201. * the block before we commit the transaction. Ideally we'd like to do
  202. * this outside the transaction context, but if we commit and then crash
  203. * we may not have zeroed the blocks and this will be exposed on
  204. * recovery of the allocation. Hence we must zero before commit.
  205. *
  206. * Further, if we are mapping unwritten extents here, we need to zero
  207. * and convert them to written so that we don't need an unwritten extent
  208. * callback for DAX. This also means that we need to be able to dip into
  209. * the reserve block pool for bmbt block allocation if there is no space
  210. * left but we need to do unwritten extent conversion.
  211. */
  212. if (IS_DAX(VFS_I(ip))) {
  213. bmapi_flags = XFS_BMAPI_CONVERT | XFS_BMAPI_ZERO;
  214. if (imap->br_state == XFS_EXT_UNWRITTEN) {
  215. tflags |= XFS_TRANS_RESERVE;
  216. resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0) << 1;
  217. }
  218. }
  219. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, resblks, resrtextents,
  220. tflags, &tp);
  221. if (error)
  222. return error;
  223. lockmode = XFS_ILOCK_EXCL;
  224. xfs_ilock(ip, lockmode);
  225. error = xfs_trans_reserve_quota_nblks(tp, ip, qblocks, 0, quota_flag);
  226. if (error)
  227. goto out_trans_cancel;
  228. xfs_trans_ijoin(tp, ip, 0);
  229. /*
  230. * From this point onwards we overwrite the imap pointer that the
  231. * caller gave to us.
  232. */
  233. xfs_defer_init(&dfops, &firstfsb);
  234. nimaps = 1;
  235. error = xfs_bmapi_write(tp, ip, offset_fsb, count_fsb,
  236. bmapi_flags, &firstfsb, resblks, imap,
  237. &nimaps, &dfops);
  238. if (error)
  239. goto out_bmap_cancel;
  240. /*
  241. * Complete the transaction
  242. */
  243. error = xfs_defer_finish(&tp, &dfops);
  244. if (error)
  245. goto out_bmap_cancel;
  246. error = xfs_trans_commit(tp);
  247. if (error)
  248. goto out_unlock;
  249. /*
  250. * Copy any maps to caller's array and return any error.
  251. */
  252. if (nimaps == 0) {
  253. error = -ENOSPC;
  254. goto out_unlock;
  255. }
  256. if (!(imap->br_startblock || XFS_IS_REALTIME_INODE(ip)))
  257. error = xfs_alert_fsblock_zero(ip, imap);
  258. out_unlock:
  259. xfs_iunlock(ip, lockmode);
  260. return error;
  261. out_bmap_cancel:
  262. xfs_defer_cancel(&dfops);
  263. xfs_trans_unreserve_quota_nblks(tp, ip, (long)qblocks, 0, quota_flag);
  264. out_trans_cancel:
  265. xfs_trans_cancel(tp);
  266. goto out_unlock;
  267. }
  268. STATIC bool
  269. xfs_quota_need_throttle(
  270. struct xfs_inode *ip,
  271. int type,
  272. xfs_fsblock_t alloc_blocks)
  273. {
  274. struct xfs_dquot *dq = xfs_inode_dquot(ip, type);
  275. if (!dq || !xfs_this_quota_on(ip->i_mount, type))
  276. return false;
  277. /* no hi watermark, no throttle */
  278. if (!dq->q_prealloc_hi_wmark)
  279. return false;
  280. /* under the lo watermark, no throttle */
  281. if (dq->q_res_bcount + alloc_blocks < dq->q_prealloc_lo_wmark)
  282. return false;
  283. return true;
  284. }
  285. STATIC void
  286. xfs_quota_calc_throttle(
  287. struct xfs_inode *ip,
  288. int type,
  289. xfs_fsblock_t *qblocks,
  290. int *qshift,
  291. int64_t *qfreesp)
  292. {
  293. int64_t freesp;
  294. int shift = 0;
  295. struct xfs_dquot *dq = xfs_inode_dquot(ip, type);
  296. /* no dq, or over hi wmark, squash the prealloc completely */
  297. if (!dq || dq->q_res_bcount >= dq->q_prealloc_hi_wmark) {
  298. *qblocks = 0;
  299. *qfreesp = 0;
  300. return;
  301. }
  302. freesp = dq->q_prealloc_hi_wmark - dq->q_res_bcount;
  303. if (freesp < dq->q_low_space[XFS_QLOWSP_5_PCNT]) {
  304. shift = 2;
  305. if (freesp < dq->q_low_space[XFS_QLOWSP_3_PCNT])
  306. shift += 2;
  307. if (freesp < dq->q_low_space[XFS_QLOWSP_1_PCNT])
  308. shift += 2;
  309. }
  310. if (freesp < *qfreesp)
  311. *qfreesp = freesp;
  312. /* only overwrite the throttle values if we are more aggressive */
  313. if ((freesp >> shift) < (*qblocks >> *qshift)) {
  314. *qblocks = freesp;
  315. *qshift = shift;
  316. }
  317. }
  318. /*
  319. * If we are doing a write at the end of the file and there are no allocations
  320. * past this one, then extend the allocation out to the file system's write
  321. * iosize.
  322. *
  323. * If we don't have a user specified preallocation size, dynamically increase
  324. * the preallocation size as the size of the file grows. Cap the maximum size
  325. * at a single extent or less if the filesystem is near full. The closer the
  326. * filesystem is to full, the smaller the maximum prealocation.
  327. *
  328. * As an exception we don't do any preallocation at all if the file is smaller
  329. * than the minimum preallocation and we are using the default dynamic
  330. * preallocation scheme, as it is likely this is the only write to the file that
  331. * is going to be done.
  332. *
  333. * We clean up any extra space left over when the file is closed in
  334. * xfs_inactive().
  335. */
  336. STATIC xfs_fsblock_t
  337. xfs_iomap_prealloc_size(
  338. struct xfs_inode *ip,
  339. loff_t offset,
  340. loff_t count,
  341. struct xfs_iext_cursor *icur)
  342. {
  343. struct xfs_mount *mp = ip->i_mount;
  344. struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
  345. xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset);
  346. struct xfs_bmbt_irec prev;
  347. int shift = 0;
  348. int64_t freesp;
  349. xfs_fsblock_t qblocks;
  350. int qshift = 0;
  351. xfs_fsblock_t alloc_blocks = 0;
  352. if (offset + count <= XFS_ISIZE(ip))
  353. return 0;
  354. if (!(mp->m_flags & XFS_MOUNT_DFLT_IOSIZE) &&
  355. (XFS_ISIZE(ip) < XFS_FSB_TO_B(mp, mp->m_writeio_blocks)))
  356. return 0;
  357. /*
  358. * If an explicit allocsize is set, the file is small, or we
  359. * are writing behind a hole, then use the minimum prealloc:
  360. */
  361. if ((mp->m_flags & XFS_MOUNT_DFLT_IOSIZE) ||
  362. XFS_ISIZE(ip) < XFS_FSB_TO_B(mp, mp->m_dalign) ||
  363. !xfs_iext_peek_prev_extent(ifp, icur, &prev) ||
  364. prev.br_startoff + prev.br_blockcount < offset_fsb)
  365. return mp->m_writeio_blocks;
  366. /*
  367. * Determine the initial size of the preallocation. We are beyond the
  368. * current EOF here, but we need to take into account whether this is
  369. * a sparse write or an extending write when determining the
  370. * preallocation size. Hence we need to look up the extent that ends
  371. * at the current write offset and use the result to determine the
  372. * preallocation size.
  373. *
  374. * If the extent is a hole, then preallocation is essentially disabled.
  375. * Otherwise we take the size of the preceding data extent as the basis
  376. * for the preallocation size. If the size of the extent is greater than
  377. * half the maximum extent length, then use the current offset as the
  378. * basis. This ensures that for large files the preallocation size
  379. * always extends to MAXEXTLEN rather than falling short due to things
  380. * like stripe unit/width alignment of real extents.
  381. */
  382. if (prev.br_blockcount <= (MAXEXTLEN >> 1))
  383. alloc_blocks = prev.br_blockcount << 1;
  384. else
  385. alloc_blocks = XFS_B_TO_FSB(mp, offset);
  386. if (!alloc_blocks)
  387. goto check_writeio;
  388. qblocks = alloc_blocks;
  389. /*
  390. * MAXEXTLEN is not a power of two value but we round the prealloc down
  391. * to the nearest power of two value after throttling. To prevent the
  392. * round down from unconditionally reducing the maximum supported prealloc
  393. * size, we round up first, apply appropriate throttling, round down and
  394. * cap the value to MAXEXTLEN.
  395. */
  396. alloc_blocks = XFS_FILEOFF_MIN(roundup_pow_of_two(MAXEXTLEN),
  397. alloc_blocks);
  398. freesp = percpu_counter_read_positive(&mp->m_fdblocks);
  399. if (freesp < mp->m_low_space[XFS_LOWSP_5_PCNT]) {
  400. shift = 2;
  401. if (freesp < mp->m_low_space[XFS_LOWSP_4_PCNT])
  402. shift++;
  403. if (freesp < mp->m_low_space[XFS_LOWSP_3_PCNT])
  404. shift++;
  405. if (freesp < mp->m_low_space[XFS_LOWSP_2_PCNT])
  406. shift++;
  407. if (freesp < mp->m_low_space[XFS_LOWSP_1_PCNT])
  408. shift++;
  409. }
  410. /*
  411. * Check each quota to cap the prealloc size, provide a shift value to
  412. * throttle with and adjust amount of available space.
  413. */
  414. if (xfs_quota_need_throttle(ip, XFS_DQ_USER, alloc_blocks))
  415. xfs_quota_calc_throttle(ip, XFS_DQ_USER, &qblocks, &qshift,
  416. &freesp);
  417. if (xfs_quota_need_throttle(ip, XFS_DQ_GROUP, alloc_blocks))
  418. xfs_quota_calc_throttle(ip, XFS_DQ_GROUP, &qblocks, &qshift,
  419. &freesp);
  420. if (xfs_quota_need_throttle(ip, XFS_DQ_PROJ, alloc_blocks))
  421. xfs_quota_calc_throttle(ip, XFS_DQ_PROJ, &qblocks, &qshift,
  422. &freesp);
  423. /*
  424. * The final prealloc size is set to the minimum of free space available
  425. * in each of the quotas and the overall filesystem.
  426. *
  427. * The shift throttle value is set to the maximum value as determined by
  428. * the global low free space values and per-quota low free space values.
  429. */
  430. alloc_blocks = min(alloc_blocks, qblocks);
  431. shift = max(shift, qshift);
  432. if (shift)
  433. alloc_blocks >>= shift;
  434. /*
  435. * rounddown_pow_of_two() returns an undefined result if we pass in
  436. * alloc_blocks = 0.
  437. */
  438. if (alloc_blocks)
  439. alloc_blocks = rounddown_pow_of_two(alloc_blocks);
  440. if (alloc_blocks > MAXEXTLEN)
  441. alloc_blocks = MAXEXTLEN;
  442. /*
  443. * If we are still trying to allocate more space than is
  444. * available, squash the prealloc hard. This can happen if we
  445. * have a large file on a small filesystem and the above
  446. * lowspace thresholds are smaller than MAXEXTLEN.
  447. */
  448. while (alloc_blocks && alloc_blocks >= freesp)
  449. alloc_blocks >>= 4;
  450. check_writeio:
  451. if (alloc_blocks < mp->m_writeio_blocks)
  452. alloc_blocks = mp->m_writeio_blocks;
  453. trace_xfs_iomap_prealloc_size(ip, alloc_blocks, shift,
  454. mp->m_writeio_blocks);
  455. return alloc_blocks;
  456. }
  457. static int
  458. xfs_file_iomap_begin_delay(
  459. struct inode *inode,
  460. loff_t offset,
  461. loff_t count,
  462. struct iomap *iomap)
  463. {
  464. struct xfs_inode *ip = XFS_I(inode);
  465. struct xfs_mount *mp = ip->i_mount;
  466. struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
  467. xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset);
  468. xfs_fileoff_t maxbytes_fsb =
  469. XFS_B_TO_FSB(mp, mp->m_super->s_maxbytes);
  470. xfs_fileoff_t end_fsb;
  471. int error = 0, eof = 0;
  472. struct xfs_bmbt_irec got;
  473. struct xfs_iext_cursor icur;
  474. xfs_fsblock_t prealloc_blocks = 0;
  475. ASSERT(!XFS_IS_REALTIME_INODE(ip));
  476. ASSERT(!xfs_get_extsz_hint(ip));
  477. xfs_ilock(ip, XFS_ILOCK_EXCL);
  478. if (unlikely(XFS_TEST_ERROR(
  479. (XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_EXTENTS &&
  480. XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_BTREE),
  481. mp, XFS_ERRTAG_BMAPIFORMAT))) {
  482. XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp);
  483. error = -EFSCORRUPTED;
  484. goto out_unlock;
  485. }
  486. XFS_STATS_INC(mp, xs_blk_mapw);
  487. if (!(ifp->if_flags & XFS_IFEXTENTS)) {
  488. error = xfs_iread_extents(NULL, ip, XFS_DATA_FORK);
  489. if (error)
  490. goto out_unlock;
  491. }
  492. eof = !xfs_iext_lookup_extent(ip, ifp, offset_fsb, &icur, &got);
  493. if (!eof && got.br_startoff <= offset_fsb) {
  494. if (xfs_is_reflink_inode(ip)) {
  495. bool shared;
  496. end_fsb = min(XFS_B_TO_FSB(mp, offset + count),
  497. maxbytes_fsb);
  498. xfs_trim_extent(&got, offset_fsb, end_fsb - offset_fsb);
  499. error = xfs_reflink_reserve_cow(ip, &got, &shared);
  500. if (error)
  501. goto out_unlock;
  502. }
  503. trace_xfs_iomap_found(ip, offset, count, 0, &got);
  504. goto done;
  505. }
  506. error = xfs_qm_dqattach_locked(ip, false);
  507. if (error)
  508. goto out_unlock;
  509. /*
  510. * We cap the maximum length we map here to MAX_WRITEBACK_PAGES pages
  511. * to keep the chunks of work done where somewhat symmetric with the
  512. * work writeback does. This is a completely arbitrary number pulled
  513. * out of thin air as a best guess for initial testing.
  514. *
  515. * Note that the values needs to be less than 32-bits wide until
  516. * the lower level functions are updated.
  517. */
  518. count = min_t(loff_t, count, 1024 * PAGE_SIZE);
  519. end_fsb = min(XFS_B_TO_FSB(mp, offset + count), maxbytes_fsb);
  520. if (eof) {
  521. prealloc_blocks = xfs_iomap_prealloc_size(ip, offset, count,
  522. &icur);
  523. if (prealloc_blocks) {
  524. xfs_extlen_t align;
  525. xfs_off_t end_offset;
  526. xfs_fileoff_t p_end_fsb;
  527. end_offset = XFS_WRITEIO_ALIGN(mp, offset + count - 1);
  528. p_end_fsb = XFS_B_TO_FSBT(mp, end_offset) +
  529. prealloc_blocks;
  530. align = xfs_eof_alignment(ip, 0);
  531. if (align)
  532. p_end_fsb = roundup_64(p_end_fsb, align);
  533. p_end_fsb = min(p_end_fsb, maxbytes_fsb);
  534. ASSERT(p_end_fsb > offset_fsb);
  535. prealloc_blocks = p_end_fsb - end_fsb;
  536. }
  537. }
  538. retry:
  539. error = xfs_bmapi_reserve_delalloc(ip, XFS_DATA_FORK, offset_fsb,
  540. end_fsb - offset_fsb, prealloc_blocks, &got, &icur,
  541. eof);
  542. switch (error) {
  543. case 0:
  544. break;
  545. case -ENOSPC:
  546. case -EDQUOT:
  547. /* retry without any preallocation */
  548. trace_xfs_delalloc_enospc(ip, offset, count);
  549. if (prealloc_blocks) {
  550. prealloc_blocks = 0;
  551. goto retry;
  552. }
  553. /*FALLTHRU*/
  554. default:
  555. goto out_unlock;
  556. }
  557. /*
  558. * Flag newly allocated delalloc blocks with IOMAP_F_NEW so we punch
  559. * them out if the write happens to fail.
  560. */
  561. iomap->flags = IOMAP_F_NEW;
  562. trace_xfs_iomap_alloc(ip, offset, count, 0, &got);
  563. done:
  564. if (isnullstartblock(got.br_startblock))
  565. got.br_startblock = DELAYSTARTBLOCK;
  566. if (!got.br_startblock) {
  567. error = xfs_alert_fsblock_zero(ip, &got);
  568. if (error)
  569. goto out_unlock;
  570. }
  571. xfs_bmbt_to_iomap(ip, iomap, &got);
  572. out_unlock:
  573. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  574. return error;
  575. }
  576. /*
  577. * Pass in a delayed allocate extent, convert it to real extents;
  578. * return to the caller the extent we create which maps on top of
  579. * the originating callers request.
  580. *
  581. * Called without a lock on the inode.
  582. *
  583. * We no longer bother to look at the incoming map - all we have to
  584. * guarantee is that whatever we allocate fills the required range.
  585. */
  586. int
  587. xfs_iomap_write_allocate(
  588. xfs_inode_t *ip,
  589. int whichfork,
  590. xfs_off_t offset,
  591. xfs_bmbt_irec_t *imap)
  592. {
  593. xfs_mount_t *mp = ip->i_mount;
  594. xfs_fileoff_t offset_fsb, last_block;
  595. xfs_fileoff_t end_fsb, map_start_fsb;
  596. xfs_fsblock_t first_block;
  597. struct xfs_defer_ops dfops;
  598. xfs_filblks_t count_fsb;
  599. xfs_trans_t *tp;
  600. int nimaps;
  601. int error = 0;
  602. int flags = XFS_BMAPI_DELALLOC;
  603. int nres;
  604. if (whichfork == XFS_COW_FORK)
  605. flags |= XFS_BMAPI_COWFORK | XFS_BMAPI_PREALLOC;
  606. /*
  607. * Make sure that the dquots are there.
  608. */
  609. error = xfs_qm_dqattach(ip);
  610. if (error)
  611. return error;
  612. offset_fsb = XFS_B_TO_FSBT(mp, offset);
  613. count_fsb = imap->br_blockcount;
  614. map_start_fsb = imap->br_startoff;
  615. XFS_STATS_ADD(mp, xs_xstrat_bytes, XFS_FSB_TO_B(mp, count_fsb));
  616. while (count_fsb != 0) {
  617. /*
  618. * Set up a transaction with which to allocate the
  619. * backing store for the file. Do allocations in a
  620. * loop until we get some space in the range we are
  621. * interested in. The other space that might be allocated
  622. * is in the delayed allocation extent on which we sit
  623. * but before our buffer starts.
  624. */
  625. nimaps = 0;
  626. while (nimaps == 0) {
  627. nres = XFS_EXTENTADD_SPACE_RES(mp, XFS_DATA_FORK);
  628. /*
  629. * We have already reserved space for the extent and any
  630. * indirect blocks when creating the delalloc extent,
  631. * there is no need to reserve space in this transaction
  632. * again.
  633. */
  634. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, 0,
  635. 0, XFS_TRANS_RESERVE, &tp);
  636. if (error)
  637. return error;
  638. xfs_ilock(ip, XFS_ILOCK_EXCL);
  639. xfs_trans_ijoin(tp, ip, 0);
  640. xfs_defer_init(&dfops, &first_block);
  641. /*
  642. * it is possible that the extents have changed since
  643. * we did the read call as we dropped the ilock for a
  644. * while. We have to be careful about truncates or hole
  645. * punchs here - we are not allowed to allocate
  646. * non-delalloc blocks here.
  647. *
  648. * The only protection against truncation is the pages
  649. * for the range we are being asked to convert are
  650. * locked and hence a truncate will block on them
  651. * first.
  652. *
  653. * As a result, if we go beyond the range we really
  654. * need and hit an delalloc extent boundary followed by
  655. * a hole while we have excess blocks in the map, we
  656. * will fill the hole incorrectly and overrun the
  657. * transaction reservation.
  658. *
  659. * Using a single map prevents this as we are forced to
  660. * check each map we look for overlap with the desired
  661. * range and abort as soon as we find it. Also, given
  662. * that we only return a single map, having one beyond
  663. * what we can return is probably a bit silly.
  664. *
  665. * We also need to check that we don't go beyond EOF;
  666. * this is a truncate optimisation as a truncate sets
  667. * the new file size before block on the pages we
  668. * currently have locked under writeback. Because they
  669. * are about to be tossed, we don't need to write them
  670. * back....
  671. */
  672. nimaps = 1;
  673. end_fsb = XFS_B_TO_FSB(mp, XFS_ISIZE(ip));
  674. error = xfs_bmap_last_offset(ip, &last_block,
  675. XFS_DATA_FORK);
  676. if (error)
  677. goto trans_cancel;
  678. last_block = XFS_FILEOFF_MAX(last_block, end_fsb);
  679. if ((map_start_fsb + count_fsb) > last_block) {
  680. count_fsb = last_block - map_start_fsb;
  681. if (count_fsb == 0) {
  682. error = -EAGAIN;
  683. goto trans_cancel;
  684. }
  685. }
  686. /*
  687. * From this point onwards we overwrite the imap
  688. * pointer that the caller gave to us.
  689. */
  690. error = xfs_bmapi_write(tp, ip, map_start_fsb,
  691. count_fsb, flags, &first_block,
  692. nres, imap, &nimaps,
  693. &dfops);
  694. if (error)
  695. goto trans_cancel;
  696. error = xfs_defer_finish(&tp, &dfops);
  697. if (error)
  698. goto trans_cancel;
  699. error = xfs_trans_commit(tp);
  700. if (error)
  701. goto error0;
  702. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  703. }
  704. /*
  705. * See if we were able to allocate an extent that
  706. * covers at least part of the callers request
  707. */
  708. if (!(imap->br_startblock || XFS_IS_REALTIME_INODE(ip)))
  709. return xfs_alert_fsblock_zero(ip, imap);
  710. if ((offset_fsb >= imap->br_startoff) &&
  711. (offset_fsb < (imap->br_startoff +
  712. imap->br_blockcount))) {
  713. XFS_STATS_INC(mp, xs_xstrat_quick);
  714. return 0;
  715. }
  716. /*
  717. * So far we have not mapped the requested part of the
  718. * file, just surrounding data, try again.
  719. */
  720. count_fsb -= imap->br_blockcount;
  721. map_start_fsb = imap->br_startoff + imap->br_blockcount;
  722. }
  723. trans_cancel:
  724. xfs_defer_cancel(&dfops);
  725. xfs_trans_cancel(tp);
  726. error0:
  727. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  728. return error;
  729. }
  730. int
  731. xfs_iomap_write_unwritten(
  732. xfs_inode_t *ip,
  733. xfs_off_t offset,
  734. xfs_off_t count,
  735. bool update_isize)
  736. {
  737. xfs_mount_t *mp = ip->i_mount;
  738. xfs_fileoff_t offset_fsb;
  739. xfs_filblks_t count_fsb;
  740. xfs_filblks_t numblks_fsb;
  741. xfs_fsblock_t firstfsb;
  742. int nimaps;
  743. xfs_trans_t *tp;
  744. xfs_bmbt_irec_t imap;
  745. struct xfs_defer_ops dfops;
  746. struct inode *inode = VFS_I(ip);
  747. xfs_fsize_t i_size;
  748. uint resblks;
  749. int error;
  750. trace_xfs_unwritten_convert(ip, offset, count);
  751. offset_fsb = XFS_B_TO_FSBT(mp, offset);
  752. count_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)offset + count);
  753. count_fsb = (xfs_filblks_t)(count_fsb - offset_fsb);
  754. /*
  755. * Reserve enough blocks in this transaction for two complete extent
  756. * btree splits. We may be converting the middle part of an unwritten
  757. * extent and in this case we will insert two new extents in the btree
  758. * each of which could cause a full split.
  759. *
  760. * This reservation amount will be used in the first call to
  761. * xfs_bmbt_split() to select an AG with enough space to satisfy the
  762. * rest of the operation.
  763. */
  764. resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0) << 1;
  765. do {
  766. /*
  767. * Set up a transaction to convert the range of extents
  768. * from unwritten to real. Do allocations in a loop until
  769. * we have covered the range passed in.
  770. *
  771. * Note that we can't risk to recursing back into the filesystem
  772. * here as we might be asked to write out the same inode that we
  773. * complete here and might deadlock on the iolock.
  774. */
  775. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, resblks, 0,
  776. XFS_TRANS_RESERVE | XFS_TRANS_NOFS, &tp);
  777. if (error)
  778. return error;
  779. xfs_ilock(ip, XFS_ILOCK_EXCL);
  780. xfs_trans_ijoin(tp, ip, 0);
  781. /*
  782. * Modify the unwritten extent state of the buffer.
  783. */
  784. xfs_defer_init(&dfops, &firstfsb);
  785. nimaps = 1;
  786. error = xfs_bmapi_write(tp, ip, offset_fsb, count_fsb,
  787. XFS_BMAPI_CONVERT, &firstfsb, resblks,
  788. &imap, &nimaps, &dfops);
  789. if (error)
  790. goto error_on_bmapi_transaction;
  791. /*
  792. * Log the updated inode size as we go. We have to be careful
  793. * to only log it up to the actual write offset if it is
  794. * halfway into a block.
  795. */
  796. i_size = XFS_FSB_TO_B(mp, offset_fsb + count_fsb);
  797. if (i_size > offset + count)
  798. i_size = offset + count;
  799. if (update_isize && i_size > i_size_read(inode))
  800. i_size_write(inode, i_size);
  801. i_size = xfs_new_eof(ip, i_size);
  802. if (i_size) {
  803. ip->i_d.di_size = i_size;
  804. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  805. }
  806. error = xfs_defer_finish(&tp, &dfops);
  807. if (error)
  808. goto error_on_bmapi_transaction;
  809. error = xfs_trans_commit(tp);
  810. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  811. if (error)
  812. return error;
  813. if (!(imap.br_startblock || XFS_IS_REALTIME_INODE(ip)))
  814. return xfs_alert_fsblock_zero(ip, &imap);
  815. if ((numblks_fsb = imap.br_blockcount) == 0) {
  816. /*
  817. * The numblks_fsb value should always get
  818. * smaller, otherwise the loop is stuck.
  819. */
  820. ASSERT(imap.br_blockcount);
  821. break;
  822. }
  823. offset_fsb += numblks_fsb;
  824. count_fsb -= numblks_fsb;
  825. } while (count_fsb > 0);
  826. return 0;
  827. error_on_bmapi_transaction:
  828. xfs_defer_cancel(&dfops);
  829. xfs_trans_cancel(tp);
  830. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  831. return error;
  832. }
  833. static inline bool
  834. imap_needs_alloc(
  835. struct inode *inode,
  836. struct xfs_bmbt_irec *imap,
  837. int nimaps)
  838. {
  839. return !nimaps ||
  840. imap->br_startblock == HOLESTARTBLOCK ||
  841. imap->br_startblock == DELAYSTARTBLOCK ||
  842. (IS_DAX(inode) && imap->br_state == XFS_EXT_UNWRITTEN);
  843. }
  844. static inline bool
  845. needs_cow_for_zeroing(
  846. struct xfs_bmbt_irec *imap,
  847. int nimaps)
  848. {
  849. return nimaps &&
  850. imap->br_startblock != HOLESTARTBLOCK &&
  851. imap->br_state != XFS_EXT_UNWRITTEN;
  852. }
  853. static int
  854. xfs_ilock_for_iomap(
  855. struct xfs_inode *ip,
  856. unsigned flags,
  857. unsigned *lockmode)
  858. {
  859. unsigned mode = XFS_ILOCK_SHARED;
  860. /*
  861. * COW writes may allocate delalloc space or convert unwritten COW
  862. * extents, so we need to make sure to take the lock exclusively here.
  863. */
  864. if (xfs_is_reflink_inode(ip) && (flags & (IOMAP_WRITE | IOMAP_ZERO))) {
  865. /*
  866. * FIXME: It could still overwrite on unshared extents and not
  867. * need allocation.
  868. */
  869. if (flags & IOMAP_NOWAIT)
  870. return -EAGAIN;
  871. mode = XFS_ILOCK_EXCL;
  872. }
  873. /*
  874. * Extents not yet cached requires exclusive access, don't block. This
  875. * is an opencoded xfs_ilock_data_map_shared() call but with
  876. * non-blocking behaviour.
  877. */
  878. if (!(ip->i_df.if_flags & XFS_IFEXTENTS)) {
  879. if (flags & IOMAP_NOWAIT)
  880. return -EAGAIN;
  881. mode = XFS_ILOCK_EXCL;
  882. }
  883. if (flags & IOMAP_NOWAIT) {
  884. if (!xfs_ilock_nowait(ip, mode))
  885. return -EAGAIN;
  886. } else {
  887. xfs_ilock(ip, mode);
  888. }
  889. *lockmode = mode;
  890. return 0;
  891. }
  892. static int
  893. xfs_file_iomap_begin(
  894. struct inode *inode,
  895. loff_t offset,
  896. loff_t length,
  897. unsigned flags,
  898. struct iomap *iomap)
  899. {
  900. struct xfs_inode *ip = XFS_I(inode);
  901. struct xfs_mount *mp = ip->i_mount;
  902. struct xfs_bmbt_irec imap;
  903. xfs_fileoff_t offset_fsb, end_fsb;
  904. int nimaps = 1, error = 0;
  905. bool shared = false, trimmed = false;
  906. unsigned lockmode;
  907. if (XFS_FORCED_SHUTDOWN(mp))
  908. return -EIO;
  909. if (((flags & (IOMAP_WRITE | IOMAP_DIRECT)) == IOMAP_WRITE) &&
  910. !IS_DAX(inode) && !xfs_get_extsz_hint(ip)) {
  911. /* Reserve delalloc blocks for regular writeback. */
  912. return xfs_file_iomap_begin_delay(inode, offset, length, iomap);
  913. }
  914. /*
  915. * Lock the inode in the manner required for the specified operation and
  916. * check for as many conditions that would result in blocking as
  917. * possible. This removes most of the non-blocking checks from the
  918. * mapping code below.
  919. */
  920. error = xfs_ilock_for_iomap(ip, flags, &lockmode);
  921. if (error)
  922. return error;
  923. ASSERT(offset <= mp->m_super->s_maxbytes);
  924. if (offset > mp->m_super->s_maxbytes - length)
  925. length = mp->m_super->s_maxbytes - offset;
  926. offset_fsb = XFS_B_TO_FSBT(mp, offset);
  927. end_fsb = XFS_B_TO_FSB(mp, offset + length);
  928. error = xfs_bmapi_read(ip, offset_fsb, end_fsb - offset_fsb, &imap,
  929. &nimaps, 0);
  930. if (error)
  931. goto out_unlock;
  932. if (flags & IOMAP_REPORT) {
  933. /* Trim the mapping to the nearest shared extent boundary. */
  934. error = xfs_reflink_trim_around_shared(ip, &imap, &shared,
  935. &trimmed);
  936. if (error)
  937. goto out_unlock;
  938. }
  939. /* Non-modifying mapping requested, so we are done */
  940. if (!(flags & (IOMAP_WRITE | IOMAP_ZERO)))
  941. goto out_found;
  942. /*
  943. * Break shared extents if necessary. Checks for non-blocking IO have
  944. * been done up front, so we don't need to do them here.
  945. */
  946. if (xfs_is_reflink_inode(ip)) {
  947. /* if zeroing doesn't need COW allocation, then we are done. */
  948. if ((flags & IOMAP_ZERO) &&
  949. !needs_cow_for_zeroing(&imap, nimaps))
  950. goto out_found;
  951. if (flags & IOMAP_DIRECT) {
  952. /* may drop and re-acquire the ilock */
  953. error = xfs_reflink_allocate_cow(ip, &imap, &shared,
  954. &lockmode);
  955. if (error)
  956. goto out_unlock;
  957. } else {
  958. error = xfs_reflink_reserve_cow(ip, &imap, &shared);
  959. if (error)
  960. goto out_unlock;
  961. }
  962. end_fsb = imap.br_startoff + imap.br_blockcount;
  963. length = XFS_FSB_TO_B(mp, end_fsb) - offset;
  964. }
  965. /* Don't need to allocate over holes when doing zeroing operations. */
  966. if (flags & IOMAP_ZERO)
  967. goto out_found;
  968. if (!imap_needs_alloc(inode, &imap, nimaps))
  969. goto out_found;
  970. /* If nowait is set bail since we are going to make allocations. */
  971. if (flags & IOMAP_NOWAIT) {
  972. error = -EAGAIN;
  973. goto out_unlock;
  974. }
  975. /*
  976. * We cap the maximum length we map to a sane size to keep the chunks
  977. * of work done where somewhat symmetric with the work writeback does.
  978. * This is a completely arbitrary number pulled out of thin air as a
  979. * best guess for initial testing.
  980. *
  981. * Note that the values needs to be less than 32-bits wide until the
  982. * lower level functions are updated.
  983. */
  984. length = min_t(loff_t, length, 1024 * PAGE_SIZE);
  985. /*
  986. * xfs_iomap_write_direct() expects the shared lock. It is unlocked on
  987. * return.
  988. */
  989. if (lockmode == XFS_ILOCK_EXCL)
  990. xfs_ilock_demote(ip, lockmode);
  991. error = xfs_iomap_write_direct(ip, offset, length, &imap,
  992. nimaps);
  993. if (error)
  994. return error;
  995. iomap->flags = IOMAP_F_NEW;
  996. trace_xfs_iomap_alloc(ip, offset, length, 0, &imap);
  997. out_finish:
  998. if (xfs_ipincount(ip) && (ip->i_itemp->ili_fsync_fields
  999. & ~XFS_ILOG_TIMESTAMP))
  1000. iomap->flags |= IOMAP_F_DIRTY;
  1001. xfs_bmbt_to_iomap(ip, iomap, &imap);
  1002. if (shared)
  1003. iomap->flags |= IOMAP_F_SHARED;
  1004. return 0;
  1005. out_found:
  1006. ASSERT(nimaps);
  1007. xfs_iunlock(ip, lockmode);
  1008. trace_xfs_iomap_found(ip, offset, length, 0, &imap);
  1009. goto out_finish;
  1010. out_unlock:
  1011. xfs_iunlock(ip, lockmode);
  1012. return error;
  1013. }
  1014. static int
  1015. xfs_file_iomap_end_delalloc(
  1016. struct xfs_inode *ip,
  1017. loff_t offset,
  1018. loff_t length,
  1019. ssize_t written,
  1020. struct iomap *iomap)
  1021. {
  1022. struct xfs_mount *mp = ip->i_mount;
  1023. xfs_fileoff_t start_fsb;
  1024. xfs_fileoff_t end_fsb;
  1025. int error = 0;
  1026. /*
  1027. * Behave as if the write failed if drop writes is enabled. Set the NEW
  1028. * flag to force delalloc cleanup.
  1029. */
  1030. if (XFS_TEST_ERROR(false, mp, XFS_ERRTAG_DROP_WRITES)) {
  1031. iomap->flags |= IOMAP_F_NEW;
  1032. written = 0;
  1033. }
  1034. /*
  1035. * start_fsb refers to the first unused block after a short write. If
  1036. * nothing was written, round offset down to point at the first block in
  1037. * the range.
  1038. */
  1039. if (unlikely(!written))
  1040. start_fsb = XFS_B_TO_FSBT(mp, offset);
  1041. else
  1042. start_fsb = XFS_B_TO_FSB(mp, offset + written);
  1043. end_fsb = XFS_B_TO_FSB(mp, offset + length);
  1044. /*
  1045. * Trim delalloc blocks if they were allocated by this write and we
  1046. * didn't manage to write the whole range.
  1047. *
  1048. * We don't need to care about racing delalloc as we hold i_mutex
  1049. * across the reserve/allocate/unreserve calls. If there are delalloc
  1050. * blocks in the range, they are ours.
  1051. */
  1052. if ((iomap->flags & IOMAP_F_NEW) && start_fsb < end_fsb) {
  1053. truncate_pagecache_range(VFS_I(ip), XFS_FSB_TO_B(mp, start_fsb),
  1054. XFS_FSB_TO_B(mp, end_fsb) - 1);
  1055. xfs_ilock(ip, XFS_ILOCK_EXCL);
  1056. error = xfs_bmap_punch_delalloc_range(ip, start_fsb,
  1057. end_fsb - start_fsb);
  1058. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1059. if (error && !XFS_FORCED_SHUTDOWN(mp)) {
  1060. xfs_alert(mp, "%s: unable to clean up ino %lld",
  1061. __func__, ip->i_ino);
  1062. return error;
  1063. }
  1064. }
  1065. return 0;
  1066. }
  1067. static int
  1068. xfs_file_iomap_end(
  1069. struct inode *inode,
  1070. loff_t offset,
  1071. loff_t length,
  1072. ssize_t written,
  1073. unsigned flags,
  1074. struct iomap *iomap)
  1075. {
  1076. if ((flags & IOMAP_WRITE) && iomap->type == IOMAP_DELALLOC)
  1077. return xfs_file_iomap_end_delalloc(XFS_I(inode), offset,
  1078. length, written, iomap);
  1079. return 0;
  1080. }
  1081. const struct iomap_ops xfs_iomap_ops = {
  1082. .iomap_begin = xfs_file_iomap_begin,
  1083. .iomap_end = xfs_file_iomap_end,
  1084. };
  1085. static int
  1086. xfs_xattr_iomap_begin(
  1087. struct inode *inode,
  1088. loff_t offset,
  1089. loff_t length,
  1090. unsigned flags,
  1091. struct iomap *iomap)
  1092. {
  1093. struct xfs_inode *ip = XFS_I(inode);
  1094. struct xfs_mount *mp = ip->i_mount;
  1095. xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset);
  1096. xfs_fileoff_t end_fsb = XFS_B_TO_FSB(mp, offset + length);
  1097. struct xfs_bmbt_irec imap;
  1098. int nimaps = 1, error = 0;
  1099. unsigned lockmode;
  1100. if (XFS_FORCED_SHUTDOWN(mp))
  1101. return -EIO;
  1102. lockmode = xfs_ilock_attr_map_shared(ip);
  1103. /* if there are no attribute fork or extents, return ENOENT */
  1104. if (!XFS_IFORK_Q(ip) || !ip->i_d.di_anextents) {
  1105. error = -ENOENT;
  1106. goto out_unlock;
  1107. }
  1108. ASSERT(ip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL);
  1109. error = xfs_bmapi_read(ip, offset_fsb, end_fsb - offset_fsb, &imap,
  1110. &nimaps, XFS_BMAPI_ATTRFORK);
  1111. out_unlock:
  1112. xfs_iunlock(ip, lockmode);
  1113. if (!error) {
  1114. ASSERT(nimaps);
  1115. xfs_bmbt_to_iomap(ip, iomap, &imap);
  1116. }
  1117. return error;
  1118. }
  1119. const struct iomap_ops xfs_xattr_iomap_ops = {
  1120. .iomap_begin = xfs_xattr_iomap_begin,
  1121. };