xfs_file.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  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. char __user *buf,
  50. int ioflags,
  51. size_t count,
  52. loff_t pos)
  53. {
  54. struct iovec iov = {buf, count};
  55. struct file *file = iocb->ki_filp;
  56. bhv_vnode_t *vp = vn_from_inode(file->f_dentry->d_inode);
  57. BUG_ON(iocb->ki_pos != pos);
  58. if (unlikely(file->f_flags & O_DIRECT))
  59. ioflags |= IO_ISDIRECT;
  60. return bhv_vop_read(vp, iocb, &iov, 1, &iocb->ki_pos, ioflags, NULL);
  61. }
  62. STATIC ssize_t
  63. xfs_file_aio_read(
  64. struct kiocb *iocb,
  65. char __user *buf,
  66. size_t count,
  67. loff_t pos)
  68. {
  69. return __xfs_file_read(iocb, buf, IO_ISAIO, count, pos);
  70. }
  71. STATIC ssize_t
  72. xfs_file_aio_read_invis(
  73. struct kiocb *iocb,
  74. char __user *buf,
  75. size_t count,
  76. loff_t pos)
  77. {
  78. return __xfs_file_read(iocb, buf, IO_ISAIO|IO_INVIS, count, pos);
  79. }
  80. STATIC inline ssize_t
  81. __xfs_file_write(
  82. struct kiocb *iocb,
  83. const char __user *buf,
  84. int ioflags,
  85. size_t count,
  86. loff_t pos)
  87. {
  88. struct iovec iov = {(void __user *)buf, count};
  89. struct file *file = iocb->ki_filp;
  90. struct inode *inode = file->f_mapping->host;
  91. bhv_vnode_t *vp = vn_from_inode(inode);
  92. BUG_ON(iocb->ki_pos != pos);
  93. if (unlikely(file->f_flags & O_DIRECT))
  94. ioflags |= IO_ISDIRECT;
  95. return bhv_vop_write(vp, iocb, &iov, 1, &iocb->ki_pos, ioflags, NULL);
  96. }
  97. STATIC ssize_t
  98. xfs_file_aio_write(
  99. struct kiocb *iocb,
  100. const char __user *buf,
  101. size_t count,
  102. loff_t pos)
  103. {
  104. return __xfs_file_write(iocb, buf, IO_ISAIO, count, pos);
  105. }
  106. STATIC ssize_t
  107. xfs_file_aio_write_invis(
  108. struct kiocb *iocb,
  109. const char __user *buf,
  110. size_t count,
  111. loff_t pos)
  112. {
  113. return __xfs_file_write(iocb, buf, IO_ISAIO|IO_INVIS, count, pos);
  114. }
  115. STATIC inline ssize_t
  116. __xfs_file_readv(
  117. struct file *file,
  118. const struct iovec *iov,
  119. int ioflags,
  120. unsigned long nr_segs,
  121. loff_t *ppos)
  122. {
  123. struct inode *inode = file->f_mapping->host;
  124. bhv_vnode_t *vp = vn_from_inode(inode);
  125. struct kiocb kiocb;
  126. ssize_t rval;
  127. init_sync_kiocb(&kiocb, file);
  128. kiocb.ki_pos = *ppos;
  129. if (unlikely(file->f_flags & O_DIRECT))
  130. ioflags |= IO_ISDIRECT;
  131. rval = bhv_vop_read(vp, &kiocb, iov, nr_segs,
  132. &kiocb.ki_pos, ioflags, NULL);
  133. *ppos = kiocb.ki_pos;
  134. return rval;
  135. }
  136. STATIC ssize_t
  137. xfs_file_readv(
  138. struct file *file,
  139. const struct iovec *iov,
  140. unsigned long nr_segs,
  141. loff_t *ppos)
  142. {
  143. return __xfs_file_readv(file, iov, 0, nr_segs, ppos);
  144. }
  145. STATIC ssize_t
  146. xfs_file_readv_invis(
  147. struct file *file,
  148. const struct iovec *iov,
  149. unsigned long nr_segs,
  150. loff_t *ppos)
  151. {
  152. return __xfs_file_readv(file, iov, IO_INVIS, nr_segs, ppos);
  153. }
  154. STATIC inline ssize_t
  155. __xfs_file_writev(
  156. struct file *file,
  157. const struct iovec *iov,
  158. int ioflags,
  159. unsigned long nr_segs,
  160. loff_t *ppos)
  161. {
  162. struct inode *inode = file->f_mapping->host;
  163. bhv_vnode_t *vp = vn_from_inode(inode);
  164. struct kiocb kiocb;
  165. ssize_t rval;
  166. init_sync_kiocb(&kiocb, file);
  167. kiocb.ki_pos = *ppos;
  168. if (unlikely(file->f_flags & O_DIRECT))
  169. ioflags |= IO_ISDIRECT;
  170. rval = bhv_vop_write(vp, &kiocb, iov, nr_segs,
  171. &kiocb.ki_pos, ioflags, NULL);
  172. *ppos = kiocb.ki_pos;
  173. return rval;
  174. }
  175. STATIC ssize_t
  176. xfs_file_writev(
  177. struct file *file,
  178. const struct iovec *iov,
  179. unsigned long nr_segs,
  180. loff_t *ppos)
  181. {
  182. return __xfs_file_writev(file, iov, 0, nr_segs, ppos);
  183. }
  184. STATIC ssize_t
  185. xfs_file_writev_invis(
  186. struct file *file,
  187. const struct iovec *iov,
  188. unsigned long nr_segs,
  189. loff_t *ppos)
  190. {
  191. return __xfs_file_writev(file, iov, IO_INVIS, nr_segs, ppos);
  192. }
  193. STATIC ssize_t
  194. xfs_file_sendfile(
  195. struct file *filp,
  196. loff_t *pos,
  197. size_t count,
  198. read_actor_t actor,
  199. void *target)
  200. {
  201. return bhv_vop_sendfile(vn_from_inode(filp->f_dentry->d_inode),
  202. filp, pos, 0, count, actor, target, NULL);
  203. }
  204. STATIC ssize_t
  205. xfs_file_sendfile_invis(
  206. struct file *filp,
  207. loff_t *pos,
  208. size_t count,
  209. read_actor_t actor,
  210. void *target)
  211. {
  212. return bhv_vop_sendfile(vn_from_inode(filp->f_dentry->d_inode),
  213. filp, pos, IO_INVIS, count, actor, target, NULL);
  214. }
  215. STATIC ssize_t
  216. xfs_file_splice_read(
  217. struct file *infilp,
  218. loff_t *ppos,
  219. struct pipe_inode_info *pipe,
  220. size_t len,
  221. unsigned int flags)
  222. {
  223. return bhv_vop_splice_read(vn_from_inode(infilp->f_dentry->d_inode),
  224. infilp, ppos, pipe, len, flags, 0, NULL);
  225. }
  226. STATIC ssize_t
  227. xfs_file_splice_read_invis(
  228. struct file *infilp,
  229. loff_t *ppos,
  230. struct pipe_inode_info *pipe,
  231. size_t len,
  232. unsigned int flags)
  233. {
  234. return bhv_vop_splice_read(vn_from_inode(infilp->f_dentry->d_inode),
  235. infilp, ppos, pipe, len, flags, IO_INVIS,
  236. NULL);
  237. }
  238. STATIC ssize_t
  239. xfs_file_splice_write(
  240. struct pipe_inode_info *pipe,
  241. struct file *outfilp,
  242. loff_t *ppos,
  243. size_t len,
  244. unsigned int flags)
  245. {
  246. return bhv_vop_splice_write(vn_from_inode(outfilp->f_dentry->d_inode),
  247. pipe, outfilp, ppos, len, flags, 0, NULL);
  248. }
  249. STATIC ssize_t
  250. xfs_file_splice_write_invis(
  251. struct pipe_inode_info *pipe,
  252. struct file *outfilp,
  253. loff_t *ppos,
  254. size_t len,
  255. unsigned int flags)
  256. {
  257. return bhv_vop_splice_write(vn_from_inode(outfilp->f_dentry->d_inode),
  258. pipe, outfilp, ppos, len, flags, IO_INVIS,
  259. NULL);
  260. }
  261. STATIC int
  262. xfs_file_open(
  263. struct inode *inode,
  264. struct file *filp)
  265. {
  266. if (!(filp->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS)
  267. return -EFBIG;
  268. return -bhv_vop_open(vn_from_inode(inode), NULL);
  269. }
  270. STATIC int
  271. xfs_file_close(
  272. struct file *filp,
  273. fl_owner_t id)
  274. {
  275. return -bhv_vop_close(vn_from_inode(filp->f_dentry->d_inode), 0,
  276. file_count(filp) > 1 ? L_FALSE : L_TRUE, NULL);
  277. }
  278. STATIC int
  279. xfs_file_release(
  280. struct inode *inode,
  281. struct file *filp)
  282. {
  283. bhv_vnode_t *vp = vn_from_inode(inode);
  284. if (vp)
  285. return -bhv_vop_release(vp);
  286. return 0;
  287. }
  288. STATIC int
  289. xfs_file_fsync(
  290. struct file *filp,
  291. struct dentry *dentry,
  292. int datasync)
  293. {
  294. bhv_vnode_t *vp = vn_from_inode(dentry->d_inode);
  295. int flags = FSYNC_WAIT;
  296. if (datasync)
  297. flags |= FSYNC_DATA;
  298. if (VN_TRUNC(vp))
  299. VUNTRUNCATE(vp);
  300. return -bhv_vop_fsync(vp, flags, NULL, (xfs_off_t)0, (xfs_off_t)-1);
  301. }
  302. #ifdef CONFIG_XFS_DMAPI
  303. STATIC struct page *
  304. xfs_vm_nopage(
  305. struct vm_area_struct *area,
  306. unsigned long address,
  307. int *type)
  308. {
  309. struct inode *inode = area->vm_file->f_dentry->d_inode;
  310. bhv_vnode_t *vp = vn_from_inode(inode);
  311. ASSERT_ALWAYS(vp->v_vfsp->vfs_flag & VFS_DMI);
  312. if (XFS_SEND_MMAP(XFS_VFSTOM(vp->v_vfsp), area, 0))
  313. return NULL;
  314. return filemap_nopage(area, address, type);
  315. }
  316. #endif /* CONFIG_XFS_DMAPI */
  317. STATIC int
  318. xfs_file_readdir(
  319. struct file *filp,
  320. void *dirent,
  321. filldir_t filldir)
  322. {
  323. int error = 0;
  324. bhv_vnode_t *vp = vn_from_inode(filp->f_dentry->d_inode);
  325. uio_t uio;
  326. iovec_t iov;
  327. int eof = 0;
  328. caddr_t read_buf;
  329. int namelen, size = 0;
  330. size_t rlen = PAGE_CACHE_SIZE;
  331. xfs_off_t start_offset, curr_offset;
  332. xfs_dirent_t *dbp = NULL;
  333. /* Try fairly hard to get memory */
  334. do {
  335. if ((read_buf = kmalloc(rlen, GFP_KERNEL)))
  336. break;
  337. rlen >>= 1;
  338. } while (rlen >= 1024);
  339. if (read_buf == NULL)
  340. return -ENOMEM;
  341. uio.uio_iov = &iov;
  342. uio.uio_segflg = UIO_SYSSPACE;
  343. curr_offset = filp->f_pos;
  344. if (filp->f_pos != 0x7fffffff)
  345. uio.uio_offset = filp->f_pos;
  346. else
  347. uio.uio_offset = 0xffffffff;
  348. while (!eof) {
  349. uio.uio_resid = iov.iov_len = rlen;
  350. iov.iov_base = read_buf;
  351. uio.uio_iovcnt = 1;
  352. start_offset = uio.uio_offset;
  353. error = bhv_vop_readdir(vp, &uio, NULL, &eof);
  354. if ((uio.uio_offset == start_offset) || error) {
  355. size = 0;
  356. break;
  357. }
  358. size = rlen - uio.uio_resid;
  359. dbp = (xfs_dirent_t *)read_buf;
  360. while (size > 0) {
  361. namelen = strlen(dbp->d_name);
  362. if (filldir(dirent, dbp->d_name, namelen,
  363. (loff_t) curr_offset & 0x7fffffff,
  364. (ino_t) dbp->d_ino,
  365. DT_UNKNOWN)) {
  366. goto done;
  367. }
  368. size -= dbp->d_reclen;
  369. curr_offset = (loff_t)dbp->d_off /* & 0x7fffffff */;
  370. dbp = (xfs_dirent_t *)((char *)dbp + dbp->d_reclen);
  371. }
  372. }
  373. done:
  374. if (!error) {
  375. if (size == 0)
  376. filp->f_pos = uio.uio_offset & 0x7fffffff;
  377. else if (dbp)
  378. filp->f_pos = curr_offset;
  379. }
  380. kfree(read_buf);
  381. return -error;
  382. }
  383. STATIC int
  384. xfs_file_mmap(
  385. struct file *filp,
  386. struct vm_area_struct *vma)
  387. {
  388. vma->vm_ops = &xfs_file_vm_ops;
  389. #ifdef CONFIG_XFS_DMAPI
  390. if (vn_from_inode(filp->f_dentry->d_inode)->v_vfsp->vfs_flag & VFS_DMI)
  391. vma->vm_ops = &xfs_dmapi_file_vm_ops;
  392. #endif /* CONFIG_XFS_DMAPI */
  393. file_accessed(filp);
  394. return 0;
  395. }
  396. STATIC long
  397. xfs_file_ioctl(
  398. struct file *filp,
  399. unsigned int cmd,
  400. unsigned long p)
  401. {
  402. int error;
  403. struct inode *inode = filp->f_dentry->d_inode;
  404. bhv_vnode_t *vp = vn_from_inode(inode);
  405. error = bhv_vop_ioctl(vp, inode, filp, 0, cmd, (void __user *)p);
  406. VMODIFY(vp);
  407. /* NOTE: some of the ioctl's return positive #'s as a
  408. * byte count indicating success, such as
  409. * readlink_by_handle. So we don't "sign flip"
  410. * like most other routines. This means true
  411. * errors need to be returned as a negative value.
  412. */
  413. return error;
  414. }
  415. STATIC long
  416. xfs_file_ioctl_invis(
  417. struct file *filp,
  418. unsigned int cmd,
  419. unsigned long p)
  420. {
  421. int error;
  422. struct inode *inode = filp->f_dentry->d_inode;
  423. bhv_vnode_t *vp = vn_from_inode(inode);
  424. error = bhv_vop_ioctl(vp, inode, filp, IO_INVIS, cmd, (void __user *)p);
  425. VMODIFY(vp);
  426. /* NOTE: some of the ioctl's return positive #'s as a
  427. * byte count indicating success, such as
  428. * readlink_by_handle. So we don't "sign flip"
  429. * like most other routines. This means true
  430. * errors need to be returned as a negative value.
  431. */
  432. return error;
  433. }
  434. #ifdef CONFIG_XFS_DMAPI
  435. #ifdef HAVE_VMOP_MPROTECT
  436. STATIC int
  437. xfs_vm_mprotect(
  438. struct vm_area_struct *vma,
  439. unsigned int newflags)
  440. {
  441. bhv_vnode_t *vp = vn_from_inode(vma->vm_file->f_dentry->d_inode);
  442. int error = 0;
  443. if (vp->v_vfsp->vfs_flag & VFS_DMI) {
  444. if ((vma->vm_flags & VM_MAYSHARE) &&
  445. (newflags & VM_WRITE) && !(vma->vm_flags & VM_WRITE)) {
  446. xfs_mount_t *mp = XFS_VFSTOM(vp->v_vfsp);
  447. error = XFS_SEND_MMAP(mp, vma, VM_WRITE);
  448. }
  449. }
  450. return error;
  451. }
  452. #endif /* HAVE_VMOP_MPROTECT */
  453. #endif /* CONFIG_XFS_DMAPI */
  454. #ifdef HAVE_FOP_OPEN_EXEC
  455. /* If the user is attempting to execute a file that is offline then
  456. * we have to trigger a DMAPI READ event before the file is marked as busy
  457. * otherwise the invisible I/O will not be able to write to the file to bring
  458. * it back online.
  459. */
  460. STATIC int
  461. xfs_file_open_exec(
  462. struct inode *inode)
  463. {
  464. bhv_vnode_t *vp = vn_from_inode(inode);
  465. if (unlikely(vp->v_vfsp->vfs_flag & VFS_DMI)) {
  466. xfs_mount_t *mp = XFS_VFSTOM(vp->v_vfsp);
  467. xfs_inode_t *ip = xfs_vtoi(vp);
  468. if (!ip)
  469. return -EINVAL;
  470. if (DM_EVENT_ENABLED(vp->v_vfsp, ip, DM_EVENT_READ))
  471. return -XFS_SEND_DATA(mp, DM_EVENT_READ, vp,
  472. 0, 0, 0, NULL);
  473. }
  474. return 0;
  475. }
  476. #endif /* HAVE_FOP_OPEN_EXEC */
  477. const struct file_operations xfs_file_operations = {
  478. .llseek = generic_file_llseek,
  479. .read = do_sync_read,
  480. .write = do_sync_write,
  481. .readv = xfs_file_readv,
  482. .writev = xfs_file_writev,
  483. .aio_read = xfs_file_aio_read,
  484. .aio_write = xfs_file_aio_write,
  485. .sendfile = xfs_file_sendfile,
  486. .splice_read = xfs_file_splice_read,
  487. .splice_write = xfs_file_splice_write,
  488. .unlocked_ioctl = xfs_file_ioctl,
  489. #ifdef CONFIG_COMPAT
  490. .compat_ioctl = xfs_file_compat_ioctl,
  491. #endif
  492. .mmap = xfs_file_mmap,
  493. .open = xfs_file_open,
  494. .flush = xfs_file_close,
  495. .release = xfs_file_release,
  496. .fsync = xfs_file_fsync,
  497. #ifdef HAVE_FOP_OPEN_EXEC
  498. .open_exec = xfs_file_open_exec,
  499. #endif
  500. };
  501. const struct file_operations xfs_invis_file_operations = {
  502. .llseek = generic_file_llseek,
  503. .read = do_sync_read,
  504. .write = do_sync_write,
  505. .readv = xfs_file_readv_invis,
  506. .writev = xfs_file_writev_invis,
  507. .aio_read = xfs_file_aio_read_invis,
  508. .aio_write = xfs_file_aio_write_invis,
  509. .sendfile = xfs_file_sendfile_invis,
  510. .splice_read = xfs_file_splice_read_invis,
  511. .splice_write = xfs_file_splice_write_invis,
  512. .unlocked_ioctl = xfs_file_ioctl_invis,
  513. #ifdef CONFIG_COMPAT
  514. .compat_ioctl = xfs_file_compat_invis_ioctl,
  515. #endif
  516. .mmap = xfs_file_mmap,
  517. .open = xfs_file_open,
  518. .flush = xfs_file_close,
  519. .release = xfs_file_release,
  520. .fsync = xfs_file_fsync,
  521. };
  522. const struct file_operations xfs_dir_file_operations = {
  523. .read = generic_read_dir,
  524. .readdir = xfs_file_readdir,
  525. .unlocked_ioctl = xfs_file_ioctl,
  526. #ifdef CONFIG_COMPAT
  527. .compat_ioctl = xfs_file_compat_ioctl,
  528. #endif
  529. .fsync = xfs_file_fsync,
  530. };
  531. static struct vm_operations_struct xfs_file_vm_ops = {
  532. .nopage = filemap_nopage,
  533. .populate = filemap_populate,
  534. };
  535. #ifdef CONFIG_XFS_DMAPI
  536. static struct vm_operations_struct xfs_dmapi_file_vm_ops = {
  537. .nopage = xfs_vm_nopage,
  538. .populate = filemap_populate,
  539. #ifdef HAVE_VMOP_MPROTECT
  540. .mprotect = xfs_vm_mprotect,
  541. #endif
  542. };
  543. #endif /* CONFIG_XFS_DMAPI */