]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - fs/9p/vfs_file.c
[PATCH] v9fs: fix vfs_inode dereference before NULL check
[mirror_ubuntu-bionic-kernel.git] / fs / 9p / vfs_file.c
CommitLineData
e69e7fe5
EVH
1/*
2 * linux/fs/9p/vfs_file.c
3 *
4 * This file contians vfs file ops for 9P2000.
5 *
6 * Copyright (C) 2004 by Eric Van Hensbergen <ericvh@gmail.com>
7 * Copyright (C) 2002 by Ron Minnich <rminnich@lanl.gov>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to:
21 * Free Software Foundation
22 * 51 Franklin Street, Fifth Floor
23 * Boston, MA 02111-1301 USA
24 *
25 */
26
27#include <linux/module.h>
28#include <linux/errno.h>
29#include <linux/fs.h>
30#include <linux/file.h>
31#include <linux/stat.h>
32#include <linux/string.h>
33#include <linux/smp_lock.h>
34#include <linux/inet.h>
531b1094 35#include <linux/version.h>
e69e7fe5
EVH
36#include <linux/list.h>
37#include <asm/uaccess.h>
38#include <linux/idr.h>
39
40#include "debug.h"
41#include "v9fs.h"
42#include "9p.h"
43#include "v9fs_vfs.h"
44#include "fid.h"
45
46/**
47 * v9fs_file_open - open a file (or directory)
48 * @inode: inode to be opened
49 * @file: file being opened
50 *
51 */
52
53int v9fs_file_open(struct inode *inode, struct file *file)
54{
55 struct v9fs_session_info *v9ses = v9fs_inode2v9ses(inode);
6a3124a3 56 struct v9fs_fid *vfid;
e69e7fe5 57 struct v9fs_fcall *fcall = NULL;
6a3124a3
LI
58 int omode;
59 int fid = V9FS_NOFID;
60 int err;
e69e7fe5 61
0b8dd177
LI
62 dprintk(DEBUG_VFS, "inode: %p file: %p \n", inode, file);
63
6a3124a3
LI
64 vfid = v9fs_fid_lookup(file->f_dentry);
65 if (!vfid) {
e69e7fe5 66 dprintk(DEBUG_ERROR, "Couldn't resolve fid from dentry\n");
0b8dd177
LI
67 return -EBADF;
68 }
e69e7fe5 69
6a3124a3
LI
70 fid = v9fs_get_idpool(&v9ses->fidpool);
71 if (fid < 0) {
16cce6d2
LI
72 eprintk(KERN_WARNING, "newfid fails!\n");
73 return -ENOSPC;
74 }
e69e7fe5 75
6a3124a3
LI
76 err = v9fs_t_walk(v9ses, vfid->fid, fid, NULL, NULL);
77 if (err < 0) {
16cce6d2 78 dprintk(DEBUG_ERROR, "rewalk didn't work\n");
6a3124a3
LI
79 goto put_fid;
80 }
81
16cce6d2
LI
82 /* TODO: do special things for O_EXCL, O_NOFOLLOW, O_SYNC */
83 /* translate open mode appropriately */
6a3124a3
LI
84 omode = v9fs_uflags2omode(file->f_flags);
85 err = v9fs_t_open(v9ses, fid, omode, &fcall);
86 if (err < 0) {
87 PRINT_FCALL_ERROR("open failed", fcall);
16cce6d2
LI
88 goto clunk_fid;
89 }
90
91 vfid = kmalloc(sizeof(struct v9fs_fid), GFP_KERNEL);
92 if (vfid == NULL) {
93 dprintk(DEBUG_ERROR, "out of memory\n");
94 err = -ENOMEM;
95 goto clunk_fid;
6a3124a3 96 }
e69e7fe5 97
6a3124a3
LI
98 file->private_data = vfid;
99 vfid->fid = fid;
100 vfid->fidopen = 1;
101 vfid->fidclunked = 0;
102 vfid->iounit = fcall->params.ropen.iounit;
103 vfid->rdir_pos = 0;
104 vfid->rdir_fcall = NULL;
105 vfid->filp = file;
106 kfree(fcall);
e69e7fe5 107
6a3124a3 108 return 0;
e69e7fe5 109
6a3124a3
LI
110clunk_fid:
111 v9fs_t_clunk(v9ses, fid);
e69e7fe5 112
6a3124a3
LI
113put_fid:
114 v9fs_put_idpool(fid, &v9ses->fidpool);
16cce6d2 115 kfree(fcall);
e69e7fe5 116
6a3124a3 117 return err;
e69e7fe5
EVH
118}
119
120/**
121 * v9fs_file_lock - lock a file (or directory)
122 * @inode: inode to be opened
123 * @file: file being opened
124 *
125 * XXX - this looks like a local only lock, we should extend into 9P
126 * by using open exclusive
127 */
128
129static int v9fs_file_lock(struct file *filp, int cmd, struct file_lock *fl)
130{
131 int res = 0;
132 struct inode *inode = filp->f_dentry->d_inode;
133
134 dprintk(DEBUG_VFS, "filp: %p lock: %p\n", filp, fl);
135
136 /* No mandatory locks */
137 if ((inode->i_mode & (S_ISGID | S_IXGRP)) == S_ISGID)
138 return -ENOLCK;
139
140 if ((IS_SETLK(cmd) || IS_SETLKW(cmd)) && fl->fl_type != F_UNLCK) {
28fd1298 141 filemap_write_and_wait(inode->i_mapping);
e69e7fe5
EVH
142 invalidate_inode_pages(&inode->i_data);
143 }
144
145 return res;
146}
147
148/**
19cba8ab 149 * v9fs_file_read - read from a file
e69e7fe5
EVH
150 * @filep: file pointer to read
151 * @data: data buffer to read data into
152 * @count: size of buffer
153 * @offset: offset at which to read data
154 *
155 */
e69e7fe5 156static ssize_t
19cba8ab
LI
157v9fs_file_read(struct file *filp, char __user * data, size_t count,
158 loff_t * offset)
e69e7fe5
EVH
159{
160 struct inode *inode = filp->f_dentry->d_inode;
161 struct v9fs_session_info *v9ses = v9fs_inode2v9ses(inode);
162 struct v9fs_fid *v9f = filp->private_data;
163 struct v9fs_fcall *fcall = NULL;
164 int fid = v9f->fid;
165 int rsize = 0;
166 int result = 0;
167 int total = 0;
19cba8ab 168 int n;
e69e7fe5
EVH
169
170 dprintk(DEBUG_VFS, "\n");
171
172 rsize = v9ses->maxdata - V9FS_IOHDRSZ;
173 if (v9f->iounit != 0 && rsize > v9f->iounit)
174 rsize = v9f->iounit;
175
176 do {
177 if (count < rsize)
178 rsize = count;
179
180 result = v9fs_t_read(v9ses, fid, *offset, rsize, &fcall);
181
182 if (result < 0) {
183 printk(KERN_ERR "9P2000: v9fs_t_read returned %d\n",
184 result);
185
186 kfree(fcall);
187 return total;
188 } else
189 *offset += result;
190
19cba8ab
LI
191 n = copy_to_user(data, fcall->params.rread.data, result);
192 if (n) {
193 dprintk(DEBUG_ERROR, "Problem copying to user %d\n", n);
194 kfree(fcall);
195 return -EFAULT;
196 }
197
e69e7fe5 198 count -= result;
19cba8ab 199 data += result;
e69e7fe5
EVH
200 total += result;
201
202 kfree(fcall);
203
204 if (result < rsize)
205 break;
206 } while (count);
207
208 return total;
209}
210
211/**
19cba8ab 212 * v9fs_file_write - write to a file
e69e7fe5
EVH
213 * @filep: file pointer to write
214 * @data: data buffer to write data from
215 * @count: size of buffer
216 * @offset: offset at which to write data
217 *
218 */
219
220static ssize_t
19cba8ab
LI
221v9fs_file_write(struct file *filp, const char __user * data,
222 size_t count, loff_t * offset)
e69e7fe5
EVH
223{
224 struct inode *inode = filp->f_dentry->d_inode;
225 struct v9fs_session_info *v9ses = v9fs_inode2v9ses(inode);
226 struct v9fs_fid *v9fid = filp->private_data;
227 struct v9fs_fcall *fcall;
228 int fid = v9fid->fid;
229 int result = -EIO;
230 int rsize = 0;
231 int total = 0;
232
19cba8ab 233 dprintk(DEBUG_VFS, "data %p count %d offset %x\n", data, (int)count,
e69e7fe5
EVH
234 (int)*offset);
235 rsize = v9ses->maxdata - V9FS_IOHDRSZ;
236 if (v9fid->iounit != 0 && rsize > v9fid->iounit)
237 rsize = v9fid->iounit;
238
e69e7fe5
EVH
239 do {
240 if (count < rsize)
241 rsize = count;
242
531b1094 243 result = v9fs_t_write(v9ses, fid, *offset, rsize, data, &fcall);
e69e7fe5 244 if (result < 0) {
531b1094 245 PRINT_FCALL_ERROR("error while writing", fcall);
e69e7fe5
EVH
246 kfree(fcall);
247 return result;
248 } else
249 *offset += result;
250
251 kfree(fcall);
19cba8ab 252 fcall = NULL;
e69e7fe5
EVH
253
254 if (result != rsize) {
255 eprintk(KERN_ERR,
256 "short write: v9fs_t_write returned %d\n",
257 result);
258 break;
259 }
260
261 count -= result;
19cba8ab 262 data += result;
e69e7fe5
EVH
263 total += result;
264 } while (count);
265
147b31cf 266 invalidate_inode_pages2(inode->i_mapping);
e69e7fe5
EVH
267 return total;
268}
269
e69e7fe5
EVH
270struct file_operations v9fs_file_operations = {
271 .llseek = generic_file_llseek,
272 .read = v9fs_file_read,
273 .write = v9fs_file_write,
274 .open = v9fs_file_open,
275 .release = v9fs_dir_release,
276 .lock = v9fs_file_lock,
147b31cf 277 .mmap = generic_file_mmap,
e69e7fe5 278};