xfs_file.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939
  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. /*
  300. * If this was a direct or synchronous I/O that failed (such as ENOSPC) then
  301. * part of the I/O may have been written to disk before the error occured. In
  302. * this case the on-disk file size may have been adjusted beyond the in-memory
  303. * file size and now needs to be truncated back.
  304. */
  305. STATIC void
  306. xfs_aio_write_newsize_update(
  307. struct xfs_inode *ip)
  308. {
  309. if (ip->i_new_size) {
  310. xfs_ilock(ip, XFS_ILOCK_EXCL);
  311. ip->i_new_size = 0;
  312. if (ip->i_d.di_size > ip->i_size)
  313. ip->i_d.di_size = ip->i_size;
  314. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  315. }
  316. }
  317. STATIC ssize_t
  318. xfs_file_splice_write(
  319. struct pipe_inode_info *pipe,
  320. struct file *outfilp,
  321. loff_t *ppos,
  322. size_t count,
  323. unsigned int flags)
  324. {
  325. struct inode *inode = outfilp->f_mapping->host;
  326. struct xfs_inode *ip = XFS_I(inode);
  327. xfs_fsize_t new_size;
  328. int ioflags = 0;
  329. ssize_t ret;
  330. XFS_STATS_INC(xs_write_calls);
  331. if (outfilp->f_mode & FMODE_NOCMTIME)
  332. ioflags |= IO_INVIS;
  333. if (XFS_FORCED_SHUTDOWN(ip->i_mount))
  334. return -EIO;
  335. xfs_ilock(ip, XFS_IOLOCK_EXCL);
  336. new_size = *ppos + count;
  337. xfs_ilock(ip, XFS_ILOCK_EXCL);
  338. if (new_size > ip->i_size)
  339. ip->i_new_size = new_size;
  340. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  341. trace_xfs_file_splice_write(ip, count, *ppos, ioflags);
  342. ret = generic_file_splice_write(pipe, outfilp, ppos, count, flags);
  343. xfs_aio_write_isize_update(inode, ppos, ret);
  344. xfs_aio_write_newsize_update(ip);
  345. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  346. return ret;
  347. }
  348. /*
  349. * This routine is called to handle zeroing any space in the last
  350. * block of the file that is beyond the EOF. We do this since the
  351. * size is being increased without writing anything to that block
  352. * and we don't want anyone to read the garbage on the disk.
  353. */
  354. STATIC int /* error (positive) */
  355. xfs_zero_last_block(
  356. xfs_inode_t *ip,
  357. xfs_fsize_t offset,
  358. xfs_fsize_t isize)
  359. {
  360. xfs_fileoff_t last_fsb;
  361. xfs_mount_t *mp = ip->i_mount;
  362. int nimaps;
  363. int zero_offset;
  364. int zero_len;
  365. int error = 0;
  366. xfs_bmbt_irec_t imap;
  367. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
  368. zero_offset = XFS_B_FSB_OFFSET(mp, isize);
  369. if (zero_offset == 0) {
  370. /*
  371. * There are no extra bytes in the last block on disk to
  372. * zero, so return.
  373. */
  374. return 0;
  375. }
  376. last_fsb = XFS_B_TO_FSBT(mp, isize);
  377. nimaps = 1;
  378. error = xfs_bmapi(NULL, ip, last_fsb, 1, 0, NULL, 0, &imap,
  379. &nimaps, NULL);
  380. if (error) {
  381. return error;
  382. }
  383. ASSERT(nimaps > 0);
  384. /*
  385. * If the block underlying isize is just a hole, then there
  386. * is nothing to zero.
  387. */
  388. if (imap.br_startblock == HOLESTARTBLOCK) {
  389. return 0;
  390. }
  391. /*
  392. * Zero the part of the last block beyond the EOF, and write it
  393. * out sync. We need to drop the ilock while we do this so we
  394. * don't deadlock when the buffer cache calls back to us.
  395. */
  396. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  397. zero_len = mp->m_sb.sb_blocksize - zero_offset;
  398. if (isize + zero_len > offset)
  399. zero_len = offset - isize;
  400. error = xfs_iozero(ip, isize, zero_len);
  401. xfs_ilock(ip, XFS_ILOCK_EXCL);
  402. ASSERT(error >= 0);
  403. return error;
  404. }
  405. /*
  406. * Zero any on disk space between the current EOF and the new,
  407. * larger EOF. This handles the normal case of zeroing the remainder
  408. * of the last block in the file and the unusual case of zeroing blocks
  409. * out beyond the size of the file. This second case only happens
  410. * with fixed size extents and when the system crashes before the inode
  411. * size was updated but after blocks were allocated. If fill is set,
  412. * then any holes in the range are filled and zeroed. If not, the holes
  413. * are left alone as holes.
  414. */
  415. int /* error (positive) */
  416. xfs_zero_eof(
  417. xfs_inode_t *ip,
  418. xfs_off_t offset, /* starting I/O offset */
  419. xfs_fsize_t isize) /* current inode size */
  420. {
  421. xfs_mount_t *mp = ip->i_mount;
  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. int nimaps;
  429. int error = 0;
  430. xfs_bmbt_irec_t imap;
  431. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_IOLOCK_EXCL));
  432. ASSERT(offset > isize);
  433. /*
  434. * First handle zeroing the block on which isize resides.
  435. * We only zero a part of that block so it is handled specially.
  436. */
  437. error = xfs_zero_last_block(ip, offset, isize);
  438. if (error) {
  439. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_IOLOCK_EXCL));
  440. return error;
  441. }
  442. /*
  443. * Calculate the range between the new size and the old
  444. * where blocks needing to be zeroed may exist. To get the
  445. * block where the last byte in the file currently resides,
  446. * we need to subtract one from the size and truncate back
  447. * to a block boundary. We subtract 1 in case the size is
  448. * exactly on a block boundary.
  449. */
  450. last_fsb = isize ? XFS_B_TO_FSBT(mp, isize - 1) : (xfs_fileoff_t)-1;
  451. start_zero_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)isize);
  452. end_zero_fsb = XFS_B_TO_FSBT(mp, offset - 1);
  453. ASSERT((xfs_sfiloff_t)last_fsb < (xfs_sfiloff_t)start_zero_fsb);
  454. if (last_fsb == end_zero_fsb) {
  455. /*
  456. * The size was only incremented on its last block.
  457. * We took care of that above, so just return.
  458. */
  459. return 0;
  460. }
  461. ASSERT(start_zero_fsb <= end_zero_fsb);
  462. while (start_zero_fsb <= end_zero_fsb) {
  463. nimaps = 1;
  464. zero_count_fsb = end_zero_fsb - start_zero_fsb + 1;
  465. error = xfs_bmapi(NULL, ip, start_zero_fsb, zero_count_fsb,
  466. 0, NULL, 0, &imap, &nimaps, NULL);
  467. if (error) {
  468. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_IOLOCK_EXCL));
  469. return error;
  470. }
  471. ASSERT(nimaps > 0);
  472. if (imap.br_state == XFS_EXT_UNWRITTEN ||
  473. imap.br_startblock == HOLESTARTBLOCK) {
  474. /*
  475. * This loop handles initializing pages that were
  476. * partially initialized by the code below this
  477. * loop. It basically zeroes the part of the page
  478. * that sits on a hole and sets the page as P_HOLE
  479. * and calls remapf if it is a mapped file.
  480. */
  481. start_zero_fsb = imap.br_startoff + imap.br_blockcount;
  482. ASSERT(start_zero_fsb <= (end_zero_fsb + 1));
  483. continue;
  484. }
  485. /*
  486. * There are blocks we need to zero.
  487. * Drop the inode lock while we're doing the I/O.
  488. * We'll still have the iolock to protect us.
  489. */
  490. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  491. zero_off = XFS_FSB_TO_B(mp, start_zero_fsb);
  492. zero_len = XFS_FSB_TO_B(mp, imap.br_blockcount);
  493. if ((zero_off + zero_len) > offset)
  494. zero_len = offset - zero_off;
  495. error = xfs_iozero(ip, zero_off, zero_len);
  496. if (error) {
  497. goto out_lock;
  498. }
  499. start_zero_fsb = imap.br_startoff + imap.br_blockcount;
  500. ASSERT(start_zero_fsb <= (end_zero_fsb + 1));
  501. xfs_ilock(ip, XFS_ILOCK_EXCL);
  502. }
  503. return 0;
  504. out_lock:
  505. xfs_ilock(ip, XFS_ILOCK_EXCL);
  506. ASSERT(error >= 0);
  507. return error;
  508. }
  509. STATIC ssize_t
  510. xfs_file_aio_write(
  511. struct kiocb *iocb,
  512. const struct iovec *iovp,
  513. unsigned long nr_segs,
  514. loff_t pos)
  515. {
  516. struct file *file = iocb->ki_filp;
  517. struct address_space *mapping = file->f_mapping;
  518. struct inode *inode = mapping->host;
  519. struct xfs_inode *ip = XFS_I(inode);
  520. struct xfs_mount *mp = ip->i_mount;
  521. ssize_t ret = 0;
  522. int ioflags = 0;
  523. xfs_fsize_t new_size;
  524. int iolock;
  525. size_t ocount = 0, count;
  526. int need_i_mutex;
  527. XFS_STATS_INC(xs_write_calls);
  528. BUG_ON(iocb->ki_pos != pos);
  529. if (unlikely(file->f_flags & O_DIRECT))
  530. ioflags |= IO_ISDIRECT;
  531. if (file->f_mode & FMODE_NOCMTIME)
  532. ioflags |= IO_INVIS;
  533. ret = generic_segment_checks(iovp, &nr_segs, &ocount, VERIFY_READ);
  534. if (ret)
  535. return ret;
  536. count = ocount;
  537. if (count == 0)
  538. return 0;
  539. xfs_wait_for_freeze(mp, SB_FREEZE_WRITE);
  540. if (XFS_FORCED_SHUTDOWN(mp))
  541. return -EIO;
  542. relock:
  543. if (ioflags & IO_ISDIRECT) {
  544. iolock = XFS_IOLOCK_SHARED;
  545. need_i_mutex = 0;
  546. } else {
  547. iolock = XFS_IOLOCK_EXCL;
  548. need_i_mutex = 1;
  549. mutex_lock(&inode->i_mutex);
  550. }
  551. xfs_ilock(ip, XFS_ILOCK_EXCL|iolock);
  552. start:
  553. ret = generic_write_checks(file, &pos, &count,
  554. S_ISBLK(inode->i_mode));
  555. if (ret) {
  556. xfs_iunlock(ip, XFS_ILOCK_EXCL|iolock);
  557. goto out_unlock_mutex;
  558. }
  559. if (ioflags & IO_ISDIRECT) {
  560. xfs_buftarg_t *target =
  561. XFS_IS_REALTIME_INODE(ip) ?
  562. mp->m_rtdev_targp : mp->m_ddev_targp;
  563. if ((pos & target->bt_smask) || (count & target->bt_smask)) {
  564. xfs_iunlock(ip, XFS_ILOCK_EXCL|iolock);
  565. return XFS_ERROR(-EINVAL);
  566. }
  567. if (!need_i_mutex && (mapping->nrpages || pos > ip->i_size)) {
  568. xfs_iunlock(ip, XFS_ILOCK_EXCL|iolock);
  569. iolock = XFS_IOLOCK_EXCL;
  570. need_i_mutex = 1;
  571. mutex_lock(&inode->i_mutex);
  572. xfs_ilock(ip, XFS_ILOCK_EXCL|iolock);
  573. goto start;
  574. }
  575. }
  576. new_size = pos + count;
  577. if (new_size > ip->i_size)
  578. ip->i_new_size = new_size;
  579. if (likely(!(ioflags & IO_INVIS)))
  580. file_update_time(file);
  581. /*
  582. * If the offset is beyond the size of the file, we have a couple
  583. * of things to do. First, if there is already space allocated
  584. * we need to either create holes or zero the disk or ...
  585. *
  586. * If there is a page where the previous size lands, we need
  587. * to zero it out up to the new size.
  588. */
  589. if (pos > ip->i_size) {
  590. ret = -xfs_zero_eof(ip, pos, ip->i_size);
  591. if (ret) {
  592. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  593. goto out_unlock_internal;
  594. }
  595. }
  596. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  597. /*
  598. * If we're writing the file then make sure to clear the
  599. * setuid and setgid bits if the process is not being run
  600. * by root. This keeps people from modifying setuid and
  601. * setgid binaries.
  602. */
  603. ret = file_remove_suid(file);
  604. if (unlikely(ret))
  605. goto out_unlock_internal;
  606. /* We can write back this queue in page reclaim */
  607. current->backing_dev_info = mapping->backing_dev_info;
  608. if ((ioflags & IO_ISDIRECT)) {
  609. if (mapping->nrpages) {
  610. WARN_ON(need_i_mutex == 0);
  611. ret = -xfs_flushinval_pages(ip,
  612. (pos & PAGE_CACHE_MASK),
  613. -1, FI_REMAPF_LOCKED);
  614. if (ret)
  615. goto out_unlock_internal;
  616. }
  617. if (need_i_mutex) {
  618. /* demote the lock now the cached pages are gone */
  619. xfs_ilock_demote(ip, XFS_IOLOCK_EXCL);
  620. mutex_unlock(&inode->i_mutex);
  621. iolock = XFS_IOLOCK_SHARED;
  622. need_i_mutex = 0;
  623. }
  624. trace_xfs_file_direct_write(ip, count, iocb->ki_pos, ioflags);
  625. ret = generic_file_direct_write(iocb, iovp,
  626. &nr_segs, pos, &iocb->ki_pos, count, ocount);
  627. /*
  628. * direct-io write to a hole: fall through to buffered I/O
  629. * for completing the rest of the request.
  630. */
  631. if (ret >= 0 && ret != count) {
  632. XFS_STATS_ADD(xs_write_bytes, ret);
  633. pos += ret;
  634. count -= ret;
  635. ioflags &= ~IO_ISDIRECT;
  636. xfs_iunlock(ip, iolock);
  637. goto relock;
  638. }
  639. } else {
  640. int enospc = 0;
  641. write_retry:
  642. trace_xfs_file_buffered_write(ip, count, iocb->ki_pos, ioflags);
  643. ret = generic_file_buffered_write(iocb, iovp, nr_segs,
  644. pos, &iocb->ki_pos, count, ret);
  645. /*
  646. * if we just got an ENOSPC, flush the inode now we
  647. * aren't holding any page locks and retry *once*
  648. */
  649. if (ret == -ENOSPC && !enospc) {
  650. ret = xfs_flush_pages(ip, 0, -1, 0, FI_NONE);
  651. if (ret)
  652. goto out_unlock_internal;
  653. enospc = 1;
  654. goto write_retry;
  655. }
  656. }
  657. current->backing_dev_info = NULL;
  658. xfs_aio_write_isize_update(inode, &iocb->ki_pos, ret);
  659. if (ret <= 0)
  660. goto out_unlock_internal;
  661. /* Handle various SYNC-type writes */
  662. if ((file->f_flags & O_DSYNC) || IS_SYNC(inode)) {
  663. loff_t end = pos + ret - 1;
  664. int error, error2;
  665. xfs_iunlock(ip, iolock);
  666. if (need_i_mutex)
  667. mutex_unlock(&inode->i_mutex);
  668. error = filemap_write_and_wait_range(mapping, pos, end);
  669. if (need_i_mutex)
  670. mutex_lock(&inode->i_mutex);
  671. xfs_ilock(ip, iolock);
  672. error2 = -xfs_file_fsync(file,
  673. (file->f_flags & __O_SYNC) ? 0 : 1);
  674. if (error)
  675. ret = error;
  676. else if (error2)
  677. ret = error2;
  678. }
  679. out_unlock_internal:
  680. xfs_aio_write_newsize_update(ip);
  681. xfs_iunlock(ip, iolock);
  682. out_unlock_mutex:
  683. if (need_i_mutex)
  684. mutex_unlock(&inode->i_mutex);
  685. return ret;
  686. }
  687. STATIC int
  688. xfs_file_open(
  689. struct inode *inode,
  690. struct file *file)
  691. {
  692. if (!(file->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS)
  693. return -EFBIG;
  694. if (XFS_FORCED_SHUTDOWN(XFS_M(inode->i_sb)))
  695. return -EIO;
  696. return 0;
  697. }
  698. STATIC int
  699. xfs_dir_open(
  700. struct inode *inode,
  701. struct file *file)
  702. {
  703. struct xfs_inode *ip = XFS_I(inode);
  704. int mode;
  705. int error;
  706. error = xfs_file_open(inode, file);
  707. if (error)
  708. return error;
  709. /*
  710. * If there are any blocks, read-ahead block 0 as we're almost
  711. * certain to have the next operation be a read there.
  712. */
  713. mode = xfs_ilock_map_shared(ip);
  714. if (ip->i_d.di_nextents > 0)
  715. xfs_da_reada_buf(NULL, ip, 0, XFS_DATA_FORK);
  716. xfs_iunlock(ip, mode);
  717. return 0;
  718. }
  719. STATIC int
  720. xfs_file_release(
  721. struct inode *inode,
  722. struct file *filp)
  723. {
  724. return -xfs_release(XFS_I(inode));
  725. }
  726. STATIC int
  727. xfs_file_readdir(
  728. struct file *filp,
  729. void *dirent,
  730. filldir_t filldir)
  731. {
  732. struct inode *inode = filp->f_path.dentry->d_inode;
  733. xfs_inode_t *ip = XFS_I(inode);
  734. int error;
  735. size_t bufsize;
  736. /*
  737. * The Linux API doesn't pass down the total size of the buffer
  738. * we read into down to the filesystem. With the filldir concept
  739. * it's not needed for correct information, but the XFS dir2 leaf
  740. * code wants an estimate of the buffer size to calculate it's
  741. * readahead window and size the buffers used for mapping to
  742. * physical blocks.
  743. *
  744. * Try to give it an estimate that's good enough, maybe at some
  745. * point we can change the ->readdir prototype to include the
  746. * buffer size. For now we use the current glibc buffer size.
  747. */
  748. bufsize = (size_t)min_t(loff_t, 32768, ip->i_d.di_size);
  749. error = xfs_readdir(ip, dirent, bufsize,
  750. (xfs_off_t *)&filp->f_pos, filldir);
  751. if (error)
  752. return -error;
  753. return 0;
  754. }
  755. STATIC int
  756. xfs_file_mmap(
  757. struct file *filp,
  758. struct vm_area_struct *vma)
  759. {
  760. vma->vm_ops = &xfs_file_vm_ops;
  761. vma->vm_flags |= VM_CAN_NONLINEAR;
  762. file_accessed(filp);
  763. return 0;
  764. }
  765. /*
  766. * mmap()d file has taken write protection fault and is being made
  767. * writable. We can set the page state up correctly for a writable
  768. * page, which means we can do correct delalloc accounting (ENOSPC
  769. * checking!) and unwritten extent mapping.
  770. */
  771. STATIC int
  772. xfs_vm_page_mkwrite(
  773. struct vm_area_struct *vma,
  774. struct vm_fault *vmf)
  775. {
  776. return block_page_mkwrite(vma, vmf, xfs_get_blocks);
  777. }
  778. const struct file_operations xfs_file_operations = {
  779. .llseek = generic_file_llseek,
  780. .read = do_sync_read,
  781. .write = do_sync_write,
  782. .aio_read = xfs_file_aio_read,
  783. .aio_write = xfs_file_aio_write,
  784. .splice_read = xfs_file_splice_read,
  785. .splice_write = xfs_file_splice_write,
  786. .unlocked_ioctl = xfs_file_ioctl,
  787. #ifdef CONFIG_COMPAT
  788. .compat_ioctl = xfs_file_compat_ioctl,
  789. #endif
  790. .mmap = xfs_file_mmap,
  791. .open = xfs_file_open,
  792. .release = xfs_file_release,
  793. .fsync = xfs_file_fsync,
  794. };
  795. const struct file_operations xfs_dir_file_operations = {
  796. .open = xfs_dir_open,
  797. .read = generic_read_dir,
  798. .readdir = xfs_file_readdir,
  799. .llseek = generic_file_llseek,
  800. .unlocked_ioctl = xfs_file_ioctl,
  801. #ifdef CONFIG_COMPAT
  802. .compat_ioctl = xfs_file_compat_ioctl,
  803. #endif
  804. .fsync = xfs_file_fsync,
  805. };
  806. static const struct vm_operations_struct xfs_file_vm_ops = {
  807. .fault = filemap_fault,
  808. .page_mkwrite = xfs_vm_page_mkwrite,
  809. };