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