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