commented early_printk patch because of rejects.
[linux-flexiantxendom0-3.2.10.git] / fs / xfs / linux / xfs_file.c
1 /*
2  * Copyright (c) 2000-2003 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/mman.h> /* for PROT_WRITE */
57
58 static struct vm_operations_struct linvfs_file_vm_ops;
59
60
61 STATIC ssize_t
62 linvfs_read(
63         struct kiocb            *iocb,
64         char __user             *buf,
65         size_t                  count,
66         loff_t                  pos)
67 {
68         struct iovec            iov = {buf, count};
69         vnode_t                 *vp;
70         int                     error;
71
72         BUG_ON(iocb->ki_pos != pos);
73         vp = LINVFS_GET_VP(iocb->ki_filp->f_dentry->d_inode);
74         VOP_READ(vp, iocb, &iov, 1, &iocb->ki_pos, NULL, error);
75
76         return error;
77 }
78
79
80 STATIC ssize_t
81 linvfs_write(
82         struct kiocb    *iocb,
83         const char      *buf,
84         size_t          count,
85         loff_t          pos)
86 {
87         struct iovec    iov = {(void *)buf, count};
88         struct file     *file = iocb->ki_filp;
89         struct inode    *inode = file->f_dentry->d_inode->i_mapping->host;
90         vnode_t         *vp = LINVFS_GET_VP(inode);
91         int             error;
92         int             direct = file->f_flags & O_DIRECT;
93
94         BUG_ON(iocb->ki_pos != pos);
95
96         if (direct) {
97                 VOP_WRITE(vp, iocb, &iov, 1, &iocb->ki_pos, NULL, error);
98         } else {
99                 down(&inode->i_sem);
100                 VOP_WRITE(vp, iocb, &iov, 1, &iocb->ki_pos, NULL, error);
101                 up(&inode->i_sem);
102         }
103
104         return error;
105 }
106
107 STATIC ssize_t
108 linvfs_readv(
109         struct file             *file,
110         const struct iovec      *iov,
111         unsigned long           nr_segs,
112         loff_t                  *ppos)
113 {
114         struct inode    *inode = file->f_dentry->d_inode->i_mapping->host;
115         vnode_t         *vp = LINVFS_GET_VP(inode);
116         struct          kiocb kiocb;
117         int             error;
118
119         init_sync_kiocb(&kiocb, file);
120         kiocb.ki_pos = *ppos;
121         VOP_READ(vp, &kiocb, iov, nr_segs, &kiocb.ki_pos, NULL, error);
122         if (-EIOCBQUEUED == error)
123                 error = wait_on_sync_kiocb(&kiocb);
124         *ppos = kiocb.ki_pos;
125
126         return error;
127 }
128
129 STATIC ssize_t
130 linvfs_writev(
131         struct file             *file,
132         const struct iovec      *iov,
133         unsigned long           nr_segs,
134         loff_t                  *ppos)
135 {
136         struct inode    *inode = file->f_dentry->d_inode->i_mapping->host;
137         vnode_t         *vp = LINVFS_GET_VP(inode);
138         struct          kiocb kiocb;
139         int             error;
140         int             direct = file->f_flags & O_DIRECT;
141
142         init_sync_kiocb(&kiocb, file);
143         kiocb.ki_pos = *ppos;
144         if (direct) {
145                 VOP_WRITE(vp, &kiocb, iov, nr_segs, &kiocb.ki_pos, NULL, error);
146         } else {
147                 down(&inode->i_sem);
148                 VOP_WRITE(vp, &kiocb, iov, nr_segs, &kiocb.ki_pos, NULL, error);
149                 up(&inode->i_sem);
150         }
151         if (-EIOCBQUEUED == error)
152                 error = wait_on_sync_kiocb(&kiocb);
153         *ppos = kiocb.ki_pos;
154
155         return error;
156 }
157
158 STATIC ssize_t
159 linvfs_sendfile(
160         struct file             *filp,
161         loff_t                  *ppos,
162         size_t                  count,
163         read_actor_t            actor,
164         void                    *target)
165 {
166         vnode_t                 *vp = LINVFS_GET_VP(filp->f_dentry->d_inode);
167         int                     error;
168
169         VOP_SENDFILE(vp, filp, ppos, count, actor, target, NULL, error);
170
171         return error;
172 }
173
174
175 STATIC int
176 linvfs_open(
177         struct inode    *inode,
178         struct file     *filp)
179 {
180         vnode_t         *vp = LINVFS_GET_VP(inode);
181         int             error;
182
183         if (!(filp->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS)
184                 return -EFBIG;
185
186         ASSERT(vp);
187         VOP_OPEN(vp, NULL, error);
188         return -error;
189 }
190
191
192 STATIC int
193 linvfs_release(
194         struct inode    *inode,
195         struct file     *filp)
196 {
197         vnode_t         *vp = LINVFS_GET_VP(inode);
198         int             error = 0;
199
200         if (vp)
201                 VOP_RELEASE(vp, error);
202         return -error;
203 }
204
205
206 STATIC int
207 linvfs_fsync(
208         struct file     *filp,
209         struct dentry   *dentry,
210         int             datasync)
211 {
212         struct inode    *inode = dentry->d_inode;
213         vnode_t         *vp = LINVFS_GET_VP(inode);
214         int             error;
215         int             flags = FSYNC_WAIT;
216
217         if (datasync)
218                 flags |= FSYNC_DATA;
219
220         ASSERT(vp);
221         VOP_FSYNC(vp, flags, NULL, (xfs_off_t)0, (xfs_off_t)-1, error);
222         return -error;
223 }
224
225 /*
226  * linvfs_readdir maps to VOP_READDIR().
227  * We need to build a uio, cred, ...
228  */
229
230 #define nextdp(dp)      ((struct xfs_dirent *)((char *)(dp) + (dp)->d_reclen))
231
232 STATIC int
233 linvfs_readdir(
234         struct file     *filp,
235         void            *dirent,
236         filldir_t       filldir)
237 {
238         int             error = 0;
239         vnode_t         *vp;
240         uio_t           uio;
241         iovec_t         iov;
242         int             eof = 0;
243         caddr_t         read_buf;
244         int             namelen, size = 0;
245         size_t          rlen = PAGE_CACHE_SIZE;
246         xfs_off_t       start_offset, curr_offset;
247         xfs_dirent_t    *dbp = NULL;
248
249         vp = LINVFS_GET_VP(filp->f_dentry->d_inode);
250         ASSERT(vp);
251
252         /* Try fairly hard to get memory */
253         do {
254                 if ((read_buf = (caddr_t)kmalloc(rlen, GFP_KERNEL)))
255                         break;
256                 rlen >>= 1;
257         } while (rlen >= 1024);
258
259         if (read_buf == NULL)
260                 return -ENOMEM;
261
262         uio.uio_iov = &iov;
263         uio.uio_fmode = filp->f_mode;
264         uio.uio_segflg = UIO_SYSSPACE;
265         curr_offset = uio.uio_offset = filp->f_pos;
266
267         while (!eof) {
268                 uio.uio_resid = iov.iov_len = rlen;
269                 iov.iov_base = read_buf;
270                 uio.uio_iovcnt = 1;
271
272                 start_offset = uio.uio_offset;
273
274                 VOP_READDIR(vp, &uio, NULL, &eof, error);
275                 if ((uio.uio_offset == start_offset) || error) {
276                         size = 0;
277                         break;
278                 }
279
280                 size = rlen - uio.uio_resid;
281                 dbp = (xfs_dirent_t *)read_buf;
282                 while (size > 0) {
283                         namelen = strlen(dbp->d_name);
284
285                         if (filldir(dirent, dbp->d_name, namelen,
286                                         (loff_t) curr_offset,
287                                         (ino_t) dbp->d_ino,
288                                         DT_UNKNOWN)) {
289                                 goto done;
290                         }
291                         size -= dbp->d_reclen;
292                         curr_offset = (loff_t)dbp->d_off & 0x7fffffff;
293                         dbp = nextdp(dbp);
294                 }
295         }
296 done:
297         if (!error) {
298                 if (size == 0)
299                         filp->f_pos = uio.uio_offset & 0x7fffffff;
300                 else if (dbp)
301                         filp->f_pos = curr_offset;
302         }
303
304         kfree(read_buf);
305         return -error;
306 }
307
308
309 STATIC int
310 linvfs_file_mmap(
311         struct file     *filp,
312         struct vm_area_struct *vma)
313 {
314         struct inode    *ip = filp->f_dentry->d_inode;
315         vnode_t         *vp = LINVFS_GET_VP(ip);
316         vattr_t         va = { .va_mask = XFS_AT_UPDATIME };
317         int             error;
318
319         if ((vp->v_type == VREG) && (vp->v_vfsp->vfs_flag & VFS_DMI)) {
320                 xfs_mount_t     *mp = XFS_VFSTOM(vp->v_vfsp);
321
322                 error = -XFS_SEND_MMAP(mp, vma, 0);
323                 if (error)
324                         return error;
325         }
326
327         vma->vm_ops = &linvfs_file_vm_ops;
328
329         VOP_SETATTR(vp, &va, XFS_AT_UPDATIME, NULL, error);
330         return 0;
331 }
332
333
334 STATIC int
335 linvfs_ioctl(
336         struct inode    *inode,
337         struct file     *filp,
338         unsigned int    cmd,
339         unsigned long   arg)
340 {
341         int             error;
342         vnode_t         *vp = LINVFS_GET_VP(inode);
343
344         ASSERT(vp);
345         VOP_IOCTL(vp, inode, filp, cmd, arg, error);
346         VMODIFY(vp);
347
348         /* NOTE:  some of the ioctl's return positive #'s as a
349          *        byte count indicating success, such as
350          *        readlink_by_handle.  So we don't "sign flip"
351          *        like most other routines.  This means true
352          *        errors need to be returned as a negative value.
353          */
354         return error;
355 }
356
357 #ifdef HAVE_VMOP_MPROTECT
358 STATIC int
359 linvfs_mprotect(
360         struct vm_area_struct *vma,
361         unsigned int    newflags)
362 {
363         vnode_t         *vp = LINVFS_GET_VP(vma->vm_file->f_dentry->d_inode);
364         int             error = 0;
365
366         if ((vp->v_type == VREG) && (vp->v_vfsp->vfs_flag & VFS_DMI)) {
367                 if ((vma->vm_flags & VM_MAYSHARE) &&
368                     (newflags & PROT_WRITE) && !(vma->vm_flags & PROT_WRITE)) {
369                         xfs_mount_t     *mp = XFS_VFSTOM(vp->v_vfsp);
370
371                         error = XFS_SEND_MMAP(mp, vma, VM_WRITE);
372                     }
373         }
374         return error;
375 }
376 #endif /* HAVE_VMOP_MPROTECT */
377
378
379 struct file_operations linvfs_file_operations = {
380         .llseek         = generic_file_llseek,
381         .read           = do_sync_read,
382         .write          = do_sync_write,
383         .readv          = linvfs_readv,
384         .writev         = linvfs_writev,
385         .aio_read       = linvfs_read,
386         .aio_write      = linvfs_write,
387         .sendfile       = linvfs_sendfile,
388         .ioctl          = linvfs_ioctl,
389         .mmap           = linvfs_file_mmap,
390         .open           = linvfs_open,
391         .release        = linvfs_release,
392         .fsync          = linvfs_fsync,
393 };
394
395 struct file_operations linvfs_dir_operations = {
396         .read           = generic_read_dir,
397         .readdir        = linvfs_readdir,
398         .ioctl          = linvfs_ioctl,
399         .fsync          = linvfs_fsync,
400 };
401
402 static struct vm_operations_struct linvfs_file_vm_ops = {
403         .nopage         = filemap_nopage,
404 #ifdef HAVE_VMOP_MPROTECT
405         .mprotect       = linvfs_mprotect,
406 #endif
407 };