f12e80a69c6881585d56b5fc7e7d37ce08a173a7
[safe/jmp/linux-2.6] / 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_dir2.h"
25 #include "xfs_trans.h"
26 #include "xfs_dmapi.h"
27 #include "xfs_mount.h"
28 #include "xfs_bmap_btree.h"
29 #include "xfs_alloc_btree.h"
30 #include "xfs_ialloc_btree.h"
31 #include "xfs_alloc.h"
32 #include "xfs_btree.h"
33 #include "xfs_attr_sf.h"
34 #include "xfs_dir2_sf.h"
35 #include "xfs_dinode.h"
36 #include "xfs_inode.h"
37 #include "xfs_error.h"
38 #include "xfs_rw.h"
39 #include "xfs_ioctl32.h"
40
41 #include <linux/dcache.h>
42 #include <linux/smp_lock.h>
43
44 static struct vm_operations_struct xfs_file_vm_ops;
45 #ifdef CONFIG_XFS_DMAPI
46 static struct vm_operations_struct xfs_dmapi_file_vm_ops;
47 #endif
48
49 STATIC_INLINE ssize_t
50 __xfs_file_read(
51         struct kiocb            *iocb,
52         const struct iovec      *iov,
53         unsigned long           nr_segs,
54         int                     ioflags,
55         loff_t                  pos)
56 {
57         struct file             *file = iocb->ki_filp;
58         bhv_vnode_t             *vp = vn_from_inode(file->f_path.dentry->d_inode);
59
60         BUG_ON(iocb->ki_pos != pos);
61         if (unlikely(file->f_flags & O_DIRECT))
62                 ioflags |= IO_ISDIRECT;
63         return bhv_vop_read(vp, iocb, iov, nr_segs, &iocb->ki_pos,
64                                 ioflags, NULL);
65 }
66
67 STATIC ssize_t
68 xfs_file_aio_read(
69         struct kiocb            *iocb,
70         const struct iovec      *iov,
71         unsigned long           nr_segs,
72         loff_t                  pos)
73 {
74         return __xfs_file_read(iocb, iov, nr_segs, IO_ISAIO, pos);
75 }
76
77 STATIC ssize_t
78 xfs_file_aio_read_invis(
79         struct kiocb            *iocb,
80         const struct iovec      *iov,
81         unsigned long           nr_segs,
82         loff_t                  pos)
83 {
84         return __xfs_file_read(iocb, iov, nr_segs, IO_ISAIO|IO_INVIS, pos);
85 }
86
87 STATIC_INLINE ssize_t
88 __xfs_file_write(
89         struct kiocb            *iocb,
90         const struct iovec      *iov,
91         unsigned long           nr_segs,
92         int                     ioflags,
93         loff_t                  pos)
94 {
95         struct file     *file = iocb->ki_filp;
96         struct inode    *inode = file->f_mapping->host;
97         bhv_vnode_t     *vp = vn_from_inode(inode);
98
99         BUG_ON(iocb->ki_pos != pos);
100         if (unlikely(file->f_flags & O_DIRECT))
101                 ioflags |= IO_ISDIRECT;
102         return bhv_vop_write(vp, iocb, iov, nr_segs, &iocb->ki_pos,
103                                 ioflags, NULL);
104 }
105
106 STATIC ssize_t
107 xfs_file_aio_write(
108         struct kiocb            *iocb,
109         const struct iovec      *iov,
110         unsigned long           nr_segs,
111         loff_t                  pos)
112 {
113         return __xfs_file_write(iocb, iov, nr_segs, IO_ISAIO, pos);
114 }
115
116 STATIC ssize_t
117 xfs_file_aio_write_invis(
118         struct kiocb            *iocb,
119         const struct iovec      *iov,
120         unsigned long           nr_segs,
121         loff_t                  pos)
122 {
123         return __xfs_file_write(iocb, iov, nr_segs, IO_ISAIO|IO_INVIS, pos);
124 }
125
126 STATIC ssize_t
127 xfs_file_splice_read(
128         struct file             *infilp,
129         loff_t                  *ppos,
130         struct pipe_inode_info  *pipe,
131         size_t                  len,
132         unsigned int            flags)
133 {
134         return bhv_vop_splice_read(vn_from_inode(infilp->f_path.dentry->d_inode),
135                                    infilp, ppos, pipe, len, flags, 0, NULL);
136 }
137
138 STATIC ssize_t
139 xfs_file_splice_read_invis(
140         struct file             *infilp,
141         loff_t                  *ppos,
142         struct pipe_inode_info  *pipe,
143         size_t                  len,
144         unsigned int            flags)
145 {
146         return bhv_vop_splice_read(vn_from_inode(infilp->f_path.dentry->d_inode),
147                                    infilp, ppos, pipe, len, flags, IO_INVIS,
148                                    NULL);
149 }
150
151 STATIC ssize_t
152 xfs_file_splice_write(
153         struct pipe_inode_info  *pipe,
154         struct file             *outfilp,
155         loff_t                  *ppos,
156         size_t                  len,
157         unsigned int            flags)
158 {
159         return bhv_vop_splice_write(vn_from_inode(outfilp->f_path.dentry->d_inode),
160                                     pipe, outfilp, ppos, len, flags, 0, NULL);
161 }
162
163 STATIC ssize_t
164 xfs_file_splice_write_invis(
165         struct pipe_inode_info  *pipe,
166         struct file             *outfilp,
167         loff_t                  *ppos,
168         size_t                  len,
169         unsigned int            flags)
170 {
171         return bhv_vop_splice_write(vn_from_inode(outfilp->f_path.dentry->d_inode),
172                                     pipe, outfilp, ppos, len, flags, IO_INVIS,
173                                     NULL);
174 }
175
176 STATIC int
177 xfs_file_open(
178         struct inode    *inode,
179         struct file     *filp)
180 {
181         if (!(filp->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS)
182                 return -EFBIG;
183         return -bhv_vop_open(vn_from_inode(inode), NULL);
184 }
185
186 STATIC int
187 xfs_file_release(
188         struct inode    *inode,
189         struct file     *filp)
190 {
191         bhv_vnode_t     *vp = vn_from_inode(inode);
192
193         if (vp)
194                 return -bhv_vop_release(vp);
195         return 0;
196 }
197
198 STATIC int
199 xfs_file_fsync(
200         struct file     *filp,
201         struct dentry   *dentry,
202         int             datasync)
203 {
204         bhv_vnode_t     *vp = vn_from_inode(dentry->d_inode);
205         int             flags = FSYNC_WAIT;
206
207         if (datasync)
208                 flags |= FSYNC_DATA;
209         if (VN_TRUNC(vp))
210                 VUNTRUNCATE(vp);
211         return -bhv_vop_fsync(vp, flags, NULL, (xfs_off_t)0, (xfs_off_t)-1);
212 }
213
214 #ifdef CONFIG_XFS_DMAPI
215 STATIC struct page *
216 xfs_vm_fault(
217         struct vm_area_struct   *vma,
218         struct fault_data       *fdata)
219 {
220         struct inode    *inode = vma->vm_file->f_path.dentry->d_inode;
221         bhv_vnode_t     *vp = vn_from_inode(inode);
222
223         ASSERT_ALWAYS(vp->v_vfsp->vfs_flag & VFS_DMI);
224         if (XFS_SEND_MMAP(XFS_VFSTOM(vp->v_vfsp), vma, 0)) {
225                 fdata->type = VM_FAULT_SIGBUS;
226                 return NULL;
227         }
228         return filemap_fault(vma, fdata);
229 }
230 #endif /* CONFIG_XFS_DMAPI */
231
232 STATIC int
233 xfs_file_readdir(
234         struct file     *filp,
235         void            *dirent,
236         filldir_t       filldir)
237 {
238         int             error = 0;
239         bhv_vnode_t     *vp = vn_from_inode(filp->f_path.dentry->d_inode);
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         /* Try fairly hard to get memory */
250         do {
251                 if ((read_buf = kmalloc(rlen, GFP_KERNEL)))
252                         break;
253                 rlen >>= 1;
254         } while (rlen >= 1024);
255
256         if (read_buf == NULL)
257                 return -ENOMEM;
258
259         uio.uio_iov = &iov;
260         uio.uio_segflg = UIO_SYSSPACE;
261         curr_offset = filp->f_pos;
262         if (filp->f_pos != 0x7fffffff)
263                 uio.uio_offset = filp->f_pos;
264         else
265                 uio.uio_offset = 0xffffffff;
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                 error = bhv_vop_readdir(vp, &uio, NULL, &eof);
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 & 0x7fffffff,
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 = (xfs_dirent_t *)((char *)dbp + dbp->d_reclen);
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 STATIC int
309 xfs_file_mmap(
310         struct file     *filp,
311         struct vm_area_struct *vma)
312 {
313         vma->vm_ops = &xfs_file_vm_ops;
314         vma->vm_flags |= VM_CAN_INVALIDATE | VM_CAN_NONLINEAR;
315
316 #ifdef CONFIG_XFS_DMAPI
317         if (vn_from_inode(filp->f_path.dentry->d_inode)->v_vfsp->vfs_flag & VFS_DMI)
318                 vma->vm_ops = &xfs_dmapi_file_vm_ops;
319 #endif /* CONFIG_XFS_DMAPI */
320
321         file_accessed(filp);
322         return 0;
323 }
324
325 STATIC long
326 xfs_file_ioctl(
327         struct file     *filp,
328         unsigned int    cmd,
329         unsigned long   p)
330 {
331         int             error;
332         struct inode    *inode = filp->f_path.dentry->d_inode;
333         bhv_vnode_t     *vp = vn_from_inode(inode);
334
335         error = bhv_vop_ioctl(vp, inode, filp, 0, cmd, (void __user *)p);
336         VMODIFY(vp);
337
338         /* NOTE:  some of the ioctl's return positive #'s as a
339          *        byte count indicating success, such as
340          *        readlink_by_handle.  So we don't "sign flip"
341          *        like most other routines.  This means true
342          *        errors need to be returned as a negative value.
343          */
344         return error;
345 }
346
347 STATIC long
348 xfs_file_ioctl_invis(
349         struct file     *filp,
350         unsigned int    cmd,
351         unsigned long   p)
352 {
353         int             error;
354         struct inode    *inode = filp->f_path.dentry->d_inode;
355         bhv_vnode_t     *vp = vn_from_inode(inode);
356
357         error = bhv_vop_ioctl(vp, inode, filp, IO_INVIS, cmd, (void __user *)p);
358         VMODIFY(vp);
359
360         /* NOTE:  some of the ioctl's return positive #'s as a
361          *        byte count indicating success, such as
362          *        readlink_by_handle.  So we don't "sign flip"
363          *        like most other routines.  This means true
364          *        errors need to be returned as a negative value.
365          */
366         return error;
367 }
368
369 #ifdef CONFIG_XFS_DMAPI
370 #ifdef HAVE_VMOP_MPROTECT
371 STATIC int
372 xfs_vm_mprotect(
373         struct vm_area_struct *vma,
374         unsigned int    newflags)
375 {
376         bhv_vnode_t     *vp = vn_from_inode(vma->vm_file->f_path.dentry->d_inode);
377         int             error = 0;
378
379         if (vp->v_vfsp->vfs_flag & VFS_DMI) {
380                 if ((vma->vm_flags & VM_MAYSHARE) &&
381                     (newflags & VM_WRITE) && !(vma->vm_flags & VM_WRITE)) {
382                         xfs_mount_t     *mp = XFS_VFSTOM(vp->v_vfsp);
383
384                         error = XFS_SEND_MMAP(mp, vma, VM_WRITE);
385                     }
386         }
387         return error;
388 }
389 #endif /* HAVE_VMOP_MPROTECT */
390 #endif /* CONFIG_XFS_DMAPI */
391
392 #ifdef HAVE_FOP_OPEN_EXEC
393 /* If the user is attempting to execute a file that is offline then
394  * we have to trigger a DMAPI READ event before the file is marked as busy
395  * otherwise the invisible I/O will not be able to write to the file to bring
396  * it back online.
397  */
398 STATIC int
399 xfs_file_open_exec(
400         struct inode    *inode)
401 {
402         bhv_vnode_t     *vp = vn_from_inode(inode);
403
404         if (unlikely(vp->v_vfsp->vfs_flag & VFS_DMI)) {
405                 xfs_mount_t     *mp = XFS_VFSTOM(vp->v_vfsp);
406                 xfs_inode_t     *ip = xfs_vtoi(vp);
407
408                 if (!ip)
409                         return -EINVAL;
410                 if (DM_EVENT_ENABLED(vp->v_vfsp, ip, DM_EVENT_READ))
411                         return -XFS_SEND_DATA(mp, DM_EVENT_READ, vp,
412                                                0, 0, 0, NULL);
413         }
414         return 0;
415 }
416 #endif /* HAVE_FOP_OPEN_EXEC */
417
418 const struct file_operations xfs_file_operations = {
419         .llseek         = generic_file_llseek,
420         .read           = do_sync_read,
421         .write          = do_sync_write,
422         .aio_read       = xfs_file_aio_read,
423         .aio_write      = xfs_file_aio_write,
424         .splice_read    = xfs_file_splice_read,
425         .splice_write   = xfs_file_splice_write,
426         .unlocked_ioctl = xfs_file_ioctl,
427 #ifdef CONFIG_COMPAT
428         .compat_ioctl   = xfs_file_compat_ioctl,
429 #endif
430         .mmap           = xfs_file_mmap,
431         .open           = xfs_file_open,
432         .release        = xfs_file_release,
433         .fsync          = xfs_file_fsync,
434 #ifdef HAVE_FOP_OPEN_EXEC
435         .open_exec      = xfs_file_open_exec,
436 #endif
437 };
438
439 const struct file_operations xfs_invis_file_operations = {
440         .llseek         = generic_file_llseek,
441         .read           = do_sync_read,
442         .write          = do_sync_write,
443         .aio_read       = xfs_file_aio_read_invis,
444         .aio_write      = xfs_file_aio_write_invis,
445         .splice_read    = xfs_file_splice_read_invis,
446         .splice_write   = xfs_file_splice_write_invis,
447         .unlocked_ioctl = xfs_file_ioctl_invis,
448 #ifdef CONFIG_COMPAT
449         .compat_ioctl   = xfs_file_compat_invis_ioctl,
450 #endif
451         .mmap           = xfs_file_mmap,
452         .open           = xfs_file_open,
453         .release        = xfs_file_release,
454         .fsync          = xfs_file_fsync,
455 };
456
457
458 const struct file_operations xfs_dir_file_operations = {
459         .read           = generic_read_dir,
460         .readdir        = xfs_file_readdir,
461         .unlocked_ioctl = xfs_file_ioctl,
462 #ifdef CONFIG_COMPAT
463         .compat_ioctl   = xfs_file_compat_ioctl,
464 #endif
465         .fsync          = xfs_file_fsync,
466 };
467
468 static struct vm_operations_struct xfs_file_vm_ops = {
469         .fault          = filemap_fault,
470 };
471
472 #ifdef CONFIG_XFS_DMAPI
473 static struct vm_operations_struct xfs_dmapi_file_vm_ops = {
474         .fault          = xfs_vm_fault,
475 #ifdef HAVE_VMOP_MPROTECT
476         .mprotect       = xfs_vm_mprotect,
477 #endif
478 };
479 #endif /* CONFIG_XFS_DMAPI */