ext4: Optimize ext4 DIO overwrites
[linux/fpc-iii.git] / fs / 9p / vfs_inode.c
blobb82423a72f685bfb429d1b73a0a8dd382c78b2bf
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * linux/fs/9p/vfs_inode.c
5 * This file contains vfs inode ops for the 9P2000 protocol.
7 * Copyright (C) 2004 by Eric Van Hensbergen <ericvh@gmail.com>
8 * Copyright (C) 2002 by Ron Minnich <rminnich@lanl.gov>
9 */
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13 #include <linux/module.h>
14 #include <linux/errno.h>
15 #include <linux/fs.h>
16 #include <linux/file.h>
17 #include <linux/pagemap.h>
18 #include <linux/stat.h>
19 #include <linux/string.h>
20 #include <linux/inet.h>
21 #include <linux/namei.h>
22 #include <linux/idr.h>
23 #include <linux/sched.h>
24 #include <linux/slab.h>
25 #include <linux/xattr.h>
26 #include <linux/posix_acl.h>
27 #include <net/9p/9p.h>
28 #include <net/9p/client.h>
30 #include "v9fs.h"
31 #include "v9fs_vfs.h"
32 #include "fid.h"
33 #include "cache.h"
34 #include "xattr.h"
35 #include "acl.h"
37 static const struct inode_operations v9fs_dir_inode_operations;
38 static const struct inode_operations v9fs_dir_inode_operations_dotu;
39 static const struct inode_operations v9fs_file_inode_operations;
40 static const struct inode_operations v9fs_symlink_inode_operations;
42 /**
43 * unixmode2p9mode - convert unix mode bits to plan 9
44 * @v9ses: v9fs session information
45 * @mode: mode to convert
49 static u32 unixmode2p9mode(struct v9fs_session_info *v9ses, umode_t mode)
51 int res;
52 res = mode & 0777;
53 if (S_ISDIR(mode))
54 res |= P9_DMDIR;
55 if (v9fs_proto_dotu(v9ses)) {
56 if (v9ses->nodev == 0) {
57 if (S_ISSOCK(mode))
58 res |= P9_DMSOCKET;
59 if (S_ISFIFO(mode))
60 res |= P9_DMNAMEDPIPE;
61 if (S_ISBLK(mode))
62 res |= P9_DMDEVICE;
63 if (S_ISCHR(mode))
64 res |= P9_DMDEVICE;
67 if ((mode & S_ISUID) == S_ISUID)
68 res |= P9_DMSETUID;
69 if ((mode & S_ISGID) == S_ISGID)
70 res |= P9_DMSETGID;
71 if ((mode & S_ISVTX) == S_ISVTX)
72 res |= P9_DMSETVTX;
74 return res;
77 /**
78 * p9mode2perm- convert plan9 mode bits to unix permission bits
79 * @v9ses: v9fs session information
80 * @stat: p9_wstat from which mode need to be derived
83 static int p9mode2perm(struct v9fs_session_info *v9ses,
84 struct p9_wstat *stat)
86 int res;
87 int mode = stat->mode;
89 res = mode & S_IALLUGO;
90 if (v9fs_proto_dotu(v9ses)) {
91 if ((mode & P9_DMSETUID) == P9_DMSETUID)
92 res |= S_ISUID;
94 if ((mode & P9_DMSETGID) == P9_DMSETGID)
95 res |= S_ISGID;
97 if ((mode & P9_DMSETVTX) == P9_DMSETVTX)
98 res |= S_ISVTX;
100 return res;
104 * p9mode2unixmode- convert plan9 mode bits to unix mode bits
105 * @v9ses: v9fs session information
106 * @stat: p9_wstat from which mode need to be derived
107 * @rdev: major number, minor number in case of device files.
110 static umode_t p9mode2unixmode(struct v9fs_session_info *v9ses,
111 struct p9_wstat *stat, dev_t *rdev)
113 int res;
114 u32 mode = stat->mode;
116 *rdev = 0;
117 res = p9mode2perm(v9ses, stat);
119 if ((mode & P9_DMDIR) == P9_DMDIR)
120 res |= S_IFDIR;
121 else if ((mode & P9_DMSYMLINK) && (v9fs_proto_dotu(v9ses)))
122 res |= S_IFLNK;
123 else if ((mode & P9_DMSOCKET) && (v9fs_proto_dotu(v9ses))
124 && (v9ses->nodev == 0))
125 res |= S_IFSOCK;
126 else if ((mode & P9_DMNAMEDPIPE) && (v9fs_proto_dotu(v9ses))
127 && (v9ses->nodev == 0))
128 res |= S_IFIFO;
129 else if ((mode & P9_DMDEVICE) && (v9fs_proto_dotu(v9ses))
130 && (v9ses->nodev == 0)) {
131 char type = 0, ext[32];
132 int major = -1, minor = -1;
134 strlcpy(ext, stat->extension, sizeof(ext));
135 sscanf(ext, "%c %i %i", &type, &major, &minor);
136 switch (type) {
137 case 'c':
138 res |= S_IFCHR;
139 break;
140 case 'b':
141 res |= S_IFBLK;
142 break;
143 default:
144 p9_debug(P9_DEBUG_ERROR, "Unknown special type %c %s\n",
145 type, stat->extension);
147 *rdev = MKDEV(major, minor);
148 } else
149 res |= S_IFREG;
151 return res;
155 * v9fs_uflags2omode- convert posix open flags to plan 9 mode bits
156 * @uflags: flags to convert
157 * @extended: if .u extensions are active
160 int v9fs_uflags2omode(int uflags, int extended)
162 int ret;
164 ret = 0;
165 switch (uflags&3) {
166 default:
167 case O_RDONLY:
168 ret = P9_OREAD;
169 break;
171 case O_WRONLY:
172 ret = P9_OWRITE;
173 break;
175 case O_RDWR:
176 ret = P9_ORDWR;
177 break;
180 if (extended) {
181 if (uflags & O_EXCL)
182 ret |= P9_OEXCL;
184 if (uflags & O_APPEND)
185 ret |= P9_OAPPEND;
188 return ret;
192 * v9fs_blank_wstat - helper function to setup a 9P stat structure
193 * @wstat: structure to initialize
197 void
198 v9fs_blank_wstat(struct p9_wstat *wstat)
200 wstat->type = ~0;
201 wstat->dev = ~0;
202 wstat->qid.type = ~0;
203 wstat->qid.version = ~0;
204 *((long long *)&wstat->qid.path) = ~0;
205 wstat->mode = ~0;
206 wstat->atime = ~0;
207 wstat->mtime = ~0;
208 wstat->length = ~0;
209 wstat->name = NULL;
210 wstat->uid = NULL;
211 wstat->gid = NULL;
212 wstat->muid = NULL;
213 wstat->n_uid = INVALID_UID;
214 wstat->n_gid = INVALID_GID;
215 wstat->n_muid = INVALID_UID;
216 wstat->extension = NULL;
220 * v9fs_alloc_inode - helper function to allocate an inode
223 struct inode *v9fs_alloc_inode(struct super_block *sb)
225 struct v9fs_inode *v9inode;
226 v9inode = (struct v9fs_inode *)kmem_cache_alloc(v9fs_inode_cache,
227 GFP_KERNEL);
228 if (!v9inode)
229 return NULL;
230 #ifdef CONFIG_9P_FSCACHE
231 v9inode->fscache = NULL;
232 mutex_init(&v9inode->fscache_lock);
233 #endif
234 v9inode->writeback_fid = NULL;
235 v9inode->cache_validity = 0;
236 mutex_init(&v9inode->v_mutex);
237 return &v9inode->vfs_inode;
241 * v9fs_free_inode - destroy an inode
245 void v9fs_free_inode(struct inode *inode)
247 kmem_cache_free(v9fs_inode_cache, V9FS_I(inode));
250 int v9fs_init_inode(struct v9fs_session_info *v9ses,
251 struct inode *inode, umode_t mode, dev_t rdev)
253 int err = 0;
255 inode_init_owner(inode, NULL, mode);
256 inode->i_blocks = 0;
257 inode->i_rdev = rdev;
258 inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
259 inode->i_mapping->a_ops = &v9fs_addr_operations;
261 switch (mode & S_IFMT) {
262 case S_IFIFO:
263 case S_IFBLK:
264 case S_IFCHR:
265 case S_IFSOCK:
266 if (v9fs_proto_dotl(v9ses)) {
267 inode->i_op = &v9fs_file_inode_operations_dotl;
268 } else if (v9fs_proto_dotu(v9ses)) {
269 inode->i_op = &v9fs_file_inode_operations;
270 } else {
271 p9_debug(P9_DEBUG_ERROR,
272 "special files without extended mode\n");
273 err = -EINVAL;
274 goto error;
276 init_special_inode(inode, inode->i_mode, inode->i_rdev);
277 break;
278 case S_IFREG:
279 if (v9fs_proto_dotl(v9ses)) {
280 inode->i_op = &v9fs_file_inode_operations_dotl;
281 if (v9ses->cache == CACHE_LOOSE ||
282 v9ses->cache == CACHE_FSCACHE)
283 inode->i_fop =
284 &v9fs_cached_file_operations_dotl;
285 else if (v9ses->cache == CACHE_MMAP)
286 inode->i_fop = &v9fs_mmap_file_operations_dotl;
287 else
288 inode->i_fop = &v9fs_file_operations_dotl;
289 } else {
290 inode->i_op = &v9fs_file_inode_operations;
291 if (v9ses->cache == CACHE_LOOSE ||
292 v9ses->cache == CACHE_FSCACHE)
293 inode->i_fop =
294 &v9fs_cached_file_operations;
295 else if (v9ses->cache == CACHE_MMAP)
296 inode->i_fop = &v9fs_mmap_file_operations;
297 else
298 inode->i_fop = &v9fs_file_operations;
301 break;
302 case S_IFLNK:
303 if (!v9fs_proto_dotu(v9ses) && !v9fs_proto_dotl(v9ses)) {
304 p9_debug(P9_DEBUG_ERROR,
305 "extended modes used with legacy protocol\n");
306 err = -EINVAL;
307 goto error;
310 if (v9fs_proto_dotl(v9ses))
311 inode->i_op = &v9fs_symlink_inode_operations_dotl;
312 else
313 inode->i_op = &v9fs_symlink_inode_operations;
315 break;
316 case S_IFDIR:
317 inc_nlink(inode);
318 if (v9fs_proto_dotl(v9ses))
319 inode->i_op = &v9fs_dir_inode_operations_dotl;
320 else if (v9fs_proto_dotu(v9ses))
321 inode->i_op = &v9fs_dir_inode_operations_dotu;
322 else
323 inode->i_op = &v9fs_dir_inode_operations;
325 if (v9fs_proto_dotl(v9ses))
326 inode->i_fop = &v9fs_dir_operations_dotl;
327 else
328 inode->i_fop = &v9fs_dir_operations;
330 break;
331 default:
332 p9_debug(P9_DEBUG_ERROR, "BAD mode 0x%hx S_IFMT 0x%x\n",
333 mode, mode & S_IFMT);
334 err = -EINVAL;
335 goto error;
337 error:
338 return err;
343 * v9fs_get_inode - helper function to setup an inode
344 * @sb: superblock
345 * @mode: mode to setup inode with
349 struct inode *v9fs_get_inode(struct super_block *sb, umode_t mode, dev_t rdev)
351 int err;
352 struct inode *inode;
353 struct v9fs_session_info *v9ses = sb->s_fs_info;
355 p9_debug(P9_DEBUG_VFS, "super block: %p mode: %ho\n", sb, mode);
357 inode = new_inode(sb);
358 if (!inode) {
359 pr_warn("%s (%d): Problem allocating inode\n",
360 __func__, task_pid_nr(current));
361 return ERR_PTR(-ENOMEM);
363 err = v9fs_init_inode(v9ses, inode, mode, rdev);
364 if (err) {
365 iput(inode);
366 return ERR_PTR(err);
368 return inode;
372 static struct v9fs_fid*
373 v9fs_clone_walk(struct v9fs_session_info *v9ses, u32 fid, struct dentry *dentry)
375 int err;
376 int nfid;
377 struct v9fs_fid *ret;
378 struct v9fs_fcall *fcall;
380 nfid = v9fs_get_idpool(&v9ses->fidpool);
381 if (nfid < 0) {
382 eprintk(KERN_WARNING, "no free fids available\n");
383 return ERR_PTR(-ENOSPC);
386 err = v9fs_t_walk(v9ses, fid, nfid, (char *) dentry->d_name.name,
387 &fcall);
389 if (err < 0) {
390 if (fcall && fcall->id == RWALK)
391 goto clunk_fid;
393 PRINT_FCALL_ERROR("walk error", fcall);
394 v9fs_put_idpool(nfid, &v9ses->fidpool);
395 goto error;
398 kfree(fcall);
399 fcall = NULL;
400 ret = v9fs_fid_create(v9ses, nfid);
401 if (!ret) {
402 err = -ENOMEM;
403 goto clunk_fid;
406 err = v9fs_fid_insert(ret, dentry);
407 if (err < 0) {
408 v9fs_fid_destroy(ret);
409 goto clunk_fid;
412 return ret;
414 clunk_fid:
415 v9fs_t_clunk(v9ses, nfid);
417 error:
418 kfree(fcall);
419 return ERR_PTR(err);
425 * v9fs_clear_inode - release an inode
426 * @inode: inode to release
429 void v9fs_evict_inode(struct inode *inode)
431 struct v9fs_inode *v9inode = V9FS_I(inode);
433 truncate_inode_pages_final(&inode->i_data);
434 clear_inode(inode);
435 filemap_fdatawrite(&inode->i_data);
437 v9fs_cache_inode_put_cookie(inode);
438 /* clunk the fid stashed in writeback_fid */
439 if (v9inode->writeback_fid) {
440 p9_client_clunk(v9inode->writeback_fid);
441 v9inode->writeback_fid = NULL;
445 static int v9fs_test_inode(struct inode *inode, void *data)
447 int umode;
448 dev_t rdev;
449 struct v9fs_inode *v9inode = V9FS_I(inode);
450 struct p9_wstat *st = (struct p9_wstat *)data;
451 struct v9fs_session_info *v9ses = v9fs_inode2v9ses(inode);
453 umode = p9mode2unixmode(v9ses, st, &rdev);
454 /* don't match inode of different type */
455 if ((inode->i_mode & S_IFMT) != (umode & S_IFMT))
456 return 0;
458 /* compare qid details */
459 if (memcmp(&v9inode->qid.version,
460 &st->qid.version, sizeof(v9inode->qid.version)))
461 return 0;
463 if (v9inode->qid.type != st->qid.type)
464 return 0;
466 if (v9inode->qid.path != st->qid.path)
467 return 0;
468 return 1;
471 static int v9fs_test_new_inode(struct inode *inode, void *data)
473 return 0;
476 static int v9fs_set_inode(struct inode *inode, void *data)
478 struct v9fs_inode *v9inode = V9FS_I(inode);
479 struct p9_wstat *st = (struct p9_wstat *)data;
481 memcpy(&v9inode->qid, &st->qid, sizeof(st->qid));
482 return 0;
485 static struct inode *v9fs_qid_iget(struct super_block *sb,
486 struct p9_qid *qid,
487 struct p9_wstat *st,
488 int new)
490 dev_t rdev;
491 int retval;
492 umode_t umode;
493 unsigned long i_ino;
494 struct inode *inode;
495 struct v9fs_session_info *v9ses = sb->s_fs_info;
496 int (*test)(struct inode *, void *);
498 if (new)
499 test = v9fs_test_new_inode;
500 else
501 test = v9fs_test_inode;
503 i_ino = v9fs_qid2ino(qid);
504 inode = iget5_locked(sb, i_ino, test, v9fs_set_inode, st);
505 if (!inode)
506 return ERR_PTR(-ENOMEM);
507 if (!(inode->i_state & I_NEW))
508 return inode;
510 * initialize the inode with the stat info
511 * FIXME!! we may need support for stale inodes
512 * later.
514 inode->i_ino = i_ino;
515 umode = p9mode2unixmode(v9ses, st, &rdev);
516 retval = v9fs_init_inode(v9ses, inode, umode, rdev);
517 if (retval)
518 goto error;
520 v9fs_stat2inode(st, inode, sb, 0);
521 v9fs_cache_inode_get_cookie(inode);
522 unlock_new_inode(inode);
523 return inode;
524 error:
525 iget_failed(inode);
526 return ERR_PTR(retval);
530 struct inode *
531 v9fs_inode_from_fid(struct v9fs_session_info *v9ses, struct p9_fid *fid,
532 struct super_block *sb, int new)
534 struct p9_wstat *st;
535 struct inode *inode = NULL;
537 st = p9_client_stat(fid);
538 if (IS_ERR(st))
539 return ERR_CAST(st);
541 inode = v9fs_qid_iget(sb, &st->qid, st, new);
542 p9stat_free(st);
543 kfree(st);
544 return inode;
548 * v9fs_at_to_dotl_flags- convert Linux specific AT flags to
549 * plan 9 AT flag.
550 * @flags: flags to convert
552 static int v9fs_at_to_dotl_flags(int flags)
554 int rflags = 0;
555 if (flags & AT_REMOVEDIR)
556 rflags |= P9_DOTL_AT_REMOVEDIR;
557 return rflags;
561 * v9fs_dec_count - helper functon to drop i_nlink.
563 * If a directory had nlink <= 2 (including . and ..), then we should not drop
564 * the link count, which indicates the underlying exported fs doesn't maintain
565 * nlink accurately. e.g.
566 * - overlayfs sets nlink to 1 for merged dir
567 * - ext4 (with dir_nlink feature enabled) sets nlink to 1 if a dir has more
568 * than EXT4_LINK_MAX (65000) links.
570 * @inode: inode whose nlink is being dropped
572 static void v9fs_dec_count(struct inode *inode)
574 if (!S_ISDIR(inode->i_mode) || inode->i_nlink > 2)
575 drop_nlink(inode);
579 * v9fs_remove - helper function to remove files and directories
580 * @dir: directory inode that is being deleted
581 * @dentry: dentry that is being deleted
582 * @flags: removing a directory
586 static int v9fs_remove(struct inode *dir, struct dentry *dentry, int flags)
588 struct inode *inode;
589 int retval = -EOPNOTSUPP;
590 struct p9_fid *v9fid, *dfid;
591 struct v9fs_session_info *v9ses;
593 p9_debug(P9_DEBUG_VFS, "inode: %p dentry: %p rmdir: %x\n",
594 dir, dentry, flags);
596 v9ses = v9fs_inode2v9ses(dir);
597 inode = d_inode(dentry);
598 dfid = v9fs_parent_fid(dentry);
599 if (IS_ERR(dfid)) {
600 retval = PTR_ERR(dfid);
601 p9_debug(P9_DEBUG_VFS, "fid lookup failed %d\n", retval);
602 return retval;
604 if (v9fs_proto_dotl(v9ses))
605 retval = p9_client_unlinkat(dfid, dentry->d_name.name,
606 v9fs_at_to_dotl_flags(flags));
607 if (retval == -EOPNOTSUPP) {
608 /* Try the one based on path */
609 v9fid = v9fs_fid_clone(dentry);
610 if (IS_ERR(v9fid))
611 return PTR_ERR(v9fid);
612 retval = p9_client_remove(v9fid);
614 if (!retval) {
616 * directories on unlink should have zero
617 * link count
619 if (flags & AT_REMOVEDIR) {
620 clear_nlink(inode);
621 v9fs_dec_count(dir);
622 } else
623 v9fs_dec_count(inode);
625 v9fs_invalidate_inode_attr(inode);
626 v9fs_invalidate_inode_attr(dir);
628 return retval;
632 * v9fs_create - Create a file
633 * @v9ses: session information
634 * @dir: directory that dentry is being created in
635 * @dentry: dentry that is being created
636 * @extension: 9p2000.u extension string to support devices, etc.
637 * @perm: create permissions
638 * @mode: open mode
641 static struct p9_fid *
642 v9fs_create(struct v9fs_session_info *v9ses, struct inode *dir,
643 struct dentry *dentry, char *extension, u32 perm, u8 mode)
645 int err;
646 const unsigned char *name;
647 struct p9_fid *dfid, *ofid, *fid;
648 struct inode *inode;
650 p9_debug(P9_DEBUG_VFS, "name %pd\n", dentry);
652 err = 0;
653 ofid = NULL;
654 fid = NULL;
655 name = dentry->d_name.name;
656 dfid = v9fs_parent_fid(dentry);
657 if (IS_ERR(dfid)) {
658 err = PTR_ERR(dfid);
659 p9_debug(P9_DEBUG_VFS, "fid lookup failed %d\n", err);
660 return ERR_PTR(err);
663 /* clone a fid to use for creation */
664 ofid = clone_fid(dfid);
665 if (IS_ERR(ofid)) {
666 err = PTR_ERR(ofid);
667 p9_debug(P9_DEBUG_VFS, "p9_client_walk failed %d\n", err);
668 return ERR_PTR(err);
671 err = p9_client_fcreate(ofid, name, perm, mode, extension);
672 if (err < 0) {
673 p9_debug(P9_DEBUG_VFS, "p9_client_fcreate failed %d\n", err);
674 goto error;
677 if (!(perm & P9_DMLINK)) {
678 /* now walk from the parent so we can get unopened fid */
679 fid = p9_client_walk(dfid, 1, &name, 1);
680 if (IS_ERR(fid)) {
681 err = PTR_ERR(fid);
682 p9_debug(P9_DEBUG_VFS,
683 "p9_client_walk failed %d\n", err);
684 fid = NULL;
685 goto error;
688 * instantiate inode and assign the unopened fid to the dentry
690 inode = v9fs_get_new_inode_from_fid(v9ses, fid, dir->i_sb);
691 if (IS_ERR(inode)) {
692 err = PTR_ERR(inode);
693 p9_debug(P9_DEBUG_VFS,
694 "inode creation failed %d\n", err);
695 goto error;
697 v9fs_fid_add(dentry, fid);
698 d_instantiate(dentry, inode);
700 return ofid;
701 error:
702 if (ofid)
703 p9_client_clunk(ofid);
705 if (fid)
706 p9_client_clunk(fid);
708 return ERR_PTR(err);
712 * v9fs_vfs_create - VFS hook to create a regular file
714 * open(.., O_CREAT) is handled in v9fs_vfs_atomic_open(). This is only called
715 * for mknod(2).
717 * @dir: directory inode that is being created
718 * @dentry: dentry that is being deleted
719 * @mode: create permissions
723 static int
724 v9fs_vfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
725 bool excl)
727 struct v9fs_session_info *v9ses = v9fs_inode2v9ses(dir);
728 u32 perm = unixmode2p9mode(v9ses, mode);
729 struct p9_fid *fid;
731 /* P9_OEXCL? */
732 fid = v9fs_create(v9ses, dir, dentry, NULL, perm, P9_ORDWR);
733 if (IS_ERR(fid))
734 return PTR_ERR(fid);
736 v9fs_invalidate_inode_attr(dir);
737 p9_client_clunk(fid);
739 return 0;
743 * v9fs_vfs_mkdir - VFS mkdir hook to create a directory
744 * @dir: inode that is being unlinked
745 * @dentry: dentry that is being unlinked
746 * @mode: mode for new directory
750 static int v9fs_vfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
752 int err;
753 u32 perm;
754 struct p9_fid *fid;
755 struct v9fs_session_info *v9ses;
757 p9_debug(P9_DEBUG_VFS, "name %pd\n", dentry);
758 err = 0;
759 v9ses = v9fs_inode2v9ses(dir);
760 perm = unixmode2p9mode(v9ses, mode | S_IFDIR);
761 fid = v9fs_create(v9ses, dir, dentry, NULL, perm, P9_OREAD);
762 if (IS_ERR(fid)) {
763 err = PTR_ERR(fid);
764 fid = NULL;
765 } else {
766 inc_nlink(dir);
767 v9fs_invalidate_inode_attr(dir);
770 if (fid)
771 p9_client_clunk(fid);
773 return err;
777 * v9fs_vfs_lookup - VFS lookup hook to "walk" to a new inode
778 * @dir: inode that is being walked from
779 * @dentry: dentry that is being walked to?
780 * @flags: lookup flags (unused)
784 struct dentry *v9fs_vfs_lookup(struct inode *dir, struct dentry *dentry,
785 unsigned int flags)
787 struct dentry *res;
788 struct v9fs_session_info *v9ses;
789 struct p9_fid *dfid, *fid;
790 struct inode *inode;
791 const unsigned char *name;
793 p9_debug(P9_DEBUG_VFS, "dir: %p dentry: (%pd) %p flags: %x\n",
794 dir, dentry, dentry, flags);
796 if (dentry->d_name.len > NAME_MAX)
797 return ERR_PTR(-ENAMETOOLONG);
799 v9ses = v9fs_inode2v9ses(dir);
800 /* We can walk d_parent because we hold the dir->i_mutex */
801 dfid = v9fs_parent_fid(dentry);
802 if (IS_ERR(dfid))
803 return ERR_CAST(dfid);
806 * Make sure we don't use a wrong inode due to parallel
807 * unlink. For cached mode create calls request for new
808 * inode. But with cache disabled, lookup should do this.
810 name = dentry->d_name.name;
811 fid = p9_client_walk(dfid, 1, &name, 1);
812 if (fid == ERR_PTR(-ENOENT))
813 inode = NULL;
814 else if (IS_ERR(fid))
815 inode = ERR_CAST(fid);
816 else if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE)
817 inode = v9fs_get_inode_from_fid(v9ses, fid, dir->i_sb);
818 else
819 inode = v9fs_get_new_inode_from_fid(v9ses, fid, dir->i_sb);
821 * If we had a rename on the server and a parallel lookup
822 * for the new name, then make sure we instantiate with
823 * the new name. ie look up for a/b, while on server somebody
824 * moved b under k and client parallely did a lookup for
825 * k/b.
827 res = d_splice_alias(inode, dentry);
828 if (!IS_ERR(fid)) {
829 if (!res)
830 v9fs_fid_add(dentry, fid);
831 else if (!IS_ERR(res))
832 v9fs_fid_add(res, fid);
833 else
834 p9_client_clunk(fid);
836 return res;
839 static int
840 v9fs_vfs_atomic_open(struct inode *dir, struct dentry *dentry,
841 struct file *file, unsigned flags, umode_t mode)
843 int err;
844 u32 perm;
845 struct v9fs_inode *v9inode;
846 struct v9fs_session_info *v9ses;
847 struct p9_fid *fid, *inode_fid;
848 struct dentry *res = NULL;
850 if (d_in_lookup(dentry)) {
851 res = v9fs_vfs_lookup(dir, dentry, 0);
852 if (IS_ERR(res))
853 return PTR_ERR(res);
855 if (res)
856 dentry = res;
859 /* Only creates */
860 if (!(flags & O_CREAT) || d_really_is_positive(dentry))
861 return finish_no_open(file, res);
863 err = 0;
865 v9ses = v9fs_inode2v9ses(dir);
866 perm = unixmode2p9mode(v9ses, mode);
867 fid = v9fs_create(v9ses, dir, dentry, NULL, perm,
868 v9fs_uflags2omode(flags,
869 v9fs_proto_dotu(v9ses)));
870 if (IS_ERR(fid)) {
871 err = PTR_ERR(fid);
872 fid = NULL;
873 goto error;
876 v9fs_invalidate_inode_attr(dir);
877 v9inode = V9FS_I(d_inode(dentry));
878 mutex_lock(&v9inode->v_mutex);
879 if ((v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE) &&
880 !v9inode->writeback_fid &&
881 ((flags & O_ACCMODE) != O_RDONLY)) {
883 * clone a fid and add it to writeback_fid
884 * we do it during open time instead of
885 * page dirty time via write_begin/page_mkwrite
886 * because we want write after unlink usecase
887 * to work.
889 inode_fid = v9fs_writeback_fid(dentry);
890 if (IS_ERR(inode_fid)) {
891 err = PTR_ERR(inode_fid);
892 mutex_unlock(&v9inode->v_mutex);
893 goto error;
895 v9inode->writeback_fid = (void *) inode_fid;
897 mutex_unlock(&v9inode->v_mutex);
898 err = finish_open(file, dentry, generic_file_open);
899 if (err)
900 goto error;
902 file->private_data = fid;
903 if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE)
904 v9fs_cache_inode_set_cookie(d_inode(dentry), file);
906 file->f_mode |= FMODE_CREATED;
907 out:
908 dput(res);
909 return err;
911 error:
912 if (fid)
913 p9_client_clunk(fid);
914 goto out;
918 * v9fs_vfs_unlink - VFS unlink hook to delete an inode
919 * @i: inode that is being unlinked
920 * @d: dentry that is being unlinked
924 int v9fs_vfs_unlink(struct inode *i, struct dentry *d)
926 return v9fs_remove(i, d, 0);
930 * v9fs_vfs_rmdir - VFS unlink hook to delete a directory
931 * @i: inode that is being unlinked
932 * @d: dentry that is being unlinked
936 int v9fs_vfs_rmdir(struct inode *i, struct dentry *d)
938 return v9fs_remove(i, d, AT_REMOVEDIR);
942 * v9fs_vfs_rename - VFS hook to rename an inode
943 * @old_dir: old dir inode
944 * @old_dentry: old dentry
945 * @new_dir: new dir inode
946 * @new_dentry: new dentry
951 v9fs_vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
952 struct inode *new_dir, struct dentry *new_dentry,
953 unsigned int flags)
955 int retval;
956 struct inode *old_inode;
957 struct inode *new_inode;
958 struct v9fs_session_info *v9ses;
959 struct p9_fid *oldfid;
960 struct p9_fid *olddirfid;
961 struct p9_fid *newdirfid;
962 struct p9_wstat wstat;
964 if (flags)
965 return -EINVAL;
967 p9_debug(P9_DEBUG_VFS, "\n");
968 retval = 0;
969 old_inode = d_inode(old_dentry);
970 new_inode = d_inode(new_dentry);
971 v9ses = v9fs_inode2v9ses(old_inode);
972 oldfid = v9fs_fid_lookup(old_dentry);
973 if (IS_ERR(oldfid))
974 return PTR_ERR(oldfid);
976 olddirfid = clone_fid(v9fs_parent_fid(old_dentry));
977 if (IS_ERR(olddirfid)) {
978 retval = PTR_ERR(olddirfid);
979 goto done;
982 newdirfid = clone_fid(v9fs_parent_fid(new_dentry));
983 if (IS_ERR(newdirfid)) {
984 retval = PTR_ERR(newdirfid);
985 goto clunk_olddir;
988 down_write(&v9ses->rename_sem);
989 if (v9fs_proto_dotl(v9ses)) {
990 retval = p9_client_renameat(olddirfid, old_dentry->d_name.name,
991 newdirfid, new_dentry->d_name.name);
992 if (retval == -EOPNOTSUPP)
993 retval = p9_client_rename(oldfid, newdirfid,
994 new_dentry->d_name.name);
995 if (retval != -EOPNOTSUPP)
996 goto clunk_newdir;
998 if (old_dentry->d_parent != new_dentry->d_parent) {
1000 * 9P .u can only handle file rename in the same directory
1003 p9_debug(P9_DEBUG_ERROR, "old dir and new dir are different\n");
1004 retval = -EXDEV;
1005 goto clunk_newdir;
1007 v9fs_blank_wstat(&wstat);
1008 wstat.muid = v9ses->uname;
1009 wstat.name = new_dentry->d_name.name;
1010 retval = p9_client_wstat(oldfid, &wstat);
1012 clunk_newdir:
1013 if (!retval) {
1014 if (new_inode) {
1015 if (S_ISDIR(new_inode->i_mode))
1016 clear_nlink(new_inode);
1017 else
1018 v9fs_dec_count(new_inode);
1020 if (S_ISDIR(old_inode->i_mode)) {
1021 if (!new_inode)
1022 inc_nlink(new_dir);
1023 v9fs_dec_count(old_dir);
1025 v9fs_invalidate_inode_attr(old_inode);
1026 v9fs_invalidate_inode_attr(old_dir);
1027 v9fs_invalidate_inode_attr(new_dir);
1029 /* successful rename */
1030 d_move(old_dentry, new_dentry);
1032 up_write(&v9ses->rename_sem);
1033 p9_client_clunk(newdirfid);
1035 clunk_olddir:
1036 p9_client_clunk(olddirfid);
1038 done:
1039 return retval;
1043 * v9fs_vfs_getattr - retrieve file metadata
1044 * @path: Object to query
1045 * @stat: metadata structure to populate
1046 * @request_mask: Mask of STATX_xxx flags indicating the caller's interests
1047 * @flags: AT_STATX_xxx setting
1051 static int
1052 v9fs_vfs_getattr(const struct path *path, struct kstat *stat,
1053 u32 request_mask, unsigned int flags)
1055 struct dentry *dentry = path->dentry;
1056 struct v9fs_session_info *v9ses;
1057 struct p9_fid *fid;
1058 struct p9_wstat *st;
1060 p9_debug(P9_DEBUG_VFS, "dentry: %p\n", dentry);
1061 v9ses = v9fs_dentry2v9ses(dentry);
1062 if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE) {
1063 generic_fillattr(d_inode(dentry), stat);
1064 return 0;
1066 fid = v9fs_fid_lookup(dentry);
1067 if (IS_ERR(fid))
1068 return PTR_ERR(fid);
1070 st = p9_client_stat(fid);
1071 if (IS_ERR(st))
1072 return PTR_ERR(st);
1074 v9fs_stat2inode(st, d_inode(dentry), dentry->d_sb, 0);
1075 generic_fillattr(d_inode(dentry), stat);
1077 p9stat_free(st);
1078 kfree(st);
1079 return 0;
1083 * v9fs_vfs_setattr - set file metadata
1084 * @dentry: file whose metadata to set
1085 * @iattr: metadata assignment structure
1089 static int v9fs_vfs_setattr(struct dentry *dentry, struct iattr *iattr)
1091 int retval;
1092 struct v9fs_session_info *v9ses;
1093 struct p9_fid *fid;
1094 struct p9_wstat wstat;
1096 p9_debug(P9_DEBUG_VFS, "\n");
1097 retval = setattr_prepare(dentry, iattr);
1098 if (retval)
1099 return retval;
1101 retval = -EPERM;
1102 v9ses = v9fs_dentry2v9ses(dentry);
1103 fid = v9fs_fid_lookup(dentry);
1104 if(IS_ERR(fid))
1105 return PTR_ERR(fid);
1107 v9fs_blank_wstat(&wstat);
1108 if (iattr->ia_valid & ATTR_MODE)
1109 wstat.mode = unixmode2p9mode(v9ses, iattr->ia_mode);
1111 if (iattr->ia_valid & ATTR_MTIME)
1112 wstat.mtime = iattr->ia_mtime.tv_sec;
1114 if (iattr->ia_valid & ATTR_ATIME)
1115 wstat.atime = iattr->ia_atime.tv_sec;
1117 if (iattr->ia_valid & ATTR_SIZE)
1118 wstat.length = iattr->ia_size;
1120 if (v9fs_proto_dotu(v9ses)) {
1121 if (iattr->ia_valid & ATTR_UID)
1122 wstat.n_uid = iattr->ia_uid;
1124 if (iattr->ia_valid & ATTR_GID)
1125 wstat.n_gid = iattr->ia_gid;
1128 /* Write all dirty data */
1129 if (d_is_reg(dentry))
1130 filemap_write_and_wait(d_inode(dentry)->i_mapping);
1132 retval = p9_client_wstat(fid, &wstat);
1133 if (retval < 0)
1134 return retval;
1136 if ((iattr->ia_valid & ATTR_SIZE) &&
1137 iattr->ia_size != i_size_read(d_inode(dentry)))
1138 truncate_setsize(d_inode(dentry), iattr->ia_size);
1140 v9fs_invalidate_inode_attr(d_inode(dentry));
1142 setattr_copy(d_inode(dentry), iattr);
1143 mark_inode_dirty(d_inode(dentry));
1144 return 0;
1148 * v9fs_stat2inode - populate an inode structure with mistat info
1149 * @stat: Plan 9 metadata (mistat) structure
1150 * @inode: inode to populate
1151 * @sb: superblock of filesystem
1152 * @flags: control flags (e.g. V9FS_STAT2INODE_KEEP_ISIZE)
1156 void
1157 v9fs_stat2inode(struct p9_wstat *stat, struct inode *inode,
1158 struct super_block *sb, unsigned int flags)
1160 umode_t mode;
1161 char ext[32];
1162 char tag_name[14];
1163 unsigned int i_nlink;
1164 struct v9fs_session_info *v9ses = sb->s_fs_info;
1165 struct v9fs_inode *v9inode = V9FS_I(inode);
1167 set_nlink(inode, 1);
1169 inode->i_atime.tv_sec = stat->atime;
1170 inode->i_mtime.tv_sec = stat->mtime;
1171 inode->i_ctime.tv_sec = stat->mtime;
1173 inode->i_uid = v9ses->dfltuid;
1174 inode->i_gid = v9ses->dfltgid;
1176 if (v9fs_proto_dotu(v9ses)) {
1177 inode->i_uid = stat->n_uid;
1178 inode->i_gid = stat->n_gid;
1180 if ((S_ISREG(inode->i_mode)) || (S_ISDIR(inode->i_mode))) {
1181 if (v9fs_proto_dotu(v9ses) && (stat->extension[0] != '\0')) {
1183 * Hadlink support got added later to
1184 * to the .u extension. So there can be
1185 * server out there that doesn't support
1186 * this even with .u extension. So check
1187 * for non NULL stat->extension
1189 strlcpy(ext, stat->extension, sizeof(ext));
1190 /* HARDLINKCOUNT %u */
1191 sscanf(ext, "%13s %u", tag_name, &i_nlink);
1192 if (!strncmp(tag_name, "HARDLINKCOUNT", 13))
1193 set_nlink(inode, i_nlink);
1196 mode = p9mode2perm(v9ses, stat);
1197 mode |= inode->i_mode & ~S_IALLUGO;
1198 inode->i_mode = mode;
1200 if (!(flags & V9FS_STAT2INODE_KEEP_ISIZE))
1201 v9fs_i_size_write(inode, stat->length);
1202 /* not real number of blocks, but 512 byte ones ... */
1203 inode->i_blocks = (stat->length + 512 - 1) >> 9;
1204 v9inode->cache_validity &= ~V9FS_INO_INVALID_ATTR;
1208 * v9fs_qid2ino - convert qid into inode number
1209 * @qid: qid to hash
1211 * BUG: potential for inode number collisions?
1214 ino_t v9fs_qid2ino(struct p9_qid *qid)
1216 u64 path = qid->path + 2;
1217 ino_t i = 0;
1219 if (sizeof(ino_t) == sizeof(path))
1220 memcpy(&i, &path, sizeof(ino_t));
1221 else
1222 i = (ino_t) (path ^ (path >> 32));
1224 return i;
1228 * v9fs_vfs_get_link - follow a symlink path
1229 * @dentry: dentry for symlink
1230 * @inode: inode for symlink
1231 * @done: delayed call for when we are done with the return value
1234 static const char *v9fs_vfs_get_link(struct dentry *dentry,
1235 struct inode *inode,
1236 struct delayed_call *done)
1238 struct v9fs_session_info *v9ses;
1239 struct p9_fid *fid;
1240 struct p9_wstat *st;
1241 char *res;
1243 if (!dentry)
1244 return ERR_PTR(-ECHILD);
1246 v9ses = v9fs_dentry2v9ses(dentry);
1247 fid = v9fs_fid_lookup(dentry);
1248 p9_debug(P9_DEBUG_VFS, "%pd\n", dentry);
1250 if (IS_ERR(fid))
1251 return ERR_CAST(fid);
1253 if (!v9fs_proto_dotu(v9ses))
1254 return ERR_PTR(-EBADF);
1256 st = p9_client_stat(fid);
1257 if (IS_ERR(st))
1258 return ERR_CAST(st);
1260 if (!(st->mode & P9_DMSYMLINK)) {
1261 p9stat_free(st);
1262 kfree(st);
1263 return ERR_PTR(-EINVAL);
1265 res = st->extension;
1266 st->extension = NULL;
1267 if (strlen(res) >= PATH_MAX)
1268 res[PATH_MAX - 1] = '\0';
1270 p9stat_free(st);
1271 kfree(st);
1272 set_delayed_call(done, kfree_link, res);
1273 return res;
1277 * v9fs_vfs_mkspecial - create a special file
1278 * @dir: inode to create special file in
1279 * @dentry: dentry to create
1280 * @perm: mode to create special file
1281 * @extension: 9p2000.u format extension string representing special file
1285 static int v9fs_vfs_mkspecial(struct inode *dir, struct dentry *dentry,
1286 u32 perm, const char *extension)
1288 struct p9_fid *fid;
1289 struct v9fs_session_info *v9ses;
1291 v9ses = v9fs_inode2v9ses(dir);
1292 if (!v9fs_proto_dotu(v9ses)) {
1293 p9_debug(P9_DEBUG_ERROR, "not extended\n");
1294 return -EPERM;
1297 fid = v9fs_create(v9ses, dir, dentry, (char *) extension, perm,
1298 P9_OREAD);
1299 if (IS_ERR(fid))
1300 return PTR_ERR(fid);
1302 v9fs_invalidate_inode_attr(dir);
1303 p9_client_clunk(fid);
1304 return 0;
1308 * v9fs_vfs_symlink - helper function to create symlinks
1309 * @dir: directory inode containing symlink
1310 * @dentry: dentry for symlink
1311 * @symname: symlink data
1313 * See Also: 9P2000.u RFC for more information
1317 static int
1318 v9fs_vfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
1320 p9_debug(P9_DEBUG_VFS, " %lu,%pd,%s\n",
1321 dir->i_ino, dentry, symname);
1323 return v9fs_vfs_mkspecial(dir, dentry, P9_DMSYMLINK, symname);
1326 #define U32_MAX_DIGITS 10
1329 * v9fs_vfs_link - create a hardlink
1330 * @old_dentry: dentry for file to link to
1331 * @dir: inode destination for new link
1332 * @dentry: dentry for link
1336 static int
1337 v9fs_vfs_link(struct dentry *old_dentry, struct inode *dir,
1338 struct dentry *dentry)
1340 int retval;
1341 char name[1 + U32_MAX_DIGITS + 2]; /* sign + number + \n + \0 */
1342 struct p9_fid *oldfid;
1344 p9_debug(P9_DEBUG_VFS, " %lu,%pd,%pd\n",
1345 dir->i_ino, dentry, old_dentry);
1347 oldfid = v9fs_fid_clone(old_dentry);
1348 if (IS_ERR(oldfid))
1349 return PTR_ERR(oldfid);
1351 sprintf(name, "%d\n", oldfid->fid);
1352 retval = v9fs_vfs_mkspecial(dir, dentry, P9_DMLINK, name);
1353 if (!retval) {
1354 v9fs_refresh_inode(oldfid, d_inode(old_dentry));
1355 v9fs_invalidate_inode_attr(dir);
1357 p9_client_clunk(oldfid);
1358 return retval;
1362 * v9fs_vfs_mknod - create a special file
1363 * @dir: inode destination for new link
1364 * @dentry: dentry for file
1365 * @mode: mode for creation
1366 * @rdev: device associated with special file
1370 static int
1371 v9fs_vfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t rdev)
1373 struct v9fs_session_info *v9ses = v9fs_inode2v9ses(dir);
1374 int retval;
1375 char name[2 + U32_MAX_DIGITS + 1 + U32_MAX_DIGITS + 1];
1376 u32 perm;
1378 p9_debug(P9_DEBUG_VFS, " %lu,%pd mode: %hx MAJOR: %u MINOR: %u\n",
1379 dir->i_ino, dentry, mode,
1380 MAJOR(rdev), MINOR(rdev));
1382 /* build extension */
1383 if (S_ISBLK(mode))
1384 sprintf(name, "b %u %u", MAJOR(rdev), MINOR(rdev));
1385 else if (S_ISCHR(mode))
1386 sprintf(name, "c %u %u", MAJOR(rdev), MINOR(rdev));
1387 else
1388 *name = 0;
1390 perm = unixmode2p9mode(v9ses, mode);
1391 retval = v9fs_vfs_mkspecial(dir, dentry, perm, name);
1393 return retval;
1396 int v9fs_refresh_inode(struct p9_fid *fid, struct inode *inode)
1398 int umode;
1399 dev_t rdev;
1400 struct p9_wstat *st;
1401 struct v9fs_session_info *v9ses;
1402 unsigned int flags;
1404 v9ses = v9fs_inode2v9ses(inode);
1405 st = p9_client_stat(fid);
1406 if (IS_ERR(st))
1407 return PTR_ERR(st);
1409 * Don't update inode if the file type is different
1411 umode = p9mode2unixmode(v9ses, st, &rdev);
1412 if ((inode->i_mode & S_IFMT) != (umode & S_IFMT))
1413 goto out;
1416 * We don't want to refresh inode->i_size,
1417 * because we may have cached data
1419 flags = (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE) ?
1420 V9FS_STAT2INODE_KEEP_ISIZE : 0;
1421 v9fs_stat2inode(st, inode, inode->i_sb, flags);
1422 out:
1423 p9stat_free(st);
1424 kfree(st);
1425 return 0;
1428 static const struct inode_operations v9fs_dir_inode_operations_dotu = {
1429 .create = v9fs_vfs_create,
1430 .lookup = v9fs_vfs_lookup,
1431 .atomic_open = v9fs_vfs_atomic_open,
1432 .symlink = v9fs_vfs_symlink,
1433 .link = v9fs_vfs_link,
1434 .unlink = v9fs_vfs_unlink,
1435 .mkdir = v9fs_vfs_mkdir,
1436 .rmdir = v9fs_vfs_rmdir,
1437 .mknod = v9fs_vfs_mknod,
1438 .rename = v9fs_vfs_rename,
1439 .getattr = v9fs_vfs_getattr,
1440 .setattr = v9fs_vfs_setattr,
1443 static const struct inode_operations v9fs_dir_inode_operations = {
1444 .create = v9fs_vfs_create,
1445 .lookup = v9fs_vfs_lookup,
1446 .atomic_open = v9fs_vfs_atomic_open,
1447 .unlink = v9fs_vfs_unlink,
1448 .mkdir = v9fs_vfs_mkdir,
1449 .rmdir = v9fs_vfs_rmdir,
1450 .mknod = v9fs_vfs_mknod,
1451 .rename = v9fs_vfs_rename,
1452 .getattr = v9fs_vfs_getattr,
1453 .setattr = v9fs_vfs_setattr,
1456 static const struct inode_operations v9fs_file_inode_operations = {
1457 .getattr = v9fs_vfs_getattr,
1458 .setattr = v9fs_vfs_setattr,
1461 static const struct inode_operations v9fs_symlink_inode_operations = {
1462 .get_link = v9fs_vfs_get_link,
1463 .getattr = v9fs_vfs_getattr,
1464 .setattr = v9fs_vfs_setattr,