xfs_iomap.c 22 KB

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