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)
141 s.projid = fa.fsx_projid + 1;
143 ret = mnt_want_write_file(file);
147 inode_lock(&inode->v);
148 if (!inode_owner_or_capable(&inode->v)) {
153 mutex_lock(&inode->ei_update_lock);
154 ret = bch2_set_projid(c, inode, s.projid);
158 ret = bch2_write_inode(c, inode, fssetxattr_inode_update_fn, &s,
161 mutex_unlock(&inode->ei_update_lock);
163 inode_unlock(&inode->v);
164 mnt_drop_write_file(file);
168 static int bch2_reinherit_attrs_fn(struct bch_inode_info *inode,
169 struct bch_inode_unpacked *bi,
172 struct bch_inode_info *dir = p;
174 return !bch2_reinherit_attrs(bi, &dir->ei_inode);
177 static int bch2_ioc_reinherit_attrs(struct bch_fs *c,
179 struct bch_inode_info *src,
180 const char __user *name)
182 struct bch_inode_info *dst;
183 struct inode *vinode = NULL;
189 kname = kmalloc(BCH_NAME_MAX + 1, GFP_KERNEL);
193 ret = strncpy_from_user(kname, name, BCH_NAME_MAX);
194 if (unlikely(ret < 0))
201 inum = bch2_dirent_lookup(c, src->v.i_ino,
207 vinode = bch2_vfs_inode_get(c, inum);
208 ret = PTR_ERR_OR_ZERO(vinode);
212 dst = to_bch_ei(vinode);
214 ret = mnt_want_write_file(file);
218 bch2_lock_inodes(INODE_UPDATE_LOCK, src, dst);
220 if (inode_attr_changing(src, dst, Inode_opt_project)) {
221 ret = bch2_fs_quota_transfer(c, dst,
224 KEY_TYPE_QUOTA_PREALLOC);
229 ret = bch2_write_inode(c, dst, bch2_reinherit_attrs_fn, src, 0);
231 bch2_unlock_inodes(INODE_UPDATE_LOCK, src, dst);
233 /* return true if we did work */
237 mnt_drop_write_file(file);
246 long bch2_fs_file_ioctl(struct file *file, unsigned cmd, unsigned long arg)
248 struct bch_inode_info *inode = file_bch_inode(file);
249 struct super_block *sb = inode->v.i_sb;
250 struct bch_fs *c = sb->s_fs_info;
253 case FS_IOC_GETFLAGS:
254 return bch2_ioc_getflags(inode, (int __user *) arg);
256 case FS_IOC_SETFLAGS:
257 return bch2_ioc_setflags(c, file, inode, (int __user *) arg);
259 case FS_IOC_FSGETXATTR:
260 return bch2_ioc_fsgetxattr(inode, (void __user *) arg);
261 case FS_IOC_FSSETXATTR:
262 return bch2_ioc_fssetxattr(c, file, inode,
263 (void __user *) arg);
265 case BCHFS_IOC_REINHERIT_ATTRS:
266 return bch2_ioc_reinherit_attrs(c, file, inode,
267 (void __user *) arg);
269 case FS_IOC_GETVERSION:
271 case FS_IOC_SETVERSION:
274 case FS_IOC_GOINGDOWN:
275 if (!capable(CAP_SYS_ADMIN))
278 down_write(&sb->s_umount);
279 sb->s_flags |= SB_RDONLY;
280 if (bch2_fs_emergency_read_only(c))
281 bch_err(c, "emergency read only due to ioctl");
282 up_write(&sb->s_umount);
286 return bch2_fs_ioctl(c, cmd, (void __user *) arg);
291 long bch2_compat_fs_ioctl(struct file *file, unsigned cmd, unsigned long arg)
293 /* These are just misnamed, they actually get/put from/to user an int */
295 case FS_IOC_GETFLAGS:
296 cmd = FS_IOC_GETFLAGS;
298 case FS_IOC32_SETFLAGS:
299 cmd = FS_IOC_SETFLAGS;
304 return bch2_fs_file_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
308 #endif /* NO_BCACHEFS_FS */