xfs_file.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  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 "xfs_vnodeops.h"
  41. #include <linux/dcache.h>
  42. #include <linux/smp_lock.h>
  43. static struct vm_operations_struct xfs_file_vm_ops;
  44. #ifdef CONFIG_XFS_DMAPI
  45. static struct vm_operations_struct xfs_dmapi_file_vm_ops;
  46. #endif
  47. STATIC_INLINE ssize_t
  48. __xfs_file_read(
  49. struct kiocb *iocb,
  50. const struct iovec *iov,
  51. unsigned long nr_segs,
  52. int ioflags,
  53. loff_t pos)
  54. {
  55. struct file *file = iocb->ki_filp;
  56. BUG_ON(iocb->ki_pos != pos);
  57. if (unlikely(file->f_flags & O_DIRECT))
  58. ioflags |= IO_ISDIRECT;
  59. return xfs_read(XFS_I(file->f_path.dentry->d_inode), iocb, iov,
  60. nr_segs, &iocb->ki_pos, ioflags);
  61. }
  62. STATIC ssize_t
  63. xfs_file_aio_read(
  64. struct kiocb *iocb,
  65. const struct iovec *iov,
  66. unsigned long nr_segs,
  67. loff_t pos)
  68. {
  69. return __xfs_file_read(iocb, iov, nr_segs, IO_ISAIO, pos);
  70. }
  71. STATIC ssize_t
  72. xfs_file_aio_read_invis(
  73. struct kiocb *iocb,
  74. const struct iovec *iov,
  75. unsigned long nr_segs,
  76. loff_t pos)
  77. {
  78. return __xfs_file_read(iocb, iov, nr_segs, IO_ISAIO|IO_INVIS, pos);
  79. }
  80. STATIC_INLINE ssize_t
  81. __xfs_file_write(
  82. struct kiocb *iocb,
  83. const struct iovec *iov,
  84. unsigned long nr_segs,
  85. int ioflags,
  86. loff_t pos)
  87. {
  88. struct file *file = iocb->ki_filp;
  89. BUG_ON(iocb->ki_pos != pos);
  90. if (unlikely(file->f_flags & O_DIRECT))
  91. ioflags |= IO_ISDIRECT;
  92. return xfs_write(XFS_I(file->f_mapping->host), iocb, iov, nr_segs,
  93. &iocb->ki_pos, ioflags);
  94. }
  95. STATIC ssize_t
  96. xfs_file_aio_write(
  97. struct kiocb *iocb,
  98. const struct iovec *iov,
  99. unsigned long nr_segs,
  100. loff_t pos)
  101. {
  102. return __xfs_file_write(iocb, iov, nr_segs, IO_ISAIO, pos);
  103. }
  104. STATIC ssize_t
  105. xfs_file_aio_write_invis(
  106. struct kiocb *iocb,
  107. const struct iovec *iov,
  108. unsigned long nr_segs,
  109. loff_t pos)
  110. {
  111. return __xfs_file_write(iocb, iov, nr_segs, IO_ISAIO|IO_INVIS, pos);
  112. }
  113. STATIC ssize_t
  114. xfs_file_splice_read(
  115. struct file *infilp,
  116. loff_t *ppos,
  117. struct pipe_inode_info *pipe,
  118. size_t len,
  119. unsigned int flags)
  120. {
  121. return xfs_splice_read(XFS_I(infilp->f_path.dentry->d_inode),
  122. infilp, ppos, pipe, len, flags, 0);
  123. }
  124. STATIC ssize_t
  125. xfs_file_splice_read_invis(
  126. struct file *infilp,
  127. loff_t *ppos,
  128. struct pipe_inode_info *pipe,
  129. size_t len,
  130. unsigned int flags)
  131. {
  132. return xfs_splice_read(XFS_I(infilp->f_path.dentry->d_inode),
  133. infilp, ppos, pipe, len, flags, IO_INVIS);
  134. }
  135. STATIC ssize_t
  136. xfs_file_splice_write(
  137. struct pipe_inode_info *pipe,
  138. struct file *outfilp,
  139. loff_t *ppos,
  140. size_t len,
  141. unsigned int flags)
  142. {
  143. return xfs_splice_write(XFS_I(outfilp->f_path.dentry->d_inode),
  144. pipe, outfilp, ppos, len, flags, 0);
  145. }
  146. STATIC ssize_t
  147. xfs_file_splice_write_invis(
  148. struct pipe_inode_info *pipe,
  149. struct file *outfilp,
  150. loff_t *ppos,
  151. size_t len,
  152. unsigned int flags)
  153. {
  154. return xfs_splice_write(XFS_I(outfilp->f_path.dentry->d_inode),
  155. pipe, outfilp, ppos, len, flags, IO_INVIS);
  156. }
  157. STATIC int
  158. xfs_file_open(
  159. struct inode *inode,
  160. struct file *filp)
  161. {
  162. if (!(filp->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS)
  163. return -EFBIG;
  164. return -xfs_open(XFS_I(inode));
  165. }
  166. STATIC int
  167. xfs_file_release(
  168. struct inode *inode,
  169. struct file *filp)
  170. {
  171. return -xfs_release(XFS_I(inode));
  172. }
  173. STATIC int
  174. xfs_file_fsync(
  175. struct file *filp,
  176. struct dentry *dentry,
  177. int datasync)
  178. {
  179. int flags = FSYNC_WAIT;
  180. if (datasync)
  181. flags |= FSYNC_DATA;
  182. xfs_iflags_clear(XFS_I(dentry->d_inode), XFS_ITRUNCATED);
  183. return -xfs_fsync(XFS_I(dentry->d_inode), flags,
  184. (xfs_off_t)0, (xfs_off_t)-1);
  185. }
  186. #ifdef CONFIG_XFS_DMAPI
  187. STATIC int
  188. xfs_vm_fault(
  189. struct vm_area_struct *vma,
  190. struct vm_fault *vmf)
  191. {
  192. struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
  193. bhv_vnode_t *vp = vn_from_inode(inode);
  194. ASSERT_ALWAYS(vp->v_vfsp->vfs_flag & VFS_DMI);
  195. if (XFS_SEND_MMAP(XFS_VFSTOM(vp->v_vfsp), vma, 0))
  196. return VM_FAULT_SIGBUS;
  197. return filemap_fault(vma, vmf);
  198. }
  199. #endif /* CONFIG_XFS_DMAPI */
  200. /*
  201. * Unfortunately we can't just use the clean and simple readdir implementation
  202. * below, because nfs might call back into ->lookup from the filldir callback
  203. * and that will deadlock the low-level btree code.
  204. *
  205. * Hopefully we'll find a better workaround that allows to use the optimal
  206. * version at least for local readdirs for 2.6.25.
  207. */
  208. #if 0
  209. STATIC int
  210. xfs_file_readdir(
  211. struct file *filp,
  212. void *dirent,
  213. filldir_t filldir)
  214. {
  215. struct inode *inode = filp->f_path.dentry->d_inode;
  216. xfs_inode_t *ip = XFS_I(inode);
  217. int error;
  218. size_t bufsize;
  219. /*
  220. * The Linux API doesn't pass down the total size of the buffer
  221. * we read into down to the filesystem. With the filldir concept
  222. * it's not needed for correct information, but the XFS dir2 leaf
  223. * code wants an estimate of the buffer size to calculate it's
  224. * readahead window and size the buffers used for mapping to
  225. * physical blocks.
  226. *
  227. * Try to give it an estimate that's good enough, maybe at some
  228. * point we can change the ->readdir prototype to include the
  229. * buffer size.
  230. */
  231. bufsize = (size_t)min_t(loff_t, PAGE_SIZE, inode->i_size);
  232. error = xfs_readdir(ip, dirent, bufsize,
  233. (xfs_off_t *)&filp->f_pos, filldir);
  234. if (error)
  235. return -error;
  236. return 0;
  237. }
  238. #else
  239. struct hack_dirent {
  240. u64 ino;
  241. loff_t offset;
  242. int namlen;
  243. unsigned int d_type;
  244. char name[];
  245. };
  246. struct hack_callback {
  247. char *dirent;
  248. size_t len;
  249. size_t used;
  250. };
  251. STATIC int
  252. xfs_hack_filldir(
  253. void *__buf,
  254. const char *name,
  255. int namlen,
  256. loff_t offset,
  257. u64 ino,
  258. unsigned int d_type)
  259. {
  260. struct hack_callback *buf = __buf;
  261. struct hack_dirent *de = (struct hack_dirent *)(buf->dirent + buf->used);
  262. unsigned int reclen;
  263. reclen = ALIGN(sizeof(struct hack_dirent) + namlen, sizeof(u64));
  264. if (buf->used + reclen > buf->len)
  265. return -EINVAL;
  266. de->namlen = namlen;
  267. de->offset = offset;
  268. de->ino = ino;
  269. de->d_type = d_type;
  270. memcpy(de->name, name, namlen);
  271. buf->used += reclen;
  272. return 0;
  273. }
  274. STATIC int
  275. xfs_file_readdir(
  276. struct file *filp,
  277. void *dirent,
  278. filldir_t filldir)
  279. {
  280. struct inode *inode = filp->f_path.dentry->d_inode;
  281. xfs_inode_t *ip = XFS_I(inode);
  282. struct hack_callback buf;
  283. struct hack_dirent *de;
  284. int error;
  285. loff_t size;
  286. int eof = 0;
  287. xfs_off_t start_offset, curr_offset, offset;
  288. /*
  289. * Try fairly hard to get memory
  290. */
  291. buf.len = PAGE_CACHE_SIZE;
  292. do {
  293. buf.dirent = kmalloc(buf.len, GFP_KERNEL);
  294. if (buf.dirent)
  295. break;
  296. buf.len >>= 1;
  297. } while (buf.len >= 1024);
  298. if (!buf.dirent)
  299. return -ENOMEM;
  300. curr_offset = filp->f_pos;
  301. if (curr_offset == 0x7fffffff)
  302. offset = 0xffffffff;
  303. else
  304. offset = filp->f_pos;
  305. while (!eof) {
  306. unsigned int reclen;
  307. start_offset = offset;
  308. buf.used = 0;
  309. error = -xfs_readdir(ip, &buf, buf.len, &offset,
  310. xfs_hack_filldir);
  311. if (error || offset == start_offset) {
  312. size = 0;
  313. break;
  314. }
  315. size = buf.used;
  316. de = (struct hack_dirent *)buf.dirent;
  317. curr_offset = de->offset /* & 0x7fffffff */;
  318. while (size > 0) {
  319. if (filldir(dirent, de->name, de->namlen,
  320. curr_offset & 0x7fffffff,
  321. de->ino, de->d_type)) {
  322. goto done;
  323. }
  324. reclen = ALIGN(sizeof(struct hack_dirent) + de->namlen,
  325. sizeof(u64));
  326. size -= reclen;
  327. de = (struct hack_dirent *)((char *)de + reclen);
  328. curr_offset = de->offset /* & 0x7fffffff */;
  329. }
  330. }
  331. done:
  332. if (!error) {
  333. if (size == 0)
  334. filp->f_pos = offset & 0x7fffffff;
  335. else if (de)
  336. filp->f_pos = curr_offset;
  337. }
  338. kfree(buf.dirent);
  339. return error;
  340. }
  341. #endif
  342. STATIC int
  343. xfs_file_mmap(
  344. struct file *filp,
  345. struct vm_area_struct *vma)
  346. {
  347. vma->vm_ops = &xfs_file_vm_ops;
  348. vma->vm_flags |= VM_CAN_NONLINEAR;
  349. #ifdef CONFIG_XFS_DMAPI
  350. if (XFS_M(filp->f_path.dentry->d_inode->i_sb)->m_flags & XFS_MOUNT_DMAPI)
  351. vma->vm_ops = &xfs_dmapi_file_vm_ops;
  352. #endif /* CONFIG_XFS_DMAPI */
  353. file_accessed(filp);
  354. return 0;
  355. }
  356. STATIC long
  357. xfs_file_ioctl(
  358. struct file *filp,
  359. unsigned int cmd,
  360. unsigned long p)
  361. {
  362. int error;
  363. struct inode *inode = filp->f_path.dentry->d_inode;
  364. error = xfs_ioctl(XFS_I(inode), filp, 0, cmd, (void __user *)p);
  365. xfs_iflags_set(XFS_I(inode), XFS_IMODIFIED);
  366. /* NOTE: some of the ioctl's return positive #'s as a
  367. * byte count indicating success, such as
  368. * readlink_by_handle. So we don't "sign flip"
  369. * like most other routines. This means true
  370. * errors need to be returned as a negative value.
  371. */
  372. return error;
  373. }
  374. STATIC long
  375. xfs_file_ioctl_invis(
  376. struct file *filp,
  377. unsigned int cmd,
  378. unsigned long p)
  379. {
  380. int error;
  381. struct inode *inode = filp->f_path.dentry->d_inode;
  382. error = xfs_ioctl(XFS_I(inode), filp, IO_INVIS, cmd, (void __user *)p);
  383. xfs_iflags_set(XFS_I(inode), XFS_IMODIFIED);
  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 CONFIG_XFS_DMAPI
  393. #ifdef HAVE_VMOP_MPROTECT
  394. STATIC int
  395. xfs_vm_mprotect(
  396. struct vm_area_struct *vma,
  397. unsigned int newflags)
  398. {
  399. struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
  400. struct xfs_mount *mp = XFS_M(inode->i_sb);
  401. int error = 0;
  402. if (mp->m_flags & XFS_MOUNT_DMAPI) {
  403. if ((vma->vm_flags & VM_MAYSHARE) &&
  404. (newflags & VM_WRITE) && !(vma->vm_flags & VM_WRITE))
  405. error = XFS_SEND_MMAP(mp, vma, VM_WRITE);
  406. }
  407. return error;
  408. }
  409. #endif /* HAVE_VMOP_MPROTECT */
  410. #endif /* CONFIG_XFS_DMAPI */
  411. #ifdef HAVE_FOP_OPEN_EXEC
  412. /* If the user is attempting to execute a file that is offline then
  413. * we have to trigger a DMAPI READ event before the file is marked as busy
  414. * otherwise the invisible I/O will not be able to write to the file to bring
  415. * it back online.
  416. */
  417. STATIC int
  418. xfs_file_open_exec(
  419. struct inode *inode)
  420. {
  421. struct xfs_mount *mp = XFS_M(inode->i_sb);
  422. if (unlikely(mp->m_flags & XFS_MOUNT_DMAPI)) {
  423. if (DM_EVENT_ENABLED(XFS_I(inode), DM_EVENT_READ)) {
  424. bhv_vnode_t *vp = vn_from_inode(inode);
  425. return -XFS_SEND_DATA(mp, DM_EVENT_READ,
  426. vp, 0, 0, 0, NULL);
  427. }
  428. }
  429. return 0;
  430. }
  431. #endif /* HAVE_FOP_OPEN_EXEC */
  432. /*
  433. * mmap()d file has taken write protection fault and is being made
  434. * writable. We can set the page state up correctly for a writable
  435. * page, which means we can do correct delalloc accounting (ENOSPC
  436. * checking!) and unwritten extent mapping.
  437. */
  438. STATIC int
  439. xfs_vm_page_mkwrite(
  440. struct vm_area_struct *vma,
  441. struct page *page)
  442. {
  443. return block_page_mkwrite(vma, page, xfs_get_blocks);
  444. }
  445. const struct file_operations xfs_file_operations = {
  446. .llseek = generic_file_llseek,
  447. .read = do_sync_read,
  448. .write = do_sync_write,
  449. .aio_read = xfs_file_aio_read,
  450. .aio_write = xfs_file_aio_write,
  451. .splice_read = xfs_file_splice_read,
  452. .splice_write = xfs_file_splice_write,
  453. .unlocked_ioctl = xfs_file_ioctl,
  454. #ifdef CONFIG_COMPAT
  455. .compat_ioctl = xfs_file_compat_ioctl,
  456. #endif
  457. .mmap = xfs_file_mmap,
  458. .open = xfs_file_open,
  459. .release = xfs_file_release,
  460. .fsync = xfs_file_fsync,
  461. #ifdef HAVE_FOP_OPEN_EXEC
  462. .open_exec = xfs_file_open_exec,
  463. #endif
  464. };
  465. const struct file_operations xfs_invis_file_operations = {
  466. .llseek = generic_file_llseek,
  467. .read = do_sync_read,
  468. .write = do_sync_write,
  469. .aio_read = xfs_file_aio_read_invis,
  470. .aio_write = xfs_file_aio_write_invis,
  471. .splice_read = xfs_file_splice_read_invis,
  472. .splice_write = xfs_file_splice_write_invis,
  473. .unlocked_ioctl = xfs_file_ioctl_invis,
  474. #ifdef CONFIG_COMPAT
  475. .compat_ioctl = xfs_file_compat_invis_ioctl,
  476. #endif
  477. .mmap = xfs_file_mmap,
  478. .open = xfs_file_open,
  479. .release = xfs_file_release,
  480. .fsync = xfs_file_fsync,
  481. };
  482. const struct file_operations xfs_dir_file_operations = {
  483. .read = generic_read_dir,
  484. .readdir = xfs_file_readdir,
  485. .unlocked_ioctl = xfs_file_ioctl,
  486. #ifdef CONFIG_COMPAT
  487. .compat_ioctl = xfs_file_compat_ioctl,
  488. #endif
  489. .fsync = xfs_file_fsync,
  490. };
  491. static struct vm_operations_struct xfs_file_vm_ops = {
  492. .fault = filemap_fault,
  493. .page_mkwrite = xfs_vm_page_mkwrite,
  494. };
  495. #ifdef CONFIG_XFS_DMAPI
  496. static struct vm_operations_struct xfs_dmapi_file_vm_ops = {
  497. .fault = xfs_vm_fault,
  498. .page_mkwrite = xfs_vm_page_mkwrite,
  499. #ifdef HAVE_VMOP_MPROTECT
  500. .mprotect = xfs_vm_mprotect,
  501. #endif
  502. };
  503. #endif /* CONFIG_XFS_DMAPI */