xattr.c 32.4 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 30 31 32
/*
 * 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.
 */

#include <linux/reiserfs_fs.h>
33
#include <linux/capability.h>
L
Linus Torvalds 已提交
34 35 36 37 38 39 40 41 42 43
#include <linux/dcache.h>
#include <linux/namei.h>
#include <linux/errno.h>
#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>
44
#include <net/checksum.h>
L
Linus Torvalds 已提交
45 46
#include <linux/smp_lock.h>
#include <linux/stat.h>
47
#include <linux/quotaops.h>
L
Linus Torvalds 已提交
48 49 50 51

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

52 53 54 55
/* 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. */
static int xattr_create(struct inode *dir, struct dentry *dentry, int mode)
L
Linus Torvalds 已提交
56
{
57 58 59 60
	BUG_ON(!mutex_is_locked(&dir->i_mutex));
	DQUOT_INIT(dir);
	return dir->i_op->create(dir, dentry, mode, NULL);
}
61

62 63 64 65 66 67
static int xattr_mkdir(struct inode *dir, struct dentry *dentry, int mode)
{
	BUG_ON(!mutex_is_locked(&dir->i_mutex));
	DQUOT_INIT(dir);
	return dir->i_op->mkdir(dir, dentry, mode);
}
68

69 70 71 72 73 74 75 76 77
/* 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));
	DQUOT_INIT(dir);
78

79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
	mutex_lock_nested(&dentry->d_inode->i_mutex, I_MUTEX_CHILD);
	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));
	DQUOT_INIT(dir);

	mutex_lock_nested(&dentry->d_inode->i_mutex, I_MUTEX_CHILD);
	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)

/* Returns and possibly creates the xattr dir. */
static struct dentry *lookup_or_create_dir(struct dentry *parent,
					    const char *name, int flags)
{
	struct dentry *dentry;
	BUG_ON(!parent);

	dentry = lookup_one_len(name, parent, strlen(name));
	if (IS_ERR(dentry))
		return dentry;
	else if (!dentry->d_inode) {
121
		int err = -ENODATA;
122 123 124 125 126 127 128 129

		if (xattr_may_create(flags)) {
			mutex_lock_nested(&parent->d_inode->i_mutex,
					  I_MUTEX_XATTR);
			err = xattr_mkdir(parent->d_inode, dentry, 0700);
			mutex_unlock(&parent->d_inode->i_mutex);
		}

130
		if (err) {
131 132
			dput(dentry);
			dentry = ERR_PTR(err);
133
		}
134 135
	}

136 137 138 139 140 141 142 143 144
	return dentry;
}

static struct dentry *open_xa_root(struct super_block *sb, int flags)
{
	struct dentry *privroot = REISERFS_SB(sb)->priv_root;
	if (!privroot)
		return ERR_PTR(-ENODATA);
	return lookup_or_create_dir(privroot, XAROOT_NAME, flags);
L
Linus Torvalds 已提交
145 146
}

147
static struct dentry *open_xa_dir(const struct inode *inode, int flags)
L
Linus Torvalds 已提交
148
{
149 150 151
	struct dentry *xaroot, *xadir;
	char namebuf[17];

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

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

160
	xadir = lookup_or_create_dir(xaroot, namebuf, flags);
161 162
	dput(xaroot);
	return xadir;
163

L
Linus Torvalds 已提交
164 165 166 167 168 169 170 171 172 173 174 175
}

/*
 * this is very similar to fs/reiserfs/dir.c:reiserfs_readdir, but
 * we need to drop the path before calling the filldir struct.  That
 * would be a big performance hit to the non-xattr case, so I've copied
 * the whole thing for now. --clm
 *
 * the big difference is that I go backwards through the directory,
 * and don't mess with f->f_pos, but the idea is the same.  Do some
 * action on each and every entry in the directory.
 *
176
 * we're called with i_mutex held, so there are no worries about the directory
L
Linus Torvalds 已提交
177 178
 * changing underneath us.
 */
179
static int __xattr_readdir(struct inode *inode, void *dirent, filldir_t filldir)
L
Linus Torvalds 已提交
180
{
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
	struct cpu_key pos_key;	/* key of current position in the directory (key of directory entry) */
	INITIALIZE_PATH(path_to_entry);
	struct buffer_head *bh;
	int entry_num;
	struct item_head *ih, tmp_ih;
	int search_res;
	char *local_buf;
	loff_t next_pos;
	char small_buf[32];	/* avoid kmalloc if we can */
	struct reiserfs_de_head *deh;
	int d_reclen;
	char *d_name;
	off_t d_off;
	ino_t d_ino;
	struct reiserfs_dir_entry de;

	/* form key for search the next directory entry using f_pos field of
	   file structure */
	next_pos = max_reiserfs_offset(inode);

	while (1) {
	      research:
		if (next_pos <= DOT_DOT_OFFSET)
			break;
		make_cpu_key(&pos_key, inode, next_pos, TYPE_DIRENTRY, 3);

		search_res =
		    search_by_entry_key(inode->i_sb, &pos_key, &path_to_entry,
					&de);
		if (search_res == IO_ERROR) {
			// FIXME: we could just skip part of directory which could
			// not be read
			pathrelse(&path_to_entry);
			return -EIO;
		}
L
Linus Torvalds 已提交
216

217 218
		if (search_res == NAME_NOT_FOUND)
			de.de_entry_num--;
L
Linus Torvalds 已提交
219

220 221 222
		set_de_name_and_namelen(&de);
		entry_num = de.de_entry_num;
		deh = &(de.de_deh[entry_num]);
L
Linus Torvalds 已提交
223

224 225
		bh = de.de_bh;
		ih = de.de_ih;
L
Linus Torvalds 已提交
226

227
		if (!is_direntry_le_ih(ih)) {
J
Jeff Mahoney 已提交
228 229
			reiserfs_error(inode->i_sb, "jdm-20000",
				       "not direntry %h", ih);
230 231 232
			break;
		}
		copy_item_head(&tmp_ih, ih);
L
Linus Torvalds 已提交
233

234 235 236 237
		/* we must have found item, that is item of this directory, */
		RFALSE(COMP_SHORT_KEYS(&(ih->ih_key), &pos_key),
		       "vs-9000: found item %h does not match to dir we readdir %K",
		       ih, &pos_key);
L
Linus Torvalds 已提交
238

239 240 241
		if (deh_offset(deh) <= DOT_DOT_OFFSET) {
			break;
		}
L
Linus Torvalds 已提交
242

243 244
		/* look for the previous entry in the directory */
		next_pos = deh_offset(deh) - 1;
L
Linus Torvalds 已提交
245

246 247 248
		if (!de_visible(deh))
			/* it is hidden entry */
			continue;
L
Linus Torvalds 已提交
249

250 251 252 253
		d_reclen = entry_length(bh, ih, entry_num);
		d_name = B_I_DEH_ENTRY_FILE_NAME(bh, ih, deh);
		d_off = deh_offset(deh);
		d_ino = deh_objectid(deh);
L
Linus Torvalds 已提交
254

255 256
		if (!d_name[d_reclen - 1])
			d_reclen = strlen(d_name);
L
Linus Torvalds 已提交
257

258 259 260 261
		if (d_reclen > REISERFS_MAX_NAME(inode->i_sb->s_blocksize)) {
			/* too big to send back to VFS */
			continue;
		}
L
Linus Torvalds 已提交
262

263 264 265 266 267 268 269 270 271 272 273 274
		/* Ignore the .reiserfs_priv entry */
		if (reiserfs_xattrs(inode->i_sb) &&
		    !old_format_only(inode->i_sb) &&
		    deh_objectid(deh) ==
		    le32_to_cpu(INODE_PKEY
				(REISERFS_SB(inode->i_sb)->priv_root->d_inode)->
				k_objectid))
			continue;

		if (d_reclen <= 32) {
			local_buf = small_buf;
		} else {
275
			local_buf = kmalloc(d_reclen, GFP_NOFS);
276 277 278 279 280
			if (!local_buf) {
				pathrelse(&path_to_entry);
				return -ENOMEM;
			}
			if (item_moved(&tmp_ih, &path_to_entry)) {
281
				kfree(local_buf);
282 283 284 285 286 287

				/* sigh, must retry.  Do this same offset again */
				next_pos = d_off;
				goto research;
			}
		}
L
Linus Torvalds 已提交
288

289 290 291 292 293
		// Note, that we copy name to user space via temporary
		// buffer (local_buf) because filldir will block if
		// user space buffer is swapped out. At that time
		// entry can move to somewhere else
		memcpy(local_buf, d_name, d_reclen);
L
Linus Torvalds 已提交
294

295 296 297 298 299 300 301 302 303
		/* the filldir function might need to start transactions,
		 * or do who knows what.  Release the path now that we've
		 * copied all the important stuff out of the deh
		 */
		pathrelse(&path_to_entry);

		if (filldir(dirent, local_buf, d_reclen, d_off, d_ino,
			    DT_UNKNOWN) < 0) {
			if (local_buf != small_buf) {
304
				kfree(local_buf);
305 306 307 308
			}
			goto end;
		}
		if (local_buf != small_buf) {
309
			kfree(local_buf);
310 311
		}
	}			/* while */
L
Linus Torvalds 已提交
312

313 314 315
      end:
	pathrelse(&path_to_entry);
	return 0;
L
Linus Torvalds 已提交
316 317 318 319 320 321 322 323 324
}

/*
 * this could be done with dedicated readdir ops for the xattr files,
 * but I want to get something working asap
 * this is stolen from vfs_readdir
 *
 */
static
325
int xattr_readdir(struct inode *inode, filldir_t filler, void *buf)
L
Linus Torvalds 已提交
326
{
327
	int res = -ENOENT;
328 329
	if (!IS_DEADDIR(inode)) {
		lock_kernel();
330
		res = __xattr_readdir(inode, buf, filler);
331 332 333
		unlock_kernel();
	}
	return res;
L
Linus Torvalds 已提交
334 335
}

336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
static int
__reiserfs_xattr_del(struct dentry *xadir, const char *name, int namelen)
{
	struct dentry *dentry;
	struct inode *dir = xadir->d_inode;
	int err = 0;

	dentry = lookup_one_len(name, xadir, namelen);
	if (IS_ERR(dentry)) {
		err = PTR_ERR(dentry);
		goto out;
	} else if (!dentry->d_inode) {
		err = -ENODATA;
		goto out_file;
	}

	/* Skip directories.. */
	if (S_ISDIR(dentry->d_inode->i_mode))
		goto out_file;

	if (!IS_PRIVATE(dentry->d_inode)) {
		reiserfs_error(dir->i_sb, "jdm-20003",
			       "OID %08x [%.*s/%.*s] doesn't have "
			       "priv flag set [parent is %sset].",
			       le32_to_cpu(INODE_PKEY(dentry->d_inode)->
					   k_objectid), xadir->d_name.len,
			       xadir->d_name.name, namelen, name,
			       IS_PRIVATE(xadir->d_inode) ? "" :
			       "not ");
		dput(dentry);
		return -EIO;
	}

369
	err = xattr_unlink(dir, dentry);
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402

out_file:
	dput(dentry);

out:
	return err;
}

/* 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. */

static int
reiserfs_delete_xattrs_filler(void *buf, const char *name, int namelen,
			      loff_t offset, u64 ino, unsigned int d_type)
{
	struct dentry *xadir = (struct dentry *)buf;

	return __reiserfs_xattr_del(xadir, name, namelen);

}

/* This is called w/ inode->i_mutex downed */
int reiserfs_delete_xattrs(struct inode *inode)
{
	struct dentry *dir, *root;
	int err = 0;

	/* Skip out, an xattr has no xattrs associated with it */
	if (IS_PRIVATE(inode) || get_inode_sd_version(inode) == STAT_DATA_V1)
		return 0;

	reiserfs_read_lock_xattrs(inode->i_sb);
403
	dir = open_xa_dir(inode, XATTR_REPLACE);
404 405 406 407 408 409 410 411 412
	reiserfs_read_unlock_xattrs(inode->i_sb);
	if (IS_ERR(dir)) {
		err = PTR_ERR(dir);
		goto out;
	} else if (!dir->d_inode) {
		dput(dir);
		return 0;
	}

413
	mutex_lock_nested(&dir->d_inode->i_mutex, I_MUTEX_XATTR);
414
	err = xattr_readdir(dir->d_inode, reiserfs_delete_xattrs_filler, dir);
415 416
	mutex_unlock(&dir->d_inode->i_mutex);
	if (err)
417 418 419 420
		goto out_dir;

	/* Leftovers besides . and .. -- that's not good. */
	if (dir->d_inode->i_nlink <= 2) {
421
		root = open_xa_root(inode->i_sb, XATTR_REPLACE);
422
		reiserfs_write_lock_xattrs(inode->i_sb);
423 424 425
		mutex_lock_nested(&root->d_inode->i_mutex, I_MUTEX_XATTR);
		err = xattr_rmdir(root->d_inode, dir);
		mutex_unlock(&root->d_inode->i_mutex);
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466
		reiserfs_write_unlock_xattrs(inode->i_sb);
		dput(root);
	} else {
		reiserfs_warning(inode->i_sb, "jdm-20006",
				 "Couldn't remove all entries in directory");
	}

out_dir:
	dput(dir);

out:
	if (!err)
		REISERFS_I(inode)->i_flags =
		    REISERFS_I(inode)->i_flags & ~i_has_xattr_dir;
	return err;
}

struct reiserfs_chown_buf {
	struct inode *inode;
	struct dentry *xadir;
	struct iattr *attrs;
};

/* XXX: If there is a better way to do this, I'd love to hear about it */
static int
reiserfs_chown_xattrs_filler(void *buf, const char *name, int namelen,
			     loff_t offset, u64 ino, unsigned int d_type)
{
	struct reiserfs_chown_buf *chown_buf = (struct reiserfs_chown_buf *)buf;
	struct dentry *xafile, *xadir = chown_buf->xadir;
	struct iattr *attrs = chown_buf->attrs;
	int err = 0;

	xafile = lookup_one_len(name, xadir, namelen);
	if (IS_ERR(xafile))
		return PTR_ERR(xafile);
	else if (!xafile->d_inode) {
		dput(xafile);
		return -ENODATA;
	}

467 468
	if (!S_ISDIR(xafile->d_inode->i_mode)) {
		mutex_lock_nested(&xafile->d_inode->i_mutex, I_MUTEX_CHILD);
469
		err = notify_change(xafile, attrs);
470 471
		mutex_unlock(&xafile->d_inode->i_mutex);
	}
472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488
	dput(xafile);

	return err;
}

int reiserfs_chown_xattrs(struct inode *inode, struct iattr *attrs)
{
	struct dentry *dir;
	int err = 0;
	struct reiserfs_chown_buf buf;
	unsigned int ia_valid = attrs->ia_valid;

	/* Skip out, an xattr has no xattrs associated with it */
	if (IS_PRIVATE(inode) || get_inode_sd_version(inode) == STAT_DATA_V1)
		return 0;

	reiserfs_read_lock_xattrs(inode->i_sb);
489
	dir = open_xa_dir(inode, XATTR_REPLACE);
490 491 492 493 494
	reiserfs_read_unlock_xattrs(inode->i_sb);
	if (IS_ERR(dir)) {
		if (PTR_ERR(dir) != -ENODATA)
			err = PTR_ERR(dir);
		goto out;
495 496
	} else if (!dir->d_inode)
		goto out_dir;
497 498 499 500 501 502

	attrs->ia_valid &= (ATTR_UID | ATTR_GID | ATTR_CTIME);
	buf.xadir = dir;
	buf.attrs = attrs;
	buf.inode = inode;

503
	mutex_lock_nested(&dir->d_inode->i_mutex, I_MUTEX_XATTR);
504 505
	err = xattr_readdir(dir->d_inode, reiserfs_chown_xattrs_filler, &buf);

506 507 508
	if (!err)
		err = notify_change(dir, attrs);
	mutex_unlock(&dir->d_inode->i_mutex);
509

510
	attrs->ia_valid = ia_valid;
511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530
out_dir:
	dput(dir);
out:
	return err;
}

#ifdef CONFIG_REISERFS_FS_XATTR
static struct reiserfs_xattr_handler *find_xattr_handler_prefix(const char
								*prefix);

/* 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. */
static struct dentry *get_xa_file_dentry(const struct inode *inode,
					 const char *name, int flags)
{
	struct dentry *xadir, *xafile;
	int err = 0;

	xadir = open_xa_dir(inode, flags);
531
	if (IS_ERR(xadir))
532 533 534 535
		return ERR_CAST(xadir);

	xafile = lookup_one_len(name, xadir, strlen(name));
	if (IS_ERR(xafile)) {
536 537
		err = PTR_ERR(xafile);
		goto out;
538 539
	}

540 541
	if (xafile->d_inode && (flags & XATTR_CREATE))
		err = -EEXIST;
542

543 544 545 546 547 548 549 550
	if (!xafile->d_inode) {
		err = -ENODATA;
		if (xattr_may_create(flags)) {
			mutex_lock_nested(&xadir->d_inode->i_mutex,
					  I_MUTEX_XATTR);
			err = xattr_create(xadir->d_inode, xafile,
					      0700|S_IFREG);
			mutex_unlock(&xadir->d_inode->i_mutex);
551 552 553
		}
	}

554 555
	if (err)
		dput(xafile);
556 557 558
out:
	dput(xadir);
	if (err)
559
		return ERR_PTR(err);
560 561 562
	return xafile;
}

L
Linus Torvalds 已提交
563
/* Internal operations on file data */
564
static inline void reiserfs_put_page(struct page *page)
L
Linus Torvalds 已提交
565
{
566 567
	kunmap(page);
	page_cache_release(page);
L
Linus Torvalds 已提交
568 569
}

570
static struct page *reiserfs_get_page(struct inode *dir, size_t n)
L
Linus Torvalds 已提交
571
{
572 573 574 575
	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 */
576
	mapping_set_gfp_mask(mapping, GFP_NOFS);
577
	page = read_mapping_page(mapping, n >> PAGE_CACHE_SHIFT, NULL);
578 579 580 581 582 583 584 585 586 587
	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 已提交
588 589
}

590
static inline __u32 xattr_hash(const char *msg, int len)
L
Linus Torvalds 已提交
591
{
592
	return csum_partial(msg, len, 0);
L
Linus Torvalds 已提交
593 594
}

V
Vladimir Saveliev 已提交
595 596 597 598 599 600
int reiserfs_commit_write(struct file *f, struct page *page,
			  unsigned from, unsigned to);
int reiserfs_prepare_write(struct file *f, struct page *page,
			   unsigned from, unsigned to);


L
Linus Torvalds 已提交
601 602 603
/* Generic extended attribute operations that can be used by xa plugins */

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

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

	/* Empty xattrs are ok, they're just empty files, no hash */
	if (buffer && buffer_size)
		xahash = xattr_hash(buffer, buffer_size);

626 627 628
	dentry = get_xa_file_dentry(inode, name, flags);
	if (IS_ERR(dentry)) {
		err = PTR_ERR(dentry);
629 630 631 632 633 634 635 636
		goto out;
	}

	REISERFS_I(inode)->i_flags |= i_has_xattr_dir;

	/* Resize it so we're ok to write there */
	newattrs.ia_size = buffer_size;
	newattrs.ia_valid = ATTR_SIZE | ATTR_CTIME;
J
Jeff Mahoney 已提交
637
	mutex_lock_nested(&dentry->d_inode->i_mutex, I_MUTEX_XATTR);
638
	err = notify_change(dentry, &newattrs);
639
	mutex_unlock(&dentry->d_inode->i_mutex);
640 641 642 643 644 645 646 647 648 649 650 651
	if (err)
		goto out_filp;

	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;

652
		page = reiserfs_get_page(dentry->d_inode, file_pos);
653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670
		if (IS_ERR(page)) {
			err = PTR_ERR(page);
			goto out_filp;
		}

		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);
		}

671
		err = reiserfs_prepare_write(NULL, page, page_offset,
V
Vladimir Saveliev 已提交
672
					    page_offset + chunk + skip);
673 674 675
		if (!err) {
			if (buffer)
				memcpy(data + skip, buffer + buffer_pos, chunk);
676 677 678
			err = reiserfs_commit_write(NULL, page, page_offset,
						    page_offset + chunk +
						    skip);
679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695
		}
		unlock_page(page);
		reiserfs_put_page(page);
		buffer_pos += chunk;
		file_pos += chunk;
		skip = 0;
		if (err || buffer_size == 0 || !buffer)
			break;
	}

	/* We can't mark the inode dirty if it's not hashed. This is the case
	 * when we're inheriting the default ACL. If we dirty it, the inode
	 * gets marked dirty, but won't (ever) make it onto the dirty list until
	 * it's synced explicitly to clear I_DIRTY. This is bad. */
	if (!hlist_unhashed(&inode->i_hash)) {
		inode->i_ctime = CURRENT_TIME_SEC;
		mark_inode_dirty(inode);
L
Linus Torvalds 已提交
696
	}
697 698

      out_filp:
699
	dput(dentry);
700 701 702

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

/*
706
 * inode->i_mutex: down
L
Linus Torvalds 已提交
707 708
 */
int
709 710
reiserfs_xattr_get(const struct inode *inode, const char *name, void *buffer,
		   size_t buffer_size)
L
Linus Torvalds 已提交
711
{
712
	ssize_t err = 0;
713
	struct dentry *dentry;
714 715 716 717 718 719 720 721 722 723 724 725 726 727
	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;

728
	dentry = get_xa_file_dentry(inode, name, XATTR_REPLACE);
729 730
	if (IS_ERR(dentry)) {
		err = PTR_ERR(dentry);
731 732 733
		goto out;
	}

J
Jeff Mahoney 已提交
734
	isize = i_size_read(dentry->d_inode);
735 736 737 738 739 740 741 742 743 744 745 746 747
	REISERFS_I(inode)->i_flags |= i_has_xattr_dir;

	/* Just return the size needed */
	if (buffer == NULL) {
		err = isize - sizeof(struct reiserfs_xattr_header);
		goto out_dput;
	}

	if (buffer_size < isize - sizeof(struct reiserfs_xattr_header)) {
		err = -ERANGE;
		goto out_dput;
	}

748 749 750 751 752 753 754 755
	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;
L
Linus Torvalds 已提交
756

757 758 759 760 761
		page = reiserfs_get_page(dentry->d_inode, file_pos);
		if (IS_ERR(page)) {
			err = PTR_ERR(page);
			goto out_dput;
		}
L
Linus Torvalds 已提交
762

763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790
		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);
				reiserfs_warning(inode->i_sb, "jdm-20001",
						 "Invalid magic for xattr (%s) "
						 "associated with %k", name,
						 INODE_PKEY(inode));
				err = -EIO;
				goto out_dput;
			}
			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);
791

792 793 794 795 796 797
	if (xattr_hash(buffer, isize - sizeof(struct reiserfs_xattr_header)) !=
	    hash) {
		reiserfs_warning(inode->i_sb, "jdm-20002",
				 "Invalid hash for xattr (%s) associated "
				 "with %k", name, INODE_PKEY(inode));
		err = -EIO;
798 799
	}

800 801
out_dput:
	dput(dentry);
802

803
out:
804
	return err;
L
Linus Torvalds 已提交
805 806
}

807
int reiserfs_xattr_del(struct inode *inode, const char *name)
L
Linus Torvalds 已提交
808
{
809
	struct dentry *dir;
810
	int err;
811

812
	dir = open_xa_dir(inode, XATTR_REPLACE);
813
	if (IS_ERR(dir)) {
814
		err = PTR_ERR(dir);
815 816 817
		goto out;
	}

818
	mutex_lock_nested(&dir->d_inode->i_mutex, I_MUTEX_XATTR);
819
	err = __reiserfs_xattr_del(dir, name, strlen(name));
820
	mutex_unlock(&dir->d_inode->i_mutex);
821
	dput(dir);
822

823 824 825
	if (!err) {
		inode->i_ctime = CURRENT_TIME_SEC;
		mark_inode_dirty(inode);
826 827 828 829 830
	}

      out:
	return err;
}
L
Linus Torvalds 已提交
831 832 833

/* Actual operations that are exported to VFS-land */

834
static struct reiserfs_xattr_handler *find_xattr_handler_prefix(const char *);
L
Linus Torvalds 已提交
835 836
/*
 * Inode operation getxattr()
837
 * Preliminary locking: we down dentry->d_inode->i_mutex
L
Linus Torvalds 已提交
838 839
 */
ssize_t
840 841
reiserfs_getxattr(struct dentry * dentry, const char *name, void *buffer,
		  size_t size)
L
Linus Torvalds 已提交
842
{
843 844 845 846 847 848 849 850 851 852 853 854 855
	struct reiserfs_xattr_handler *xah = find_xattr_handler_prefix(name);
	int err;

	if (!xah || !reiserfs_xattrs(dentry->d_sb) ||
	    get_inode_sd_version(dentry->d_inode) == STAT_DATA_V1)
		return -EOPNOTSUPP;

	reiserfs_read_lock_xattr_i(dentry->d_inode);
	reiserfs_read_lock_xattrs(dentry->d_sb);
	err = xah->get(dentry->d_inode, name, buffer, size);
	reiserfs_read_unlock_xattrs(dentry->d_sb);
	reiserfs_read_unlock_xattr_i(dentry->d_inode);
	return err;
L
Linus Torvalds 已提交
856 857 858 859 860
}

/*
 * Inode operation setxattr()
 *
861
 * dentry->d_inode->i_mutex down
L
Linus Torvalds 已提交
862 863
 */
int
864 865
reiserfs_setxattr(struct dentry *dentry, const char *name, const void *value,
		  size_t size, int flags)
L
Linus Torvalds 已提交
866
{
867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887
	struct reiserfs_xattr_handler *xah = find_xattr_handler_prefix(name);
	int err;
	int lock;

	if (!xah || !reiserfs_xattrs(dentry->d_sb) ||
	    get_inode_sd_version(dentry->d_inode) == STAT_DATA_V1)
		return -EOPNOTSUPP;

	reiserfs_write_lock_xattr_i(dentry->d_inode);
	lock = !has_xattr_dir(dentry->d_inode);
	if (lock)
		reiserfs_write_lock_xattrs(dentry->d_sb);
	else
		reiserfs_read_lock_xattrs(dentry->d_sb);
	err = xah->set(dentry->d_inode, name, value, size, flags);
	if (lock)
		reiserfs_write_unlock_xattrs(dentry->d_sb);
	else
		reiserfs_read_unlock_xattrs(dentry->d_sb);
	reiserfs_write_unlock_xattr_i(dentry->d_inode);
	return err;
L
Linus Torvalds 已提交
888 889 890 891 892
}

/*
 * Inode operation removexattr()
 *
893
 * dentry->d_inode->i_mutex down
L
Linus Torvalds 已提交
894
 */
895
int reiserfs_removexattr(struct dentry *dentry, const char *name)
L
Linus Torvalds 已提交
896
{
897 898
	int err;
	struct reiserfs_xattr_handler *xah = find_xattr_handler_prefix(name);
L
Linus Torvalds 已提交
899

900 901 902
	if (!xah || !reiserfs_xattrs(dentry->d_sb) ||
	    get_inode_sd_version(dentry->d_inode) == STAT_DATA_V1)
		return -EOPNOTSUPP;
L
Linus Torvalds 已提交
903

904 905
	reiserfs_write_lock_xattr_i(dentry->d_inode);
	reiserfs_read_lock_xattrs(dentry->d_sb);
L
Linus Torvalds 已提交
906

907 908 909 910 911 912
	/* Deletion pre-operation */
	if (xah->del) {
		err = xah->del(dentry->d_inode, name);
		if (err)
			goto out;
	}
L
Linus Torvalds 已提交
913

914
	err = reiserfs_xattr_del(dentry->d_inode, name);
L
Linus Torvalds 已提交
915

916 917
	dentry->d_inode->i_ctime = CURRENT_TIME_SEC;
	mark_inode_dirty(dentry->d_inode);
L
Linus Torvalds 已提交
918

919 920 921 922
      out:
	reiserfs_read_unlock_xattrs(dentry->d_sb);
	reiserfs_write_unlock_xattr_i(dentry->d_inode);
	return err;
L
Linus Torvalds 已提交
923 924 925 926 927 928 929 930
}

/* This is what filldir will use:
 * r_pos will always contain the amount of space required for the entire
 * list. If r_pos becomes larger than r_size, we need more space and we
 * return an error indicating this. If r_pos is less than r_size, then we've
 * filled the buffer successfully and we return success */
struct reiserfs_listxattr_buf {
931 932 933 934
	int r_pos;
	int r_size;
	char *r_buf;
	struct inode *r_inode;
L
Linus Torvalds 已提交
935 936 937
};

static int
938
reiserfs_listxattr_filler(void *buf, const char *name, int namelen,
939
			  loff_t offset, u64 ino, unsigned int d_type)
L
Linus Torvalds 已提交
940
{
941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964
	struct reiserfs_listxattr_buf *b = (struct reiserfs_listxattr_buf *)buf;
	int len = 0;
	if (name[0] != '.'
	    || (namelen != 1 && (name[1] != '.' || namelen != 2))) {
		struct reiserfs_xattr_handler *xah =
		    find_xattr_handler_prefix(name);
		if (!xah)
			return 0;	/* Unsupported xattr name, skip it */

		/* We call ->list() twice because the operation isn't required to just
		 * return the name back - we want to make sure we have enough space */
		len += xah->list(b->r_inode, name, namelen, NULL);

		if (len) {
			if (b->r_pos + len + 1 <= b->r_size) {
				char *p = b->r_buf + b->r_pos;
				p += xah->list(b->r_inode, name, namelen, p);
				*p++ = '\0';
			}
			b->r_pos += len + 1;
		}
	}

	return 0;
L
Linus Torvalds 已提交
965
}
966

L
Linus Torvalds 已提交
967 968 969
/*
 * Inode operation listxattr()
 *
970
 * Preliminary locking: we down dentry->d_inode->i_mutex
L
Linus Torvalds 已提交
971
 */
972
ssize_t reiserfs_listxattr(struct dentry * dentry, char *buffer, size_t size)
L
Linus Torvalds 已提交
973
{
974 975 976 977 978 979 980 981 982 983 984 985 986
	struct dentry *dir;
	int err = 0;
	struct reiserfs_listxattr_buf buf;

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

	if (!reiserfs_xattrs(dentry->d_sb) ||
	    get_inode_sd_version(dentry->d_inode) == STAT_DATA_V1)
		return -EOPNOTSUPP;

	reiserfs_read_lock_xattr_i(dentry->d_inode);
	reiserfs_read_lock_xattrs(dentry->d_sb);
987
	dir = open_xa_dir(dentry->d_inode, XATTR_REPLACE);
988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002
	reiserfs_read_unlock_xattrs(dentry->d_sb);
	if (IS_ERR(dir)) {
		err = PTR_ERR(dir);
		if (err == -ENODATA)
			err = 0;	/* Not an error if there aren't any xattrs */
		goto out;
	}

	buf.r_buf = buffer;
	buf.r_size = buffer ? size : 0;
	buf.r_pos = 0;
	buf.r_inode = dentry->d_inode;

	REISERFS_I(dentry->d_inode)->i_flags |= i_has_xattr_dir;

1003
	mutex_lock_nested(&dir->d_inode->i_mutex, I_MUTEX_XATTR);
1004
	err = xattr_readdir(dir->d_inode, reiserfs_listxattr_filler, &buf);
1005
	mutex_unlock(&dir->d_inode->i_mutex);
1006 1007 1008 1009 1010 1011 1012 1013 1014
	if (err)
		goto out_dir;

	if (buf.r_pos > buf.r_size && buffer != NULL)
		err = -ERANGE;
	else
		err = buf.r_pos;

      out_dir:
1015
	dput(dir);
1016 1017 1018 1019

      out:
	reiserfs_read_unlock_xattr_i(dentry->d_inode);
	return err;
L
Linus Torvalds 已提交
1020 1021 1022
}

/* This is the implementation for the xattr plugin infrastructure */
1023
static LIST_HEAD(xattr_handlers);
L
Linus Torvalds 已提交
1024 1025
static DEFINE_RWLOCK(handler_lock);

1026 1027
static struct reiserfs_xattr_handler *find_xattr_handler_prefix(const char
								*prefix)
L
Linus Torvalds 已提交
1028
{
1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041
	struct reiserfs_xattr_handler *xah = NULL;
	struct list_head *p;

	read_lock(&handler_lock);
	list_for_each(p, &xattr_handlers) {
		xah = list_entry(p, struct reiserfs_xattr_handler, handlers);
		if (strncmp(xah->prefix, prefix, strlen(xah->prefix)) == 0)
			break;
		xah = NULL;
	}

	read_unlock(&handler_lock);
	return xah;
L
Linus Torvalds 已提交
1042 1043
}

1044
static void __unregister_handlers(void)
L
Linus Torvalds 已提交
1045
{
1046 1047
	struct reiserfs_xattr_handler *xah;
	struct list_head *p, *tmp;
L
Linus Torvalds 已提交
1048

1049 1050 1051 1052
	list_for_each_safe(p, tmp, &xattr_handlers) {
		xah = list_entry(p, struct reiserfs_xattr_handler, handlers);
		if (xah->exit)
			xah->exit();
L
Linus Torvalds 已提交
1053

1054 1055 1056
		list_del_init(p);
	}
	INIT_LIST_HEAD(&xattr_handlers);
L
Linus Torvalds 已提交
1057 1058
}

1059
int __init reiserfs_xattr_register_handlers(void)
L
Linus Torvalds 已提交
1060
{
1061 1062 1063
	int err = 0;
	struct reiserfs_xattr_handler *xah;
	struct list_head *p;
L
Linus Torvalds 已提交
1064

1065
	write_lock(&handler_lock);
L
Linus Torvalds 已提交
1066

1067 1068 1069 1070 1071
	/* If we're already initialized, nothing to do */
	if (!list_empty(&xattr_handlers)) {
		write_unlock(&handler_lock);
		return 0;
	}
L
Linus Torvalds 已提交
1072

1073 1074 1075
	/* Add the handlers */
	list_add_tail(&user_handler.handlers, &xattr_handlers);
	list_add_tail(&trusted_handler.handlers, &xattr_handlers);
L
Linus Torvalds 已提交
1076
#ifdef CONFIG_REISERFS_FS_SECURITY
1077
	list_add_tail(&security_handler.handlers, &xattr_handlers);
L
Linus Torvalds 已提交
1078 1079
#endif
#ifdef CONFIG_REISERFS_FS_POSIX_ACL
1080 1081
	list_add_tail(&posix_acl_access_handler.handlers, &xattr_handlers);
	list_add_tail(&posix_acl_default_handler.handlers, &xattr_handlers);
L
Linus Torvalds 已提交
1082 1083
#endif

1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101
	/* Run initializers, if available */
	list_for_each(p, &xattr_handlers) {
		xah = list_entry(p, struct reiserfs_xattr_handler, handlers);
		if (xah->init) {
			err = xah->init();
			if (err) {
				list_del_init(p);
				break;
			}
		}
	}

	/* Clean up other handlers, if any failed */
	if (err)
		__unregister_handlers();

	write_unlock(&handler_lock);
	return err;
L
Linus Torvalds 已提交
1102 1103
}

1104
void reiserfs_xattr_unregister_handlers(void)
L
Linus Torvalds 已提交
1105
{
1106 1107 1108
	write_lock(&handler_lock);
	__unregister_handlers();
	write_unlock(&handler_lock);
L
Linus Torvalds 已提交
1109 1110
}

1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156
static int reiserfs_check_acl(struct inode *inode, int mask)
{
	struct posix_acl *acl;
	int error = -EAGAIN; /* do regular unix permission checks by default */

	reiserfs_read_lock_xattr_i(inode);
	reiserfs_read_lock_xattrs(inode->i_sb);

	acl = reiserfs_get_acl(inode, ACL_TYPE_ACCESS);

	reiserfs_read_unlock_xattrs(inode->i_sb);
	reiserfs_read_unlock_xattr_i(inode);

	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);
	}

	return error;
}

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;
	/*
	 * Stat data v1 doesn't support ACLs.
	 */
	if (get_inode_sd_version(inode) == STAT_DATA_V1)
		return generic_permission(inode, mask, NULL);
	else
		return generic_permission(inode, mask, reiserfs_check_acl);
}

static int create_privroot(struct dentry *dentry)
{
	int err;
	struct inode *inode = dentry->d_parent->d_inode;
	mutex_lock_nested(&inode->i_mutex, I_MUTEX_XATTR);
1157
	err = xattr_mkdir(inode, dentry, 0700);
1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198
	mutex_unlock(&inode->i_mutex);
	if (err) {
		dput(dentry);
		dentry = NULL;
	}

	if (dentry && dentry->d_inode)
		reiserfs_info(dentry->d_sb, "Created %s - reserved for xattr "
			      "storage.\n", PRIVROOT_NAME);

	return err;
}

static int xattr_mount_check(struct super_block *s)
{
	/* We need generation numbers to ensure that the oid mapping is correct
	 * v3.5 filesystems don't have them. */
	if (!old_format_only(s)) {
		set_bit(REISERFS_XATTRS, &(REISERFS_SB(s)->s_mount_opt));
	} else if (reiserfs_xattrs_optional(s)) {
		/* Old format filesystem, but optional xattrs have been enabled
		 * at mount time. Error out. */
		reiserfs_warning(s, "jdm-20005",
				 "xattrs/ACLs not supported on pre v3.6 "
				 "format filesystem. Failing mount.");
		return -EOPNOTSUPP;
	} else {
		/* Old format filesystem, but no optional xattrs have
		 * been enabled. This means we silently disable xattrs
		 * on the filesystem. */
		clear_bit(REISERFS_XATTRS, &(REISERFS_SB(s)->s_mount_opt));
	}

	return 0;
}

#else
int __init reiserfs_xattr_register_handlers(void) { return 0; }
void reiserfs_xattr_unregister_handlers(void) {}
#endif

L
Linus Torvalds 已提交
1199 1200
/* This will catch lookups from the fs root to .reiserfs_priv */
static int
1201
xattr_lookup_poison(struct dentry *dentry, struct qstr *q1, struct qstr *name)
L
Linus Torvalds 已提交
1202
{
1203 1204 1205 1206 1207 1208 1209 1210 1211
	struct dentry *priv_root = REISERFS_SB(dentry->d_sb)->priv_root;
	if (name->len == priv_root->d_name.len &&
	    name->hash == priv_root->d_name.hash &&
	    !memcmp(name->name, priv_root->d_name.name, name->len)) {
		return -ENOENT;
	} else if (q1->len == name->len &&
		   !memcmp(q1->name, name->name, name->len))
		return 0;
	return 1;
L
Linus Torvalds 已提交
1212 1213 1214
}

static struct dentry_operations xattr_lookup_poison_ops = {
1215
	.d_compare = xattr_lookup_poison,
L
Linus Torvalds 已提交
1216 1217 1218 1219 1220
};

/* 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 */
1221
int reiserfs_xattr_init(struct super_block *s, int mount_flags)
L
Linus Torvalds 已提交
1222
{
1223 1224
	int err = 0;

1225 1226 1227
#ifdef CONFIG_REISERFS_FS_XATTR
	err = xattr_mount_check(s);
	if (err)
1228
		goto error;
1229
#endif
1230 1231

	/* If we don't have the privroot located yet - go find it */
1232
	if (!REISERFS_SB(s)->priv_root) {
1233 1234 1235 1236
		struct dentry *dentry;
		dentry = lookup_one_len(PRIVROOT_NAME, s->s_root,
					strlen(PRIVROOT_NAME));
		if (!IS_ERR(dentry)) {
1237 1238 1239 1240 1241
#ifdef CONFIG_REISERFS_FS_XATTR
			if (!(mount_flags & MS_RDONLY) && !dentry->d_inode)
				err = create_privroot(dentry);
#endif
			if (!dentry->d_inode) {
1242 1243 1244 1245 1246 1247 1248 1249
				dput(dentry);
				dentry = NULL;
			}
		} else
			err = PTR_ERR(dentry);

		if (!err && dentry) {
			s->s_root->d_op = &xattr_lookup_poison_ops;
1250
			dentry->d_inode->i_flags |= S_PRIVATE;
1251
			REISERFS_SB(s)->priv_root = dentry;
1252 1253 1254 1255 1256 1257 1258
#ifdef CONFIG_REISERFS_FS_XATTR
		/* xattrs are unavailable */
		} else if (!(mount_flags & MS_RDONLY)) {
			/* If we're read-only it just means that the dir
			 * hasn't been created. Not an error -- just no
			 * xattrs on the fs. We'll check again if we
			 * go read-write */
J
Jeff Mahoney 已提交
1259 1260 1261 1262
			reiserfs_warning(s, "jdm-20006",
					 "xattrs/ACLs enabled and couldn't "
					 "find/create .reiserfs_priv. "
					 "Failing mount.");
1263
			err = -EOPNOTSUPP;
1264
#endif
1265 1266 1267
		}
	}

1268 1269
#ifdef CONFIG_REISERFS_FS_XATTR
error:
1270 1271 1272 1273 1274
	if (err) {
		clear_bit(REISERFS_XATTRS, &(REISERFS_SB(s)->s_mount_opt));
		clear_bit(REISERFS_XATTRS_USER, &(REISERFS_SB(s)->s_mount_opt));
		clear_bit(REISERFS_POSIXACL, &(REISERFS_SB(s)->s_mount_opt));
	}
1275
#endif
1276 1277 1278

	/* The super_block MS_POSIXACL must mirror the (no)acl mount option. */
	s->s_flags = s->s_flags & ~MS_POSIXACL;
1279
#ifdef CONFIG_REISERFS_FS_POSIX_ACL
1280 1281
	if (reiserfs_posixacl(s))
		s->s_flags |= MS_POSIXACL;
1282
#endif
1283 1284

	return err;
L
Linus Torvalds 已提交
1285
}