]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/fs-ioctl.c
cmd_set_passphrase: revoke the invalidated key
[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/fsnotify.h>
14 #include <linux/mount.h>
15 #include <linux/namei.h>
16 #include <linux/security.h>
17 #include <linux/writeback.h>
18
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 */
23
24 struct flags_set {
25         unsigned                mask;
26         unsigned                flags;
27
28         unsigned                projid;
29
30         bool                    set_projinherit;
31         bool                    projinherit;
32 };
33
34 static int bch2_inode_flags_set(struct btree_trans *trans,
35                                 struct bch_inode_info *inode,
36                                 struct bch_inode_unpacked *bi,
37                                 void *p)
38 {
39         struct bch_fs *c = inode->v.i_sb->s_fs_info;
40         /*
41          * We're relying on btree locking here for exclusion with other ioctl
42          * calls - use the flags in the btree (@bi), not inode->i_flags:
43          */
44         struct flags_set *s = p;
45         unsigned newflags = s->flags;
46         unsigned oldflags = bi->bi_flags & s->mask;
47
48         if (((newflags ^ oldflags) & (BCH_INODE_APPEND|BCH_INODE_IMMUTABLE)) &&
49             !capable(CAP_LINUX_IMMUTABLE))
50                 return -EPERM;
51
52         if (!S_ISREG(bi->bi_mode) &&
53             !S_ISDIR(bi->bi_mode) &&
54             (newflags & (BCH_INODE_NODUMP|BCH_INODE_NOATIME)) != newflags)
55                 return -EINVAL;
56
57         if (s->set_projinherit) {
58                 bi->bi_fields_set &= ~(1 << Inode_opt_project);
59                 bi->bi_fields_set |= ((int) s->projinherit << Inode_opt_project);
60         }
61
62         bi->bi_flags &= ~s->mask;
63         bi->bi_flags |= newflags;
64
65         bi->bi_ctime = timespec_to_bch2_time(c, current_time(&inode->v));
66         return 0;
67 }
68
69 static int bch2_ioc_getflags(struct bch_inode_info *inode, int __user *arg)
70 {
71         unsigned flags = map_flags(bch_flags_to_uflags, inode->ei_inode.bi_flags);
72
73         return put_user(flags, arg);
74 }
75
76 static int bch2_ioc_setflags(struct bch_fs *c,
77                              struct file *file,
78                              struct bch_inode_info *inode,
79                              void __user *arg)
80 {
81         struct flags_set s = { .mask = map_defined(bch_flags_to_uflags) };
82         unsigned uflags;
83         int ret;
84
85         if (get_user(uflags, (int __user *) arg))
86                 return -EFAULT;
87
88         s.flags = map_flags_rev(bch_flags_to_uflags, uflags);
89         if (uflags)
90                 return -EOPNOTSUPP;
91
92         ret = mnt_want_write_file(file);
93         if (ret)
94                 return ret;
95
96         inode_lock(&inode->v);
97         if (!inode_owner_or_capable(file_mnt_idmap(file), &inode->v)) {
98                 ret = -EACCES;
99                 goto setflags_out;
100         }
101
102         mutex_lock(&inode->ei_update_lock);
103         ret = bch2_write_inode(c, inode, bch2_inode_flags_set, &s,
104                                ATTR_CTIME);
105         mutex_unlock(&inode->ei_update_lock);
106
107 setflags_out:
108         inode_unlock(&inode->v);
109         mnt_drop_write_file(file);
110         return ret;
111 }
112
113 static int bch2_ioc_fsgetxattr(struct bch_inode_info *inode,
114                                struct fsxattr __user *arg)
115 {
116         struct fsxattr fa = { 0 };
117
118         fa.fsx_xflags = map_flags(bch_flags_to_xflags, inode->ei_inode.bi_flags);
119
120         if (inode->ei_inode.bi_fields_set & (1 << Inode_opt_project))
121                 fa.fsx_xflags |= FS_XFLAG_PROJINHERIT;
122
123         fa.fsx_projid = inode->ei_qid.q[QTYP_PRJ];
124
125         return copy_to_user(arg, &fa, sizeof(fa));
126 }
127
128 static int fssetxattr_inode_update_fn(struct btree_trans *trans,
129                                       struct bch_inode_info *inode,
130                                       struct bch_inode_unpacked *bi,
131                                       void *p)
132 {
133         struct flags_set *s = p;
134
135         if (s->projid != bi->bi_project) {
136                 bi->bi_fields_set |= 1U << Inode_opt_project;
137                 bi->bi_project = s->projid;
138         }
139
140         return bch2_inode_flags_set(trans, inode, bi, p);
141 }
142
143 static int bch2_ioc_fssetxattr(struct bch_fs *c,
144                                struct file *file,
145                                struct bch_inode_info *inode,
146                                struct fsxattr __user *arg)
147 {
148         struct flags_set s = { .mask = map_defined(bch_flags_to_xflags) };
149         struct fsxattr fa;
150         int ret;
151
152         if (copy_from_user(&fa, arg, sizeof(fa)))
153                 return -EFAULT;
154
155         s.set_projinherit = true;
156         s.projinherit = (fa.fsx_xflags & FS_XFLAG_PROJINHERIT) != 0;
157         fa.fsx_xflags &= ~FS_XFLAG_PROJINHERIT;
158
159         s.flags = map_flags_rev(bch_flags_to_xflags, fa.fsx_xflags);
160         if (fa.fsx_xflags)
161                 return -EOPNOTSUPP;
162
163         if (fa.fsx_projid >= U32_MAX)
164                 return -EINVAL;
165
166         /*
167          * inode fields accessible via the xattr interface are stored with a +1
168          * bias, so that 0 means unset:
169          */
170         s.projid = fa.fsx_projid + 1;
171
172         ret = mnt_want_write_file(file);
173         if (ret)
174                 return ret;
175
176         inode_lock(&inode->v);
177         if (!inode_owner_or_capable(file_mnt_idmap(file), &inode->v)) {
178                 ret = -EACCES;
179                 goto err;
180         }
181
182         mutex_lock(&inode->ei_update_lock);
183         ret = bch2_set_projid(c, inode, fa.fsx_projid);
184         if (ret)
185                 goto err_unlock;
186
187         ret = bch2_write_inode(c, inode, fssetxattr_inode_update_fn, &s,
188                                ATTR_CTIME);
189 err_unlock:
190         mutex_unlock(&inode->ei_update_lock);
191 err:
192         inode_unlock(&inode->v);
193         mnt_drop_write_file(file);
194         return ret;
195 }
196
197 static int bch2_reinherit_attrs_fn(struct btree_trans *trans,
198                                    struct bch_inode_info *inode,
199                                    struct bch_inode_unpacked *bi,
200                                    void *p)
201 {
202         struct bch_inode_info *dir = p;
203
204         return !bch2_reinherit_attrs(bi, &dir->ei_inode);
205 }
206
207 static int bch2_ioc_reinherit_attrs(struct bch_fs *c,
208                                     struct file *file,
209                                     struct bch_inode_info *src,
210                                     const char __user *name)
211 {
212         struct bch_hash_info hash = bch2_hash_info_init(c, &src->ei_inode);
213         struct bch_inode_info *dst;
214         struct inode *vinode = NULL;
215         char *kname = NULL;
216         struct qstr qstr;
217         int ret = 0;
218         subvol_inum inum;
219
220         kname = kmalloc(BCH_NAME_MAX + 1, GFP_KERNEL);
221         if (!kname)
222                 return -ENOMEM;
223
224         ret = strncpy_from_user(kname, name, BCH_NAME_MAX);
225         if (unlikely(ret < 0))
226                 goto err1;
227
228         qstr.len        = ret;
229         qstr.name       = kname;
230
231         ret = bch2_dirent_lookup(c, inode_inum(src), &hash, &qstr, &inum);
232         if (ret)
233                 goto err1;
234
235         vinode = bch2_vfs_inode_get(c, inum);
236         ret = PTR_ERR_OR_ZERO(vinode);
237         if (ret)
238                 goto err1;
239
240         dst = to_bch_ei(vinode);
241
242         ret = mnt_want_write_file(file);
243         if (ret)
244                 goto err2;
245
246         bch2_lock_inodes(INODE_UPDATE_LOCK, src, dst);
247
248         if (inode_attr_changing(src, dst, Inode_opt_project)) {
249                 ret = bch2_fs_quota_transfer(c, dst,
250                                              src->ei_qid,
251                                              1 << QTYP_PRJ,
252                                              KEY_TYPE_QUOTA_PREALLOC);
253                 if (ret)
254                         goto err3;
255         }
256
257         ret = bch2_write_inode(c, dst, bch2_reinherit_attrs_fn, src, 0);
258 err3:
259         bch2_unlock_inodes(INODE_UPDATE_LOCK, src, dst);
260
261         /* return true if we did work */
262         if (ret >= 0)
263                 ret = !ret;
264
265         mnt_drop_write_file(file);
266 err2:
267         iput(vinode);
268 err1:
269         kfree(kname);
270
271         return ret;
272 }
273
274 static int bch2_ioc_goingdown(struct bch_fs *c, u32 __user *arg)
275 {
276         u32 flags;
277         int ret = 0;
278
279         if (!capable(CAP_SYS_ADMIN))
280                 return -EPERM;
281
282         if (get_user(flags, arg))
283                 return -EFAULT;
284
285         bch_notice(c, "shutdown by ioctl type %u", flags);
286
287         down_write(&c->vfs_sb->s_umount);
288
289         switch (flags) {
290         case FSOP_GOING_FLAGS_DEFAULT:
291                 ret = freeze_bdev(c->vfs_sb->s_bdev);
292                 if (ret)
293                         goto err;
294
295                 bch2_journal_flush(&c->journal);
296                 c->vfs_sb->s_flags |= SB_RDONLY;
297                 bch2_fs_emergency_read_only(c);
298                 thaw_bdev(c->vfs_sb->s_bdev);
299                 break;
300
301         case FSOP_GOING_FLAGS_LOGFLUSH:
302                 bch2_journal_flush(&c->journal);
303                 fallthrough;
304
305         case FSOP_GOING_FLAGS_NOLOGFLUSH:
306                 c->vfs_sb->s_flags |= SB_RDONLY;
307                 bch2_fs_emergency_read_only(c);
308                 break;
309         default:
310                 ret = -EINVAL;
311                 break;
312         }
313 err:
314         up_write(&c->vfs_sb->s_umount);
315         return ret;
316 }
317
318 static long bch2_ioctl_subvolume_create(struct bch_fs *c, struct file *filp,
319                                 struct bch_ioctl_subvolume arg)
320 {
321         struct inode *dir;
322         struct bch_inode_info *inode;
323         struct user_namespace *s_user_ns;
324         struct dentry *dst_dentry;
325         struct path src_path, dst_path;
326         int how = LOOKUP_FOLLOW;
327         int error;
328         subvol_inum snapshot_src = { 0 };
329         unsigned lookup_flags = 0;
330         unsigned create_flags = BCH_CREATE_SUBVOL;
331
332         if (arg.flags & ~(BCH_SUBVOL_SNAPSHOT_CREATE|
333                           BCH_SUBVOL_SNAPSHOT_RO))
334                 return -EINVAL;
335
336         if (!(arg.flags & BCH_SUBVOL_SNAPSHOT_CREATE) &&
337             (arg.src_ptr ||
338              (arg.flags & BCH_SUBVOL_SNAPSHOT_RO)))
339                 return -EINVAL;
340
341         if (arg.flags & BCH_SUBVOL_SNAPSHOT_CREATE)
342                 create_flags |= BCH_CREATE_SNAPSHOT;
343
344         if (arg.flags & BCH_SUBVOL_SNAPSHOT_RO)
345                 create_flags |= BCH_CREATE_SNAPSHOT_RO;
346
347         /* why do we need this lock? */
348         down_read(&c->vfs_sb->s_umount);
349
350         if (arg.flags & BCH_SUBVOL_SNAPSHOT_CREATE)
351                 sync_inodes_sb(c->vfs_sb);
352 retry:
353         if (arg.src_ptr) {
354                 error = user_path_at(arg.dirfd,
355                                 (const char __user *)(unsigned long)arg.src_ptr,
356                                 how, &src_path);
357                 if (error)
358                         goto err1;
359
360                 if (src_path.dentry->d_sb->s_fs_info != c) {
361                         path_put(&src_path);
362                         error = -EXDEV;
363                         goto err1;
364                 }
365
366                 snapshot_src = inode_inum(to_bch_ei(src_path.dentry->d_inode));
367         }
368
369         dst_dentry = user_path_create(arg.dirfd,
370                         (const char __user *)(unsigned long)arg.dst_ptr,
371                         &dst_path, lookup_flags);
372         error = PTR_ERR_OR_ZERO(dst_dentry);
373         if (error)
374                 goto err2;
375
376         if (dst_dentry->d_sb->s_fs_info != c) {
377                 error = -EXDEV;
378                 goto err3;
379         }
380
381         if (dst_dentry->d_inode) {
382                 error = -EEXIST;
383                 goto err3;
384         }
385
386         dir = dst_path.dentry->d_inode;
387         if (IS_DEADDIR(dir)) {
388                 error = -BCH_ERR_ENOENT_directory_dead;
389                 goto err3;
390         }
391
392         s_user_ns = dir->i_sb->s_user_ns;
393         if (!kuid_has_mapping(s_user_ns, current_fsuid()) ||
394             !kgid_has_mapping(s_user_ns, current_fsgid())) {
395                 error = -EOVERFLOW;
396                 goto err3;
397         }
398
399         error = inode_permission(file_mnt_idmap(filp),
400                                  dir, MAY_WRITE | MAY_EXEC);
401         if (error)
402                 goto err3;
403
404         if (!IS_POSIXACL(dir))
405                 arg.mode &= ~current_umask();
406
407         error = security_path_mkdir(&dst_path, dst_dentry, arg.mode);
408         if (error)
409                 goto err3;
410
411         if ((arg.flags & BCH_SUBVOL_SNAPSHOT_CREATE) &&
412             !arg.src_ptr)
413                 snapshot_src.subvol = to_bch_ei(dir)->ei_inode.bi_subvol;
414
415         inode = __bch2_create(file_mnt_idmap(filp), to_bch_ei(dir),
416                               dst_dentry, arg.mode|S_IFDIR,
417                               0, snapshot_src, create_flags);
418         error = PTR_ERR_OR_ZERO(inode);
419         if (error)
420                 goto err3;
421
422         d_instantiate(dst_dentry, &inode->v);
423         fsnotify_mkdir(dir, dst_dentry);
424 err3:
425         done_path_create(&dst_path, dst_dentry);
426 err2:
427         if (arg.src_ptr)
428                 path_put(&src_path);
429
430         if (retry_estale(error, lookup_flags)) {
431                 lookup_flags |= LOOKUP_REVAL;
432                 goto retry;
433         }
434 err1:
435         up_read(&c->vfs_sb->s_umount);
436
437         return error;
438 }
439
440 static long bch2_ioctl_subvolume_destroy(struct bch_fs *c, struct file *filp,
441                                 struct bch_ioctl_subvolume arg)
442 {
443         struct path path;
444         struct inode *dir;
445         int ret = 0;
446
447         if (arg.flags)
448                 return -EINVAL;
449
450         ret = user_path_at(arg.dirfd,
451                         (const char __user *)(unsigned long)arg.dst_ptr,
452                         LOOKUP_FOLLOW, &path);
453         if (ret)
454                 return ret;
455
456         if (path.dentry->d_sb->s_fs_info != c) {
457                 ret = -EXDEV;
458                 goto err;
459         }
460
461         dir = path.dentry->d_parent->d_inode;
462
463         ret = __bch2_unlink(dir, path.dentry, true);
464         if (ret)
465                 goto err;
466
467         fsnotify_rmdir(dir, path.dentry);
468         d_delete(path.dentry);
469 err:
470         path_put(&path);
471         return ret;
472 }
473
474 long bch2_fs_file_ioctl(struct file *file, unsigned cmd, unsigned long arg)
475 {
476         struct bch_inode_info *inode = file_bch_inode(file);
477         struct bch_fs *c = inode->v.i_sb->s_fs_info;
478         long ret;
479
480         switch (cmd) {
481         case FS_IOC_GETFLAGS:
482                 ret = bch2_ioc_getflags(inode, (int __user *) arg);
483                 break;
484
485         case FS_IOC_SETFLAGS:
486                 ret = bch2_ioc_setflags(c, file, inode, (int __user *) arg);
487                 break;
488
489         case FS_IOC_FSGETXATTR:
490                 ret = bch2_ioc_fsgetxattr(inode, (void __user *) arg);
491                 break;
492
493         case FS_IOC_FSSETXATTR:
494                 ret = bch2_ioc_fssetxattr(c, file, inode,
495                                           (void __user *) arg);
496                 break;
497
498         case BCHFS_IOC_REINHERIT_ATTRS:
499                 ret = bch2_ioc_reinherit_attrs(c, file, inode,
500                                                (void __user *) arg);
501                 break;
502
503         case FS_IOC_GETVERSION:
504                 ret = -ENOTTY;
505                 break;
506
507         case FS_IOC_SETVERSION:
508                 ret = -ENOTTY;
509                 break;
510
511         case FS_IOC_GOINGDOWN:
512                 ret = bch2_ioc_goingdown(c, (u32 __user *) arg);
513                 break;
514
515         case BCH_IOCTL_SUBVOLUME_CREATE: {
516                 struct bch_ioctl_subvolume i;
517
518                 ret = copy_from_user(&i, (void __user *) arg, sizeof(i))
519                         ? -EFAULT
520                         : bch2_ioctl_subvolume_create(c, file, i);
521                 break;
522         }
523
524         case BCH_IOCTL_SUBVOLUME_DESTROY: {
525                 struct bch_ioctl_subvolume i;
526
527                 ret = copy_from_user(&i, (void __user *) arg, sizeof(i))
528                         ? -EFAULT
529                         : bch2_ioctl_subvolume_destroy(c, file, i);
530                 break;
531         }
532
533         default:
534                 ret = bch2_fs_ioctl(c, cmd, (void __user *) arg);
535                 break;
536         }
537
538         return bch2_err_class(ret);
539 }
540
541 #ifdef CONFIG_COMPAT
542 long bch2_compat_fs_ioctl(struct file *file, unsigned cmd, unsigned long arg)
543 {
544         /* These are just misnamed, they actually get/put from/to user an int */
545         switch (cmd) {
546         case FS_IOC_GETFLAGS:
547                 cmd = FS_IOC_GETFLAGS;
548                 break;
549         case FS_IOC32_SETFLAGS:
550                 cmd = FS_IOC_SETFLAGS;
551                 break;
552         default:
553                 return -ENOIOCTLCMD;
554         }
555         return bch2_fs_file_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
556 }
557 #endif
558
559 #endif /* NO_BCACHEFS_FS */