xfs_iomap.c 25 KB

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