1 // SPDX-License-Identifier: GPL-2.0
12 #include <linux/compat.h>
13 #include <linux/mount.h>
15 #define FS_IOC_GOINGDOWN _IOR('X', 125, __u32)
24 static int bch2_inode_flags_set(struct bch_inode_info *inode,
25 struct bch_inode_unpacked *bi,
28 struct bch_fs *c = inode->v.i_sb->s_fs_info;
30 * We're relying on btree locking here for exclusion with other ioctl
31 * calls - use the flags in the btree (@bi), not inode->i_flags:
33 struct flags_set *s = p;
34 unsigned newflags = s->flags;
35 unsigned oldflags = bi->bi_flags & s->mask;
37 if (((newflags ^ oldflags) & (BCH_INODE_APPEND|BCH_INODE_IMMUTABLE)) &&
38 !capable(CAP_LINUX_IMMUTABLE))
41 if (!S_ISREG(bi->bi_mode) &&
42 !S_ISDIR(bi->bi_mode) &&
43 (newflags & (BCH_INODE_NODUMP|BCH_INODE_NOATIME)) != newflags)
46 bi->bi_flags &= ~s->mask;
47 bi->bi_flags |= newflags;
49 bi->bi_ctime = timespec_to_bch2_time(c, current_time(&inode->v));
53 static int bch2_ioc_getflags(struct bch_inode_info *inode, int __user *arg)
55 unsigned flags = map_flags(bch_flags_to_uflags, inode->ei_inode.bi_flags);
57 return put_user(flags, arg);
60 static int bch2_ioc_setflags(struct bch_fs *c,
62 struct bch_inode_info *inode,
65 struct flags_set s = { .mask = map_defined(bch_flags_to_uflags) };
69 if (get_user(uflags, (int __user *) arg))
72 s.flags = map_flags_rev(bch_flags_to_uflags, uflags);
76 ret = mnt_want_write_file(file);
80 inode_lock(&inode->v);
81 if (!inode_owner_or_capable(&inode->v)) {
86 mutex_lock(&inode->ei_update_lock);
87 ret = bch2_write_inode(c, inode, bch2_inode_flags_set, &s,
89 mutex_unlock(&inode->ei_update_lock);
92 inode_unlock(&inode->v);
93 mnt_drop_write_file(file);
97 static int bch2_ioc_fsgetxattr(struct bch_inode_info *inode,
98 struct fsxattr __user *arg)
100 struct fsxattr fa = { 0 };
102 fa.fsx_xflags = map_flags(bch_flags_to_xflags, inode->ei_inode.bi_flags);
103 fa.fsx_projid = inode->ei_qid.q[QTYP_PRJ];
105 return copy_to_user(arg, &fa, sizeof(fa));
108 static int fssetxattr_inode_update_fn(struct bch_inode_info *inode,
109 struct bch_inode_unpacked *bi,
112 struct flags_set *s = p;
114 if (s->projid != bi->bi_project) {
115 bi->bi_fields_set |= 1U << Inode_opt_project;
116 bi->bi_project = s->projid;
119 return bch2_inode_flags_set(inode, bi, p);
122 static int bch2_ioc_fssetxattr(struct bch_fs *c,
124 struct bch_inode_info *inode,
125 struct fsxattr __user *arg)
127 struct flags_set s = { .mask = map_defined(bch_flags_to_xflags) };
131 if (copy_from_user(&fa, arg, sizeof(fa)))
134 s.flags = map_flags_rev(bch_flags_to_xflags, fa.fsx_xflags);
138 if (fa.fsx_projid >= U32_MAX)
142 * inode fields accessible via the xattr interface are stored with a +1
143 * bias, so that 0 means unset:
145 s.projid = fa.fsx_projid + 1;
147 ret = mnt_want_write_file(file);
151 inode_lock(&inode->v);
152 if (!inode_owner_or_capable(&inode->v)) {
157 mutex_lock(&inode->ei_update_lock);
158 ret = bch2_set_projid(c, inode, fa.fsx_projid);
162 ret = bch2_write_inode(c, inode, fssetxattr_inode_update_fn, &s,
165 mutex_unlock(&inode->ei_update_lock);
167 inode_unlock(&inode->v);
168 mnt_drop_write_file(file);
172 static int bch2_reinherit_attrs_fn(struct bch_inode_info *inode,
173 struct bch_inode_unpacked *bi,
176 struct bch_inode_info *dir = p;
178 return !bch2_reinherit_attrs(bi, &dir->ei_inode);
181 static int bch2_ioc_reinherit_attrs(struct bch_fs *c,
183 struct bch_inode_info *src,
184 const char __user *name)
186 struct bch_inode_info *dst;
187 struct inode *vinode = NULL;
193 kname = kmalloc(BCH_NAME_MAX + 1, GFP_KERNEL);
197 ret = strncpy_from_user(kname, name, BCH_NAME_MAX);
198 if (unlikely(ret < 0))
205 inum = bch2_dirent_lookup(c, src->v.i_ino,
211 vinode = bch2_vfs_inode_get(c, inum);
212 ret = PTR_ERR_OR_ZERO(vinode);
216 dst = to_bch_ei(vinode);
218 ret = mnt_want_write_file(file);
222 bch2_lock_inodes(INODE_UPDATE_LOCK, src, dst);
224 if (inode_attr_changing(src, dst, Inode_opt_project)) {
225 ret = bch2_fs_quota_transfer(c, dst,
228 KEY_TYPE_QUOTA_PREALLOC);
233 ret = bch2_write_inode(c, dst, bch2_reinherit_attrs_fn, src, 0);
235 bch2_unlock_inodes(INODE_UPDATE_LOCK, src, dst);
237 /* return true if we did work */
241 mnt_drop_write_file(file);
250 long bch2_fs_file_ioctl(struct file *file, unsigned cmd, unsigned long arg)
252 struct bch_inode_info *inode = file_bch_inode(file);
253 struct super_block *sb = inode->v.i_sb;
254 struct bch_fs *c = sb->s_fs_info;
257 case FS_IOC_GETFLAGS:
258 return bch2_ioc_getflags(inode, (int __user *) arg);
260 case FS_IOC_SETFLAGS:
261 return bch2_ioc_setflags(c, file, inode, (int __user *) arg);
263 case FS_IOC_FSGETXATTR:
264 return bch2_ioc_fsgetxattr(inode, (void __user *) arg);
265 case FS_IOC_FSSETXATTR:
266 return bch2_ioc_fssetxattr(c, file, inode,
267 (void __user *) arg);
269 case BCHFS_IOC_REINHERIT_ATTRS:
270 return bch2_ioc_reinherit_attrs(c, file, inode,
271 (void __user *) arg);
273 case FS_IOC_GETVERSION:
275 case FS_IOC_SETVERSION:
278 case FS_IOC_GOINGDOWN:
279 if (!capable(CAP_SYS_ADMIN))
282 down_write(&sb->s_umount);
283 sb->s_flags |= SB_RDONLY;
284 if (bch2_fs_emergency_read_only(c))
285 bch_err(c, "emergency read only due to ioctl");
286 up_write(&sb->s_umount);
290 return bch2_fs_ioctl(c, cmd, (void __user *) arg);
295 long bch2_compat_fs_ioctl(struct file *file, unsigned cmd, unsigned long arg)
297 /* These are just misnamed, they actually get/put from/to user an int */
299 case FS_IOC_GETFLAGS:
300 cmd = FS_IOC_GETFLAGS;
302 case FS_IOC32_SETFLAGS:
303 cmd = FS_IOC_SETFLAGS;
308 return bch2_fs_file_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
312 #endif /* NO_BCACHEFS_FS */