xfs_file.c 14 KB

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