xfs_iomap.c 21 KB

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