xfs_iomap.c 24 KB

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