1 // SPDX-License-Identifier: GPL-2.0-only
3 * This file contians vfs file ops for 9P2000.
5 * Copyright (C) 2004 by Eric Van Hensbergen <ericvh@gmail.com>
6 * Copyright (C) 2002 by Ron Minnich <rminnich@lanl.gov>
9 #include <linux/module.h>
10 #include <linux/errno.h>
12 #include <linux/sched.h>
13 #include <linux/file.h>
14 #include <linux/stat.h>
15 #include <linux/string.h>
16 #include <linux/inet.h>
17 #include <linux/list.h>
18 #include <linux/pagemap.h>
19 #include <linux/utsname.h>
20 #include <linux/uaccess.h>
21 #include <linux/idr.h>
22 #include <linux/uio.h>
23 #include <linux/slab.h>
24 #include <net/9p/9p.h>
25 #include <net/9p/client.h>
32 static const struct vm_operations_struct v9fs_file_vm_ops;
33 static const struct vm_operations_struct v9fs_mmap_file_vm_ops;
36 * v9fs_file_open - open a file (or directory)
37 * @inode: inode to be opened
38 * @file: file being opened
42 int v9fs_file_open(struct inode *inode, struct file *file)
45 struct v9fs_inode *v9inode;
46 struct v9fs_session_info *v9ses;
47 struct p9_fid *fid, *writeback_fid;
50 p9_debug(P9_DEBUG_VFS, "inode: %p file: %p\n", inode, file);
51 v9inode = V9FS_I(inode);
52 v9ses = v9fs_inode2v9ses(inode);
53 if (v9fs_proto_dotl(v9ses))
54 omode = v9fs_open_to_dotl_flags(file->f_flags);
56 omode = v9fs_uflags2omode(file->f_flags,
57 v9fs_proto_dotu(v9ses));
58 fid = file->private_data;
60 fid = v9fs_fid_clone(file_dentry(file));
64 err = p9_client_open(fid, omode);
69 if ((file->f_flags & O_APPEND) &&
70 (!v9fs_proto_dotu(v9ses) && !v9fs_proto_dotl(v9ses)))
71 generic_file_llseek(file, 0, SEEK_END);
73 file->private_data = fid;
76 mutex_lock(&v9inode->v_mutex);
77 if ((v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE) &&
78 !v9inode->writeback_fid &&
79 ((file->f_flags & O_ACCMODE) != O_RDONLY)) {
81 * clone a fid and add it to writeback_fid
82 * we do it during open time instead of
83 * page dirty time via write_begin/page_mkwrite
84 * because we want write after unlink usecase
87 writeback_fid = v9fs_writeback_fid(file_dentry(file));
88 if (IS_ERR(writeback_fid)) {
89 err = PTR_ERR(writeback_fid);
90 mutex_unlock(&v9inode->v_mutex);
93 v9inode->writeback_fid = (void *) writeback_fid;
95 mutex_unlock(&v9inode->v_mutex);
96 if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE)
97 fscache_use_cookie(v9fs_inode_cookie(v9inode),
98 file->f_mode & FMODE_WRITE);
99 v9fs_open_fid_add(inode, &fid);
102 p9_fid_put(file->private_data);
103 file->private_data = NULL;
108 * v9fs_file_lock - lock a file (or directory)
109 * @filp: file to be locked
111 * @fl: file lock structure
113 * Bugs: this looks like a local only lock, we should extend into 9P
114 * by using open exclusive
117 static int v9fs_file_lock(struct file *filp, int cmd, struct file_lock *fl)
119 struct inode *inode = file_inode(filp);
121 p9_debug(P9_DEBUG_VFS, "filp: %p lock: %p\n", filp, fl);
123 if ((IS_SETLK(cmd) || IS_SETLKW(cmd)) && fl->fl_type != F_UNLCK) {
124 filemap_write_and_wait(inode->i_mapping);
125 invalidate_mapping_pages(&inode->i_data, 0, -1);
131 static int v9fs_file_do_lock(struct file *filp, int cmd, struct file_lock *fl)
133 struct p9_flock flock;
135 uint8_t status = P9_LOCK_ERROR;
137 unsigned char fl_type;
138 struct v9fs_session_info *v9ses;
140 fid = filp->private_data;
143 BUG_ON((fl->fl_flags & FL_POSIX) != FL_POSIX);
145 res = locks_lock_file_wait(filp, fl);
149 /* convert posix lock to p9 tlock args */
150 memset(&flock, 0, sizeof(flock));
151 /* map the lock type */
152 switch (fl->fl_type) {
154 flock.type = P9_LOCK_TYPE_RDLCK;
157 flock.type = P9_LOCK_TYPE_WRLCK;
160 flock.type = P9_LOCK_TYPE_UNLCK;
163 flock.start = fl->fl_start;
164 if (fl->fl_end == OFFSET_MAX)
167 flock.length = fl->fl_end - fl->fl_start + 1;
168 flock.proc_id = fl->fl_pid;
169 flock.client_id = fid->clnt->name;
171 flock.flags = P9_LOCK_FLAGS_BLOCK;
173 v9ses = v9fs_inode2v9ses(file_inode(filp));
176 * if its a blocked request and we get P9_LOCK_BLOCKED as the status
177 * for lock request, keep on trying
180 res = p9_client_lock_dotl(fid, &flock, &status);
184 if (status != P9_LOCK_BLOCKED)
186 if (status == P9_LOCK_BLOCKED && !IS_SETLKW(cmd))
188 if (schedule_timeout_interruptible(v9ses->session_lock_timeout)
192 * p9_client_lock_dotl overwrites flock.client_id with the
193 * server message, free and reuse the client name
195 if (flock.client_id != fid->clnt->name) {
196 kfree(flock.client_id);
197 flock.client_id = fid->clnt->name;
201 /* map 9p status to VFS status */
203 case P9_LOCK_SUCCESS:
206 case P9_LOCK_BLOCKED:
210 WARN_ONCE(1, "unknown lock status code: %d\n", status);
220 * incase server returned error for lock request, revert
223 if (res < 0 && fl->fl_type != F_UNLCK) {
224 fl_type = fl->fl_type;
225 fl->fl_type = F_UNLCK;
226 /* Even if this fails we want to return the remote error */
227 locks_lock_file_wait(filp, fl);
228 fl->fl_type = fl_type;
230 if (flock.client_id != fid->clnt->name)
231 kfree(flock.client_id);
236 static int v9fs_file_getlock(struct file *filp, struct file_lock *fl)
238 struct p9_getlock glock;
242 fid = filp->private_data;
245 posix_test_lock(filp, fl);
247 * if we have a conflicting lock locally, no need to validate
250 if (fl->fl_type != F_UNLCK)
253 /* convert posix lock to p9 tgetlock args */
254 memset(&glock, 0, sizeof(glock));
255 glock.type = P9_LOCK_TYPE_UNLCK;
256 glock.start = fl->fl_start;
257 if (fl->fl_end == OFFSET_MAX)
260 glock.length = fl->fl_end - fl->fl_start + 1;
261 glock.proc_id = fl->fl_pid;
262 glock.client_id = fid->clnt->name;
264 res = p9_client_getlock_dotl(fid, &glock);
267 /* map 9p lock type to os lock type */
268 switch (glock.type) {
269 case P9_LOCK_TYPE_RDLCK:
270 fl->fl_type = F_RDLCK;
272 case P9_LOCK_TYPE_WRLCK:
273 fl->fl_type = F_WRLCK;
275 case P9_LOCK_TYPE_UNLCK:
276 fl->fl_type = F_UNLCK;
279 if (glock.type != P9_LOCK_TYPE_UNLCK) {
280 fl->fl_start = glock.start;
281 if (glock.length == 0)
282 fl->fl_end = OFFSET_MAX;
284 fl->fl_end = glock.start + glock.length - 1;
285 fl->fl_pid = -glock.proc_id;
288 if (glock.client_id != fid->clnt->name)
289 kfree(glock.client_id);
294 * v9fs_file_lock_dotl - lock a file (or directory)
295 * @filp: file to be locked
297 * @fl: file lock structure
301 static int v9fs_file_lock_dotl(struct file *filp, int cmd, struct file_lock *fl)
303 struct inode *inode = file_inode(filp);
306 p9_debug(P9_DEBUG_VFS, "filp: %p cmd:%d lock: %p name: %pD\n",
307 filp, cmd, fl, filp);
309 if ((IS_SETLK(cmd) || IS_SETLKW(cmd)) && fl->fl_type != F_UNLCK) {
310 filemap_write_and_wait(inode->i_mapping);
311 invalidate_mapping_pages(&inode->i_data, 0, -1);
314 if (IS_SETLK(cmd) || IS_SETLKW(cmd))
315 ret = v9fs_file_do_lock(filp, cmd, fl);
316 else if (IS_GETLK(cmd))
317 ret = v9fs_file_getlock(filp, fl);
324 * v9fs_file_flock_dotl - lock a file
325 * @filp: file to be locked
327 * @fl: file lock structure
331 static int v9fs_file_flock_dotl(struct file *filp, int cmd,
332 struct file_lock *fl)
334 struct inode *inode = file_inode(filp);
337 p9_debug(P9_DEBUG_VFS, "filp: %p cmd:%d lock: %p name: %pD\n",
338 filp, cmd, fl, filp);
340 if (!(fl->fl_flags & FL_FLOCK))
343 if ((IS_SETLK(cmd) || IS_SETLKW(cmd)) && fl->fl_type != F_UNLCK) {
344 filemap_write_and_wait(inode->i_mapping);
345 invalidate_mapping_pages(&inode->i_data, 0, -1);
347 /* Convert flock to posix lock */
348 fl->fl_flags |= FL_POSIX;
349 fl->fl_flags ^= FL_FLOCK;
351 if (IS_SETLK(cmd) | IS_SETLKW(cmd))
352 ret = v9fs_file_do_lock(filp, cmd, fl);
360 * v9fs_file_read_iter - read from a file
361 * @iocb: The operation parameters
362 * @to: The buffer to read into
366 v9fs_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
368 struct p9_fid *fid = iocb->ki_filp->private_data;
371 p9_debug(P9_DEBUG_VFS, "count %zu offset %lld\n",
372 iov_iter_count(to), iocb->ki_pos);
374 if (iocb->ki_filp->f_flags & O_NONBLOCK)
375 ret = p9_client_read_once(fid, iocb->ki_pos, to, &err);
377 ret = p9_client_read(fid, iocb->ki_pos, to, &err);
386 * v9fs_file_write_iter - write to a file
387 * @iocb: The operation parameters
388 * @from: The data to write
392 v9fs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
394 struct file *file = iocb->ki_filp;
399 retval = generic_write_checks(iocb, from);
403 origin = iocb->ki_pos;
404 retval = p9_client_write(file->private_data, iocb->ki_pos, from, &err);
406 struct inode *inode = file_inode(file);
408 unsigned long pg_start, pg_end;
410 pg_start = origin >> PAGE_SHIFT;
411 pg_end = (origin + retval - 1) >> PAGE_SHIFT;
412 if (inode->i_mapping && inode->i_mapping->nrpages)
413 invalidate_inode_pages2_range(inode->i_mapping,
415 iocb->ki_pos += retval;
416 i_size = i_size_read(inode);
417 if (iocb->ki_pos > i_size) {
418 inode_add_bytes(inode, iocb->ki_pos - i_size);
420 * Need to serialize against i_size_write() in
423 v9fs_i_size_write(inode, iocb->ki_pos);
430 static int v9fs_file_fsync(struct file *filp, loff_t start, loff_t end,
434 struct inode *inode = filp->f_mapping->host;
435 struct p9_wstat wstat;
438 retval = file_write_and_wait_range(filp, start, end);
443 p9_debug(P9_DEBUG_VFS, "filp %p datasync %x\n", filp, datasync);
445 fid = filp->private_data;
446 v9fs_blank_wstat(&wstat);
448 retval = p9_client_wstat(fid, &wstat);
454 int v9fs_file_fsync_dotl(struct file *filp, loff_t start, loff_t end,
458 struct inode *inode = filp->f_mapping->host;
461 retval = file_write_and_wait_range(filp, start, end);
466 p9_debug(P9_DEBUG_VFS, "filp %p datasync %x\n", filp, datasync);
468 fid = filp->private_data;
470 retval = p9_client_fsync(fid, datasync);
477 v9fs_file_mmap(struct file *filp, struct vm_area_struct *vma)
482 retval = generic_file_mmap(filp, vma);
484 vma->vm_ops = &v9fs_file_vm_ops;
490 v9fs_mmap_file_mmap(struct file *filp, struct vm_area_struct *vma)
494 struct v9fs_inode *v9inode;
497 inode = file_inode(filp);
498 v9inode = V9FS_I(inode);
499 mutex_lock(&v9inode->v_mutex);
500 if (!v9inode->writeback_fid &&
501 (vma->vm_flags & VM_SHARED) &&
502 (vma->vm_flags & VM_WRITE)) {
504 * clone a fid and add it to writeback_fid
505 * we do it during mmap instead of
506 * page dirty time via write_begin/page_mkwrite
507 * because we want write after unlink usecase
510 fid = v9fs_writeback_fid(file_dentry(filp));
512 retval = PTR_ERR(fid);
513 mutex_unlock(&v9inode->v_mutex);
516 v9inode->writeback_fid = (void *) fid;
518 mutex_unlock(&v9inode->v_mutex);
520 retval = generic_file_mmap(filp, vma);
522 vma->vm_ops = &v9fs_mmap_file_vm_ops;
528 v9fs_vm_page_mkwrite(struct vm_fault *vmf)
530 struct v9fs_inode *v9inode;
531 struct folio *folio = page_folio(vmf->page);
532 struct file *filp = vmf->vma->vm_file;
533 struct inode *inode = file_inode(filp);
536 p9_debug(P9_DEBUG_VFS, "folio %p fid %lx\n",
537 folio, (unsigned long)filp->private_data);
539 v9inode = V9FS_I(inode);
541 /* Wait for the page to be written to the cache before we allow it to
542 * be modified. We then assume the entire page will need writing back.
544 #ifdef CONFIG_9P_FSCACHE
545 if (folio_test_fscache(folio) &&
546 folio_wait_fscache_killable(folio) < 0)
547 return VM_FAULT_NOPAGE;
550 /* Update file times before taking page lock */
551 file_update_time(filp);
553 BUG_ON(!v9inode->writeback_fid);
554 if (folio_lock_killable(folio) < 0)
555 return VM_FAULT_RETRY;
556 if (folio_mapping(folio) != inode->i_mapping)
558 folio_wait_stable(folio);
560 return VM_FAULT_LOCKED;
563 return VM_FAULT_NOPAGE;
567 * v9fs_mmap_file_read_iter - read from a file
568 * @iocb: The operation parameters
569 * @to: The buffer to read into
573 v9fs_mmap_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
575 /* TODO: Check if there are dirty pages */
576 return v9fs_file_read_iter(iocb, to);
580 * v9fs_mmap_file_write_iter - write to a file
581 * @iocb: The operation parameters
582 * @from: The data to write
586 v9fs_mmap_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
589 * TODO: invalidate mmaps on filp's inode between
590 * offset and offset+count
592 return v9fs_file_write_iter(iocb, from);
595 static void v9fs_mmap_vm_close(struct vm_area_struct *vma)
599 struct writeback_control wbc = {
600 .nr_to_write = LONG_MAX,
601 .sync_mode = WB_SYNC_ALL,
602 .range_start = (loff_t)vma->vm_pgoff * PAGE_SIZE,
603 /* absolute end, byte at end included */
604 .range_end = (loff_t)vma->vm_pgoff * PAGE_SIZE +
605 (vma->vm_end - vma->vm_start - 1),
608 if (!(vma->vm_flags & VM_SHARED))
611 p9_debug(P9_DEBUG_VFS, "9p VMA close, %p, flushing", vma);
613 inode = file_inode(vma->vm_file);
614 filemap_fdatawrite_wbc(inode->i_mapping, &wbc);
618 static const struct vm_operations_struct v9fs_file_vm_ops = {
619 .fault = filemap_fault,
620 .map_pages = filemap_map_pages,
621 .page_mkwrite = v9fs_vm_page_mkwrite,
624 static const struct vm_operations_struct v9fs_mmap_file_vm_ops = {
625 .close = v9fs_mmap_vm_close,
626 .fault = filemap_fault,
627 .map_pages = filemap_map_pages,
628 .page_mkwrite = v9fs_vm_page_mkwrite,
632 const struct file_operations v9fs_cached_file_operations = {
633 .llseek = generic_file_llseek,
634 .read_iter = generic_file_read_iter,
635 .write_iter = generic_file_write_iter,
636 .open = v9fs_file_open,
637 .release = v9fs_dir_release,
638 .lock = v9fs_file_lock,
639 .mmap = v9fs_file_mmap,
640 .splice_read = generic_file_splice_read,
641 .splice_write = iter_file_splice_write,
642 .fsync = v9fs_file_fsync,
645 const struct file_operations v9fs_cached_file_operations_dotl = {
646 .llseek = generic_file_llseek,
647 .read_iter = generic_file_read_iter,
648 .write_iter = generic_file_write_iter,
649 .open = v9fs_file_open,
650 .release = v9fs_dir_release,
651 .lock = v9fs_file_lock_dotl,
652 .flock = v9fs_file_flock_dotl,
653 .mmap = v9fs_file_mmap,
654 .splice_read = generic_file_splice_read,
655 .splice_write = iter_file_splice_write,
656 .fsync = v9fs_file_fsync_dotl,
659 const struct file_operations v9fs_file_operations = {
660 .llseek = generic_file_llseek,
661 .read_iter = v9fs_file_read_iter,
662 .write_iter = v9fs_file_write_iter,
663 .open = v9fs_file_open,
664 .release = v9fs_dir_release,
665 .lock = v9fs_file_lock,
666 .mmap = generic_file_readonly_mmap,
667 .splice_read = generic_file_splice_read,
668 .splice_write = iter_file_splice_write,
669 .fsync = v9fs_file_fsync,
672 const struct file_operations v9fs_file_operations_dotl = {
673 .llseek = generic_file_llseek,
674 .read_iter = v9fs_file_read_iter,
675 .write_iter = v9fs_file_write_iter,
676 .open = v9fs_file_open,
677 .release = v9fs_dir_release,
678 .lock = v9fs_file_lock_dotl,
679 .flock = v9fs_file_flock_dotl,
680 .mmap = generic_file_readonly_mmap,
681 .splice_read = generic_file_splice_read,
682 .splice_write = iter_file_splice_write,
683 .fsync = v9fs_file_fsync_dotl,
686 const struct file_operations v9fs_mmap_file_operations = {
687 .llseek = generic_file_llseek,
688 .read_iter = v9fs_mmap_file_read_iter,
689 .write_iter = v9fs_mmap_file_write_iter,
690 .open = v9fs_file_open,
691 .release = v9fs_dir_release,
692 .lock = v9fs_file_lock,
693 .mmap = v9fs_mmap_file_mmap,
694 .splice_read = generic_file_splice_read,
695 .splice_write = iter_file_splice_write,
696 .fsync = v9fs_file_fsync,
699 const struct file_operations v9fs_mmap_file_operations_dotl = {
700 .llseek = generic_file_llseek,
701 .read_iter = v9fs_mmap_file_read_iter,
702 .write_iter = v9fs_mmap_file_write_iter,
703 .open = v9fs_file_open,
704 .release = v9fs_dir_release,
705 .lock = v9fs_file_lock_dotl,
706 .flock = v9fs_file_flock_dotl,
707 .mmap = v9fs_mmap_file_mmap,
708 .splice_read = generic_file_splice_read,
709 .splice_write = iter_file_splice_write,
710 .fsync = v9fs_file_fsync_dotl,