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