inode.c 8.64 KB
Newer Older
1 2 3 4 5 6 7 8 9
/*
 * fs/kernfs/inode.c - kernfs inode implementation
 *
 * Copyright (c) 2001-3 Patrick Mochel
 * Copyright (c) 2007 SUSE Linux Products GmbH
 * Copyright (c) 2007, 2013 Tejun Heo <tj@kernel.org>
 *
 * This file is released under the GPLv2.
 */
10 11 12 13 14 15 16 17 18 19 20

#include <linux/pagemap.h>
#include <linux/backing-dev.h>
#include <linux/capability.h>
#include <linux/errno.h>
#include <linux/slab.h>
#include <linux/xattr.h>
#include <linux/security.h>

#include "kernfs-internal.h"

21
static const struct address_space_operations kernfs_aops = {
22 23 24 25 26
	.readpage	= simple_readpage,
	.write_begin	= simple_write_begin,
	.write_end	= simple_write_end,
};

27
static const struct inode_operations kernfs_iops = {
28 29 30 31 32 33 34
	.permission	= kernfs_iop_permission,
	.setattr	= kernfs_iop_setattr,
	.getattr	= kernfs_iop_getattr,
	.setxattr	= kernfs_iop_setxattr,
	.removexattr	= kernfs_iop_removexattr,
	.getxattr	= kernfs_iop_getxattr,
	.listxattr	= kernfs_iop_listxattr,
35 36
};

37
static struct kernfs_iattrs *kernfs_iattrs(struct kernfs_node *kn)
38
{
39 40
	static DEFINE_MUTEX(iattr_mutex);
	struct kernfs_iattrs *ret;
41 42
	struct iattr *iattrs;

43 44
	mutex_lock(&iattr_mutex);

45
	if (kn->iattr)
46
		goto out_unlock;
47

48
	kn->iattr = kzalloc(sizeof(struct kernfs_iattrs), GFP_KERNEL);
49
	if (!kn->iattr)
50
		goto out_unlock;
51
	iattrs = &kn->iattr->ia_iattr;
52 53

	/* assign default attributes */
54
	iattrs->ia_mode = kn->mode;
55 56 57 58
	iattrs->ia_uid = GLOBAL_ROOT_UID;
	iattrs->ia_gid = GLOBAL_ROOT_GID;
	iattrs->ia_atime = iattrs->ia_mtime = iattrs->ia_ctime = CURRENT_TIME;

59
	simple_xattrs_init(&kn->iattr->xattrs);
60 61 62 63
out_unlock:
	ret = kn->iattr;
	mutex_unlock(&iattr_mutex);
	return ret;
64 65
}

66
static int __kernfs_setattr(struct kernfs_node *kn, const struct iattr *iattr)
67
{
68
	struct kernfs_iattrs *attrs;
69 70 71
	struct iattr *iattrs;
	unsigned int ia_valid = iattr->ia_valid;

72
	attrs = kernfs_iattrs(kn);
73 74
	if (!attrs)
		return -ENOMEM;
75

76
	iattrs = &attrs->ia_iattr;
77 78 79 80 81 82 83 84 85 86 87 88 89

	if (ia_valid & ATTR_UID)
		iattrs->ia_uid = iattr->ia_uid;
	if (ia_valid & ATTR_GID)
		iattrs->ia_gid = iattr->ia_gid;
	if (ia_valid & ATTR_ATIME)
		iattrs->ia_atime = iattr->ia_atime;
	if (ia_valid & ATTR_MTIME)
		iattrs->ia_mtime = iattr->ia_mtime;
	if (ia_valid & ATTR_CTIME)
		iattrs->ia_ctime = iattr->ia_ctime;
	if (ia_valid & ATTR_MODE) {
		umode_t mode = iattr->ia_mode;
90
		iattrs->ia_mode = kn->mode = mode;
91 92 93 94 95 96
	}
	return 0;
}

/**
 * kernfs_setattr - set iattr on a node
97
 * @kn: target node
98 99 100 101
 * @iattr: iattr to set
 *
 * Returns 0 on success, -errno on failure.
 */
102
int kernfs_setattr(struct kernfs_node *kn, const struct iattr *iattr)
103 104 105
{
	int ret;

106
	mutex_lock(&kernfs_mutex);
107
	ret = __kernfs_setattr(kn, iattr);
108
	mutex_unlock(&kernfs_mutex);
109 110 111
	return ret;
}

112
int kernfs_iop_setattr(struct dentry *dentry, struct iattr *iattr)
113
{
114
	struct inode *inode = d_inode(dentry);
115
	struct kernfs_node *kn = dentry->d_fsdata;
116 117
	int error;

118
	if (!kn)
119 120
		return -EINVAL;

121
	mutex_lock(&kernfs_mutex);
122 123 124 125
	error = inode_change_ok(inode, iattr);
	if (error)
		goto out;

126
	error = __kernfs_setattr(kn, iattr);
127 128 129 130 131 132 133
	if (error)
		goto out;

	/* this ignores size changes */
	setattr_copy(inode, iattr);

out:
134
	mutex_unlock(&kernfs_mutex);
135 136 137
	return error;
}

138 139
static int kernfs_node_setsecdata(struct kernfs_node *kn, void **secdata,
				  u32 *secdata_len)
140
{
141
	struct kernfs_iattrs *attrs;
142 143 144
	void *old_secdata;
	size_t old_secdata_len;

145
	attrs = kernfs_iattrs(kn);
146 147
	if (!attrs)
		return -ENOMEM;
148

149 150
	old_secdata = attrs->ia_secdata;
	old_secdata_len = attrs->ia_secdata_len;
151

152 153
	attrs->ia_secdata = *secdata;
	attrs->ia_secdata_len = *secdata_len;
154 155 156 157 158 159

	*secdata = old_secdata;
	*secdata_len = old_secdata_len;
	return 0;
}

160 161
int kernfs_iop_setxattr(struct dentry *dentry, const char *name,
			const void *value, size_t size, int flags)
162
{
163
	struct kernfs_node *kn = dentry->d_fsdata;
164
	struct kernfs_iattrs *attrs;
165 166 167 168
	void *secdata;
	int error;
	u32 secdata_len = 0;

169
	attrs = kernfs_iattrs(kn);
170 171
	if (!attrs)
		return -ENOMEM;
172 173 174

	if (!strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN)) {
		const char *suffix = name + XATTR_SECURITY_PREFIX_LEN;
175
		error = security_inode_setsecurity(d_inode(dentry), suffix,
176 177
						value, size, flags);
		if (error)
178
			return error;
179
		error = security_inode_getsecctx(d_inode(dentry),
180 181
						&secdata, &secdata_len);
		if (error)
182
			return error;
183

184
		mutex_lock(&kernfs_mutex);
185
		error = kernfs_node_setsecdata(kn, &secdata, &secdata_len);
186
		mutex_unlock(&kernfs_mutex);
187 188 189

		if (secdata)
			security_release_secctx(secdata, secdata_len);
190 191 192 193 194 195 196 197 198
		return error;
	} else if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN)) {
		return simple_xattr_set(&attrs->xattrs, name, value, size,
					flags);
	}

	return -EINVAL;
}

199
int kernfs_iop_removexattr(struct dentry *dentry, const char *name)
200
{
201
	struct kernfs_node *kn = dentry->d_fsdata;
202
	struct kernfs_iattrs *attrs;
203

204
	attrs = kernfs_iattrs(kn);
205 206 207
	if (!attrs)
		return -ENOMEM;

208
	return simple_xattr_set(&attrs->xattrs, name, NULL, 0, XATTR_REPLACE);
209 210
}

211 212
ssize_t kernfs_iop_getxattr(struct dentry *unused, struct inode *inode,
			    const char *name, void *buf, size_t size)
213
{
214
	struct kernfs_node *kn = inode->i_private;
215
	struct kernfs_iattrs *attrs;
216

217
	attrs = kernfs_iattrs(kn);
218 219 220 221 222 223
	if (!attrs)
		return -ENOMEM;

	return simple_xattr_get(&attrs->xattrs, name, buf, size);
}

224
ssize_t kernfs_iop_listxattr(struct dentry *dentry, char *buf, size_t size)
225
{
226
	struct kernfs_node *kn = dentry->d_fsdata;
227
	struct kernfs_iattrs *attrs;
228

229
	attrs = kernfs_iattrs(kn);
230 231 232
	if (!attrs)
		return -ENOMEM;

233
	return simple_xattr_list(d_inode(dentry), &attrs->xattrs, buf, size);
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
}

static inline void set_default_inode_attr(struct inode *inode, umode_t mode)
{
	inode->i_mode = mode;
	inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
}

static inline void set_inode_attr(struct inode *inode, struct iattr *iattr)
{
	inode->i_uid = iattr->ia_uid;
	inode->i_gid = iattr->ia_gid;
	inode->i_atime = iattr->ia_atime;
	inode->i_mtime = iattr->ia_mtime;
	inode->i_ctime = iattr->ia_ctime;
}

251
static void kernfs_refresh_inode(struct kernfs_node *kn, struct inode *inode)
252
{
253
	struct kernfs_iattrs *attrs = kn->iattr;
254

255
	inode->i_mode = kn->mode;
256
	if (attrs) {
257 258 259
		/*
		 * kernfs_node has non-default attributes get them from
		 * persistent copy in kernfs_node.
260
		 */
261 262 263
		set_inode_attr(inode, &attrs->ia_iattr);
		security_inode_notifysecctx(inode, attrs->ia_secdata,
					    attrs->ia_secdata_len);
264 265
	}

266
	if (kernfs_type(kn) == KERNFS_DIR)
267
		set_nlink(inode, kn->dir.subdirs + 2);
268 269
}

270 271
int kernfs_iop_getattr(struct vfsmount *mnt, struct dentry *dentry,
		   struct kstat *stat)
272
{
273
	struct kernfs_node *kn = dentry->d_fsdata;
274
	struct inode *inode = d_inode(dentry);
275

276
	mutex_lock(&kernfs_mutex);
277
	kernfs_refresh_inode(kn, inode);
278
	mutex_unlock(&kernfs_mutex);
279 280 281 282 283

	generic_fillattr(inode, stat);
	return 0;
}

284
static void kernfs_init_inode(struct kernfs_node *kn, struct inode *inode)
285
{
286 287
	kernfs_get(kn);
	inode->i_private = kn;
288 289
	inode->i_mapping->a_ops = &kernfs_aops;
	inode->i_op = &kernfs_iops;
290

291
	set_default_inode_attr(inode, kn->mode);
292
	kernfs_refresh_inode(kn, inode);
293 294

	/* initialize inode according to type */
295 296
	switch (kernfs_type(kn)) {
	case KERNFS_DIR:
297 298
		inode->i_op = &kernfs_dir_iops;
		inode->i_fop = &kernfs_dir_fops;
299 300
		if (kn->flags & KERNFS_EMPTY_DIR)
			make_empty_dir_inode(inode);
301
		break;
302
	case KERNFS_FILE:
303
		inode->i_size = kn->attr.size;
304
		inode->i_fop = &kernfs_file_fops;
305
		break;
306
	case KERNFS_LINK:
307
		inode->i_op = &kernfs_symlink_iops;
308 309 310 311 312 313 314 315 316
		break;
	default:
		BUG();
	}

	unlock_new_inode(inode);
}

/**
317
 *	kernfs_get_inode - get inode for kernfs_node
318
 *	@sb: super block
319
 *	@kn: kernfs_node to allocate inode for
320
 *
321 322 323
 *	Get inode for @kn.  If such inode doesn't exist, a new inode is
 *	allocated and basics are initialized.  New inode is returned
 *	locked.
324 325 326 327 328 329 330
 *
 *	LOCKING:
 *	Kernel thread context (may sleep).
 *
 *	RETURNS:
 *	Pointer to allocated inode on success, NULL on failure.
 */
331
struct inode *kernfs_get_inode(struct super_block *sb, struct kernfs_node *kn)
332 333 334
{
	struct inode *inode;

335
	inode = iget_locked(sb, kn->ino);
336
	if (inode && (inode->i_state & I_NEW))
337
		kernfs_init_inode(kn, inode);
338 339 340 341 342

	return inode;
}

/*
343 344 345
 * The kernfs_node serves as both an inode and a directory entry for
 * kernfs.  To prevent the kernfs inode numbers from being freed
 * prematurely we take a reference to kernfs_node from the kernfs inode.  A
346 347 348
 * super_operations.evict_inode() implementation is needed to drop that
 * reference upon inode destruction.
 */
349
void kernfs_evict_inode(struct inode *inode)
350
{
351
	struct kernfs_node *kn = inode->i_private;
352

353
	truncate_inode_pages_final(&inode->i_data);
354
	clear_inode(inode);
355
	kernfs_put(kn);
356 357
}

358
int kernfs_iop_permission(struct inode *inode, int mask)
359
{
360
	struct kernfs_node *kn;
361 362 363 364

	if (mask & MAY_NOT_BLOCK)
		return -ECHILD;

365
	kn = inode->i_private;
366

367
	mutex_lock(&kernfs_mutex);
368
	kernfs_refresh_inode(kn, inode);
369
	mutex_unlock(&kernfs_mutex);
370 371 372

	return generic_permission(inode, mask);
}