inode.c 8.83 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
	iattrs->ia_uid = GLOBAL_ROOT_UID;
	iattrs->ia_gid = GLOBAL_ROOT_GID;
57 58 59 60

	ktime_get_real_ts(&iattrs->ia_atime);
	iattrs->ia_mtime = iattrs->ia_atime;
	iattrs->ia_ctime = iattrs->ia_atime;
61

62
	simple_xattrs_init(&kn->iattr->xattrs);
63 64 65 66
out_unlock:
	ret = kn->iattr;
	mutex_unlock(&iattr_mutex);
	return ret;
67 68
}

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

75
	attrs = kernfs_iattrs(kn);
76 77
	if (!attrs)
		return -ENOMEM;
78

79
	iattrs = &attrs->ia_iattr;
80 81 82 83 84 85 86 87 88 89 90 91 92

	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;
93
		iattrs->ia_mode = kn->mode = mode;
94 95 96 97 98 99
	}
	return 0;
}

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

109
	mutex_lock(&kernfs_mutex);
110
	ret = __kernfs_setattr(kn, iattr);
111
	mutex_unlock(&kernfs_mutex);
112 113 114
	return ret;
}

115
int kernfs_iop_setattr(struct dentry *dentry, struct iattr *iattr)
116
{
117
	struct inode *inode = d_inode(dentry);
118
	struct kernfs_node *kn = dentry->d_fsdata;
119 120
	int error;

121
	if (!kn)
122 123
		return -EINVAL;

124
	mutex_lock(&kernfs_mutex);
125 126 127 128
	error = inode_change_ok(inode, iattr);
	if (error)
		goto out;

129
	error = __kernfs_setattr(kn, iattr);
130 131 132 133 134 135 136
	if (error)
		goto out;

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

out:
137
	mutex_unlock(&kernfs_mutex);
138 139 140
	return error;
}

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

148
	attrs = kernfs_iattrs(kn);
149 150
	if (!attrs)
		return -ENOMEM;
151

152 153
	old_secdata = attrs->ia_secdata;
	old_secdata_len = attrs->ia_secdata_len;
154

155 156
	attrs->ia_secdata = *secdata;
	attrs->ia_secdata_len = *secdata_len;
157 158 159 160 161 162

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

163 164 165
int kernfs_iop_setxattr(struct dentry *unused, struct inode *inode,
			const char *name, const void *value,
			size_t size, int flags)
166
{
167
	struct kernfs_node *kn = inode->i_private;
168
	struct kernfs_iattrs *attrs;
169 170 171 172
	void *secdata;
	int error;
	u32 secdata_len = 0;

173
	attrs = kernfs_iattrs(kn);
174 175
	if (!attrs)
		return -ENOMEM;
176 177 178

	if (!strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN)) {
		const char *suffix = name + XATTR_SECURITY_PREFIX_LEN;
179
		error = security_inode_setsecurity(inode, suffix,
180 181
						value, size, flags);
		if (error)
182
			return error;
183
		error = security_inode_getsecctx(inode,
184 185
						&secdata, &secdata_len);
		if (error)
186
			return error;
187

188
		mutex_lock(&kernfs_mutex);
189
		error = kernfs_node_setsecdata(kn, &secdata, &secdata_len);
190
		mutex_unlock(&kernfs_mutex);
191 192 193

		if (secdata)
			security_release_secctx(secdata, secdata_len);
194 195 196 197 198 199 200 201 202
		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;
}

203
int kernfs_iop_removexattr(struct dentry *dentry, const char *name)
204
{
205
	struct kernfs_node *kn = dentry->d_fsdata;
206
	struct kernfs_iattrs *attrs;
207

208
	attrs = kernfs_iattrs(kn);
209 210 211
	if (!attrs)
		return -ENOMEM;

212
	return simple_xattr_set(&attrs->xattrs, name, NULL, 0, XATTR_REPLACE);
213 214
}

215 216
ssize_t kernfs_iop_getxattr(struct dentry *unused, struct inode *inode,
			    const char *name, void *buf, size_t size)
217
{
218
	struct kernfs_node *kn = inode->i_private;
219
	struct kernfs_iattrs *attrs;
220

221
	attrs = kernfs_iattrs(kn);
222 223 224 225 226 227
	if (!attrs)
		return -ENOMEM;

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

228
ssize_t kernfs_iop_listxattr(struct dentry *dentry, char *buf, size_t size)
229
{
230
	struct kernfs_node *kn = dentry->d_fsdata;
231
	struct kernfs_iattrs *attrs;
232

233
	attrs = kernfs_iattrs(kn);
234 235 236
	if (!attrs)
		return -ENOMEM;

237
	return simple_xattr_list(d_inode(dentry), &attrs->xattrs, buf, size);
238 239 240 241 242
}

static inline void set_default_inode_attr(struct inode *inode, umode_t mode)
{
	inode->i_mode = mode;
243
	inode->i_atime = inode->i_mtime =
244
		inode->i_ctime = current_time(inode);
245 246 247 248
}

static inline void set_inode_attr(struct inode *inode, struct iattr *iattr)
{
249
	struct super_block *sb = inode->i_sb;
250 251
	inode->i_uid = iattr->ia_uid;
	inode->i_gid = iattr->ia_gid;
252 253 254
	inode->i_atime = timespec_trunc(iattr->ia_atime, sb->s_time_gran);
	inode->i_mtime = timespec_trunc(iattr->ia_mtime, sb->s_time_gran);
	inode->i_ctime = timespec_trunc(iattr->ia_ctime, sb->s_time_gran);
255 256
}

257
static void kernfs_refresh_inode(struct kernfs_node *kn, struct inode *inode)
258
{
259
	struct kernfs_iattrs *attrs = kn->iattr;
260

261
	inode->i_mode = kn->mode;
262
	if (attrs) {
263 264 265
		/*
		 * kernfs_node has non-default attributes get them from
		 * persistent copy in kernfs_node.
266
		 */
267 268 269
		set_inode_attr(inode, &attrs->ia_iattr);
		security_inode_notifysecctx(inode, attrs->ia_secdata,
					    attrs->ia_secdata_len);
270 271
	}

272
	if (kernfs_type(kn) == KERNFS_DIR)
273
		set_nlink(inode, kn->dir.subdirs + 2);
274 275
}

276 277
int kernfs_iop_getattr(struct vfsmount *mnt, struct dentry *dentry,
		   struct kstat *stat)
278
{
279
	struct kernfs_node *kn = dentry->d_fsdata;
280
	struct inode *inode = d_inode(dentry);
281

282
	mutex_lock(&kernfs_mutex);
283
	kernfs_refresh_inode(kn, inode);
284
	mutex_unlock(&kernfs_mutex);
285 286 287 288 289

	generic_fillattr(inode, stat);
	return 0;
}

290
static void kernfs_init_inode(struct kernfs_node *kn, struct inode *inode)
291
{
292 293
	kernfs_get(kn);
	inode->i_private = kn;
294 295
	inode->i_mapping->a_ops = &kernfs_aops;
	inode->i_op = &kernfs_iops;
296

297
	set_default_inode_attr(inode, kn->mode);
298
	kernfs_refresh_inode(kn, inode);
299 300

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

	unlock_new_inode(inode);
}

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

341
	inode = iget_locked(sb, kn->ino);
342
	if (inode && (inode->i_state & I_NEW))
343
		kernfs_init_inode(kn, inode);
344 345 346 347 348

	return inode;
}

/*
349 350 351
 * 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
352 353 354
 * super_operations.evict_inode() implementation is needed to drop that
 * reference upon inode destruction.
 */
355
void kernfs_evict_inode(struct inode *inode)
356
{
357
	struct kernfs_node *kn = inode->i_private;
358

359
	truncate_inode_pages_final(&inode->i_data);
360
	clear_inode(inode);
361
	kernfs_put(kn);
362 363
}

364
int kernfs_iop_permission(struct inode *inode, int mask)
365
{
366
	struct kernfs_node *kn;
367 368 369 370

	if (mask & MAY_NOT_BLOCK)
		return -ECHILD;

371
	kn = inode->i_private;
372

373
	mutex_lock(&kernfs_mutex);
374
	kernfs_refresh_inode(kn, inode);
375
	mutex_unlock(&kernfs_mutex);
376 377 378

	return generic_permission(inode, mask);
}