xfs_file.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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. int flags = FSYNC_WAIT;
  180. if (datasync)
  181. flags |= FSYNC_DATA;
  182. xfs_iflags_clear(XFS_I(dentry->d_inode), XFS_ITRUNCATED);
  183. return -xfs_fsync(XFS_I(dentry->d_inode), flags,
  184. (xfs_off_t)0, (xfs_off_t)-1);
  185. }
  186. #ifdef CONFIG_XFS_DMAPI
  187. STATIC int
  188. xfs_vm_fault(
  189. struct vm_area_struct *vma,
  190. struct vm_fault *vmf)
  191. {
  192. struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
  193. bhv_vnode_t *vp = vn_from_inode(inode);
  194. ASSERT_ALWAYS(vp->v_vfsp->vfs_flag & VFS_DMI);
  195. if (XFS_SEND_MMAP(XFS_VFSTOM(vp->v_vfsp), vma, 0))
  196. return VM_FAULT_SIGBUS;
  197. return filemap_fault(vma, vmf);
  198. }
  199. #endif /* CONFIG_XFS_DMAPI */
  200. STATIC int
  201. xfs_file_readdir(
  202. struct file *filp,
  203. void *dirent,
  204. filldir_t filldir)
  205. {
  206. struct inode *inode = filp->f_path.dentry->d_inode;
  207. xfs_inode_t *ip = XFS_I(inode);
  208. int error;
  209. size_t bufsize;
  210. /*
  211. * The Linux API doesn't pass down the total size of the buffer
  212. * we read into down to the filesystem. With the filldir concept
  213. * it's not needed for correct information, but the XFS dir2 leaf
  214. * code wants an estimate of the buffer size to calculate it's
  215. * readahead window and size the buffers used for mapping to
  216. * physical blocks.
  217. *
  218. * Try to give it an estimate that's good enough, maybe at some
  219. * point we can change the ->readdir prototype to include the
  220. * buffer size.
  221. */
  222. bufsize = (size_t)min_t(loff_t, PAGE_SIZE, inode->i_size);
  223. error = xfs_readdir(ip, dirent, bufsize,
  224. (xfs_off_t *)&filp->f_pos, filldir);
  225. if (error)
  226. return -error;
  227. return 0;
  228. }
  229. STATIC int
  230. xfs_file_mmap(
  231. struct file *filp,
  232. struct vm_area_struct *vma)
  233. {
  234. vma->vm_ops = &xfs_file_vm_ops;
  235. vma->vm_flags |= VM_CAN_NONLINEAR;
  236. #ifdef CONFIG_XFS_DMAPI
  237. if (XFS_M(filp->f_path.dentry->d_inode->i_sb)->m_flags & XFS_MOUNT_DMAPI)
  238. vma->vm_ops = &xfs_dmapi_file_vm_ops;
  239. #endif /* CONFIG_XFS_DMAPI */
  240. file_accessed(filp);
  241. return 0;
  242. }
  243. STATIC long
  244. xfs_file_ioctl(
  245. struct file *filp,
  246. unsigned int cmd,
  247. unsigned long p)
  248. {
  249. int error;
  250. struct inode *inode = filp->f_path.dentry->d_inode;
  251. error = xfs_ioctl(XFS_I(inode), filp, 0, cmd, (void __user *)p);
  252. xfs_iflags_set(XFS_I(inode), XFS_IMODIFIED);
  253. /* NOTE: some of the ioctl's return positive #'s as a
  254. * byte count indicating success, such as
  255. * readlink_by_handle. So we don't "sign flip"
  256. * like most other routines. This means true
  257. * errors need to be returned as a negative value.
  258. */
  259. return error;
  260. }
  261. STATIC long
  262. xfs_file_ioctl_invis(
  263. struct file *filp,
  264. unsigned int cmd,
  265. unsigned long p)
  266. {
  267. int error;
  268. struct inode *inode = filp->f_path.dentry->d_inode;
  269. error = xfs_ioctl(XFS_I(inode), filp, IO_INVIS, cmd, (void __user *)p);
  270. xfs_iflags_set(XFS_I(inode), XFS_IMODIFIED);
  271. /* NOTE: some of the ioctl's return positive #'s as a
  272. * byte count indicating success, such as
  273. * readlink_by_handle. So we don't "sign flip"
  274. * like most other routines. This means true
  275. * errors need to be returned as a negative value.
  276. */
  277. return error;
  278. }
  279. #ifdef CONFIG_XFS_DMAPI
  280. #ifdef HAVE_VMOP_MPROTECT
  281. STATIC int
  282. xfs_vm_mprotect(
  283. struct vm_area_struct *vma,
  284. unsigned int newflags)
  285. {
  286. struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
  287. struct xfs_mount *mp = XFS_M(inode->i_sb);
  288. int error = 0;
  289. if (mp->m_flags & XFS_MOUNT_DMAPI) {
  290. if ((vma->vm_flags & VM_MAYSHARE) &&
  291. (newflags & VM_WRITE) && !(vma->vm_flags & VM_WRITE))
  292. error = XFS_SEND_MMAP(mp, vma, VM_WRITE);
  293. }
  294. return error;
  295. }
  296. #endif /* HAVE_VMOP_MPROTECT */
  297. #endif /* CONFIG_XFS_DMAPI */
  298. #ifdef HAVE_FOP_OPEN_EXEC
  299. /* If the user is attempting to execute a file that is offline then
  300. * we have to trigger a DMAPI READ event before the file is marked as busy
  301. * otherwise the invisible I/O will not be able to write to the file to bring
  302. * it back online.
  303. */
  304. STATIC int
  305. xfs_file_open_exec(
  306. struct inode *inode)
  307. {
  308. struct xfs_mount *mp = XFS_M(inode->i_sb);
  309. if (unlikely(mp->m_flags & XFS_MOUNT_DMAPI)) {
  310. if (DM_EVENT_ENABLED(XFS_I(inode), DM_EVENT_READ)) {
  311. bhv_vnode_t *vp = vn_from_inode(inode);
  312. return -XFS_SEND_DATA(mp, DM_EVENT_READ,
  313. vp, 0, 0, 0, NULL);
  314. }
  315. }
  316. return 0;
  317. }
  318. #endif /* HAVE_FOP_OPEN_EXEC */
  319. /*
  320. * mmap()d file has taken write protection fault and is being made
  321. * writable. We can set the page state up correctly for a writable
  322. * page, which means we can do correct delalloc accounting (ENOSPC
  323. * checking!) and unwritten extent mapping.
  324. */
  325. STATIC int
  326. xfs_vm_page_mkwrite(
  327. struct vm_area_struct *vma,
  328. struct page *page)
  329. {
  330. return block_page_mkwrite(vma, page, xfs_get_blocks);
  331. }
  332. const struct file_operations xfs_file_operations = {
  333. .llseek = generic_file_llseek,
  334. .read = do_sync_read,
  335. .write = do_sync_write,
  336. .aio_read = xfs_file_aio_read,
  337. .aio_write = xfs_file_aio_write,
  338. .splice_read = xfs_file_splice_read,
  339. .splice_write = xfs_file_splice_write,
  340. .unlocked_ioctl = xfs_file_ioctl,
  341. #ifdef CONFIG_COMPAT
  342. .compat_ioctl = xfs_file_compat_ioctl,
  343. #endif
  344. .mmap = xfs_file_mmap,
  345. .open = xfs_file_open,
  346. .release = xfs_file_release,
  347. .fsync = xfs_file_fsync,
  348. #ifdef HAVE_FOP_OPEN_EXEC
  349. .open_exec = xfs_file_open_exec,
  350. #endif
  351. };
  352. const struct file_operations xfs_invis_file_operations = {
  353. .llseek = generic_file_llseek,
  354. .read = do_sync_read,
  355. .write = do_sync_write,
  356. .aio_read = xfs_file_aio_read_invis,
  357. .aio_write = xfs_file_aio_write_invis,
  358. .splice_read = xfs_file_splice_read_invis,
  359. .splice_write = xfs_file_splice_write_invis,
  360. .unlocked_ioctl = xfs_file_ioctl_invis,
  361. #ifdef CONFIG_COMPAT
  362. .compat_ioctl = xfs_file_compat_invis_ioctl,
  363. #endif
  364. .mmap = xfs_file_mmap,
  365. .open = xfs_file_open,
  366. .release = xfs_file_release,
  367. .fsync = xfs_file_fsync,
  368. };
  369. const struct file_operations xfs_dir_file_operations = {
  370. .read = generic_read_dir,
  371. .readdir = xfs_file_readdir,
  372. .unlocked_ioctl = xfs_file_ioctl,
  373. #ifdef CONFIG_COMPAT
  374. .compat_ioctl = xfs_file_compat_ioctl,
  375. #endif
  376. .fsync = xfs_file_fsync,
  377. };
  378. static struct vm_operations_struct xfs_file_vm_ops = {
  379. .fault = filemap_fault,
  380. .page_mkwrite = xfs_vm_page_mkwrite,
  381. };
  382. #ifdef CONFIG_XFS_DMAPI
  383. static struct vm_operations_struct xfs_dmapi_file_vm_ops = {
  384. .fault = xfs_vm_fault,
  385. .page_mkwrite = xfs_vm_page_mkwrite,
  386. #ifdef HAVE_VMOP_MPROTECT
  387. .mprotect = xfs_vm_mprotect,
  388. #endif
  389. };
  390. #endif /* CONFIG_XFS_DMAPI */