xfs_iomap.c 26 KB

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