xattr.c 26.5 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
/*
 * linux/fs/reiserfs/xattr.c
 *
 * Copyright (c) 2002 by Jeff Mahoney, <jeffm@suse.com>
 *
 */

/*
 * In order to implement EA/ACLs in a clean, backwards compatible manner,
 * they are implemented as files in a "private" directory.
 * Each EA is in it's own file, with the directory layout like so (/ is assumed
 * to be relative to fs root). Inside the /.reiserfs_priv/xattrs directory,
 * directories named using the capital-hex form of the objectid and
 * generation number are used. Inside each directory are individual files
 * named with the name of the extended attribute.
 *
 * So, for objectid 12648430, we could have:
 * /.reiserfs_priv/xattrs/C0FFEE.0/system.posix_acl_access
 * /.reiserfs_priv/xattrs/C0FFEE.0/system.posix_acl_default
 * /.reiserfs_priv/xattrs/C0FFEE.0/user.Content-Type
 * .. or similar.
 *
 * The file contents are the text of the EA. The size is known based on the
 * stat data describing the file.
 *
 * In the case of system.posix_acl_access and system.posix_acl_default, since
 * these are special cases for filesystem ACLs, they are interpreted by the
 * kernel, in addition, they are negatively and positively cached and attached
 * to the inode so that unnecessary lookups are avoided.
30 31
 *
 * Locking works like so:
32 33
 * Directory components (xattr root, xattr dir) are protectd by their i_mutex.
 * The xattrs themselves are protected by the xattr_sem.
L
Linus Torvalds 已提交
34 35 36
 */

#include <linux/reiserfs_fs.h>
37
#include <linux/capability.h>
L
Linus Torvalds 已提交
38 39 40
#include <linux/dcache.h>
#include <linux/namei.h>
#include <linux/errno.h>
41
#include <linux/gfp.h>
L
Linus Torvalds 已提交
42 43 44 45 46 47 48
#include <linux/fs.h>
#include <linux/file.h>
#include <linux/pagemap.h>
#include <linux/xattr.h>
#include <linux/reiserfs_xattr.h>
#include <linux/reiserfs_acl.h>
#include <asm/uaccess.h>
49
#include <net/checksum.h>
L
Linus Torvalds 已提交
50
#include <linux/stat.h>
51
#include <linux/quotaops.h>
52
#include <linux/security.h>
L
Linus Torvalds 已提交
53 54 55 56 57

#define PRIVROOT_NAME ".reiserfs_priv"
#define XAROOT_NAME   "xattrs"


58 59 60
/* Helpers for inode ops. We do this so that we don't have all the VFS
 * overhead and also for proper i_mutex annotation.
 * dir->i_mutex must be held for all of them. */
61
#ifdef CONFIG_REISERFS_FS_XATTR
62
static int xattr_create(struct inode *dir, struct dentry *dentry, int mode)
L
Linus Torvalds 已提交
63
{
64 65 66
	BUG_ON(!mutex_is_locked(&dir->i_mutex));
	return dir->i_op->create(dir, dentry, mode, NULL);
}
67
#endif
68

69 70 71 72 73
static int xattr_mkdir(struct inode *dir, struct dentry *dentry, int mode)
{
	BUG_ON(!mutex_is_locked(&dir->i_mutex));
	return dir->i_op->mkdir(dir, dentry, mode);
}
74

75 76 77 78 79 80 81 82
/* We use I_MUTEX_CHILD here to silence lockdep. It's safe because xattr
 * mutation ops aren't called during rename or splace, which are the
 * only other users of I_MUTEX_CHILD. It violates the ordering, but that's
 * better than allocating another subclass just for this code. */
static int xattr_unlink(struct inode *dir, struct dentry *dentry)
{
	int error;
	BUG_ON(!mutex_is_locked(&dir->i_mutex));
83

84 85
	reiserfs_mutex_lock_nested_safe(&dentry->d_inode->i_mutex,
					I_MUTEX_CHILD, dir->i_sb);
86 87 88 89 90 91 92 93 94 95 96 97 98
	error = dir->i_op->unlink(dir, dentry);
	mutex_unlock(&dentry->d_inode->i_mutex);

	if (!error)
		d_delete(dentry);
	return error;
}

static int xattr_rmdir(struct inode *dir, struct dentry *dentry)
{
	int error;
	BUG_ON(!mutex_is_locked(&dir->i_mutex));

99 100
	reiserfs_mutex_lock_nested_safe(&dentry->d_inode->i_mutex,
					I_MUTEX_CHILD, dir->i_sb);
101 102 103 104 105 106 107 108 109 110 111 112 113 114
	dentry_unhash(dentry);
	error = dir->i_op->rmdir(dir, dentry);
	if (!error)
		dentry->d_inode->i_flags |= S_DEAD;
	mutex_unlock(&dentry->d_inode->i_mutex);
	if (!error)
		d_delete(dentry);
	dput(dentry);

	return error;
}

#define xattr_may_create(flags)	(!flags || flags & XATTR_CREATE)

115
static struct dentry *open_xa_root(struct super_block *sb, int flags)
116
{
117 118 119 120
	struct dentry *privroot = REISERFS_SB(sb)->priv_root;
	struct dentry *xaroot;
	if (!privroot->d_inode)
		return ERR_PTR(-ENODATA);
121

122
	mutex_lock_nested(&privroot->d_inode->i_mutex, I_MUTEX_XATTR);
123

124
	xaroot = dget(REISERFS_SB(sb)->xattr_root);
125 126 127
	if (!xaroot)
		xaroot = ERR_PTR(-ENODATA);
	else if (!xaroot->d_inode) {
128
		int err = -ENODATA;
129
		if (xattr_may_create(flags))
130
			err = xattr_mkdir(privroot->d_inode, xaroot, 0700);
131
		if (err) {
132 133
			dput(xaroot);
			xaroot = ERR_PTR(err);
134
		}
135
	}
136

137 138
	mutex_unlock(&privroot->d_inode->i_mutex);
	return xaroot;
L
Linus Torvalds 已提交
139 140
}

141
static struct dentry *open_xa_dir(const struct inode *inode, int flags)
L
Linus Torvalds 已提交
142
{
143 144 145
	struct dentry *xaroot, *xadir;
	char namebuf[17];

146
	xaroot = open_xa_root(inode->i_sb, flags);
147
	if (IS_ERR(xaroot))
148 149 150 151 152 153
		return xaroot;

	snprintf(namebuf, sizeof(namebuf), "%X.%X",
		 le32_to_cpu(INODE_PKEY(inode)->k_objectid),
		 inode->i_generation);

154 155 156 157 158 159 160 161 162 163 164 165 166 167
	mutex_lock_nested(&xaroot->d_inode->i_mutex, I_MUTEX_XATTR);

	xadir = lookup_one_len(namebuf, xaroot, strlen(namebuf));
	if (!IS_ERR(xadir) && !xadir->d_inode) {
		int err = -ENODATA;
		if (xattr_may_create(flags))
			err = xattr_mkdir(xaroot->d_inode, xadir, 0700);
		if (err) {
			dput(xadir);
			xadir = ERR_PTR(err);
		}
	}

	mutex_unlock(&xaroot->d_inode->i_mutex);
168 169
	dput(xaroot);
	return xadir;
L
Linus Torvalds 已提交
170 171
}

172 173 174
/* The following are side effects of other operations that aren't explicitly
 * modifying extended attributes. This includes operations such as permissions
 * or ownership changes, object deletions, etc. */
175 176 177 178 179
struct reiserfs_dentry_buf {
	struct dentry *xadir;
	int count;
	struct dentry *dentries[8];
};
180

181
static int
182 183
fill_with_dentries(void *buf, const char *name, int namelen, loff_t offset,
		    u64 ino, unsigned int d_type)
184
{
185
	struct reiserfs_dentry_buf *dbuf = buf;
186
	struct dentry *dentry;
187
	WARN_ON_ONCE(!mutex_is_locked(&dbuf->xadir->d_inode->i_mutex));
188

189 190
	if (dbuf->count == ARRAY_SIZE(dbuf->dentries))
		return -ENOSPC;
191

192 193 194
	if (name[0] == '.' && (name[1] == '\0' ||
			       (name[1] == '.' && name[2] == '\0')))
		return 0;
195

196
	dentry = lookup_one_len(name, dbuf->xadir, namelen);
197
	if (IS_ERR(dentry)) {
198
		return PTR_ERR(dentry);
199
	} else if (!dentry->d_inode) {
200 201 202 203 204 205 206
		/* A directory entry exists, but no file? */
		reiserfs_error(dentry->d_sb, "xattr-20003",
			       "Corrupted directory: xattr %s listed but "
			       "not found for file %s.\n",
			       dentry->d_name.name, dbuf->xadir->d_name.name);
		dput(dentry);
		return -EIO;
207
	}
L
Linus Torvalds 已提交
208

209 210
	dbuf->dentries[dbuf->count++] = dentry;
	return 0;
L
Linus Torvalds 已提交
211 212
}

213 214
static void
cleanup_dentry_buf(struct reiserfs_dentry_buf *buf)
L
Linus Torvalds 已提交
215
{
216 217 218 219
	int i;
	for (i = 0; i < buf->count; i++)
		if (buf->dentries[i])
			dput(buf->dentries[i]);
220 221
}

222 223 224
static int reiserfs_for_each_xattr(struct inode *inode,
				   int (*action)(struct dentry *, void *),
				   void *data)
225
{
226 227 228 229 230 231
	struct dentry *dir;
	int i, err = 0;
	loff_t pos = 0;
	struct reiserfs_dentry_buf buf = {
		.count = 0,
	};
L
Linus Torvalds 已提交
232

233 234 235
	/* Skip out, an xattr has no xattrs associated with it */
	if (IS_PRIVATE(inode) || get_inode_sd_version(inode) == STAT_DATA_V1)
		return 0;
L
Linus Torvalds 已提交
236

237
	reiserfs_write_unlock(inode->i_sb);
238
	dir = open_xa_dir(inode, XATTR_REPLACE);
239 240
	if (IS_ERR(dir)) {
		err = PTR_ERR(dir);
241
		reiserfs_write_lock(inode->i_sb);
242 243
		goto out;
	} else if (!dir->d_inode) {
244
		err = 0;
245
		reiserfs_write_lock(inode->i_sb);
246
		goto out_dir;
247
	}
L
Linus Torvalds 已提交
248

249
	mutex_lock_nested(&dir->d_inode->i_mutex, I_MUTEX_XATTR);
250 251 252

	reiserfs_write_lock(inode->i_sb);

253 254 255 256
	buf.xadir = dir;
	err = reiserfs_readdir_dentry(dir, &buf, fill_with_dentries, &pos);
	while ((err == 0 || err == -ENOSPC) && buf.count) {
		err = 0;
L
Linus Torvalds 已提交
257

258 259 260
		for (i = 0; i < buf.count && buf.dentries[i]; i++) {
			int lerr = 0;
			struct dentry *dentry = buf.dentries[i];
L
Linus Torvalds 已提交
261

262 263
			if (err == 0 && !S_ISDIR(dentry->d_inode->i_mode))
				lerr = action(dentry, data);
L
Linus Torvalds 已提交
264

265 266 267
			dput(dentry);
			buf.dentries[i] = NULL;
			err = lerr ?: err;
268
		}
269 270 271 272
		buf.count = 0;
		if (!err)
			err = reiserfs_readdir_dentry(dir, &buf,
						      fill_with_dentries, &pos);
273
	}
274
	mutex_unlock(&dir->d_inode->i_mutex);
L
Linus Torvalds 已提交
275

276 277
	/* Clean up after a failed readdir */
	cleanup_dentry_buf(&buf);
L
Linus Torvalds 已提交
278

279
	if (!err) {
280 281 282 283 284 285 286 287 288 289 290
		/* We start a transaction here to avoid a ABBA situation
		 * between the xattr root's i_mutex and the journal lock.
		 * This doesn't incur much additional overhead since the
		 * new transaction will just nest inside the
		 * outer transaction. */
		int blocks = JOURNAL_PER_BALANCE_CNT * 2 + 2 +
			     4 * REISERFS_QUOTA_TRANS_BLOCKS(inode->i_sb);
		struct reiserfs_transaction_handle th;
		err = journal_begin(&th, inode->i_sb, blocks);
		if (!err) {
			int jerror;
291 292 293
			reiserfs_mutex_lock_nested_safe(
					  &dir->d_parent->d_inode->i_mutex,
					  I_MUTEX_XATTR, inode->i_sb);
294 295 296 297 298
			err = action(dir, data);
			jerror = journal_end(&th, inode->i_sb, blocks);
			mutex_unlock(&dir->d_parent->d_inode->i_mutex);
			err = jerror ?: err;
		}
299
	}
300 301
out_dir:
	dput(dir);
302
out:
303 304 305
	/* -ENODATA isn't an error */
	if (err == -ENODATA)
		err = 0;
306 307
	return err;
}
L
Linus Torvalds 已提交
308

309
static int delete_one_xattr(struct dentry *dentry, void *data)
310
{
311
	struct inode *dir = dentry->d_parent->d_inode;
L
Linus Torvalds 已提交
312

313 314 315
	/* This is the xattr dir, handle specially. */
	if (S_ISDIR(dentry->d_inode->i_mode))
		return xattr_rmdir(dir, dentry);
L
Linus Torvalds 已提交
316

317 318
	return xattr_unlink(dir, dentry);
}
319

320 321 322 323 324
static int chown_one_xattr(struct dentry *dentry, void *data)
{
	struct iattr *attrs = data;
	return reiserfs_setattr(dentry, attrs);
}
L
Linus Torvalds 已提交
325

326 327 328 329 330 331 332
/* No i_mutex, but the inode is unconnected. */
int reiserfs_delete_xattrs(struct inode *inode)
{
	int err = reiserfs_for_each_xattr(inode, delete_one_xattr, NULL);
	if (err)
		reiserfs_warning(inode->i_sb, "jdm-20004",
				 "Couldn't delete all xattrs (%d)\n", err);
333 334
	return err;
}
L
Linus Torvalds 已提交
335

336
/* inode->i_mutex: down */
337 338
int reiserfs_chown_xattrs(struct inode *inode, struct iattr *attrs)
{
339
	int err = reiserfs_for_each_xattr(inode, chown_one_xattr, attrs);
340 341 342
	if (err)
		reiserfs_warning(inode->i_sb, "jdm-20007",
				 "Couldn't chown all xattrs (%d)\n", err);
343
	return err;
L
Linus Torvalds 已提交
344 345
}

346 347 348 349
#ifdef CONFIG_REISERFS_FS_XATTR
/* Returns a dentry corresponding to a specific extended attribute file
 * for the inode. If flags allow, the file is created. Otherwise, a
 * valid or negative dentry, or an error is returned. */
350 351
static struct dentry *xattr_lookup(struct inode *inode, const char *name,
				    int flags)
L
Linus Torvalds 已提交
352
{
353 354 355 356
	struct dentry *xadir, *xafile;
	int err = 0;

	xadir = open_xa_dir(inode, flags);
357
	if (IS_ERR(xadir))
358 359
		return ERR_CAST(xadir);

360
	mutex_lock_nested(&xadir->d_inode->i_mutex, I_MUTEX_XATTR);
361 362
	xafile = lookup_one_len(name, xadir, strlen(name));
	if (IS_ERR(xafile)) {
363 364
		err = PTR_ERR(xafile);
		goto out;
365
	}
366

367 368
	if (xafile->d_inode && (flags & XATTR_CREATE))
		err = -EEXIST;
369

370 371
	if (!xafile->d_inode) {
		err = -ENODATA;
372
		if (xattr_may_create(flags))
373 374
			err = xattr_create(xadir->d_inode, xafile,
					      0700|S_IFREG);
375 376
	}

377 378
	if (err)
		dput(xafile);
379
out:
380
	mutex_unlock(&xadir->d_inode->i_mutex);
381 382
	dput(xadir);
	if (err)
383
		return ERR_PTR(err);
384
	return xafile;
L
Linus Torvalds 已提交
385 386 387
}

/* Internal operations on file data */
388
static inline void reiserfs_put_page(struct page *page)
L
Linus Torvalds 已提交
389
{
390 391
	kunmap(page);
	page_cache_release(page);
L
Linus Torvalds 已提交
392 393
}

394
static struct page *reiserfs_get_page(struct inode *dir, size_t n)
L
Linus Torvalds 已提交
395
{
396 397 398 399
	struct address_space *mapping = dir->i_mapping;
	struct page *page;
	/* We can deadlock if we try to free dentries,
	   and an unlink/rmdir has just occured - GFP_NOFS avoids this */
400
	mapping_set_gfp_mask(mapping, GFP_NOFS);
401
	page = read_mapping_page(mapping, n >> PAGE_CACHE_SHIFT, NULL);
402 403 404 405 406 407 408 409 410 411
	if (!IS_ERR(page)) {
		kmap(page);
		if (PageError(page))
			goto fail;
	}
	return page;

      fail:
	reiserfs_put_page(page);
	return ERR_PTR(-EIO);
L
Linus Torvalds 已提交
412 413
}

414
static inline __u32 xattr_hash(const char *msg, int len)
L
Linus Torvalds 已提交
415
{
416
	return csum_partial(msg, len, 0);
L
Linus Torvalds 已提交
417 418
}

V
Vladimir Saveliev 已提交
419 420 421
int reiserfs_commit_write(struct file *f, struct page *page,
			  unsigned from, unsigned to);

422 423 424
static void update_ctime(struct inode *inode)
{
	struct timespec now = current_fs_time(inode->i_sb);
A
Al Viro 已提交
425
	if (inode_unhashed(inode) || !inode->i_nlink ||
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
	    timespec_equal(&inode->i_ctime, &now))
		return;

	inode->i_ctime = CURRENT_TIME_SEC;
	mark_inode_dirty(inode);
}

static int lookup_and_delete_xattr(struct inode *inode, const char *name)
{
	int err = 0;
	struct dentry *dentry, *xadir;

	xadir = open_xa_dir(inode, XATTR_REPLACE);
	if (IS_ERR(xadir))
		return PTR_ERR(xadir);

442
	mutex_lock_nested(&xadir->d_inode->i_mutex, I_MUTEX_XATTR);
443 444 445 446 447 448 449
	dentry = lookup_one_len(name, xadir, strlen(name));
	if (IS_ERR(dentry)) {
		err = PTR_ERR(dentry);
		goto out_dput;
	}

	if (dentry->d_inode) {
450
		reiserfs_write_lock(inode->i_sb);
451
		err = xattr_unlink(xadir->d_inode, dentry);
452
		reiserfs_write_unlock(inode->i_sb);
453 454 455 456 457
		update_ctime(inode);
	}

	dput(dentry);
out_dput:
458
	mutex_unlock(&xadir->d_inode->i_mutex);
459 460 461 462
	dput(xadir);
	return err;
}

V
Vladimir Saveliev 已提交
463

L
Linus Torvalds 已提交
464 465 466
/* Generic extended attribute operations that can be used by xa plugins */

/*
467
 * inode->i_mutex: down
L
Linus Torvalds 已提交
468 469
 */
int
J
Jeff Mahoney 已提交
470 471 472
reiserfs_xattr_set_handle(struct reiserfs_transaction_handle *th,
			  struct inode *inode, const char *name,
			  const void *buffer, size_t buffer_size, int flags)
L
Linus Torvalds 已提交
473
{
474
	int err = 0;
475
	struct dentry *dentry;
476 477 478 479
	struct page *page;
	char *data;
	size_t file_pos = 0;
	size_t buffer_pos = 0;
480
	size_t new_size;
481 482 483 484 485
	__u32 xahash = 0;

	if (get_inode_sd_version(inode) == STAT_DATA_V1)
		return -EOPNOTSUPP;

486
	reiserfs_write_unlock(inode->i_sb);
487 488 489 490 491 492 493

	if (!buffer) {
		err = lookup_and_delete_xattr(inode, name);
		reiserfs_write_lock(inode->i_sb);
		return err;
	}

494
	dentry = xattr_lookup(inode, name, flags);
495 496
	if (IS_ERR(dentry)) {
		reiserfs_write_lock(inode->i_sb);
497
		return PTR_ERR(dentry);
498 499
	}

500
	down_write(&REISERFS_I(inode)->i_xattr_sem);
501

502
	reiserfs_write_lock(inode->i_sb);
503

504
	xahash = xattr_hash(buffer, buffer_size);
505 506 507 508 509 510 511 512 513
	while (buffer_pos < buffer_size || buffer_pos == 0) {
		size_t chunk;
		size_t skip = 0;
		size_t page_offset = (file_pos & (PAGE_CACHE_SIZE - 1));
		if (buffer_size - buffer_pos > PAGE_CACHE_SIZE)
			chunk = PAGE_CACHE_SIZE;
		else
			chunk = buffer_size - buffer_pos;

514
		page = reiserfs_get_page(dentry->d_inode, file_pos);
515 516
		if (IS_ERR(page)) {
			err = PTR_ERR(page);
517
			goto out_unlock;
518 519 520 521 522 523 524 525 526 527 528 529 530 531 532
		}

		lock_page(page);
		data = page_address(page);

		if (file_pos == 0) {
			struct reiserfs_xattr_header *rxh;
			skip = file_pos = sizeof(struct reiserfs_xattr_header);
			if (chunk + skip > PAGE_CACHE_SIZE)
				chunk = PAGE_CACHE_SIZE - skip;
			rxh = (struct reiserfs_xattr_header *)data;
			rxh->h_magic = cpu_to_le32(REISERFS_XATTR_MAGIC);
			rxh->h_hash = cpu_to_le32(xahash);
		}

C
Christoph Hellwig 已提交
533
		err = __reiserfs_write_begin(page, page_offset, chunk + skip);
534 535 536
		if (!err) {
			if (buffer)
				memcpy(data + skip, buffer + buffer_pos, chunk);
537 538 539
			err = reiserfs_commit_write(NULL, page, page_offset,
						    page_offset + chunk +
						    skip);
540 541 542 543 544 545 546 547 548 549
		}
		unlock_page(page);
		reiserfs_put_page(page);
		buffer_pos += chunk;
		file_pos += chunk;
		skip = 0;
		if (err || buffer_size == 0 || !buffer)
			break;
	}

550 551 552 553
	new_size = buffer_size + sizeof(struct reiserfs_xattr_header);
	if (!err && new_size < i_size_read(dentry->d_inode)) {
		struct iattr newattrs = {
			.ia_ctime = current_fs_time(inode->i_sb),
554
			.ia_size = new_size,
555 556
			.ia_valid = ATTR_SIZE | ATTR_CTIME,
		};
557 558

		reiserfs_write_unlock(inode->i_sb);
559 560
		mutex_lock_nested(&dentry->d_inode->i_mutex, I_MUTEX_XATTR);
		down_write(&dentry->d_inode->i_alloc_sem);
561 562
		reiserfs_write_lock(inode->i_sb);

563 564 565 566 567 568
		err = reiserfs_setattr(dentry, &newattrs);
		up_write(&dentry->d_inode->i_alloc_sem);
		mutex_unlock(&dentry->d_inode->i_mutex);
	} else
		update_ctime(inode);
out_unlock:
569
	up_write(&REISERFS_I(inode)->i_xattr_sem);
570
	dput(dentry);
571 572
	return err;
}
573

J
Jeff Mahoney 已提交
574 575 576
/* We need to start a transaction to maintain lock ordering */
int reiserfs_xattr_set(struct inode *inode, const char *name,
		       const void *buffer, size_t buffer_size, int flags)
577
{
J
Jeff Mahoney 已提交
578 579 580 581 582 583 584 585 586 587 588 589 590

	struct reiserfs_transaction_handle th;
	int error, error2;
	size_t jbegin_count = reiserfs_xattr_nblocks(inode, buffer_size);

	if (!(flags & XATTR_REPLACE))
		jbegin_count += reiserfs_xattr_jcreate_nblocks(inode);

	reiserfs_write_lock(inode->i_sb);
	error = journal_begin(&th, inode->i_sb, jbegin_count);
	if (error) {
		reiserfs_write_unlock(inode->i_sb);
		return error;
L
Linus Torvalds 已提交
591
	}
592

J
Jeff Mahoney 已提交
593 594
	error = reiserfs_xattr_set_handle(&th, inode, name,
					  buffer, buffer_size, flags);
595

J
Jeff Mahoney 已提交
596 597 598 599 600 601
	error2 = journal_end(&th, inode->i_sb, jbegin_count);
	if (error == 0)
		error = error2;
	reiserfs_write_unlock(inode->i_sb);

	return error;
L
Linus Torvalds 已提交
602 603 604
}

/*
605
 * inode->i_mutex: down
L
Linus Torvalds 已提交
606 607
 */
int
608
reiserfs_xattr_get(struct inode *inode, const char *name, void *buffer,
609
		   size_t buffer_size)
L
Linus Torvalds 已提交
610
{
611
	ssize_t err = 0;
612
	struct dentry *dentry;
613 614 615 616 617 618 619 620 621 622 623 624 625 626
	size_t isize;
	size_t file_pos = 0;
	size_t buffer_pos = 0;
	struct page *page;
	__u32 hash = 0;

	if (name == NULL)
		return -EINVAL;

	/* We can't have xattrs attached to v1 items since they don't have
	 * generation numbers */
	if (get_inode_sd_version(inode) == STAT_DATA_V1)
		return -EOPNOTSUPP;

627
	dentry = xattr_lookup(inode, name, XATTR_REPLACE);
628 629
	if (IS_ERR(dentry)) {
		err = PTR_ERR(dentry);
630 631 632
		goto out;
	}

633
	down_read(&REISERFS_I(inode)->i_xattr_sem);
634

J
Jeff Mahoney 已提交
635
	isize = i_size_read(dentry->d_inode);
636 637 638 639

	/* Just return the size needed */
	if (buffer == NULL) {
		err = isize - sizeof(struct reiserfs_xattr_header);
640
		goto out_unlock;
641 642 643 644
	}

	if (buffer_size < isize - sizeof(struct reiserfs_xattr_header)) {
		err = -ERANGE;
645
		goto out_unlock;
646 647 648 649 650 651 652 653 654 655 656
	}

	while (file_pos < isize) {
		size_t chunk;
		char *data;
		size_t skip = 0;
		if (isize - file_pos > PAGE_CACHE_SIZE)
			chunk = PAGE_CACHE_SIZE;
		else
			chunk = isize - file_pos;

657
		page = reiserfs_get_page(dentry->d_inode, file_pos);
658 659
		if (IS_ERR(page)) {
			err = PTR_ERR(page);
660
			goto out_unlock;
661 662 663 664 665 666 667 668 669 670 671 672 673
		}

		lock_page(page);
		data = page_address(page);
		if (file_pos == 0) {
			struct reiserfs_xattr_header *rxh =
			    (struct reiserfs_xattr_header *)data;
			skip = file_pos = sizeof(struct reiserfs_xattr_header);
			chunk -= skip;
			/* Magic doesn't match up.. */
			if (rxh->h_magic != cpu_to_le32(REISERFS_XATTR_MAGIC)) {
				unlock_page(page);
				reiserfs_put_page(page);
674
				reiserfs_warning(inode->i_sb, "jdm-20001",
675 676 677 678
						 "Invalid magic for xattr (%s) "
						 "associated with %k", name,
						 INODE_PKEY(inode));
				err = -EIO;
679
				goto out_unlock;
680 681 682 683 684 685 686 687 688 689 690 691 692 693
			}
			hash = le32_to_cpu(rxh->h_hash);
		}
		memcpy(buffer + buffer_pos, data + skip, chunk);
		unlock_page(page);
		reiserfs_put_page(page);
		file_pos += chunk;
		buffer_pos += chunk;
		skip = 0;
	}
	err = isize - sizeof(struct reiserfs_xattr_header);

	if (xattr_hash(buffer, isize - sizeof(struct reiserfs_xattr_header)) !=
	    hash) {
694
		reiserfs_warning(inode->i_sb, "jdm-20002",
695 696 697 698 699
				 "Invalid hash for xattr (%s) associated "
				 "with %k", name, INODE_PKEY(inode));
		err = -EIO;
	}

700 701
out_unlock:
	up_read(&REISERFS_I(inode)->i_xattr_sem);
702
	dput(dentry);
703

704
out:
705
	return err;
L
Linus Torvalds 已提交
706 707
}

708 709 710 711 712 713 714 715 716 717 718 719 720
/*
 * In order to implement different sets of xattr operations for each xattr
 * prefix with the generic xattr API, a filesystem should create a
 * null-terminated array of struct xattr_handler (one for each prefix) and
 * hang a pointer to it off of the s_xattr field of the superblock.
 *
 * The generic_fooxattr() functions will use this list to dispatch xattr
 * operations to the correct xattr_handler.
 */
#define for_each_xattr_handler(handlers, handler)		\
		for ((handler) = *(handlers)++;			\
			(handler) != NULL;			\
			(handler) = *(handlers)++)
L
Linus Torvalds 已提交
721

722
/* This is the implementation for the xattr plugin infrastructure */
723 724
static inline const struct xattr_handler *
find_xattr_handler_prefix(const struct xattr_handler **handlers,
725
			   const char *name)
L
Linus Torvalds 已提交
726
{
727
	const struct xattr_handler *xah;
728

729 730
	if (!handlers)
		return NULL;
731

732 733 734
	for_each_xattr_handler(handlers, xah) {
		if (strncmp(xah->prefix, name, strlen(xah->prefix)) == 0)
			break;
735 736
	}

737
	return xah;
738
}
L
Linus Torvalds 已提交
739 740 741 742 743 744


/*
 * Inode operation getxattr()
 */
ssize_t
745 746
reiserfs_getxattr(struct dentry * dentry, const char *name, void *buffer,
		  size_t size)
L
Linus Torvalds 已提交
747
{
748
	const struct xattr_handler *handler;
749

750
	handler = find_xattr_handler_prefix(dentry->d_sb->s_xattr, name);
751

752
	if (!handler || get_inode_sd_version(dentry->d_inode) == STAT_DATA_V1)
753 754
		return -EOPNOTSUPP;

755
	return handler->get(dentry, name, buffer, size, handler->flags);
L
Linus Torvalds 已提交
756 757 758 759 760
}

/*
 * Inode operation setxattr()
 *
761
 * dentry->d_inode->i_mutex down
L
Linus Torvalds 已提交
762 763
 */
int
764 765
reiserfs_setxattr(struct dentry *dentry, const char *name, const void *value,
		  size_t size, int flags)
L
Linus Torvalds 已提交
766
{
767
	const struct xattr_handler *handler;
768

769
	handler = find_xattr_handler_prefix(dentry->d_sb->s_xattr, name);
770

771
	if (!handler || get_inode_sd_version(dentry->d_inode) == STAT_DATA_V1)
772 773
		return -EOPNOTSUPP;

774
	return handler->set(dentry, name, value, size, flags, handler->flags);
L
Linus Torvalds 已提交
775 776 777 778 779
}

/*
 * Inode operation removexattr()
 *
780
 * dentry->d_inode->i_mutex down
L
Linus Torvalds 已提交
781
 */
782
int reiserfs_removexattr(struct dentry *dentry, const char *name)
L
Linus Torvalds 已提交
783
{
784
	const struct xattr_handler *handler;
785
	handler = find_xattr_handler_prefix(dentry->d_sb->s_xattr, name);
L
Linus Torvalds 已提交
786

787
	if (!handler || get_inode_sd_version(dentry->d_inode) == STAT_DATA_V1)
788
		return -EOPNOTSUPP;
L
Linus Torvalds 已提交
789

790
	return handler->set(dentry, name, NULL, 0, XATTR_REPLACE, handler->flags);
L
Linus Torvalds 已提交
791 792
}

793 794 795 796
struct listxattr_buf {
	size_t size;
	size_t pos;
	char *buf;
797
	struct dentry *dentry;
L
Linus Torvalds 已提交
798 799
};

800 801
static int listxattr_filler(void *buf, const char *name, int namelen,
			    loff_t offset, u64 ino, unsigned int d_type)
L
Linus Torvalds 已提交
802
{
803 804 805 806
	struct listxattr_buf *b = (struct listxattr_buf *)buf;
	size_t size;
	if (name[0] != '.' ||
	    (namelen != 1 && (name[1] != '.' || namelen != 2))) {
807
		const struct xattr_handler *handler;
808
		handler = find_xattr_handler_prefix(b->dentry->d_sb->s_xattr,
809 810 811 812
						    name);
		if (!handler)	/* Unsupported xattr name */
			return 0;
		if (b->buf) {
813 814 815
			size = handler->list(b->dentry, b->buf + b->pos,
					 b->size, name, namelen,
					 handler->flags);
816 817 818
			if (size > b->size)
				return -ERANGE;
		} else {
819 820
			size = handler->list(b->dentry, NULL, 0, name,
					     namelen, handler->flags);
821 822
		}

823 824
		b->pos += size;
	}
825
	return 0;
L
Linus Torvalds 已提交
826
}
827

L
Linus Torvalds 已提交
828 829 830
/*
 * Inode operation listxattr()
 *
831 832 833
 * We totally ignore the generic listxattr here because it would be stupid
 * not to. Since the xattrs are organized in a directory, we can just
 * readdir to find them.
L
Linus Torvalds 已提交
834
 */
835
ssize_t reiserfs_listxattr(struct dentry * dentry, char *buffer, size_t size)
L
Linus Torvalds 已提交
836
{
837 838
	struct dentry *dir;
	int err = 0;
839
	loff_t pos = 0;
840
	struct listxattr_buf buf = {
841
		.dentry = dentry,
842 843 844
		.buf = buffer,
		.size = buffer ? size : 0,
	};
845 846 847 848

	if (!dentry->d_inode)
		return -EINVAL;

849
	if (!dentry->d_sb->s_xattr ||
850 851 852
	    get_inode_sd_version(dentry->d_inode) == STAT_DATA_V1)
		return -EOPNOTSUPP;

853
	dir = open_xa_dir(dentry->d_inode, XATTR_REPLACE);
854 855 856
	if (IS_ERR(dir)) {
		err = PTR_ERR(dir);
		if (err == -ENODATA)
857
			err = 0;  /* Not an error if there aren't any xattrs */
858 859 860
		goto out;
	}

861
	mutex_lock_nested(&dir->d_inode->i_mutex, I_MUTEX_XATTR);
862
	err = reiserfs_readdir_dentry(dir, &buf, listxattr_filler, &pos);
863
	mutex_unlock(&dir->d_inode->i_mutex);
864

865 866
	if (!err)
		err = buf.pos;
867

868
	dput(dir);
869
out:
870
	return err;
L
Linus Torvalds 已提交
871 872
}

873
static int reiserfs_check_acl(struct inode *inode, int mask)
L
Linus Torvalds 已提交
874
{
875 876
	struct posix_acl *acl;
	int error = -EAGAIN; /* do regular unix permission checks by default */
877

878 879 880 881 882 883 884 885
	acl = reiserfs_get_acl(inode, ACL_TYPE_ACCESS);

	if (acl) {
		if (!IS_ERR(acl)) {
			error = posix_acl_permission(inode, acl, mask);
			posix_acl_release(acl);
		} else if (PTR_ERR(acl) != -ENODATA)
			error = PTR_ERR(acl);
886 887
	}

888
	return error;
L
Linus Torvalds 已提交
889 890
}

891
static int create_privroot(struct dentry *dentry)
L
Linus Torvalds 已提交
892
{
893 894
	int err;
	struct inode *inode = dentry->d_parent->d_inode;
895 896
	WARN_ON_ONCE(!mutex_is_locked(&inode->i_mutex));

897
	err = xattr_mkdir(inode, dentry, 0700);
898 899 900 901 902 903
	if (err || !dentry->d_inode) {
		reiserfs_warning(dentry->d_sb, "jdm-20006",
				 "xattrs/ACLs enabled and couldn't "
				 "find/create .reiserfs_priv. "
				 "Failing mount.");
		return -EOPNOTSUPP;
904 905
	}

906 907 908
	dentry->d_inode->i_flags |= S_PRIVATE;
	reiserfs_info(dentry->d_sb, "Created %s - reserved for xattr "
		      "storage.\n", PRIVROOT_NAME);
909

910
	return 0;
L
Linus Torvalds 已提交
911 912
}

J
Jeff Mahoney 已提交
913 914 915 916 917 918 919
#else
int __init reiserfs_xattr_register_handlers(void) { return 0; }
void reiserfs_xattr_unregister_handlers(void) {}
static int create_privroot(struct dentry *dentry) { return 0; }
#endif

/* Actual operations that are exported to VFS-land */
920
const struct xattr_handler *reiserfs_xattr_handlers[] = {
J
Jeff Mahoney 已提交
921 922 923 924 925 926 927 928 929 930 931 932 933 934
#ifdef CONFIG_REISERFS_FS_XATTR
	&reiserfs_xattr_user_handler,
	&reiserfs_xattr_trusted_handler,
#endif
#ifdef CONFIG_REISERFS_FS_SECURITY
	&reiserfs_xattr_security_handler,
#endif
#ifdef CONFIG_REISERFS_FS_POSIX_ACL
	&reiserfs_posix_acl_access_handler,
	&reiserfs_posix_acl_default_handler,
#endif
	NULL
};

935
static int xattr_mount_check(struct super_block *s)
L
Linus Torvalds 已提交
936
{
937 938
	/* We need generation numbers to ensure that the oid mapping is correct
	 * v3.5 filesystems don't have them. */
939 940 941 942 943 944 945 946 947 948
	if (old_format_only(s)) {
		if (reiserfs_xattrs_optional(s)) {
			/* Old format filesystem, but optional xattrs have
			 * been enabled. Error out. */
			reiserfs_warning(s, "jdm-2005",
					 "xattrs/ACLs not supported "
					 "on pre-v3.6 format filesystems. "
					 "Failing mount.");
			return -EOPNOTSUPP;
		}
949 950 951
	}

	return 0;
L
Linus Torvalds 已提交
952 953
}

954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972
int reiserfs_permission(struct inode *inode, int mask)
{
	/*
	 * We don't do permission checks on the internal objects.
	 * Permissions are determined by the "owning" object.
	 */
	if (IS_PRIVATE(inode))
		return 0;

#ifdef CONFIG_REISERFS_FS_XATTR
	/*
	 * Stat data v1 doesn't support ACLs.
	 */
	if (get_inode_sd_version(inode) != STAT_DATA_V1)
		return generic_permission(inode, mask, reiserfs_check_acl);
#endif
	return generic_permission(inode, mask, NULL);
}

973
static int xattr_hide_revalidate(struct dentry *dentry, struct nameidata *nd)
L
Linus Torvalds 已提交
974
{
975
	return -EPERM;
L
Linus Torvalds 已提交
976 977
}

978
static const struct dentry_operations xattr_lookup_poison_ops = {
979
	.d_revalidate = xattr_hide_revalidate,
L
Linus Torvalds 已提交
980 981
};

982 983 984 985 986 987
int reiserfs_lookup_privroot(struct super_block *s)
{
	struct dentry *dentry;
	int err = 0;

	/* If we don't have the privroot located yet - go find it */
988
	reiserfs_mutex_lock_safe(&s->s_root->d_inode->i_mutex, s);
989 990 991 992
	dentry = lookup_one_len(PRIVROOT_NAME, s->s_root,
				strlen(PRIVROOT_NAME));
	if (!IS_ERR(dentry)) {
		REISERFS_SB(s)->priv_root = dentry;
993
		dentry->d_op = &xattr_lookup_poison_ops;
994 995 996 997 998 999 1000 1001 1002
		if (dentry->d_inode)
			dentry->d_inode->i_flags |= S_PRIVATE;
	} else
		err = PTR_ERR(dentry);
	mutex_unlock(&s->s_root->d_inode->i_mutex);

	return err;
}

L
Linus Torvalds 已提交
1003 1004 1005
/* We need to take a copy of the mount flags since things like
 * MS_RDONLY don't get set until *after* we're called.
 * mount_flags != mount_options */
1006
int reiserfs_xattr_init(struct super_block *s, int mount_flags)
L
Linus Torvalds 已提交
1007
{
1008
	int err = 0;
1009
	struct dentry *privroot = REISERFS_SB(s)->priv_root;
1010

1011 1012
	err = xattr_mount_check(s);
	if (err)
1013 1014
		goto error;

1015
	if (!privroot->d_inode && !(mount_flags & MS_RDONLY)) {
1016
		reiserfs_mutex_lock_safe(&s->s_root->d_inode->i_mutex, s);
1017
		err = create_privroot(REISERFS_SB(s)->priv_root);
1018
		mutex_unlock(&s->s_root->d_inode->i_mutex);
1019
	}
1020 1021

	if (privroot->d_inode) {
1022
		s->s_xattr = reiserfs_xattr_handlers;
1023
		reiserfs_mutex_lock_safe(&privroot->d_inode->i_mutex, s);
1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034
		if (!REISERFS_SB(s)->xattr_root) {
			struct dentry *dentry;
			dentry = lookup_one_len(XAROOT_NAME, privroot,
						strlen(XAROOT_NAME));
			if (!IS_ERR(dentry))
				REISERFS_SB(s)->xattr_root = dentry;
			else
				err = PTR_ERR(dentry);
		}
		mutex_unlock(&privroot->d_inode->i_mutex);
	}
1035

1036
error:
1037 1038 1039 1040 1041 1042 1043 1044
	if (err) {
		clear_bit(REISERFS_XATTRS_USER, &(REISERFS_SB(s)->s_mount_opt));
		clear_bit(REISERFS_POSIXACL, &(REISERFS_SB(s)->s_mount_opt));
	}

	/* The super_block MS_POSIXACL must mirror the (no)acl mount option. */
	if (reiserfs_posixacl(s))
		s->s_flags |= MS_POSIXACL;
1045 1046
	else
		s->s_flags &= ~MS_POSIXACL;
1047 1048

	return err;
L
Linus Torvalds 已提交
1049
}