xfs_lrw.c 25 KB

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