xfs_iomap.c 34 KB

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