xfs_iomap.c 25 KB

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