xfs_file.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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 "xfs_da_btree.h"
  42. #include <linux/dcache.h>
  43. #include <linux/smp_lock.h>
  44. static struct vm_operations_struct xfs_file_vm_ops;
  45. STATIC_INLINE ssize_t
  46. __xfs_file_read(
  47. struct kiocb *iocb,
  48. const struct iovec *iov,
  49. unsigned long nr_segs,
  50. int ioflags,
  51. loff_t pos)
  52. {
  53. struct file *file = iocb->ki_filp;
  54. BUG_ON(iocb->ki_pos != pos);
  55. if (unlikely(file->f_flags & O_DIRECT))
  56. ioflags |= IO_ISDIRECT;
  57. return xfs_read(XFS_I(file->f_path.dentry->d_inode), iocb, iov,
  58. nr_segs, &iocb->ki_pos, ioflags);
  59. }
  60. STATIC ssize_t
  61. xfs_file_aio_read(
  62. struct kiocb *iocb,
  63. const struct iovec *iov,
  64. unsigned long nr_segs,
  65. loff_t pos)
  66. {
  67. return __xfs_file_read(iocb, iov, nr_segs, IO_ISAIO, pos);
  68. }
  69. STATIC ssize_t
  70. xfs_file_aio_read_invis(
  71. struct kiocb *iocb,
  72. const struct iovec *iov,
  73. unsigned long nr_segs,
  74. loff_t pos)
  75. {
  76. return __xfs_file_read(iocb, iov, nr_segs, IO_ISAIO|IO_INVIS, pos);
  77. }
  78. STATIC_INLINE ssize_t
  79. __xfs_file_write(
  80. struct kiocb *iocb,
  81. const struct iovec *iov,
  82. unsigned long nr_segs,
  83. int ioflags,
  84. loff_t pos)
  85. {
  86. struct file *file = iocb->ki_filp;
  87. BUG_ON(iocb->ki_pos != pos);
  88. if (unlikely(file->f_flags & O_DIRECT))
  89. ioflags |= IO_ISDIRECT;
  90. return xfs_write(XFS_I(file->f_mapping->host), iocb, iov, nr_segs,
  91. &iocb->ki_pos, ioflags);
  92. }
  93. STATIC ssize_t
  94. xfs_file_aio_write(
  95. struct kiocb *iocb,
  96. const struct iovec *iov,
  97. unsigned long nr_segs,
  98. loff_t pos)
  99. {
  100. return __xfs_file_write(iocb, iov, nr_segs, IO_ISAIO, pos);
  101. }
  102. STATIC ssize_t
  103. xfs_file_aio_write_invis(
  104. struct kiocb *iocb,
  105. const struct iovec *iov,
  106. unsigned long nr_segs,
  107. loff_t pos)
  108. {
  109. return __xfs_file_write(iocb, iov, nr_segs, IO_ISAIO|IO_INVIS, pos);
  110. }
  111. STATIC ssize_t
  112. xfs_file_splice_read(
  113. struct file *infilp,
  114. loff_t *ppos,
  115. struct pipe_inode_info *pipe,
  116. size_t len,
  117. unsigned int flags)
  118. {
  119. return xfs_splice_read(XFS_I(infilp->f_path.dentry->d_inode),
  120. infilp, ppos, pipe, len, flags, 0);
  121. }
  122. STATIC ssize_t
  123. xfs_file_splice_read_invis(
  124. struct file *infilp,
  125. loff_t *ppos,
  126. struct pipe_inode_info *pipe,
  127. size_t len,
  128. unsigned int flags)
  129. {
  130. return xfs_splice_read(XFS_I(infilp->f_path.dentry->d_inode),
  131. infilp, ppos, pipe, len, flags, IO_INVIS);
  132. }
  133. STATIC ssize_t
  134. xfs_file_splice_write(
  135. struct pipe_inode_info *pipe,
  136. struct file *outfilp,
  137. loff_t *ppos,
  138. size_t len,
  139. unsigned int flags)
  140. {
  141. return xfs_splice_write(XFS_I(outfilp->f_path.dentry->d_inode),
  142. pipe, outfilp, ppos, len, flags, 0);
  143. }
  144. STATIC ssize_t
  145. xfs_file_splice_write_invis(
  146. struct pipe_inode_info *pipe,
  147. struct file *outfilp,
  148. loff_t *ppos,
  149. size_t len,
  150. unsigned int flags)
  151. {
  152. return xfs_splice_write(XFS_I(outfilp->f_path.dentry->d_inode),
  153. pipe, outfilp, ppos, len, flags, IO_INVIS);
  154. }
  155. STATIC int
  156. xfs_file_open(
  157. struct inode *inode,
  158. struct file *file)
  159. {
  160. if (!(file->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS)
  161. return -EFBIG;
  162. if (XFS_FORCED_SHUTDOWN(XFS_M(inode->i_sb)))
  163. return -EIO;
  164. return 0;
  165. }
  166. STATIC int
  167. xfs_dir_open(
  168. struct inode *inode,
  169. struct file *file)
  170. {
  171. struct xfs_inode *ip = XFS_I(inode);
  172. int mode;
  173. int error;
  174. error = xfs_file_open(inode, file);
  175. if (error)
  176. return error;
  177. /*
  178. * If there are any blocks, read-ahead block 0 as we're almost
  179. * certain to have the next operation be a read there.
  180. */
  181. mode = xfs_ilock_map_shared(ip);
  182. if (ip->i_d.di_nextents > 0)
  183. xfs_da_reada_buf(NULL, ip, 0, XFS_DATA_FORK);
  184. xfs_iunlock(ip, mode);
  185. return 0;
  186. }
  187. STATIC int
  188. xfs_file_release(
  189. struct inode *inode,
  190. struct file *filp)
  191. {
  192. return -xfs_release(XFS_I(inode));
  193. }
  194. /*
  195. * We ignore the datasync flag here because a datasync is effectively
  196. * identical to an fsync. That is, datasync implies that we need to write
  197. * only the metadata needed to be able to access the data that is written
  198. * if we crash after the call completes. Hence if we are writing beyond
  199. * EOF we have to log the inode size change as well, which makes it a
  200. * full fsync. If we don't write beyond EOF, the inode core will be
  201. * clean in memory and so we don't need to log the inode, just like
  202. * fsync.
  203. */
  204. STATIC int
  205. xfs_file_fsync(
  206. struct file *filp,
  207. struct dentry *dentry,
  208. int datasync)
  209. {
  210. xfs_iflags_clear(XFS_I(dentry->d_inode), XFS_ITRUNCATED);
  211. return -xfs_fsync(XFS_I(dentry->d_inode));
  212. }
  213. STATIC int
  214. xfs_file_readdir(
  215. struct file *filp,
  216. void *dirent,
  217. filldir_t filldir)
  218. {
  219. struct inode *inode = filp->f_path.dentry->d_inode;
  220. xfs_inode_t *ip = XFS_I(inode);
  221. int error;
  222. size_t bufsize;
  223. /*
  224. * The Linux API doesn't pass down the total size of the buffer
  225. * we read into down to the filesystem. With the filldir concept
  226. * it's not needed for correct information, but the XFS dir2 leaf
  227. * code wants an estimate of the buffer size to calculate it's
  228. * readahead window and size the buffers used for mapping to
  229. * physical blocks.
  230. *
  231. * Try to give it an estimate that's good enough, maybe at some
  232. * point we can change the ->readdir prototype to include the
  233. * buffer size.
  234. */
  235. bufsize = (size_t)min_t(loff_t, PAGE_SIZE, inode->i_size);
  236. error = xfs_readdir(ip, dirent, bufsize,
  237. (xfs_off_t *)&filp->f_pos, filldir);
  238. if (error)
  239. return -error;
  240. return 0;
  241. }
  242. STATIC int
  243. xfs_file_mmap(
  244. struct file *filp,
  245. struct vm_area_struct *vma)
  246. {
  247. vma->vm_ops = &xfs_file_vm_ops;
  248. vma->vm_flags |= VM_CAN_NONLINEAR;
  249. file_accessed(filp);
  250. return 0;
  251. }
  252. STATIC long
  253. xfs_file_ioctl(
  254. struct file *filp,
  255. unsigned int cmd,
  256. unsigned long p)
  257. {
  258. int error;
  259. struct inode *inode = filp->f_path.dentry->d_inode;
  260. error = xfs_ioctl(XFS_I(inode), filp, 0, cmd, (void __user *)p);
  261. xfs_iflags_set(XFS_I(inode), XFS_IMODIFIED);
  262. /* NOTE: some of the ioctl's return positive #'s as a
  263. * byte count indicating success, such as
  264. * readlink_by_handle. So we don't "sign flip"
  265. * like most other routines. This means true
  266. * errors need to be returned as a negative value.
  267. */
  268. return error;
  269. }
  270. STATIC long
  271. xfs_file_ioctl_invis(
  272. struct file *filp,
  273. unsigned int cmd,
  274. unsigned long p)
  275. {
  276. int error;
  277. struct inode *inode = filp->f_path.dentry->d_inode;
  278. error = xfs_ioctl(XFS_I(inode), filp, IO_INVIS, cmd, (void __user *)p);
  279. xfs_iflags_set(XFS_I(inode), XFS_IMODIFIED);
  280. /* NOTE: some of the ioctl's return positive #'s as a
  281. * byte count indicating success, such as
  282. * readlink_by_handle. So we don't "sign flip"
  283. * like most other routines. This means true
  284. * errors need to be returned as a negative value.
  285. */
  286. return error;
  287. }
  288. /*
  289. * mmap()d file has taken write protection fault and is being made
  290. * writable. We can set the page state up correctly for a writable
  291. * page, which means we can do correct delalloc accounting (ENOSPC
  292. * checking!) and unwritten extent mapping.
  293. */
  294. STATIC int
  295. xfs_vm_page_mkwrite(
  296. struct vm_area_struct *vma,
  297. struct page *page)
  298. {
  299. return block_page_mkwrite(vma, page, xfs_get_blocks);
  300. }
  301. const struct file_operations xfs_file_operations = {
  302. .llseek = generic_file_llseek,
  303. .read = do_sync_read,
  304. .write = do_sync_write,
  305. .aio_read = xfs_file_aio_read,
  306. .aio_write = xfs_file_aio_write,
  307. .splice_read = xfs_file_splice_read,
  308. .splice_write = xfs_file_splice_write,
  309. .unlocked_ioctl = xfs_file_ioctl,
  310. #ifdef CONFIG_COMPAT
  311. .compat_ioctl = xfs_file_compat_ioctl,
  312. #endif
  313. .mmap = xfs_file_mmap,
  314. .open = xfs_file_open,
  315. .release = xfs_file_release,
  316. .fsync = xfs_file_fsync,
  317. #ifdef HAVE_FOP_OPEN_EXEC
  318. .open_exec = xfs_file_open_exec,
  319. #endif
  320. };
  321. const struct file_operations xfs_invis_file_operations = {
  322. .llseek = generic_file_llseek,
  323. .read = do_sync_read,
  324. .write = do_sync_write,
  325. .aio_read = xfs_file_aio_read_invis,
  326. .aio_write = xfs_file_aio_write_invis,
  327. .splice_read = xfs_file_splice_read_invis,
  328. .splice_write = xfs_file_splice_write_invis,
  329. .unlocked_ioctl = xfs_file_ioctl_invis,
  330. #ifdef CONFIG_COMPAT
  331. .compat_ioctl = xfs_file_compat_invis_ioctl,
  332. #endif
  333. .mmap = xfs_file_mmap,
  334. .open = xfs_file_open,
  335. .release = xfs_file_release,
  336. .fsync = xfs_file_fsync,
  337. };
  338. const struct file_operations xfs_dir_file_operations = {
  339. .open = xfs_dir_open,
  340. .read = generic_read_dir,
  341. .readdir = xfs_file_readdir,
  342. .llseek = generic_file_llseek,
  343. .unlocked_ioctl = xfs_file_ioctl,
  344. #ifdef CONFIG_COMPAT
  345. .compat_ioctl = xfs_file_compat_ioctl,
  346. #endif
  347. .fsync = xfs_file_fsync,
  348. };
  349. static struct vm_operations_struct xfs_file_vm_ops = {
  350. .fault = filemap_fault,
  351. .page_mkwrite = xfs_vm_page_mkwrite,
  352. };