xfs_iomap.c 26 KB

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