e368b7354424973da947b54c2cdeadd2aabcf93c
[platform/kernel/linux-rpi.git] / ipc / kdbus / fs.c
1 /*
2  * Copyright (C) 2013-2015 Kay Sievers
3  * Copyright (C) 2013-2015 Greg Kroah-Hartman <gregkh@linuxfoundation.org>
4  * Copyright (C) 2013-2015 Daniel Mack <daniel@zonque.org>
5  * Copyright (C) 2013-2015 David Herrmann <dh.herrmann@gmail.com>
6  * Copyright (C) 2013-2015 Linux Foundation
7  *
8  * kdbus is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU Lesser General Public License as published by the
10  * Free Software Foundation; either version 2.1 of the License, or (at
11  * your option) any later version.
12  */
13
14 #include <linux/dcache.h>
15 #include <linux/fs.h>
16 #include <linux/fsnotify.h>
17 #include <linux/init.h>
18 #include <linux/ipc_namespace.h>
19 #include <linux/magic.h>
20 #include <linux/module.h>
21 #include <linux/mount.h>
22 #include <linux/mutex.h>
23 #include <linux/namei.h>
24 #include <linux/pagemap.h>
25 #include <linux/sched.h>
26 #include <linux/slab.h>
27
28 #include "bus.h"
29 #include "domain.h"
30 #include "endpoint.h"
31 #include "fs.h"
32 #include "handle.h"
33 #include "node.h"
34
35 #define kdbus_node_from_dentry(_dentry) \
36         ((struct kdbus_node *)(_dentry)->d_fsdata)
37
38 static struct inode *fs_inode_get(struct super_block *sb,
39                                   struct kdbus_node *node);
40
41 /*
42  * Directory Management
43  */
44
45 static inline unsigned char kdbus_dt_type(struct kdbus_node *node)
46 {
47         switch (node->type) {
48         case KDBUS_NODE_DOMAIN:
49         case KDBUS_NODE_BUS:
50                 return DT_DIR;
51         case KDBUS_NODE_CONTROL:
52         case KDBUS_NODE_ENDPOINT:
53                 return DT_REG;
54         }
55
56         return DT_UNKNOWN;
57 }
58
59 static int fs_dir_fop_iterate(struct file *file, struct dir_context *ctx)
60 {
61         struct dentry *dentry = file->f_path.dentry;
62         struct kdbus_node *parent = kdbus_node_from_dentry(dentry);
63         struct kdbus_node *old, *next = file->private_data;
64
65         /*
66          * kdbusfs directory iterator (modelled after sysfs/kernfs)
67          * When iterating kdbusfs directories, we iterate all children of the
68          * parent kdbus_node object. We use ctx->pos to store the hash of the
69          * child and file->private_data to store a reference to the next node
70          * object. If ctx->pos is not modified via llseek while you iterate a
71          * directory, then we use the file->private_data node pointer to
72          * directly access the next node in the tree.
73          * However, if you directly seek on the directory, we have to find the
74          * closest node to that position and cannot use our node pointer. This
75          * means iterating the rb-tree to find the closest match and start over
76          * from there.
77          * Note that hash values are not necessarily unique. Therefore, llseek
78          * is not guaranteed to seek to the same node that you got when you
79          * retrieved the position. Seeking to 0, 1, 2 and >=INT_MAX is safe,
80          * though. We could use the inode-number as position, but this would
81          * require another rb-tree for fast access. Kernfs and others already
82          * ignore those conflicts, so we should be fine, too.
83          */
84
85         if (!dir_emit_dots(file, ctx))
86                 return 0;
87
88         /* acquire @next; if deactivated, or seek detected, find next node */
89         old = next;
90         if (next && ctx->pos == next->hash) {
91                 if (kdbus_node_acquire(next))
92                         kdbus_node_ref(next);
93                 else
94                         next = kdbus_node_next_child(parent, next);
95         } else {
96                 next = kdbus_node_find_closest(parent, ctx->pos);
97         }
98         kdbus_node_unref(old);
99
100         while (next) {
101                 /* emit @next */
102                 file->private_data = next;
103                 ctx->pos = next->hash;
104
105                 kdbus_node_release(next);
106
107                 if (!dir_emit(ctx, next->name, strlen(next->name), next->id,
108                               kdbus_dt_type(next)))
109                         return 0;
110
111                 /* find next node after @next */
112                 old = next;
113                 next = kdbus_node_next_child(parent, next);
114                 kdbus_node_unref(old);
115         }
116
117         file->private_data = NULL;
118         ctx->pos = INT_MAX;
119
120         return 0;
121 }
122
123 static loff_t fs_dir_fop_llseek(struct file *file, loff_t offset, int whence)
124 {
125         struct inode *inode = file_inode(file);
126         loff_t ret;
127
128         /* protect f_off against fop_iterate */
129         inode_lock(inode);
130         ret = generic_file_llseek(file, offset, whence);
131         inode_unlock(inode);
132
133         return ret;
134 }
135
136 static int fs_dir_fop_release(struct inode *inode, struct file *file)
137 {
138         kdbus_node_unref(file->private_data);
139         return 0;
140 }
141
142 static const struct file_operations fs_dir_fops = {
143         .read           = generic_read_dir,
144         .iterate        = fs_dir_fop_iterate,
145         .llseek         = fs_dir_fop_llseek,
146         .release        = fs_dir_fop_release,
147 };
148
149 static struct dentry *fs_dir_iop_lookup(struct inode *dir,
150                                         struct dentry *dentry,
151                                         unsigned int flags)
152 {
153         struct dentry *dnew = NULL;
154         struct kdbus_node *parent;
155         struct kdbus_node *node;
156         struct inode *inode;
157
158         parent = kdbus_node_from_dentry(dentry->d_parent);
159         if (!kdbus_node_acquire(parent))
160                 return NULL;
161
162         /* returns reference to _acquired_ child node */
163         node = kdbus_node_find_child(parent, dentry->d_name.name);
164         if (node) {
165                 dentry->d_fsdata = node;
166                 inode = fs_inode_get(dir->i_sb, node);
167                 if (IS_ERR(inode))
168                         dnew = ERR_CAST(inode);
169                 else
170                         dnew = d_splice_alias(inode, dentry);
171
172                 kdbus_node_release(node);
173         }
174
175         kdbus_node_release(parent);
176         return dnew;
177 }
178
179 static const struct inode_operations fs_dir_iops = {
180         .permission     = generic_permission,
181         .lookup         = fs_dir_iop_lookup,
182 };
183
184 /*
185  * Inode Management
186  */
187
188 static const struct inode_operations fs_inode_iops = {
189         .permission     = generic_permission,
190 };
191
192 static struct inode *fs_inode_get(struct super_block *sb,
193                                   struct kdbus_node *node)
194 {
195         struct inode *inode;
196
197         inode = iget_locked(sb, node->id);
198         if (!inode)
199                 return ERR_PTR(-ENOMEM);
200         if (!(inode->i_state & I_NEW))
201                 return inode;
202
203         inode->i_private = kdbus_node_ref(node);
204         inode->i_mapping->a_ops = &empty_aops;
205         inode->i_mode = node->mode & S_IALLUGO;
206         inode->i_atime = inode->i_ctime = inode->i_mtime = current_time(inode);
207         inode->i_uid = node->uid;
208         inode->i_gid = node->gid;
209
210         switch (node->type) {
211         case KDBUS_NODE_DOMAIN:
212         case KDBUS_NODE_BUS:
213                 inode->i_mode |= S_IFDIR;
214                 inode->i_op = &fs_dir_iops;
215                 inode->i_fop = &fs_dir_fops;
216                 set_nlink(inode, 2);
217                 break;
218         case KDBUS_NODE_CONTROL:
219         case KDBUS_NODE_ENDPOINT:
220                 inode->i_mode |= S_IFREG;
221                 inode->i_op = &fs_inode_iops;
222                 inode->i_fop = &kdbus_handle_ops;
223                 break;
224         }
225
226         unlock_new_inode(inode);
227
228         return inode;
229 }
230
231 /*
232  * Superblock Management
233  */
234
235 static int fs_super_dop_revalidate(struct dentry *dentry, unsigned int flags)
236 {
237         struct kdbus_node *node;
238
239         /* Force lookup on negatives */
240         if (!dentry->d_inode)
241                 return 0;
242
243         node = kdbus_node_from_dentry(dentry);
244
245         /* see whether the node has been removed */
246         if (!kdbus_node_is_active(node))
247                 return 0;
248
249         return 1;
250 }
251
252 static void fs_super_dop_release(struct dentry *dentry)
253 {
254         kdbus_node_unref(dentry->d_fsdata);
255 }
256
257 static const struct dentry_operations fs_super_dops = {
258         .d_revalidate   = fs_super_dop_revalidate,
259         .d_release      = fs_super_dop_release,
260 };
261
262 static void fs_super_sop_evict_inode(struct inode *inode)
263 {
264         struct kdbus_node *node = kdbus_node_from_inode(inode);
265
266         truncate_inode_pages_final(&inode->i_data);
267         clear_inode(inode);
268         kdbus_node_unref(node);
269 }
270
271 static const struct super_operations fs_super_sops = {
272         .statfs         = simple_statfs,
273         .drop_inode     = generic_delete_inode,
274         .evict_inode    = fs_super_sop_evict_inode,
275 };
276
277 static int fs_super_fill(struct super_block *sb)
278 {
279         struct kdbus_domain *domain = sb->s_fs_info;
280         struct inode *inode;
281         int ret;
282
283         sb->s_blocksize = PAGE_SIZE;
284         sb->s_blocksize_bits = PAGE_SHIFT;
285         sb->s_magic = KDBUS_SUPER_MAGIC;
286         sb->s_maxbytes = MAX_LFS_FILESIZE;
287         sb->s_op = &fs_super_sops;
288         sb->s_time_gran = 1;
289
290         inode = fs_inode_get(sb, &domain->node);
291         if (IS_ERR(inode))
292                 return PTR_ERR(inode);
293
294         sb->s_root = d_make_root(inode);
295         if (!sb->s_root) {
296                 /* d_make_root iput()s the inode on failure */
297                 return -ENOMEM;
298         }
299
300         /* sb holds domain reference */
301         sb->s_root->d_fsdata = &domain->node;
302         sb->s_d_op = &fs_super_dops;
303
304         /* sb holds root reference */
305         domain->dentry = sb->s_root;
306
307         if (!kdbus_node_activate(&domain->node))
308                 return -ESHUTDOWN;
309
310         ret = kdbus_domain_populate(domain, KDBUS_MAKE_ACCESS_WORLD);
311         if (ret < 0)
312                 return ret;
313
314         sb->s_flags |= MS_ACTIVE;
315         return 0;
316 }
317
318 static void fs_super_kill(struct super_block *sb)
319 {
320         struct kdbus_domain *domain = sb->s_fs_info;
321
322         if (domain) {
323                 kdbus_node_deactivate(&domain->node);
324                 domain->dentry = NULL;
325         }
326
327         kill_anon_super(sb);
328         kdbus_domain_unref(domain);
329 }
330
331 static int fs_super_set(struct super_block *sb, void *data)
332 {
333         int ret;
334
335         ret = set_anon_super(sb, data);
336         if (!ret)
337                 sb->s_fs_info = data;
338
339         return ret;
340 }
341
342 static struct dentry *fs_super_mount(struct file_system_type *fs_type,
343                                      int flags, const char *dev_name,
344                                      void *data)
345 {
346         struct kdbus_domain *domain;
347         struct super_block *sb;
348         int ret;
349
350         domain = kdbus_domain_new(KDBUS_MAKE_ACCESS_WORLD);
351         if (IS_ERR(domain))
352                 return ERR_CAST(domain);
353
354         sb = sget(fs_type, NULL, fs_super_set, flags, domain);
355         if (IS_ERR(sb)) {
356                 kdbus_node_deactivate(&domain->node);
357                 kdbus_domain_unref(domain);
358                 return ERR_CAST(sb);
359         }
360
361         WARN_ON(sb->s_fs_info != domain);
362         WARN_ON(sb->s_root);
363
364         ret = fs_super_fill(sb);
365         if (ret < 0) {
366                 /* calls into ->kill_sb() when done */
367                 deactivate_locked_super(sb);
368                 return ERR_PTR(ret);
369         }
370
371         return dget(sb->s_root);
372 }
373
374 static struct file_system_type fs_type = {
375         .name           = KBUILD_MODNAME "fs",
376         .owner          = THIS_MODULE,
377         .mount          = fs_super_mount,
378         .kill_sb        = fs_super_kill,
379         .fs_flags       = FS_USERNS_MOUNT,
380 };
381
382 /**
383  * kdbus_fs_init() - register kdbus filesystem
384  *
385  * This registers a filesystem with the VFS layer. The filesystem is called
386  * `KBUILD_MODNAME "fs"', which usually resolves to `kdbusfs'. The nameing
387  * scheme allows to set KBUILD_MODNAME to "kdbus2" and you will get an
388  * independent filesystem for developers.
389  *
390  * Each mount of the kdbusfs filesystem has an kdbus_domain attached.
391  * Operations on this mount will only affect the attached domain. On each mount
392  * a new domain is automatically created and used for this mount exclusively.
393  * If you want to share a domain across multiple mounts, you need to bind-mount
394  * it.
395  *
396  * Mounts of kdbusfs (with a different domain each) are unrelated to each other
397  * and will never have any effect on any domain but their own.
398  *
399  * Return: 0 on success, negative error otherwise.
400  */
401 int kdbus_fs_init(void)
402 {
403         return register_filesystem(&fs_type);
404 }
405
406 /**
407  * kdbus_fs_exit() - unregister kdbus filesystem
408  *
409  * This does the reverse to kdbus_fs_init(). It unregisters the kdbusfs
410  * filesystem from VFS and cleans up any allocated resources.
411  */
412 void kdbus_fs_exit(void)
413 {
414         unregister_filesystem(&fs_type);
415 }
416
417 /* acquire domain of @node, making sure all ancestors are active */
418 static struct kdbus_domain *fs_acquire_domain(struct kdbus_node *node)
419 {
420         struct kdbus_domain *domain;
421         struct kdbus_node *iter;
422
423         /* caller must guarantee that @node is linked */
424         for (iter = node; iter->parent; iter = iter->parent)
425                 if (!kdbus_node_is_active(iter->parent))
426                         return NULL;
427
428         /* root nodes are always domains */
429         if (WARN_ON(iter->type != KDBUS_NODE_DOMAIN))
430                 return NULL;
431
432         domain = kdbus_domain_from_node(iter);
433         if (!kdbus_node_acquire(&domain->node))
434                 return NULL;
435
436         return domain;
437 }
438
439 /**
440  * kdbus_fs_flush() - flush dcache entries of a node
441  * @node:               Node to flush entries of
442  *
443  * This flushes all VFS filesystem cache entries for a node and all its
444  * children. This should be called whenever a node is destroyed during
445  * runtime. It will flush the cache entries so the linked objects can be
446  * deallocated.
447  *
448  * This is a no-op if you call it on active nodes (they really should stay in
449  * cache) or on nodes with deactivated parents (flushing the parent is enough).
450  * Furthermore, there is no need to call it on nodes whose lifetime is bound to
451  * their parents'. In those cases, the parent-flush will always also flush the
452  * children.
453  */
454 void kdbus_fs_flush(struct kdbus_node *node)
455 {
456         struct dentry *dentry, *parent_dentry = NULL;
457         struct kdbus_domain *domain;
458         struct qstr name;
459
460         /* active nodes should remain in cache */
461         if (!kdbus_node_is_deactivated(node))
462                 return;
463
464         /* nodes that were never linked were never instantiated */
465         if (!node->parent)
466                 return;
467
468         /* acquire domain and verify all ancestors are active */
469         domain = fs_acquire_domain(node);
470         if (!domain)
471                 return;
472
473         switch (node->type) {
474         case KDBUS_NODE_ENDPOINT:
475                 if (WARN_ON(!node->parent || !node->parent->name))
476                         goto exit;
477
478                 name.name = node->parent->name;
479                 name.len = strlen(node->parent->name);
480                 parent_dentry = d_hash_and_lookup(domain->dentry, &name);
481                 if (IS_ERR_OR_NULL(parent_dentry))
482                         goto exit;
483
484                 /* fallthrough */
485         case KDBUS_NODE_BUS:
486                 if (WARN_ON(!node->name))
487                         goto exit;
488
489                 name.name = node->name;
490                 name.len = strlen(node->name);
491                 dentry = d_hash_and_lookup(parent_dentry ? : domain->dentry,
492                                            &name);
493                 if (!IS_ERR_OR_NULL(dentry)) {
494                         d_invalidate(dentry);
495                         dput(dentry);
496                 }
497
498                 dput(parent_dentry);
499                 break;
500
501         default:
502                 /* all other types are bound to their parent lifetime */
503                 break;
504         }
505
506 exit:
507         kdbus_node_release(&domain->node);
508 }