xfs_file.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  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_close(
  172. struct file *filp,
  173. fl_owner_t id)
  174. {
  175. return -bhv_vop_close(vn_from_inode(filp->f_path.dentry->d_inode), 0,
  176. file_count(filp) > 1 ? L_FALSE : L_TRUE, NULL);
  177. }
  178. STATIC int
  179. xfs_file_release(
  180. struct inode *inode,
  181. struct file *filp)
  182. {
  183. bhv_vnode_t *vp = vn_from_inode(inode);
  184. if (vp)
  185. return -bhv_vop_release(vp);
  186. return 0;
  187. }
  188. STATIC int
  189. xfs_file_fsync(
  190. struct file *filp,
  191. struct dentry *dentry,
  192. int datasync)
  193. {
  194. bhv_vnode_t *vp = vn_from_inode(dentry->d_inode);
  195. int flags = FSYNC_WAIT;
  196. if (datasync)
  197. flags |= FSYNC_DATA;
  198. if (VN_TRUNC(vp))
  199. VUNTRUNCATE(vp);
  200. return -bhv_vop_fsync(vp, flags, NULL, (xfs_off_t)0, (xfs_off_t)-1);
  201. }
  202. #ifdef CONFIG_XFS_DMAPI
  203. STATIC struct page *
  204. xfs_vm_nopage(
  205. struct vm_area_struct *area,
  206. unsigned long address,
  207. int *type)
  208. {
  209. struct inode *inode = area->vm_file->f_path.dentry->d_inode;
  210. bhv_vnode_t *vp = vn_from_inode(inode);
  211. ASSERT_ALWAYS(vp->v_vfsp->vfs_flag & VFS_DMI);
  212. if (XFS_SEND_MMAP(XFS_VFSTOM(vp->v_vfsp), area, 0))
  213. return NULL;
  214. return filemap_nopage(area, address, type);
  215. }
  216. #endif /* CONFIG_XFS_DMAPI */
  217. STATIC int
  218. xfs_file_readdir(
  219. struct file *filp,
  220. void *dirent,
  221. filldir_t filldir)
  222. {
  223. int error = 0;
  224. bhv_vnode_t *vp = vn_from_inode(filp->f_path.dentry->d_inode);
  225. uio_t uio;
  226. iovec_t iov;
  227. int eof = 0;
  228. caddr_t read_buf;
  229. int namelen, size = 0;
  230. size_t rlen = PAGE_CACHE_SIZE;
  231. xfs_off_t start_offset, curr_offset;
  232. xfs_dirent_t *dbp = NULL;
  233. /* Try fairly hard to get memory */
  234. do {
  235. if ((read_buf = kmalloc(rlen, GFP_KERNEL)))
  236. break;
  237. rlen >>= 1;
  238. } while (rlen >= 1024);
  239. if (read_buf == NULL)
  240. return -ENOMEM;
  241. uio.uio_iov = &iov;
  242. uio.uio_segflg = UIO_SYSSPACE;
  243. curr_offset = filp->f_pos;
  244. if (filp->f_pos != 0x7fffffff)
  245. uio.uio_offset = filp->f_pos;
  246. else
  247. uio.uio_offset = 0xffffffff;
  248. while (!eof) {
  249. uio.uio_resid = iov.iov_len = rlen;
  250. iov.iov_base = read_buf;
  251. uio.uio_iovcnt = 1;
  252. start_offset = uio.uio_offset;
  253. error = bhv_vop_readdir(vp, &uio, NULL, &eof);
  254. if ((uio.uio_offset == start_offset) || error) {
  255. size = 0;
  256. break;
  257. }
  258. size = rlen - uio.uio_resid;
  259. dbp = (xfs_dirent_t *)read_buf;
  260. while (size > 0) {
  261. namelen = strlen(dbp->d_name);
  262. if (filldir(dirent, dbp->d_name, namelen,
  263. (loff_t) curr_offset & 0x7fffffff,
  264. (ino_t) dbp->d_ino,
  265. DT_UNKNOWN)) {
  266. goto done;
  267. }
  268. size -= dbp->d_reclen;
  269. curr_offset = (loff_t)dbp->d_off /* & 0x7fffffff */;
  270. dbp = (xfs_dirent_t *)((char *)dbp + dbp->d_reclen);
  271. }
  272. }
  273. done:
  274. if (!error) {
  275. if (size == 0)
  276. filp->f_pos = uio.uio_offset & 0x7fffffff;
  277. else if (dbp)
  278. filp->f_pos = curr_offset;
  279. }
  280. kfree(read_buf);
  281. return -error;
  282. }
  283. STATIC int
  284. xfs_file_mmap(
  285. struct file *filp,
  286. struct vm_area_struct *vma)
  287. {
  288. vma->vm_ops = &xfs_file_vm_ops;
  289. #ifdef CONFIG_XFS_DMAPI
  290. if (vn_from_inode(filp->f_path.dentry->d_inode)->v_vfsp->vfs_flag & VFS_DMI)
  291. vma->vm_ops = &xfs_dmapi_file_vm_ops;
  292. #endif /* CONFIG_XFS_DMAPI */
  293. file_accessed(filp);
  294. return 0;
  295. }
  296. STATIC long
  297. xfs_file_ioctl(
  298. struct file *filp,
  299. unsigned int cmd,
  300. unsigned long p)
  301. {
  302. int error;
  303. struct inode *inode = filp->f_path.dentry->d_inode;
  304. bhv_vnode_t *vp = vn_from_inode(inode);
  305. error = bhv_vop_ioctl(vp, inode, filp, 0, cmd, (void __user *)p);
  306. VMODIFY(vp);
  307. /* NOTE: some of the ioctl's return positive #'s as a
  308. * byte count indicating success, such as
  309. * readlink_by_handle. So we don't "sign flip"
  310. * like most other routines. This means true
  311. * errors need to be returned as a negative value.
  312. */
  313. return error;
  314. }
  315. STATIC long
  316. xfs_file_ioctl_invis(
  317. struct file *filp,
  318. unsigned int cmd,
  319. unsigned long p)
  320. {
  321. int error;
  322. struct inode *inode = filp->f_path.dentry->d_inode;
  323. bhv_vnode_t *vp = vn_from_inode(inode);
  324. error = bhv_vop_ioctl(vp, inode, filp, IO_INVIS, cmd, (void __user *)p);
  325. VMODIFY(vp);
  326. /* NOTE: some of the ioctl's return positive #'s as a
  327. * byte count indicating success, such as
  328. * readlink_by_handle. So we don't "sign flip"
  329. * like most other routines. This means true
  330. * errors need to be returned as a negative value.
  331. */
  332. return error;
  333. }
  334. #ifdef CONFIG_XFS_DMAPI
  335. #ifdef HAVE_VMOP_MPROTECT
  336. STATIC int
  337. xfs_vm_mprotect(
  338. struct vm_area_struct *vma,
  339. unsigned int newflags)
  340. {
  341. bhv_vnode_t *vp = vn_from_inode(vma->vm_file->f_path.dentry->d_inode);
  342. int error = 0;
  343. if (vp->v_vfsp->vfs_flag & VFS_DMI) {
  344. if ((vma->vm_flags & VM_MAYSHARE) &&
  345. (newflags & VM_WRITE) && !(vma->vm_flags & VM_WRITE)) {
  346. xfs_mount_t *mp = XFS_VFSTOM(vp->v_vfsp);
  347. error = XFS_SEND_MMAP(mp, vma, VM_WRITE);
  348. }
  349. }
  350. return error;
  351. }
  352. #endif /* HAVE_VMOP_MPROTECT */
  353. #endif /* CONFIG_XFS_DMAPI */
  354. #ifdef HAVE_FOP_OPEN_EXEC
  355. /* If the user is attempting to execute a file that is offline then
  356. * we have to trigger a DMAPI READ event before the file is marked as busy
  357. * otherwise the invisible I/O will not be able to write to the file to bring
  358. * it back online.
  359. */
  360. STATIC int
  361. xfs_file_open_exec(
  362. struct inode *inode)
  363. {
  364. bhv_vnode_t *vp = vn_from_inode(inode);
  365. if (unlikely(vp->v_vfsp->vfs_flag & VFS_DMI)) {
  366. xfs_mount_t *mp = XFS_VFSTOM(vp->v_vfsp);
  367. xfs_inode_t *ip = xfs_vtoi(vp);
  368. if (!ip)
  369. return -EINVAL;
  370. if (DM_EVENT_ENABLED(vp->v_vfsp, ip, DM_EVENT_READ))
  371. return -XFS_SEND_DATA(mp, DM_EVENT_READ, vp,
  372. 0, 0, 0, NULL);
  373. }
  374. return 0;
  375. }
  376. #endif /* HAVE_FOP_OPEN_EXEC */
  377. const struct file_operations xfs_file_operations = {
  378. .llseek = generic_file_llseek,
  379. .read = do_sync_read,
  380. .write = do_sync_write,
  381. .aio_read = xfs_file_aio_read,
  382. .aio_write = xfs_file_aio_write,
  383. .splice_read = xfs_file_splice_read,
  384. .splice_write = xfs_file_splice_write,
  385. .unlocked_ioctl = xfs_file_ioctl,
  386. #ifdef CONFIG_COMPAT
  387. .compat_ioctl = xfs_file_compat_ioctl,
  388. #endif
  389. .mmap = xfs_file_mmap,
  390. .open = xfs_file_open,
  391. .flush = xfs_file_close,
  392. .release = xfs_file_release,
  393. .fsync = xfs_file_fsync,
  394. #ifdef HAVE_FOP_OPEN_EXEC
  395. .open_exec = xfs_file_open_exec,
  396. #endif
  397. };
  398. const struct file_operations xfs_invis_file_operations = {
  399. .llseek = generic_file_llseek,
  400. .read = do_sync_read,
  401. .write = do_sync_write,
  402. .aio_read = xfs_file_aio_read_invis,
  403. .aio_write = xfs_file_aio_write_invis,
  404. .splice_read = xfs_file_splice_read_invis,
  405. .splice_write = xfs_file_splice_write_invis,
  406. .unlocked_ioctl = xfs_file_ioctl_invis,
  407. #ifdef CONFIG_COMPAT
  408. .compat_ioctl = xfs_file_compat_invis_ioctl,
  409. #endif
  410. .mmap = xfs_file_mmap,
  411. .open = xfs_file_open,
  412. .flush = xfs_file_close,
  413. .release = xfs_file_release,
  414. .fsync = xfs_file_fsync,
  415. };
  416. const struct file_operations xfs_dir_file_operations = {
  417. .read = generic_read_dir,
  418. .readdir = xfs_file_readdir,
  419. .unlocked_ioctl = xfs_file_ioctl,
  420. #ifdef CONFIG_COMPAT
  421. .compat_ioctl = xfs_file_compat_ioctl,
  422. #endif
  423. .fsync = xfs_file_fsync,
  424. };
  425. static struct vm_operations_struct xfs_file_vm_ops = {
  426. .nopage = filemap_nopage,
  427. .populate = filemap_populate,
  428. };
  429. #ifdef CONFIG_XFS_DMAPI
  430. static struct vm_operations_struct xfs_dmapi_file_vm_ops = {
  431. .nopage = xfs_vm_nopage,
  432. .populate = filemap_populate,
  433. #ifdef HAVE_VMOP_MPROTECT
  434. .mprotect = xfs_vm_mprotect,
  435. #endif
  436. };
  437. #endif /* CONFIG_XFS_DMAPI */