xfs_file.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135
  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. {
  835. struct inode *inode = file->f_mapping->host;
  836. struct xfs_inode *ip = XFS_I(inode);
  837. struct xfs_mount *mp = ip->i_mount;
  838. struct xfs_bmbt_irec map[2];
  839. int nmap = 2;
  840. loff_t uninitialized_var(offset);
  841. xfs_fsize_t isize;
  842. xfs_fileoff_t fsbno;
  843. xfs_filblks_t end;
  844. uint lock;
  845. int error;
  846. lock = xfs_ilock_map_shared(ip);
  847. isize = i_size_read(inode);
  848. if (start >= isize) {
  849. error = ENXIO;
  850. goto out_unlock;
  851. }
  852. fsbno = XFS_B_TO_FSBT(mp, start);
  853. /*
  854. * Try to read extents from the first block indicated
  855. * by fsbno to the end block of the file.
  856. */
  857. end = XFS_B_TO_FSB(mp, isize);
  858. error = xfs_bmapi_read(ip, fsbno, end - fsbno, map, &nmap,
  859. XFS_BMAPI_ENTIRE);
  860. if (error)
  861. goto out_unlock;
  862. /*
  863. * Treat unwritten extent as data extent since it might
  864. * contains dirty data in page cache.
  865. */
  866. if (map[0].br_startblock != HOLESTARTBLOCK) {
  867. offset = max_t(loff_t, start,
  868. XFS_FSB_TO_B(mp, map[0].br_startoff));
  869. } else {
  870. if (nmap == 1) {
  871. error = ENXIO;
  872. goto out_unlock;
  873. }
  874. offset = max_t(loff_t, start,
  875. XFS_FSB_TO_B(mp, map[1].br_startoff));
  876. }
  877. if (offset != file->f_pos)
  878. file->f_pos = offset;
  879. out_unlock:
  880. xfs_iunlock_map_shared(ip, lock);
  881. if (error)
  882. return -error;
  883. return offset;
  884. }
  885. STATIC loff_t
  886. xfs_seek_hole(
  887. struct file *file,
  888. loff_t start)
  889. {
  890. struct inode *inode = file->f_mapping->host;
  891. struct xfs_inode *ip = XFS_I(inode);
  892. struct xfs_mount *mp = ip->i_mount;
  893. loff_t uninitialized_var(offset);
  894. loff_t holeoff;
  895. xfs_fsize_t isize;
  896. xfs_fileoff_t fsbno;
  897. uint lock;
  898. int error;
  899. if (XFS_FORCED_SHUTDOWN(mp))
  900. return -XFS_ERROR(EIO);
  901. lock = xfs_ilock_map_shared(ip);
  902. isize = i_size_read(inode);
  903. if (start >= isize) {
  904. error = ENXIO;
  905. goto out_unlock;
  906. }
  907. fsbno = XFS_B_TO_FSBT(mp, start);
  908. error = xfs_bmap_first_unused(NULL, ip, 1, &fsbno, XFS_DATA_FORK);
  909. if (error)
  910. goto out_unlock;
  911. holeoff = XFS_FSB_TO_B(mp, fsbno);
  912. if (holeoff <= start)
  913. offset = start;
  914. else {
  915. /*
  916. * xfs_bmap_first_unused() could return a value bigger than
  917. * isize if there are no more holes past the supplied offset.
  918. */
  919. offset = min_t(loff_t, holeoff, isize);
  920. }
  921. if (offset != file->f_pos)
  922. file->f_pos = offset;
  923. out_unlock:
  924. xfs_iunlock_map_shared(ip, lock);
  925. if (error)
  926. return -error;
  927. return offset;
  928. }
  929. STATIC loff_t
  930. xfs_file_llseek(
  931. struct file *file,
  932. loff_t offset,
  933. int origin)
  934. {
  935. switch (origin) {
  936. case SEEK_END:
  937. case SEEK_CUR:
  938. case SEEK_SET:
  939. return generic_file_llseek(file, offset, origin);
  940. case SEEK_DATA:
  941. return xfs_seek_data(file, offset);
  942. case SEEK_HOLE:
  943. return xfs_seek_hole(file, offset);
  944. default:
  945. return -EINVAL;
  946. }
  947. }
  948. const struct file_operations xfs_file_operations = {
  949. .llseek = xfs_file_llseek,
  950. .read = do_sync_read,
  951. .write = do_sync_write,
  952. .aio_read = xfs_file_aio_read,
  953. .aio_write = xfs_file_aio_write,
  954. .splice_read = xfs_file_splice_read,
  955. .splice_write = xfs_file_splice_write,
  956. .unlocked_ioctl = xfs_file_ioctl,
  957. #ifdef CONFIG_COMPAT
  958. .compat_ioctl = xfs_file_compat_ioctl,
  959. #endif
  960. .mmap = xfs_file_mmap,
  961. .open = xfs_file_open,
  962. .release = xfs_file_release,
  963. .fsync = xfs_file_fsync,
  964. .fallocate = xfs_file_fallocate,
  965. };
  966. const struct file_operations xfs_dir_file_operations = {
  967. .open = xfs_dir_open,
  968. .read = generic_read_dir,
  969. .readdir = xfs_file_readdir,
  970. .llseek = generic_file_llseek,
  971. .unlocked_ioctl = xfs_file_ioctl,
  972. #ifdef CONFIG_COMPAT
  973. .compat_ioctl = xfs_file_compat_ioctl,
  974. #endif
  975. .fsync = xfs_dir_fsync,
  976. };
  977. static const struct vm_operations_struct xfs_file_vm_ops = {
  978. .fault = filemap_fault,
  979. .page_mkwrite = xfs_vm_page_mkwrite,
  980. };