]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - fs/xfs/linux-2.6/xfs_file.c
[XFS] fix NULL pointer dereference in xfs_log_force_umount
[mirror_ubuntu-artful-kernel.git] / fs / xfs / linux-2.6 / xfs_file.c
CommitLineData
1da177e4 1/*
7b718769
NS
2 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
1da177e4 4 *
7b718769
NS
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
1da177e4
LT
7 * published by the Free Software Foundation.
8 *
7b718769
NS
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.
1da177e4 13 *
7b718769
NS
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
1da177e4 17 */
1da177e4 18#include "xfs.h"
a844f451 19#include "xfs_bit.h"
1da177e4 20#include "xfs_log.h"
a844f451 21#include "xfs_inum.h"
1da177e4 22#include "xfs_sb.h"
a844f451 23#include "xfs_ag.h"
1da177e4
LT
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"
1da177e4
LT
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"
739bfb2a 40#include "xfs_vnodeops.h"
1da177e4
LT
41
42#include <linux/dcache.h>
43#include <linux/smp_lock.h>
44
3562fd45 45static struct vm_operations_struct xfs_file_vm_ops;
1da177e4 46
7989cb8e 47STATIC_INLINE ssize_t
3562fd45 48__xfs_file_read(
1da177e4 49 struct kiocb *iocb,
027445c3
BP
50 const struct iovec *iov,
51 unsigned long nr_segs,
1da177e4 52 int ioflags,
1da177e4
LT
53 loff_t pos)
54{
1da177e4 55 struct file *file = iocb->ki_filp;
1da177e4
LT
56
57 BUG_ON(iocb->ki_pos != pos);
1da177e4
LT
58 if (unlikely(file->f_flags & O_DIRECT))
59 ioflags |= IO_ISDIRECT;
739bfb2a
CH
60 return xfs_read(XFS_I(file->f_path.dentry->d_inode), iocb, iov,
61 nr_segs, &iocb->ki_pos, ioflags);
1da177e4
LT
62}
63
1da177e4 64STATIC ssize_t
3562fd45 65xfs_file_aio_read(
1da177e4 66 struct kiocb *iocb,
027445c3
BP
67 const struct iovec *iov,
68 unsigned long nr_segs,
1da177e4
LT
69 loff_t pos)
70{
027445c3 71 return __xfs_file_read(iocb, iov, nr_segs, IO_ISAIO, pos);
1da177e4
LT
72}
73
74STATIC ssize_t
3562fd45 75xfs_file_aio_read_invis(
1da177e4 76 struct kiocb *iocb,
027445c3
BP
77 const struct iovec *iov,
78 unsigned long nr_segs,
1da177e4
LT
79 loff_t pos)
80{
027445c3 81 return __xfs_file_read(iocb, iov, nr_segs, IO_ISAIO|IO_INVIS, pos);
1da177e4
LT
82}
83
7989cb8e 84STATIC_INLINE ssize_t
3562fd45 85__xfs_file_write(
027445c3
BP
86 struct kiocb *iocb,
87 const struct iovec *iov,
88 unsigned long nr_segs,
89 int ioflags,
90 loff_t pos)
1da177e4 91{
1da177e4 92 struct file *file = iocb->ki_filp;
1da177e4
LT
93
94 BUG_ON(iocb->ki_pos != pos);
95 if (unlikely(file->f_flags & O_DIRECT))
96 ioflags |= IO_ISDIRECT;
739bfb2a
CH
97 return xfs_write(XFS_I(file->f_mapping->host), iocb, iov, nr_segs,
98 &iocb->ki_pos, ioflags);
1da177e4
LT
99}
100
1da177e4 101STATIC ssize_t
3562fd45 102xfs_file_aio_write(
1da177e4 103 struct kiocb *iocb,
027445c3
BP
104 const struct iovec *iov,
105 unsigned long nr_segs,
1da177e4
LT
106 loff_t pos)
107{
027445c3 108 return __xfs_file_write(iocb, iov, nr_segs, IO_ISAIO, pos);
1da177e4
LT
109}
110
111STATIC ssize_t
3562fd45 112xfs_file_aio_write_invis(
1da177e4 113 struct kiocb *iocb,
027445c3
BP
114 const struct iovec *iov,
115 unsigned long nr_segs,
1da177e4
LT
116 loff_t pos)
117{
027445c3 118 return __xfs_file_write(iocb, iov, nr_segs, IO_ISAIO|IO_INVIS, pos);
1da177e4
LT
119}
120
1b895840
NS
121STATIC ssize_t
122xfs_file_splice_read(
123 struct file *infilp,
cbb7e577 124 loff_t *ppos,
3a326a2c 125 struct pipe_inode_info *pipe,
1b895840
NS
126 size_t len,
127 unsigned int flags)
128{
739bfb2a
CH
129 return xfs_splice_read(XFS_I(infilp->f_path.dentry->d_inode),
130 infilp, ppos, pipe, len, flags, 0);
1b895840
NS
131}
132
133STATIC ssize_t
134xfs_file_splice_read_invis(
135 struct file *infilp,
cbb7e577 136 loff_t *ppos,
3a326a2c 137 struct pipe_inode_info *pipe,
1b895840
NS
138 size_t len,
139 unsigned int flags)
140{
739bfb2a
CH
141 return xfs_splice_read(XFS_I(infilp->f_path.dentry->d_inode),
142 infilp, ppos, pipe, len, flags, IO_INVIS);
1b895840
NS
143}
144
145STATIC ssize_t
146xfs_file_splice_write(
3a326a2c 147 struct pipe_inode_info *pipe,
1b895840 148 struct file *outfilp,
cbb7e577 149 loff_t *ppos,
1b895840
NS
150 size_t len,
151 unsigned int flags)
152{
739bfb2a
CH
153 return xfs_splice_write(XFS_I(outfilp->f_path.dentry->d_inode),
154 pipe, outfilp, ppos, len, flags, 0);
1b895840
NS
155}
156
157STATIC ssize_t
158xfs_file_splice_write_invis(
3a326a2c 159 struct pipe_inode_info *pipe,
1b895840 160 struct file *outfilp,
cbb7e577 161 loff_t *ppos,
1b895840
NS
162 size_t len,
163 unsigned int flags)
164{
739bfb2a
CH
165 return xfs_splice_write(XFS_I(outfilp->f_path.dentry->d_inode),
166 pipe, outfilp, ppos, len, flags, IO_INVIS);
1b895840 167}
1da177e4
LT
168
169STATIC int
3562fd45 170xfs_file_open(
1da177e4
LT
171 struct inode *inode,
172 struct file *filp)
173{
1da177e4
LT
174 if (!(filp->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS)
175 return -EFBIG;
739bfb2a 176 return -xfs_open(XFS_I(inode));
1da177e4
LT
177}
178
1da177e4 179STATIC int
3562fd45 180xfs_file_release(
1da177e4
LT
181 struct inode *inode,
182 struct file *filp)
183{
739bfb2a 184 return -xfs_release(XFS_I(inode));
1da177e4
LT
185}
186
978b7237
DC
187/*
188 * We ignore the datasync flag here because a datasync is effectively
189 * identical to an fsync. That is, datasync implies that we need to write
190 * only the metadata needed to be able to access the data that is written
191 * if we crash after the call completes. Hence if we are writing beyond
192 * EOF we have to log the inode size change as well, which makes it a
193 * full fsync. If we don't write beyond EOF, the inode core will be
194 * clean in memory and so we don't need to log the inode, just like
195 * fsync.
196 */
1da177e4 197STATIC int
3562fd45 198xfs_file_fsync(
1da177e4
LT
199 struct file *filp,
200 struct dentry *dentry,
201 int datasync)
202{
b3aea4ed 203 xfs_iflags_clear(XFS_I(dentry->d_inode), XFS_ITRUNCATED);
978b7237 204 return -xfs_fsync(XFS_I(dentry->d_inode));
1da177e4
LT
205}
206
1da177e4 207STATIC int
3562fd45 208xfs_file_readdir(
1da177e4
LT
209 struct file *filp,
210 void *dirent,
211 filldir_t filldir)
212{
051e7cd4 213 struct inode *inode = filp->f_path.dentry->d_inode;
739bfb2a 214 xfs_inode_t *ip = XFS_I(inode);
051e7cd4
CH
215 int error;
216 size_t bufsize;
217
218 /*
219 * The Linux API doesn't pass down the total size of the buffer
220 * we read into down to the filesystem. With the filldir concept
221 * it's not needed for correct information, but the XFS dir2 leaf
222 * code wants an estimate of the buffer size to calculate it's
223 * readahead window and size the buffers used for mapping to
224 * physical blocks.
225 *
226 * Try to give it an estimate that's good enough, maybe at some
227 * point we can change the ->readdir prototype to include the
228 * buffer size.
229 */
230 bufsize = (size_t)min_t(loff_t, PAGE_SIZE, inode->i_size);
231
739bfb2a 232 error = xfs_readdir(ip, dirent, bufsize,
051e7cd4
CH
233 (xfs_off_t *)&filp->f_pos, filldir);
234 if (error)
235 return -error;
236 return 0;
1da177e4
LT
237}
238
1da177e4 239STATIC int
3562fd45 240xfs_file_mmap(
1da177e4
LT
241 struct file *filp,
242 struct vm_area_struct *vma)
243{
3562fd45 244 vma->vm_ops = &xfs_file_vm_ops;
d0217ac0 245 vma->vm_flags |= VM_CAN_NONLINEAR;
6fac0cb4 246
fbc1462b 247 file_accessed(filp);
1da177e4
LT
248 return 0;
249}
250
1da177e4 251STATIC long
3562fd45 252xfs_file_ioctl(
1da177e4
LT
253 struct file *filp,
254 unsigned int cmd,
67fcaa73 255 unsigned long p)
1da177e4
LT
256{
257 int error;
e678fb0d 258 struct inode *inode = filp->f_path.dentry->d_inode;
1da177e4 259
739bfb2a 260 error = xfs_ioctl(XFS_I(inode), filp, 0, cmd, (void __user *)p);
b3aea4ed 261 xfs_iflags_set(XFS_I(inode), XFS_IMODIFIED);
1da177e4
LT
262
263 /* NOTE: some of the ioctl's return positive #'s as a
264 * byte count indicating success, such as
265 * readlink_by_handle. So we don't "sign flip"
266 * like most other routines. This means true
267 * errors need to be returned as a negative value.
268 */
269 return error;
270}
271
272STATIC long
3562fd45 273xfs_file_ioctl_invis(
1da177e4
LT
274 struct file *filp,
275 unsigned int cmd,
67fcaa73 276 unsigned long p)
1da177e4 277{
1b895840 278 int error;
e678fb0d 279 struct inode *inode = filp->f_path.dentry->d_inode;
1da177e4 280
739bfb2a 281 error = xfs_ioctl(XFS_I(inode), filp, IO_INVIS, cmd, (void __user *)p);
b3aea4ed 282 xfs_iflags_set(XFS_I(inode), XFS_IMODIFIED);
1da177e4
LT
283
284 /* NOTE: some of the ioctl's return positive #'s as a
285 * byte count indicating success, such as
286 * readlink_by_handle. So we don't "sign flip"
287 * like most other routines. This means true
288 * errors need to be returned as a negative value.
289 */
290 return error;
291}
292
4f57dbc6
DC
293/*
294 * mmap()d file has taken write protection fault and is being made
295 * writable. We can set the page state up correctly for a writable
296 * page, which means we can do correct delalloc accounting (ENOSPC
297 * checking!) and unwritten extent mapping.
298 */
299STATIC int
300xfs_vm_page_mkwrite(
301 struct vm_area_struct *vma,
302 struct page *page)
303{
304 return block_page_mkwrite(vma, page, xfs_get_blocks);
305}
306
4b6f5d20 307const struct file_operations xfs_file_operations = {
1da177e4
LT
308 .llseek = generic_file_llseek,
309 .read = do_sync_read,
bb3f724e 310 .write = do_sync_write,
3562fd45
NS
311 .aio_read = xfs_file_aio_read,
312 .aio_write = xfs_file_aio_write,
1b895840
NS
313 .splice_read = xfs_file_splice_read,
314 .splice_write = xfs_file_splice_write,
3562fd45 315 .unlocked_ioctl = xfs_file_ioctl,
1da177e4 316#ifdef CONFIG_COMPAT
3562fd45 317 .compat_ioctl = xfs_file_compat_ioctl,
1da177e4 318#endif
3562fd45
NS
319 .mmap = xfs_file_mmap,
320 .open = xfs_file_open,
321 .release = xfs_file_release,
322 .fsync = xfs_file_fsync,
1da177e4 323#ifdef HAVE_FOP_OPEN_EXEC
3562fd45 324 .open_exec = xfs_file_open_exec,
1da177e4
LT
325#endif
326};
327
4b6f5d20 328const struct file_operations xfs_invis_file_operations = {
1da177e4
LT
329 .llseek = generic_file_llseek,
330 .read = do_sync_read,
bb3f724e 331 .write = do_sync_write,
3562fd45
NS
332 .aio_read = xfs_file_aio_read_invis,
333 .aio_write = xfs_file_aio_write_invis,
1b895840
NS
334 .splice_read = xfs_file_splice_read_invis,
335 .splice_write = xfs_file_splice_write_invis,
3562fd45 336 .unlocked_ioctl = xfs_file_ioctl_invis,
1da177e4 337#ifdef CONFIG_COMPAT
3562fd45 338 .compat_ioctl = xfs_file_compat_invis_ioctl,
1da177e4 339#endif
3562fd45
NS
340 .mmap = xfs_file_mmap,
341 .open = xfs_file_open,
342 .release = xfs_file_release,
343 .fsync = xfs_file_fsync,
1da177e4
LT
344};
345
346
4b6f5d20 347const struct file_operations xfs_dir_file_operations = {
1da177e4 348 .read = generic_read_dir,
3562fd45 349 .readdir = xfs_file_readdir,
59af1584 350 .llseek = generic_file_llseek,
3562fd45 351 .unlocked_ioctl = xfs_file_ioctl,
d3870398 352#ifdef CONFIG_COMPAT
3562fd45 353 .compat_ioctl = xfs_file_compat_ioctl,
d3870398 354#endif
3562fd45 355 .fsync = xfs_file_fsync,
1da177e4
LT
356};
357
3562fd45 358static struct vm_operations_struct xfs_file_vm_ops = {
54cb8821 359 .fault = filemap_fault,
4f57dbc6 360 .page_mkwrite = xfs_vm_page_mkwrite,
6fac0cb4 361};