blob: 52f8ae79db21925ec7bed3d1890e752b18c4d47a [file] [log] [blame]
Thomas Gleixner1f327612019-05-28 09:57:16 -07001// SPDX-License-Identifier: GPL-2.0-only
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -06002/*
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -06003 * This file contains vfs inode ops for the 9P2000.L protocol.
4 *
5 * Copyright (C) 2004 by Eric Van Hensbergen <ericvh@gmail.com>
6 * Copyright (C) 2002 by Ron Minnich <rminnich@lanl.gov>
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -06007 */
8
9#include <linux/module.h>
10#include <linux/errno.h>
11#include <linux/fs.h>
12#include <linux/file.h>
13#include <linux/pagemap.h>
14#include <linux/stat.h>
15#include <linux/string.h>
16#include <linux/inet.h>
17#include <linux/namei.h>
18#include <linux/idr.h>
19#include <linux/sched.h>
20#include <linux/slab.h>
21#include <linux/xattr.h>
22#include <linux/posix_acl.h>
23#include <net/9p/9p.h>
24#include <net/9p/client.h>
25
26#include "v9fs.h"
27#include "v9fs_vfs.h"
28#include "fid.h"
29#include "cache.h"
30#include "xattr.h"
31#include "acl.h"
32
33static int
Christian Brauner549c7292021-01-21 14:19:43 +010034v9fs_vfs_mknod_dotl(struct user_namespace *mnt_userns, struct inode *dir,
35 struct dentry *dentry, umode_t omode, dev_t rdev);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -060036
37/**
David Howellsbc868032021-10-04 22:07:22 +010038 * v9fs_get_fsgid_for_create - Helper function to get the gid for a new object
39 * @dir_inode: The directory inode
40 *
41 * Helper function to get the gid for creating a
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -060042 * new file system object. This checks the S_ISGID to determine the owning
43 * group of the new file system object.
44 */
45
Eric W. Biedermand4ef4e32013-01-30 12:08:21 -080046static kgid_t v9fs_get_fsgid_for_create(struct inode *dir_inode)
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -060047{
48 BUG_ON(dir_inode == NULL);
49
50 if (dir_inode->i_mode & S_ISGID) {
51 /* set_gid bit is set.*/
52 return dir_inode->i_gid;
53 }
54 return current_fsgid();
55}
56
Aneesh Kumar K.Vfd2421f2011-07-11 16:40:59 +000057static int v9fs_test_inode_dotl(struct inode *inode, void *data)
58{
59 struct v9fs_inode *v9inode = V9FS_I(inode);
60 struct p9_stat_dotl *st = (struct p9_stat_dotl *)data;
61
62 /* don't match inode of different type */
Al Viro6e3e2c42021-03-01 20:37:10 -050063 if (inode_wrong_type(inode, st->st_mode))
Aneesh Kumar K.Vfd2421f2011-07-11 16:40:59 +000064 return 0;
65
66 if (inode->i_generation != st->st_gen)
67 return 0;
68
69 /* compare qid details */
70 if (memcmp(&v9inode->qid.version,
71 &st->qid.version, sizeof(v9inode->qid.version)))
72 return 0;
73
74 if (v9inode->qid.type != st->qid.type)
75 return 0;
Tuomas Tynkkynen8ee03162017-09-06 17:59:07 +030076
77 if (v9inode->qid.path != st->qid.path)
78 return 0;
Aneesh Kumar K.Vfd2421f2011-07-11 16:40:59 +000079 return 1;
80}
81
Aneesh Kumar K.Ved80fcf2011-07-06 16:32:31 +053082/* Always get a new inode */
83static int v9fs_test_new_inode_dotl(struct inode *inode, void *data)
84{
85 return 0;
86}
87
Aneesh Kumar K.Vfd2421f2011-07-11 16:40:59 +000088static int v9fs_set_inode_dotl(struct inode *inode, void *data)
89{
90 struct v9fs_inode *v9inode = V9FS_I(inode);
91 struct p9_stat_dotl *st = (struct p9_stat_dotl *)data;
92
93 memcpy(&v9inode->qid, &st->qid, sizeof(st->qid));
94 inode->i_generation = st->st_gen;
95 return 0;
96}
97
Aneesh Kumar K.V5ffc0cb2011-02-28 17:04:01 +053098static struct inode *v9fs_qid_iget_dotl(struct super_block *sb,
99 struct p9_qid *qid,
100 struct p9_fid *fid,
Aneesh Kumar K.Ved80fcf2011-07-06 16:32:31 +0530101 struct p9_stat_dotl *st,
102 int new)
Aneesh Kumar K.V5ffc0cb2011-02-28 17:04:01 +0530103{
104 int retval;
105 unsigned long i_ino;
106 struct inode *inode;
107 struct v9fs_session_info *v9ses = sb->s_fs_info;
Dominique Martinet6e195b02021-11-02 22:16:43 +0900108 int (*test)(struct inode *inode, void *data);
Aneesh Kumar K.Ved80fcf2011-07-06 16:32:31 +0530109
110 if (new)
111 test = v9fs_test_new_inode_dotl;
112 else
113 test = v9fs_test_inode_dotl;
Aneesh Kumar K.V5ffc0cb2011-02-28 17:04:01 +0530114
115 i_ino = v9fs_qid2ino(qid);
Aneesh Kumar K.Ved80fcf2011-07-06 16:32:31 +0530116 inode = iget5_locked(sb, i_ino, test, v9fs_set_inode_dotl, st);
Aneesh Kumar K.V5ffc0cb2011-02-28 17:04:01 +0530117 if (!inode)
118 return ERR_PTR(-ENOMEM);
119 if (!(inode->i_state & I_NEW))
120 return inode;
121 /*
122 * initialize the inode with the stat info
123 * FIXME!! we may need support for stale inodes
124 * later.
125 */
Aneesh Kumar K.Vfd2421f2011-07-11 16:40:59 +0000126 inode->i_ino = i_ino;
Aneesh Kumar K.V45089142011-07-25 18:06:33 +0000127 retval = v9fs_init_inode(v9ses, inode,
128 st->st_mode, new_decode_dev(st->st_rdev));
Aneesh Kumar K.V5ffc0cb2011-02-28 17:04:01 +0530129 if (retval)
130 goto error;
131
Hou Tao5e3cc1e2019-01-24 14:35:13 +0800132 v9fs_stat2inode_dotl(st, inode, 0);
Aneesh Kumar K.V5ffc0cb2011-02-28 17:04:01 +0530133 v9fs_cache_inode_get_cookie(inode);
Aneesh Kumar K.V5ffc0cb2011-02-28 17:04:01 +0530134 retval = v9fs_get_acl(inode, fid);
135 if (retval)
136 goto error;
137
138 unlock_new_inode(inode);
139 return inode;
140error:
Al Viro0a73d0a2015-07-12 10:34:29 -0400141 iget_failed(inode);
Aneesh Kumar K.V5ffc0cb2011-02-28 17:04:01 +0530142 return ERR_PTR(retval);
143
144}
145
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600146struct inode *
Aneesh Kumar K.Va78ce052011-02-28 17:04:02 +0530147v9fs_inode_from_fid_dotl(struct v9fs_session_info *v9ses, struct p9_fid *fid,
Aneesh Kumar K.Ved80fcf2011-07-06 16:32:31 +0530148 struct super_block *sb, int new)
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600149{
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600150 struct p9_stat_dotl *st;
Aneesh Kumar K.V5ffc0cb2011-02-28 17:04:01 +0530151 struct inode *inode = NULL;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600152
Aneesh Kumar K.Vfd2421f2011-07-11 16:40:59 +0000153 st = p9_client_getattr_dotl(fid, P9_STATS_BASIC | P9_STATS_GEN);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600154 if (IS_ERR(st))
155 return ERR_CAST(st);
156
Aneesh Kumar K.Ved80fcf2011-07-06 16:32:31 +0530157 inode = v9fs_qid_iget_dotl(sb, &st->qid, fid, st, new);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600158 kfree(st);
Aneesh Kumar K.V5ffc0cb2011-02-28 17:04:01 +0530159 return inode;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600160}
161
Aneesh Kumar K.Vf88657c2011-08-03 19:55:32 +0530162struct dotl_openflag_map {
163 int open_flag;
164 int dotl_flag;
165};
166
167static int v9fs_mapped_dotl_flags(int flags)
168{
169 int i;
170 int rflags = 0;
171 struct dotl_openflag_map dotl_oflag_map[] = {
172 { O_CREAT, P9_DOTL_CREATE },
173 { O_EXCL, P9_DOTL_EXCL },
174 { O_NOCTTY, P9_DOTL_NOCTTY },
Aneesh Kumar K.Vf88657c2011-08-03 19:55:32 +0530175 { O_APPEND, P9_DOTL_APPEND },
176 { O_NONBLOCK, P9_DOTL_NONBLOCK },
177 { O_DSYNC, P9_DOTL_DSYNC },
178 { FASYNC, P9_DOTL_FASYNC },
179 { O_DIRECT, P9_DOTL_DIRECT },
180 { O_LARGEFILE, P9_DOTL_LARGEFILE },
181 { O_DIRECTORY, P9_DOTL_DIRECTORY },
182 { O_NOFOLLOW, P9_DOTL_NOFOLLOW },
183 { O_NOATIME, P9_DOTL_NOATIME },
184 { O_CLOEXEC, P9_DOTL_CLOEXEC },
185 { O_SYNC, P9_DOTL_SYNC},
186 };
187 for (i = 0; i < ARRAY_SIZE(dotl_oflag_map); i++) {
188 if (flags & dotl_oflag_map[i].open_flag)
189 rflags |= dotl_oflag_map[i].dotl_flag;
190 }
191 return rflags;
192}
193
194/**
195 * v9fs_open_to_dotl_flags- convert Linux specific open flags to
196 * plan 9 open flag.
197 * @flags: flags to convert
198 */
199int v9fs_open_to_dotl_flags(int flags)
200{
201 int rflags = 0;
202
203 /*
204 * We have same bits for P9_DOTL_READONLY, P9_DOTL_WRONLY
205 * and P9_DOTL_NOACCESS
206 */
207 rflags |= flags & O_ACCMODE;
208 rflags |= v9fs_mapped_dotl_flags(flags);
209
210 return rflags;
211}
212
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600213/**
214 * v9fs_vfs_create_dotl - VFS hook to create files for 9P2000.L protocol.
David Howellsbc868032021-10-04 22:07:22 +0100215 * @mnt_userns: The user namespace of the mount
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600216 * @dir: directory inode that is being created
217 * @dentry: dentry that is being deleted
Fabian Frederickfd2916b2014-06-04 16:06:26 -0700218 * @omode: create permissions
David Howellsbc868032021-10-04 22:07:22 +0100219 * @excl: True if the file must not yet exist
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600220 *
221 */
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600222static int
Christian Brauner549c7292021-01-21 14:19:43 +0100223v9fs_vfs_create_dotl(struct user_namespace *mnt_userns, struct inode *dir,
224 struct dentry *dentry, umode_t omode, bool excl)
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600225{
Christian Brauner549c7292021-01-21 14:19:43 +0100226 return v9fs_vfs_mknod_dotl(mnt_userns, dir, dentry, omode, 0);
Miklos Szeredie43ae792012-06-05 15:10:26 +0200227}
228
Al Virod9585272012-06-22 12:39:14 +0400229static int
Miklos Szeredie43ae792012-06-05 15:10:26 +0200230v9fs_vfs_atomic_open_dotl(struct inode *dir, struct dentry *dentry,
Dominique Martinet6e195b02021-11-02 22:16:43 +0900231 struct file *file, unsigned int flags, umode_t omode)
Miklos Szeredie43ae792012-06-05 15:10:26 +0200232{
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600233 int err = 0;
Eric W. Biedermand4ef4e32013-01-30 12:08:21 -0800234 kgid_t gid;
Al Virod3fb6122011-07-23 18:37:50 -0400235 umode_t mode;
Al Viro7880b432017-01-12 04:01:17 -0500236 const unsigned char *name = NULL;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600237 struct p9_qid qid;
238 struct inode *inode;
Aneesh Kumar K.V6b39f6d2011-02-28 17:04:03 +0530239 struct p9_fid *fid = NULL;
240 struct v9fs_inode *v9inode;
241 struct p9_fid *dfid, *ofid, *inode_fid;
242 struct v9fs_session_info *v9ses;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600243 struct posix_acl *pacl = NULL, *dacl = NULL;
Miklos Szeredie43ae792012-06-05 15:10:26 +0200244 struct dentry *res = NULL;
245
Al Viro00699ad2016-07-05 09:44:53 -0400246 if (d_in_lookup(dentry)) {
Al Viro00cd8dd2012-06-10 17:13:09 -0400247 res = v9fs_vfs_lookup(dir, dentry, 0);
Miklos Szeredie43ae792012-06-05 15:10:26 +0200248 if (IS_ERR(res))
Al Virod9585272012-06-22 12:39:14 +0400249 return PTR_ERR(res);
Miklos Szeredie43ae792012-06-05 15:10:26 +0200250
251 if (res)
252 dentry = res;
253 }
254
255 /* Only creates */
David Howells2b0143b2015-03-17 22:25:59 +0000256 if (!(flags & O_CREAT) || d_really_is_positive(dentry))
M. Mohan Kumarb6f4bee2013-02-05 14:25:05 +0530257 return finish_no_open(file, res);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600258
259 v9ses = v9fs_inode2v9ses(dir);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600260
Al Viro7880b432017-01-12 04:01:17 -0500261 name = dentry->d_name.name;
Dominique Martinet6e195b02021-11-02 22:16:43 +0900262 p9_debug(P9_DEBUG_VFS, "name:%s flags:0x%x mode:0x%x\n",
Joe Perches5d385152011-11-28 10:40:46 -0800263 name, flags, omode);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600264
Al Viro77d5a6b2016-05-29 15:29:26 -0400265 dfid = v9fs_parent_fid(dentry);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600266 if (IS_ERR(dfid)) {
267 err = PTR_ERR(dfid);
Joe Perches5d385152011-11-28 10:40:46 -0800268 p9_debug(P9_DEBUG_VFS, "fid lookup failed %d\n", err);
Al Virod9585272012-06-22 12:39:14 +0400269 goto out;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600270 }
271
272 /* clone a fid to use for creation */
Al Viro7d50a292016-08-03 11:12:12 -0400273 ofid = clone_fid(dfid);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600274 if (IS_ERR(ofid)) {
275 err = PTR_ERR(ofid);
Joe Perches5d385152011-11-28 10:40:46 -0800276 p9_debug(P9_DEBUG_VFS, "p9_client_walk failed %d\n", err);
Al Virod9585272012-06-22 12:39:14 +0400277 goto out;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600278 }
279
280 gid = v9fs_get_fsgid_for_create(dir);
281
282 mode = omode;
283 /* Update mode based on ACL value */
284 err = v9fs_acl_mode(dir, &mode, &dacl, &pacl);
285 if (err) {
Joe Perches5d385152011-11-28 10:40:46 -0800286 p9_debug(P9_DEBUG_VFS, "Failed to get acl values in creat %d\n",
287 err);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600288 goto error;
289 }
Aneesh Kumar K.Vf88657c2011-08-03 19:55:32 +0530290 err = p9_client_create_dotl(ofid, name, v9fs_open_to_dotl_flags(flags),
291 mode, gid, &qid);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600292 if (err < 0) {
Joe Perches5d385152011-11-28 10:40:46 -0800293 p9_debug(P9_DEBUG_VFS, "p9_client_open_dotl failed in creat %d\n",
294 err);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600295 goto error;
296 }
Aneesh Kumar K.Vd28c61f2011-02-28 17:04:08 +0530297 v9fs_invalidate_inode_attr(dir);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600298
Aneesh Kumar K.Vaf7542f2011-01-10 14:22:21 -0600299 /* instantiate inode and assign the unopened fid to the dentry */
300 fid = p9_client_walk(dfid, 1, &name, 1);
Jianyong Wu6636b6d2020-09-23 22:11:46 +0800301 p9_client_clunk(dfid);
Aneesh Kumar K.Vaf7542f2011-01-10 14:22:21 -0600302 if (IS_ERR(fid)) {
303 err = PTR_ERR(fid);
Joe Perches5d385152011-11-28 10:40:46 -0800304 p9_debug(P9_DEBUG_VFS, "p9_client_walk failed %d\n", err);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600305 fid = NULL;
Aneesh Kumar K.Vaf7542f2011-01-10 14:22:21 -0600306 goto error;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600307 }
Aneesh Kumar K.Ved80fcf2011-07-06 16:32:31 +0530308 inode = v9fs_get_new_inode_from_fid(v9ses, fid, dir->i_sb);
Aneesh Kumar K.Vaf7542f2011-01-10 14:22:21 -0600309 if (IS_ERR(inode)) {
310 err = PTR_ERR(inode);
Joe Perches5d385152011-11-28 10:40:46 -0800311 p9_debug(P9_DEBUG_VFS, "inode creation failed %d\n", err);
Aneesh Kumar K.Vaf7542f2011-01-10 14:22:21 -0600312 goto error;
313 }
Al Viro3592ac42013-01-31 13:45:39 -0500314 /* Now set the ACL based on the default value */
315 v9fs_set_create_acl(inode, fid, dacl, pacl);
316
Al Viro2ea03e1d2013-02-28 01:18:14 -0500317 v9fs_fid_add(dentry, fid);
Aneesh Kumar K.V5441ae52011-07-25 18:06:32 +0000318 d_instantiate(dentry, inode);
Aneesh Kumar K.Vaf7542f2011-01-10 14:22:21 -0600319
Aneesh Kumar K.V6b39f6d2011-02-28 17:04:03 +0530320 v9inode = V9FS_I(inode);
Aneesh Kumar K.V5a7e0a82011-03-08 16:39:46 +0530321 mutex_lock(&v9inode->v_mutex);
Dominique Martinetfb89b452014-01-10 13:44:09 +0100322 if ((v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE) &&
323 !v9inode->writeback_fid &&
Aneesh Kumar K.V7add6972011-03-08 16:39:49 +0530324 ((flags & O_ACCMODE) != O_RDONLY)) {
Aneesh Kumar K.V3cf387d2011-02-28 17:03:57 +0530325 /*
Aneesh Kumar K.V6b39f6d2011-02-28 17:04:03 +0530326 * clone a fid and add it to writeback_fid
Aneesh Kumar K.V3cf387d2011-02-28 17:03:57 +0530327 * we do it during open time instead of
328 * page dirty time via write_begin/page_mkwrite
329 * because we want write after unlink usecase
330 * to work.
331 */
332 inode_fid = v9fs_writeback_fid(dentry);
333 if (IS_ERR(inode_fid)) {
334 err = PTR_ERR(inode_fid);
Aneesh Kumar K.V5a7e0a82011-03-08 16:39:46 +0530335 mutex_unlock(&v9inode->v_mutex);
Aneesh Kumar K.V398c4f02011-05-20 18:55:51 +0000336 goto err_clunk_old_fid;
Aneesh Kumar K.V3cf387d2011-02-28 17:03:57 +0530337 }
Aneesh Kumar K.V6b39f6d2011-02-28 17:04:03 +0530338 v9inode->writeback_fid = (void *) inode_fid;
Aneesh Kumar K.V3cf387d2011-02-28 17:03:57 +0530339 }
Aneesh Kumar K.V5a7e0a82011-03-08 16:39:46 +0530340 mutex_unlock(&v9inode->v_mutex);
Aneesh Kumar K.Vaf7542f2011-01-10 14:22:21 -0600341 /* Since we are opening a file, assign the open fid to the file */
Al Virobe12af32018-06-08 11:44:56 -0400342 err = finish_open(file, dentry, generic_file_open);
Al Viro30d90492012-06-22 12:40:19 +0400343 if (err)
Aneesh Kumar K.V398c4f02011-05-20 18:55:51 +0000344 goto err_clunk_old_fid;
Al Viro30d90492012-06-22 12:40:19 +0400345 file->private_data = ofid;
Dominique Martinetfb89b452014-01-10 13:44:09 +0100346 if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE)
Al Viro30d90492012-06-22 12:40:19 +0400347 v9fs_cache_inode_set_cookie(inode, file);
Greg Kurz987a6482020-09-23 22:11:44 +0800348 v9fs_open_fid_add(inode, ofid);
Al Viro73a09dd2018-06-08 13:22:02 -0400349 file->f_mode |= FMODE_CREATED;
Miklos Szeredie43ae792012-06-05 15:10:26 +0200350out:
Al Viro5fa63002013-01-31 13:31:23 -0500351 v9fs_put_acl(dacl, pacl);
Miklos Szeredie43ae792012-06-05 15:10:26 +0200352 dput(res);
Al Virod9585272012-06-22 12:39:14 +0400353 return err;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600354
355error:
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600356 if (fid)
357 p9_client_clunk(fid);
Aneesh Kumar K.V398c4f02011-05-20 18:55:51 +0000358err_clunk_old_fid:
359 if (ofid)
360 p9_client_clunk(ofid);
Miklos Szeredie43ae792012-06-05 15:10:26 +0200361 goto out;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600362}
363
364/**
365 * v9fs_vfs_mkdir_dotl - VFS mkdir hook to create a directory
David Howellsbc868032021-10-04 22:07:22 +0100366 * @mnt_userns: The user namespace of the mount
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600367 * @dir: inode that is being unlinked
368 * @dentry: dentry that is being unlinked
Fabian Frederickfd2916b2014-06-04 16:06:26 -0700369 * @omode: mode for new directory
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600370 *
371 */
372
Christian Brauner549c7292021-01-21 14:19:43 +0100373static int v9fs_vfs_mkdir_dotl(struct user_namespace *mnt_userns,
374 struct inode *dir, struct dentry *dentry,
375 umode_t omode)
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600376{
377 int err;
378 struct v9fs_session_info *v9ses;
379 struct p9_fid *fid = NULL, *dfid = NULL;
Eric W. Biedermand4ef4e32013-01-30 12:08:21 -0800380 kgid_t gid;
Al Viro7880b432017-01-12 04:01:17 -0500381 const unsigned char *name;
Al Virod3fb6122011-07-23 18:37:50 -0400382 umode_t mode;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600383 struct inode *inode;
384 struct p9_qid qid;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600385 struct posix_acl *dacl = NULL, *pacl = NULL;
386
Al Viro4b8e9922014-08-19 20:17:38 -0400387 p9_debug(P9_DEBUG_VFS, "name %pd\n", dentry);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600388 err = 0;
389 v9ses = v9fs_inode2v9ses(dir);
390
391 omode |= S_IFDIR;
392 if (dir->i_mode & S_ISGID)
393 omode |= S_ISGID;
394
Al Viro77d5a6b2016-05-29 15:29:26 -0400395 dfid = v9fs_parent_fid(dentry);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600396 if (IS_ERR(dfid)) {
397 err = PTR_ERR(dfid);
Joe Perches5d385152011-11-28 10:40:46 -0800398 p9_debug(P9_DEBUG_VFS, "fid lookup failed %d\n", err);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600399 dfid = NULL;
400 goto error;
401 }
402
403 gid = v9fs_get_fsgid_for_create(dir);
404 mode = omode;
405 /* Update mode based on ACL value */
406 err = v9fs_acl_mode(dir, &mode, &dacl, &pacl);
407 if (err) {
Joe Perches5d385152011-11-28 10:40:46 -0800408 p9_debug(P9_DEBUG_VFS, "Failed to get acl values in mkdir %d\n",
409 err);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600410 goto error;
411 }
Al Viro7880b432017-01-12 04:01:17 -0500412 name = dentry->d_name.name;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600413 err = p9_client_mkdir_dotl(dfid, name, mode, gid, &qid);
414 if (err < 0)
415 goto error;
Al Viro3592ac42013-01-31 13:45:39 -0500416 fid = p9_client_walk(dfid, 1, &name, 1);
417 if (IS_ERR(fid)) {
418 err = PTR_ERR(fid);
419 p9_debug(P9_DEBUG_VFS, "p9_client_walk failed %d\n",
420 err);
421 fid = NULL;
422 goto error;
423 }
424
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600425 /* instantiate inode and assign the unopened fid to the dentry */
426 if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE) {
Aneesh Kumar K.Ved80fcf2011-07-06 16:32:31 +0530427 inode = v9fs_get_new_inode_from_fid(v9ses, fid, dir->i_sb);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600428 if (IS_ERR(inode)) {
429 err = PTR_ERR(inode);
Joe Perches5d385152011-11-28 10:40:46 -0800430 p9_debug(P9_DEBUG_VFS, "inode creation failed %d\n",
431 err);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600432 goto error;
433 }
Al Viro2ea03e1d2013-02-28 01:18:14 -0500434 v9fs_fid_add(dentry, fid);
Al Viro3592ac42013-01-31 13:45:39 -0500435 v9fs_set_create_acl(inode, fid, dacl, pacl);
Aneesh Kumar K.V5441ae52011-07-25 18:06:32 +0000436 d_instantiate(dentry, inode);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600437 fid = NULL;
Al Viro2ea03e1d2013-02-28 01:18:14 -0500438 err = 0;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600439 } else {
440 /*
441 * Not in cached mode. No need to populate
442 * inode with stat. We need to get an inode
443 * so that we can set the acl with dentry
444 */
Aneesh Kumar K.V45089142011-07-25 18:06:33 +0000445 inode = v9fs_get_inode(dir->i_sb, mode, 0);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600446 if (IS_ERR(inode)) {
447 err = PTR_ERR(inode);
448 goto error;
449 }
Al Viro3592ac42013-01-31 13:45:39 -0500450 v9fs_set_create_acl(inode, fid, dacl, pacl);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600451 d_instantiate(dentry, inode);
452 }
Aneesh Kumar K.Vb271ec42011-02-28 17:04:05 +0530453 inc_nlink(dir);
Aneesh Kumar K.Vd28c61f2011-02-28 17:04:08 +0530454 v9fs_invalidate_inode_attr(dir);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600455error:
456 if (fid)
457 p9_client_clunk(fid);
Al Viro5fa63002013-01-31 13:31:23 -0500458 v9fs_put_acl(dacl, pacl);
Jianyong Wu6636b6d2020-09-23 22:11:46 +0800459 p9_client_clunk(dfid);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600460 return err;
461}
462
463static int
Christian Brauner549c7292021-01-21 14:19:43 +0100464v9fs_vfs_getattr_dotl(struct user_namespace *mnt_userns,
465 const struct path *path, struct kstat *stat,
466 u32 request_mask, unsigned int flags)
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600467{
David Howellsa528d352017-01-31 16:46:22 +0000468 struct dentry *dentry = path->dentry;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600469 struct v9fs_session_info *v9ses;
470 struct p9_fid *fid;
471 struct p9_stat_dotl *st;
472
Joe Perches5d385152011-11-28 10:40:46 -0800473 p9_debug(P9_DEBUG_VFS, "dentry: %p\n", dentry);
Aneesh Kumar K.V42869c82011-03-08 16:39:50 +0530474 v9ses = v9fs_dentry2v9ses(dentry);
Aneesh Kumar K.Va1211902011-02-28 17:04:01 +0530475 if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE) {
Christian Brauner0d56a452021-01-21 14:19:30 +0100476 generic_fillattr(&init_user_ns, d_inode(dentry), stat);
Aneesh Kumar K.Va1211902011-02-28 17:04:01 +0530477 return 0;
478 }
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600479 fid = v9fs_fid_lookup(dentry);
480 if (IS_ERR(fid))
481 return PTR_ERR(fid);
482
483 /* Ask for all the fields in stat structure. Server will return
484 * whatever it supports
485 */
486
487 st = p9_client_getattr_dotl(fid, P9_STATS_ALL);
Jianyong Wu6636b6d2020-09-23 22:11:46 +0800488 p9_client_clunk(fid);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600489 if (IS_ERR(st))
490 return PTR_ERR(st);
491
Hou Tao5e3cc1e2019-01-24 14:35:13 +0800492 v9fs_stat2inode_dotl(st, d_inode(dentry), 0);
Christian Brauner0d56a452021-01-21 14:19:30 +0100493 generic_fillattr(&init_user_ns, d_inode(dentry), stat);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600494 /* Change block size to what the server returned */
495 stat->blksize = st->st_blksize;
496
497 kfree(st);
498 return 0;
499}
500
Aneesh Kumar K.Vf7666192011-12-18 23:03:13 +0530501/*
502 * Attribute flags.
503 */
504#define P9_ATTR_MODE (1 << 0)
505#define P9_ATTR_UID (1 << 1)
506#define P9_ATTR_GID (1 << 2)
507#define P9_ATTR_SIZE (1 << 3)
508#define P9_ATTR_ATIME (1 << 4)
509#define P9_ATTR_MTIME (1 << 5)
510#define P9_ATTR_CTIME (1 << 6)
511#define P9_ATTR_ATIME_SET (1 << 7)
512#define P9_ATTR_MTIME_SET (1 << 8)
513
514struct dotl_iattr_map {
515 int iattr_valid;
516 int p9_iattr_valid;
517};
518
519static int v9fs_mapped_iattr_valid(int iattr_valid)
520{
521 int i;
522 int p9_iattr_valid = 0;
523 struct dotl_iattr_map dotl_iattr_map[] = {
524 { ATTR_MODE, P9_ATTR_MODE },
525 { ATTR_UID, P9_ATTR_UID },
526 { ATTR_GID, P9_ATTR_GID },
527 { ATTR_SIZE, P9_ATTR_SIZE },
528 { ATTR_ATIME, P9_ATTR_ATIME },
529 { ATTR_MTIME, P9_ATTR_MTIME },
530 { ATTR_CTIME, P9_ATTR_CTIME },
531 { ATTR_ATIME_SET, P9_ATTR_ATIME_SET },
532 { ATTR_MTIME_SET, P9_ATTR_MTIME_SET },
533 };
534 for (i = 0; i < ARRAY_SIZE(dotl_iattr_map); i++) {
535 if (iattr_valid & dotl_iattr_map[i].iattr_valid)
536 p9_iattr_valid |= dotl_iattr_map[i].p9_iattr_valid;
537 }
538 return p9_iattr_valid;
539}
540
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600541/**
542 * v9fs_vfs_setattr_dotl - set file metadata
David Howellsbc868032021-10-04 22:07:22 +0100543 * @mnt_userns: The user namespace of the mount
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600544 * @dentry: file whose metadata to set
545 * @iattr: metadata assignment structure
546 *
547 */
548
Christian Brauner549c7292021-01-21 14:19:43 +0100549int v9fs_vfs_setattr_dotl(struct user_namespace *mnt_userns,
550 struct dentry *dentry, struct iattr *iattr)
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600551{
Jianyong Wu6636b6d2020-09-23 22:11:46 +0800552 int retval, use_dentry = 0;
Jianyong Wu66246642020-07-10 18:15:48 +0800553 struct p9_fid *fid = NULL;
Christian Brauner3cb6ee92021-11-29 12:44:34 +0100554 struct p9_iattr_dotl p9attr = {
555 .uid = INVALID_UID,
556 .gid = INVALID_GID,
557 };
David Howells2b0143b2015-03-17 22:25:59 +0000558 struct inode *inode = d_inode(dentry);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600559
Joe Perches5d385152011-11-28 10:40:46 -0800560 p9_debug(P9_DEBUG_VFS, "\n");
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600561
Christian Brauner2f221d62021-01-21 14:19:26 +0100562 retval = setattr_prepare(&init_user_ns, dentry, iattr);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600563 if (retval)
564 return retval;
565
Aneesh Kumar K.Vf7666192011-12-18 23:03:13 +0530566 p9attr.valid = v9fs_mapped_iattr_valid(iattr->ia_valid);
Christian Brauner3cb6ee92021-11-29 12:44:34 +0100567 if (iattr->ia_valid & ATTR_MODE)
568 p9attr.mode = iattr->ia_mode;
569 if (iattr->ia_valid & ATTR_UID)
570 p9attr.uid = iattr->ia_uid;
571 if (iattr->ia_valid & ATTR_GID)
572 p9attr.gid = iattr->ia_gid;
573 if (iattr->ia_valid & ATTR_SIZE)
574 p9attr.size = iattr->ia_size;
575 if (iattr->ia_valid & ATTR_ATIME_SET) {
576 p9attr.atime_sec = iattr->ia_atime.tv_sec;
577 p9attr.atime_nsec = iattr->ia_atime.tv_nsec;
578 }
579 if (iattr->ia_valid & ATTR_MTIME_SET) {
580 p9attr.mtime_sec = iattr->ia_mtime.tv_sec;
581 p9attr.mtime_nsec = iattr->ia_mtime.tv_nsec;
582 }
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600583
Jianyong Wu66246642020-07-10 18:15:48 +0800584 if (iattr->ia_valid & ATTR_FILE) {
585 fid = iattr->ia_file->private_data;
586 WARN_ON(!fid);
587 }
Jianyong Wu6636b6d2020-09-23 22:11:46 +0800588 if (!fid) {
Jianyong Wu66246642020-07-10 18:15:48 +0800589 fid = v9fs_fid_lookup(dentry);
Jianyong Wu6636b6d2020-09-23 22:11:46 +0800590 use_dentry = 1;
591 }
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600592 if (IS_ERR(fid))
593 return PTR_ERR(fid);
594
Aneesh Kumar K.V3dc54362011-02-28 17:04:11 +0530595 /* Write all dirty data */
Al Virobe308f02013-01-31 12:58:16 -0500596 if (S_ISREG(inode->i_mode))
597 filemap_write_and_wait(inode->i_mapping);
Aneesh Kumar K.V3dc54362011-02-28 17:04:11 +0530598
Aneesh Kumar K.Vf10fc502011-02-28 17:04:10 +0530599 retval = p9_client_setattr(fid, &p9attr);
Jianyong Wu6636b6d2020-09-23 22:11:46 +0800600 if (retval < 0) {
601 if (use_dentry)
602 p9_client_clunk(fid);
Aneesh Kumar K.Vf10fc502011-02-28 17:04:10 +0530603 return retval;
Jianyong Wu6636b6d2020-09-23 22:11:46 +0800604 }
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600605
Aneesh Kumar K.V059c1382011-03-08 16:39:48 +0530606 if ((iattr->ia_valid & ATTR_SIZE) &&
Al Virobe308f02013-01-31 12:58:16 -0500607 iattr->ia_size != i_size_read(inode))
608 truncate_setsize(inode, iattr->ia_size);
Aneesh Kumar K.V059c1382011-03-08 16:39:48 +0530609
Al Virobe308f02013-01-31 12:58:16 -0500610 v9fs_invalidate_inode_attr(inode);
Christian Brauner2f221d62021-01-21 14:19:26 +0100611 setattr_copy(&init_user_ns, inode, iattr);
Al Virobe308f02013-01-31 12:58:16 -0500612 mark_inode_dirty(inode);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600613 if (iattr->ia_valid & ATTR_MODE) {
614 /* We also want to update ACL when we update mode bits */
Al Virobe308f02013-01-31 12:58:16 -0500615 retval = v9fs_acl_chmod(inode, fid);
Jianyong Wu6636b6d2020-09-23 22:11:46 +0800616 if (retval < 0) {
617 if (use_dentry)
618 p9_client_clunk(fid);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600619 return retval;
Jianyong Wu6636b6d2020-09-23 22:11:46 +0800620 }
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600621 }
Jianyong Wu6636b6d2020-09-23 22:11:46 +0800622 if (use_dentry)
623 p9_client_clunk(fid);
624
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600625 return 0;
626}
627
628/**
629 * v9fs_stat2inode_dotl - populate an inode structure with stat info
630 * @stat: stat structure
631 * @inode: inode to populate
Hou Tao5e3cc1e2019-01-24 14:35:13 +0800632 * @flags: ctrl flags (e.g. V9FS_STAT2INODE_KEEP_ISIZE)
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600633 *
634 */
635
636void
Hou Tao5e3cc1e2019-01-24 14:35:13 +0800637v9fs_stat2inode_dotl(struct p9_stat_dotl *stat, struct inode *inode,
638 unsigned int flags)
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600639{
Al Viro3eda0de2011-07-26 02:53:22 -0400640 umode_t mode;
Aneesh Kumar K.Vb3cbea02011-02-28 17:04:06 +0530641 struct v9fs_inode *v9inode = V9FS_I(inode);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600642
643 if ((stat->st_result_mask & P9_STATS_BASIC) == P9_STATS_BASIC) {
644 inode->i_atime.tv_sec = stat->st_atime_sec;
645 inode->i_atime.tv_nsec = stat->st_atime_nsec;
646 inode->i_mtime.tv_sec = stat->st_mtime_sec;
647 inode->i_mtime.tv_nsec = stat->st_mtime_nsec;
648 inode->i_ctime.tv_sec = stat->st_ctime_sec;
649 inode->i_ctime.tv_nsec = stat->st_ctime_nsec;
650 inode->i_uid = stat->st_uid;
651 inode->i_gid = stat->st_gid;
Miklos Szeredibfe86842011-10-28 14:13:29 +0200652 set_nlink(inode, stat->st_nlink);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600653
Aneesh Kumar K.V45089142011-07-25 18:06:33 +0000654 mode = stat->st_mode & S_IALLUGO;
655 mode |= inode->i_mode & ~S_IALLUGO;
656 inode->i_mode = mode;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600657
Hou Tao5e3cc1e2019-01-24 14:35:13 +0800658 if (!(flags & V9FS_STAT2INODE_KEEP_ISIZE))
659 v9fs_i_size_write(inode, stat->st_size);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600660 inode->i_blocks = stat->st_blocks;
661 } else {
662 if (stat->st_result_mask & P9_STATS_ATIME) {
663 inode->i_atime.tv_sec = stat->st_atime_sec;
664 inode->i_atime.tv_nsec = stat->st_atime_nsec;
665 }
666 if (stat->st_result_mask & P9_STATS_MTIME) {
667 inode->i_mtime.tv_sec = stat->st_mtime_sec;
668 inode->i_mtime.tv_nsec = stat->st_mtime_nsec;
669 }
670 if (stat->st_result_mask & P9_STATS_CTIME) {
671 inode->i_ctime.tv_sec = stat->st_ctime_sec;
672 inode->i_ctime.tv_nsec = stat->st_ctime_nsec;
673 }
674 if (stat->st_result_mask & P9_STATS_UID)
675 inode->i_uid = stat->st_uid;
676 if (stat->st_result_mask & P9_STATS_GID)
677 inode->i_gid = stat->st_gid;
678 if (stat->st_result_mask & P9_STATS_NLINK)
Miklos Szeredibfe86842011-10-28 14:13:29 +0200679 set_nlink(inode, stat->st_nlink);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600680 if (stat->st_result_mask & P9_STATS_MODE) {
Al Virob577d0c2021-01-31 14:37:39 -0500681 mode = stat->st_mode & S_IALLUGO;
682 mode |= inode->i_mode & ~S_IALLUGO;
683 inode->i_mode = mode;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600684 }
Hou Tao5e3cc1e2019-01-24 14:35:13 +0800685 if (!(flags & V9FS_STAT2INODE_KEEP_ISIZE) &&
686 stat->st_result_mask & P9_STATS_SIZE)
687 v9fs_i_size_write(inode, stat->st_size);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600688 if (stat->st_result_mask & P9_STATS_BLOCKS)
689 inode->i_blocks = stat->st_blocks;
690 }
691 if (stat->st_result_mask & P9_STATS_GEN)
Aneesh Kumar K.Vfd2421f2011-07-11 16:40:59 +0000692 inode->i_generation = stat->st_gen;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600693
694 /* Currently we don't support P9_STATS_BTIME and P9_STATS_DATA_VERSION
695 * because the inode structure does not have fields for them.
696 */
Aneesh Kumar K.Vb3cbea02011-02-28 17:04:06 +0530697 v9inode->cache_validity &= ~V9FS_INO_INVALID_ATTR;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600698}
699
700static int
Christian Brauner549c7292021-01-21 14:19:43 +0100701v9fs_vfs_symlink_dotl(struct user_namespace *mnt_userns, struct inode *dir,
702 struct dentry *dentry, const char *symname)
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600703{
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600704 int err;
Eric W. Biedermand4ef4e32013-01-30 12:08:21 -0800705 kgid_t gid;
Al Viro7880b432017-01-12 04:01:17 -0500706 const unsigned char *name;
Aneesh Kumar K.Vd28c61f2011-02-28 17:04:08 +0530707 struct p9_qid qid;
708 struct inode *inode;
709 struct p9_fid *dfid;
710 struct p9_fid *fid = NULL;
711 struct v9fs_session_info *v9ses;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600712
Al Viro7880b432017-01-12 04:01:17 -0500713 name = dentry->d_name.name;
Joe Perches5d385152011-11-28 10:40:46 -0800714 p9_debug(P9_DEBUG_VFS, "%lu,%s,%s\n", dir->i_ino, name, symname);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600715 v9ses = v9fs_inode2v9ses(dir);
716
Al Viro77d5a6b2016-05-29 15:29:26 -0400717 dfid = v9fs_parent_fid(dentry);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600718 if (IS_ERR(dfid)) {
719 err = PTR_ERR(dfid);
Joe Perches5d385152011-11-28 10:40:46 -0800720 p9_debug(P9_DEBUG_VFS, "fid lookup failed %d\n", err);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600721 return err;
722 }
723
724 gid = v9fs_get_fsgid_for_create(dir);
725
726 /* Server doesn't alter fid on TSYMLINK. Hence no need to clone it. */
Al Viro7880b432017-01-12 04:01:17 -0500727 err = p9_client_symlink(dfid, name, symname, gid, &qid);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600728
729 if (err < 0) {
Joe Perches5d385152011-11-28 10:40:46 -0800730 p9_debug(P9_DEBUG_VFS, "p9_client_symlink failed %d\n", err);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600731 goto error;
732 }
733
Aneesh Kumar K.Vd28c61f2011-02-28 17:04:08 +0530734 v9fs_invalidate_inode_attr(dir);
Dominique Martinetfb89b452014-01-10 13:44:09 +0100735 if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE) {
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600736 /* Now walk from the parent so we can get an unopened fid. */
737 fid = p9_client_walk(dfid, 1, &name, 1);
738 if (IS_ERR(fid)) {
739 err = PTR_ERR(fid);
Joe Perches5d385152011-11-28 10:40:46 -0800740 p9_debug(P9_DEBUG_VFS, "p9_client_walk failed %d\n",
741 err);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600742 fid = NULL;
743 goto error;
744 }
745
746 /* instantiate inode and assign the unopened fid to dentry */
Aneesh Kumar K.Ved80fcf2011-07-06 16:32:31 +0530747 inode = v9fs_get_new_inode_from_fid(v9ses, fid, dir->i_sb);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600748 if (IS_ERR(inode)) {
749 err = PTR_ERR(inode);
Joe Perches5d385152011-11-28 10:40:46 -0800750 p9_debug(P9_DEBUG_VFS, "inode creation failed %d\n",
751 err);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600752 goto error;
753 }
Al Viro2ea03e1d2013-02-28 01:18:14 -0500754 v9fs_fid_add(dentry, fid);
Aneesh Kumar K.V5441ae52011-07-25 18:06:32 +0000755 d_instantiate(dentry, inode);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600756 fid = NULL;
Al Viro2ea03e1d2013-02-28 01:18:14 -0500757 err = 0;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600758 } else {
759 /* Not in cached mode. No need to populate inode with stat */
Aneesh Kumar K.V45089142011-07-25 18:06:33 +0000760 inode = v9fs_get_inode(dir->i_sb, S_IFLNK, 0);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600761 if (IS_ERR(inode)) {
762 err = PTR_ERR(inode);
763 goto error;
764 }
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600765 d_instantiate(dentry, inode);
766 }
767
768error:
769 if (fid)
770 p9_client_clunk(fid);
771
Jianyong Wu6636b6d2020-09-23 22:11:46 +0800772 p9_client_clunk(dfid);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600773 return err;
774}
775
776/**
777 * v9fs_vfs_link_dotl - create a hardlink for dotl
778 * @old_dentry: dentry for file to link to
779 * @dir: inode destination for new link
780 * @dentry: dentry for link
781 *
782 */
783
784static int
785v9fs_vfs_link_dotl(struct dentry *old_dentry, struct inode *dir,
786 struct dentry *dentry)
787{
788 int err;
Aneesh Kumar K.Vd28c61f2011-02-28 17:04:08 +0530789 struct p9_fid *dfid, *oldfid;
790 struct v9fs_session_info *v9ses;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600791
Al Viro4b8e9922014-08-19 20:17:38 -0400792 p9_debug(P9_DEBUG_VFS, "dir ino: %lu, old_name: %pd, new_name: %pd\n",
793 dir->i_ino, old_dentry, dentry);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600794
795 v9ses = v9fs_inode2v9ses(dir);
Al Viro77d5a6b2016-05-29 15:29:26 -0400796 dfid = v9fs_parent_fid(dentry);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600797 if (IS_ERR(dfid))
798 return PTR_ERR(dfid);
799
800 oldfid = v9fs_fid_lookup(old_dentry);
Jianyong Wu6636b6d2020-09-23 22:11:46 +0800801 if (IS_ERR(oldfid)) {
802 p9_client_clunk(dfid);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600803 return PTR_ERR(oldfid);
Jianyong Wu6636b6d2020-09-23 22:11:46 +0800804 }
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600805
Al Viro7880b432017-01-12 04:01:17 -0500806 err = p9_client_link(dfid, oldfid, dentry->d_name.name);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600807
Jianyong Wu6636b6d2020-09-23 22:11:46 +0800808 p9_client_clunk(dfid);
809 p9_client_clunk(oldfid);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600810 if (err < 0) {
Joe Perches5d385152011-11-28 10:40:46 -0800811 p9_debug(P9_DEBUG_VFS, "p9_client_link failed %d\n", err);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600812 return err;
813 }
814
Aneesh Kumar K.Vd28c61f2011-02-28 17:04:08 +0530815 v9fs_invalidate_inode_attr(dir);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600816 if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE) {
817 /* Get the latest stat info from server. */
818 struct p9_fid *fid;
Dominique Martinet6e195b02021-11-02 22:16:43 +0900819
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600820 fid = v9fs_fid_lookup(old_dentry);
821 if (IS_ERR(fid))
822 return PTR_ERR(fid);
823
David Howells2b0143b2015-03-17 22:25:59 +0000824 v9fs_refresh_inode_dotl(fid, d_inode(old_dentry));
Jianyong Wu6636b6d2020-09-23 22:11:46 +0800825 p9_client_clunk(fid);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600826 }
David Howells2b0143b2015-03-17 22:25:59 +0000827 ihold(d_inode(old_dentry));
828 d_instantiate(dentry, d_inode(old_dentry));
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600829
830 return err;
831}
832
833/**
834 * v9fs_vfs_mknod_dotl - create a special file
David Howellsbc868032021-10-04 22:07:22 +0100835 * @mnt_userns: The user namespace of the mount
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600836 * @dir: inode destination for new link
837 * @dentry: dentry for file
Fabian Frederickfd2916b2014-06-04 16:06:26 -0700838 * @omode: mode for creation
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600839 * @rdev: device associated with special file
840 *
841 */
842static int
Christian Brauner549c7292021-01-21 14:19:43 +0100843v9fs_vfs_mknod_dotl(struct user_namespace *mnt_userns, struct inode *dir,
844 struct dentry *dentry, umode_t omode, dev_t rdev)
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600845{
846 int err;
Eric W. Biedermand4ef4e32013-01-30 12:08:21 -0800847 kgid_t gid;
Al Viro7880b432017-01-12 04:01:17 -0500848 const unsigned char *name;
Al Virod3fb6122011-07-23 18:37:50 -0400849 umode_t mode;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600850 struct v9fs_session_info *v9ses;
851 struct p9_fid *fid = NULL, *dfid = NULL;
852 struct inode *inode;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600853 struct p9_qid qid;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600854 struct posix_acl *dacl = NULL, *pacl = NULL;
855
Dominique Martinet6e195b02021-11-02 22:16:43 +0900856 p9_debug(P9_DEBUG_VFS, " %lu,%pd mode: %x MAJOR: %u MINOR: %u\n",
Al Viroa4555892014-10-21 20:11:25 -0400857 dir->i_ino, dentry, omode,
Joe Perches5d385152011-11-28 10:40:46 -0800858 MAJOR(rdev), MINOR(rdev));
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600859
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600860 v9ses = v9fs_inode2v9ses(dir);
Al Viro77d5a6b2016-05-29 15:29:26 -0400861 dfid = v9fs_parent_fid(dentry);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600862 if (IS_ERR(dfid)) {
863 err = PTR_ERR(dfid);
Joe Perches5d385152011-11-28 10:40:46 -0800864 p9_debug(P9_DEBUG_VFS, "fid lookup failed %d\n", err);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600865 dfid = NULL;
866 goto error;
867 }
868
869 gid = v9fs_get_fsgid_for_create(dir);
870 mode = omode;
871 /* Update mode based on ACL value */
872 err = v9fs_acl_mode(dir, &mode, &dacl, &pacl);
873 if (err) {
Joe Perches5d385152011-11-28 10:40:46 -0800874 p9_debug(P9_DEBUG_VFS, "Failed to get acl values in mknod %d\n",
875 err);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600876 goto error;
877 }
Al Viro7880b432017-01-12 04:01:17 -0500878 name = dentry->d_name.name;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600879
880 err = p9_client_mknod_dotl(dfid, name, mode, rdev, gid, &qid);
881 if (err < 0)
882 goto error;
883
Aneesh Kumar K.Vd28c61f2011-02-28 17:04:08 +0530884 v9fs_invalidate_inode_attr(dir);
Al Viro3592ac42013-01-31 13:45:39 -0500885 fid = p9_client_walk(dfid, 1, &name, 1);
886 if (IS_ERR(fid)) {
887 err = PTR_ERR(fid);
888 p9_debug(P9_DEBUG_VFS, "p9_client_walk failed %d\n",
889 err);
890 fid = NULL;
891 goto error;
892 }
893
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600894 /* instantiate inode and assign the unopened fid to the dentry */
895 if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE) {
Aneesh Kumar K.Ved80fcf2011-07-06 16:32:31 +0530896 inode = v9fs_get_new_inode_from_fid(v9ses, fid, dir->i_sb);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600897 if (IS_ERR(inode)) {
898 err = PTR_ERR(inode);
Joe Perches5d385152011-11-28 10:40:46 -0800899 p9_debug(P9_DEBUG_VFS, "inode creation failed %d\n",
900 err);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600901 goto error;
902 }
Al Viro3592ac42013-01-31 13:45:39 -0500903 v9fs_set_create_acl(inode, fid, dacl, pacl);
Al Viro2ea03e1d2013-02-28 01:18:14 -0500904 v9fs_fid_add(dentry, fid);
Aneesh Kumar K.V5441ae52011-07-25 18:06:32 +0000905 d_instantiate(dentry, inode);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600906 fid = NULL;
Al Viro2ea03e1d2013-02-28 01:18:14 -0500907 err = 0;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600908 } else {
909 /*
910 * Not in cached mode. No need to populate inode with stat.
911 * socket syscall returns a fd, so we need instantiate
912 */
Aneesh Kumar K.V45089142011-07-25 18:06:33 +0000913 inode = v9fs_get_inode(dir->i_sb, mode, rdev);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600914 if (IS_ERR(inode)) {
915 err = PTR_ERR(inode);
916 goto error;
917 }
Al Viro3592ac42013-01-31 13:45:39 -0500918 v9fs_set_create_acl(inode, fid, dacl, pacl);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600919 d_instantiate(dentry, inode);
920 }
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600921error:
922 if (fid)
923 p9_client_clunk(fid);
Al Viro5fa63002013-01-31 13:31:23 -0500924 v9fs_put_acl(dacl, pacl);
Jianyong Wu6636b6d2020-09-23 22:11:46 +0800925 p9_client_clunk(dfid);
926
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600927 return err;
928}
929
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600930/**
Al Viro6b255392015-11-17 10:20:54 -0500931 * v9fs_vfs_get_link_dotl - follow a symlink path
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600932 * @dentry: dentry for symlink
Al Viro6b255392015-11-17 10:20:54 -0500933 * @inode: inode for symlink
Al Virofceef392015-12-29 15:58:39 -0500934 * @done: destructor for return value
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600935 */
936
Al Viro680baac2015-05-02 13:32:22 -0400937static const char *
Al Viro6b255392015-11-17 10:20:54 -0500938v9fs_vfs_get_link_dotl(struct dentry *dentry,
Al Virofceef392015-12-29 15:58:39 -0500939 struct inode *inode,
940 struct delayed_call *done)
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600941{
Al Viro6b255392015-11-17 10:20:54 -0500942 struct p9_fid *fid;
M. Mohan Kumar31b6cea2011-01-08 07:28:46 +0530943 char *target;
Al Viro90e4fc82015-04-14 17:42:49 -0400944 int retval;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600945
Al Viro6b255392015-11-17 10:20:54 -0500946 if (!dentry)
947 return ERR_PTR(-ECHILD);
948
Al Viro4b8e9922014-08-19 20:17:38 -0400949 p9_debug(P9_DEBUG_VFS, "%pd\n", dentry);
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600950
Al Viro6b255392015-11-17 10:20:54 -0500951 fid = v9fs_fid_lookup(dentry);
Al Viro90e4fc82015-04-14 17:42:49 -0400952 if (IS_ERR(fid))
953 return ERR_CAST(fid);
M. Mohan Kumar31b6cea2011-01-08 07:28:46 +0530954 retval = p9_client_readlink(fid, &target);
Jianyong Wu6636b6d2020-09-23 22:11:46 +0800955 p9_client_clunk(fid);
Al Viro90e4fc82015-04-14 17:42:49 -0400956 if (retval)
957 return ERR_PTR(retval);
Al Virofceef392015-12-29 15:58:39 -0500958 set_delayed_call(done, kfree_link, target);
959 return target;
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600960}
961
Aneesh Kumar K.Vb3cbea02011-02-28 17:04:06 +0530962int v9fs_refresh_inode_dotl(struct p9_fid *fid, struct inode *inode)
963{
Aneesh Kumar K.Vb3cbea02011-02-28 17:04:06 +0530964 struct p9_stat_dotl *st;
965 struct v9fs_session_info *v9ses;
Hou Tao5e3cc1e2019-01-24 14:35:13 +0800966 unsigned int flags;
Aneesh Kumar K.Vb3cbea02011-02-28 17:04:06 +0530967
968 v9ses = v9fs_inode2v9ses(inode);
969 st = p9_client_getattr_dotl(fid, P9_STATS_ALL);
970 if (IS_ERR(st))
971 return PTR_ERR(st);
Aneesh Kumar K.V45089142011-07-25 18:06:33 +0000972 /*
973 * Don't update inode if the file type is different
974 */
Al Viro6e3e2c42021-03-01 20:37:10 -0500975 if (inode_wrong_type(inode, st->st_mode))
Aneesh Kumar K.V45089142011-07-25 18:06:33 +0000976 goto out;
Aneesh Kumar K.Vb3cbea02011-02-28 17:04:06 +0530977
Aneesh Kumar K.Vb3cbea02011-02-28 17:04:06 +0530978 /*
979 * We don't want to refresh inode->i_size,
980 * because we may have cached data
981 */
Hou Tao5e3cc1e2019-01-24 14:35:13 +0800982 flags = (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE) ?
983 V9FS_STAT2INODE_KEEP_ISIZE : 0;
984 v9fs_stat2inode_dotl(st, inode, flags);
Aneesh Kumar K.V45089142011-07-25 18:06:33 +0000985out:
Aneesh Kumar K.Vb3cbea02011-02-28 17:04:06 +0530986 kfree(st);
987 return 0;
988}
989
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600990const struct inode_operations v9fs_dir_inode_operations_dotl = {
991 .create = v9fs_vfs_create_dotl,
Miklos Szeredie43ae792012-06-05 15:10:26 +0200992 .atomic_open = v9fs_vfs_atomic_open_dotl,
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -0600993 .lookup = v9fs_vfs_lookup,
994 .link = v9fs_vfs_link_dotl,
995 .symlink = v9fs_vfs_symlink_dotl,
996 .unlink = v9fs_vfs_unlink,
997 .mkdir = v9fs_vfs_mkdir_dotl,
998 .rmdir = v9fs_vfs_rmdir,
999 .mknod = v9fs_vfs_mknod_dotl,
1000 .rename = v9fs_vfs_rename,
1001 .getattr = v9fs_vfs_getattr_dotl,
1002 .setattr = v9fs_vfs_setattr_dotl,
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -06001003 .listxattr = v9fs_listxattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02001004 .get_acl = v9fs_iop_get_acl,
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -06001005};
1006
1007const struct inode_operations v9fs_file_inode_operations_dotl = {
1008 .getattr = v9fs_vfs_getattr_dotl,
1009 .setattr = v9fs_vfs_setattr_dotl,
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -06001010 .listxattr = v9fs_listxattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02001011 .get_acl = v9fs_iop_get_acl,
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -06001012};
1013
1014const struct inode_operations v9fs_symlink_inode_operations_dotl = {
Al Viro6b255392015-11-17 10:20:54 -05001015 .get_link = v9fs_vfs_get_link_dotl,
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -06001016 .getattr = v9fs_vfs_getattr_dotl,
1017 .setattr = v9fs_vfs_setattr_dotl,
Aneesh Kumar K.V53c06f42011-01-10 13:51:47 -06001018 .listxattr = v9fs_listxattr,
1019};