xfs_file.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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_ioctl32.h"
  40. #include "xfs_vnodeops.h"
  41. #include <linux/dcache.h>
  42. #include <linux/smp_lock.h>
  43. static struct vm_operations_struct xfs_file_vm_ops;
  44. #ifdef CONFIG_XFS_DMAPI
  45. static struct vm_operations_struct xfs_dmapi_file_vm_ops;
  46. #endif
  47. STATIC_INLINE ssize_t
  48. __xfs_file_read(
  49. struct kiocb *iocb,
  50. const struct iovec *iov,
  51. unsigned long nr_segs,
  52. int ioflags,
  53. loff_t pos)
  54. {
  55. struct file *file = iocb->ki_filp;
  56. BUG_ON(iocb->ki_pos != pos);
  57. if (unlikely(file->f_flags & O_DIRECT))
  58. ioflags |= IO_ISDIRECT;
  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_read(
  64. struct kiocb *iocb,
  65. const struct iovec *iov,
  66. unsigned long nr_segs,
  67. loff_t pos)
  68. {
  69. return __xfs_file_read(iocb, iov, nr_segs, IO_ISAIO, pos);
  70. }
  71. STATIC ssize_t
  72. xfs_file_aio_read_invis(
  73. struct kiocb *iocb,
  74. const struct iovec *iov,
  75. unsigned long nr_segs,
  76. loff_t pos)
  77. {
  78. return __xfs_file_read(iocb, iov, nr_segs, IO_ISAIO|IO_INVIS, pos);
  79. }
  80. STATIC_INLINE ssize_t
  81. __xfs_file_write(
  82. struct kiocb *iocb,
  83. const struct iovec *iov,
  84. unsigned long nr_segs,
  85. int ioflags,
  86. loff_t pos)
  87. {
  88. struct file *file = iocb->ki_filp;
  89. BUG_ON(iocb->ki_pos != pos);
  90. if (unlikely(file->f_flags & O_DIRECT))
  91. ioflags |= IO_ISDIRECT;
  92. return xfs_write(XFS_I(file->f_mapping->host), iocb, iov, nr_segs,
  93. &iocb->ki_pos, ioflags);
  94. }
  95. STATIC ssize_t
  96. xfs_file_aio_write(
  97. struct kiocb *iocb,
  98. const struct iovec *iov,
  99. unsigned long nr_segs,
  100. loff_t pos)
  101. {
  102. return __xfs_file_write(iocb, iov, nr_segs, IO_ISAIO, pos);
  103. }
  104. STATIC ssize_t
  105. xfs_file_aio_write_invis(
  106. struct kiocb *iocb,
  107. const struct iovec *iov,
  108. unsigned long nr_segs,
  109. loff_t pos)
  110. {
  111. return __xfs_file_write(iocb, iov, nr_segs, IO_ISAIO|IO_INVIS, pos);
  112. }
  113. STATIC ssize_t
  114. xfs_file_splice_read(
  115. struct file *infilp,
  116. loff_t *ppos,
  117. struct pipe_inode_info *pipe,
  118. size_t len,
  119. unsigned int flags)
  120. {
  121. return xfs_splice_read(XFS_I(infilp->f_path.dentry->d_inode),
  122. infilp, ppos, pipe, len, flags, 0);
  123. }
  124. STATIC ssize_t
  125. xfs_file_splice_read_invis(
  126. struct file *infilp,
  127. loff_t *ppos,
  128. struct pipe_inode_info *pipe,
  129. size_t len,
  130. unsigned int flags)
  131. {
  132. return xfs_splice_read(XFS_I(infilp->f_path.dentry->d_inode),
  133. infilp, ppos, pipe, len, flags, IO_INVIS);
  134. }
  135. STATIC ssize_t
  136. xfs_file_splice_write(
  137. struct pipe_inode_info *pipe,
  138. struct file *outfilp,
  139. loff_t *ppos,
  140. size_t len,
  141. unsigned int flags)
  142. {
  143. return xfs_splice_write(XFS_I(outfilp->f_path.dentry->d_inode),
  144. pipe, outfilp, ppos, len, flags, 0);
  145. }
  146. STATIC ssize_t
  147. xfs_file_splice_write_invis(
  148. struct pipe_inode_info *pipe,
  149. struct file *outfilp,
  150. loff_t *ppos,
  151. size_t len,
  152. unsigned int flags)
  153. {
  154. return xfs_splice_write(XFS_I(outfilp->f_path.dentry->d_inode),
  155. pipe, outfilp, ppos, len, flags, IO_INVIS);
  156. }
  157. STATIC int
  158. xfs_file_open(
  159. struct inode *inode,
  160. struct file *filp)
  161. {
  162. if (!(filp->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS)
  163. return -EFBIG;
  164. return -xfs_open(XFS_I(inode));
  165. }
  166. STATIC int
  167. xfs_file_release(
  168. struct inode *inode,
  169. struct file *filp)
  170. {
  171. return -xfs_release(XFS_I(inode));
  172. }
  173. STATIC int
  174. xfs_file_fsync(
  175. struct file *filp,
  176. struct dentry *dentry,
  177. int datasync)
  178. {
  179. bhv_vnode_t *vp = vn_from_inode(dentry->d_inode);
  180. int flags = FSYNC_WAIT;
  181. if (datasync)
  182. flags |= FSYNC_DATA;
  183. if (VN_TRUNC(vp))
  184. VUNTRUNCATE(vp);
  185. return -xfs_fsync(XFS_I(dentry->d_inode), flags,
  186. (xfs_off_t)0, (xfs_off_t)-1);
  187. }
  188. #ifdef CONFIG_XFS_DMAPI
  189. STATIC int
  190. xfs_vm_fault(
  191. struct vm_area_struct *vma,
  192. struct vm_fault *vmf)
  193. {
  194. struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
  195. bhv_vnode_t *vp = vn_from_inode(inode);
  196. ASSERT_ALWAYS(vp->v_vfsp->vfs_flag & VFS_DMI);
  197. if (XFS_SEND_MMAP(XFS_VFSTOM(vp->v_vfsp), vma, 0))
  198. return VM_FAULT_SIGBUS;
  199. return filemap_fault(vma, vmf);
  200. }
  201. #endif /* CONFIG_XFS_DMAPI */
  202. STATIC int
  203. xfs_file_readdir(
  204. struct file *filp,
  205. void *dirent,
  206. filldir_t filldir)
  207. {
  208. struct inode *inode = filp->f_path.dentry->d_inode;
  209. xfs_inode_t *ip = XFS_I(inode);
  210. int error;
  211. size_t bufsize;
  212. /*
  213. * The Linux API doesn't pass down the total size of the buffer
  214. * we read into down to the filesystem. With the filldir concept
  215. * it's not needed for correct information, but the XFS dir2 leaf
  216. * code wants an estimate of the buffer size to calculate it's
  217. * readahead window and size the buffers used for mapping to
  218. * physical blocks.
  219. *
  220. * Try to give it an estimate that's good enough, maybe at some
  221. * point we can change the ->readdir prototype to include the
  222. * buffer size.
  223. */
  224. bufsize = (size_t)min_t(loff_t, PAGE_SIZE, inode->i_size);
  225. error = xfs_readdir(ip, dirent, bufsize,
  226. (xfs_off_t *)&filp->f_pos, filldir);
  227. if (error)
  228. return -error;
  229. return 0;
  230. }
  231. STATIC int
  232. xfs_file_mmap(
  233. struct file *filp,
  234. struct vm_area_struct *vma)
  235. {
  236. vma->vm_ops = &xfs_file_vm_ops;
  237. vma->vm_flags |= VM_CAN_NONLINEAR;
  238. #ifdef CONFIG_XFS_DMAPI
  239. if (vfs_from_sb(filp->f_path.dentry->d_inode->i_sb)->vfs_flag & VFS_DMI)
  240. vma->vm_ops = &xfs_dmapi_file_vm_ops;
  241. #endif /* CONFIG_XFS_DMAPI */
  242. file_accessed(filp);
  243. return 0;
  244. }
  245. STATIC long
  246. xfs_file_ioctl(
  247. struct file *filp,
  248. unsigned int cmd,
  249. unsigned long p)
  250. {
  251. int error;
  252. struct inode *inode = filp->f_path.dentry->d_inode;
  253. bhv_vnode_t *vp = vn_from_inode(inode);
  254. error = xfs_ioctl(XFS_I(inode), filp, 0, cmd, (void __user *)p);
  255. VMODIFY(vp);
  256. /* NOTE: some of the ioctl's return positive #'s as a
  257. * byte count indicating success, such as
  258. * readlink_by_handle. So we don't "sign flip"
  259. * like most other routines. This means true
  260. * errors need to be returned as a negative value.
  261. */
  262. return error;
  263. }
  264. STATIC long
  265. xfs_file_ioctl_invis(
  266. struct file *filp,
  267. unsigned int cmd,
  268. unsigned long p)
  269. {
  270. int error;
  271. struct inode *inode = filp->f_path.dentry->d_inode;
  272. bhv_vnode_t *vp = vn_from_inode(inode);
  273. error = xfs_ioctl(XFS_I(inode), filp, IO_INVIS, cmd, (void __user *)p);
  274. VMODIFY(vp);
  275. /* NOTE: some of the ioctl's return positive #'s as a
  276. * byte count indicating success, such as
  277. * readlink_by_handle. So we don't "sign flip"
  278. * like most other routines. This means true
  279. * errors need to be returned as a negative value.
  280. */
  281. return error;
  282. }
  283. #ifdef CONFIG_XFS_DMAPI
  284. #ifdef HAVE_VMOP_MPROTECT
  285. STATIC int
  286. xfs_vm_mprotect(
  287. struct vm_area_struct *vma,
  288. unsigned int newflags)
  289. {
  290. struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
  291. bhv_vfs_t *vfsp = vfs_from_sb(inode->i_sb);
  292. int error = 0;
  293. if (vfsp->vfs_flag & VFS_DMI) {
  294. if ((vma->vm_flags & VM_MAYSHARE) &&
  295. (newflags & VM_WRITE) && !(vma->vm_flags & VM_WRITE))
  296. error = XFS_SEND_MMAP(XFS_VFSTOM(vfsp), vma, VM_WRITE);
  297. }
  298. return error;
  299. }
  300. #endif /* HAVE_VMOP_MPROTECT */
  301. #endif /* CONFIG_XFS_DMAPI */
  302. #ifdef HAVE_FOP_OPEN_EXEC
  303. /* If the user is attempting to execute a file that is offline then
  304. * we have to trigger a DMAPI READ event before the file is marked as busy
  305. * otherwise the invisible I/O will not be able to write to the file to bring
  306. * it back online.
  307. */
  308. STATIC int
  309. xfs_file_open_exec(
  310. struct inode *inode)
  311. {
  312. bhv_vfs_t *vfsp = vfs_from_sb(inode->i_sb);
  313. if (unlikely(vfsp->vfs_flag & VFS_DMI)) {
  314. if (DM_EVENT_ENABLED(XFS_I(inode), DM_EVENT_READ)) {
  315. bhv_vnode_t *vp = vn_from_inode(inode);
  316. return -XFS_SEND_DATA(XFS_VFSTOM(vfsp), DM_EVENT_READ,
  317. vp, 0, 0, 0, NULL);
  318. }
  319. }
  320. return 0;
  321. }
  322. #endif /* HAVE_FOP_OPEN_EXEC */
  323. /*
  324. * mmap()d file has taken write protection fault and is being made
  325. * writable. We can set the page state up correctly for a writable
  326. * page, which means we can do correct delalloc accounting (ENOSPC
  327. * checking!) and unwritten extent mapping.
  328. */
  329. STATIC int
  330. xfs_vm_page_mkwrite(
  331. struct vm_area_struct *vma,
  332. struct page *page)
  333. {
  334. return block_page_mkwrite(vma, page, xfs_get_blocks);
  335. }
  336. const struct file_operations xfs_file_operations = {
  337. .llseek = generic_file_llseek,
  338. .read = do_sync_read,
  339. .write = do_sync_write,
  340. .aio_read = xfs_file_aio_read,
  341. .aio_write = xfs_file_aio_write,
  342. .splice_read = xfs_file_splice_read,
  343. .splice_write = xfs_file_splice_write,
  344. .unlocked_ioctl = xfs_file_ioctl,
  345. #ifdef CONFIG_COMPAT
  346. .compat_ioctl = xfs_file_compat_ioctl,
  347. #endif
  348. .mmap = xfs_file_mmap,
  349. .open = xfs_file_open,
  350. .release = xfs_file_release,
  351. .fsync = xfs_file_fsync,
  352. #ifdef HAVE_FOP_OPEN_EXEC
  353. .open_exec = xfs_file_open_exec,
  354. #endif
  355. };
  356. const struct file_operations xfs_invis_file_operations = {
  357. .llseek = generic_file_llseek,
  358. .read = do_sync_read,
  359. .write = do_sync_write,
  360. .aio_read = xfs_file_aio_read_invis,
  361. .aio_write = xfs_file_aio_write_invis,
  362. .splice_read = xfs_file_splice_read_invis,
  363. .splice_write = xfs_file_splice_write_invis,
  364. .unlocked_ioctl = xfs_file_ioctl_invis,
  365. #ifdef CONFIG_COMPAT
  366. .compat_ioctl = xfs_file_compat_invis_ioctl,
  367. #endif
  368. .mmap = xfs_file_mmap,
  369. .open = xfs_file_open,
  370. .release = xfs_file_release,
  371. .fsync = xfs_file_fsync,
  372. };
  373. const struct file_operations xfs_dir_file_operations = {
  374. .read = generic_read_dir,
  375. .readdir = xfs_file_readdir,
  376. .unlocked_ioctl = xfs_file_ioctl,
  377. #ifdef CONFIG_COMPAT
  378. .compat_ioctl = xfs_file_compat_ioctl,
  379. #endif
  380. .fsync = xfs_file_fsync,
  381. };
  382. static struct vm_operations_struct xfs_file_vm_ops = {
  383. .fault = filemap_fault,
  384. .page_mkwrite = xfs_vm_page_mkwrite,
  385. };
  386. #ifdef CONFIG_XFS_DMAPI
  387. static struct vm_operations_struct xfs_dmapi_file_vm_ops = {
  388. .fault = xfs_vm_fault,
  389. .page_mkwrite = xfs_vm_page_mkwrite,
  390. #ifdef HAVE_VMOP_MPROTECT
  391. .mprotect = xfs_vm_mprotect,
  392. #endif
  393. };
  394. #endif /* CONFIG_XFS_DMAPI */