xfs_iomap.c 25 KB

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