/fs/xfs/xfs_file.c

https://bitbucket.org/cyanogenmod/android_kernel_asus_tf300t · C · 1118 lines · 718 code · 146 blank · 254 comment · 121 complexity · 0c045492e141b345ae5b69e892611918 MD5 · raw file

  1. /*
  2. * Copyright (c) 2000-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_sb.h"
  24. #include "xfs_ag.h"
  25. #include "xfs_trans.h"
  26. #include "xfs_mount.h"
  27. #include "xfs_bmap_btree.h"
  28. #include "xfs_alloc.h"
  29. #include "xfs_dinode.h"
  30. #include "xfs_inode.h"
  31. #include "xfs_inode_item.h"
  32. #include "xfs_bmap.h"
  33. #include "xfs_error.h"
  34. #include "xfs_vnodeops.h"
  35. #include "xfs_da_btree.h"
  36. #include "xfs_ioctl.h"
  37. #include "xfs_trace.h"
  38. #include <linux/dcache.h>
  39. #include <linux/falloc.h>
  40. static const struct vm_operations_struct xfs_file_vm_ops;
  41. /*
  42. * Locking primitives for read and write IO paths to ensure we consistently use
  43. * and order the inode->i_mutex, ip->i_lock and ip->i_iolock.
  44. */
  45. static inline void
  46. xfs_rw_ilock(
  47. struct xfs_inode *ip,
  48. int type)
  49. {
  50. if (type & XFS_IOLOCK_EXCL)
  51. mutex_lock(&VFS_I(ip)->i_mutex);
  52. xfs_ilock(ip, type);
  53. }
  54. static inline void
  55. xfs_rw_iunlock(
  56. struct xfs_inode *ip,
  57. int type)
  58. {
  59. xfs_iunlock(ip, type);
  60. if (type & XFS_IOLOCK_EXCL)
  61. mutex_unlock(&VFS_I(ip)->i_mutex);
  62. }
  63. static inline void
  64. xfs_rw_ilock_demote(
  65. struct xfs_inode *ip,
  66. int type)
  67. {
  68. xfs_ilock_demote(ip, type);
  69. if (type & XFS_IOLOCK_EXCL)
  70. mutex_unlock(&VFS_I(ip)->i_mutex);
  71. }
  72. /*
  73. * xfs_iozero
  74. *
  75. * xfs_iozero clears the specified range of buffer supplied,
  76. * and marks all the affected blocks as valid and modified. If
  77. * an affected block is not allocated, it will be allocated. If
  78. * an affected block is not completely overwritten, and is not
  79. * valid before the operation, it will be read from disk before
  80. * being partially zeroed.
  81. */
  82. STATIC int
  83. xfs_iozero(
  84. struct xfs_inode *ip, /* inode */
  85. loff_t pos, /* offset in file */
  86. size_t count) /* size of data to zero */
  87. {
  88. struct page *page;
  89. struct address_space *mapping;
  90. int status;
  91. mapping = VFS_I(ip)->i_mapping;
  92. do {
  93. unsigned offset, bytes;
  94. void *fsdata;
  95. offset = (pos & (PAGE_CACHE_SIZE -1)); /* Within page */
  96. bytes = PAGE_CACHE_SIZE - offset;
  97. if (bytes > count)
  98. bytes = count;
  99. status = pagecache_write_begin(NULL, mapping, pos, bytes,
  100. AOP_FLAG_UNINTERRUPTIBLE,
  101. &page, &fsdata);
  102. if (status)
  103. break;
  104. zero_user(page, offset, bytes);
  105. status = pagecache_write_end(NULL, mapping, pos, bytes, bytes,
  106. page, fsdata);
  107. WARN_ON(status <= 0); /* can't return less than zero! */
  108. pos += bytes;
  109. count -= bytes;
  110. status = 0;
  111. } while (count);
  112. return (-status);
  113. }
  114. STATIC int
  115. xfs_file_fsync(
  116. struct file *file,
  117. loff_t start,
  118. loff_t end,
  119. int datasync)
  120. {
  121. struct inode *inode = file->f_mapping->host;
  122. struct xfs_inode *ip = XFS_I(inode);
  123. struct xfs_mount *mp = ip->i_mount;
  124. struct xfs_trans *tp;
  125. int error = 0;
  126. int log_flushed = 0;
  127. trace_xfs_file_fsync(ip);
  128. error = filemap_write_and_wait_range(inode->i_mapping, start, end);
  129. if (error)
  130. return error;
  131. if (XFS_FORCED_SHUTDOWN(mp))
  132. return -XFS_ERROR(EIO);
  133. xfs_iflags_clear(ip, XFS_ITRUNCATED);
  134. xfs_ilock(ip, XFS_IOLOCK_SHARED);
  135. xfs_ioend_wait(ip);
  136. xfs_iunlock(ip, XFS_IOLOCK_SHARED);
  137. if (mp->m_flags & XFS_MOUNT_BARRIER) {
  138. /*
  139. * If we have an RT and/or log subvolume we need to make sure
  140. * to flush the write cache the device used for file data
  141. * first. This is to ensure newly written file data make
  142. * it to disk before logging the new inode size in case of
  143. * an extending write.
  144. */
  145. if (XFS_IS_REALTIME_INODE(ip))
  146. xfs_blkdev_issue_flush(mp->m_rtdev_targp);
  147. else if (mp->m_logdev_targp != mp->m_ddev_targp)
  148. xfs_blkdev_issue_flush(mp->m_ddev_targp);
  149. }
  150. /*
  151. * We always need to make sure that the required inode state is safe on
  152. * disk. The inode might be clean but we still might need to force the
  153. * log because of committed transactions that haven't hit the disk yet.
  154. * Likewise, there could be unflushed non-transactional changes to the
  155. * inode core that have to go to disk and this requires us to issue
  156. * a synchronous transaction to capture these changes correctly.
  157. *
  158. * This code relies on the assumption that if the i_update_core field
  159. * of the inode is clear and the inode is unpinned then it is clean
  160. * and no action is required.
  161. */
  162. xfs_ilock(ip, XFS_ILOCK_SHARED);
  163. /*
  164. * First check if the VFS inode is marked dirty. All the dirtying
  165. * of non-transactional updates no goes through mark_inode_dirty*,
  166. * which allows us to distinguish beteeen pure timestamp updates
  167. * and i_size updates which need to be caught for fdatasync.
  168. * After that also theck for the dirty state in the XFS inode, which
  169. * might gets cleared when the inode gets written out via the AIL
  170. * or xfs_iflush_cluster.
  171. */
  172. if (((inode->i_state & I_DIRTY_DATASYNC) ||
  173. ((inode->i_state & I_DIRTY_SYNC) && !datasync)) &&
  174. ip->i_update_core) {
  175. /*
  176. * Kick off a transaction to log the inode core to get the
  177. * updates. The sync transaction will also force the log.
  178. */
  179. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  180. tp = xfs_trans_alloc(mp, XFS_TRANS_FSYNC_TS);
  181. error = xfs_trans_reserve(tp, 0,
  182. XFS_FSYNC_TS_LOG_RES(mp), 0, 0, 0);
  183. if (error) {
  184. xfs_trans_cancel(tp, 0);
  185. return -error;
  186. }
  187. xfs_ilock(ip, XFS_ILOCK_EXCL);
  188. /*
  189. * Note - it's possible that we might have pushed ourselves out
  190. * of the way during trans_reserve which would flush the inode.
  191. * But there's no guarantee that the inode buffer has actually
  192. * gone out yet (it's delwri). Plus the buffer could be pinned
  193. * anyway if it's part of an inode in another recent
  194. * transaction. So we play it safe and fire off the
  195. * transaction anyway.
  196. */
  197. xfs_trans_ijoin(tp, ip);
  198. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  199. xfs_trans_set_sync(tp);
  200. error = _xfs_trans_commit(tp, 0, &log_flushed);
  201. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  202. } else {
  203. /*
  204. * Timestamps/size haven't changed since last inode flush or
  205. * inode transaction commit. That means either nothing got
  206. * written or a transaction committed which caught the updates.
  207. * If the latter happened and the transaction hasn't hit the
  208. * disk yet, the inode will be still be pinned. If it is,
  209. * force the log.
  210. */
  211. if (xfs_ipincount(ip)) {
  212. error = _xfs_log_force_lsn(mp,
  213. ip->i_itemp->ili_last_lsn,
  214. XFS_LOG_SYNC, &log_flushed);
  215. }
  216. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  217. }
  218. /*
  219. * If we only have a single device, and the log force about was
  220. * a no-op we might have to flush the data device cache here.
  221. * This can only happen for fdatasync/O_DSYNC if we were overwriting
  222. * an already allocated file and thus do not have any metadata to
  223. * commit.
  224. */
  225. if ((mp->m_flags & XFS_MOUNT_BARRIER) &&
  226. mp->m_logdev_targp == mp->m_ddev_targp &&
  227. !XFS_IS_REALTIME_INODE(ip) &&
  228. !log_flushed)
  229. xfs_blkdev_issue_flush(mp->m_ddev_targp);
  230. return -error;
  231. }
  232. STATIC ssize_t
  233. xfs_file_aio_read(
  234. struct kiocb *iocb,
  235. const struct iovec *iovp,
  236. unsigned long nr_segs,
  237. loff_t pos)
  238. {
  239. struct file *file = iocb->ki_filp;
  240. struct inode *inode = file->f_mapping->host;
  241. struct xfs_inode *ip = XFS_I(inode);
  242. struct xfs_mount *mp = ip->i_mount;
  243. size_t size = 0;
  244. ssize_t ret = 0;
  245. int ioflags = 0;
  246. xfs_fsize_t n;
  247. unsigned long seg;
  248. XFS_STATS_INC(xs_read_calls);
  249. BUG_ON(iocb->ki_pos != pos);
  250. if (unlikely(file->f_flags & O_DIRECT))
  251. ioflags |= IO_ISDIRECT;
  252. if (file->f_mode & FMODE_NOCMTIME)
  253. ioflags |= IO_INVIS;
  254. /* START copy & waste from filemap.c */
  255. for (seg = 0; seg < nr_segs; seg++) {
  256. const struct iovec *iv = &iovp[seg];
  257. /*
  258. * If any segment has a negative length, or the cumulative
  259. * length ever wraps negative then return -EINVAL.
  260. */
  261. size += iv->iov_len;
  262. if (unlikely((ssize_t)(size|iv->iov_len) < 0))
  263. return XFS_ERROR(-EINVAL);
  264. }
  265. /* END copy & waste from filemap.c */
  266. if (unlikely(ioflags & IO_ISDIRECT)) {
  267. xfs_buftarg_t *target =
  268. XFS_IS_REALTIME_INODE(ip) ?
  269. mp->m_rtdev_targp : mp->m_ddev_targp;
  270. if ((iocb->ki_pos & target->bt_smask) ||
  271. (size & target->bt_smask)) {
  272. if (iocb->ki_pos == ip->i_size)
  273. return 0;
  274. return -XFS_ERROR(EINVAL);
  275. }
  276. }
  277. n = XFS_MAXIOFFSET(mp) - iocb->ki_pos;
  278. if (n <= 0 || size == 0)
  279. return 0;
  280. if (n < size)
  281. size = n;
  282. if (XFS_FORCED_SHUTDOWN(mp))
  283. return -EIO;
  284. /*
  285. * Locking is a bit tricky here. If we take an exclusive lock
  286. * for direct IO, we effectively serialise all new concurrent
  287. * read IO to this file and block it behind IO that is currently in
  288. * progress because IO in progress holds the IO lock shared. We only
  289. * need to hold the lock exclusive to blow away the page cache, so
  290. * only take lock exclusively if the page cache needs invalidation.
  291. * This allows the normal direct IO case of no page cache pages to
  292. * proceeed concurrently without serialisation.
  293. */
  294. xfs_rw_ilock(ip, XFS_IOLOCK_SHARED);
  295. if ((ioflags & IO_ISDIRECT) && inode->i_mapping->nrpages) {
  296. xfs_rw_iunlock(ip, XFS_IOLOCK_SHARED);
  297. xfs_rw_ilock(ip, XFS_IOLOCK_EXCL);
  298. if (inode->i_mapping->nrpages) {
  299. ret = -xfs_flushinval_pages(ip,
  300. (iocb->ki_pos & PAGE_CACHE_MASK),
  301. -1, FI_REMAPF_LOCKED);
  302. if (ret) {
  303. xfs_rw_iunlock(ip, XFS_IOLOCK_EXCL);
  304. return ret;
  305. }
  306. }
  307. xfs_rw_ilock_demote(ip, XFS_IOLOCK_EXCL);
  308. }
  309. trace_xfs_file_read(ip, size, iocb->ki_pos, ioflags);
  310. ret = generic_file_aio_read(iocb, iovp, nr_segs, iocb->ki_pos);
  311. if (ret > 0)
  312. XFS_STATS_ADD(xs_read_bytes, ret);
  313. xfs_rw_iunlock(ip, XFS_IOLOCK_SHARED);
  314. return ret;
  315. }
  316. STATIC ssize_t
  317. xfs_file_splice_read(
  318. struct file *infilp,
  319. loff_t *ppos,
  320. struct pipe_inode_info *pipe,
  321. size_t count,
  322. unsigned int flags)
  323. {
  324. struct xfs_inode *ip = XFS_I(infilp->f_mapping->host);
  325. int ioflags = 0;
  326. ssize_t ret;
  327. XFS_STATS_INC(xs_read_calls);
  328. if (infilp->f_mode & FMODE_NOCMTIME)
  329. ioflags |= IO_INVIS;
  330. if (XFS_FORCED_SHUTDOWN(ip->i_mount))
  331. return -EIO;
  332. xfs_rw_ilock(ip, XFS_IOLOCK_SHARED);
  333. trace_xfs_file_splice_read(ip, count, *ppos, ioflags);
  334. ret = generic_file_splice_read(infilp, ppos, pipe, count, flags);
  335. if (ret > 0)
  336. XFS_STATS_ADD(xs_read_bytes, ret);
  337. xfs_rw_iunlock(ip, XFS_IOLOCK_SHARED);
  338. return ret;
  339. }
  340. STATIC void
  341. xfs_aio_write_isize_update(
  342. struct inode *inode,
  343. loff_t *ppos,
  344. ssize_t bytes_written)
  345. {
  346. struct xfs_inode *ip = XFS_I(inode);
  347. xfs_fsize_t isize = i_size_read(inode);
  348. if (bytes_written > 0)
  349. XFS_STATS_ADD(xs_write_bytes, bytes_written);
  350. if (unlikely(bytes_written < 0 && bytes_written != -EFAULT &&
  351. *ppos > isize))
  352. *ppos = isize;
  353. if (*ppos > ip->i_size) {
  354. xfs_rw_ilock(ip, XFS_ILOCK_EXCL);
  355. if (*ppos > ip->i_size)
  356. ip->i_size = *ppos;
  357. xfs_rw_iunlock(ip, XFS_ILOCK_EXCL);
  358. }
  359. }
  360. /*
  361. * If this was a direct or synchronous I/O that failed (such as ENOSPC) then
  362. * part of the I/O may have been written to disk before the error occurred. In
  363. * this case the on-disk file size may have been adjusted beyond the in-memory
  364. * file size and now needs to be truncated back.
  365. */
  366. STATIC void
  367. xfs_aio_write_newsize_update(
  368. struct xfs_inode *ip)
  369. {
  370. if (ip->i_new_size) {
  371. xfs_rw_ilock(ip, XFS_ILOCK_EXCL);
  372. ip->i_new_size = 0;
  373. if (ip->i_d.di_size > ip->i_size)
  374. ip->i_d.di_size = ip->i_size;
  375. xfs_rw_iunlock(ip, XFS_ILOCK_EXCL);
  376. }
  377. }
  378. /*
  379. * xfs_file_splice_write() does not use xfs_rw_ilock() because
  380. * generic_file_splice_write() takes the i_mutex itself. This, in theory,
  381. * couuld cause lock inversions between the aio_write path and the splice path
  382. * if someone is doing concurrent splice(2) based writes and write(2) based
  383. * writes to the same inode. The only real way to fix this is to re-implement
  384. * the generic code here with correct locking orders.
  385. */
  386. STATIC ssize_t
  387. xfs_file_splice_write(
  388. struct pipe_inode_info *pipe,
  389. struct file *outfilp,
  390. loff_t *ppos,
  391. size_t count,
  392. unsigned int flags)
  393. {
  394. struct inode *inode = outfilp->f_mapping->host;
  395. struct xfs_inode *ip = XFS_I(inode);
  396. xfs_fsize_t new_size;
  397. int ioflags = 0;
  398. ssize_t ret;
  399. XFS_STATS_INC(xs_write_calls);
  400. if (outfilp->f_mode & FMODE_NOCMTIME)
  401. ioflags |= IO_INVIS;
  402. if (XFS_FORCED_SHUTDOWN(ip->i_mount))
  403. return -EIO;
  404. xfs_ilock(ip, XFS_IOLOCK_EXCL);
  405. new_size = *ppos + count;
  406. xfs_ilock(ip, XFS_ILOCK_EXCL);
  407. if (new_size > ip->i_size)
  408. ip->i_new_size = new_size;
  409. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  410. trace_xfs_file_splice_write(ip, count, *ppos, ioflags);
  411. ret = generic_file_splice_write(pipe, outfilp, ppos, count, flags);
  412. xfs_aio_write_isize_update(inode, ppos, ret);
  413. xfs_aio_write_newsize_update(ip);
  414. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  415. return ret;
  416. }
  417. /*
  418. * This routine is called to handle zeroing any space in the last
  419. * block of the file that is beyond the EOF. We do this since the
  420. * size is being increased without writing anything to that block
  421. * and we don't want anyone to read the garbage on the disk.
  422. */
  423. STATIC int /* error (positive) */
  424. xfs_zero_last_block(
  425. xfs_inode_t *ip,
  426. xfs_fsize_t offset,
  427. xfs_fsize_t isize)
  428. {
  429. xfs_fileoff_t last_fsb;
  430. xfs_mount_t *mp = ip->i_mount;
  431. int nimaps;
  432. int zero_offset;
  433. int zero_len;
  434. int error = 0;
  435. xfs_bmbt_irec_t imap;
  436. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
  437. zero_offset = XFS_B_FSB_OFFSET(mp, isize);
  438. if (zero_offset == 0) {
  439. /*
  440. * There are no extra bytes in the last block on disk to
  441. * zero, so return.
  442. */
  443. return 0;
  444. }
  445. last_fsb = XFS_B_TO_FSBT(mp, isize);
  446. nimaps = 1;
  447. error = xfs_bmapi(NULL, ip, last_fsb, 1, 0, NULL, 0, &imap,
  448. &nimaps, NULL);
  449. if (error) {
  450. return error;
  451. }
  452. ASSERT(nimaps > 0);
  453. /*
  454. * If the block underlying isize is just a hole, then there
  455. * is nothing to zero.
  456. */
  457. if (imap.br_startblock == HOLESTARTBLOCK) {
  458. return 0;
  459. }
  460. /*
  461. * Zero the part of the last block beyond the EOF, and write it
  462. * out sync. We need to drop the ilock while we do this so we
  463. * don't deadlock when the buffer cache calls back to us.
  464. */
  465. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  466. zero_len = mp->m_sb.sb_blocksize - zero_offset;
  467. if (isize + zero_len > offset)
  468. zero_len = offset - isize;
  469. error = xfs_iozero(ip, isize, zero_len);
  470. xfs_ilock(ip, XFS_ILOCK_EXCL);
  471. ASSERT(error >= 0);
  472. return error;
  473. }
  474. /*
  475. * Zero any on disk space between the current EOF and the new,
  476. * larger EOF. This handles the normal case of zeroing the remainder
  477. * of the last block in the file and the unusual case of zeroing blocks
  478. * out beyond the size of the file. This second case only happens
  479. * with fixed size extents and when the system crashes before the inode
  480. * size was updated but after blocks were allocated. If fill is set,
  481. * then any holes in the range are filled and zeroed. If not, the holes
  482. * are left alone as holes.
  483. */
  484. int /* error (positive) */
  485. xfs_zero_eof(
  486. xfs_inode_t *ip,
  487. xfs_off_t offset, /* starting I/O offset */
  488. xfs_fsize_t isize) /* current inode size */
  489. {
  490. xfs_mount_t *mp = ip->i_mount;
  491. xfs_fileoff_t start_zero_fsb;
  492. xfs_fileoff_t end_zero_fsb;
  493. xfs_fileoff_t zero_count_fsb;
  494. xfs_fileoff_t last_fsb;
  495. xfs_fileoff_t zero_off;
  496. xfs_fsize_t zero_len;
  497. int nimaps;
  498. int error = 0;
  499. xfs_bmbt_irec_t imap;
  500. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_IOLOCK_EXCL));
  501. ASSERT(offset > isize);
  502. /*
  503. * First handle zeroing the block on which isize resides.
  504. * We only zero a part of that block so it is handled specially.
  505. */
  506. error = xfs_zero_last_block(ip, offset, isize);
  507. if (error) {
  508. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_IOLOCK_EXCL));
  509. return error;
  510. }
  511. /*
  512. * Calculate the range between the new size and the old
  513. * where blocks needing to be zeroed may exist. To get the
  514. * block where the last byte in the file currently resides,
  515. * we need to subtract one from the size and truncate back
  516. * to a block boundary. We subtract 1 in case the size is
  517. * exactly on a block boundary.
  518. */
  519. last_fsb = isize ? XFS_B_TO_FSBT(mp, isize - 1) : (xfs_fileoff_t)-1;
  520. start_zero_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)isize);
  521. end_zero_fsb = XFS_B_TO_FSBT(mp, offset - 1);
  522. ASSERT((xfs_sfiloff_t)last_fsb < (xfs_sfiloff_t)start_zero_fsb);
  523. if (last_fsb == end_zero_fsb) {
  524. /*
  525. * The size was only incremented on its last block.
  526. * We took care of that above, so just return.
  527. */
  528. return 0;
  529. }
  530. ASSERT(start_zero_fsb <= end_zero_fsb);
  531. while (start_zero_fsb <= end_zero_fsb) {
  532. nimaps = 1;
  533. zero_count_fsb = end_zero_fsb - start_zero_fsb + 1;
  534. error = xfs_bmapi(NULL, ip, start_zero_fsb, zero_count_fsb,
  535. 0, NULL, 0, &imap, &nimaps, NULL);
  536. if (error) {
  537. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_IOLOCK_EXCL));
  538. return error;
  539. }
  540. ASSERT(nimaps > 0);
  541. if (imap.br_state == XFS_EXT_UNWRITTEN ||
  542. imap.br_startblock == HOLESTARTBLOCK) {
  543. /*
  544. * This loop handles initializing pages that were
  545. * partially initialized by the code below this
  546. * loop. It basically zeroes the part of the page
  547. * that sits on a hole and sets the page as P_HOLE
  548. * and calls remapf if it is a mapped file.
  549. */
  550. start_zero_fsb = imap.br_startoff + imap.br_blockcount;
  551. ASSERT(start_zero_fsb <= (end_zero_fsb + 1));
  552. continue;
  553. }
  554. /*
  555. * There are blocks we need to zero.
  556. * Drop the inode lock while we're doing the I/O.
  557. * We'll still have the iolock to protect us.
  558. */
  559. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  560. zero_off = XFS_FSB_TO_B(mp, start_zero_fsb);
  561. zero_len = XFS_FSB_TO_B(mp, imap.br_blockcount);
  562. if ((zero_off + zero_len) > offset)
  563. zero_len = offset - zero_off;
  564. error = xfs_iozero(ip, zero_off, zero_len);
  565. if (error) {
  566. goto out_lock;
  567. }
  568. start_zero_fsb = imap.br_startoff + imap.br_blockcount;
  569. ASSERT(start_zero_fsb <= (end_zero_fsb + 1));
  570. xfs_ilock(ip, XFS_ILOCK_EXCL);
  571. }
  572. return 0;
  573. out_lock:
  574. xfs_ilock(ip, XFS_ILOCK_EXCL);
  575. ASSERT(error >= 0);
  576. return error;
  577. }
  578. /*
  579. * Common pre-write limit and setup checks.
  580. *
  581. * Returns with iolock held according to @iolock.
  582. */
  583. STATIC ssize_t
  584. xfs_file_aio_write_checks(
  585. struct file *file,
  586. loff_t *pos,
  587. size_t *count,
  588. int *iolock)
  589. {
  590. struct inode *inode = file->f_mapping->host;
  591. struct xfs_inode *ip = XFS_I(inode);
  592. xfs_fsize_t new_size;
  593. int error = 0;
  594. xfs_rw_ilock(ip, XFS_ILOCK_EXCL);
  595. error = generic_write_checks(file, pos, count, S_ISBLK(inode->i_mode));
  596. if (error) {
  597. xfs_rw_iunlock(ip, XFS_ILOCK_EXCL | *iolock);
  598. *iolock = 0;
  599. return error;
  600. }
  601. new_size = *pos + *count;
  602. if (new_size > ip->i_size)
  603. ip->i_new_size = new_size;
  604. if (likely(!(file->f_mode & FMODE_NOCMTIME)))
  605. file_update_time(file);
  606. /*
  607. * If the offset is beyond the size of the file, we need to zero any
  608. * blocks that fall between the existing EOF and the start of this
  609. * write.
  610. */
  611. if (*pos > ip->i_size)
  612. error = -xfs_zero_eof(ip, *pos, ip->i_size);
  613. xfs_rw_iunlock(ip, XFS_ILOCK_EXCL);
  614. if (error)
  615. return error;
  616. /*
  617. * If we're writing the file then make sure to clear the setuid and
  618. * setgid bits if the process is not being run by root. This keeps
  619. * people from modifying setuid and setgid binaries.
  620. */
  621. return file_remove_suid(file);
  622. }
  623. /*
  624. * xfs_file_dio_aio_write - handle direct IO writes
  625. *
  626. * Lock the inode appropriately to prepare for and issue a direct IO write.
  627. * By separating it from the buffered write path we remove all the tricky to
  628. * follow locking changes and looping.
  629. *
  630. * If there are cached pages or we're extending the file, we need IOLOCK_EXCL
  631. * until we're sure the bytes at the new EOF have been zeroed and/or the cached
  632. * pages are flushed out.
  633. *
  634. * In most cases the direct IO writes will be done holding IOLOCK_SHARED
  635. * allowing them to be done in parallel with reads and other direct IO writes.
  636. * However, if the IO is not aligned to filesystem blocks, the direct IO layer
  637. * needs to do sub-block zeroing and that requires serialisation against other
  638. * direct IOs to the same block. In this case we need to serialise the
  639. * submission of the unaligned IOs so that we don't get racing block zeroing in
  640. * the dio layer. To avoid the problem with aio, we also need to wait for
  641. * outstanding IOs to complete so that unwritten extent conversion is completed
  642. * before we try to map the overlapping block. This is currently implemented by
  643. * hitting it with a big hammer (i.e. xfs_ioend_wait()).
  644. *
  645. * Returns with locks held indicated by @iolock and errors indicated by
  646. * negative return values.
  647. */
  648. STATIC ssize_t
  649. xfs_file_dio_aio_write(
  650. struct kiocb *iocb,
  651. const struct iovec *iovp,
  652. unsigned long nr_segs,
  653. loff_t pos,
  654. size_t ocount,
  655. int *iolock)
  656. {
  657. struct file *file = iocb->ki_filp;
  658. struct address_space *mapping = file->f_mapping;
  659. struct inode *inode = mapping->host;
  660. struct xfs_inode *ip = XFS_I(inode);
  661. struct xfs_mount *mp = ip->i_mount;
  662. ssize_t ret = 0;
  663. size_t count = ocount;
  664. int unaligned_io = 0;
  665. struct xfs_buftarg *target = XFS_IS_REALTIME_INODE(ip) ?
  666. mp->m_rtdev_targp : mp->m_ddev_targp;
  667. *iolock = 0;
  668. if ((pos & target->bt_smask) || (count & target->bt_smask))
  669. return -XFS_ERROR(EINVAL);
  670. if ((pos & mp->m_blockmask) || ((pos + count) & mp->m_blockmask))
  671. unaligned_io = 1;
  672. if (unaligned_io || mapping->nrpages || pos > ip->i_size)
  673. *iolock = XFS_IOLOCK_EXCL;
  674. else
  675. *iolock = XFS_IOLOCK_SHARED;
  676. xfs_rw_ilock(ip, *iolock);
  677. ret = xfs_file_aio_write_checks(file, &pos, &count, iolock);
  678. if (ret)
  679. return ret;
  680. /*
  681. * Recheck if there are cached pages that need invalidate after we got
  682. * the iolock to protect against other threads adding new pages while
  683. * we were waiting for the iolock.
  684. */
  685. if (mapping->nrpages && *iolock == XFS_IOLOCK_SHARED) {
  686. xfs_rw_iunlock(ip, *iolock);
  687. *iolock = XFS_IOLOCK_EXCL;
  688. xfs_rw_ilock(ip, *iolock);
  689. }
  690. if (mapping->nrpages) {
  691. ret = -xfs_flushinval_pages(ip, (pos & PAGE_CACHE_MASK), -1,
  692. FI_REMAPF_LOCKED);
  693. if (ret)
  694. return ret;
  695. }
  696. /*
  697. * If we are doing unaligned IO, wait for all other IO to drain,
  698. * otherwise demote the lock if we had to flush cached pages
  699. */
  700. if (unaligned_io)
  701. xfs_ioend_wait(ip);
  702. else if (*iolock == XFS_IOLOCK_EXCL) {
  703. xfs_rw_ilock_demote(ip, XFS_IOLOCK_EXCL);
  704. *iolock = XFS_IOLOCK_SHARED;
  705. }
  706. trace_xfs_file_direct_write(ip, count, iocb->ki_pos, 0);
  707. ret = generic_file_direct_write(iocb, iovp,
  708. &nr_segs, pos, &iocb->ki_pos, count, ocount);
  709. /* No fallback to buffered IO on errors for XFS. */
  710. ASSERT(ret < 0 || ret == count);
  711. return ret;
  712. }
  713. STATIC ssize_t
  714. xfs_file_buffered_aio_write(
  715. struct kiocb *iocb,
  716. const struct iovec *iovp,
  717. unsigned long nr_segs,
  718. loff_t pos,
  719. size_t ocount,
  720. int *iolock)
  721. {
  722. struct file *file = iocb->ki_filp;
  723. struct address_space *mapping = file->f_mapping;
  724. struct inode *inode = mapping->host;
  725. struct xfs_inode *ip = XFS_I(inode);
  726. ssize_t ret;
  727. int enospc = 0;
  728. size_t count = ocount;
  729. *iolock = XFS_IOLOCK_EXCL;
  730. xfs_rw_ilock(ip, *iolock);
  731. ret = xfs_file_aio_write_checks(file, &pos, &count, iolock);
  732. if (ret)
  733. return ret;
  734. /* We can write back this queue in page reclaim */
  735. current->backing_dev_info = mapping->backing_dev_info;
  736. write_retry:
  737. trace_xfs_file_buffered_write(ip, count, iocb->ki_pos, 0);
  738. ret = generic_file_buffered_write(iocb, iovp, nr_segs,
  739. pos, &iocb->ki_pos, count, ret);
  740. /*
  741. * if we just got an ENOSPC, flush the inode now we aren't holding any
  742. * page locks and retry *once*
  743. */
  744. if (ret == -ENOSPC && !enospc) {
  745. ret = -xfs_flush_pages(ip, 0, -1, 0, FI_NONE);
  746. if (ret)
  747. return ret;
  748. enospc = 1;
  749. goto write_retry;
  750. }
  751. current->backing_dev_info = NULL;
  752. return ret;
  753. }
  754. STATIC ssize_t
  755. xfs_file_aio_write(
  756. struct kiocb *iocb,
  757. const struct iovec *iovp,
  758. unsigned long nr_segs,
  759. loff_t pos)
  760. {
  761. struct file *file = iocb->ki_filp;
  762. struct address_space *mapping = file->f_mapping;
  763. struct inode *inode = mapping->host;
  764. struct xfs_inode *ip = XFS_I(inode);
  765. ssize_t ret;
  766. int iolock;
  767. size_t ocount = 0;
  768. XFS_STATS_INC(xs_write_calls);
  769. BUG_ON(iocb->ki_pos != pos);
  770. ret = generic_segment_checks(iovp, &nr_segs, &ocount, VERIFY_READ);
  771. if (ret)
  772. return ret;
  773. if (ocount == 0)
  774. return 0;
  775. xfs_wait_for_freeze(ip->i_mount, SB_FREEZE_WRITE);
  776. if (XFS_FORCED_SHUTDOWN(ip->i_mount))
  777. return -EIO;
  778. if (unlikely(file->f_flags & O_DIRECT))
  779. ret = xfs_file_dio_aio_write(iocb, iovp, nr_segs, pos,
  780. ocount, &iolock);
  781. else
  782. ret = xfs_file_buffered_aio_write(iocb, iovp, nr_segs, pos,
  783. ocount, &iolock);
  784. xfs_aio_write_isize_update(inode, &iocb->ki_pos, ret);
  785. if (ret <= 0)
  786. goto out_unlock;
  787. /* Handle various SYNC-type writes */
  788. if ((file->f_flags & O_DSYNC) || IS_SYNC(inode)) {
  789. loff_t end = pos + ret - 1;
  790. int error;
  791. xfs_rw_iunlock(ip, iolock);
  792. error = xfs_file_fsync(file, pos, end,
  793. (file->f_flags & __O_SYNC) ? 0 : 1);
  794. xfs_rw_ilock(ip, iolock);
  795. if (error)
  796. ret = error;
  797. }
  798. out_unlock:
  799. xfs_aio_write_newsize_update(ip);
  800. xfs_rw_iunlock(ip, iolock);
  801. return ret;
  802. }
  803. STATIC long
  804. xfs_file_fallocate(
  805. struct file *file,
  806. int mode,
  807. loff_t offset,
  808. loff_t len)
  809. {
  810. struct inode *inode = file->f_path.dentry->d_inode;
  811. long error;
  812. loff_t new_size = 0;
  813. xfs_flock64_t bf;
  814. xfs_inode_t *ip = XFS_I(inode);
  815. int cmd = XFS_IOC_RESVSP;
  816. int attr_flags = XFS_ATTR_NOLOCK;
  817. if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
  818. return -EOPNOTSUPP;
  819. bf.l_whence = 0;
  820. bf.l_start = offset;
  821. bf.l_len = len;
  822. xfs_ilock(ip, XFS_IOLOCK_EXCL);
  823. if (mode & FALLOC_FL_PUNCH_HOLE)
  824. cmd = XFS_IOC_UNRESVSP;
  825. /* check the new inode size is valid before allocating */
  826. if (!(mode & FALLOC_FL_KEEP_SIZE) &&
  827. offset + len > i_size_read(inode)) {
  828. new_size = offset + len;
  829. error = inode_newsize_ok(inode, new_size);
  830. if (error)
  831. goto out_unlock;
  832. }
  833. if (file->f_flags & O_DSYNC)
  834. attr_flags |= XFS_ATTR_SYNC;
  835. error = -xfs_change_file_space(ip, cmd, &bf, 0, attr_flags);
  836. if (error)
  837. goto out_unlock;
  838. /* Change file size if needed */
  839. if (new_size) {
  840. struct iattr iattr;
  841. iattr.ia_valid = ATTR_SIZE;
  842. iattr.ia_size = new_size;
  843. error = -xfs_setattr_size(ip, &iattr, XFS_ATTR_NOLOCK);
  844. }
  845. out_unlock:
  846. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  847. return error;
  848. }
  849. STATIC int
  850. xfs_file_open(
  851. struct inode *inode,
  852. struct file *file)
  853. {
  854. if (!(file->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS)
  855. return -EFBIG;
  856. if (XFS_FORCED_SHUTDOWN(XFS_M(inode->i_sb)))
  857. return -EIO;
  858. return 0;
  859. }
  860. STATIC int
  861. xfs_dir_open(
  862. struct inode *inode,
  863. struct file *file)
  864. {
  865. struct xfs_inode *ip = XFS_I(inode);
  866. int mode;
  867. int error;
  868. error = xfs_file_open(inode, file);
  869. if (error)
  870. return error;
  871. /*
  872. * If there are any blocks, read-ahead block 0 as we're almost
  873. * certain to have the next operation be a read there.
  874. */
  875. mode = xfs_ilock_map_shared(ip);
  876. if (ip->i_d.di_nextents > 0)
  877. xfs_da_reada_buf(NULL, ip, 0, XFS_DATA_FORK);
  878. xfs_iunlock(ip, mode);
  879. return 0;
  880. }
  881. STATIC int
  882. xfs_file_release(
  883. struct inode *inode,
  884. struct file *filp)
  885. {
  886. return -xfs_release(XFS_I(inode));
  887. }
  888. STATIC int
  889. xfs_file_readdir(
  890. struct file *filp,
  891. void *dirent,
  892. filldir_t filldir)
  893. {
  894. struct inode *inode = filp->f_path.dentry->d_inode;
  895. xfs_inode_t *ip = XFS_I(inode);
  896. int error;
  897. size_t bufsize;
  898. /*
  899. * The Linux API doesn't pass down the total size of the buffer
  900. * we read into down to the filesystem. With the filldir concept
  901. * it's not needed for correct information, but the XFS dir2 leaf
  902. * code wants an estimate of the buffer size to calculate it's
  903. * readahead window and size the buffers used for mapping to
  904. * physical blocks.
  905. *
  906. * Try to give it an estimate that's good enough, maybe at some
  907. * point we can change the ->readdir prototype to include the
  908. * buffer size. For now we use the current glibc buffer size.
  909. */
  910. bufsize = (size_t)min_t(loff_t, 32768, ip->i_d.di_size);
  911. error = xfs_readdir(ip, dirent, bufsize,
  912. (xfs_off_t *)&filp->f_pos, filldir);
  913. if (error)
  914. return -error;
  915. return 0;
  916. }
  917. STATIC int
  918. xfs_file_mmap(
  919. struct file *filp,
  920. struct vm_area_struct *vma)
  921. {
  922. vma->vm_ops = &xfs_file_vm_ops;
  923. vma->vm_flags |= VM_CAN_NONLINEAR;
  924. file_accessed(filp);
  925. return 0;
  926. }
  927. /*
  928. * mmap()d file has taken write protection fault and is being made
  929. * writable. We can set the page state up correctly for a writable
  930. * page, which means we can do correct delalloc accounting (ENOSPC
  931. * checking!) and unwritten extent mapping.
  932. */
  933. STATIC int
  934. xfs_vm_page_mkwrite(
  935. struct vm_area_struct *vma,
  936. struct vm_fault *vmf)
  937. {
  938. return block_page_mkwrite(vma, vmf, xfs_get_blocks);
  939. }
  940. const struct file_operations xfs_file_operations = {
  941. .llseek = generic_file_llseek,
  942. .read = do_sync_read,
  943. .write = do_sync_write,
  944. .aio_read = xfs_file_aio_read,
  945. .aio_write = xfs_file_aio_write,
  946. .splice_read = xfs_file_splice_read,
  947. .splice_write = xfs_file_splice_write,
  948. .unlocked_ioctl = xfs_file_ioctl,
  949. #ifdef CONFIG_COMPAT
  950. .compat_ioctl = xfs_file_compat_ioctl,
  951. #endif
  952. .mmap = xfs_file_mmap,
  953. .open = xfs_file_open,
  954. .release = xfs_file_release,
  955. .fsync = xfs_file_fsync,
  956. .fallocate = xfs_file_fallocate,
  957. };
  958. const struct file_operations xfs_dir_file_operations = {
  959. .open = xfs_dir_open,
  960. .read = generic_read_dir,
  961. .readdir = xfs_file_readdir,
  962. .llseek = generic_file_llseek,
  963. .unlocked_ioctl = xfs_file_ioctl,
  964. #ifdef CONFIG_COMPAT
  965. .compat_ioctl = xfs_file_compat_ioctl,
  966. #endif
  967. .fsync = xfs_file_fsync,
  968. };
  969. static const struct vm_operations_struct xfs_file_vm_ops = {
  970. .fault = filemap_fault,
  971. .page_mkwrite = xfs_vm_page_mkwrite,
  972. };