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