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
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.
14 #include <linux/dcache.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>
35 #define kdbus_node_from_dentry(_dentry) \
36 ((struct kdbus_node *)(_dentry)->d_fsdata)
38 static struct inode *fs_inode_get(struct super_block *sb,
39 struct kdbus_node *node);
42 * Directory Management
45 static inline unsigned char kdbus_dt_type(struct kdbus_node *node)
48 case KDBUS_NODE_DOMAIN:
51 case KDBUS_NODE_CONTROL:
52 case KDBUS_NODE_ENDPOINT:
59 static int fs_dir_fop_iterate(struct file *file, struct dir_context *ctx)
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;
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
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.
85 if (!dir_emit_dots(file, ctx))
88 /* acquire @next; if deactivated, or seek detected, find next node */
90 if (next && ctx->pos == next->hash) {
91 if (kdbus_node_acquire(next))
94 next = kdbus_node_next_child(parent, next);
96 next = kdbus_node_find_closest(parent, ctx->pos);
98 kdbus_node_unref(old);
102 file->private_data = next;
103 ctx->pos = next->hash;
105 kdbus_node_release(next);
107 if (!dir_emit(ctx, next->name, strlen(next->name), next->id,
108 kdbus_dt_type(next)))
111 /* find next node after @next */
113 next = kdbus_node_next_child(parent, next);
114 kdbus_node_unref(old);
117 file->private_data = NULL;
123 static loff_t fs_dir_fop_llseek(struct file *file, loff_t offset, int whence)
125 struct inode *inode = file_inode(file);
128 /* protect f_off against fop_iterate */
129 mutex_lock(&inode->i_mutex);
130 ret = generic_file_llseek(file, offset, whence);
131 mutex_unlock(&inode->i_mutex);
136 static int fs_dir_fop_release(struct inode *inode, struct file *file)
138 kdbus_node_unref(file->private_data);
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,
149 static struct dentry *fs_dir_iop_lookup(struct inode *dir,
150 struct dentry *dentry,
153 struct dentry *dnew = NULL;
154 struct kdbus_node *parent;
155 struct kdbus_node *node;
158 parent = kdbus_node_from_dentry(dentry->d_parent);
159 if (!kdbus_node_acquire(parent))
162 /* returns reference to _acquired_ child node */
163 node = kdbus_node_find_child(parent, dentry->d_name.name);
165 dentry->d_fsdata = node;
166 inode = fs_inode_get(dir->i_sb, node);
168 dnew = ERR_CAST(inode);
170 dnew = d_splice_alias(inode, dentry);
172 kdbus_node_release(node);
175 kdbus_node_release(parent);
179 static const struct inode_operations fs_dir_iops = {
180 .permission = generic_permission,
181 .lookup = fs_dir_iop_lookup,
188 static const struct inode_operations fs_inode_iops = {
189 .permission = generic_permission,
192 static struct inode *fs_inode_get(struct super_block *sb,
193 struct kdbus_node *node)
197 inode = iget_locked(sb, node->id);
199 return ERR_PTR(-ENOMEM);
200 if (!(inode->i_state & I_NEW))
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;
207 inode->i_uid = node->uid;
208 inode->i_gid = node->gid;
210 switch (node->type) {
211 case KDBUS_NODE_DOMAIN:
213 inode->i_mode |= S_IFDIR;
214 inode->i_op = &fs_dir_iops;
215 inode->i_fop = &fs_dir_fops;
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;
226 unlock_new_inode(inode);
232 * Superblock Management
235 static int fs_super_dop_revalidate(struct dentry *dentry, unsigned int flags)
237 struct kdbus_node *node;
239 /* Force lookup on negatives */
240 if (!dentry->d_inode)
243 node = kdbus_node_from_dentry(dentry);
245 /* see whether the node has been removed */
246 if (!kdbus_node_is_active(node))
252 static void fs_super_dop_release(struct dentry *dentry)
254 kdbus_node_unref(dentry->d_fsdata);
257 static const struct dentry_operations fs_super_dops = {
258 .d_revalidate = fs_super_dop_revalidate,
259 .d_release = fs_super_dop_release,
262 static void fs_super_sop_evict_inode(struct inode *inode)
264 struct kdbus_node *node = kdbus_node_from_inode(inode);
266 truncate_inode_pages_final(&inode->i_data);
268 kdbus_node_unref(node);
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,
277 static int fs_super_fill(struct super_block *sb)
279 struct kdbus_domain *domain = sb->s_fs_info;
283 sb->s_blocksize = PAGE_CACHE_SIZE;
284 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
285 sb->s_magic = KDBUS_SUPER_MAGIC;
286 sb->s_maxbytes = MAX_LFS_FILESIZE;
287 sb->s_op = &fs_super_sops;
290 inode = fs_inode_get(sb, &domain->node);
292 return PTR_ERR(inode);
294 sb->s_root = d_make_root(inode);
296 /* d_make_root iput()s the inode on failure */
300 /* sb holds domain reference */
301 sb->s_root->d_fsdata = &domain->node;
302 sb->s_d_op = &fs_super_dops;
304 /* sb holds root reference */
305 domain->dentry = sb->s_root;
307 if (!kdbus_node_activate(&domain->node))
310 ret = kdbus_domain_populate(domain, KDBUS_MAKE_ACCESS_WORLD);
314 sb->s_flags |= MS_ACTIVE;
318 static void fs_super_kill(struct super_block *sb)
320 struct kdbus_domain *domain = sb->s_fs_info;
323 kdbus_node_deactivate(&domain->node);
324 domain->dentry = NULL;
328 kdbus_domain_unref(domain);
331 static int fs_super_set(struct super_block *sb, void *data)
335 ret = set_anon_super(sb, data);
337 sb->s_fs_info = data;
342 static struct dentry *fs_super_mount(struct file_system_type *fs_type,
343 int flags, const char *dev_name,
346 struct kdbus_domain *domain;
347 struct super_block *sb;
350 domain = kdbus_domain_new(KDBUS_MAKE_ACCESS_WORLD);
352 return ERR_CAST(domain);
354 sb = sget(fs_type, NULL, fs_super_set, flags, domain);
356 kdbus_node_deactivate(&domain->node);
357 kdbus_domain_unref(domain);
361 WARN_ON(sb->s_fs_info != domain);
364 ret = fs_super_fill(sb);
366 /* calls into ->kill_sb() when done */
367 deactivate_locked_super(sb);
371 return dget(sb->s_root);
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,
383 * kdbus_fs_init() - register kdbus filesystem
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.
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
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.
399 * Return: 0 on success, negative error otherwise.
401 int kdbus_fs_init(void)
403 return register_filesystem(&fs_type);
407 * kdbus_fs_exit() - unregister kdbus filesystem
409 * This does the reverse to kdbus_fs_init(). It unregisters the kdbusfs
410 * filesystem from VFS and cleans up any allocated resources.
412 void kdbus_fs_exit(void)
414 unregister_filesystem(&fs_type);
417 /* acquire domain of @node, making sure all ancestors are active */
418 static struct kdbus_domain *fs_acquire_domain(struct kdbus_node *node)
420 struct kdbus_domain *domain;
421 struct kdbus_node *iter;
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))
428 /* root nodes are always domains */
429 if (WARN_ON(iter->type != KDBUS_NODE_DOMAIN))
432 domain = kdbus_domain_from_node(iter);
433 if (!kdbus_node_acquire(&domain->node))
440 * kdbus_fs_flush() - flush dcache entries of a node
441 * @node: Node to flush entries of
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
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
454 void kdbus_fs_flush(struct kdbus_node *node)
456 struct dentry *dentry, *parent_dentry = NULL;
457 struct kdbus_domain *domain;
460 /* active nodes should remain in cache */
461 if (!kdbus_node_is_deactivated(node))
464 /* nodes that were never linked were never instantiated */
468 /* acquire domain and verify all ancestors are active */
469 domain = fs_acquire_domain(node);
473 switch (node->type) {
474 case KDBUS_NODE_ENDPOINT:
475 if (WARN_ON(!node->parent || !node->parent->name))
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))
486 if (WARN_ON(!node->name))
489 name.name = node->name;
490 name.len = strlen(node->name);
491 dentry = d_hash_and_lookup(parent_dentry ? : domain->dentry,
493 if (!IS_ERR_OR_NULL(dentry)) {
494 d_invalidate(dentry);
502 /* all other types are bound to their parent lifetime */
507 kdbus_node_release(&domain->node);