Blame view

fs/reiserfs/xattr.c 25.4 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
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.
d984561b3   Jeff Mahoney   reiserfs: elimina...
30
31
   *
   * Locking works like so:
8b6dd72a4   Jeff Mahoney   reiserfs: make pe...
32
33
   * Directory components (xattr root, xattr dir) are protectd by their i_mutex.
   * The xattrs themselves are protected by the xattr_sem.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
34
   */
f466c6fdb   Al Viro   move private bits...
35
  #include "reiserfs.h"
16f7e0fe2   Randy Dunlap   [PATCH] capable/c...
36
  #include <linux/capability.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
37
38
39
  #include <linux/dcache.h>
  #include <linux/namei.h>
  #include <linux/errno.h>
5a0e3ad6a   Tejun Heo   include cleanup: ...
40
  #include <linux/gfp.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
41
42
43
44
  #include <linux/fs.h>
  #include <linux/file.h>
  #include <linux/pagemap.h>
  #include <linux/xattr.h>
c45ac8887   Al Viro   take private bits...
45
  #include "xattr.h"
a3063ab88   Al Viro   move reiserfs_acl...
46
  #include "acl.h"
17093991a   Fabian Frederick   fs/reiserfs: use ...
47
  #include <linux/uaccess.h>
3277c39f8   Al Viro   [NET]: Kill direc...
48
  #include <net/checksum.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
49
  #include <linux/stat.h>
6c17675e1   Jeff Mahoney   reiserfs: simplif...
50
  #include <linux/quotaops.h>
431547b3c   Christoph Hellwig   sanitize xattr ha...
51
  #include <linux/security.h>
47f70d08f   Christoph Hellwig   reiserfs: use gen...
52
  #include <linux/posix_acl_xattr.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
53

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
54
55
  #define PRIVROOT_NAME ".reiserfs_priv"
  #define XAROOT_NAME   "xattrs"
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
56

098297b27   Jeff Mahoney   reiserfs: cleanup...
57
58
  /*
   * Helpers for inode ops. We do this so that we don't have all the VFS
6c17675e1   Jeff Mahoney   reiserfs: simplif...
59
   * overhead and also for proper i_mutex annotation.
098297b27   Jeff Mahoney   reiserfs: cleanup...
60
61
   * dir->i_mutex must be held for all of them.
   */
3a355cc61   Jeff Mahoney   reiserfs: xattr_c...
62
  #ifdef CONFIG_REISERFS_FS_XATTR
6c17675e1   Jeff Mahoney   reiserfs: simplif...
63
  static int xattr_create(struct inode *dir, struct dentry *dentry, int mode)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
64
  {
5955102c9   Al Viro   wrappers for ->i_...
65
  	BUG_ON(!inode_is_locked(dir));
ebfc3b49a   Al Viro   don't pass nameid...
66
  	return dir->i_op->create(dir, dentry, mode, true);
6c17675e1   Jeff Mahoney   reiserfs: simplif...
67
  }
3a355cc61   Jeff Mahoney   reiserfs: xattr_c...
68
  #endif
bd4c625c0   Linus Torvalds   reiserfs: run scr...
69

18bb1db3e   Al Viro   switch vfs_mkdir(...
70
  static int xattr_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
6c17675e1   Jeff Mahoney   reiserfs: simplif...
71
  {
5955102c9   Al Viro   wrappers for ->i_...
72
  	BUG_ON(!inode_is_locked(dir));
6c17675e1   Jeff Mahoney   reiserfs: simplif...
73
74
  	return dir->i_op->mkdir(dir, dentry, mode);
  }
bd4c625c0   Linus Torvalds   reiserfs: run scr...
75

098297b27   Jeff Mahoney   reiserfs: cleanup...
76
77
  /*
   * We use I_MUTEX_CHILD here to silence lockdep. It's safe because xattr
6c17675e1   Jeff Mahoney   reiserfs: simplif...
78
79
   * 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
098297b27   Jeff Mahoney   reiserfs: cleanup...
80
81
   * better than allocating another subclass just for this code.
   */
6c17675e1   Jeff Mahoney   reiserfs: simplif...
82
83
84
  static int xattr_unlink(struct inode *dir, struct dentry *dentry)
  {
  	int error;
f3fb9e273   Fabian Frederick   fs/reiserfs/xattr...
85

5955102c9   Al Viro   wrappers for ->i_...
86
  	BUG_ON(!inode_is_locked(dir));
bd4c625c0   Linus Torvalds   reiserfs: run scr...
87

5955102c9   Al Viro   wrappers for ->i_...
88
  	inode_lock_nested(d_inode(dentry), I_MUTEX_CHILD);
6c17675e1   Jeff Mahoney   reiserfs: simplif...
89
  	error = dir->i_op->unlink(dir, dentry);
5955102c9   Al Viro   wrappers for ->i_...
90
  	inode_unlock(d_inode(dentry));
6c17675e1   Jeff Mahoney   reiserfs: simplif...
91
92
93
94
95
96
97
98
99
  
  	if (!error)
  		d_delete(dentry);
  	return error;
  }
  
  static int xattr_rmdir(struct inode *dir, struct dentry *dentry)
  {
  	int error;
f3fb9e273   Fabian Frederick   fs/reiserfs/xattr...
100

5955102c9   Al Viro   wrappers for ->i_...
101
  	BUG_ON(!inode_is_locked(dir));
6c17675e1   Jeff Mahoney   reiserfs: simplif...
102

5955102c9   Al Viro   wrappers for ->i_...
103
  	inode_lock_nested(d_inode(dentry), I_MUTEX_CHILD);
6c17675e1   Jeff Mahoney   reiserfs: simplif...
104
105
  	error = dir->i_op->rmdir(dir, dentry);
  	if (!error)
2b0143b5c   David Howells   VFS: normal files...
106
  		d_inode(dentry)->i_flags |= S_DEAD;
5955102c9   Al Viro   wrappers for ->i_...
107
  	inode_unlock(d_inode(dentry));
6c17675e1   Jeff Mahoney   reiserfs: simplif...
108
109
  	if (!error)
  		d_delete(dentry);
6c17675e1   Jeff Mahoney   reiserfs: simplif...
110
111
112
  
  	return error;
  }
6c17675e1   Jeff Mahoney   reiserfs: simplif...
113
  #define xattr_may_create(flags)	(!flags || flags & XATTR_CREATE)
ab17c4f02   Jeff Mahoney   reiserfs: fixup x...
114
  static struct dentry *open_xa_root(struct super_block *sb, int flags)
6c17675e1   Jeff Mahoney   reiserfs: simplif...
115
  {
ab17c4f02   Jeff Mahoney   reiserfs: fixup x...
116
117
  	struct dentry *privroot = REISERFS_SB(sb)->priv_root;
  	struct dentry *xaroot;
f3fb9e273   Fabian Frederick   fs/reiserfs/xattr...
118

2b0143b5c   David Howells   VFS: normal files...
119
  	if (d_really_is_negative(privroot))
ab17c4f02   Jeff Mahoney   reiserfs: fixup x...
120
  		return ERR_PTR(-ENODATA);
6c17675e1   Jeff Mahoney   reiserfs: simplif...
121

5955102c9   Al Viro   wrappers for ->i_...
122
  	inode_lock_nested(d_inode(privroot), I_MUTEX_XATTR);
6c17675e1   Jeff Mahoney   reiserfs: simplif...
123

ab17c4f02   Jeff Mahoney   reiserfs: fixup x...
124
  	xaroot = dget(REISERFS_SB(sb)->xattr_root);
ceb5edc45   Jeff Mahoney   reiserfs: deal wi...
125
126
  	if (!xaroot)
  		xaroot = ERR_PTR(-ENODATA);
2b0143b5c   David Howells   VFS: normal files...
127
  	else if (d_really_is_negative(xaroot)) {
ab17c4f02   Jeff Mahoney   reiserfs: fixup x...
128
  		int err = -ENODATA;
f3fb9e273   Fabian Frederick   fs/reiserfs/xattr...
129

5a6059c35   Jeff Mahoney   reiserfs: Expand ...
130
  		if (xattr_may_create(flags))
2b0143b5c   David Howells   VFS: normal files...
131
  			err = xattr_mkdir(d_inode(privroot), xaroot, 0700);
9b7f37550   Jeff Mahoney   reiserfs: fix xat...
132
  		if (err) {
ab17c4f02   Jeff Mahoney   reiserfs: fixup x...
133
134
  			dput(xaroot);
  			xaroot = ERR_PTR(err);
9b7f37550   Jeff Mahoney   reiserfs: fix xat...
135
  		}
bd4c625c0   Linus Torvalds   reiserfs: run scr...
136
  	}
6c17675e1   Jeff Mahoney   reiserfs: simplif...
137

5955102c9   Al Viro   wrappers for ->i_...
138
  	inode_unlock(d_inode(privroot));
ab17c4f02   Jeff Mahoney   reiserfs: fixup x...
139
  	return xaroot;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
140
  }
bd4c625c0   Linus Torvalds   reiserfs: run scr...
141
  static struct dentry *open_xa_dir(const struct inode *inode, int flags)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
142
  {
bd4c625c0   Linus Torvalds   reiserfs: run scr...
143
144
  	struct dentry *xaroot, *xadir;
  	char namebuf[17];
6c17675e1   Jeff Mahoney   reiserfs: simplif...
145
  	xaroot = open_xa_root(inode->i_sb, flags);
9b7f37550   Jeff Mahoney   reiserfs: fix xat...
146
  	if (IS_ERR(xaroot))
bd4c625c0   Linus Torvalds   reiserfs: run scr...
147
  		return xaroot;
bd4c625c0   Linus Torvalds   reiserfs: run scr...
148

bd4c625c0   Linus Torvalds   reiserfs: run scr...
149
150
151
  	snprintf(namebuf, sizeof(namebuf), "%X.%X",
  		 le32_to_cpu(INODE_PKEY(inode)->k_objectid),
  		 inode->i_generation);
bd4c625c0   Linus Torvalds   reiserfs: run scr...
152

5955102c9   Al Viro   wrappers for ->i_...
153
  	inode_lock_nested(d_inode(xaroot), I_MUTEX_XATTR);
ab17c4f02   Jeff Mahoney   reiserfs: fixup x...
154
155
  
  	xadir = lookup_one_len(namebuf, xaroot, strlen(namebuf));
2b0143b5c   David Howells   VFS: normal files...
156
  	if (!IS_ERR(xadir) && d_really_is_negative(xadir)) {
ab17c4f02   Jeff Mahoney   reiserfs: fixup x...
157
  		int err = -ENODATA;
f3fb9e273   Fabian Frederick   fs/reiserfs/xattr...
158

ab17c4f02   Jeff Mahoney   reiserfs: fixup x...
159
  		if (xattr_may_create(flags))
2b0143b5c   David Howells   VFS: normal files...
160
  			err = xattr_mkdir(d_inode(xaroot), xadir, 0700);
ab17c4f02   Jeff Mahoney   reiserfs: fixup x...
161
162
163
164
165
  		if (err) {
  			dput(xadir);
  			xadir = ERR_PTR(err);
  		}
  	}
5955102c9   Al Viro   wrappers for ->i_...
166
  	inode_unlock(d_inode(xaroot));
bd4c625c0   Linus Torvalds   reiserfs: run scr...
167
168
  	dput(xaroot);
  	return xadir;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
169
  }
098297b27   Jeff Mahoney   reiserfs: cleanup...
170
171
  /*
   * The following are side effects of other operations that aren't explicitly
48b32a355   Jeff Mahoney   reiserfs: use gen...
172
   * modifying extended attributes. This includes operations such as permissions
098297b27   Jeff Mahoney   reiserfs: cleanup...
173
174
   * or ownership changes, object deletions, etc.
   */
a41f1a471   Jeff Mahoney   reiserfs: use gen...
175
  struct reiserfs_dentry_buf {
4acf381e1   Al Viro   [readdir] convert...
176
  	struct dir_context ctx;
a41f1a471   Jeff Mahoney   reiserfs: use gen...
177
178
179
180
  	struct dentry *xadir;
  	int count;
  	struct dentry *dentries[8];
  };
bd4c625c0   Linus Torvalds   reiserfs: run scr...
181

a72bdb1cd   Jeff Mahoney   reiserfs: Clean u...
182
  static int
ac7576f4b   Miklos Szeredi   vfs: make first a...
183
184
  fill_with_dentries(struct dir_context *ctx, const char *name, int namelen,
  		   loff_t offset, u64 ino, unsigned int d_type)
a72bdb1cd   Jeff Mahoney   reiserfs: Clean u...
185
  {
ac7576f4b   Miklos Szeredi   vfs: make first a...
186
187
  	struct reiserfs_dentry_buf *dbuf =
  		container_of(ctx, struct reiserfs_dentry_buf, ctx);
a72bdb1cd   Jeff Mahoney   reiserfs: Clean u...
188
  	struct dentry *dentry;
f3fb9e273   Fabian Frederick   fs/reiserfs/xattr...
189

5955102c9   Al Viro   wrappers for ->i_...
190
  	WARN_ON_ONCE(!inode_is_locked(d_inode(dbuf->xadir)));
bd4c625c0   Linus Torvalds   reiserfs: run scr...
191

a41f1a471   Jeff Mahoney   reiserfs: use gen...
192
193
  	if (dbuf->count == ARRAY_SIZE(dbuf->dentries))
  		return -ENOSPC;
bd4c625c0   Linus Torvalds   reiserfs: run scr...
194

35e5cbc0a   Jan Kara   reiserfs: Fix war...
195
196
  	if (name[0] == '.' && (namelen < 2 ||
  			       (namelen == 2 && name[1] == '.')))
a41f1a471   Jeff Mahoney   reiserfs: use gen...
197
  		return 0;
bd4c625c0   Linus Torvalds   reiserfs: run scr...
198

a41f1a471   Jeff Mahoney   reiserfs: use gen...
199
  	dentry = lookup_one_len(name, dbuf->xadir, namelen);
a72bdb1cd   Jeff Mahoney   reiserfs: Clean u...
200
  	if (IS_ERR(dentry)) {
a41f1a471   Jeff Mahoney   reiserfs: use gen...
201
  		return PTR_ERR(dentry);
2b0143b5c   David Howells   VFS: normal files...
202
  	} else if (d_really_is_negative(dentry)) {
a41f1a471   Jeff Mahoney   reiserfs: use gen...
203
204
  		/* A directory entry exists, but no file? */
  		reiserfs_error(dentry->d_sb, "xattr-20003",
a455589f1   Al Viro   assorted conversi...
205
206
207
208
  			       "Corrupted directory: xattr %pd listed but "
  			       "not found for file %pd.
  ",
  			       dentry, dbuf->xadir);
a41f1a471   Jeff Mahoney   reiserfs: use gen...
209
210
  		dput(dentry);
  		return -EIO;
bd4c625c0   Linus Torvalds   reiserfs: run scr...
211
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
212

a41f1a471   Jeff Mahoney   reiserfs: use gen...
213
214
  	dbuf->dentries[dbuf->count++] = dentry;
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
215
  }
a41f1a471   Jeff Mahoney   reiserfs: use gen...
216
217
  static void
  cleanup_dentry_buf(struct reiserfs_dentry_buf *buf)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
218
  {
a41f1a471   Jeff Mahoney   reiserfs: use gen...
219
  	int i;
f3fb9e273   Fabian Frederick   fs/reiserfs/xattr...
220

a41f1a471   Jeff Mahoney   reiserfs: use gen...
221
222
223
  	for (i = 0; i < buf->count; i++)
  		if (buf->dentries[i])
  			dput(buf->dentries[i]);
a72bdb1cd   Jeff Mahoney   reiserfs: Clean u...
224
  }
a41f1a471   Jeff Mahoney   reiserfs: use gen...
225
226
227
  static int reiserfs_for_each_xattr(struct inode *inode,
  				   int (*action)(struct dentry *, void *),
  				   void *data)
a72bdb1cd   Jeff Mahoney   reiserfs: Clean u...
228
  {
a41f1a471   Jeff Mahoney   reiserfs: use gen...
229
230
  	struct dentry *dir;
  	int i, err = 0;
a41f1a471   Jeff Mahoney   reiserfs: use gen...
231
  	struct reiserfs_dentry_buf buf = {
4acf381e1   Al Viro   [readdir] convert...
232
  		.ctx.actor = fill_with_dentries,
a41f1a471   Jeff Mahoney   reiserfs: use gen...
233
  	};
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
234

a72bdb1cd   Jeff Mahoney   reiserfs: Clean u...
235
236
237
  	/* Skip out, an xattr has no xattrs associated with it */
  	if (IS_PRIVATE(inode) || get_inode_sd_version(inode) == STAT_DATA_V1)
  		return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
238

6c17675e1   Jeff Mahoney   reiserfs: simplif...
239
  	dir = open_xa_dir(inode, XATTR_REPLACE);
a72bdb1cd   Jeff Mahoney   reiserfs: Clean u...
240
241
242
  	if (IS_ERR(dir)) {
  		err = PTR_ERR(dir);
  		goto out;
2b0143b5c   David Howells   VFS: normal files...
243
  	} else if (d_really_is_negative(dir)) {
a41f1a471   Jeff Mahoney   reiserfs: use gen...
244
245
  		err = 0;
  		goto out_dir;
a72bdb1cd   Jeff Mahoney   reiserfs: Clean u...
246
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
247

5955102c9   Al Viro   wrappers for ->i_...
248
  	inode_lock_nested(d_inode(dir), I_MUTEX_XATTR);
27026a05b   Frederic Weisbecker   reiserfs: Fix rei...
249

a41f1a471   Jeff Mahoney   reiserfs: use gen...
250
  	buf.xadir = dir;
cd62cdae0   Al Viro   reiserfs: switch ...
251
  	while (1) {
2b0143b5c   David Howells   VFS: normal files...
252
  		err = reiserfs_readdir_inode(d_inode(dir), &buf.ctx);
cd62cdae0   Al Viro   reiserfs: switch ...
253
254
255
256
257
  		if (err)
  			break;
  		if (!buf.count)
  			break;
  		for (i = 0; !err && i < buf.count && buf.dentries[i]; i++) {
a41f1a471   Jeff Mahoney   reiserfs: use gen...
258
  			struct dentry *dentry = buf.dentries[i];
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
259

e36cb0b89   David Howells   VFS: (Scripted) C...
260
  			if (!d_is_dir(dentry))
cd62cdae0   Al Viro   reiserfs: switch ...
261
  				err = action(dentry, data);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
262

a41f1a471   Jeff Mahoney   reiserfs: use gen...
263
264
  			dput(dentry);
  			buf.dentries[i] = NULL;
bd4c625c0   Linus Torvalds   reiserfs: run scr...
265
  		}
cd62cdae0   Al Viro   reiserfs: switch ...
266
267
  		if (err)
  			break;
a41f1a471   Jeff Mahoney   reiserfs: use gen...
268
  		buf.count = 0;
8b6dd72a4   Jeff Mahoney   reiserfs: make pe...
269
  	}
5955102c9   Al Viro   wrappers for ->i_...
270
  	inode_unlock(d_inode(dir));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
271

a41f1a471   Jeff Mahoney   reiserfs: use gen...
272
  	cleanup_dentry_buf(&buf);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
273

d984561b3   Jeff Mahoney   reiserfs: elimina...
274
  	if (!err) {
098297b27   Jeff Mahoney   reiserfs: cleanup...
275
276
  		/*
  		 * We start a transaction here to avoid a ABBA situation
a41f1a471   Jeff Mahoney   reiserfs: use gen...
277
278
279
  		 * 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
098297b27   Jeff Mahoney   reiserfs: cleanup...
280
281
  		 * outer transaction.
  		 */
a41f1a471   Jeff Mahoney   reiserfs: use gen...
282
283
284
  		int blocks = JOURNAL_PER_BALANCE_CNT * 2 + 2 +
  			     4 * REISERFS_QUOTA_TRANS_BLOCKS(inode->i_sb);
  		struct reiserfs_transaction_handle th;
f3fb9e273   Fabian Frederick   fs/reiserfs/xattr...
285

4c05141df   Jeff Mahoney   reiserfs: locking...
286
  		reiserfs_write_lock(inode->i_sb);
a41f1a471   Jeff Mahoney   reiserfs: use gen...
287
  		err = journal_begin(&th, inode->i_sb, blocks);
4c05141df   Jeff Mahoney   reiserfs: locking...
288
  		reiserfs_write_unlock(inode->i_sb);
a41f1a471   Jeff Mahoney   reiserfs: use gen...
289
290
  		if (!err) {
  			int jerror;
f3fb9e273   Fabian Frederick   fs/reiserfs/xattr...
291

5955102c9   Al Viro   wrappers for ->i_...
292
  			inode_lock_nested(d_inode(dir->d_parent),
4c05141df   Jeff Mahoney   reiserfs: locking...
293
  					  I_MUTEX_XATTR);
a41f1a471   Jeff Mahoney   reiserfs: use gen...
294
  			err = action(dir, data);
4c05141df   Jeff Mahoney   reiserfs: locking...
295
  			reiserfs_write_lock(inode->i_sb);
58d854265   Jeff Mahoney   reiserfs: cleanup...
296
  			jerror = journal_end(&th);
4c05141df   Jeff Mahoney   reiserfs: locking...
297
  			reiserfs_write_unlock(inode->i_sb);
5955102c9   Al Viro   wrappers for ->i_...
298
  			inode_unlock(d_inode(dir->d_parent));
a41f1a471   Jeff Mahoney   reiserfs: use gen...
299
300
  			err = jerror ?: err;
  		}
a72bdb1cd   Jeff Mahoney   reiserfs: Clean u...
301
  	}
a41f1a471   Jeff Mahoney   reiserfs: use gen...
302
303
  out_dir:
  	dput(dir);
a72bdb1cd   Jeff Mahoney   reiserfs: Clean u...
304
  out:
a41f1a471   Jeff Mahoney   reiserfs: use gen...
305
306
307
  	/* -ENODATA isn't an error */
  	if (err == -ENODATA)
  		err = 0;
a72bdb1cd   Jeff Mahoney   reiserfs: Clean u...
308
309
  	return err;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
310

a41f1a471   Jeff Mahoney   reiserfs: use gen...
311
  static int delete_one_xattr(struct dentry *dentry, void *data)
a72bdb1cd   Jeff Mahoney   reiserfs: Clean u...
312
  {
2b0143b5c   David Howells   VFS: normal files...
313
  	struct inode *dir = d_inode(dentry->d_parent);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
314

a41f1a471   Jeff Mahoney   reiserfs: use gen...
315
  	/* This is the xattr dir, handle specially. */
e36cb0b89   David Howells   VFS: (Scripted) C...
316
  	if (d_is_dir(dentry))
a41f1a471   Jeff Mahoney   reiserfs: use gen...
317
  		return xattr_rmdir(dir, dentry);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
318

a41f1a471   Jeff Mahoney   reiserfs: use gen...
319
320
  	return xattr_unlink(dir, dentry);
  }
bd4c625c0   Linus Torvalds   reiserfs: run scr...
321

a41f1a471   Jeff Mahoney   reiserfs: use gen...
322
323
324
  static int chown_one_xattr(struct dentry *dentry, void *data)
  {
  	struct iattr *attrs = data;
4a8570112   Jeff Mahoney   reiserfs: fix pro...
325
326
327
328
329
330
331
332
333
334
335
336
337
  	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);
  	err = reiserfs_setattr(dentry, attrs);
  	attrs->ia_valid = ia_valid;
  
  	return err;
a41f1a471   Jeff Mahoney   reiserfs: use gen...
338
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
339

a41f1a471   Jeff Mahoney   reiserfs: use gen...
340
341
342
343
  /* 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);
f3fb9e273   Fabian Frederick   fs/reiserfs/xattr...
344

a41f1a471   Jeff Mahoney   reiserfs: use gen...
345
346
347
348
  	if (err)
  		reiserfs_warning(inode->i_sb, "jdm-20004",
  				 "Couldn't delete all xattrs (%d)
  ", err);
a72bdb1cd   Jeff Mahoney   reiserfs: Clean u...
349
350
  	return err;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
351

a41f1a471   Jeff Mahoney   reiserfs: use gen...
352
  /* inode->i_mutex: down */
a72bdb1cd   Jeff Mahoney   reiserfs: Clean u...
353
354
  int reiserfs_chown_xattrs(struct inode *inode, struct iattr *attrs)
  {
a41f1a471   Jeff Mahoney   reiserfs: use gen...
355
  	int err = reiserfs_for_each_xattr(inode, chown_one_xattr, attrs);
f3fb9e273   Fabian Frederick   fs/reiserfs/xattr...
356

8b6dd72a4   Jeff Mahoney   reiserfs: make pe...
357
358
359
360
  	if (err)
  		reiserfs_warning(inode->i_sb, "jdm-20007",
  				 "Couldn't chown all xattrs (%d)
  ", err);
a72bdb1cd   Jeff Mahoney   reiserfs: Clean u...
361
  	return err;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
362
  }
a72bdb1cd   Jeff Mahoney   reiserfs: Clean u...
363
  #ifdef CONFIG_REISERFS_FS_XATTR
098297b27   Jeff Mahoney   reiserfs: cleanup...
364
365
  /*
   * Returns a dentry corresponding to a specific extended attribute file
a72bdb1cd   Jeff Mahoney   reiserfs: Clean u...
366
   * for the inode. If flags allow, the file is created. Otherwise, a
098297b27   Jeff Mahoney   reiserfs: cleanup...
367
368
   * valid or negative dentry, or an error is returned.
   */
48b32a355   Jeff Mahoney   reiserfs: use gen...
369
370
  static struct dentry *xattr_lookup(struct inode *inode, const char *name,
  				    int flags)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
371
  {
a72bdb1cd   Jeff Mahoney   reiserfs: Clean u...
372
373
374
375
  	struct dentry *xadir, *xafile;
  	int err = 0;
  
  	xadir = open_xa_dir(inode, flags);
6c17675e1   Jeff Mahoney   reiserfs: simplif...
376
  	if (IS_ERR(xadir))
a72bdb1cd   Jeff Mahoney   reiserfs: Clean u...
377
  		return ERR_CAST(xadir);
a72bdb1cd   Jeff Mahoney   reiserfs: Clean u...
378

5955102c9   Al Viro   wrappers for ->i_...
379
  	inode_lock_nested(d_inode(xadir), I_MUTEX_XATTR);
a72bdb1cd   Jeff Mahoney   reiserfs: Clean u...
380
381
  	xafile = lookup_one_len(name, xadir, strlen(name));
  	if (IS_ERR(xafile)) {
6c17675e1   Jeff Mahoney   reiserfs: simplif...
382
383
  		err = PTR_ERR(xafile);
  		goto out;
bd4c625c0   Linus Torvalds   reiserfs: run scr...
384
  	}
a72bdb1cd   Jeff Mahoney   reiserfs: Clean u...
385

2b0143b5c   David Howells   VFS: normal files...
386
  	if (d_really_is_positive(xafile) && (flags & XATTR_CREATE))
6c17675e1   Jeff Mahoney   reiserfs: simplif...
387
  		err = -EEXIST;
a72bdb1cd   Jeff Mahoney   reiserfs: Clean u...
388

2b0143b5c   David Howells   VFS: normal files...
389
  	if (d_really_is_negative(xafile)) {
6c17675e1   Jeff Mahoney   reiserfs: simplif...
390
  		err = -ENODATA;
5a6059c35   Jeff Mahoney   reiserfs: Expand ...
391
  		if (xattr_may_create(flags))
2b0143b5c   David Howells   VFS: normal files...
392
  			err = xattr_create(d_inode(xadir), xafile,
6c17675e1   Jeff Mahoney   reiserfs: simplif...
393
  					      0700|S_IFREG);
a72bdb1cd   Jeff Mahoney   reiserfs: Clean u...
394
  	}
6c17675e1   Jeff Mahoney   reiserfs: simplif...
395
396
  	if (err)
  		dput(xafile);
a72bdb1cd   Jeff Mahoney   reiserfs: Clean u...
397
  out:
5955102c9   Al Viro   wrappers for ->i_...
398
  	inode_unlock(d_inode(xadir));
a72bdb1cd   Jeff Mahoney   reiserfs: Clean u...
399
400
  	dput(xadir);
  	if (err)
6c17675e1   Jeff Mahoney   reiserfs: simplif...
401
  		return ERR_PTR(err);
a72bdb1cd   Jeff Mahoney   reiserfs: Clean u...
402
  	return xafile;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
403
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
404
  /* Internal operations on file data */
bd4c625c0   Linus Torvalds   reiserfs: run scr...
405
  static inline void reiserfs_put_page(struct page *page)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
406
  {
bd4c625c0   Linus Torvalds   reiserfs: run scr...
407
  	kunmap(page);
09cbfeaf1   Kirill A. Shutemov   mm, fs: get rid o...
408
  	put_page(page);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
409
  }
ec6ea56b2   Jeff Mahoney   reiserfs: xattr r...
410
  static struct page *reiserfs_get_page(struct inode *dir, size_t n)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
411
  {
bd4c625c0   Linus Torvalds   reiserfs: run scr...
412
413
  	struct address_space *mapping = dir->i_mapping;
  	struct page *page;
098297b27   Jeff Mahoney   reiserfs: cleanup...
414
415
416
417
  	/*
  	 * We can deadlock if we try to free dentries,
  	 * and an unlink/rmdir has just occurred - GFP_NOFS avoids this
  	 */
c4cdd0383   Al Viro   [PATCH] gfp_t: re...
418
  	mapping_set_gfp_mask(mapping, GFP_NOFS);
09cbfeaf1   Kirill A. Shutemov   mm, fs: get rid o...
419
  	page = read_mapping_page(mapping, n >> PAGE_SHIFT, NULL);
bd4c625c0   Linus Torvalds   reiserfs: run scr...
420
  	if (!IS_ERR(page)) {
bd4c625c0   Linus Torvalds   reiserfs: run scr...
421
  		kmap(page);
bd4c625c0   Linus Torvalds   reiserfs: run scr...
422
423
424
425
  		if (PageError(page))
  			goto fail;
  	}
  	return page;
cf776a7a4   Jeff Mahoney   reiserfs: cleanup...
426
  fail:
bd4c625c0   Linus Torvalds   reiserfs: run scr...
427
428
  	reiserfs_put_page(page);
  	return ERR_PTR(-EIO);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
429
  }
bd4c625c0   Linus Torvalds   reiserfs: run scr...
430
  static inline __u32 xattr_hash(const char *msg, int len)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
431
  {
bd4c625c0   Linus Torvalds   reiserfs: run scr...
432
  	return csum_partial(msg, len, 0);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
433
  }
ba9d8cec6   Vladimir Saveliev   reiserfs: convert...
434
435
  int reiserfs_commit_write(struct file *f, struct page *page,
  			  unsigned from, unsigned to);
ba9d8cec6   Vladimir Saveliev   reiserfs: convert...
436

48b32a355   Jeff Mahoney   reiserfs: use gen...
437
438
439
  static void update_ctime(struct inode *inode)
  {
  	struct timespec now = current_fs_time(inode->i_sb);
f3fb9e273   Fabian Frederick   fs/reiserfs/xattr...
440

1d3382cbf   Al Viro   new helper: inode...
441
  	if (inode_unhashed(inode) || !inode->i_nlink ||
48b32a355   Jeff Mahoney   reiserfs: use gen...
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
  	    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);
5955102c9   Al Viro   wrappers for ->i_...
457
  	inode_lock_nested(d_inode(xadir), I_MUTEX_XATTR);
48b32a355   Jeff Mahoney   reiserfs: use gen...
458
459
460
461
462
  	dentry = lookup_one_len(name, xadir, strlen(name));
  	if (IS_ERR(dentry)) {
  		err = PTR_ERR(dentry);
  		goto out_dput;
  	}
2b0143b5c   David Howells   VFS: normal files...
463
464
  	if (d_really_is_positive(dentry)) {
  		err = xattr_unlink(d_inode(xadir), dentry);
48b32a355   Jeff Mahoney   reiserfs: use gen...
465
466
467
468
469
  		update_ctime(inode);
  	}
  
  	dput(dentry);
  out_dput:
5955102c9   Al Viro   wrappers for ->i_...
470
  	inode_unlock(d_inode(xadir));
48b32a355   Jeff Mahoney   reiserfs: use gen...
471
472
473
  	dput(xadir);
  	return err;
  }
ba9d8cec6   Vladimir Saveliev   reiserfs: convert...
474

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
475
476
477
  /* Generic extended attribute operations that can be used by xa plugins */
  
  /*
1b1dcc1b5   Jes Sorensen   [PATCH] mutex sub...
478
   * inode->i_mutex: down
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
479
480
   */
  int
0ab2621eb   Jeff Mahoney   reiserfs: journal...
481
482
483
  reiserfs_xattr_set_handle(struct reiserfs_transaction_handle *th,
  			  struct inode *inode, const char *name,
  			  const void *buffer, size_t buffer_size, int flags)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
484
  {
bd4c625c0   Linus Torvalds   reiserfs: run scr...
485
  	int err = 0;
3227e14c3   Jeff Mahoney   [PATCH] reiserfs:...
486
  	struct dentry *dentry;
bd4c625c0   Linus Torvalds   reiserfs: run scr...
487
488
  	struct page *page;
  	char *data;
bd4c625c0   Linus Torvalds   reiserfs: run scr...
489
490
  	size_t file_pos = 0;
  	size_t buffer_pos = 0;
48b32a355   Jeff Mahoney   reiserfs: use gen...
491
  	size_t new_size;
bd4c625c0   Linus Torvalds   reiserfs: run scr...
492
  	__u32 xahash = 0;
bd4c625c0   Linus Torvalds   reiserfs: run scr...
493
494
  	if (get_inode_sd_version(inode) == STAT_DATA_V1)
  		return -EOPNOTSUPP;
4f3be1b5a   Frederic Weisbecker   reiserfs: Relax l...
495
496
  	if (!buffer) {
  		err = lookup_and_delete_xattr(inode, name);
4f3be1b5a   Frederic Weisbecker   reiserfs: Relax l...
497
498
  		return err;
  	}
48b32a355   Jeff Mahoney   reiserfs: use gen...
499
  	dentry = xattr_lookup(inode, name, flags);
4c05141df   Jeff Mahoney   reiserfs: locking...
500
  	if (IS_ERR(dentry))
48b32a355   Jeff Mahoney   reiserfs: use gen...
501
  		return PTR_ERR(dentry);
3f14fea6b   Frederic Weisbecker   reiserfs: Relax l...
502

f3e22f48f   Frederic Weisbecker   reiserfs: Fix mis...
503
  	down_write(&REISERFS_I(inode)->i_xattr_sem);
bd4c625c0   Linus Torvalds   reiserfs: run scr...
504

8b6dd72a4   Jeff Mahoney   reiserfs: make pe...
505
  	xahash = xattr_hash(buffer, buffer_size);
bd4c625c0   Linus Torvalds   reiserfs: run scr...
506
507
508
  	while (buffer_pos < buffer_size || buffer_pos == 0) {
  		size_t chunk;
  		size_t skip = 0;
09cbfeaf1   Kirill A. Shutemov   mm, fs: get rid o...
509
  		size_t page_offset = (file_pos & (PAGE_SIZE - 1));
f3fb9e273   Fabian Frederick   fs/reiserfs/xattr...
510

09cbfeaf1   Kirill A. Shutemov   mm, fs: get rid o...
511
512
  		if (buffer_size - buffer_pos > PAGE_SIZE)
  			chunk = PAGE_SIZE;
bd4c625c0   Linus Torvalds   reiserfs: run scr...
513
514
  		else
  			chunk = buffer_size - buffer_pos;
2b0143b5c   David Howells   VFS: normal files...
515
  		page = reiserfs_get_page(d_inode(dentry), file_pos);
bd4c625c0   Linus Torvalds   reiserfs: run scr...
516
517
  		if (IS_ERR(page)) {
  			err = PTR_ERR(page);
48b32a355   Jeff Mahoney   reiserfs: use gen...
518
  			goto out_unlock;
bd4c625c0   Linus Torvalds   reiserfs: run scr...
519
520
521
522
523
524
525
  		}
  
  		lock_page(page);
  		data = page_address(page);
  
  		if (file_pos == 0) {
  			struct reiserfs_xattr_header *rxh;
f3fb9e273   Fabian Frederick   fs/reiserfs/xattr...
526

bd4c625c0   Linus Torvalds   reiserfs: run scr...
527
  			skip = file_pos = sizeof(struct reiserfs_xattr_header);
09cbfeaf1   Kirill A. Shutemov   mm, fs: get rid o...
528
529
  			if (chunk + skip > PAGE_SIZE)
  				chunk = PAGE_SIZE - skip;
bd4c625c0   Linus Torvalds   reiserfs: run scr...
530
531
532
533
  			rxh = (struct reiserfs_xattr_header *)data;
  			rxh->h_magic = cpu_to_le32(REISERFS_XATTR_MAGIC);
  			rxh->h_hash = cpu_to_le32(xahash);
  		}
4c05141df   Jeff Mahoney   reiserfs: locking...
534
  		reiserfs_write_lock(inode->i_sb);
ebdec241d   Christoph Hellwig   fs: kill block_pr...
535
  		err = __reiserfs_write_begin(page, page_offset, chunk + skip);
bd4c625c0   Linus Torvalds   reiserfs: run scr...
536
537
538
  		if (!err) {
  			if (buffer)
  				memcpy(data + skip, buffer + buffer_pos, chunk);
3227e14c3   Jeff Mahoney   [PATCH] reiserfs:...
539
540
541
  			err = reiserfs_commit_write(NULL, page, page_offset,
  						    page_offset + chunk +
  						    skip);
bd4c625c0   Linus Torvalds   reiserfs: run scr...
542
  		}
4c05141df   Jeff Mahoney   reiserfs: locking...
543
  		reiserfs_write_unlock(inode->i_sb);
bd4c625c0   Linus Torvalds   reiserfs: run scr...
544
545
546
547
548
549
550
551
  		unlock_page(page);
  		reiserfs_put_page(page);
  		buffer_pos += chunk;
  		file_pos += chunk;
  		skip = 0;
  		if (err || buffer_size == 0 || !buffer)
  			break;
  	}
48b32a355   Jeff Mahoney   reiserfs: use gen...
552
  	new_size = buffer_size + sizeof(struct reiserfs_xattr_header);
2b0143b5c   David Howells   VFS: normal files...
553
  	if (!err && new_size < i_size_read(d_inode(dentry))) {
48b32a355   Jeff Mahoney   reiserfs: use gen...
554
555
  		struct iattr newattrs = {
  			.ia_ctime = current_fs_time(inode->i_sb),
fb2162df7   Jeff Mahoney   reiserfs: fix cor...
556
  			.ia_size = new_size,
48b32a355   Jeff Mahoney   reiserfs: use gen...
557
558
  			.ia_valid = ATTR_SIZE | ATTR_CTIME,
  		};
31370f62b   Frederic Weisbecker   reiserfs: Relax r...
559

5955102c9   Al Viro   wrappers for ->i_...
560
  		inode_lock_nested(d_inode(dentry), I_MUTEX_XATTR);
2b0143b5c   David Howells   VFS: normal files...
561
  		inode_dio_wait(d_inode(dentry));
31370f62b   Frederic Weisbecker   reiserfs: Relax r...
562

48b32a355   Jeff Mahoney   reiserfs: use gen...
563
  		err = reiserfs_setattr(dentry, &newattrs);
5955102c9   Al Viro   wrappers for ->i_...
564
  		inode_unlock(d_inode(dentry));
48b32a355   Jeff Mahoney   reiserfs: use gen...
565
566
567
  	} else
  		update_ctime(inode);
  out_unlock:
8b6dd72a4   Jeff Mahoney   reiserfs: make pe...
568
  	up_write(&REISERFS_I(inode)->i_xattr_sem);
3227e14c3   Jeff Mahoney   [PATCH] reiserfs:...
569
  	dput(dentry);
48b32a355   Jeff Mahoney   reiserfs: use gen...
570
571
  	return err;
  }
bd4c625c0   Linus Torvalds   reiserfs: run scr...
572

0ab2621eb   Jeff Mahoney   reiserfs: journal...
573
574
575
  /* 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)
48b32a355   Jeff Mahoney   reiserfs: use gen...
576
  {
0ab2621eb   Jeff Mahoney   reiserfs: journal...
577
578
579
580
581
582
583
584
585
586
  
  	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);
4c05141df   Jeff Mahoney   reiserfs: locking...
587
  	reiserfs_write_unlock(inode->i_sb);
0ab2621eb   Jeff Mahoney   reiserfs: journal...
588
  	if (error) {
0ab2621eb   Jeff Mahoney   reiserfs: journal...
589
  		return error;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
590
  	}
bd4c625c0   Linus Torvalds   reiserfs: run scr...
591

0ab2621eb   Jeff Mahoney   reiserfs: journal...
592
593
  	error = reiserfs_xattr_set_handle(&th, inode, name,
  					  buffer, buffer_size, flags);
bd4c625c0   Linus Torvalds   reiserfs: run scr...
594

4c05141df   Jeff Mahoney   reiserfs: locking...
595
  	reiserfs_write_lock(inode->i_sb);
58d854265   Jeff Mahoney   reiserfs: cleanup...
596
  	error2 = journal_end(&th);
4c05141df   Jeff Mahoney   reiserfs: locking...
597
  	reiserfs_write_unlock(inode->i_sb);
0ab2621eb   Jeff Mahoney   reiserfs: journal...
598
599
  	if (error == 0)
  		error = error2;
0ab2621eb   Jeff Mahoney   reiserfs: journal...
600
601
  
  	return error;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
602
603
604
  }
  
  /*
1b1dcc1b5   Jes Sorensen   [PATCH] mutex sub...
605
   * inode->i_mutex: down
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
606
607
   */
  int
48b32a355   Jeff Mahoney   reiserfs: use gen...
608
  reiserfs_xattr_get(struct inode *inode, const char *name, void *buffer,
bd4c625c0   Linus Torvalds   reiserfs: run scr...
609
  		   size_t buffer_size)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
610
  {
bd4c625c0   Linus Torvalds   reiserfs: run scr...
611
  	ssize_t err = 0;
3227e14c3   Jeff Mahoney   [PATCH] reiserfs:...
612
  	struct dentry *dentry;
bd4c625c0   Linus Torvalds   reiserfs: run scr...
613
614
615
616
  	size_t isize;
  	size_t file_pos = 0;
  	size_t buffer_pos = 0;
  	struct page *page;
bd4c625c0   Linus Torvalds   reiserfs: run scr...
617
618
619
620
  	__u32 hash = 0;
  
  	if (name == NULL)
  		return -EINVAL;
098297b27   Jeff Mahoney   reiserfs: cleanup...
621
622
623
624
  	/*
  	 * We can't have xattrs attached to v1 items since they don't have
  	 * generation numbers
  	 */
bd4c625c0   Linus Torvalds   reiserfs: run scr...
625
626
  	if (get_inode_sd_version(inode) == STAT_DATA_V1)
  		return -EOPNOTSUPP;
48b32a355   Jeff Mahoney   reiserfs: use gen...
627
  	dentry = xattr_lookup(inode, name, XATTR_REPLACE);
3227e14c3   Jeff Mahoney   [PATCH] reiserfs:...
628
629
  	if (IS_ERR(dentry)) {
  		err = PTR_ERR(dentry);
bd4c625c0   Linus Torvalds   reiserfs: run scr...
630
631
  		goto out;
  	}
8b6dd72a4   Jeff Mahoney   reiserfs: make pe...
632
  	down_read(&REISERFS_I(inode)->i_xattr_sem);
d984561b3   Jeff Mahoney   reiserfs: elimina...
633

2b0143b5c   David Howells   VFS: normal files...
634
  	isize = i_size_read(d_inode(dentry));
bd4c625c0   Linus Torvalds   reiserfs: run scr...
635
636
637
638
  
  	/* Just return the size needed */
  	if (buffer == NULL) {
  		err = isize - sizeof(struct reiserfs_xattr_header);
8b6dd72a4   Jeff Mahoney   reiserfs: make pe...
639
  		goto out_unlock;
bd4c625c0   Linus Torvalds   reiserfs: run scr...
640
641
642
643
  	}
  
  	if (buffer_size < isize - sizeof(struct reiserfs_xattr_header)) {
  		err = -ERANGE;
8b6dd72a4   Jeff Mahoney   reiserfs: make pe...
644
  		goto out_unlock;
bd4c625c0   Linus Torvalds   reiserfs: run scr...
645
646
647
648
649
650
  	}
  
  	while (file_pos < isize) {
  		size_t chunk;
  		char *data;
  		size_t skip = 0;
f3fb9e273   Fabian Frederick   fs/reiserfs/xattr...
651

09cbfeaf1   Kirill A. Shutemov   mm, fs: get rid o...
652
653
  		if (isize - file_pos > PAGE_SIZE)
  			chunk = PAGE_SIZE;
bd4c625c0   Linus Torvalds   reiserfs: run scr...
654
655
  		else
  			chunk = isize - file_pos;
2b0143b5c   David Howells   VFS: normal files...
656
  		page = reiserfs_get_page(d_inode(dentry), file_pos);
bd4c625c0   Linus Torvalds   reiserfs: run scr...
657
658
  		if (IS_ERR(page)) {
  			err = PTR_ERR(page);
8b6dd72a4   Jeff Mahoney   reiserfs: make pe...
659
  			goto out_unlock;
bd4c625c0   Linus Torvalds   reiserfs: run scr...
660
661
662
663
664
665
666
667
668
669
670
671
672
  		}
  
  		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);
a72bdb1cd   Jeff Mahoney   reiserfs: Clean u...
673
  				reiserfs_warning(inode->i_sb, "jdm-20001",
bd4c625c0   Linus Torvalds   reiserfs: run scr...
674
675
676
677
  						 "Invalid magic for xattr (%s) "
  						 "associated with %k", name,
  						 INODE_PKEY(inode));
  				err = -EIO;
8b6dd72a4   Jeff Mahoney   reiserfs: make pe...
678
  				goto out_unlock;
bd4c625c0   Linus Torvalds   reiserfs: run scr...
679
680
681
682
683
684
685
686
687
688
689
690
691
692
  			}
  			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) {
a72bdb1cd   Jeff Mahoney   reiserfs: Clean u...
693
  		reiserfs_warning(inode->i_sb, "jdm-20002",
bd4c625c0   Linus Torvalds   reiserfs: run scr...
694
695
696
697
  				 "Invalid hash for xattr (%s) associated "
  				 "with %k", name, INODE_PKEY(inode));
  		err = -EIO;
  	}
8b6dd72a4   Jeff Mahoney   reiserfs: make pe...
698
699
  out_unlock:
  	up_read(&REISERFS_I(inode)->i_xattr_sem);
3227e14c3   Jeff Mahoney   [PATCH] reiserfs:...
700
  	dput(dentry);
bd4c625c0   Linus Torvalds   reiserfs: run scr...
701

a72bdb1cd   Jeff Mahoney   reiserfs: Clean u...
702
  out:
bd4c625c0   Linus Torvalds   reiserfs: run scr...
703
  	return err;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
704
  }
48b32a355   Jeff Mahoney   reiserfs: use gen...
705
706
707
708
709
710
711
712
713
714
715
716
717
  /*
   * 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)++)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
718

48b32a355   Jeff Mahoney   reiserfs: use gen...
719
  /* This is the implementation for the xattr plugin infrastructure */
94d09a98c   Stephen Hemminger   reiserfs: constif...
720
721
  static inline const struct xattr_handler *
  find_xattr_handler_prefix(const struct xattr_handler **handlers,
48b32a355   Jeff Mahoney   reiserfs: use gen...
722
  			   const char *name)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
723
  {
94d09a98c   Stephen Hemminger   reiserfs: constif...
724
  	const struct xattr_handler *xah;
bd4c625c0   Linus Torvalds   reiserfs: run scr...
725

48b32a355   Jeff Mahoney   reiserfs: use gen...
726
727
  	if (!handlers)
  		return NULL;
bd4c625c0   Linus Torvalds   reiserfs: run scr...
728

48b32a355   Jeff Mahoney   reiserfs: use gen...
729
  	for_each_xattr_handler(handlers, xah) {
98e9cb571   Andreas Gruenbacher   vfs: Distinguish ...
730
731
  		const char *prefix = xattr_prefix(xah);
  		if (strncmp(prefix, name, strlen(prefix)) == 0)
48b32a355   Jeff Mahoney   reiserfs: use gen...
732
  			break;
bd4c625c0   Linus Torvalds   reiserfs: run scr...
733
  	}
48b32a355   Jeff Mahoney   reiserfs: use gen...
734
  	return xah;
bd4c625c0   Linus Torvalds   reiserfs: run scr...
735
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
736

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
737
738
739
  
  /*
   * Inode operation getxattr()
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
740
741
   */
  ssize_t
bd4c625c0   Linus Torvalds   reiserfs: run scr...
742
743
  reiserfs_getxattr(struct dentry * dentry, const char *name, void *buffer,
  		  size_t size)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
744
  {
94d09a98c   Stephen Hemminger   reiserfs: constif...
745
  	const struct xattr_handler *handler;
bd4c625c0   Linus Torvalds   reiserfs: run scr...
746

431547b3c   Christoph Hellwig   sanitize xattr ha...
747
  	handler = find_xattr_handler_prefix(dentry->d_sb->s_xattr, name);
48b32a355   Jeff Mahoney   reiserfs: use gen...
748

2b0143b5c   David Howells   VFS: normal files...
749
  	if (!handler || get_inode_sd_version(d_inode(dentry)) == STAT_DATA_V1)
bd4c625c0   Linus Torvalds   reiserfs: run scr...
750
  		return -EOPNOTSUPP;
d9a82a040   Andreas Gruenbacher   xattr handlers: P...
751
  	return handler->get(handler, dentry, name, buffer, size);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
752
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
753
754
755
  /*
   * Inode operation setxattr()
   *
2b0143b5c   David Howells   VFS: normal files...
756
   * d_inode(dentry)->i_mutex down
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
757
758
   */
  int
bd4c625c0   Linus Torvalds   reiserfs: run scr...
759
760
  reiserfs_setxattr(struct dentry *dentry, const char *name, const void *value,
  		  size_t size, int flags)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
761
  {
94d09a98c   Stephen Hemminger   reiserfs: constif...
762
  	const struct xattr_handler *handler;
bd4c625c0   Linus Torvalds   reiserfs: run scr...
763

431547b3c   Christoph Hellwig   sanitize xattr ha...
764
  	handler = find_xattr_handler_prefix(dentry->d_sb->s_xattr, name);
48b32a355   Jeff Mahoney   reiserfs: use gen...
765

2b0143b5c   David Howells   VFS: normal files...
766
  	if (!handler || get_inode_sd_version(d_inode(dentry)) == STAT_DATA_V1)
bd4c625c0   Linus Torvalds   reiserfs: run scr...
767
  		return -EOPNOTSUPP;
d9a82a040   Andreas Gruenbacher   xattr handlers: P...
768
  	return handler->set(handler, dentry, name, value, size, flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
769
770
771
772
773
  }
  
  /*
   * Inode operation removexattr()
   *
2b0143b5c   David Howells   VFS: normal files...
774
   * d_inode(dentry)->i_mutex down
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
775
   */
bd4c625c0   Linus Torvalds   reiserfs: run scr...
776
  int reiserfs_removexattr(struct dentry *dentry, const char *name)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
777
  {
94d09a98c   Stephen Hemminger   reiserfs: constif...
778
  	const struct xattr_handler *handler;
f3fb9e273   Fabian Frederick   fs/reiserfs/xattr...
779

431547b3c   Christoph Hellwig   sanitize xattr ha...
780
  	handler = find_xattr_handler_prefix(dentry->d_sb->s_xattr, name);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
781

2b0143b5c   David Howells   VFS: normal files...
782
  	if (!handler || get_inode_sd_version(d_inode(dentry)) == STAT_DATA_V1)
bd4c625c0   Linus Torvalds   reiserfs: run scr...
783
  		return -EOPNOTSUPP;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
784

d9a82a040   Andreas Gruenbacher   xattr handlers: P...
785
  	return handler->set(handler, dentry, name, NULL, 0, XATTR_REPLACE);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
786
  }
48b32a355   Jeff Mahoney   reiserfs: use gen...
787
  struct listxattr_buf {
4acf381e1   Al Viro   [readdir] convert...
788
  	struct dir_context ctx;
48b32a355   Jeff Mahoney   reiserfs: use gen...
789
790
791
  	size_t size;
  	size_t pos;
  	char *buf;
431547b3c   Christoph Hellwig   sanitize xattr ha...
792
  	struct dentry *dentry;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
793
  };
ac7576f4b   Miklos Szeredi   vfs: make first a...
794
795
796
  static int listxattr_filler(struct dir_context *ctx, const char *name,
  			    int namelen, loff_t offset, u64 ino,
  			    unsigned int d_type)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
797
  {
ac7576f4b   Miklos Szeredi   vfs: make first a...
798
799
  	struct listxattr_buf *b =
  		container_of(ctx, struct listxattr_buf, ctx);
48b32a355   Jeff Mahoney   reiserfs: use gen...
800
  	size_t size;
f3fb9e273   Fabian Frederick   fs/reiserfs/xattr...
801

48b32a355   Jeff Mahoney   reiserfs: use gen...
802
803
  	if (name[0] != '.' ||
  	    (namelen != 1 && (name[1] != '.' || namelen != 2))) {
94d09a98c   Stephen Hemminger   reiserfs: constif...
804
  		const struct xattr_handler *handler;
f3fb9e273   Fabian Frederick   fs/reiserfs/xattr...
805

431547b3c   Christoph Hellwig   sanitize xattr ha...
806
  		handler = find_xattr_handler_prefix(b->dentry->d_sb->s_xattr,
48b32a355   Jeff Mahoney   reiserfs: use gen...
807
  						    name);
764a5c6b1   Andreas Gruenbacher   xattr handlers: S...
808
809
  		if (!handler /* Unsupported xattr name */ ||
  		    (handler->list && !handler->list(b->dentry)))
48b32a355   Jeff Mahoney   reiserfs: use gen...
810
  			return 0;
764a5c6b1   Andreas Gruenbacher   xattr handlers: S...
811
  		size = namelen + 1;
48b32a355   Jeff Mahoney   reiserfs: use gen...
812
  		if (b->buf) {
48b32a355   Jeff Mahoney   reiserfs: use gen...
813
814
  			if (size > b->size)
  				return -ERANGE;
764a5c6b1   Andreas Gruenbacher   xattr handlers: S...
815
816
  			memcpy(b->buf + b->pos, name, namelen);
  			b->buf[b->pos + namelen] = 0;
bd4c625c0   Linus Torvalds   reiserfs: run scr...
817
  		}
48b32a355   Jeff Mahoney   reiserfs: use gen...
818
819
  		b->pos += size;
  	}
bd4c625c0   Linus Torvalds   reiserfs: run scr...
820
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
821
  }
bd4c625c0   Linus Torvalds   reiserfs: run scr...
822

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
823
824
825
  /*
   * Inode operation listxattr()
   *
48b32a355   Jeff Mahoney   reiserfs: use gen...
826
827
828
   * 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.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
829
   */
bd4c625c0   Linus Torvalds   reiserfs: run scr...
830
  ssize_t reiserfs_listxattr(struct dentry * dentry, char *buffer, size_t size)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
831
  {
bd4c625c0   Linus Torvalds   reiserfs: run scr...
832
833
  	struct dentry *dir;
  	int err = 0;
48b32a355   Jeff Mahoney   reiserfs: use gen...
834
  	struct listxattr_buf buf = {
4acf381e1   Al Viro   [readdir] convert...
835
  		.ctx.actor = listxattr_filler,
431547b3c   Christoph Hellwig   sanitize xattr ha...
836
  		.dentry = dentry,
48b32a355   Jeff Mahoney   reiserfs: use gen...
837
838
839
  		.buf = buffer,
  		.size = buffer ? size : 0,
  	};
bd4c625c0   Linus Torvalds   reiserfs: run scr...
840

2b0143b5c   David Howells   VFS: normal files...
841
  	if (d_really_is_negative(dentry))
bd4c625c0   Linus Torvalds   reiserfs: run scr...
842
  		return -EINVAL;
677c9b2e3   Jeff Mahoney   reiserfs: remove ...
843
  	if (!dentry->d_sb->s_xattr ||
2b0143b5c   David Howells   VFS: normal files...
844
  	    get_inode_sd_version(d_inode(dentry)) == STAT_DATA_V1)
bd4c625c0   Linus Torvalds   reiserfs: run scr...
845
  		return -EOPNOTSUPP;
2b0143b5c   David Howells   VFS: normal files...
846
  	dir = open_xa_dir(d_inode(dentry), XATTR_REPLACE);
bd4c625c0   Linus Torvalds   reiserfs: run scr...
847
848
849
  	if (IS_ERR(dir)) {
  		err = PTR_ERR(dir);
  		if (err == -ENODATA)
48b32a355   Jeff Mahoney   reiserfs: use gen...
850
  			err = 0;  /* Not an error if there aren't any xattrs */
bd4c625c0   Linus Torvalds   reiserfs: run scr...
851
852
  		goto out;
  	}
5955102c9   Al Viro   wrappers for ->i_...
853
  	inode_lock_nested(d_inode(dir), I_MUTEX_XATTR);
2b0143b5c   David Howells   VFS: normal files...
854
  	err = reiserfs_readdir_inode(d_inode(dir), &buf.ctx);
5955102c9   Al Viro   wrappers for ->i_...
855
  	inode_unlock(d_inode(dir));
bd4c625c0   Linus Torvalds   reiserfs: run scr...
856

48b32a355   Jeff Mahoney   reiserfs: use gen...
857
858
  	if (!err)
  		err = buf.pos;
bd4c625c0   Linus Torvalds   reiserfs: run scr...
859

3227e14c3   Jeff Mahoney   [PATCH] reiserfs:...
860
  	dput(dir);
8b6dd72a4   Jeff Mahoney   reiserfs: make pe...
861
  out:
bd4c625c0   Linus Torvalds   reiserfs: run scr...
862
  	return err;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
863
  }
a72bdb1cd   Jeff Mahoney   reiserfs: Clean u...
864
  static int create_privroot(struct dentry *dentry)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
865
  {
a72bdb1cd   Jeff Mahoney   reiserfs: Clean u...
866
  	int err;
2b0143b5c   David Howells   VFS: normal files...
867
  	struct inode *inode = d_inode(dentry->d_parent);
f3fb9e273   Fabian Frederick   fs/reiserfs/xattr...
868

5955102c9   Al Viro   wrappers for ->i_...
869
  	WARN_ON_ONCE(!inode_is_locked(inode));
5a6059c35   Jeff Mahoney   reiserfs: Expand ...
870

6c17675e1   Jeff Mahoney   reiserfs: simplif...
871
  	err = xattr_mkdir(inode, dentry, 0700);
2b0143b5c   David Howells   VFS: normal files...
872
  	if (err || d_really_is_negative(dentry)) {
edcc37a04   Al Viro   Always lookup pri...
873
874
875
876
877
  		reiserfs_warning(dentry->d_sb, "jdm-20006",
  				 "xattrs/ACLs enabled and couldn't "
  				 "find/create .reiserfs_priv. "
  				 "Failing mount.");
  		return -EOPNOTSUPP;
bd4c625c0   Linus Torvalds   reiserfs: run scr...
878
  	}
2b0143b5c   David Howells   VFS: normal files...
879
  	d_inode(dentry)->i_flags |= S_PRIVATE;
edcc37a04   Al Viro   Always lookup pri...
880
881
882
  	reiserfs_info(dentry->d_sb, "Created %s - reserved for xattr "
  		      "storage.
  ", PRIVROOT_NAME);
bd4c625c0   Linus Torvalds   reiserfs: run scr...
883

edcc37a04   Al Viro   Always lookup pri...
884
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
885
  }
12abb35a0   Jeff Mahoney   reiserfs: clean u...
886
887
888
889
890
891
892
  #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 */
da02eb72f   Sachin Kamat   reiserfs: Make re...
893
  static const struct xattr_handler *reiserfs_xattr_handlers[] = {
12abb35a0   Jeff Mahoney   reiserfs: clean u...
894
895
896
897
898
899
900
901
  #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
47f70d08f   Christoph Hellwig   reiserfs: use gen...
902
903
  	&posix_acl_access_xattr_handler,
  	&posix_acl_default_xattr_handler,
12abb35a0   Jeff Mahoney   reiserfs: clean u...
904
905
906
  #endif
  	NULL
  };
a72bdb1cd   Jeff Mahoney   reiserfs: Clean u...
907
  static int xattr_mount_check(struct super_block *s)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
908
  {
098297b27   Jeff Mahoney   reiserfs: cleanup...
909
910
911
912
  	/*
  	 * We need generation numbers to ensure that the oid mapping is correct
  	 * v3.5 filesystems don't have them.
  	 */
48b32a355   Jeff Mahoney   reiserfs: use gen...
913
914
  	if (old_format_only(s)) {
  		if (reiserfs_xattrs_optional(s)) {
098297b27   Jeff Mahoney   reiserfs: cleanup...
915
916
917
918
  			/*
  			 * Old format filesystem, but optional xattrs have
  			 * been enabled. Error out.
  			 */
48b32a355   Jeff Mahoney   reiserfs: use gen...
919
920
921
922
923
924
  			reiserfs_warning(s, "jdm-2005",
  					 "xattrs/ACLs not supported "
  					 "on pre-v3.6 format filesystems. "
  					 "Failing mount.");
  			return -EOPNOTSUPP;
  		}
a72bdb1cd   Jeff Mahoney   reiserfs: Clean u...
925
926
927
  	}
  
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
928
  }
10556cb21   Al Viro   ->permission() sa...
929
  int reiserfs_permission(struct inode *inode, int mask)
b83674c0d   Jeff Mahoney   reiserfs: fixup p...
930
931
932
933
934
935
936
  {
  	/*
  	 * We don't do permission checks on the internal objects.
  	 * Permissions are determined by the "owning" object.
  	 */
  	if (IS_PRIVATE(inode))
  		return 0;
2830ba7f3   Al Viro   ->permission() sa...
937
  	return generic_permission(inode, mask);
b83674c0d   Jeff Mahoney   reiserfs: fixup p...
938
  }
0b728e191   Al Viro   stop passing name...
939
  static int xattr_hide_revalidate(struct dentry *dentry, unsigned int flags)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
940
  {
cac36f707   Jeff Mahoney   reiserfs: fix per...
941
  	return -EPERM;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
942
  }
e16404ed0   Al Viro   constify dentry_o...
943
  static const struct dentry_operations xattr_lookup_poison_ops = {
cac36f707   Jeff Mahoney   reiserfs: fix per...
944
  	.d_revalidate = xattr_hide_revalidate,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
945
  };
edcc37a04   Al Viro   Always lookup pri...
946
947
948
949
950
951
  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 */
5955102c9   Al Viro   wrappers for ->i_...
952
  	inode_lock(d_inode(s->s_root));
edcc37a04   Al Viro   Always lookup pri...
953
954
955
956
  	dentry = lookup_one_len(PRIVROOT_NAME, s->s_root,
  				strlen(PRIVROOT_NAME));
  	if (!IS_ERR(dentry)) {
  		REISERFS_SB(s)->priv_root = dentry;
fb045adb9   Nick Piggin   fs: dcache reduce...
957
  		d_set_d_op(dentry, &xattr_lookup_poison_ops);
2b0143b5c   David Howells   VFS: normal files...
958
959
  		if (d_really_is_positive(dentry))
  			d_inode(dentry)->i_flags |= S_PRIVATE;
edcc37a04   Al Viro   Always lookup pri...
960
961
  	} else
  		err = PTR_ERR(dentry);
5955102c9   Al Viro   wrappers for ->i_...
962
  	inode_unlock(d_inode(s->s_root));
edcc37a04   Al Viro   Always lookup pri...
963
964
965
  
  	return err;
  }
098297b27   Jeff Mahoney   reiserfs: cleanup...
966
967
  /*
   * We need to take a copy of the mount flags since things like
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
968
   * MS_RDONLY don't get set until *after* we're called.
098297b27   Jeff Mahoney   reiserfs: cleanup...
969
970
   * mount_flags != mount_options
   */
bd4c625c0   Linus Torvalds   reiserfs: run scr...
971
  int reiserfs_xattr_init(struct super_block *s, int mount_flags)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
972
  {
bd4c625c0   Linus Torvalds   reiserfs: run scr...
973
  	int err = 0;
ab17c4f02   Jeff Mahoney   reiserfs: fixup x...
974
  	struct dentry *privroot = REISERFS_SB(s)->priv_root;
bd4c625c0   Linus Torvalds   reiserfs: run scr...
975

a72bdb1cd   Jeff Mahoney   reiserfs: Clean u...
976
977
  	err = xattr_mount_check(s);
  	if (err)
bd4c625c0   Linus Torvalds   reiserfs: run scr...
978
  		goto error;
bd4c625c0   Linus Torvalds   reiserfs: run scr...
979

2b0143b5c   David Howells   VFS: normal files...
980
  	if (d_really_is_negative(privroot) && !(mount_flags & MS_RDONLY)) {
5955102c9   Al Viro   wrappers for ->i_...
981
  		inode_lock(d_inode(s->s_root));
edcc37a04   Al Viro   Always lookup pri...
982
  		err = create_privroot(REISERFS_SB(s)->priv_root);
5955102c9   Al Viro   wrappers for ->i_...
983
  		inode_unlock(d_inode(s->s_root));
bd4c625c0   Linus Torvalds   reiserfs: run scr...
984
  	}
ab17c4f02   Jeff Mahoney   reiserfs: fixup x...
985

2b0143b5c   David Howells   VFS: normal files...
986
  	if (d_really_is_positive(privroot)) {
48b32a355   Jeff Mahoney   reiserfs: use gen...
987
  		s->s_xattr = reiserfs_xattr_handlers;
5955102c9   Al Viro   wrappers for ->i_...
988
  		inode_lock(d_inode(privroot));
ab17c4f02   Jeff Mahoney   reiserfs: fixup x...
989
990
  		if (!REISERFS_SB(s)->xattr_root) {
  			struct dentry *dentry;
f3fb9e273   Fabian Frederick   fs/reiserfs/xattr...
991

ab17c4f02   Jeff Mahoney   reiserfs: fixup x...
992
993
994
995
996
997
998
  			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);
  		}
5955102c9   Al Viro   wrappers for ->i_...
999
  		inode_unlock(d_inode(privroot));
ab17c4f02   Jeff Mahoney   reiserfs: fixup x...
1000
  	}
48b32a355   Jeff Mahoney   reiserfs: use gen...
1001

a72bdb1cd   Jeff Mahoney   reiserfs: Clean u...
1002
  error:
bd4c625c0   Linus Torvalds   reiserfs: run scr...
1003
  	if (err) {
a228bf8f0   Jeff Mahoney   reiserfs: cleanup...
1004
1005
  		clear_bit(REISERFS_XATTRS_USER, &REISERFS_SB(s)->s_mount_opt);
  		clear_bit(REISERFS_POSIXACL, &REISERFS_SB(s)->s_mount_opt);
bd4c625c0   Linus Torvalds   reiserfs: run scr...
1006
1007
1008
  	}
  
  	/* The super_block MS_POSIXACL must mirror the (no)acl mount option. */
bd4c625c0   Linus Torvalds   reiserfs: run scr...
1009
1010
  	if (reiserfs_posixacl(s))
  		s->s_flags |= MS_POSIXACL;
ab17c4f02   Jeff Mahoney   reiserfs: fixup x...
1011
  	else
ab17c4f02   Jeff Mahoney   reiserfs: fixup x...
1012
  		s->s_flags &= ~MS_POSIXACL;
bd4c625c0   Linus Torvalds   reiserfs: run scr...
1013
1014
  
  	return err;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1015
  }