xfs_iomap.c 20 KB

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