xattr_acl.c 12.6 KB
Newer Older
1
#include <linux/capability.h>
L
Linus Torvalds 已提交
2 3
#include <linux/fs.h>
#include <linux/posix_acl.h>
4
#include "reiserfs.h"
L
Linus Torvalds 已提交
5 6 7
#include <linux/errno.h>
#include <linux/pagemap.h>
#include <linux/xattr.h>
8
#include <linux/slab.h>
9
#include <linux/posix_acl_xattr.h>
10
#include "xattr.h"
11
#include "acl.h"
L
Linus Torvalds 已提交
12 13
#include <asm/uaccess.h>

J
Jeff Mahoney 已提交
14 15
static int reiserfs_set_acl(struct reiserfs_transaction_handle *th,
			    struct inode *inode, int type,
16
			    struct posix_acl *acl);
L
Linus Torvalds 已提交
17 18

static int
19 20
posix_acl_set(struct dentry *dentry, const char *name, const void *value,
		size_t size, int flags, int type)
L
Linus Torvalds 已提交
21
{
22
	struct inode *inode = dentry->d_inode;
L
Linus Torvalds 已提交
23
	struct posix_acl *acl;
J
Jeff Mahoney 已提交
24 25 26
	int error, error2;
	struct reiserfs_transaction_handle th;
	size_t jcreate_blocks;
L
Linus Torvalds 已提交
27 28
	if (!reiserfs_posixacl(inode->i_sb))
		return -EOPNOTSUPP;
29
	if (!inode_owner_or_capable(inode))
L
Linus Torvalds 已提交
30 31 32
		return -EPERM;

	if (value) {
33
		acl = posix_acl_from_xattr(&init_user_ns, value, size);
L
Linus Torvalds 已提交
34 35 36 37 38 39 40 41 42 43
		if (IS_ERR(acl)) {
			return PTR_ERR(acl);
		} else if (acl) {
			error = posix_acl_valid(acl);
			if (error)
				goto release_and_out;
		}
	} else
		acl = NULL;

J
Jeff Mahoney 已提交
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
	/* Pessimism: We can't assume that anything from the xattr root up
	 * has been created. */

	jcreate_blocks = reiserfs_xattr_jcreate_nblocks(inode) +
			 reiserfs_xattr_nblocks(inode, size) * 2;

	reiserfs_write_lock(inode->i_sb);
	error = journal_begin(&th, inode->i_sb, jcreate_blocks);
	if (error == 0) {
		error = reiserfs_set_acl(&th, inode, type, acl);
		error2 = journal_end(&th, inode->i_sb, jcreate_blocks);
		if (error2)
			error = error2;
	}
	reiserfs_write_unlock(inode->i_sb);
L
Linus Torvalds 已提交
59

60
      release_and_out:
L
Linus Torvalds 已提交
61 62 63 64 65
	posix_acl_release(acl);
	return error;
}

static int
66 67
posix_acl_get(struct dentry *dentry, const char *name, void *buffer,
		size_t size, int type)
L
Linus Torvalds 已提交
68 69 70 71
{
	struct posix_acl *acl;
	int error;

72
	if (!reiserfs_posixacl(dentry->d_sb))
L
Linus Torvalds 已提交
73 74
		return -EOPNOTSUPP;

75
	acl = reiserfs_get_acl(dentry->d_inode, type);
L
Linus Torvalds 已提交
76 77 78 79
	if (IS_ERR(acl))
		return PTR_ERR(acl);
	if (acl == NULL)
		return -ENODATA;
80
	error = posix_acl_to_xattr(&init_user_ns, acl, buffer, size);
L
Linus Torvalds 已提交
81 82 83 84 85 86 87 88
	posix_acl_release(acl);

	return error;
}

/*
 * Convert from filesystem to in-memory representation.
 */
89
static struct posix_acl *posix_acl_from_disk(const void *value, size_t size)
L
Linus Torvalds 已提交
90 91 92 93 94 95 96 97
{
	const char *end = (char *)value + size;
	int n, count;
	struct posix_acl *acl;

	if (!value)
		return NULL;
	if (size < sizeof(reiserfs_acl_header))
98 99
		return ERR_PTR(-EINVAL);
	if (((reiserfs_acl_header *) value)->a_version !=
L
Linus Torvalds 已提交
100 101 102 103 104 105 106 107 108 109 110
	    cpu_to_le32(REISERFS_ACL_VERSION))
		return ERR_PTR(-EINVAL);
	value = (char *)value + sizeof(reiserfs_acl_header);
	count = reiserfs_acl_count(size);
	if (count < 0)
		return ERR_PTR(-EINVAL);
	if (count == 0)
		return NULL;
	acl = posix_acl_alloc(count, GFP_NOFS);
	if (!acl)
		return ERR_PTR(-ENOMEM);
111 112
	for (n = 0; n < count; n++) {
		reiserfs_acl_entry *entry = (reiserfs_acl_entry *) value;
L
Linus Torvalds 已提交
113 114
		if ((char *)value + sizeof(reiserfs_acl_entry_short) > end)
			goto fail;
115
		acl->a_entries[n].e_tag = le16_to_cpu(entry->e_tag);
L
Linus Torvalds 已提交
116
		acl->a_entries[n].e_perm = le16_to_cpu(entry->e_perm);
117 118 119 120 121 122 123 124 125 126
		switch (acl->a_entries[n].e_tag) {
		case ACL_USER_OBJ:
		case ACL_GROUP_OBJ:
		case ACL_MASK:
		case ACL_OTHER:
			value = (char *)value +
			    sizeof(reiserfs_acl_entry_short);
			break;

		case ACL_USER:
127 128 129 130 131 132 133
			value = (char *)value + sizeof(reiserfs_acl_entry);
			if ((char *)value > end)
				goto fail;
			acl->a_entries[n].e_uid = 
				make_kuid(&init_user_ns,
					  le32_to_cpu(entry->e_id));
			break;
134 135 136
		case ACL_GROUP:
			value = (char *)value + sizeof(reiserfs_acl_entry);
			if ((char *)value > end)
L
Linus Torvalds 已提交
137
				goto fail;
138 139 140
			acl->a_entries[n].e_gid =
				make_kgid(&init_user_ns,
					  le32_to_cpu(entry->e_id));
141 142 143 144
			break;

		default:
			goto fail;
L
Linus Torvalds 已提交
145 146 147 148 149 150
		}
	}
	if (value != end)
		goto fail;
	return acl;

151
      fail:
L
Linus Torvalds 已提交
152 153 154 155 156 157 158
	posix_acl_release(acl);
	return ERR_PTR(-EINVAL);
}

/*
 * Convert from in-memory to filesystem representation.
 */
159
static void *posix_acl_to_disk(const struct posix_acl *acl, size_t * size)
L
Linus Torvalds 已提交
160 161 162 163 164 165
{
	reiserfs_acl_header *ext_acl;
	char *e;
	int n;

	*size = reiserfs_acl_size(acl->a_count);
166
	ext_acl = kmalloc(sizeof(reiserfs_acl_header) +
167 168 169
						  acl->a_count *
						  sizeof(reiserfs_acl_entry),
						  GFP_NOFS);
L
Linus Torvalds 已提交
170 171 172 173
	if (!ext_acl)
		return ERR_PTR(-ENOMEM);
	ext_acl->a_version = cpu_to_le32(REISERFS_ACL_VERSION);
	e = (char *)ext_acl + sizeof(reiserfs_acl_header);
174
	for (n = 0; n < acl->a_count; n++) {
175
		const struct posix_acl_entry *acl_e = &acl->a_entries[n];
176 177
		reiserfs_acl_entry *entry = (reiserfs_acl_entry *) e;
		entry->e_tag = cpu_to_le16(acl->a_entries[n].e_tag);
L
Linus Torvalds 已提交
178
		entry->e_perm = cpu_to_le16(acl->a_entries[n].e_perm);
179 180
		switch (acl->a_entries[n].e_tag) {
		case ACL_USER:
181 182 183 184
			entry->e_id = cpu_to_le32(
				from_kuid(&init_user_ns, acl_e->e_uid));
			e += sizeof(reiserfs_acl_entry);
			break;
185
		case ACL_GROUP:
186 187
			entry->e_id = cpu_to_le32(
				from_kgid(&init_user_ns, acl_e->e_gid));
188 189 190 191 192 193 194 195 196 197 198 199
			e += sizeof(reiserfs_acl_entry);
			break;

		case ACL_USER_OBJ:
		case ACL_GROUP_OBJ:
		case ACL_MASK:
		case ACL_OTHER:
			e += sizeof(reiserfs_acl_entry_short);
			break;

		default:
			goto fail;
L
Linus Torvalds 已提交
200 201 202 203
		}
	}
	return (char *)ext_acl;

204
      fail:
L
Linus Torvalds 已提交
205 206 207 208 209 210 211
	kfree(ext_acl);
	return ERR_PTR(-EINVAL);
}

/*
 * Inode operation get_posix_acl().
 *
212
 * inode->i_mutex: down
L
Linus Torvalds 已提交
213 214
 * BKL held [before 2.5.x]
 */
215
struct posix_acl *reiserfs_get_acl(struct inode *inode, int type)
L
Linus Torvalds 已提交
216 217
{
	char *name, *value;
218
	struct posix_acl *acl;
219
	int size;
L
Linus Torvalds 已提交
220
	int retval;
221

222 223 224 225
	acl = get_cached_acl(inode, type);
	if (acl != ACL_NOT_CACHED)
		return acl;

226 227 228 229 230 231 232 233
	switch (type) {
	case ACL_TYPE_ACCESS:
		name = POSIX_ACL_XATTR_ACCESS;
		break;
	case ACL_TYPE_DEFAULT:
		name = POSIX_ACL_XATTR_DEFAULT;
		break;
	default:
234
		BUG();
235 236 237
	}

	size = reiserfs_xattr_get(inode, name, NULL, 0);
238
	if (size < 0) {
239
		if (size == -ENODATA || size == -ENOSYS) {
240
			set_cached_acl(inode, type, NULL);
241 242 243 244
			return NULL;
		}
		return ERR_PTR(size);
	}
L
Linus Torvalds 已提交
245

246 247 248
	value = kmalloc(size, GFP_NOFS);
	if (!value)
		return ERR_PTR(-ENOMEM);
L
Linus Torvalds 已提交
249 250 251 252 253 254

	retval = reiserfs_xattr_get(inode, name, value, size);
	if (retval == -ENODATA || retval == -ENOSYS) {
		/* This shouldn't actually happen as it should have
		   been caught above.. but just in case */
		acl = NULL;
255
	} else if (retval < 0) {
L
Linus Torvalds 已提交
256 257 258
		acl = ERR_PTR(retval);
	} else {
		acl = posix_acl_from_disk(value, retval);
259
	}
260 261
	if (!IS_ERR(acl))
		set_cached_acl(inode, type, acl);
L
Linus Torvalds 已提交
262 263 264 265 266 267 268 269

	kfree(value);
	return acl;
}

/*
 * Inode operation set_posix_acl().
 *
270
 * inode->i_mutex: down
L
Linus Torvalds 已提交
271 272 273
 * BKL held [before 2.5.x]
 */
static int
J
Jeff Mahoney 已提交
274 275
reiserfs_set_acl(struct reiserfs_transaction_handle *th, struct inode *inode,
		 int type, struct posix_acl *acl)
L
Linus Torvalds 已提交
276
{
277
	char *name;
L
Linus Torvalds 已提交
278
	void *value = NULL;
279
	size_t size = 0;
L
Linus Torvalds 已提交
280 281 282 283 284
	int error;

	if (S_ISLNK(inode->i_mode))
		return -EOPNOTSUPP;

285 286 287 288
	switch (type) {
	case ACL_TYPE_ACCESS:
		name = POSIX_ACL_XATTR_ACCESS;
		if (acl) {
289
			error = posix_acl_equiv_mode(acl, &inode->i_mode);
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
			if (error < 0)
				return error;
			else {
				if (error == 0)
					acl = NULL;
			}
		}
		break;
	case ACL_TYPE_DEFAULT:
		name = POSIX_ACL_XATTR_DEFAULT;
		if (!S_ISDIR(inode->i_mode))
			return acl ? -EACCES : 0;
		break;
	default:
		return -EINVAL;
	}

	if (acl) {
		value = posix_acl_to_disk(acl, &size);
		if (IS_ERR(value))
			return (int)PTR_ERR(value);
311 312
	}

J
Jeff Mahoney 已提交
313
	error = reiserfs_xattr_set_handle(th, inode, name, value, size, 0);
314 315 316 317 318 319 320 321 322 323 324

	/*
	 * Ensure that the inode gets dirtied if we're only using
	 * the mode bits and an old ACL didn't exist. We don't need
	 * to check if the inode is hashed here since we won't get
	 * called by reiserfs_inherit_default_acl().
	 */
	if (error == -ENODATA) {
		error = 0;
		if (type == ACL_TYPE_ACCESS) {
			inode->i_ctime = CURRENT_TIME_SEC;
325 326 327
			mark_inode_dirty(inode);
		}
	}
L
Linus Torvalds 已提交
328

329
	kfree(value);
L
Linus Torvalds 已提交
330

331
	if (!error)
332
		set_cached_acl(inode, type, acl);
L
Linus Torvalds 已提交
333 334 335 336

	return error;
}

337
/* dir->i_mutex: locked,
L
Linus Torvalds 已提交
338 339
 * inode is new and not released into the wild yet */
int
J
Jeff Mahoney 已提交
340 341
reiserfs_inherit_default_acl(struct reiserfs_transaction_handle *th,
			     struct inode *dir, struct dentry *dentry,
342
			     struct inode *inode)
L
Linus Torvalds 已提交
343
{
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
	struct posix_acl *acl;
	int err = 0;

	/* ACLs only get applied to files and directories */
	if (S_ISLNK(inode->i_mode))
		return 0;

	/* ACLs can only be used on "new" objects, so if it's an old object
	 * there is nothing to inherit from */
	if (get_inode_sd_version(dir) == STAT_DATA_V1)
		goto apply_umask;

	/* Don't apply ACLs to objects in the .reiserfs_priv tree.. This
	 * would be useless since permissions are ignored, and a pain because
	 * it introduces locking cycles */
359 360
	if (IS_PRIVATE(dir)) {
		inode->i_flags |= S_PRIVATE;
361 362 363 364
		goto apply_umask;
	}

	acl = reiserfs_get_acl(dir, ACL_TYPE_DEFAULT);
365
	if (IS_ERR(acl))
366 367 368 369 370
		return PTR_ERR(acl);

	if (acl) {
		/* Copy the default ACL to the default ACL of a new directory */
		if (S_ISDIR(inode->i_mode)) {
J
Jeff Mahoney 已提交
371 372
			err = reiserfs_set_acl(th, inode, ACL_TYPE_DEFAULT,
					       acl);
373 374 375 376 377 378
			if (err)
				goto cleanup;
		}

		/* Now we reconcile the new ACL and the mode,
		   potentially modifying both */
379
		err = posix_acl_create(&acl, GFP_NOFS, &inode->i_mode);
380 381
		if (err < 0)
			return err;
382

383 384 385
		/* If we need an ACL.. */
		if (err > 0)
			err = reiserfs_set_acl(th, inode, ACL_TYPE_ACCESS, acl);
386 387 388 389 390
	      cleanup:
		posix_acl_release(acl);
	} else {
	      apply_umask:
		/* no ACL, apply umask */
A
Al Viro 已提交
391
		inode->i_mode &= ~current_umask();
392 393 394
	}

	return err;
L
Linus Torvalds 已提交
395 396
}

J
Jeff Mahoney 已提交
397 398 399 400 401 402 403 404 405 406 407
/* This is used to cache the default acl before a new object is created.
 * The biggest reason for this is to get an idea of how many blocks will
 * actually be required for the create operation if we must inherit an ACL.
 * An ACL write can add up to 3 object creations and an additional file write
 * so we'd prefer not to reserve that many blocks in the journal if we can.
 * It also has the advantage of not loading the ACL with a transaction open,
 * this may seem silly, but if the owner of the directory is doing the
 * creation, the ACL may not be loaded since the permissions wouldn't require
 * it.
 * We return the number of blocks required for the transaction.
 */
408
int reiserfs_cache_default_acl(struct inode *inode)
L
Linus Torvalds 已提交
409
{
J
Jeff Mahoney 已提交
410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
	struct posix_acl *acl;
	int nblocks = 0;

	if (IS_PRIVATE(inode))
		return 0;

	acl = reiserfs_get_acl(inode, ACL_TYPE_DEFAULT);

	if (acl && !IS_ERR(acl)) {
		int size = reiserfs_acl_size(acl->a_count);

		/* Other xattrs can be created during inode creation. We don't
		 * want to claim too many blocks, so we check to see if we
		 * we need to create the tree to the xattrs, and then we
		 * just want two files. */
		nblocks = reiserfs_xattr_jcreate_nblocks(inode);
		nblocks += JOURNAL_BLOCKS_PER_OBJECT(inode->i_sb);

		REISERFS_I(inode)->i_flags |= i_has_xattr_dir;

		/* We need to account for writes + bitmaps for two files */
		nblocks += reiserfs_xattr_nblocks(inode, size) * 4;
		posix_acl_release(acl);
433 434
	}

J
Jeff Mahoney 已提交
435
	return nblocks;
L
Linus Torvalds 已提交
436 437
}

438
int reiserfs_acl_chmod(struct inode *inode)
L
Linus Torvalds 已提交
439
{
440 441 442 443
	struct reiserfs_transaction_handle th;
	struct posix_acl *acl;
	size_t size;
	int depth;
444
	int error;
L
Linus Torvalds 已提交
445

446 447 448
	if (IS_PRIVATE(inode))
		return 0;

449 450
	if (S_ISLNK(inode->i_mode))
		return -EOPNOTSUPP;
L
Linus Torvalds 已提交
451

452 453 454
	if (get_inode_sd_version(inode) == STAT_DATA_V1 ||
	    !reiserfs_posixacl(inode->i_sb)) {
		return 0;
L
Linus Torvalds 已提交
455 456
	}

457
	reiserfs_write_unlock(inode->i_sb);
458
	acl = reiserfs_get_acl(inode, ACL_TYPE_ACCESS);
459
	reiserfs_write_lock(inode->i_sb);
460 461 462 463
	if (!acl)
		return 0;
	if (IS_ERR(acl))
		return PTR_ERR(acl);
464 465 466 467 468 469 470
	error = posix_acl_chmod(&acl, GFP_NOFS, inode->i_mode);
	if (error)
		return error;

	size = reiserfs_xattr_nblocks(inode, reiserfs_acl_size(acl->a_count));
	depth = reiserfs_write_lock_once(inode->i_sb);
	error = journal_begin(&th, inode->i_sb, size * 2);
J
Jeff Mahoney 已提交
471
	if (!error) {
472 473 474 475 476
		int error2;
		error = reiserfs_set_acl(&th, inode, ACL_TYPE_ACCESS, acl);
		error2 = journal_end(&th, inode->i_sb, size * 2);
		if (error2)
			error = error2;
J
Jeff Mahoney 已提交
477
	}
478 479
	reiserfs_write_unlock_once(inode->i_sb, depth);
	posix_acl_release(acl);
480
	return error;
L
Linus Torvalds 已提交
481 482
}

483
static size_t posix_acl_access_list(struct dentry *dentry, char *list,
484
				    size_t list_size, const char *name,
485
				    size_t name_len, int type)
L
Linus Torvalds 已提交
486
{
487
	const size_t size = sizeof(POSIX_ACL_XATTR_ACCESS);
488
	if (!reiserfs_posixacl(dentry->d_sb))
489
		return 0;
490 491 492
	if (list && size <= list_size)
		memcpy(list, POSIX_ACL_XATTR_ACCESS, size);
	return size;
L
Linus Torvalds 已提交
493 494
}

495
const struct xattr_handler reiserfs_posix_acl_access_handler = {
496
	.prefix = POSIX_ACL_XATTR_ACCESS,
497 498 499
	.flags = ACL_TYPE_ACCESS,
	.get = posix_acl_get,
	.set = posix_acl_set,
L
Linus Torvalds 已提交
500 501 502
	.list = posix_acl_access_list,
};

503
static size_t posix_acl_default_list(struct dentry *dentry, char *list,
504
				     size_t list_size, const char *name,
505
				     size_t name_len, int type)
L
Linus Torvalds 已提交
506
{
507
	const size_t size = sizeof(POSIX_ACL_XATTR_DEFAULT);
508
	if (!reiserfs_posixacl(dentry->d_sb))
509
		return 0;
510 511 512
	if (list && size <= list_size)
		memcpy(list, POSIX_ACL_XATTR_DEFAULT, size);
	return size;
L
Linus Torvalds 已提交
513 514
}

515
const struct xattr_handler reiserfs_posix_acl_default_handler = {
516
	.prefix = POSIX_ACL_XATTR_DEFAULT,
517 518 519
	.flags = ACL_TYPE_DEFAULT,
	.get = posix_acl_get,
	.set = posix_acl_set,
L
Linus Torvalds 已提交
520 521
	.list = posix_acl_default_list,
};
新手
引导
客服 返回
顶部