xfs_iomap.c 24 KB

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