]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - fs/reiserfs/xattr.c
reiserfs: Fix unreachable statement
[mirror_ubuntu-bionic-kernel.git] / fs / reiserfs / xattr.c
CommitLineData
1da177e4
LT
1/*
2 * linux/fs/reiserfs/xattr.c
3 *
4 * Copyright (c) 2002 by Jeff Mahoney, <jeffm@suse.com>
5 *
6 */
7
8/*
9 * In order to implement EA/ACLs in a clean, backwards compatible manner,
10 * they are implemented as files in a "private" directory.
11 * Each EA is in it's own file, with the directory layout like so (/ is assumed
12 * to be relative to fs root). Inside the /.reiserfs_priv/xattrs directory,
13 * directories named using the capital-hex form of the objectid and
14 * generation number are used. Inside each directory are individual files
15 * named with the name of the extended attribute.
16 *
17 * So, for objectid 12648430, we could have:
18 * /.reiserfs_priv/xattrs/C0FFEE.0/system.posix_acl_access
19 * /.reiserfs_priv/xattrs/C0FFEE.0/system.posix_acl_default
20 * /.reiserfs_priv/xattrs/C0FFEE.0/user.Content-Type
21 * .. or similar.
22 *
23 * The file contents are the text of the EA. The size is known based on the
24 * stat data describing the file.
25 *
26 * In the case of system.posix_acl_access and system.posix_acl_default, since
27 * these are special cases for filesystem ACLs, they are interpreted by the
28 * kernel, in addition, they are negatively and positively cached and attached
29 * to the inode so that unnecessary lookups are avoided.
d984561b
JM
30 *
31 * Locking works like so:
8b6dd72a
JM
32 * Directory components (xattr root, xattr dir) are protectd by their i_mutex.
33 * The xattrs themselves are protected by the xattr_sem.
1da177e4
LT
34 */
35
36#include <linux/reiserfs_fs.h>
16f7e0fe 37#include <linux/capability.h>
1da177e4
LT
38#include <linux/dcache.h>
39#include <linux/namei.h>
40#include <linux/errno.h>
41#include <linux/fs.h>
42#include <linux/file.h>
43#include <linux/pagemap.h>
44#include <linux/xattr.h>
45#include <linux/reiserfs_xattr.h>
46#include <linux/reiserfs_acl.h>
1da177e4 47#include <asm/uaccess.h>
3277c39f 48#include <net/checksum.h>
1da177e4 49#include <linux/stat.h>
6c17675e 50#include <linux/quotaops.h>
1da177e4 51
1da177e4
LT
52#define PRIVROOT_NAME ".reiserfs_priv"
53#define XAROOT_NAME "xattrs"
54
1da177e4 55
6c17675e
JM
56/* Helpers for inode ops. We do this so that we don't have all the VFS
57 * overhead and also for proper i_mutex annotation.
58 * dir->i_mutex must be held for all of them. */
3a355cc6 59#ifdef CONFIG_REISERFS_FS_XATTR
6c17675e 60static int xattr_create(struct inode *dir, struct dentry *dentry, int mode)
1da177e4 61{
6c17675e 62 BUG_ON(!mutex_is_locked(&dir->i_mutex));
e1c50248 63 vfs_dq_init(dir);
6c17675e
JM
64 return dir->i_op->create(dir, dentry, mode, NULL);
65}
3a355cc6 66#endif
bd4c625c 67
6c17675e
JM
68static int xattr_mkdir(struct inode *dir, struct dentry *dentry, int mode)
69{
70 BUG_ON(!mutex_is_locked(&dir->i_mutex));
e1c50248 71 vfs_dq_init(dir);
6c17675e
JM
72 return dir->i_op->mkdir(dir, dentry, mode);
73}
bd4c625c 74
6c17675e
JM
75/* We use I_MUTEX_CHILD here to silence lockdep. It's safe because xattr
76 * mutation ops aren't called during rename or splace, which are the
77 * only other users of I_MUTEX_CHILD. It violates the ordering, but that's
78 * better than allocating another subclass just for this code. */
79static int xattr_unlink(struct inode *dir, struct dentry *dentry)
80{
81 int error;
82 BUG_ON(!mutex_is_locked(&dir->i_mutex));
e1c50248 83 vfs_dq_init(dir);
bd4c625c 84
4dd85969
FW
85 reiserfs_mutex_lock_nested_safe(&dentry->d_inode->i_mutex,
86 I_MUTEX_CHILD, dir->i_sb);
6c17675e
JM
87 error = dir->i_op->unlink(dir, dentry);
88 mutex_unlock(&dentry->d_inode->i_mutex);
89
90 if (!error)
91 d_delete(dentry);
92 return error;
93}
94
95static int xattr_rmdir(struct inode *dir, struct dentry *dentry)
96{
97 int error;
98 BUG_ON(!mutex_is_locked(&dir->i_mutex));
e1c50248 99 vfs_dq_init(dir);
6c17675e 100
835d5247
FW
101 reiserfs_mutex_lock_nested_safe(&dentry->d_inode->i_mutex,
102 I_MUTEX_CHILD, dir->i_sb);
6c17675e
JM
103 dentry_unhash(dentry);
104 error = dir->i_op->rmdir(dir, dentry);
105 if (!error)
106 dentry->d_inode->i_flags |= S_DEAD;
107 mutex_unlock(&dentry->d_inode->i_mutex);
108 if (!error)
109 d_delete(dentry);
110 dput(dentry);
111
112 return error;
113}
114
6c17675e
JM
115#define xattr_may_create(flags) (!flags || flags & XATTR_CREATE)
116
ab17c4f0 117static struct dentry *open_xa_root(struct super_block *sb, int flags)
6c17675e 118{
ab17c4f0
JM
119 struct dentry *privroot = REISERFS_SB(sb)->priv_root;
120 struct dentry *xaroot;
121 if (!privroot->d_inode)
122 return ERR_PTR(-ENODATA);
6c17675e 123
ab17c4f0 124 mutex_lock_nested(&privroot->d_inode->i_mutex, I_MUTEX_XATTR);
6c17675e 125
ab17c4f0 126 xaroot = dget(REISERFS_SB(sb)->xattr_root);
ceb5edc4
JM
127 if (!xaroot)
128 xaroot = ERR_PTR(-ENODATA);
129 else if (!xaroot->d_inode) {
ab17c4f0 130 int err = -ENODATA;
5a6059c3 131 if (xattr_may_create(flags))
ab17c4f0 132 err = xattr_mkdir(privroot->d_inode, xaroot, 0700);
9b7f3755 133 if (err) {
ab17c4f0
JM
134 dput(xaroot);
135 xaroot = ERR_PTR(err);
9b7f3755 136 }
bd4c625c 137 }
6c17675e 138
ab17c4f0
JM
139 mutex_unlock(&privroot->d_inode->i_mutex);
140 return xaroot;
1da177e4
LT
141}
142
bd4c625c 143static struct dentry *open_xa_dir(const struct inode *inode, int flags)
1da177e4 144{
bd4c625c
LT
145 struct dentry *xaroot, *xadir;
146 char namebuf[17];
147
6c17675e 148 xaroot = open_xa_root(inode->i_sb, flags);
9b7f3755 149 if (IS_ERR(xaroot))
bd4c625c 150 return xaroot;
bd4c625c 151
bd4c625c
LT
152 snprintf(namebuf, sizeof(namebuf), "%X.%X",
153 le32_to_cpu(INODE_PKEY(inode)->k_objectid),
154 inode->i_generation);
bd4c625c 155
ab17c4f0
JM
156 mutex_lock_nested(&xaroot->d_inode->i_mutex, I_MUTEX_XATTR);
157
158 xadir = lookup_one_len(namebuf, xaroot, strlen(namebuf));
159 if (!IS_ERR(xadir) && !xadir->d_inode) {
160 int err = -ENODATA;
161 if (xattr_may_create(flags))
162 err = xattr_mkdir(xaroot->d_inode, xadir, 0700);
163 if (err) {
164 dput(xadir);
165 xadir = ERR_PTR(err);
166 }
167 }
168
169 mutex_unlock(&xaroot->d_inode->i_mutex);
bd4c625c
LT
170 dput(xaroot);
171 return xadir;
1da177e4
LT
172}
173
48b32a35
JM
174/* The following are side effects of other operations that aren't explicitly
175 * modifying extended attributes. This includes operations such as permissions
176 * or ownership changes, object deletions, etc. */
a41f1a47
JM
177struct reiserfs_dentry_buf {
178 struct dentry *xadir;
179 int count;
180 struct dentry *dentries[8];
181};
bd4c625c 182
a72bdb1c 183static int
a41f1a47
JM
184fill_with_dentries(void *buf, const char *name, int namelen, loff_t offset,
185 u64 ino, unsigned int d_type)
a72bdb1c 186{
a41f1a47 187 struct reiserfs_dentry_buf *dbuf = buf;
a72bdb1c 188 struct dentry *dentry;
5a6059c3 189 WARN_ON_ONCE(!mutex_is_locked(&dbuf->xadir->d_inode->i_mutex));
bd4c625c 190
a41f1a47
JM
191 if (dbuf->count == ARRAY_SIZE(dbuf->dentries))
192 return -ENOSPC;
bd4c625c 193
a41f1a47
JM
194 if (name[0] == '.' && (name[1] == '\0' ||
195 (name[1] == '.' && name[2] == '\0')))
196 return 0;
bd4c625c 197
a41f1a47 198 dentry = lookup_one_len(name, dbuf->xadir, namelen);
a72bdb1c 199 if (IS_ERR(dentry)) {
a41f1a47 200 return PTR_ERR(dentry);
a72bdb1c 201 } else if (!dentry->d_inode) {
a41f1a47
JM
202 /* A directory entry exists, but no file? */
203 reiserfs_error(dentry->d_sb, "xattr-20003",
204 "Corrupted directory: xattr %s listed but "
205 "not found for file %s.\n",
206 dentry->d_name.name, dbuf->xadir->d_name.name);
207 dput(dentry);
208 return -EIO;
bd4c625c 209 }
1da177e4 210
a41f1a47
JM
211 dbuf->dentries[dbuf->count++] = dentry;
212 return 0;
1da177e4
LT
213}
214
a41f1a47
JM
215static void
216cleanup_dentry_buf(struct reiserfs_dentry_buf *buf)
1da177e4 217{
a41f1a47
JM
218 int i;
219 for (i = 0; i < buf->count; i++)
220 if (buf->dentries[i])
221 dput(buf->dentries[i]);
a72bdb1c
JM
222}
223
a41f1a47
JM
224static int reiserfs_for_each_xattr(struct inode *inode,
225 int (*action)(struct dentry *, void *),
226 void *data)
a72bdb1c 227{
a41f1a47
JM
228 struct dentry *dir;
229 int i, err = 0;
230 loff_t pos = 0;
231 struct reiserfs_dentry_buf buf = {
232 .count = 0,
233 };
1da177e4 234
a72bdb1c
JM
235 /* Skip out, an xattr has no xattrs associated with it */
236 if (IS_PRIVATE(inode) || get_inode_sd_version(inode) == STAT_DATA_V1)
237 return 0;
1da177e4 238
27026a05 239 reiserfs_write_unlock(inode->i_sb);
6c17675e 240 dir = open_xa_dir(inode, XATTR_REPLACE);
a72bdb1c
JM
241 if (IS_ERR(dir)) {
242 err = PTR_ERR(dir);
27026a05 243 reiserfs_write_lock(inode->i_sb);
a72bdb1c
JM
244 goto out;
245 } else if (!dir->d_inode) {
a41f1a47 246 err = 0;
27026a05 247 reiserfs_write_lock(inode->i_sb);
a41f1a47 248 goto out_dir;
a72bdb1c 249 }
1da177e4 250
27026a05
FW
251 mutex_lock_nested(&dir->d_inode->i_mutex, I_MUTEX_XATTR);
252
253 reiserfs_write_lock(inode->i_sb);
254
a41f1a47
JM
255 buf.xadir = dir;
256 err = reiserfs_readdir_dentry(dir, &buf, fill_with_dentries, &pos);
257 while ((err == 0 || err == -ENOSPC) && buf.count) {
258 err = 0;
1da177e4 259
a41f1a47
JM
260 for (i = 0; i < buf.count && buf.dentries[i]; i++) {
261 int lerr = 0;
262 struct dentry *dentry = buf.dentries[i];
1da177e4 263
a41f1a47
JM
264 if (err == 0 && !S_ISDIR(dentry->d_inode->i_mode))
265 lerr = action(dentry, data);
1da177e4 266
a41f1a47
JM
267 dput(dentry);
268 buf.dentries[i] = NULL;
269 err = lerr ?: err;
bd4c625c 270 }
a41f1a47
JM
271 buf.count = 0;
272 if (!err)
273 err = reiserfs_readdir_dentry(dir, &buf,
274 fill_with_dentries, &pos);
8b6dd72a 275 }
a41f1a47 276 mutex_unlock(&dir->d_inode->i_mutex);
1da177e4 277
a41f1a47
JM
278 /* Clean up after a failed readdir */
279 cleanup_dentry_buf(&buf);
1da177e4 280
d984561b 281 if (!err) {
a41f1a47
JM
282 /* We start a transaction here to avoid a ABBA situation
283 * between the xattr root's i_mutex and the journal lock.
284 * This doesn't incur much additional overhead since the
285 * new transaction will just nest inside the
286 * outer transaction. */
287 int blocks = JOURNAL_PER_BALANCE_CNT * 2 + 2 +
288 4 * REISERFS_QUOTA_TRANS_BLOCKS(inode->i_sb);
289 struct reiserfs_transaction_handle th;
290 err = journal_begin(&th, inode->i_sb, blocks);
291 if (!err) {
292 int jerror;
8b513f56
FW
293 reiserfs_mutex_lock_nested_safe(
294 &dir->d_parent->d_inode->i_mutex,
295 I_MUTEX_XATTR, inode->i_sb);
a41f1a47
JM
296 err = action(dir, data);
297 jerror = journal_end(&th, inode->i_sb, blocks);
298 mutex_unlock(&dir->d_parent->d_inode->i_mutex);
299 err = jerror ?: err;
300 }
a72bdb1c 301 }
a41f1a47
JM
302out_dir:
303 dput(dir);
a72bdb1c 304out:
a41f1a47
JM
305 /* -ENODATA isn't an error */
306 if (err == -ENODATA)
307 err = 0;
a72bdb1c
JM
308 return err;
309}
1da177e4 310
a41f1a47 311static int delete_one_xattr(struct dentry *dentry, void *data)
a72bdb1c 312{
a41f1a47 313 struct inode *dir = dentry->d_parent->d_inode;
1da177e4 314
a41f1a47
JM
315 /* This is the xattr dir, handle specially. */
316 if (S_ISDIR(dentry->d_inode->i_mode))
317 return xattr_rmdir(dir, dentry);
1da177e4 318
a41f1a47
JM
319 return xattr_unlink(dir, dentry);
320}
bd4c625c 321
a41f1a47
JM
322static int chown_one_xattr(struct dentry *dentry, void *data)
323{
324 struct iattr *attrs = data;
325 return reiserfs_setattr(dentry, attrs);
326}
1da177e4 327
a41f1a47
JM
328/* No i_mutex, but the inode is unconnected. */
329int reiserfs_delete_xattrs(struct inode *inode)
330{
331 int err = reiserfs_for_each_xattr(inode, delete_one_xattr, NULL);
332 if (err)
333 reiserfs_warning(inode->i_sb, "jdm-20004",
334 "Couldn't delete all xattrs (%d)\n", err);
a72bdb1c
JM
335 return err;
336}
1da177e4 337
a41f1a47 338/* inode->i_mutex: down */
a72bdb1c
JM
339int reiserfs_chown_xattrs(struct inode *inode, struct iattr *attrs)
340{
a41f1a47 341 int err = reiserfs_for_each_xattr(inode, chown_one_xattr, attrs);
8b6dd72a
JM
342 if (err)
343 reiserfs_warning(inode->i_sb, "jdm-20007",
344 "Couldn't chown all xattrs (%d)\n", err);
a72bdb1c 345 return err;
1da177e4
LT
346}
347
a72bdb1c 348#ifdef CONFIG_REISERFS_FS_XATTR
a72bdb1c
JM
349/* Returns a dentry corresponding to a specific extended attribute file
350 * for the inode. If flags allow, the file is created. Otherwise, a
351 * valid or negative dentry, or an error is returned. */
48b32a35
JM
352static struct dentry *xattr_lookup(struct inode *inode, const char *name,
353 int flags)
1da177e4 354{
a72bdb1c
JM
355 struct dentry *xadir, *xafile;
356 int err = 0;
357
358 xadir = open_xa_dir(inode, flags);
6c17675e 359 if (IS_ERR(xadir))
a72bdb1c 360 return ERR_CAST(xadir);
a72bdb1c 361
5a6059c3 362 mutex_lock_nested(&xadir->d_inode->i_mutex, I_MUTEX_XATTR);
a72bdb1c
JM
363 xafile = lookup_one_len(name, xadir, strlen(name));
364 if (IS_ERR(xafile)) {
6c17675e
JM
365 err = PTR_ERR(xafile);
366 goto out;
bd4c625c 367 }
a72bdb1c 368
6c17675e
JM
369 if (xafile->d_inode && (flags & XATTR_CREATE))
370 err = -EEXIST;
a72bdb1c 371
6c17675e
JM
372 if (!xafile->d_inode) {
373 err = -ENODATA;
5a6059c3 374 if (xattr_may_create(flags))
6c17675e
JM
375 err = xattr_create(xadir->d_inode, xafile,
376 0700|S_IFREG);
a72bdb1c
JM
377 }
378
6c17675e
JM
379 if (err)
380 dput(xafile);
a72bdb1c 381out:
5a6059c3 382 mutex_unlock(&xadir->d_inode->i_mutex);
a72bdb1c
JM
383 dput(xadir);
384 if (err)
6c17675e 385 return ERR_PTR(err);
a72bdb1c 386 return xafile;
1da177e4
LT
387}
388
1da177e4 389/* Internal operations on file data */
bd4c625c 390static inline void reiserfs_put_page(struct page *page)
1da177e4 391{
bd4c625c
LT
392 kunmap(page);
393 page_cache_release(page);
1da177e4
LT
394}
395
ec6ea56b 396static struct page *reiserfs_get_page(struct inode *dir, size_t n)
1da177e4 397{
bd4c625c
LT
398 struct address_space *mapping = dir->i_mapping;
399 struct page *page;
400 /* We can deadlock if we try to free dentries,
401 and an unlink/rmdir has just occured - GFP_NOFS avoids this */
c4cdd038 402 mapping_set_gfp_mask(mapping, GFP_NOFS);
ec6ea56b 403 page = read_mapping_page(mapping, n >> PAGE_CACHE_SHIFT, NULL);
bd4c625c 404 if (!IS_ERR(page)) {
bd4c625c 405 kmap(page);
bd4c625c
LT
406 if (PageError(page))
407 goto fail;
408 }
409 return page;
410
411 fail:
412 reiserfs_put_page(page);
413 return ERR_PTR(-EIO);
1da177e4
LT
414}
415
bd4c625c 416static inline __u32 xattr_hash(const char *msg, int len)
1da177e4 417{
bd4c625c 418 return csum_partial(msg, len, 0);
1da177e4
LT
419}
420
ba9d8cec
VS
421int reiserfs_commit_write(struct file *f, struct page *page,
422 unsigned from, unsigned to);
423int reiserfs_prepare_write(struct file *f, struct page *page,
424 unsigned from, unsigned to);
425
48b32a35
JM
426static void update_ctime(struct inode *inode)
427{
428 struct timespec now = current_fs_time(inode->i_sb);
429 if (hlist_unhashed(&inode->i_hash) || !inode->i_nlink ||
430 timespec_equal(&inode->i_ctime, &now))
431 return;
432
433 inode->i_ctime = CURRENT_TIME_SEC;
434 mark_inode_dirty(inode);
435}
436
437static int lookup_and_delete_xattr(struct inode *inode, const char *name)
438{
439 int err = 0;
440 struct dentry *dentry, *xadir;
441
442 xadir = open_xa_dir(inode, XATTR_REPLACE);
443 if (IS_ERR(xadir))
444 return PTR_ERR(xadir);
445
5a6059c3 446 mutex_lock_nested(&xadir->d_inode->i_mutex, I_MUTEX_XATTR);
48b32a35
JM
447 dentry = lookup_one_len(name, xadir, strlen(name));
448 if (IS_ERR(dentry)) {
449 err = PTR_ERR(dentry);
450 goto out_dput;
451 }
452
453 if (dentry->d_inode) {
4f3be1b5 454 reiserfs_write_lock(inode->i_sb);
48b32a35 455 err = xattr_unlink(xadir->d_inode, dentry);
4f3be1b5 456 reiserfs_write_unlock(inode->i_sb);
48b32a35
JM
457 update_ctime(inode);
458 }
459
460 dput(dentry);
461out_dput:
5a6059c3 462 mutex_unlock(&xadir->d_inode->i_mutex);
48b32a35
JM
463 dput(xadir);
464 return err;
465}
466
ba9d8cec 467
1da177e4
LT
468/* Generic extended attribute operations that can be used by xa plugins */
469
470/*
1b1dcc1b 471 * inode->i_mutex: down
1da177e4
LT
472 */
473int
0ab2621e
JM
474reiserfs_xattr_set_handle(struct reiserfs_transaction_handle *th,
475 struct inode *inode, const char *name,
476 const void *buffer, size_t buffer_size, int flags)
1da177e4 477{
bd4c625c 478 int err = 0;
3227e14c 479 struct dentry *dentry;
bd4c625c
LT
480 struct page *page;
481 char *data;
bd4c625c
LT
482 size_t file_pos = 0;
483 size_t buffer_pos = 0;
48b32a35 484 size_t new_size;
bd4c625c
LT
485 __u32 xahash = 0;
486
bd4c625c
LT
487 if (get_inode_sd_version(inode) == STAT_DATA_V1)
488 return -EOPNOTSUPP;
489
3f14fea6 490 reiserfs_write_unlock(inode->i_sb);
4f3be1b5
FW
491
492 if (!buffer) {
493 err = lookup_and_delete_xattr(inode, name);
494 reiserfs_write_lock(inode->i_sb);
495 return err;
496 }
497
48b32a35 498 dentry = xattr_lookup(inode, name, flags);
3f14fea6
FW
499 if (IS_ERR(dentry)) {
500 reiserfs_write_lock(inode->i_sb);
48b32a35 501 return PTR_ERR(dentry);
3f14fea6
FW
502 }
503
f3e22f48 504 down_write(&REISERFS_I(inode)->i_xattr_sem);
bd4c625c 505
3f14fea6 506 reiserfs_write_lock(inode->i_sb);
bd4c625c 507
8b6dd72a 508 xahash = xattr_hash(buffer, buffer_size);
bd4c625c
LT
509 while (buffer_pos < buffer_size || buffer_pos == 0) {
510 size_t chunk;
511 size_t skip = 0;
512 size_t page_offset = (file_pos & (PAGE_CACHE_SIZE - 1));
513 if (buffer_size - buffer_pos > PAGE_CACHE_SIZE)
514 chunk = PAGE_CACHE_SIZE;
515 else
516 chunk = buffer_size - buffer_pos;
517
ec6ea56b 518 page = reiserfs_get_page(dentry->d_inode, file_pos);
bd4c625c
LT
519 if (IS_ERR(page)) {
520 err = PTR_ERR(page);
48b32a35 521 goto out_unlock;
bd4c625c
LT
522 }
523
524 lock_page(page);
525 data = page_address(page);
526
527 if (file_pos == 0) {
528 struct reiserfs_xattr_header *rxh;
529 skip = file_pos = sizeof(struct reiserfs_xattr_header);
530 if (chunk + skip > PAGE_CACHE_SIZE)
531 chunk = PAGE_CACHE_SIZE - skip;
532 rxh = (struct reiserfs_xattr_header *)data;
533 rxh->h_magic = cpu_to_le32(REISERFS_XATTR_MAGIC);
534 rxh->h_hash = cpu_to_le32(xahash);
535 }
536
3227e14c 537 err = reiserfs_prepare_write(NULL, page, page_offset,
ba9d8cec 538 page_offset + chunk + skip);
bd4c625c
LT
539 if (!err) {
540 if (buffer)
541 memcpy(data + skip, buffer + buffer_pos, chunk);
3227e14c
JM
542 err = reiserfs_commit_write(NULL, page, page_offset,
543 page_offset + chunk +
544 skip);
bd4c625c
LT
545 }
546 unlock_page(page);
547 reiserfs_put_page(page);
548 buffer_pos += chunk;
549 file_pos += chunk;
550 skip = 0;
551 if (err || buffer_size == 0 || !buffer)
552 break;
553 }
554
48b32a35
JM
555 new_size = buffer_size + sizeof(struct reiserfs_xattr_header);
556 if (!err && new_size < i_size_read(dentry->d_inode)) {
557 struct iattr newattrs = {
558 .ia_ctime = current_fs_time(inode->i_sb),
559 .ia_size = buffer_size,
560 .ia_valid = ATTR_SIZE | ATTR_CTIME,
561 };
562 mutex_lock_nested(&dentry->d_inode->i_mutex, I_MUTEX_XATTR);
563 down_write(&dentry->d_inode->i_alloc_sem);
564 err = reiserfs_setattr(dentry, &newattrs);
565 up_write(&dentry->d_inode->i_alloc_sem);
566 mutex_unlock(&dentry->d_inode->i_mutex);
567 } else
568 update_ctime(inode);
569out_unlock:
8b6dd72a 570 up_write(&REISERFS_I(inode)->i_xattr_sem);
3227e14c 571 dput(dentry);
48b32a35
JM
572 return err;
573}
bd4c625c 574
0ab2621e
JM
575/* We need to start a transaction to maintain lock ordering */
576int reiserfs_xattr_set(struct inode *inode, const char *name,
577 const void *buffer, size_t buffer_size, int flags)
48b32a35 578{
0ab2621e
JM
579
580 struct reiserfs_transaction_handle th;
581 int error, error2;
582 size_t jbegin_count = reiserfs_xattr_nblocks(inode, buffer_size);
583
584 if (!(flags & XATTR_REPLACE))
585 jbegin_count += reiserfs_xattr_jcreate_nblocks(inode);
586
587 reiserfs_write_lock(inode->i_sb);
588 error = journal_begin(&th, inode->i_sb, jbegin_count);
589 if (error) {
590 reiserfs_write_unlock(inode->i_sb);
591 return error;
1da177e4 592 }
bd4c625c 593
0ab2621e
JM
594 error = reiserfs_xattr_set_handle(&th, inode, name,
595 buffer, buffer_size, flags);
bd4c625c 596
0ab2621e
JM
597 error2 = journal_end(&th, inode->i_sb, jbegin_count);
598 if (error == 0)
599 error = error2;
600 reiserfs_write_unlock(inode->i_sb);
601
602 return error;
1da177e4
LT
603}
604
605/*
1b1dcc1b 606 * inode->i_mutex: down
1da177e4
LT
607 */
608int
48b32a35 609reiserfs_xattr_get(struct inode *inode, const char *name, void *buffer,
bd4c625c 610 size_t buffer_size)
1da177e4 611{
bd4c625c 612 ssize_t err = 0;
3227e14c 613 struct dentry *dentry;
bd4c625c
LT
614 size_t isize;
615 size_t file_pos = 0;
616 size_t buffer_pos = 0;
617 struct page *page;
bd4c625c
LT
618 __u32 hash = 0;
619
620 if (name == NULL)
621 return -EINVAL;
622
623 /* We can't have xattrs attached to v1 items since they don't have
624 * generation numbers */
625 if (get_inode_sd_version(inode) == STAT_DATA_V1)
626 return -EOPNOTSUPP;
627
48b32a35 628 dentry = xattr_lookup(inode, name, XATTR_REPLACE);
3227e14c
JM
629 if (IS_ERR(dentry)) {
630 err = PTR_ERR(dentry);
bd4c625c
LT
631 goto out;
632 }
633
8b6dd72a 634 down_read(&REISERFS_I(inode)->i_xattr_sem);
d984561b 635
f437c529 636 isize = i_size_read(dentry->d_inode);
bd4c625c
LT
637
638 /* Just return the size needed */
639 if (buffer == NULL) {
640 err = isize - sizeof(struct reiserfs_xattr_header);
8b6dd72a 641 goto out_unlock;
bd4c625c
LT
642 }
643
644 if (buffer_size < isize - sizeof(struct reiserfs_xattr_header)) {
645 err = -ERANGE;
8b6dd72a 646 goto out_unlock;
bd4c625c
LT
647 }
648
649 while (file_pos < isize) {
650 size_t chunk;
651 char *data;
652 size_t skip = 0;
653 if (isize - file_pos > PAGE_CACHE_SIZE)
654 chunk = PAGE_CACHE_SIZE;
655 else
656 chunk = isize - file_pos;
657
a72bdb1c 658 page = reiserfs_get_page(dentry->d_inode, file_pos);
bd4c625c
LT
659 if (IS_ERR(page)) {
660 err = PTR_ERR(page);
8b6dd72a 661 goto out_unlock;
bd4c625c
LT
662 }
663
664 lock_page(page);
665 data = page_address(page);
666 if (file_pos == 0) {
667 struct reiserfs_xattr_header *rxh =
668 (struct reiserfs_xattr_header *)data;
669 skip = file_pos = sizeof(struct reiserfs_xattr_header);
670 chunk -= skip;
671 /* Magic doesn't match up.. */
672 if (rxh->h_magic != cpu_to_le32(REISERFS_XATTR_MAGIC)) {
673 unlock_page(page);
674 reiserfs_put_page(page);
a72bdb1c 675 reiserfs_warning(inode->i_sb, "jdm-20001",
bd4c625c
LT
676 "Invalid magic for xattr (%s) "
677 "associated with %k", name,
678 INODE_PKEY(inode));
679 err = -EIO;
8b6dd72a 680 goto out_unlock;
bd4c625c
LT
681 }
682 hash = le32_to_cpu(rxh->h_hash);
683 }
684 memcpy(buffer + buffer_pos, data + skip, chunk);
685 unlock_page(page);
686 reiserfs_put_page(page);
687 file_pos += chunk;
688 buffer_pos += chunk;
689 skip = 0;
690 }
691 err = isize - sizeof(struct reiserfs_xattr_header);
692
693 if (xattr_hash(buffer, isize - sizeof(struct reiserfs_xattr_header)) !=
694 hash) {
a72bdb1c 695 reiserfs_warning(inode->i_sb, "jdm-20002",
bd4c625c
LT
696 "Invalid hash for xattr (%s) associated "
697 "with %k", name, INODE_PKEY(inode));
698 err = -EIO;
699 }
700
8b6dd72a
JM
701out_unlock:
702 up_read(&REISERFS_I(inode)->i_xattr_sem);
3227e14c 703 dput(dentry);
bd4c625c 704
a72bdb1c 705out:
bd4c625c 706 return err;
1da177e4
LT
707}
708
48b32a35
JM
709/*
710 * In order to implement different sets of xattr operations for each xattr
711 * prefix with the generic xattr API, a filesystem should create a
712 * null-terminated array of struct xattr_handler (one for each prefix) and
713 * hang a pointer to it off of the s_xattr field of the superblock.
714 *
715 * The generic_fooxattr() functions will use this list to dispatch xattr
716 * operations to the correct xattr_handler.
717 */
718#define for_each_xattr_handler(handlers, handler) \
719 for ((handler) = *(handlers)++; \
720 (handler) != NULL; \
721 (handler) = *(handlers)++)
1da177e4 722
48b32a35
JM
723/* This is the implementation for the xattr plugin infrastructure */
724static inline struct xattr_handler *
725find_xattr_handler_prefix(struct xattr_handler **handlers,
726 const char *name)
1da177e4 727{
48b32a35 728 struct xattr_handler *xah;
bd4c625c 729
48b32a35
JM
730 if (!handlers)
731 return NULL;
bd4c625c 732
48b32a35
JM
733 for_each_xattr_handler(handlers, xah) {
734 if (strncmp(xah->prefix, name, strlen(xah->prefix)) == 0)
735 break;
bd4c625c
LT
736 }
737
48b32a35 738 return xah;
bd4c625c 739}
1da177e4 740
1da177e4
LT
741
742/*
743 * Inode operation getxattr()
1da177e4
LT
744 */
745ssize_t
bd4c625c
LT
746reiserfs_getxattr(struct dentry * dentry, const char *name, void *buffer,
747 size_t size)
1da177e4 748{
48b32a35
JM
749 struct inode *inode = dentry->d_inode;
750 struct xattr_handler *handler;
bd4c625c 751
48b32a35
JM
752 handler = find_xattr_handler_prefix(inode->i_sb->s_xattr, name);
753
754 if (!handler || get_inode_sd_version(inode) == STAT_DATA_V1)
bd4c625c
LT
755 return -EOPNOTSUPP;
756
48b32a35 757 return handler->get(inode, name, buffer, size);
1da177e4
LT
758}
759
1da177e4
LT
760/*
761 * Inode operation setxattr()
762 *
1b1dcc1b 763 * dentry->d_inode->i_mutex down
1da177e4
LT
764 */
765int
bd4c625c
LT
766reiserfs_setxattr(struct dentry *dentry, const char *name, const void *value,
767 size_t size, int flags)
1da177e4 768{
48b32a35
JM
769 struct inode *inode = dentry->d_inode;
770 struct xattr_handler *handler;
bd4c625c 771
48b32a35
JM
772 handler = find_xattr_handler_prefix(inode->i_sb->s_xattr, name);
773
774 if (!handler || get_inode_sd_version(inode) == STAT_DATA_V1)
bd4c625c
LT
775 return -EOPNOTSUPP;
776
48b32a35 777 return handler->set(inode, name, value, size, flags);
1da177e4
LT
778}
779
780/*
781 * Inode operation removexattr()
782 *
1b1dcc1b 783 * dentry->d_inode->i_mutex down
1da177e4 784 */
bd4c625c 785int reiserfs_removexattr(struct dentry *dentry, const char *name)
1da177e4 786{
48b32a35
JM
787 struct inode *inode = dentry->d_inode;
788 struct xattr_handler *handler;
789 handler = find_xattr_handler_prefix(inode->i_sb->s_xattr, name);
1da177e4 790
48b32a35 791 if (!handler || get_inode_sd_version(inode) == STAT_DATA_V1)
bd4c625c 792 return -EOPNOTSUPP;
1da177e4 793
48b32a35 794 return handler->set(inode, name, NULL, 0, XATTR_REPLACE);
1da177e4
LT
795}
796
48b32a35
JM
797struct listxattr_buf {
798 size_t size;
799 size_t pos;
800 char *buf;
801 struct inode *inode;
1da177e4
LT
802};
803
48b32a35
JM
804static int listxattr_filler(void *buf, const char *name, int namelen,
805 loff_t offset, u64 ino, unsigned int d_type)
1da177e4 806{
48b32a35
JM
807 struct listxattr_buf *b = (struct listxattr_buf *)buf;
808 size_t size;
809 if (name[0] != '.' ||
810 (namelen != 1 && (name[1] != '.' || namelen != 2))) {
811 struct xattr_handler *handler;
812 handler = find_xattr_handler_prefix(b->inode->i_sb->s_xattr,
813 name);
814 if (!handler) /* Unsupported xattr name */
815 return 0;
816 if (b->buf) {
817 size = handler->list(b->inode, b->buf + b->pos,
818 b->size, name, namelen);
819 if (size > b->size)
820 return -ERANGE;
821 } else {
822 size = handler->list(b->inode, NULL, 0, name, namelen);
bd4c625c 823 }
bd4c625c 824
48b32a35
JM
825 b->pos += size;
826 }
bd4c625c 827 return 0;
1da177e4 828}
bd4c625c 829
1da177e4
LT
830/*
831 * Inode operation listxattr()
832 *
48b32a35
JM
833 * We totally ignore the generic listxattr here because it would be stupid
834 * not to. Since the xattrs are organized in a directory, we can just
835 * readdir to find them.
1da177e4 836 */
bd4c625c 837ssize_t reiserfs_listxattr(struct dentry * dentry, char *buffer, size_t size)
1da177e4 838{
bd4c625c
LT
839 struct dentry *dir;
840 int err = 0;
a41f1a47 841 loff_t pos = 0;
48b32a35
JM
842 struct listxattr_buf buf = {
843 .inode = dentry->d_inode,
844 .buf = buffer,
845 .size = buffer ? size : 0,
846 };
bd4c625c
LT
847
848 if (!dentry->d_inode)
849 return -EINVAL;
850
677c9b2e 851 if (!dentry->d_sb->s_xattr ||
bd4c625c
LT
852 get_inode_sd_version(dentry->d_inode) == STAT_DATA_V1)
853 return -EOPNOTSUPP;
854
6c17675e 855 dir = open_xa_dir(dentry->d_inode, XATTR_REPLACE);
bd4c625c
LT
856 if (IS_ERR(dir)) {
857 err = PTR_ERR(dir);
858 if (err == -ENODATA)
48b32a35 859 err = 0; /* Not an error if there aren't any xattrs */
bd4c625c
LT
860 goto out;
861 }
862
6c17675e 863 mutex_lock_nested(&dir->d_inode->i_mutex, I_MUTEX_XATTR);
a41f1a47 864 err = reiserfs_readdir_dentry(dir, &buf, listxattr_filler, &pos);
6c17675e 865 mutex_unlock(&dir->d_inode->i_mutex);
bd4c625c 866
48b32a35
JM
867 if (!err)
868 err = buf.pos;
bd4c625c 869
3227e14c 870 dput(dir);
8b6dd72a 871out:
bd4c625c 872 return err;
1da177e4
LT
873}
874
a72bdb1c 875static int reiserfs_check_acl(struct inode *inode, int mask)
1da177e4 876{
a72bdb1c
JM
877 struct posix_acl *acl;
878 int error = -EAGAIN; /* do regular unix permission checks by default */
bd4c625c 879
a72bdb1c
JM
880 acl = reiserfs_get_acl(inode, ACL_TYPE_ACCESS);
881
a72bdb1c
JM
882 if (acl) {
883 if (!IS_ERR(acl)) {
884 error = posix_acl_permission(inode, acl, mask);
885 posix_acl_release(acl);
886 } else if (PTR_ERR(acl) != -ENODATA)
887 error = PTR_ERR(acl);
bd4c625c
LT
888 }
889
a72bdb1c 890 return error;
1da177e4
LT
891}
892
a72bdb1c 893static int create_privroot(struct dentry *dentry)
1da177e4 894{
a72bdb1c
JM
895 int err;
896 struct inode *inode = dentry->d_parent->d_inode;
5a6059c3
JM
897 WARN_ON_ONCE(!mutex_is_locked(&inode->i_mutex));
898
6c17675e 899 err = xattr_mkdir(inode, dentry, 0700);
edcc37a0
AV
900 if (err || !dentry->d_inode) {
901 reiserfs_warning(dentry->d_sb, "jdm-20006",
902 "xattrs/ACLs enabled and couldn't "
903 "find/create .reiserfs_priv. "
904 "Failing mount.");
905 return -EOPNOTSUPP;
bd4c625c
LT
906 }
907
edcc37a0
AV
908 dentry->d_inode->i_flags |= S_PRIVATE;
909 reiserfs_info(dentry->d_sb, "Created %s - reserved for xattr "
910 "storage.\n", PRIVROOT_NAME);
bd4c625c 911
edcc37a0 912 return 0;
1da177e4
LT
913}
914
12abb35a
JM
915#else
916int __init reiserfs_xattr_register_handlers(void) { return 0; }
917void reiserfs_xattr_unregister_handlers(void) {}
918static int create_privroot(struct dentry *dentry) { return 0; }
919#endif
920
921/* Actual operations that are exported to VFS-land */
922struct xattr_handler *reiserfs_xattr_handlers[] = {
923#ifdef CONFIG_REISERFS_FS_XATTR
924 &reiserfs_xattr_user_handler,
925 &reiserfs_xattr_trusted_handler,
926#endif
927#ifdef CONFIG_REISERFS_FS_SECURITY
928 &reiserfs_xattr_security_handler,
929#endif
930#ifdef CONFIG_REISERFS_FS_POSIX_ACL
931 &reiserfs_posix_acl_access_handler,
932 &reiserfs_posix_acl_default_handler,
933#endif
934 NULL
935};
936
a72bdb1c 937static int xattr_mount_check(struct super_block *s)
1da177e4 938{
a72bdb1c
JM
939 /* We need generation numbers to ensure that the oid mapping is correct
940 * v3.5 filesystems don't have them. */
48b32a35
JM
941 if (old_format_only(s)) {
942 if (reiserfs_xattrs_optional(s)) {
943 /* Old format filesystem, but optional xattrs have
944 * been enabled. Error out. */
945 reiserfs_warning(s, "jdm-2005",
946 "xattrs/ACLs not supported "
947 "on pre-v3.6 format filesystems. "
948 "Failing mount.");
949 return -EOPNOTSUPP;
950 }
a72bdb1c
JM
951 }
952
953 return 0;
1da177e4
LT
954}
955
b83674c0
JM
956int reiserfs_permission(struct inode *inode, int mask)
957{
958 /*
959 * We don't do permission checks on the internal objects.
960 * Permissions are determined by the "owning" object.
961 */
962 if (IS_PRIVATE(inode))
963 return 0;
964
965#ifdef CONFIG_REISERFS_FS_XATTR
966 /*
967 * Stat data v1 doesn't support ACLs.
968 */
969 if (get_inode_sd_version(inode) != STAT_DATA_V1)
970 return generic_permission(inode, mask, reiserfs_check_acl);
971#endif
972 return generic_permission(inode, mask, NULL);
973}
974
1da177e4
LT
975/* This will catch lookups from the fs root to .reiserfs_priv */
976static int
bd4c625c 977xattr_lookup_poison(struct dentry *dentry, struct qstr *q1, struct qstr *name)
1da177e4 978{
bd4c625c 979 struct dentry *priv_root = REISERFS_SB(dentry->d_sb)->priv_root;
edcc37a0 980 if (container_of(q1, struct dentry, d_name) == priv_root)
bd4c625c 981 return -ENOENT;
edcc37a0 982 if (q1->len == name->len &&
bd4c625c
LT
983 !memcmp(q1->name, name->name, name->len))
984 return 0;
985 return 1;
1da177e4
LT
986}
987
e16404ed 988static const struct dentry_operations xattr_lookup_poison_ops = {
bd4c625c 989 .d_compare = xattr_lookup_poison,
1da177e4
LT
990};
991
edcc37a0
AV
992int reiserfs_lookup_privroot(struct super_block *s)
993{
994 struct dentry *dentry;
995 int err = 0;
996
997 /* If we don't have the privroot located yet - go find it */
c72e0575 998 reiserfs_mutex_lock_safe(&s->s_root->d_inode->i_mutex, s);
edcc37a0
AV
999 dentry = lookup_one_len(PRIVROOT_NAME, s->s_root,
1000 strlen(PRIVROOT_NAME));
1001 if (!IS_ERR(dentry)) {
1002 REISERFS_SB(s)->priv_root = dentry;
73422811
JM
1003 if (!reiserfs_expose_privroot(s))
1004 s->s_root->d_op = &xattr_lookup_poison_ops;
edcc37a0
AV
1005 if (dentry->d_inode)
1006 dentry->d_inode->i_flags |= S_PRIVATE;
1007 } else
1008 err = PTR_ERR(dentry);
1009 mutex_unlock(&s->s_root->d_inode->i_mutex);
1010
1011 return err;
1012}
1013
1da177e4
LT
1014/* We need to take a copy of the mount flags since things like
1015 * MS_RDONLY don't get set until *after* we're called.
1016 * mount_flags != mount_options */
bd4c625c 1017int reiserfs_xattr_init(struct super_block *s, int mount_flags)
1da177e4 1018{
bd4c625c 1019 int err = 0;
ab17c4f0 1020 struct dentry *privroot = REISERFS_SB(s)->priv_root;
bd4c625c 1021
a72bdb1c
JM
1022 err = xattr_mount_check(s);
1023 if (err)
bd4c625c 1024 goto error;
bd4c625c 1025
ab17c4f0 1026 if (!privroot->d_inode && !(mount_flags & MS_RDONLY)) {
ae635c0b 1027 reiserfs_mutex_lock_safe(&s->s_root->d_inode->i_mutex, s);
edcc37a0 1028 err = create_privroot(REISERFS_SB(s)->priv_root);
5a6059c3 1029 mutex_unlock(&s->s_root->d_inode->i_mutex);
bd4c625c 1030 }
ab17c4f0
JM
1031
1032 if (privroot->d_inode) {
48b32a35 1033 s->s_xattr = reiserfs_xattr_handlers;
c72e0575 1034 reiserfs_mutex_lock_safe(&privroot->d_inode->i_mutex, s);
ab17c4f0
JM
1035 if (!REISERFS_SB(s)->xattr_root) {
1036 struct dentry *dentry;
1037 dentry = lookup_one_len(XAROOT_NAME, privroot,
1038 strlen(XAROOT_NAME));
1039 if (!IS_ERR(dentry))
1040 REISERFS_SB(s)->xattr_root = dentry;
1041 else
1042 err = PTR_ERR(dentry);
1043 }
1044 mutex_unlock(&privroot->d_inode->i_mutex);
1045 }
48b32a35 1046
a72bdb1c 1047error:
bd4c625c 1048 if (err) {
bd4c625c
LT
1049 clear_bit(REISERFS_XATTRS_USER, &(REISERFS_SB(s)->s_mount_opt));
1050 clear_bit(REISERFS_POSIXACL, &(REISERFS_SB(s)->s_mount_opt));
1051 }
1052
1053 /* The super_block MS_POSIXACL must mirror the (no)acl mount option. */
bd4c625c
LT
1054 if (reiserfs_posixacl(s))
1055 s->s_flags |= MS_POSIXACL;
ab17c4f0 1056 else
ab17c4f0 1057 s->s_flags &= ~MS_POSIXACL;
bd4c625c
LT
1058
1059 return err;
1da177e4 1060}