xfs_file.c 24 KB

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