xfs_lrw.c 24 KB

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