xfs_file.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940
  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. static const struct vm_operations_struct xfs_file_vm_ops;
  40. /*
  41. * xfs_iozero
  42. *
  43. * xfs_iozero clears the specified range of buffer supplied,
  44. * and marks all the affected blocks as valid and modified. If
  45. * an affected block is not allocated, it will be allocated. If
  46. * an affected block is not completely overwritten, and is not
  47. * valid before the operation, it will be read from disk before
  48. * being partially zeroed.
  49. */
  50. STATIC int
  51. xfs_iozero(
  52. struct xfs_inode *ip, /* inode */
  53. loff_t pos, /* offset in file */
  54. size_t count) /* size of data to zero */
  55. {
  56. struct page *page;
  57. struct address_space *mapping;
  58. int status;
  59. mapping = VFS_I(ip)->i_mapping;
  60. do {
  61. unsigned offset, bytes;
  62. void *fsdata;
  63. offset = (pos & (PAGE_CACHE_SIZE -1)); /* Within page */
  64. bytes = PAGE_CACHE_SIZE - offset;
  65. if (bytes > count)
  66. bytes = count;
  67. status = pagecache_write_begin(NULL, mapping, pos, bytes,
  68. AOP_FLAG_UNINTERRUPTIBLE,
  69. &page, &fsdata);
  70. if (status)
  71. break;
  72. zero_user(page, offset, bytes);
  73. status = pagecache_write_end(NULL, mapping, pos, bytes, bytes,
  74. page, fsdata);
  75. WARN_ON(status <= 0); /* can't return less than zero! */
  76. pos += bytes;
  77. count -= bytes;
  78. status = 0;
  79. } while (count);
  80. return (-status);
  81. }
  82. STATIC int
  83. xfs_file_fsync(
  84. struct file *file,
  85. int datasync)
  86. {
  87. struct inode *inode = file->f_mapping->host;
  88. struct xfs_inode *ip = XFS_I(inode);
  89. struct xfs_trans *tp;
  90. int error = 0;
  91. int log_flushed = 0;
  92. trace_xfs_file_fsync(ip);
  93. if (XFS_FORCED_SHUTDOWN(ip->i_mount))
  94. return -XFS_ERROR(EIO);
  95. xfs_iflags_clear(ip, XFS_ITRUNCATED);
  96. xfs_ioend_wait(ip);
  97. /*
  98. * We always need to make sure that the required inode state is safe on
  99. * disk. The inode might be clean but we still might need to force the
  100. * log because of committed transactions that haven't hit the disk yet.
  101. * Likewise, there could be unflushed non-transactional changes to the
  102. * inode core that have to go to disk and this requires us to issue
  103. * a synchronous transaction to capture these changes correctly.
  104. *
  105. * This code relies on the assumption that if the i_update_core field
  106. * of the inode is clear and the inode is unpinned then it is clean
  107. * and no action is required.
  108. */
  109. xfs_ilock(ip, XFS_ILOCK_SHARED);
  110. /*
  111. * First check if the VFS inode is marked dirty. All the dirtying
  112. * of non-transactional updates no goes through mark_inode_dirty*,
  113. * which allows us to distinguish beteeen pure timestamp updates
  114. * and i_size updates which need to be caught for fdatasync.
  115. * After that also theck for the dirty state in the XFS inode, which
  116. * might gets cleared when the inode gets written out via the AIL
  117. * or xfs_iflush_cluster.
  118. */
  119. if (((inode->i_state & I_DIRTY_DATASYNC) ||
  120. ((inode->i_state & I_DIRTY_SYNC) && !datasync)) &&
  121. ip->i_update_core) {
  122. /*
  123. * Kick off a transaction to log the inode core to get the
  124. * updates. The sync transaction will also force the log.
  125. */
  126. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  127. tp = xfs_trans_alloc(ip->i_mount, XFS_TRANS_FSYNC_TS);
  128. error = xfs_trans_reserve(tp, 0,
  129. XFS_FSYNC_TS_LOG_RES(ip->i_mount), 0, 0, 0);
  130. if (error) {
  131. xfs_trans_cancel(tp, 0);
  132. return -error;
  133. }
  134. xfs_ilock(ip, XFS_ILOCK_EXCL);
  135. /*
  136. * Note - it's possible that we might have pushed ourselves out
  137. * of the way during trans_reserve which would flush the inode.
  138. * But there's no guarantee that the inode buffer has actually
  139. * gone out yet (it's delwri). Plus the buffer could be pinned
  140. * anyway if it's part of an inode in another recent
  141. * transaction. So we play it safe and fire off the
  142. * transaction anyway.
  143. */
  144. xfs_trans_ijoin(tp, ip);
  145. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  146. xfs_trans_set_sync(tp);
  147. error = _xfs_trans_commit(tp, 0, &log_flushed);
  148. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  149. } else {
  150. /*
  151. * Timestamps/size haven't changed since last inode flush or
  152. * inode transaction commit. That means either nothing got
  153. * written or a transaction committed which caught the updates.
  154. * If the latter happened and the transaction hasn't hit the
  155. * disk yet, the inode will be still be pinned. If it is,
  156. * force the log.
  157. */
  158. if (xfs_ipincount(ip)) {
  159. error = _xfs_log_force_lsn(ip->i_mount,
  160. ip->i_itemp->ili_last_lsn,
  161. XFS_LOG_SYNC, &log_flushed);
  162. }
  163. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  164. }
  165. if (ip->i_mount->m_flags & XFS_MOUNT_BARRIER) {
  166. /*
  167. * If the log write didn't issue an ordered tag we need
  168. * to flush the disk cache for the data device now.
  169. */
  170. if (!log_flushed)
  171. xfs_blkdev_issue_flush(ip->i_mount->m_ddev_targp);
  172. /*
  173. * If this inode is on the RT dev we need to flush that
  174. * cache as well.
  175. */
  176. if (XFS_IS_REALTIME_INODE(ip))
  177. xfs_blkdev_issue_flush(ip->i_mount->m_rtdev_targp);
  178. }
  179. return -error;
  180. }
  181. STATIC ssize_t
  182. xfs_file_aio_read(
  183. struct kiocb *iocb,
  184. const struct iovec *iovp,
  185. unsigned long nr_segs,
  186. loff_t pos)
  187. {
  188. struct file *file = iocb->ki_filp;
  189. struct inode *inode = file->f_mapping->host;
  190. struct xfs_inode *ip = XFS_I(inode);
  191. struct xfs_mount *mp = ip->i_mount;
  192. size_t size = 0;
  193. ssize_t ret = 0;
  194. int ioflags = 0;
  195. xfs_fsize_t n;
  196. unsigned long seg;
  197. XFS_STATS_INC(xs_read_calls);
  198. BUG_ON(iocb->ki_pos != pos);
  199. if (unlikely(file->f_flags & O_DIRECT))
  200. ioflags |= IO_ISDIRECT;
  201. if (file->f_mode & FMODE_NOCMTIME)
  202. ioflags |= IO_INVIS;
  203. /* START copy & waste from filemap.c */
  204. for (seg = 0; seg < nr_segs; seg++) {
  205. const struct iovec *iv = &iovp[seg];
  206. /*
  207. * If any segment has a negative length, or the cumulative
  208. * length ever wraps negative then return -EINVAL.
  209. */
  210. size += iv->iov_len;
  211. if (unlikely((ssize_t)(size|iv->iov_len) < 0))
  212. return XFS_ERROR(-EINVAL);
  213. }
  214. /* END copy & waste from filemap.c */
  215. if (unlikely(ioflags & IO_ISDIRECT)) {
  216. xfs_buftarg_t *target =
  217. XFS_IS_REALTIME_INODE(ip) ?
  218. mp->m_rtdev_targp : mp->m_ddev_targp;
  219. if ((iocb->ki_pos & target->bt_smask) ||
  220. (size & target->bt_smask)) {
  221. if (iocb->ki_pos == ip->i_size)
  222. return 0;
  223. return -XFS_ERROR(EINVAL);
  224. }
  225. }
  226. n = XFS_MAXIOFFSET(mp) - iocb->ki_pos;
  227. if (n <= 0 || size == 0)
  228. return 0;
  229. if (n < size)
  230. size = n;
  231. if (XFS_FORCED_SHUTDOWN(mp))
  232. return -EIO;
  233. if (unlikely(ioflags & IO_ISDIRECT))
  234. mutex_lock(&inode->i_mutex);
  235. xfs_ilock(ip, XFS_IOLOCK_SHARED);
  236. if (unlikely(ioflags & IO_ISDIRECT)) {
  237. if (inode->i_mapping->nrpages) {
  238. ret = -xfs_flushinval_pages(ip,
  239. (iocb->ki_pos & PAGE_CACHE_MASK),
  240. -1, FI_REMAPF_LOCKED);
  241. }
  242. mutex_unlock(&inode->i_mutex);
  243. if (ret) {
  244. xfs_iunlock(ip, XFS_IOLOCK_SHARED);
  245. return ret;
  246. }
  247. }
  248. trace_xfs_file_read(ip, size, iocb->ki_pos, ioflags);
  249. ret = generic_file_aio_read(iocb, iovp, nr_segs, iocb->ki_pos);
  250. if (ret > 0)
  251. XFS_STATS_ADD(xs_read_bytes, ret);
  252. xfs_iunlock(ip, XFS_IOLOCK_SHARED);
  253. return ret;
  254. }
  255. STATIC ssize_t
  256. xfs_file_splice_read(
  257. struct file *infilp,
  258. loff_t *ppos,
  259. struct pipe_inode_info *pipe,
  260. size_t count,
  261. unsigned int flags)
  262. {
  263. struct xfs_inode *ip = XFS_I(infilp->f_mapping->host);
  264. int ioflags = 0;
  265. ssize_t ret;
  266. XFS_STATS_INC(xs_read_calls);
  267. if (infilp->f_mode & FMODE_NOCMTIME)
  268. ioflags |= IO_INVIS;
  269. if (XFS_FORCED_SHUTDOWN(ip->i_mount))
  270. return -EIO;
  271. xfs_ilock(ip, XFS_IOLOCK_SHARED);
  272. trace_xfs_file_splice_read(ip, count, *ppos, ioflags);
  273. ret = generic_file_splice_read(infilp, ppos, pipe, count, flags);
  274. if (ret > 0)
  275. XFS_STATS_ADD(xs_read_bytes, ret);
  276. xfs_iunlock(ip, XFS_IOLOCK_SHARED);
  277. return ret;
  278. }
  279. STATIC void
  280. xfs_aio_write_isize_update(
  281. struct inode *inode,
  282. loff_t *ppos,
  283. ssize_t bytes_written)
  284. {
  285. struct xfs_inode *ip = XFS_I(inode);
  286. xfs_fsize_t isize = i_size_read(inode);
  287. if (bytes_written > 0)
  288. XFS_STATS_ADD(xs_write_bytes, bytes_written);
  289. if (unlikely(bytes_written < 0 && bytes_written != -EFAULT &&
  290. *ppos > isize))
  291. *ppos = isize;
  292. if (*ppos > ip->i_size) {
  293. xfs_ilock(ip, XFS_ILOCK_EXCL);
  294. if (*ppos > ip->i_size)
  295. ip->i_size = *ppos;
  296. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  297. }
  298. }
  299. STATIC ssize_t
  300. xfs_file_splice_write(
  301. struct pipe_inode_info *pipe,
  302. struct file *outfilp,
  303. loff_t *ppos,
  304. size_t count,
  305. unsigned int flags)
  306. {
  307. struct inode *inode = outfilp->f_mapping->host;
  308. struct xfs_inode *ip = XFS_I(inode);
  309. xfs_fsize_t new_size;
  310. int ioflags = 0;
  311. ssize_t ret;
  312. XFS_STATS_INC(xs_write_calls);
  313. if (outfilp->f_mode & FMODE_NOCMTIME)
  314. ioflags |= IO_INVIS;
  315. if (XFS_FORCED_SHUTDOWN(ip->i_mount))
  316. return -EIO;
  317. xfs_ilock(ip, XFS_IOLOCK_EXCL);
  318. new_size = *ppos + count;
  319. xfs_ilock(ip, XFS_ILOCK_EXCL);
  320. if (new_size > ip->i_size)
  321. ip->i_new_size = new_size;
  322. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  323. trace_xfs_file_splice_write(ip, count, *ppos, ioflags);
  324. ret = generic_file_splice_write(pipe, outfilp, ppos, count, flags);
  325. xfs_aio_write_isize_update(inode, ppos, ret);
  326. if (ip->i_new_size) {
  327. xfs_ilock(ip, XFS_ILOCK_EXCL);
  328. ip->i_new_size = 0;
  329. if (ip->i_d.di_size > ip->i_size)
  330. ip->i_d.di_size = ip->i_size;
  331. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  332. }
  333. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  334. return ret;
  335. }
  336. /*
  337. * This routine is called to handle zeroing any space in the last
  338. * block of the file that is beyond the EOF. We do this since the
  339. * size is being increased without writing anything to that block
  340. * and we don't want anyone to read the garbage on the disk.
  341. */
  342. STATIC int /* error (positive) */
  343. xfs_zero_last_block(
  344. xfs_inode_t *ip,
  345. xfs_fsize_t offset,
  346. xfs_fsize_t isize)
  347. {
  348. xfs_fileoff_t last_fsb;
  349. xfs_mount_t *mp = ip->i_mount;
  350. int nimaps;
  351. int zero_offset;
  352. int zero_len;
  353. int error = 0;
  354. xfs_bmbt_irec_t imap;
  355. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
  356. zero_offset = XFS_B_FSB_OFFSET(mp, isize);
  357. if (zero_offset == 0) {
  358. /*
  359. * There are no extra bytes in the last block on disk to
  360. * zero, so return.
  361. */
  362. return 0;
  363. }
  364. last_fsb = XFS_B_TO_FSBT(mp, isize);
  365. nimaps = 1;
  366. error = xfs_bmapi(NULL, ip, last_fsb, 1, 0, NULL, 0, &imap,
  367. &nimaps, NULL);
  368. if (error) {
  369. return error;
  370. }
  371. ASSERT(nimaps > 0);
  372. /*
  373. * If the block underlying isize is just a hole, then there
  374. * is nothing to zero.
  375. */
  376. if (imap.br_startblock == HOLESTARTBLOCK) {
  377. return 0;
  378. }
  379. /*
  380. * Zero the part of the last block beyond the EOF, and write it
  381. * out sync. We need to drop the ilock while we do this so we
  382. * don't deadlock when the buffer cache calls back to us.
  383. */
  384. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  385. zero_len = mp->m_sb.sb_blocksize - zero_offset;
  386. if (isize + zero_len > offset)
  387. zero_len = offset - isize;
  388. error = xfs_iozero(ip, isize, zero_len);
  389. xfs_ilock(ip, XFS_ILOCK_EXCL);
  390. ASSERT(error >= 0);
  391. return error;
  392. }
  393. /*
  394. * Zero any on disk space between the current EOF and the new,
  395. * larger EOF. This handles the normal case of zeroing the remainder
  396. * of the last block in the file and the unusual case of zeroing blocks
  397. * out beyond the size of the file. This second case only happens
  398. * with fixed size extents and when the system crashes before the inode
  399. * size was updated but after blocks were allocated. If fill is set,
  400. * then any holes in the range are filled and zeroed. If not, the holes
  401. * are left alone as holes.
  402. */
  403. int /* error (positive) */
  404. xfs_zero_eof(
  405. xfs_inode_t *ip,
  406. xfs_off_t offset, /* starting I/O offset */
  407. xfs_fsize_t isize) /* current inode size */
  408. {
  409. xfs_mount_t *mp = ip->i_mount;
  410. xfs_fileoff_t start_zero_fsb;
  411. xfs_fileoff_t end_zero_fsb;
  412. xfs_fileoff_t zero_count_fsb;
  413. xfs_fileoff_t last_fsb;
  414. xfs_fileoff_t zero_off;
  415. xfs_fsize_t zero_len;
  416. int nimaps;
  417. int error = 0;
  418. xfs_bmbt_irec_t imap;
  419. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_IOLOCK_EXCL));
  420. ASSERT(offset > isize);
  421. /*
  422. * First handle zeroing the block on which isize resides.
  423. * We only zero a part of that block so it is handled specially.
  424. */
  425. error = xfs_zero_last_block(ip, offset, isize);
  426. if (error) {
  427. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_IOLOCK_EXCL));
  428. return error;
  429. }
  430. /*
  431. * Calculate the range between the new size and the old
  432. * where blocks needing to be zeroed may exist. To get the
  433. * block where the last byte in the file currently resides,
  434. * we need to subtract one from the size and truncate back
  435. * to a block boundary. We subtract 1 in case the size is
  436. * exactly on a block boundary.
  437. */
  438. last_fsb = isize ? XFS_B_TO_FSBT(mp, isize - 1) : (xfs_fileoff_t)-1;
  439. start_zero_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)isize);
  440. end_zero_fsb = XFS_B_TO_FSBT(mp, offset - 1);
  441. ASSERT((xfs_sfiloff_t)last_fsb < (xfs_sfiloff_t)start_zero_fsb);
  442. if (last_fsb == end_zero_fsb) {
  443. /*
  444. * The size was only incremented on its last block.
  445. * We took care of that above, so just return.
  446. */
  447. return 0;
  448. }
  449. ASSERT(start_zero_fsb <= end_zero_fsb);
  450. while (start_zero_fsb <= end_zero_fsb) {
  451. nimaps = 1;
  452. zero_count_fsb = end_zero_fsb - start_zero_fsb + 1;
  453. error = xfs_bmapi(NULL, ip, start_zero_fsb, zero_count_fsb,
  454. 0, NULL, 0, &imap, &nimaps, NULL);
  455. if (error) {
  456. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_IOLOCK_EXCL));
  457. return error;
  458. }
  459. ASSERT(nimaps > 0);
  460. if (imap.br_state == XFS_EXT_UNWRITTEN ||
  461. imap.br_startblock == HOLESTARTBLOCK) {
  462. /*
  463. * This loop handles initializing pages that were
  464. * partially initialized by the code below this
  465. * loop. It basically zeroes the part of the page
  466. * that sits on a hole and sets the page as P_HOLE
  467. * and calls remapf if it is a mapped file.
  468. */
  469. start_zero_fsb = imap.br_startoff + imap.br_blockcount;
  470. ASSERT(start_zero_fsb <= (end_zero_fsb + 1));
  471. continue;
  472. }
  473. /*
  474. * There are blocks we need to zero.
  475. * Drop the inode lock while we're doing the I/O.
  476. * We'll still have the iolock to protect us.
  477. */
  478. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  479. zero_off = XFS_FSB_TO_B(mp, start_zero_fsb);
  480. zero_len = XFS_FSB_TO_B(mp, imap.br_blockcount);
  481. if ((zero_off + zero_len) > offset)
  482. zero_len = offset - zero_off;
  483. error = xfs_iozero(ip, zero_off, zero_len);
  484. if (error) {
  485. goto out_lock;
  486. }
  487. start_zero_fsb = imap.br_startoff + imap.br_blockcount;
  488. ASSERT(start_zero_fsb <= (end_zero_fsb + 1));
  489. xfs_ilock(ip, XFS_ILOCK_EXCL);
  490. }
  491. return 0;
  492. out_lock:
  493. xfs_ilock(ip, XFS_ILOCK_EXCL);
  494. ASSERT(error >= 0);
  495. return error;
  496. }
  497. STATIC ssize_t
  498. xfs_file_aio_write(
  499. struct kiocb *iocb,
  500. const struct iovec *iovp,
  501. unsigned long nr_segs,
  502. loff_t pos)
  503. {
  504. struct file *file = iocb->ki_filp;
  505. struct address_space *mapping = file->f_mapping;
  506. struct inode *inode = mapping->host;
  507. struct xfs_inode *ip = XFS_I(inode);
  508. struct xfs_mount *mp = ip->i_mount;
  509. ssize_t ret = 0;
  510. int ioflags = 0;
  511. xfs_fsize_t new_size;
  512. int iolock;
  513. size_t ocount = 0, count;
  514. int need_i_mutex;
  515. XFS_STATS_INC(xs_write_calls);
  516. BUG_ON(iocb->ki_pos != pos);
  517. if (unlikely(file->f_flags & O_DIRECT))
  518. ioflags |= IO_ISDIRECT;
  519. if (file->f_mode & FMODE_NOCMTIME)
  520. ioflags |= IO_INVIS;
  521. ret = generic_segment_checks(iovp, &nr_segs, &ocount, VERIFY_READ);
  522. if (ret)
  523. return ret;
  524. count = ocount;
  525. if (count == 0)
  526. return 0;
  527. xfs_wait_for_freeze(mp, SB_FREEZE_WRITE);
  528. if (XFS_FORCED_SHUTDOWN(mp))
  529. return -EIO;
  530. relock:
  531. if (ioflags & IO_ISDIRECT) {
  532. iolock = XFS_IOLOCK_SHARED;
  533. need_i_mutex = 0;
  534. } else {
  535. iolock = XFS_IOLOCK_EXCL;
  536. need_i_mutex = 1;
  537. mutex_lock(&inode->i_mutex);
  538. }
  539. xfs_ilock(ip, XFS_ILOCK_EXCL|iolock);
  540. start:
  541. ret = generic_write_checks(file, &pos, &count,
  542. S_ISBLK(inode->i_mode));
  543. if (ret) {
  544. xfs_iunlock(ip, XFS_ILOCK_EXCL|iolock);
  545. goto out_unlock_mutex;
  546. }
  547. if (ioflags & IO_ISDIRECT) {
  548. xfs_buftarg_t *target =
  549. XFS_IS_REALTIME_INODE(ip) ?
  550. mp->m_rtdev_targp : mp->m_ddev_targp;
  551. if ((pos & target->bt_smask) || (count & target->bt_smask)) {
  552. xfs_iunlock(ip, XFS_ILOCK_EXCL|iolock);
  553. return XFS_ERROR(-EINVAL);
  554. }
  555. if (!need_i_mutex && (mapping->nrpages || pos > ip->i_size)) {
  556. xfs_iunlock(ip, XFS_ILOCK_EXCL|iolock);
  557. iolock = XFS_IOLOCK_EXCL;
  558. need_i_mutex = 1;
  559. mutex_lock(&inode->i_mutex);
  560. xfs_ilock(ip, XFS_ILOCK_EXCL|iolock);
  561. goto start;
  562. }
  563. }
  564. new_size = pos + count;
  565. if (new_size > ip->i_size)
  566. ip->i_new_size = new_size;
  567. if (likely(!(ioflags & IO_INVIS)))
  568. file_update_time(file);
  569. /*
  570. * If the offset is beyond the size of the file, we have a couple
  571. * of things to do. First, if there is already space allocated
  572. * we need to either create holes or zero the disk or ...
  573. *
  574. * If there is a page where the previous size lands, we need
  575. * to zero it out up to the new size.
  576. */
  577. if (pos > ip->i_size) {
  578. ret = -xfs_zero_eof(ip, pos, ip->i_size);
  579. if (ret) {
  580. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  581. goto out_unlock_internal;
  582. }
  583. }
  584. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  585. /*
  586. * If we're writing the file then make sure to clear the
  587. * setuid and setgid bits if the process is not being run
  588. * by root. This keeps people from modifying setuid and
  589. * setgid binaries.
  590. */
  591. ret = file_remove_suid(file);
  592. if (unlikely(ret))
  593. goto out_unlock_internal;
  594. /* We can write back this queue in page reclaim */
  595. current->backing_dev_info = mapping->backing_dev_info;
  596. if ((ioflags & IO_ISDIRECT)) {
  597. if (mapping->nrpages) {
  598. WARN_ON(need_i_mutex == 0);
  599. ret = -xfs_flushinval_pages(ip,
  600. (pos & PAGE_CACHE_MASK),
  601. -1, FI_REMAPF_LOCKED);
  602. if (ret)
  603. goto out_unlock_internal;
  604. }
  605. if (need_i_mutex) {
  606. /* demote the lock now the cached pages are gone */
  607. xfs_ilock_demote(ip, XFS_IOLOCK_EXCL);
  608. mutex_unlock(&inode->i_mutex);
  609. iolock = XFS_IOLOCK_SHARED;
  610. need_i_mutex = 0;
  611. }
  612. trace_xfs_file_direct_write(ip, count, iocb->ki_pos, ioflags);
  613. ret = generic_file_direct_write(iocb, iovp,
  614. &nr_segs, pos, &iocb->ki_pos, count, ocount);
  615. /*
  616. * direct-io write to a hole: fall through to buffered I/O
  617. * for completing the rest of the request.
  618. */
  619. if (ret >= 0 && ret != count) {
  620. XFS_STATS_ADD(xs_write_bytes, ret);
  621. pos += ret;
  622. count -= ret;
  623. ioflags &= ~IO_ISDIRECT;
  624. xfs_iunlock(ip, iolock);
  625. goto relock;
  626. }
  627. } else {
  628. int enospc = 0;
  629. write_retry:
  630. trace_xfs_file_buffered_write(ip, count, iocb->ki_pos, ioflags);
  631. ret = generic_file_buffered_write(iocb, iovp, nr_segs,
  632. pos, &iocb->ki_pos, count, ret);
  633. /*
  634. * if we just got an ENOSPC, flush the inode now we
  635. * aren't holding any page locks and retry *once*
  636. */
  637. if (ret == -ENOSPC && !enospc) {
  638. ret = xfs_flush_pages(ip, 0, -1, 0, FI_NONE);
  639. if (ret)
  640. goto out_unlock_internal;
  641. enospc = 1;
  642. goto write_retry;
  643. }
  644. }
  645. current->backing_dev_info = NULL;
  646. xfs_aio_write_isize_update(inode, &iocb->ki_pos, ret);
  647. if (ret <= 0)
  648. goto out_unlock_internal;
  649. /* Handle various SYNC-type writes */
  650. if ((file->f_flags & O_DSYNC) || IS_SYNC(inode)) {
  651. loff_t end = pos + ret - 1;
  652. int error, error2;
  653. xfs_iunlock(ip, iolock);
  654. if (need_i_mutex)
  655. mutex_unlock(&inode->i_mutex);
  656. error = filemap_write_and_wait_range(mapping, pos, end);
  657. if (need_i_mutex)
  658. mutex_lock(&inode->i_mutex);
  659. xfs_ilock(ip, iolock);
  660. error2 = -xfs_file_fsync(file,
  661. (file->f_flags & __O_SYNC) ? 0 : 1);
  662. if (error)
  663. ret = error;
  664. else if (error2)
  665. ret = error2;
  666. }
  667. out_unlock_internal:
  668. if (ip->i_new_size) {
  669. xfs_ilock(ip, XFS_ILOCK_EXCL);
  670. ip->i_new_size = 0;
  671. /*
  672. * If this was a direct or synchronous I/O that failed (such
  673. * as ENOSPC) then part of the I/O may have been written to
  674. * disk before the error occured. In this case the on-disk
  675. * file size may have been adjusted beyond the in-memory file
  676. * size and now needs to be truncated back.
  677. */
  678. if (ip->i_d.di_size > ip->i_size)
  679. ip->i_d.di_size = ip->i_size;
  680. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  681. }
  682. xfs_iunlock(ip, iolock);
  683. out_unlock_mutex:
  684. if (need_i_mutex)
  685. mutex_unlock(&inode->i_mutex);
  686. return ret;
  687. }
  688. STATIC int
  689. xfs_file_open(
  690. struct inode *inode,
  691. struct file *file)
  692. {
  693. if (!(file->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS)
  694. return -EFBIG;
  695. if (XFS_FORCED_SHUTDOWN(XFS_M(inode->i_sb)))
  696. return -EIO;
  697. return 0;
  698. }
  699. STATIC int
  700. xfs_dir_open(
  701. struct inode *inode,
  702. struct file *file)
  703. {
  704. struct xfs_inode *ip = XFS_I(inode);
  705. int mode;
  706. int error;
  707. error = xfs_file_open(inode, file);
  708. if (error)
  709. return error;
  710. /*
  711. * If there are any blocks, read-ahead block 0 as we're almost
  712. * certain to have the next operation be a read there.
  713. */
  714. mode = xfs_ilock_map_shared(ip);
  715. if (ip->i_d.di_nextents > 0)
  716. xfs_da_reada_buf(NULL, ip, 0, XFS_DATA_FORK);
  717. xfs_iunlock(ip, mode);
  718. return 0;
  719. }
  720. STATIC int
  721. xfs_file_release(
  722. struct inode *inode,
  723. struct file *filp)
  724. {
  725. return -xfs_release(XFS_I(inode));
  726. }
  727. STATIC int
  728. xfs_file_readdir(
  729. struct file *filp,
  730. void *dirent,
  731. filldir_t filldir)
  732. {
  733. struct inode *inode = filp->f_path.dentry->d_inode;
  734. xfs_inode_t *ip = XFS_I(inode);
  735. int error;
  736. size_t bufsize;
  737. /*
  738. * The Linux API doesn't pass down the total size of the buffer
  739. * we read into down to the filesystem. With the filldir concept
  740. * it's not needed for correct information, but the XFS dir2 leaf
  741. * code wants an estimate of the buffer size to calculate it's
  742. * readahead window and size the buffers used for mapping to
  743. * physical blocks.
  744. *
  745. * Try to give it an estimate that's good enough, maybe at some
  746. * point we can change the ->readdir prototype to include the
  747. * buffer size. For now we use the current glibc buffer size.
  748. */
  749. bufsize = (size_t)min_t(loff_t, 32768, ip->i_d.di_size);
  750. error = xfs_readdir(ip, dirent, bufsize,
  751. (xfs_off_t *)&filp->f_pos, filldir);
  752. if (error)
  753. return -error;
  754. return 0;
  755. }
  756. STATIC int
  757. xfs_file_mmap(
  758. struct file *filp,
  759. struct vm_area_struct *vma)
  760. {
  761. vma->vm_ops = &xfs_file_vm_ops;
  762. vma->vm_flags |= VM_CAN_NONLINEAR;
  763. file_accessed(filp);
  764. return 0;
  765. }
  766. /*
  767. * mmap()d file has taken write protection fault and is being made
  768. * writable. We can set the page state up correctly for a writable
  769. * page, which means we can do correct delalloc accounting (ENOSPC
  770. * checking!) and unwritten extent mapping.
  771. */
  772. STATIC int
  773. xfs_vm_page_mkwrite(
  774. struct vm_area_struct *vma,
  775. struct vm_fault *vmf)
  776. {
  777. return block_page_mkwrite(vma, vmf, xfs_get_blocks);
  778. }
  779. const struct file_operations xfs_file_operations = {
  780. .llseek = generic_file_llseek,
  781. .read = do_sync_read,
  782. .write = do_sync_write,
  783. .aio_read = xfs_file_aio_read,
  784. .aio_write = xfs_file_aio_write,
  785. .splice_read = xfs_file_splice_read,
  786. .splice_write = xfs_file_splice_write,
  787. .unlocked_ioctl = xfs_file_ioctl,
  788. #ifdef CONFIG_COMPAT
  789. .compat_ioctl = xfs_file_compat_ioctl,
  790. #endif
  791. .mmap = xfs_file_mmap,
  792. .open = xfs_file_open,
  793. .release = xfs_file_release,
  794. .fsync = xfs_file_fsync,
  795. };
  796. const struct file_operations xfs_dir_file_operations = {
  797. .open = xfs_dir_open,
  798. .read = generic_read_dir,
  799. .readdir = xfs_file_readdir,
  800. .llseek = generic_file_llseek,
  801. .unlocked_ioctl = xfs_file_ioctl,
  802. #ifdef CONFIG_COMPAT
  803. .compat_ioctl = xfs_file_compat_ioctl,
  804. #endif
  805. .fsync = xfs_file_fsync,
  806. };
  807. static const struct vm_operations_struct xfs_file_vm_ops = {
  808. .fault = filemap_fault,
  809. .page_mkwrite = xfs_vm_page_mkwrite,
  810. };