xfs_iomap.c 24 KB

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