xattr.c 25.3 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
Linus Torvalds's avatar
Linus Torvalds committed
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 30
/*
 * 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.
31 32
 *
 * Locking works like so:
33 34
 * Directory components (xattr root, xattr dir) are protectd by their i_mutex.
 * The xattrs themselves are protected by the xattr_sem.
Linus Torvalds's avatar
Linus Torvalds committed
35 36
 */

37
#include "reiserfs.h"
38
#include <linux/capability.h>
Linus Torvalds's avatar
Linus Torvalds committed
39 40 41
#include <linux/dcache.h>
#include <linux/namei.h>
#include <linux/errno.h>
42
#include <linux/gfp.h>
Linus Torvalds's avatar
Linus Torvalds committed
43 44 45 46
#include <linux/fs.h>
#include <linux/file.h>
#include <linux/pagemap.h>
#include <linux/xattr.h>
47
#include "xattr.h"
48
#include "acl.h"
49
#include <linux/uaccess.h>
50
#include <net/checksum.h>
Linus Torvalds's avatar
Linus Torvalds committed
51
#include <linux/stat.h>
52
#include <linux/quotaops.h>
53
#include <linux/security.h>
54
#include <linux/posix_acl_xattr.h>
55
#include <linux/xattr.h>
Linus Torvalds's avatar
Linus Torvalds committed
56 57 58 59 60

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


61 62
/*
 * Helpers for inode ops. We do this so that we don't have all the VFS
63
 * overhead and also for proper i_mutex annotation.
64 65
 * dir->i_mutex must be held for all of them.
 */
66
#ifdef CONFIG_REISERFS_FS_XATTR
67
static int xattr_create(struct inode *dir, struct dentry *dentry, int mode)
Linus Torvalds's avatar
Linus Torvalds committed
68
{
Al Viro's avatar
Al Viro committed
69
	BUG_ON(!inode_is_locked(dir));
70
	return dir->i_op->create(&nop_mnt_idmap, dir, dentry, mode, true);
71
}
72
#endif
73

74
static int xattr_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
75
{
Al Viro's avatar
Al Viro committed
76
	BUG_ON(!inode_is_locked(dir));
77
	return dir->i_op->mkdir(&nop_mnt_idmap, dir, dentry, mode);
78
}
79

80 81
/*
 * We use I_MUTEX_CHILD here to silence lockdep. It's safe because xattr
82 83
 * 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
84 85
 * better than allocating another subclass just for this code.
 */
86 87 88
static int xattr_unlink(struct inode *dir, struct dentry *dentry)
{
	int error;
89

Al Viro's avatar
Al Viro committed
90
	BUG_ON(!inode_is_locked(dir));
91

Al Viro's avatar
Al Viro committed
92
	inode_lock_nested(d_inode(dentry), I_MUTEX_CHILD);
93
	error = dir->i_op->unlink(dir, dentry);
Al Viro's avatar
Al Viro committed
94
	inode_unlock(d_inode(dentry));
95 96 97 98 99 100 101 102 103

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

static int xattr_rmdir(struct inode *dir, struct dentry *dentry)
{
	int error;
104

Al Viro's avatar
Al Viro committed
105
	BUG_ON(!inode_is_locked(dir));
106

Al Viro's avatar
Al Viro committed
107
	inode_lock_nested(d_inode(dentry), I_MUTEX_CHILD);
108 109
	error = dir->i_op->rmdir(dir, dentry);
	if (!error)
110
		d_inode(dentry)->i_flags |= S_DEAD;
Al Viro's avatar
Al Viro committed
111
	inode_unlock(d_inode(dentry));
112 113 114 115 116 117 118 119
	if (!error)
		d_delete(dentry);

	return error;
}

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

120
static struct dentry *open_xa_root(struct super_block *sb, int flags)
121
{
122 123
	struct dentry *privroot = REISERFS_SB(sb)->priv_root;
	struct dentry *xaroot;
124

125
	if (d_really_is_negative(privroot))
126
		return ERR_PTR(-EOPNOTSUPP);
127

Al Viro's avatar
Al Viro committed
128
	inode_lock_nested(d_inode(privroot), I_MUTEX_XATTR);
129

130
	xaroot = dget(REISERFS_SB(sb)->xattr_root);
131
	if (!xaroot)
132
		xaroot = ERR_PTR(-EOPNOTSUPP);
133
	else if (d_really_is_negative(xaroot)) {
134
		int err = -ENODATA;
135

136
		if (xattr_may_create(flags))
137
			err = xattr_mkdir(d_inode(privroot), xaroot, 0700);
138
		if (err) {
139 140
			dput(xaroot);
			xaroot = ERR_PTR(err);
141
		}
142
	}
143

Al Viro's avatar
Al Viro committed
144
	inode_unlock(d_inode(privroot));
145
	return xaroot;
Linus Torvalds's avatar
Linus Torvalds committed
146 147
}

148
static struct dentry *open_xa_dir(const struct inode *inode, int flags)
Linus Torvalds's avatar
Linus Torvalds committed
149
{
150 151 152
	struct dentry *xaroot, *xadir;
	char namebuf[17];

153
	xaroot = open_xa_root(inode->i_sb, flags);
154
	if (IS_ERR(xaroot))
155 156 157 158 159 160
		return xaroot;

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

Al Viro's avatar
Al Viro committed
161
	inode_lock_nested(d_inode(xaroot), I_MUTEX_XATTR);
162 163

	xadir = lookup_one_len(namebuf, xaroot, strlen(namebuf));
164
	if (!IS_ERR(xadir) && d_really_is_negative(xadir)) {
165
		int err = -ENODATA;
166

167
		if (xattr_may_create(flags))
168
			err = xattr_mkdir(d_inode(xaroot), xadir, 0700);
169 170 171 172 173 174
		if (err) {
			dput(xadir);
			xadir = ERR_PTR(err);
		}
	}

Al Viro's avatar
Al Viro committed
175
	inode_unlock(d_inode(xaroot));
176 177
	dput(xaroot);
	return xadir;
Linus Torvalds's avatar
Linus Torvalds committed
178 179
}

180 181
/*
 * The following are side effects of other operations that aren't explicitly
182
 * modifying extended attributes. This includes operations such as permissions
183 184
 * or ownership changes, object deletions, etc.
 */
185
struct reiserfs_dentry_buf {
Al Viro's avatar
Al Viro committed
186
	struct dir_context ctx;
187 188
	struct dentry *xadir;
	int count;
189
	int err;
190 191
	struct dentry *dentries[8];
};
192

193
static bool
194 195
fill_with_dentries(struct dir_context *ctx, const char *name, int namelen,
		   loff_t offset, u64 ino, unsigned int d_type)
196
{
197 198
	struct reiserfs_dentry_buf *dbuf =
		container_of(ctx, struct reiserfs_dentry_buf, ctx);
199
	struct dentry *dentry;
200

Al Viro's avatar
Al Viro committed
201
	WARN_ON_ONCE(!inode_is_locked(d_inode(dbuf->xadir)));
202

203
	if (dbuf->count == ARRAY_SIZE(dbuf->dentries))
204
		return false;
205

206 207
	if (name[0] == '.' && (namelen < 2 ||
			       (namelen == 2 && name[1] == '.')))
208
		return true;
209

210
	dentry = lookup_one_len(name, dbuf->xadir, namelen);
211
	if (IS_ERR(dentry)) {
212
		dbuf->err = PTR_ERR(dentry);
213
		return false;
214
	} else if (d_really_is_negative(dentry)) {
215 216
		/* A directory entry exists, but no file? */
		reiserfs_error(dentry->d_sb, "xattr-20003",
Al Viro's avatar
Al Viro committed
217 218 219
			       "Corrupted directory: xattr %pd listed but "
			       "not found for file %pd.\n",
			       dentry, dbuf->xadir);
220
		dput(dentry);
221
		dbuf->err = -EIO;
222
		return false;
223
	}
Linus Torvalds's avatar
Linus Torvalds committed
224

225
	dbuf->dentries[dbuf->count++] = dentry;
226
	return true;
Linus Torvalds's avatar
Linus Torvalds committed
227 228
}

229 230
static void
cleanup_dentry_buf(struct reiserfs_dentry_buf *buf)
Linus Torvalds's avatar
Linus Torvalds committed
231
{
232
	int i;
233

234 235 236
	for (i = 0; i < buf->count; i++)
		if (buf->dentries[i])
			dput(buf->dentries[i]);
237 238
}

239 240 241
static int reiserfs_for_each_xattr(struct inode *inode,
				   int (*action)(struct dentry *, void *),
				   void *data)
242
{
243 244 245
	struct dentry *dir;
	int i, err = 0;
	struct reiserfs_dentry_buf buf = {
Al Viro's avatar
Al Viro committed
246
		.ctx.actor = fill_with_dentries,
247
	};
Linus Torvalds's avatar
Linus Torvalds committed
248

249 250 251
	/* Skip out, an xattr has no xattrs associated with it */
	if (IS_PRIVATE(inode) || get_inode_sd_version(inode) == STAT_DATA_V1)
		return 0;
Linus Torvalds's avatar
Linus Torvalds committed
252

253
	dir = open_xa_dir(inode, XATTR_REPLACE);
254 255 256
	if (IS_ERR(dir)) {
		err = PTR_ERR(dir);
		goto out;
257
	} else if (d_really_is_negative(dir)) {
258 259
		err = 0;
		goto out_dir;
260
	}
Linus Torvalds's avatar
Linus Torvalds committed
261

Al Viro's avatar
Al Viro committed
262
	inode_lock_nested(d_inode(dir), I_MUTEX_XATTR);
263

264
	buf.xadir = dir;
265
	while (1) {
266
		err = reiserfs_readdir_inode(d_inode(dir), &buf.ctx);
267 268
		if (err)
			break;
269 270 271 272
		if (buf.err) {
			err = buf.err;
			break;
		}
273 274 275
		if (!buf.count)
			break;
		for (i = 0; !err && i < buf.count && buf.dentries[i]; i++) {
276
			struct dentry *dentry = buf.dentries[i];
Linus Torvalds's avatar
Linus Torvalds committed
277

278
			if (!d_is_dir(dentry))
279
				err = action(dentry, data);
Linus Torvalds's avatar
Linus Torvalds committed
280

281 282
			dput(dentry);
			buf.dentries[i] = NULL;
283
		}
284 285
		if (err)
			break;
286
		buf.count = 0;
287
	}
Al Viro's avatar
Al Viro committed
288
	inode_unlock(d_inode(dir));
Linus Torvalds's avatar
Linus Torvalds committed
289

290
	cleanup_dentry_buf(&buf);
Linus Torvalds's avatar
Linus Torvalds committed
291

292
	if (!err) {
293 294
		/*
		 * We start a transaction here to avoid a ABBA situation
295 296 297
		 * 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
298 299
		 * outer transaction.
		 */
300 301 302
		int blocks = JOURNAL_PER_BALANCE_CNT * 2 + 2 +
			     4 * REISERFS_QUOTA_TRANS_BLOCKS(inode->i_sb);
		struct reiserfs_transaction_handle th;
303

304
		reiserfs_write_lock(inode->i_sb);
305
		err = journal_begin(&th, inode->i_sb, blocks);
306
		reiserfs_write_unlock(inode->i_sb);
307 308
		if (!err) {
			int jerror;
309

Al Viro's avatar
Al Viro committed
310
			inode_lock_nested(d_inode(dir->d_parent),
311
					  I_MUTEX_XATTR);
312
			err = action(dir, data);
313
			reiserfs_write_lock(inode->i_sb);
314
			jerror = journal_end(&th);
315
			reiserfs_write_unlock(inode->i_sb);
Al Viro's avatar
Al Viro committed
316
			inode_unlock(d_inode(dir->d_parent));
317 318
			err = jerror ?: err;
		}
319
	}
320 321
out_dir:
	dput(dir);
322
out:
323 324 325 326 327 328
	/*
	 * -ENODATA: this object doesn't have any xattrs
	 * -EOPNOTSUPP: this file system doesn't have xattrs enabled on disk.
	 * Neither are errors
	 */
	if (err == -ENODATA || err == -EOPNOTSUPP)
329
		err = 0;
330 331
	return err;
}
Linus Torvalds's avatar
Linus Torvalds committed
332

333
static int delete_one_xattr(struct dentry *dentry, void *data)
334
{
335
	struct inode *dir = d_inode(dentry->d_parent);
Linus Torvalds's avatar
Linus Torvalds committed
336

337
	/* This is the xattr dir, handle specially. */
338
	if (d_is_dir(dentry))
339
		return xattr_rmdir(dir, dentry);
Linus Torvalds's avatar
Linus Torvalds committed
340

341 342
	return xattr_unlink(dir, dentry);
}
343

344 345 346
static int chown_one_xattr(struct dentry *dentry, void *data)
{
	struct iattr *attrs = data;
347 348 349 350 351 352 353 354 355
	int ia_valid = attrs->ia_valid;
	int err;

	/*
	 * We only want the ownership bits. Otherwise, we'll do
	 * things like change a directory to a regular file if
	 * ATTR_MODE is set.
	 */
	attrs->ia_valid &= (ATTR_UID|ATTR_GID);
356
	err = reiserfs_setattr(&nop_mnt_idmap, dentry, attrs);
357 358 359
	attrs->ia_valid = ia_valid;

	return err;
360
}
Linus Torvalds's avatar
Linus Torvalds committed
361

362 363 364 365
/* 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);
366

367 368 369
	if (err)
		reiserfs_warning(inode->i_sb, "jdm-20004",
				 "Couldn't delete all xattrs (%d)\n", err);
370 371
	return err;
}
Linus Torvalds's avatar
Linus Torvalds committed
372

373
/* inode->i_mutex: down */
374 375
int reiserfs_chown_xattrs(struct inode *inode, struct iattr *attrs)
{
376
	int err = reiserfs_for_each_xattr(inode, chown_one_xattr, attrs);
377

378 379 380
	if (err)
		reiserfs_warning(inode->i_sb, "jdm-20007",
				 "Couldn't chown all xattrs (%d)\n", err);
381
	return err;
Linus Torvalds's avatar
Linus Torvalds committed
382 383
}

384
#ifdef CONFIG_REISERFS_FS_XATTR
385 386
/*
 * Returns a dentry corresponding to a specific extended attribute file
387
 * for the inode. If flags allow, the file is created. Otherwise, a
388 389
 * valid or negative dentry, or an error is returned.
 */
390 391
static struct dentry *xattr_lookup(struct inode *inode, const char *name,
				    int flags)
Linus Torvalds's avatar
Linus Torvalds committed
392
{
393 394 395 396
	struct dentry *xadir, *xafile;
	int err = 0;

	xadir = open_xa_dir(inode, flags);
397
	if (IS_ERR(xadir))
398 399
		return ERR_CAST(xadir);

Al Viro's avatar
Al Viro committed
400
	inode_lock_nested(d_inode(xadir), I_MUTEX_XATTR);
401 402
	xafile = lookup_one_len(name, xadir, strlen(name));
	if (IS_ERR(xafile)) {
403 404
		err = PTR_ERR(xafile);
		goto out;
405
	}
406

407
	if (d_really_is_positive(xafile) && (flags & XATTR_CREATE))
408
		err = -EEXIST;
409

410
	if (d_really_is_negative(xafile)) {
411
		err = -ENODATA;
412
		if (xattr_may_create(flags))
413
			err = xattr_create(d_inode(xadir), xafile,
414
					      0700|S_IFREG);
415 416
	}

417 418
	if (err)
		dput(xafile);
419
out:
Al Viro's avatar
Al Viro committed
420
	inode_unlock(d_inode(xadir));
421 422
	dput(xadir);
	if (err)
423
		return ERR_PTR(err);
424
	return xafile;
Linus Torvalds's avatar
Linus Torvalds committed
425 426 427
}

/* Internal operations on file data */
428
static inline void reiserfs_put_page(struct page *page)
Linus Torvalds's avatar
Linus Torvalds committed
429
{
430
	kunmap(page);
431
	put_page(page);
Linus Torvalds's avatar
Linus Torvalds committed
432 433
}

434
static struct page *reiserfs_get_page(struct inode *dir, size_t n)
Linus Torvalds's avatar
Linus Torvalds committed
435
{
436 437
	struct address_space *mapping = dir->i_mapping;
	struct page *page;
438 439 440 441
	/*
	 * We can deadlock if we try to free dentries,
	 * and an unlink/rmdir has just occurred - GFP_NOFS avoids this
	 */
442
	mapping_set_gfp_mask(mapping, GFP_NOFS);
443
	page = read_mapping_page(mapping, n >> PAGE_SHIFT, NULL);
444
	if (!IS_ERR(page))
445 446
		kmap(page);
	return page;
Linus Torvalds's avatar
Linus Torvalds committed
447 448
}

449
static inline __u32 xattr_hash(const char *msg, int len)
Linus Torvalds's avatar
Linus Torvalds committed
450
{
451 452 453 454 455 456 457 458 459
	/*
	 * csum_partial() gives different results for little-endian and
	 * big endian hosts. Images created on little-endian hosts and
	 * mounted on big-endian hosts(and vice versa) will see csum mismatches
	 * when trying to fetch xattrs. Treating the hash as __wsum_t would
	 * lower the frequency of mismatch.  This is an endianness bug in
	 * reiserfs.  The return statement would result in a sparse warning. Do
	 * not fix the sparse warning so as to not hide a reminder of the bug.
	 */
460
	return csum_partial(msg, len, 0);
Linus Torvalds's avatar
Linus Torvalds committed
461 462
}

463 464 465
int reiserfs_commit_write(struct file *f, struct page *page,
			  unsigned from, unsigned to);

466 467
static void update_ctime(struct inode *inode)
{
468
	struct timespec64 now = current_time(inode);
469
	struct timespec64 ctime = inode_get_ctime(inode);
470

Al Viro's avatar
Al Viro committed
471
	if (inode_unhashed(inode) || !inode->i_nlink ||
472
	    timespec64_equal(&ctime, &now))
473 474
		return;

475
	inode_set_ctime_to_ts(inode, now);
476 477 478 479 480 481 482 483 484 485 486 487
	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);

Al Viro's avatar
Al Viro committed
488
	inode_lock_nested(d_inode(xadir), I_MUTEX_XATTR);
489 490 491 492 493 494
	dentry = lookup_one_len(name, xadir, strlen(name));
	if (IS_ERR(dentry)) {
		err = PTR_ERR(dentry);
		goto out_dput;
	}

495 496
	if (d_really_is_positive(dentry)) {
		err = xattr_unlink(d_inode(xadir), dentry);
497 498 499 500 501
		update_ctime(inode);
	}

	dput(dentry);
out_dput:
Al Viro's avatar
Al Viro committed
502
	inode_unlock(d_inode(xadir));
503 504 505 506
	dput(xadir);
	return err;
}

507

Linus Torvalds's avatar
Linus Torvalds committed
508 509 510
/* Generic extended attribute operations that can be used by xa plugins */

/*
511
 * inode->i_mutex: down
Linus Torvalds's avatar
Linus Torvalds committed
512 513
 */
int
Jeff Mahoney's avatar
Jeff Mahoney committed
514 515 516
reiserfs_xattr_set_handle(struct reiserfs_transaction_handle *th,
			  struct inode *inode, const char *name,
			  const void *buffer, size_t buffer_size, int flags)
Linus Torvalds's avatar
Linus Torvalds committed
517
{
518
	int err = 0;
519
	struct dentry *dentry;
520 521 522 523
	struct page *page;
	char *data;
	size_t file_pos = 0;
	size_t buffer_pos = 0;
524
	size_t new_size;
525 526 527 528 529
	__u32 xahash = 0;

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

530 531 532 533 534
	if (!buffer) {
		err = lookup_and_delete_xattr(inode, name);
		return err;
	}

535
	dentry = xattr_lookup(inode, name, flags);
536
	if (IS_ERR(dentry))
537
		return PTR_ERR(dentry);
538

539
	down_write(&REISERFS_I(inode)->i_xattr_sem);
540

541
	xahash = xattr_hash(buffer, buffer_size);
542 543 544
	while (buffer_pos < buffer_size || buffer_pos == 0) {
		size_t chunk;
		size_t skip = 0;
545
		size_t page_offset = (file_pos & (PAGE_SIZE - 1));
546

547 548
		if (buffer_size - buffer_pos > PAGE_SIZE)
			chunk = PAGE_SIZE;
549 550 551
		else
			chunk = buffer_size - buffer_pos;

552
		page = reiserfs_get_page(d_inode(dentry), file_pos);
553 554
		if (IS_ERR(page)) {
			err = PTR_ERR(page);
555
			goto out_unlock;
556 557 558 559 560 561 562
		}

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

		if (file_pos == 0) {
			struct reiserfs_xattr_header *rxh;
563

564
			skip = file_pos = sizeof(struct reiserfs_xattr_header);
565 566
			if (chunk + skip > PAGE_SIZE)
				chunk = PAGE_SIZE - skip;
567 568 569 570 571
			rxh = (struct reiserfs_xattr_header *)data;
			rxh->h_magic = cpu_to_le32(REISERFS_XATTR_MAGIC);
			rxh->h_hash = cpu_to_le32(xahash);
		}

572
		reiserfs_write_lock(inode->i_sb);
573
		err = __reiserfs_write_begin(page, page_offset, chunk + skip);
574 575 576
		if (!err) {
			if (buffer)
				memcpy(data + skip, buffer + buffer_pos, chunk);
577 578 579
			err = reiserfs_commit_write(NULL, page, page_offset,
						    page_offset + chunk +
						    skip);
580
		}
581
		reiserfs_write_unlock(inode->i_sb);
582 583 584 585 586 587 588 589 590
		unlock_page(page);
		reiserfs_put_page(page);
		buffer_pos += chunk;
		file_pos += chunk;
		skip = 0;
		if (err || buffer_size == 0 || !buffer)
			break;
	}

591
	new_size = buffer_size + sizeof(struct reiserfs_xattr_header);
592
	if (!err && new_size < i_size_read(d_inode(dentry))) {
593
		struct iattr newattrs = {
594
			.ia_ctime = current_time(inode),
595
			.ia_size = new_size,
596 597
			.ia_valid = ATTR_SIZE | ATTR_CTIME,
		};
598

Al Viro's avatar
Al Viro committed
599
		inode_lock_nested(d_inode(dentry), I_MUTEX_XATTR);
600
		inode_dio_wait(d_inode(dentry));
601

602
		err = reiserfs_setattr(&nop_mnt_idmap, dentry, &newattrs);
Al Viro's avatar
Al Viro committed
603
		inode_unlock(d_inode(dentry));
604 605 606
	} else
		update_ctime(inode);
out_unlock:
607
	up_write(&REISERFS_I(inode)->i_xattr_sem);
608
	dput(dentry);
609 610
	return err;
}
611

Jeff Mahoney's avatar
Jeff Mahoney committed
612 613 614
/* 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)
615
{
Jeff Mahoney's avatar
Jeff Mahoney committed
616 617 618 619 620

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

621 622 623 624
	/* Check before we start a transaction and then do nothing. */
	if (!d_really_is_positive(REISERFS_SB(inode->i_sb)->priv_root))
		return -EOPNOTSUPP;

Jeff Mahoney's avatar
Jeff Mahoney committed
625 626 627 628 629
	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);
630
	reiserfs_write_unlock(inode->i_sb);
Jeff Mahoney's avatar
Jeff Mahoney committed
631 632
	if (error) {
		return error;
Linus Torvalds's avatar
Linus Torvalds committed
633
	}
634

Jeff Mahoney's avatar
Jeff Mahoney committed
635 636
	error = reiserfs_xattr_set_handle(&th, inode, name,
					  buffer, buffer_size, flags);
637

638
	reiserfs_write_lock(inode->i_sb);
639
	error2 = journal_end(&th);
640
	reiserfs_write_unlock(inode->i_sb);
Jeff Mahoney's avatar
Jeff Mahoney committed
641 642 643 644
	if (error == 0)
		error = error2;

	return error;
Linus Torvalds's avatar
Linus Torvalds committed
645 646 647
}

/*
648
 * inode->i_mutex: down
Linus Torvalds's avatar
Linus Torvalds committed
649 650
 */
int
651
reiserfs_xattr_get(struct inode *inode, const char *name, void *buffer,
652
		   size_t buffer_size)
Linus Torvalds's avatar
Linus Torvalds committed
653
{
654
	ssize_t err = 0;
655
	struct dentry *dentry;
656 657 658 659 660 661 662 663 664
	size_t isize;
	size_t file_pos = 0;
	size_t buffer_pos = 0;
	struct page *page;
	__u32 hash = 0;

	if (name == NULL)
		return -EINVAL;

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

Jan Kara's avatar
Jan Kara committed
672 673 674 675 676 677 678
	/*
	 * priv_root needn't be initialized during mount so allow initial
	 * lookups to succeed.
	 */
	if (!REISERFS_SB(inode->i_sb)->priv_root)
		return 0;

679
	dentry = xattr_lookup(inode, name, XATTR_REPLACE);
680 681
	if (IS_ERR(dentry)) {
		err = PTR_ERR(dentry);
682 683 684
		goto out;
	}

685
	down_read(&REISERFS_I(inode)->i_xattr_sem);
686

687
	isize = i_size_read(d_inode(dentry));
688 689 690 691

	/* Just return the size needed */
	if (buffer == NULL) {
		err = isize - sizeof(struct reiserfs_xattr_header);
692
		goto out_unlock;
693 694 695 696
	}

	if (buffer_size < isize - sizeof(struct reiserfs_xattr_header)) {
		err = -ERANGE;
697
		goto out_unlock;
698 699 700 701 702 703
	}

	while (file_pos < isize) {
		size_t chunk;
		char *data;
		size_t skip = 0;
704

705 706
		if (isize - file_pos > PAGE_SIZE)
			chunk = PAGE_SIZE;
707 708 709
		else
			chunk = isize - file_pos;

710
		page = reiserfs_get_page(d_inode(dentry), file_pos);
711 712
		if (IS_ERR(page)) {
			err = PTR_ERR(page);
713
			goto out_unlock;
714 715 716 717 718 719 720 721 722 723 724 725 726
		}

		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);
727
				reiserfs_warning(inode->i_sb, "jdm-20001",
728 729 730 731
						 "Invalid magic for xattr (%s) "
						 "associated with %k", name,
						 INODE_PKEY(inode));
				err = -EIO;
732
				goto out_unlock;
733 734 735 736 737 738 739 740 741 742 743 744 745 746
			}
			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) {
747
		reiserfs_warning(inode->i_sb, "jdm-20002",
748 749 750 751 752
				 "Invalid hash for xattr (%s) associated "
				 "with %k", name, INODE_PKEY(inode));
		err = -EIO;
	}

753 754
out_unlock:
	up_read(&REISERFS_I(inode)->i_xattr_sem);
755
	dput(dentry);
756

757
out:
758
	return err;
Linus Torvalds's avatar
Linus Torvalds committed
759 760
}

761 762 763 764 765 766 767 768 769 770 771 772 773
/*
 * 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)++)
Linus Torvalds's avatar
Linus Torvalds committed
774

775 776 777 778 779 780 781
static inline bool reiserfs_posix_acl_list(const char *name,
					   struct dentry *dentry)
{
	return (posix_acl_type(name) >= 0) &&
	       IS_POSIXACL(d_backing_inode(dentry));
}

782
/* This is the implementation for the xattr plugin infrastructure */
783 784
static inline bool reiserfs_xattr_list(const struct xattr_handler **handlers,
				       const char *name, struct dentry *dentry)
Linus Torvalds's avatar
Linus Torvalds committed
785
{
786 787
	if (handlers) {
		const struct xattr_handler *xah = NULL;
788

789 790
		for_each_xattr_handler(handlers, xah) {
			const char *prefix = xattr_prefix(xah);
791

792 793 794 795 796 797 798 799
			if (strncmp(prefix, name, strlen(prefix)))
				continue;

			if (!xattr_handler_can_list(xah, dentry))
				return false;

			return true;
		}
800 801
	}

802
	return reiserfs_posix_acl_list(name, dentry);
803
}
Linus Torvalds's avatar
Linus Torvalds committed
804

805
struct listxattr_buf {
Al Viro's avatar
Al Viro committed
806
	struct dir_context ctx;
807 808 809
	size_t size;
	size_t pos;
	char *buf;
810
	struct dentry *dentry;
Linus Torvalds's avatar
Linus Torvalds committed
811 812
};

813
static bool listxattr_filler(struct dir_context *ctx, const char *name,
814 815
			    int namelen, loff_t offset, u64 ino,
			    unsigned int d_type)
Linus Torvalds's avatar
Linus Torvalds committed
816
{
817 818
	struct listxattr_buf *b =
		container_of(ctx, struct listxattr_buf, ctx);
819
	size_t size;
820

821 822
	if (name[0] != '.' ||
	    (namelen != 1 && (name[1] != '.' || namelen != 2))) {
823 824
		if (!reiserfs_xattr_list(b->dentry->d_sb->s_xattr, name,
					 b->dentry))
825
			return true;
826
		size = namelen + 1;
827
		if (b->buf) {
828 829
			if (b->pos + size > b->size) {
				b->pos = -ERANGE;
830
				return false;
831
			}
832 833
			memcpy(b->buf + b->pos, name, namelen);
			b->buf[b->pos + namelen] = 0;
834
		}
835 836
		b->pos += size;
	}
837
	return true;
Linus Torvalds's avatar
Linus Torvalds committed
838
}
839

Linus Torvalds's avatar
Linus Torvalds committed
840 841 842
/*
 * Inode operation listxattr()
 *
843 844 845
 * 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.
Linus Torvalds's avatar
Linus Torvalds committed
846
 */
847
ssize_t reiserfs_listxattr(struct dentry * dentry, char *buffer, size_t size)
Linus Torvalds's avatar
Linus Torvalds committed
848
{
849 850
	struct dentry *dir;
	int err = 0;
851
	struct listxattr_buf buf = {
Al Viro's avatar
Al Viro committed
852
		.ctx.actor = listxattr_filler,
853
		.dentry = dentry,
854 855 856
		.buf = buffer,
		.size = buffer ? size : 0,
	};
857

858
	if (d_really_is_negative(dentry))
859 860
		return -EINVAL;

861
	if (get_inode_sd_version(d_inode(dentry)) == STAT_DATA_V1)
862 863
		return -EOPNOTSUPP;

864
	dir = open_xa_dir(d_inode(dentry), XATTR_REPLACE);
865 866 867
	if (IS_ERR(dir)) {
		err = PTR_ERR(dir);
		if (err == -ENODATA)
868
			err = 0;  /* Not an error if there aren't any xattrs */
869 870 871
		goto out;
	}

Al Viro's avatar
Al Viro committed
872
	inode_lock_nested(d_inode(dir), I_MUTEX_XATTR);
873
	err = reiserfs_readdir_inode(d_inode(dir), &buf.ctx);
Al Viro's avatar
Al Viro committed
874
	inode_unlock(d_inode(dir));
875

876 877
	if (!err)
		err = buf.pos;
878

879
	dput(dir);
880
out:
881
	return err;
Linus Torvalds's avatar
Linus Torvalds committed
882 883
}

884
static int create_privroot(struct dentry *dentry)
Linus Torvalds's avatar
Linus Torvalds committed
885
{
886
	int err;
887
	struct inode *inode = d_inode(dentry->d_parent);
888

Al Viro's avatar
Al Viro committed
889
	WARN_ON_ONCE(!inode_is_locked(inode));
890

891
	err = xattr_mkdir(inode, dentry, 0700);
892
	if (err || d_really_is_negative(dentry)) {
893 894 895 896 897
		reiserfs_warning(dentry->d_sb, "jdm-20006",
				 "xattrs/ACLs enabled and couldn't "
				 "find/create .reiserfs_priv. "
				 "Failing mount.");
		return -EOPNOTSUPP;
898 899
	}

900
	reiserfs_init_priv_inode(d_inode(dentry));
901 902
	reiserfs_info(dentry->d_sb, "Created %s - reserved for xattr "
		      "storage.\n", PRIVROOT_NAME);
903

904
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
905 906
}

Jeff Mahoney's avatar
Jeff Mahoney committed
907 908 909 910 911 912 913
#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 */
914
const struct xattr_handler *reiserfs_xattr_handlers[] = {
Jeff Mahoney's avatar
Jeff Mahoney committed
915 916 917 918 919 920 921 922 923 924
#ifdef CONFIG_REISERFS_FS_XATTR
	&reiserfs_xattr_user_handler,
	&reiserfs_xattr_trusted_handler,
#endif
#ifdef CONFIG_REISERFS_FS_SECURITY
	&reiserfs_xattr_security_handler,
#endif
	NULL
};

925
static int xattr_mount_check(struct super_block *s)
Linus Torvalds's avatar
Linus Torvalds committed
926
{
927 928 929 930
	/*
	 * We need generation numbers to ensure that the oid mapping is correct
	 * v3.5 filesystems don't have them.
	 */
931 932
	if (old_format_only(s)) {
		if (reiserfs_xattrs_optional(s)) {
933 934 935 936
			/*
			 * Old format filesystem, but optional xattrs have
			 * been enabled. Error out.
			 */
937 938 939 940 941 942
			reiserfs_warning(s, "jdm-2005",
					 "xattrs/ACLs not supported "
					 "on pre-v3.6 format filesystems. "
					 "Failing mount.");
			return -EOPNOTSUPP;
		}
943 944 945
	}

	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
946 947
}

948
int reiserfs_permission(struct mnt_idmap *idmap, struct inode *inode,
949
			int mask)
950 951 952 953 954 955 956 957
{
	/*
	 * We don't do permission checks on the internal objects.
	 * Permissions are determined by the "owning" object.
	 */
	if (IS_PRIVATE(inode))
		return 0;

958
	return generic_permission(&nop_mnt_idmap, inode, mask);
959 960
}

961
static int xattr_hide_revalidate(struct dentry *dentry, unsigned int flags)
Linus Torvalds's avatar
Linus Torvalds committed
962
{
963
	return -EPERM;
Linus Torvalds's avatar
Linus Torvalds committed
964 965
}

966
static const struct dentry_operations xattr_lookup_poison_ops = {
967
	.d_revalidate = xattr_hide_revalidate,
Linus Torvalds's avatar
Linus Torvalds committed
968 969
};

970 971 972 973 974 975
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 */
Al Viro's avatar
Al Viro committed
976
	inode_lock(d_inode(s->s_root));
977 978 979 980
	dentry = lookup_one_len(PRIVROOT_NAME, s->s_root,
				strlen(PRIVROOT_NAME));
	if (!IS_ERR(dentry)) {
		REISERFS_SB(s)->priv_root = dentry;
981
		d_set_d_op(dentry, &xattr_lookup_poison_ops);
982 983
		if (d_really_is_positive(dentry))
			reiserfs_init_priv_inode(d_inode(dentry));
984 985
	} else
		err = PTR_ERR(dentry);
Al Viro's avatar
Al Viro committed
986
	inode_unlock(d_inode(s->s_root));
987 988 989 990

	return err;
}

991 992
/*
 * We need to take a copy of the mount flags since things like
993
 * SB_RDONLY don't get set until *after* we're called.
994 995
 * mount_flags != mount_options
 */
996
int reiserfs_xattr_init(struct super_block *s, int mount_flags)
Linus Torvalds's avatar
Linus Torvalds committed
997
{
998
	int err = 0;
999
	struct dentry *privroot = REISERFS_SB(s)->priv_root;
1000

1001 1002
	err = xattr_mount_check(s);
	if (err)
1003 1004
		goto error;

1005
	if (d_really_is_negative(privroot) && !(mount_flags & SB_RDONLY)) {
Al Viro's avatar
Al Viro committed
1006
		inode_lock(d_inode(s->s_root));
1007
		err = create_privroot(REISERFS_SB(s)->priv_root);
Al Viro's avatar
Al Viro committed
1008
		inode_unlock(d_inode(s->s_root));
1009
	}
1010

1011
	if (d_really_is_positive(privroot)) {
Al Viro's avatar
Al Viro committed
1012
		inode_lock(d_inode(privroot));
1013 1014
		if (!REISERFS_SB(s)->xattr_root) {
			struct dentry *dentry;
1015

1016 1017 1018 1019 1020 1021 1022
			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);
		}
Al Viro's avatar
Al Viro committed
1023
		inode_unlock(d_inode(privroot));
1024
	}
1025

1026
error:
1027
	if (err) {
1028 1029
		clear_bit(REISERFS_XATTRS_USER, &REISERFS_SB(s)->s_mount_opt);
		clear_bit(REISERFS_POSIXACL, &REISERFS_SB(s)->s_mount_opt);
1030 1031
	}

1032
	/* The super_block SB_POSIXACL must mirror the (no)acl mount option. */
1033
	if (reiserfs_posixacl(s))
1034
		s->s_flags |= SB_POSIXACL;
1035
	else
1036
		s->s_flags &= ~SB_POSIXACL;
1037 1038

	return err;
Linus Torvalds's avatar
Linus Torvalds committed
1039
}