xfs_iomap.c 20 KB

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