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