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