xfs_iomap.c 21 KB

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