xfs_iomap.c 25 KB

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