xfs_iomap.c 22 KB

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