xfs_iomap.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961
  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. extsz = xfs_get_extsz_hint(ip);
  421. isize = ip->i_size;
  422. if (io->io_new_size > isize)
  423. isize = io->io_new_size;
  424. offset_fsb = XFS_B_TO_FSBT(mp, offset);
  425. last_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)(offset + count)));
  426. if ((offset + count) > isize) {
  427. error = xfs_iomap_eof_align_last_fsb(mp, io, isize, extsz,
  428. &last_fsb);
  429. if (error)
  430. goto error_out;
  431. } else {
  432. if (found && (ret_imap->br_startblock == HOLESTARTBLOCK))
  433. last_fsb = MIN(last_fsb, (xfs_fileoff_t)
  434. ret_imap->br_blockcount +
  435. ret_imap->br_startoff);
  436. }
  437. count_fsb = last_fsb - offset_fsb;
  438. ASSERT(count_fsb > 0);
  439. resaligned = count_fsb;
  440. if (unlikely(extsz)) {
  441. if ((temp = do_mod(offset_fsb, extsz)))
  442. resaligned += temp;
  443. if ((temp = do_mod(resaligned, extsz)))
  444. resaligned += extsz - temp;
  445. }
  446. if (unlikely(rt)) {
  447. resrtextents = qblocks = resaligned;
  448. resrtextents /= mp->m_sb.sb_rextsize;
  449. resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
  450. quota_flag = XFS_QMOPT_RES_RTBLKS;
  451. } else {
  452. resrtextents = 0;
  453. resblks = qblocks = XFS_DIOSTRAT_SPACE_RES(mp, resaligned);
  454. quota_flag = XFS_QMOPT_RES_REGBLKS;
  455. }
  456. /*
  457. * Allocate and setup the transaction
  458. */
  459. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  460. tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
  461. error = xfs_trans_reserve(tp, resblks,
  462. XFS_WRITE_LOG_RES(mp), resrtextents,
  463. XFS_TRANS_PERM_LOG_RES,
  464. XFS_WRITE_LOG_COUNT);
  465. /*
  466. * Check for running out of space, note: need lock to return
  467. */
  468. if (error)
  469. xfs_trans_cancel(tp, 0);
  470. xfs_ilock(ip, XFS_ILOCK_EXCL);
  471. if (error)
  472. goto error_out;
  473. error = XFS_TRANS_RESERVE_QUOTA_NBLKS(mp, tp, ip,
  474. qblocks, 0, quota_flag);
  475. if (error)
  476. goto error1;
  477. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  478. xfs_trans_ihold(tp, ip);
  479. bmapi_flag = XFS_BMAPI_WRITE;
  480. if ((flags & BMAPI_DIRECT) && (offset < ip->i_size || extsz))
  481. bmapi_flag |= XFS_BMAPI_PREALLOC;
  482. /*
  483. * Issue the xfs_bmapi() call to allocate the blocks
  484. */
  485. XFS_BMAP_INIT(&free_list, &firstfsb);
  486. nimaps = 1;
  487. error = XFS_BMAPI(mp, tp, io, offset_fsb, count_fsb, bmapi_flag,
  488. &firstfsb, 0, &imap, &nimaps, &free_list, NULL);
  489. if (error)
  490. goto error0;
  491. /*
  492. * Complete the transaction
  493. */
  494. error = xfs_bmap_finish(&tp, &free_list, &committed);
  495. if (error)
  496. goto error0;
  497. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  498. if (error)
  499. goto error_out;
  500. /*
  501. * Copy any maps to caller's array and return any error.
  502. */
  503. if (nimaps == 0) {
  504. error = ENOSPC;
  505. goto error_out;
  506. }
  507. if (unlikely(!imap.br_startblock && !(io->io_flags & XFS_IOCORE_RT))) {
  508. error = xfs_cmn_err_fsblock_zero(ip, &imap);
  509. goto error_out;
  510. }
  511. *ret_imap = imap;
  512. *nmaps = 1;
  513. return 0;
  514. error0: /* Cancel bmap, unlock inode, unreserve quota blocks, cancel trans */
  515. xfs_bmap_cancel(&free_list);
  516. XFS_TRANS_UNRESERVE_QUOTA_NBLKS(mp, tp, ip, qblocks, 0, quota_flag);
  517. error1: /* Just cancel transaction */
  518. xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
  519. *nmaps = 0; /* nothing set-up here */
  520. error_out:
  521. return XFS_ERROR(error);
  522. }
  523. /*
  524. * If the caller is doing a write at the end of the file,
  525. * then extend the allocation out to the file system's write
  526. * iosize. We clean up any extra space left over when the
  527. * file is closed in xfs_inactive().
  528. *
  529. * For sync writes, we are flushing delayed allocate space to
  530. * try to make additional space available for allocation near
  531. * the filesystem full boundary - preallocation hurts in that
  532. * situation, of course.
  533. */
  534. STATIC int
  535. xfs_iomap_eof_want_preallocate(
  536. xfs_mount_t *mp,
  537. xfs_iocore_t *io,
  538. xfs_fsize_t isize,
  539. xfs_off_t offset,
  540. size_t count,
  541. int ioflag,
  542. xfs_bmbt_irec_t *imap,
  543. int nimaps,
  544. int *prealloc)
  545. {
  546. xfs_fileoff_t start_fsb;
  547. xfs_filblks_t count_fsb;
  548. xfs_fsblock_t firstblock;
  549. int n, error, imaps;
  550. *prealloc = 0;
  551. if ((ioflag & BMAPI_SYNC) || (offset + count) <= isize)
  552. return 0;
  553. /*
  554. * If there are any real blocks past eof, then don't
  555. * do any speculative allocation.
  556. */
  557. start_fsb = XFS_B_TO_FSBT(mp, ((xfs_ufsize_t)(offset + count - 1)));
  558. count_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_MAXIOFFSET(mp));
  559. while (count_fsb > 0) {
  560. imaps = nimaps;
  561. firstblock = NULLFSBLOCK;
  562. error = XFS_BMAPI(mp, NULL, io, start_fsb, count_fsb, 0,
  563. &firstblock, 0, imap, &imaps, NULL, NULL);
  564. if (error)
  565. return error;
  566. for (n = 0; n < imaps; n++) {
  567. if ((imap[n].br_startblock != HOLESTARTBLOCK) &&
  568. (imap[n].br_startblock != DELAYSTARTBLOCK))
  569. return 0;
  570. start_fsb += imap[n].br_blockcount;
  571. count_fsb -= imap[n].br_blockcount;
  572. }
  573. }
  574. *prealloc = 1;
  575. return 0;
  576. }
  577. int
  578. xfs_iomap_write_delay(
  579. xfs_inode_t *ip,
  580. xfs_off_t offset,
  581. size_t count,
  582. int ioflag,
  583. xfs_bmbt_irec_t *ret_imap,
  584. int *nmaps)
  585. {
  586. xfs_mount_t *mp = ip->i_mount;
  587. xfs_iocore_t *io = &ip->i_iocore;
  588. xfs_fileoff_t offset_fsb;
  589. xfs_fileoff_t last_fsb;
  590. xfs_off_t aligned_offset;
  591. xfs_fileoff_t ioalign;
  592. xfs_fsblock_t firstblock;
  593. xfs_extlen_t extsz;
  594. xfs_fsize_t isize;
  595. int nimaps;
  596. xfs_bmbt_irec_t imap[XFS_WRITE_IMAPS];
  597. int prealloc, fsynced = 0;
  598. int error;
  599. ASSERT(ismrlocked(&ip->i_lock, MR_UPDATE) != 0);
  600. /*
  601. * Make sure that the dquots are there. This doesn't hold
  602. * the ilock across a disk read.
  603. */
  604. error = XFS_QM_DQATTACH(mp, ip, XFS_QMOPT_ILOCKED);
  605. if (error)
  606. return XFS_ERROR(error);
  607. extsz = xfs_get_extsz_hint(ip);
  608. offset_fsb = XFS_B_TO_FSBT(mp, offset);
  609. retry:
  610. isize = ip->i_size;
  611. if (io->io_new_size > isize)
  612. isize = io->io_new_size;
  613. error = xfs_iomap_eof_want_preallocate(mp, io, isize, offset, count,
  614. ioflag, imap, XFS_WRITE_IMAPS, &prealloc);
  615. if (error)
  616. return error;
  617. if (prealloc) {
  618. aligned_offset = XFS_WRITEIO_ALIGN(mp, (offset + count - 1));
  619. ioalign = XFS_B_TO_FSBT(mp, aligned_offset);
  620. last_fsb = ioalign + mp->m_writeio_blocks;
  621. } else {
  622. last_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)(offset + count)));
  623. }
  624. if (prealloc || extsz) {
  625. error = xfs_iomap_eof_align_last_fsb(mp, io, isize, extsz,
  626. &last_fsb);
  627. if (error)
  628. return error;
  629. }
  630. nimaps = XFS_WRITE_IMAPS;
  631. firstblock = NULLFSBLOCK;
  632. error = XFS_BMAPI(mp, NULL, io, offset_fsb,
  633. (xfs_filblks_t)(last_fsb - offset_fsb),
  634. XFS_BMAPI_DELAY | XFS_BMAPI_WRITE |
  635. XFS_BMAPI_ENTIRE, &firstblock, 1, imap,
  636. &nimaps, NULL, NULL);
  637. if (error && (error != ENOSPC))
  638. return XFS_ERROR(error);
  639. /*
  640. * If bmapi returned us nothing, and if we didn't get back EDQUOT,
  641. * then we must have run out of space - flush delalloc, and retry..
  642. */
  643. if (nimaps == 0) {
  644. xfs_iomap_enter_trace(XFS_IOMAP_WRITE_NOSPACE,
  645. io, offset, count);
  646. if (xfs_flush_space(ip, &fsynced, &ioflag))
  647. return XFS_ERROR(ENOSPC);
  648. error = 0;
  649. goto retry;
  650. }
  651. if (unlikely(!imap[0].br_startblock && !(io->io_flags & XFS_IOCORE_RT)))
  652. return xfs_cmn_err_fsblock_zero(ip, &imap[0]);
  653. *ret_imap = imap[0];
  654. *nmaps = 1;
  655. return 0;
  656. }
  657. /*
  658. * Pass in a delayed allocate extent, convert it to real extents;
  659. * return to the caller the extent we create which maps on top of
  660. * the originating callers request.
  661. *
  662. * Called without a lock on the inode.
  663. */
  664. int
  665. xfs_iomap_write_allocate(
  666. xfs_inode_t *ip,
  667. xfs_off_t offset,
  668. size_t count,
  669. xfs_bmbt_irec_t *map,
  670. int *retmap)
  671. {
  672. xfs_mount_t *mp = ip->i_mount;
  673. xfs_iocore_t *io = &ip->i_iocore;
  674. xfs_fileoff_t offset_fsb, last_block;
  675. xfs_fileoff_t end_fsb, map_start_fsb;
  676. xfs_fsblock_t first_block;
  677. xfs_bmap_free_t free_list;
  678. xfs_filblks_t count_fsb;
  679. xfs_bmbt_irec_t imap[XFS_STRAT_WRITE_IMAPS];
  680. xfs_trans_t *tp;
  681. int i, nimaps, committed;
  682. int error = 0;
  683. int nres;
  684. *retmap = 0;
  685. /*
  686. * Make sure that the dquots are there.
  687. */
  688. if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
  689. return XFS_ERROR(error);
  690. offset_fsb = XFS_B_TO_FSBT(mp, offset);
  691. count_fsb = map->br_blockcount;
  692. map_start_fsb = map->br_startoff;
  693. XFS_STATS_ADD(xs_xstrat_bytes, XFS_FSB_TO_B(mp, count_fsb));
  694. while (count_fsb != 0) {
  695. /*
  696. * Set up a transaction with which to allocate the
  697. * backing store for the file. Do allocations in a
  698. * loop until we get some space in the range we are
  699. * interested in. The other space that might be allocated
  700. * is in the delayed allocation extent on which we sit
  701. * but before our buffer starts.
  702. */
  703. nimaps = 0;
  704. while (nimaps == 0) {
  705. tp = xfs_trans_alloc(mp, XFS_TRANS_STRAT_WRITE);
  706. tp->t_flags |= XFS_TRANS_RESERVE;
  707. nres = XFS_EXTENTADD_SPACE_RES(mp, XFS_DATA_FORK);
  708. error = xfs_trans_reserve(tp, nres,
  709. XFS_WRITE_LOG_RES(mp),
  710. 0, XFS_TRANS_PERM_LOG_RES,
  711. XFS_WRITE_LOG_COUNT);
  712. if (error) {
  713. xfs_trans_cancel(tp, 0);
  714. return XFS_ERROR(error);
  715. }
  716. xfs_ilock(ip, XFS_ILOCK_EXCL);
  717. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  718. xfs_trans_ihold(tp, ip);
  719. XFS_BMAP_INIT(&free_list, &first_block);
  720. nimaps = XFS_STRAT_WRITE_IMAPS;
  721. /*
  722. * Ensure we don't go beyond eof - it is possible
  723. * the extents changed since we did the read call,
  724. * we dropped the ilock in the interim.
  725. */
  726. end_fsb = XFS_B_TO_FSB(mp, ip->i_size);
  727. xfs_bmap_last_offset(NULL, ip, &last_block,
  728. XFS_DATA_FORK);
  729. last_block = XFS_FILEOFF_MAX(last_block, end_fsb);
  730. if ((map_start_fsb + count_fsb) > last_block) {
  731. count_fsb = last_block - map_start_fsb;
  732. if (count_fsb == 0) {
  733. error = EAGAIN;
  734. goto trans_cancel;
  735. }
  736. }
  737. /* Go get the actual blocks */
  738. error = XFS_BMAPI(mp, tp, io, map_start_fsb, count_fsb,
  739. XFS_BMAPI_WRITE, &first_block, 1,
  740. imap, &nimaps, &free_list, NULL);
  741. if (error)
  742. goto trans_cancel;
  743. error = xfs_bmap_finish(&tp, &free_list, &committed);
  744. if (error)
  745. goto trans_cancel;
  746. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  747. if (error)
  748. goto error0;
  749. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  750. }
  751. /*
  752. * See if we were able to allocate an extent that
  753. * covers at least part of the callers request
  754. */
  755. for (i = 0; i < nimaps; i++) {
  756. if (unlikely(!imap[i].br_startblock &&
  757. !(io->io_flags & XFS_IOCORE_RT)))
  758. return xfs_cmn_err_fsblock_zero(ip, &imap[i]);
  759. if ((offset_fsb >= imap[i].br_startoff) &&
  760. (offset_fsb < (imap[i].br_startoff +
  761. imap[i].br_blockcount))) {
  762. *map = imap[i];
  763. *retmap = 1;
  764. XFS_STATS_INC(xs_xstrat_quick);
  765. return 0;
  766. }
  767. count_fsb -= imap[i].br_blockcount;
  768. }
  769. /* So far we have not mapped the requested part of the
  770. * file, just surrounding data, try again.
  771. */
  772. nimaps--;
  773. map_start_fsb = imap[nimaps].br_startoff +
  774. imap[nimaps].br_blockcount;
  775. }
  776. trans_cancel:
  777. xfs_bmap_cancel(&free_list);
  778. xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
  779. error0:
  780. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  781. return XFS_ERROR(error);
  782. }
  783. int
  784. xfs_iomap_write_unwritten(
  785. xfs_inode_t *ip,
  786. xfs_off_t offset,
  787. size_t count)
  788. {
  789. xfs_mount_t *mp = ip->i_mount;
  790. xfs_iocore_t *io = &ip->i_iocore;
  791. xfs_fileoff_t offset_fsb;
  792. xfs_filblks_t count_fsb;
  793. xfs_filblks_t numblks_fsb;
  794. xfs_fsblock_t firstfsb;
  795. int nimaps;
  796. xfs_trans_t *tp;
  797. xfs_bmbt_irec_t imap;
  798. xfs_bmap_free_t free_list;
  799. uint resblks;
  800. int committed;
  801. int error;
  802. xfs_iomap_enter_trace(XFS_IOMAP_UNWRITTEN,
  803. &ip->i_iocore, offset, count);
  804. offset_fsb = XFS_B_TO_FSBT(mp, offset);
  805. count_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)offset + count);
  806. count_fsb = (xfs_filblks_t)(count_fsb - offset_fsb);
  807. resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0) << 1;
  808. do {
  809. /*
  810. * set up a transaction to convert the range of extents
  811. * from unwritten to real. Do allocations in a loop until
  812. * we have covered the range passed in.
  813. */
  814. tp = xfs_trans_alloc(mp, XFS_TRANS_STRAT_WRITE);
  815. tp->t_flags |= XFS_TRANS_RESERVE;
  816. error = xfs_trans_reserve(tp, resblks,
  817. XFS_WRITE_LOG_RES(mp), 0,
  818. XFS_TRANS_PERM_LOG_RES,
  819. XFS_WRITE_LOG_COUNT);
  820. if (error) {
  821. xfs_trans_cancel(tp, 0);
  822. return XFS_ERROR(error);
  823. }
  824. xfs_ilock(ip, XFS_ILOCK_EXCL);
  825. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  826. xfs_trans_ihold(tp, ip);
  827. /*
  828. * Modify the unwritten extent state of the buffer.
  829. */
  830. XFS_BMAP_INIT(&free_list, &firstfsb);
  831. nimaps = 1;
  832. error = XFS_BMAPI(mp, tp, io, offset_fsb, count_fsb,
  833. XFS_BMAPI_WRITE|XFS_BMAPI_CONVERT, &firstfsb,
  834. 1, &imap, &nimaps, &free_list, NULL);
  835. if (error)
  836. goto error_on_bmapi_transaction;
  837. error = xfs_bmap_finish(&(tp), &(free_list), &committed);
  838. if (error)
  839. goto error_on_bmapi_transaction;
  840. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  841. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  842. if (error)
  843. return XFS_ERROR(error);
  844. if (unlikely(!imap.br_startblock &&
  845. !(io->io_flags & XFS_IOCORE_RT)))
  846. return xfs_cmn_err_fsblock_zero(ip, &imap);
  847. if ((numblks_fsb = imap.br_blockcount) == 0) {
  848. /*
  849. * The numblks_fsb value should always get
  850. * smaller, otherwise the loop is stuck.
  851. */
  852. ASSERT(imap.br_blockcount);
  853. break;
  854. }
  855. offset_fsb += numblks_fsb;
  856. count_fsb -= numblks_fsb;
  857. } while (count_fsb > 0);
  858. return 0;
  859. error_on_bmapi_transaction:
  860. xfs_bmap_cancel(&free_list);
  861. xfs_trans_cancel(tp, (XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT));
  862. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  863. return XFS_ERROR(error);
  864. }