| 1 |
// SPDX-License-Identifier: GPL-2.0-only |
| 2 |
/* |
| 3 |
* This file contains vfs inode ops for the 9P2000 protocol. |
| 4 |
* |
| 5 |
* Copyright (C) 2004 by Eric Van Hensbergen <[email protected]> |
| 6 |
* Copyright (C) 2002 by Ron Minnich <[email protected]> |
| 7 |
*/ |
| 8 |
|
| 9 |
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 10 |
|
| 11 |
#include <linux/module.h> |
| 12 |
#include <linux/errno.h> |
| 13 |
#include <linux/fs.h> |
| 14 |
#include <linux/file.h> |
| 15 |
#include <linux/pagemap.h> |
| 16 |
#include <linux/stat.h> |
| 17 |
#include <linux/string.h> |
| 18 |
#include <linux/namei.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 |
|
| 33 |
static const struct inode_operations v9fs_dir_inode_operations; |
| 34 |
static const struct inode_operations v9fs_dir_inode_operations_dotu; |
| 35 |
static const struct inode_operations v9fs_file_inode_operations; |
| 36 |
static const struct inode_operations v9fs_symlink_inode_operations; |
| 37 |
|
| 38 |
/** |
| 39 |
* unixmode2p9mode - convert unix mode bits to plan 9 |
| 40 |
* @v9ses: v9fs session information |
| 41 |
* @mode: mode to convert |
| 42 |
* |
| 43 |
*/ |
| 44 |
|
| 45 |
static u32 unixmode2p9mode(struct v9fs_session_info *v9ses, umode_t mode) |
| 46 |
{ |
| 47 |
int res; |
| 48 |
|
| 49 |
res = mode & 0777; |
| 50 |
if (S_ISDIR(mode)) |
| 51 |
res |= P9_DMDIR; |
| 52 |
if (v9fs_proto_dotu(v9ses)) { |
| 53 |
if (v9ses->nodev == 0) { |
| 54 |
if (S_ISSOCK(mode)) |
| 55 |
res |= P9_DMSOCKET; |
| 56 |
if (S_ISFIFO(mode)) |
| 57 |
res |= P9_DMNAMEDPIPE; |
| 58 |
if (S_ISBLK(mode)) |
| 59 |
res |= P9_DMDEVICE; |
| 60 |
if (S_ISCHR(mode)) |
| 61 |
res |= P9_DMDEVICE; |
| 62 |
} |
| 63 |
|
| 64 |
if ((mode & S_ISUID) == S_ISUID) |
| 65 |
res |= P9_DMSETUID; |
| 66 |
if ((mode & S_ISGID) == S_ISGID) |
| 67 |
res |= P9_DMSETGID; |
| 68 |
if ((mode & S_ISVTX) == S_ISVTX) |
| 69 |
res |= P9_DMSETVTX; |
| 70 |
} |
| 71 |
return res; |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* p9mode2perm- convert plan9 mode bits to unix permission bits |
| 76 |
* @v9ses: v9fs session information |
| 77 |
* @stat: p9_wstat from which mode need to be derived |
| 78 |
* |
| 79 |
*/ |
| 80 |
static int p9mode2perm(struct v9fs_session_info *v9ses, |
| 81 |
struct p9_wstat *stat) |
| 82 |
{ |
| 83 |
int res; |
| 84 |
int mode = stat->mode; |
| 85 |
|
| 86 |
res = mode & 0777; /* S_IRWXUGO */ |
| 87 |
if (v9fs_proto_dotu(v9ses)) { |
| 88 |
if ((mode & P9_DMSETUID) == P9_DMSETUID) |
| 89 |
res |= S_ISUID; |
| 90 |
|
| 91 |
if ((mode & P9_DMSETGID) == P9_DMSETGID) |
| 92 |
res |= S_ISGID; |
| 93 |
|
| 94 |
if ((mode & P9_DMSETVTX) == P9_DMSETVTX) |
| 95 |
res |= S_ISVTX; |
| 96 |
} |
| 97 |
return res; |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* p9mode2unixmode- convert plan9 mode bits to unix mode bits |
| 102 |
* @v9ses: v9fs session information |
| 103 |
* @stat: p9_wstat from which mode need to be derived |
| 104 |
* @rdev: major number, minor number in case of device files. |
| 105 |
* |
| 106 |
*/ |
| 107 |
static umode_t p9mode2unixmode(struct v9fs_session_info *v9ses, |
| 108 |
struct p9_wstat *stat, dev_t *rdev) |
| 109 |
{ |
| 110 |
int res, r; |
| 111 |
u32 mode = stat->mode; |
| 112 |
|
| 113 |
*rdev = 0; |
| 114 |
res = p9mode2perm(v9ses, stat); |
| 115 |
|
| 116 |
if ((mode & P9_DMDIR) == P9_DMDIR) |
| 117 |
res |= S_IFDIR; |
| 118 |
else if ((mode & P9_DMSYMLINK) && (v9fs_proto_dotu(v9ses))) |
| 119 |
res |= S_IFLNK; |
| 120 |
else if ((mode & P9_DMSOCKET) && (v9fs_proto_dotu(v9ses)) |
| 121 |
&& (v9ses->nodev == 0)) |
| 122 |
res |= S_IFSOCK; |
| 123 |
else if ((mode & P9_DMNAMEDPIPE) && (v9fs_proto_dotu(v9ses)) |
| 124 |
&& (v9ses->nodev == 0)) |
| 125 |
res |= S_IFIFO; |
| 126 |
else if ((mode & P9_DMDEVICE) && (v9fs_proto_dotu(v9ses)) |
| 127 |
&& (v9ses->nodev == 0)) { |
| 128 |
char type = 0; |
| 129 |
int major = -1, minor = -1; |
| 130 |
|
| 131 |
r = sscanf(stat->extension, "%c %i %i", &type, &major, &minor); |
| 132 |
if (r != 3) { |
| 133 |
p9_debug(P9_DEBUG_ERROR, |
| 134 |
"invalid device string, umode will be bogus: %s\n", |
| 135 |
stat->extension); |
| 136 |
return res; |
| 137 |
} |
| 138 |
switch (type) { |
| 139 |
case 'c': |
| 140 |
res |= S_IFCHR; |
| 141 |
break; |
| 142 |
case 'b': |
| 143 |
res |= S_IFBLK; |
| 144 |
break; |
| 145 |
default: |
| 146 |
p9_debug(P9_DEBUG_ERROR, "Unknown special type %c %s\n", |
| 147 |
type, stat->extension); |
| 148 |
} |
| 149 |
*rdev = MKDEV(major, minor); |
| 150 |
} else |
| 151 |
res |= S_IFREG; |
| 152 |
|
| 153 |
return res; |
| 154 |
} |
| 155 |
|
| 156 |
/** |
| 157 |
* v9fs_uflags2omode- convert posix open flags to plan 9 mode bits |
| 158 |
* @uflags: flags to convert |
| 159 |
* @extended: if .u extensions are active |
| 160 |
*/ |
| 161 |
|
| 162 |
int v9fs_uflags2omode(int uflags, int extended) |
| 163 |
{ |
| 164 |
int ret; |
| 165 |
|
| 166 |
switch (uflags&3) { |
| 167 |
default: |
| 168 |
case O_RDONLY: |
| 169 |
ret = P9_OREAD; |
| 170 |
break; |
| 171 |
|
| 172 |
case O_WRONLY: |
| 173 |
ret = P9_OWRITE; |
| 174 |
break; |
| 175 |
|
| 176 |
case O_RDWR: |
| 177 |
ret = P9_ORDWR; |
| 178 |
break; |
| 179 |
} |
| 180 |
|
| 181 |
if (uflags & O_TRUNC) |
| 182 |
ret |= P9_OTRUNC; |
| 183 |
|
| 184 |
if (extended) { |
| 185 |
if (uflags & O_EXCL) |
| 186 |
ret |= P9_OEXCL; |
| 187 |
|
| 188 |
if (uflags & O_APPEND) |
| 189 |
ret |= P9_OAPPEND; |
| 190 |
} |
| 191 |
|
| 192 |
return ret; |
| 193 |
} |
| 194 |
|
| 195 |
/** |
| 196 |
* v9fs_blank_wstat - helper function to setup a 9P stat structure |
| 197 |
* @wstat: structure to initialize |
| 198 |
* |
| 199 |
*/ |
| 200 |
|
| 201 |
void |
| 202 |
v9fs_blank_wstat(struct p9_wstat *wstat) |
| 203 |
{ |
| 204 |
wstat->type = ~0; |
| 205 |
wstat->dev = ~0; |
| 206 |
wstat->qid.type = ~0; |
| 207 |
wstat->qid.version = ~0; |
| 208 |
*((long long *)&wstat->qid.path) = ~0; |
| 209 |
wstat->mode = ~0; |
| 210 |
wstat->atime = ~0; |
| 211 |
wstat->mtime = ~0; |
| 212 |
wstat->length = ~0; |
| 213 |
wstat->name = NULL; |
| 214 |
wstat->uid = NULL; |
| 215 |
wstat->gid = NULL; |
| 216 |
wstat->muid = NULL; |
| 217 |
wstat->n_uid = INVALID_UID; |
| 218 |
wstat->n_gid = INVALID_GID; |
| 219 |
wstat->n_muid = INVALID_UID; |
| 220 |
wstat->extension = NULL; |
| 221 |
} |
| 222 |
|
| 223 |
/** |
| 224 |
* v9fs_alloc_inode - helper function to allocate an inode |
| 225 |
* @sb: The superblock to allocate the inode from |
| 226 |
*/ |
| 227 |
struct inode *v9fs_alloc_inode(struct super_block *sb) |
| 228 |
{ |
| 229 |
struct v9fs_inode *v9inode; |
| 230 |
|
| 231 |
v9inode = alloc_inode_sb(sb, v9fs_inode_cache, GFP_KERNEL); |
| 232 |
if (!v9inode) |
| 233 |
return NULL; |
| 234 |
v9inode->cache_validity = 0; |
| 235 |
mutex_init(&v9inode->v_mutex); |
| 236 |
return &v9inode->netfs.inode; |
| 237 |
} |
| 238 |
|
| 239 |
/** |
| 240 |
* v9fs_free_inode - destroy an inode |
| 241 |
* @inode: The inode to be freed |
| 242 |
*/ |
| 243 |
|
| 244 |
void v9fs_free_inode(struct inode *inode) |
| 245 |
{ |
| 246 |
kmem_cache_free(v9fs_inode_cache, V9FS_I(inode)); |
| 247 |
} |
| 248 |
|
| 249 |
/* |
| 250 |
* Set parameters for the netfs library |
| 251 |
*/ |
| 252 |
void v9fs_set_netfs_context(struct inode *inode) |
| 253 |
{ |
| 254 |
struct v9fs_inode *v9inode = V9FS_I(inode); |
| 255 |
netfs_inode_init(&v9inode->netfs, &v9fs_req_ops, true); |
| 256 |
} |
| 257 |
|
| 258 |
int v9fs_init_inode(struct v9fs_session_info *v9ses, |
| 259 |
struct inode *inode, umode_t mode, dev_t rdev) |
| 260 |
{ |
| 261 |
int err = 0; |
| 262 |
|
| 263 |
inode_init_owner(&nop_mnt_idmap, inode, NULL, mode); |
| 264 |
inode->i_blocks = 0; |
| 265 |
inode->i_rdev = rdev; |
| 266 |
simple_inode_init_ts(inode); |
| 267 |
inode->i_mapping->a_ops = &v9fs_addr_operations; |
| 268 |
inode->i_private = NULL; |
| 269 |
|
| 270 |
switch (mode & S_IFMT) { |
| 271 |
case S_IFIFO: |
| 272 |
case S_IFBLK: |
| 273 |
case S_IFCHR: |
| 274 |
case S_IFSOCK: |
| 275 |
if (v9fs_proto_dotl(v9ses)) { |
| 276 |
inode->i_op = &v9fs_file_inode_operations_dotl; |
| 277 |
} else if (v9fs_proto_dotu(v9ses)) { |
| 278 |
inode->i_op = &v9fs_file_inode_operations; |
| 279 |
} else { |
| 280 |
p9_debug(P9_DEBUG_ERROR, |
| 281 |
"special files without extended mode\n"); |
| 282 |
err = -EINVAL; |
| 283 |
goto error; |
| 284 |
} |
| 285 |
init_special_inode(inode, inode->i_mode, inode->i_rdev); |
| 286 |
break; |
| 287 |
case S_IFREG: |
| 288 |
if (v9fs_proto_dotl(v9ses)) { |
| 289 |
inode->i_op = &v9fs_file_inode_operations_dotl; |
| 290 |
inode->i_fop = &v9fs_file_operations_dotl; |
| 291 |
} else { |
| 292 |
inode->i_op = &v9fs_file_inode_operations; |
| 293 |
inode->i_fop = &v9fs_file_operations; |
| 294 |
} |
| 295 |
|
| 296 |
break; |
| 297 |
case S_IFLNK: |
| 298 |
if (!v9fs_proto_dotu(v9ses) && !v9fs_proto_dotl(v9ses)) { |
| 299 |
p9_debug(P9_DEBUG_ERROR, |
| 300 |
"extended modes used with legacy protocol\n"); |
| 301 |
err = -EINVAL; |
| 302 |
goto error; |
| 303 |
} |
| 304 |
|
| 305 |
if (v9fs_proto_dotl(v9ses)) |
| 306 |
inode->i_op = &v9fs_symlink_inode_operations_dotl; |
| 307 |
else |
| 308 |
inode->i_op = &v9fs_symlink_inode_operations; |
| 309 |
|
| 310 |
break; |
| 311 |
case S_IFDIR: |
| 312 |
inc_nlink(inode); |
| 313 |
if (v9fs_proto_dotl(v9ses)) |
| 314 |
inode->i_op = &v9fs_dir_inode_operations_dotl; |
| 315 |
else if (v9fs_proto_dotu(v9ses)) |
| 316 |
inode->i_op = &v9fs_dir_inode_operations_dotu; |
| 317 |
else |
| 318 |
inode->i_op = &v9fs_dir_inode_operations; |
| 319 |
|
| 320 |
if (v9fs_proto_dotl(v9ses)) |
| 321 |
inode->i_fop = &v9fs_dir_operations_dotl; |
| 322 |
else |
| 323 |
inode->i_fop = &v9fs_dir_operations; |
| 324 |
|
| 325 |
break; |
| 326 |
default: |
| 327 |
p9_debug(P9_DEBUG_ERROR, "BAD mode 0x%hx S_IFMT 0x%x\n", |
| 328 |
mode, mode & S_IFMT); |
| 329 |
err = -EINVAL; |
| 330 |
goto error; |
| 331 |
} |
| 332 |
error: |
| 333 |
return err; |
| 334 |
|
| 335 |
} |
| 336 |
|
| 337 |
/** |
| 338 |
* v9fs_evict_inode - Remove an inode from the inode cache |
| 339 |
* @inode: inode to release |
| 340 |
* |
| 341 |
*/ |
| 342 |
void v9fs_evict_inode(struct inode *inode) |
| 343 |
{ |
| 344 |
struct v9fs_inode __maybe_unused *v9inode = V9FS_I(inode); |
| 345 |
__le32 __maybe_unused version; |
| 346 |
|
| 347 |
if (!is_bad_inode(inode)) { |
| 348 |
netfs_wait_for_outstanding_io(inode); |
| 349 |
truncate_inode_pages_final(&inode->i_data); |
| 350 |
|
| 351 |
version = cpu_to_le32(v9inode->qid.version); |
| 352 |
netfs_clear_inode_writeback(inode, &version); |
| 353 |
|
| 354 |
clear_inode(inode); |
| 355 |
filemap_fdatawrite(&inode->i_data); |
| 356 |
|
| 357 |
#ifdef CONFIG_9P_FSCACHE |
| 358 |
if (v9fs_inode_cookie(v9inode)) |
| 359 |
fscache_relinquish_cookie(v9fs_inode_cookie(v9inode), false); |
| 360 |
#endif |
| 361 |
} else |
| 362 |
clear_inode(inode); |
| 363 |
} |
| 364 |
|
| 365 |
static int v9fs_test_inode(struct inode *inode, void *data) |
| 366 |
{ |
| 367 |
int umode; |
| 368 |
dev_t rdev; |
| 369 |
struct v9fs_inode *v9inode = V9FS_I(inode); |
| 370 |
struct p9_wstat *st = (struct p9_wstat *)data; |
| 371 |
struct v9fs_session_info *v9ses = v9fs_inode2v9ses(inode); |
| 372 |
|
| 373 |
umode = p9mode2unixmode(v9ses, st, &rdev); |
| 374 |
/* don't match inode of different type */ |
| 375 |
if (inode_wrong_type(inode, umode)) |
| 376 |
return 0; |
| 377 |
|
| 378 |
/* compare qid details */ |
| 379 |
if (memcmp(&v9inode->qid.version, |
| 380 |
&st->qid.version, sizeof(v9inode->qid.version))) |
| 381 |
return 0; |
| 382 |
|
| 383 |
if (v9inode->qid.type != st->qid.type) |
| 384 |
return 0; |
| 385 |
|
| 386 |
if (v9inode->qid.path != st->qid.path) |
| 387 |
return 0; |
| 388 |
return 1; |
| 389 |
} |
| 390 |
|
| 391 |
static int v9fs_test_new_inode(struct inode *inode, void *data) |
| 392 |
{ |
| 393 |
return 0; |
| 394 |
} |
| 395 |
|
| 396 |
static int v9fs_set_inode(struct inode *inode, void *data) |
| 397 |
{ |
| 398 |
struct v9fs_inode *v9inode = V9FS_I(inode); |
| 399 |
struct p9_wstat *st = (struct p9_wstat *)data; |
| 400 |
|
| 401 |
memcpy(&v9inode->qid, &st->qid, sizeof(st->qid)); |
| 402 |
return 0; |
| 403 |
} |
| 404 |
|
| 405 |
static struct inode *v9fs_qid_iget(struct super_block *sb, |
| 406 |
struct p9_qid *qid, |
| 407 |
struct p9_wstat *st, |
| 408 |
int new) |
| 409 |
{ |
| 410 |
dev_t rdev; |
| 411 |
int retval; |
| 412 |
umode_t umode; |
| 413 |
struct inode *inode; |
| 414 |
struct v9fs_session_info *v9ses = sb->s_fs_info; |
| 415 |
int (*test)(struct inode *inode, void *data); |
| 416 |
|
| 417 |
if (new) |
| 418 |
test = v9fs_test_new_inode; |
| 419 |
else |
| 420 |
test = v9fs_test_inode; |
| 421 |
|
| 422 |
inode = iget5_locked(sb, QID2INO(qid), test, v9fs_set_inode, st); |
| 423 |
if (!inode) |
| 424 |
return ERR_PTR(-ENOMEM); |
| 425 |
if (!(inode_state_read_once(inode) & I_NEW)) |
| 426 |
return inode; |
| 427 |
/* |
| 428 |
* initialize the inode with the stat info |
| 429 |
* FIXME!! we may need support for stale inodes |
| 430 |
* later. |
| 431 |
*/ |
| 432 |
inode->i_ino = QID2INO(qid); |
| 433 |
umode = p9mode2unixmode(v9ses, st, &rdev); |
| 434 |
retval = v9fs_init_inode(v9ses, inode, umode, rdev); |
| 435 |
if (retval) |
| 436 |
goto error; |
| 437 |
|
| 438 |
v9fs_stat2inode(st, inode, sb, 0); |
| 439 |
v9fs_set_netfs_context(inode); |
| 440 |
v9fs_cache_inode_get_cookie(inode); |
| 441 |
unlock_new_inode(inode); |
| 442 |
return inode; |
| 443 |
error: |
| 444 |
iget_failed(inode); |
| 445 |
return ERR_PTR(retval); |
| 446 |
|
| 447 |
} |
| 448 |
|
| 449 |
struct inode * |
| 450 |
v9fs_inode_from_fid(struct v9fs_session_info *v9ses, struct p9_fid *fid, |
| 451 |
struct super_block *sb, int new) |
| 452 |
{ |
| 453 |
struct p9_wstat *st; |
| 454 |
struct inode *inode = NULL; |
| 455 |
|
| 456 |
st = p9_client_stat(fid); |
| 457 |
if (IS_ERR(st)) |
| 458 |
return ERR_CAST(st); |
| 459 |
|
| 460 |
inode = v9fs_qid_iget(sb, &st->qid, st, new); |
| 461 |
p9stat_free(st); |
| 462 |
kfree(st); |
| 463 |
return inode; |
| 464 |
} |
| 465 |
|
| 466 |
/** |
| 467 |
* v9fs_at_to_dotl_flags- convert Linux specific AT flags to |
| 468 |
* plan 9 AT flag. |
| 469 |
* @flags: flags to convert |
| 470 |
*/ |
| 471 |
static int v9fs_at_to_dotl_flags(int flags) |
| 472 |
{ |
| 473 |
int rflags = 0; |
| 474 |
|
| 475 |
if (flags & AT_REMOVEDIR) |
| 476 |
rflags |= P9_DOTL_AT_REMOVEDIR; |
| 477 |
|
| 478 |
return rflags; |
| 479 |
} |
| 480 |
|
| 481 |
/** |
| 482 |
* v9fs_dec_count - helper functon to drop i_nlink. |
| 483 |
* |
| 484 |
* If a directory had nlink <= 2 (including . and ..), then we should not drop |
| 485 |
* the link count, which indicates the underlying exported fs doesn't maintain |
| 486 |
* nlink accurately. e.g. |
| 487 |
* - overlayfs sets nlink to 1 for merged dir |
| 488 |
* - ext4 (with dir_nlink feature enabled) sets nlink to 1 if a dir has more |
| 489 |
* than EXT4_LINK_MAX (65000) links. |
| 490 |
* |
| 491 |
* @inode: inode whose nlink is being dropped |
| 492 |
*/ |
| 493 |
static void v9fs_dec_count(struct inode *inode) |
| 494 |
{ |
| 495 |
if (!S_ISDIR(inode->i_mode) || inode->i_nlink > 2) |
| 496 |
drop_nlink(inode); |
| 497 |
} |
| 498 |
|
| 499 |
/** |
| 500 |
* v9fs_remove - helper function to remove files and directories |
| 501 |
* @dir: directory inode that is being deleted |
| 502 |
* @dentry: dentry that is being deleted |
| 503 |
* @flags: removing a directory |
| 504 |
* |
| 505 |
*/ |
| 506 |
|
| 507 |
static int v9fs_remove(struct inode *dir, struct dentry *dentry, int flags) |
| 508 |
{ |
| 509 |
struct inode *inode; |
| 510 |
int retval = -EOPNOTSUPP; |
| 511 |
struct p9_fid *v9fid, *dfid; |
| 512 |
struct v9fs_session_info *v9ses; |
| 513 |
|
| 514 |
p9_debug(P9_DEBUG_VFS, "inode: %p dentry: %p rmdir: %x\n", |
| 515 |
dir, dentry, flags); |
| 516 |
|
| 517 |
v9ses = v9fs_inode2v9ses(dir); |
| 518 |
inode = d_inode(dentry); |
| 519 |
dfid = v9fs_parent_fid(dentry); |
| 520 |
if (IS_ERR(dfid)) { |
| 521 |
retval = PTR_ERR(dfid); |
| 522 |
p9_debug(P9_DEBUG_VFS, "fid lookup failed %d\n", retval); |
| 523 |
return retval; |
| 524 |
} |
| 525 |
if (v9fs_proto_dotl(v9ses)) |
| 526 |
retval = p9_client_unlinkat(dfid, dentry->d_name.name, |
| 527 |
v9fs_at_to_dotl_flags(flags)); |
| 528 |
p9_fid_put(dfid); |
| 529 |
if (retval == -EOPNOTSUPP) { |
| 530 |
/* Try the one based on path */ |
| 531 |
v9fid = v9fs_fid_clone(dentry); |
| 532 |
if (IS_ERR(v9fid)) |
| 533 |
return PTR_ERR(v9fid); |
| 534 |
retval = p9_client_remove(v9fid); |
| 535 |
} |
| 536 |
if (!retval) { |
| 537 |
/* |
| 538 |
* directories on unlink should have zero |
| 539 |
* link count |
| 540 |
*/ |
| 541 |
if (flags & AT_REMOVEDIR) { |
| 542 |
clear_nlink(inode); |
| 543 |
v9fs_dec_count(dir); |
| 544 |
} else |
| 545 |
v9fs_dec_count(inode); |
| 546 |
|
| 547 |
v9fs_invalidate_inode_attr(inode); |
| 548 |
v9fs_invalidate_inode_attr(dir); |
| 549 |
|
| 550 |
/* invalidate all fids associated with dentry */ |
| 551 |
/* NOTE: This will not include open fids */ |
| 552 |
dentry->d_op->d_release(dentry); |
| 553 |
} |
| 554 |
return retval; |
| 555 |
} |
| 556 |
|
| 557 |
/** |
| 558 |
* v9fs_create - Create a file |
| 559 |
* @v9ses: session information |
| 560 |
* @dir: directory that dentry is being created in |
| 561 |
* @dentry: dentry that is being created |
| 562 |
* @extension: 9p2000.u extension string to support devices, etc. |
| 563 |
* @perm: create permissions |
| 564 |
* @mode: open mode |
| 565 |
* |
| 566 |
*/ |
| 567 |
static struct p9_fid * |
| 568 |
v9fs_create(struct v9fs_session_info *v9ses, struct inode *dir, |
| 569 |
struct dentry *dentry, char *extension, u32 perm, u8 mode) |
| 570 |
{ |
| 571 |
int err; |
| 572 |
const unsigned char *name; |
| 573 |
struct p9_fid *dfid, *ofid = NULL, *fid = NULL; |
| 574 |
struct inode *inode; |
| 575 |
|
| 576 |
p9_debug(P9_DEBUG_VFS, "name %pd\n", dentry); |
| 577 |
|
| 578 |
name = dentry->d_name.name; |
| 579 |
dfid = v9fs_parent_fid(dentry); |
| 580 |
if (IS_ERR(dfid)) { |
| 581 |
err = PTR_ERR(dfid); |
| 582 |
p9_debug(P9_DEBUG_VFS, "fid lookup failed %d\n", err); |
| 583 |
return ERR_PTR(err); |
| 584 |
} |
| 585 |
|
| 586 |
/* clone a fid to use for creation */ |
| 587 |
ofid = clone_fid(dfid); |
| 588 |
if (IS_ERR(ofid)) { |
| 589 |
err = PTR_ERR(ofid); |
| 590 |
p9_debug(P9_DEBUG_VFS, "p9_client_walk failed %d\n", err); |
| 591 |
goto error; |
| 592 |
} |
| 593 |
|
| 594 |
err = p9_client_fcreate(ofid, name, perm, mode, extension); |
| 595 |
if (err < 0) { |
| 596 |
p9_debug(P9_DEBUG_VFS, "p9_client_fcreate failed %d\n", err); |
| 597 |
goto error; |
| 598 |
} |
| 599 |
|
| 600 |
if (!(perm & P9_DMLINK)) { |
| 601 |
/* now walk from the parent so we can get unopened fid */ |
| 602 |
fid = p9_client_walk(dfid, 1, &name, 1); |
| 603 |
if (IS_ERR(fid)) { |
| 604 |
err = PTR_ERR(fid); |
| 605 |
p9_debug(P9_DEBUG_VFS, |
| 606 |
"p9_client_walk failed %d\n", err); |
| 607 |
goto error; |
| 608 |
} |
| 609 |
/* |
| 610 |
* instantiate inode and assign the unopened fid to the dentry |
| 611 |
*/ |
| 612 |
inode = v9fs_get_new_inode_from_fid(v9ses, fid, dir->i_sb); |
| 613 |
if (IS_ERR(inode)) { |
| 614 |
err = PTR_ERR(inode); |
| 615 |
p9_debug(P9_DEBUG_VFS, |
| 616 |
"inode creation failed %d\n", err); |
| 617 |
goto error; |
| 618 |
} |
| 619 |
v9fs_fid_add(dentry, &fid); |
| 620 |
d_instantiate(dentry, inode); |
| 621 |
} |
| 622 |
p9_fid_put(dfid); |
| 623 |
return ofid; |
| 624 |
error: |
| 625 |
p9_fid_put(dfid); |
| 626 |
p9_fid_put(ofid); |
| 627 |
p9_fid_put(fid); |
| 628 |
return ERR_PTR(err); |
| 629 |
} |
| 630 |
|
| 631 |
/** |
| 632 |
* v9fs_vfs_create - VFS hook to create a regular file |
| 633 |
* @idmap: idmap of the mount |
| 634 |
* @dir: The parent directory |
| 635 |
* @dentry: The name of file to be created |
| 636 |
* @mode: The UNIX file mode to set |
| 637 |
* @excl: True if the file must not yet exist |
| 638 |
* |
| 639 |
* open(.., O_CREAT) is handled in v9fs_vfs_atomic_open(). This is only called |
| 640 |
* for mknod(2). |
| 641 |
* |
| 642 |
*/ |
| 643 |
|
| 644 |
static int |
| 645 |
v9fs_vfs_create(struct mnt_idmap *idmap, struct inode *dir, |
| 646 |
struct dentry *dentry, umode_t mode, bool excl) |
| 647 |
{ |
| 648 |
struct v9fs_session_info *v9ses = v9fs_inode2v9ses(dir); |
| 649 |
u32 perm = unixmode2p9mode(v9ses, mode); |
| 650 |
struct p9_fid *fid; |
| 651 |
|
| 652 |
/* P9_OEXCL? */ |
| 653 |
fid = v9fs_create(v9ses, dir, dentry, NULL, perm, P9_ORDWR); |
| 654 |
if (IS_ERR(fid)) |
| 655 |
return PTR_ERR(fid); |
| 656 |
|
| 657 |
v9fs_invalidate_inode_attr(dir); |
| 658 |
p9_fid_put(fid); |
| 659 |
|
| 660 |
return 0; |
| 661 |
} |
| 662 |
|
| 663 |
/** |
| 664 |
* v9fs_vfs_mkdir - VFS mkdir hook to create a directory |
| 665 |
* @idmap: idmap of the mount |
| 666 |
* @dir: inode that is being unlinked |
| 667 |
* @dentry: dentry that is being unlinked |
| 668 |
* @mode: mode for new directory |
| 669 |
* |
| 670 |
*/ |
| 671 |
|
| 672 |
static struct dentry *v9fs_vfs_mkdir(struct mnt_idmap *idmap, struct inode *dir, |
| 673 |
struct dentry *dentry, umode_t mode) |
| 674 |
{ |
| 675 |
int err; |
| 676 |
u32 perm; |
| 677 |
struct p9_fid *fid; |
| 678 |
struct v9fs_session_info *v9ses; |
| 679 |
|
| 680 |
p9_debug(P9_DEBUG_VFS, "name %pd\n", dentry); |
| 681 |
err = 0; |
| 682 |
v9ses = v9fs_inode2v9ses(dir); |
| 683 |
perm = unixmode2p9mode(v9ses, mode | S_IFDIR); |
| 684 |
fid = v9fs_create(v9ses, dir, dentry, NULL, perm, P9_OREAD); |
| 685 |
if (IS_ERR(fid)) { |
| 686 |
err = PTR_ERR(fid); |
| 687 |
fid = NULL; |
| 688 |
} else { |
| 689 |
inc_nlink(dir); |
| 690 |
v9fs_invalidate_inode_attr(dir); |
| 691 |
} |
| 692 |
|
| 693 |
if (fid) |
| 694 |
p9_fid_put(fid); |
| 695 |
return ERR_PTR(err); |
| 696 |
} |
| 697 |
|
| 698 |
/** |
| 699 |
* v9fs_vfs_lookup - VFS lookup hook to "walk" to a new inode |
| 700 |
* @dir: inode that is being walked from |
| 701 |
* @dentry: dentry that is being walked to? |
| 702 |
* @flags: lookup flags (unused) |
| 703 |
* |
| 704 |
*/ |
| 705 |
|
| 706 |
struct dentry *v9fs_vfs_lookup(struct inode *dir, struct dentry *dentry, |
| 707 |
unsigned int flags) |
| 708 |
{ |
| 709 |
struct dentry *res; |
| 710 |
struct v9fs_session_info *v9ses; |
| 711 |
struct p9_fid *dfid, *fid; |
| 712 |
struct inode *inode; |
| 713 |
const unsigned char *name; |
| 714 |
|
| 715 |
p9_debug(P9_DEBUG_VFS, "dir: %p dentry: (%pd) %p flags: %x\n", |
| 716 |
dir, dentry, dentry, flags); |
| 717 |
|
| 718 |
if (dentry->d_name.len > NAME_MAX) |
| 719 |
return ERR_PTR(-ENAMETOOLONG); |
| 720 |
|
| 721 |
v9ses = v9fs_inode2v9ses(dir); |
| 722 |
/* We can walk d_parent because we hold the dir->i_mutex */ |
| 723 |
dfid = v9fs_parent_fid(dentry); |
| 724 |
if (IS_ERR(dfid)) |
| 725 |
return ERR_CAST(dfid); |
| 726 |
|
| 727 |
/* |
| 728 |
* Make sure we don't use a wrong inode due to parallel |
| 729 |
* unlink. For cached mode create calls request for new |
| 730 |
* inode. But with cache disabled, lookup should do this. |
| 731 |
*/ |
| 732 |
name = dentry->d_name.name; |
| 733 |
fid = p9_client_walk(dfid, 1, &name, 1); |
| 734 |
p9_fid_put(dfid); |
| 735 |
if (fid == ERR_PTR(-ENOENT)) |
| 736 |
inode = NULL; |
| 737 |
else if (IS_ERR(fid)) |
| 738 |
inode = ERR_CAST(fid); |
| 739 |
else if (v9ses->cache & (CACHE_META|CACHE_LOOSE)) |
| 740 |
inode = v9fs_get_inode_from_fid(v9ses, fid, dir->i_sb); |
| 741 |
else |
| 742 |
inode = v9fs_get_new_inode_from_fid(v9ses, fid, dir->i_sb); |
| 743 |
/* |
| 744 |
* If we had a rename on the server and a parallel lookup |
| 745 |
* for the new name, then make sure we instantiate with |
| 746 |
* the new name. ie look up for a/b, while on server somebody |
| 747 |
* moved b under k and client parallely did a lookup for |
| 748 |
* k/b. |
| 749 |
*/ |
| 750 |
res = d_splice_alias(inode, dentry); |
| 751 |
if (!IS_ERR(fid)) { |
| 752 |
if (!res) |
| 753 |
v9fs_fid_add(dentry, &fid); |
| 754 |
else if (!IS_ERR(res)) |
| 755 |
v9fs_fid_add(res, &fid); |
| 756 |
else |
| 757 |
p9_fid_put(fid); |
| 758 |
} |
| 759 |
return res; |
| 760 |
} |
| 761 |
|
| 762 |
static int |
| 763 |
v9fs_vfs_atomic_open(struct inode *dir, struct dentry *dentry, |
| 764 |
struct file *file, unsigned int flags, umode_t mode) |
| 765 |
{ |
| 766 |
int err; |
| 767 |
u32 perm; |
| 768 |
struct v9fs_inode __maybe_unused *v9inode; |
| 769 |
struct v9fs_session_info *v9ses; |
| 770 |
struct p9_fid *fid; |
| 771 |
struct inode *inode; |
| 772 |
int p9_omode; |
| 773 |
|
| 774 |
if (d_in_lookup(dentry)) { |
| 775 |
struct dentry *res = v9fs_vfs_lookup(dir, dentry, 0); |
| 776 |
if (res || d_really_is_positive(dentry)) |
| 777 |
return finish_no_open(file, res); |
| 778 |
} |
| 779 |
|
| 780 |
/* Only creates */ |
| 781 |
if (!(flags & O_CREAT)) |
| 782 |
return finish_no_open(file, NULL); |
| 783 |
|
| 784 |
v9ses = v9fs_inode2v9ses(dir); |
| 785 |
perm = unixmode2p9mode(v9ses, mode); |
| 786 |
p9_omode = v9fs_uflags2omode(flags, v9fs_proto_dotu(v9ses)); |
| 787 |
|
| 788 |
if ((v9ses->cache & CACHE_WRITEBACK) && (p9_omode & P9_OWRITE)) { |
| 789 |
p9_omode = (p9_omode & ~(P9_OWRITE | P9_OAPPEND)) | P9_ORDWR; |
| 790 |
p9_debug(P9_DEBUG_CACHE, |
| 791 |
"write-only file with writeback enabled, creating w/ O_RDWR\n"); |
| 792 |
} |
| 793 |
fid = v9fs_create(v9ses, dir, dentry, NULL, perm, p9_omode); |
| 794 |
if (IS_ERR(fid)) |
| 795 |
return PTR_ERR(fid); |
| 796 |
|
| 797 |
v9fs_invalidate_inode_attr(dir); |
| 798 |
inode = d_inode(dentry); |
| 799 |
v9inode = V9FS_I(inode); |
| 800 |
err = finish_open(file, dentry, generic_file_open); |
| 801 |
if (unlikely(err)) { |
| 802 |
p9_fid_put(fid); |
| 803 |
return err; |
| 804 |
} |
| 805 |
|
| 806 |
file->private_data = fid; |
| 807 |
#ifdef CONFIG_9P_FSCACHE |
| 808 |
if (v9ses->cache & CACHE_FSCACHE) |
| 809 |
fscache_use_cookie(v9fs_inode_cookie(v9inode), |
| 810 |
file->f_mode & FMODE_WRITE); |
| 811 |
#endif |
| 812 |
|
| 813 |
v9fs_fid_add_modes(fid, v9ses->flags, v9ses->cache, file->f_flags); |
| 814 |
v9fs_open_fid_add(inode, &fid); |
| 815 |
|
| 816 |
file->f_mode |= FMODE_CREATED; |
| 817 |
return 0; |
| 818 |
} |
| 819 |
|
| 820 |
/** |
| 821 |
* v9fs_vfs_unlink - VFS unlink hook to delete an inode |
| 822 |
* @i: inode that is being unlinked |
| 823 |
* @d: dentry that is being unlinked |
| 824 |
* |
| 825 |
*/ |
| 826 |
|
| 827 |
int v9fs_vfs_unlink(struct inode *i, struct dentry *d) |
| 828 |
{ |
| 829 |
return v9fs_remove(i, d, 0); |
| 830 |
} |
| 831 |
|
| 832 |
/** |
| 833 |
* v9fs_vfs_rmdir - VFS unlink hook to delete a directory |
| 834 |
* @i: inode that is being unlinked |
| 835 |
* @d: dentry that is being unlinked |
| 836 |
* |
| 837 |
*/ |
| 838 |
|
| 839 |
int v9fs_vfs_rmdir(struct inode *i, struct dentry *d) |
| 840 |
{ |
| 841 |
return v9fs_remove(i, d, AT_REMOVEDIR); |
| 842 |
} |
| 843 |
|
| 844 |
/** |
| 845 |
* v9fs_vfs_rename - VFS hook to rename an inode |
| 846 |
* @idmap: The idmap of the mount |
| 847 |
* @old_dir: old dir inode |
| 848 |
* @old_dentry: old dentry |
| 849 |
* @new_dir: new dir inode |
| 850 |
* @new_dentry: new dentry |
| 851 |
* @flags: RENAME_* flags |
| 852 |
* |
| 853 |
*/ |
| 854 |
|
| 855 |
int |
| 856 |
v9fs_vfs_rename(struct mnt_idmap *idmap, struct inode *old_dir, |
| 857 |
struct dentry *old_dentry, struct inode *new_dir, |
| 858 |
struct dentry *new_dentry, unsigned int flags) |
| 859 |
{ |
| 860 |
int retval; |
| 861 |
struct inode *old_inode; |
| 862 |
struct inode *new_inode; |
| 863 |
struct v9fs_session_info *v9ses; |
| 864 |
struct p9_fid *oldfid = NULL, *dfid = NULL; |
| 865 |
struct p9_fid *olddirfid = NULL; |
| 866 |
struct p9_fid *newdirfid = NULL; |
| 867 |
struct p9_wstat wstat; |
| 868 |
|
| 869 |
if (flags) |
| 870 |
return -EINVAL; |
| 871 |
|
| 872 |
p9_debug(P9_DEBUG_VFS, "\n"); |
| 873 |
old_inode = d_inode(old_dentry); |
| 874 |
new_inode = d_inode(new_dentry); |
| 875 |
v9ses = v9fs_inode2v9ses(old_inode); |
| 876 |
oldfid = v9fs_fid_lookup(old_dentry); |
| 877 |
if (IS_ERR(oldfid)) |
| 878 |
return PTR_ERR(oldfid); |
| 879 |
|
| 880 |
dfid = v9fs_parent_fid(old_dentry); |
| 881 |
olddirfid = clone_fid(dfid); |
| 882 |
p9_fid_put(dfid); |
| 883 |
dfid = NULL; |
| 884 |
|
| 885 |
if (IS_ERR(olddirfid)) { |
| 886 |
retval = PTR_ERR(olddirfid); |
| 887 |
goto error; |
| 888 |
} |
| 889 |
|
| 890 |
dfid = v9fs_parent_fid(new_dentry); |
| 891 |
newdirfid = clone_fid(dfid); |
| 892 |
p9_fid_put(dfid); |
| 893 |
dfid = NULL; |
| 894 |
|
| 895 |
if (IS_ERR(newdirfid)) { |
| 896 |
retval = PTR_ERR(newdirfid); |
| 897 |
goto error; |
| 898 |
} |
| 899 |
|
| 900 |
down_write(&v9ses->rename_sem); |
| 901 |
if (v9fs_proto_dotl(v9ses)) { |
| 902 |
retval = p9_client_renameat(olddirfid, old_dentry->d_name.name, |
| 903 |
newdirfid, new_dentry->d_name.name); |
| 904 |
if (retval == -EOPNOTSUPP) |
| 905 |
retval = p9_client_rename(oldfid, newdirfid, |
| 906 |
new_dentry->d_name.name); |
| 907 |
if (retval != -EOPNOTSUPP) |
| 908 |
goto error_locked; |
| 909 |
} |
| 910 |
if (old_dentry->d_parent != new_dentry->d_parent) { |
| 911 |
/* |
| 912 |
* 9P .u can only handle file rename in the same directory |
| 913 |
*/ |
| 914 |
|
| 915 |
p9_debug(P9_DEBUG_ERROR, "old dir and new dir are different\n"); |
| 916 |
retval = -EXDEV; |
| 917 |
goto error_locked; |
| 918 |
} |
| 919 |
v9fs_blank_wstat(&wstat); |
| 920 |
wstat.muid = v9ses->uname; |
| 921 |
wstat.name = new_dentry->d_name.name; |
| 922 |
retval = p9_client_wstat(oldfid, &wstat); |
| 923 |
|
| 924 |
error_locked: |
| 925 |
if (!retval) { |
| 926 |
if (new_inode) { |
| 927 |
if (S_ISDIR(new_inode->i_mode)) |
| 928 |
clear_nlink(new_inode); |
| 929 |
else |
| 930 |
v9fs_dec_count(new_inode); |
| 931 |
} |
| 932 |
if (S_ISDIR(old_inode->i_mode)) { |
| 933 |
if (!new_inode) |
| 934 |
inc_nlink(new_dir); |
| 935 |
v9fs_dec_count(old_dir); |
| 936 |
} |
| 937 |
v9fs_invalidate_inode_attr(old_inode); |
| 938 |
v9fs_invalidate_inode_attr(old_dir); |
| 939 |
v9fs_invalidate_inode_attr(new_dir); |
| 940 |
|
| 941 |
/* successful rename */ |
| 942 |
d_move(old_dentry, new_dentry); |
| 943 |
} |
| 944 |
up_write(&v9ses->rename_sem); |
| 945 |
|
| 946 |
error: |
| 947 |
p9_fid_put(newdirfid); |
| 948 |
p9_fid_put(olddirfid); |
| 949 |
p9_fid_put(oldfid); |
| 950 |
return retval; |
| 951 |
} |
| 952 |
|
| 953 |
/** |
| 954 |
* v9fs_vfs_getattr - retrieve file metadata |
| 955 |
* @idmap: idmap of the mount |
| 956 |
* @path: Object to query |
| 957 |
* @stat: metadata structure to populate |
| 958 |
* @request_mask: Mask of STATX_xxx flags indicating the caller's interests |
| 959 |
* @flags: AT_STATX_xxx setting |
| 960 |
* |
| 961 |
*/ |
| 962 |
|
| 963 |
static int |
| 964 |
v9fs_vfs_getattr(struct mnt_idmap *idmap, const struct path *path, |
| 965 |
struct kstat *stat, u32 request_mask, unsigned int flags) |
| 966 |
{ |
| 967 |
struct dentry *dentry = path->dentry; |
| 968 |
struct inode *inode = d_inode(dentry); |
| 969 |
struct v9fs_session_info *v9ses; |
| 970 |
struct p9_fid *fid; |
| 971 |
struct p9_wstat *st; |
| 972 |
|
| 973 |
p9_debug(P9_DEBUG_VFS, "dentry: %p\n", dentry); |
| 974 |
v9ses = v9fs_dentry2v9ses(dentry); |
| 975 |
if (v9ses->cache & (CACHE_META|CACHE_LOOSE)) { |
| 976 |
generic_fillattr(&nop_mnt_idmap, request_mask, inode, stat); |
| 977 |
return 0; |
| 978 |
} else if (v9ses->cache & CACHE_WRITEBACK) { |
| 979 |
if (S_ISREG(inode->i_mode)) { |
| 980 |
int retval = filemap_fdatawrite(inode->i_mapping); |
| 981 |
|
| 982 |
if (retval) |
| 983 |
p9_debug(P9_DEBUG_ERROR, |
| 984 |
"flushing writeback during getattr returned %d\n", retval); |
| 985 |
} |
| 986 |
} |
| 987 |
fid = v9fs_fid_lookup(dentry); |
| 988 |
if (IS_ERR(fid)) |
| 989 |
return PTR_ERR(fid); |
| 990 |
|
| 991 |
st = p9_client_stat(fid); |
| 992 |
p9_fid_put(fid); |
| 993 |
if (IS_ERR(st)) |
| 994 |
return PTR_ERR(st); |
| 995 |
|
| 996 |
v9fs_stat2inode(st, d_inode(dentry), dentry->d_sb, 0); |
| 997 |
generic_fillattr(&nop_mnt_idmap, request_mask, d_inode(dentry), stat); |
| 998 |
|
| 999 |
p9stat_free(st); |
| 1000 |
kfree(st); |
| 1001 |
return 0; |
| 1002 |
} |
| 1003 |
|
| 1004 |
/** |
| 1005 |
* v9fs_vfs_setattr - set file metadata |
| 1006 |
* @idmap: idmap of the mount |
| 1007 |
* @dentry: file whose metadata to set |
| 1008 |
* @iattr: metadata assignment structure |
| 1009 |
* |
| 1010 |
*/ |
| 1011 |
|
| 1012 |
static int v9fs_vfs_setattr(struct mnt_idmap *idmap, |
| 1013 |
struct dentry *dentry, struct iattr *iattr) |
| 1014 |
{ |
| 1015 |
int retval, use_dentry = 0; |
| 1016 |
struct inode *inode = d_inode(dentry); |
| 1017 |
struct v9fs_session_info *v9ses; |
| 1018 |
struct p9_fid *fid = NULL; |
| 1019 |
struct p9_wstat wstat; |
| 1020 |
|
| 1021 |
p9_debug(P9_DEBUG_VFS, "\n"); |
| 1022 |
retval = setattr_prepare(&nop_mnt_idmap, dentry, iattr); |
| 1023 |
if (retval) |
| 1024 |
return retval; |
| 1025 |
|
| 1026 |
v9ses = v9fs_dentry2v9ses(dentry); |
| 1027 |
if (iattr->ia_valid & ATTR_FILE) { |
| 1028 |
fid = iattr->ia_file->private_data; |
| 1029 |
WARN_ON(!fid); |
| 1030 |
} |
| 1031 |
if (!fid) { |
| 1032 |
fid = v9fs_fid_lookup(dentry); |
| 1033 |
use_dentry = 1; |
| 1034 |
} |
| 1035 |
if (IS_ERR(fid)) |
| 1036 |
return PTR_ERR(fid); |
| 1037 |
|
| 1038 |
v9fs_blank_wstat(&wstat); |
| 1039 |
if (iattr->ia_valid & ATTR_MODE) |
| 1040 |
wstat.mode = unixmode2p9mode(v9ses, iattr->ia_mode); |
| 1041 |
|
| 1042 |
if (iattr->ia_valid & ATTR_MTIME) |
| 1043 |
wstat.mtime = iattr->ia_mtime.tv_sec; |
| 1044 |
|
| 1045 |
if (iattr->ia_valid & ATTR_ATIME) |
| 1046 |
wstat.atime = iattr->ia_atime.tv_sec; |
| 1047 |
|
| 1048 |
if (iattr->ia_valid & ATTR_SIZE) |
| 1049 |
wstat.length = iattr->ia_size; |
| 1050 |
|
| 1051 |
if (v9fs_proto_dotu(v9ses)) { |
| 1052 |
if (iattr->ia_valid & ATTR_UID) |
| 1053 |
wstat.n_uid = iattr->ia_uid; |
| 1054 |
|
| 1055 |
if (iattr->ia_valid & ATTR_GID) |
| 1056 |
wstat.n_gid = iattr->ia_gid; |
| 1057 |
} |
| 1058 |
|
| 1059 |
/* Write all dirty data */ |
| 1060 |
if (d_is_reg(dentry)) { |
| 1061 |
retval = filemap_fdatawrite(inode->i_mapping); |
| 1062 |
if (retval) |
| 1063 |
p9_debug(P9_DEBUG_ERROR, |
| 1064 |
"flushing writeback during setattr returned %d\n", retval); |
| 1065 |
} |
| 1066 |
|
| 1067 |
retval = p9_client_wstat(fid, &wstat); |
| 1068 |
|
| 1069 |
if (use_dentry) |
| 1070 |
p9_fid_put(fid); |
| 1071 |
|
| 1072 |
if (retval < 0) |
| 1073 |
return retval; |
| 1074 |
|
| 1075 |
if ((iattr->ia_valid & ATTR_SIZE) && |
| 1076 |
iattr->ia_size != i_size_read(inode)) { |
| 1077 |
truncate_setsize(inode, iattr->ia_size); |
| 1078 |
netfs_resize_file(netfs_inode(inode), iattr->ia_size, true); |
| 1079 |
|
| 1080 |
#ifdef CONFIG_9P_FSCACHE |
| 1081 |
if (v9ses->cache & CACHE_FSCACHE) { |
| 1082 |
struct v9fs_inode *v9inode = V9FS_I(inode); |
| 1083 |
|
| 1084 |
fscache_resize_cookie(v9fs_inode_cookie(v9inode), iattr->ia_size); |
| 1085 |
} |
| 1086 |
#endif |
| 1087 |
} |
| 1088 |
|
| 1089 |
v9fs_invalidate_inode_attr(inode); |
| 1090 |
|
| 1091 |
setattr_copy(&nop_mnt_idmap, inode, iattr); |
| 1092 |
mark_inode_dirty(inode); |
| 1093 |
return 0; |
| 1094 |
} |
| 1095 |
|
| 1096 |
/** |
| 1097 |
* v9fs_stat2inode - populate an inode structure with mistat info |
| 1098 |
* @stat: Plan 9 metadata (mistat) structure |
| 1099 |
* @inode: inode to populate |
| 1100 |
* @sb: superblock of filesystem |
| 1101 |
* @flags: control flags (e.g. V9FS_STAT2INODE_KEEP_ISIZE) |
| 1102 |
* |
| 1103 |
*/ |
| 1104 |
|
| 1105 |
void |
| 1106 |
v9fs_stat2inode(struct p9_wstat *stat, struct inode *inode, |
| 1107 |
struct super_block *sb, unsigned int flags) |
| 1108 |
{ |
| 1109 |
umode_t mode; |
| 1110 |
struct v9fs_session_info *v9ses = sb->s_fs_info; |
| 1111 |
struct v9fs_inode *v9inode = V9FS_I(inode); |
| 1112 |
|
| 1113 |
inode_set_atime(inode, stat->atime, 0); |
| 1114 |
inode_set_mtime(inode, stat->mtime, 0); |
| 1115 |
inode_set_ctime(inode, stat->mtime, 0); |
| 1116 |
|
| 1117 |
inode->i_uid = v9ses->dfltuid; |
| 1118 |
inode->i_gid = v9ses->dfltgid; |
| 1119 |
|
| 1120 |
if (v9fs_proto_dotu(v9ses)) { |
| 1121 |
inode->i_uid = stat->n_uid; |
| 1122 |
inode->i_gid = stat->n_gid; |
| 1123 |
} |
| 1124 |
if ((S_ISREG(inode->i_mode)) || (S_ISDIR(inode->i_mode))) { |
| 1125 |
if (v9fs_proto_dotu(v9ses)) { |
| 1126 |
unsigned int i_nlink; |
| 1127 |
/* |
| 1128 |
* Hadlink support got added later to the .u extension. |
| 1129 |
* So there can be a server out there that doesn't |
| 1130 |
* support this even with .u extension. That would |
| 1131 |
* just leave us with stat->extension being an empty |
| 1132 |
* string, though. |
| 1133 |
*/ |
| 1134 |
/* HARDLINKCOUNT %u */ |
| 1135 |
if (sscanf(stat->extension, |
| 1136 |
" HARDLINKCOUNT %u", &i_nlink) == 1) |
| 1137 |
set_nlink(inode, i_nlink); |
| 1138 |
} |
| 1139 |
} |
| 1140 |
mode = p9mode2perm(v9ses, stat); |
| 1141 |
mode |= inode->i_mode & ~S_IALLUGO; |
| 1142 |
inode->i_mode = mode; |
| 1143 |
|
| 1144 |
v9inode->netfs.remote_i_size = stat->length; |
| 1145 |
if (!(flags & V9FS_STAT2INODE_KEEP_ISIZE)) |
| 1146 |
v9fs_i_size_write(inode, stat->length); |
| 1147 |
/* not real number of blocks, but 512 byte ones ... */ |
| 1148 |
inode->i_blocks = (stat->length + 512 - 1) >> 9; |
| 1149 |
v9inode->cache_validity &= ~V9FS_INO_INVALID_ATTR; |
| 1150 |
} |
| 1151 |
|
| 1152 |
/** |
| 1153 |
* v9fs_vfs_get_link - follow a symlink path |
| 1154 |
* @dentry: dentry for symlink |
| 1155 |
* @inode: inode for symlink |
| 1156 |
* @done: delayed call for when we are done with the return value |
| 1157 |
*/ |
| 1158 |
|
| 1159 |
static const char *v9fs_vfs_get_link(struct dentry *dentry, |
| 1160 |
struct inode *inode, |
| 1161 |
struct delayed_call *done) |
| 1162 |
{ |
| 1163 |
struct v9fs_session_info *v9ses; |
| 1164 |
struct p9_fid *fid; |
| 1165 |
struct p9_wstat *st; |
| 1166 |
char *res; |
| 1167 |
|
| 1168 |
if (!dentry) |
| 1169 |
return ERR_PTR(-ECHILD); |
| 1170 |
|
| 1171 |
v9ses = v9fs_dentry2v9ses(dentry); |
| 1172 |
if (!v9fs_proto_dotu(v9ses)) |
| 1173 |
return ERR_PTR(-EBADF); |
| 1174 |
|
| 1175 |
p9_debug(P9_DEBUG_VFS, "%pd\n", dentry); |
| 1176 |
fid = v9fs_fid_lookup(dentry); |
| 1177 |
|
| 1178 |
if (IS_ERR(fid)) |
| 1179 |
return ERR_CAST(fid); |
| 1180 |
|
| 1181 |
st = p9_client_stat(fid); |
| 1182 |
p9_fid_put(fid); |
| 1183 |
if (IS_ERR(st)) |
| 1184 |
return ERR_CAST(st); |
| 1185 |
|
| 1186 |
if (!(st->mode & P9_DMSYMLINK)) { |
| 1187 |
p9stat_free(st); |
| 1188 |
kfree(st); |
| 1189 |
return ERR_PTR(-EINVAL); |
| 1190 |
} |
| 1191 |
res = st->extension; |
| 1192 |
st->extension = NULL; |
| 1193 |
if (strlen(res) >= PATH_MAX) |
| 1194 |
res[PATH_MAX - 1] = '\0'; |
| 1195 |
|
| 1196 |
p9stat_free(st); |
| 1197 |
kfree(st); |
| 1198 |
set_delayed_call(done, kfree_link, res); |
| 1199 |
return res; |
| 1200 |
} |
| 1201 |
|
| 1202 |
/** |
| 1203 |
* v9fs_vfs_mkspecial - create a special file |
| 1204 |
* @dir: inode to create special file in |
| 1205 |
* @dentry: dentry to create |
| 1206 |
* @perm: mode to create special file |
| 1207 |
* @extension: 9p2000.u format extension string representing special file |
| 1208 |
* |
| 1209 |
*/ |
| 1210 |
|
| 1211 |
static int v9fs_vfs_mkspecial(struct inode *dir, struct dentry *dentry, |
| 1212 |
u32 perm, const char *extension) |
| 1213 |
{ |
| 1214 |
struct p9_fid *fid; |
| 1215 |
struct v9fs_session_info *v9ses; |
| 1216 |
|
| 1217 |
v9ses = v9fs_inode2v9ses(dir); |
| 1218 |
if (!v9fs_proto_dotu(v9ses)) { |
| 1219 |
p9_debug(P9_DEBUG_ERROR, "not extended\n"); |
| 1220 |
return -EPERM; |
| 1221 |
} |
| 1222 |
|
| 1223 |
fid = v9fs_create(v9ses, dir, dentry, (char *) extension, perm, |
| 1224 |
P9_OREAD); |
| 1225 |
if (IS_ERR(fid)) |
| 1226 |
return PTR_ERR(fid); |
| 1227 |
|
| 1228 |
v9fs_invalidate_inode_attr(dir); |
| 1229 |
p9_fid_put(fid); |
| 1230 |
return 0; |
| 1231 |
} |
| 1232 |
|
| 1233 |
/** |
| 1234 |
* v9fs_vfs_symlink - helper function to create symlinks |
| 1235 |
* @idmap: idmap of the mount |
| 1236 |
* @dir: directory inode containing symlink |
| 1237 |
* @dentry: dentry for symlink |
| 1238 |
* @symname: symlink data |
| 1239 |
* |
| 1240 |
* See Also: 9P2000.u RFC for more information |
| 1241 |
* |
| 1242 |
*/ |
| 1243 |
|
| 1244 |
static int |
| 1245 |
v9fs_vfs_symlink(struct mnt_idmap *idmap, struct inode *dir, |
| 1246 |
struct dentry *dentry, const char *symname) |
| 1247 |
{ |
| 1248 |
p9_debug(P9_DEBUG_VFS, " %lu,%pd,%s\n", |
| 1249 |
dir->i_ino, dentry, symname); |
| 1250 |
|
| 1251 |
return v9fs_vfs_mkspecial(dir, dentry, P9_DMSYMLINK, symname); |
| 1252 |
} |
| 1253 |
|
| 1254 |
#define U32_MAX_DIGITS 10 |
| 1255 |
|
| 1256 |
/** |
| 1257 |
* v9fs_vfs_link - create a hardlink |
| 1258 |
* @old_dentry: dentry for file to link to |
| 1259 |
* @dir: inode destination for new link |
| 1260 |
* @dentry: dentry for link |
| 1261 |
* |
| 1262 |
*/ |
| 1263 |
|
| 1264 |
static int |
| 1265 |
v9fs_vfs_link(struct dentry *old_dentry, struct inode *dir, |
| 1266 |
struct dentry *dentry) |
| 1267 |
{ |
| 1268 |
int retval; |
| 1269 |
char name[1 + U32_MAX_DIGITS + 2]; /* sign + number + \n + \0 */ |
| 1270 |
struct p9_fid *oldfid; |
| 1271 |
|
| 1272 |
p9_debug(P9_DEBUG_VFS, " %lu,%pd,%pd\n", |
| 1273 |
dir->i_ino, dentry, old_dentry); |
| 1274 |
|
| 1275 |
oldfid = v9fs_fid_clone(old_dentry); |
| 1276 |
if (IS_ERR(oldfid)) |
| 1277 |
return PTR_ERR(oldfid); |
| 1278 |
|
| 1279 |
sprintf(name, "%d\n", oldfid->fid); |
| 1280 |
retval = v9fs_vfs_mkspecial(dir, dentry, P9_DMLINK, name); |
| 1281 |
if (!retval) { |
| 1282 |
v9fs_refresh_inode(oldfid, d_inode(old_dentry)); |
| 1283 |
v9fs_invalidate_inode_attr(dir); |
| 1284 |
} |
| 1285 |
p9_fid_put(oldfid); |
| 1286 |
return retval; |
| 1287 |
} |
| 1288 |
|
| 1289 |
/** |
| 1290 |
* v9fs_vfs_mknod - create a special file |
| 1291 |
* @idmap: idmap of the mount |
| 1292 |
* @dir: inode destination for new link |
| 1293 |
* @dentry: dentry for file |
| 1294 |
* @mode: mode for creation |
| 1295 |
* @rdev: device associated with special file |
| 1296 |
* |
| 1297 |
*/ |
| 1298 |
|
| 1299 |
static int |
| 1300 |
v9fs_vfs_mknod(struct mnt_idmap *idmap, struct inode *dir, |
| 1301 |
struct dentry *dentry, umode_t mode, dev_t rdev) |
| 1302 |
{ |
| 1303 |
struct v9fs_session_info *v9ses = v9fs_inode2v9ses(dir); |
| 1304 |
int retval; |
| 1305 |
char name[2 + U32_MAX_DIGITS + 1 + U32_MAX_DIGITS + 1]; |
| 1306 |
u32 perm; |
| 1307 |
|
| 1308 |
p9_debug(P9_DEBUG_VFS, " %lu,%pd mode: %x MAJOR: %u MINOR: %u\n", |
| 1309 |
dir->i_ino, dentry, mode, |
| 1310 |
MAJOR(rdev), MINOR(rdev)); |
| 1311 |
|
| 1312 |
/* build extension */ |
| 1313 |
if (S_ISBLK(mode)) |
| 1314 |
sprintf(name, "b %u %u", MAJOR(rdev), MINOR(rdev)); |
| 1315 |
else if (S_ISCHR(mode)) |
| 1316 |
sprintf(name, "c %u %u", MAJOR(rdev), MINOR(rdev)); |
| 1317 |
else |
| 1318 |
*name = 0; |
| 1319 |
|
| 1320 |
perm = unixmode2p9mode(v9ses, mode); |
| 1321 |
retval = v9fs_vfs_mkspecial(dir, dentry, perm, name); |
| 1322 |
|
| 1323 |
return retval; |
| 1324 |
} |
| 1325 |
|
| 1326 |
int v9fs_refresh_inode(struct p9_fid *fid, struct inode *inode) |
| 1327 |
{ |
| 1328 |
int umode; |
| 1329 |
dev_t rdev; |
| 1330 |
struct p9_wstat *st; |
| 1331 |
struct v9fs_session_info *v9ses; |
| 1332 |
unsigned int flags; |
| 1333 |
|
| 1334 |
v9ses = v9fs_inode2v9ses(inode); |
| 1335 |
st = p9_client_stat(fid); |
| 1336 |
if (IS_ERR(st)) |
| 1337 |
return PTR_ERR(st); |
| 1338 |
/* |
| 1339 |
* Don't update inode if the file type is different |
| 1340 |
*/ |
| 1341 |
umode = p9mode2unixmode(v9ses, st, &rdev); |
| 1342 |
if (inode_wrong_type(inode, umode)) |
| 1343 |
goto out; |
| 1344 |
|
| 1345 |
/* |
| 1346 |
* We don't want to refresh inode->i_size, |
| 1347 |
* because we may have cached data |
| 1348 |
*/ |
| 1349 |
flags = (v9ses->cache & CACHE_LOOSE) ? |
| 1350 |
V9FS_STAT2INODE_KEEP_ISIZE : 0; |
| 1351 |
v9fs_stat2inode(st, inode, inode->i_sb, flags); |
| 1352 |
out: |
| 1353 |
p9stat_free(st); |
| 1354 |
kfree(st); |
| 1355 |
return 0; |
| 1356 |
} |
| 1357 |
|
| 1358 |
static const struct inode_operations v9fs_dir_inode_operations_dotu = { |
| 1359 |
.create = v9fs_vfs_create, |
| 1360 |
.lookup = v9fs_vfs_lookup, |
| 1361 |
.atomic_open = v9fs_vfs_atomic_open, |
| 1362 |
.symlink = v9fs_vfs_symlink, |
| 1363 |
.link = v9fs_vfs_link, |
| 1364 |
.unlink = v9fs_vfs_unlink, |
| 1365 |
.mkdir = v9fs_vfs_mkdir, |
| 1366 |
.rmdir = v9fs_vfs_rmdir, |
| 1367 |
.mknod = v9fs_vfs_mknod, |
| 1368 |
.rename = v9fs_vfs_rename, |
| 1369 |
.getattr = v9fs_vfs_getattr, |
| 1370 |
.setattr = v9fs_vfs_setattr, |
| 1371 |
}; |
| 1372 |
|
| 1373 |
static const struct inode_operations v9fs_dir_inode_operations = { |
| 1374 |
.create = v9fs_vfs_create, |
| 1375 |
.lookup = v9fs_vfs_lookup, |
| 1376 |
.atomic_open = v9fs_vfs_atomic_open, |
| 1377 |
.unlink = v9fs_vfs_unlink, |
| 1378 |
.mkdir = v9fs_vfs_mkdir, |
| 1379 |
.rmdir = v9fs_vfs_rmdir, |
| 1380 |
.mknod = v9fs_vfs_mknod, |
| 1381 |
.rename = v9fs_vfs_rename, |
| 1382 |
.getattr = v9fs_vfs_getattr, |
| 1383 |
.setattr = v9fs_vfs_setattr, |
| 1384 |
}; |
| 1385 |
|
| 1386 |
static const struct inode_operations v9fs_file_inode_operations = { |
| 1387 |
.getattr = v9fs_vfs_getattr, |
| 1388 |
.setattr = v9fs_vfs_setattr, |
| 1389 |
}; |
| 1390 |
|
| 1391 |
static const struct inode_operations v9fs_symlink_inode_operations = { |
| 1392 |
.get_link = v9fs_vfs_get_link, |
| 1393 |
.getattr = v9fs_vfs_getattr, |
| 1394 |
.setattr = v9fs_vfs_setattr, |
| 1395 |
}; |