xfs_iomap.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961
  1. /*
  2. * Copyright (c) 2000-2006 Silicon Graphics, Inc.
  3. * All Rights Reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it would be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write the Free Software Foundation,
  16. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "xfs.h"
  19. #include "xfs_fs.h"
  20. #include "xfs_shared.h"
  21. #include "xfs_format.h"
  22. #include "xfs_log_format.h"
  23. #include "xfs_trans_resv.h"
  24. #include "xfs_mount.h"
  25. #include "xfs_inode.h"
  26. #include "xfs_btree.h"
  27. #include "xfs_bmap_btree.h"
  28. #include "xfs_bmap.h"
  29. #include "xfs_bmap_util.h"
  30. #include "xfs_error.h"
  31. #include "xfs_trans.h"
  32. #include "xfs_trans_space.h"
  33. #include "xfs_iomap.h"
  34. #include "xfs_trace.h"
  35. #include "xfs_icache.h"
  36. #include "xfs_quota.h"
  37. #include "xfs_dquot_item.h"
  38. #include "xfs_dquot.h"
  39. #define XFS_WRITEIO_ALIGN(mp,off) (((off) >> mp->m_writeio_log) \
  40. << mp->m_writeio_log)
  41. #define XFS_WRITE_IMAPS XFS_BMAP_MAX_NMAP
  42. STATIC int
  43. xfs_iomap_eof_align_last_fsb(
  44. xfs_mount_t *mp,
  45. xfs_inode_t *ip,
  46. xfs_extlen_t extsize,
  47. xfs_fileoff_t *last_fsb)
  48. {
  49. xfs_extlen_t align = 0;
  50. int eof, error;
  51. if (!XFS_IS_REALTIME_INODE(ip)) {
  52. /*
  53. * Round up the allocation request to a stripe unit
  54. * (m_dalign) boundary if the file size is >= stripe unit
  55. * size, and we are allocating past the allocation eof.
  56. *
  57. * If mounted with the "-o swalloc" option the alignment is
  58. * increased from the strip unit size to the stripe width.
  59. */
  60. if (mp->m_swidth && (mp->m_flags & XFS_MOUNT_SWALLOC))
  61. align = mp->m_swidth;
  62. else if (mp->m_dalign)
  63. align = mp->m_dalign;
  64. if (align && XFS_ISIZE(ip) < XFS_FSB_TO_B(mp, align))
  65. align = 0;
  66. }
  67. /*
  68. * Always round up the allocation request to an extent boundary
  69. * (when file on a real-time subvolume or has di_extsize hint).
  70. */
  71. if (extsize) {
  72. if (align)
  73. align = roundup_64(align, extsize);
  74. else
  75. align = extsize;
  76. }
  77. if (align) {
  78. xfs_fileoff_t new_last_fsb = roundup_64(*last_fsb, align);
  79. error = xfs_bmap_eof(ip, new_last_fsb, XFS_DATA_FORK, &eof);
  80. if (error)
  81. return error;
  82. if (eof)
  83. *last_fsb = new_last_fsb;
  84. }
  85. return 0;
  86. }
  87. STATIC int
  88. xfs_alert_fsblock_zero(
  89. xfs_inode_t *ip,
  90. xfs_bmbt_irec_t *imap)
  91. {
  92. xfs_alert_tag(ip->i_mount, XFS_PTAG_FSBLOCK_ZERO,
  93. "Access to block zero in inode %llu "
  94. "start_block: %llx start_off: %llx "
  95. "blkcnt: %llx extent-state: %x",
  96. (unsigned long long)ip->i_ino,
  97. (unsigned long long)imap->br_startblock,
  98. (unsigned long long)imap->br_startoff,
  99. (unsigned long long)imap->br_blockcount,
  100. imap->br_state);
  101. return -EFSCORRUPTED;
  102. }
  103. int
  104. xfs_iomap_write_direct(
  105. xfs_inode_t *ip,
  106. xfs_off_t offset,
  107. size_t count,
  108. xfs_bmbt_irec_t *imap,
  109. int nmaps)
  110. {
  111. xfs_mount_t *mp = ip->i_mount;
  112. xfs_fileoff_t offset_fsb;
  113. xfs_fileoff_t last_fsb;
  114. xfs_filblks_t count_fsb, resaligned;
  115. xfs_fsblock_t firstfsb;
  116. xfs_extlen_t extsz, temp;
  117. int nimaps;
  118. int quota_flag;
  119. int rt;
  120. xfs_trans_t *tp;
  121. xfs_bmap_free_t free_list;
  122. uint qblocks, resblks, resrtextents;
  123. int error;
  124. int lockmode;
  125. int bmapi_flags = XFS_BMAPI_PREALLOC;
  126. rt = XFS_IS_REALTIME_INODE(ip);
  127. extsz = xfs_get_extsz_hint(ip);
  128. lockmode = XFS_ILOCK_SHARED; /* locked by caller */
  129. ASSERT(xfs_isilocked(ip, lockmode));
  130. offset_fsb = XFS_B_TO_FSBT(mp, offset);
  131. last_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)(offset + count)));
  132. if ((offset + count) > XFS_ISIZE(ip)) {
  133. /*
  134. * Assert that the in-core extent list is present since this can
  135. * call xfs_iread_extents() and we only have the ilock shared.
  136. * This should be safe because the lock was held around a bmapi
  137. * call in the caller and we only need it to access the in-core
  138. * list.
  139. */
  140. ASSERT(XFS_IFORK_PTR(ip, XFS_DATA_FORK)->if_flags &
  141. XFS_IFEXTENTS);
  142. error = xfs_iomap_eof_align_last_fsb(mp, ip, extsz, &last_fsb);
  143. if (error)
  144. goto out_unlock;
  145. } else {
  146. if (nmaps && (imap->br_startblock == HOLESTARTBLOCK))
  147. last_fsb = MIN(last_fsb, (xfs_fileoff_t)
  148. imap->br_blockcount +
  149. imap->br_startoff);
  150. }
  151. count_fsb = last_fsb - offset_fsb;
  152. ASSERT(count_fsb > 0);
  153. resaligned = count_fsb;
  154. if (unlikely(extsz)) {
  155. if ((temp = do_mod(offset_fsb, extsz)))
  156. resaligned += temp;
  157. if ((temp = do_mod(resaligned, extsz)))
  158. resaligned += extsz - temp;
  159. }
  160. if (unlikely(rt)) {
  161. resrtextents = qblocks = resaligned;
  162. resrtextents /= mp->m_sb.sb_rextsize;
  163. resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
  164. quota_flag = XFS_QMOPT_RES_RTBLKS;
  165. } else {
  166. resrtextents = 0;
  167. resblks = qblocks = XFS_DIOSTRAT_SPACE_RES(mp, resaligned);
  168. quota_flag = XFS_QMOPT_RES_REGBLKS;
  169. }
  170. /*
  171. * Drop the shared lock acquired by the caller, attach the dquot if
  172. * necessary and move on to transaction setup.
  173. */
  174. xfs_iunlock(ip, lockmode);
  175. error = xfs_qm_dqattach(ip, 0);
  176. if (error)
  177. return error;
  178. /*
  179. * Allocate and setup the transaction
  180. */
  181. tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
  182. /*
  183. * For DAX, we do not allocate unwritten extents, but instead we zero
  184. * the block before we commit the transaction. Ideally we'd like to do
  185. * this outside the transaction context, but if we commit and then crash
  186. * we may not have zeroed the blocks and this will be exposed on
  187. * recovery of the allocation. Hence we must zero before commit.
  188. *
  189. * Further, if we are mapping unwritten extents here, we need to zero
  190. * and convert them to written so that we don't need an unwritten extent
  191. * callback for DAX. This also means that we need to be able to dip into
  192. * the reserve block pool for bmbt block allocation if there is no space
  193. * left but we need to do unwritten extent conversion.
  194. */
  195. if (IS_DAX(VFS_I(ip))) {
  196. bmapi_flags = XFS_BMAPI_CONVERT | XFS_BMAPI_ZERO;
  197. if (ISUNWRITTEN(imap)) {
  198. tp->t_flags |= XFS_TRANS_RESERVE;
  199. resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0) << 1;
  200. }
  201. }
  202. error = xfs_trans_reserve(tp, &M_RES(mp)->tr_write,
  203. resblks, resrtextents);
  204. /*
  205. * Check for running out of space, note: need lock to return
  206. */
  207. if (error) {
  208. xfs_trans_cancel(tp);
  209. return error;
  210. }
  211. lockmode = XFS_ILOCK_EXCL;
  212. xfs_ilock(ip, lockmode);
  213. error = xfs_trans_reserve_quota_nblks(tp, ip, qblocks, 0, quota_flag);
  214. if (error)
  215. goto out_trans_cancel;
  216. xfs_trans_ijoin(tp, ip, 0);
  217. /*
  218. * From this point onwards we overwrite the imap pointer that the
  219. * caller gave to us.
  220. */
  221. xfs_bmap_init(&free_list, &firstfsb);
  222. nimaps = 1;
  223. error = xfs_bmapi_write(tp, ip, offset_fsb, count_fsb,
  224. bmapi_flags, &firstfsb, resblks, imap,
  225. &nimaps, &free_list);
  226. if (error)
  227. goto out_bmap_cancel;
  228. /*
  229. * Complete the transaction
  230. */
  231. error = xfs_bmap_finish(&tp, &free_list, NULL);
  232. if (error)
  233. goto out_bmap_cancel;
  234. error = xfs_trans_commit(tp);
  235. if (error)
  236. goto out_unlock;
  237. /*
  238. * Copy any maps to caller's array and return any error.
  239. */
  240. if (nimaps == 0) {
  241. error = -ENOSPC;
  242. goto out_unlock;
  243. }
  244. if (!(imap->br_startblock || XFS_IS_REALTIME_INODE(ip)))
  245. error = xfs_alert_fsblock_zero(ip, imap);
  246. out_unlock:
  247. xfs_iunlock(ip, lockmode);
  248. return error;
  249. out_bmap_cancel:
  250. xfs_bmap_cancel(&free_list);
  251. xfs_trans_unreserve_quota_nblks(tp, ip, (long)qblocks, 0, quota_flag);
  252. out_trans_cancel:
  253. xfs_trans_cancel(tp);
  254. goto out_unlock;
  255. }
  256. /*
  257. * If the caller is doing a write at the end of the file, then extend the
  258. * allocation out to the file system's write iosize. We clean up any extra
  259. * space left over when the file is closed in xfs_inactive().
  260. *
  261. * If we find we already have delalloc preallocation beyond EOF, don't do more
  262. * preallocation as it it not needed.
  263. */
  264. STATIC int
  265. xfs_iomap_eof_want_preallocate(
  266. xfs_mount_t *mp,
  267. xfs_inode_t *ip,
  268. xfs_off_t offset,
  269. size_t count,
  270. xfs_bmbt_irec_t *imap,
  271. int nimaps,
  272. int *prealloc)
  273. {
  274. xfs_fileoff_t start_fsb;
  275. xfs_filblks_t count_fsb;
  276. int n, error, imaps;
  277. int found_delalloc = 0;
  278. *prealloc = 0;
  279. if (offset + count <= XFS_ISIZE(ip))
  280. return 0;
  281. /*
  282. * If the file is smaller than the minimum prealloc and we are using
  283. * dynamic preallocation, don't do any preallocation at all as it is
  284. * likely this is the only write to the file that is going to be done.
  285. */
  286. if (!(mp->m_flags & XFS_MOUNT_DFLT_IOSIZE) &&
  287. XFS_ISIZE(ip) < XFS_FSB_TO_B(mp, mp->m_writeio_blocks))
  288. return 0;
  289. /*
  290. * If there are any real blocks past eof, then don't
  291. * do any speculative allocation.
  292. */
  293. start_fsb = XFS_B_TO_FSBT(mp, ((xfs_ufsize_t)(offset + count - 1)));
  294. count_fsb = XFS_B_TO_FSB(mp, mp->m_super->s_maxbytes);
  295. while (count_fsb > 0) {
  296. imaps = nimaps;
  297. error = xfs_bmapi_read(ip, start_fsb, count_fsb, imap, &imaps,
  298. 0);
  299. if (error)
  300. return error;
  301. for (n = 0; n < imaps; n++) {
  302. if ((imap[n].br_startblock != HOLESTARTBLOCK) &&
  303. (imap[n].br_startblock != DELAYSTARTBLOCK))
  304. return 0;
  305. start_fsb += imap[n].br_blockcount;
  306. count_fsb -= imap[n].br_blockcount;
  307. if (imap[n].br_startblock == DELAYSTARTBLOCK)
  308. found_delalloc = 1;
  309. }
  310. }
  311. if (!found_delalloc)
  312. *prealloc = 1;
  313. return 0;
  314. }
  315. /*
  316. * Determine the initial size of the preallocation. We are beyond the current
  317. * EOF here, but we need to take into account whether this is a sparse write or
  318. * an extending write when determining the preallocation size. Hence we need to
  319. * look up the extent that ends at the current write offset and use the result
  320. * to determine the preallocation size.
  321. *
  322. * If the extent is a hole, then preallocation is essentially disabled.
  323. * Otherwise we take the size of the preceeding data extent as the basis for the
  324. * preallocation size. If the size of the extent is greater than half the
  325. * maximum extent length, then use the current offset as the basis. This ensures
  326. * that for large files the preallocation size always extends to MAXEXTLEN
  327. * rather than falling short due to things like stripe unit/width alignment of
  328. * real extents.
  329. */
  330. STATIC xfs_fsblock_t
  331. xfs_iomap_eof_prealloc_initial_size(
  332. struct xfs_mount *mp,
  333. struct xfs_inode *ip,
  334. xfs_off_t offset,
  335. xfs_bmbt_irec_t *imap,
  336. int nimaps)
  337. {
  338. xfs_fileoff_t start_fsb;
  339. int imaps = 1;
  340. int error;
  341. ASSERT(nimaps >= imaps);
  342. /* if we are using a specific prealloc size, return now */
  343. if (mp->m_flags & XFS_MOUNT_DFLT_IOSIZE)
  344. return 0;
  345. /* If the file is small, then use the minimum prealloc */
  346. if (XFS_ISIZE(ip) < XFS_FSB_TO_B(mp, mp->m_dalign))
  347. return 0;
  348. /*
  349. * As we write multiple pages, the offset will always align to the
  350. * start of a page and hence point to a hole at EOF. i.e. if the size is
  351. * 4096 bytes, we only have one block at FSB 0, but XFS_B_TO_FSB(4096)
  352. * will return FSB 1. Hence if there are blocks in the file, we want to
  353. * point to the block prior to the EOF block and not the hole that maps
  354. * directly at @offset.
  355. */
  356. start_fsb = XFS_B_TO_FSB(mp, offset);
  357. if (start_fsb)
  358. start_fsb--;
  359. error = xfs_bmapi_read(ip, start_fsb, 1, imap, &imaps, XFS_BMAPI_ENTIRE);
  360. if (error)
  361. return 0;
  362. ASSERT(imaps == 1);
  363. if (imap[0].br_startblock == HOLESTARTBLOCK)
  364. return 0;
  365. if (imap[0].br_blockcount <= (MAXEXTLEN >> 1))
  366. return imap[0].br_blockcount << 1;
  367. return XFS_B_TO_FSB(mp, offset);
  368. }
  369. STATIC bool
  370. xfs_quota_need_throttle(
  371. struct xfs_inode *ip,
  372. int type,
  373. xfs_fsblock_t alloc_blocks)
  374. {
  375. struct xfs_dquot *dq = xfs_inode_dquot(ip, type);
  376. if (!dq || !xfs_this_quota_on(ip->i_mount, type))
  377. return false;
  378. /* no hi watermark, no throttle */
  379. if (!dq->q_prealloc_hi_wmark)
  380. return false;
  381. /* under the lo watermark, no throttle */
  382. if (dq->q_res_bcount + alloc_blocks < dq->q_prealloc_lo_wmark)
  383. return false;
  384. return true;
  385. }
  386. STATIC void
  387. xfs_quota_calc_throttle(
  388. struct xfs_inode *ip,
  389. int type,
  390. xfs_fsblock_t *qblocks,
  391. int *qshift,
  392. int64_t *qfreesp)
  393. {
  394. int64_t freesp;
  395. int shift = 0;
  396. struct xfs_dquot *dq = xfs_inode_dquot(ip, type);
  397. /* no dq, or over hi wmark, squash the prealloc completely */
  398. if (!dq || dq->q_res_bcount >= dq->q_prealloc_hi_wmark) {
  399. *qblocks = 0;
  400. *qfreesp = 0;
  401. return;
  402. }
  403. freesp = dq->q_prealloc_hi_wmark - dq->q_res_bcount;
  404. if (freesp < dq->q_low_space[XFS_QLOWSP_5_PCNT]) {
  405. shift = 2;
  406. if (freesp < dq->q_low_space[XFS_QLOWSP_3_PCNT])
  407. shift += 2;
  408. if (freesp < dq->q_low_space[XFS_QLOWSP_1_PCNT])
  409. shift += 2;
  410. }
  411. if (freesp < *qfreesp)
  412. *qfreesp = freesp;
  413. /* only overwrite the throttle values if we are more aggressive */
  414. if ((freesp >> shift) < (*qblocks >> *qshift)) {
  415. *qblocks = freesp;
  416. *qshift = shift;
  417. }
  418. }
  419. /*
  420. * If we don't have a user specified preallocation size, dynamically increase
  421. * the preallocation size as the size of the file grows. Cap the maximum size
  422. * at a single extent or less if the filesystem is near full. The closer the
  423. * filesystem is to full, the smaller the maximum prealocation.
  424. */
  425. STATIC xfs_fsblock_t
  426. xfs_iomap_prealloc_size(
  427. struct xfs_mount *mp,
  428. struct xfs_inode *ip,
  429. xfs_off_t offset,
  430. struct xfs_bmbt_irec *imap,
  431. int nimaps)
  432. {
  433. xfs_fsblock_t alloc_blocks = 0;
  434. int shift = 0;
  435. int64_t freesp;
  436. xfs_fsblock_t qblocks;
  437. int qshift = 0;
  438. alloc_blocks = xfs_iomap_eof_prealloc_initial_size(mp, ip, offset,
  439. imap, nimaps);
  440. if (!alloc_blocks)
  441. goto check_writeio;
  442. qblocks = alloc_blocks;
  443. /*
  444. * MAXEXTLEN is not a power of two value but we round the prealloc down
  445. * to the nearest power of two value after throttling. To prevent the
  446. * round down from unconditionally reducing the maximum supported prealloc
  447. * size, we round up first, apply appropriate throttling, round down and
  448. * cap the value to MAXEXTLEN.
  449. */
  450. alloc_blocks = XFS_FILEOFF_MIN(roundup_pow_of_two(MAXEXTLEN),
  451. alloc_blocks);
  452. freesp = percpu_counter_read_positive(&mp->m_fdblocks);
  453. if (freesp < mp->m_low_space[XFS_LOWSP_5_PCNT]) {
  454. shift = 2;
  455. if (freesp < mp->m_low_space[XFS_LOWSP_4_PCNT])
  456. shift++;
  457. if (freesp < mp->m_low_space[XFS_LOWSP_3_PCNT])
  458. shift++;
  459. if (freesp < mp->m_low_space[XFS_LOWSP_2_PCNT])
  460. shift++;
  461. if (freesp < mp->m_low_space[XFS_LOWSP_1_PCNT])
  462. shift++;
  463. }
  464. /*
  465. * Check each quota to cap the prealloc size, provide a shift value to
  466. * throttle with and adjust amount of available space.
  467. */
  468. if (xfs_quota_need_throttle(ip, XFS_DQ_USER, alloc_blocks))
  469. xfs_quota_calc_throttle(ip, XFS_DQ_USER, &qblocks, &qshift,
  470. &freesp);
  471. if (xfs_quota_need_throttle(ip, XFS_DQ_GROUP, alloc_blocks))
  472. xfs_quota_calc_throttle(ip, XFS_DQ_GROUP, &qblocks, &qshift,
  473. &freesp);
  474. if (xfs_quota_need_throttle(ip, XFS_DQ_PROJ, alloc_blocks))
  475. xfs_quota_calc_throttle(ip, XFS_DQ_PROJ, &qblocks, &qshift,
  476. &freesp);
  477. /*
  478. * The final prealloc size is set to the minimum of free space available
  479. * in each of the quotas and the overall filesystem.
  480. *
  481. * The shift throttle value is set to the maximum value as determined by
  482. * the global low free space values and per-quota low free space values.
  483. */
  484. alloc_blocks = MIN(alloc_blocks, qblocks);
  485. shift = MAX(shift, qshift);
  486. if (shift)
  487. alloc_blocks >>= shift;
  488. /*
  489. * rounddown_pow_of_two() returns an undefined result if we pass in
  490. * alloc_blocks = 0.
  491. */
  492. if (alloc_blocks)
  493. alloc_blocks = rounddown_pow_of_two(alloc_blocks);
  494. if (alloc_blocks > MAXEXTLEN)
  495. alloc_blocks = MAXEXTLEN;
  496. /*
  497. * If we are still trying to allocate more space than is
  498. * available, squash the prealloc hard. This can happen if we
  499. * have a large file on a small filesystem and the above
  500. * lowspace thresholds are smaller than MAXEXTLEN.
  501. */
  502. while (alloc_blocks && alloc_blocks >= freesp)
  503. alloc_blocks >>= 4;
  504. check_writeio:
  505. if (alloc_blocks < mp->m_writeio_blocks)
  506. alloc_blocks = mp->m_writeio_blocks;
  507. trace_xfs_iomap_prealloc_size(ip, alloc_blocks, shift,
  508. mp->m_writeio_blocks);
  509. return alloc_blocks;
  510. }
  511. int
  512. xfs_iomap_write_delay(
  513. xfs_inode_t *ip,
  514. xfs_off_t offset,
  515. size_t count,
  516. xfs_bmbt_irec_t *ret_imap)
  517. {
  518. xfs_mount_t *mp = ip->i_mount;
  519. xfs_fileoff_t offset_fsb;
  520. xfs_fileoff_t last_fsb;
  521. xfs_off_t aligned_offset;
  522. xfs_fileoff_t ioalign;
  523. xfs_extlen_t extsz;
  524. int nimaps;
  525. xfs_bmbt_irec_t imap[XFS_WRITE_IMAPS];
  526. int prealloc;
  527. int error;
  528. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
  529. /*
  530. * Make sure that the dquots are there. This doesn't hold
  531. * the ilock across a disk read.
  532. */
  533. error = xfs_qm_dqattach_locked(ip, 0);
  534. if (error)
  535. return error;
  536. extsz = xfs_get_extsz_hint(ip);
  537. offset_fsb = XFS_B_TO_FSBT(mp, offset);
  538. error = xfs_iomap_eof_want_preallocate(mp, ip, offset, count,
  539. imap, XFS_WRITE_IMAPS, &prealloc);
  540. if (error)
  541. return error;
  542. retry:
  543. if (prealloc) {
  544. xfs_fsblock_t alloc_blocks;
  545. alloc_blocks = xfs_iomap_prealloc_size(mp, ip, offset, imap,
  546. XFS_WRITE_IMAPS);
  547. aligned_offset = XFS_WRITEIO_ALIGN(mp, (offset + count - 1));
  548. ioalign = XFS_B_TO_FSBT(mp, aligned_offset);
  549. last_fsb = ioalign + alloc_blocks;
  550. } else {
  551. last_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)(offset + count)));
  552. }
  553. if (prealloc || extsz) {
  554. error = xfs_iomap_eof_align_last_fsb(mp, ip, extsz, &last_fsb);
  555. if (error)
  556. return error;
  557. }
  558. /*
  559. * Make sure preallocation does not create extents beyond the range we
  560. * actually support in this filesystem.
  561. */
  562. if (last_fsb > XFS_B_TO_FSB(mp, mp->m_super->s_maxbytes))
  563. last_fsb = XFS_B_TO_FSB(mp, mp->m_super->s_maxbytes);
  564. ASSERT(last_fsb > offset_fsb);
  565. nimaps = XFS_WRITE_IMAPS;
  566. error = xfs_bmapi_delay(ip, offset_fsb, last_fsb - offset_fsb,
  567. imap, &nimaps, XFS_BMAPI_ENTIRE);
  568. switch (error) {
  569. case 0:
  570. case -ENOSPC:
  571. case -EDQUOT:
  572. break;
  573. default:
  574. return error;
  575. }
  576. /*
  577. * If bmapi returned us nothing, we got either ENOSPC or EDQUOT. Retry
  578. * without EOF preallocation.
  579. */
  580. if (nimaps == 0) {
  581. trace_xfs_delalloc_enospc(ip, offset, count);
  582. if (prealloc) {
  583. prealloc = 0;
  584. error = 0;
  585. goto retry;
  586. }
  587. return error ? error : -ENOSPC;
  588. }
  589. if (!(imap[0].br_startblock || XFS_IS_REALTIME_INODE(ip)))
  590. return xfs_alert_fsblock_zero(ip, &imap[0]);
  591. /*
  592. * Tag the inode as speculatively preallocated so we can reclaim this
  593. * space on demand, if necessary.
  594. */
  595. if (prealloc)
  596. xfs_inode_set_eofblocks_tag(ip);
  597. *ret_imap = imap[0];
  598. return 0;
  599. }
  600. /*
  601. * Pass in a delayed allocate extent, convert it to real extents;
  602. * return to the caller the extent we create which maps on top of
  603. * the originating callers request.
  604. *
  605. * Called without a lock on the inode.
  606. *
  607. * We no longer bother to look at the incoming map - all we have to
  608. * guarantee is that whatever we allocate fills the required range.
  609. */
  610. int
  611. xfs_iomap_write_allocate(
  612. xfs_inode_t *ip,
  613. xfs_off_t offset,
  614. xfs_bmbt_irec_t *imap)
  615. {
  616. xfs_mount_t *mp = ip->i_mount;
  617. xfs_fileoff_t offset_fsb, last_block;
  618. xfs_fileoff_t end_fsb, map_start_fsb;
  619. xfs_fsblock_t first_block;
  620. xfs_bmap_free_t free_list;
  621. xfs_filblks_t count_fsb;
  622. xfs_trans_t *tp;
  623. int nimaps;
  624. int error = 0;
  625. int nres;
  626. /*
  627. * Make sure that the dquots are there.
  628. */
  629. error = xfs_qm_dqattach(ip, 0);
  630. if (error)
  631. return error;
  632. offset_fsb = XFS_B_TO_FSBT(mp, offset);
  633. count_fsb = imap->br_blockcount;
  634. map_start_fsb = imap->br_startoff;
  635. XFS_STATS_ADD(mp, xs_xstrat_bytes, XFS_FSB_TO_B(mp, count_fsb));
  636. while (count_fsb != 0) {
  637. /*
  638. * Set up a transaction with which to allocate the
  639. * backing store for the file. Do allocations in a
  640. * loop until we get some space in the range we are
  641. * interested in. The other space that might be allocated
  642. * is in the delayed allocation extent on which we sit
  643. * but before our buffer starts.
  644. */
  645. nimaps = 0;
  646. while (nimaps == 0) {
  647. tp = xfs_trans_alloc(mp, XFS_TRANS_STRAT_WRITE);
  648. tp->t_flags |= XFS_TRANS_RESERVE;
  649. nres = XFS_EXTENTADD_SPACE_RES(mp, XFS_DATA_FORK);
  650. error = xfs_trans_reserve(tp, &M_RES(mp)->tr_write,
  651. nres, 0);
  652. if (error) {
  653. xfs_trans_cancel(tp);
  654. return error;
  655. }
  656. xfs_ilock(ip, XFS_ILOCK_EXCL);
  657. xfs_trans_ijoin(tp, ip, 0);
  658. xfs_bmap_init(&free_list, &first_block);
  659. /*
  660. * it is possible that the extents have changed since
  661. * we did the read call as we dropped the ilock for a
  662. * while. We have to be careful about truncates or hole
  663. * punchs here - we are not allowed to allocate
  664. * non-delalloc blocks here.
  665. *
  666. * The only protection against truncation is the pages
  667. * for the range we are being asked to convert are
  668. * locked and hence a truncate will block on them
  669. * first.
  670. *
  671. * As a result, if we go beyond the range we really
  672. * need and hit an delalloc extent boundary followed by
  673. * a hole while we have excess blocks in the map, we
  674. * will fill the hole incorrectly and overrun the
  675. * transaction reservation.
  676. *
  677. * Using a single map prevents this as we are forced to
  678. * check each map we look for overlap with the desired
  679. * range and abort as soon as we find it. Also, given
  680. * that we only return a single map, having one beyond
  681. * what we can return is probably a bit silly.
  682. *
  683. * We also need to check that we don't go beyond EOF;
  684. * this is a truncate optimisation as a truncate sets
  685. * the new file size before block on the pages we
  686. * currently have locked under writeback. Because they
  687. * are about to be tossed, we don't need to write them
  688. * back....
  689. */
  690. nimaps = 1;
  691. end_fsb = XFS_B_TO_FSB(mp, XFS_ISIZE(ip));
  692. error = xfs_bmap_last_offset(ip, &last_block,
  693. XFS_DATA_FORK);
  694. if (error)
  695. goto trans_cancel;
  696. last_block = XFS_FILEOFF_MAX(last_block, end_fsb);
  697. if ((map_start_fsb + count_fsb) > last_block) {
  698. count_fsb = last_block - map_start_fsb;
  699. if (count_fsb == 0) {
  700. error = -EAGAIN;
  701. goto trans_cancel;
  702. }
  703. }
  704. /*
  705. * From this point onwards we overwrite the imap
  706. * pointer that the caller gave to us.
  707. */
  708. error = xfs_bmapi_write(tp, ip, map_start_fsb,
  709. count_fsb, 0, &first_block,
  710. nres, imap, &nimaps,
  711. &free_list);
  712. if (error)
  713. goto trans_cancel;
  714. error = xfs_bmap_finish(&tp, &free_list, NULL);
  715. if (error)
  716. goto trans_cancel;
  717. error = xfs_trans_commit(tp);
  718. if (error)
  719. goto error0;
  720. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  721. }
  722. /*
  723. * See if we were able to allocate an extent that
  724. * covers at least part of the callers request
  725. */
  726. if (!(imap->br_startblock || XFS_IS_REALTIME_INODE(ip)))
  727. return xfs_alert_fsblock_zero(ip, imap);
  728. if ((offset_fsb >= imap->br_startoff) &&
  729. (offset_fsb < (imap->br_startoff +
  730. imap->br_blockcount))) {
  731. XFS_STATS_INC(mp, xs_xstrat_quick);
  732. return 0;
  733. }
  734. /*
  735. * So far we have not mapped the requested part of the
  736. * file, just surrounding data, try again.
  737. */
  738. count_fsb -= imap->br_blockcount;
  739. map_start_fsb = imap->br_startoff + imap->br_blockcount;
  740. }
  741. trans_cancel:
  742. xfs_bmap_cancel(&free_list);
  743. xfs_trans_cancel(tp);
  744. error0:
  745. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  746. return error;
  747. }
  748. int
  749. xfs_iomap_write_unwritten(
  750. xfs_inode_t *ip,
  751. xfs_off_t offset,
  752. xfs_off_t count)
  753. {
  754. xfs_mount_t *mp = ip->i_mount;
  755. xfs_fileoff_t offset_fsb;
  756. xfs_filblks_t count_fsb;
  757. xfs_filblks_t numblks_fsb;
  758. xfs_fsblock_t firstfsb;
  759. int nimaps;
  760. xfs_trans_t *tp;
  761. xfs_bmbt_irec_t imap;
  762. xfs_bmap_free_t free_list;
  763. xfs_fsize_t i_size;
  764. uint resblks;
  765. int error;
  766. trace_xfs_unwritten_convert(ip, offset, count);
  767. offset_fsb = XFS_B_TO_FSBT(mp, offset);
  768. count_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)offset + count);
  769. count_fsb = (xfs_filblks_t)(count_fsb - offset_fsb);
  770. /*
  771. * Reserve enough blocks in this transaction for two complete extent
  772. * btree splits. We may be converting the middle part of an unwritten
  773. * extent and in this case we will insert two new extents in the btree
  774. * each of which could cause a full split.
  775. *
  776. * This reservation amount will be used in the first call to
  777. * xfs_bmbt_split() to select an AG with enough space to satisfy the
  778. * rest of the operation.
  779. */
  780. resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0) << 1;
  781. do {
  782. /*
  783. * set up a transaction to convert the range of extents
  784. * from unwritten to real. Do allocations in a loop until
  785. * we have covered the range passed in.
  786. *
  787. * Note that we open code the transaction allocation here
  788. * to pass KM_NOFS--we can't risk to recursing back into
  789. * the filesystem here as we might be asked to write out
  790. * the same inode that we complete here and might deadlock
  791. * on the iolock.
  792. */
  793. sb_start_intwrite(mp->m_super);
  794. tp = _xfs_trans_alloc(mp, XFS_TRANS_STRAT_WRITE, KM_NOFS);
  795. tp->t_flags |= XFS_TRANS_RESERVE | XFS_TRANS_FREEZE_PROT;
  796. error = xfs_trans_reserve(tp, &M_RES(mp)->tr_write,
  797. resblks, 0);
  798. if (error) {
  799. xfs_trans_cancel(tp);
  800. return error;
  801. }
  802. xfs_ilock(ip, XFS_ILOCK_EXCL);
  803. xfs_trans_ijoin(tp, ip, 0);
  804. /*
  805. * Modify the unwritten extent state of the buffer.
  806. */
  807. xfs_bmap_init(&free_list, &firstfsb);
  808. nimaps = 1;
  809. error = xfs_bmapi_write(tp, ip, offset_fsb, count_fsb,
  810. XFS_BMAPI_CONVERT, &firstfsb, resblks,
  811. &imap, &nimaps, &free_list);
  812. if (error)
  813. goto error_on_bmapi_transaction;
  814. /*
  815. * Log the updated inode size as we go. We have to be careful
  816. * to only log it up to the actual write offset if it is
  817. * halfway into a block.
  818. */
  819. i_size = XFS_FSB_TO_B(mp, offset_fsb + count_fsb);
  820. if (i_size > offset + count)
  821. i_size = offset + count;
  822. i_size = xfs_new_eof(ip, i_size);
  823. if (i_size) {
  824. ip->i_d.di_size = i_size;
  825. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  826. }
  827. error = xfs_bmap_finish(&tp, &free_list, NULL);
  828. if (error)
  829. goto error_on_bmapi_transaction;
  830. error = xfs_trans_commit(tp);
  831. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  832. if (error)
  833. return error;
  834. if (!(imap.br_startblock || XFS_IS_REALTIME_INODE(ip)))
  835. return xfs_alert_fsblock_zero(ip, &imap);
  836. if ((numblks_fsb = imap.br_blockcount) == 0) {
  837. /*
  838. * The numblks_fsb value should always get
  839. * smaller, otherwise the loop is stuck.
  840. */
  841. ASSERT(imap.br_blockcount);
  842. break;
  843. }
  844. offset_fsb += numblks_fsb;
  845. count_fsb -= numblks_fsb;
  846. } while (count_fsb > 0);
  847. return 0;
  848. error_on_bmapi_transaction:
  849. xfs_bmap_cancel(&free_list);
  850. xfs_trans_cancel(tp);
  851. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  852. return error;
  853. }