]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - fs/9p/vfs_file.c
fs/9p: Add v9fs_inode
[mirror_ubuntu-artful-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
42e8c509
EVH
10 * it under the terms of the GNU General Public License version 2
11 * as published by the Free Software Foundation.
e69e7fe5
EVH
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to:
20 * Free Software Foundation
21 * 51 Franklin Street, Fifth Floor
22 * Boston, MA 02111-1301 USA
23 *
24 */
25
26#include <linux/module.h>
27#include <linux/errno.h>
28#include <linux/fs.h>
914e2637 29#include <linux/sched.h>
e69e7fe5
EVH
30#include <linux/file.h>
31#include <linux/stat.h>
32#include <linux/string.h>
e69e7fe5 33#include <linux/inet.h>
e69e7fe5 34#include <linux/list.h>
637d020a 35#include <linux/pagemap.h>
a099027c 36#include <linux/utsname.h>
e69e7fe5
EVH
37#include <asm/uaccess.h>
38#include <linux/idr.h>
bd238fb4
LI
39#include <net/9p/9p.h>
40#include <net/9p/client.h>
e69e7fe5 41
e69e7fe5 42#include "v9fs.h"
e69e7fe5
EVH
43#include "v9fs_vfs.h"
44#include "fid.h"
60e78d2c 45#include "cache.h"
e69e7fe5 46
7263cebe
AK
47static const struct vm_operations_struct v9fs_file_vm_ops;
48
e69e7fe5
EVH
49/**
50 * v9fs_file_open - open a file (or directory)
51 * @inode: inode to be opened
52 * @file: file being opened
53 *
54 */
55
56int v9fs_file_open(struct inode *inode, struct file *file)
57{
6a3124a3 58 int err;
bd238fb4
LI
59 struct v9fs_session_info *v9ses;
60 struct p9_fid *fid;
61 int omode;
e69e7fe5 62
ef56547e 63 P9_DPRINTK(P9_DEBUG_VFS, "inode: %p file: %p\n", inode, file);
bd238fb4 64 v9ses = v9fs_inode2v9ses(inode);
ef56547e
MK
65 if (v9fs_proto_dotl(v9ses))
66 omode = file->f_flags;
67 else
68 omode = v9fs_uflags2omode(file->f_flags,
69 v9fs_proto_dotu(v9ses));
bd238fb4
LI
70 fid = file->private_data;
71 if (!fid) {
72 fid = v9fs_fid_clone(file->f_path.dentry);
73 if (IS_ERR(fid))
74 return PTR_ERR(fid);
75
76 err = p9_client_open(fid, omode);
9523a841 77 if (err < 0) {
bd238fb4
LI
78 p9_client_clunk(fid);
79 return err;
80 }
ef56547e 81 if (file->f_flags & O_TRUNC) {
7549ae3e 82 i_size_write(inode, 0);
9523a841
EVH
83 inode->i_blocks = 0;
84 }
ef56547e
MK
85 if ((file->f_flags & O_APPEND) &&
86 (!v9fs_proto_dotu(v9ses) && !v9fs_proto_dotl(v9ses)))
2e4bef41 87 generic_file_llseek(file, 0, SEEK_END);
6a3124a3 88 }
e69e7fe5 89
bd238fb4 90 file->private_data = fid;
3cf387d7
AK
91 if (v9ses->cache && !inode->i_private) {
92 /*
93 * clone a fid and add it to inode->i_private
94 * we do it during open time instead of
95 * page dirty time via write_begin/page_mkwrite
96 * because we want write after unlink usecase
97 * to work.
98 */
99 fid = v9fs_writeback_fid(file->f_path.dentry);
100 if (IS_ERR(fid)) {
101 err = PTR_ERR(fid);
102 goto out_error;
103 }
104 inode->i_private = (void *) fid;
105 }
29236f4e 106#ifdef CONFIG_9P_FSCACHE
46848de0 107 if (v9ses->cache)
60e78d2c 108 v9fs_cache_inode_set_cookie(inode, file);
29236f4e 109#endif
6a3124a3 110 return 0;
3cf387d7
AK
111out_error:
112 p9_client_clunk(file->private_data);
113 file->private_data = NULL;
114 return err;
e69e7fe5
EVH
115}
116
117/**
118 * v9fs_file_lock - lock a file (or directory)
ee443996
EVH
119 * @filp: file to be locked
120 * @cmd: lock command
121 * @fl: file lock structure
e69e7fe5 122 *
ee443996 123 * Bugs: this looks like a local only lock, we should extend into 9P
e69e7fe5
EVH
124 * by using open exclusive
125 */
126
127static int v9fs_file_lock(struct file *filp, int cmd, struct file_lock *fl)
128{
129 int res = 0;
d6f787bc 130 struct inode *inode = filp->f_path.dentry->d_inode;
e69e7fe5 131
bd238fb4 132 P9_DPRINTK(P9_DEBUG_VFS, "filp: %p lock: %p\n", filp, fl);
e69e7fe5
EVH
133
134 /* No mandatory locks */
f78233dd 135 if (__mandatory_lock(inode) && fl->fl_type != F_UNLCK)
e69e7fe5
EVH
136 return -ENOLCK;
137
138 if ((IS_SETLK(cmd) || IS_SETLKW(cmd)) && fl->fl_type != F_UNLCK) {
28fd1298 139 filemap_write_and_wait(inode->i_mapping);
fc0ecff6 140 invalidate_mapping_pages(&inode->i_data, 0, -1);
e69e7fe5
EVH
141 }
142
143 return res;
144}
145
a099027c
MK
146static int v9fs_file_do_lock(struct file *filp, int cmd, struct file_lock *fl)
147{
148 struct p9_flock flock;
149 struct p9_fid *fid;
150 uint8_t status;
151 int res = 0;
152 unsigned char fl_type;
153
154 fid = filp->private_data;
155 BUG_ON(fid == NULL);
156
157 if ((fl->fl_flags & FL_POSIX) != FL_POSIX)
158 BUG();
159
160 res = posix_lock_file_wait(filp, fl);
161 if (res < 0)
162 goto out;
163
164 /* convert posix lock to p9 tlock args */
165 memset(&flock, 0, sizeof(flock));
166 flock.type = fl->fl_type;
167 flock.start = fl->fl_start;
168 if (fl->fl_end == OFFSET_MAX)
169 flock.length = 0;
170 else
171 flock.length = fl->fl_end - fl->fl_start + 1;
172 flock.proc_id = fl->fl_pid;
173 flock.client_id = utsname()->nodename;
174 if (IS_SETLKW(cmd))
175 flock.flags = P9_LOCK_FLAGS_BLOCK;
176
177 /*
178 * if its a blocked request and we get P9_LOCK_BLOCKED as the status
179 * for lock request, keep on trying
180 */
181 for (;;) {
182 res = p9_client_lock_dotl(fid, &flock, &status);
183 if (res < 0)
184 break;
185
186 if (status != P9_LOCK_BLOCKED)
187 break;
188 if (status == P9_LOCK_BLOCKED && !IS_SETLKW(cmd))
189 break;
190 schedule_timeout_interruptible(P9_LOCK_TIMEOUT);
191 }
192
193 /* map 9p status to VFS status */
194 switch (status) {
195 case P9_LOCK_SUCCESS:
196 res = 0;
197 break;
198 case P9_LOCK_BLOCKED:
199 res = -EAGAIN;
200 break;
201 case P9_LOCK_ERROR:
202 case P9_LOCK_GRACE:
203 res = -ENOLCK;
204 break;
205 default:
206 BUG();
207 }
208
209 /*
210 * incase server returned error for lock request, revert
211 * it locally
212 */
213 if (res < 0 && fl->fl_type != F_UNLCK) {
214 fl_type = fl->fl_type;
215 fl->fl_type = F_UNLCK;
216 res = posix_lock_file_wait(filp, fl);
217 fl->fl_type = fl_type;
218 }
219out:
220 return res;
221}
222
1d769cd1
MK
223static int v9fs_file_getlock(struct file *filp, struct file_lock *fl)
224{
225 struct p9_getlock glock;
226 struct p9_fid *fid;
227 int res = 0;
228
229 fid = filp->private_data;
230 BUG_ON(fid == NULL);
231
232 posix_test_lock(filp, fl);
233 /*
234 * if we have a conflicting lock locally, no need to validate
235 * with server
236 */
237 if (fl->fl_type != F_UNLCK)
238 return res;
239
240 /* convert posix lock to p9 tgetlock args */
241 memset(&glock, 0, sizeof(glock));
242 glock.type = fl->fl_type;
243 glock.start = fl->fl_start;
244 if (fl->fl_end == OFFSET_MAX)
245 glock.length = 0;
246 else
247 glock.length = fl->fl_end - fl->fl_start + 1;
248 glock.proc_id = fl->fl_pid;
249 glock.client_id = utsname()->nodename;
250
251 res = p9_client_getlock_dotl(fid, &glock);
252 if (res < 0)
253 return res;
254 if (glock.type != F_UNLCK) {
255 fl->fl_type = glock.type;
256 fl->fl_start = glock.start;
257 if (glock.length == 0)
258 fl->fl_end = OFFSET_MAX;
259 else
260 fl->fl_end = glock.start + glock.length - 1;
261 fl->fl_pid = glock.proc_id;
262 } else
263 fl->fl_type = F_UNLCK;
264
265 return res;
266}
267
a099027c
MK
268/**
269 * v9fs_file_lock_dotl - lock a file (or directory)
270 * @filp: file to be locked
271 * @cmd: lock command
272 * @fl: file lock structure
273 *
274 */
275
276static int v9fs_file_lock_dotl(struct file *filp, int cmd, struct file_lock *fl)
277{
278 struct inode *inode = filp->f_path.dentry->d_inode;
279 int ret = -ENOLCK;
280
281 P9_DPRINTK(P9_DEBUG_VFS, "filp: %p cmd:%d lock: %p name: %s\n", filp,
282 cmd, fl, filp->f_path.dentry->d_name.name);
283
284 /* No mandatory locks */
285 if (__mandatory_lock(inode) && fl->fl_type != F_UNLCK)
286 goto out_err;
287
288 if ((IS_SETLK(cmd) || IS_SETLKW(cmd)) && fl->fl_type != F_UNLCK) {
289 filemap_write_and_wait(inode->i_mapping);
290 invalidate_mapping_pages(&inode->i_data, 0, -1);
291 }
292
293 if (IS_SETLK(cmd) || IS_SETLKW(cmd))
294 ret = v9fs_file_do_lock(filp, cmd, fl);
1d769cd1
MK
295 else if (IS_GETLK(cmd))
296 ret = v9fs_file_getlock(filp, fl);
a099027c
MK
297 else
298 ret = -EINVAL;
299out_err:
300 return ret;
301}
302
303/**
304 * v9fs_file_flock_dotl - lock a file
305 * @filp: file to be locked
306 * @cmd: lock command
307 * @fl: file lock structure
308 *
309 */
310
311static int v9fs_file_flock_dotl(struct file *filp, int cmd,
312 struct file_lock *fl)
313{
314 struct inode *inode = filp->f_path.dentry->d_inode;
315 int ret = -ENOLCK;
316
317 P9_DPRINTK(P9_DEBUG_VFS, "filp: %p cmd:%d lock: %p name: %s\n", filp,
318 cmd, fl, filp->f_path.dentry->d_name.name);
319
320 /* No mandatory locks */
321 if (__mandatory_lock(inode) && fl->fl_type != F_UNLCK)
322 goto out_err;
323
324 if (!(fl->fl_flags & FL_FLOCK))
325 goto out_err;
326
327 if ((IS_SETLK(cmd) || IS_SETLKW(cmd)) && fl->fl_type != F_UNLCK) {
328 filemap_write_and_wait(inode->i_mapping);
329 invalidate_mapping_pages(&inode->i_data, 0, -1);
330 }
331 /* Convert flock to posix lock */
332 fl->fl_owner = (fl_owner_t)filp;
333 fl->fl_start = 0;
334 fl->fl_end = OFFSET_MAX;
335 fl->fl_flags |= FL_POSIX;
336 fl->fl_flags ^= FL_FLOCK;
337
338 if (IS_SETLK(cmd) | IS_SETLKW(cmd))
339 ret = v9fs_file_do_lock(filp, cmd, fl);
340 else
341 ret = -EINVAL;
342out_err:
343 return ret;
344}
345
e69e7fe5 346/**
17311779
AK
347 * v9fs_fid_readn - read from a fid
348 * @fid: fid to read
e69e7fe5 349 * @data: data buffer to read data into
fbedadc1 350 * @udata: user data buffer to read data into
e69e7fe5
EVH
351 * @count: size of buffer
352 * @offset: offset at which to read data
353 *
354 */
fbedadc1 355ssize_t
17311779 356v9fs_fid_readn(struct p9_fid *fid, char *data, char __user *udata, u32 count,
fbedadc1
EVH
357 u64 offset)
358{
97e8442b 359 int n, total, size;
fbedadc1 360
51a87c55 361 P9_DPRINTK(P9_DEBUG_VFS, "fid %d offset %llu count %d\n", fid->fid,
17311779 362 (long long unsigned) offset, count);
fbedadc1
EVH
363 n = 0;
364 total = 0;
97e8442b 365 size = fid->iounit ? fid->iounit : fid->clnt->msize - P9_IOHDRSZ;
fbedadc1
EVH
366 do {
367 n = p9_client_read(fid, data, udata, offset, count);
368 if (n <= 0)
369 break;
370
371 if (data)
372 data += n;
373 if (udata)
374 udata += n;
375
376 offset += n;
377 count -= n;
378 total += n;
97e8442b 379 } while (count > 0 && n == size);
fbedadc1
EVH
380
381 if (n < 0)
382 total = n;
383
384 return total;
385}
386
17311779
AK
387/**
388 * v9fs_file_readn - read from a file
389 * @filp: file pointer to read
390 * @data: data buffer to read data into
391 * @udata: user data buffer to read data into
392 * @count: size of buffer
393 * @offset: offset at which to read data
394 *
395 */
396ssize_t
397v9fs_file_readn(struct file *filp, char *data, char __user *udata, u32 count,
398 u64 offset)
399{
400 return v9fs_fid_readn(filp->private_data, data, udata, count, offset);
401}
402
fbedadc1
EVH
403/**
404 * v9fs_file_read - read from a file
405 * @filp: file pointer to read
406 * @udata: user data buffer to read data into
407 * @count: size of buffer
408 * @offset: offset at which to read data
409 *
410 */
411
e69e7fe5 412static ssize_t
fbedadc1 413v9fs_file_read(struct file *filp, char __user *udata, size_t count,
19cba8ab 414 loff_t * offset)
e69e7fe5 415{
bd238fb4
LI
416 int ret;
417 struct p9_fid *fid;
97e8442b 418 size_t size;
e69e7fe5 419
ea2e7996 420 P9_DPRINTK(P9_DEBUG_VFS, "count %zu offset %lld\n", count, *offset);
bd238fb4 421 fid = filp->private_data;
fbedadc1 422
97e8442b
MK
423 size = fid->iounit ? fid->iounit : fid->clnt->msize - P9_IOHDRSZ;
424 if (count > size)
fbedadc1
EVH
425 ret = v9fs_file_readn(filp, NULL, udata, count, *offset);
426 else
427 ret = p9_client_read(fid, NULL, udata, *offset, count);
428
bd238fb4
LI
429 if (ret > 0)
430 *offset += ret;
e69e7fe5 431
bd238fb4 432 return ret;
e69e7fe5
EVH
433}
434
17311779
AK
435ssize_t
436v9fs_file_write_internal(struct inode *inode, struct p9_fid *fid,
437 const char __user *data, size_t count,
438 loff_t *offset, int invalidate)
e69e7fe5 439{
8d40fa24 440 int n;
17311779 441 size_t total = 0;
dfb0ec2e 442 struct p9_client *clnt;
fc0f2961 443 loff_t origin = *offset;
637d020a 444 unsigned long pg_start, pg_end;
e69e7fe5 445
bd238fb4
LI
446 P9_DPRINTK(P9_DEBUG_VFS, "data %p count %d offset %x\n", data,
447 (int)count, (int)*offset);
e69e7fe5 448
dfb0ec2e 449 clnt = fid->clnt;
dfb0ec2e 450 do {
8d40fa24 451 n = p9_client_write(fid, NULL, data+total, origin+total, count);
dfb0ec2e
EVH
452 if (n <= 0)
453 break;
454 count -= n;
455 total += n;
456 } while (count > 0);
457
17311779 458 if (invalidate && (total > 0)) {
637d020a
AK
459 pg_start = origin >> PAGE_CACHE_SHIFT;
460 pg_end = (origin + total - 1) >> PAGE_CACHE_SHIFT;
60e78d2c
AK
461 if (inode->i_mapping && inode->i_mapping->nrpages)
462 invalidate_inode_pages2_range(inode->i_mapping,
463 pg_start, pg_end);
dfb0ec2e 464 *offset += total;
637d020a 465 i_size_write(inode, i_size_read(inode) + total);
7549ae3e 466 inode->i_blocks = (i_size_read(inode) + 512 - 1) >> 9;
9523a841 467 }
dfb0ec2e 468 if (n < 0)
17311779
AK
469 return n;
470
471 return total;
472}
473
474/**
475 * v9fs_file_write - write to a file
476 * @filp: file pointer to write
477 * @data: data buffer to write data from
478 * @count: size of buffer
479 * @offset: offset at which to write data
480 *
481 */
482static ssize_t
483v9fs_file_write(struct file *filp, const char __user * data,
484 size_t count, loff_t *offset)
485{
486 ssize_t retval = 0;
487 loff_t origin = *offset;
488
489
490 retval = generic_write_checks(filp, &origin, &count, 0);
491 if (retval)
492 goto out;
493
494 retval = -EINVAL;
495 if ((ssize_t) count < 0)
496 goto out;
497 retval = 0;
498 if (!count)
499 goto out;
500
501 return v9fs_file_write_internal(filp->f_path.dentry->d_inode,
502 filp->private_data,
503 data, count, offset, 1);
3834b12a
HPB
504out:
505 return retval;
e69e7fe5
EVH
506}
507
7263cebe 508
7ea80859 509static int v9fs_file_fsync(struct file *filp, int datasync)
7a4439c4
MK
510{
511 struct p9_fid *fid;
512 struct p9_wstat wstat;
513 int retval;
514
7ea80859 515 P9_DPRINTK(P9_DEBUG_VFS, "filp %p datasync %x\n", filp, datasync);
7a4439c4
MK
516
517 fid = filp->private_data;
518 v9fs_blank_wstat(&wstat);
519
520 retval = p9_client_wstat(fid, &wstat);
521 return retval;
522}
523
b165d601 524int v9fs_file_fsync_dotl(struct file *filp, int datasync)
920e65dc
VJJ
525{
526 struct p9_fid *fid;
527 int retval;
528
529 P9_DPRINTK(P9_DEBUG_VFS, "v9fs_file_fsync_dotl: filp %p datasync %x\n",
530 filp, datasync);
531
532 fid = filp->private_data;
533
b165d601 534 retval = p9_client_fsync(fid, datasync);
920e65dc
VJJ
535 return retval;
536}
537
7263cebe
AK
538static int
539v9fs_file_mmap(struct file *file, struct vm_area_struct *vma)
540{
541 int retval;
542
543 retval = generic_file_mmap(file, vma);
544 if (!retval)
545 vma->vm_ops = &v9fs_file_vm_ops;
546
547 return retval;
548}
549
550static int
551v9fs_vm_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
552{
553 struct page *page = vmf->page;
554 struct file *filp = vma->vm_file;
555 struct inode *inode = filp->f_path.dentry->d_inode;
556
557
558 P9_DPRINTK(P9_DEBUG_VFS, "page %p fid %lx\n",
559 page, (unsigned long)filp->private_data);
560
561 /* make sure the cache has finished storing the page */
562 v9fs_fscache_wait_on_page_write(inode, page);
563 BUG_ON(!inode->i_private);
564 lock_page(page);
565 if (page->mapping != inode->i_mapping)
566 goto out_unlock;
567
568 return VM_FAULT_LOCKED;
569out_unlock:
570 unlock_page(page);
571 return VM_FAULT_NOPAGE;
572}
573
574static const struct vm_operations_struct v9fs_file_vm_ops = {
575 .fault = filemap_fault,
576 .page_mkwrite = v9fs_vm_page_mkwrite,
577};
578
29236f4e 579const struct file_operations v9fs_cached_file_operations = {
e03abc0c
EVH
580 .llseek = generic_file_llseek,
581 .read = do_sync_read,
7263cebe 582 .write = do_sync_write,
e03abc0c 583 .aio_read = generic_file_aio_read,
7263cebe 584 .aio_write = generic_file_aio_write,
e03abc0c
EVH
585 .open = v9fs_file_open,
586 .release = v9fs_dir_release,
587 .lock = v9fs_file_lock,
7263cebe 588 .mmap = v9fs_file_mmap,
7a4439c4 589 .fsync = v9fs_file_fsync,
e03abc0c
EVH
590};
591
29236f4e 592const struct file_operations v9fs_cached_file_operations_dotl = {
b04faaf3
VJJ
593 .llseek = generic_file_llseek,
594 .read = do_sync_read,
7263cebe 595 .write = do_sync_write,
b04faaf3 596 .aio_read = generic_file_aio_read,
7263cebe 597 .aio_write = generic_file_aio_write,
b04faaf3
VJJ
598 .open = v9fs_file_open,
599 .release = v9fs_dir_release,
a099027c
MK
600 .lock = v9fs_file_lock_dotl,
601 .flock = v9fs_file_flock_dotl,
7263cebe 602 .mmap = v9fs_file_mmap,
920e65dc 603 .fsync = v9fs_file_fsync_dotl,
b04faaf3
VJJ
604};
605
4b6f5d20 606const struct file_operations v9fs_file_operations = {
e69e7fe5
EVH
607 .llseek = generic_file_llseek,
608 .read = v9fs_file_read,
609 .write = v9fs_file_write,
610 .open = v9fs_file_open,
611 .release = v9fs_dir_release,
612 .lock = v9fs_file_lock,
14b8869f 613 .mmap = generic_file_readonly_mmap,
7a4439c4 614 .fsync = v9fs_file_fsync,
e69e7fe5 615};
9b6533c9
SK
616
617const struct file_operations v9fs_file_operations_dotl = {
618 .llseek = generic_file_llseek,
619 .read = v9fs_file_read,
620 .write = v9fs_file_write,
621 .open = v9fs_file_open,
622 .release = v9fs_dir_release,
a099027c
MK
623 .lock = v9fs_file_lock_dotl,
624 .flock = v9fs_file_flock_dotl,
9b6533c9 625 .mmap = generic_file_readonly_mmap,
920e65dc 626 .fsync = v9fs_file_fsync_dotl,
9b6533c9 627};