xfs_iomap.c 25 KB

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