xfs_iomap.c 26 KB

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