xfs_iomap.c 20 KB

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