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