]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/fs-ioctl.c
Update bcachefs sources to 96b991466a bcachefs: Improve error message in fsck
[bcachefs-tools-debian] / libbcachefs / fs-ioctl.c
1 // SPDX-License-Identifier: GPL-2.0
2 #ifndef NO_BCACHEFS_FS
3
4 #include "bcachefs.h"
5 #include "chardev.h"
6 #include "dirent.h"
7 #include "fs.h"
8 #include "fs-common.h"
9 #include "fs-ioctl.h"
10 #include "quota.h"
11
12 #include <linux/compat.h>
13 #include <linux/mount.h>
14
15 #define FS_IOC_GOINGDOWN             _IOR('X', 125, __u32)
16
17 struct flags_set {
18         unsigned                mask;
19         unsigned                flags;
20
21         unsigned                projid;
22 };
23
24 static int bch2_inode_flags_set(struct bch_inode_info *inode,
25                                 struct bch_inode_unpacked *bi,
26                                 void *p)
27 {
28         struct bch_fs *c = inode->v.i_sb->s_fs_info;
29         /*
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:
32          */
33         struct flags_set *s = p;
34         unsigned newflags = s->flags;
35         unsigned oldflags = bi->bi_flags & s->mask;
36
37         if (((newflags ^ oldflags) & (BCH_INODE_APPEND|BCH_INODE_IMMUTABLE)) &&
38             !capable(CAP_LINUX_IMMUTABLE))
39                 return -EPERM;
40
41         if (!S_ISREG(bi->bi_mode) &&
42             !S_ISDIR(bi->bi_mode) &&
43             (newflags & (BCH_INODE_NODUMP|BCH_INODE_NOATIME)) != newflags)
44                 return -EINVAL;
45
46         bi->bi_flags &= ~s->mask;
47         bi->bi_flags |= newflags;
48
49         bi->bi_ctime = timespec_to_bch2_time(c, current_time(&inode->v));
50         return 0;
51 }
52
53 static int bch2_ioc_getflags(struct bch_inode_info *inode, int __user *arg)
54 {
55         unsigned flags = map_flags(bch_flags_to_uflags, inode->ei_inode.bi_flags);
56
57         return put_user(flags, arg);
58 }
59
60 static int bch2_ioc_setflags(struct bch_fs *c,
61                              struct file *file,
62                              struct bch_inode_info *inode,
63                              void __user *arg)
64 {
65         struct flags_set s = { .mask = map_defined(bch_flags_to_uflags) };
66         unsigned uflags;
67         int ret;
68
69         if (get_user(uflags, (int __user *) arg))
70                 return -EFAULT;
71
72         s.flags = map_flags_rev(bch_flags_to_uflags, uflags);
73         if (uflags)
74                 return -EOPNOTSUPP;
75
76         ret = mnt_want_write_file(file);
77         if (ret)
78                 return ret;
79
80         inode_lock(&inode->v);
81         if (!inode_owner_or_capable(&inode->v)) {
82                 ret = -EACCES;
83                 goto setflags_out;
84         }
85
86         mutex_lock(&inode->ei_update_lock);
87         ret = bch2_write_inode(c, inode, bch2_inode_flags_set, &s,
88                                ATTR_CTIME);
89         mutex_unlock(&inode->ei_update_lock);
90
91 setflags_out:
92         inode_unlock(&inode->v);
93         mnt_drop_write_file(file);
94         return ret;
95 }
96
97 static int bch2_ioc_fsgetxattr(struct bch_inode_info *inode,
98                                struct fsxattr __user *arg)
99 {
100         struct fsxattr fa = { 0 };
101
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];
104
105         return copy_to_user(arg, &fa, sizeof(fa));
106 }
107
108 static int fssetxattr_inode_update_fn(struct bch_inode_info *inode,
109                                       struct bch_inode_unpacked *bi,
110                                       void *p)
111 {
112         struct flags_set *s = p;
113
114         if (s->projid != bi->bi_project) {
115                 bi->bi_fields_set |= 1U << Inode_opt_project;
116                 bi->bi_project = s->projid;
117         }
118
119         return bch2_inode_flags_set(inode, bi, p);
120 }
121
122 static int bch2_ioc_fssetxattr(struct bch_fs *c,
123                                struct file *file,
124                                struct bch_inode_info *inode,
125                                struct fsxattr __user *arg)
126 {
127         struct flags_set s = { .mask = map_defined(bch_flags_to_xflags) };
128         struct fsxattr fa;
129         int ret;
130
131         if (copy_from_user(&fa, arg, sizeof(fa)))
132                 return -EFAULT;
133
134         s.flags = map_flags_rev(bch_flags_to_xflags, fa.fsx_xflags);
135         if (fa.fsx_xflags)
136                 return -EOPNOTSUPP;
137
138         if (fa.fsx_projid >= U32_MAX)
139                 return -EINVAL;
140
141         s.projid = fa.fsx_projid + 1;
142
143         ret = mnt_want_write_file(file);
144         if (ret)
145                 return ret;
146
147         inode_lock(&inode->v);
148         if (!inode_owner_or_capable(&inode->v)) {
149                 ret = -EACCES;
150                 goto err;
151         }
152
153         mutex_lock(&inode->ei_update_lock);
154         ret = bch2_set_projid(c, inode, s.projid);
155         if (ret)
156                 goto err_unlock;
157
158         ret = bch2_write_inode(c, inode, fssetxattr_inode_update_fn, &s,
159                                ATTR_CTIME);
160 err_unlock:
161         mutex_unlock(&inode->ei_update_lock);
162 err:
163         inode_unlock(&inode->v);
164         mnt_drop_write_file(file);
165         return ret;
166 }
167
168 static int bch2_reinherit_attrs_fn(struct bch_inode_info *inode,
169                                    struct bch_inode_unpacked *bi,
170                                    void *p)
171 {
172         struct bch_inode_info *dir = p;
173
174         return !bch2_reinherit_attrs(bi, &dir->ei_inode);
175 }
176
177 static int bch2_ioc_reinherit_attrs(struct bch_fs *c,
178                                     struct file *file,
179                                     struct bch_inode_info *src,
180                                     const char __user *name)
181 {
182         struct bch_inode_info *dst;
183         struct inode *vinode = NULL;
184         char *kname = NULL;
185         struct qstr qstr;
186         int ret = 0;
187         u64 inum;
188
189         kname = kmalloc(BCH_NAME_MAX + 1, GFP_KERNEL);
190         if (!kname)
191                 return -ENOMEM;
192
193         ret = strncpy_from_user(kname, name, BCH_NAME_MAX);
194         if (unlikely(ret < 0))
195                 goto err1;
196
197         qstr.len        = ret;
198         qstr.name       = kname;
199
200         ret = -ENOENT;
201         inum = bch2_dirent_lookup(c, src->v.i_ino,
202                                   &src->ei_str_hash,
203                                   &qstr);
204         if (!inum)
205                 goto err1;
206
207         vinode = bch2_vfs_inode_get(c, inum);
208         ret = PTR_ERR_OR_ZERO(vinode);
209         if (ret)
210                 goto err1;
211
212         dst = to_bch_ei(vinode);
213
214         ret = mnt_want_write_file(file);
215         if (ret)
216                 goto err2;
217
218         bch2_lock_inodes(INODE_UPDATE_LOCK, src, dst);
219
220         if (inode_attr_changing(src, dst, Inode_opt_project)) {
221                 ret = bch2_fs_quota_transfer(c, dst,
222                                              src->ei_qid,
223                                              1 << QTYP_PRJ,
224                                              KEY_TYPE_QUOTA_PREALLOC);
225                 if (ret)
226                         goto err3;
227         }
228
229         ret = bch2_write_inode(c, dst, bch2_reinherit_attrs_fn, src, 0);
230 err3:
231         bch2_unlock_inodes(INODE_UPDATE_LOCK, src, dst);
232
233         /* return true if we did work */
234         if (ret >= 0)
235                 ret = !ret;
236
237         mnt_drop_write_file(file);
238 err2:
239         iput(vinode);
240 err1:
241         kfree(kname);
242
243         return ret;
244 }
245
246 long bch2_fs_file_ioctl(struct file *file, unsigned cmd, unsigned long arg)
247 {
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;
251
252         switch (cmd) {
253         case FS_IOC_GETFLAGS:
254                 return bch2_ioc_getflags(inode, (int __user *) arg);
255
256         case FS_IOC_SETFLAGS:
257                 return bch2_ioc_setflags(c, file, inode, (int __user *) arg);
258
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);
264
265         case BCHFS_IOC_REINHERIT_ATTRS:
266                 return bch2_ioc_reinherit_attrs(c, file, inode,
267                                                 (void __user *) arg);
268
269         case FS_IOC_GETVERSION:
270                 return -ENOTTY;
271         case FS_IOC_SETVERSION:
272                 return -ENOTTY;
273
274         case FS_IOC_GOINGDOWN:
275                 if (!capable(CAP_SYS_ADMIN))
276                         return -EPERM;
277
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);
283                 return 0;
284
285         default:
286                 return bch2_fs_ioctl(c, cmd, (void __user *) arg);
287         }
288 }
289
290 #ifdef CONFIG_COMPAT
291 long bch2_compat_fs_ioctl(struct file *file, unsigned cmd, unsigned long arg)
292 {
293         /* These are just misnamed, they actually get/put from/to user an int */
294         switch (cmd) {
295         case FS_IOC_GETFLAGS:
296                 cmd = FS_IOC_GETFLAGS;
297                 break;
298         case FS_IOC32_SETFLAGS:
299                 cmd = FS_IOC_SETFLAGS;
300                 break;
301         default:
302                 return -ENOIOCTLCMD;
303         }
304         return bch2_fs_file_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
305 }
306 #endif
307
308 #endif /* NO_BCACHEFS_FS */