xfs_file.c 27 KB

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