1 // SPDX-License-Identifier: GPL-2.0
12 #include <linux/compat.h>
13 #include <linux/fsnotify.h>
14 #include <linux/mount.h>
15 #include <linux/namei.h>
16 #include <linux/security.h>
17 #include <linux/writeback.h>
19 #define FS_IOC_GOINGDOWN _IOR('X', 125, __u32)
20 #define FSOP_GOING_FLAGS_DEFAULT 0x0 /* going down */
21 #define FSOP_GOING_FLAGS_LOGFLUSH 0x1 /* flush log but not data */
22 #define FSOP_GOING_FLAGS_NOLOGFLUSH 0x2 /* don't flush log nor data */
31 static int bch2_inode_flags_set(struct bch_inode_info *inode,
32 struct bch_inode_unpacked *bi,
35 struct bch_fs *c = inode->v.i_sb->s_fs_info;
37 * We're relying on btree locking here for exclusion with other ioctl
38 * calls - use the flags in the btree (@bi), not inode->i_flags:
40 struct flags_set *s = p;
41 unsigned newflags = s->flags;
42 unsigned oldflags = bi->bi_flags & s->mask;
44 if (((newflags ^ oldflags) & (BCH_INODE_APPEND|BCH_INODE_IMMUTABLE)) &&
45 !capable(CAP_LINUX_IMMUTABLE))
48 if (!S_ISREG(bi->bi_mode) &&
49 !S_ISDIR(bi->bi_mode) &&
50 (newflags & (BCH_INODE_NODUMP|BCH_INODE_NOATIME)) != newflags)
53 bi->bi_flags &= ~s->mask;
54 bi->bi_flags |= newflags;
56 bi->bi_ctime = timespec_to_bch2_time(c, current_time(&inode->v));
60 static int bch2_ioc_getflags(struct bch_inode_info *inode, int __user *arg)
62 unsigned flags = map_flags(bch_flags_to_uflags, inode->ei_inode.bi_flags);
64 return put_user(flags, arg);
67 static int bch2_ioc_setflags(struct bch_fs *c,
69 struct bch_inode_info *inode,
72 struct flags_set s = { .mask = map_defined(bch_flags_to_uflags) };
76 if (get_user(uflags, (int __user *) arg))
79 s.flags = map_flags_rev(bch_flags_to_uflags, uflags);
83 ret = mnt_want_write_file(file);
87 inode_lock(&inode->v);
88 if (!inode_owner_or_capable(file_mnt_user_ns(file), &inode->v)) {
93 mutex_lock(&inode->ei_update_lock);
94 ret = bch2_write_inode(c, inode, bch2_inode_flags_set, &s,
96 mutex_unlock(&inode->ei_update_lock);
99 inode_unlock(&inode->v);
100 mnt_drop_write_file(file);
104 static int bch2_ioc_fsgetxattr(struct bch_inode_info *inode,
105 struct fsxattr __user *arg)
107 struct fsxattr fa = { 0 };
109 fa.fsx_xflags = map_flags(bch_flags_to_xflags, inode->ei_inode.bi_flags);
110 fa.fsx_projid = inode->ei_qid.q[QTYP_PRJ];
112 return copy_to_user(arg, &fa, sizeof(fa));
115 static int fssetxattr_inode_update_fn(struct bch_inode_info *inode,
116 struct bch_inode_unpacked *bi,
119 struct flags_set *s = p;
121 if (s->projid != bi->bi_project) {
122 bi->bi_fields_set |= 1U << Inode_opt_project;
123 bi->bi_project = s->projid;
126 return bch2_inode_flags_set(inode, bi, p);
129 static int bch2_ioc_fssetxattr(struct bch_fs *c,
131 struct bch_inode_info *inode,
132 struct fsxattr __user *arg)
134 struct flags_set s = { .mask = map_defined(bch_flags_to_xflags) };
138 if (copy_from_user(&fa, arg, sizeof(fa)))
141 s.flags = map_flags_rev(bch_flags_to_xflags, fa.fsx_xflags);
145 if (fa.fsx_projid >= U32_MAX)
149 * inode fields accessible via the xattr interface are stored with a +1
150 * bias, so that 0 means unset:
152 s.projid = fa.fsx_projid + 1;
154 ret = mnt_want_write_file(file);
158 inode_lock(&inode->v);
159 if (!inode_owner_or_capable(file_mnt_user_ns(file), &inode->v)) {
164 mutex_lock(&inode->ei_update_lock);
165 ret = bch2_set_projid(c, inode, fa.fsx_projid);
169 ret = bch2_write_inode(c, inode, fssetxattr_inode_update_fn, &s,
172 mutex_unlock(&inode->ei_update_lock);
174 inode_unlock(&inode->v);
175 mnt_drop_write_file(file);
179 static int bch2_reinherit_attrs_fn(struct bch_inode_info *inode,
180 struct bch_inode_unpacked *bi,
183 struct bch_inode_info *dir = p;
185 return !bch2_reinherit_attrs(bi, &dir->ei_inode);
188 static int bch2_ioc_reinherit_attrs(struct bch_fs *c,
190 struct bch_inode_info *src,
191 const char __user *name)
193 struct bch_hash_info hash = bch2_hash_info_init(c, &src->ei_inode);
194 struct bch_inode_info *dst;
195 struct inode *vinode = NULL;
201 kname = kmalloc(BCH_NAME_MAX + 1, GFP_KERNEL);
205 ret = strncpy_from_user(kname, name, BCH_NAME_MAX);
206 if (unlikely(ret < 0))
212 ret = bch2_dirent_lookup(c, inode_inum(src), &hash, &qstr, &inum);
216 vinode = bch2_vfs_inode_get(c, inum);
217 ret = PTR_ERR_OR_ZERO(vinode);
221 dst = to_bch_ei(vinode);
223 ret = mnt_want_write_file(file);
227 bch2_lock_inodes(INODE_UPDATE_LOCK, src, dst);
229 if (inode_attr_changing(src, dst, Inode_opt_project)) {
230 ret = bch2_fs_quota_transfer(c, dst,
233 KEY_TYPE_QUOTA_PREALLOC);
238 ret = bch2_write_inode(c, dst, bch2_reinherit_attrs_fn, src, 0);
240 bch2_unlock_inodes(INODE_UPDATE_LOCK, src, dst);
242 /* return true if we did work */
246 mnt_drop_write_file(file);
255 static int bch2_ioc_goingdown(struct bch_fs *c, u32 __user *arg)
260 if (!capable(CAP_SYS_ADMIN))
263 if (get_user(flags, arg))
266 bch_notice(c, "shutdown by ioctl type %u", flags);
268 down_write(&c->vfs_sb->s_umount);
271 case FSOP_GOING_FLAGS_DEFAULT:
272 ret = freeze_bdev(c->vfs_sb->s_bdev);
276 bch2_journal_flush(&c->journal);
277 c->vfs_sb->s_flags |= SB_RDONLY;
278 bch2_fs_emergency_read_only(c);
279 thaw_bdev(c->vfs_sb->s_bdev);
282 case FSOP_GOING_FLAGS_LOGFLUSH:
283 bch2_journal_flush(&c->journal);
286 case FSOP_GOING_FLAGS_NOLOGFLUSH:
287 c->vfs_sb->s_flags |= SB_RDONLY;
288 bch2_fs_emergency_read_only(c);
295 up_write(&c->vfs_sb->s_umount);
299 static long bch2_ioctl_subvolume_create(struct bch_fs *c, struct file *filp,
300 struct bch_ioctl_subvolume arg)
303 struct bch_inode_info *inode;
304 struct user_namespace *s_user_ns;
305 struct dentry *dst_dentry;
306 struct path src_path, dst_path;
307 int how = LOOKUP_FOLLOW;
309 subvol_inum snapshot_src = { 0 };
310 unsigned lookup_flags = 0;
311 unsigned create_flags = BCH_CREATE_SUBVOL;
313 if (arg.flags & ~(BCH_SUBVOL_SNAPSHOT_CREATE|
314 BCH_SUBVOL_SNAPSHOT_RO))
317 if (!(arg.flags & BCH_SUBVOL_SNAPSHOT_CREATE) &&
319 (arg.flags & BCH_SUBVOL_SNAPSHOT_RO)))
322 if (arg.flags & BCH_SUBVOL_SNAPSHOT_CREATE)
323 create_flags |= BCH_CREATE_SNAPSHOT;
325 if (arg.flags & BCH_SUBVOL_SNAPSHOT_RO)
326 create_flags |= BCH_CREATE_SNAPSHOT_RO;
328 /* why do we need this lock? */
329 down_read(&c->vfs_sb->s_umount);
331 if (arg.flags & BCH_SUBVOL_SNAPSHOT_CREATE)
332 sync_inodes_sb(c->vfs_sb);
335 error = user_path_at(arg.dirfd,
336 (const char __user *)(unsigned long)arg.src_ptr,
341 if (src_path.dentry->d_sb->s_fs_info != c) {
347 snapshot_src = inode_inum(to_bch_ei(src_path.dentry->d_inode));
350 dst_dentry = user_path_create(arg.dirfd,
351 (const char __user *)(unsigned long)arg.dst_ptr,
352 &dst_path, lookup_flags);
353 error = PTR_ERR_OR_ZERO(dst_dentry);
357 if (dst_dentry->d_sb->s_fs_info != c) {
362 if (dst_dentry->d_inode) {
367 dir = dst_path.dentry->d_inode;
368 if (IS_DEADDIR(dir)) {
373 s_user_ns = dir->i_sb->s_user_ns;
374 if (!kuid_has_mapping(s_user_ns, current_fsuid()) ||
375 !kgid_has_mapping(s_user_ns, current_fsgid())) {
380 error = inode_permission(file_mnt_user_ns(filp),
381 dir, MAY_WRITE | MAY_EXEC);
385 if (!IS_POSIXACL(dir))
386 arg.mode &= ~current_umask();
388 error = security_path_mkdir(&dst_path, dst_dentry, arg.mode);
392 if ((arg.flags & BCH_SUBVOL_SNAPSHOT_CREATE) &&
394 snapshot_src.subvol = to_bch_ei(dir)->ei_inode.bi_subvol;
396 inode = __bch2_create(file_mnt_user_ns(filp), to_bch_ei(dir),
397 dst_dentry, arg.mode|S_IFDIR,
398 0, snapshot_src, create_flags);
399 error = PTR_ERR_OR_ZERO(inode);
403 d_instantiate(dst_dentry, &inode->v);
404 fsnotify_mkdir(dir, dst_dentry);
406 done_path_create(&dst_path, dst_dentry);
411 if (retry_estale(error, lookup_flags)) {
412 lookup_flags |= LOOKUP_REVAL;
416 up_read(&c->vfs_sb->s_umount);
421 static long bch2_ioctl_subvolume_destroy(struct bch_fs *c, struct file *filp,
422 struct bch_ioctl_subvolume arg)
431 ret = user_path_at(arg.dirfd,
432 (const char __user *)(unsigned long)arg.dst_ptr,
433 LOOKUP_FOLLOW, &path);
437 if (path.dentry->d_sb->s_fs_info != c) {
442 dir = path.dentry->d_parent->d_inode;
444 ret = __bch2_unlink(dir, path.dentry, true);
446 fsnotify_rmdir(dir, path.dentry);
447 d_delete(path.dentry);
454 long bch2_fs_file_ioctl(struct file *file, unsigned cmd, unsigned long arg)
456 struct bch_inode_info *inode = file_bch_inode(file);
457 struct bch_fs *c = inode->v.i_sb->s_fs_info;
461 case FS_IOC_GETFLAGS:
462 ret = bch2_ioc_getflags(inode, (int __user *) arg);
465 case FS_IOC_SETFLAGS:
466 ret = bch2_ioc_setflags(c, file, inode, (int __user *) arg);
469 case FS_IOC_FSGETXATTR:
470 ret = bch2_ioc_fsgetxattr(inode, (void __user *) arg);
473 case FS_IOC_FSSETXATTR:
474 ret = bch2_ioc_fssetxattr(c, file, inode,
475 (void __user *) arg);
478 case BCHFS_IOC_REINHERIT_ATTRS:
479 ret = bch2_ioc_reinherit_attrs(c, file, inode,
480 (void __user *) arg);
483 case FS_IOC_GETVERSION:
487 case FS_IOC_SETVERSION:
491 case FS_IOC_GOINGDOWN:
492 ret = bch2_ioc_goingdown(c, (u32 __user *) arg);
495 case BCH_IOCTL_SUBVOLUME_CREATE: {
496 struct bch_ioctl_subvolume i;
498 ret = copy_from_user(&i, (void __user *) arg, sizeof(i))
500 : bch2_ioctl_subvolume_create(c, file, i);
504 case BCH_IOCTL_SUBVOLUME_DESTROY: {
505 struct bch_ioctl_subvolume i;
507 ret = copy_from_user(&i, (void __user *) arg, sizeof(i))
509 : bch2_ioctl_subvolume_destroy(c, file, i);
514 ret = bch2_fs_ioctl(c, cmd, (void __user *) arg);
518 return bch2_err_class(ret);
522 long bch2_compat_fs_ioctl(struct file *file, unsigned cmd, unsigned long arg)
524 /* These are just misnamed, they actually get/put from/to user an int */
526 case FS_IOC_GETFLAGS:
527 cmd = FS_IOC_GETFLAGS;
529 case FS_IOC32_SETFLAGS:
530 cmd = FS_IOC_SETFLAGS;
535 return bch2_fs_file_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
539 #endif /* NO_BCACHEFS_FS */