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, 0);
  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, XFS_TRANS_RELEASE_LOG_RES);
  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, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
  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. xfs_icsb_sync_counters(mp, XFS_ICSB_LAZY_COUNT);
  415. freesp = mp->m_sb.sb_fdblocks;
  416. if (freesp < mp->m_low_space[XFS_LOWSP_5_PCNT]) {
  417. shift = 2;
  418. if (freesp < mp->m_low_space[XFS_LOWSP_4_PCNT])
  419. shift++;
  420. if (freesp < mp->m_low_space[XFS_LOWSP_3_PCNT])
  421. shift++;
  422. if (freesp < mp->m_low_space[XFS_LOWSP_2_PCNT])
  423. shift++;
  424. if (freesp < mp->m_low_space[XFS_LOWSP_1_PCNT])
  425. shift++;
  426. }
  427. /*
  428. * Check each quota to cap the prealloc size, provide a shift value to
  429. * throttle with and adjust amount of available space.
  430. */
  431. if (xfs_quota_need_throttle(ip, XFS_DQ_USER, alloc_blocks))
  432. xfs_quota_calc_throttle(ip, XFS_DQ_USER, &qblocks, &qshift,
  433. &freesp);
  434. if (xfs_quota_need_throttle(ip, XFS_DQ_GROUP, alloc_blocks))
  435. xfs_quota_calc_throttle(ip, XFS_DQ_GROUP, &qblocks, &qshift,
  436. &freesp);
  437. if (xfs_quota_need_throttle(ip, XFS_DQ_PROJ, alloc_blocks))
  438. xfs_quota_calc_throttle(ip, XFS_DQ_PROJ, &qblocks, &qshift,
  439. &freesp);
  440. /*
  441. * The final prealloc size is set to the minimum of free space available
  442. * in each of the quotas and the overall filesystem.
  443. *
  444. * The shift throttle value is set to the maximum value as determined by
  445. * the global low free space values and per-quota low free space values.
  446. */
  447. alloc_blocks = MIN(alloc_blocks, qblocks);
  448. shift = MAX(shift, qshift);
  449. if (shift)
  450. alloc_blocks >>= shift;
  451. /*
  452. * rounddown_pow_of_two() returns an undefined result if we pass in
  453. * alloc_blocks = 0.
  454. */
  455. if (alloc_blocks)
  456. alloc_blocks = rounddown_pow_of_two(alloc_blocks);
  457. if (alloc_blocks > MAXEXTLEN)
  458. alloc_blocks = MAXEXTLEN;
  459. /*
  460. * If we are still trying to allocate more space than is
  461. * available, squash the prealloc hard. This can happen if we
  462. * have a large file on a small filesystem and the above
  463. * lowspace thresholds are smaller than MAXEXTLEN.
  464. */
  465. while (alloc_blocks && alloc_blocks >= freesp)
  466. alloc_blocks >>= 4;
  467. check_writeio:
  468. if (alloc_blocks < mp->m_writeio_blocks)
  469. alloc_blocks = mp->m_writeio_blocks;
  470. trace_xfs_iomap_prealloc_size(ip, alloc_blocks, shift,
  471. mp->m_writeio_blocks);
  472. return alloc_blocks;
  473. }
  474. int
  475. xfs_iomap_write_delay(
  476. xfs_inode_t *ip,
  477. xfs_off_t offset,
  478. size_t count,
  479. xfs_bmbt_irec_t *ret_imap)
  480. {
  481. xfs_mount_t *mp = ip->i_mount;
  482. xfs_fileoff_t offset_fsb;
  483. xfs_fileoff_t last_fsb;
  484. xfs_off_t aligned_offset;
  485. xfs_fileoff_t ioalign;
  486. xfs_extlen_t extsz;
  487. int nimaps;
  488. xfs_bmbt_irec_t imap[XFS_WRITE_IMAPS];
  489. int prealloc;
  490. int error;
  491. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
  492. /*
  493. * Make sure that the dquots are there. This doesn't hold
  494. * the ilock across a disk read.
  495. */
  496. error = xfs_qm_dqattach_locked(ip, 0);
  497. if (error)
  498. return error;
  499. extsz = xfs_get_extsz_hint(ip);
  500. offset_fsb = XFS_B_TO_FSBT(mp, offset);
  501. error = xfs_iomap_eof_want_preallocate(mp, ip, offset, count,
  502. imap, XFS_WRITE_IMAPS, &prealloc);
  503. if (error)
  504. return error;
  505. retry:
  506. if (prealloc) {
  507. xfs_fsblock_t alloc_blocks;
  508. alloc_blocks = xfs_iomap_prealloc_size(mp, ip, offset, imap,
  509. XFS_WRITE_IMAPS);
  510. aligned_offset = XFS_WRITEIO_ALIGN(mp, (offset + count - 1));
  511. ioalign = XFS_B_TO_FSBT(mp, aligned_offset);
  512. last_fsb = ioalign + alloc_blocks;
  513. } else {
  514. last_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)(offset + count)));
  515. }
  516. if (prealloc || extsz) {
  517. error = xfs_iomap_eof_align_last_fsb(mp, ip, extsz, &last_fsb);
  518. if (error)
  519. return error;
  520. }
  521. /*
  522. * Make sure preallocation does not create extents beyond the range we
  523. * actually support in this filesystem.
  524. */
  525. if (last_fsb > XFS_B_TO_FSB(mp, mp->m_super->s_maxbytes))
  526. last_fsb = XFS_B_TO_FSB(mp, mp->m_super->s_maxbytes);
  527. ASSERT(last_fsb > offset_fsb);
  528. nimaps = XFS_WRITE_IMAPS;
  529. error = xfs_bmapi_delay(ip, offset_fsb, last_fsb - offset_fsb,
  530. imap, &nimaps, XFS_BMAPI_ENTIRE);
  531. switch (error) {
  532. case 0:
  533. case -ENOSPC:
  534. case -EDQUOT:
  535. break;
  536. default:
  537. return error;
  538. }
  539. /*
  540. * If bmapi returned us nothing, we got either ENOSPC or EDQUOT. Retry
  541. * without EOF preallocation.
  542. */
  543. if (nimaps == 0) {
  544. trace_xfs_delalloc_enospc(ip, offset, count);
  545. if (prealloc) {
  546. prealloc = 0;
  547. error = 0;
  548. goto retry;
  549. }
  550. return error ? error : -ENOSPC;
  551. }
  552. if (!(imap[0].br_startblock || XFS_IS_REALTIME_INODE(ip)))
  553. return xfs_alert_fsblock_zero(ip, &imap[0]);
  554. /*
  555. * Tag the inode as speculatively preallocated so we can reclaim this
  556. * space on demand, if necessary.
  557. */
  558. if (prealloc)
  559. xfs_inode_set_eofblocks_tag(ip);
  560. *ret_imap = imap[0];
  561. return 0;
  562. }
  563. /*
  564. * Pass in a delayed allocate extent, convert it to real extents;
  565. * return to the caller the extent we create which maps on top of
  566. * the originating callers request.
  567. *
  568. * Called without a lock on the inode.
  569. *
  570. * We no longer bother to look at the incoming map - all we have to
  571. * guarantee is that whatever we allocate fills the required range.
  572. */
  573. int
  574. xfs_iomap_write_allocate(
  575. xfs_inode_t *ip,
  576. xfs_off_t offset,
  577. xfs_bmbt_irec_t *imap)
  578. {
  579. xfs_mount_t *mp = ip->i_mount;
  580. xfs_fileoff_t offset_fsb, last_block;
  581. xfs_fileoff_t end_fsb, map_start_fsb;
  582. xfs_fsblock_t first_block;
  583. xfs_bmap_free_t free_list;
  584. xfs_filblks_t count_fsb;
  585. xfs_trans_t *tp;
  586. int nimaps, committed;
  587. int error = 0;
  588. int nres;
  589. /*
  590. * Make sure that the dquots are there.
  591. */
  592. error = xfs_qm_dqattach(ip, 0);
  593. if (error)
  594. return error;
  595. offset_fsb = XFS_B_TO_FSBT(mp, offset);
  596. count_fsb = imap->br_blockcount;
  597. map_start_fsb = imap->br_startoff;
  598. XFS_STATS_ADD(xs_xstrat_bytes, XFS_FSB_TO_B(mp, count_fsb));
  599. while (count_fsb != 0) {
  600. /*
  601. * Set up a transaction with which to allocate the
  602. * backing store for the file. Do allocations in a
  603. * loop until we get some space in the range we are
  604. * interested in. The other space that might be allocated
  605. * is in the delayed allocation extent on which we sit
  606. * but before our buffer starts.
  607. */
  608. nimaps = 0;
  609. while (nimaps == 0) {
  610. tp = xfs_trans_alloc(mp, XFS_TRANS_STRAT_WRITE);
  611. tp->t_flags |= XFS_TRANS_RESERVE;
  612. nres = XFS_EXTENTADD_SPACE_RES(mp, XFS_DATA_FORK);
  613. error = xfs_trans_reserve(tp, &M_RES(mp)->tr_write,
  614. nres, 0);
  615. if (error) {
  616. xfs_trans_cancel(tp, 0);
  617. return error;
  618. }
  619. xfs_ilock(ip, XFS_ILOCK_EXCL);
  620. xfs_trans_ijoin(tp, ip, 0);
  621. xfs_bmap_init(&free_list, &first_block);
  622. /*
  623. * it is possible that the extents have changed since
  624. * we did the read call as we dropped the ilock for a
  625. * while. We have to be careful about truncates or hole
  626. * punchs here - we are not allowed to allocate
  627. * non-delalloc blocks here.
  628. *
  629. * The only protection against truncation is the pages
  630. * for the range we are being asked to convert are
  631. * locked and hence a truncate will block on them
  632. * first.
  633. *
  634. * As a result, if we go beyond the range we really
  635. * need and hit an delalloc extent boundary followed by
  636. * a hole while we have excess blocks in the map, we
  637. * will fill the hole incorrectly and overrun the
  638. * transaction reservation.
  639. *
  640. * Using a single map prevents this as we are forced to
  641. * check each map we look for overlap with the desired
  642. * range and abort as soon as we find it. Also, given
  643. * that we only return a single map, having one beyond
  644. * what we can return is probably a bit silly.
  645. *
  646. * We also need to check that we don't go beyond EOF;
  647. * this is a truncate optimisation as a truncate sets
  648. * the new file size before block on the pages we
  649. * currently have locked under writeback. Because they
  650. * are about to be tossed, we don't need to write them
  651. * back....
  652. */
  653. nimaps = 1;
  654. end_fsb = XFS_B_TO_FSB(mp, XFS_ISIZE(ip));
  655. error = xfs_bmap_last_offset(ip, &last_block,
  656. XFS_DATA_FORK);
  657. if (error)
  658. goto trans_cancel;
  659. last_block = XFS_FILEOFF_MAX(last_block, end_fsb);
  660. if ((map_start_fsb + count_fsb) > last_block) {
  661. count_fsb = last_block - map_start_fsb;
  662. if (count_fsb == 0) {
  663. error = -EAGAIN;
  664. goto trans_cancel;
  665. }
  666. }
  667. /*
  668. * From this point onwards we overwrite the imap
  669. * pointer that the caller gave to us.
  670. */
  671. error = xfs_bmapi_write(tp, ip, map_start_fsb,
  672. count_fsb, 0,
  673. &first_block, 1,
  674. imap, &nimaps, &free_list);
  675. if (error)
  676. goto trans_cancel;
  677. error = xfs_bmap_finish(&tp, &free_list, &committed);
  678. if (error)
  679. goto trans_cancel;
  680. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  681. if (error)
  682. goto error0;
  683. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  684. }
  685. /*
  686. * See if we were able to allocate an extent that
  687. * covers at least part of the callers request
  688. */
  689. if (!(imap->br_startblock || XFS_IS_REALTIME_INODE(ip)))
  690. return xfs_alert_fsblock_zero(ip, imap);
  691. if ((offset_fsb >= imap->br_startoff) &&
  692. (offset_fsb < (imap->br_startoff +
  693. imap->br_blockcount))) {
  694. XFS_STATS_INC(xs_xstrat_quick);
  695. return 0;
  696. }
  697. /*
  698. * So far we have not mapped the requested part of the
  699. * file, just surrounding data, try again.
  700. */
  701. count_fsb -= imap->br_blockcount;
  702. map_start_fsb = imap->br_startoff + imap->br_blockcount;
  703. }
  704. trans_cancel:
  705. xfs_bmap_cancel(&free_list);
  706. xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
  707. error0:
  708. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  709. return error;
  710. }
  711. int
  712. xfs_iomap_write_unwritten(
  713. xfs_inode_t *ip,
  714. xfs_off_t offset,
  715. xfs_off_t count)
  716. {
  717. xfs_mount_t *mp = ip->i_mount;
  718. xfs_fileoff_t offset_fsb;
  719. xfs_filblks_t count_fsb;
  720. xfs_filblks_t numblks_fsb;
  721. xfs_fsblock_t firstfsb;
  722. int nimaps;
  723. xfs_trans_t *tp;
  724. xfs_bmbt_irec_t imap;
  725. xfs_bmap_free_t free_list;
  726. xfs_fsize_t i_size;
  727. uint resblks;
  728. int committed;
  729. int error;
  730. trace_xfs_unwritten_convert(ip, offset, count);
  731. offset_fsb = XFS_B_TO_FSBT(mp, offset);
  732. count_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)offset + count);
  733. count_fsb = (xfs_filblks_t)(count_fsb - offset_fsb);
  734. /*
  735. * Reserve enough blocks in this transaction for two complete extent
  736. * btree splits. We may be converting the middle part of an unwritten
  737. * extent and in this case we will insert two new extents in the btree
  738. * each of which could cause a full split.
  739. *
  740. * This reservation amount will be used in the first call to
  741. * xfs_bmbt_split() to select an AG with enough space to satisfy the
  742. * rest of the operation.
  743. */
  744. resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0) << 1;
  745. do {
  746. /*
  747. * set up a transaction to convert the range of extents
  748. * from unwritten to real. Do allocations in a loop until
  749. * we have covered the range passed in.
  750. *
  751. * Note that we open code the transaction allocation here
  752. * to pass KM_NOFS--we can't risk to recursing back into
  753. * the filesystem here as we might be asked to write out
  754. * the same inode that we complete here and might deadlock
  755. * on the iolock.
  756. */
  757. sb_start_intwrite(mp->m_super);
  758. tp = _xfs_trans_alloc(mp, XFS_TRANS_STRAT_WRITE, KM_NOFS);
  759. tp->t_flags |= XFS_TRANS_RESERVE | XFS_TRANS_FREEZE_PROT;
  760. error = xfs_trans_reserve(tp, &M_RES(mp)->tr_write,
  761. resblks, 0);
  762. if (error) {
  763. xfs_trans_cancel(tp, 0);
  764. return error;
  765. }
  766. xfs_ilock(ip, XFS_ILOCK_EXCL);
  767. xfs_trans_ijoin(tp, ip, 0);
  768. /*
  769. * Modify the unwritten extent state of the buffer.
  770. */
  771. xfs_bmap_init(&free_list, &firstfsb);
  772. nimaps = 1;
  773. error = xfs_bmapi_write(tp, ip, offset_fsb, count_fsb,
  774. XFS_BMAPI_CONVERT, &firstfsb,
  775. 1, &imap, &nimaps, &free_list);
  776. if (error)
  777. goto error_on_bmapi_transaction;
  778. /*
  779. * Log the updated inode size as we go. We have to be careful
  780. * to only log it up to the actual write offset if it is
  781. * halfway into a block.
  782. */
  783. i_size = XFS_FSB_TO_B(mp, offset_fsb + count_fsb);
  784. if (i_size > offset + count)
  785. i_size = offset + count;
  786. i_size = xfs_new_eof(ip, i_size);
  787. if (i_size) {
  788. ip->i_d.di_size = i_size;
  789. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  790. }
  791. error = xfs_bmap_finish(&tp, &free_list, &committed);
  792. if (error)
  793. goto error_on_bmapi_transaction;
  794. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  795. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  796. if (error)
  797. return error;
  798. if (!(imap.br_startblock || XFS_IS_REALTIME_INODE(ip)))
  799. return xfs_alert_fsblock_zero(ip, &imap);
  800. if ((numblks_fsb = imap.br_blockcount) == 0) {
  801. /*
  802. * The numblks_fsb value should always get
  803. * smaller, otherwise the loop is stuck.
  804. */
  805. ASSERT(imap.br_blockcount);
  806. break;
  807. }
  808. offset_fsb += numblks_fsb;
  809. count_fsb -= numblks_fsb;
  810. } while (count_fsb > 0);
  811. return 0;
  812. error_on_bmapi_transaction:
  813. xfs_bmap_cancel(&free_list);
  814. xfs_trans_cancel(tp, (XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT));
  815. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  816. return error;
  817. }