xfs_iomap.c 25 KB

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