xfs_iomap.c 26 KB

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