xfs_file.c 13 KB

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