xfs_file.c 27 KB

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