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