Linux-2.6.12-rc2
[linux-flexiantxendom0-natty.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
61
62 STATIC inline ssize_t
63 __linvfs_read(
64         struct kiocb            *iocb,
65         char                    __user *buf,
66         int                     ioflags,
67         size_t                  count,
68         loff_t                  pos)
69 {
70         struct iovec            iov = {buf, count};
71         struct file             *file = iocb->ki_filp;
72         vnode_t                 *vp = LINVFS_GET_VP(file->f_dentry->d_inode);
73         ssize_t                 rval;
74
75         BUG_ON(iocb->ki_pos != pos);
76
77         if (unlikely(file->f_flags & O_DIRECT))
78                 ioflags |= IO_ISDIRECT;
79         VOP_READ(vp, iocb, &iov, 1, &iocb->ki_pos, ioflags, NULL, rval);
80         return rval;
81 }
82
83
84 STATIC ssize_t
85 linvfs_aio_read(
86         struct kiocb            *iocb,
87         char                    __user *buf,
88         size_t                  count,
89         loff_t                  pos)
90 {
91         return __linvfs_read(iocb, buf, IO_ISAIO, count, pos);
92 }
93
94 STATIC ssize_t
95 linvfs_aio_read_invis(
96         struct kiocb            *iocb,
97         char                    __user *buf,
98         size_t                  count,
99         loff_t                  pos)
100 {
101         return __linvfs_read(iocb, buf, IO_ISAIO|IO_INVIS, count, pos);
102 }
103
104
105 STATIC inline ssize_t
106 __linvfs_write(
107         struct kiocb    *iocb,
108         const char      __user *buf,
109         int             ioflags,
110         size_t          count,
111         loff_t          pos)
112 {
113         struct iovec    iov = {(void __user *)buf, count};
114         struct file     *file = iocb->ki_filp;
115         struct inode    *inode = file->f_mapping->host;
116         vnode_t         *vp = LINVFS_GET_VP(inode);
117         ssize_t         rval;
118
119         BUG_ON(iocb->ki_pos != pos);
120         if (unlikely(file->f_flags & O_DIRECT))
121                 ioflags |= IO_ISDIRECT;
122
123         VOP_WRITE(vp, iocb, &iov, 1, &iocb->ki_pos, ioflags, NULL, rval);
124         return rval;
125 }
126
127
128 STATIC ssize_t
129 linvfs_aio_write(
130         struct kiocb            *iocb,
131         const char              __user *buf,
132         size_t                  count,
133         loff_t                  pos)
134 {
135         return __linvfs_write(iocb, buf, IO_ISAIO, count, pos);
136 }
137
138 STATIC ssize_t
139 linvfs_aio_write_invis(
140         struct kiocb            *iocb,
141         const char              __user *buf,
142         size_t                  count,
143         loff_t                  pos)
144 {
145         return __linvfs_write(iocb, buf, IO_ISAIO|IO_INVIS, count, pos);
146 }
147
148
149 STATIC inline ssize_t
150 __linvfs_readv(
151         struct file             *file,
152         const struct iovec      *iov,
153         int                     ioflags,
154         unsigned long           nr_segs,
155         loff_t                  *ppos)
156 {
157         struct inode    *inode = file->f_mapping->host;
158         vnode_t         *vp = LINVFS_GET_VP(inode);
159         struct          kiocb kiocb;
160         ssize_t         rval;
161
162         init_sync_kiocb(&kiocb, file);
163         kiocb.ki_pos = *ppos;
164
165         if (unlikely(file->f_flags & O_DIRECT))
166                 ioflags |= IO_ISDIRECT;
167         VOP_READ(vp, &kiocb, iov, nr_segs, &kiocb.ki_pos, ioflags, NULL, rval);
168
169         *ppos = kiocb.ki_pos;
170         return rval;
171 }
172
173 STATIC ssize_t
174 linvfs_readv(
175         struct file             *file,
176         const struct iovec      *iov,
177         unsigned long           nr_segs,
178         loff_t                  *ppos)
179 {
180         return __linvfs_readv(file, iov, 0, nr_segs, ppos);
181 }
182
183 STATIC ssize_t
184 linvfs_readv_invis(
185         struct file             *file,
186         const struct iovec      *iov,
187         unsigned long           nr_segs,
188         loff_t                  *ppos)
189 {
190         return __linvfs_readv(file, iov, IO_INVIS, nr_segs, ppos);
191 }
192
193
194 STATIC inline ssize_t
195 __linvfs_writev(
196         struct file             *file,
197         const struct iovec      *iov,
198         int                     ioflags,
199         unsigned long           nr_segs,
200         loff_t                  *ppos)
201 {
202         struct inode    *inode = file->f_mapping->host;
203         vnode_t         *vp = LINVFS_GET_VP(inode);
204         struct          kiocb kiocb;
205         ssize_t         rval;
206
207         init_sync_kiocb(&kiocb, file);
208         kiocb.ki_pos = *ppos;
209         if (unlikely(file->f_flags & O_DIRECT))
210                 ioflags |= IO_ISDIRECT;
211
212         VOP_WRITE(vp, &kiocb, iov, nr_segs, &kiocb.ki_pos, ioflags, NULL, rval);
213
214         *ppos = kiocb.ki_pos;
215         return rval;
216 }
217
218
219 STATIC ssize_t
220 linvfs_writev(
221         struct file             *file,
222         const struct iovec      *iov,
223         unsigned long           nr_segs,
224         loff_t                  *ppos)
225 {
226         return __linvfs_writev(file, iov, 0, nr_segs, ppos);
227 }
228
229 STATIC ssize_t
230 linvfs_writev_invis(
231         struct file             *file,
232         const struct iovec      *iov,
233         unsigned long           nr_segs,
234         loff_t                  *ppos)
235 {
236         return __linvfs_writev(file, iov, IO_INVIS, nr_segs, ppos);
237 }
238
239 STATIC ssize_t
240 linvfs_sendfile(
241         struct file             *filp,
242         loff_t                  *ppos,
243         size_t                  count,
244         read_actor_t            actor,
245         void                    *target)
246 {
247         vnode_t                 *vp = LINVFS_GET_VP(filp->f_dentry->d_inode);
248         ssize_t                 rval;
249
250         VOP_SENDFILE(vp, filp, ppos, 0, count, actor, target, NULL, rval);
251         return rval;
252 }
253
254
255 STATIC int
256 linvfs_open(
257         struct inode    *inode,
258         struct file     *filp)
259 {
260         vnode_t         *vp = LINVFS_GET_VP(inode);
261         int             error;
262
263         if (!(filp->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS)
264                 return -EFBIG;
265
266         ASSERT(vp);
267         VOP_OPEN(vp, NULL, error);
268         return -error;
269 }
270
271
272 STATIC int
273 linvfs_release(
274         struct inode    *inode,
275         struct file     *filp)
276 {
277         vnode_t         *vp = LINVFS_GET_VP(inode);
278         int             error = 0;
279
280         if (vp)
281                 VOP_RELEASE(vp, error);
282         return -error;
283 }
284
285
286 STATIC int
287 linvfs_fsync(
288         struct file     *filp,
289         struct dentry   *dentry,
290         int             datasync)
291 {
292         struct inode    *inode = dentry->d_inode;
293         vnode_t         *vp = LINVFS_GET_VP(inode);
294         int             error;
295         int             flags = FSYNC_WAIT;
296
297         if (datasync)
298                 flags |= FSYNC_DATA;
299
300         ASSERT(vp);
301         VOP_FSYNC(vp, flags, NULL, (xfs_off_t)0, (xfs_off_t)-1, error);
302         return -error;
303 }
304
305 /*
306  * linvfs_readdir maps to VOP_READDIR().
307  * We need to build a uio, cred, ...
308  */
309
310 #define nextdp(dp)      ((struct xfs_dirent *)((char *)(dp) + (dp)->d_reclen))
311
312 STATIC int
313 linvfs_readdir(
314         struct file     *filp,
315         void            *dirent,
316         filldir_t       filldir)
317 {
318         int             error = 0;
319         vnode_t         *vp;
320         uio_t           uio;
321         iovec_t         iov;
322         int             eof = 0;
323         caddr_t         read_buf;
324         int             namelen, size = 0;
325         size_t          rlen = PAGE_CACHE_SIZE;
326         xfs_off_t       start_offset, curr_offset;
327         xfs_dirent_t    *dbp = NULL;
328
329         vp = LINVFS_GET_VP(filp->f_dentry->d_inode);
330         ASSERT(vp);
331
332         /* Try fairly hard to get memory */
333         do {
334                 if ((read_buf = (caddr_t)kmalloc(rlen, GFP_KERNEL)))
335                         break;
336                 rlen >>= 1;
337         } while (rlen >= 1024);
338
339         if (read_buf == NULL)
340                 return -ENOMEM;
341
342         uio.uio_iov = &iov;
343         uio.uio_segflg = UIO_SYSSPACE;
344         curr_offset = filp->f_pos;
345         if (filp->f_pos != 0x7fffffff)
346                 uio.uio_offset = filp->f_pos;
347         else
348                 uio.uio_offset = 0xffffffff;
349
350         while (!eof) {
351                 uio.uio_resid = iov.iov_len = rlen;
352                 iov.iov_base = read_buf;
353                 uio.uio_iovcnt = 1;
354
355                 start_offset = uio.uio_offset;
356
357                 VOP_READDIR(vp, &uio, NULL, &eof, error);
358                 if ((uio.uio_offset == start_offset) || error) {
359                         size = 0;
360                         break;
361                 }
362
363                 size = rlen - uio.uio_resid;
364                 dbp = (xfs_dirent_t *)read_buf;
365                 while (size > 0) {
366                         namelen = strlen(dbp->d_name);
367
368                         if (filldir(dirent, dbp->d_name, namelen,
369                                         (loff_t) curr_offset & 0x7fffffff,
370                                         (ino_t) dbp->d_ino,
371                                         DT_UNKNOWN)) {
372                                 goto done;
373                         }
374                         size -= dbp->d_reclen;
375                         curr_offset = (loff_t)dbp->d_off /* & 0x7fffffff */;
376                         dbp = nextdp(dbp);
377                 }
378         }
379 done:
380         if (!error) {
381                 if (size == 0)
382                         filp->f_pos = uio.uio_offset & 0x7fffffff;
383                 else if (dbp)
384                         filp->f_pos = curr_offset;
385         }
386
387         kfree(read_buf);
388         return -error;
389 }
390
391
392 STATIC int
393 linvfs_file_mmap(
394         struct file     *filp,
395         struct vm_area_struct *vma)
396 {
397         struct inode    *ip = filp->f_dentry->d_inode;
398         vnode_t         *vp = LINVFS_GET_VP(ip);
399         vattr_t         va = { .va_mask = XFS_AT_UPDATIME };
400         int             error;
401
402         if (vp->v_vfsp->vfs_flag & VFS_DMI) {
403                 xfs_mount_t     *mp = XFS_VFSTOM(vp->v_vfsp);
404
405                 error = -XFS_SEND_MMAP(mp, vma, 0);
406                 if (error)
407                         return error;
408         }
409
410         vma->vm_ops = &linvfs_file_vm_ops;
411
412         VOP_SETATTR(vp, &va, XFS_AT_UPDATIME, NULL, error);
413         if (!error)
414                 vn_revalidate(vp);      /* update Linux inode flags */
415         return 0;
416 }
417
418
419 STATIC long
420 linvfs_ioctl(
421         struct file     *filp,
422         unsigned int    cmd,
423         unsigned long   arg)
424 {
425         int             error;
426         struct inode *inode = filp->f_dentry->d_inode;
427         vnode_t         *vp = LINVFS_GET_VP(inode);
428
429         VOP_IOCTL(vp, inode, filp, 0, cmd, (void __user *)arg, error);
430         VMODIFY(vp);
431
432         /* NOTE:  some of the ioctl's return positive #'s as a
433          *        byte count indicating success, such as
434          *        readlink_by_handle.  So we don't "sign flip"
435          *        like most other routines.  This means true
436          *        errors need to be returned as a negative value.
437          */
438         return error;
439 }
440
441 STATIC long
442 linvfs_ioctl_invis(
443         struct file     *filp,
444         unsigned int    cmd,
445         unsigned long   arg)
446 {
447         int             error;
448         struct inode *inode = filp->f_dentry->d_inode;
449         vnode_t         *vp = LINVFS_GET_VP(inode);
450
451         ASSERT(vp);
452         VOP_IOCTL(vp, inode, filp, IO_INVIS, cmd, (void __user *)arg, error);
453         VMODIFY(vp);
454
455         /* NOTE:  some of the ioctl's return positive #'s as a
456          *        byte count indicating success, such as
457          *        readlink_by_handle.  So we don't "sign flip"
458          *        like most other routines.  This means true
459          *        errors need to be returned as a negative value.
460          */
461         return error;
462 }
463
464 #ifdef HAVE_VMOP_MPROTECT
465 STATIC int
466 linvfs_mprotect(
467         struct vm_area_struct *vma,
468         unsigned int    newflags)
469 {
470         vnode_t         *vp = LINVFS_GET_VP(vma->vm_file->f_dentry->d_inode);
471         int             error = 0;
472
473         if (vp->v_vfsp->vfs_flag & VFS_DMI) {
474                 if ((vma->vm_flags & VM_MAYSHARE) &&
475                     (newflags & VM_WRITE) && !(vma->vm_flags & VM_WRITE)) {
476                         xfs_mount_t     *mp = XFS_VFSTOM(vp->v_vfsp);
477
478                         error = XFS_SEND_MMAP(mp, vma, VM_WRITE);
479                     }
480         }
481         return error;
482 }
483 #endif /* HAVE_VMOP_MPROTECT */
484
485 #ifdef HAVE_FOP_OPEN_EXEC
486 /* If the user is attempting to execute a file that is offline then
487  * we have to trigger a DMAPI READ event before the file is marked as busy
488  * otherwise the invisible I/O will not be able to write to the file to bring
489  * it back online.
490  */
491 STATIC int
492 linvfs_open_exec(
493         struct inode    *inode)
494 {
495         vnode_t         *vp = LINVFS_GET_VP(inode);
496         xfs_mount_t     *mp = XFS_VFSTOM(vp->v_vfsp);
497         int             error = 0;
498         bhv_desc_t      *bdp;
499         xfs_inode_t     *ip;
500
501         if (vp->v_vfsp->vfs_flag & VFS_DMI) {
502                 bdp = vn_bhv_lookup(VN_BHV_HEAD(vp), &xfs_vnodeops);
503                 if (!bdp) {
504                         error = -EINVAL;
505                         goto open_exec_out;
506                 }
507                 ip = XFS_BHVTOI(bdp);
508                 if (DM_EVENT_ENABLED(vp->v_vfsp, ip, DM_EVENT_READ)) {
509                         error = -XFS_SEND_DATA(mp, DM_EVENT_READ, vp,
510                                                0, 0, 0, NULL);
511                 }
512         }
513 open_exec_out:
514         return error;
515 }
516 #endif /* HAVE_FOP_OPEN_EXEC */
517
518 struct file_operations linvfs_file_operations = {
519         .llseek         = generic_file_llseek,
520         .read           = do_sync_read,
521         .write          = do_sync_write,
522         .readv          = linvfs_readv,
523         .writev         = linvfs_writev,
524         .aio_read       = linvfs_aio_read,
525         .aio_write      = linvfs_aio_write,
526         .sendfile       = linvfs_sendfile,
527         .unlocked_ioctl = linvfs_ioctl,
528 #ifdef CONFIG_COMPAT
529         .compat_ioctl   = xfs_compat_ioctl,
530 #endif
531         .mmap           = linvfs_file_mmap,
532         .open           = linvfs_open,
533         .release        = linvfs_release,
534         .fsync          = linvfs_fsync,
535 #ifdef HAVE_FOP_OPEN_EXEC
536         .open_exec      = linvfs_open_exec,
537 #endif
538 };
539
540 struct file_operations linvfs_invis_file_operations = {
541         .llseek         = generic_file_llseek,
542         .read           = do_sync_read,
543         .write          = do_sync_write,
544         .readv          = linvfs_readv_invis,
545         .writev         = linvfs_writev_invis,
546         .aio_read       = linvfs_aio_read_invis,
547         .aio_write      = linvfs_aio_write_invis,
548         .sendfile       = linvfs_sendfile,
549         .unlocked_ioctl = linvfs_ioctl_invis,
550 #ifdef CONFIG_COMPAT
551         .compat_ioctl   = xfs_compat_invis_ioctl,
552 #endif
553         .mmap           = linvfs_file_mmap,
554         .open           = linvfs_open,
555         .release        = linvfs_release,
556         .fsync          = linvfs_fsync,
557 };
558
559
560 struct file_operations linvfs_dir_operations = {
561         .read           = generic_read_dir,
562         .readdir        = linvfs_readdir,
563         .unlocked_ioctl = linvfs_ioctl,
564         .fsync          = linvfs_fsync,
565 };
566
567 static struct vm_operations_struct linvfs_file_vm_ops = {
568         .nopage         = filemap_nopage,
569         .populate       = filemap_populate,
570 #ifdef HAVE_VMOP_MPROTECT
571         .mprotect       = linvfs_mprotect,
572 #endif
573 };