xfs_iomap.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757
  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_bit.h"
  21. #include "xfs_log.h"
  22. #include "xfs_trans.h"
  23. #include "xfs_sb.h"
  24. #include "xfs_ag.h"
  25. #include "xfs_alloc.h"
  26. #include "xfs_quota.h"
  27. #include "xfs_mount.h"
  28. #include "xfs_bmap_btree.h"
  29. #include "xfs_alloc_btree.h"
  30. #include "xfs_ialloc_btree.h"
  31. #include "xfs_dinode.h"
  32. #include "xfs_inode.h"
  33. #include "xfs_inode_item.h"
  34. #include "xfs_btree.h"
  35. #include "xfs_bmap.h"
  36. #include "xfs_rtalloc.h"
  37. #include "xfs_error.h"
  38. #include "xfs_itable.h"
  39. #include "xfs_rw.h"
  40. #include "xfs_attr.h"
  41. #include "xfs_buf_item.h"
  42. #include "xfs_trans_space.h"
  43. #include "xfs_utils.h"
  44. #include "xfs_iomap.h"
  45. #include "xfs_trace.h"
  46. #define XFS_WRITEIO_ALIGN(mp,off) (((off) >> mp->m_writeio_log) \
  47. << mp->m_writeio_log)
  48. #define XFS_WRITE_IMAPS XFS_BMAP_MAX_NMAP
  49. STATIC int
  50. xfs_iomap_eof_align_last_fsb(
  51. xfs_mount_t *mp,
  52. xfs_inode_t *ip,
  53. xfs_extlen_t extsize,
  54. xfs_fileoff_t *last_fsb)
  55. {
  56. xfs_fileoff_t new_last_fsb = 0;
  57. xfs_extlen_t align = 0;
  58. int eof, error;
  59. if (!XFS_IS_REALTIME_INODE(ip)) {
  60. /*
  61. * Round up the allocation request to a stripe unit
  62. * (m_dalign) boundary if the file size is >= stripe unit
  63. * size, and we are allocating past the allocation eof.
  64. *
  65. * If mounted with the "-o swalloc" option the alignment is
  66. * increased from the strip unit size to the stripe width.
  67. */
  68. if (mp->m_swidth && (mp->m_flags & XFS_MOUNT_SWALLOC))
  69. align = mp->m_swidth;
  70. else if (mp->m_dalign)
  71. align = mp->m_dalign;
  72. if (align && XFS_ISIZE(ip) >= XFS_FSB_TO_B(mp, align))
  73. new_last_fsb = roundup_64(*last_fsb, align);
  74. }
  75. /*
  76. * Always round up the allocation request to an extent boundary
  77. * (when file on a real-time subvolume or has di_extsize hint).
  78. */
  79. if (extsize) {
  80. if (new_last_fsb)
  81. align = roundup_64(new_last_fsb, extsize);
  82. else
  83. align = extsize;
  84. new_last_fsb = roundup_64(*last_fsb, align);
  85. }
  86. if (new_last_fsb) {
  87. error = xfs_bmap_eof(ip, new_last_fsb, XFS_DATA_FORK, &eof);
  88. if (error)
  89. return error;
  90. if (eof)
  91. *last_fsb = new_last_fsb;
  92. }
  93. return 0;
  94. }
  95. STATIC int
  96. xfs_alert_fsblock_zero(
  97. xfs_inode_t *ip,
  98. xfs_bmbt_irec_t *imap)
  99. {
  100. xfs_alert_tag(ip->i_mount, XFS_PTAG_FSBLOCK_ZERO,
  101. "Access to block zero in inode %llu "
  102. "start_block: %llx start_off: %llx "
  103. "blkcnt: %llx extent-state: %x\n",
  104. (unsigned long long)ip->i_ino,
  105. (unsigned long long)imap->br_startblock,
  106. (unsigned long long)imap->br_startoff,
  107. (unsigned long long)imap->br_blockcount,
  108. imap->br_state);
  109. return EFSCORRUPTED;
  110. }
  111. int
  112. xfs_iomap_write_direct(
  113. xfs_inode_t *ip,
  114. xfs_off_t offset,
  115. size_t count,
  116. xfs_bmbt_irec_t *imap,
  117. int nmaps)
  118. {
  119. xfs_mount_t *mp = ip->i_mount;
  120. xfs_fileoff_t offset_fsb;
  121. xfs_fileoff_t last_fsb;
  122. xfs_filblks_t count_fsb, resaligned;
  123. xfs_fsblock_t firstfsb;
  124. xfs_extlen_t extsz, temp;
  125. int nimaps;
  126. int bmapi_flag;
  127. int quota_flag;
  128. int rt;
  129. xfs_trans_t *tp;
  130. xfs_bmap_free_t free_list;
  131. uint qblocks, resblks, resrtextents;
  132. int committed;
  133. int error;
  134. error = xfs_qm_dqattach(ip, 0);
  135. if (error)
  136. return XFS_ERROR(error);
  137. rt = XFS_IS_REALTIME_INODE(ip);
  138. extsz = xfs_get_extsz_hint(ip);
  139. offset_fsb = XFS_B_TO_FSBT(mp, offset);
  140. last_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)(offset + count)));
  141. if ((offset + count) > XFS_ISIZE(ip)) {
  142. error = xfs_iomap_eof_align_last_fsb(mp, ip, extsz, &last_fsb);
  143. if (error)
  144. return XFS_ERROR(error);
  145. } else {
  146. if (nmaps && (imap->br_startblock == HOLESTARTBLOCK))
  147. last_fsb = MIN(last_fsb, (xfs_fileoff_t)
  148. imap->br_blockcount +
  149. imap->br_startoff);
  150. }
  151. count_fsb = last_fsb - offset_fsb;
  152. ASSERT(count_fsb > 0);
  153. resaligned = count_fsb;
  154. if (unlikely(extsz)) {
  155. if ((temp = do_mod(offset_fsb, extsz)))
  156. resaligned += temp;
  157. if ((temp = do_mod(resaligned, extsz)))
  158. resaligned += extsz - temp;
  159. }
  160. if (unlikely(rt)) {
  161. resrtextents = qblocks = resaligned;
  162. resrtextents /= mp->m_sb.sb_rextsize;
  163. resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
  164. quota_flag = XFS_QMOPT_RES_RTBLKS;
  165. } else {
  166. resrtextents = 0;
  167. resblks = qblocks = XFS_DIOSTRAT_SPACE_RES(mp, resaligned);
  168. quota_flag = XFS_QMOPT_RES_REGBLKS;
  169. }
  170. /*
  171. * Allocate and setup the transaction
  172. */
  173. tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
  174. error = xfs_trans_reserve(tp, resblks,
  175. XFS_WRITE_LOG_RES(mp), resrtextents,
  176. XFS_TRANS_PERM_LOG_RES,
  177. XFS_WRITE_LOG_COUNT);
  178. /*
  179. * Check for running out of space, note: need lock to return
  180. */
  181. if (error) {
  182. xfs_trans_cancel(tp, 0);
  183. return XFS_ERROR(error);
  184. }
  185. xfs_ilock(ip, XFS_ILOCK_EXCL);
  186. error = xfs_trans_reserve_quota_nblks(tp, ip, qblocks, 0, quota_flag);
  187. if (error)
  188. goto out_trans_cancel;
  189. xfs_trans_ijoin(tp, ip, 0);
  190. bmapi_flag = 0;
  191. if (offset < XFS_ISIZE(ip) || extsz)
  192. bmapi_flag |= XFS_BMAPI_PREALLOC;
  193. /*
  194. * From this point onwards we overwrite the imap pointer that the
  195. * caller gave to us.
  196. */
  197. xfs_bmap_init(&free_list, &firstfsb);
  198. nimaps = 1;
  199. error = xfs_bmapi_write(tp, ip, offset_fsb, count_fsb, bmapi_flag,
  200. &firstfsb, 0, imap, &nimaps, &free_list);
  201. if (error)
  202. goto out_bmap_cancel;
  203. /*
  204. * Complete the transaction
  205. */
  206. error = xfs_bmap_finish(&tp, &free_list, &committed);
  207. if (error)
  208. goto out_bmap_cancel;
  209. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  210. if (error)
  211. goto out_unlock;
  212. /*
  213. * Copy any maps to caller's array and return any error.
  214. */
  215. if (nimaps == 0) {
  216. error = XFS_ERROR(ENOSPC);
  217. goto out_unlock;
  218. }
  219. if (!(imap->br_startblock || XFS_IS_REALTIME_INODE(ip)))
  220. error = xfs_alert_fsblock_zero(ip, imap);
  221. out_unlock:
  222. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  223. return error;
  224. out_bmap_cancel:
  225. xfs_bmap_cancel(&free_list);
  226. xfs_trans_unreserve_quota_nblks(tp, ip, qblocks, 0, quota_flag);
  227. out_trans_cancel:
  228. xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
  229. goto out_unlock;
  230. }
  231. /*
  232. * If the caller is doing a write at the end of the file, then extend the
  233. * allocation out to the file system's write iosize. We clean up any extra
  234. * space left over when the file is closed in xfs_inactive().
  235. *
  236. * If we find we already have delalloc preallocation beyond EOF, don't do more
  237. * preallocation as it it not needed.
  238. */
  239. STATIC int
  240. xfs_iomap_eof_want_preallocate(
  241. xfs_mount_t *mp,
  242. xfs_inode_t *ip,
  243. xfs_off_t offset,
  244. size_t count,
  245. xfs_bmbt_irec_t *imap,
  246. int nimaps,
  247. int *prealloc)
  248. {
  249. xfs_fileoff_t start_fsb;
  250. xfs_filblks_t count_fsb;
  251. xfs_fsblock_t firstblock;
  252. int n, error, imaps;
  253. int found_delalloc = 0;
  254. *prealloc = 0;
  255. if (offset + count <= XFS_ISIZE(ip))
  256. return 0;
  257. /*
  258. * If there are any real blocks past eof, then don't
  259. * do any speculative allocation.
  260. */
  261. start_fsb = XFS_B_TO_FSBT(mp, ((xfs_ufsize_t)(offset + count - 1)));
  262. count_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_MAXIOFFSET(mp));
  263. while (count_fsb > 0) {
  264. imaps = nimaps;
  265. firstblock = NULLFSBLOCK;
  266. error = xfs_bmapi_read(ip, start_fsb, count_fsb, imap, &imaps,
  267. 0);
  268. if (error)
  269. return error;
  270. for (n = 0; n < imaps; n++) {
  271. if ((imap[n].br_startblock != HOLESTARTBLOCK) &&
  272. (imap[n].br_startblock != DELAYSTARTBLOCK))
  273. return 0;
  274. start_fsb += imap[n].br_blockcount;
  275. count_fsb -= imap[n].br_blockcount;
  276. if (imap[n].br_startblock == DELAYSTARTBLOCK)
  277. found_delalloc = 1;
  278. }
  279. }
  280. if (!found_delalloc)
  281. *prealloc = 1;
  282. return 0;
  283. }
  284. /*
  285. * If we don't have a user specified preallocation size, dynamically increase
  286. * the preallocation size as the size of the file grows. Cap the maximum size
  287. * at a single extent or less if the filesystem is near full. The closer the
  288. * filesystem is to full, the smaller the maximum prealocation.
  289. */
  290. STATIC xfs_fsblock_t
  291. xfs_iomap_prealloc_size(
  292. struct xfs_mount *mp,
  293. struct xfs_inode *ip)
  294. {
  295. xfs_fsblock_t alloc_blocks = 0;
  296. if (!(mp->m_flags & XFS_MOUNT_DFLT_IOSIZE)) {
  297. int shift = 0;
  298. int64_t freesp;
  299. /*
  300. * rounddown_pow_of_two() returns an undefined result
  301. * if we pass in alloc_blocks = 0. Hence the "+ 1" to
  302. * ensure we always pass in a non-zero value.
  303. */
  304. alloc_blocks = XFS_B_TO_FSB(mp, XFS_ISIZE(ip)) + 1;
  305. alloc_blocks = XFS_FILEOFF_MIN(MAXEXTLEN,
  306. rounddown_pow_of_two(alloc_blocks));
  307. xfs_icsb_sync_counters(mp, XFS_ICSB_LAZY_COUNT);
  308. freesp = mp->m_sb.sb_fdblocks;
  309. if (freesp < mp->m_low_space[XFS_LOWSP_5_PCNT]) {
  310. shift = 2;
  311. if (freesp < mp->m_low_space[XFS_LOWSP_4_PCNT])
  312. shift++;
  313. if (freesp < mp->m_low_space[XFS_LOWSP_3_PCNT])
  314. shift++;
  315. if (freesp < mp->m_low_space[XFS_LOWSP_2_PCNT])
  316. shift++;
  317. if (freesp < mp->m_low_space[XFS_LOWSP_1_PCNT])
  318. shift++;
  319. }
  320. if (shift)
  321. alloc_blocks >>= shift;
  322. }
  323. if (alloc_blocks < mp->m_writeio_blocks)
  324. alloc_blocks = mp->m_writeio_blocks;
  325. return alloc_blocks;
  326. }
  327. int
  328. xfs_iomap_write_delay(
  329. xfs_inode_t *ip,
  330. xfs_off_t offset,
  331. size_t count,
  332. xfs_bmbt_irec_t *ret_imap)
  333. {
  334. xfs_mount_t *mp = ip->i_mount;
  335. xfs_fileoff_t offset_fsb;
  336. xfs_fileoff_t last_fsb;
  337. xfs_off_t aligned_offset;
  338. xfs_fileoff_t ioalign;
  339. xfs_extlen_t extsz;
  340. int nimaps;
  341. xfs_bmbt_irec_t imap[XFS_WRITE_IMAPS];
  342. int prealloc, flushed = 0;
  343. int error;
  344. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
  345. /*
  346. * Make sure that the dquots are there. This doesn't hold
  347. * the ilock across a disk read.
  348. */
  349. error = xfs_qm_dqattach_locked(ip, 0);
  350. if (error)
  351. return XFS_ERROR(error);
  352. extsz = xfs_get_extsz_hint(ip);
  353. offset_fsb = XFS_B_TO_FSBT(mp, offset);
  354. error = xfs_iomap_eof_want_preallocate(mp, ip, offset, count,
  355. imap, XFS_WRITE_IMAPS, &prealloc);
  356. if (error)
  357. return error;
  358. retry:
  359. if (prealloc) {
  360. xfs_fsblock_t alloc_blocks = xfs_iomap_prealloc_size(mp, ip);
  361. aligned_offset = XFS_WRITEIO_ALIGN(mp, (offset + count - 1));
  362. ioalign = XFS_B_TO_FSBT(mp, aligned_offset);
  363. last_fsb = ioalign + alloc_blocks;
  364. } else {
  365. last_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)(offset + count)));
  366. }
  367. if (prealloc || extsz) {
  368. error = xfs_iomap_eof_align_last_fsb(mp, ip, extsz, &last_fsb);
  369. if (error)
  370. return error;
  371. }
  372. /*
  373. * Make sure preallocation does not create extents beyond the range we
  374. * actually support in this filesystem.
  375. */
  376. if (last_fsb > XFS_B_TO_FSB(mp, mp->m_maxioffset))
  377. last_fsb = XFS_B_TO_FSB(mp, mp->m_maxioffset);
  378. ASSERT(last_fsb > offset_fsb);
  379. nimaps = XFS_WRITE_IMAPS;
  380. error = xfs_bmapi_delay(ip, offset_fsb, last_fsb - offset_fsb,
  381. imap, &nimaps, XFS_BMAPI_ENTIRE);
  382. switch (error) {
  383. case 0:
  384. case ENOSPC:
  385. case EDQUOT:
  386. break;
  387. default:
  388. return XFS_ERROR(error);
  389. }
  390. /*
  391. * If bmapi returned us nothing, we got either ENOSPC or EDQUOT. For
  392. * ENOSPC, * flush all other inodes with delalloc blocks to free up
  393. * some of the excess reserved metadata space. For both cases, retry
  394. * without EOF preallocation.
  395. */
  396. if (nimaps == 0) {
  397. trace_xfs_delalloc_enospc(ip, offset, count);
  398. if (flushed)
  399. return XFS_ERROR(error ? error : ENOSPC);
  400. if (error == ENOSPC) {
  401. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  402. xfs_flush_inodes(ip);
  403. xfs_ilock(ip, XFS_ILOCK_EXCL);
  404. }
  405. flushed = 1;
  406. error = 0;
  407. prealloc = 0;
  408. goto retry;
  409. }
  410. if (!(imap[0].br_startblock || XFS_IS_REALTIME_INODE(ip)))
  411. return xfs_alert_fsblock_zero(ip, &imap[0]);
  412. *ret_imap = imap[0];
  413. return 0;
  414. }
  415. /*
  416. * Pass in a delayed allocate extent, convert it to real extents;
  417. * return to the caller the extent we create which maps on top of
  418. * the originating callers request.
  419. *
  420. * Called without a lock on the inode.
  421. *
  422. * We no longer bother to look at the incoming map - all we have to
  423. * guarantee is that whatever we allocate fills the required range.
  424. */
  425. int
  426. xfs_iomap_write_allocate(
  427. xfs_inode_t *ip,
  428. xfs_off_t offset,
  429. size_t count,
  430. xfs_bmbt_irec_t *imap)
  431. {
  432. xfs_mount_t *mp = ip->i_mount;
  433. xfs_fileoff_t offset_fsb, last_block;
  434. xfs_fileoff_t end_fsb, map_start_fsb;
  435. xfs_fsblock_t first_block;
  436. xfs_bmap_free_t free_list;
  437. xfs_filblks_t count_fsb;
  438. xfs_trans_t *tp;
  439. int nimaps, committed;
  440. int error = 0;
  441. int nres;
  442. /*
  443. * Make sure that the dquots are there.
  444. */
  445. error = xfs_qm_dqattach(ip, 0);
  446. if (error)
  447. return XFS_ERROR(error);
  448. offset_fsb = XFS_B_TO_FSBT(mp, offset);
  449. count_fsb = imap->br_blockcount;
  450. map_start_fsb = imap->br_startoff;
  451. XFS_STATS_ADD(xs_xstrat_bytes, XFS_FSB_TO_B(mp, count_fsb));
  452. while (count_fsb != 0) {
  453. /*
  454. * Set up a transaction with which to allocate the
  455. * backing store for the file. Do allocations in a
  456. * loop until we get some space in the range we are
  457. * interested in. The other space that might be allocated
  458. * is in the delayed allocation extent on which we sit
  459. * but before our buffer starts.
  460. */
  461. nimaps = 0;
  462. while (nimaps == 0) {
  463. tp = xfs_trans_alloc(mp, XFS_TRANS_STRAT_WRITE);
  464. tp->t_flags |= XFS_TRANS_RESERVE;
  465. nres = XFS_EXTENTADD_SPACE_RES(mp, XFS_DATA_FORK);
  466. error = xfs_trans_reserve(tp, nres,
  467. XFS_WRITE_LOG_RES(mp),
  468. 0, XFS_TRANS_PERM_LOG_RES,
  469. XFS_WRITE_LOG_COUNT);
  470. if (error) {
  471. xfs_trans_cancel(tp, 0);
  472. return XFS_ERROR(error);
  473. }
  474. xfs_ilock(ip, XFS_ILOCK_EXCL);
  475. xfs_trans_ijoin(tp, ip, 0);
  476. xfs_bmap_init(&free_list, &first_block);
  477. /*
  478. * it is possible that the extents have changed since
  479. * we did the read call as we dropped the ilock for a
  480. * while. We have to be careful about truncates or hole
  481. * punchs here - we are not allowed to allocate
  482. * non-delalloc blocks here.
  483. *
  484. * The only protection against truncation is the pages
  485. * for the range we are being asked to convert are
  486. * locked and hence a truncate will block on them
  487. * first.
  488. *
  489. * As a result, if we go beyond the range we really
  490. * need and hit an delalloc extent boundary followed by
  491. * a hole while we have excess blocks in the map, we
  492. * will fill the hole incorrectly and overrun the
  493. * transaction reservation.
  494. *
  495. * Using a single map prevents this as we are forced to
  496. * check each map we look for overlap with the desired
  497. * range and abort as soon as we find it. Also, given
  498. * that we only return a single map, having one beyond
  499. * what we can return is probably a bit silly.
  500. *
  501. * We also need to check that we don't go beyond EOF;
  502. * this is a truncate optimisation as a truncate sets
  503. * the new file size before block on the pages we
  504. * currently have locked under writeback. Because they
  505. * are about to be tossed, we don't need to write them
  506. * back....
  507. */
  508. nimaps = 1;
  509. end_fsb = XFS_B_TO_FSB(mp, XFS_ISIZE(ip));
  510. error = xfs_bmap_last_offset(NULL, ip, &last_block,
  511. XFS_DATA_FORK);
  512. if (error)
  513. goto trans_cancel;
  514. last_block = XFS_FILEOFF_MAX(last_block, end_fsb);
  515. if ((map_start_fsb + count_fsb) > last_block) {
  516. count_fsb = last_block - map_start_fsb;
  517. if (count_fsb == 0) {
  518. error = EAGAIN;
  519. goto trans_cancel;
  520. }
  521. }
  522. /*
  523. * From this point onwards we overwrite the imap
  524. * pointer that the caller gave to us.
  525. */
  526. error = xfs_bmapi_write(tp, ip, map_start_fsb,
  527. count_fsb, 0, &first_block, 1,
  528. imap, &nimaps, &free_list);
  529. if (error)
  530. goto trans_cancel;
  531. error = xfs_bmap_finish(&tp, &free_list, &committed);
  532. if (error)
  533. goto trans_cancel;
  534. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  535. if (error)
  536. goto error0;
  537. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  538. }
  539. /*
  540. * See if we were able to allocate an extent that
  541. * covers at least part of the callers request
  542. */
  543. if (!(imap->br_startblock || XFS_IS_REALTIME_INODE(ip)))
  544. return xfs_alert_fsblock_zero(ip, imap);
  545. if ((offset_fsb >= imap->br_startoff) &&
  546. (offset_fsb < (imap->br_startoff +
  547. imap->br_blockcount))) {
  548. XFS_STATS_INC(xs_xstrat_quick);
  549. return 0;
  550. }
  551. /*
  552. * So far we have not mapped the requested part of the
  553. * file, just surrounding data, try again.
  554. */
  555. count_fsb -= imap->br_blockcount;
  556. map_start_fsb = imap->br_startoff + imap->br_blockcount;
  557. }
  558. trans_cancel:
  559. xfs_bmap_cancel(&free_list);
  560. xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
  561. error0:
  562. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  563. return XFS_ERROR(error);
  564. }
  565. int
  566. xfs_iomap_write_unwritten(
  567. xfs_inode_t *ip,
  568. xfs_off_t offset,
  569. size_t count)
  570. {
  571. xfs_mount_t *mp = ip->i_mount;
  572. xfs_fileoff_t offset_fsb;
  573. xfs_filblks_t count_fsb;
  574. xfs_filblks_t numblks_fsb;
  575. xfs_fsblock_t firstfsb;
  576. int nimaps;
  577. xfs_trans_t *tp;
  578. xfs_bmbt_irec_t imap;
  579. xfs_bmap_free_t free_list;
  580. xfs_fsize_t i_size;
  581. uint resblks;
  582. int committed;
  583. int error;
  584. trace_xfs_unwritten_convert(ip, offset, count);
  585. offset_fsb = XFS_B_TO_FSBT(mp, offset);
  586. count_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)offset + count);
  587. count_fsb = (xfs_filblks_t)(count_fsb - offset_fsb);
  588. /*
  589. * Reserve enough blocks in this transaction for two complete extent
  590. * btree splits. We may be converting the middle part of an unwritten
  591. * extent and in this case we will insert two new extents in the btree
  592. * each of which could cause a full split.
  593. *
  594. * This reservation amount will be used in the first call to
  595. * xfs_bmbt_split() to select an AG with enough space to satisfy the
  596. * rest of the operation.
  597. */
  598. resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0) << 1;
  599. do {
  600. /*
  601. * set up a transaction to convert the range of extents
  602. * from unwritten to real. Do allocations in a loop until
  603. * we have covered the range passed in.
  604. *
  605. * Note that we open code the transaction allocation here
  606. * to pass KM_NOFS--we can't risk to recursing back into
  607. * the filesystem here as we might be asked to write out
  608. * the same inode that we complete here and might deadlock
  609. * on the iolock.
  610. */
  611. xfs_wait_for_freeze(mp, SB_FREEZE_TRANS);
  612. tp = _xfs_trans_alloc(mp, XFS_TRANS_STRAT_WRITE, KM_NOFS);
  613. tp->t_flags |= XFS_TRANS_RESERVE;
  614. error = xfs_trans_reserve(tp, resblks,
  615. XFS_WRITE_LOG_RES(mp), 0,
  616. XFS_TRANS_PERM_LOG_RES,
  617. XFS_WRITE_LOG_COUNT);
  618. if (error) {
  619. xfs_trans_cancel(tp, 0);
  620. return XFS_ERROR(error);
  621. }
  622. xfs_ilock(ip, XFS_ILOCK_EXCL);
  623. xfs_trans_ijoin(tp, ip, 0);
  624. /*
  625. * Modify the unwritten extent state of the buffer.
  626. */
  627. xfs_bmap_init(&free_list, &firstfsb);
  628. nimaps = 1;
  629. error = xfs_bmapi_write(tp, ip, offset_fsb, count_fsb,
  630. XFS_BMAPI_CONVERT, &firstfsb,
  631. 1, &imap, &nimaps, &free_list);
  632. if (error)
  633. goto error_on_bmapi_transaction;
  634. /*
  635. * Log the updated inode size as we go. We have to be careful
  636. * to only log it up to the actual write offset if it is
  637. * halfway into a block.
  638. */
  639. i_size = XFS_FSB_TO_B(mp, offset_fsb + count_fsb);
  640. if (i_size > offset + count)
  641. i_size = offset + count;
  642. i_size = xfs_new_eof(ip, i_size);
  643. if (i_size) {
  644. ip->i_d.di_size = i_size;
  645. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  646. }
  647. error = xfs_bmap_finish(&tp, &free_list, &committed);
  648. if (error)
  649. goto error_on_bmapi_transaction;
  650. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  651. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  652. if (error)
  653. return XFS_ERROR(error);
  654. if (!(imap.br_startblock || XFS_IS_REALTIME_INODE(ip)))
  655. return xfs_alert_fsblock_zero(ip, &imap);
  656. if ((numblks_fsb = imap.br_blockcount) == 0) {
  657. /*
  658. * The numblks_fsb value should always get
  659. * smaller, otherwise the loop is stuck.
  660. */
  661. ASSERT(imap.br_blockcount);
  662. break;
  663. }
  664. offset_fsb += numblks_fsb;
  665. count_fsb -= numblks_fsb;
  666. } while (count_fsb > 0);
  667. return 0;
  668. error_on_bmapi_transaction:
  669. xfs_bmap_cancel(&free_list);
  670. xfs_trans_cancel(tp, (XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT));
  671. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  672. return XFS_ERROR(error);
  673. }