9p: Use netfslib read/write_iter
[linux-block.git] / fs / 9p / vfs_inode_dotl.c
CommitLineData
1f327613 1// SPDX-License-Identifier: GPL-2.0-only
53c06f4e 2/*
53c06f4e
AK
3 * 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>
53c06f4e
AK
7 */
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>
53c06f4e 16#include <linux/namei.h>
53c06f4e
AK
17#include <linux/sched.h>
18#include <linux/slab.h>
19#include <linux/xattr.h>
20#include <linux/posix_acl.h>
21#include <net/9p/9p.h>
22#include <net/9p/client.h>
23
24#include "v9fs.h"
25#include "v9fs_vfs.h"
26#include "fid.h"
27#include "cache.h"
28#include "xattr.h"
29#include "acl.h"
30
31static int
5ebb29be 32v9fs_vfs_mknod_dotl(struct mnt_idmap *idmap, struct inode *dir,
549c7297 33 struct dentry *dentry, umode_t omode, dev_t rdev);
53c06f4e
AK
34
35/**
bc868036
DH
36 * v9fs_get_fsgid_for_create - Helper function to get the gid for a new object
37 * @dir_inode: The directory inode
38 *
39 * Helper function to get the gid for creating a
53c06f4e
AK
40 * new file system object. This checks the S_ISGID to determine the owning
41 * group of the new file system object.
42 */
43
d4ef4e35 44static kgid_t v9fs_get_fsgid_for_create(struct inode *dir_inode)
53c06f4e
AK
45{
46 BUG_ON(dir_inode == NULL);
47
48 if (dir_inode->i_mode & S_ISGID) {
49 /* set_gid bit is set.*/
50 return dir_inode->i_gid;
51 }
52 return current_fsgid();
53}
54
fd2421f5
AK
55static int v9fs_test_inode_dotl(struct inode *inode, void *data)
56{
57 struct v9fs_inode *v9inode = V9FS_I(inode);
58 struct p9_stat_dotl *st = (struct p9_stat_dotl *)data;
59
60 /* don't match inode of different type */
6e3e2c43 61 if (inode_wrong_type(inode, st->st_mode))
fd2421f5
AK
62 return 0;
63
64 if (inode->i_generation != st->st_gen)
65 return 0;
66
67 /* compare qid details */
68 if (memcmp(&v9inode->qid.version,
69 &st->qid.version, sizeof(v9inode->qid.version)))
70 return 0;
71
72 if (v9inode->qid.type != st->qid.type)
73 return 0;
8ee03163
TT
74
75 if (v9inode->qid.path != st->qid.path)
76 return 0;
fd2421f5
AK
77 return 1;
78}
79
ed80fcfa
AK
80/* Always get a new inode */
81static int v9fs_test_new_inode_dotl(struct inode *inode, void *data)
82{
83 return 0;
84}
85
fd2421f5
AK
86static int v9fs_set_inode_dotl(struct inode *inode, void *data)
87{
88 struct v9fs_inode *v9inode = V9FS_I(inode);
89 struct p9_stat_dotl *st = (struct p9_stat_dotl *)data;
90
91 memcpy(&v9inode->qid, &st->qid, sizeof(st->qid));
92 inode->i_generation = st->st_gen;
93 return 0;
94}
95
5ffc0cb3
AK
96static struct inode *v9fs_qid_iget_dotl(struct super_block *sb,
97 struct p9_qid *qid,
98 struct p9_fid *fid,
ed80fcfa
AK
99 struct p9_stat_dotl *st,
100 int new)
5ffc0cb3
AK
101{
102 int retval;
103 unsigned long i_ino;
104 struct inode *inode;
105 struct v9fs_session_info *v9ses = sb->s_fs_info;
6e195b0f 106 int (*test)(struct inode *inode, void *data);
ed80fcfa
AK
107
108 if (new)
109 test = v9fs_test_new_inode_dotl;
110 else
111 test = v9fs_test_inode_dotl;
5ffc0cb3
AK
112
113 i_ino = v9fs_qid2ino(qid);
ed80fcfa 114 inode = iget5_locked(sb, i_ino, test, v9fs_set_inode_dotl, st);
5ffc0cb3
AK
115 if (!inode)
116 return ERR_PTR(-ENOMEM);
117 if (!(inode->i_state & I_NEW))
118 return inode;
119 /*
120 * initialize the inode with the stat info
121 * FIXME!! we may need support for stale inodes
122 * later.
123 */
fd2421f5 124 inode->i_ino = i_ino;
45089142
AK
125 retval = v9fs_init_inode(v9ses, inode,
126 st->st_mode, new_decode_dev(st->st_rdev));
5ffc0cb3
AK
127 if (retval)
128 goto error;
129
5e3cc1ee 130 v9fs_stat2inode_dotl(st, inode, 0);
5ffc0cb3 131 v9fs_cache_inode_get_cookie(inode);
5ffc0cb3
AK
132 retval = v9fs_get_acl(inode, fid);
133 if (retval)
134 goto error;
135
136 unlock_new_inode(inode);
137 return inode;
138error:
0a73d0a2 139 iget_failed(inode);
5ffc0cb3
AK
140 return ERR_PTR(retval);
141
142}
143
53c06f4e 144struct inode *
a78ce05d 145v9fs_inode_from_fid_dotl(struct v9fs_session_info *v9ses, struct p9_fid *fid,
ed80fcfa 146 struct super_block *sb, int new)
53c06f4e 147{
53c06f4e 148 struct p9_stat_dotl *st;
5ffc0cb3 149 struct inode *inode = NULL;
53c06f4e 150
fd2421f5 151 st = p9_client_getattr_dotl(fid, P9_STATS_BASIC | P9_STATS_GEN);
53c06f4e
AK
152 if (IS_ERR(st))
153 return ERR_CAST(st);
154
ed80fcfa 155 inode = v9fs_qid_iget_dotl(sb, &st->qid, fid, st, new);
53c06f4e 156 kfree(st);
5ffc0cb3 157 return inode;
53c06f4e
AK
158}
159
f88657ce
AK
160struct dotl_openflag_map {
161 int open_flag;
162 int dotl_flag;
163};
164
165static int v9fs_mapped_dotl_flags(int flags)
166{
167 int i;
168 int rflags = 0;
169 struct dotl_openflag_map dotl_oflag_map[] = {
170 { O_CREAT, P9_DOTL_CREATE },
171 { O_EXCL, P9_DOTL_EXCL },
172 { O_NOCTTY, P9_DOTL_NOCTTY },
f88657ce
AK
173 { O_APPEND, P9_DOTL_APPEND },
174 { O_NONBLOCK, P9_DOTL_NONBLOCK },
175 { O_DSYNC, P9_DOTL_DSYNC },
176 { FASYNC, P9_DOTL_FASYNC },
177 { O_DIRECT, P9_DOTL_DIRECT },
178 { O_LARGEFILE, P9_DOTL_LARGEFILE },
179 { O_DIRECTORY, P9_DOTL_DIRECTORY },
180 { O_NOFOLLOW, P9_DOTL_NOFOLLOW },
181 { O_NOATIME, P9_DOTL_NOATIME },
182 { O_CLOEXEC, P9_DOTL_CLOEXEC },
183 { O_SYNC, P9_DOTL_SYNC},
184 };
185 for (i = 0; i < ARRAY_SIZE(dotl_oflag_map); i++) {
186 if (flags & dotl_oflag_map[i].open_flag)
187 rflags |= dotl_oflag_map[i].dotl_flag;
188 }
189 return rflags;
190}
191
192/**
193 * v9fs_open_to_dotl_flags- convert Linux specific open flags to
194 * plan 9 open flag.
195 * @flags: flags to convert
196 */
197int v9fs_open_to_dotl_flags(int flags)
198{
199 int rflags = 0;
200
201 /*
202 * We have same bits for P9_DOTL_READONLY, P9_DOTL_WRONLY
203 * and P9_DOTL_NOACCESS
204 */
205 rflags |= flags & O_ACCMODE;
206 rflags |= v9fs_mapped_dotl_flags(flags);
207
208 return rflags;
209}
210
53c06f4e
AK
211/**
212 * v9fs_vfs_create_dotl - VFS hook to create files for 9P2000.L protocol.
6c960e68 213 * @idmap: The user namespace of the mount
53c06f4e
AK
214 * @dir: directory inode that is being created
215 * @dentry: dentry that is being deleted
fd2916bd 216 * @omode: create permissions
bc868036 217 * @excl: True if the file must not yet exist
53c06f4e
AK
218 *
219 */
53c06f4e 220static int
6c960e68 221v9fs_vfs_create_dotl(struct mnt_idmap *idmap, struct inode *dir,
549c7297 222 struct dentry *dentry, umode_t omode, bool excl)
e43ae79c 223{
5ebb29be 224 return v9fs_vfs_mknod_dotl(idmap, dir, dentry, omode, 0);
e43ae79c
MS
225}
226
d9585277 227static int
e43ae79c 228v9fs_vfs_atomic_open_dotl(struct inode *dir, struct dentry *dentry,
6e195b0f 229 struct file *file, unsigned int flags, umode_t omode)
53c06f4e
AK
230{
231 int err = 0;
d4ef4e35 232 kgid_t gid;
d3fb6120 233 umode_t mode;
1543b4c5 234 int p9_omode = v9fs_open_to_dotl_flags(flags);
7880b43b 235 const unsigned char *name = NULL;
53c06f4e
AK
236 struct p9_qid qid;
237 struct inode *inode;
6b39f6d2 238 struct p9_fid *fid = NULL;
1543b4c5 239 struct p9_fid *dfid = NULL, *ofid = NULL;
6b39f6d2 240 struct v9fs_session_info *v9ses;
53c06f4e 241 struct posix_acl *pacl = NULL, *dacl = NULL;
e43ae79c 242 struct dentry *res = NULL;
53c06f4e 243
00699ad8 244 if (d_in_lookup(dentry)) {
00cd8dd3 245 res = v9fs_vfs_lookup(dir, dentry, 0);
e43ae79c 246 if (IS_ERR(res))
d9585277 247 return PTR_ERR(res);
e43ae79c
MS
248
249 if (res)
250 dentry = res;
251 }
252
253 /* Only creates */
2b0143b5 254 if (!(flags & O_CREAT) || d_really_is_positive(dentry))
b6f4bee0 255 return finish_no_open(file, res);
53c06f4e 256
e43ae79c
MS
257 v9ses = v9fs_inode2v9ses(dir);
258
7880b43b 259 name = dentry->d_name.name;
6e195b0f 260 p9_debug(P9_DEBUG_VFS, "name:%s flags:0x%x mode:0x%x\n",
5d385153 261 name, flags, omode);
53c06f4e 262
77d5a6b7 263 dfid = v9fs_parent_fid(dentry);
53c06f4e
AK
264 if (IS_ERR(dfid)) {
265 err = PTR_ERR(dfid);
5d385153 266 p9_debug(P9_DEBUG_VFS, "fid lookup failed %d\n", err);
d9585277 267 goto out;
53c06f4e
AK
268 }
269
270 /* clone a fid to use for creation */
7d50a29f 271 ofid = clone_fid(dfid);
53c06f4e
AK
272 if (IS_ERR(ofid)) {
273 err = PTR_ERR(ofid);
5d385153 274 p9_debug(P9_DEBUG_VFS, "p9_client_walk failed %d\n", err);
d9585277 275 goto out;
53c06f4e
AK
276 }
277
278 gid = v9fs_get_fsgid_for_create(dir);
279
280 mode = omode;
281 /* Update mode based on ACL value */
282 err = v9fs_acl_mode(dir, &mode, &dacl, &pacl);
283 if (err) {
1543b4c5 284 p9_debug(P9_DEBUG_VFS, "Failed to get acl values in create %d\n",
5d385153 285 err);
dafbe689 286 goto out;
53c06f4e 287 }
1543b4c5 288
4eb31178 289 if ((v9ses->cache & CACHE_WRITEBACK) && (p9_omode & P9_OWRITE)) {
21e26d5e 290 p9_omode = (p9_omode & ~P9_OWRITE) | P9_ORDWR;
1543b4c5
EVH
291 p9_debug(P9_DEBUG_CACHE,
292 "write-only file with writeback enabled, creating w/ O_RDWR\n");
293 }
294 err = p9_client_create_dotl(ofid, name, p9_omode, mode, gid, &qid);
53c06f4e 295 if (err < 0) {
1543b4c5 296 p9_debug(P9_DEBUG_VFS, "p9_client_open_dotl failed in create %d\n",
5d385153 297 err);
dafbe689 298 goto out;
53c06f4e 299 }
d28c61f0 300 v9fs_invalidate_inode_attr(dir);
53c06f4e 301
af7542fc
AK
302 /* instantiate inode and assign the unopened fid to the dentry */
303 fid = p9_client_walk(dfid, 1, &name, 1);
304 if (IS_ERR(fid)) {
305 err = PTR_ERR(fid);
5d385153 306 p9_debug(P9_DEBUG_VFS, "p9_client_walk failed %d\n", err);
dafbe689 307 goto out;
53c06f4e 308 }
ed80fcfa 309 inode = v9fs_get_new_inode_from_fid(v9ses, fid, dir->i_sb);
af7542fc
AK
310 if (IS_ERR(inode)) {
311 err = PTR_ERR(inode);
5d385153 312 p9_debug(P9_DEBUG_VFS, "inode creation failed %d\n", err);
dafbe689 313 goto out;
af7542fc 314 }
3592ac44
AV
315 /* Now set the ACL based on the default value */
316 v9fs_set_create_acl(inode, fid, dacl, pacl);
317
dafbe689 318 v9fs_fid_add(dentry, &fid);
5441ae5e 319 d_instantiate(dentry, inode);
af7542fc 320
af7542fc 321 /* Since we are opening a file, assign the open fid to the file */
be12af3e 322 err = finish_open(file, dentry, generic_file_open);
30d90494 323 if (err)
dafbe689 324 goto out;
30d90494 325 file->private_data = ofid;
344504e9 326#ifdef CONFIG_9P_FSCACHE
4eb31178 327 if (v9ses->cache & CACHE_FSCACHE) {
1543b4c5 328 struct v9fs_inode *v9inode = V9FS_I(inode);
24e42e32
DH
329 fscache_use_cookie(v9fs_inode_cookie(v9inode),
330 file->f_mode & FMODE_WRITE);
1543b4c5 331 }
344504e9 332#endif
1543b4c5 333 v9fs_fid_add_modes(ofid, v9ses->flags, v9ses->cache, flags);
dafbe689 334 v9fs_open_fid_add(inode, &ofid);
73a09dd9 335 file->f_mode |= FMODE_CREATED;
e43ae79c 336out:
dafbe689
DM
337 p9_fid_put(dfid);
338 p9_fid_put(ofid);
339 p9_fid_put(fid);
5fa6300a 340 v9fs_put_acl(dacl, pacl);
e43ae79c 341 dput(res);
d9585277 342 return err;
53c06f4e
AK
343}
344
345/**
346 * v9fs_vfs_mkdir_dotl - VFS mkdir hook to create a directory
c54bd91e 347 * @idmap: The idmap of the mount
53c06f4e
AK
348 * @dir: inode that is being unlinked
349 * @dentry: dentry that is being unlinked
fd2916bd 350 * @omode: mode for new directory
53c06f4e
AK
351 *
352 */
353
c54bd91e 354static int v9fs_vfs_mkdir_dotl(struct mnt_idmap *idmap,
549c7297
CB
355 struct inode *dir, struct dentry *dentry,
356 umode_t omode)
53c06f4e
AK
357{
358 int err;
359 struct v9fs_session_info *v9ses;
360 struct p9_fid *fid = NULL, *dfid = NULL;
d4ef4e35 361 kgid_t gid;
7880b43b 362 const unsigned char *name;
d3fb6120 363 umode_t mode;
53c06f4e
AK
364 struct inode *inode;
365 struct p9_qid qid;
53c06f4e
AK
366 struct posix_acl *dacl = NULL, *pacl = NULL;
367
4b8e9923 368 p9_debug(P9_DEBUG_VFS, "name %pd\n", dentry);
53c06f4e
AK
369 v9ses = v9fs_inode2v9ses(dir);
370
371 omode |= S_IFDIR;
372 if (dir->i_mode & S_ISGID)
373 omode |= S_ISGID;
374
77d5a6b7 375 dfid = v9fs_parent_fid(dentry);
53c06f4e
AK
376 if (IS_ERR(dfid)) {
377 err = PTR_ERR(dfid);
5d385153 378 p9_debug(P9_DEBUG_VFS, "fid lookup failed %d\n", err);
53c06f4e
AK
379 goto error;
380 }
381
382 gid = v9fs_get_fsgid_for_create(dir);
383 mode = omode;
384 /* Update mode based on ACL value */
385 err = v9fs_acl_mode(dir, &mode, &dacl, &pacl);
386 if (err) {
5d385153
JP
387 p9_debug(P9_DEBUG_VFS, "Failed to get acl values in mkdir %d\n",
388 err);
53c06f4e
AK
389 goto error;
390 }
7880b43b 391 name = dentry->d_name.name;
53c06f4e
AK
392 err = p9_client_mkdir_dotl(dfid, name, mode, gid, &qid);
393 if (err < 0)
394 goto error;
3592ac44
AV
395 fid = p9_client_walk(dfid, 1, &name, 1);
396 if (IS_ERR(fid)) {
397 err = PTR_ERR(fid);
398 p9_debug(P9_DEBUG_VFS, "p9_client_walk failed %d\n",
399 err);
3592ac44
AV
400 goto error;
401 }
402
53c06f4e 403 /* instantiate inode and assign the unopened fid to the dentry */
4eb31178 404 if (v9ses->cache & (CACHE_META|CACHE_LOOSE)) {
ed80fcfa 405 inode = v9fs_get_new_inode_from_fid(v9ses, fid, dir->i_sb);
53c06f4e
AK
406 if (IS_ERR(inode)) {
407 err = PTR_ERR(inode);
5d385153
JP
408 p9_debug(P9_DEBUG_VFS, "inode creation failed %d\n",
409 err);
53c06f4e
AK
410 goto error;
411 }
dafbe689 412 v9fs_fid_add(dentry, &fid);
3592ac44 413 v9fs_set_create_acl(inode, fid, dacl, pacl);
5441ae5e 414 d_instantiate(dentry, inode);
2ea03e1d 415 err = 0;
53c06f4e
AK
416 } else {
417 /*
418 * Not in cached mode. No need to populate
419 * inode with stat. We need to get an inode
420 * so that we can set the acl with dentry
421 */
45089142 422 inode = v9fs_get_inode(dir->i_sb, mode, 0);
53c06f4e
AK
423 if (IS_ERR(inode)) {
424 err = PTR_ERR(inode);
425 goto error;
426 }
3592ac44 427 v9fs_set_create_acl(inode, fid, dacl, pacl);
53c06f4e
AK
428 d_instantiate(dentry, inode);
429 }
b271ec47 430 inc_nlink(dir);
d28c61f0 431 v9fs_invalidate_inode_attr(dir);
53c06f4e 432error:
dafbe689 433 p9_fid_put(fid);
5fa6300a 434 v9fs_put_acl(dacl, pacl);
b48dbb99 435 p9_fid_put(dfid);
53c06f4e
AK
436 return err;
437}
438
439static int
b74d24f7 440v9fs_vfs_getattr_dotl(struct mnt_idmap *idmap,
549c7297
CB
441 const struct path *path, struct kstat *stat,
442 u32 request_mask, unsigned int flags)
53c06f4e 443{
a528d35e 444 struct dentry *dentry = path->dentry;
53c06f4e
AK
445 struct v9fs_session_info *v9ses;
446 struct p9_fid *fid;
d9bc0d11 447 struct inode *inode = d_inode(dentry);
53c06f4e
AK
448 struct p9_stat_dotl *st;
449
5d385153 450 p9_debug(P9_DEBUG_VFS, "dentry: %p\n", dentry);
42869c8a 451 v9ses = v9fs_dentry2v9ses(dentry);
4eb31178 452 if (v9ses->cache & (CACHE_META|CACHE_LOOSE)) {
0d72b928 453 generic_fillattr(&nop_mnt_idmap, request_mask, inode, stat);
a1211908 454 return 0;
1543b4c5 455 } else if (v9ses->cache) {
d9bc0d11
EVH
456 if (S_ISREG(inode->i_mode)) {
457 int retval = filemap_fdatawrite(inode->i_mapping);
458
459 if (retval)
460 p9_debug(P9_DEBUG_ERROR,
461 "flushing writeback during getattr returned %d\n", retval);
462 }
a1211908 463 }
53c06f4e
AK
464 fid = v9fs_fid_lookup(dentry);
465 if (IS_ERR(fid))
466 return PTR_ERR(fid);
467
468 /* Ask for all the fields in stat structure. Server will return
469 * whatever it supports
470 */
471
472 st = p9_client_getattr_dotl(fid, P9_STATS_ALL);
b48dbb99 473 p9_fid_put(fid);
53c06f4e
AK
474 if (IS_ERR(st))
475 return PTR_ERR(st);
476
5e3cc1ee 477 v9fs_stat2inode_dotl(st, d_inode(dentry), 0);
0d72b928 478 generic_fillattr(&nop_mnt_idmap, request_mask, d_inode(dentry), stat);
53c06f4e
AK
479 /* Change block size to what the server returned */
480 stat->blksize = st->st_blksize;
481
482 kfree(st);
483 return 0;
484}
485
f766619d
AK
486/*
487 * Attribute flags.
488 */
489#define P9_ATTR_MODE (1 << 0)
490#define P9_ATTR_UID (1 << 1)
491#define P9_ATTR_GID (1 << 2)
492#define P9_ATTR_SIZE (1 << 3)
493#define P9_ATTR_ATIME (1 << 4)
494#define P9_ATTR_MTIME (1 << 5)
495#define P9_ATTR_CTIME (1 << 6)
496#define P9_ATTR_ATIME_SET (1 << 7)
497#define P9_ATTR_MTIME_SET (1 << 8)
498
499struct dotl_iattr_map {
500 int iattr_valid;
501 int p9_iattr_valid;
502};
503
504static int v9fs_mapped_iattr_valid(int iattr_valid)
505{
506 int i;
507 int p9_iattr_valid = 0;
508 struct dotl_iattr_map dotl_iattr_map[] = {
509 { ATTR_MODE, P9_ATTR_MODE },
510 { ATTR_UID, P9_ATTR_UID },
511 { ATTR_GID, P9_ATTR_GID },
512 { ATTR_SIZE, P9_ATTR_SIZE },
513 { ATTR_ATIME, P9_ATTR_ATIME },
514 { ATTR_MTIME, P9_ATTR_MTIME },
515 { ATTR_CTIME, P9_ATTR_CTIME },
516 { ATTR_ATIME_SET, P9_ATTR_ATIME_SET },
517 { ATTR_MTIME_SET, P9_ATTR_MTIME_SET },
518 };
519 for (i = 0; i < ARRAY_SIZE(dotl_iattr_map); i++) {
520 if (iattr_valid & dotl_iattr_map[i].iattr_valid)
521 p9_iattr_valid |= dotl_iattr_map[i].p9_iattr_valid;
522 }
523 return p9_iattr_valid;
524}
525
53c06f4e
AK
526/**
527 * v9fs_vfs_setattr_dotl - set file metadata
c1632a0f 528 * @idmap: idmap of the mount
53c06f4e
AK
529 * @dentry: file whose metadata to set
530 * @iattr: metadata assignment structure
531 *
532 */
533
c1632a0f 534int v9fs_vfs_setattr_dotl(struct mnt_idmap *idmap,
549c7297 535 struct dentry *dentry, struct iattr *iattr)
53c06f4e 536{
6636b6dc 537 int retval, use_dentry = 0;
d9bc0d11 538 struct inode *inode = d_inode(dentry);
4eb31178 539 struct v9fs_session_info __maybe_unused *v9ses;
66246641 540 struct p9_fid *fid = NULL;
3cb6ee99
CB
541 struct p9_iattr_dotl p9attr = {
542 .uid = INVALID_UID,
543 .gid = INVALID_GID,
544 };
53c06f4e 545
5d385153 546 p9_debug(P9_DEBUG_VFS, "\n");
53c06f4e 547
c1632a0f 548 retval = setattr_prepare(&nop_mnt_idmap, dentry, iattr);
53c06f4e
AK
549 if (retval)
550 return retval;
551
d9bc0d11
EVH
552 v9ses = v9fs_dentry2v9ses(dentry);
553
f766619d 554 p9attr.valid = v9fs_mapped_iattr_valid(iattr->ia_valid);
3cb6ee99
CB
555 if (iattr->ia_valid & ATTR_MODE)
556 p9attr.mode = iattr->ia_mode;
557 if (iattr->ia_valid & ATTR_UID)
558 p9attr.uid = iattr->ia_uid;
559 if (iattr->ia_valid & ATTR_GID)
560 p9attr.gid = iattr->ia_gid;
561 if (iattr->ia_valid & ATTR_SIZE)
562 p9attr.size = iattr->ia_size;
563 if (iattr->ia_valid & ATTR_ATIME_SET) {
564 p9attr.atime_sec = iattr->ia_atime.tv_sec;
565 p9attr.atime_nsec = iattr->ia_atime.tv_nsec;
566 }
567 if (iattr->ia_valid & ATTR_MTIME_SET) {
568 p9attr.mtime_sec = iattr->ia_mtime.tv_sec;
569 p9attr.mtime_nsec = iattr->ia_mtime.tv_nsec;
570 }
53c06f4e 571
66246641
JW
572 if (iattr->ia_valid & ATTR_FILE) {
573 fid = iattr->ia_file->private_data;
574 WARN_ON(!fid);
575 }
6636b6dc 576 if (!fid) {
66246641 577 fid = v9fs_fid_lookup(dentry);
6636b6dc
JW
578 use_dentry = 1;
579 }
53c06f4e
AK
580 if (IS_ERR(fid))
581 return PTR_ERR(fid);
582
3dc5436a 583 /* Write all dirty data */
d9bc0d11
EVH
584 if (S_ISREG(inode->i_mode)) {
585 retval = filemap_fdatawrite(inode->i_mapping);
586 if (retval < 0)
587 p9_debug(P9_DEBUG_ERROR,
588 "Flushing file prior to setattr failed: %d\n", retval);
589 }
3dc5436a 590
f10fc50f 591 retval = p9_client_setattr(fid, &p9attr);
6636b6dc
JW
592 if (retval < 0) {
593 if (use_dentry)
b48dbb99 594 p9_fid_put(fid);
f10fc50f 595 return retval;
6636b6dc 596 }
53c06f4e 597
d9bc0d11
EVH
598 if ((iattr->ia_valid & ATTR_SIZE) && iattr->ia_size !=
599 i_size_read(inode)) {
be308f07 600 truncate_setsize(inode, iattr->ia_size);
80105ed2 601 netfs_resize_file(netfs_inode(inode), iattr->ia_size, true);
1543b4c5 602
4eb31178
EVH
603#ifdef CONFIG_9P_FSCACHE
604 if (v9ses->cache & CACHE_FSCACHE)
605 fscache_resize_cookie(v9fs_inode_cookie(V9FS_I(inode)),
606 iattr->ia_size);
607#endif
d9bc0d11 608 }
059c138b 609
be308f07 610 v9fs_invalidate_inode_attr(inode);
c1632a0f 611 setattr_copy(&nop_mnt_idmap, inode, iattr);
be308f07 612 mark_inode_dirty(inode);
53c06f4e
AK
613 if (iattr->ia_valid & ATTR_MODE) {
614 /* We also want to update ACL when we update mode bits */
be308f07 615 retval = v9fs_acl_chmod(inode, fid);
6636b6dc
JW
616 if (retval < 0) {
617 if (use_dentry)
b48dbb99 618 p9_fid_put(fid);
53c06f4e 619 return retval;
6636b6dc 620 }
53c06f4e 621 }
6636b6dc 622 if (use_dentry)
b48dbb99 623 p9_fid_put(fid);
6636b6dc 624
53c06f4e
AK
625 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
5e3cc1ee 632 * @flags: ctrl flags (e.g. V9FS_STAT2INODE_KEEP_ISIZE)
53c06f4e
AK
633 *
634 */
635
636void
5e3cc1ee
HT
637v9fs_stat2inode_dotl(struct p9_stat_dotl *stat, struct inode *inode,
638 unsigned int flags)
53c06f4e 639{
3eda0de6 640 umode_t mode;
b3cbea03 641 struct v9fs_inode *v9inode = V9FS_I(inode);
53c06f4e
AK
642
643 if ((stat->st_result_mask & P9_STATS_BASIC) == P9_STATS_BASIC) {
d0242a3a
JL
644 inode_set_atime(inode, stat->st_atime_sec,
645 stat->st_atime_nsec);
646 inode_set_mtime(inode, stat->st_mtime_sec,
647 stat->st_mtime_nsec);
4f871800
JL
648 inode_set_ctime(inode, stat->st_ctime_sec,
649 stat->st_ctime_nsec);
53c06f4e
AK
650 inode->i_uid = stat->st_uid;
651 inode->i_gid = stat->st_gid;
bfe86848 652 set_nlink(inode, stat->st_nlink);
53c06f4e 653
45089142
AK
654 mode = stat->st_mode & S_IALLUGO;
655 mode |= inode->i_mode & ~S_IALLUGO;
656 inode->i_mode = mode;
53c06f4e 657
80105ed2 658 v9inode->netfs.remote_i_size = stat->st_size;
5e3cc1ee
HT
659 if (!(flags & V9FS_STAT2INODE_KEEP_ISIZE))
660 v9fs_i_size_write(inode, stat->st_size);
53c06f4e
AK
661 inode->i_blocks = stat->st_blocks;
662 } else {
663 if (stat->st_result_mask & P9_STATS_ATIME) {
d0242a3a
JL
664 inode_set_atime(inode, stat->st_atime_sec,
665 stat->st_atime_nsec);
53c06f4e
AK
666 }
667 if (stat->st_result_mask & P9_STATS_MTIME) {
d0242a3a
JL
668 inode_set_mtime(inode, stat->st_mtime_sec,
669 stat->st_mtime_nsec);
53c06f4e
AK
670 }
671 if (stat->st_result_mask & P9_STATS_CTIME) {
4f871800
JL
672 inode_set_ctime(inode, stat->st_ctime_sec,
673 stat->st_ctime_nsec);
53c06f4e
AK
674 }
675 if (stat->st_result_mask & P9_STATS_UID)
676 inode->i_uid = stat->st_uid;
677 if (stat->st_result_mask & P9_STATS_GID)
678 inode->i_gid = stat->st_gid;
679 if (stat->st_result_mask & P9_STATS_NLINK)
bfe86848 680 set_nlink(inode, stat->st_nlink);
53c06f4e 681 if (stat->st_result_mask & P9_STATS_MODE) {
b577d0cd
AV
682 mode = stat->st_mode & S_IALLUGO;
683 mode |= inode->i_mode & ~S_IALLUGO;
684 inode->i_mode = mode;
53c06f4e 685 }
5e3cc1ee 686 if (!(flags & V9FS_STAT2INODE_KEEP_ISIZE) &&
80105ed2
DH
687 stat->st_result_mask & P9_STATS_SIZE) {
688 v9inode->netfs.remote_i_size = stat->st_size;
5e3cc1ee 689 v9fs_i_size_write(inode, stat->st_size);
80105ed2 690 }
53c06f4e
AK
691 if (stat->st_result_mask & P9_STATS_BLOCKS)
692 inode->i_blocks = stat->st_blocks;
693 }
694 if (stat->st_result_mask & P9_STATS_GEN)
fd2421f5 695 inode->i_generation = stat->st_gen;
53c06f4e
AK
696
697 /* Currently we don't support P9_STATS_BTIME and P9_STATS_DATA_VERSION
698 * because the inode structure does not have fields for them.
699 */
b3cbea03 700 v9inode->cache_validity &= ~V9FS_INO_INVALID_ATTR;
53c06f4e
AK
701}
702
703static int
7a77db95 704v9fs_vfs_symlink_dotl(struct mnt_idmap *idmap, struct inode *dir,
549c7297 705 struct dentry *dentry, const char *symname)
53c06f4e 706{
53c06f4e 707 int err;
d4ef4e35 708 kgid_t gid;
7880b43b 709 const unsigned char *name;
d28c61f0
AK
710 struct p9_qid qid;
711 struct inode *inode;
712 struct p9_fid *dfid;
713 struct p9_fid *fid = NULL;
714 struct v9fs_session_info *v9ses;
53c06f4e 715
7880b43b 716 name = dentry->d_name.name;
5d385153 717 p9_debug(P9_DEBUG_VFS, "%lu,%s,%s\n", dir->i_ino, name, symname);
53c06f4e
AK
718 v9ses = v9fs_inode2v9ses(dir);
719
77d5a6b7 720 dfid = v9fs_parent_fid(dentry);
53c06f4e
AK
721 if (IS_ERR(dfid)) {
722 err = PTR_ERR(dfid);
5d385153 723 p9_debug(P9_DEBUG_VFS, "fid lookup failed %d\n", err);
53c06f4e
AK
724 return err;
725 }
726
727 gid = v9fs_get_fsgid_for_create(dir);
728
729 /* Server doesn't alter fid on TSYMLINK. Hence no need to clone it. */
7880b43b 730 err = p9_client_symlink(dfid, name, symname, gid, &qid);
53c06f4e
AK
731
732 if (err < 0) {
5d385153 733 p9_debug(P9_DEBUG_VFS, "p9_client_symlink failed %d\n", err);
53c06f4e
AK
734 goto error;
735 }
736
d28c61f0 737 v9fs_invalidate_inode_attr(dir);
4eb31178 738 if (v9ses->cache & (CACHE_META|CACHE_LOOSE)) {
53c06f4e
AK
739 /* Now walk from the parent so we can get an unopened fid. */
740 fid = p9_client_walk(dfid, 1, &name, 1);
741 if (IS_ERR(fid)) {
742 err = PTR_ERR(fid);
5d385153
JP
743 p9_debug(P9_DEBUG_VFS, "p9_client_walk failed %d\n",
744 err);
53c06f4e
AK
745 goto error;
746 }
747
748 /* instantiate inode and assign the unopened fid to dentry */
ed80fcfa 749 inode = v9fs_get_new_inode_from_fid(v9ses, fid, dir->i_sb);
53c06f4e
AK
750 if (IS_ERR(inode)) {
751 err = PTR_ERR(inode);
5d385153
JP
752 p9_debug(P9_DEBUG_VFS, "inode creation failed %d\n",
753 err);
53c06f4e
AK
754 goto error;
755 }
dafbe689 756 v9fs_fid_add(dentry, &fid);
5441ae5e 757 d_instantiate(dentry, inode);
2ea03e1d 758 err = 0;
53c06f4e
AK
759 } else {
760 /* Not in cached mode. No need to populate inode with stat */
45089142 761 inode = v9fs_get_inode(dir->i_sb, S_IFLNK, 0);
53c06f4e
AK
762 if (IS_ERR(inode)) {
763 err = PTR_ERR(inode);
764 goto error;
765 }
53c06f4e
AK
766 d_instantiate(dentry, inode);
767 }
768
769error:
dafbe689 770 p9_fid_put(fid);
b48dbb99 771 p9_fid_put(dfid);
53c06f4e
AK
772 return err;
773}
774
775/**
776 * v9fs_vfs_link_dotl - create a hardlink for dotl
777 * @old_dentry: dentry for file to link to
778 * @dir: inode destination for new link
779 * @dentry: dentry for link
780 *
781 */
782
783static int
784v9fs_vfs_link_dotl(struct dentry *old_dentry, struct inode *dir,
785 struct dentry *dentry)
786{
787 int err;
d28c61f0
AK
788 struct p9_fid *dfid, *oldfid;
789 struct v9fs_session_info *v9ses;
53c06f4e 790
4b8e9923
AV
791 p9_debug(P9_DEBUG_VFS, "dir ino: %lu, old_name: %pd, new_name: %pd\n",
792 dir->i_ino, old_dentry, dentry);
53c06f4e
AK
793
794 v9ses = v9fs_inode2v9ses(dir);
77d5a6b7 795 dfid = v9fs_parent_fid(dentry);
53c06f4e
AK
796 if (IS_ERR(dfid))
797 return PTR_ERR(dfid);
798
799 oldfid = v9fs_fid_lookup(old_dentry);
6636b6dc 800 if (IS_ERR(oldfid)) {
b48dbb99 801 p9_fid_put(dfid);
53c06f4e 802 return PTR_ERR(oldfid);
6636b6dc 803 }
53c06f4e 804
7880b43b 805 err = p9_client_link(dfid, oldfid, dentry->d_name.name);
53c06f4e 806
b48dbb99
DM
807 p9_fid_put(dfid);
808 p9_fid_put(oldfid);
53c06f4e 809 if (err < 0) {
5d385153 810 p9_debug(P9_DEBUG_VFS, "p9_client_link failed %d\n", err);
53c06f4e
AK
811 return err;
812 }
813
d28c61f0 814 v9fs_invalidate_inode_attr(dir);
4eb31178 815 if (v9ses->cache & (CACHE_META|CACHE_LOOSE)) {
53c06f4e
AK
816 /* Get the latest stat info from server. */
817 struct p9_fid *fid;
6e195b0f 818
53c06f4e
AK
819 fid = v9fs_fid_lookup(old_dentry);
820 if (IS_ERR(fid))
821 return PTR_ERR(fid);
822
2b0143b5 823 v9fs_refresh_inode_dotl(fid, d_inode(old_dentry));
b48dbb99 824 p9_fid_put(fid);
53c06f4e 825 }
2b0143b5
DH
826 ihold(d_inode(old_dentry));
827 d_instantiate(dentry, d_inode(old_dentry));
53c06f4e
AK
828
829 return err;
830}
831
832/**
833 * v9fs_vfs_mknod_dotl - create a special file
5ebb29be 834 * @idmap: The idmap of the mount
53c06f4e
AK
835 * @dir: inode destination for new link
836 * @dentry: dentry for file
fd2916bd 837 * @omode: mode for creation
53c06f4e
AK
838 * @rdev: device associated with special file
839 *
840 */
841static int
5ebb29be 842v9fs_vfs_mknod_dotl(struct mnt_idmap *idmap, struct inode *dir,
549c7297 843 struct dentry *dentry, umode_t omode, dev_t rdev)
53c06f4e
AK
844{
845 int err;
d4ef4e35 846 kgid_t gid;
7880b43b 847 const unsigned char *name;
d3fb6120 848 umode_t mode;
53c06f4e
AK
849 struct v9fs_session_info *v9ses;
850 struct p9_fid *fid = NULL, *dfid = NULL;
851 struct inode *inode;
53c06f4e 852 struct p9_qid qid;
53c06f4e
AK
853 struct posix_acl *dacl = NULL, *pacl = NULL;
854
6e195b0f 855 p9_debug(P9_DEBUG_VFS, " %lu,%pd mode: %x MAJOR: %u MINOR: %u\n",
a455589f 856 dir->i_ino, dentry, omode,
5d385153 857 MAJOR(rdev), MINOR(rdev));
53c06f4e 858
53c06f4e 859 v9ses = v9fs_inode2v9ses(dir);
77d5a6b7 860 dfid = v9fs_parent_fid(dentry);
53c06f4e
AK
861 if (IS_ERR(dfid)) {
862 err = PTR_ERR(dfid);
5d385153 863 p9_debug(P9_DEBUG_VFS, "fid lookup failed %d\n", err);
53c06f4e
AK
864 goto error;
865 }
866
867 gid = v9fs_get_fsgid_for_create(dir);
868 mode = omode;
869 /* Update mode based on ACL value */
870 err = v9fs_acl_mode(dir, &mode, &dacl, &pacl);
871 if (err) {
5d385153
JP
872 p9_debug(P9_DEBUG_VFS, "Failed to get acl values in mknod %d\n",
873 err);
53c06f4e
AK
874 goto error;
875 }
7880b43b 876 name = dentry->d_name.name;
53c06f4e
AK
877
878 err = p9_client_mknod_dotl(dfid, name, mode, rdev, gid, &qid);
879 if (err < 0)
880 goto error;
881
d28c61f0 882 v9fs_invalidate_inode_attr(dir);
3592ac44
AV
883 fid = p9_client_walk(dfid, 1, &name, 1);
884 if (IS_ERR(fid)) {
885 err = PTR_ERR(fid);
886 p9_debug(P9_DEBUG_VFS, "p9_client_walk failed %d\n",
887 err);
3592ac44
AV
888 goto error;
889 }
890
53c06f4e 891 /* instantiate inode and assign the unopened fid to the dentry */
4eb31178 892 if (v9ses->cache & (CACHE_META|CACHE_LOOSE)) {
ed80fcfa 893 inode = v9fs_get_new_inode_from_fid(v9ses, fid, dir->i_sb);
53c06f4e
AK
894 if (IS_ERR(inode)) {
895 err = PTR_ERR(inode);
5d385153
JP
896 p9_debug(P9_DEBUG_VFS, "inode creation failed %d\n",
897 err);
53c06f4e
AK
898 goto error;
899 }
3592ac44 900 v9fs_set_create_acl(inode, fid, dacl, pacl);
dafbe689 901 v9fs_fid_add(dentry, &fid);
5441ae5e 902 d_instantiate(dentry, inode);
2ea03e1d 903 err = 0;
53c06f4e
AK
904 } else {
905 /*
906 * Not in cached mode. No need to populate inode with stat.
907 * socket syscall returns a fd, so we need instantiate
908 */
45089142 909 inode = v9fs_get_inode(dir->i_sb, mode, rdev);
53c06f4e
AK
910 if (IS_ERR(inode)) {
911 err = PTR_ERR(inode);
912 goto error;
913 }
3592ac44 914 v9fs_set_create_acl(inode, fid, dacl, pacl);
53c06f4e
AK
915 d_instantiate(dentry, inode);
916 }
53c06f4e 917error:
dafbe689 918 p9_fid_put(fid);
5fa6300a 919 v9fs_put_acl(dacl, pacl);
b48dbb99 920 p9_fid_put(dfid);
6636b6dc 921
53c06f4e
AK
922 return err;
923}
924
53c06f4e 925/**
6b255391 926 * v9fs_vfs_get_link_dotl - follow a symlink path
53c06f4e 927 * @dentry: dentry for symlink
6b255391 928 * @inode: inode for symlink
fceef393 929 * @done: destructor for return value
53c06f4e
AK
930 */
931
680baacb 932static const char *
6b255391 933v9fs_vfs_get_link_dotl(struct dentry *dentry,
fceef393
AV
934 struct inode *inode,
935 struct delayed_call *done)
53c06f4e 936{
6b255391 937 struct p9_fid *fid;
31b6ceac 938 char *target;
90e4fc88 939 int retval;
53c06f4e 940
6b255391
AV
941 if (!dentry)
942 return ERR_PTR(-ECHILD);
943
4b8e9923 944 p9_debug(P9_DEBUG_VFS, "%pd\n", dentry);
53c06f4e 945
6b255391 946 fid = v9fs_fid_lookup(dentry);
90e4fc88
AV
947 if (IS_ERR(fid))
948 return ERR_CAST(fid);
31b6ceac 949 retval = p9_client_readlink(fid, &target);
b48dbb99 950 p9_fid_put(fid);
90e4fc88
AV
951 if (retval)
952 return ERR_PTR(retval);
fceef393
AV
953 set_delayed_call(done, kfree_link, target);
954 return target;
53c06f4e
AK
955}
956
b3cbea03
AK
957int v9fs_refresh_inode_dotl(struct p9_fid *fid, struct inode *inode)
958{
b3cbea03
AK
959 struct p9_stat_dotl *st;
960 struct v9fs_session_info *v9ses;
5e3cc1ee 961 unsigned int flags;
b3cbea03
AK
962
963 v9ses = v9fs_inode2v9ses(inode);
964 st = p9_client_getattr_dotl(fid, P9_STATS_ALL);
965 if (IS_ERR(st))
966 return PTR_ERR(st);
45089142
AK
967 /*
968 * Don't update inode if the file type is different
969 */
6e3e2c43 970 if (inode_wrong_type(inode, st->st_mode))
45089142 971 goto out;
b3cbea03 972
b3cbea03
AK
973 /*
974 * We don't want to refresh inode->i_size,
975 * because we may have cached data
976 */
4eb31178 977 flags = (v9ses->cache & CACHE_LOOSE) ?
5e3cc1ee
HT
978 V9FS_STAT2INODE_KEEP_ISIZE : 0;
979 v9fs_stat2inode_dotl(st, inode, flags);
45089142 980out:
b3cbea03
AK
981 kfree(st);
982 return 0;
983}
984
53c06f4e
AK
985const struct inode_operations v9fs_dir_inode_operations_dotl = {
986 .create = v9fs_vfs_create_dotl,
e43ae79c 987 .atomic_open = v9fs_vfs_atomic_open_dotl,
53c06f4e
AK
988 .lookup = v9fs_vfs_lookup,
989 .link = v9fs_vfs_link_dotl,
990 .symlink = v9fs_vfs_symlink_dotl,
991 .unlink = v9fs_vfs_unlink,
992 .mkdir = v9fs_vfs_mkdir_dotl,
993 .rmdir = v9fs_vfs_rmdir,
994 .mknod = v9fs_vfs_mknod_dotl,
995 .rename = v9fs_vfs_rename,
996 .getattr = v9fs_vfs_getattr_dotl,
997 .setattr = v9fs_vfs_setattr_dotl,
53c06f4e 998 .listxattr = v9fs_listxattr,
6cd4d4e8
CB
999 .get_inode_acl = v9fs_iop_get_inode_acl,
1000 .get_acl = v9fs_iop_get_acl,
079da629 1001 .set_acl = v9fs_iop_set_acl,
53c06f4e
AK
1002};
1003
1004const struct inode_operations v9fs_file_inode_operations_dotl = {
1005 .getattr = v9fs_vfs_getattr_dotl,
1006 .setattr = v9fs_vfs_setattr_dotl,
53c06f4e 1007 .listxattr = v9fs_listxattr,
6cd4d4e8
CB
1008 .get_inode_acl = v9fs_iop_get_inode_acl,
1009 .get_acl = v9fs_iop_get_acl,
079da629 1010 .set_acl = v9fs_iop_set_acl,
53c06f4e
AK
1011};
1012
1013const struct inode_operations v9fs_symlink_inode_operations_dotl = {
6b255391 1014 .get_link = v9fs_vfs_get_link_dotl,
53c06f4e
AK
1015 .getattr = v9fs_vfs_getattr_dotl,
1016 .setattr = v9fs_vfs_setattr_dotl,
53c06f4e
AK
1017 .listxattr = v9fs_listxattr,
1018};