xfs_iomap.c 26 KB

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