xfs_lrw.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022
  1. /*
  2. * Copyright (c) 2000-2003,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_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_bmap.h"
  39. #include "xfs_btree.h"
  40. #include "xfs_ialloc.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_attr.h"
  47. #include "xfs_inode_item.h"
  48. #include "xfs_buf_item.h"
  49. #include "xfs_utils.h"
  50. #include "xfs_iomap.h"
  51. #include <linux/capability.h>
  52. #include <linux/writeback.h>
  53. #if defined(XFS_RW_TRACE)
  54. void
  55. xfs_rw_enter_trace(
  56. int tag,
  57. xfs_iocore_t *io,
  58. void *data,
  59. size_t segs,
  60. loff_t offset,
  61. int ioflags)
  62. {
  63. xfs_inode_t *ip = XFS_IO_INODE(io);
  64. if (ip->i_rwtrace == NULL)
  65. return;
  66. ktrace_enter(ip->i_rwtrace,
  67. (void *)(unsigned long)tag,
  68. (void *)ip,
  69. (void *)((unsigned long)((ip->i_d.di_size >> 32) & 0xffffffff)),
  70. (void *)((unsigned long)(ip->i_d.di_size & 0xffffffff)),
  71. (void *)data,
  72. (void *)((unsigned long)segs),
  73. (void *)((unsigned long)((offset >> 32) & 0xffffffff)),
  74. (void *)((unsigned long)(offset & 0xffffffff)),
  75. (void *)((unsigned long)ioflags),
  76. (void *)((unsigned long)((io->io_new_size >> 32) & 0xffffffff)),
  77. (void *)((unsigned long)(io->io_new_size & 0xffffffff)),
  78. (void *)((unsigned long)current_pid()),
  79. (void *)NULL,
  80. (void *)NULL,
  81. (void *)NULL,
  82. (void *)NULL);
  83. }
  84. void
  85. xfs_inval_cached_trace(
  86. xfs_iocore_t *io,
  87. xfs_off_t offset,
  88. xfs_off_t len,
  89. xfs_off_t first,
  90. xfs_off_t last)
  91. {
  92. xfs_inode_t *ip = XFS_IO_INODE(io);
  93. if (ip->i_rwtrace == NULL)
  94. return;
  95. ktrace_enter(ip->i_rwtrace,
  96. (void *)(__psint_t)XFS_INVAL_CACHED,
  97. (void *)ip,
  98. (void *)((unsigned long)((offset >> 32) & 0xffffffff)),
  99. (void *)((unsigned long)(offset & 0xffffffff)),
  100. (void *)((unsigned long)((len >> 32) & 0xffffffff)),
  101. (void *)((unsigned long)(len & 0xffffffff)),
  102. (void *)((unsigned long)((first >> 32) & 0xffffffff)),
  103. (void *)((unsigned long)(first & 0xffffffff)),
  104. (void *)((unsigned long)((last >> 32) & 0xffffffff)),
  105. (void *)((unsigned long)(last & 0xffffffff)),
  106. (void *)((unsigned long)current_pid()),
  107. (void *)NULL,
  108. (void *)NULL,
  109. (void *)NULL,
  110. (void *)NULL,
  111. (void *)NULL);
  112. }
  113. #endif
  114. /*
  115. * xfs_iozero
  116. *
  117. * xfs_iozero clears the specified range of buffer supplied,
  118. * and marks all the affected blocks as valid and modified. If
  119. * an affected block is not allocated, it will be allocated. If
  120. * an affected block is not completely overwritten, and is not
  121. * valid before the operation, it will be read from disk before
  122. * being partially zeroed.
  123. */
  124. STATIC int
  125. xfs_iozero(
  126. struct inode *ip, /* inode */
  127. loff_t pos, /* offset in file */
  128. size_t count) /* size of data to zero */
  129. {
  130. unsigned bytes;
  131. struct page *page;
  132. struct address_space *mapping;
  133. int status;
  134. mapping = ip->i_mapping;
  135. do {
  136. unsigned long index, offset;
  137. offset = (pos & (PAGE_CACHE_SIZE -1)); /* Within page */
  138. index = pos >> PAGE_CACHE_SHIFT;
  139. bytes = PAGE_CACHE_SIZE - offset;
  140. if (bytes > count)
  141. bytes = count;
  142. status = -ENOMEM;
  143. page = grab_cache_page(mapping, index);
  144. if (!page)
  145. break;
  146. status = mapping->a_ops->prepare_write(NULL, page, offset,
  147. offset + bytes);
  148. if (status)
  149. goto unlock;
  150. memclear_highpage_flush(page, offset, bytes);
  151. status = mapping->a_ops->commit_write(NULL, page, offset,
  152. offset + bytes);
  153. if (!status) {
  154. pos += bytes;
  155. count -= bytes;
  156. }
  157. unlock:
  158. unlock_page(page);
  159. page_cache_release(page);
  160. if (status)
  161. break;
  162. } while (count);
  163. return (-status);
  164. }
  165. ssize_t /* bytes read, or (-) error */
  166. xfs_read(
  167. bhv_desc_t *bdp,
  168. struct kiocb *iocb,
  169. const struct iovec *iovp,
  170. unsigned int segs,
  171. loff_t *offset,
  172. int ioflags,
  173. cred_t *credp)
  174. {
  175. struct file *file = iocb->ki_filp;
  176. struct inode *inode = file->f_mapping->host;
  177. size_t size = 0;
  178. ssize_t ret;
  179. xfs_fsize_t n;
  180. xfs_inode_t *ip;
  181. xfs_mount_t *mp;
  182. bhv_vnode_t *vp;
  183. unsigned long seg;
  184. ip = XFS_BHVTOI(bdp);
  185. vp = BHV_TO_VNODE(bdp);
  186. mp = ip->i_mount;
  187. XFS_STATS_INC(xs_read_calls);
  188. /* START copy & waste from filemap.c */
  189. for (seg = 0; seg < segs; seg++) {
  190. const struct iovec *iv = &iovp[seg];
  191. /*
  192. * If any segment has a negative length, or the cumulative
  193. * length ever wraps negative then return -EINVAL.
  194. */
  195. size += iv->iov_len;
  196. if (unlikely((ssize_t)(size|iv->iov_len) < 0))
  197. return XFS_ERROR(-EINVAL);
  198. }
  199. /* END copy & waste from filemap.c */
  200. if (unlikely(ioflags & IO_ISDIRECT)) {
  201. xfs_buftarg_t *target =
  202. (ip->i_d.di_flags & XFS_DIFLAG_REALTIME) ?
  203. mp->m_rtdev_targp : mp->m_ddev_targp;
  204. if ((*offset & target->bt_smask) ||
  205. (size & target->bt_smask)) {
  206. if (*offset == ip->i_d.di_size) {
  207. return (0);
  208. }
  209. return -XFS_ERROR(EINVAL);
  210. }
  211. }
  212. n = XFS_MAXIOFFSET(mp) - *offset;
  213. if ((n <= 0) || (size == 0))
  214. return 0;
  215. if (n < size)
  216. size = n;
  217. if (XFS_FORCED_SHUTDOWN(mp))
  218. return -EIO;
  219. if (unlikely(ioflags & IO_ISDIRECT))
  220. mutex_lock(&inode->i_mutex);
  221. xfs_ilock(ip, XFS_IOLOCK_SHARED);
  222. if (DM_EVENT_ENABLED(vp->v_vfsp, ip, DM_EVENT_READ) &&
  223. !(ioflags & IO_INVIS)) {
  224. bhv_vrwlock_t locktype = VRWLOCK_READ;
  225. int dmflags = FILP_DELAY_FLAG(file) | DM_SEM_FLAG_RD(ioflags);
  226. ret = -XFS_SEND_DATA(mp, DM_EVENT_READ,
  227. BHV_TO_VNODE(bdp), *offset, size,
  228. dmflags, &locktype);
  229. if (ret) {
  230. xfs_iunlock(ip, XFS_IOLOCK_SHARED);
  231. if (unlikely(ioflags & IO_ISDIRECT))
  232. mutex_unlock(&inode->i_mutex);
  233. return ret;
  234. }
  235. }
  236. if (unlikely(ioflags & IO_ISDIRECT)) {
  237. if (VN_CACHED(vp))
  238. bhv_vop_flushinval_pages(vp, ctooff(offtoct(*offset)),
  239. -1, FI_REMAPF_LOCKED);
  240. mutex_unlock(&inode->i_mutex);
  241. }
  242. xfs_rw_enter_trace(XFS_READ_ENTER, &ip->i_iocore,
  243. (void *)iovp, segs, *offset, ioflags);
  244. iocb->ki_pos = *offset;
  245. ret = generic_file_aio_read(iocb, iovp, segs, *offset);
  246. if (ret == -EIOCBQUEUED && !(ioflags & IO_ISAIO))
  247. ret = wait_on_sync_kiocb(iocb);
  248. if (ret > 0)
  249. XFS_STATS_ADD(xs_read_bytes, ret);
  250. xfs_iunlock(ip, XFS_IOLOCK_SHARED);
  251. return ret;
  252. }
  253. ssize_t
  254. xfs_sendfile(
  255. bhv_desc_t *bdp,
  256. struct file *filp,
  257. loff_t *offset,
  258. int ioflags,
  259. size_t count,
  260. read_actor_t actor,
  261. void *target,
  262. cred_t *credp)
  263. {
  264. xfs_inode_t *ip = XFS_BHVTOI(bdp);
  265. xfs_mount_t *mp = ip->i_mount;
  266. ssize_t ret;
  267. XFS_STATS_INC(xs_read_calls);
  268. if (XFS_FORCED_SHUTDOWN(mp))
  269. return -EIO;
  270. xfs_ilock(ip, XFS_IOLOCK_SHARED);
  271. if (DM_EVENT_ENABLED(BHV_TO_VNODE(bdp)->v_vfsp, ip, DM_EVENT_READ) &&
  272. (!(ioflags & IO_INVIS))) {
  273. bhv_vrwlock_t locktype = VRWLOCK_READ;
  274. int error;
  275. error = XFS_SEND_DATA(mp, DM_EVENT_READ, BHV_TO_VNODE(bdp),
  276. *offset, count,
  277. FILP_DELAY_FLAG(filp), &locktype);
  278. if (error) {
  279. xfs_iunlock(ip, XFS_IOLOCK_SHARED);
  280. return -error;
  281. }
  282. }
  283. xfs_rw_enter_trace(XFS_SENDFILE_ENTER, &ip->i_iocore,
  284. (void *)(unsigned long)target, count, *offset, ioflags);
  285. ret = generic_file_sendfile(filp, offset, count, actor, target);
  286. if (ret > 0)
  287. XFS_STATS_ADD(xs_read_bytes, ret);
  288. xfs_iunlock(ip, XFS_IOLOCK_SHARED);
  289. return ret;
  290. }
  291. ssize_t
  292. xfs_splice_read(
  293. bhv_desc_t *bdp,
  294. struct file *infilp,
  295. loff_t *ppos,
  296. struct pipe_inode_info *pipe,
  297. size_t count,
  298. int flags,
  299. int ioflags,
  300. cred_t *credp)
  301. {
  302. xfs_inode_t *ip = XFS_BHVTOI(bdp);
  303. xfs_mount_t *mp = ip->i_mount;
  304. ssize_t ret;
  305. XFS_STATS_INC(xs_read_calls);
  306. if (XFS_FORCED_SHUTDOWN(ip->i_mount))
  307. return -EIO;
  308. xfs_ilock(ip, XFS_IOLOCK_SHARED);
  309. if (DM_EVENT_ENABLED(BHV_TO_VNODE(bdp)->v_vfsp, ip, DM_EVENT_READ) &&
  310. (!(ioflags & IO_INVIS))) {
  311. bhv_vrwlock_t locktype = VRWLOCK_READ;
  312. int error;
  313. error = XFS_SEND_DATA(mp, DM_EVENT_READ, BHV_TO_VNODE(bdp),
  314. *ppos, count,
  315. FILP_DELAY_FLAG(infilp), &locktype);
  316. if (error) {
  317. xfs_iunlock(ip, XFS_IOLOCK_SHARED);
  318. return -error;
  319. }
  320. }
  321. xfs_rw_enter_trace(XFS_SPLICE_READ_ENTER, &ip->i_iocore,
  322. pipe, count, *ppos, ioflags);
  323. ret = generic_file_splice_read(infilp, ppos, pipe, count, flags);
  324. if (ret > 0)
  325. XFS_STATS_ADD(xs_read_bytes, ret);
  326. xfs_iunlock(ip, XFS_IOLOCK_SHARED);
  327. return ret;
  328. }
  329. ssize_t
  330. xfs_splice_write(
  331. bhv_desc_t *bdp,
  332. struct pipe_inode_info *pipe,
  333. struct file *outfilp,
  334. loff_t *ppos,
  335. size_t count,
  336. int flags,
  337. int ioflags,
  338. cred_t *credp)
  339. {
  340. xfs_inode_t *ip = XFS_BHVTOI(bdp);
  341. xfs_mount_t *mp = ip->i_mount;
  342. ssize_t ret;
  343. struct inode *inode = outfilp->f_mapping->host;
  344. xfs_fsize_t isize;
  345. XFS_STATS_INC(xs_write_calls);
  346. if (XFS_FORCED_SHUTDOWN(ip->i_mount))
  347. return -EIO;
  348. xfs_ilock(ip, XFS_IOLOCK_EXCL);
  349. if (DM_EVENT_ENABLED(BHV_TO_VNODE(bdp)->v_vfsp, ip, DM_EVENT_WRITE) &&
  350. (!(ioflags & IO_INVIS))) {
  351. bhv_vrwlock_t locktype = VRWLOCK_WRITE;
  352. int error;
  353. error = XFS_SEND_DATA(mp, DM_EVENT_WRITE, BHV_TO_VNODE(bdp),
  354. *ppos, count,
  355. FILP_DELAY_FLAG(outfilp), &locktype);
  356. if (error) {
  357. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  358. return -error;
  359. }
  360. }
  361. xfs_rw_enter_trace(XFS_SPLICE_WRITE_ENTER, &ip->i_iocore,
  362. pipe, count, *ppos, ioflags);
  363. ret = generic_file_splice_write(pipe, outfilp, ppos, count, flags);
  364. if (ret > 0)
  365. XFS_STATS_ADD(xs_write_bytes, ret);
  366. isize = i_size_read(inode);
  367. if (unlikely(ret < 0 && ret != -EFAULT && *ppos > isize))
  368. *ppos = isize;
  369. if (*ppos > ip->i_d.di_size) {
  370. xfs_ilock(ip, XFS_ILOCK_EXCL);
  371. if (*ppos > ip->i_d.di_size) {
  372. ip->i_d.di_size = *ppos;
  373. i_size_write(inode, *ppos);
  374. ip->i_update_core = 1;
  375. ip->i_update_size = 1;
  376. }
  377. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  378. }
  379. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  380. return ret;
  381. }
  382. /*
  383. * This routine is called to handle zeroing any space in the last
  384. * block of the file that is beyond the EOF. We do this since the
  385. * size is being increased without writing anything to that block
  386. * and we don't want anyone to read the garbage on the disk.
  387. */
  388. STATIC int /* error (positive) */
  389. xfs_zero_last_block(
  390. struct inode *ip,
  391. xfs_iocore_t *io,
  392. xfs_fsize_t offset,
  393. xfs_fsize_t isize)
  394. {
  395. xfs_fileoff_t last_fsb;
  396. xfs_mount_t *mp = io->io_mount;
  397. int nimaps;
  398. int zero_offset;
  399. int zero_len;
  400. int error = 0;
  401. xfs_bmbt_irec_t imap;
  402. ASSERT(ismrlocked(io->io_lock, MR_UPDATE) != 0);
  403. zero_offset = XFS_B_FSB_OFFSET(mp, isize);
  404. if (zero_offset == 0) {
  405. /*
  406. * There are no extra bytes in the last block on disk to
  407. * zero, so return.
  408. */
  409. return 0;
  410. }
  411. last_fsb = XFS_B_TO_FSBT(mp, isize);
  412. nimaps = 1;
  413. error = XFS_BMAPI(mp, NULL, io, last_fsb, 1, 0, NULL, 0, &imap,
  414. &nimaps, NULL, NULL);
  415. if (error) {
  416. return error;
  417. }
  418. ASSERT(nimaps > 0);
  419. /*
  420. * If the block underlying isize is just a hole, then there
  421. * is nothing to zero.
  422. */
  423. if (imap.br_startblock == HOLESTARTBLOCK) {
  424. return 0;
  425. }
  426. /*
  427. * Zero the part of the last block beyond the EOF, and write it
  428. * out sync. We need to drop the ilock while we do this so we
  429. * don't deadlock when the buffer cache calls back to us.
  430. */
  431. XFS_IUNLOCK(mp, io, XFS_ILOCK_EXCL| XFS_EXTSIZE_RD);
  432. zero_len = mp->m_sb.sb_blocksize - zero_offset;
  433. if (isize + zero_len > offset)
  434. zero_len = offset - isize;
  435. error = xfs_iozero(ip, isize, zero_len);
  436. XFS_ILOCK(mp, io, XFS_ILOCK_EXCL|XFS_EXTSIZE_RD);
  437. ASSERT(error >= 0);
  438. return error;
  439. }
  440. /*
  441. * Zero any on disk space between the current EOF and the new,
  442. * larger EOF. This handles the normal case of zeroing the remainder
  443. * of the last block in the file and the unusual case of zeroing blocks
  444. * out beyond the size of the file. This second case only happens
  445. * with fixed size extents and when the system crashes before the inode
  446. * size was updated but after blocks were allocated. If fill is set,
  447. * then any holes in the range are filled and zeroed. If not, the holes
  448. * are left alone as holes.
  449. */
  450. int /* error (positive) */
  451. xfs_zero_eof(
  452. bhv_vnode_t *vp,
  453. xfs_iocore_t *io,
  454. xfs_off_t offset, /* starting I/O offset */
  455. xfs_fsize_t isize) /* current inode size */
  456. {
  457. struct inode *ip = vn_to_inode(vp);
  458. xfs_fileoff_t start_zero_fsb;
  459. xfs_fileoff_t end_zero_fsb;
  460. xfs_fileoff_t zero_count_fsb;
  461. xfs_fileoff_t last_fsb;
  462. xfs_fileoff_t zero_off;
  463. xfs_fsize_t zero_len;
  464. xfs_mount_t *mp = io->io_mount;
  465. int nimaps;
  466. int error = 0;
  467. xfs_bmbt_irec_t imap;
  468. ASSERT(ismrlocked(io->io_lock, MR_UPDATE));
  469. ASSERT(ismrlocked(io->io_iolock, MR_UPDATE));
  470. ASSERT(offset > isize);
  471. /*
  472. * First handle zeroing the block on which isize resides.
  473. * We only zero a part of that block so it is handled specially.
  474. */
  475. error = xfs_zero_last_block(ip, io, offset, isize);
  476. if (error) {
  477. ASSERT(ismrlocked(io->io_lock, MR_UPDATE));
  478. ASSERT(ismrlocked(io->io_iolock, MR_UPDATE));
  479. return error;
  480. }
  481. /*
  482. * Calculate the range between the new size and the old
  483. * where blocks needing to be zeroed may exist. To get the
  484. * block where the last byte in the file currently resides,
  485. * we need to subtract one from the size and truncate back
  486. * to a block boundary. We subtract 1 in case the size is
  487. * exactly on a block boundary.
  488. */
  489. last_fsb = isize ? XFS_B_TO_FSBT(mp, isize - 1) : (xfs_fileoff_t)-1;
  490. start_zero_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)isize);
  491. end_zero_fsb = XFS_B_TO_FSBT(mp, offset - 1);
  492. ASSERT((xfs_sfiloff_t)last_fsb < (xfs_sfiloff_t)start_zero_fsb);
  493. if (last_fsb == end_zero_fsb) {
  494. /*
  495. * The size was only incremented on its last block.
  496. * We took care of that above, so just return.
  497. */
  498. return 0;
  499. }
  500. ASSERT(start_zero_fsb <= end_zero_fsb);
  501. while (start_zero_fsb <= end_zero_fsb) {
  502. nimaps = 1;
  503. zero_count_fsb = end_zero_fsb - start_zero_fsb + 1;
  504. error = XFS_BMAPI(mp, NULL, io, start_zero_fsb, zero_count_fsb,
  505. 0, NULL, 0, &imap, &nimaps, NULL, NULL);
  506. if (error) {
  507. ASSERT(ismrlocked(io->io_lock, MR_UPDATE));
  508. ASSERT(ismrlocked(io->io_iolock, MR_UPDATE));
  509. return error;
  510. }
  511. ASSERT(nimaps > 0);
  512. if (imap.br_state == XFS_EXT_UNWRITTEN ||
  513. imap.br_startblock == HOLESTARTBLOCK) {
  514. /*
  515. * This loop handles initializing pages that were
  516. * partially initialized by the code below this
  517. * loop. It basically zeroes the part of the page
  518. * that sits on a hole and sets the page as P_HOLE
  519. * and calls remapf if it is a mapped file.
  520. */
  521. start_zero_fsb = imap.br_startoff + imap.br_blockcount;
  522. ASSERT(start_zero_fsb <= (end_zero_fsb + 1));
  523. continue;
  524. }
  525. /*
  526. * There are blocks we need to zero.
  527. * Drop the inode lock while we're doing the I/O.
  528. * We'll still have the iolock to protect us.
  529. */
  530. XFS_IUNLOCK(mp, io, XFS_ILOCK_EXCL|XFS_EXTSIZE_RD);
  531. zero_off = XFS_FSB_TO_B(mp, start_zero_fsb);
  532. zero_len = XFS_FSB_TO_B(mp, imap.br_blockcount);
  533. if ((zero_off + zero_len) > offset)
  534. zero_len = offset - zero_off;
  535. error = xfs_iozero(ip, zero_off, zero_len);
  536. if (error) {
  537. goto out_lock;
  538. }
  539. start_zero_fsb = imap.br_startoff + imap.br_blockcount;
  540. ASSERT(start_zero_fsb <= (end_zero_fsb + 1));
  541. XFS_ILOCK(mp, io, XFS_ILOCK_EXCL|XFS_EXTSIZE_RD);
  542. }
  543. return 0;
  544. out_lock:
  545. XFS_ILOCK(mp, io, XFS_ILOCK_EXCL|XFS_EXTSIZE_RD);
  546. ASSERT(error >= 0);
  547. return error;
  548. }
  549. ssize_t /* bytes written, or (-) error */
  550. xfs_write(
  551. bhv_desc_t *bdp,
  552. struct kiocb *iocb,
  553. const struct iovec *iovp,
  554. unsigned int nsegs,
  555. loff_t *offset,
  556. int ioflags,
  557. cred_t *credp)
  558. {
  559. struct file *file = iocb->ki_filp;
  560. struct address_space *mapping = file->f_mapping;
  561. struct inode *inode = mapping->host;
  562. unsigned long segs = nsegs;
  563. xfs_inode_t *xip;
  564. xfs_mount_t *mp;
  565. ssize_t ret = 0, error = 0;
  566. xfs_fsize_t isize, new_size;
  567. xfs_iocore_t *io;
  568. bhv_vnode_t *vp;
  569. unsigned long seg;
  570. int iolock;
  571. int eventsent = 0;
  572. bhv_vrwlock_t locktype;
  573. size_t ocount = 0, count;
  574. loff_t pos;
  575. int need_i_mutex = 1, need_flush = 0;
  576. XFS_STATS_INC(xs_write_calls);
  577. vp = BHV_TO_VNODE(bdp);
  578. xip = XFS_BHVTOI(bdp);
  579. for (seg = 0; seg < segs; seg++) {
  580. const struct iovec *iv = &iovp[seg];
  581. /*
  582. * If any segment has a negative length, or the cumulative
  583. * length ever wraps negative then return -EINVAL.
  584. */
  585. ocount += iv->iov_len;
  586. if (unlikely((ssize_t)(ocount|iv->iov_len) < 0))
  587. return -EINVAL;
  588. if (access_ok(VERIFY_READ, iv->iov_base, iv->iov_len))
  589. continue;
  590. if (seg == 0)
  591. return -EFAULT;
  592. segs = seg;
  593. ocount -= iv->iov_len; /* This segment is no good */
  594. break;
  595. }
  596. count = ocount;
  597. pos = *offset;
  598. if (count == 0)
  599. return 0;
  600. io = &xip->i_iocore;
  601. mp = io->io_mount;
  602. vfs_wait_for_freeze(vp->v_vfsp, SB_FREEZE_WRITE);
  603. if (XFS_FORCED_SHUTDOWN(mp))
  604. return -EIO;
  605. if (ioflags & IO_ISDIRECT) {
  606. xfs_buftarg_t *target =
  607. (xip->i_d.di_flags & XFS_DIFLAG_REALTIME) ?
  608. mp->m_rtdev_targp : mp->m_ddev_targp;
  609. if ((pos & target->bt_smask) || (count & target->bt_smask))
  610. return XFS_ERROR(-EINVAL);
  611. if (!VN_CACHED(vp) && pos < i_size_read(inode))
  612. need_i_mutex = 0;
  613. if (VN_CACHED(vp))
  614. need_flush = 1;
  615. }
  616. relock:
  617. if (need_i_mutex) {
  618. iolock = XFS_IOLOCK_EXCL;
  619. locktype = VRWLOCK_WRITE;
  620. mutex_lock(&inode->i_mutex);
  621. } else {
  622. iolock = XFS_IOLOCK_SHARED;
  623. locktype = VRWLOCK_WRITE_DIRECT;
  624. }
  625. xfs_ilock(xip, XFS_ILOCK_EXCL|iolock);
  626. isize = i_size_read(inode);
  627. if (file->f_flags & O_APPEND)
  628. *offset = isize;
  629. start:
  630. error = -generic_write_checks(file, &pos, &count,
  631. S_ISBLK(inode->i_mode));
  632. if (error) {
  633. xfs_iunlock(xip, XFS_ILOCK_EXCL|iolock);
  634. goto out_unlock_mutex;
  635. }
  636. new_size = pos + count;
  637. if (new_size > isize)
  638. io->io_new_size = new_size;
  639. if ((DM_EVENT_ENABLED(vp->v_vfsp, xip, DM_EVENT_WRITE) &&
  640. !(ioflags & IO_INVIS) && !eventsent)) {
  641. loff_t savedsize = pos;
  642. int dmflags = FILP_DELAY_FLAG(file);
  643. if (need_i_mutex)
  644. dmflags |= DM_FLAGS_IMUX;
  645. xfs_iunlock(xip, XFS_ILOCK_EXCL);
  646. error = XFS_SEND_DATA(xip->i_mount, DM_EVENT_WRITE, vp,
  647. pos, count,
  648. dmflags, &locktype);
  649. if (error) {
  650. xfs_iunlock(xip, iolock);
  651. goto out_unlock_mutex;
  652. }
  653. xfs_ilock(xip, XFS_ILOCK_EXCL);
  654. eventsent = 1;
  655. /*
  656. * The iolock was dropped and reacquired in XFS_SEND_DATA
  657. * so we have to recheck the size when appending.
  658. * We will only "goto start;" once, since having sent the
  659. * event prevents another call to XFS_SEND_DATA, which is
  660. * what allows the size to change in the first place.
  661. */
  662. if ((file->f_flags & O_APPEND) && savedsize != isize) {
  663. pos = isize = xip->i_d.di_size;
  664. goto start;
  665. }
  666. }
  667. if (likely(!(ioflags & IO_INVIS))) {
  668. file_update_time(file);
  669. xfs_ichgtime_fast(xip, inode,
  670. XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  671. }
  672. /*
  673. * If the offset is beyond the size of the file, we have a couple
  674. * of things to do. First, if there is already space allocated
  675. * we need to either create holes or zero the disk or ...
  676. *
  677. * If there is a page where the previous size lands, we need
  678. * to zero it out up to the new size.
  679. */
  680. if (pos > isize) {
  681. error = xfs_zero_eof(BHV_TO_VNODE(bdp), io, pos, isize);
  682. if (error) {
  683. xfs_iunlock(xip, XFS_ILOCK_EXCL|iolock);
  684. goto out_unlock_mutex;
  685. }
  686. }
  687. xfs_iunlock(xip, XFS_ILOCK_EXCL);
  688. /*
  689. * If we're writing the file then make sure to clear the
  690. * setuid and setgid bits if the process is not being run
  691. * by root. This keeps people from modifying setuid and
  692. * setgid binaries.
  693. */
  694. if (((xip->i_d.di_mode & S_ISUID) ||
  695. ((xip->i_d.di_mode & (S_ISGID | S_IXGRP)) ==
  696. (S_ISGID | S_IXGRP))) &&
  697. !capable(CAP_FSETID)) {
  698. error = xfs_write_clear_setuid(xip);
  699. if (likely(!error))
  700. error = -remove_suid(file->f_path.dentry);
  701. if (unlikely(error)) {
  702. xfs_iunlock(xip, iolock);
  703. goto out_unlock_mutex;
  704. }
  705. }
  706. retry:
  707. /* We can write back this queue in page reclaim */
  708. current->backing_dev_info = mapping->backing_dev_info;
  709. if ((ioflags & IO_ISDIRECT)) {
  710. if (need_flush) {
  711. xfs_inval_cached_trace(io, pos, -1,
  712. ctooff(offtoct(pos)), -1);
  713. bhv_vop_flushinval_pages(vp, ctooff(offtoct(pos)),
  714. -1, FI_REMAPF_LOCKED);
  715. }
  716. if (need_i_mutex) {
  717. /* demote the lock now the cached pages are gone */
  718. XFS_ILOCK_DEMOTE(mp, io, XFS_IOLOCK_EXCL);
  719. mutex_unlock(&inode->i_mutex);
  720. iolock = XFS_IOLOCK_SHARED;
  721. locktype = VRWLOCK_WRITE_DIRECT;
  722. need_i_mutex = 0;
  723. }
  724. xfs_rw_enter_trace(XFS_DIOWR_ENTER, io, (void *)iovp, segs,
  725. *offset, ioflags);
  726. ret = generic_file_direct_write(iocb, iovp,
  727. &segs, pos, offset, count, ocount);
  728. /*
  729. * direct-io write to a hole: fall through to buffered I/O
  730. * for completing the rest of the request.
  731. */
  732. if (ret >= 0 && ret != count) {
  733. XFS_STATS_ADD(xs_write_bytes, ret);
  734. pos += ret;
  735. count -= ret;
  736. need_i_mutex = 1;
  737. ioflags &= ~IO_ISDIRECT;
  738. xfs_iunlock(xip, iolock);
  739. goto relock;
  740. }
  741. } else {
  742. xfs_rw_enter_trace(XFS_WRITE_ENTER, io, (void *)iovp, segs,
  743. *offset, ioflags);
  744. ret = generic_file_buffered_write(iocb, iovp, segs,
  745. pos, offset, count, ret);
  746. }
  747. current->backing_dev_info = NULL;
  748. if (ret == -EIOCBQUEUED && !(ioflags & IO_ISAIO))
  749. ret = wait_on_sync_kiocb(iocb);
  750. if ((ret == -ENOSPC) &&
  751. DM_EVENT_ENABLED(vp->v_vfsp, xip, DM_EVENT_NOSPACE) &&
  752. !(ioflags & IO_INVIS)) {
  753. xfs_rwunlock(bdp, locktype);
  754. if (need_i_mutex)
  755. mutex_unlock(&inode->i_mutex);
  756. error = XFS_SEND_NAMESP(xip->i_mount, DM_EVENT_NOSPACE, vp,
  757. DM_RIGHT_NULL, vp, DM_RIGHT_NULL, NULL, NULL,
  758. 0, 0, 0); /* Delay flag intentionally unused */
  759. if (error)
  760. goto out_nounlocks;
  761. if (need_i_mutex)
  762. mutex_lock(&inode->i_mutex);
  763. xfs_rwlock(bdp, locktype);
  764. pos = xip->i_d.di_size;
  765. ret = 0;
  766. goto retry;
  767. }
  768. isize = i_size_read(inode);
  769. if (unlikely(ret < 0 && ret != -EFAULT && *offset > isize))
  770. *offset = isize;
  771. if (*offset > xip->i_d.di_size) {
  772. xfs_ilock(xip, XFS_ILOCK_EXCL);
  773. if (*offset > xip->i_d.di_size) {
  774. xip->i_d.di_size = *offset;
  775. i_size_write(inode, *offset);
  776. xip->i_update_core = 1;
  777. xip->i_update_size = 1;
  778. }
  779. xfs_iunlock(xip, XFS_ILOCK_EXCL);
  780. }
  781. error = -ret;
  782. if (ret <= 0)
  783. goto out_unlock_internal;
  784. XFS_STATS_ADD(xs_write_bytes, ret);
  785. /* Handle various SYNC-type writes */
  786. if ((file->f_flags & O_SYNC) || IS_SYNC(inode)) {
  787. error = xfs_write_sync_logforce(mp, xip);
  788. if (error)
  789. goto out_unlock_internal;
  790. xfs_rwunlock(bdp, locktype);
  791. if (need_i_mutex)
  792. mutex_unlock(&inode->i_mutex);
  793. error = sync_page_range(inode, mapping, pos, ret);
  794. if (!error)
  795. error = ret;
  796. return error;
  797. }
  798. out_unlock_internal:
  799. xfs_rwunlock(bdp, locktype);
  800. out_unlock_mutex:
  801. if (need_i_mutex)
  802. mutex_unlock(&inode->i_mutex);
  803. out_nounlocks:
  804. return -error;
  805. }
  806. /*
  807. * All xfs metadata buffers except log state machine buffers
  808. * get this attached as their b_bdstrat callback function.
  809. * This is so that we can catch a buffer
  810. * after prematurely unpinning it to forcibly shutdown the filesystem.
  811. */
  812. int
  813. xfs_bdstrat_cb(struct xfs_buf *bp)
  814. {
  815. xfs_mount_t *mp;
  816. mp = XFS_BUF_FSPRIVATE3(bp, xfs_mount_t *);
  817. if (!XFS_FORCED_SHUTDOWN(mp)) {
  818. xfs_buf_iorequest(bp);
  819. return 0;
  820. } else {
  821. xfs_buftrace("XFS__BDSTRAT IOERROR", bp);
  822. /*
  823. * Metadata write that didn't get logged but
  824. * written delayed anyway. These aren't associated
  825. * with a transaction, and can be ignored.
  826. */
  827. if (XFS_BUF_IODONE_FUNC(bp) == NULL &&
  828. (XFS_BUF_ISREAD(bp)) == 0)
  829. return (xfs_bioerror_relse(bp));
  830. else
  831. return (xfs_bioerror(bp));
  832. }
  833. }
  834. int
  835. xfs_bmap(bhv_desc_t *bdp,
  836. xfs_off_t offset,
  837. ssize_t count,
  838. int flags,
  839. xfs_iomap_t *iomapp,
  840. int *niomaps)
  841. {
  842. xfs_inode_t *ip = XFS_BHVTOI(bdp);
  843. xfs_iocore_t *io = &ip->i_iocore;
  844. ASSERT((ip->i_d.di_mode & S_IFMT) == S_IFREG);
  845. ASSERT(((ip->i_d.di_flags & XFS_DIFLAG_REALTIME) != 0) ==
  846. ((ip->i_iocore.io_flags & XFS_IOCORE_RT) != 0));
  847. return xfs_iomap(io, offset, count, flags, iomapp, niomaps);
  848. }
  849. /*
  850. * Wrapper around bdstrat so that we can stop data
  851. * from going to disk in case we are shutting down the filesystem.
  852. * Typically user data goes thru this path; one of the exceptions
  853. * is the superblock.
  854. */
  855. int
  856. xfsbdstrat(
  857. struct xfs_mount *mp,
  858. struct xfs_buf *bp)
  859. {
  860. ASSERT(mp);
  861. if (!XFS_FORCED_SHUTDOWN(mp)) {
  862. /* Grio redirection would go here
  863. * if (XFS_BUF_IS_GRIO(bp)) {
  864. */
  865. xfs_buf_iorequest(bp);
  866. return 0;
  867. }
  868. xfs_buftrace("XFSBDSTRAT IOERROR", bp);
  869. return (xfs_bioerror_relse(bp));
  870. }
  871. /*
  872. * If the underlying (data/log/rt) device is readonly, there are some
  873. * operations that cannot proceed.
  874. */
  875. int
  876. xfs_dev_is_read_only(
  877. xfs_mount_t *mp,
  878. char *message)
  879. {
  880. if (xfs_readonly_buftarg(mp->m_ddev_targp) ||
  881. xfs_readonly_buftarg(mp->m_logdev_targp) ||
  882. (mp->m_rtdev_targp && xfs_readonly_buftarg(mp->m_rtdev_targp))) {
  883. cmn_err(CE_NOTE,
  884. "XFS: %s required on read-only device.", message);
  885. cmn_err(CE_NOTE,
  886. "XFS: write access unavailable, cannot proceed.");
  887. return EROFS;
  888. }
  889. return 0;
  890. }