xfs_iomap.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222
  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. iomap->dax_dev = xfs_find_daxdev_for_inode(VFS_I(ip));
  69. }
  70. xfs_extlen_t
  71. xfs_eof_alignment(
  72. struct xfs_inode *ip,
  73. xfs_extlen_t extsize)
  74. {
  75. struct xfs_mount *mp = ip->i_mount;
  76. xfs_extlen_t align = 0;
  77. if (!XFS_IS_REALTIME_INODE(ip)) {
  78. /*
  79. * Round up the allocation request to a stripe unit
  80. * (m_dalign) boundary if the file size is >= stripe unit
  81. * size, and we are allocating past the allocation eof.
  82. *
  83. * If mounted with the "-o swalloc" option the alignment is
  84. * increased from the strip unit size to the stripe width.
  85. */
  86. if (mp->m_swidth && (mp->m_flags & XFS_MOUNT_SWALLOC))
  87. align = mp->m_swidth;
  88. else if (mp->m_dalign)
  89. align = mp->m_dalign;
  90. if (align && XFS_ISIZE(ip) < XFS_FSB_TO_B(mp, align))
  91. align = 0;
  92. }
  93. /*
  94. * Always round up the allocation request to an extent boundary
  95. * (when file on a real-time subvolume or has di_extsize hint).
  96. */
  97. if (extsize) {
  98. if (align)
  99. align = roundup_64(align, extsize);
  100. else
  101. align = extsize;
  102. }
  103. return align;
  104. }
  105. STATIC int
  106. xfs_iomap_eof_align_last_fsb(
  107. struct xfs_inode *ip,
  108. xfs_extlen_t extsize,
  109. xfs_fileoff_t *last_fsb)
  110. {
  111. xfs_extlen_t align = xfs_eof_alignment(ip, extsize);
  112. if (align) {
  113. xfs_fileoff_t new_last_fsb = roundup_64(*last_fsb, align);
  114. int eof, error;
  115. error = xfs_bmap_eof(ip, new_last_fsb, XFS_DATA_FORK, &eof);
  116. if (error)
  117. return error;
  118. if (eof)
  119. *last_fsb = new_last_fsb;
  120. }
  121. return 0;
  122. }
  123. STATIC int
  124. xfs_alert_fsblock_zero(
  125. xfs_inode_t *ip,
  126. xfs_bmbt_irec_t *imap)
  127. {
  128. xfs_alert_tag(ip->i_mount, XFS_PTAG_FSBLOCK_ZERO,
  129. "Access to block zero in inode %llu "
  130. "start_block: %llx start_off: %llx "
  131. "blkcnt: %llx extent-state: %x",
  132. (unsigned long long)ip->i_ino,
  133. (unsigned long long)imap->br_startblock,
  134. (unsigned long long)imap->br_startoff,
  135. (unsigned long long)imap->br_blockcount,
  136. imap->br_state);
  137. return -EFSCORRUPTED;
  138. }
  139. int
  140. xfs_iomap_write_direct(
  141. xfs_inode_t *ip,
  142. xfs_off_t offset,
  143. size_t count,
  144. xfs_bmbt_irec_t *imap,
  145. int nmaps)
  146. {
  147. xfs_mount_t *mp = ip->i_mount;
  148. xfs_fileoff_t offset_fsb;
  149. xfs_fileoff_t last_fsb;
  150. xfs_filblks_t count_fsb, resaligned;
  151. xfs_fsblock_t firstfsb;
  152. xfs_extlen_t extsz;
  153. int nimaps;
  154. int quota_flag;
  155. int rt;
  156. xfs_trans_t *tp;
  157. struct xfs_defer_ops dfops;
  158. uint qblocks, resblks, resrtextents;
  159. int error;
  160. int lockmode;
  161. int bmapi_flags = XFS_BMAPI_PREALLOC;
  162. uint tflags = 0;
  163. rt = XFS_IS_REALTIME_INODE(ip);
  164. extsz = xfs_get_extsz_hint(ip);
  165. lockmode = XFS_ILOCK_SHARED; /* locked by caller */
  166. ASSERT(xfs_isilocked(ip, lockmode));
  167. offset_fsb = XFS_B_TO_FSBT(mp, offset);
  168. last_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)(offset + count)));
  169. if ((offset + count) > XFS_ISIZE(ip)) {
  170. /*
  171. * Assert that the in-core extent list is present since this can
  172. * call xfs_iread_extents() and we only have the ilock shared.
  173. * This should be safe because the lock was held around a bmapi
  174. * call in the caller and we only need it to access the in-core
  175. * list.
  176. */
  177. ASSERT(XFS_IFORK_PTR(ip, XFS_DATA_FORK)->if_flags &
  178. XFS_IFEXTENTS);
  179. error = xfs_iomap_eof_align_last_fsb(ip, extsz, &last_fsb);
  180. if (error)
  181. goto out_unlock;
  182. } else {
  183. if (nmaps && (imap->br_startblock == HOLESTARTBLOCK))
  184. last_fsb = MIN(last_fsb, (xfs_fileoff_t)
  185. imap->br_blockcount +
  186. imap->br_startoff);
  187. }
  188. count_fsb = last_fsb - offset_fsb;
  189. ASSERT(count_fsb > 0);
  190. resaligned = xfs_aligned_fsb_count(offset_fsb, count_fsb, extsz);
  191. if (unlikely(rt)) {
  192. resrtextents = qblocks = resaligned;
  193. resrtextents /= mp->m_sb.sb_rextsize;
  194. resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
  195. quota_flag = XFS_QMOPT_RES_RTBLKS;
  196. } else {
  197. resrtextents = 0;
  198. resblks = qblocks = XFS_DIOSTRAT_SPACE_RES(mp, resaligned);
  199. quota_flag = XFS_QMOPT_RES_REGBLKS;
  200. }
  201. /*
  202. * Drop the shared lock acquired by the caller, attach the dquot if
  203. * necessary and move on to transaction setup.
  204. */
  205. xfs_iunlock(ip, lockmode);
  206. error = xfs_qm_dqattach(ip, 0);
  207. if (error)
  208. return error;
  209. /*
  210. * For DAX, we do not allocate unwritten extents, but instead we zero
  211. * the block before we commit the transaction. Ideally we'd like to do
  212. * this outside the transaction context, but if we commit and then crash
  213. * we may not have zeroed the blocks and this will be exposed on
  214. * recovery of the allocation. Hence we must zero before commit.
  215. *
  216. * Further, if we are mapping unwritten extents here, we need to zero
  217. * and convert them to written so that we don't need an unwritten extent
  218. * callback for DAX. This also means that we need to be able to dip into
  219. * the reserve block pool for bmbt block allocation if there is no space
  220. * left but we need to do unwritten extent conversion.
  221. */
  222. if (IS_DAX(VFS_I(ip))) {
  223. bmapi_flags = XFS_BMAPI_CONVERT | XFS_BMAPI_ZERO;
  224. if (imap->br_state == XFS_EXT_UNWRITTEN) {
  225. tflags |= XFS_TRANS_RESERVE;
  226. resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0) << 1;
  227. }
  228. }
  229. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, resblks, resrtextents,
  230. tflags, &tp);
  231. if (error)
  232. return error;
  233. lockmode = XFS_ILOCK_EXCL;
  234. xfs_ilock(ip, lockmode);
  235. error = xfs_trans_reserve_quota_nblks(tp, ip, qblocks, 0, quota_flag);
  236. if (error)
  237. goto out_trans_cancel;
  238. xfs_trans_ijoin(tp, ip, 0);
  239. /*
  240. * From this point onwards we overwrite the imap pointer that the
  241. * caller gave to us.
  242. */
  243. xfs_defer_init(&dfops, &firstfsb);
  244. nimaps = 1;
  245. error = xfs_bmapi_write(tp, ip, offset_fsb, count_fsb,
  246. bmapi_flags, &firstfsb, resblks, imap,
  247. &nimaps, &dfops);
  248. if (error)
  249. goto out_bmap_cancel;
  250. /*
  251. * Complete the transaction
  252. */
  253. error = xfs_defer_finish(&tp, &dfops);
  254. if (error)
  255. goto out_bmap_cancel;
  256. error = xfs_trans_commit(tp);
  257. if (error)
  258. goto out_unlock;
  259. /*
  260. * Copy any maps to caller's array and return any error.
  261. */
  262. if (nimaps == 0) {
  263. error = -ENOSPC;
  264. goto out_unlock;
  265. }
  266. if (!(imap->br_startblock || XFS_IS_REALTIME_INODE(ip)))
  267. error = xfs_alert_fsblock_zero(ip, imap);
  268. out_unlock:
  269. xfs_iunlock(ip, lockmode);
  270. return error;
  271. out_bmap_cancel:
  272. xfs_defer_cancel(&dfops);
  273. xfs_trans_unreserve_quota_nblks(tp, ip, (long)qblocks, 0, quota_flag);
  274. out_trans_cancel:
  275. xfs_trans_cancel(tp);
  276. goto out_unlock;
  277. }
  278. STATIC bool
  279. xfs_quota_need_throttle(
  280. struct xfs_inode *ip,
  281. int type,
  282. xfs_fsblock_t alloc_blocks)
  283. {
  284. struct xfs_dquot *dq = xfs_inode_dquot(ip, type);
  285. if (!dq || !xfs_this_quota_on(ip->i_mount, type))
  286. return false;
  287. /* no hi watermark, no throttle */
  288. if (!dq->q_prealloc_hi_wmark)
  289. return false;
  290. /* under the lo watermark, no throttle */
  291. if (dq->q_res_bcount + alloc_blocks < dq->q_prealloc_lo_wmark)
  292. return false;
  293. return true;
  294. }
  295. STATIC void
  296. xfs_quota_calc_throttle(
  297. struct xfs_inode *ip,
  298. int type,
  299. xfs_fsblock_t *qblocks,
  300. int *qshift,
  301. int64_t *qfreesp)
  302. {
  303. int64_t freesp;
  304. int shift = 0;
  305. struct xfs_dquot *dq = xfs_inode_dquot(ip, type);
  306. /* no dq, or over hi wmark, squash the prealloc completely */
  307. if (!dq || dq->q_res_bcount >= dq->q_prealloc_hi_wmark) {
  308. *qblocks = 0;
  309. *qfreesp = 0;
  310. return;
  311. }
  312. freesp = dq->q_prealloc_hi_wmark - dq->q_res_bcount;
  313. if (freesp < dq->q_low_space[XFS_QLOWSP_5_PCNT]) {
  314. shift = 2;
  315. if (freesp < dq->q_low_space[XFS_QLOWSP_3_PCNT])
  316. shift += 2;
  317. if (freesp < dq->q_low_space[XFS_QLOWSP_1_PCNT])
  318. shift += 2;
  319. }
  320. if (freesp < *qfreesp)
  321. *qfreesp = freesp;
  322. /* only overwrite the throttle values if we are more aggressive */
  323. if ((freesp >> shift) < (*qblocks >> *qshift)) {
  324. *qblocks = freesp;
  325. *qshift = shift;
  326. }
  327. }
  328. /*
  329. * If we are doing a write at the end of the file and there are no allocations
  330. * past this one, then extend the allocation out to the file system's write
  331. * iosize.
  332. *
  333. * If we don't have a user specified preallocation size, dynamically increase
  334. * the preallocation size as the size of the file grows. Cap the maximum size
  335. * at a single extent or less if the filesystem is near full. The closer the
  336. * filesystem is to full, the smaller the maximum prealocation.
  337. *
  338. * As an exception we don't do any preallocation at all if the file is smaller
  339. * than the minimum preallocation and we are using the default dynamic
  340. * preallocation scheme, as it is likely this is the only write to the file that
  341. * is going to be done.
  342. *
  343. * We clean up any extra space left over when the file is closed in
  344. * xfs_inactive().
  345. */
  346. STATIC xfs_fsblock_t
  347. xfs_iomap_prealloc_size(
  348. struct xfs_inode *ip,
  349. loff_t offset,
  350. loff_t count,
  351. xfs_extnum_t idx)
  352. {
  353. struct xfs_mount *mp = ip->i_mount;
  354. struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
  355. xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset);
  356. struct xfs_bmbt_irec prev;
  357. int shift = 0;
  358. int64_t freesp;
  359. xfs_fsblock_t qblocks;
  360. int qshift = 0;
  361. xfs_fsblock_t alloc_blocks = 0;
  362. if (offset + count <= XFS_ISIZE(ip))
  363. return 0;
  364. if (!(mp->m_flags & XFS_MOUNT_DFLT_IOSIZE) &&
  365. (XFS_ISIZE(ip) < XFS_FSB_TO_B(mp, mp->m_writeio_blocks)))
  366. return 0;
  367. /*
  368. * If an explicit allocsize is set, the file is small, or we
  369. * are writing behind a hole, then use the minimum prealloc:
  370. */
  371. if ((mp->m_flags & XFS_MOUNT_DFLT_IOSIZE) ||
  372. XFS_ISIZE(ip) < XFS_FSB_TO_B(mp, mp->m_dalign) ||
  373. !xfs_iext_get_extent(ifp, idx - 1, &prev) ||
  374. prev.br_startoff + prev.br_blockcount < offset_fsb)
  375. return mp->m_writeio_blocks;
  376. /*
  377. * Determine the initial size of the preallocation. We are beyond the
  378. * current EOF here, but we need to take into account whether this is
  379. * a sparse write or an extending write when determining the
  380. * preallocation size. Hence we need to look up the extent that ends
  381. * at the current write offset and use the result to determine the
  382. * preallocation size.
  383. *
  384. * If the extent is a hole, then preallocation is essentially disabled.
  385. * Otherwise we take the size of the preceding data extent as the basis
  386. * for the preallocation size. If the size of the extent is greater than
  387. * half the maximum extent length, then use the current offset as the
  388. * basis. This ensures that for large files the preallocation size
  389. * always extends to MAXEXTLEN rather than falling short due to things
  390. * like stripe unit/width alignment of real extents.
  391. */
  392. if (prev.br_blockcount <= (MAXEXTLEN >> 1))
  393. alloc_blocks = prev.br_blockcount << 1;
  394. else
  395. alloc_blocks = XFS_B_TO_FSB(mp, offset);
  396. if (!alloc_blocks)
  397. goto check_writeio;
  398. qblocks = alloc_blocks;
  399. /*
  400. * MAXEXTLEN is not a power of two value but we round the prealloc down
  401. * to the nearest power of two value after throttling. To prevent the
  402. * round down from unconditionally reducing the maximum supported prealloc
  403. * size, we round up first, apply appropriate throttling, round down and
  404. * cap the value to MAXEXTLEN.
  405. */
  406. alloc_blocks = XFS_FILEOFF_MIN(roundup_pow_of_two(MAXEXTLEN),
  407. alloc_blocks);
  408. freesp = percpu_counter_read_positive(&mp->m_fdblocks);
  409. if (freesp < mp->m_low_space[XFS_LOWSP_5_PCNT]) {
  410. shift = 2;
  411. if (freesp < mp->m_low_space[XFS_LOWSP_4_PCNT])
  412. shift++;
  413. if (freesp < mp->m_low_space[XFS_LOWSP_3_PCNT])
  414. shift++;
  415. if (freesp < mp->m_low_space[XFS_LOWSP_2_PCNT])
  416. shift++;
  417. if (freesp < mp->m_low_space[XFS_LOWSP_1_PCNT])
  418. shift++;
  419. }
  420. /*
  421. * Check each quota to cap the prealloc size, provide a shift value to
  422. * throttle with and adjust amount of available space.
  423. */
  424. if (xfs_quota_need_throttle(ip, XFS_DQ_USER, alloc_blocks))
  425. xfs_quota_calc_throttle(ip, XFS_DQ_USER, &qblocks, &qshift,
  426. &freesp);
  427. if (xfs_quota_need_throttle(ip, XFS_DQ_GROUP, alloc_blocks))
  428. xfs_quota_calc_throttle(ip, XFS_DQ_GROUP, &qblocks, &qshift,
  429. &freesp);
  430. if (xfs_quota_need_throttle(ip, XFS_DQ_PROJ, alloc_blocks))
  431. xfs_quota_calc_throttle(ip, XFS_DQ_PROJ, &qblocks, &qshift,
  432. &freesp);
  433. /*
  434. * The final prealloc size is set to the minimum of free space available
  435. * in each of the quotas and the overall filesystem.
  436. *
  437. * The shift throttle value is set to the maximum value as determined by
  438. * the global low free space values and per-quota low free space values.
  439. */
  440. alloc_blocks = MIN(alloc_blocks, qblocks);
  441. shift = MAX(shift, qshift);
  442. if (shift)
  443. alloc_blocks >>= shift;
  444. /*
  445. * rounddown_pow_of_two() returns an undefined result if we pass in
  446. * alloc_blocks = 0.
  447. */
  448. if (alloc_blocks)
  449. alloc_blocks = rounddown_pow_of_two(alloc_blocks);
  450. if (alloc_blocks > MAXEXTLEN)
  451. alloc_blocks = MAXEXTLEN;
  452. /*
  453. * If we are still trying to allocate more space than is
  454. * available, squash the prealloc hard. This can happen if we
  455. * have a large file on a small filesystem and the above
  456. * lowspace thresholds are smaller than MAXEXTLEN.
  457. */
  458. while (alloc_blocks && alloc_blocks >= freesp)
  459. alloc_blocks >>= 4;
  460. check_writeio:
  461. if (alloc_blocks < mp->m_writeio_blocks)
  462. alloc_blocks = mp->m_writeio_blocks;
  463. trace_xfs_iomap_prealloc_size(ip, alloc_blocks, shift,
  464. mp->m_writeio_blocks);
  465. return alloc_blocks;
  466. }
  467. static int
  468. xfs_file_iomap_begin_delay(
  469. struct inode *inode,
  470. loff_t offset,
  471. loff_t count,
  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))) {
  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);
  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. bool update_isize)
  744. {
  745. xfs_mount_t *mp = ip->i_mount;
  746. xfs_fileoff_t offset_fsb;
  747. xfs_filblks_t count_fsb;
  748. xfs_filblks_t numblks_fsb;
  749. xfs_fsblock_t firstfsb;
  750. int nimaps;
  751. xfs_trans_t *tp;
  752. xfs_bmbt_irec_t imap;
  753. struct xfs_defer_ops dfops;
  754. struct inode *inode = VFS_I(ip);
  755. xfs_fsize_t i_size;
  756. uint resblks;
  757. int error;
  758. trace_xfs_unwritten_convert(ip, offset, count);
  759. offset_fsb = XFS_B_TO_FSBT(mp, offset);
  760. count_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)offset + count);
  761. count_fsb = (xfs_filblks_t)(count_fsb - offset_fsb);
  762. /*
  763. * Reserve enough blocks in this transaction for two complete extent
  764. * btree splits. We may be converting the middle part of an unwritten
  765. * extent and in this case we will insert two new extents in the btree
  766. * each of which could cause a full split.
  767. *
  768. * This reservation amount will be used in the first call to
  769. * xfs_bmbt_split() to select an AG with enough space to satisfy the
  770. * rest of the operation.
  771. */
  772. resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0) << 1;
  773. do {
  774. /*
  775. * Set up a transaction to convert the range of extents
  776. * from unwritten to real. Do allocations in a loop until
  777. * we have covered the range passed in.
  778. *
  779. * Note that we can't risk to recursing back into the filesystem
  780. * here as we might be asked to write out the same inode that we
  781. * complete here and might deadlock on the iolock.
  782. */
  783. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, resblks, 0,
  784. XFS_TRANS_RESERVE | XFS_TRANS_NOFS, &tp);
  785. if (error)
  786. return error;
  787. xfs_ilock(ip, XFS_ILOCK_EXCL);
  788. xfs_trans_ijoin(tp, ip, 0);
  789. /*
  790. * Modify the unwritten extent state of the buffer.
  791. */
  792. xfs_defer_init(&dfops, &firstfsb);
  793. nimaps = 1;
  794. error = xfs_bmapi_write(tp, ip, offset_fsb, count_fsb,
  795. XFS_BMAPI_CONVERT, &firstfsb, resblks,
  796. &imap, &nimaps, &dfops);
  797. if (error)
  798. goto error_on_bmapi_transaction;
  799. /*
  800. * Log the updated inode size as we go. We have to be careful
  801. * to only log it up to the actual write offset if it is
  802. * halfway into a block.
  803. */
  804. i_size = XFS_FSB_TO_B(mp, offset_fsb + count_fsb);
  805. if (i_size > offset + count)
  806. i_size = offset + count;
  807. if (update_isize && i_size > i_size_read(inode))
  808. i_size_write(inode, i_size);
  809. i_size = xfs_new_eof(ip, i_size);
  810. if (i_size) {
  811. ip->i_d.di_size = i_size;
  812. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  813. }
  814. error = xfs_defer_finish(&tp, &dfops);
  815. if (error)
  816. goto error_on_bmapi_transaction;
  817. error = xfs_trans_commit(tp);
  818. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  819. if (error)
  820. return error;
  821. if (!(imap.br_startblock || XFS_IS_REALTIME_INODE(ip)))
  822. return xfs_alert_fsblock_zero(ip, &imap);
  823. if ((numblks_fsb = imap.br_blockcount) == 0) {
  824. /*
  825. * The numblks_fsb value should always get
  826. * smaller, otherwise the loop is stuck.
  827. */
  828. ASSERT(imap.br_blockcount);
  829. break;
  830. }
  831. offset_fsb += numblks_fsb;
  832. count_fsb -= numblks_fsb;
  833. } while (count_fsb > 0);
  834. return 0;
  835. error_on_bmapi_transaction:
  836. xfs_defer_cancel(&dfops);
  837. xfs_trans_cancel(tp);
  838. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  839. return error;
  840. }
  841. static inline bool imap_needs_alloc(struct inode *inode,
  842. struct xfs_bmbt_irec *imap, int nimaps)
  843. {
  844. return !nimaps ||
  845. imap->br_startblock == HOLESTARTBLOCK ||
  846. imap->br_startblock == DELAYSTARTBLOCK ||
  847. (IS_DAX(inode) && imap->br_state == XFS_EXT_UNWRITTEN);
  848. }
  849. static inline bool need_excl_ilock(struct xfs_inode *ip, unsigned flags)
  850. {
  851. /*
  852. * COW writes will allocate delalloc space, so we need to make sure
  853. * to take the lock exclusively here.
  854. */
  855. if (xfs_is_reflink_inode(ip) && (flags & (IOMAP_WRITE | IOMAP_ZERO)))
  856. return true;
  857. if ((flags & IOMAP_DIRECT) && (flags & IOMAP_WRITE))
  858. return true;
  859. return false;
  860. }
  861. static int
  862. xfs_file_iomap_begin(
  863. struct inode *inode,
  864. loff_t offset,
  865. loff_t length,
  866. unsigned flags,
  867. struct iomap *iomap)
  868. {
  869. struct xfs_inode *ip = XFS_I(inode);
  870. struct xfs_mount *mp = ip->i_mount;
  871. struct xfs_bmbt_irec imap;
  872. xfs_fileoff_t offset_fsb, end_fsb;
  873. int nimaps = 1, error = 0;
  874. bool shared = false, trimmed = false;
  875. unsigned lockmode;
  876. if (XFS_FORCED_SHUTDOWN(mp))
  877. return -EIO;
  878. if (((flags & (IOMAP_WRITE | IOMAP_DIRECT)) == IOMAP_WRITE) &&
  879. !IS_DAX(inode) && !xfs_get_extsz_hint(ip)) {
  880. /* Reserve delalloc blocks for regular writeback. */
  881. return xfs_file_iomap_begin_delay(inode, offset, length, iomap);
  882. }
  883. if (need_excl_ilock(ip, flags)) {
  884. lockmode = XFS_ILOCK_EXCL;
  885. xfs_ilock(ip, XFS_ILOCK_EXCL);
  886. } else {
  887. lockmode = xfs_ilock_data_map_shared(ip);
  888. }
  889. if ((flags & IOMAP_NOWAIT) && !(ip->i_df.if_flags & XFS_IFEXTENTS)) {
  890. error = -EAGAIN;
  891. goto out_unlock;
  892. }
  893. ASSERT(offset <= mp->m_super->s_maxbytes);
  894. if ((xfs_fsize_t)offset + length > mp->m_super->s_maxbytes)
  895. length = mp->m_super->s_maxbytes - offset;
  896. offset_fsb = XFS_B_TO_FSBT(mp, offset);
  897. end_fsb = XFS_B_TO_FSB(mp, offset + length);
  898. error = xfs_bmapi_read(ip, offset_fsb, end_fsb - offset_fsb, &imap,
  899. &nimaps, 0);
  900. if (error)
  901. goto out_unlock;
  902. if (flags & IOMAP_REPORT) {
  903. /* Trim the mapping to the nearest shared extent boundary. */
  904. error = xfs_reflink_trim_around_shared(ip, &imap, &shared,
  905. &trimmed);
  906. if (error)
  907. goto out_unlock;
  908. }
  909. if ((flags & (IOMAP_WRITE | IOMAP_ZERO)) && xfs_is_reflink_inode(ip)) {
  910. if (flags & IOMAP_DIRECT) {
  911. /*
  912. * A reflinked inode will result in CoW alloc.
  913. * FIXME: It could still overwrite on unshared extents
  914. * and not need allocation.
  915. */
  916. if (flags & IOMAP_NOWAIT) {
  917. error = -EAGAIN;
  918. goto out_unlock;
  919. }
  920. /* may drop and re-acquire the ilock */
  921. error = xfs_reflink_allocate_cow(ip, &imap, &shared,
  922. &lockmode);
  923. if (error)
  924. goto out_unlock;
  925. } else {
  926. error = xfs_reflink_reserve_cow(ip, &imap, &shared);
  927. if (error)
  928. goto out_unlock;
  929. }
  930. end_fsb = imap.br_startoff + imap.br_blockcount;
  931. length = XFS_FSB_TO_B(mp, end_fsb) - offset;
  932. }
  933. if ((flags & IOMAP_WRITE) && imap_needs_alloc(inode, &imap, nimaps)) {
  934. /*
  935. * If nowait is set bail since we are going to make
  936. * allocations.
  937. */
  938. if (flags & IOMAP_NOWAIT) {
  939. error = -EAGAIN;
  940. goto out_unlock;
  941. }
  942. /*
  943. * We cap the maximum length we map here to MAX_WRITEBACK_PAGES
  944. * pages to keep the chunks of work done where somewhat symmetric
  945. * with the work writeback does. This is a completely arbitrary
  946. * number pulled out of thin air as a best guess for initial
  947. * testing.
  948. *
  949. * Note that the values needs to be less than 32-bits wide until
  950. * the lower level functions are updated.
  951. */
  952. length = min_t(loff_t, length, 1024 * PAGE_SIZE);
  953. /*
  954. * xfs_iomap_write_direct() expects the shared lock. It
  955. * is unlocked on return.
  956. */
  957. if (lockmode == XFS_ILOCK_EXCL)
  958. xfs_ilock_demote(ip, lockmode);
  959. error = xfs_iomap_write_direct(ip, offset, length, &imap,
  960. nimaps);
  961. if (error)
  962. return error;
  963. iomap->flags = IOMAP_F_NEW;
  964. trace_xfs_iomap_alloc(ip, offset, length, 0, &imap);
  965. } else {
  966. ASSERT(nimaps);
  967. xfs_iunlock(ip, lockmode);
  968. trace_xfs_iomap_found(ip, offset, length, 0, &imap);
  969. }
  970. xfs_bmbt_to_iomap(ip, iomap, &imap);
  971. if (shared)
  972. iomap->flags |= IOMAP_F_SHARED;
  973. return 0;
  974. out_unlock:
  975. xfs_iunlock(ip, lockmode);
  976. return error;
  977. }
  978. static int
  979. xfs_file_iomap_end_delalloc(
  980. struct xfs_inode *ip,
  981. loff_t offset,
  982. loff_t length,
  983. ssize_t written,
  984. struct iomap *iomap)
  985. {
  986. struct xfs_mount *mp = ip->i_mount;
  987. xfs_fileoff_t start_fsb;
  988. xfs_fileoff_t end_fsb;
  989. int error = 0;
  990. /*
  991. * Behave as if the write failed if drop writes is enabled. Set the NEW
  992. * flag to force delalloc cleanup.
  993. */
  994. if (XFS_TEST_ERROR(false, mp, XFS_ERRTAG_DROP_WRITES)) {
  995. iomap->flags |= IOMAP_F_NEW;
  996. written = 0;
  997. }
  998. /*
  999. * start_fsb refers to the first unused block after a short write. If
  1000. * nothing was written, round offset down to point at the first block in
  1001. * the range.
  1002. */
  1003. if (unlikely(!written))
  1004. start_fsb = XFS_B_TO_FSBT(mp, offset);
  1005. else
  1006. start_fsb = XFS_B_TO_FSB(mp, offset + written);
  1007. end_fsb = XFS_B_TO_FSB(mp, offset + length);
  1008. /*
  1009. * Trim delalloc blocks if they were allocated by this write and we
  1010. * didn't manage to write the whole range.
  1011. *
  1012. * We don't need to care about racing delalloc as we hold i_mutex
  1013. * across the reserve/allocate/unreserve calls. If there are delalloc
  1014. * blocks in the range, they are ours.
  1015. */
  1016. if ((iomap->flags & IOMAP_F_NEW) && start_fsb < end_fsb) {
  1017. truncate_pagecache_range(VFS_I(ip), XFS_FSB_TO_B(mp, start_fsb),
  1018. XFS_FSB_TO_B(mp, end_fsb) - 1);
  1019. xfs_ilock(ip, XFS_ILOCK_EXCL);
  1020. error = xfs_bmap_punch_delalloc_range(ip, start_fsb,
  1021. end_fsb - start_fsb);
  1022. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1023. if (error && !XFS_FORCED_SHUTDOWN(mp)) {
  1024. xfs_alert(mp, "%s: unable to clean up ino %lld",
  1025. __func__, ip->i_ino);
  1026. return error;
  1027. }
  1028. }
  1029. return 0;
  1030. }
  1031. static int
  1032. xfs_file_iomap_end(
  1033. struct inode *inode,
  1034. loff_t offset,
  1035. loff_t length,
  1036. ssize_t written,
  1037. unsigned flags,
  1038. struct iomap *iomap)
  1039. {
  1040. if ((flags & IOMAP_WRITE) && iomap->type == IOMAP_DELALLOC)
  1041. return xfs_file_iomap_end_delalloc(XFS_I(inode), offset,
  1042. length, written, iomap);
  1043. return 0;
  1044. }
  1045. const struct iomap_ops xfs_iomap_ops = {
  1046. .iomap_begin = xfs_file_iomap_begin,
  1047. .iomap_end = xfs_file_iomap_end,
  1048. };
  1049. static int
  1050. xfs_xattr_iomap_begin(
  1051. struct inode *inode,
  1052. loff_t offset,
  1053. loff_t length,
  1054. unsigned flags,
  1055. struct iomap *iomap)
  1056. {
  1057. struct xfs_inode *ip = XFS_I(inode);
  1058. struct xfs_mount *mp = ip->i_mount;
  1059. xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset);
  1060. xfs_fileoff_t end_fsb = XFS_B_TO_FSB(mp, offset + length);
  1061. struct xfs_bmbt_irec imap;
  1062. int nimaps = 1, error = 0;
  1063. unsigned lockmode;
  1064. if (XFS_FORCED_SHUTDOWN(mp))
  1065. return -EIO;
  1066. lockmode = xfs_ilock_attr_map_shared(ip);
  1067. /* if there are no attribute fork or extents, return ENOENT */
  1068. if (!XFS_IFORK_Q(ip) || !ip->i_d.di_anextents) {
  1069. error = -ENOENT;
  1070. goto out_unlock;
  1071. }
  1072. ASSERT(ip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL);
  1073. error = xfs_bmapi_read(ip, offset_fsb, end_fsb - offset_fsb, &imap,
  1074. &nimaps, XFS_BMAPI_ENTIRE | XFS_BMAPI_ATTRFORK);
  1075. out_unlock:
  1076. xfs_iunlock(ip, lockmode);
  1077. if (!error) {
  1078. ASSERT(nimaps);
  1079. xfs_bmbt_to_iomap(ip, iomap, &imap);
  1080. }
  1081. return error;
  1082. }
  1083. const struct iomap_ops xfs_xattr_iomap_ops = {
  1084. .iomap_begin = xfs_xattr_iomap_begin,
  1085. };