xfs_file.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096
  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. STATIC int
  115. xfs_file_fsync(
  116. struct file *file,
  117. loff_t start,
  118. loff_t end,
  119. int datasync)
  120. {
  121. struct inode *inode = file->f_mapping->host;
  122. struct xfs_inode *ip = XFS_I(inode);
  123. struct xfs_mount *mp = ip->i_mount;
  124. struct xfs_trans *tp;
  125. int error = 0;
  126. int log_flushed = 0;
  127. trace_xfs_file_fsync(ip);
  128. error = filemap_write_and_wait_range(inode->i_mapping, start, end);
  129. if (error)
  130. return error;
  131. if (XFS_FORCED_SHUTDOWN(mp))
  132. return -XFS_ERROR(EIO);
  133. xfs_iflags_clear(ip, XFS_ITRUNCATED);
  134. xfs_ilock(ip, XFS_IOLOCK_SHARED);
  135. xfs_ioend_wait(ip);
  136. xfs_iunlock(ip, XFS_IOLOCK_SHARED);
  137. if (mp->m_flags & XFS_MOUNT_BARRIER) {
  138. /*
  139. * If we have an RT and/or log subvolume we need to make sure
  140. * to flush the write cache the device used for file data
  141. * first. This is to ensure newly written file data make
  142. * it to disk before logging the new inode size in case of
  143. * an extending write.
  144. */
  145. if (XFS_IS_REALTIME_INODE(ip))
  146. xfs_blkdev_issue_flush(mp->m_rtdev_targp);
  147. else if (mp->m_logdev_targp != mp->m_ddev_targp)
  148. xfs_blkdev_issue_flush(mp->m_ddev_targp);
  149. }
  150. /*
  151. * We always need to make sure that the required inode state is safe on
  152. * disk. The inode might be clean but we still might need to force the
  153. * log because of committed transactions that haven't hit the disk yet.
  154. * Likewise, there could be unflushed non-transactional changes to the
  155. * inode core that have to go to disk and this requires us to issue
  156. * a synchronous transaction to capture these changes correctly.
  157. *
  158. * This code relies on the assumption that if the i_update_core field
  159. * of the inode is clear and the inode is unpinned then it is clean
  160. * and no action is required.
  161. */
  162. xfs_ilock(ip, XFS_ILOCK_SHARED);
  163. /*
  164. * First check if the VFS inode is marked dirty. All the dirtying
  165. * of non-transactional updates no goes through mark_inode_dirty*,
  166. * which allows us to distinguish beteeen pure timestamp updates
  167. * and i_size updates which need to be caught for fdatasync.
  168. * After that also theck for the dirty state in the XFS inode, which
  169. * might gets cleared when the inode gets written out via the AIL
  170. * or xfs_iflush_cluster.
  171. */
  172. if (((inode->i_state & I_DIRTY_DATASYNC) ||
  173. ((inode->i_state & I_DIRTY_SYNC) && !datasync)) &&
  174. ip->i_update_core) {
  175. /*
  176. * Kick off a transaction to log the inode core to get the
  177. * updates. The sync transaction will also force the log.
  178. */
  179. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  180. tp = xfs_trans_alloc(mp, XFS_TRANS_FSYNC_TS);
  181. error = xfs_trans_reserve(tp, 0,
  182. XFS_FSYNC_TS_LOG_RES(mp), 0, 0, 0);
  183. if (error) {
  184. xfs_trans_cancel(tp, 0);
  185. return -error;
  186. }
  187. xfs_ilock(ip, XFS_ILOCK_EXCL);
  188. /*
  189. * Note - it's possible that we might have pushed ourselves out
  190. * of the way during trans_reserve which would flush the inode.
  191. * But there's no guarantee that the inode buffer has actually
  192. * gone out yet (it's delwri). Plus the buffer could be pinned
  193. * anyway if it's part of an inode in another recent
  194. * transaction. So we play it safe and fire off the
  195. * transaction anyway.
  196. */
  197. xfs_trans_ijoin(tp, ip);
  198. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  199. xfs_trans_set_sync(tp);
  200. error = _xfs_trans_commit(tp, 0, &log_flushed);
  201. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  202. } else {
  203. /*
  204. * Timestamps/size haven't changed since last inode flush or
  205. * inode transaction commit. That means either nothing got
  206. * written or a transaction committed which caught the updates.
  207. * If the latter happened and the transaction hasn't hit the
  208. * disk yet, the inode will be still be pinned. If it is,
  209. * force the log.
  210. */
  211. if (xfs_ipincount(ip)) {
  212. error = _xfs_log_force_lsn(mp,
  213. ip->i_itemp->ili_last_lsn,
  214. XFS_LOG_SYNC, &log_flushed);
  215. }
  216. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  217. }
  218. /*
  219. * If we only have a single device, and the log force about was
  220. * a no-op we might have to flush the data device cache here.
  221. * This can only happen for fdatasync/O_DSYNC if we were overwriting
  222. * an already allocated file and thus do not have any metadata to
  223. * commit.
  224. */
  225. if ((mp->m_flags & XFS_MOUNT_BARRIER) &&
  226. mp->m_logdev_targp == mp->m_ddev_targp &&
  227. !XFS_IS_REALTIME_INODE(ip) &&
  228. !log_flushed)
  229. xfs_blkdev_issue_flush(mp->m_ddev_targp);
  230. return -error;
  231. }
  232. STATIC ssize_t
  233. xfs_file_aio_read(
  234. struct kiocb *iocb,
  235. const struct iovec *iovp,
  236. unsigned long nr_segs,
  237. loff_t pos)
  238. {
  239. struct file *file = iocb->ki_filp;
  240. struct inode *inode = file->f_mapping->host;
  241. struct xfs_inode *ip = XFS_I(inode);
  242. struct xfs_mount *mp = ip->i_mount;
  243. size_t size = 0;
  244. ssize_t ret = 0;
  245. int ioflags = 0;
  246. xfs_fsize_t n;
  247. unsigned long seg;
  248. XFS_STATS_INC(xs_read_calls);
  249. BUG_ON(iocb->ki_pos != pos);
  250. if (unlikely(file->f_flags & O_DIRECT))
  251. ioflags |= IO_ISDIRECT;
  252. if (file->f_mode & FMODE_NOCMTIME)
  253. ioflags |= IO_INVIS;
  254. /* START copy & waste from filemap.c */
  255. for (seg = 0; seg < nr_segs; seg++) {
  256. const struct iovec *iv = &iovp[seg];
  257. /*
  258. * If any segment has a negative length, or the cumulative
  259. * length ever wraps negative then return -EINVAL.
  260. */
  261. size += iv->iov_len;
  262. if (unlikely((ssize_t)(size|iv->iov_len) < 0))
  263. return XFS_ERROR(-EINVAL);
  264. }
  265. /* END copy & waste from filemap.c */
  266. if (unlikely(ioflags & IO_ISDIRECT)) {
  267. xfs_buftarg_t *target =
  268. XFS_IS_REALTIME_INODE(ip) ?
  269. mp->m_rtdev_targp : mp->m_ddev_targp;
  270. if ((iocb->ki_pos & target->bt_smask) ||
  271. (size & target->bt_smask)) {
  272. if (iocb->ki_pos == ip->i_size)
  273. return 0;
  274. return -XFS_ERROR(EINVAL);
  275. }
  276. }
  277. n = XFS_MAXIOFFSET(mp) - iocb->ki_pos;
  278. if (n <= 0 || size == 0)
  279. return 0;
  280. if (n < size)
  281. size = n;
  282. if (XFS_FORCED_SHUTDOWN(mp))
  283. return -EIO;
  284. if (unlikely(ioflags & IO_ISDIRECT)) {
  285. xfs_rw_ilock(ip, XFS_IOLOCK_EXCL);
  286. if (inode->i_mapping->nrpages) {
  287. ret = -xfs_flushinval_pages(ip,
  288. (iocb->ki_pos & PAGE_CACHE_MASK),
  289. -1, FI_REMAPF_LOCKED);
  290. if (ret) {
  291. xfs_rw_iunlock(ip, XFS_IOLOCK_EXCL);
  292. return ret;
  293. }
  294. }
  295. xfs_rw_ilock_demote(ip, XFS_IOLOCK_EXCL);
  296. } else
  297. xfs_rw_ilock(ip, XFS_IOLOCK_SHARED);
  298. trace_xfs_file_read(ip, size, iocb->ki_pos, ioflags);
  299. ret = generic_file_aio_read(iocb, iovp, nr_segs, iocb->ki_pos);
  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. STATIC ssize_t
  306. xfs_file_splice_read(
  307. struct file *infilp,
  308. loff_t *ppos,
  309. struct pipe_inode_info *pipe,
  310. size_t count,
  311. unsigned int flags)
  312. {
  313. struct xfs_inode *ip = XFS_I(infilp->f_mapping->host);
  314. int ioflags = 0;
  315. ssize_t ret;
  316. XFS_STATS_INC(xs_read_calls);
  317. if (infilp->f_mode & FMODE_NOCMTIME)
  318. ioflags |= IO_INVIS;
  319. if (XFS_FORCED_SHUTDOWN(ip->i_mount))
  320. return -EIO;
  321. xfs_rw_ilock(ip, XFS_IOLOCK_SHARED);
  322. trace_xfs_file_splice_read(ip, count, *ppos, ioflags);
  323. ret = generic_file_splice_read(infilp, ppos, pipe, count, flags);
  324. if (ret > 0)
  325. XFS_STATS_ADD(xs_read_bytes, ret);
  326. xfs_rw_iunlock(ip, XFS_IOLOCK_SHARED);
  327. return ret;
  328. }
  329. STATIC void
  330. xfs_aio_write_isize_update(
  331. struct inode *inode,
  332. loff_t *ppos,
  333. ssize_t bytes_written)
  334. {
  335. struct xfs_inode *ip = XFS_I(inode);
  336. xfs_fsize_t isize = i_size_read(inode);
  337. if (bytes_written > 0)
  338. XFS_STATS_ADD(xs_write_bytes, bytes_written);
  339. if (unlikely(bytes_written < 0 && bytes_written != -EFAULT &&
  340. *ppos > isize))
  341. *ppos = isize;
  342. if (*ppos > ip->i_size) {
  343. xfs_rw_ilock(ip, XFS_ILOCK_EXCL);
  344. if (*ppos > ip->i_size)
  345. ip->i_size = *ppos;
  346. xfs_rw_iunlock(ip, XFS_ILOCK_EXCL);
  347. }
  348. }
  349. /*
  350. * If this was a direct or synchronous I/O that failed (such as ENOSPC) then
  351. * part of the I/O may have been written to disk before the error occurred. In
  352. * this case the on-disk file size may have been adjusted beyond the in-memory
  353. * file size and now needs to be truncated back.
  354. */
  355. STATIC void
  356. xfs_aio_write_newsize_update(
  357. struct xfs_inode *ip)
  358. {
  359. if (ip->i_new_size) {
  360. xfs_rw_ilock(ip, XFS_ILOCK_EXCL);
  361. ip->i_new_size = 0;
  362. if (ip->i_d.di_size > ip->i_size)
  363. ip->i_d.di_size = ip->i_size;
  364. xfs_rw_iunlock(ip, XFS_ILOCK_EXCL);
  365. }
  366. }
  367. /*
  368. * xfs_file_splice_write() does not use xfs_rw_ilock() because
  369. * generic_file_splice_write() takes the i_mutex itself. This, in theory,
  370. * couuld cause lock inversions between the aio_write path and the splice path
  371. * if someone is doing concurrent splice(2) based writes and write(2) based
  372. * writes to the same inode. The only real way to fix this is to re-implement
  373. * the generic code here with correct locking orders.
  374. */
  375. STATIC ssize_t
  376. xfs_file_splice_write(
  377. struct pipe_inode_info *pipe,
  378. struct file *outfilp,
  379. loff_t *ppos,
  380. size_t count,
  381. unsigned int flags)
  382. {
  383. struct inode *inode = outfilp->f_mapping->host;
  384. struct xfs_inode *ip = XFS_I(inode);
  385. xfs_fsize_t new_size;
  386. int ioflags = 0;
  387. ssize_t ret;
  388. XFS_STATS_INC(xs_write_calls);
  389. if (outfilp->f_mode & FMODE_NOCMTIME)
  390. ioflags |= IO_INVIS;
  391. if (XFS_FORCED_SHUTDOWN(ip->i_mount))
  392. return -EIO;
  393. xfs_ilock(ip, XFS_IOLOCK_EXCL);
  394. new_size = *ppos + count;
  395. xfs_ilock(ip, XFS_ILOCK_EXCL);
  396. if (new_size > ip->i_size)
  397. ip->i_new_size = new_size;
  398. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  399. trace_xfs_file_splice_write(ip, count, *ppos, ioflags);
  400. ret = generic_file_splice_write(pipe, outfilp, ppos, count, flags);
  401. xfs_aio_write_isize_update(inode, ppos, ret);
  402. xfs_aio_write_newsize_update(ip);
  403. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  404. return ret;
  405. }
  406. /*
  407. * This routine is called to handle zeroing any space in the last
  408. * block of the file that is beyond the EOF. We do this since the
  409. * size is being increased without writing anything to that block
  410. * and we don't want anyone to read the garbage on the disk.
  411. */
  412. STATIC int /* error (positive) */
  413. xfs_zero_last_block(
  414. xfs_inode_t *ip,
  415. xfs_fsize_t offset,
  416. xfs_fsize_t isize)
  417. {
  418. xfs_fileoff_t last_fsb;
  419. xfs_mount_t *mp = ip->i_mount;
  420. int nimaps;
  421. int zero_offset;
  422. int zero_len;
  423. int error = 0;
  424. xfs_bmbt_irec_t imap;
  425. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
  426. zero_offset = XFS_B_FSB_OFFSET(mp, isize);
  427. if (zero_offset == 0) {
  428. /*
  429. * There are no extra bytes in the last block on disk to
  430. * zero, so return.
  431. */
  432. return 0;
  433. }
  434. last_fsb = XFS_B_TO_FSBT(mp, isize);
  435. nimaps = 1;
  436. error = xfs_bmapi(NULL, ip, last_fsb, 1, 0, NULL, 0, &imap,
  437. &nimaps, NULL);
  438. if (error) {
  439. return error;
  440. }
  441. ASSERT(nimaps > 0);
  442. /*
  443. * If the block underlying isize is just a hole, then there
  444. * is nothing to zero.
  445. */
  446. if (imap.br_startblock == HOLESTARTBLOCK) {
  447. return 0;
  448. }
  449. /*
  450. * Zero the part of the last block beyond the EOF, and write it
  451. * out sync. We need to drop the ilock while we do this so we
  452. * don't deadlock when the buffer cache calls back to us.
  453. */
  454. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  455. zero_len = mp->m_sb.sb_blocksize - zero_offset;
  456. if (isize + zero_len > offset)
  457. zero_len = offset - isize;
  458. error = xfs_iozero(ip, isize, zero_len);
  459. xfs_ilock(ip, XFS_ILOCK_EXCL);
  460. ASSERT(error >= 0);
  461. return error;
  462. }
  463. /*
  464. * Zero any on disk space between the current EOF and the new,
  465. * larger EOF. This handles the normal case of zeroing the remainder
  466. * of the last block in the file and the unusual case of zeroing blocks
  467. * out beyond the size of the file. This second case only happens
  468. * with fixed size extents and when the system crashes before the inode
  469. * size was updated but after blocks were allocated. If fill is set,
  470. * then any holes in the range are filled and zeroed. If not, the holes
  471. * are left alone as holes.
  472. */
  473. int /* error (positive) */
  474. xfs_zero_eof(
  475. xfs_inode_t *ip,
  476. xfs_off_t offset, /* starting I/O offset */
  477. xfs_fsize_t isize) /* current inode size */
  478. {
  479. xfs_mount_t *mp = ip->i_mount;
  480. xfs_fileoff_t start_zero_fsb;
  481. xfs_fileoff_t end_zero_fsb;
  482. xfs_fileoff_t zero_count_fsb;
  483. xfs_fileoff_t last_fsb;
  484. xfs_fileoff_t zero_off;
  485. xfs_fsize_t zero_len;
  486. int nimaps;
  487. int error = 0;
  488. xfs_bmbt_irec_t imap;
  489. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_IOLOCK_EXCL));
  490. ASSERT(offset > isize);
  491. /*
  492. * First handle zeroing the block on which isize resides.
  493. * We only zero a part of that block so it is handled specially.
  494. */
  495. error = xfs_zero_last_block(ip, offset, isize);
  496. if (error) {
  497. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_IOLOCK_EXCL));
  498. return error;
  499. }
  500. /*
  501. * Calculate the range between the new size and the old
  502. * where blocks needing to be zeroed may exist. To get the
  503. * block where the last byte in the file currently resides,
  504. * we need to subtract one from the size and truncate back
  505. * to a block boundary. We subtract 1 in case the size is
  506. * exactly on a block boundary.
  507. */
  508. last_fsb = isize ? XFS_B_TO_FSBT(mp, isize - 1) : (xfs_fileoff_t)-1;
  509. start_zero_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)isize);
  510. end_zero_fsb = XFS_B_TO_FSBT(mp, offset - 1);
  511. ASSERT((xfs_sfiloff_t)last_fsb < (xfs_sfiloff_t)start_zero_fsb);
  512. if (last_fsb == end_zero_fsb) {
  513. /*
  514. * The size was only incremented on its last block.
  515. * We took care of that above, so just return.
  516. */
  517. return 0;
  518. }
  519. ASSERT(start_zero_fsb <= end_zero_fsb);
  520. while (start_zero_fsb <= end_zero_fsb) {
  521. nimaps = 1;
  522. zero_count_fsb = end_zero_fsb - start_zero_fsb + 1;
  523. error = xfs_bmapi(NULL, ip, start_zero_fsb, zero_count_fsb,
  524. 0, NULL, 0, &imap, &nimaps, NULL);
  525. if (error) {
  526. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_IOLOCK_EXCL));
  527. return error;
  528. }
  529. ASSERT(nimaps > 0);
  530. if (imap.br_state == XFS_EXT_UNWRITTEN ||
  531. imap.br_startblock == HOLESTARTBLOCK) {
  532. /*
  533. * This loop handles initializing pages that were
  534. * partially initialized by the code below this
  535. * loop. It basically zeroes the part of the page
  536. * that sits on a hole and sets the page as P_HOLE
  537. * and calls remapf if it is a mapped file.
  538. */
  539. start_zero_fsb = imap.br_startoff + imap.br_blockcount;
  540. ASSERT(start_zero_fsb <= (end_zero_fsb + 1));
  541. continue;
  542. }
  543. /*
  544. * There are blocks we need to zero.
  545. * Drop the inode lock while we're doing the I/O.
  546. * We'll still have the iolock to protect us.
  547. */
  548. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  549. zero_off = XFS_FSB_TO_B(mp, start_zero_fsb);
  550. zero_len = XFS_FSB_TO_B(mp, imap.br_blockcount);
  551. if ((zero_off + zero_len) > offset)
  552. zero_len = offset - zero_off;
  553. error = xfs_iozero(ip, zero_off, zero_len);
  554. if (error) {
  555. goto out_lock;
  556. }
  557. start_zero_fsb = imap.br_startoff + imap.br_blockcount;
  558. ASSERT(start_zero_fsb <= (end_zero_fsb + 1));
  559. xfs_ilock(ip, XFS_ILOCK_EXCL);
  560. }
  561. return 0;
  562. out_lock:
  563. xfs_ilock(ip, XFS_ILOCK_EXCL);
  564. ASSERT(error >= 0);
  565. return error;
  566. }
  567. /*
  568. * Common pre-write limit and setup checks.
  569. *
  570. * Returns with iolock held according to @iolock.
  571. */
  572. STATIC ssize_t
  573. xfs_file_aio_write_checks(
  574. struct file *file,
  575. loff_t *pos,
  576. size_t *count,
  577. int *iolock)
  578. {
  579. struct inode *inode = file->f_mapping->host;
  580. struct xfs_inode *ip = XFS_I(inode);
  581. xfs_fsize_t new_size;
  582. int error = 0;
  583. error = generic_write_checks(file, pos, count, S_ISBLK(inode->i_mode));
  584. if (error) {
  585. xfs_rw_iunlock(ip, XFS_ILOCK_EXCL | *iolock);
  586. *iolock = 0;
  587. return error;
  588. }
  589. new_size = *pos + *count;
  590. if (new_size > ip->i_size)
  591. ip->i_new_size = new_size;
  592. if (likely(!(file->f_mode & FMODE_NOCMTIME)))
  593. file_update_time(file);
  594. /*
  595. * If the offset is beyond the size of the file, we need to zero any
  596. * blocks that fall between the existing EOF and the start of this
  597. * write.
  598. */
  599. if (*pos > ip->i_size)
  600. error = -xfs_zero_eof(ip, *pos, ip->i_size);
  601. xfs_rw_iunlock(ip, XFS_ILOCK_EXCL);
  602. if (error)
  603. return error;
  604. /*
  605. * If we're writing the file then make sure to clear the setuid and
  606. * setgid bits if the process is not being run by root. This keeps
  607. * people from modifying setuid and setgid binaries.
  608. */
  609. return file_remove_suid(file);
  610. }
  611. /*
  612. * xfs_file_dio_aio_write - handle direct IO writes
  613. *
  614. * Lock the inode appropriately to prepare for and issue a direct IO write.
  615. * By separating it from the buffered write path we remove all the tricky to
  616. * follow locking changes and looping.
  617. *
  618. * If there are cached pages or we're extending the file, we need IOLOCK_EXCL
  619. * until we're sure the bytes at the new EOF have been zeroed and/or the cached
  620. * pages are flushed out.
  621. *
  622. * In most cases the direct IO writes will be done holding IOLOCK_SHARED
  623. * allowing them to be done in parallel with reads and other direct IO writes.
  624. * However, if the IO is not aligned to filesystem blocks, the direct IO layer
  625. * needs to do sub-block zeroing and that requires serialisation against other
  626. * direct IOs to the same block. In this case we need to serialise the
  627. * submission of the unaligned IOs so that we don't get racing block zeroing in
  628. * the dio layer. To avoid the problem with aio, we also need to wait for
  629. * outstanding IOs to complete so that unwritten extent conversion is completed
  630. * before we try to map the overlapping block. This is currently implemented by
  631. * hitting it with a big hammer (i.e. xfs_ioend_wait()).
  632. *
  633. * Returns with locks held indicated by @iolock and errors indicated by
  634. * negative return values.
  635. */
  636. STATIC ssize_t
  637. xfs_file_dio_aio_write(
  638. struct kiocb *iocb,
  639. const struct iovec *iovp,
  640. unsigned long nr_segs,
  641. loff_t pos,
  642. size_t ocount,
  643. int *iolock)
  644. {
  645. struct file *file = iocb->ki_filp;
  646. struct address_space *mapping = file->f_mapping;
  647. struct inode *inode = mapping->host;
  648. struct xfs_inode *ip = XFS_I(inode);
  649. struct xfs_mount *mp = ip->i_mount;
  650. ssize_t ret = 0;
  651. size_t count = ocount;
  652. int unaligned_io = 0;
  653. struct xfs_buftarg *target = XFS_IS_REALTIME_INODE(ip) ?
  654. mp->m_rtdev_targp : mp->m_ddev_targp;
  655. *iolock = 0;
  656. if ((pos & target->bt_smask) || (count & target->bt_smask))
  657. return -XFS_ERROR(EINVAL);
  658. if ((pos & mp->m_blockmask) || ((pos + count) & mp->m_blockmask))
  659. unaligned_io = 1;
  660. if (unaligned_io || mapping->nrpages || pos > ip->i_size)
  661. *iolock = XFS_IOLOCK_EXCL;
  662. else
  663. *iolock = XFS_IOLOCK_SHARED;
  664. xfs_rw_ilock(ip, XFS_ILOCK_EXCL | *iolock);
  665. ret = xfs_file_aio_write_checks(file, &pos, &count, iolock);
  666. if (ret)
  667. return ret;
  668. if (mapping->nrpages) {
  669. WARN_ON(*iolock != XFS_IOLOCK_EXCL);
  670. ret = -xfs_flushinval_pages(ip, (pos & PAGE_CACHE_MASK), -1,
  671. FI_REMAPF_LOCKED);
  672. if (ret)
  673. return ret;
  674. }
  675. /*
  676. * If we are doing unaligned IO, wait for all other IO to drain,
  677. * otherwise demote the lock if we had to flush cached pages
  678. */
  679. if (unaligned_io)
  680. xfs_ioend_wait(ip);
  681. else if (*iolock == XFS_IOLOCK_EXCL) {
  682. xfs_rw_ilock_demote(ip, XFS_IOLOCK_EXCL);
  683. *iolock = XFS_IOLOCK_SHARED;
  684. }
  685. trace_xfs_file_direct_write(ip, count, iocb->ki_pos, 0);
  686. ret = generic_file_direct_write(iocb, iovp,
  687. &nr_segs, pos, &iocb->ki_pos, count, ocount);
  688. /* No fallback to buffered IO on errors for XFS. */
  689. ASSERT(ret < 0 || ret == count);
  690. return ret;
  691. }
  692. STATIC ssize_t
  693. xfs_file_buffered_aio_write(
  694. struct kiocb *iocb,
  695. const struct iovec *iovp,
  696. unsigned long nr_segs,
  697. loff_t pos,
  698. size_t ocount,
  699. int *iolock)
  700. {
  701. struct file *file = iocb->ki_filp;
  702. struct address_space *mapping = file->f_mapping;
  703. struct inode *inode = mapping->host;
  704. struct xfs_inode *ip = XFS_I(inode);
  705. ssize_t ret;
  706. int enospc = 0;
  707. size_t count = ocount;
  708. *iolock = XFS_IOLOCK_EXCL;
  709. xfs_rw_ilock(ip, XFS_ILOCK_EXCL | *iolock);
  710. ret = xfs_file_aio_write_checks(file, &pos, &count, iolock);
  711. if (ret)
  712. return ret;
  713. /* We can write back this queue in page reclaim */
  714. current->backing_dev_info = mapping->backing_dev_info;
  715. write_retry:
  716. trace_xfs_file_buffered_write(ip, count, iocb->ki_pos, 0);
  717. ret = generic_file_buffered_write(iocb, iovp, nr_segs,
  718. pos, &iocb->ki_pos, count, ret);
  719. /*
  720. * if we just got an ENOSPC, flush the inode now we aren't holding any
  721. * page locks and retry *once*
  722. */
  723. if (ret == -ENOSPC && !enospc) {
  724. ret = -xfs_flush_pages(ip, 0, -1, 0, FI_NONE);
  725. if (ret)
  726. return ret;
  727. enospc = 1;
  728. goto write_retry;
  729. }
  730. current->backing_dev_info = NULL;
  731. return ret;
  732. }
  733. STATIC ssize_t
  734. xfs_file_aio_write(
  735. struct kiocb *iocb,
  736. const struct iovec *iovp,
  737. unsigned long nr_segs,
  738. loff_t pos)
  739. {
  740. struct file *file = iocb->ki_filp;
  741. struct address_space *mapping = file->f_mapping;
  742. struct inode *inode = mapping->host;
  743. struct xfs_inode *ip = XFS_I(inode);
  744. ssize_t ret;
  745. int iolock;
  746. size_t ocount = 0;
  747. XFS_STATS_INC(xs_write_calls);
  748. BUG_ON(iocb->ki_pos != pos);
  749. ret = generic_segment_checks(iovp, &nr_segs, &ocount, VERIFY_READ);
  750. if (ret)
  751. return ret;
  752. if (ocount == 0)
  753. return 0;
  754. xfs_wait_for_freeze(ip->i_mount, SB_FREEZE_WRITE);
  755. if (XFS_FORCED_SHUTDOWN(ip->i_mount))
  756. return -EIO;
  757. if (unlikely(file->f_flags & O_DIRECT))
  758. ret = xfs_file_dio_aio_write(iocb, iovp, nr_segs, pos,
  759. ocount, &iolock);
  760. else
  761. ret = xfs_file_buffered_aio_write(iocb, iovp, nr_segs, pos,
  762. ocount, &iolock);
  763. xfs_aio_write_isize_update(inode, &iocb->ki_pos, ret);
  764. if (ret <= 0)
  765. goto out_unlock;
  766. /* Handle various SYNC-type writes */
  767. if ((file->f_flags & O_DSYNC) || IS_SYNC(inode)) {
  768. loff_t end = pos + ret - 1;
  769. int error;
  770. xfs_rw_iunlock(ip, iolock);
  771. error = xfs_file_fsync(file, pos, end,
  772. (file->f_flags & __O_SYNC) ? 0 : 1);
  773. xfs_rw_ilock(ip, iolock);
  774. if (error)
  775. ret = error;
  776. }
  777. out_unlock:
  778. xfs_aio_write_newsize_update(ip);
  779. xfs_rw_iunlock(ip, iolock);
  780. return ret;
  781. }
  782. STATIC long
  783. xfs_file_fallocate(
  784. struct file *file,
  785. int mode,
  786. loff_t offset,
  787. loff_t len)
  788. {
  789. struct inode *inode = file->f_path.dentry->d_inode;
  790. long error;
  791. loff_t new_size = 0;
  792. xfs_flock64_t bf;
  793. xfs_inode_t *ip = XFS_I(inode);
  794. int cmd = XFS_IOC_RESVSP;
  795. int attr_flags = XFS_ATTR_NOLOCK;
  796. if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
  797. return -EOPNOTSUPP;
  798. bf.l_whence = 0;
  799. bf.l_start = offset;
  800. bf.l_len = len;
  801. xfs_ilock(ip, XFS_IOLOCK_EXCL);
  802. if (mode & FALLOC_FL_PUNCH_HOLE)
  803. cmd = XFS_IOC_UNRESVSP;
  804. /* check the new inode size is valid before allocating */
  805. if (!(mode & FALLOC_FL_KEEP_SIZE) &&
  806. offset + len > i_size_read(inode)) {
  807. new_size = offset + len;
  808. error = inode_newsize_ok(inode, new_size);
  809. if (error)
  810. goto out_unlock;
  811. }
  812. if (file->f_flags & O_DSYNC)
  813. attr_flags |= XFS_ATTR_SYNC;
  814. error = -xfs_change_file_space(ip, cmd, &bf, 0, attr_flags);
  815. if (error)
  816. goto out_unlock;
  817. /* Change file size if needed */
  818. if (new_size) {
  819. struct iattr iattr;
  820. iattr.ia_valid = ATTR_SIZE;
  821. iattr.ia_size = new_size;
  822. error = -xfs_setattr_size(ip, &iattr, XFS_ATTR_NOLOCK);
  823. }
  824. out_unlock:
  825. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  826. return error;
  827. }
  828. STATIC int
  829. xfs_file_open(
  830. struct inode *inode,
  831. struct file *file)
  832. {
  833. if (!(file->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS)
  834. return -EFBIG;
  835. if (XFS_FORCED_SHUTDOWN(XFS_M(inode->i_sb)))
  836. return -EIO;
  837. return 0;
  838. }
  839. STATIC int
  840. xfs_dir_open(
  841. struct inode *inode,
  842. struct file *file)
  843. {
  844. struct xfs_inode *ip = XFS_I(inode);
  845. int mode;
  846. int error;
  847. error = xfs_file_open(inode, file);
  848. if (error)
  849. return error;
  850. /*
  851. * If there are any blocks, read-ahead block 0 as we're almost
  852. * certain to have the next operation be a read there.
  853. */
  854. mode = xfs_ilock_map_shared(ip);
  855. if (ip->i_d.di_nextents > 0)
  856. xfs_da_reada_buf(NULL, ip, 0, XFS_DATA_FORK);
  857. xfs_iunlock(ip, mode);
  858. return 0;
  859. }
  860. STATIC int
  861. xfs_file_release(
  862. struct inode *inode,
  863. struct file *filp)
  864. {
  865. return -xfs_release(XFS_I(inode));
  866. }
  867. STATIC int
  868. xfs_file_readdir(
  869. struct file *filp,
  870. void *dirent,
  871. filldir_t filldir)
  872. {
  873. struct inode *inode = filp->f_path.dentry->d_inode;
  874. xfs_inode_t *ip = XFS_I(inode);
  875. int error;
  876. size_t bufsize;
  877. /*
  878. * The Linux API doesn't pass down the total size of the buffer
  879. * we read into down to the filesystem. With the filldir concept
  880. * it's not needed for correct information, but the XFS dir2 leaf
  881. * code wants an estimate of the buffer size to calculate it's
  882. * readahead window and size the buffers used for mapping to
  883. * physical blocks.
  884. *
  885. * Try to give it an estimate that's good enough, maybe at some
  886. * point we can change the ->readdir prototype to include the
  887. * buffer size. For now we use the current glibc buffer size.
  888. */
  889. bufsize = (size_t)min_t(loff_t, 32768, ip->i_d.di_size);
  890. error = xfs_readdir(ip, dirent, bufsize,
  891. (xfs_off_t *)&filp->f_pos, filldir);
  892. if (error)
  893. return -error;
  894. return 0;
  895. }
  896. STATIC int
  897. xfs_file_mmap(
  898. struct file *filp,
  899. struct vm_area_struct *vma)
  900. {
  901. vma->vm_ops = &xfs_file_vm_ops;
  902. vma->vm_flags |= VM_CAN_NONLINEAR;
  903. file_accessed(filp);
  904. return 0;
  905. }
  906. /*
  907. * mmap()d file has taken write protection fault and is being made
  908. * writable. We can set the page state up correctly for a writable
  909. * page, which means we can do correct delalloc accounting (ENOSPC
  910. * checking!) and unwritten extent mapping.
  911. */
  912. STATIC int
  913. xfs_vm_page_mkwrite(
  914. struct vm_area_struct *vma,
  915. struct vm_fault *vmf)
  916. {
  917. return block_page_mkwrite(vma, vmf, xfs_get_blocks);
  918. }
  919. const struct file_operations xfs_file_operations = {
  920. .llseek = generic_file_llseek,
  921. .read = do_sync_read,
  922. .write = do_sync_write,
  923. .aio_read = xfs_file_aio_read,
  924. .aio_write = xfs_file_aio_write,
  925. .splice_read = xfs_file_splice_read,
  926. .splice_write = xfs_file_splice_write,
  927. .unlocked_ioctl = xfs_file_ioctl,
  928. #ifdef CONFIG_COMPAT
  929. .compat_ioctl = xfs_file_compat_ioctl,
  930. #endif
  931. .mmap = xfs_file_mmap,
  932. .open = xfs_file_open,
  933. .release = xfs_file_release,
  934. .fsync = xfs_file_fsync,
  935. .fallocate = xfs_file_fallocate,
  936. };
  937. const struct file_operations xfs_dir_file_operations = {
  938. .open = xfs_dir_open,
  939. .read = generic_read_dir,
  940. .readdir = xfs_file_readdir,
  941. .llseek = generic_file_llseek,
  942. .unlocked_ioctl = xfs_file_ioctl,
  943. #ifdef CONFIG_COMPAT
  944. .compat_ioctl = xfs_file_compat_ioctl,
  945. #endif
  946. .fsync = xfs_file_fsync,
  947. };
  948. static const struct vm_operations_struct xfs_file_vm_ops = {
  949. .fault = filemap_fault,
  950. .page_mkwrite = xfs_vm_page_mkwrite,
  951. };