xfs_iomap.c 26 KB

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