xfs_iomap.c 24 KB

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