xfs_iomap.c 26 KB

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