xfs_lrw.c 24 KB

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