iio: ep93xx: remove redundant return value check of platform_get_resource()
[linux-2.6/btrfs-unstable.git] / kernel / bpf / inode.c
blob01aaef1a77c5af164660b0f75ac99e4bd7c55a52
1 /*
2 * Minimal file system backend for holding eBPF maps and programs,
3 * used by bpf(2) object pinning.
5 * Authors:
7 * Daniel Borkmann <daniel@iogearbox.net>
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * version 2 as published by the Free Software Foundation.
14 #include <linux/init.h>
15 #include <linux/magic.h>
16 #include <linux/major.h>
17 #include <linux/mount.h>
18 #include <linux/namei.h>
19 #include <linux/fs.h>
20 #include <linux/kdev_t.h>
21 #include <linux/parser.h>
22 #include <linux/filter.h>
23 #include <linux/bpf.h>
24 #include <linux/bpf_trace.h>
26 enum bpf_type {
27 BPF_TYPE_UNSPEC = 0,
28 BPF_TYPE_PROG,
29 BPF_TYPE_MAP,
32 static void *bpf_any_get(void *raw, enum bpf_type type)
34 switch (type) {
35 case BPF_TYPE_PROG:
36 raw = bpf_prog_inc(raw);
37 break;
38 case BPF_TYPE_MAP:
39 raw = bpf_map_inc(raw, true);
40 break;
41 default:
42 WARN_ON_ONCE(1);
43 break;
46 return raw;
49 static void bpf_any_put(void *raw, enum bpf_type type)
51 switch (type) {
52 case BPF_TYPE_PROG:
53 bpf_prog_put(raw);
54 break;
55 case BPF_TYPE_MAP:
56 bpf_map_put_with_uref(raw);
57 break;
58 default:
59 WARN_ON_ONCE(1);
60 break;
64 static void *bpf_fd_probe_obj(u32 ufd, enum bpf_type *type)
66 void *raw;
68 *type = BPF_TYPE_MAP;
69 raw = bpf_map_get_with_uref(ufd);
70 if (IS_ERR(raw)) {
71 *type = BPF_TYPE_PROG;
72 raw = bpf_prog_get(ufd);
75 return raw;
78 static const struct inode_operations bpf_dir_iops;
80 static const struct inode_operations bpf_prog_iops = { };
81 static const struct inode_operations bpf_map_iops = { };
83 static struct inode *bpf_get_inode(struct super_block *sb,
84 const struct inode *dir,
85 umode_t mode)
87 struct inode *inode;
89 switch (mode & S_IFMT) {
90 case S_IFDIR:
91 case S_IFREG:
92 case S_IFLNK:
93 break;
94 default:
95 return ERR_PTR(-EINVAL);
98 inode = new_inode(sb);
99 if (!inode)
100 return ERR_PTR(-ENOSPC);
102 inode->i_ino = get_next_ino();
103 inode->i_atime = current_time(inode);
104 inode->i_mtime = inode->i_atime;
105 inode->i_ctime = inode->i_atime;
107 inode_init_owner(inode, dir, mode);
109 return inode;
112 static int bpf_inode_type(const struct inode *inode, enum bpf_type *type)
114 *type = BPF_TYPE_UNSPEC;
115 if (inode->i_op == &bpf_prog_iops)
116 *type = BPF_TYPE_PROG;
117 else if (inode->i_op == &bpf_map_iops)
118 *type = BPF_TYPE_MAP;
119 else
120 return -EACCES;
122 return 0;
125 static void bpf_dentry_finalize(struct dentry *dentry, struct inode *inode,
126 struct inode *dir)
128 d_instantiate(dentry, inode);
129 dget(dentry);
131 dir->i_mtime = current_time(dir);
132 dir->i_ctime = dir->i_mtime;
135 static int bpf_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
137 struct inode *inode;
139 inode = bpf_get_inode(dir->i_sb, dir, mode | S_IFDIR);
140 if (IS_ERR(inode))
141 return PTR_ERR(inode);
143 inode->i_op = &bpf_dir_iops;
144 inode->i_fop = &simple_dir_operations;
146 inc_nlink(inode);
147 inc_nlink(dir);
149 bpf_dentry_finalize(dentry, inode, dir);
150 return 0;
153 static int bpf_mkobj_ops(struct inode *dir, struct dentry *dentry,
154 umode_t mode, const struct inode_operations *iops)
156 struct inode *inode;
158 inode = bpf_get_inode(dir->i_sb, dir, mode | S_IFREG);
159 if (IS_ERR(inode))
160 return PTR_ERR(inode);
162 inode->i_op = iops;
163 inode->i_private = dentry->d_fsdata;
165 bpf_dentry_finalize(dentry, inode, dir);
166 return 0;
169 static int bpf_mkobj(struct inode *dir, struct dentry *dentry, umode_t mode,
170 dev_t devt)
172 enum bpf_type type = MINOR(devt);
174 if (MAJOR(devt) != UNNAMED_MAJOR || !S_ISREG(mode) ||
175 dentry->d_fsdata == NULL)
176 return -EPERM;
178 switch (type) {
179 case BPF_TYPE_PROG:
180 return bpf_mkobj_ops(dir, dentry, mode, &bpf_prog_iops);
181 case BPF_TYPE_MAP:
182 return bpf_mkobj_ops(dir, dentry, mode, &bpf_map_iops);
183 default:
184 return -EPERM;
188 static struct dentry *
189 bpf_lookup(struct inode *dir, struct dentry *dentry, unsigned flags)
191 if (strchr(dentry->d_name.name, '.'))
192 return ERR_PTR(-EPERM);
194 return simple_lookup(dir, dentry, flags);
197 static int bpf_symlink(struct inode *dir, struct dentry *dentry,
198 const char *target)
200 char *link = kstrdup(target, GFP_USER | __GFP_NOWARN);
201 struct inode *inode;
203 if (!link)
204 return -ENOMEM;
206 inode = bpf_get_inode(dir->i_sb, dir, S_IRWXUGO | S_IFLNK);
207 if (IS_ERR(inode)) {
208 kfree(link);
209 return PTR_ERR(inode);
212 inode->i_op = &simple_symlink_inode_operations;
213 inode->i_link = link;
215 bpf_dentry_finalize(dentry, inode, dir);
216 return 0;
219 static const struct inode_operations bpf_dir_iops = {
220 .lookup = bpf_lookup,
221 .mknod = bpf_mkobj,
222 .mkdir = bpf_mkdir,
223 .symlink = bpf_symlink,
224 .rmdir = simple_rmdir,
225 .rename = simple_rename,
226 .link = simple_link,
227 .unlink = simple_unlink,
230 static int bpf_obj_do_pin(const struct filename *pathname, void *raw,
231 enum bpf_type type)
233 struct dentry *dentry;
234 struct inode *dir;
235 struct path path;
236 umode_t mode;
237 dev_t devt;
238 int ret;
240 dentry = kern_path_create(AT_FDCWD, pathname->name, &path, 0);
241 if (IS_ERR(dentry))
242 return PTR_ERR(dentry);
244 mode = S_IFREG | ((S_IRUSR | S_IWUSR) & ~current_umask());
245 devt = MKDEV(UNNAMED_MAJOR, type);
247 ret = security_path_mknod(&path, dentry, mode, devt);
248 if (ret)
249 goto out;
251 dir = d_inode(path.dentry);
252 if (dir->i_op != &bpf_dir_iops) {
253 ret = -EPERM;
254 goto out;
257 dentry->d_fsdata = raw;
258 ret = vfs_mknod(dir, dentry, mode, devt);
259 dentry->d_fsdata = NULL;
260 out:
261 done_path_create(&path, dentry);
262 return ret;
265 int bpf_obj_pin_user(u32 ufd, const char __user *pathname)
267 struct filename *pname;
268 enum bpf_type type;
269 void *raw;
270 int ret;
272 pname = getname(pathname);
273 if (IS_ERR(pname))
274 return PTR_ERR(pname);
276 raw = bpf_fd_probe_obj(ufd, &type);
277 if (IS_ERR(raw)) {
278 ret = PTR_ERR(raw);
279 goto out;
282 ret = bpf_obj_do_pin(pname, raw, type);
283 if (ret != 0)
284 bpf_any_put(raw, type);
285 if ((trace_bpf_obj_pin_prog_enabled() ||
286 trace_bpf_obj_pin_map_enabled()) && !ret) {
287 if (type == BPF_TYPE_PROG)
288 trace_bpf_obj_pin_prog(raw, ufd, pname);
289 if (type == BPF_TYPE_MAP)
290 trace_bpf_obj_pin_map(raw, ufd, pname);
292 out:
293 putname(pname);
294 return ret;
297 static void *bpf_obj_do_get(const struct filename *pathname,
298 enum bpf_type *type, int flags)
300 struct inode *inode;
301 struct path path;
302 void *raw;
303 int ret;
305 ret = kern_path(pathname->name, LOOKUP_FOLLOW, &path);
306 if (ret)
307 return ERR_PTR(ret);
309 inode = d_backing_inode(path.dentry);
310 ret = inode_permission(inode, ACC_MODE(flags));
311 if (ret)
312 goto out;
314 ret = bpf_inode_type(inode, type);
315 if (ret)
316 goto out;
318 raw = bpf_any_get(inode->i_private, *type);
319 if (!IS_ERR(raw))
320 touch_atime(&path);
322 path_put(&path);
323 return raw;
324 out:
325 path_put(&path);
326 return ERR_PTR(ret);
329 int bpf_obj_get_user(const char __user *pathname, int flags)
331 enum bpf_type type = BPF_TYPE_UNSPEC;
332 struct filename *pname;
333 int ret = -ENOENT;
334 int f_flags;
335 void *raw;
337 f_flags = bpf_get_file_flag(flags);
338 if (f_flags < 0)
339 return f_flags;
341 pname = getname(pathname);
342 if (IS_ERR(pname))
343 return PTR_ERR(pname);
345 raw = bpf_obj_do_get(pname, &type, f_flags);
346 if (IS_ERR(raw)) {
347 ret = PTR_ERR(raw);
348 goto out;
351 if (type == BPF_TYPE_PROG)
352 ret = bpf_prog_new_fd(raw);
353 else if (type == BPF_TYPE_MAP)
354 ret = bpf_map_new_fd(raw, f_flags);
355 else
356 goto out;
358 if (ret < 0) {
359 bpf_any_put(raw, type);
360 } else if (trace_bpf_obj_get_prog_enabled() ||
361 trace_bpf_obj_get_map_enabled()) {
362 if (type == BPF_TYPE_PROG)
363 trace_bpf_obj_get_prog(raw, ret, pname);
364 if (type == BPF_TYPE_MAP)
365 trace_bpf_obj_get_map(raw, ret, pname);
367 out:
368 putname(pname);
369 return ret;
371 EXPORT_SYMBOL_GPL(bpf_obj_get_user);
373 static void bpf_evict_inode(struct inode *inode)
375 enum bpf_type type;
377 truncate_inode_pages_final(&inode->i_data);
378 clear_inode(inode);
380 if (S_ISLNK(inode->i_mode))
381 kfree(inode->i_link);
382 if (!bpf_inode_type(inode, &type))
383 bpf_any_put(inode->i_private, type);
387 * Display the mount options in /proc/mounts.
389 static int bpf_show_options(struct seq_file *m, struct dentry *root)
391 umode_t mode = d_inode(root)->i_mode & S_IALLUGO & ~S_ISVTX;
393 if (mode != S_IRWXUGO)
394 seq_printf(m, ",mode=%o", mode);
395 return 0;
398 static const struct super_operations bpf_super_ops = {
399 .statfs = simple_statfs,
400 .drop_inode = generic_delete_inode,
401 .show_options = bpf_show_options,
402 .evict_inode = bpf_evict_inode,
405 enum {
406 OPT_MODE,
407 OPT_ERR,
410 static const match_table_t bpf_mount_tokens = {
411 { OPT_MODE, "mode=%o" },
412 { OPT_ERR, NULL },
415 struct bpf_mount_opts {
416 umode_t mode;
419 static int bpf_parse_options(char *data, struct bpf_mount_opts *opts)
421 substring_t args[MAX_OPT_ARGS];
422 int option, token;
423 char *ptr;
425 opts->mode = S_IRWXUGO;
427 while ((ptr = strsep(&data, ",")) != NULL) {
428 if (!*ptr)
429 continue;
431 token = match_token(ptr, bpf_mount_tokens, args);
432 switch (token) {
433 case OPT_MODE:
434 if (match_octal(&args[0], &option))
435 return -EINVAL;
436 opts->mode = option & S_IALLUGO;
437 break;
438 /* We might like to report bad mount options here, but
439 * traditionally we've ignored all mount options, so we'd
440 * better continue to ignore non-existing options for bpf.
445 return 0;
448 static int bpf_fill_super(struct super_block *sb, void *data, int silent)
450 static const struct tree_descr bpf_rfiles[] = { { "" } };
451 struct bpf_mount_opts opts;
452 struct inode *inode;
453 int ret;
455 ret = bpf_parse_options(data, &opts);
456 if (ret)
457 return ret;
459 ret = simple_fill_super(sb, BPF_FS_MAGIC, bpf_rfiles);
460 if (ret)
461 return ret;
463 sb->s_op = &bpf_super_ops;
465 inode = sb->s_root->d_inode;
466 inode->i_op = &bpf_dir_iops;
467 inode->i_mode &= ~S_IALLUGO;
468 inode->i_mode |= S_ISVTX | opts.mode;
470 return 0;
473 static struct dentry *bpf_mount(struct file_system_type *type, int flags,
474 const char *dev_name, void *data)
476 return mount_nodev(type, flags, data, bpf_fill_super);
479 static struct file_system_type bpf_fs_type = {
480 .owner = THIS_MODULE,
481 .name = "bpf",
482 .mount = bpf_mount,
483 .kill_sb = kill_litter_super,
486 static int __init bpf_init(void)
488 int ret;
490 ret = sysfs_create_mount_point(fs_kobj, "bpf");
491 if (ret)
492 return ret;
494 ret = register_filesystem(&bpf_fs_type);
495 if (ret)
496 sysfs_remove_mount_point(fs_kobj, "bpf");
498 return ret;
500 fs_initcall(bpf_init);