Linux Audio

Check our new training course

Loading...
v6.2
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * fs/kernfs/inode.c - kernfs inode implementation
  4 *
  5 * Copyright (c) 2001-3 Patrick Mochel
  6 * Copyright (c) 2007 SUSE Linux Products GmbH
  7 * Copyright (c) 2007, 2013 Tejun Heo <tj@kernel.org>
 
 
  8 */
  9
 10#include <linux/pagemap.h>
 11#include <linux/backing-dev.h>
 12#include <linux/capability.h>
 13#include <linux/errno.h>
 14#include <linux/slab.h>
 15#include <linux/xattr.h>
 16#include <linux/security.h>
 17
 18#include "kernfs-internal.h"
 19
 
 
 
 
 
 
 
 
 
 
 
 
 20static const struct inode_operations kernfs_iops = {
 21	.permission	= kernfs_iop_permission,
 22	.setattr	= kernfs_iop_setattr,
 23	.getattr	= kernfs_iop_getattr,
 
 
 
 24	.listxattr	= kernfs_iop_listxattr,
 25};
 26
 27static struct kernfs_iattrs *__kernfs_iattrs(struct kernfs_node *kn, int alloc)
 
 
 
 
 
 
 28{
 29	static DEFINE_MUTEX(iattr_mutex);
 30	struct kernfs_iattrs *ret;
 
 31
 32	mutex_lock(&iattr_mutex);
 33
 34	if (kn->iattr || !alloc)
 35		goto out_unlock;
 36
 37	kn->iattr = kmem_cache_zalloc(kernfs_iattrs_cache, GFP_KERNEL);
 38	if (!kn->iattr)
 39		goto out_unlock;
 
 40
 41	/* assign default attributes */
 42	kn->iattr->ia_uid = GLOBAL_ROOT_UID;
 43	kn->iattr->ia_gid = GLOBAL_ROOT_GID;
 44
 45	ktime_get_real_ts64(&kn->iattr->ia_atime);
 46	kn->iattr->ia_mtime = kn->iattr->ia_atime;
 47	kn->iattr->ia_ctime = kn->iattr->ia_atime;
 48
 49	simple_xattrs_init(&kn->iattr->xattrs);
 50	atomic_set(&kn->iattr->nr_user_xattrs, 0);
 51	atomic_set(&kn->iattr->user_xattr_size, 0);
 52out_unlock:
 53	ret = kn->iattr;
 54	mutex_unlock(&iattr_mutex);
 55	return ret;
 56}
 57
 58static struct kernfs_iattrs *kernfs_iattrs(struct kernfs_node *kn)
 59{
 60	return __kernfs_iattrs(kn, 1);
 61}
 62
 63static struct kernfs_iattrs *kernfs_iattrs_noalloc(struct kernfs_node *kn)
 64{
 65	return __kernfs_iattrs(kn, 0);
 66}
 67
 68int __kernfs_setattr(struct kernfs_node *kn, const struct iattr *iattr)
 69{
 70	struct kernfs_iattrs *attrs;
 
 71	unsigned int ia_valid = iattr->ia_valid;
 72
 73	attrs = kernfs_iattrs(kn);
 74	if (!attrs)
 75		return -ENOMEM;
 76
 
 
 77	if (ia_valid & ATTR_UID)
 78		attrs->ia_uid = iattr->ia_uid;
 79	if (ia_valid & ATTR_GID)
 80		attrs->ia_gid = iattr->ia_gid;
 81	if (ia_valid & ATTR_ATIME)
 82		attrs->ia_atime = iattr->ia_atime;
 83	if (ia_valid & ATTR_MTIME)
 84		attrs->ia_mtime = iattr->ia_mtime;
 85	if (ia_valid & ATTR_CTIME)
 86		attrs->ia_ctime = iattr->ia_ctime;
 87	if (ia_valid & ATTR_MODE)
 88		kn->mode = iattr->ia_mode;
 
 
 89	return 0;
 90}
 91
 92/**
 93 * kernfs_setattr - set iattr on a node
 94 * @kn: target node
 95 * @iattr: iattr to set
 96 *
 97 * Return: %0 on success, -errno on failure.
 98 */
 99int kernfs_setattr(struct kernfs_node *kn, const struct iattr *iattr)
100{
101	int ret;
102	struct kernfs_root *root = kernfs_root(kn);
103
104	down_write(&root->kernfs_rwsem);
105	ret = __kernfs_setattr(kn, iattr);
106	up_write(&root->kernfs_rwsem);
107	return ret;
108}
109
110int kernfs_iop_setattr(struct user_namespace *mnt_userns, struct dentry *dentry,
111		       struct iattr *iattr)
112{
113	struct inode *inode = d_inode(dentry);
114	struct kernfs_node *kn = inode->i_private;
115	struct kernfs_root *root;
116	int error;
117
118	if (!kn)
119		return -EINVAL;
120
121	root = kernfs_root(kn);
122	down_write(&root->kernfs_rwsem);
123	error = setattr_prepare(&init_user_ns, dentry, iattr);
124	if (error)
125		goto out;
126
127	error = __kernfs_setattr(kn, iattr);
128	if (error)
129		goto out;
130
131	/* this ignores size changes */
132	setattr_copy(&init_user_ns, inode, iattr);
133
134out:
135	up_write(&root->kernfs_rwsem);
136	return error;
137}
138
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
139ssize_t kernfs_iop_listxattr(struct dentry *dentry, char *buf, size_t size)
140{
141	struct kernfs_node *kn = kernfs_dentry_node(dentry);
142	struct kernfs_iattrs *attrs;
143
144	attrs = kernfs_iattrs(kn);
145	if (!attrs)
146		return -ENOMEM;
147
148	return simple_xattr_list(d_inode(dentry), &attrs->xattrs, buf, size);
149}
150
151static inline void set_default_inode_attr(struct inode *inode, umode_t mode)
152{
153	inode->i_mode = mode;
154	inode->i_atime = inode->i_mtime =
155		inode->i_ctime = current_time(inode);
156}
157
158static inline void set_inode_attr(struct inode *inode,
159				  struct kernfs_iattrs *attrs)
160{
161	inode->i_uid = attrs->ia_uid;
162	inode->i_gid = attrs->ia_gid;
163	inode->i_atime = attrs->ia_atime;
164	inode->i_mtime = attrs->ia_mtime;
165	inode->i_ctime = attrs->ia_ctime;
166}
167
168static void kernfs_refresh_inode(struct kernfs_node *kn, struct inode *inode)
169{
170	struct kernfs_iattrs *attrs = kn->iattr;
171
172	inode->i_mode = kn->mode;
173	if (attrs)
174		/*
175		 * kernfs_node has non-default attributes get them from
176		 * persistent copy in kernfs_node.
177		 */
178		set_inode_attr(inode, attrs);
 
 
 
179
180	if (kernfs_type(kn) == KERNFS_DIR)
181		set_nlink(inode, kn->dir.subdirs + 2);
182}
183
184int kernfs_iop_getattr(struct user_namespace *mnt_userns,
185		       const struct path *path, struct kstat *stat,
186		       u32 request_mask, unsigned int query_flags)
187{
188	struct inode *inode = d_inode(path->dentry);
189	struct kernfs_node *kn = inode->i_private;
190	struct kernfs_root *root = kernfs_root(kn);
191
192	down_read(&root->kernfs_rwsem);
193	kernfs_refresh_inode(kn, inode);
194	generic_fillattr(&init_user_ns, inode, stat);
195	up_read(&root->kernfs_rwsem);
196
 
197	return 0;
198}
199
200static void kernfs_init_inode(struct kernfs_node *kn, struct inode *inode)
201{
202	kernfs_get(kn);
203	inode->i_private = kn;
204	inode->i_mapping->a_ops = &ram_aops;
 
205	inode->i_op = &kernfs_iops;
206	inode->i_generation = kernfs_gen(kn);
207
208	set_default_inode_attr(inode, kn->mode);
209	kernfs_refresh_inode(kn, inode);
210
211	/* initialize inode according to type */
212	switch (kernfs_type(kn)) {
213	case KERNFS_DIR:
214		inode->i_op = &kernfs_dir_iops;
215		inode->i_fop = &kernfs_dir_fops;
216		if (kn->flags & KERNFS_EMPTY_DIR)
217			make_empty_dir_inode(inode);
218		break;
219	case KERNFS_FILE:
220		inode->i_size = kn->attr.size;
221		inode->i_fop = &kernfs_file_fops;
222		break;
223	case KERNFS_LINK:
224		inode->i_op = &kernfs_symlink_iops;
225		break;
226	default:
227		BUG();
228	}
229
230	unlock_new_inode(inode);
231}
232
233/**
234 *	kernfs_get_inode - get inode for kernfs_node
235 *	@sb: super block
236 *	@kn: kernfs_node to allocate inode for
237 *
238 *	Get inode for @kn.  If such inode doesn't exist, a new inode is
239 *	allocated and basics are initialized.  New inode is returned
240 *	locked.
241 *
242 *	Locking:
243 *	Kernel thread context (may sleep).
244 *
245 *	Return:
246 *	Pointer to allocated inode on success, %NULL on failure.
247 */
248struct inode *kernfs_get_inode(struct super_block *sb, struct kernfs_node *kn)
249{
250	struct inode *inode;
251
252	inode = iget_locked(sb, kernfs_ino(kn));
253	if (inode && (inode->i_state & I_NEW))
254		kernfs_init_inode(kn, inode);
255
256	return inode;
257}
258
259/*
260 * The kernfs_node serves as both an inode and a directory entry for
261 * kernfs.  To prevent the kernfs inode numbers from being freed
262 * prematurely we take a reference to kernfs_node from the kernfs inode.  A
263 * super_operations.evict_inode() implementation is needed to drop that
264 * reference upon inode destruction.
265 */
266void kernfs_evict_inode(struct inode *inode)
267{
268	struct kernfs_node *kn = inode->i_private;
269
270	truncate_inode_pages_final(&inode->i_data);
271	clear_inode(inode);
272	kernfs_put(kn);
273}
274
275int kernfs_iop_permission(struct user_namespace *mnt_userns,
276			  struct inode *inode, int mask)
277{
278	struct kernfs_node *kn;
279	struct kernfs_root *root;
280	int ret;
281
282	if (mask & MAY_NOT_BLOCK)
283		return -ECHILD;
284
285	kn = inode->i_private;
286	root = kernfs_root(kn);
287
288	down_read(&root->kernfs_rwsem);
289	kernfs_refresh_inode(kn, inode);
290	ret = generic_permission(&init_user_ns, inode, mask);
291	up_read(&root->kernfs_rwsem);
292
293	return ret;
294}
295
296int kernfs_xattr_get(struct kernfs_node *kn, const char *name,
297		     void *value, size_t size)
298{
299	struct kernfs_iattrs *attrs = kernfs_iattrs_noalloc(kn);
300	if (!attrs)
301		return -ENODATA;
302
303	return simple_xattr_get(&attrs->xattrs, name, value, size);
304}
305
306int kernfs_xattr_set(struct kernfs_node *kn, const char *name,
307		     const void *value, size_t size, int flags)
308{
309	struct kernfs_iattrs *attrs = kernfs_iattrs(kn);
310	if (!attrs)
311		return -ENOMEM;
312
313	return simple_xattr_set(&attrs->xattrs, name, value, size, flags, NULL);
314}
315
316static int kernfs_vfs_xattr_get(const struct xattr_handler *handler,
317				struct dentry *unused, struct inode *inode,
318				const char *suffix, void *value, size_t size)
319{
320	const char *name = xattr_full_name(handler, suffix);
321	struct kernfs_node *kn = inode->i_private;
322
323	return kernfs_xattr_get(kn, name, value, size);
324}
325
326static int kernfs_vfs_xattr_set(const struct xattr_handler *handler,
327				struct user_namespace *mnt_userns,
328				struct dentry *unused, struct inode *inode,
329				const char *suffix, const void *value,
330				size_t size, int flags)
331{
332	const char *name = xattr_full_name(handler, suffix);
333	struct kernfs_node *kn = inode->i_private;
334
335	return kernfs_xattr_set(kn, name, value, size, flags);
336}
337
338static int kernfs_vfs_user_xattr_add(struct kernfs_node *kn,
339				     const char *full_name,
340				     struct simple_xattrs *xattrs,
341				     const void *value, size_t size, int flags)
342{
343	atomic_t *sz = &kn->iattr->user_xattr_size;
344	atomic_t *nr = &kn->iattr->nr_user_xattrs;
345	ssize_t removed_size;
346	int ret;
347
348	if (atomic_inc_return(nr) > KERNFS_MAX_USER_XATTRS) {
349		ret = -ENOSPC;
350		goto dec_count_out;
351	}
352
353	if (atomic_add_return(size, sz) > KERNFS_USER_XATTR_SIZE_LIMIT) {
354		ret = -ENOSPC;
355		goto dec_size_out;
356	}
357
358	ret = simple_xattr_set(xattrs, full_name, value, size, flags,
359			       &removed_size);
360
361	if (!ret && removed_size >= 0)
362		size = removed_size;
363	else if (!ret)
364		return 0;
365dec_size_out:
366	atomic_sub(size, sz);
367dec_count_out:
368	atomic_dec(nr);
369	return ret;
370}
371
372static int kernfs_vfs_user_xattr_rm(struct kernfs_node *kn,
373				    const char *full_name,
374				    struct simple_xattrs *xattrs,
375				    const void *value, size_t size, int flags)
376{
377	atomic_t *sz = &kn->iattr->user_xattr_size;
378	atomic_t *nr = &kn->iattr->nr_user_xattrs;
379	ssize_t removed_size;
380	int ret;
381
382	ret = simple_xattr_set(xattrs, full_name, value, size, flags,
383			       &removed_size);
384
385	if (removed_size >= 0) {
386		atomic_sub(removed_size, sz);
387		atomic_dec(nr);
388	}
389
390	return ret;
391}
392
393static int kernfs_vfs_user_xattr_set(const struct xattr_handler *handler,
394				     struct user_namespace *mnt_userns,
395				     struct dentry *unused, struct inode *inode,
396				     const char *suffix, const void *value,
397				     size_t size, int flags)
398{
399	const char *full_name = xattr_full_name(handler, suffix);
400	struct kernfs_node *kn = inode->i_private;
401	struct kernfs_iattrs *attrs;
402
403	if (!(kernfs_root(kn)->flags & KERNFS_ROOT_SUPPORT_USER_XATTR))
404		return -EOPNOTSUPP;
405
406	attrs = kernfs_iattrs(kn);
407	if (!attrs)
408		return -ENOMEM;
409
410	if (value)
411		return kernfs_vfs_user_xattr_add(kn, full_name, &attrs->xattrs,
412						 value, size, flags);
413	else
414		return kernfs_vfs_user_xattr_rm(kn, full_name, &attrs->xattrs,
415						value, size, flags);
416
 
417}
418
419static const struct xattr_handler kernfs_trusted_xattr_handler = {
420	.prefix = XATTR_TRUSTED_PREFIX,
421	.get = kernfs_vfs_xattr_get,
422	.set = kernfs_vfs_xattr_set,
423};
424
425static const struct xattr_handler kernfs_security_xattr_handler = {
426	.prefix = XATTR_SECURITY_PREFIX,
427	.get = kernfs_vfs_xattr_get,
428	.set = kernfs_vfs_xattr_set,
429};
430
431static const struct xattr_handler kernfs_user_xattr_handler = {
432	.prefix = XATTR_USER_PREFIX,
433	.get = kernfs_vfs_xattr_get,
434	.set = kernfs_vfs_user_xattr_set,
435};
436
437const struct xattr_handler *kernfs_xattr_handlers[] = {
438	&kernfs_trusted_xattr_handler,
439	&kernfs_security_xattr_handler,
440	&kernfs_user_xattr_handler,
441	NULL
442};
v3.15
 
  1/*
  2 * fs/kernfs/inode.c - kernfs inode implementation
  3 *
  4 * Copyright (c) 2001-3 Patrick Mochel
  5 * Copyright (c) 2007 SUSE Linux Products GmbH
  6 * Copyright (c) 2007, 2013 Tejun Heo <tj@kernel.org>
  7 *
  8 * This file is released under the GPLv2.
  9 */
 10
 11#include <linux/pagemap.h>
 12#include <linux/backing-dev.h>
 13#include <linux/capability.h>
 14#include <linux/errno.h>
 15#include <linux/slab.h>
 16#include <linux/xattr.h>
 17#include <linux/security.h>
 18
 19#include "kernfs-internal.h"
 20
 21static const struct address_space_operations kernfs_aops = {
 22	.readpage	= simple_readpage,
 23	.write_begin	= simple_write_begin,
 24	.write_end	= simple_write_end,
 25};
 26
 27static struct backing_dev_info kernfs_bdi = {
 28	.name		= "kernfs",
 29	.ra_pages	= 0,	/* No readahead */
 30	.capabilities	= BDI_CAP_NO_ACCT_AND_WRITEBACK,
 31};
 32
 33static const struct inode_operations kernfs_iops = {
 34	.permission	= kernfs_iop_permission,
 35	.setattr	= kernfs_iop_setattr,
 36	.getattr	= kernfs_iop_getattr,
 37	.setxattr	= kernfs_iop_setxattr,
 38	.removexattr	= kernfs_iop_removexattr,
 39	.getxattr	= kernfs_iop_getxattr,
 40	.listxattr	= kernfs_iop_listxattr,
 41};
 42
 43void __init kernfs_inode_init(void)
 44{
 45	if (bdi_init(&kernfs_bdi))
 46		panic("failed to init kernfs_bdi");
 47}
 48
 49static struct kernfs_iattrs *kernfs_iattrs(struct kernfs_node *kn)
 50{
 51	static DEFINE_MUTEX(iattr_mutex);
 52	struct kernfs_iattrs *ret;
 53	struct iattr *iattrs;
 54
 55	mutex_lock(&iattr_mutex);
 56
 57	if (kn->iattr)
 58		goto out_unlock;
 59
 60	kn->iattr = kzalloc(sizeof(struct kernfs_iattrs), GFP_KERNEL);
 61	if (!kn->iattr)
 62		goto out_unlock;
 63	iattrs = &kn->iattr->ia_iattr;
 64
 65	/* assign default attributes */
 66	iattrs->ia_mode = kn->mode;
 67	iattrs->ia_uid = GLOBAL_ROOT_UID;
 68	iattrs->ia_gid = GLOBAL_ROOT_GID;
 69	iattrs->ia_atime = iattrs->ia_mtime = iattrs->ia_ctime = CURRENT_TIME;
 
 
 70
 71	simple_xattrs_init(&kn->iattr->xattrs);
 
 
 72out_unlock:
 73	ret = kn->iattr;
 74	mutex_unlock(&iattr_mutex);
 75	return ret;
 76}
 77
 78static int __kernfs_setattr(struct kernfs_node *kn, const struct iattr *iattr)
 
 
 
 
 
 
 
 
 
 
 79{
 80	struct kernfs_iattrs *attrs;
 81	struct iattr *iattrs;
 82	unsigned int ia_valid = iattr->ia_valid;
 83
 84	attrs = kernfs_iattrs(kn);
 85	if (!attrs)
 86		return -ENOMEM;
 87
 88	iattrs = &attrs->ia_iattr;
 89
 90	if (ia_valid & ATTR_UID)
 91		iattrs->ia_uid = iattr->ia_uid;
 92	if (ia_valid & ATTR_GID)
 93		iattrs->ia_gid = iattr->ia_gid;
 94	if (ia_valid & ATTR_ATIME)
 95		iattrs->ia_atime = iattr->ia_atime;
 96	if (ia_valid & ATTR_MTIME)
 97		iattrs->ia_mtime = iattr->ia_mtime;
 98	if (ia_valid & ATTR_CTIME)
 99		iattrs->ia_ctime = iattr->ia_ctime;
100	if (ia_valid & ATTR_MODE) {
101		umode_t mode = iattr->ia_mode;
102		iattrs->ia_mode = kn->mode = mode;
103	}
104	return 0;
105}
106
107/**
108 * kernfs_setattr - set iattr on a node
109 * @kn: target node
110 * @iattr: iattr to set
111 *
112 * Returns 0 on success, -errno on failure.
113 */
114int kernfs_setattr(struct kernfs_node *kn, const struct iattr *iattr)
115{
116	int ret;
 
117
118	mutex_lock(&kernfs_mutex);
119	ret = __kernfs_setattr(kn, iattr);
120	mutex_unlock(&kernfs_mutex);
121	return ret;
122}
123
124int kernfs_iop_setattr(struct dentry *dentry, struct iattr *iattr)
 
125{
126	struct inode *inode = dentry->d_inode;
127	struct kernfs_node *kn = dentry->d_fsdata;
 
128	int error;
129
130	if (!kn)
131		return -EINVAL;
132
133	mutex_lock(&kernfs_mutex);
134	error = inode_change_ok(inode, iattr);
 
135	if (error)
136		goto out;
137
138	error = __kernfs_setattr(kn, iattr);
139	if (error)
140		goto out;
141
142	/* this ignores size changes */
143	setattr_copy(inode, iattr);
144
145out:
146	mutex_unlock(&kernfs_mutex);
147	return error;
148}
149
150static int kernfs_node_setsecdata(struct kernfs_node *kn, void **secdata,
151				  u32 *secdata_len)
152{
153	struct kernfs_iattrs *attrs;
154	void *old_secdata;
155	size_t old_secdata_len;
156
157	attrs = kernfs_iattrs(kn);
158	if (!attrs)
159		return -ENOMEM;
160
161	old_secdata = attrs->ia_secdata;
162	old_secdata_len = attrs->ia_secdata_len;
163
164	attrs->ia_secdata = *secdata;
165	attrs->ia_secdata_len = *secdata_len;
166
167	*secdata = old_secdata;
168	*secdata_len = old_secdata_len;
169	return 0;
170}
171
172int kernfs_iop_setxattr(struct dentry *dentry, const char *name,
173			const void *value, size_t size, int flags)
174{
175	struct kernfs_node *kn = dentry->d_fsdata;
176	struct kernfs_iattrs *attrs;
177	void *secdata;
178	int error;
179	u32 secdata_len = 0;
180
181	attrs = kernfs_iattrs(kn);
182	if (!attrs)
183		return -ENOMEM;
184
185	if (!strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN)) {
186		const char *suffix = name + XATTR_SECURITY_PREFIX_LEN;
187		error = security_inode_setsecurity(dentry->d_inode, suffix,
188						value, size, flags);
189		if (error)
190			return error;
191		error = security_inode_getsecctx(dentry->d_inode,
192						&secdata, &secdata_len);
193		if (error)
194			return error;
195
196		mutex_lock(&kernfs_mutex);
197		error = kernfs_node_setsecdata(kn, &secdata, &secdata_len);
198		mutex_unlock(&kernfs_mutex);
199
200		if (secdata)
201			security_release_secctx(secdata, secdata_len);
202		return error;
203	} else if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN)) {
204		return simple_xattr_set(&attrs->xattrs, name, value, size,
205					flags);
206	}
207
208	return -EINVAL;
209}
210
211int kernfs_iop_removexattr(struct dentry *dentry, const char *name)
212{
213	struct kernfs_node *kn = dentry->d_fsdata;
214	struct kernfs_iattrs *attrs;
215
216	attrs = kernfs_iattrs(kn);
217	if (!attrs)
218		return -ENOMEM;
219
220	return simple_xattr_remove(&attrs->xattrs, name);
221}
222
223ssize_t kernfs_iop_getxattr(struct dentry *dentry, const char *name, void *buf,
224			    size_t size)
225{
226	struct kernfs_node *kn = dentry->d_fsdata;
227	struct kernfs_iattrs *attrs;
228
229	attrs = kernfs_iattrs(kn);
230	if (!attrs)
231		return -ENOMEM;
232
233	return simple_xattr_get(&attrs->xattrs, name, buf, size);
234}
235
236ssize_t kernfs_iop_listxattr(struct dentry *dentry, char *buf, size_t size)
237{
238	struct kernfs_node *kn = dentry->d_fsdata;
239	struct kernfs_iattrs *attrs;
240
241	attrs = kernfs_iattrs(kn);
242	if (!attrs)
243		return -ENOMEM;
244
245	return simple_xattr_list(&attrs->xattrs, buf, size);
246}
247
248static inline void set_default_inode_attr(struct inode *inode, umode_t mode)
249{
250	inode->i_mode = mode;
251	inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
 
252}
253
254static inline void set_inode_attr(struct inode *inode, struct iattr *iattr)
 
255{
256	inode->i_uid = iattr->ia_uid;
257	inode->i_gid = iattr->ia_gid;
258	inode->i_atime = iattr->ia_atime;
259	inode->i_mtime = iattr->ia_mtime;
260	inode->i_ctime = iattr->ia_ctime;
261}
262
263static void kernfs_refresh_inode(struct kernfs_node *kn, struct inode *inode)
264{
265	struct kernfs_iattrs *attrs = kn->iattr;
266
267	inode->i_mode = kn->mode;
268	if (attrs) {
269		/*
270		 * kernfs_node has non-default attributes get them from
271		 * persistent copy in kernfs_node.
272		 */
273		set_inode_attr(inode, &attrs->ia_iattr);
274		security_inode_notifysecctx(inode, attrs->ia_secdata,
275					    attrs->ia_secdata_len);
276	}
277
278	if (kernfs_type(kn) == KERNFS_DIR)
279		set_nlink(inode, kn->dir.subdirs + 2);
280}
281
282int kernfs_iop_getattr(struct vfsmount *mnt, struct dentry *dentry,
283		   struct kstat *stat)
 
284{
285	struct kernfs_node *kn = dentry->d_fsdata;
286	struct inode *inode = dentry->d_inode;
 
287
288	mutex_lock(&kernfs_mutex);
289	kernfs_refresh_inode(kn, inode);
290	mutex_unlock(&kernfs_mutex);
 
291
292	generic_fillattr(inode, stat);
293	return 0;
294}
295
296static void kernfs_init_inode(struct kernfs_node *kn, struct inode *inode)
297{
298	kernfs_get(kn);
299	inode->i_private = kn;
300	inode->i_mapping->a_ops = &kernfs_aops;
301	inode->i_mapping->backing_dev_info = &kernfs_bdi;
302	inode->i_op = &kernfs_iops;
 
303
304	set_default_inode_attr(inode, kn->mode);
305	kernfs_refresh_inode(kn, inode);
306
307	/* initialize inode according to type */
308	switch (kernfs_type(kn)) {
309	case KERNFS_DIR:
310		inode->i_op = &kernfs_dir_iops;
311		inode->i_fop = &kernfs_dir_fops;
 
 
312		break;
313	case KERNFS_FILE:
314		inode->i_size = kn->attr.size;
315		inode->i_fop = &kernfs_file_fops;
316		break;
317	case KERNFS_LINK:
318		inode->i_op = &kernfs_symlink_iops;
319		break;
320	default:
321		BUG();
322	}
323
324	unlock_new_inode(inode);
325}
326
327/**
328 *	kernfs_get_inode - get inode for kernfs_node
329 *	@sb: super block
330 *	@kn: kernfs_node to allocate inode for
331 *
332 *	Get inode for @kn.  If such inode doesn't exist, a new inode is
333 *	allocated and basics are initialized.  New inode is returned
334 *	locked.
335 *
336 *	LOCKING:
337 *	Kernel thread context (may sleep).
338 *
339 *	RETURNS:
340 *	Pointer to allocated inode on success, NULL on failure.
341 */
342struct inode *kernfs_get_inode(struct super_block *sb, struct kernfs_node *kn)
343{
344	struct inode *inode;
345
346	inode = iget_locked(sb, kn->ino);
347	if (inode && (inode->i_state & I_NEW))
348		kernfs_init_inode(kn, inode);
349
350	return inode;
351}
352
353/*
354 * The kernfs_node serves as both an inode and a directory entry for
355 * kernfs.  To prevent the kernfs inode numbers from being freed
356 * prematurely we take a reference to kernfs_node from the kernfs inode.  A
357 * super_operations.evict_inode() implementation is needed to drop that
358 * reference upon inode destruction.
359 */
360void kernfs_evict_inode(struct inode *inode)
361{
362	struct kernfs_node *kn = inode->i_private;
363
364	truncate_inode_pages_final(&inode->i_data);
365	clear_inode(inode);
366	kernfs_put(kn);
367}
368
369int kernfs_iop_permission(struct inode *inode, int mask)
 
370{
371	struct kernfs_node *kn;
 
 
372
373	if (mask & MAY_NOT_BLOCK)
374		return -ECHILD;
375
376	kn = inode->i_private;
 
377
378	mutex_lock(&kernfs_mutex);
379	kernfs_refresh_inode(kn, inode);
380	mutex_unlock(&kernfs_mutex);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
381
382	return generic_permission(inode, mask);
383}