Blame view

fs/anon_inodes.c 4.56 KB
457c89965   Thomas Gleixner   treewide: Add SPD...
1
  // SPDX-License-Identifier: GPL-2.0-only
5dc8bf813   Davide Libenzi   signal/timer/even...
2
3
4
5
6
7
8
9
10
  /*
   *  fs/anon_inodes.c
   *
   *  Copyright (C) 2007  Davide Libenzi <davidel@xmailserver.org>
   *
   *  Thanks to Arnd Bergmann for code review and suggestions.
   *  More changes for Thomas Gleixner suggestions.
   *
   */
a99bbaf5e   Alexey Dobriyan   headers: remove s...
11
  #include <linux/cred.h>
5dc8bf813   Davide Libenzi   signal/timer/even...
12
13
  #include <linux/file.h>
  #include <linux/poll.h>
a99bbaf5e   Alexey Dobriyan   headers: remove s...
14
  #include <linux/sched.h>
5dc8bf813   Davide Libenzi   signal/timer/even...
15
16
17
18
19
20
21
  #include <linux/init.h>
  #include <linux/fs.h>
  #include <linux/mount.h>
  #include <linux/module.h>
  #include <linux/kernel.h>
  #include <linux/magic.h>
  #include <linux/anon_inodes.h>
33cada40b   David Howells   vfs: Convert anon...
22
  #include <linux/pseudo_fs.h>
5dc8bf813   Davide Libenzi   signal/timer/even...
23

7c0f6ba68   Linus Torvalds   Replace <asm/uacc...
24
  #include <linux/uaccess.h>
5dc8bf813   Davide Libenzi   signal/timer/even...
25
26
27
  
  static struct vfsmount *anon_inode_mnt __read_mostly;
  static struct inode *anon_inode_inode;
5dc8bf813   Davide Libenzi   signal/timer/even...
28

b9aff027b   Nick Piggin   fs: anon_inodes i...
29
30
31
32
33
34
35
36
  /*
   * anon_inodefs_dname() is called from d_path().
   */
  static char *anon_inodefs_dname(struct dentry *dentry, char *buffer, int buflen)
  {
  	return dynamic_dname(dentry, buffer, buflen, "anon_inode:%s",
  				dentry->d_name.name);
  }
c74a1cbb3   Al Viro   pass default dent...
37
38
39
  static const struct dentry_operations anon_inodefs_dentry_operations = {
  	.d_dname	= anon_inodefs_dname,
  };
33cada40b   David Howells   vfs: Convert anon...
40
  static int anon_inodefs_init_fs_context(struct fs_context *fc)
ca7068c41   Al Viro   anon_inodes: move...
41
  {
33cada40b   David Howells   vfs: Convert anon...
42
43
44
45
46
  	struct pseudo_fs_context *ctx = init_pseudo(fc, ANON_INODE_FS_MAGIC);
  	if (!ctx)
  		return -ENOMEM;
  	ctx->dops = &anon_inodefs_dentry_operations;
  	return 0;
ca7068c41   Al Viro   anon_inodes: move...
47
48
49
50
  }
  
  static struct file_system_type anon_inode_fs_type = {
  	.name		= "anon_inodefs",
33cada40b   David Howells   vfs: Convert anon...
51
  	.init_fs_context = anon_inodefs_init_fs_context,
ca7068c41   Al Viro   anon_inodes: move...
52
53
  	.kill_sb	= kill_anon_super,
  };
5dc8bf813   Davide Libenzi   signal/timer/even...
54
  /**
c0d8768af   Namhyung Kim   anon_inodes: fix ...
55
56
57
   * anon_inode_getfile - creates a new file instance by hooking it up to an
   *                      anonymous inode, and a dentry that describe the "class"
   *                      of the file
5dc8bf813   Davide Libenzi   signal/timer/even...
58
   *
5dc8bf813   Davide Libenzi   signal/timer/even...
59
   * @name:    [in]    name of the "class" of the new file
7d9dbca34   Ulrich Drepper   flag parameters: ...
60
61
62
   * @fops:    [in]    file operations for the new file
   * @priv:    [in]    private data for the new file (will be file's private_data)
   * @flags:   [in]    flags
5dc8bf813   Davide Libenzi   signal/timer/even...
63
64
65
   *
   * Creates a new file by hooking it on a single inode. This is useful for files
   * that do not need to have a full-fledged inode in order to operate correctly.
562787a5c   Davide Libenzi   anonfd: split int...
66
   * All the files created with anon_inode_getfile() will share a single inode,
5dc8bf813   Davide Libenzi   signal/timer/even...
67
   * hence saving memory and avoiding code duplication for the file/inode/dentry
562787a5c   Davide Libenzi   anonfd: split int...
68
   * setup.  Returns the newly created file* or an error pointer.
5dc8bf813   Davide Libenzi   signal/timer/even...
69
   */
562787a5c   Davide Libenzi   anonfd: split int...
70
71
72
  struct file *anon_inode_getfile(const char *name,
  				const struct file_operations *fops,
  				void *priv, int flags)
5dc8bf813   Davide Libenzi   signal/timer/even...
73
  {
5dc8bf813   Davide Libenzi   signal/timer/even...
74
  	struct file *file;
5dc8bf813   Davide Libenzi   signal/timer/even...
75
76
  
  	if (IS_ERR(anon_inode_inode))
562787a5c   Davide Libenzi   anonfd: split int...
77
  		return ERR_PTR(-ENODEV);
5dc8bf813   Davide Libenzi   signal/timer/even...
78

e3a2a0d4e   Christian Borntraeger   anon_inodes: use ...
79
  	if (fops->owner && !try_module_get(fops->owner))
562787a5c   Davide Libenzi   anonfd: split int...
80
  		return ERR_PTR(-ENOENT);
5dc8bf813   Davide Libenzi   signal/timer/even...
81
82
  
  	/*
96fdc72dd   Davide Libenzi   anon-inodes use o...
83
  	 * We know the anon_inode inode count is always greater than zero,
7de9c6ee3   Al Viro   new helper: ihold()
84
  	 * so ihold() is safe.
96fdc72dd   Davide Libenzi   anon-inodes use o...
85
  	 */
7de9c6ee3   Al Viro   new helper: ihold()
86
  	ihold(anon_inode_inode);
52c91f8b3   Al Viro   anon_inode_getfil...
87
88
  	file = alloc_file_pseudo(anon_inode_inode, anon_inode_mnt, name,
  				 flags & (O_ACCMODE | O_NONBLOCK), fops);
39b652527   Anatol Pomozov   fs: Preserve erro...
89
  	if (IS_ERR(file))
52c91f8b3   Al Viro   anon_inode_getfil...
90
  		goto err;
96fdc72dd   Davide Libenzi   anon-inodes use o...
91
  	file->f_mapping = anon_inode_inode->i_mapping;
5dc8bf813   Davide Libenzi   signal/timer/even...
92

5dc8bf813   Davide Libenzi   signal/timer/even...
93
  	file->private_data = priv;
562787a5c   Davide Libenzi   anonfd: split int...
94
  	return file;
52c91f8b3   Al Viro   anon_inode_getfil...
95
96
  err:
  	iput(anon_inode_inode);
562787a5c   Davide Libenzi   anonfd: split int...
97
  	module_put(fops->owner);
39b652527   Anatol Pomozov   fs: Preserve erro...
98
  	return file;
562787a5c   Davide Libenzi   anonfd: split int...
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
  }
  EXPORT_SYMBOL_GPL(anon_inode_getfile);
  
  /**
   * anon_inode_getfd - creates a new file instance by hooking it up to an
   *                    anonymous inode, and a dentry that describe the "class"
   *                    of the file
   *
   * @name:    [in]    name of the "class" of the new file
   * @fops:    [in]    file operations for the new file
   * @priv:    [in]    private data for the new file (will be file's private_data)
   * @flags:   [in]    flags
   *
   * Creates a new file by hooking it on a single inode. This is useful for files
   * that do not need to have a full-fledged inode in order to operate correctly.
   * All the files created with anon_inode_getfd() will share a single inode,
   * hence saving memory and avoiding code duplication for the file/inode/dentry
   * setup.  Returns new descriptor or an error code.
   */
  int anon_inode_getfd(const char *name, const struct file_operations *fops,
  		     void *priv, int flags)
  {
  	int error, fd;
  	struct file *file;
  
  	error = get_unused_fd_flags(flags);
  	if (error < 0)
  		return error;
  	fd = error;
  
  	file = anon_inode_getfile(name, fops, priv, flags);
  	if (IS_ERR(file)) {
  		error = PTR_ERR(file);
  		goto err_put_unused_fd;
  	}
5dc8bf813   Davide Libenzi   signal/timer/even...
134
  	fd_install(fd, file);
2030a42ce   Al Viro   [PATCH] sanitize ...
135
  	return fd;
5dc8bf813   Davide Libenzi   signal/timer/even...
136
137
138
  
  err_put_unused_fd:
  	put_unused_fd(fd);
5dc8bf813   Davide Libenzi   signal/timer/even...
139
140
  	return error;
  }
d6d281684   Avi Kivity   KVM: Remove kvmfs...
141
  EXPORT_SYMBOL_GPL(anon_inode_getfd);
5dc8bf813   Davide Libenzi   signal/timer/even...
142

5dc8bf813   Davide Libenzi   signal/timer/even...
143
144
  static int __init anon_inode_init(void)
  {
5dc8bf813   Davide Libenzi   signal/timer/even...
145
  	anon_inode_mnt = kern_mount(&anon_inode_fs_type);
75c5a52da   Jan Kara   vfs: Allocate ano...
146
147
148
  	if (IS_ERR(anon_inode_mnt))
  		panic("anon_inode_init() kernel mount failed (%ld)
  ", PTR_ERR(anon_inode_mnt));
5dc8bf813   Davide Libenzi   signal/timer/even...
149

75c5a52da   Jan Kara   vfs: Allocate ano...
150
151
152
153
154
155
  	anon_inode_inode = alloc_anon_inode(anon_inode_mnt->mnt_sb);
  	if (IS_ERR(anon_inode_inode))
  		panic("anon_inode_init() inode allocation failed (%ld)
  ", PTR_ERR(anon_inode_inode));
  
  	return 0;
5dc8bf813   Davide Libenzi   signal/timer/even...
156
157
158
  }
  
  fs_initcall(anon_inode_init);