xfs_iomap.c 26 KB

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