xfs_iomap.c 33 KB

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