xfs_file.c 12 KB

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