xfs_iomap.c 24 KB

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