xfs_iomap.c 25 KB

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