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