xfs_iomap.c 22 KB

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