xfs_iomap.c 34 KB

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