Blame view

fs/reiserfs/xattr.c 25.8 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
35
36
   */
  
  #include <linux/reiserfs_fs.h>
16f7e0fe2   Randy Dunlap   [PATCH] capable/c...
37
  #include <linux/capability.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
38
39
40
  #include <linux/dcache.h>
  #include <linux/namei.h>
  #include <linux/errno.h>
5a0e3ad6a   Tejun Heo   include cleanup: ...
41
  #include <linux/gfp.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
42
43
44
45
46
47
  #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>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
48
  #include <asm/uaccess.h>
3277c39f8   Al Viro   [NET]: Kill direc...
49
  #include <net/checksum.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
50
  #include <linux/stat.h>
6c17675e1   Jeff Mahoney   reiserfs: simplif...
51
  #include <linux/quotaops.h>
431547b3c   Christoph Hellwig   sanitize xattr ha...
52
  #include <linux/security.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

6c17675e1   Jeff Mahoney   reiserfs: simplif...
57
58
59
  /* 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. */
3a355cc61   Jeff Mahoney   reiserfs: xattr_c...
60
  #ifdef CONFIG_REISERFS_FS_XATTR
6c17675e1   Jeff Mahoney   reiserfs: simplif...
61
  static int xattr_create(struct inode *dir, struct dentry *dentry, int mode)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
62
  {
6c17675e1   Jeff Mahoney   reiserfs: simplif...
63
  	BUG_ON(!mutex_is_locked(&dir->i_mutex));
6c17675e1   Jeff Mahoney   reiserfs: simplif...
64
65
  	return dir->i_op->create(dir, dentry, mode, NULL);
  }
3a355cc61   Jeff Mahoney   reiserfs: xattr_c...
66
  #endif
bd4c625c0   Linus Torvalds   reiserfs: run scr...
67

18bb1db3e   Al Viro   switch vfs_mkdir(...
68
  static int xattr_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
6c17675e1   Jeff Mahoney   reiserfs: simplif...
69
70
  {
  	BUG_ON(!mutex_is_locked(&dir->i_mutex));
6c17675e1   Jeff Mahoney   reiserfs: simplif...
71
72
  	return dir->i_op->mkdir(dir, dentry, mode);
  }
bd4c625c0   Linus Torvalds   reiserfs: run scr...
73

6c17675e1   Jeff Mahoney   reiserfs: simplif...
74
75
76
77
78
79
80
81
  /* 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));
bd4c625c0   Linus Torvalds   reiserfs: run scr...
82

4dd859697   Frederic Weisbecker   reiserfs: Fix jou...
83
84
  	reiserfs_mutex_lock_nested_safe(&dentry->d_inode->i_mutex,
  					I_MUTEX_CHILD, dir->i_sb);
6c17675e1   Jeff Mahoney   reiserfs: simplif...
85
86
87
88
89
90
91
92
93
94
95
96
  	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));
6c17675e1   Jeff Mahoney   reiserfs: simplif...
97

835d5247d   Frederic Weisbecker   reiserfs: Safely ...
98
99
  	reiserfs_mutex_lock_nested_safe(&dentry->d_inode->i_mutex,
  					I_MUTEX_CHILD, dir->i_sb);
6c17675e1   Jeff Mahoney   reiserfs: simplif...
100
101
102
103
104
105
  	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);
6c17675e1   Jeff Mahoney   reiserfs: simplif...
106
107
108
  
  	return error;
  }
6c17675e1   Jeff Mahoney   reiserfs: simplif...
109
  #define xattr_may_create(flags)	(!flags || flags & XATTR_CREATE)
ab17c4f02   Jeff Mahoney   reiserfs: fixup x...
110
  static struct dentry *open_xa_root(struct super_block *sb, int flags)
6c17675e1   Jeff Mahoney   reiserfs: simplif...
111
  {
ab17c4f02   Jeff Mahoney   reiserfs: fixup x...
112
113
114
115
  	struct dentry *privroot = REISERFS_SB(sb)->priv_root;
  	struct dentry *xaroot;
  	if (!privroot->d_inode)
  		return ERR_PTR(-ENODATA);
6c17675e1   Jeff Mahoney   reiserfs: simplif...
116

ab17c4f02   Jeff Mahoney   reiserfs: fixup x...
117
  	mutex_lock_nested(&privroot->d_inode->i_mutex, I_MUTEX_XATTR);
6c17675e1   Jeff Mahoney   reiserfs: simplif...
118

ab17c4f02   Jeff Mahoney   reiserfs: fixup x...
119
  	xaroot = dget(REISERFS_SB(sb)->xattr_root);
ceb5edc45   Jeff Mahoney   reiserfs: deal wi...
120
121
122
  	if (!xaroot)
  		xaroot = ERR_PTR(-ENODATA);
  	else if (!xaroot->d_inode) {
ab17c4f02   Jeff Mahoney   reiserfs: fixup x...
123
  		int err = -ENODATA;
5a6059c35   Jeff Mahoney   reiserfs: Expand ...
124
  		if (xattr_may_create(flags))
ab17c4f02   Jeff Mahoney   reiserfs: fixup x...
125
  			err = xattr_mkdir(privroot->d_inode, xaroot, 0700);
9b7f37550   Jeff Mahoney   reiserfs: fix xat...
126
  		if (err) {
ab17c4f02   Jeff Mahoney   reiserfs: fixup x...
127
128
  			dput(xaroot);
  			xaroot = ERR_PTR(err);
9b7f37550   Jeff Mahoney   reiserfs: fix xat...
129
  		}
bd4c625c0   Linus Torvalds   reiserfs: run scr...
130
  	}
6c17675e1   Jeff Mahoney   reiserfs: simplif...
131

ab17c4f02   Jeff Mahoney   reiserfs: fixup x...
132
133
  	mutex_unlock(&privroot->d_inode->i_mutex);
  	return xaroot;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
134
  }
bd4c625c0   Linus Torvalds   reiserfs: run scr...
135
  static struct dentry *open_xa_dir(const struct inode *inode, int flags)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
136
  {
bd4c625c0   Linus Torvalds   reiserfs: run scr...
137
138
  	struct dentry *xaroot, *xadir;
  	char namebuf[17];
6c17675e1   Jeff Mahoney   reiserfs: simplif...
139
  	xaroot = open_xa_root(inode->i_sb, flags);
9b7f37550   Jeff Mahoney   reiserfs: fix xat...
140
  	if (IS_ERR(xaroot))
bd4c625c0   Linus Torvalds   reiserfs: run scr...
141
  		return xaroot;
bd4c625c0   Linus Torvalds   reiserfs: run scr...
142

bd4c625c0   Linus Torvalds   reiserfs: run scr...
143
144
145
  	snprintf(namebuf, sizeof(namebuf), "%X.%X",
  		 le32_to_cpu(INODE_PKEY(inode)->k_objectid),
  		 inode->i_generation);
bd4c625c0   Linus Torvalds   reiserfs: run scr...
146

ab17c4f02   Jeff Mahoney   reiserfs: fixup x...
147
148
149
150
151
152
153
154
155
156
157
158
159
160
  	mutex_lock_nested(&xaroot->d_inode->i_mutex, I_MUTEX_XATTR);
  
  	xadir = lookup_one_len(namebuf, xaroot, strlen(namebuf));
  	if (!IS_ERR(xadir) && !xadir->d_inode) {
  		int err = -ENODATA;
  		if (xattr_may_create(flags))
  			err = xattr_mkdir(xaroot->d_inode, xadir, 0700);
  		if (err) {
  			dput(xadir);
  			xadir = ERR_PTR(err);
  		}
  	}
  
  	mutex_unlock(&xaroot->d_inode->i_mutex);
bd4c625c0   Linus Torvalds   reiserfs: run scr...
161
162
  	dput(xaroot);
  	return xadir;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
163
  }
48b32a355   Jeff Mahoney   reiserfs: use gen...
164
165
166
  /* 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. */
a41f1a471   Jeff Mahoney   reiserfs: use gen...
167
168
169
170
171
  struct reiserfs_dentry_buf {
  	struct dentry *xadir;
  	int count;
  	struct dentry *dentries[8];
  };
bd4c625c0   Linus Torvalds   reiserfs: run scr...
172

a72bdb1cd   Jeff Mahoney   reiserfs: Clean u...
173
  static int
a41f1a471   Jeff Mahoney   reiserfs: use gen...
174
175
  fill_with_dentries(void *buf, const char *name, int namelen, loff_t offset,
  		    u64 ino, unsigned int d_type)
a72bdb1cd   Jeff Mahoney   reiserfs: Clean u...
176
  {
a41f1a471   Jeff Mahoney   reiserfs: use gen...
177
  	struct reiserfs_dentry_buf *dbuf = buf;
a72bdb1cd   Jeff Mahoney   reiserfs: Clean u...
178
  	struct dentry *dentry;
5a6059c35   Jeff Mahoney   reiserfs: Expand ...
179
  	WARN_ON_ONCE(!mutex_is_locked(&dbuf->xadir->d_inode->i_mutex));
bd4c625c0   Linus Torvalds   reiserfs: run scr...
180

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

a41f1a471   Jeff Mahoney   reiserfs: use gen...
184
185
186
  	if (name[0] == '.' && (name[1] == '\0' ||
  			       (name[1] == '.' && name[2] == '\0')))
  		return 0;
bd4c625c0   Linus Torvalds   reiserfs: run scr...
187

a41f1a471   Jeff Mahoney   reiserfs: use gen...
188
  	dentry = lookup_one_len(name, dbuf->xadir, namelen);
a72bdb1cd   Jeff Mahoney   reiserfs: Clean u...
189
  	if (IS_ERR(dentry)) {
a41f1a471   Jeff Mahoney   reiserfs: use gen...
190
  		return PTR_ERR(dentry);
a72bdb1cd   Jeff Mahoney   reiserfs: Clean u...
191
  	} else if (!dentry->d_inode) {
a41f1a471   Jeff Mahoney   reiserfs: use gen...
192
193
194
195
196
197
198
199
  		/* A directory entry exists, but no file? */
  		reiserfs_error(dentry->d_sb, "xattr-20003",
  			       "Corrupted directory: xattr %s listed but "
  			       "not found for file %s.
  ",
  			       dentry->d_name.name, dbuf->xadir->d_name.name);
  		dput(dentry);
  		return -EIO;
bd4c625c0   Linus Torvalds   reiserfs: run scr...
200
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
201

a41f1a471   Jeff Mahoney   reiserfs: use gen...
202
203
  	dbuf->dentries[dbuf->count++] = dentry;
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
204
  }
a41f1a471   Jeff Mahoney   reiserfs: use gen...
205
206
  static void
  cleanup_dentry_buf(struct reiserfs_dentry_buf *buf)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
207
  {
a41f1a471   Jeff Mahoney   reiserfs: use gen...
208
209
210
211
  	int i;
  	for (i = 0; i < buf->count; i++)
  		if (buf->dentries[i])
  			dput(buf->dentries[i]);
a72bdb1cd   Jeff Mahoney   reiserfs: Clean u...
212
  }
a41f1a471   Jeff Mahoney   reiserfs: use gen...
213
214
215
  static int reiserfs_for_each_xattr(struct inode *inode,
  				   int (*action)(struct dentry *, void *),
  				   void *data)
a72bdb1cd   Jeff Mahoney   reiserfs: Clean u...
216
  {
a41f1a471   Jeff Mahoney   reiserfs: use gen...
217
218
219
220
221
222
  	struct dentry *dir;
  	int i, err = 0;
  	loff_t pos = 0;
  	struct reiserfs_dentry_buf buf = {
  		.count = 0,
  	};
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
223

a72bdb1cd   Jeff Mahoney   reiserfs: Clean u...
224
225
226
  	/* 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
227

27026a05b   Frederic Weisbecker   reiserfs: Fix rei...
228
  	reiserfs_write_unlock(inode->i_sb);
6c17675e1   Jeff Mahoney   reiserfs: simplif...
229
  	dir = open_xa_dir(inode, XATTR_REPLACE);
a72bdb1cd   Jeff Mahoney   reiserfs: Clean u...
230
231
  	if (IS_ERR(dir)) {
  		err = PTR_ERR(dir);
27026a05b   Frederic Weisbecker   reiserfs: Fix rei...
232
  		reiserfs_write_lock(inode->i_sb);
a72bdb1cd   Jeff Mahoney   reiserfs: Clean u...
233
234
  		goto out;
  	} else if (!dir->d_inode) {
a41f1a471   Jeff Mahoney   reiserfs: use gen...
235
  		err = 0;
27026a05b   Frederic Weisbecker   reiserfs: Fix rei...
236
  		reiserfs_write_lock(inode->i_sb);
a41f1a471   Jeff Mahoney   reiserfs: use gen...
237
  		goto out_dir;
a72bdb1cd   Jeff Mahoney   reiserfs: Clean u...
238
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
239

6c17675e1   Jeff Mahoney   reiserfs: simplif...
240
  	mutex_lock_nested(&dir->d_inode->i_mutex, I_MUTEX_XATTR);
27026a05b   Frederic Weisbecker   reiserfs: Fix rei...
241
242
  
  	reiserfs_write_lock(inode->i_sb);
a41f1a471   Jeff Mahoney   reiserfs: use gen...
243
244
245
246
  	buf.xadir = dir;
  	err = reiserfs_readdir_dentry(dir, &buf, fill_with_dentries, &pos);
  	while ((err == 0 || err == -ENOSPC) && buf.count) {
  		err = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
247

a41f1a471   Jeff Mahoney   reiserfs: use gen...
248
249
250
  		for (i = 0; i < buf.count && buf.dentries[i]; i++) {
  			int lerr = 0;
  			struct dentry *dentry = buf.dentries[i];
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
251

a41f1a471   Jeff Mahoney   reiserfs: use gen...
252
253
  			if (err == 0 && !S_ISDIR(dentry->d_inode->i_mode))
  				lerr = action(dentry, data);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
254

a41f1a471   Jeff Mahoney   reiserfs: use gen...
255
256
257
  			dput(dentry);
  			buf.dentries[i] = NULL;
  			err = lerr ?: err;
bd4c625c0   Linus Torvalds   reiserfs: run scr...
258
  		}
a41f1a471   Jeff Mahoney   reiserfs: use gen...
259
260
261
262
  		buf.count = 0;
  		if (!err)
  			err = reiserfs_readdir_dentry(dir, &buf,
  						      fill_with_dentries, &pos);
8b6dd72a4   Jeff Mahoney   reiserfs: make pe...
263
  	}
a41f1a471   Jeff Mahoney   reiserfs: use gen...
264
  	mutex_unlock(&dir->d_inode->i_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
265

a41f1a471   Jeff Mahoney   reiserfs: use gen...
266
267
  	/* Clean up after a failed readdir */
  	cleanup_dentry_buf(&buf);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
268

d984561b3   Jeff Mahoney   reiserfs: elimina...
269
  	if (!err) {
a41f1a471   Jeff Mahoney   reiserfs: use gen...
270
271
272
273
274
275
276
277
278
279
280
  		/* We start a transaction here to avoid a ABBA situation
  		 * between the xattr root's i_mutex and the journal lock.
  		 * This doesn't incur much additional overhead since the
  		 * new transaction will just nest inside the
  		 * outer transaction. */
  		int blocks = JOURNAL_PER_BALANCE_CNT * 2 + 2 +
  			     4 * REISERFS_QUOTA_TRANS_BLOCKS(inode->i_sb);
  		struct reiserfs_transaction_handle th;
  		err = journal_begin(&th, inode->i_sb, blocks);
  		if (!err) {
  			int jerror;
8b513f56d   Frederic Weisbecker   reiserfs: Safely ...
281
282
283
  			reiserfs_mutex_lock_nested_safe(
  					  &dir->d_parent->d_inode->i_mutex,
  					  I_MUTEX_XATTR, inode->i_sb);
a41f1a471   Jeff Mahoney   reiserfs: use gen...
284
285
286
287
288
  			err = action(dir, data);
  			jerror = journal_end(&th, inode->i_sb, blocks);
  			mutex_unlock(&dir->d_parent->d_inode->i_mutex);
  			err = jerror ?: err;
  		}
a72bdb1cd   Jeff Mahoney   reiserfs: Clean u...
289
  	}
a41f1a471   Jeff Mahoney   reiserfs: use gen...
290
291
  out_dir:
  	dput(dir);
a72bdb1cd   Jeff Mahoney   reiserfs: Clean u...
292
  out:
a41f1a471   Jeff Mahoney   reiserfs: use gen...
293
294
295
  	/* -ENODATA isn't an error */
  	if (err == -ENODATA)
  		err = 0;
a72bdb1cd   Jeff Mahoney   reiserfs: Clean u...
296
297
  	return err;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
298

a41f1a471   Jeff Mahoney   reiserfs: use gen...
299
  static int delete_one_xattr(struct dentry *dentry, void *data)
a72bdb1cd   Jeff Mahoney   reiserfs: Clean u...
300
  {
a41f1a471   Jeff Mahoney   reiserfs: use gen...
301
  	struct inode *dir = dentry->d_parent->d_inode;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
302

a41f1a471   Jeff Mahoney   reiserfs: use gen...
303
304
305
  	/* This is the xattr dir, handle specially. */
  	if (S_ISDIR(dentry->d_inode->i_mode))
  		return xattr_rmdir(dir, dentry);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
306

a41f1a471   Jeff Mahoney   reiserfs: use gen...
307
308
  	return xattr_unlink(dir, dentry);
  }
bd4c625c0   Linus Torvalds   reiserfs: run scr...
309

a41f1a471   Jeff Mahoney   reiserfs: use gen...
310
311
312
313
314
  static int chown_one_xattr(struct dentry *dentry, void *data)
  {
  	struct iattr *attrs = data;
  	return reiserfs_setattr(dentry, attrs);
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
315

a41f1a471   Jeff Mahoney   reiserfs: use gen...
316
317
318
319
320
321
322
323
  /* No i_mutex, but the inode is unconnected. */
  int reiserfs_delete_xattrs(struct inode *inode)
  {
  	int err = reiserfs_for_each_xattr(inode, delete_one_xattr, NULL);
  	if (err)
  		reiserfs_warning(inode->i_sb, "jdm-20004",
  				 "Couldn't delete all xattrs (%d)
  ", err);
a72bdb1cd   Jeff Mahoney   reiserfs: Clean u...
324
325
  	return err;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
326

a41f1a471   Jeff Mahoney   reiserfs: use gen...
327
  /* inode->i_mutex: down */
a72bdb1cd   Jeff Mahoney   reiserfs: Clean u...
328
329
  int reiserfs_chown_xattrs(struct inode *inode, struct iattr *attrs)
  {
a41f1a471   Jeff Mahoney   reiserfs: use gen...
330
  	int err = reiserfs_for_each_xattr(inode, chown_one_xattr, attrs);
8b6dd72a4   Jeff Mahoney   reiserfs: make pe...
331
332
333
334
  	if (err)
  		reiserfs_warning(inode->i_sb, "jdm-20007",
  				 "Couldn't chown all xattrs (%d)
  ", err);
a72bdb1cd   Jeff Mahoney   reiserfs: Clean u...
335
  	return err;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
336
  }
a72bdb1cd   Jeff Mahoney   reiserfs: Clean u...
337
  #ifdef CONFIG_REISERFS_FS_XATTR
a72bdb1cd   Jeff Mahoney   reiserfs: Clean u...
338
339
340
  /* 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. */
48b32a355   Jeff Mahoney   reiserfs: use gen...
341
342
  static struct dentry *xattr_lookup(struct inode *inode, const char *name,
  				    int flags)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
343
  {
a72bdb1cd   Jeff Mahoney   reiserfs: Clean u...
344
345
346
347
  	struct dentry *xadir, *xafile;
  	int err = 0;
  
  	xadir = open_xa_dir(inode, flags);
6c17675e1   Jeff Mahoney   reiserfs: simplif...
348
  	if (IS_ERR(xadir))
a72bdb1cd   Jeff Mahoney   reiserfs: Clean u...
349
  		return ERR_CAST(xadir);
a72bdb1cd   Jeff Mahoney   reiserfs: Clean u...
350

5a6059c35   Jeff Mahoney   reiserfs: Expand ...
351
  	mutex_lock_nested(&xadir->d_inode->i_mutex, I_MUTEX_XATTR);
a72bdb1cd   Jeff Mahoney   reiserfs: Clean u...
352
353
  	xafile = lookup_one_len(name, xadir, strlen(name));
  	if (IS_ERR(xafile)) {
6c17675e1   Jeff Mahoney   reiserfs: simplif...
354
355
  		err = PTR_ERR(xafile);
  		goto out;
bd4c625c0   Linus Torvalds   reiserfs: run scr...
356
  	}
a72bdb1cd   Jeff Mahoney   reiserfs: Clean u...
357

6c17675e1   Jeff Mahoney   reiserfs: simplif...
358
359
  	if (xafile->d_inode && (flags & XATTR_CREATE))
  		err = -EEXIST;
a72bdb1cd   Jeff Mahoney   reiserfs: Clean u...
360

6c17675e1   Jeff Mahoney   reiserfs: simplif...
361
362
  	if (!xafile->d_inode) {
  		err = -ENODATA;
5a6059c35   Jeff Mahoney   reiserfs: Expand ...
363
  		if (xattr_may_create(flags))
6c17675e1   Jeff Mahoney   reiserfs: simplif...
364
365
  			err = xattr_create(xadir->d_inode, xafile,
  					      0700|S_IFREG);
a72bdb1cd   Jeff Mahoney   reiserfs: Clean u...
366
  	}
6c17675e1   Jeff Mahoney   reiserfs: simplif...
367
368
  	if (err)
  		dput(xafile);
a72bdb1cd   Jeff Mahoney   reiserfs: Clean u...
369
  out:
5a6059c35   Jeff Mahoney   reiserfs: Expand ...
370
  	mutex_unlock(&xadir->d_inode->i_mutex);
a72bdb1cd   Jeff Mahoney   reiserfs: Clean u...
371
372
  	dput(xadir);
  	if (err)
6c17675e1   Jeff Mahoney   reiserfs: simplif...
373
  		return ERR_PTR(err);
a72bdb1cd   Jeff Mahoney   reiserfs: Clean u...
374
  	return xafile;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
375
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
376
  /* Internal operations on file data */
bd4c625c0   Linus Torvalds   reiserfs: run scr...
377
  static inline void reiserfs_put_page(struct page *page)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
378
  {
bd4c625c0   Linus Torvalds   reiserfs: run scr...
379
380
  	kunmap(page);
  	page_cache_release(page);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
381
  }
ec6ea56b2   Jeff Mahoney   reiserfs: xattr r...
382
  static struct page *reiserfs_get_page(struct inode *dir, size_t n)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
383
  {
bd4c625c0   Linus Torvalds   reiserfs: run scr...
384
385
386
  	struct address_space *mapping = dir->i_mapping;
  	struct page *page;
  	/* We can deadlock if we try to free dentries,
25985edce   Lucas De Marchi   Fix common misspe...
387
  	   and an unlink/rmdir has just occurred - GFP_NOFS avoids this */
c4cdd0383   Al Viro   [PATCH] gfp_t: re...
388
  	mapping_set_gfp_mask(mapping, GFP_NOFS);
ec6ea56b2   Jeff Mahoney   reiserfs: xattr r...
389
  	page = read_mapping_page(mapping, n >> PAGE_CACHE_SHIFT, NULL);
bd4c625c0   Linus Torvalds   reiserfs: run scr...
390
  	if (!IS_ERR(page)) {
bd4c625c0   Linus Torvalds   reiserfs: run scr...
391
  		kmap(page);
bd4c625c0   Linus Torvalds   reiserfs: run scr...
392
393
394
395
396
397
398
399
  		if (PageError(page))
  			goto fail;
  	}
  	return page;
  
        fail:
  	reiserfs_put_page(page);
  	return ERR_PTR(-EIO);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
400
  }
bd4c625c0   Linus Torvalds   reiserfs: run scr...
401
  static inline __u32 xattr_hash(const char *msg, int len)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
402
  {
bd4c625c0   Linus Torvalds   reiserfs: run scr...
403
  	return csum_partial(msg, len, 0);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
404
  }
ba9d8cec6   Vladimir Saveliev   reiserfs: convert...
405
406
  int reiserfs_commit_write(struct file *f, struct page *page,
  			  unsigned from, unsigned to);
ba9d8cec6   Vladimir Saveliev   reiserfs: convert...
407

48b32a355   Jeff Mahoney   reiserfs: use gen...
408
409
410
  static void update_ctime(struct inode *inode)
  {
  	struct timespec now = current_fs_time(inode->i_sb);
1d3382cbf   Al Viro   new helper: inode...
411
  	if (inode_unhashed(inode) || !inode->i_nlink ||
48b32a355   Jeff Mahoney   reiserfs: use gen...
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
  	    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);
5a6059c35   Jeff Mahoney   reiserfs: Expand ...
427
  	mutex_lock_nested(&xadir->d_inode->i_mutex, I_MUTEX_XATTR);
48b32a355   Jeff Mahoney   reiserfs: use gen...
428
429
430
431
432
433
434
  	dentry = lookup_one_len(name, xadir, strlen(name));
  	if (IS_ERR(dentry)) {
  		err = PTR_ERR(dentry);
  		goto out_dput;
  	}
  
  	if (dentry->d_inode) {
4f3be1b5a   Frederic Weisbecker   reiserfs: Relax l...
435
  		reiserfs_write_lock(inode->i_sb);
48b32a355   Jeff Mahoney   reiserfs: use gen...
436
  		err = xattr_unlink(xadir->d_inode, dentry);
4f3be1b5a   Frederic Weisbecker   reiserfs: Relax l...
437
  		reiserfs_write_unlock(inode->i_sb);
48b32a355   Jeff Mahoney   reiserfs: use gen...
438
439
440
441
442
  		update_ctime(inode);
  	}
  
  	dput(dentry);
  out_dput:
5a6059c35   Jeff Mahoney   reiserfs: Expand ...
443
  	mutex_unlock(&xadir->d_inode->i_mutex);
48b32a355   Jeff Mahoney   reiserfs: use gen...
444
445
446
  	dput(xadir);
  	return err;
  }
ba9d8cec6   Vladimir Saveliev   reiserfs: convert...
447

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
448
449
450
  /* Generic extended attribute operations that can be used by xa plugins */
  
  /*
1b1dcc1b5   Jes Sorensen   [PATCH] mutex sub...
451
   * inode->i_mutex: down
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
452
453
   */
  int
0ab2621eb   Jeff Mahoney   reiserfs: journal...
454
455
456
  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
457
  {
bd4c625c0   Linus Torvalds   reiserfs: run scr...
458
  	int err = 0;
3227e14c3   Jeff Mahoney   [PATCH] reiserfs:...
459
  	struct dentry *dentry;
bd4c625c0   Linus Torvalds   reiserfs: run scr...
460
461
  	struct page *page;
  	char *data;
bd4c625c0   Linus Torvalds   reiserfs: run scr...
462
463
  	size_t file_pos = 0;
  	size_t buffer_pos = 0;
48b32a355   Jeff Mahoney   reiserfs: use gen...
464
  	size_t new_size;
bd4c625c0   Linus Torvalds   reiserfs: run scr...
465
  	__u32 xahash = 0;
bd4c625c0   Linus Torvalds   reiserfs: run scr...
466
467
  	if (get_inode_sd_version(inode) == STAT_DATA_V1)
  		return -EOPNOTSUPP;
3f14fea6b   Frederic Weisbecker   reiserfs: Relax l...
468
  	reiserfs_write_unlock(inode->i_sb);
4f3be1b5a   Frederic Weisbecker   reiserfs: Relax l...
469
470
471
472
473
474
  
  	if (!buffer) {
  		err = lookup_and_delete_xattr(inode, name);
  		reiserfs_write_lock(inode->i_sb);
  		return err;
  	}
48b32a355   Jeff Mahoney   reiserfs: use gen...
475
  	dentry = xattr_lookup(inode, name, flags);
3f14fea6b   Frederic Weisbecker   reiserfs: Relax l...
476
477
  	if (IS_ERR(dentry)) {
  		reiserfs_write_lock(inode->i_sb);
48b32a355   Jeff Mahoney   reiserfs: use gen...
478
  		return PTR_ERR(dentry);
3f14fea6b   Frederic Weisbecker   reiserfs: Relax l...
479
  	}
f3e22f48f   Frederic Weisbecker   reiserfs: Fix mis...
480
  	down_write(&REISERFS_I(inode)->i_xattr_sem);
bd4c625c0   Linus Torvalds   reiserfs: run scr...
481

3f14fea6b   Frederic Weisbecker   reiserfs: Relax l...
482
  	reiserfs_write_lock(inode->i_sb);
bd4c625c0   Linus Torvalds   reiserfs: run scr...
483

8b6dd72a4   Jeff Mahoney   reiserfs: make pe...
484
  	xahash = xattr_hash(buffer, buffer_size);
bd4c625c0   Linus Torvalds   reiserfs: run scr...
485
486
487
488
489
490
491
492
  	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;
ec6ea56b2   Jeff Mahoney   reiserfs: xattr r...
493
  		page = reiserfs_get_page(dentry->d_inode, file_pos);
bd4c625c0   Linus Torvalds   reiserfs: run scr...
494
495
  		if (IS_ERR(page)) {
  			err = PTR_ERR(page);
48b32a355   Jeff Mahoney   reiserfs: use gen...
496
  			goto out_unlock;
bd4c625c0   Linus Torvalds   reiserfs: run scr...
497
498
499
500
501
502
503
504
505
506
507
508
509
510
  		}
  
  		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);
  		}
ebdec241d   Christoph Hellwig   fs: kill block_pr...
511
  		err = __reiserfs_write_begin(page, page_offset, chunk + skip);
bd4c625c0   Linus Torvalds   reiserfs: run scr...
512
513
514
  		if (!err) {
  			if (buffer)
  				memcpy(data + skip, buffer + buffer_pos, chunk);
3227e14c3   Jeff Mahoney   [PATCH] reiserfs:...
515
516
517
  			err = reiserfs_commit_write(NULL, page, page_offset,
  						    page_offset + chunk +
  						    skip);
bd4c625c0   Linus Torvalds   reiserfs: run scr...
518
519
520
521
522
523
524
525
526
  		}
  		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...
527
528
529
530
  	new_size = buffer_size + sizeof(struct reiserfs_xattr_header);
  	if (!err && new_size < i_size_read(dentry->d_inode)) {
  		struct iattr newattrs = {
  			.ia_ctime = current_fs_time(inode->i_sb),
fb2162df7   Jeff Mahoney   reiserfs: fix cor...
531
  			.ia_size = new_size,
48b32a355   Jeff Mahoney   reiserfs: use gen...
532
533
  			.ia_valid = ATTR_SIZE | ATTR_CTIME,
  		};
31370f62b   Frederic Weisbecker   reiserfs: Relax r...
534
535
  
  		reiserfs_write_unlock(inode->i_sb);
48b32a355   Jeff Mahoney   reiserfs: use gen...
536
  		mutex_lock_nested(&dentry->d_inode->i_mutex, I_MUTEX_XATTR);
bd5fe6c5e   Christoph Hellwig   fs: kill i_alloc_sem
537
  		inode_dio_wait(dentry->d_inode);
31370f62b   Frederic Weisbecker   reiserfs: Relax r...
538
  		reiserfs_write_lock(inode->i_sb);
48b32a355   Jeff Mahoney   reiserfs: use gen...
539
  		err = reiserfs_setattr(dentry, &newattrs);
48b32a355   Jeff Mahoney   reiserfs: use gen...
540
541
542
543
  		mutex_unlock(&dentry->d_inode->i_mutex);
  	} else
  		update_ctime(inode);
  out_unlock:
8b6dd72a4   Jeff Mahoney   reiserfs: make pe...
544
  	up_write(&REISERFS_I(inode)->i_xattr_sem);
3227e14c3   Jeff Mahoney   [PATCH] reiserfs:...
545
  	dput(dentry);
48b32a355   Jeff Mahoney   reiserfs: use gen...
546
547
  	return err;
  }
bd4c625c0   Linus Torvalds   reiserfs: run scr...
548

0ab2621eb   Jeff Mahoney   reiserfs: journal...
549
550
551
  /* 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...
552
  {
0ab2621eb   Jeff Mahoney   reiserfs: journal...
553
554
555
556
557
558
559
560
561
562
563
564
565
  
  	struct reiserfs_transaction_handle th;
  	int error, error2;
  	size_t jbegin_count = reiserfs_xattr_nblocks(inode, buffer_size);
  
  	if (!(flags & XATTR_REPLACE))
  		jbegin_count += reiserfs_xattr_jcreate_nblocks(inode);
  
  	reiserfs_write_lock(inode->i_sb);
  	error = journal_begin(&th, inode->i_sb, jbegin_count);
  	if (error) {
  		reiserfs_write_unlock(inode->i_sb);
  		return error;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
566
  	}
bd4c625c0   Linus Torvalds   reiserfs: run scr...
567

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

0ab2621eb   Jeff Mahoney   reiserfs: journal...
571
572
573
574
575
576
  	error2 = journal_end(&th, inode->i_sb, jbegin_count);
  	if (error == 0)
  		error = error2;
  	reiserfs_write_unlock(inode->i_sb);
  
  	return error;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
577
578
579
  }
  
  /*
1b1dcc1b5   Jes Sorensen   [PATCH] mutex sub...
580
   * inode->i_mutex: down
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
581
582
   */
  int
48b32a355   Jeff Mahoney   reiserfs: use gen...
583
  reiserfs_xattr_get(struct inode *inode, const char *name, void *buffer,
bd4c625c0   Linus Torvalds   reiserfs: run scr...
584
  		   size_t buffer_size)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
585
  {
bd4c625c0   Linus Torvalds   reiserfs: run scr...
586
  	ssize_t err = 0;
3227e14c3   Jeff Mahoney   [PATCH] reiserfs:...
587
  	struct dentry *dentry;
bd4c625c0   Linus Torvalds   reiserfs: run scr...
588
589
590
591
  	size_t isize;
  	size_t file_pos = 0;
  	size_t buffer_pos = 0;
  	struct page *page;
bd4c625c0   Linus Torvalds   reiserfs: run scr...
592
593
594
595
596
597
598
599
600
  	__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;
48b32a355   Jeff Mahoney   reiserfs: use gen...
601
  	dentry = xattr_lookup(inode, name, XATTR_REPLACE);
3227e14c3   Jeff Mahoney   [PATCH] reiserfs:...
602
603
  	if (IS_ERR(dentry)) {
  		err = PTR_ERR(dentry);
bd4c625c0   Linus Torvalds   reiserfs: run scr...
604
605
  		goto out;
  	}
8b6dd72a4   Jeff Mahoney   reiserfs: make pe...
606
  	down_read(&REISERFS_I(inode)->i_xattr_sem);
d984561b3   Jeff Mahoney   reiserfs: elimina...
607

f437c529e   Jeff Mahoney   reiserfs: small v...
608
  	isize = i_size_read(dentry->d_inode);
bd4c625c0   Linus Torvalds   reiserfs: run scr...
609
610
611
612
  
  	/* Just return the size needed */
  	if (buffer == NULL) {
  		err = isize - sizeof(struct reiserfs_xattr_header);
8b6dd72a4   Jeff Mahoney   reiserfs: make pe...
613
  		goto out_unlock;
bd4c625c0   Linus Torvalds   reiserfs: run scr...
614
615
616
617
  	}
  
  	if (buffer_size < isize - sizeof(struct reiserfs_xattr_header)) {
  		err = -ERANGE;
8b6dd72a4   Jeff Mahoney   reiserfs: make pe...
618
  		goto out_unlock;
bd4c625c0   Linus Torvalds   reiserfs: run scr...
619
620
621
622
623
624
625
626
627
628
  	}
  
  	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;
a72bdb1cd   Jeff Mahoney   reiserfs: Clean u...
629
  		page = reiserfs_get_page(dentry->d_inode, file_pos);
bd4c625c0   Linus Torvalds   reiserfs: run scr...
630
631
  		if (IS_ERR(page)) {
  			err = PTR_ERR(page);
8b6dd72a4   Jeff Mahoney   reiserfs: make pe...
632
  			goto out_unlock;
bd4c625c0   Linus Torvalds   reiserfs: run scr...
633
634
635
636
637
638
639
640
641
642
643
644
645
  		}
  
  		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...
646
  				reiserfs_warning(inode->i_sb, "jdm-20001",
bd4c625c0   Linus Torvalds   reiserfs: run scr...
647
648
649
650
  						 "Invalid magic for xattr (%s) "
  						 "associated with %k", name,
  						 INODE_PKEY(inode));
  				err = -EIO;
8b6dd72a4   Jeff Mahoney   reiserfs: make pe...
651
  				goto out_unlock;
bd4c625c0   Linus Torvalds   reiserfs: run scr...
652
653
654
655
656
657
658
659
660
661
662
663
664
665
  			}
  			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...
666
  		reiserfs_warning(inode->i_sb, "jdm-20002",
bd4c625c0   Linus Torvalds   reiserfs: run scr...
667
668
669
670
  				 "Invalid hash for xattr (%s) associated "
  				 "with %k", name, INODE_PKEY(inode));
  		err = -EIO;
  	}
8b6dd72a4   Jeff Mahoney   reiserfs: make pe...
671
672
  out_unlock:
  	up_read(&REISERFS_I(inode)->i_xattr_sem);
3227e14c3   Jeff Mahoney   [PATCH] reiserfs:...
673
  	dput(dentry);
bd4c625c0   Linus Torvalds   reiserfs: run scr...
674

a72bdb1cd   Jeff Mahoney   reiserfs: Clean u...
675
  out:
bd4c625c0   Linus Torvalds   reiserfs: run scr...
676
  	return err;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
677
  }
48b32a355   Jeff Mahoney   reiserfs: use gen...
678
679
680
681
682
683
684
685
686
687
688
689
690
  /*
   * 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
691

48b32a355   Jeff Mahoney   reiserfs: use gen...
692
  /* This is the implementation for the xattr plugin infrastructure */
94d09a98c   Stephen Hemminger   reiserfs: constif...
693
694
  static inline const struct xattr_handler *
  find_xattr_handler_prefix(const struct xattr_handler **handlers,
48b32a355   Jeff Mahoney   reiserfs: use gen...
695
  			   const char *name)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
696
  {
94d09a98c   Stephen Hemminger   reiserfs: constif...
697
  	const struct xattr_handler *xah;
bd4c625c0   Linus Torvalds   reiserfs: run scr...
698

48b32a355   Jeff Mahoney   reiserfs: use gen...
699
700
  	if (!handlers)
  		return NULL;
bd4c625c0   Linus Torvalds   reiserfs: run scr...
701

48b32a355   Jeff Mahoney   reiserfs: use gen...
702
703
704
  	for_each_xattr_handler(handlers, xah) {
  		if (strncmp(xah->prefix, name, strlen(xah->prefix)) == 0)
  			break;
bd4c625c0   Linus Torvalds   reiserfs: run scr...
705
  	}
48b32a355   Jeff Mahoney   reiserfs: use gen...
706
  	return xah;
bd4c625c0   Linus Torvalds   reiserfs: run scr...
707
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
708

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
709
710
711
  
  /*
   * Inode operation getxattr()
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
712
713
   */
  ssize_t
bd4c625c0   Linus Torvalds   reiserfs: run scr...
714
715
  reiserfs_getxattr(struct dentry * dentry, const char *name, void *buffer,
  		  size_t size)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
716
  {
94d09a98c   Stephen Hemminger   reiserfs: constif...
717
  	const struct xattr_handler *handler;
bd4c625c0   Linus Torvalds   reiserfs: run scr...
718

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

431547b3c   Christoph Hellwig   sanitize xattr ha...
721
  	if (!handler || get_inode_sd_version(dentry->d_inode) == STAT_DATA_V1)
bd4c625c0   Linus Torvalds   reiserfs: run scr...
722
  		return -EOPNOTSUPP;
431547b3c   Christoph Hellwig   sanitize xattr ha...
723
  	return handler->get(dentry, name, buffer, size, handler->flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
724
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
725
726
727
  /*
   * Inode operation setxattr()
   *
1b1dcc1b5   Jes Sorensen   [PATCH] mutex sub...
728
   * dentry->d_inode->i_mutex down
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
729
730
   */
  int
bd4c625c0   Linus Torvalds   reiserfs: run scr...
731
732
  reiserfs_setxattr(struct dentry *dentry, const char *name, const void *value,
  		  size_t size, int flags)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
733
  {
94d09a98c   Stephen Hemminger   reiserfs: constif...
734
  	const struct xattr_handler *handler;
bd4c625c0   Linus Torvalds   reiserfs: run scr...
735

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

431547b3c   Christoph Hellwig   sanitize xattr ha...
738
  	if (!handler || get_inode_sd_version(dentry->d_inode) == STAT_DATA_V1)
bd4c625c0   Linus Torvalds   reiserfs: run scr...
739
  		return -EOPNOTSUPP;
431547b3c   Christoph Hellwig   sanitize xattr ha...
740
  	return handler->set(dentry, name, value, size, flags, handler->flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
741
742
743
744
745
  }
  
  /*
   * Inode operation removexattr()
   *
1b1dcc1b5   Jes Sorensen   [PATCH] mutex sub...
746
   * dentry->d_inode->i_mutex down
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
747
   */
bd4c625c0   Linus Torvalds   reiserfs: run scr...
748
  int reiserfs_removexattr(struct dentry *dentry, const char *name)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
749
  {
94d09a98c   Stephen Hemminger   reiserfs: constif...
750
  	const struct xattr_handler *handler;
431547b3c   Christoph Hellwig   sanitize xattr ha...
751
  	handler = find_xattr_handler_prefix(dentry->d_sb->s_xattr, name);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
752

431547b3c   Christoph Hellwig   sanitize xattr ha...
753
  	if (!handler || get_inode_sd_version(dentry->d_inode) == STAT_DATA_V1)
bd4c625c0   Linus Torvalds   reiserfs: run scr...
754
  		return -EOPNOTSUPP;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
755

431547b3c   Christoph Hellwig   sanitize xattr ha...
756
  	return handler->set(dentry, name, NULL, 0, XATTR_REPLACE, handler->flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
757
  }
48b32a355   Jeff Mahoney   reiserfs: use gen...
758
759
760
761
  struct listxattr_buf {
  	size_t size;
  	size_t pos;
  	char *buf;
431547b3c   Christoph Hellwig   sanitize xattr ha...
762
  	struct dentry *dentry;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
763
  };
48b32a355   Jeff Mahoney   reiserfs: use gen...
764
765
  static int listxattr_filler(void *buf, const char *name, int namelen,
  			    loff_t offset, u64 ino, unsigned int d_type)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
766
  {
48b32a355   Jeff Mahoney   reiserfs: use gen...
767
768
769
770
  	struct listxattr_buf *b = (struct listxattr_buf *)buf;
  	size_t size;
  	if (name[0] != '.' ||
  	    (namelen != 1 && (name[1] != '.' || namelen != 2))) {
94d09a98c   Stephen Hemminger   reiserfs: constif...
771
  		const struct xattr_handler *handler;
431547b3c   Christoph Hellwig   sanitize xattr ha...
772
  		handler = find_xattr_handler_prefix(b->dentry->d_sb->s_xattr,
48b32a355   Jeff Mahoney   reiserfs: use gen...
773
774
775
776
  						    name);
  		if (!handler)	/* Unsupported xattr name */
  			return 0;
  		if (b->buf) {
431547b3c   Christoph Hellwig   sanitize xattr ha...
777
778
779
  			size = handler->list(b->dentry, b->buf + b->pos,
  					 b->size, name, namelen,
  					 handler->flags);
48b32a355   Jeff Mahoney   reiserfs: use gen...
780
781
782
  			if (size > b->size)
  				return -ERANGE;
  		} else {
431547b3c   Christoph Hellwig   sanitize xattr ha...
783
784
  			size = handler->list(b->dentry, NULL, 0, name,
  					     namelen, handler->flags);
bd4c625c0   Linus Torvalds   reiserfs: run scr...
785
  		}
bd4c625c0   Linus Torvalds   reiserfs: run scr...
786

48b32a355   Jeff Mahoney   reiserfs: use gen...
787
788
  		b->pos += size;
  	}
bd4c625c0   Linus Torvalds   reiserfs: run scr...
789
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
790
  }
bd4c625c0   Linus Torvalds   reiserfs: run scr...
791

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
792
793
794
  /*
   * Inode operation listxattr()
   *
48b32a355   Jeff Mahoney   reiserfs: use gen...
795
796
797
   * 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
798
   */
bd4c625c0   Linus Torvalds   reiserfs: run scr...
799
  ssize_t reiserfs_listxattr(struct dentry * dentry, char *buffer, size_t size)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
800
  {
bd4c625c0   Linus Torvalds   reiserfs: run scr...
801
802
  	struct dentry *dir;
  	int err = 0;
a41f1a471   Jeff Mahoney   reiserfs: use gen...
803
  	loff_t pos = 0;
48b32a355   Jeff Mahoney   reiserfs: use gen...
804
  	struct listxattr_buf buf = {
431547b3c   Christoph Hellwig   sanitize xattr ha...
805
  		.dentry = dentry,
48b32a355   Jeff Mahoney   reiserfs: use gen...
806
807
808
  		.buf = buffer,
  		.size = buffer ? size : 0,
  	};
bd4c625c0   Linus Torvalds   reiserfs: run scr...
809
810
811
  
  	if (!dentry->d_inode)
  		return -EINVAL;
677c9b2e3   Jeff Mahoney   reiserfs: remove ...
812
  	if (!dentry->d_sb->s_xattr ||
bd4c625c0   Linus Torvalds   reiserfs: run scr...
813
814
  	    get_inode_sd_version(dentry->d_inode) == STAT_DATA_V1)
  		return -EOPNOTSUPP;
6c17675e1   Jeff Mahoney   reiserfs: simplif...
815
  	dir = open_xa_dir(dentry->d_inode, XATTR_REPLACE);
bd4c625c0   Linus Torvalds   reiserfs: run scr...
816
817
818
  	if (IS_ERR(dir)) {
  		err = PTR_ERR(dir);
  		if (err == -ENODATA)
48b32a355   Jeff Mahoney   reiserfs: use gen...
819
  			err = 0;  /* Not an error if there aren't any xattrs */
bd4c625c0   Linus Torvalds   reiserfs: run scr...
820
821
  		goto out;
  	}
6c17675e1   Jeff Mahoney   reiserfs: simplif...
822
  	mutex_lock_nested(&dir->d_inode->i_mutex, I_MUTEX_XATTR);
a41f1a471   Jeff Mahoney   reiserfs: use gen...
823
  	err = reiserfs_readdir_dentry(dir, &buf, listxattr_filler, &pos);
6c17675e1   Jeff Mahoney   reiserfs: simplif...
824
  	mutex_unlock(&dir->d_inode->i_mutex);
bd4c625c0   Linus Torvalds   reiserfs: run scr...
825

48b32a355   Jeff Mahoney   reiserfs: use gen...
826
827
  	if (!err)
  		err = buf.pos;
bd4c625c0   Linus Torvalds   reiserfs: run scr...
828

3227e14c3   Jeff Mahoney   [PATCH] reiserfs:...
829
  	dput(dir);
8b6dd72a4   Jeff Mahoney   reiserfs: make pe...
830
  out:
bd4c625c0   Linus Torvalds   reiserfs: run scr...
831
  	return err;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
832
  }
a72bdb1cd   Jeff Mahoney   reiserfs: Clean u...
833
  static int create_privroot(struct dentry *dentry)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
834
  {
a72bdb1cd   Jeff Mahoney   reiserfs: Clean u...
835
836
  	int err;
  	struct inode *inode = dentry->d_parent->d_inode;
5a6059c35   Jeff Mahoney   reiserfs: Expand ...
837
  	WARN_ON_ONCE(!mutex_is_locked(&inode->i_mutex));
6c17675e1   Jeff Mahoney   reiserfs: simplif...
838
  	err = xattr_mkdir(inode, dentry, 0700);
edcc37a04   Al Viro   Always lookup pri...
839
840
841
842
843
844
  	if (err || !dentry->d_inode) {
  		reiserfs_warning(dentry->d_sb, "jdm-20006",
  				 "xattrs/ACLs enabled and couldn't "
  				 "find/create .reiserfs_priv. "
  				 "Failing mount.");
  		return -EOPNOTSUPP;
bd4c625c0   Linus Torvalds   reiserfs: run scr...
845
  	}
edcc37a04   Al Viro   Always lookup pri...
846
847
848
849
  	dentry->d_inode->i_flags |= S_PRIVATE;
  	reiserfs_info(dentry->d_sb, "Created %s - reserved for xattr "
  		      "storage.
  ", PRIVROOT_NAME);
bd4c625c0   Linus Torvalds   reiserfs: run scr...
850

edcc37a04   Al Viro   Always lookup pri...
851
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
852
  }
12abb35a0   Jeff Mahoney   reiserfs: clean u...
853
854
855
856
857
858
859
  #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 */
94d09a98c   Stephen Hemminger   reiserfs: constif...
860
  const struct xattr_handler *reiserfs_xattr_handlers[] = {
12abb35a0   Jeff Mahoney   reiserfs: clean u...
861
862
863
864
865
866
867
868
869
870
871
872
873
  #ifdef CONFIG_REISERFS_FS_XATTR
  	&reiserfs_xattr_user_handler,
  	&reiserfs_xattr_trusted_handler,
  #endif
  #ifdef CONFIG_REISERFS_FS_SECURITY
  	&reiserfs_xattr_security_handler,
  #endif
  #ifdef CONFIG_REISERFS_FS_POSIX_ACL
  	&reiserfs_posix_acl_access_handler,
  	&reiserfs_posix_acl_default_handler,
  #endif
  	NULL
  };
a72bdb1cd   Jeff Mahoney   reiserfs: Clean u...
874
  static int xattr_mount_check(struct super_block *s)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
875
  {
a72bdb1cd   Jeff Mahoney   reiserfs: Clean u...
876
877
  	/* 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...
878
879
880
881
882
883
884
885
886
887
  	if (old_format_only(s)) {
  		if (reiserfs_xattrs_optional(s)) {
  			/* Old format filesystem, but optional xattrs have
  			 * been enabled. Error out. */
  			reiserfs_warning(s, "jdm-2005",
  					 "xattrs/ACLs not supported "
  					 "on pre-v3.6 format filesystems. "
  					 "Failing mount.");
  			return -EOPNOTSUPP;
  		}
a72bdb1cd   Jeff Mahoney   reiserfs: Clean u...
888
889
890
  	}
  
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
891
  }
10556cb21   Al Viro   ->permission() sa...
892
  int reiserfs_permission(struct inode *inode, int mask)
b83674c0d   Jeff Mahoney   reiserfs: fixup p...
893
894
895
896
897
898
899
  {
  	/*
  	 * 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...
900
  	return generic_permission(inode, mask);
b83674c0d   Jeff Mahoney   reiserfs: fixup p...
901
  }
cac36f707   Jeff Mahoney   reiserfs: fix per...
902
  static int xattr_hide_revalidate(struct dentry *dentry, struct nameidata *nd)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
903
  {
cac36f707   Jeff Mahoney   reiserfs: fix per...
904
  	return -EPERM;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
905
  }
e16404ed0   Al Viro   constify dentry_o...
906
  static const struct dentry_operations xattr_lookup_poison_ops = {
cac36f707   Jeff Mahoney   reiserfs: fix per...
907
  	.d_revalidate = xattr_hide_revalidate,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
908
  };
edcc37a04   Al Viro   Always lookup pri...
909
910
911
912
913
914
  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 */
c72e05756   Frederic Weisbecker   kill-the-bkl/reis...
915
  	reiserfs_mutex_lock_safe(&s->s_root->d_inode->i_mutex, s);
edcc37a04   Al Viro   Always lookup pri...
916
917
918
919
  	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...
920
  		d_set_d_op(dentry, &xattr_lookup_poison_ops);
edcc37a04   Al Viro   Always lookup pri...
921
922
923
924
925
926
927
928
  		if (dentry->d_inode)
  			dentry->d_inode->i_flags |= S_PRIVATE;
  	} else
  		err = PTR_ERR(dentry);
  	mutex_unlock(&s->s_root->d_inode->i_mutex);
  
  	return err;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
929
930
931
  /* 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 */
bd4c625c0   Linus Torvalds   reiserfs: run scr...
932
  int reiserfs_xattr_init(struct super_block *s, int mount_flags)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
933
  {
bd4c625c0   Linus Torvalds   reiserfs: run scr...
934
  	int err = 0;
ab17c4f02   Jeff Mahoney   reiserfs: fixup x...
935
  	struct dentry *privroot = REISERFS_SB(s)->priv_root;
bd4c625c0   Linus Torvalds   reiserfs: run scr...
936

a72bdb1cd   Jeff Mahoney   reiserfs: Clean u...
937
938
  	err = xattr_mount_check(s);
  	if (err)
bd4c625c0   Linus Torvalds   reiserfs: run scr...
939
  		goto error;
bd4c625c0   Linus Torvalds   reiserfs: run scr...
940

ab17c4f02   Jeff Mahoney   reiserfs: fixup x...
941
  	if (!privroot->d_inode && !(mount_flags & MS_RDONLY)) {
ae635c0bb   Frederic Weisbecker   kill-the-bkl/reis...
942
  		reiserfs_mutex_lock_safe(&s->s_root->d_inode->i_mutex, s);
edcc37a04   Al Viro   Always lookup pri...
943
  		err = create_privroot(REISERFS_SB(s)->priv_root);
5a6059c35   Jeff Mahoney   reiserfs: Expand ...
944
  		mutex_unlock(&s->s_root->d_inode->i_mutex);
bd4c625c0   Linus Torvalds   reiserfs: run scr...
945
  	}
ab17c4f02   Jeff Mahoney   reiserfs: fixup x...
946
947
  
  	if (privroot->d_inode) {
48b32a355   Jeff Mahoney   reiserfs: use gen...
948
  		s->s_xattr = reiserfs_xattr_handlers;
c72e05756   Frederic Weisbecker   kill-the-bkl/reis...
949
  		reiserfs_mutex_lock_safe(&privroot->d_inode->i_mutex, s);
ab17c4f02   Jeff Mahoney   reiserfs: fixup x...
950
951
952
953
954
955
956
957
958
959
960
  		if (!REISERFS_SB(s)->xattr_root) {
  			struct dentry *dentry;
  			dentry = lookup_one_len(XAROOT_NAME, privroot,
  						strlen(XAROOT_NAME));
  			if (!IS_ERR(dentry))
  				REISERFS_SB(s)->xattr_root = dentry;
  			else
  				err = PTR_ERR(dentry);
  		}
  		mutex_unlock(&privroot->d_inode->i_mutex);
  	}
48b32a355   Jeff Mahoney   reiserfs: use gen...
961

a72bdb1cd   Jeff Mahoney   reiserfs: Clean u...
962
  error:
bd4c625c0   Linus Torvalds   reiserfs: run scr...
963
  	if (err) {
bd4c625c0   Linus Torvalds   reiserfs: run scr...
964
965
966
967
968
  		clear_bit(REISERFS_XATTRS_USER, &(REISERFS_SB(s)->s_mount_opt));
  		clear_bit(REISERFS_POSIXACL, &(REISERFS_SB(s)->s_mount_opt));
  	}
  
  	/* The super_block MS_POSIXACL must mirror the (no)acl mount option. */
bd4c625c0   Linus Torvalds   reiserfs: run scr...
969
970
  	if (reiserfs_posixacl(s))
  		s->s_flags |= MS_POSIXACL;
ab17c4f02   Jeff Mahoney   reiserfs: fixup x...
971
  	else
ab17c4f02   Jeff Mahoney   reiserfs: fixup x...
972
  		s->s_flags &= ~MS_POSIXACL;
bd4c625c0   Linus Torvalds   reiserfs: run scr...
973
974
  
  	return err;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
975
  }