xfs_iomap.c 25 KB

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