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