1 // SPDX-License-Identifier: GPL-2.0
11 #include <linux/compat.h>
12 #include <linux/mount.h>
14 #define FS_IOC_GOINGDOWN _IOR('X', 125, __u32)
23 static int bch2_inode_flags_set(struct bch_inode_info *inode,
24 struct bch_inode_unpacked *bi,
27 struct bch_fs *c = inode->v.i_sb->s_fs_info;
29 * We're relying on btree locking here for exclusion with other ioctl
30 * calls - use the flags in the btree (@bi), not inode->i_flags:
32 struct flags_set *s = p;
33 unsigned newflags = s->flags;
34 unsigned oldflags = bi->bi_flags & s->mask;
36 if (((newflags ^ oldflags) & (BCH_INODE_APPEND|BCH_INODE_IMMUTABLE)) &&
37 !capable(CAP_LINUX_IMMUTABLE))
40 if (!S_ISREG(bi->bi_mode) &&
41 !S_ISDIR(bi->bi_mode) &&
42 (newflags & (BCH_INODE_NODUMP|BCH_INODE_NOATIME)) != newflags)
45 bi->bi_flags &= ~s->mask;
46 bi->bi_flags |= newflags;
48 bi->bi_ctime = timespec_to_bch2_time(c, current_time(&inode->v));
52 static int bch2_ioc_getflags(struct bch_inode_info *inode, int __user *arg)
54 unsigned flags = map_flags(bch_flags_to_uflags, inode->ei_inode.bi_flags);
56 return put_user(flags, arg);
59 static int bch2_ioc_setflags(struct bch_fs *c,
61 struct bch_inode_info *inode,
64 struct flags_set s = { .mask = map_defined(bch_flags_to_uflags) };
68 if (get_user(uflags, (int __user *) arg))
71 s.flags = map_flags_rev(bch_flags_to_uflags, uflags);
75 ret = mnt_want_write_file(file);
79 inode_lock(&inode->v);
80 if (!inode_owner_or_capable(&inode->v)) {
85 mutex_lock(&inode->ei_update_lock);
86 ret = bch2_write_inode(c, inode, bch2_inode_flags_set, &s,
88 mutex_unlock(&inode->ei_update_lock);
91 inode_unlock(&inode->v);
92 mnt_drop_write_file(file);
96 static int bch2_ioc_fsgetxattr(struct bch_inode_info *inode,
97 struct fsxattr __user *arg)
99 struct fsxattr fa = { 0 };
101 fa.fsx_xflags = map_flags(bch_flags_to_xflags, inode->ei_inode.bi_flags);
102 fa.fsx_projid = inode->ei_qid.q[QTYP_PRJ];
104 return copy_to_user(arg, &fa, sizeof(fa));
107 static int fssetxattr_inode_update_fn(struct bch_inode_info *inode,
108 struct bch_inode_unpacked *bi,
111 struct flags_set *s = p;
113 if (s->projid != bi->bi_project) {
114 bi->bi_fields_set |= 1U << Inode_opt_project;
115 bi->bi_project = s->projid;
118 return bch2_inode_flags_set(inode, bi, p);
121 static int bch2_ioc_fssetxattr(struct bch_fs *c,
123 struct bch_inode_info *inode,
124 struct fsxattr __user *arg)
126 struct flags_set s = { .mask = map_defined(bch_flags_to_xflags) };
130 if (copy_from_user(&fa, arg, sizeof(fa)))
133 s.flags = map_flags_rev(bch_flags_to_xflags, fa.fsx_xflags);
137 if (fa.fsx_projid >= U32_MAX)
140 s.projid = fa.fsx_projid + 1;
142 ret = mnt_want_write_file(file);
146 inode_lock(&inode->v);
147 if (!inode_owner_or_capable(&inode->v)) {
152 mutex_lock(&inode->ei_update_lock);
153 ret = bch2_set_projid(c, inode, s.projid);
157 ret = bch2_write_inode(c, inode, fssetxattr_inode_update_fn, &s,
160 mutex_unlock(&inode->ei_update_lock);
162 inode_unlock(&inode->v);
163 mnt_drop_write_file(file);
167 static int bch2_ioc_reinherit_attrs(struct bch_fs *c,
169 struct bch_inode_info *src,
170 const char __user *name)
172 struct bch_inode_info *dst;
173 struct inode *vinode = NULL;
179 kname = kmalloc(BCH_NAME_MAX + 1, GFP_KERNEL);
183 ret = strncpy_from_user(kname, name, BCH_NAME_MAX);
184 if (unlikely(ret < 0))
191 inum = bch2_dirent_lookup(c, src->v.i_ino,
197 vinode = bch2_vfs_inode_get(c, inum);
198 ret = PTR_ERR_OR_ZERO(vinode);
202 dst = to_bch_ei(vinode);
204 ret = mnt_want_write_file(file);
208 bch2_lock_inodes(INODE_UPDATE_LOCK, src, dst);
210 if (inode_attr_changing(src, dst, Inode_opt_project)) {
211 ret = bch2_fs_quota_transfer(c, dst,
214 KEY_TYPE_QUOTA_PREALLOC);
219 ret = bch2_write_inode(c, dst, bch2_reinherit_attrs_fn, src, 0);
221 bch2_unlock_inodes(INODE_UPDATE_LOCK, src, dst);
223 /* return true if we did work */
227 mnt_drop_write_file(file);
236 long bch2_fs_file_ioctl(struct file *file, unsigned cmd, unsigned long arg)
238 struct bch_inode_info *inode = file_bch_inode(file);
239 struct super_block *sb = inode->v.i_sb;
240 struct bch_fs *c = sb->s_fs_info;
243 case FS_IOC_GETFLAGS:
244 return bch2_ioc_getflags(inode, (int __user *) arg);
246 case FS_IOC_SETFLAGS:
247 return bch2_ioc_setflags(c, file, inode, (int __user *) arg);
249 case FS_IOC_FSGETXATTR:
250 return bch2_ioc_fsgetxattr(inode, (void __user *) arg);
251 case FS_IOC_FSSETXATTR:
252 return bch2_ioc_fssetxattr(c, file, inode,
253 (void __user *) arg);
255 case BCHFS_IOC_REINHERIT_ATTRS:
256 return bch2_ioc_reinherit_attrs(c, file, inode,
257 (void __user *) arg);
259 case FS_IOC_GETVERSION:
261 case FS_IOC_SETVERSION:
264 case FS_IOC_GOINGDOWN:
265 if (!capable(CAP_SYS_ADMIN))
268 down_write(&sb->s_umount);
269 sb->s_flags |= SB_RDONLY;
270 if (bch2_fs_emergency_read_only(c))
271 bch_err(c, "emergency read only due to ioctl");
272 up_write(&sb->s_umount);
276 return bch2_fs_ioctl(c, cmd, (void __user *) arg);
281 long bch2_compat_fs_ioctl(struct file *file, unsigned cmd, unsigned long arg)
283 /* These are just misnamed, they actually get/put from/to user an int */
285 case FS_IOC_GETFLAGS:
286 cmd = FS_IOC_GETFLAGS;
288 case FS_IOC32_SETFLAGS:
289 cmd = FS_IOC_SETFLAGS;
294 return bch2_fs_file_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
298 #endif /* NO_BCACHEFS_FS */