xfs_file.c 24 KB

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