]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - fs/xfs/linux-2.6/xfs_file.c
Merge acpi-2.6.12 to-akpm
[mirror_ubuntu-bionic-kernel.git] / fs / xfs / linux-2.6 / xfs_file.c
1 /*
2 * Copyright (c) 2000-2005 Silicon Graphics, Inc. All Rights Reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it would be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11 *
12 * Further, this software is distributed without any warranty that it is
13 * free of the rightful claim of any third person regarding infringement
14 * or the like. Any license provided herein, whether implied or
15 * otherwise, applies only to this software file. Patent licenses, if
16 * any, provided herein do not apply to combinations of this program with
17 * other software, or any other product whatsoever.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write the Free Software Foundation, Inc., 59
21 * Temple Place - Suite 330, Boston MA 02111-1307, USA.
22 *
23 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
25 *
26 * http://www.sgi.com
27 *
28 * For further information regarding this notice, see:
29 *
30 * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
31 */
32
33 #include "xfs.h"
34 #include "xfs_inum.h"
35 #include "xfs_log.h"
36 #include "xfs_sb.h"
37 #include "xfs_dir.h"
38 #include "xfs_dir2.h"
39 #include "xfs_trans.h"
40 #include "xfs_dmapi.h"
41 #include "xfs_mount.h"
42 #include "xfs_bmap_btree.h"
43 #include "xfs_alloc_btree.h"
44 #include "xfs_ialloc_btree.h"
45 #include "xfs_alloc.h"
46 #include "xfs_btree.h"
47 #include "xfs_attr_sf.h"
48 #include "xfs_dir_sf.h"
49 #include "xfs_dir2_sf.h"
50 #include "xfs_dinode.h"
51 #include "xfs_inode.h"
52 #include "xfs_error.h"
53 #include "xfs_rw.h"
54 #include "xfs_ioctl32.h"
55
56 #include <linux/dcache.h>
57 #include <linux/smp_lock.h>
58
59 static struct vm_operations_struct linvfs_file_vm_ops;
60 #ifdef CONFIG_XFS_DMAPI
61 static struct vm_operations_struct linvfs_dmapi_file_vm_ops;
62 #endif
63
64 STATIC inline ssize_t
65 __linvfs_read(
66 struct kiocb *iocb,
67 char __user *buf,
68 int ioflags,
69 size_t count,
70 loff_t pos)
71 {
72 struct iovec iov = {buf, count};
73 struct file *file = iocb->ki_filp;
74 vnode_t *vp = LINVFS_GET_VP(file->f_dentry->d_inode);
75 ssize_t rval;
76
77 BUG_ON(iocb->ki_pos != pos);
78
79 if (unlikely(file->f_flags & O_DIRECT))
80 ioflags |= IO_ISDIRECT;
81 VOP_READ(vp, iocb, &iov, 1, &iocb->ki_pos, ioflags, NULL, rval);
82 return rval;
83 }
84
85
86 STATIC ssize_t
87 linvfs_aio_read(
88 struct kiocb *iocb,
89 char __user *buf,
90 size_t count,
91 loff_t pos)
92 {
93 return __linvfs_read(iocb, buf, IO_ISAIO, count, pos);
94 }
95
96 STATIC ssize_t
97 linvfs_aio_read_invis(
98 struct kiocb *iocb,
99 char __user *buf,
100 size_t count,
101 loff_t pos)
102 {
103 return __linvfs_read(iocb, buf, IO_ISAIO|IO_INVIS, count, pos);
104 }
105
106
107 STATIC inline ssize_t
108 __linvfs_write(
109 struct kiocb *iocb,
110 const char __user *buf,
111 int ioflags,
112 size_t count,
113 loff_t pos)
114 {
115 struct iovec iov = {(void __user *)buf, count};
116 struct file *file = iocb->ki_filp;
117 struct inode *inode = file->f_mapping->host;
118 vnode_t *vp = LINVFS_GET_VP(inode);
119 ssize_t rval;
120
121 BUG_ON(iocb->ki_pos != pos);
122 if (unlikely(file->f_flags & O_DIRECT))
123 ioflags |= IO_ISDIRECT;
124
125 VOP_WRITE(vp, iocb, &iov, 1, &iocb->ki_pos, ioflags, NULL, rval);
126 return rval;
127 }
128
129
130 STATIC ssize_t
131 linvfs_aio_write(
132 struct kiocb *iocb,
133 const char __user *buf,
134 size_t count,
135 loff_t pos)
136 {
137 return __linvfs_write(iocb, buf, IO_ISAIO, count, pos);
138 }
139
140 STATIC ssize_t
141 linvfs_aio_write_invis(
142 struct kiocb *iocb,
143 const char __user *buf,
144 size_t count,
145 loff_t pos)
146 {
147 return __linvfs_write(iocb, buf, IO_ISAIO|IO_INVIS, count, pos);
148 }
149
150
151 STATIC inline ssize_t
152 __linvfs_readv(
153 struct file *file,
154 const struct iovec *iov,
155 int ioflags,
156 unsigned long nr_segs,
157 loff_t *ppos)
158 {
159 struct inode *inode = file->f_mapping->host;
160 vnode_t *vp = LINVFS_GET_VP(inode);
161 struct kiocb kiocb;
162 ssize_t rval;
163
164 init_sync_kiocb(&kiocb, file);
165 kiocb.ki_pos = *ppos;
166
167 if (unlikely(file->f_flags & O_DIRECT))
168 ioflags |= IO_ISDIRECT;
169 VOP_READ(vp, &kiocb, iov, nr_segs, &kiocb.ki_pos, ioflags, NULL, rval);
170
171 *ppos = kiocb.ki_pos;
172 return rval;
173 }
174
175 STATIC ssize_t
176 linvfs_readv(
177 struct file *file,
178 const struct iovec *iov,
179 unsigned long nr_segs,
180 loff_t *ppos)
181 {
182 return __linvfs_readv(file, iov, 0, nr_segs, ppos);
183 }
184
185 STATIC ssize_t
186 linvfs_readv_invis(
187 struct file *file,
188 const struct iovec *iov,
189 unsigned long nr_segs,
190 loff_t *ppos)
191 {
192 return __linvfs_readv(file, iov, IO_INVIS, nr_segs, ppos);
193 }
194
195
196 STATIC inline ssize_t
197 __linvfs_writev(
198 struct file *file,
199 const struct iovec *iov,
200 int ioflags,
201 unsigned long nr_segs,
202 loff_t *ppos)
203 {
204 struct inode *inode = file->f_mapping->host;
205 vnode_t *vp = LINVFS_GET_VP(inode);
206 struct kiocb kiocb;
207 ssize_t rval;
208
209 init_sync_kiocb(&kiocb, file);
210 kiocb.ki_pos = *ppos;
211 if (unlikely(file->f_flags & O_DIRECT))
212 ioflags |= IO_ISDIRECT;
213
214 VOP_WRITE(vp, &kiocb, iov, nr_segs, &kiocb.ki_pos, ioflags, NULL, rval);
215
216 *ppos = kiocb.ki_pos;
217 return rval;
218 }
219
220
221 STATIC ssize_t
222 linvfs_writev(
223 struct file *file,
224 const struct iovec *iov,
225 unsigned long nr_segs,
226 loff_t *ppos)
227 {
228 return __linvfs_writev(file, iov, 0, nr_segs, ppos);
229 }
230
231 STATIC ssize_t
232 linvfs_writev_invis(
233 struct file *file,
234 const struct iovec *iov,
235 unsigned long nr_segs,
236 loff_t *ppos)
237 {
238 return __linvfs_writev(file, iov, IO_INVIS, nr_segs, ppos);
239 }
240
241 STATIC ssize_t
242 linvfs_sendfile(
243 struct file *filp,
244 loff_t *ppos,
245 size_t count,
246 read_actor_t actor,
247 void *target)
248 {
249 vnode_t *vp = LINVFS_GET_VP(filp->f_dentry->d_inode);
250 ssize_t rval;
251
252 VOP_SENDFILE(vp, filp, ppos, 0, count, actor, target, NULL, rval);
253 return rval;
254 }
255
256
257 STATIC int
258 linvfs_open(
259 struct inode *inode,
260 struct file *filp)
261 {
262 vnode_t *vp = LINVFS_GET_VP(inode);
263 int error;
264
265 if (!(filp->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS)
266 return -EFBIG;
267
268 ASSERT(vp);
269 VOP_OPEN(vp, NULL, error);
270 return -error;
271 }
272
273
274 STATIC int
275 linvfs_release(
276 struct inode *inode,
277 struct file *filp)
278 {
279 vnode_t *vp = LINVFS_GET_VP(inode);
280 int error = 0;
281
282 if (vp)
283 VOP_RELEASE(vp, error);
284 return -error;
285 }
286
287
288 STATIC int
289 linvfs_fsync(
290 struct file *filp,
291 struct dentry *dentry,
292 int datasync)
293 {
294 struct inode *inode = dentry->d_inode;
295 vnode_t *vp = LINVFS_GET_VP(inode);
296 int error;
297 int flags = FSYNC_WAIT;
298
299 if (datasync)
300 flags |= FSYNC_DATA;
301
302 ASSERT(vp);
303 VOP_FSYNC(vp, flags, NULL, (xfs_off_t)0, (xfs_off_t)-1, error);
304 return -error;
305 }
306
307 /*
308 * linvfs_readdir maps to VOP_READDIR().
309 * We need to build a uio, cred, ...
310 */
311
312 #define nextdp(dp) ((struct xfs_dirent *)((char *)(dp) + (dp)->d_reclen))
313
314 STATIC int
315 linvfs_readdir(
316 struct file *filp,
317 void *dirent,
318 filldir_t filldir)
319 {
320 int error = 0;
321 vnode_t *vp;
322 uio_t uio;
323 iovec_t iov;
324 int eof = 0;
325 caddr_t read_buf;
326 int namelen, size = 0;
327 size_t rlen = PAGE_CACHE_SIZE;
328 xfs_off_t start_offset, curr_offset;
329 xfs_dirent_t *dbp = NULL;
330
331 vp = LINVFS_GET_VP(filp->f_dentry->d_inode);
332 ASSERT(vp);
333
334 /* Try fairly hard to get memory */
335 do {
336 if ((read_buf = (caddr_t)kmalloc(rlen, GFP_KERNEL)))
337 break;
338 rlen >>= 1;
339 } while (rlen >= 1024);
340
341 if (read_buf == NULL)
342 return -ENOMEM;
343
344 uio.uio_iov = &iov;
345 uio.uio_segflg = UIO_SYSSPACE;
346 curr_offset = filp->f_pos;
347 if (filp->f_pos != 0x7fffffff)
348 uio.uio_offset = filp->f_pos;
349 else
350 uio.uio_offset = 0xffffffff;
351
352 while (!eof) {
353 uio.uio_resid = iov.iov_len = rlen;
354 iov.iov_base = read_buf;
355 uio.uio_iovcnt = 1;
356
357 start_offset = uio.uio_offset;
358
359 VOP_READDIR(vp, &uio, NULL, &eof, error);
360 if ((uio.uio_offset == start_offset) || error) {
361 size = 0;
362 break;
363 }
364
365 size = rlen - uio.uio_resid;
366 dbp = (xfs_dirent_t *)read_buf;
367 while (size > 0) {
368 namelen = strlen(dbp->d_name);
369
370 if (filldir(dirent, dbp->d_name, namelen,
371 (loff_t) curr_offset & 0x7fffffff,
372 (ino_t) dbp->d_ino,
373 DT_UNKNOWN)) {
374 goto done;
375 }
376 size -= dbp->d_reclen;
377 curr_offset = (loff_t)dbp->d_off /* & 0x7fffffff */;
378 dbp = nextdp(dbp);
379 }
380 }
381 done:
382 if (!error) {
383 if (size == 0)
384 filp->f_pos = uio.uio_offset & 0x7fffffff;
385 else if (dbp)
386 filp->f_pos = curr_offset;
387 }
388
389 kfree(read_buf);
390 return -error;
391 }
392
393 #ifdef CONFIG_XFS_DMAPI
394 STATIC void
395 linvfs_mmap_close(
396 struct vm_area_struct *vma)
397 {
398 xfs_dm_mm_put(vma);
399 }
400 #endif /* CONFIG_XFS_DMAPI */
401
402 STATIC int
403 linvfs_file_mmap(
404 struct file *filp,
405 struct vm_area_struct *vma)
406 {
407 struct inode *ip = filp->f_dentry->d_inode;
408 vnode_t *vp = LINVFS_GET_VP(ip);
409 vattr_t va = { .va_mask = XFS_AT_UPDATIME };
410 int error;
411
412 vma->vm_ops = &linvfs_file_vm_ops;
413
414 if (vp->v_vfsp->vfs_flag & VFS_DMI) {
415 xfs_mount_t *mp = XFS_VFSTOM(vp->v_vfsp);
416
417 error = -XFS_SEND_MMAP(mp, vma, 0);
418 if (error)
419 return error;
420 #ifdef CONFIG_XFS_DMAPI
421 vma->vm_ops = &linvfs_dmapi_file_vm_ops;
422 #endif
423 }
424
425 VOP_SETATTR(vp, &va, XFS_AT_UPDATIME, NULL, error);
426 if (!error)
427 vn_revalidate(vp); /* update Linux inode flags */
428 return 0;
429 }
430
431
432 STATIC long
433 linvfs_ioctl(
434 struct file *filp,
435 unsigned int cmd,
436 unsigned long arg)
437 {
438 int error;
439 struct inode *inode = filp->f_dentry->d_inode;
440 vnode_t *vp = LINVFS_GET_VP(inode);
441
442 VOP_IOCTL(vp, inode, filp, 0, cmd, (void __user *)arg, error);
443 VMODIFY(vp);
444
445 /* NOTE: some of the ioctl's return positive #'s as a
446 * byte count indicating success, such as
447 * readlink_by_handle. So we don't "sign flip"
448 * like most other routines. This means true
449 * errors need to be returned as a negative value.
450 */
451 return error;
452 }
453
454 STATIC long
455 linvfs_ioctl_invis(
456 struct file *filp,
457 unsigned int cmd,
458 unsigned long arg)
459 {
460 int error;
461 struct inode *inode = filp->f_dentry->d_inode;
462 vnode_t *vp = LINVFS_GET_VP(inode);
463
464 ASSERT(vp);
465 VOP_IOCTL(vp, inode, filp, IO_INVIS, cmd, (void __user *)arg, error);
466 VMODIFY(vp);
467
468 /* NOTE: some of the ioctl's return positive #'s as a
469 * byte count indicating success, such as
470 * readlink_by_handle. So we don't "sign flip"
471 * like most other routines. This means true
472 * errors need to be returned as a negative value.
473 */
474 return error;
475 }
476
477 #ifdef HAVE_VMOP_MPROTECT
478 STATIC int
479 linvfs_mprotect(
480 struct vm_area_struct *vma,
481 unsigned int newflags)
482 {
483 vnode_t *vp = LINVFS_GET_VP(vma->vm_file->f_dentry->d_inode);
484 int error = 0;
485
486 if (vp->v_vfsp->vfs_flag & VFS_DMI) {
487 if ((vma->vm_flags & VM_MAYSHARE) &&
488 (newflags & VM_WRITE) && !(vma->vm_flags & VM_WRITE)) {
489 xfs_mount_t *mp = XFS_VFSTOM(vp->v_vfsp);
490
491 error = XFS_SEND_MMAP(mp, vma, VM_WRITE);
492 }
493 }
494 return error;
495 }
496 #endif /* HAVE_VMOP_MPROTECT */
497
498 #ifdef HAVE_FOP_OPEN_EXEC
499 /* If the user is attempting to execute a file that is offline then
500 * we have to trigger a DMAPI READ event before the file is marked as busy
501 * otherwise the invisible I/O will not be able to write to the file to bring
502 * it back online.
503 */
504 STATIC int
505 linvfs_open_exec(
506 struct inode *inode)
507 {
508 vnode_t *vp = LINVFS_GET_VP(inode);
509 xfs_mount_t *mp = XFS_VFSTOM(vp->v_vfsp);
510 int error = 0;
511 bhv_desc_t *bdp;
512 xfs_inode_t *ip;
513
514 if (vp->v_vfsp->vfs_flag & VFS_DMI) {
515 bdp = vn_bhv_lookup(VN_BHV_HEAD(vp), &xfs_vnodeops);
516 if (!bdp) {
517 error = -EINVAL;
518 goto open_exec_out;
519 }
520 ip = XFS_BHVTOI(bdp);
521 if (DM_EVENT_ENABLED(vp->v_vfsp, ip, DM_EVENT_READ)) {
522 error = -XFS_SEND_DATA(mp, DM_EVENT_READ, vp,
523 0, 0, 0, NULL);
524 }
525 }
526 open_exec_out:
527 return error;
528 }
529 #endif /* HAVE_FOP_OPEN_EXEC */
530
531 /*
532 * Temporary workaround to the AIO direct IO write problem.
533 * This code can go and we can revert to do_sync_write once
534 * the writepage(s) rework is merged.
535 */
536 STATIC ssize_t
537 linvfs_write(
538 struct file *filp,
539 const char __user *buf,
540 size_t len,
541 loff_t *ppos)
542 {
543 struct kiocb kiocb;
544 ssize_t ret;
545
546 init_sync_kiocb(&kiocb, filp);
547 kiocb.ki_pos = *ppos;
548 ret = __linvfs_write(&kiocb, buf, 0, len, kiocb.ki_pos);
549 *ppos = kiocb.ki_pos;
550 return ret;
551 }
552 STATIC ssize_t
553 linvfs_write_invis(
554 struct file *filp,
555 const char __user *buf,
556 size_t len,
557 loff_t *ppos)
558 {
559 struct kiocb kiocb;
560 ssize_t ret;
561
562 init_sync_kiocb(&kiocb, filp);
563 kiocb.ki_pos = *ppos;
564 ret = __linvfs_write(&kiocb, buf, IO_INVIS, len, kiocb.ki_pos);
565 *ppos = kiocb.ki_pos;
566 return ret;
567 }
568
569
570 struct file_operations linvfs_file_operations = {
571 .llseek = generic_file_llseek,
572 .read = do_sync_read,
573 .write = linvfs_write,
574 .readv = linvfs_readv,
575 .writev = linvfs_writev,
576 .aio_read = linvfs_aio_read,
577 .aio_write = linvfs_aio_write,
578 .sendfile = linvfs_sendfile,
579 .unlocked_ioctl = linvfs_ioctl,
580 #ifdef CONFIG_COMPAT
581 .compat_ioctl = linvfs_compat_ioctl,
582 #endif
583 .mmap = linvfs_file_mmap,
584 .open = linvfs_open,
585 .release = linvfs_release,
586 .fsync = linvfs_fsync,
587 #ifdef HAVE_FOP_OPEN_EXEC
588 .open_exec = linvfs_open_exec,
589 #endif
590 };
591
592 struct file_operations linvfs_invis_file_operations = {
593 .llseek = generic_file_llseek,
594 .read = do_sync_read,
595 .write = linvfs_write_invis,
596 .readv = linvfs_readv_invis,
597 .writev = linvfs_writev_invis,
598 .aio_read = linvfs_aio_read_invis,
599 .aio_write = linvfs_aio_write_invis,
600 .sendfile = linvfs_sendfile,
601 .unlocked_ioctl = linvfs_ioctl_invis,
602 #ifdef CONFIG_COMPAT
603 .compat_ioctl = linvfs_compat_invis_ioctl,
604 #endif
605 .mmap = linvfs_file_mmap,
606 .open = linvfs_open,
607 .release = linvfs_release,
608 .fsync = linvfs_fsync,
609 };
610
611
612 struct file_operations linvfs_dir_operations = {
613 .read = generic_read_dir,
614 .readdir = linvfs_readdir,
615 .unlocked_ioctl = linvfs_ioctl,
616 #ifdef CONFIG_COMPAT
617 .compat_ioctl = linvfs_compat_ioctl,
618 #endif
619 .fsync = linvfs_fsync,
620 };
621
622 static struct vm_operations_struct linvfs_file_vm_ops = {
623 .nopage = filemap_nopage,
624 .populate = filemap_populate,
625 };
626
627 #ifdef CONFIG_XFS_DMAPI
628 static struct vm_operations_struct linvfs_dmapi_file_vm_ops = {
629 .close = linvfs_mmap_close,
630 .nopage = filemap_nopage,
631 .populate = filemap_populate,
632 #ifdef HAVE_VMOP_MPROTECT
633 .mprotect = linvfs_mprotect,
634 #endif
635 };
636 #endif /* CONFIG_XFS_DMAPI */