xfs_file.c 28 KB

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