xfs_iomap.c 25 KB

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