]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - fs/9p/vfs_file.c
fs/9p: Add buffered write support for v9fs.
[mirror_ubuntu-artful-kernel.git] / fs / 9p / vfs_file.c
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 version 2
11 * as published by the Free Software Foundation.
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>
29 #include <linux/sched.h>
30 #include <linux/file.h>
31 #include <linux/stat.h>
32 #include <linux/string.h>
33 #include <linux/inet.h>
34 #include <linux/list.h>
35 #include <linux/pagemap.h>
36 #include <linux/utsname.h>
37 #include <asm/uaccess.h>
38 #include <linux/idr.h>
39 #include <net/9p/9p.h>
40 #include <net/9p/client.h>
41
42 #include "v9fs.h"
43 #include "v9fs_vfs.h"
44 #include "fid.h"
45 #include "cache.h"
46
47 static const struct vm_operations_struct v9fs_file_vm_ops;
48
49 /**
50 * v9fs_file_open - open a file (or directory)
51 * @inode: inode to be opened
52 * @file: file being opened
53 *
54 */
55
56 int v9fs_file_open(struct inode *inode, struct file *file)
57 {
58 int err;
59 struct v9fs_session_info *v9ses;
60 struct p9_fid *fid;
61 int omode;
62
63 P9_DPRINTK(P9_DEBUG_VFS, "inode: %p file: %p\n", inode, file);
64 v9ses = v9fs_inode2v9ses(inode);
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));
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);
77 if (err < 0) {
78 p9_client_clunk(fid);
79 return err;
80 }
81 if (file->f_flags & O_TRUNC) {
82 i_size_write(inode, 0);
83 inode->i_blocks = 0;
84 }
85 if ((file->f_flags & O_APPEND) &&
86 (!v9fs_proto_dotu(v9ses) && !v9fs_proto_dotl(v9ses)))
87 generic_file_llseek(file, 0, SEEK_END);
88 }
89
90 file->private_data = fid;
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 }
106 #ifdef CONFIG_9P_FSCACHE
107 if (v9ses->cache)
108 v9fs_cache_inode_set_cookie(inode, file);
109 #endif
110 return 0;
111 out_error:
112 p9_client_clunk(file->private_data);
113 file->private_data = NULL;
114 return err;
115 }
116
117 /**
118 * v9fs_file_lock - lock a file (or directory)
119 * @filp: file to be locked
120 * @cmd: lock command
121 * @fl: file lock structure
122 *
123 * Bugs: this looks like a local only lock, we should extend into 9P
124 * by using open exclusive
125 */
126
127 static int v9fs_file_lock(struct file *filp, int cmd, struct file_lock *fl)
128 {
129 int res = 0;
130 struct inode *inode = filp->f_path.dentry->d_inode;
131
132 P9_DPRINTK(P9_DEBUG_VFS, "filp: %p lock: %p\n", filp, fl);
133
134 /* No mandatory locks */
135 if (__mandatory_lock(inode) && fl->fl_type != F_UNLCK)
136 return -ENOLCK;
137
138 if ((IS_SETLK(cmd) || IS_SETLKW(cmd)) && fl->fl_type != F_UNLCK) {
139 filemap_write_and_wait(inode->i_mapping);
140 invalidate_mapping_pages(&inode->i_data, 0, -1);
141 }
142
143 return res;
144 }
145
146 static 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 }
219 out:
220 return res;
221 }
222
223 static 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
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
276 static 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);
295 else if (IS_GETLK(cmd))
296 ret = v9fs_file_getlock(filp, fl);
297 else
298 ret = -EINVAL;
299 out_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
311 static 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;
342 out_err:
343 return ret;
344 }
345
346 /**
347 * v9fs_fid_readn - read from a fid
348 * @fid: fid to read
349 * @data: data buffer to read data into
350 * @udata: user data buffer to read data into
351 * @count: size of buffer
352 * @offset: offset at which to read data
353 *
354 */
355 ssize_t
356 v9fs_fid_readn(struct p9_fid *fid, char *data, char __user *udata, u32 count,
357 u64 offset)
358 {
359 int n, total, size;
360
361 P9_DPRINTK(P9_DEBUG_VFS, "fid %d offset %llu count %d\n", fid->fid,
362 (long long unsigned) offset, count);
363 n = 0;
364 total = 0;
365 size = fid->iounit ? fid->iounit : fid->clnt->msize - P9_IOHDRSZ;
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;
379 } while (count > 0 && n == size);
380
381 if (n < 0)
382 total = n;
383
384 return total;
385 }
386
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 */
396 ssize_t
397 v9fs_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
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
412 static ssize_t
413 v9fs_file_read(struct file *filp, char __user *udata, size_t count,
414 loff_t * offset)
415 {
416 int ret;
417 struct p9_fid *fid;
418 size_t size;
419
420 P9_DPRINTK(P9_DEBUG_VFS, "count %zu offset %lld\n", count, *offset);
421 fid = filp->private_data;
422
423 size = fid->iounit ? fid->iounit : fid->clnt->msize - P9_IOHDRSZ;
424 if (count > size)
425 ret = v9fs_file_readn(filp, NULL, udata, count, *offset);
426 else
427 ret = p9_client_read(fid, NULL, udata, *offset, count);
428
429 if (ret > 0)
430 *offset += ret;
431
432 return ret;
433 }
434
435 ssize_t
436 v9fs_file_write_internal(struct inode *inode, struct p9_fid *fid,
437 const char __user *data, size_t count,
438 loff_t *offset, int invalidate)
439 {
440 int n;
441 size_t total = 0;
442 struct p9_client *clnt;
443 loff_t origin = *offset;
444 unsigned long pg_start, pg_end;
445
446 P9_DPRINTK(P9_DEBUG_VFS, "data %p count %d offset %x\n", data,
447 (int)count, (int)*offset);
448
449 clnt = fid->clnt;
450 do {
451 n = p9_client_write(fid, NULL, data+total, origin+total, count);
452 if (n <= 0)
453 break;
454 count -= n;
455 total += n;
456 } while (count > 0);
457
458 if (invalidate && (total > 0)) {
459 pg_start = origin >> PAGE_CACHE_SHIFT;
460 pg_end = (origin + total - 1) >> PAGE_CACHE_SHIFT;
461 if (inode->i_mapping && inode->i_mapping->nrpages)
462 invalidate_inode_pages2_range(inode->i_mapping,
463 pg_start, pg_end);
464 *offset += total;
465 i_size_write(inode, i_size_read(inode) + total);
466 inode->i_blocks = (i_size_read(inode) + 512 - 1) >> 9;
467 }
468 if (n < 0)
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 */
482 static ssize_t
483 v9fs_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);
504 out:
505 return retval;
506 }
507
508
509 static int v9fs_file_fsync(struct file *filp, int datasync)
510 {
511 struct p9_fid *fid;
512 struct p9_wstat wstat;
513 int retval;
514
515 P9_DPRINTK(P9_DEBUG_VFS, "filp %p datasync %x\n", filp, datasync);
516
517 fid = filp->private_data;
518 v9fs_blank_wstat(&wstat);
519
520 retval = p9_client_wstat(fid, &wstat);
521 return retval;
522 }
523
524 int v9fs_file_fsync_dotl(struct file *filp, int datasync)
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
534 retval = p9_client_fsync(fid, datasync);
535 return retval;
536 }
537
538 static int
539 v9fs_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
550 static int
551 v9fs_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;
569 out_unlock:
570 unlock_page(page);
571 return VM_FAULT_NOPAGE;
572 }
573
574 static const struct vm_operations_struct v9fs_file_vm_ops = {
575 .fault = filemap_fault,
576 .page_mkwrite = v9fs_vm_page_mkwrite,
577 };
578
579 const struct file_operations v9fs_cached_file_operations = {
580 .llseek = generic_file_llseek,
581 .read = do_sync_read,
582 .write = do_sync_write,
583 .aio_read = generic_file_aio_read,
584 .aio_write = generic_file_aio_write,
585 .open = v9fs_file_open,
586 .release = v9fs_dir_release,
587 .lock = v9fs_file_lock,
588 .mmap = v9fs_file_mmap,
589 .fsync = v9fs_file_fsync,
590 };
591
592 const struct file_operations v9fs_cached_file_operations_dotl = {
593 .llseek = generic_file_llseek,
594 .read = do_sync_read,
595 .write = do_sync_write,
596 .aio_read = generic_file_aio_read,
597 .aio_write = generic_file_aio_write,
598 .open = v9fs_file_open,
599 .release = v9fs_dir_release,
600 .lock = v9fs_file_lock_dotl,
601 .flock = v9fs_file_flock_dotl,
602 .mmap = v9fs_file_mmap,
603 .fsync = v9fs_file_fsync_dotl,
604 };
605
606 const struct file_operations v9fs_file_operations = {
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,
613 .mmap = generic_file_readonly_mmap,
614 .fsync = v9fs_file_fsync,
615 };
616
617 const 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,
623 .lock = v9fs_file_lock_dotl,
624 .flock = v9fs_file_flock_dotl,
625 .mmap = generic_file_readonly_mmap,
626 .fsync = v9fs_file_fsync_dotl,
627 };