xfs_file.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  1. /*
  2. * Copyright (c) 2000-2005 Silicon Graphics, Inc. All Rights Reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of version 2 of the GNU General Public License as
  6. * published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it would be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. *
  12. * Further, this software is distributed without any warranty that it is
  13. * free of the rightful claim of any third person regarding infringement
  14. * or the like. Any license provided herein, whether implied or
  15. * otherwise, applies only to this software file. Patent licenses, if
  16. * any, provided herein do not apply to combinations of this program with
  17. * other software, or any other product whatsoever.
  18. *
  19. * You should have received a copy of the GNU General Public License along
  20. * with this program; if not, write the Free Software Foundation, Inc., 59
  21. * Temple Place - Suite 330, Boston MA 02111-1307, USA.
  22. *
  23. * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
  24. * Mountain View, CA 94043, or:
  25. *
  26. * http://www.sgi.com
  27. *
  28. * For further information regarding this notice, see:
  29. *
  30. * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
  31. */
  32. #include "xfs.h"
  33. #include "xfs_inum.h"
  34. #include "xfs_log.h"
  35. #include "xfs_sb.h"
  36. #include "xfs_dir.h"
  37. #include "xfs_dir2.h"
  38. #include "xfs_trans.h"
  39. #include "xfs_dmapi.h"
  40. #include "xfs_mount.h"
  41. #include "xfs_bmap_btree.h"
  42. #include "xfs_alloc_btree.h"
  43. #include "xfs_ialloc_btree.h"
  44. #include "xfs_alloc.h"
  45. #include "xfs_btree.h"
  46. #include "xfs_attr_sf.h"
  47. #include "xfs_dir_sf.h"
  48. #include "xfs_dir2_sf.h"
  49. #include "xfs_dinode.h"
  50. #include "xfs_inode.h"
  51. #include "xfs_error.h"
  52. #include "xfs_rw.h"
  53. #include "xfs_ioctl32.h"
  54. #include <linux/dcache.h>
  55. #include <linux/smp_lock.h>
  56. static struct vm_operations_struct linvfs_file_vm_ops;
  57. STATIC inline ssize_t
  58. __linvfs_read(
  59. struct kiocb *iocb,
  60. char __user *buf,
  61. int ioflags,
  62. size_t count,
  63. loff_t pos)
  64. {
  65. struct iovec iov = {buf, count};
  66. struct file *file = iocb->ki_filp;
  67. vnode_t *vp = LINVFS_GET_VP(file->f_dentry->d_inode);
  68. ssize_t rval;
  69. BUG_ON(iocb->ki_pos != pos);
  70. if (unlikely(file->f_flags & O_DIRECT))
  71. ioflags |= IO_ISDIRECT;
  72. VOP_READ(vp, iocb, &iov, 1, &iocb->ki_pos, ioflags, NULL, rval);
  73. return rval;
  74. }
  75. STATIC ssize_t
  76. linvfs_aio_read(
  77. struct kiocb *iocb,
  78. char __user *buf,
  79. size_t count,
  80. loff_t pos)
  81. {
  82. return __linvfs_read(iocb, buf, IO_ISAIO, count, pos);
  83. }
  84. STATIC ssize_t
  85. linvfs_aio_read_invis(
  86. struct kiocb *iocb,
  87. char __user *buf,
  88. size_t count,
  89. loff_t pos)
  90. {
  91. return __linvfs_read(iocb, buf, IO_ISAIO|IO_INVIS, count, pos);
  92. }
  93. STATIC inline ssize_t
  94. __linvfs_write(
  95. struct kiocb *iocb,
  96. const char __user *buf,
  97. int ioflags,
  98. size_t count,
  99. loff_t pos)
  100. {
  101. struct iovec iov = {(void __user *)buf, count};
  102. struct file *file = iocb->ki_filp;
  103. struct inode *inode = file->f_mapping->host;
  104. vnode_t *vp = LINVFS_GET_VP(inode);
  105. ssize_t rval;
  106. BUG_ON(iocb->ki_pos != pos);
  107. if (unlikely(file->f_flags & O_DIRECT))
  108. ioflags |= IO_ISDIRECT;
  109. VOP_WRITE(vp, iocb, &iov, 1, &iocb->ki_pos, ioflags, NULL, rval);
  110. return rval;
  111. }
  112. STATIC ssize_t
  113. linvfs_aio_write(
  114. struct kiocb *iocb,
  115. const char __user *buf,
  116. size_t count,
  117. loff_t pos)
  118. {
  119. return __linvfs_write(iocb, buf, IO_ISAIO, count, pos);
  120. }
  121. STATIC ssize_t
  122. linvfs_aio_write_invis(
  123. struct kiocb *iocb,
  124. const char __user *buf,
  125. size_t count,
  126. loff_t pos)
  127. {
  128. return __linvfs_write(iocb, buf, IO_ISAIO|IO_INVIS, count, pos);
  129. }
  130. STATIC inline ssize_t
  131. __linvfs_readv(
  132. struct file *file,
  133. const struct iovec *iov,
  134. int ioflags,
  135. unsigned long nr_segs,
  136. loff_t *ppos)
  137. {
  138. struct inode *inode = file->f_mapping->host;
  139. vnode_t *vp = LINVFS_GET_VP(inode);
  140. struct kiocb kiocb;
  141. ssize_t rval;
  142. init_sync_kiocb(&kiocb, file);
  143. kiocb.ki_pos = *ppos;
  144. if (unlikely(file->f_flags & O_DIRECT))
  145. ioflags |= IO_ISDIRECT;
  146. VOP_READ(vp, &kiocb, iov, nr_segs, &kiocb.ki_pos, ioflags, NULL, rval);
  147. *ppos = kiocb.ki_pos;
  148. return rval;
  149. }
  150. STATIC ssize_t
  151. linvfs_readv(
  152. struct file *file,
  153. const struct iovec *iov,
  154. unsigned long nr_segs,
  155. loff_t *ppos)
  156. {
  157. return __linvfs_readv(file, iov, 0, nr_segs, ppos);
  158. }
  159. STATIC ssize_t
  160. linvfs_readv_invis(
  161. struct file *file,
  162. const struct iovec *iov,
  163. unsigned long nr_segs,
  164. loff_t *ppos)
  165. {
  166. return __linvfs_readv(file, iov, IO_INVIS, nr_segs, ppos);
  167. }
  168. STATIC inline ssize_t
  169. __linvfs_writev(
  170. struct file *file,
  171. const struct iovec *iov,
  172. int ioflags,
  173. unsigned long nr_segs,
  174. loff_t *ppos)
  175. {
  176. struct inode *inode = file->f_mapping->host;
  177. vnode_t *vp = LINVFS_GET_VP(inode);
  178. struct kiocb kiocb;
  179. ssize_t rval;
  180. init_sync_kiocb(&kiocb, file);
  181. kiocb.ki_pos = *ppos;
  182. if (unlikely(file->f_flags & O_DIRECT))
  183. ioflags |= IO_ISDIRECT;
  184. VOP_WRITE(vp, &kiocb, iov, nr_segs, &kiocb.ki_pos, ioflags, NULL, rval);
  185. *ppos = kiocb.ki_pos;
  186. return rval;
  187. }
  188. STATIC ssize_t
  189. linvfs_writev(
  190. struct file *file,
  191. const struct iovec *iov,
  192. unsigned long nr_segs,
  193. loff_t *ppos)
  194. {
  195. return __linvfs_writev(file, iov, 0, nr_segs, ppos);
  196. }
  197. STATIC ssize_t
  198. linvfs_writev_invis(
  199. struct file *file,
  200. const struct iovec *iov,
  201. unsigned long nr_segs,
  202. loff_t *ppos)
  203. {
  204. return __linvfs_writev(file, iov, IO_INVIS, nr_segs, ppos);
  205. }
  206. STATIC ssize_t
  207. linvfs_sendfile(
  208. struct file *filp,
  209. loff_t *ppos,
  210. size_t count,
  211. read_actor_t actor,
  212. void *target)
  213. {
  214. vnode_t *vp = LINVFS_GET_VP(filp->f_dentry->d_inode);
  215. ssize_t rval;
  216. VOP_SENDFILE(vp, filp, ppos, 0, count, actor, target, NULL, rval);
  217. return rval;
  218. }
  219. STATIC int
  220. linvfs_open(
  221. struct inode *inode,
  222. struct file *filp)
  223. {
  224. vnode_t *vp = LINVFS_GET_VP(inode);
  225. int error;
  226. if (!(filp->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS)
  227. return -EFBIG;
  228. ASSERT(vp);
  229. VOP_OPEN(vp, NULL, error);
  230. return -error;
  231. }
  232. STATIC int
  233. linvfs_release(
  234. struct inode *inode,
  235. struct file *filp)
  236. {
  237. vnode_t *vp = LINVFS_GET_VP(inode);
  238. int error = 0;
  239. if (vp)
  240. VOP_RELEASE(vp, error);
  241. return -error;
  242. }
  243. STATIC int
  244. linvfs_fsync(
  245. struct file *filp,
  246. struct dentry *dentry,
  247. int datasync)
  248. {
  249. struct inode *inode = dentry->d_inode;
  250. vnode_t *vp = LINVFS_GET_VP(inode);
  251. int error;
  252. int flags = FSYNC_WAIT;
  253. if (datasync)
  254. flags |= FSYNC_DATA;
  255. ASSERT(vp);
  256. VOP_FSYNC(vp, flags, NULL, (xfs_off_t)0, (xfs_off_t)-1, error);
  257. return -error;
  258. }
  259. /*
  260. * linvfs_readdir maps to VOP_READDIR().
  261. * We need to build a uio, cred, ...
  262. */
  263. #define nextdp(dp) ((struct xfs_dirent *)((char *)(dp) + (dp)->d_reclen))
  264. STATIC int
  265. linvfs_readdir(
  266. struct file *filp,
  267. void *dirent,
  268. filldir_t filldir)
  269. {
  270. int error = 0;
  271. vnode_t *vp;
  272. uio_t uio;
  273. iovec_t iov;
  274. int eof = 0;
  275. caddr_t read_buf;
  276. int namelen, size = 0;
  277. size_t rlen = PAGE_CACHE_SIZE;
  278. xfs_off_t start_offset, curr_offset;
  279. xfs_dirent_t *dbp = NULL;
  280. vp = LINVFS_GET_VP(filp->f_dentry->d_inode);
  281. ASSERT(vp);
  282. /* Try fairly hard to get memory */
  283. do {
  284. if ((read_buf = (caddr_t)kmalloc(rlen, GFP_KERNEL)))
  285. break;
  286. rlen >>= 1;
  287. } while (rlen >= 1024);
  288. if (read_buf == NULL)
  289. return -ENOMEM;
  290. uio.uio_iov = &iov;
  291. uio.uio_segflg = UIO_SYSSPACE;
  292. curr_offset = filp->f_pos;
  293. if (filp->f_pos != 0x7fffffff)
  294. uio.uio_offset = filp->f_pos;
  295. else
  296. uio.uio_offset = 0xffffffff;
  297. while (!eof) {
  298. uio.uio_resid = iov.iov_len = rlen;
  299. iov.iov_base = read_buf;
  300. uio.uio_iovcnt = 1;
  301. start_offset = uio.uio_offset;
  302. VOP_READDIR(vp, &uio, NULL, &eof, error);
  303. if ((uio.uio_offset == start_offset) || error) {
  304. size = 0;
  305. break;
  306. }
  307. size = rlen - uio.uio_resid;
  308. dbp = (xfs_dirent_t *)read_buf;
  309. while (size > 0) {
  310. namelen = strlen(dbp->d_name);
  311. if (filldir(dirent, dbp->d_name, namelen,
  312. (loff_t) curr_offset & 0x7fffffff,
  313. (ino_t) dbp->d_ino,
  314. DT_UNKNOWN)) {
  315. goto done;
  316. }
  317. size -= dbp->d_reclen;
  318. curr_offset = (loff_t)dbp->d_off /* & 0x7fffffff */;
  319. dbp = nextdp(dbp);
  320. }
  321. }
  322. done:
  323. if (!error) {
  324. if (size == 0)
  325. filp->f_pos = uio.uio_offset & 0x7fffffff;
  326. else if (dbp)
  327. filp->f_pos = curr_offset;
  328. }
  329. kfree(read_buf);
  330. return -error;
  331. }
  332. STATIC int
  333. linvfs_file_mmap(
  334. struct file *filp,
  335. struct vm_area_struct *vma)
  336. {
  337. struct inode *ip = filp->f_dentry->d_inode;
  338. vnode_t *vp = LINVFS_GET_VP(ip);
  339. vattr_t va = { .va_mask = XFS_AT_UPDATIME };
  340. int error;
  341. if (vp->v_vfsp->vfs_flag & VFS_DMI) {
  342. xfs_mount_t *mp = XFS_VFSTOM(vp->v_vfsp);
  343. error = -XFS_SEND_MMAP(mp, vma, 0);
  344. if (error)
  345. return error;
  346. }
  347. vma->vm_ops = &linvfs_file_vm_ops;
  348. VOP_SETATTR(vp, &va, XFS_AT_UPDATIME, NULL, error);
  349. if (!error)
  350. vn_revalidate(vp); /* update Linux inode flags */
  351. return 0;
  352. }
  353. STATIC long
  354. linvfs_ioctl(
  355. struct file *filp,
  356. unsigned int cmd,
  357. unsigned long arg)
  358. {
  359. int error;
  360. struct inode *inode = filp->f_dentry->d_inode;
  361. vnode_t *vp = LINVFS_GET_VP(inode);
  362. VOP_IOCTL(vp, inode, filp, 0, cmd, (void __user *)arg, error);
  363. VMODIFY(vp);
  364. /* NOTE: some of the ioctl's return positive #'s as a
  365. * byte count indicating success, such as
  366. * readlink_by_handle. So we don't "sign flip"
  367. * like most other routines. This means true
  368. * errors need to be returned as a negative value.
  369. */
  370. return error;
  371. }
  372. STATIC long
  373. linvfs_ioctl_invis(
  374. struct file *filp,
  375. unsigned int cmd,
  376. unsigned long arg)
  377. {
  378. int error;
  379. struct inode *inode = filp->f_dentry->d_inode;
  380. vnode_t *vp = LINVFS_GET_VP(inode);
  381. ASSERT(vp);
  382. VOP_IOCTL(vp, inode, filp, IO_INVIS, cmd, (void __user *)arg, error);
  383. VMODIFY(vp);
  384. /* NOTE: some of the ioctl's return positive #'s as a
  385. * byte count indicating success, such as
  386. * readlink_by_handle. So we don't "sign flip"
  387. * like most other routines. This means true
  388. * errors need to be returned as a negative value.
  389. */
  390. return error;
  391. }
  392. #ifdef HAVE_VMOP_MPROTECT
  393. STATIC int
  394. linvfs_mprotect(
  395. struct vm_area_struct *vma,
  396. unsigned int newflags)
  397. {
  398. vnode_t *vp = LINVFS_GET_VP(vma->vm_file->f_dentry->d_inode);
  399. int error = 0;
  400. if (vp->v_vfsp->vfs_flag & VFS_DMI) {
  401. if ((vma->vm_flags & VM_MAYSHARE) &&
  402. (newflags & VM_WRITE) && !(vma->vm_flags & VM_WRITE)) {
  403. xfs_mount_t *mp = XFS_VFSTOM(vp->v_vfsp);
  404. error = XFS_SEND_MMAP(mp, vma, VM_WRITE);
  405. }
  406. }
  407. return error;
  408. }
  409. #endif /* HAVE_VMOP_MPROTECT */
  410. #ifdef HAVE_FOP_OPEN_EXEC
  411. /* If the user is attempting to execute a file that is offline then
  412. * we have to trigger a DMAPI READ event before the file is marked as busy
  413. * otherwise the invisible I/O will not be able to write to the file to bring
  414. * it back online.
  415. */
  416. STATIC int
  417. linvfs_open_exec(
  418. struct inode *inode)
  419. {
  420. vnode_t *vp = LINVFS_GET_VP(inode);
  421. xfs_mount_t *mp = XFS_VFSTOM(vp->v_vfsp);
  422. int error = 0;
  423. bhv_desc_t *bdp;
  424. xfs_inode_t *ip;
  425. if (vp->v_vfsp->vfs_flag & VFS_DMI) {
  426. bdp = vn_bhv_lookup(VN_BHV_HEAD(vp), &xfs_vnodeops);
  427. if (!bdp) {
  428. error = -EINVAL;
  429. goto open_exec_out;
  430. }
  431. ip = XFS_BHVTOI(bdp);
  432. if (DM_EVENT_ENABLED(vp->v_vfsp, ip, DM_EVENT_READ)) {
  433. error = -XFS_SEND_DATA(mp, DM_EVENT_READ, vp,
  434. 0, 0, 0, NULL);
  435. }
  436. }
  437. open_exec_out:
  438. return error;
  439. }
  440. #endif /* HAVE_FOP_OPEN_EXEC */
  441. /*
  442. * Temporary workaround to the AIO direct IO write problem.
  443. * This code can go and we can revert to do_sync_write once
  444. * the writepage(s) rework is merged.
  445. */
  446. STATIC ssize_t
  447. linvfs_write(
  448. struct file *filp,
  449. const char __user *buf,
  450. size_t len,
  451. loff_t *ppos)
  452. {
  453. struct kiocb kiocb;
  454. ssize_t ret;
  455. init_sync_kiocb(&kiocb, filp);
  456. kiocb.ki_pos = *ppos;
  457. ret = __linvfs_write(&kiocb, buf, 0, len, kiocb.ki_pos);
  458. *ppos = kiocb.ki_pos;
  459. return ret;
  460. }
  461. STATIC ssize_t
  462. linvfs_write_invis(
  463. struct file *filp,
  464. const char __user *buf,
  465. size_t len,
  466. loff_t *ppos)
  467. {
  468. struct kiocb kiocb;
  469. ssize_t ret;
  470. init_sync_kiocb(&kiocb, filp);
  471. kiocb.ki_pos = *ppos;
  472. ret = __linvfs_write(&kiocb, buf, IO_INVIS, len, kiocb.ki_pos);
  473. *ppos = kiocb.ki_pos;
  474. return ret;
  475. }
  476. struct file_operations linvfs_file_operations = {
  477. .llseek = generic_file_llseek,
  478. .read = do_sync_read,
  479. .write = linvfs_write,
  480. .readv = linvfs_readv,
  481. .writev = linvfs_writev,
  482. .aio_read = linvfs_aio_read,
  483. .aio_write = linvfs_aio_write,
  484. .sendfile = linvfs_sendfile,
  485. .unlocked_ioctl = linvfs_ioctl,
  486. #ifdef CONFIG_COMPAT
  487. .compat_ioctl = linvfs_compat_ioctl,
  488. #endif
  489. .mmap = linvfs_file_mmap,
  490. .open = linvfs_open,
  491. .release = linvfs_release,
  492. .fsync = linvfs_fsync,
  493. #ifdef HAVE_FOP_OPEN_EXEC
  494. .open_exec = linvfs_open_exec,
  495. #endif
  496. };
  497. struct file_operations linvfs_invis_file_operations = {
  498. .llseek = generic_file_llseek,
  499. .read = do_sync_read,
  500. .write = linvfs_write_invis,
  501. .readv = linvfs_readv_invis,
  502. .writev = linvfs_writev_invis,
  503. .aio_read = linvfs_aio_read_invis,
  504. .aio_write = linvfs_aio_write_invis,
  505. .sendfile = linvfs_sendfile,
  506. .unlocked_ioctl = linvfs_ioctl_invis,
  507. #ifdef CONFIG_COMPAT
  508. .compat_ioctl = linvfs_compat_invis_ioctl,
  509. #endif
  510. .mmap = linvfs_file_mmap,
  511. .open = linvfs_open,
  512. .release = linvfs_release,
  513. .fsync = linvfs_fsync,
  514. };
  515. struct file_operations linvfs_dir_operations = {
  516. .read = generic_read_dir,
  517. .readdir = linvfs_readdir,
  518. .unlocked_ioctl = linvfs_ioctl,
  519. #ifdef CONFIG_COMPAT
  520. .compat_ioctl = linvfs_compat_ioctl,
  521. #endif
  522. .fsync = linvfs_fsync,
  523. };
  524. static struct vm_operations_struct linvfs_file_vm_ops = {
  525. .nopage = filemap_nopage,
  526. .populate = filemap_populate,
  527. #ifdef HAVE_VMOP_MPROTECT
  528. .mprotect = linvfs_mprotect,
  529. #endif
  530. };