xfs_file.c 28 KB

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