xfs_iomap.c 24 KB

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