xfs_iomap.c 21 KB

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