]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/fs-ioctl.c
0873d2f0928cdcffb021969c34e82d1493000d37
[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         /*
142          * inode fields accessible via the xattr interface are stored with a +1
143          * bias, so that 0 means unset:
144          */
145         s.projid = fa.fsx_projid + 1;
146
147         ret = mnt_want_write_file(file);
148         if (ret)
149                 return ret;
150
151         inode_lock(&inode->v);
152         if (!inode_owner_or_capable(&inode->v)) {
153                 ret = -EACCES;
154                 goto err;
155         }
156
157         mutex_lock(&inode->ei_update_lock);
158         ret = bch2_set_projid(c, inode, fa.fsx_projid);
159         if (ret)
160                 goto err_unlock;
161
162         ret = bch2_write_inode(c, inode, fssetxattr_inode_update_fn, &s,
163                                ATTR_CTIME);
164 err_unlock:
165         mutex_unlock(&inode->ei_update_lock);
166 err:
167         inode_unlock(&inode->v);
168         mnt_drop_write_file(file);
169         return ret;
170 }
171
172 static int bch2_reinherit_attrs_fn(struct bch_inode_info *inode,
173                                    struct bch_inode_unpacked *bi,
174                                    void *p)
175 {
176         struct bch_inode_info *dir = p;
177
178         return !bch2_reinherit_attrs(bi, &dir->ei_inode);
179 }
180
181 static int bch2_ioc_reinherit_attrs(struct bch_fs *c,
182                                     struct file *file,
183                                     struct bch_inode_info *src,
184                                     const char __user *name)
185 {
186         struct bch_inode_info *dst;
187         struct inode *vinode = NULL;
188         char *kname = NULL;
189         struct qstr qstr;
190         int ret = 0;
191         u64 inum;
192
193         kname = kmalloc(BCH_NAME_MAX + 1, GFP_KERNEL);
194         if (!kname)
195                 return -ENOMEM;
196
197         ret = strncpy_from_user(kname, name, BCH_NAME_MAX);
198         if (unlikely(ret < 0))
199                 goto err1;
200
201         qstr.len        = ret;
202         qstr.name       = kname;
203
204         ret = -ENOENT;
205         inum = bch2_dirent_lookup(c, src->v.i_ino,
206                                   &src->ei_str_hash,
207                                   &qstr);
208         if (!inum)
209                 goto err1;
210
211         vinode = bch2_vfs_inode_get(c, inum);
212         ret = PTR_ERR_OR_ZERO(vinode);
213         if (ret)
214                 goto err1;
215
216         dst = to_bch_ei(vinode);
217
218         ret = mnt_want_write_file(file);
219         if (ret)
220                 goto err2;
221
222         bch2_lock_inodes(INODE_UPDATE_LOCK, src, dst);
223
224         if (inode_attr_changing(src, dst, Inode_opt_project)) {
225                 ret = bch2_fs_quota_transfer(c, dst,
226                                              src->ei_qid,
227                                              1 << QTYP_PRJ,
228                                              KEY_TYPE_QUOTA_PREALLOC);
229                 if (ret)
230                         goto err3;
231         }
232
233         ret = bch2_write_inode(c, dst, bch2_reinherit_attrs_fn, src, 0);
234 err3:
235         bch2_unlock_inodes(INODE_UPDATE_LOCK, src, dst);
236
237         /* return true if we did work */
238         if (ret >= 0)
239                 ret = !ret;
240
241         mnt_drop_write_file(file);
242 err2:
243         iput(vinode);
244 err1:
245         kfree(kname);
246
247         return ret;
248 }
249
250 long bch2_fs_file_ioctl(struct file *file, unsigned cmd, unsigned long arg)
251 {
252         struct bch_inode_info *inode = file_bch_inode(file);
253         struct super_block *sb = inode->v.i_sb;
254         struct bch_fs *c = sb->s_fs_info;
255
256         switch (cmd) {
257         case FS_IOC_GETFLAGS:
258                 return bch2_ioc_getflags(inode, (int __user *) arg);
259
260         case FS_IOC_SETFLAGS:
261                 return bch2_ioc_setflags(c, file, inode, (int __user *) arg);
262
263         case FS_IOC_FSGETXATTR:
264                 return bch2_ioc_fsgetxattr(inode, (void __user *) arg);
265         case FS_IOC_FSSETXATTR:
266                 return bch2_ioc_fssetxattr(c, file, inode,
267                                            (void __user *) arg);
268
269         case BCHFS_IOC_REINHERIT_ATTRS:
270                 return bch2_ioc_reinherit_attrs(c, file, inode,
271                                                 (void __user *) arg);
272
273         case FS_IOC_GETVERSION:
274                 return -ENOTTY;
275         case FS_IOC_SETVERSION:
276                 return -ENOTTY;
277
278         case FS_IOC_GOINGDOWN:
279                 if (!capable(CAP_SYS_ADMIN))
280                         return -EPERM;
281
282                 down_write(&sb->s_umount);
283                 sb->s_flags |= SB_RDONLY;
284                 if (bch2_fs_emergency_read_only(c))
285                         bch_err(c, "emergency read only due to ioctl");
286                 up_write(&sb->s_umount);
287                 return 0;
288
289         default:
290                 return bch2_fs_ioctl(c, cmd, (void __user *) arg);
291         }
292 }
293
294 #ifdef CONFIG_COMPAT
295 long bch2_compat_fs_ioctl(struct file *file, unsigned cmd, unsigned long arg)
296 {
297         /* These are just misnamed, they actually get/put from/to user an int */
298         switch (cmd) {
299         case FS_IOC_GETFLAGS:
300                 cmd = FS_IOC_GETFLAGS;
301                 break;
302         case FS_IOC32_SETFLAGS:
303                 cmd = FS_IOC_SETFLAGS;
304                 break;
305         default:
306                 return -ENOIOCTLCMD;
307         }
308         return bch2_fs_file_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
309 }
310 #endif
311
312 #endif /* NO_BCACHEFS_FS */