Merge refs/heads/devtree from rsync://oak/kernels/iseries/work/.git
[powerpc.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 #ifdef CONFIG_XFS_DMAPI
315
316 STATIC struct page *
317 linvfs_filemap_nopage(
318         struct vm_area_struct   *area,
319         unsigned long           address,
320         int                     *type)
321 {
322         struct inode    *inode = area->vm_file->f_dentry->d_inode;
323         vnode_t         *vp = LINVFS_GET_VP(inode);
324         xfs_mount_t     *mp = XFS_VFSTOM(vp->v_vfsp);
325         int             error;
326
327         ASSERT_ALWAYS(vp->v_vfsp->vfs_flag & VFS_DMI);
328
329         error = XFS_SEND_MMAP(mp, area, 0);
330         if (error)
331                 return NULL;
332
333         return filemap_nopage(area, address, type);
334 }
335
336 #endif /* CONFIG_XFS_DMAPI */
337
338
339 STATIC int
340 linvfs_readdir(
341         struct file     *filp,
342         void            *dirent,
343         filldir_t       filldir)
344 {
345         int             error = 0;
346         vnode_t         *vp;
347         uio_t           uio;
348         iovec_t         iov;
349         int             eof = 0;
350         caddr_t         read_buf;
351         int             namelen, size = 0;
352         size_t          rlen = PAGE_CACHE_SIZE;
353         xfs_off_t       start_offset, curr_offset;
354         xfs_dirent_t    *dbp = NULL;
355
356         vp = LINVFS_GET_VP(filp->f_dentry->d_inode);
357         ASSERT(vp);
358
359         /* Try fairly hard to get memory */
360         do {
361                 if ((read_buf = (caddr_t)kmalloc(rlen, GFP_KERNEL)))
362                         break;
363                 rlen >>= 1;
364         } while (rlen >= 1024);
365
366         if (read_buf == NULL)
367                 return -ENOMEM;
368
369         uio.uio_iov = &iov;
370         uio.uio_segflg = UIO_SYSSPACE;
371         curr_offset = filp->f_pos;
372         if (filp->f_pos != 0x7fffffff)
373                 uio.uio_offset = filp->f_pos;
374         else
375                 uio.uio_offset = 0xffffffff;
376
377         while (!eof) {
378                 uio.uio_resid = iov.iov_len = rlen;
379                 iov.iov_base = read_buf;
380                 uio.uio_iovcnt = 1;
381
382                 start_offset = uio.uio_offset;
383
384                 VOP_READDIR(vp, &uio, NULL, &eof, error);
385                 if ((uio.uio_offset == start_offset) || error) {
386                         size = 0;
387                         break;
388                 }
389
390                 size = rlen - uio.uio_resid;
391                 dbp = (xfs_dirent_t *)read_buf;
392                 while (size > 0) {
393                         namelen = strlen(dbp->d_name);
394
395                         if (filldir(dirent, dbp->d_name, namelen,
396                                         (loff_t) curr_offset & 0x7fffffff,
397                                         (ino_t) dbp->d_ino,
398                                         DT_UNKNOWN)) {
399                                 goto done;
400                         }
401                         size -= dbp->d_reclen;
402                         curr_offset = (loff_t)dbp->d_off /* & 0x7fffffff */;
403                         dbp = nextdp(dbp);
404                 }
405         }
406 done:
407         if (!error) {
408                 if (size == 0)
409                         filp->f_pos = uio.uio_offset & 0x7fffffff;
410                 else if (dbp)
411                         filp->f_pos = curr_offset;
412         }
413
414         kfree(read_buf);
415         return -error;
416 }
417
418
419 STATIC int
420 linvfs_file_mmap(
421         struct file     *filp,
422         struct vm_area_struct *vma)
423 {
424         struct inode    *ip = filp->f_dentry->d_inode;
425         vnode_t         *vp = LINVFS_GET_VP(ip);
426         vattr_t         va = { .va_mask = XFS_AT_UPDATIME };
427         int             error;
428
429         vma->vm_ops = &linvfs_file_vm_ops;
430
431 #ifdef CONFIG_XFS_DMAPI
432         if (vp->v_vfsp->vfs_flag & VFS_DMI) {
433                 vma->vm_ops = &linvfs_dmapi_file_vm_ops;
434         }
435 #endif /* CONFIG_XFS_DMAPI */
436
437         VOP_SETATTR(vp, &va, XFS_AT_UPDATIME, NULL, error);
438         if (!error)
439                 vn_revalidate(vp);      /* update Linux inode flags */
440         return 0;
441 }
442
443
444 STATIC long
445 linvfs_ioctl(
446         struct file     *filp,
447         unsigned int    cmd,
448         unsigned long   arg)
449 {
450         int             error;
451         struct inode *inode = filp->f_dentry->d_inode;
452         vnode_t         *vp = LINVFS_GET_VP(inode);
453
454         VOP_IOCTL(vp, inode, filp, 0, cmd, (void __user *)arg, error);
455         VMODIFY(vp);
456
457         /* NOTE:  some of the ioctl's return positive #'s as a
458          *        byte count indicating success, such as
459          *        readlink_by_handle.  So we don't "sign flip"
460          *        like most other routines.  This means true
461          *        errors need to be returned as a negative value.
462          */
463         return error;
464 }
465
466 STATIC long
467 linvfs_ioctl_invis(
468         struct file     *filp,
469         unsigned int    cmd,
470         unsigned long   arg)
471 {
472         int             error;
473         struct inode *inode = filp->f_dentry->d_inode;
474         vnode_t         *vp = LINVFS_GET_VP(inode);
475
476         ASSERT(vp);
477         VOP_IOCTL(vp, inode, filp, IO_INVIS, cmd, (void __user *)arg, error);
478         VMODIFY(vp);
479
480         /* NOTE:  some of the ioctl's return positive #'s as a
481          *        byte count indicating success, such as
482          *        readlink_by_handle.  So we don't "sign flip"
483          *        like most other routines.  This means true
484          *        errors need to be returned as a negative value.
485          */
486         return error;
487 }
488
489 #ifdef CONFIG_XFS_DMAPI
490 #ifdef HAVE_VMOP_MPROTECT
491 STATIC int
492 linvfs_mprotect(
493         struct vm_area_struct *vma,
494         unsigned int    newflags)
495 {
496         vnode_t         *vp = LINVFS_GET_VP(vma->vm_file->f_dentry->d_inode);
497         int             error = 0;
498
499         if (vp->v_vfsp->vfs_flag & VFS_DMI) {
500                 if ((vma->vm_flags & VM_MAYSHARE) &&
501                     (newflags & VM_WRITE) && !(vma->vm_flags & VM_WRITE)) {
502                         xfs_mount_t     *mp = XFS_VFSTOM(vp->v_vfsp);
503
504                         error = XFS_SEND_MMAP(mp, vma, VM_WRITE);
505                     }
506         }
507         return error;
508 }
509 #endif /* HAVE_VMOP_MPROTECT */
510 #endif /* CONFIG_XFS_DMAPI */
511
512 #ifdef HAVE_FOP_OPEN_EXEC
513 /* If the user is attempting to execute a file that is offline then
514  * we have to trigger a DMAPI READ event before the file is marked as busy
515  * otherwise the invisible I/O will not be able to write to the file to bring
516  * it back online.
517  */
518 STATIC int
519 linvfs_open_exec(
520         struct inode    *inode)
521 {
522         vnode_t         *vp = LINVFS_GET_VP(inode);
523         xfs_mount_t     *mp = XFS_VFSTOM(vp->v_vfsp);
524         int             error = 0;
525         bhv_desc_t      *bdp;
526         xfs_inode_t     *ip;
527
528         if (vp->v_vfsp->vfs_flag & VFS_DMI) {
529                 bdp = vn_bhv_lookup(VN_BHV_HEAD(vp), &xfs_vnodeops);
530                 if (!bdp) {
531                         error = -EINVAL;
532                         goto open_exec_out;
533                 }
534                 ip = XFS_BHVTOI(bdp);
535                 if (DM_EVENT_ENABLED(vp->v_vfsp, ip, DM_EVENT_READ)) {
536                         error = -XFS_SEND_DATA(mp, DM_EVENT_READ, vp,
537                                                0, 0, 0, NULL);
538                 }
539         }
540 open_exec_out:
541         return error;
542 }
543 #endif /* HAVE_FOP_OPEN_EXEC */
544
545 struct file_operations linvfs_file_operations = {
546         .llseek         = generic_file_llseek,
547         .read           = do_sync_read,
548         .write          = do_sync_write,
549         .readv          = linvfs_readv,
550         .writev         = linvfs_writev,
551         .aio_read       = linvfs_aio_read,
552         .aio_write      = linvfs_aio_write,
553         .sendfile       = linvfs_sendfile,
554         .unlocked_ioctl = linvfs_ioctl,
555 #ifdef CONFIG_COMPAT
556         .compat_ioctl   = linvfs_compat_ioctl,
557 #endif
558         .mmap           = linvfs_file_mmap,
559         .open           = linvfs_open,
560         .release        = linvfs_release,
561         .fsync          = linvfs_fsync,
562 #ifdef HAVE_FOP_OPEN_EXEC
563         .open_exec      = linvfs_open_exec,
564 #endif
565 };
566
567 struct file_operations linvfs_invis_file_operations = {
568         .llseek         = generic_file_llseek,
569         .read           = do_sync_read,
570         .write          = do_sync_write,
571         .readv          = linvfs_readv_invis,
572         .writev         = linvfs_writev_invis,
573         .aio_read       = linvfs_aio_read_invis,
574         .aio_write      = linvfs_aio_write_invis,
575         .sendfile       = linvfs_sendfile,
576         .unlocked_ioctl = linvfs_ioctl_invis,
577 #ifdef CONFIG_COMPAT
578         .compat_ioctl   = linvfs_compat_invis_ioctl,
579 #endif
580         .mmap           = linvfs_file_mmap,
581         .open           = linvfs_open,
582         .release        = linvfs_release,
583         .fsync          = linvfs_fsync,
584 };
585
586
587 struct file_operations linvfs_dir_operations = {
588         .read           = generic_read_dir,
589         .readdir        = linvfs_readdir,
590         .unlocked_ioctl = linvfs_ioctl,
591 #ifdef CONFIG_COMPAT
592         .compat_ioctl   = linvfs_compat_ioctl,
593 #endif
594         .fsync          = linvfs_fsync,
595 };
596
597 static struct vm_operations_struct linvfs_file_vm_ops = {
598         .nopage         = filemap_nopage,
599         .populate       = filemap_populate,
600 };
601
602 #ifdef CONFIG_XFS_DMAPI
603 static struct vm_operations_struct linvfs_dmapi_file_vm_ops = {
604         .nopage         = linvfs_filemap_nopage,
605         .populate       = filemap_populate,
606 #ifdef HAVE_VMOP_MPROTECT
607         .mprotect       = linvfs_mprotect,
608 #endif
609 };
610 #endif /* CONFIG_XFS_DMAPI */