sendfile: remove .sendfile from filesystems that use generic_file_sendfile()
[deliverable/linux.git] / fs / xfs / linux-2.6 / xfs_file.c
CommitLineData
1da177e4 1/*
7b718769
NS
2 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
1da177e4 4 *
7b718769
NS
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
1da177e4
LT
7 * published by the Free Software Foundation.
8 *
7b718769
NS
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.
1da177e4 13 *
7b718769
NS
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
1da177e4 17 */
1da177e4 18#include "xfs.h"
a844f451 19#include "xfs_bit.h"
1da177e4 20#include "xfs_log.h"
a844f451 21#include "xfs_inum.h"
1da177e4 22#include "xfs_sb.h"
a844f451 23#include "xfs_ag.h"
1da177e4
LT
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"
1da177e4
LT
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
41#include <linux/dcache.h>
42#include <linux/smp_lock.h>
43
3562fd45 44static struct vm_operations_struct xfs_file_vm_ops;
6fac0cb4 45#ifdef CONFIG_XFS_DMAPI
3562fd45 46static struct vm_operations_struct xfs_dmapi_file_vm_ops;
6fac0cb4 47#endif
1da177e4 48
7989cb8e 49STATIC_INLINE ssize_t
3562fd45 50__xfs_file_read(
1da177e4 51 struct kiocb *iocb,
027445c3
BP
52 const struct iovec *iov,
53 unsigned long nr_segs,
1da177e4 54 int ioflags,
1da177e4
LT
55 loff_t pos)
56{
1da177e4 57 struct file *file = iocb->ki_filp;
e678fb0d 58 bhv_vnode_t *vp = vn_from_inode(file->f_path.dentry->d_inode);
1da177e4
LT
59
60 BUG_ON(iocb->ki_pos != pos);
1da177e4
LT
61 if (unlikely(file->f_flags & O_DIRECT))
62 ioflags |= IO_ISDIRECT;
027445c3
BP
63 return bhv_vop_read(vp, iocb, iov, nr_segs, &iocb->ki_pos,
64 ioflags, NULL);
1da177e4
LT
65}
66
1da177e4 67STATIC ssize_t
3562fd45 68xfs_file_aio_read(
1da177e4 69 struct kiocb *iocb,
027445c3
BP
70 const struct iovec *iov,
71 unsigned long nr_segs,
1da177e4
LT
72 loff_t pos)
73{
027445c3 74 return __xfs_file_read(iocb, iov, nr_segs, IO_ISAIO, pos);
1da177e4
LT
75}
76
77STATIC ssize_t
3562fd45 78xfs_file_aio_read_invis(
1da177e4 79 struct kiocb *iocb,
027445c3
BP
80 const struct iovec *iov,
81 unsigned long nr_segs,
1da177e4
LT
82 loff_t pos)
83{
027445c3 84 return __xfs_file_read(iocb, iov, nr_segs, IO_ISAIO|IO_INVIS, pos);
1da177e4
LT
85}
86
7989cb8e 87STATIC_INLINE ssize_t
3562fd45 88__xfs_file_write(
027445c3
BP
89 struct kiocb *iocb,
90 const struct iovec *iov,
91 unsigned long nr_segs,
92 int ioflags,
93 loff_t pos)
1da177e4 94{
1da177e4
LT
95 struct file *file = iocb->ki_filp;
96 struct inode *inode = file->f_mapping->host;
67fcaa73 97 bhv_vnode_t *vp = vn_from_inode(inode);
1da177e4
LT
98
99 BUG_ON(iocb->ki_pos != pos);
100 if (unlikely(file->f_flags & O_DIRECT))
101 ioflags |= IO_ISDIRECT;
027445c3
BP
102 return bhv_vop_write(vp, iocb, iov, nr_segs, &iocb->ki_pos,
103 ioflags, NULL);
1da177e4
LT
104}
105
1da177e4 106STATIC ssize_t
3562fd45 107xfs_file_aio_write(
1da177e4 108 struct kiocb *iocb,
027445c3
BP
109 const struct iovec *iov,
110 unsigned long nr_segs,
1da177e4
LT
111 loff_t pos)
112{
027445c3 113 return __xfs_file_write(iocb, iov, nr_segs, IO_ISAIO, pos);
1da177e4
LT
114}
115
116STATIC ssize_t
3562fd45 117xfs_file_aio_write_invis(
1da177e4 118 struct kiocb *iocb,
027445c3
BP
119 const struct iovec *iov,
120 unsigned long nr_segs,
1da177e4
LT
121 loff_t pos)
122{
027445c3 123 return __xfs_file_write(iocb, iov, nr_segs, IO_ISAIO|IO_INVIS, pos);
1da177e4
LT
124}
125
1b895840
NS
126STATIC ssize_t
127xfs_file_splice_read(
128 struct file *infilp,
cbb7e577 129 loff_t *ppos,
3a326a2c 130 struct pipe_inode_info *pipe,
1b895840
NS
131 size_t len,
132 unsigned int flags)
133{
e678fb0d 134 return bhv_vop_splice_read(vn_from_inode(infilp->f_path.dentry->d_inode),
67fcaa73 135 infilp, ppos, pipe, len, flags, 0, NULL);
1b895840
NS
136}
137
138STATIC ssize_t
139xfs_file_splice_read_invis(
140 struct file *infilp,
cbb7e577 141 loff_t *ppos,
3a326a2c 142 struct pipe_inode_info *pipe,
1b895840
NS
143 size_t len,
144 unsigned int flags)
145{
e678fb0d 146 return bhv_vop_splice_read(vn_from_inode(infilp->f_path.dentry->d_inode),
67fcaa73
NS
147 infilp, ppos, pipe, len, flags, IO_INVIS,
148 NULL);
1b895840
NS
149}
150
151STATIC ssize_t
152xfs_file_splice_write(
3a326a2c 153 struct pipe_inode_info *pipe,
1b895840 154 struct file *outfilp,
cbb7e577 155 loff_t *ppos,
1b895840
NS
156 size_t len,
157 unsigned int flags)
158{
e678fb0d 159 return bhv_vop_splice_write(vn_from_inode(outfilp->f_path.dentry->d_inode),
67fcaa73 160 pipe, outfilp, ppos, len, flags, 0, NULL);
1b895840
NS
161}
162
163STATIC ssize_t
164xfs_file_splice_write_invis(
3a326a2c 165 struct pipe_inode_info *pipe,
1b895840 166 struct file *outfilp,
cbb7e577 167 loff_t *ppos,
1b895840
NS
168 size_t len,
169 unsigned int flags)
170{
e678fb0d 171 return bhv_vop_splice_write(vn_from_inode(outfilp->f_path.dentry->d_inode),
67fcaa73
NS
172 pipe, outfilp, ppos, len, flags, IO_INVIS,
173 NULL);
1b895840 174}
1da177e4
LT
175
176STATIC int
3562fd45 177xfs_file_open(
1da177e4
LT
178 struct inode *inode,
179 struct file *filp)
180{
1da177e4
LT
181 if (!(filp->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS)
182 return -EFBIG;
67fcaa73 183 return -bhv_vop_open(vn_from_inode(inode), NULL);
1da177e4
LT
184}
185
7d4fb40a
NS
186STATIC int
187xfs_file_close(
1e788f8d
AD
188 struct file *filp,
189 fl_owner_t id)
7d4fb40a 190{
e678fb0d 191 return -bhv_vop_close(vn_from_inode(filp->f_path.dentry->d_inode), 0,
67fcaa73 192 file_count(filp) > 1 ? L_FALSE : L_TRUE, NULL);
7d4fb40a
NS
193}
194
1da177e4 195STATIC int
3562fd45 196xfs_file_release(
1da177e4
LT
197 struct inode *inode,
198 struct file *filp)
199{
67fcaa73 200 bhv_vnode_t *vp = vn_from_inode(inode);
1da177e4
LT
201
202 if (vp)
67fcaa73
NS
203 return -bhv_vop_release(vp);
204 return 0;
1da177e4
LT
205}
206
1da177e4 207STATIC int
3562fd45 208xfs_file_fsync(
1da177e4
LT
209 struct file *filp,
210 struct dentry *dentry,
211 int datasync)
212{
67fcaa73 213 bhv_vnode_t *vp = vn_from_inode(dentry->d_inode);
1da177e4
LT
214 int flags = FSYNC_WAIT;
215
216 if (datasync)
217 flags |= FSYNC_DATA;
7d4fb40a
NS
218 if (VN_TRUNC(vp))
219 VUNTRUNCATE(vp);
67fcaa73 220 return -bhv_vop_fsync(vp, flags, NULL, (xfs_off_t)0, (xfs_off_t)-1);
1da177e4
LT
221}
222
bb3f724e 223#ifdef CONFIG_XFS_DMAPI
bb3f724e 224STATIC struct page *
3562fd45 225xfs_vm_nopage(
bb3f724e
DR
226 struct vm_area_struct *area,
227 unsigned long address,
228 int *type)
229{
e678fb0d 230 struct inode *inode = area->vm_file->f_path.dentry->d_inode;
67fcaa73 231 bhv_vnode_t *vp = vn_from_inode(inode);
bb3f724e
DR
232
233 ASSERT_ALWAYS(vp->v_vfsp->vfs_flag & VFS_DMI);
fbc1462b 234 if (XFS_SEND_MMAP(XFS_VFSTOM(vp->v_vfsp), area, 0))
bb3f724e 235 return NULL;
bb3f724e
DR
236 return filemap_nopage(area, address, type);
237}
bb3f724e
DR
238#endif /* CONFIG_XFS_DMAPI */
239
1da177e4 240STATIC int
3562fd45 241xfs_file_readdir(
1da177e4
LT
242 struct file *filp,
243 void *dirent,
244 filldir_t filldir)
245{
246 int error = 0;
e678fb0d 247 bhv_vnode_t *vp = vn_from_inode(filp->f_path.dentry->d_inode);
1da177e4
LT
248 uio_t uio;
249 iovec_t iov;
250 int eof = 0;
251 caddr_t read_buf;
252 int namelen, size = 0;
253 size_t rlen = PAGE_CACHE_SIZE;
254 xfs_off_t start_offset, curr_offset;
255 xfs_dirent_t *dbp = NULL;
256
1da177e4
LT
257 /* Try fairly hard to get memory */
258 do {
f52720ca 259 if ((read_buf = kmalloc(rlen, GFP_KERNEL)))
1da177e4
LT
260 break;
261 rlen >>= 1;
262 } while (rlen >= 1024);
263
264 if (read_buf == NULL)
265 return -ENOMEM;
266
267 uio.uio_iov = &iov;
268 uio.uio_segflg = UIO_SYSSPACE;
269 curr_offset = filp->f_pos;
270 if (filp->f_pos != 0x7fffffff)
271 uio.uio_offset = filp->f_pos;
272 else
273 uio.uio_offset = 0xffffffff;
274
275 while (!eof) {
276 uio.uio_resid = iov.iov_len = rlen;
277 iov.iov_base = read_buf;
278 uio.uio_iovcnt = 1;
279
280 start_offset = uio.uio_offset;
281
67fcaa73 282 error = bhv_vop_readdir(vp, &uio, NULL, &eof);
1da177e4
LT
283 if ((uio.uio_offset == start_offset) || error) {
284 size = 0;
285 break;
286 }
287
288 size = rlen - uio.uio_resid;
289 dbp = (xfs_dirent_t *)read_buf;
290 while (size > 0) {
291 namelen = strlen(dbp->d_name);
292
293 if (filldir(dirent, dbp->d_name, namelen,
294 (loff_t) curr_offset & 0x7fffffff,
295 (ino_t) dbp->d_ino,
296 DT_UNKNOWN)) {
297 goto done;
298 }
299 size -= dbp->d_reclen;
300 curr_offset = (loff_t)dbp->d_off /* & 0x7fffffff */;
1b895840 301 dbp = (xfs_dirent_t *)((char *)dbp + dbp->d_reclen);
1da177e4
LT
302 }
303 }
304done:
305 if (!error) {
306 if (size == 0)
307 filp->f_pos = uio.uio_offset & 0x7fffffff;
308 else if (dbp)
309 filp->f_pos = curr_offset;
310 }
311
312 kfree(read_buf);
313 return -error;
314}
315
1da177e4 316STATIC int
3562fd45 317xfs_file_mmap(
1da177e4
LT
318 struct file *filp,
319 struct vm_area_struct *vma)
320{
3562fd45 321 vma->vm_ops = &xfs_file_vm_ops;
6fac0cb4 322
6fac0cb4 323#ifdef CONFIG_XFS_DMAPI
e678fb0d 324 if (vn_from_inode(filp->f_path.dentry->d_inode)->v_vfsp->vfs_flag & VFS_DMI)
3562fd45 325 vma->vm_ops = &xfs_dmapi_file_vm_ops;
bb3f724e 326#endif /* CONFIG_XFS_DMAPI */
1da177e4 327
fbc1462b 328 file_accessed(filp);
1da177e4
LT
329 return 0;
330}
331
1da177e4 332STATIC long
3562fd45 333xfs_file_ioctl(
1da177e4
LT
334 struct file *filp,
335 unsigned int cmd,
67fcaa73 336 unsigned long p)
1da177e4
LT
337{
338 int error;
e678fb0d 339 struct inode *inode = filp->f_path.dentry->d_inode;
67fcaa73 340 bhv_vnode_t *vp = vn_from_inode(inode);
1da177e4 341
67fcaa73 342 error = bhv_vop_ioctl(vp, inode, filp, 0, cmd, (void __user *)p);
1da177e4
LT
343 VMODIFY(vp);
344
345 /* NOTE: some of the ioctl's return positive #'s as a
346 * byte count indicating success, such as
347 * readlink_by_handle. So we don't "sign flip"
348 * like most other routines. This means true
349 * errors need to be returned as a negative value.
350 */
351 return error;
352}
353
354STATIC long
3562fd45 355xfs_file_ioctl_invis(
1da177e4
LT
356 struct file *filp,
357 unsigned int cmd,
67fcaa73 358 unsigned long p)
1da177e4 359{
1b895840 360 int error;
e678fb0d 361 struct inode *inode = filp->f_path.dentry->d_inode;
67fcaa73 362 bhv_vnode_t *vp = vn_from_inode(inode);
1da177e4 363
67fcaa73 364 error = bhv_vop_ioctl(vp, inode, filp, IO_INVIS, cmd, (void __user *)p);
1da177e4
LT
365 VMODIFY(vp);
366
367 /* NOTE: some of the ioctl's return positive #'s as a
368 * byte count indicating success, such as
369 * readlink_by_handle. So we don't "sign flip"
370 * like most other routines. This means true
371 * errors need to be returned as a negative value.
372 */
373 return error;
374}
375
bb3f724e 376#ifdef CONFIG_XFS_DMAPI
1da177e4
LT
377#ifdef HAVE_VMOP_MPROTECT
378STATIC int
3562fd45 379xfs_vm_mprotect(
1da177e4
LT
380 struct vm_area_struct *vma,
381 unsigned int newflags)
382{
e678fb0d 383 bhv_vnode_t *vp = vn_from_inode(vma->vm_file->f_path.dentry->d_inode);
1da177e4
LT
384 int error = 0;
385
386 if (vp->v_vfsp->vfs_flag & VFS_DMI) {
387 if ((vma->vm_flags & VM_MAYSHARE) &&
388 (newflags & VM_WRITE) && !(vma->vm_flags & VM_WRITE)) {
389 xfs_mount_t *mp = XFS_VFSTOM(vp->v_vfsp);
390
391 error = XFS_SEND_MMAP(mp, vma, VM_WRITE);
392 }
393 }
394 return error;
395}
396#endif /* HAVE_VMOP_MPROTECT */
bb3f724e 397#endif /* CONFIG_XFS_DMAPI */
1da177e4
LT
398
399#ifdef HAVE_FOP_OPEN_EXEC
400/* If the user is attempting to execute a file that is offline then
401 * we have to trigger a DMAPI READ event before the file is marked as busy
402 * otherwise the invisible I/O will not be able to write to the file to bring
403 * it back online.
404 */
405STATIC int
3562fd45 406xfs_file_open_exec(
1da177e4
LT
407 struct inode *inode)
408{
67fcaa73 409 bhv_vnode_t *vp = vn_from_inode(inode);
1da177e4 410
1d47bec2
NS
411 if (unlikely(vp->v_vfsp->vfs_flag & VFS_DMI)) {
412 xfs_mount_t *mp = XFS_VFSTOM(vp->v_vfsp);
413 xfs_inode_t *ip = xfs_vtoi(vp);
414
415 if (!ip)
416 return -EINVAL;
417 if (DM_EVENT_ENABLED(vp->v_vfsp, ip, DM_EVENT_READ))
418 return -XFS_SEND_DATA(mp, DM_EVENT_READ, vp,
1da177e4 419 0, 0, 0, NULL);
1da177e4 420 }
1d47bec2 421 return 0;
1da177e4
LT
422}
423#endif /* HAVE_FOP_OPEN_EXEC */
424
4b6f5d20 425const struct file_operations xfs_file_operations = {
1da177e4
LT
426 .llseek = generic_file_llseek,
427 .read = do_sync_read,
bb3f724e 428 .write = do_sync_write,
3562fd45
NS
429 .aio_read = xfs_file_aio_read,
430 .aio_write = xfs_file_aio_write,
1b895840
NS
431 .splice_read = xfs_file_splice_read,
432 .splice_write = xfs_file_splice_write,
3562fd45 433 .unlocked_ioctl = xfs_file_ioctl,
1da177e4 434#ifdef CONFIG_COMPAT
3562fd45 435 .compat_ioctl = xfs_file_compat_ioctl,
1da177e4 436#endif
3562fd45
NS
437 .mmap = xfs_file_mmap,
438 .open = xfs_file_open,
7d4fb40a 439 .flush = xfs_file_close,
3562fd45
NS
440 .release = xfs_file_release,
441 .fsync = xfs_file_fsync,
1da177e4 442#ifdef HAVE_FOP_OPEN_EXEC
3562fd45 443 .open_exec = xfs_file_open_exec,
1da177e4
LT
444#endif
445};
446
4b6f5d20 447const struct file_operations xfs_invis_file_operations = {
1da177e4
LT
448 .llseek = generic_file_llseek,
449 .read = do_sync_read,
bb3f724e 450 .write = do_sync_write,
3562fd45
NS
451 .aio_read = xfs_file_aio_read_invis,
452 .aio_write = xfs_file_aio_write_invis,
1b895840
NS
453 .splice_read = xfs_file_splice_read_invis,
454 .splice_write = xfs_file_splice_write_invis,
3562fd45 455 .unlocked_ioctl = xfs_file_ioctl_invis,
1da177e4 456#ifdef CONFIG_COMPAT
3562fd45 457 .compat_ioctl = xfs_file_compat_invis_ioctl,
1da177e4 458#endif
3562fd45
NS
459 .mmap = xfs_file_mmap,
460 .open = xfs_file_open,
7d4fb40a 461 .flush = xfs_file_close,
3562fd45
NS
462 .release = xfs_file_release,
463 .fsync = xfs_file_fsync,
1da177e4
LT
464};
465
466
4b6f5d20 467const struct file_operations xfs_dir_file_operations = {
1da177e4 468 .read = generic_read_dir,
3562fd45
NS
469 .readdir = xfs_file_readdir,
470 .unlocked_ioctl = xfs_file_ioctl,
d3870398 471#ifdef CONFIG_COMPAT
3562fd45 472 .compat_ioctl = xfs_file_compat_ioctl,
d3870398 473#endif
3562fd45 474 .fsync = xfs_file_fsync,
1da177e4
LT
475};
476
3562fd45 477static struct vm_operations_struct xfs_file_vm_ops = {
1da177e4
LT
478 .nopage = filemap_nopage,
479 .populate = filemap_populate,
6fac0cb4
DR
480};
481
482#ifdef CONFIG_XFS_DMAPI
3562fd45
NS
483static struct vm_operations_struct xfs_dmapi_file_vm_ops = {
484 .nopage = xfs_vm_nopage,
6fac0cb4 485 .populate = filemap_populate,
1da177e4 486#ifdef HAVE_VMOP_MPROTECT
3562fd45 487 .mprotect = xfs_vm_mprotect,
1da177e4
LT
488#endif
489};
6fac0cb4 490#endif /* CONFIG_XFS_DMAPI */
This page took 0.246487 seconds and 5 git commands to generate.