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