xfs_file.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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_bit.h"
  20. #include "xfs_log.h"
  21. #include "xfs_inum.h"
  22. #include "xfs_sb.h"
  23. #include "xfs_ag.h"
  24. #include "xfs_dir2.h"
  25. #include "xfs_trans.h"
  26. #include "xfs_dmapi.h"
  27. #include "xfs_mount.h"
  28. #include "xfs_bmap_btree.h"
  29. #include "xfs_alloc_btree.h"
  30. #include "xfs_ialloc_btree.h"
  31. #include "xfs_alloc.h"
  32. #include "xfs_btree.h"
  33. #include "xfs_attr_sf.h"
  34. #include "xfs_dir2_sf.h"
  35. #include "xfs_dinode.h"
  36. #include "xfs_inode.h"
  37. #include "xfs_error.h"
  38. #include "xfs_rw.h"
  39. #include "xfs_vnodeops.h"
  40. #include "xfs_da_btree.h"
  41. #include "xfs_ioctl.h"
  42. #include <linux/dcache.h>
  43. #include <linux/smp_lock.h>
  44. static struct vm_operations_struct xfs_file_vm_ops;
  45. STATIC ssize_t
  46. xfs_file_aio_read(
  47. struct kiocb *iocb,
  48. const struct iovec *iov,
  49. unsigned long nr_segs,
  50. loff_t pos)
  51. {
  52. struct file *file = iocb->ki_filp;
  53. int ioflags = IO_ISAIO;
  54. BUG_ON(iocb->ki_pos != pos);
  55. if (unlikely(file->f_flags & O_DIRECT))
  56. ioflags |= IO_ISDIRECT;
  57. if (file->f_mode & FMODE_NOCMTIME)
  58. ioflags |= IO_INVIS;
  59. return xfs_read(XFS_I(file->f_path.dentry->d_inode), iocb, iov,
  60. nr_segs, &iocb->ki_pos, ioflags);
  61. }
  62. STATIC ssize_t
  63. xfs_file_aio_write(
  64. struct kiocb *iocb,
  65. const struct iovec *iov,
  66. unsigned long nr_segs,
  67. loff_t pos)
  68. {
  69. struct file *file = iocb->ki_filp;
  70. int ioflags = IO_ISAIO;
  71. BUG_ON(iocb->ki_pos != pos);
  72. if (unlikely(file->f_flags & O_DIRECT))
  73. ioflags |= IO_ISDIRECT;
  74. if (file->f_mode & FMODE_NOCMTIME)
  75. ioflags |= IO_INVIS;
  76. return xfs_write(XFS_I(file->f_mapping->host), iocb, iov, nr_segs,
  77. &iocb->ki_pos, ioflags);
  78. }
  79. STATIC ssize_t
  80. xfs_file_splice_read(
  81. struct file *infilp,
  82. loff_t *ppos,
  83. struct pipe_inode_info *pipe,
  84. size_t len,
  85. unsigned int flags)
  86. {
  87. int ioflags = 0;
  88. if (infilp->f_mode & FMODE_NOCMTIME)
  89. ioflags |= IO_INVIS;
  90. return xfs_splice_read(XFS_I(infilp->f_path.dentry->d_inode),
  91. infilp, ppos, pipe, len, flags, ioflags);
  92. }
  93. STATIC ssize_t
  94. xfs_file_splice_write(
  95. struct pipe_inode_info *pipe,
  96. struct file *outfilp,
  97. loff_t *ppos,
  98. size_t len,
  99. unsigned int flags)
  100. {
  101. int ioflags = 0;
  102. if (outfilp->f_mode & FMODE_NOCMTIME)
  103. ioflags |= IO_INVIS;
  104. return xfs_splice_write(XFS_I(outfilp->f_path.dentry->d_inode),
  105. pipe, outfilp, ppos, len, flags, ioflags);
  106. }
  107. STATIC int
  108. xfs_file_open(
  109. struct inode *inode,
  110. struct file *file)
  111. {
  112. if (!(file->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS)
  113. return -EFBIG;
  114. if (XFS_FORCED_SHUTDOWN(XFS_M(inode->i_sb)))
  115. return -EIO;
  116. return 0;
  117. }
  118. STATIC int
  119. xfs_dir_open(
  120. struct inode *inode,
  121. struct file *file)
  122. {
  123. struct xfs_inode *ip = XFS_I(inode);
  124. int mode;
  125. int error;
  126. error = xfs_file_open(inode, file);
  127. if (error)
  128. return error;
  129. /*
  130. * If there are any blocks, read-ahead block 0 as we're almost
  131. * certain to have the next operation be a read there.
  132. */
  133. mode = xfs_ilock_map_shared(ip);
  134. if (ip->i_d.di_nextents > 0)
  135. xfs_da_reada_buf(NULL, ip, 0, XFS_DATA_FORK);
  136. xfs_iunlock(ip, mode);
  137. return 0;
  138. }
  139. STATIC int
  140. xfs_file_release(
  141. struct inode *inode,
  142. struct file *filp)
  143. {
  144. return -xfs_release(XFS_I(inode));
  145. }
  146. /*
  147. * We ignore the datasync flag here because a datasync is effectively
  148. * identical to an fsync. That is, datasync implies that we need to write
  149. * only the metadata needed to be able to access the data that is written
  150. * if we crash after the call completes. Hence if we are writing beyond
  151. * EOF we have to log the inode size change as well, which makes it a
  152. * full fsync. If we don't write beyond EOF, the inode core will be
  153. * clean in memory and so we don't need to log the inode, just like
  154. * fsync.
  155. */
  156. STATIC int
  157. xfs_file_fsync(
  158. struct file *filp,
  159. struct dentry *dentry,
  160. int datasync)
  161. {
  162. xfs_iflags_clear(XFS_I(dentry->d_inode), XFS_ITRUNCATED);
  163. return -xfs_fsync(XFS_I(dentry->d_inode));
  164. }
  165. STATIC int
  166. xfs_file_readdir(
  167. struct file *filp,
  168. void *dirent,
  169. filldir_t filldir)
  170. {
  171. struct inode *inode = filp->f_path.dentry->d_inode;
  172. xfs_inode_t *ip = XFS_I(inode);
  173. int error;
  174. size_t bufsize;
  175. /*
  176. * The Linux API doesn't pass down the total size of the buffer
  177. * we read into down to the filesystem. With the filldir concept
  178. * it's not needed for correct information, but the XFS dir2 leaf
  179. * code wants an estimate of the buffer size to calculate it's
  180. * readahead window and size the buffers used for mapping to
  181. * physical blocks.
  182. *
  183. * Try to give it an estimate that's good enough, maybe at some
  184. * point we can change the ->readdir prototype to include the
  185. * buffer size.
  186. */
  187. bufsize = (size_t)min_t(loff_t, PAGE_SIZE, ip->i_d.di_size);
  188. error = xfs_readdir(ip, dirent, bufsize,
  189. (xfs_off_t *)&filp->f_pos, filldir);
  190. if (error)
  191. return -error;
  192. return 0;
  193. }
  194. STATIC int
  195. xfs_file_mmap(
  196. struct file *filp,
  197. struct vm_area_struct *vma)
  198. {
  199. vma->vm_ops = &xfs_file_vm_ops;
  200. vma->vm_flags |= VM_CAN_NONLINEAR;
  201. file_accessed(filp);
  202. return 0;
  203. }
  204. /*
  205. * mmap()d file has taken write protection fault and is being made
  206. * writable. We can set the page state up correctly for a writable
  207. * page, which means we can do correct delalloc accounting (ENOSPC
  208. * checking!) and unwritten extent mapping.
  209. */
  210. STATIC int
  211. xfs_vm_page_mkwrite(
  212. struct vm_area_struct *vma,
  213. struct vm_fault *vmf)
  214. {
  215. return block_page_mkwrite(vma, vmf, xfs_get_blocks);
  216. }
  217. const struct file_operations xfs_file_operations = {
  218. .llseek = generic_file_llseek,
  219. .read = do_sync_read,
  220. .write = do_sync_write,
  221. .aio_read = xfs_file_aio_read,
  222. .aio_write = xfs_file_aio_write,
  223. .splice_read = xfs_file_splice_read,
  224. .splice_write = xfs_file_splice_write,
  225. .unlocked_ioctl = xfs_file_ioctl,
  226. #ifdef CONFIG_COMPAT
  227. .compat_ioctl = xfs_file_compat_ioctl,
  228. #endif
  229. .mmap = xfs_file_mmap,
  230. .open = xfs_file_open,
  231. .release = xfs_file_release,
  232. .fsync = xfs_file_fsync,
  233. #ifdef HAVE_FOP_OPEN_EXEC
  234. .open_exec = xfs_file_open_exec,
  235. #endif
  236. };
  237. const struct file_operations xfs_dir_file_operations = {
  238. .open = xfs_dir_open,
  239. .read = generic_read_dir,
  240. .readdir = xfs_file_readdir,
  241. .llseek = generic_file_llseek,
  242. .unlocked_ioctl = xfs_file_ioctl,
  243. #ifdef CONFIG_COMPAT
  244. .compat_ioctl = xfs_file_compat_ioctl,
  245. #endif
  246. .fsync = xfs_file_fsync,
  247. };
  248. static struct vm_operations_struct xfs_file_vm_ops = {
  249. .fault = filemap_fault,
  250. .page_mkwrite = xfs_vm_page_mkwrite,
  251. };