xfs_lrw.c 24 KB

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