xfs_iomap.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481
  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 "xfs.h"
  8. #include "xfs_fs.h"
  9. #include "xfs_shared.h"
  10. #include "xfs_format.h"
  11. #include "xfs_log_format.h"
  12. #include "xfs_trans_resv.h"
  13. #include "xfs_mount.h"
  14. #include "xfs_inode.h"
  15. #include "xfs_btree.h"
  16. #include "xfs_bmap_btree.h"
  17. #include "xfs_bmap.h"
  18. #include "xfs_bmap_util.h"
  19. #include "xfs_errortag.h"
  20. #include "xfs_error.h"
  21. #include "xfs_trans.h"
  22. #include "xfs_trans_space.h"
  23. #include "xfs_inode_item.h"
  24. #include "xfs_iomap.h"
  25. #include "xfs_trace.h"
  26. #include "xfs_quota.h"
  27. #include "xfs_dquot_item.h"
  28. #include "xfs_dquot.h"
  29. #include "xfs_reflink.h"
  30. #include "xfs_health.h"
  31. #include "xfs_rtbitmap.h"
  32. #define XFS_ALLOC_ALIGN(mp, off) \
  33. (((off) >> mp->m_allocsize_log) << mp->m_allocsize_log)
  34. static int
  35. xfs_alert_fsblock_zero(
  36. xfs_inode_t *ip,
  37. xfs_bmbt_irec_t *imap)
  38. {
  39. xfs_alert_tag(ip->i_mount, XFS_PTAG_FSBLOCK_ZERO,
  40. "Access to block zero in inode %llu "
  41. "start_block: %llx start_off: %llx "
  42. "blkcnt: %llx extent-state: %x",
  43. (unsigned long long)ip->i_ino,
  44. (unsigned long long)imap->br_startblock,
  45. (unsigned long long)imap->br_startoff,
  46. (unsigned long long)imap->br_blockcount,
  47. imap->br_state);
  48. xfs_bmap_mark_sick(ip, XFS_DATA_FORK);
  49. return -EFSCORRUPTED;
  50. }
  51. u64
  52. xfs_iomap_inode_sequence(
  53. struct xfs_inode *ip,
  54. u16 iomap_flags)
  55. {
  56. u64 cookie = 0;
  57. if (iomap_flags & IOMAP_F_XATTR)
  58. return READ_ONCE(ip->i_af.if_seq);
  59. if ((iomap_flags & IOMAP_F_SHARED) && ip->i_cowfp)
  60. cookie = (u64)READ_ONCE(ip->i_cowfp->if_seq) << 32;
  61. return cookie | READ_ONCE(ip->i_df.if_seq);
  62. }
  63. /*
  64. * Check that the iomap passed to us is still valid for the given offset and
  65. * length.
  66. */
  67. static bool
  68. xfs_iomap_valid(
  69. struct inode *inode,
  70. const struct iomap *iomap)
  71. {
  72. struct xfs_inode *ip = XFS_I(inode);
  73. if (iomap->validity_cookie !=
  74. xfs_iomap_inode_sequence(ip, iomap->flags)) {
  75. trace_xfs_iomap_invalid(ip, iomap);
  76. return false;
  77. }
  78. XFS_ERRORTAG_DELAY(ip->i_mount, XFS_ERRTAG_WRITE_DELAY_MS);
  79. return true;
  80. }
  81. static const struct iomap_folio_ops xfs_iomap_folio_ops = {
  82. .iomap_valid = xfs_iomap_valid,
  83. };
  84. int
  85. xfs_bmbt_to_iomap(
  86. struct xfs_inode *ip,
  87. struct iomap *iomap,
  88. struct xfs_bmbt_irec *imap,
  89. unsigned int mapping_flags,
  90. u16 iomap_flags,
  91. u64 sequence_cookie)
  92. {
  93. struct xfs_mount *mp = ip->i_mount;
  94. struct xfs_buftarg *target = xfs_inode_buftarg(ip);
  95. if (unlikely(!xfs_valid_startblock(ip, imap->br_startblock))) {
  96. xfs_bmap_mark_sick(ip, XFS_DATA_FORK);
  97. return xfs_alert_fsblock_zero(ip, imap);
  98. }
  99. if (imap->br_startblock == HOLESTARTBLOCK) {
  100. iomap->addr = IOMAP_NULL_ADDR;
  101. iomap->type = IOMAP_HOLE;
  102. } else if (imap->br_startblock == DELAYSTARTBLOCK ||
  103. isnullstartblock(imap->br_startblock)) {
  104. iomap->addr = IOMAP_NULL_ADDR;
  105. iomap->type = IOMAP_DELALLOC;
  106. } else {
  107. iomap->addr = BBTOB(xfs_fsb_to_db(ip, imap->br_startblock));
  108. if (mapping_flags & IOMAP_DAX)
  109. iomap->addr += target->bt_dax_part_off;
  110. if (imap->br_state == XFS_EXT_UNWRITTEN)
  111. iomap->type = IOMAP_UNWRITTEN;
  112. else
  113. iomap->type = IOMAP_MAPPED;
  114. }
  115. iomap->offset = XFS_FSB_TO_B(mp, imap->br_startoff);
  116. iomap->length = XFS_FSB_TO_B(mp, imap->br_blockcount);
  117. if (mapping_flags & IOMAP_DAX)
  118. iomap->dax_dev = target->bt_daxdev;
  119. else
  120. iomap->bdev = target->bt_bdev;
  121. iomap->flags = iomap_flags;
  122. if (xfs_ipincount(ip) &&
  123. (ip->i_itemp->ili_fsync_fields & ~XFS_ILOG_TIMESTAMP))
  124. iomap->flags |= IOMAP_F_DIRTY;
  125. iomap->validity_cookie = sequence_cookie;
  126. iomap->folio_ops = &xfs_iomap_folio_ops;
  127. return 0;
  128. }
  129. static void
  130. xfs_hole_to_iomap(
  131. struct xfs_inode *ip,
  132. struct iomap *iomap,
  133. xfs_fileoff_t offset_fsb,
  134. xfs_fileoff_t end_fsb)
  135. {
  136. struct xfs_buftarg *target = xfs_inode_buftarg(ip);
  137. iomap->addr = IOMAP_NULL_ADDR;
  138. iomap->type = IOMAP_HOLE;
  139. iomap->offset = XFS_FSB_TO_B(ip->i_mount, offset_fsb);
  140. iomap->length = XFS_FSB_TO_B(ip->i_mount, end_fsb - offset_fsb);
  141. iomap->bdev = target->bt_bdev;
  142. iomap->dax_dev = target->bt_daxdev;
  143. }
  144. static inline xfs_fileoff_t
  145. xfs_iomap_end_fsb(
  146. struct xfs_mount *mp,
  147. loff_t offset,
  148. loff_t count)
  149. {
  150. ASSERT(offset <= mp->m_super->s_maxbytes);
  151. return min(XFS_B_TO_FSB(mp, offset + count),
  152. XFS_B_TO_FSB(mp, mp->m_super->s_maxbytes));
  153. }
  154. static xfs_extlen_t
  155. xfs_eof_alignment(
  156. struct xfs_inode *ip)
  157. {
  158. struct xfs_mount *mp = ip->i_mount;
  159. xfs_extlen_t align = 0;
  160. if (!XFS_IS_REALTIME_INODE(ip)) {
  161. /*
  162. * Round up the allocation request to a stripe unit
  163. * (m_dalign) boundary if the file size is >= stripe unit
  164. * size, and we are allocating past the allocation eof.
  165. *
  166. * If mounted with the "-o swalloc" option the alignment is
  167. * increased from the strip unit size to the stripe width.
  168. */
  169. if (mp->m_swidth && xfs_has_swalloc(mp))
  170. align = mp->m_swidth;
  171. else if (mp->m_dalign)
  172. align = mp->m_dalign;
  173. if (align && XFS_ISIZE(ip) < XFS_FSB_TO_B(mp, align))
  174. align = 0;
  175. }
  176. return align;
  177. }
  178. /*
  179. * Check if last_fsb is outside the last extent, and if so grow it to the next
  180. * stripe unit boundary.
  181. */
  182. xfs_fileoff_t
  183. xfs_iomap_eof_align_last_fsb(
  184. struct xfs_inode *ip,
  185. xfs_fileoff_t end_fsb)
  186. {
  187. struct xfs_ifork *ifp = xfs_ifork_ptr(ip, XFS_DATA_FORK);
  188. xfs_extlen_t extsz = xfs_get_extsz_hint(ip);
  189. xfs_extlen_t align = xfs_eof_alignment(ip);
  190. struct xfs_bmbt_irec irec;
  191. struct xfs_iext_cursor icur;
  192. ASSERT(!xfs_need_iread_extents(ifp));
  193. /*
  194. * Always round up the allocation request to the extent hint boundary.
  195. */
  196. if (extsz) {
  197. if (align)
  198. align = roundup_64(align, extsz);
  199. else
  200. align = extsz;
  201. }
  202. if (align) {
  203. xfs_fileoff_t aligned_end_fsb = roundup_64(end_fsb, align);
  204. xfs_iext_last(ifp, &icur);
  205. if (!xfs_iext_get_extent(ifp, &icur, &irec) ||
  206. aligned_end_fsb >= irec.br_startoff + irec.br_blockcount)
  207. return aligned_end_fsb;
  208. }
  209. return end_fsb;
  210. }
  211. int
  212. xfs_iomap_write_direct(
  213. struct xfs_inode *ip,
  214. xfs_fileoff_t offset_fsb,
  215. xfs_fileoff_t count_fsb,
  216. unsigned int flags,
  217. struct xfs_bmbt_irec *imap,
  218. u64 *seq)
  219. {
  220. struct xfs_mount *mp = ip->i_mount;
  221. struct xfs_trans *tp;
  222. xfs_filblks_t resaligned;
  223. int nimaps;
  224. unsigned int dblocks, rblocks;
  225. bool force = false;
  226. int error;
  227. int bmapi_flags = XFS_BMAPI_PREALLOC;
  228. int nr_exts = XFS_IEXT_ADD_NOSPLIT_CNT;
  229. ASSERT(count_fsb > 0);
  230. resaligned = xfs_aligned_fsb_count(offset_fsb, count_fsb,
  231. xfs_get_extsz_hint(ip));
  232. if (unlikely(XFS_IS_REALTIME_INODE(ip))) {
  233. dblocks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
  234. rblocks = resaligned;
  235. } else {
  236. dblocks = XFS_DIOSTRAT_SPACE_RES(mp, resaligned);
  237. rblocks = 0;
  238. }
  239. error = xfs_qm_dqattach(ip);
  240. if (error)
  241. return error;
  242. /*
  243. * For DAX, we do not allocate unwritten extents, but instead we zero
  244. * the block before we commit the transaction. Ideally we'd like to do
  245. * this outside the transaction context, but if we commit and then crash
  246. * we may not have zeroed the blocks and this will be exposed on
  247. * recovery of the allocation. Hence we must zero before commit.
  248. *
  249. * Further, if we are mapping unwritten extents here, we need to zero
  250. * and convert them to written so that we don't need an unwritten extent
  251. * callback for DAX. This also means that we need to be able to dip into
  252. * the reserve block pool for bmbt block allocation if there is no space
  253. * left but we need to do unwritten extent conversion.
  254. */
  255. if (flags & IOMAP_DAX) {
  256. bmapi_flags = XFS_BMAPI_CONVERT | XFS_BMAPI_ZERO;
  257. if (imap->br_state == XFS_EXT_UNWRITTEN) {
  258. force = true;
  259. nr_exts = XFS_IEXT_WRITE_UNWRITTEN_CNT;
  260. dblocks = XFS_DIOSTRAT_SPACE_RES(mp, 0) << 1;
  261. }
  262. }
  263. error = xfs_trans_alloc_inode(ip, &M_RES(mp)->tr_write, dblocks,
  264. rblocks, force, &tp);
  265. if (error)
  266. return error;
  267. error = xfs_iext_count_extend(tp, ip, XFS_DATA_FORK, nr_exts);
  268. if (error)
  269. goto out_trans_cancel;
  270. /*
  271. * From this point onwards we overwrite the imap pointer that the
  272. * caller gave to us.
  273. */
  274. nimaps = 1;
  275. error = xfs_bmapi_write(tp, ip, offset_fsb, count_fsb, bmapi_flags, 0,
  276. imap, &nimaps);
  277. if (error)
  278. goto out_trans_cancel;
  279. /*
  280. * Complete the transaction
  281. */
  282. error = xfs_trans_commit(tp);
  283. if (error)
  284. goto out_unlock;
  285. if (unlikely(!xfs_valid_startblock(ip, imap->br_startblock))) {
  286. xfs_bmap_mark_sick(ip, XFS_DATA_FORK);
  287. error = xfs_alert_fsblock_zero(ip, imap);
  288. }
  289. out_unlock:
  290. *seq = xfs_iomap_inode_sequence(ip, 0);
  291. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  292. return error;
  293. out_trans_cancel:
  294. xfs_trans_cancel(tp);
  295. goto out_unlock;
  296. }
  297. STATIC bool
  298. xfs_quota_need_throttle(
  299. struct xfs_inode *ip,
  300. xfs_dqtype_t type,
  301. xfs_fsblock_t alloc_blocks)
  302. {
  303. struct xfs_dquot *dq = xfs_inode_dquot(ip, type);
  304. if (!dq || !xfs_this_quota_on(ip->i_mount, type))
  305. return false;
  306. /* no hi watermark, no throttle */
  307. if (!dq->q_prealloc_hi_wmark)
  308. return false;
  309. /* under the lo watermark, no throttle */
  310. if (dq->q_blk.reserved + alloc_blocks < dq->q_prealloc_lo_wmark)
  311. return false;
  312. return true;
  313. }
  314. STATIC void
  315. xfs_quota_calc_throttle(
  316. struct xfs_inode *ip,
  317. xfs_dqtype_t type,
  318. xfs_fsblock_t *qblocks,
  319. int *qshift,
  320. int64_t *qfreesp)
  321. {
  322. struct xfs_dquot *dq = xfs_inode_dquot(ip, type);
  323. int64_t freesp;
  324. int shift = 0;
  325. /* no dq, or over hi wmark, squash the prealloc completely */
  326. if (!dq || dq->q_blk.reserved >= dq->q_prealloc_hi_wmark) {
  327. *qblocks = 0;
  328. *qfreesp = 0;
  329. return;
  330. }
  331. freesp = dq->q_prealloc_hi_wmark - dq->q_blk.reserved;
  332. if (freesp < dq->q_low_space[XFS_QLOWSP_5_PCNT]) {
  333. shift = 2;
  334. if (freesp < dq->q_low_space[XFS_QLOWSP_3_PCNT])
  335. shift += 2;
  336. if (freesp < dq->q_low_space[XFS_QLOWSP_1_PCNT])
  337. shift += 2;
  338. }
  339. if (freesp < *qfreesp)
  340. *qfreesp = freesp;
  341. /* only overwrite the throttle values if we are more aggressive */
  342. if ((freesp >> shift) < (*qblocks >> *qshift)) {
  343. *qblocks = freesp;
  344. *qshift = shift;
  345. }
  346. }
  347. static int64_t
  348. xfs_iomap_freesp(
  349. struct percpu_counter *counter,
  350. uint64_t low_space[XFS_LOWSP_MAX],
  351. int *shift)
  352. {
  353. int64_t freesp;
  354. freesp = percpu_counter_read_positive(counter);
  355. if (freesp < low_space[XFS_LOWSP_5_PCNT]) {
  356. *shift = 2;
  357. if (freesp < low_space[XFS_LOWSP_4_PCNT])
  358. (*shift)++;
  359. if (freesp < low_space[XFS_LOWSP_3_PCNT])
  360. (*shift)++;
  361. if (freesp < low_space[XFS_LOWSP_2_PCNT])
  362. (*shift)++;
  363. if (freesp < low_space[XFS_LOWSP_1_PCNT])
  364. (*shift)++;
  365. }
  366. return freesp;
  367. }
  368. /*
  369. * If we don't have a user specified preallocation size, dynamically increase
  370. * the preallocation size as the size of the file grows. Cap the maximum size
  371. * at a single extent or less if the filesystem is near full. The closer the
  372. * filesystem is to being full, the smaller the maximum preallocation.
  373. */
  374. STATIC xfs_fsblock_t
  375. xfs_iomap_prealloc_size(
  376. struct xfs_inode *ip,
  377. int whichfork,
  378. loff_t offset,
  379. loff_t count,
  380. struct xfs_iext_cursor *icur)
  381. {
  382. struct xfs_iext_cursor ncur = *icur;
  383. struct xfs_bmbt_irec prev, got;
  384. struct xfs_mount *mp = ip->i_mount;
  385. struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
  386. xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset);
  387. int64_t freesp;
  388. xfs_fsblock_t qblocks;
  389. xfs_fsblock_t alloc_blocks = 0;
  390. xfs_extlen_t plen;
  391. int shift = 0;
  392. int qshift = 0;
  393. /*
  394. * As an exception we don't do any preallocation at all if the file is
  395. * smaller than the minimum preallocation and we are using the default
  396. * dynamic preallocation scheme, as it is likely this is the only write
  397. * to the file that is going to be done.
  398. */
  399. if (XFS_ISIZE(ip) < XFS_FSB_TO_B(mp, mp->m_allocsize_blocks))
  400. return 0;
  401. /*
  402. * Use the minimum preallocation size for small files or if we are
  403. * writing right after a hole.
  404. */
  405. if (XFS_ISIZE(ip) < XFS_FSB_TO_B(mp, mp->m_dalign) ||
  406. !xfs_iext_prev_extent(ifp, &ncur, &prev) ||
  407. prev.br_startoff + prev.br_blockcount < offset_fsb)
  408. return mp->m_allocsize_blocks;
  409. /*
  410. * Take the size of the preceding data extents as the basis for the
  411. * preallocation size. Note that we don't care if the previous extents
  412. * are written or not.
  413. */
  414. plen = prev.br_blockcount;
  415. while (xfs_iext_prev_extent(ifp, &ncur, &got)) {
  416. if (plen > XFS_MAX_BMBT_EXTLEN / 2 ||
  417. isnullstartblock(got.br_startblock) ||
  418. got.br_startoff + got.br_blockcount != prev.br_startoff ||
  419. got.br_startblock + got.br_blockcount != prev.br_startblock)
  420. break;
  421. plen += got.br_blockcount;
  422. prev = got;
  423. }
  424. /*
  425. * If the size of the extents is greater than half the maximum extent
  426. * length, then use the current offset as the basis. This ensures that
  427. * for large files the preallocation size always extends to
  428. * XFS_BMBT_MAX_EXTLEN rather than falling short due to things like stripe
  429. * unit/width alignment of real extents.
  430. */
  431. alloc_blocks = plen * 2;
  432. if (alloc_blocks > XFS_MAX_BMBT_EXTLEN)
  433. alloc_blocks = XFS_B_TO_FSB(mp, offset);
  434. qblocks = alloc_blocks;
  435. /*
  436. * XFS_BMBT_MAX_EXTLEN is not a power of two value but we round the prealloc
  437. * down to the nearest power of two value after throttling. To prevent
  438. * the round down from unconditionally reducing the maximum supported
  439. * prealloc size, we round up first, apply appropriate throttling, round
  440. * down and cap the value to XFS_BMBT_MAX_EXTLEN.
  441. */
  442. alloc_blocks = XFS_FILEOFF_MIN(roundup_pow_of_two(XFS_MAX_BMBT_EXTLEN),
  443. alloc_blocks);
  444. if (unlikely(XFS_IS_REALTIME_INODE(ip)))
  445. freesp = xfs_rtx_to_rtb(mp,
  446. xfs_iomap_freesp(&mp->m_frextents,
  447. mp->m_low_rtexts, &shift));
  448. else
  449. freesp = xfs_iomap_freesp(&mp->m_fdblocks, mp->m_low_space,
  450. &shift);
  451. /*
  452. * Check each quota to cap the prealloc size, provide a shift value to
  453. * throttle with and adjust amount of available space.
  454. */
  455. if (xfs_quota_need_throttle(ip, XFS_DQTYPE_USER, alloc_blocks))
  456. xfs_quota_calc_throttle(ip, XFS_DQTYPE_USER, &qblocks, &qshift,
  457. &freesp);
  458. if (xfs_quota_need_throttle(ip, XFS_DQTYPE_GROUP, alloc_blocks))
  459. xfs_quota_calc_throttle(ip, XFS_DQTYPE_GROUP, &qblocks, &qshift,
  460. &freesp);
  461. if (xfs_quota_need_throttle(ip, XFS_DQTYPE_PROJ, alloc_blocks))
  462. xfs_quota_calc_throttle(ip, XFS_DQTYPE_PROJ, &qblocks, &qshift,
  463. &freesp);
  464. /*
  465. * The final prealloc size is set to the minimum of free space available
  466. * in each of the quotas and the overall filesystem.
  467. *
  468. * The shift throttle value is set to the maximum value as determined by
  469. * the global low free space values and per-quota low free space values.
  470. */
  471. alloc_blocks = min(alloc_blocks, qblocks);
  472. shift = max(shift, qshift);
  473. if (shift)
  474. alloc_blocks >>= shift;
  475. /*
  476. * rounddown_pow_of_two() returns an undefined result if we pass in
  477. * alloc_blocks = 0.
  478. */
  479. if (alloc_blocks)
  480. alloc_blocks = rounddown_pow_of_two(alloc_blocks);
  481. if (alloc_blocks > XFS_MAX_BMBT_EXTLEN)
  482. alloc_blocks = XFS_MAX_BMBT_EXTLEN;
  483. /*
  484. * If we are still trying to allocate more space than is
  485. * available, squash the prealloc hard. This can happen if we
  486. * have a large file on a small filesystem and the above
  487. * lowspace thresholds are smaller than XFS_BMBT_MAX_EXTLEN.
  488. */
  489. while (alloc_blocks && alloc_blocks >= freesp)
  490. alloc_blocks >>= 4;
  491. if (alloc_blocks < mp->m_allocsize_blocks)
  492. alloc_blocks = mp->m_allocsize_blocks;
  493. trace_xfs_iomap_prealloc_size(ip, alloc_blocks, shift,
  494. mp->m_allocsize_blocks);
  495. return alloc_blocks;
  496. }
  497. int
  498. xfs_iomap_write_unwritten(
  499. xfs_inode_t *ip,
  500. xfs_off_t offset,
  501. xfs_off_t count,
  502. bool update_isize)
  503. {
  504. xfs_mount_t *mp = ip->i_mount;
  505. xfs_fileoff_t offset_fsb;
  506. xfs_filblks_t count_fsb;
  507. xfs_filblks_t numblks_fsb;
  508. int nimaps;
  509. xfs_trans_t *tp;
  510. xfs_bmbt_irec_t imap;
  511. struct inode *inode = VFS_I(ip);
  512. xfs_fsize_t i_size;
  513. uint resblks;
  514. int error;
  515. trace_xfs_unwritten_convert(ip, offset, count);
  516. offset_fsb = XFS_B_TO_FSBT(mp, offset);
  517. count_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)offset + count);
  518. count_fsb = (xfs_filblks_t)(count_fsb - offset_fsb);
  519. /*
  520. * Reserve enough blocks in this transaction for two complete extent
  521. * btree splits. We may be converting the middle part of an unwritten
  522. * extent and in this case we will insert two new extents in the btree
  523. * each of which could cause a full split.
  524. *
  525. * This reservation amount will be used in the first call to
  526. * xfs_bmbt_split() to select an AG with enough space to satisfy the
  527. * rest of the operation.
  528. */
  529. resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0) << 1;
  530. /* Attach dquots so that bmbt splits are accounted correctly. */
  531. error = xfs_qm_dqattach(ip);
  532. if (error)
  533. return error;
  534. do {
  535. /*
  536. * Set up a transaction to convert the range of extents
  537. * from unwritten to real. Do allocations in a loop until
  538. * we have covered the range passed in.
  539. *
  540. * Note that we can't risk to recursing back into the filesystem
  541. * here as we might be asked to write out the same inode that we
  542. * complete here and might deadlock on the iolock.
  543. */
  544. error = xfs_trans_alloc_inode(ip, &M_RES(mp)->tr_write, resblks,
  545. 0, true, &tp);
  546. if (error)
  547. return error;
  548. error = xfs_iext_count_extend(tp, ip, XFS_DATA_FORK,
  549. XFS_IEXT_WRITE_UNWRITTEN_CNT);
  550. if (error)
  551. goto error_on_bmapi_transaction;
  552. /*
  553. * Modify the unwritten extent state of the buffer.
  554. */
  555. nimaps = 1;
  556. error = xfs_bmapi_write(tp, ip, offset_fsb, count_fsb,
  557. XFS_BMAPI_CONVERT, resblks, &imap,
  558. &nimaps);
  559. if (error)
  560. goto error_on_bmapi_transaction;
  561. /*
  562. * Log the updated inode size as we go. We have to be careful
  563. * to only log it up to the actual write offset if it is
  564. * halfway into a block.
  565. */
  566. i_size = XFS_FSB_TO_B(mp, offset_fsb + count_fsb);
  567. if (i_size > offset + count)
  568. i_size = offset + count;
  569. if (update_isize && i_size > i_size_read(inode))
  570. i_size_write(inode, i_size);
  571. i_size = xfs_new_eof(ip, i_size);
  572. if (i_size) {
  573. ip->i_disk_size = i_size;
  574. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  575. }
  576. error = xfs_trans_commit(tp);
  577. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  578. if (error)
  579. return error;
  580. if (unlikely(!xfs_valid_startblock(ip, imap.br_startblock))) {
  581. xfs_bmap_mark_sick(ip, XFS_DATA_FORK);
  582. return xfs_alert_fsblock_zero(ip, &imap);
  583. }
  584. if ((numblks_fsb = imap.br_blockcount) == 0) {
  585. /*
  586. * The numblks_fsb value should always get
  587. * smaller, otherwise the loop is stuck.
  588. */
  589. ASSERT(imap.br_blockcount);
  590. break;
  591. }
  592. offset_fsb += numblks_fsb;
  593. count_fsb -= numblks_fsb;
  594. } while (count_fsb > 0);
  595. return 0;
  596. error_on_bmapi_transaction:
  597. xfs_trans_cancel(tp);
  598. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  599. return error;
  600. }
  601. static inline bool
  602. imap_needs_alloc(
  603. struct inode *inode,
  604. unsigned flags,
  605. struct xfs_bmbt_irec *imap,
  606. int nimaps)
  607. {
  608. /* don't allocate blocks when just zeroing */
  609. if (flags & IOMAP_ZERO)
  610. return false;
  611. if (!nimaps ||
  612. imap->br_startblock == HOLESTARTBLOCK ||
  613. imap->br_startblock == DELAYSTARTBLOCK)
  614. return true;
  615. /* we convert unwritten extents before copying the data for DAX */
  616. if ((flags & IOMAP_DAX) && imap->br_state == XFS_EXT_UNWRITTEN)
  617. return true;
  618. return false;
  619. }
  620. static inline bool
  621. imap_needs_cow(
  622. struct xfs_inode *ip,
  623. unsigned int flags,
  624. struct xfs_bmbt_irec *imap,
  625. int nimaps)
  626. {
  627. if (!xfs_is_cow_inode(ip))
  628. return false;
  629. /* when zeroing we don't have to COW holes or unwritten extents */
  630. if (flags & (IOMAP_UNSHARE | IOMAP_ZERO)) {
  631. if (!nimaps ||
  632. imap->br_startblock == HOLESTARTBLOCK ||
  633. imap->br_state == XFS_EXT_UNWRITTEN)
  634. return false;
  635. }
  636. return true;
  637. }
  638. /*
  639. * Extents not yet cached requires exclusive access, don't block for
  640. * IOMAP_NOWAIT.
  641. *
  642. * This is basically an opencoded xfs_ilock_data_map_shared() call, but with
  643. * support for IOMAP_NOWAIT.
  644. */
  645. static int
  646. xfs_ilock_for_iomap(
  647. struct xfs_inode *ip,
  648. unsigned flags,
  649. unsigned *lockmode)
  650. {
  651. if (flags & IOMAP_NOWAIT) {
  652. if (xfs_need_iread_extents(&ip->i_df))
  653. return -EAGAIN;
  654. if (!xfs_ilock_nowait(ip, *lockmode))
  655. return -EAGAIN;
  656. } else {
  657. if (xfs_need_iread_extents(&ip->i_df))
  658. *lockmode = XFS_ILOCK_EXCL;
  659. xfs_ilock(ip, *lockmode);
  660. }
  661. return 0;
  662. }
  663. /*
  664. * Check that the imap we are going to return to the caller spans the entire
  665. * range that the caller requested for the IO.
  666. */
  667. static bool
  668. imap_spans_range(
  669. struct xfs_bmbt_irec *imap,
  670. xfs_fileoff_t offset_fsb,
  671. xfs_fileoff_t end_fsb)
  672. {
  673. if (imap->br_startoff > offset_fsb)
  674. return false;
  675. if (imap->br_startoff + imap->br_blockcount < end_fsb)
  676. return false;
  677. return true;
  678. }
  679. static int
  680. xfs_direct_write_iomap_begin(
  681. struct inode *inode,
  682. loff_t offset,
  683. loff_t length,
  684. unsigned flags,
  685. struct iomap *iomap,
  686. struct iomap *srcmap)
  687. {
  688. struct xfs_inode *ip = XFS_I(inode);
  689. struct xfs_mount *mp = ip->i_mount;
  690. struct xfs_bmbt_irec imap, cmap;
  691. xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset);
  692. xfs_fileoff_t end_fsb = xfs_iomap_end_fsb(mp, offset, length);
  693. int nimaps = 1, error = 0;
  694. bool shared = false;
  695. u16 iomap_flags = 0;
  696. unsigned int lockmode;
  697. u64 seq;
  698. ASSERT(flags & (IOMAP_WRITE | IOMAP_ZERO));
  699. if (xfs_is_shutdown(mp))
  700. return -EIO;
  701. /*
  702. * Writes that span EOF might trigger an IO size update on completion,
  703. * so consider them to be dirty for the purposes of O_DSYNC even if
  704. * there is no other metadata changes pending or have been made here.
  705. */
  706. if (offset + length > i_size_read(inode))
  707. iomap_flags |= IOMAP_F_DIRTY;
  708. /*
  709. * COW writes may allocate delalloc space or convert unwritten COW
  710. * extents, so we need to make sure to take the lock exclusively here.
  711. */
  712. if (xfs_is_cow_inode(ip))
  713. lockmode = XFS_ILOCK_EXCL;
  714. else
  715. lockmode = XFS_ILOCK_SHARED;
  716. relock:
  717. error = xfs_ilock_for_iomap(ip, flags, &lockmode);
  718. if (error)
  719. return error;
  720. /*
  721. * The reflink iflag could have changed since the earlier unlocked
  722. * check, check if it again and relock if needed.
  723. */
  724. if (xfs_is_cow_inode(ip) && lockmode == XFS_ILOCK_SHARED) {
  725. xfs_iunlock(ip, lockmode);
  726. lockmode = XFS_ILOCK_EXCL;
  727. goto relock;
  728. }
  729. error = xfs_bmapi_read(ip, offset_fsb, end_fsb - offset_fsb, &imap,
  730. &nimaps, 0);
  731. if (error)
  732. goto out_unlock;
  733. if (imap_needs_cow(ip, flags, &imap, nimaps)) {
  734. error = -EAGAIN;
  735. if (flags & IOMAP_NOWAIT)
  736. goto out_unlock;
  737. /* may drop and re-acquire the ilock */
  738. error = xfs_reflink_allocate_cow(ip, &imap, &cmap, &shared,
  739. &lockmode,
  740. (flags & IOMAP_DIRECT) || IS_DAX(inode));
  741. if (error)
  742. goto out_unlock;
  743. if (shared)
  744. goto out_found_cow;
  745. end_fsb = imap.br_startoff + imap.br_blockcount;
  746. length = XFS_FSB_TO_B(mp, end_fsb) - offset;
  747. }
  748. if (imap_needs_alloc(inode, flags, &imap, nimaps))
  749. goto allocate_blocks;
  750. /*
  751. * NOWAIT and OVERWRITE I/O needs to span the entire requested I/O with
  752. * a single map so that we avoid partial IO failures due to the rest of
  753. * the I/O range not covered by this map triggering an EAGAIN condition
  754. * when it is subsequently mapped and aborting the I/O.
  755. */
  756. if (flags & (IOMAP_NOWAIT | IOMAP_OVERWRITE_ONLY)) {
  757. error = -EAGAIN;
  758. if (!imap_spans_range(&imap, offset_fsb, end_fsb))
  759. goto out_unlock;
  760. }
  761. /*
  762. * For overwrite only I/O, we cannot convert unwritten extents without
  763. * requiring sub-block zeroing. This can only be done under an
  764. * exclusive IOLOCK, hence return -EAGAIN if this is not a written
  765. * extent to tell the caller to try again.
  766. */
  767. if (flags & IOMAP_OVERWRITE_ONLY) {
  768. error = -EAGAIN;
  769. if (imap.br_state != XFS_EXT_NORM &&
  770. ((offset | length) & mp->m_blockmask))
  771. goto out_unlock;
  772. }
  773. seq = xfs_iomap_inode_sequence(ip, iomap_flags);
  774. xfs_iunlock(ip, lockmode);
  775. trace_xfs_iomap_found(ip, offset, length, XFS_DATA_FORK, &imap);
  776. return xfs_bmbt_to_iomap(ip, iomap, &imap, flags, iomap_flags, seq);
  777. allocate_blocks:
  778. error = -EAGAIN;
  779. if (flags & (IOMAP_NOWAIT | IOMAP_OVERWRITE_ONLY))
  780. goto out_unlock;
  781. /*
  782. * We cap the maximum length we map to a sane size to keep the chunks
  783. * of work done where somewhat symmetric with the work writeback does.
  784. * This is a completely arbitrary number pulled out of thin air as a
  785. * best guess for initial testing.
  786. *
  787. * Note that the values needs to be less than 32-bits wide until the
  788. * lower level functions are updated.
  789. */
  790. length = min_t(loff_t, length, 1024 * PAGE_SIZE);
  791. end_fsb = xfs_iomap_end_fsb(mp, offset, length);
  792. if (offset + length > XFS_ISIZE(ip))
  793. end_fsb = xfs_iomap_eof_align_last_fsb(ip, end_fsb);
  794. else if (nimaps && imap.br_startblock == HOLESTARTBLOCK)
  795. end_fsb = min(end_fsb, imap.br_startoff + imap.br_blockcount);
  796. xfs_iunlock(ip, lockmode);
  797. error = xfs_iomap_write_direct(ip, offset_fsb, end_fsb - offset_fsb,
  798. flags, &imap, &seq);
  799. if (error)
  800. return error;
  801. trace_xfs_iomap_alloc(ip, offset, length, XFS_DATA_FORK, &imap);
  802. return xfs_bmbt_to_iomap(ip, iomap, &imap, flags,
  803. iomap_flags | IOMAP_F_NEW, seq);
  804. out_found_cow:
  805. length = XFS_FSB_TO_B(mp, cmap.br_startoff + cmap.br_blockcount);
  806. trace_xfs_iomap_found(ip, offset, length - offset, XFS_COW_FORK, &cmap);
  807. if (imap.br_startblock != HOLESTARTBLOCK) {
  808. seq = xfs_iomap_inode_sequence(ip, 0);
  809. error = xfs_bmbt_to_iomap(ip, srcmap, &imap, flags, 0, seq);
  810. if (error)
  811. goto out_unlock;
  812. }
  813. seq = xfs_iomap_inode_sequence(ip, IOMAP_F_SHARED);
  814. xfs_iunlock(ip, lockmode);
  815. return xfs_bmbt_to_iomap(ip, iomap, &cmap, flags, IOMAP_F_SHARED, seq);
  816. out_unlock:
  817. if (lockmode)
  818. xfs_iunlock(ip, lockmode);
  819. return error;
  820. }
  821. const struct iomap_ops xfs_direct_write_iomap_ops = {
  822. .iomap_begin = xfs_direct_write_iomap_begin,
  823. };
  824. static int
  825. xfs_dax_write_iomap_end(
  826. struct inode *inode,
  827. loff_t pos,
  828. loff_t length,
  829. ssize_t written,
  830. unsigned flags,
  831. struct iomap *iomap)
  832. {
  833. struct xfs_inode *ip = XFS_I(inode);
  834. if (!xfs_is_cow_inode(ip))
  835. return 0;
  836. if (!written)
  837. return xfs_reflink_cancel_cow_range(ip, pos, length, true);
  838. return xfs_reflink_end_cow(ip, pos, written);
  839. }
  840. const struct iomap_ops xfs_dax_write_iomap_ops = {
  841. .iomap_begin = xfs_direct_write_iomap_begin,
  842. .iomap_end = xfs_dax_write_iomap_end,
  843. };
  844. static int
  845. xfs_buffered_write_iomap_begin(
  846. struct inode *inode,
  847. loff_t offset,
  848. loff_t count,
  849. unsigned flags,
  850. struct iomap *iomap,
  851. struct iomap *srcmap)
  852. {
  853. struct xfs_inode *ip = XFS_I(inode);
  854. struct xfs_mount *mp = ip->i_mount;
  855. xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset);
  856. xfs_fileoff_t end_fsb = xfs_iomap_end_fsb(mp, offset, count);
  857. struct xfs_bmbt_irec imap, cmap;
  858. struct xfs_iext_cursor icur, ccur;
  859. xfs_fsblock_t prealloc_blocks = 0;
  860. bool eof = false, cow_eof = false, shared = false;
  861. int allocfork = XFS_DATA_FORK;
  862. int error = 0;
  863. unsigned int lockmode = XFS_ILOCK_EXCL;
  864. unsigned int iomap_flags = 0;
  865. u64 seq;
  866. if (xfs_is_shutdown(mp))
  867. return -EIO;
  868. /* we can't use delayed allocations when using extent size hints */
  869. if (xfs_get_extsz_hint(ip))
  870. return xfs_direct_write_iomap_begin(inode, offset, count,
  871. flags, iomap, srcmap);
  872. error = xfs_qm_dqattach(ip);
  873. if (error)
  874. return error;
  875. error = xfs_ilock_for_iomap(ip, flags, &lockmode);
  876. if (error)
  877. return error;
  878. if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(&ip->i_df)) ||
  879. XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) {
  880. xfs_bmap_mark_sick(ip, XFS_DATA_FORK);
  881. error = -EFSCORRUPTED;
  882. goto out_unlock;
  883. }
  884. XFS_STATS_INC(mp, xs_blk_mapw);
  885. error = xfs_iread_extents(NULL, ip, XFS_DATA_FORK);
  886. if (error)
  887. goto out_unlock;
  888. /*
  889. * Search the data fork first to look up our source mapping. We
  890. * always need the data fork map, as we have to return it to the
  891. * iomap code so that the higher level write code can read data in to
  892. * perform read-modify-write cycles for unaligned writes.
  893. */
  894. eof = !xfs_iext_lookup_extent(ip, &ip->i_df, offset_fsb, &icur, &imap);
  895. if (eof)
  896. imap.br_startoff = end_fsb; /* fake hole until the end */
  897. /* We never need to allocate blocks for zeroing or unsharing a hole. */
  898. if ((flags & (IOMAP_UNSHARE | IOMAP_ZERO)) &&
  899. imap.br_startoff > offset_fsb) {
  900. xfs_hole_to_iomap(ip, iomap, offset_fsb, imap.br_startoff);
  901. goto out_unlock;
  902. }
  903. /*
  904. * For zeroing, trim a delalloc extent that extends beyond the EOF
  905. * block. If it starts beyond the EOF block, convert it to an
  906. * unwritten extent.
  907. */
  908. if ((flags & IOMAP_ZERO) && imap.br_startoff <= offset_fsb &&
  909. isnullstartblock(imap.br_startblock)) {
  910. xfs_fileoff_t eof_fsb = XFS_B_TO_FSB(mp, XFS_ISIZE(ip));
  911. if (offset_fsb >= eof_fsb)
  912. goto convert_delay;
  913. if (end_fsb > eof_fsb) {
  914. end_fsb = eof_fsb;
  915. xfs_trim_extent(&imap, offset_fsb,
  916. end_fsb - offset_fsb);
  917. }
  918. }
  919. /*
  920. * Search the COW fork extent list even if we did not find a data fork
  921. * extent. This serves two purposes: first this implements the
  922. * speculative preallocation using cowextsize, so that we also unshare
  923. * block adjacent to shared blocks instead of just the shared blocks
  924. * themselves. Second the lookup in the extent list is generally faster
  925. * than going out to the shared extent tree.
  926. */
  927. if (xfs_is_cow_inode(ip)) {
  928. if (!ip->i_cowfp) {
  929. ASSERT(!xfs_is_reflink_inode(ip));
  930. xfs_ifork_init_cow(ip);
  931. }
  932. cow_eof = !xfs_iext_lookup_extent(ip, ip->i_cowfp, offset_fsb,
  933. &ccur, &cmap);
  934. if (!cow_eof && cmap.br_startoff <= offset_fsb) {
  935. trace_xfs_reflink_cow_found(ip, &cmap);
  936. goto found_cow;
  937. }
  938. }
  939. if (imap.br_startoff <= offset_fsb) {
  940. /*
  941. * For reflink files we may need a delalloc reservation when
  942. * overwriting shared extents. This includes zeroing of
  943. * existing extents that contain data.
  944. */
  945. if (!xfs_is_cow_inode(ip) ||
  946. ((flags & IOMAP_ZERO) && imap.br_state != XFS_EXT_NORM)) {
  947. trace_xfs_iomap_found(ip, offset, count, XFS_DATA_FORK,
  948. &imap);
  949. goto found_imap;
  950. }
  951. xfs_trim_extent(&imap, offset_fsb, end_fsb - offset_fsb);
  952. /* Trim the mapping to the nearest shared extent boundary. */
  953. error = xfs_bmap_trim_cow(ip, &imap, &shared);
  954. if (error)
  955. goto out_unlock;
  956. /* Not shared? Just report the (potentially capped) extent. */
  957. if (!shared) {
  958. trace_xfs_iomap_found(ip, offset, count, XFS_DATA_FORK,
  959. &imap);
  960. goto found_imap;
  961. }
  962. /*
  963. * Fork all the shared blocks from our write offset until the
  964. * end of the extent.
  965. */
  966. allocfork = XFS_COW_FORK;
  967. end_fsb = imap.br_startoff + imap.br_blockcount;
  968. } else {
  969. /*
  970. * We cap the maximum length we map here to MAX_WRITEBACK_PAGES
  971. * pages to keep the chunks of work done where somewhat
  972. * symmetric with the work writeback does. This is a completely
  973. * arbitrary number pulled out of thin air.
  974. *
  975. * Note that the values needs to be less than 32-bits wide until
  976. * the lower level functions are updated.
  977. */
  978. count = min_t(loff_t, count, 1024 * PAGE_SIZE);
  979. end_fsb = xfs_iomap_end_fsb(mp, offset, count);
  980. if (xfs_is_always_cow_inode(ip))
  981. allocfork = XFS_COW_FORK;
  982. }
  983. if (eof && offset + count > XFS_ISIZE(ip)) {
  984. /*
  985. * Determine the initial size of the preallocation.
  986. * We clean up any extra preallocation when the file is closed.
  987. */
  988. if (xfs_has_allocsize(mp))
  989. prealloc_blocks = mp->m_allocsize_blocks;
  990. else if (allocfork == XFS_DATA_FORK)
  991. prealloc_blocks = xfs_iomap_prealloc_size(ip, allocfork,
  992. offset, count, &icur);
  993. else
  994. prealloc_blocks = xfs_iomap_prealloc_size(ip, allocfork,
  995. offset, count, &ccur);
  996. if (prealloc_blocks) {
  997. xfs_extlen_t align;
  998. xfs_off_t end_offset;
  999. xfs_fileoff_t p_end_fsb;
  1000. end_offset = XFS_ALLOC_ALIGN(mp, offset + count - 1);
  1001. p_end_fsb = XFS_B_TO_FSBT(mp, end_offset) +
  1002. prealloc_blocks;
  1003. align = xfs_eof_alignment(ip);
  1004. if (align)
  1005. p_end_fsb = roundup_64(p_end_fsb, align);
  1006. p_end_fsb = min(p_end_fsb,
  1007. XFS_B_TO_FSB(mp, mp->m_super->s_maxbytes));
  1008. ASSERT(p_end_fsb > offset_fsb);
  1009. prealloc_blocks = p_end_fsb - end_fsb;
  1010. }
  1011. }
  1012. /*
  1013. * Flag newly allocated delalloc blocks with IOMAP_F_NEW so we punch
  1014. * them out if the write happens to fail.
  1015. */
  1016. iomap_flags |= IOMAP_F_NEW;
  1017. if (allocfork == XFS_COW_FORK) {
  1018. error = xfs_bmapi_reserve_delalloc(ip, allocfork, offset_fsb,
  1019. end_fsb - offset_fsb, prealloc_blocks, &cmap,
  1020. &ccur, cow_eof);
  1021. if (error)
  1022. goto out_unlock;
  1023. trace_xfs_iomap_alloc(ip, offset, count, allocfork, &cmap);
  1024. goto found_cow;
  1025. }
  1026. error = xfs_bmapi_reserve_delalloc(ip, allocfork, offset_fsb,
  1027. end_fsb - offset_fsb, prealloc_blocks, &imap, &icur,
  1028. eof);
  1029. if (error)
  1030. goto out_unlock;
  1031. trace_xfs_iomap_alloc(ip, offset, count, allocfork, &imap);
  1032. found_imap:
  1033. seq = xfs_iomap_inode_sequence(ip, iomap_flags);
  1034. xfs_iunlock(ip, lockmode);
  1035. return xfs_bmbt_to_iomap(ip, iomap, &imap, flags, iomap_flags, seq);
  1036. convert_delay:
  1037. xfs_iunlock(ip, lockmode);
  1038. truncate_pagecache(inode, offset);
  1039. error = xfs_bmapi_convert_delalloc(ip, XFS_DATA_FORK, offset,
  1040. iomap, NULL);
  1041. if (error)
  1042. return error;
  1043. trace_xfs_iomap_alloc(ip, offset, count, XFS_DATA_FORK, &imap);
  1044. return 0;
  1045. found_cow:
  1046. if (imap.br_startoff <= offset_fsb) {
  1047. error = xfs_bmbt_to_iomap(ip, srcmap, &imap, flags, 0,
  1048. xfs_iomap_inode_sequence(ip, 0));
  1049. if (error)
  1050. goto out_unlock;
  1051. } else {
  1052. xfs_trim_extent(&cmap, offset_fsb,
  1053. imap.br_startoff - offset_fsb);
  1054. }
  1055. iomap_flags |= IOMAP_F_SHARED;
  1056. seq = xfs_iomap_inode_sequence(ip, iomap_flags);
  1057. xfs_iunlock(ip, lockmode);
  1058. return xfs_bmbt_to_iomap(ip, iomap, &cmap, flags, iomap_flags, seq);
  1059. out_unlock:
  1060. xfs_iunlock(ip, lockmode);
  1061. return error;
  1062. }
  1063. static void
  1064. xfs_buffered_write_delalloc_punch(
  1065. struct inode *inode,
  1066. loff_t offset,
  1067. loff_t length,
  1068. struct iomap *iomap)
  1069. {
  1070. xfs_bmap_punch_delalloc_range(XFS_I(inode),
  1071. (iomap->flags & IOMAP_F_SHARED) ?
  1072. XFS_COW_FORK : XFS_DATA_FORK,
  1073. offset, offset + length);
  1074. }
  1075. static int
  1076. xfs_buffered_write_iomap_end(
  1077. struct inode *inode,
  1078. loff_t offset,
  1079. loff_t length,
  1080. ssize_t written,
  1081. unsigned flags,
  1082. struct iomap *iomap)
  1083. {
  1084. loff_t start_byte, end_byte;
  1085. /* If we didn't reserve the blocks, we're not allowed to punch them. */
  1086. if (iomap->type != IOMAP_DELALLOC || !(iomap->flags & IOMAP_F_NEW))
  1087. return 0;
  1088. /* Nothing to do if we've written the entire delalloc extent */
  1089. start_byte = iomap_last_written_block(inode, offset, written);
  1090. end_byte = round_up(offset + length, i_blocksize(inode));
  1091. if (start_byte >= end_byte)
  1092. return 0;
  1093. /* For zeroing operations the callers already hold invalidate_lock. */
  1094. if (flags & (IOMAP_UNSHARE | IOMAP_ZERO)) {
  1095. rwsem_assert_held_write(&inode->i_mapping->invalidate_lock);
  1096. iomap_write_delalloc_release(inode, start_byte, end_byte, flags,
  1097. iomap, xfs_buffered_write_delalloc_punch);
  1098. } else {
  1099. filemap_invalidate_lock(inode->i_mapping);
  1100. iomap_write_delalloc_release(inode, start_byte, end_byte, flags,
  1101. iomap, xfs_buffered_write_delalloc_punch);
  1102. filemap_invalidate_unlock(inode->i_mapping);
  1103. }
  1104. return 0;
  1105. }
  1106. const struct iomap_ops xfs_buffered_write_iomap_ops = {
  1107. .iomap_begin = xfs_buffered_write_iomap_begin,
  1108. .iomap_end = xfs_buffered_write_iomap_end,
  1109. };
  1110. /*
  1111. * iomap_page_mkwrite() will never fail in a way that requires delalloc extents
  1112. * that it allocated to be revoked. Hence we do not need an .iomap_end method
  1113. * for this operation.
  1114. */
  1115. const struct iomap_ops xfs_page_mkwrite_iomap_ops = {
  1116. .iomap_begin = xfs_buffered_write_iomap_begin,
  1117. };
  1118. static int
  1119. xfs_read_iomap_begin(
  1120. struct inode *inode,
  1121. loff_t offset,
  1122. loff_t length,
  1123. unsigned flags,
  1124. struct iomap *iomap,
  1125. struct iomap *srcmap)
  1126. {
  1127. struct xfs_inode *ip = XFS_I(inode);
  1128. struct xfs_mount *mp = ip->i_mount;
  1129. struct xfs_bmbt_irec imap;
  1130. xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset);
  1131. xfs_fileoff_t end_fsb = xfs_iomap_end_fsb(mp, offset, length);
  1132. int nimaps = 1, error = 0;
  1133. bool shared = false;
  1134. unsigned int lockmode = XFS_ILOCK_SHARED;
  1135. u64 seq;
  1136. ASSERT(!(flags & (IOMAP_WRITE | IOMAP_ZERO)));
  1137. if (xfs_is_shutdown(mp))
  1138. return -EIO;
  1139. error = xfs_ilock_for_iomap(ip, flags, &lockmode);
  1140. if (error)
  1141. return error;
  1142. error = xfs_bmapi_read(ip, offset_fsb, end_fsb - offset_fsb, &imap,
  1143. &nimaps, 0);
  1144. if (!error && ((flags & IOMAP_REPORT) || IS_DAX(inode)))
  1145. error = xfs_reflink_trim_around_shared(ip, &imap, &shared);
  1146. seq = xfs_iomap_inode_sequence(ip, shared ? IOMAP_F_SHARED : 0);
  1147. xfs_iunlock(ip, lockmode);
  1148. if (error)
  1149. return error;
  1150. trace_xfs_iomap_found(ip, offset, length, XFS_DATA_FORK, &imap);
  1151. return xfs_bmbt_to_iomap(ip, iomap, &imap, flags,
  1152. shared ? IOMAP_F_SHARED : 0, seq);
  1153. }
  1154. const struct iomap_ops xfs_read_iomap_ops = {
  1155. .iomap_begin = xfs_read_iomap_begin,
  1156. };
  1157. static int
  1158. xfs_seek_iomap_begin(
  1159. struct inode *inode,
  1160. loff_t offset,
  1161. loff_t length,
  1162. unsigned flags,
  1163. struct iomap *iomap,
  1164. struct iomap *srcmap)
  1165. {
  1166. struct xfs_inode *ip = XFS_I(inode);
  1167. struct xfs_mount *mp = ip->i_mount;
  1168. xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset);
  1169. xfs_fileoff_t end_fsb = XFS_B_TO_FSB(mp, offset + length);
  1170. xfs_fileoff_t cow_fsb = NULLFILEOFF, data_fsb = NULLFILEOFF;
  1171. struct xfs_iext_cursor icur;
  1172. struct xfs_bmbt_irec imap, cmap;
  1173. int error = 0;
  1174. unsigned lockmode;
  1175. u64 seq;
  1176. if (xfs_is_shutdown(mp))
  1177. return -EIO;
  1178. lockmode = xfs_ilock_data_map_shared(ip);
  1179. error = xfs_iread_extents(NULL, ip, XFS_DATA_FORK);
  1180. if (error)
  1181. goto out_unlock;
  1182. if (xfs_iext_lookup_extent(ip, &ip->i_df, offset_fsb, &icur, &imap)) {
  1183. /*
  1184. * If we found a data extent we are done.
  1185. */
  1186. if (imap.br_startoff <= offset_fsb)
  1187. goto done;
  1188. data_fsb = imap.br_startoff;
  1189. } else {
  1190. /*
  1191. * Fake a hole until the end of the file.
  1192. */
  1193. data_fsb = xfs_iomap_end_fsb(mp, offset, length);
  1194. }
  1195. /*
  1196. * If a COW fork extent covers the hole, report it - capped to the next
  1197. * data fork extent:
  1198. */
  1199. if (xfs_inode_has_cow_data(ip) &&
  1200. xfs_iext_lookup_extent(ip, ip->i_cowfp, offset_fsb, &icur, &cmap))
  1201. cow_fsb = cmap.br_startoff;
  1202. if (cow_fsb != NULLFILEOFF && cow_fsb <= offset_fsb) {
  1203. if (data_fsb < cow_fsb + cmap.br_blockcount)
  1204. end_fsb = min(end_fsb, data_fsb);
  1205. xfs_trim_extent(&cmap, offset_fsb, end_fsb - offset_fsb);
  1206. seq = xfs_iomap_inode_sequence(ip, IOMAP_F_SHARED);
  1207. error = xfs_bmbt_to_iomap(ip, iomap, &cmap, flags,
  1208. IOMAP_F_SHARED, seq);
  1209. /*
  1210. * This is a COW extent, so we must probe the page cache
  1211. * because there could be dirty page cache being backed
  1212. * by this extent.
  1213. */
  1214. iomap->type = IOMAP_UNWRITTEN;
  1215. goto out_unlock;
  1216. }
  1217. /*
  1218. * Else report a hole, capped to the next found data or COW extent.
  1219. */
  1220. if (cow_fsb != NULLFILEOFF && cow_fsb < data_fsb)
  1221. imap.br_blockcount = cow_fsb - offset_fsb;
  1222. else
  1223. imap.br_blockcount = data_fsb - offset_fsb;
  1224. imap.br_startoff = offset_fsb;
  1225. imap.br_startblock = HOLESTARTBLOCK;
  1226. imap.br_state = XFS_EXT_NORM;
  1227. done:
  1228. seq = xfs_iomap_inode_sequence(ip, 0);
  1229. xfs_trim_extent(&imap, offset_fsb, end_fsb - offset_fsb);
  1230. error = xfs_bmbt_to_iomap(ip, iomap, &imap, flags, 0, seq);
  1231. out_unlock:
  1232. xfs_iunlock(ip, lockmode);
  1233. return error;
  1234. }
  1235. const struct iomap_ops xfs_seek_iomap_ops = {
  1236. .iomap_begin = xfs_seek_iomap_begin,
  1237. };
  1238. static int
  1239. xfs_xattr_iomap_begin(
  1240. struct inode *inode,
  1241. loff_t offset,
  1242. loff_t length,
  1243. unsigned flags,
  1244. struct iomap *iomap,
  1245. struct iomap *srcmap)
  1246. {
  1247. struct xfs_inode *ip = XFS_I(inode);
  1248. struct xfs_mount *mp = ip->i_mount;
  1249. xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset);
  1250. xfs_fileoff_t end_fsb = XFS_B_TO_FSB(mp, offset + length);
  1251. struct xfs_bmbt_irec imap;
  1252. int nimaps = 1, error = 0;
  1253. unsigned lockmode;
  1254. int seq;
  1255. if (xfs_is_shutdown(mp))
  1256. return -EIO;
  1257. lockmode = xfs_ilock_attr_map_shared(ip);
  1258. /* if there are no attribute fork or extents, return ENOENT */
  1259. if (!xfs_inode_has_attr_fork(ip) || !ip->i_af.if_nextents) {
  1260. error = -ENOENT;
  1261. goto out_unlock;
  1262. }
  1263. ASSERT(ip->i_af.if_format != XFS_DINODE_FMT_LOCAL);
  1264. error = xfs_bmapi_read(ip, offset_fsb, end_fsb - offset_fsb, &imap,
  1265. &nimaps, XFS_BMAPI_ATTRFORK);
  1266. out_unlock:
  1267. seq = xfs_iomap_inode_sequence(ip, IOMAP_F_XATTR);
  1268. xfs_iunlock(ip, lockmode);
  1269. if (error)
  1270. return error;
  1271. ASSERT(nimaps);
  1272. return xfs_bmbt_to_iomap(ip, iomap, &imap, flags, IOMAP_F_XATTR, seq);
  1273. }
  1274. const struct iomap_ops xfs_xattr_iomap_ops = {
  1275. .iomap_begin = xfs_xattr_iomap_begin,
  1276. };
  1277. int
  1278. xfs_zero_range(
  1279. struct xfs_inode *ip,
  1280. loff_t pos,
  1281. loff_t len,
  1282. bool *did_zero)
  1283. {
  1284. struct inode *inode = VFS_I(ip);
  1285. xfs_assert_ilocked(ip, XFS_IOLOCK_EXCL | XFS_MMAPLOCK_EXCL);
  1286. if (IS_DAX(inode))
  1287. return dax_zero_range(inode, pos, len, did_zero,
  1288. &xfs_dax_write_iomap_ops);
  1289. return iomap_zero_range(inode, pos, len, did_zero,
  1290. &xfs_buffered_write_iomap_ops);
  1291. }
  1292. int
  1293. xfs_truncate_page(
  1294. struct xfs_inode *ip,
  1295. loff_t pos,
  1296. bool *did_zero)
  1297. {
  1298. struct inode *inode = VFS_I(ip);
  1299. if (IS_DAX(inode))
  1300. return dax_truncate_page(inode, pos, did_zero,
  1301. &xfs_dax_write_iomap_ops);
  1302. return iomap_truncate_page(inode, pos, did_zero,
  1303. &xfs_buffered_write_iomap_ops);
  1304. }