xfs_iomap.c 22 KB

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