]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - fs/xfs/linux-2.6/xfs_file.c
[PATCH] Remove readv/writev methods and use aio_read/aio_write instead
[mirror_ubuntu-bionic-kernel.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
LT
48
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;
67fcaa73 58 bhv_vnode_t *vp = vn_from_inode(file->f_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
1da177e4 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
1da177e4 126STATIC ssize_t
3562fd45 127xfs_file_sendfile(
1da177e4 128 struct file *filp,
1b895840 129 loff_t *pos,
1da177e4
LT
130 size_t count,
131 read_actor_t actor,
132 void *target)
133{
67fcaa73
NS
134 return bhv_vop_sendfile(vn_from_inode(filp->f_dentry->d_inode),
135 filp, pos, 0, count, actor, target, NULL);
1da177e4
LT
136}
137
1b895840
NS
138STATIC ssize_t
139xfs_file_sendfile_invis(
140 struct file *filp,
141 loff_t *pos,
142 size_t count,
143 read_actor_t actor,
144 void *target)
145{
67fcaa73
NS
146 return bhv_vop_sendfile(vn_from_inode(filp->f_dentry->d_inode),
147 filp, pos, IO_INVIS, count, actor, target, NULL);
1b895840
NS
148}
149
150STATIC ssize_t
151xfs_file_splice_read(
152 struct file *infilp,
cbb7e577 153 loff_t *ppos,
3a326a2c 154 struct pipe_inode_info *pipe,
1b895840
NS
155 size_t len,
156 unsigned int flags)
157{
67fcaa73
NS
158 return bhv_vop_splice_read(vn_from_inode(infilp->f_dentry->d_inode),
159 infilp, ppos, pipe, len, flags, 0, NULL);
1b895840
NS
160}
161
162STATIC ssize_t
163xfs_file_splice_read_invis(
164 struct file *infilp,
cbb7e577 165 loff_t *ppos,
3a326a2c 166 struct pipe_inode_info *pipe,
1b895840
NS
167 size_t len,
168 unsigned int flags)
169{
67fcaa73
NS
170 return bhv_vop_splice_read(vn_from_inode(infilp->f_dentry->d_inode),
171 infilp, ppos, pipe, len, flags, IO_INVIS,
172 NULL);
1b895840
NS
173}
174
175STATIC ssize_t
176xfs_file_splice_write(
3a326a2c 177 struct pipe_inode_info *pipe,
1b895840 178 struct file *outfilp,
cbb7e577 179 loff_t *ppos,
1b895840
NS
180 size_t len,
181 unsigned int flags)
182{
67fcaa73
NS
183 return bhv_vop_splice_write(vn_from_inode(outfilp->f_dentry->d_inode),
184 pipe, outfilp, ppos, len, flags, 0, NULL);
1b895840
NS
185}
186
187STATIC ssize_t
188xfs_file_splice_write_invis(
3a326a2c 189 struct pipe_inode_info *pipe,
1b895840 190 struct file *outfilp,
cbb7e577 191 loff_t *ppos,
1b895840
NS
192 size_t len,
193 unsigned int flags)
194{
67fcaa73
NS
195 return bhv_vop_splice_write(vn_from_inode(outfilp->f_dentry->d_inode),
196 pipe, outfilp, ppos, len, flags, IO_INVIS,
197 NULL);
1b895840 198}
1da177e4
LT
199
200STATIC int
3562fd45 201xfs_file_open(
1da177e4
LT
202 struct inode *inode,
203 struct file *filp)
204{
1da177e4
LT
205 if (!(filp->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS)
206 return -EFBIG;
67fcaa73 207 return -bhv_vop_open(vn_from_inode(inode), NULL);
1da177e4
LT
208}
209
7d4fb40a
NS
210STATIC int
211xfs_file_close(
1e788f8d
AD
212 struct file *filp,
213 fl_owner_t id)
7d4fb40a 214{
67fcaa73
NS
215 return -bhv_vop_close(vn_from_inode(filp->f_dentry->d_inode), 0,
216 file_count(filp) > 1 ? L_FALSE : L_TRUE, NULL);
7d4fb40a
NS
217}
218
1da177e4 219STATIC int
3562fd45 220xfs_file_release(
1da177e4
LT
221 struct inode *inode,
222 struct file *filp)
223{
67fcaa73 224 bhv_vnode_t *vp = vn_from_inode(inode);
1da177e4
LT
225
226 if (vp)
67fcaa73
NS
227 return -bhv_vop_release(vp);
228 return 0;
1da177e4
LT
229}
230
1da177e4 231STATIC int
3562fd45 232xfs_file_fsync(
1da177e4
LT
233 struct file *filp,
234 struct dentry *dentry,
235 int datasync)
236{
67fcaa73 237 bhv_vnode_t *vp = vn_from_inode(dentry->d_inode);
1da177e4
LT
238 int flags = FSYNC_WAIT;
239
240 if (datasync)
241 flags |= FSYNC_DATA;
7d4fb40a
NS
242 if (VN_TRUNC(vp))
243 VUNTRUNCATE(vp);
67fcaa73 244 return -bhv_vop_fsync(vp, flags, NULL, (xfs_off_t)0, (xfs_off_t)-1);
1da177e4
LT
245}
246
bb3f724e 247#ifdef CONFIG_XFS_DMAPI
bb3f724e 248STATIC struct page *
3562fd45 249xfs_vm_nopage(
bb3f724e
DR
250 struct vm_area_struct *area,
251 unsigned long address,
252 int *type)
253{
254 struct inode *inode = area->vm_file->f_dentry->d_inode;
67fcaa73 255 bhv_vnode_t *vp = vn_from_inode(inode);
bb3f724e
DR
256
257 ASSERT_ALWAYS(vp->v_vfsp->vfs_flag & VFS_DMI);
fbc1462b 258 if (XFS_SEND_MMAP(XFS_VFSTOM(vp->v_vfsp), area, 0))
bb3f724e 259 return NULL;
bb3f724e
DR
260 return filemap_nopage(area, address, type);
261}
bb3f724e
DR
262#endif /* CONFIG_XFS_DMAPI */
263
1da177e4 264STATIC int
3562fd45 265xfs_file_readdir(
1da177e4
LT
266 struct file *filp,
267 void *dirent,
268 filldir_t filldir)
269{
270 int error = 0;
67fcaa73 271 bhv_vnode_t *vp = vn_from_inode(filp->f_dentry->d_inode);
1da177e4
LT
272 uio_t uio;
273 iovec_t iov;
274 int eof = 0;
275 caddr_t read_buf;
276 int namelen, size = 0;
277 size_t rlen = PAGE_CACHE_SIZE;
278 xfs_off_t start_offset, curr_offset;
279 xfs_dirent_t *dbp = NULL;
280
1da177e4
LT
281 /* Try fairly hard to get memory */
282 do {
f52720ca 283 if ((read_buf = kmalloc(rlen, GFP_KERNEL)))
1da177e4
LT
284 break;
285 rlen >>= 1;
286 } while (rlen >= 1024);
287
288 if (read_buf == NULL)
289 return -ENOMEM;
290
291 uio.uio_iov = &iov;
292 uio.uio_segflg = UIO_SYSSPACE;
293 curr_offset = filp->f_pos;
294 if (filp->f_pos != 0x7fffffff)
295 uio.uio_offset = filp->f_pos;
296 else
297 uio.uio_offset = 0xffffffff;
298
299 while (!eof) {
300 uio.uio_resid = iov.iov_len = rlen;
301 iov.iov_base = read_buf;
302 uio.uio_iovcnt = 1;
303
304 start_offset = uio.uio_offset;
305
67fcaa73 306 error = bhv_vop_readdir(vp, &uio, NULL, &eof);
1da177e4
LT
307 if ((uio.uio_offset == start_offset) || error) {
308 size = 0;
309 break;
310 }
311
312 size = rlen - uio.uio_resid;
313 dbp = (xfs_dirent_t *)read_buf;
314 while (size > 0) {
315 namelen = strlen(dbp->d_name);
316
317 if (filldir(dirent, dbp->d_name, namelen,
318 (loff_t) curr_offset & 0x7fffffff,
319 (ino_t) dbp->d_ino,
320 DT_UNKNOWN)) {
321 goto done;
322 }
323 size -= dbp->d_reclen;
324 curr_offset = (loff_t)dbp->d_off /* & 0x7fffffff */;
1b895840 325 dbp = (xfs_dirent_t *)((char *)dbp + dbp->d_reclen);
1da177e4
LT
326 }
327 }
328done:
329 if (!error) {
330 if (size == 0)
331 filp->f_pos = uio.uio_offset & 0x7fffffff;
332 else if (dbp)
333 filp->f_pos = curr_offset;
334 }
335
336 kfree(read_buf);
337 return -error;
338}
339
1da177e4 340STATIC int
3562fd45 341xfs_file_mmap(
1da177e4
LT
342 struct file *filp,
343 struct vm_area_struct *vma)
344{
3562fd45 345 vma->vm_ops = &xfs_file_vm_ops;
6fac0cb4 346
6fac0cb4 347#ifdef CONFIG_XFS_DMAPI
fbc1462b 348 if (vn_from_inode(filp->f_dentry->d_inode)->v_vfsp->vfs_flag & VFS_DMI)
3562fd45 349 vma->vm_ops = &xfs_dmapi_file_vm_ops;
bb3f724e 350#endif /* CONFIG_XFS_DMAPI */
1da177e4 351
fbc1462b 352 file_accessed(filp);
1da177e4
LT
353 return 0;
354}
355
1da177e4 356STATIC long
3562fd45 357xfs_file_ioctl(
1da177e4
LT
358 struct file *filp,
359 unsigned int cmd,
67fcaa73 360 unsigned long p)
1da177e4
LT
361{
362 int error;
1f6553f9 363 struct inode *inode = filp->f_dentry->d_inode;
67fcaa73 364 bhv_vnode_t *vp = vn_from_inode(inode);
1da177e4 365
67fcaa73 366 error = bhv_vop_ioctl(vp, inode, filp, 0, cmd, (void __user *)p);
1da177e4
LT
367 VMODIFY(vp);
368
369 /* NOTE: some of the ioctl's return positive #'s as a
370 * byte count indicating success, such as
371 * readlink_by_handle. So we don't "sign flip"
372 * like most other routines. This means true
373 * errors need to be returned as a negative value.
374 */
375 return error;
376}
377
378STATIC long
3562fd45 379xfs_file_ioctl_invis(
1da177e4
LT
380 struct file *filp,
381 unsigned int cmd,
67fcaa73 382 unsigned long p)
1da177e4 383{
1b895840 384 int error;
67fcaa73
NS
385 struct inode *inode = filp->f_dentry->d_inode;
386 bhv_vnode_t *vp = vn_from_inode(inode);
1da177e4 387
67fcaa73 388 error = bhv_vop_ioctl(vp, inode, filp, IO_INVIS, cmd, (void __user *)p);
1da177e4
LT
389 VMODIFY(vp);
390
391 /* NOTE: some of the ioctl's return positive #'s as a
392 * byte count indicating success, such as
393 * readlink_by_handle. So we don't "sign flip"
394 * like most other routines. This means true
395 * errors need to be returned as a negative value.
396 */
397 return error;
398}
399
bb3f724e 400#ifdef CONFIG_XFS_DMAPI
1da177e4
LT
401#ifdef HAVE_VMOP_MPROTECT
402STATIC int
3562fd45 403xfs_vm_mprotect(
1da177e4
LT
404 struct vm_area_struct *vma,
405 unsigned int newflags)
406{
67fcaa73 407 bhv_vnode_t *vp = vn_from_inode(vma->vm_file->f_dentry->d_inode);
1da177e4
LT
408 int error = 0;
409
410 if (vp->v_vfsp->vfs_flag & VFS_DMI) {
411 if ((vma->vm_flags & VM_MAYSHARE) &&
412 (newflags & VM_WRITE) && !(vma->vm_flags & VM_WRITE)) {
413 xfs_mount_t *mp = XFS_VFSTOM(vp->v_vfsp);
414
415 error = XFS_SEND_MMAP(mp, vma, VM_WRITE);
416 }
417 }
418 return error;
419}
420#endif /* HAVE_VMOP_MPROTECT */
bb3f724e 421#endif /* CONFIG_XFS_DMAPI */
1da177e4
LT
422
423#ifdef HAVE_FOP_OPEN_EXEC
424/* If the user is attempting to execute a file that is offline then
425 * we have to trigger a DMAPI READ event before the file is marked as busy
426 * otherwise the invisible I/O will not be able to write to the file to bring
427 * it back online.
428 */
429STATIC int
3562fd45 430xfs_file_open_exec(
1da177e4
LT
431 struct inode *inode)
432{
67fcaa73 433 bhv_vnode_t *vp = vn_from_inode(inode);
1da177e4 434
1d47bec2
NS
435 if (unlikely(vp->v_vfsp->vfs_flag & VFS_DMI)) {
436 xfs_mount_t *mp = XFS_VFSTOM(vp->v_vfsp);
437 xfs_inode_t *ip = xfs_vtoi(vp);
438
439 if (!ip)
440 return -EINVAL;
441 if (DM_EVENT_ENABLED(vp->v_vfsp, ip, DM_EVENT_READ))
442 return -XFS_SEND_DATA(mp, DM_EVENT_READ, vp,
1da177e4 443 0, 0, 0, NULL);
1da177e4 444 }
1d47bec2 445 return 0;
1da177e4
LT
446}
447#endif /* HAVE_FOP_OPEN_EXEC */
448
4b6f5d20 449const struct file_operations xfs_file_operations = {
1da177e4
LT
450 .llseek = generic_file_llseek,
451 .read = do_sync_read,
bb3f724e 452 .write = do_sync_write,
3562fd45
NS
453 .aio_read = xfs_file_aio_read,
454 .aio_write = xfs_file_aio_write,
455 .sendfile = xfs_file_sendfile,
1b895840
NS
456 .splice_read = xfs_file_splice_read,
457 .splice_write = xfs_file_splice_write,
3562fd45 458 .unlocked_ioctl = xfs_file_ioctl,
1da177e4 459#ifdef CONFIG_COMPAT
3562fd45 460 .compat_ioctl = xfs_file_compat_ioctl,
1da177e4 461#endif
3562fd45
NS
462 .mmap = xfs_file_mmap,
463 .open = xfs_file_open,
7d4fb40a 464 .flush = xfs_file_close,
3562fd45
NS
465 .release = xfs_file_release,
466 .fsync = xfs_file_fsync,
1da177e4 467#ifdef HAVE_FOP_OPEN_EXEC
3562fd45 468 .open_exec = xfs_file_open_exec,
1da177e4
LT
469#endif
470};
471
4b6f5d20 472const struct file_operations xfs_invis_file_operations = {
1da177e4
LT
473 .llseek = generic_file_llseek,
474 .read = do_sync_read,
bb3f724e 475 .write = do_sync_write,
3562fd45
NS
476 .aio_read = xfs_file_aio_read_invis,
477 .aio_write = xfs_file_aio_write_invis,
1b895840
NS
478 .sendfile = xfs_file_sendfile_invis,
479 .splice_read = xfs_file_splice_read_invis,
480 .splice_write = xfs_file_splice_write_invis,
3562fd45 481 .unlocked_ioctl = xfs_file_ioctl_invis,
1da177e4 482#ifdef CONFIG_COMPAT
3562fd45 483 .compat_ioctl = xfs_file_compat_invis_ioctl,
1da177e4 484#endif
3562fd45
NS
485 .mmap = xfs_file_mmap,
486 .open = xfs_file_open,
7d4fb40a 487 .flush = xfs_file_close,
3562fd45
NS
488 .release = xfs_file_release,
489 .fsync = xfs_file_fsync,
1da177e4
LT
490};
491
492
4b6f5d20 493const struct file_operations xfs_dir_file_operations = {
1da177e4 494 .read = generic_read_dir,
3562fd45
NS
495 .readdir = xfs_file_readdir,
496 .unlocked_ioctl = xfs_file_ioctl,
d3870398 497#ifdef CONFIG_COMPAT
3562fd45 498 .compat_ioctl = xfs_file_compat_ioctl,
d3870398 499#endif
3562fd45 500 .fsync = xfs_file_fsync,
1da177e4
LT
501};
502
3562fd45 503static struct vm_operations_struct xfs_file_vm_ops = {
1da177e4
LT
504 .nopage = filemap_nopage,
505 .populate = filemap_populate,
6fac0cb4
DR
506};
507
508#ifdef CONFIG_XFS_DMAPI
3562fd45
NS
509static struct vm_operations_struct xfs_dmapi_file_vm_ops = {
510 .nopage = xfs_vm_nopage,
6fac0cb4 511 .populate = filemap_populate,
1da177e4 512#ifdef HAVE_VMOP_MPROTECT
3562fd45 513 .mprotect = xfs_vm_mprotect,
1da177e4
LT
514#endif
515};
6fac0cb4 516#endif /* CONFIG_XFS_DMAPI */