xfs_iomap.c 25 KB

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