4 * Copyright (C) 2005 Greg Kroah-Hartman <gregkh@suse.de>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License version
8 * 2 as published by the Free Software Foundation.
10 * Based on fs/debugfs/inode.c which had the following copyright notice:
11 * Copyright (C) 2004 Greg Kroah-Hartman <greg@kroah.com>
12 * Copyright (C) 2004 IBM Inc.
16 #include <linux/module.h>
18 #include <linux/mount.h>
19 #include <linux/pagemap.h>
20 #include <linux/init.h>
21 #include <linux/namei.h>
22 #include <linux/security.h>
24 #define SECURITYFS_MAGIC 0x73636673
26 static struct vfsmount
*mount
;
27 static int mount_count
;
31 * I think I can get rid of these default_file_ops, but not quite sure...
33 static ssize_t
default_read_file(struct file
*file
, char __user
*buf
,
34 size_t count
, loff_t
*ppos
)
39 static ssize_t
default_write_file(struct file
*file
, const char __user
*buf
,
40 size_t count
, loff_t
*ppos
)
45 static int default_open(struct inode
*inode
, struct file
*file
)
47 if (inode
->u
.generic_ip
)
48 file
->private_data
= inode
->u
.generic_ip
;
53 static struct file_operations default_file_ops
= {
54 .read
= default_read_file
,
55 .write
= default_write_file
,
59 static struct inode
*get_inode(struct super_block
*sb
, int mode
, dev_t dev
)
61 struct inode
*inode
= new_inode(sb
);
67 inode
->i_blksize
= PAGE_CACHE_SIZE
;
69 inode
->i_atime
= inode
->i_mtime
= inode
->i_ctime
= CURRENT_TIME
;
70 switch (mode
& S_IFMT
) {
72 init_special_inode(inode
, mode
, dev
);
75 inode
->i_fop
= &default_file_ops
;
78 inode
->i_op
= &simple_dir_inode_operations
;
79 inode
->i_fop
= &simple_dir_operations
;
81 /* directory inodes start off with i_nlink == 2 (for "." entry) */
90 static int mknod(struct inode
*dir
, struct dentry
*dentry
,
99 inode
= get_inode(dir
->i_sb
, mode
, dev
);
101 d_instantiate(dentry
, inode
);
108 static int mkdir(struct inode
*dir
, struct dentry
*dentry
, int mode
)
112 mode
= (mode
& (S_IRWXUGO
| S_ISVTX
)) | S_IFDIR
;
113 res
= mknod(dir
, dentry
, mode
, 0);
119 static int create(struct inode
*dir
, struct dentry
*dentry
, int mode
)
121 mode
= (mode
& S_IALLUGO
) | S_IFREG
;
122 return mknod(dir
, dentry
, mode
, 0);
125 static inline int positive(struct dentry
*dentry
)
127 return dentry
->d_inode
&& !d_unhashed(dentry
);
130 static int fill_super(struct super_block
*sb
, void *data
, int silent
)
132 static struct tree_descr files
[] = {{""}};
134 return simple_fill_super(sb
, SECURITYFS_MAGIC
, files
);
137 static int get_sb(struct file_system_type
*fs_type
,
138 int flags
, const char *dev_name
,
139 void *data
, struct vfsmount
*mnt
)
141 return get_sb_single(fs_type
, flags
, data
, fill_super
, mnt
);
144 static struct file_system_type fs_type
= {
145 .owner
= THIS_MODULE
,
146 .name
= "securityfs",
148 .kill_sb
= kill_litter_super
,
151 static int create_by_name(const char *name
, mode_t mode
,
152 struct dentry
*parent
,
153 struct dentry
**dentry
)
159 /* If the parent is not specified, we create it in the root.
160 * We need the root dentry to do this, which is in the super
161 * block. A pointer to that is in the struct vfsmount that we
165 if (mount
&& mount
->mnt_sb
) {
166 parent
= mount
->mnt_sb
->s_root
;
170 pr_debug("securityfs: Ah! can not find a parent!\n");
174 mutex_lock(&parent
->d_inode
->i_mutex
);
175 *dentry
= lookup_one_len(name
, parent
, strlen(name
));
176 if (!IS_ERR(dentry
)) {
177 if ((mode
& S_IFMT
) == S_IFDIR
)
178 error
= mkdir(parent
->d_inode
, *dentry
, mode
);
180 error
= create(parent
->d_inode
, *dentry
, mode
);
182 error
= PTR_ERR(dentry
);
183 mutex_unlock(&parent
->d_inode
->i_mutex
);
189 * securityfs_create_file - create a file in the securityfs filesystem
191 * @name: a pointer to a string containing the name of the file to create.
192 * @mode: the permission that the file should have
193 * @parent: a pointer to the parent dentry for this file. This should be a
194 * directory dentry if set. If this paramater is NULL, then the
195 * file will be created in the root of the securityfs filesystem.
196 * @data: a pointer to something that the caller will want to get to later
197 * on. The inode.u.generic_ip pointer will point to this value on
199 * @fops: a pointer to a struct file_operations that should be used for
202 * This is the basic "create a file" function for securityfs. It allows for a
203 * wide range of flexibility in createing a file, or a directory (if you
204 * want to create a directory, the securityfs_create_dir() function is
205 * recommended to be used instead.)
207 * This function will return a pointer to a dentry if it succeeds. This
208 * pointer must be passed to the securityfs_remove() function when the file is
209 * to be removed (no automatic cleanup happens if your module is unloaded,
210 * you are responsible here.) If an error occurs, NULL will be returned.
212 * If securityfs is not enabled in the kernel, the value -ENODEV will be
213 * returned. It is not wise to check for this value, but rather, check for
214 * NULL or !NULL instead as to eliminate the need for #ifdef in the calling
217 struct dentry
*securityfs_create_file(const char *name
, mode_t mode
,
218 struct dentry
*parent
, void *data
,
219 struct file_operations
*fops
)
221 struct dentry
*dentry
= NULL
;
224 pr_debug("securityfs: creating file '%s'\n",name
);
226 error
= simple_pin_fs(&fs_type
, &mount
, &mount_count
);
228 dentry
= ERR_PTR(error
);
232 error
= create_by_name(name
, mode
, parent
, &dentry
);
234 dentry
= ERR_PTR(error
);
235 simple_release_fs(&mount
, &mount_count
);
239 if (dentry
->d_inode
) {
241 dentry
->d_inode
->i_fop
= fops
;
243 dentry
->d_inode
->u
.generic_ip
= data
;
248 EXPORT_SYMBOL_GPL(securityfs_create_file
);
251 * securityfs_create_dir - create a directory in the securityfs filesystem
253 * @name: a pointer to a string containing the name of the directory to
255 * @parent: a pointer to the parent dentry for this file. This should be a
256 * directory dentry if set. If this paramater is NULL, then the
257 * directory will be created in the root of the securityfs filesystem.
259 * This function creates a directory in securityfs with the given name.
261 * This function will return a pointer to a dentry if it succeeds. This
262 * pointer must be passed to the securityfs_remove() function when the file is
263 * to be removed (no automatic cleanup happens if your module is unloaded,
264 * you are responsible here.) If an error occurs, NULL will be returned.
266 * If securityfs is not enabled in the kernel, the value -ENODEV will be
267 * returned. It is not wise to check for this value, but rather, check for
268 * NULL or !NULL instead as to eliminate the need for #ifdef in the calling
271 struct dentry
*securityfs_create_dir(const char *name
, struct dentry
*parent
)
273 return securityfs_create_file(name
,
274 S_IFDIR
| S_IRWXU
| S_IRUGO
| S_IXUGO
,
277 EXPORT_SYMBOL_GPL(securityfs_create_dir
);
280 * securityfs_remove - removes a file or directory from the securityfs filesystem
282 * @dentry: a pointer to a the dentry of the file or directory to be
285 * This function removes a file or directory in securityfs that was previously
286 * created with a call to another securityfs function (like
287 * securityfs_create_file() or variants thereof.)
289 * This function is required to be called in order for the file to be
290 * removed, no automatic cleanup of files will happen when a module is
291 * removed, you are responsible here.
293 void securityfs_remove(struct dentry
*dentry
)
295 struct dentry
*parent
;
300 parent
= dentry
->d_parent
;
301 if (!parent
|| !parent
->d_inode
)
304 mutex_lock(&parent
->d_inode
->i_mutex
);
305 if (positive(dentry
)) {
306 if (dentry
->d_inode
) {
307 if (S_ISDIR(dentry
->d_inode
->i_mode
))
308 simple_rmdir(parent
->d_inode
, dentry
);
310 simple_unlink(parent
->d_inode
, dentry
);
314 mutex_unlock(&parent
->d_inode
->i_mutex
);
315 simple_release_fs(&mount
, &mount_count
);
317 EXPORT_SYMBOL_GPL(securityfs_remove
);
319 static decl_subsys(security
, NULL
, NULL
);
321 static int __init
securityfs_init(void)
325 kset_set_kset_s(&security_subsys
, kernel_subsys
);
326 retval
= subsystem_register(&security_subsys
);
330 retval
= register_filesystem(&fs_type
);
332 subsystem_unregister(&security_subsys
);
336 static void __exit
securityfs_exit(void)
338 simple_release_fs(&mount
, &mount_count
);
339 unregister_filesystem(&fs_type
);
340 subsystem_unregister(&security_subsys
);
343 core_initcall(securityfs_init
);
344 module_exit(securityfs_exit
);
345 MODULE_LICENSE("GPL");