xfs_lrw.c 25 KB

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