xfs_file.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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 <linux/dcache.h>
  41. #include <linux/smp_lock.h>
  42. static struct vm_operations_struct xfs_file_vm_ops;
  43. #ifdef CONFIG_XFS_DMAPI
  44. static struct vm_operations_struct xfs_dmapi_file_vm_ops;
  45. #endif
  46. STATIC_INLINE ssize_t
  47. __xfs_file_read(
  48. struct kiocb *iocb,
  49. const struct iovec *iov,
  50. unsigned long nr_segs,
  51. int ioflags,
  52. loff_t pos)
  53. {
  54. struct file *file = iocb->ki_filp;
  55. bhv_vnode_t *vp = vn_from_inode(file->f_path.dentry->d_inode);
  56. BUG_ON(iocb->ki_pos != pos);
  57. if (unlikely(file->f_flags & O_DIRECT))
  58. ioflags |= IO_ISDIRECT;
  59. return bhv_vop_read(vp, iocb, iov, nr_segs, &iocb->ki_pos,
  60. ioflags, NULL);
  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. struct inode *inode = file->f_mapping->host;
  90. bhv_vnode_t *vp = vn_from_inode(inode);
  91. BUG_ON(iocb->ki_pos != pos);
  92. if (unlikely(file->f_flags & O_DIRECT))
  93. ioflags |= IO_ISDIRECT;
  94. return bhv_vop_write(vp, iocb, iov, nr_segs, &iocb->ki_pos,
  95. ioflags, NULL);
  96. }
  97. STATIC ssize_t
  98. xfs_file_aio_write(
  99. struct kiocb *iocb,
  100. const struct iovec *iov,
  101. unsigned long nr_segs,
  102. loff_t pos)
  103. {
  104. return __xfs_file_write(iocb, iov, nr_segs, IO_ISAIO, pos);
  105. }
  106. STATIC ssize_t
  107. xfs_file_aio_write_invis(
  108. struct kiocb *iocb,
  109. const struct iovec *iov,
  110. unsigned long nr_segs,
  111. loff_t pos)
  112. {
  113. return __xfs_file_write(iocb, iov, nr_segs, IO_ISAIO|IO_INVIS, pos);
  114. }
  115. STATIC ssize_t
  116. xfs_file_splice_read(
  117. struct file *infilp,
  118. loff_t *ppos,
  119. struct pipe_inode_info *pipe,
  120. size_t len,
  121. unsigned int flags)
  122. {
  123. return bhv_vop_splice_read(vn_from_inode(infilp->f_path.dentry->d_inode),
  124. infilp, ppos, pipe, len, flags, 0, NULL);
  125. }
  126. STATIC ssize_t
  127. xfs_file_splice_read_invis(
  128. struct file *infilp,
  129. loff_t *ppos,
  130. struct pipe_inode_info *pipe,
  131. size_t len,
  132. unsigned int flags)
  133. {
  134. return bhv_vop_splice_read(vn_from_inode(infilp->f_path.dentry->d_inode),
  135. infilp, ppos, pipe, len, flags, IO_INVIS,
  136. NULL);
  137. }
  138. STATIC ssize_t
  139. xfs_file_splice_write(
  140. struct pipe_inode_info *pipe,
  141. struct file *outfilp,
  142. loff_t *ppos,
  143. size_t len,
  144. unsigned int flags)
  145. {
  146. return bhv_vop_splice_write(vn_from_inode(outfilp->f_path.dentry->d_inode),
  147. pipe, outfilp, ppos, len, flags, 0, NULL);
  148. }
  149. STATIC ssize_t
  150. xfs_file_splice_write_invis(
  151. struct pipe_inode_info *pipe,
  152. struct file *outfilp,
  153. loff_t *ppos,
  154. size_t len,
  155. unsigned int flags)
  156. {
  157. return bhv_vop_splice_write(vn_from_inode(outfilp->f_path.dentry->d_inode),
  158. pipe, outfilp, ppos, len, flags, IO_INVIS,
  159. NULL);
  160. }
  161. STATIC int
  162. xfs_file_open(
  163. struct inode *inode,
  164. struct file *filp)
  165. {
  166. if (!(filp->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS)
  167. return -EFBIG;
  168. return -bhv_vop_open(vn_from_inode(inode), NULL);
  169. }
  170. STATIC int
  171. xfs_file_release(
  172. struct inode *inode,
  173. struct file *filp)
  174. {
  175. bhv_vnode_t *vp = vn_from_inode(inode);
  176. if (vp)
  177. return -bhv_vop_release(vp);
  178. return 0;
  179. }
  180. STATIC int
  181. xfs_file_fsync(
  182. struct file *filp,
  183. struct dentry *dentry,
  184. int datasync)
  185. {
  186. bhv_vnode_t *vp = vn_from_inode(dentry->d_inode);
  187. int flags = FSYNC_WAIT;
  188. if (datasync)
  189. flags |= FSYNC_DATA;
  190. if (VN_TRUNC(vp))
  191. VUNTRUNCATE(vp);
  192. return -bhv_vop_fsync(vp, flags, NULL, (xfs_off_t)0, (xfs_off_t)-1);
  193. }
  194. #ifdef CONFIG_XFS_DMAPI
  195. STATIC struct page *
  196. xfs_vm_nopage(
  197. struct vm_area_struct *area,
  198. unsigned long address,
  199. int *type)
  200. {
  201. struct inode *inode = area->vm_file->f_path.dentry->d_inode;
  202. bhv_vnode_t *vp = vn_from_inode(inode);
  203. ASSERT_ALWAYS(vp->v_vfsp->vfs_flag & VFS_DMI);
  204. if (XFS_SEND_MMAP(XFS_VFSTOM(vp->v_vfsp), area, 0))
  205. return NULL;
  206. return filemap_nopage(area, address, type);
  207. }
  208. #endif /* CONFIG_XFS_DMAPI */
  209. STATIC int
  210. xfs_file_readdir(
  211. struct file *filp,
  212. void *dirent,
  213. filldir_t filldir)
  214. {
  215. int error = 0;
  216. bhv_vnode_t *vp = vn_from_inode(filp->f_path.dentry->d_inode);
  217. uio_t uio;
  218. iovec_t iov;
  219. int eof = 0;
  220. caddr_t read_buf;
  221. int namelen, size = 0;
  222. size_t rlen = PAGE_CACHE_SIZE;
  223. xfs_off_t start_offset, curr_offset;
  224. xfs_dirent_t *dbp = NULL;
  225. /* Try fairly hard to get memory */
  226. do {
  227. if ((read_buf = kmalloc(rlen, GFP_KERNEL)))
  228. break;
  229. rlen >>= 1;
  230. } while (rlen >= 1024);
  231. if (read_buf == NULL)
  232. return -ENOMEM;
  233. uio.uio_iov = &iov;
  234. uio.uio_segflg = UIO_SYSSPACE;
  235. curr_offset = filp->f_pos;
  236. if (filp->f_pos != 0x7fffffff)
  237. uio.uio_offset = filp->f_pos;
  238. else
  239. uio.uio_offset = 0xffffffff;
  240. while (!eof) {
  241. uio.uio_resid = iov.iov_len = rlen;
  242. iov.iov_base = read_buf;
  243. uio.uio_iovcnt = 1;
  244. start_offset = uio.uio_offset;
  245. error = bhv_vop_readdir(vp, &uio, NULL, &eof);
  246. if ((uio.uio_offset == start_offset) || error) {
  247. size = 0;
  248. break;
  249. }
  250. size = rlen - uio.uio_resid;
  251. dbp = (xfs_dirent_t *)read_buf;
  252. while (size > 0) {
  253. namelen = strlen(dbp->d_name);
  254. if (filldir(dirent, dbp->d_name, namelen,
  255. (loff_t) curr_offset & 0x7fffffff,
  256. (ino_t) dbp->d_ino,
  257. DT_UNKNOWN)) {
  258. goto done;
  259. }
  260. size -= dbp->d_reclen;
  261. curr_offset = (loff_t)dbp->d_off /* & 0x7fffffff */;
  262. dbp = (xfs_dirent_t *)((char *)dbp + dbp->d_reclen);
  263. }
  264. }
  265. done:
  266. if (!error) {
  267. if (size == 0)
  268. filp->f_pos = uio.uio_offset & 0x7fffffff;
  269. else if (dbp)
  270. filp->f_pos = curr_offset;
  271. }
  272. kfree(read_buf);
  273. return -error;
  274. }
  275. STATIC int
  276. xfs_file_mmap(
  277. struct file *filp,
  278. struct vm_area_struct *vma)
  279. {
  280. vma->vm_ops = &xfs_file_vm_ops;
  281. vma->vm_flags |= VM_CAN_INVALIDATE;
  282. #ifdef CONFIG_XFS_DMAPI
  283. if (vn_from_inode(filp->f_path.dentry->d_inode)->v_vfsp->vfs_flag & VFS_DMI)
  284. vma->vm_ops = &xfs_dmapi_file_vm_ops;
  285. #endif /* CONFIG_XFS_DMAPI */
  286. file_accessed(filp);
  287. return 0;
  288. }
  289. STATIC long
  290. xfs_file_ioctl(
  291. struct file *filp,
  292. unsigned int cmd,
  293. unsigned long p)
  294. {
  295. int error;
  296. struct inode *inode = filp->f_path.dentry->d_inode;
  297. bhv_vnode_t *vp = vn_from_inode(inode);
  298. error = bhv_vop_ioctl(vp, inode, filp, 0, cmd, (void __user *)p);
  299. VMODIFY(vp);
  300. /* NOTE: some of the ioctl's return positive #'s as a
  301. * byte count indicating success, such as
  302. * readlink_by_handle. So we don't "sign flip"
  303. * like most other routines. This means true
  304. * errors need to be returned as a negative value.
  305. */
  306. return error;
  307. }
  308. STATIC long
  309. xfs_file_ioctl_invis(
  310. struct file *filp,
  311. unsigned int cmd,
  312. unsigned long p)
  313. {
  314. int error;
  315. struct inode *inode = filp->f_path.dentry->d_inode;
  316. bhv_vnode_t *vp = vn_from_inode(inode);
  317. error = bhv_vop_ioctl(vp, inode, filp, IO_INVIS, cmd, (void __user *)p);
  318. VMODIFY(vp);
  319. /* NOTE: some of the ioctl's return positive #'s as a
  320. * byte count indicating success, such as
  321. * readlink_by_handle. So we don't "sign flip"
  322. * like most other routines. This means true
  323. * errors need to be returned as a negative value.
  324. */
  325. return error;
  326. }
  327. #ifdef CONFIG_XFS_DMAPI
  328. #ifdef HAVE_VMOP_MPROTECT
  329. STATIC int
  330. xfs_vm_mprotect(
  331. struct vm_area_struct *vma,
  332. unsigned int newflags)
  333. {
  334. bhv_vnode_t *vp = vn_from_inode(vma->vm_file->f_path.dentry->d_inode);
  335. int error = 0;
  336. if (vp->v_vfsp->vfs_flag & VFS_DMI) {
  337. if ((vma->vm_flags & VM_MAYSHARE) &&
  338. (newflags & VM_WRITE) && !(vma->vm_flags & VM_WRITE)) {
  339. xfs_mount_t *mp = XFS_VFSTOM(vp->v_vfsp);
  340. error = XFS_SEND_MMAP(mp, vma, VM_WRITE);
  341. }
  342. }
  343. return error;
  344. }
  345. #endif /* HAVE_VMOP_MPROTECT */
  346. #endif /* CONFIG_XFS_DMAPI */
  347. #ifdef HAVE_FOP_OPEN_EXEC
  348. /* If the user is attempting to execute a file that is offline then
  349. * we have to trigger a DMAPI READ event before the file is marked as busy
  350. * otherwise the invisible I/O will not be able to write to the file to bring
  351. * it back online.
  352. */
  353. STATIC int
  354. xfs_file_open_exec(
  355. struct inode *inode)
  356. {
  357. bhv_vnode_t *vp = vn_from_inode(inode);
  358. if (unlikely(vp->v_vfsp->vfs_flag & VFS_DMI)) {
  359. xfs_mount_t *mp = XFS_VFSTOM(vp->v_vfsp);
  360. xfs_inode_t *ip = xfs_vtoi(vp);
  361. if (!ip)
  362. return -EINVAL;
  363. if (DM_EVENT_ENABLED(vp->v_vfsp, ip, DM_EVENT_READ))
  364. return -XFS_SEND_DATA(mp, DM_EVENT_READ, vp,
  365. 0, 0, 0, NULL);
  366. }
  367. return 0;
  368. }
  369. #endif /* HAVE_FOP_OPEN_EXEC */
  370. const struct file_operations xfs_file_operations = {
  371. .llseek = generic_file_llseek,
  372. .read = do_sync_read,
  373. .write = do_sync_write,
  374. .aio_read = xfs_file_aio_read,
  375. .aio_write = xfs_file_aio_write,
  376. .splice_read = xfs_file_splice_read,
  377. .splice_write = xfs_file_splice_write,
  378. .unlocked_ioctl = xfs_file_ioctl,
  379. #ifdef CONFIG_COMPAT
  380. .compat_ioctl = xfs_file_compat_ioctl,
  381. #endif
  382. .mmap = xfs_file_mmap,
  383. .open = xfs_file_open,
  384. .release = xfs_file_release,
  385. .fsync = xfs_file_fsync,
  386. #ifdef HAVE_FOP_OPEN_EXEC
  387. .open_exec = xfs_file_open_exec,
  388. #endif
  389. };
  390. const struct file_operations xfs_invis_file_operations = {
  391. .llseek = generic_file_llseek,
  392. .read = do_sync_read,
  393. .write = do_sync_write,
  394. .aio_read = xfs_file_aio_read_invis,
  395. .aio_write = xfs_file_aio_write_invis,
  396. .splice_read = xfs_file_splice_read_invis,
  397. .splice_write = xfs_file_splice_write_invis,
  398. .unlocked_ioctl = xfs_file_ioctl_invis,
  399. #ifdef CONFIG_COMPAT
  400. .compat_ioctl = xfs_file_compat_invis_ioctl,
  401. #endif
  402. .mmap = xfs_file_mmap,
  403. .open = xfs_file_open,
  404. .release = xfs_file_release,
  405. .fsync = xfs_file_fsync,
  406. };
  407. const struct file_operations xfs_dir_file_operations = {
  408. .read = generic_read_dir,
  409. .readdir = xfs_file_readdir,
  410. .unlocked_ioctl = xfs_file_ioctl,
  411. #ifdef CONFIG_COMPAT
  412. .compat_ioctl = xfs_file_compat_ioctl,
  413. #endif
  414. .fsync = xfs_file_fsync,
  415. };
  416. static struct vm_operations_struct xfs_file_vm_ops = {
  417. .nopage = filemap_nopage,
  418. .populate = filemap_populate,
  419. };
  420. #ifdef CONFIG_XFS_DMAPI
  421. static struct vm_operations_struct xfs_dmapi_file_vm_ops = {
  422. .nopage = xfs_vm_nopage,
  423. .populate = filemap_populate,
  424. #ifdef HAVE_VMOP_MPROTECT
  425. .mprotect = xfs_vm_mprotect,
  426. #endif
  427. };
  428. #endif /* CONFIG_XFS_DMAPI */