[PATCH] capable/capability.h (fs/)
[linux-block.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.
30 */
31
32#include <linux/reiserfs_fs.h>
16f7e0fe 33#include <linux/capability.h>
1da177e4
LT
34#include <linux/dcache.h>
35#include <linux/namei.h>
36#include <linux/errno.h>
37#include <linux/fs.h>
38#include <linux/file.h>
39#include <linux/pagemap.h>
40#include <linux/xattr.h>
41#include <linux/reiserfs_xattr.h>
42#include <linux/reiserfs_acl.h>
1da177e4
LT
43#include <asm/uaccess.h>
44#include <asm/checksum.h>
45#include <linux/smp_lock.h>
46#include <linux/stat.h>
47#include <asm/semaphore.h>
48
49#define FL_READONLY 128
50#define FL_DIR_SEM_HELD 256
51#define PRIVROOT_NAME ".reiserfs_priv"
52#define XAROOT_NAME "xattrs"
53
bd4c625c
LT
54static struct reiserfs_xattr_handler *find_xattr_handler_prefix(const char
55 *prefix);
1da177e4 56
bd4c625c 57static struct dentry *create_xa_root(struct super_block *sb)
1da177e4 58{
bd4c625c
LT
59 struct dentry *privroot = dget(REISERFS_SB(sb)->priv_root);
60 struct dentry *xaroot;
61
62 /* This needs to be created at mount-time */
63 if (!privroot)
64 return ERR_PTR(-EOPNOTSUPP);
65
66 xaroot = lookup_one_len(XAROOT_NAME, privroot, strlen(XAROOT_NAME));
67 if (IS_ERR(xaroot)) {
68 goto out;
69 } else if (!xaroot->d_inode) {
70 int err;
1b1dcc1b 71 mutex_lock(&privroot->d_inode->i_mutex);
bd4c625c
LT
72 err =
73 privroot->d_inode->i_op->mkdir(privroot->d_inode, xaroot,
74 0700);
1b1dcc1b 75 mutex_unlock(&privroot->d_inode->i_mutex);
bd4c625c
LT
76
77 if (err) {
78 dput(xaroot);
79 dput(privroot);
80 return ERR_PTR(err);
81 }
82 REISERFS_SB(sb)->xattr_root = dget(xaroot);
83 }
84
85 out:
86 dput(privroot);
87 return xaroot;
1da177e4
LT
88}
89
90/* This will return a dentry, or error, refering to the xa root directory.
91 * If the xa root doesn't exist yet, the dentry will be returned without
92 * an associated inode. This dentry can be used with ->mkdir to create
93 * the xa directory. */
bd4c625c 94static struct dentry *__get_xa_root(struct super_block *s)
1da177e4 95{
bd4c625c
LT
96 struct dentry *privroot = dget(REISERFS_SB(s)->priv_root);
97 struct dentry *xaroot = NULL;
98
99 if (IS_ERR(privroot) || !privroot)
100 return privroot;
101
102 xaroot = lookup_one_len(XAROOT_NAME, privroot, strlen(XAROOT_NAME));
103 if (IS_ERR(xaroot)) {
104 goto out;
105 } else if (!xaroot->d_inode) {
106 dput(xaroot);
107 xaroot = NULL;
108 goto out;
109 }
110
111 REISERFS_SB(s)->xattr_root = dget(xaroot);
112
113 out:
114 dput(privroot);
115 return xaroot;
1da177e4
LT
116}
117
118/* Returns the dentry (or NULL) referring to the root of the extended
4a4efbde
MM
119 * attribute directory tree. If it has already been retrieved, it is used.
120 * Otherwise, we attempt to retrieve it from disk. It may also return
1da177e4
LT
121 * a pointer-encoded error.
122 */
bd4c625c 123static inline struct dentry *get_xa_root(struct super_block *s)
1da177e4 124{
bd4c625c 125 struct dentry *dentry = dget(REISERFS_SB(s)->xattr_root);
1da177e4 126
bd4c625c
LT
127 if (!dentry)
128 dentry = __get_xa_root(s);
1da177e4 129
bd4c625c 130 return dentry;
1da177e4
LT
131}
132
133/* Opens the directory corresponding to the inode's extended attribute store.
134 * If flags allow, the tree to the directory may be created. If creation is
135 * prohibited, -ENODATA is returned. */
bd4c625c 136static struct dentry *open_xa_dir(const struct inode *inode, int flags)
1da177e4 137{
bd4c625c
LT
138 struct dentry *xaroot, *xadir;
139 char namebuf[17];
140
141 xaroot = get_xa_root(inode->i_sb);
142 if (IS_ERR(xaroot)) {
143 return xaroot;
144 } else if (!xaroot) {
145 if (flags == 0 || flags & XATTR_CREATE) {
146 xaroot = create_xa_root(inode->i_sb);
147 if (IS_ERR(xaroot))
148 return xaroot;
149 }
150 if (!xaroot)
151 return ERR_PTR(-ENODATA);
152 }
153
154 /* ok, we have xaroot open */
155
156 snprintf(namebuf, sizeof(namebuf), "%X.%X",
157 le32_to_cpu(INODE_PKEY(inode)->k_objectid),
158 inode->i_generation);
159 xadir = lookup_one_len(namebuf, xaroot, strlen(namebuf));
160 if (IS_ERR(xadir)) {
161 dput(xaroot);
162 return xadir;
163 }
164
165 if (!xadir->d_inode) {
166 int err;
167 if (flags == 0 || flags & XATTR_CREATE) {
168 /* Although there is nothing else trying to create this directory,
169 * another directory with the same hash may be created, so we need
170 * to protect against that */
171 err =
172 xaroot->d_inode->i_op->mkdir(xaroot->d_inode, xadir,
173 0700);
174 if (err) {
175 dput(xaroot);
176 dput(xadir);
177 return ERR_PTR(err);
178 }
179 }
180 if (!xadir->d_inode) {
181 dput(xaroot);
182 dput(xadir);
183 return ERR_PTR(-ENODATA);
184 }
185 }
186
187 dput(xaroot);
188 return xadir;
1da177e4
LT
189}
190
191/* Returns a dentry corresponding to a specific extended attribute file
192 * for the inode. If flags allow, the file is created. Otherwise, a
193 * valid or negative dentry, or an error is returned. */
bd4c625c
LT
194static struct dentry *get_xa_file_dentry(const struct inode *inode,
195 const char *name, int flags)
1da177e4 196{
bd4c625c
LT
197 struct dentry *xadir, *xafile;
198 int err = 0;
199
200 xadir = open_xa_dir(inode, flags);
201 if (IS_ERR(xadir)) {
202 return ERR_PTR(PTR_ERR(xadir));
203 } else if (xadir && !xadir->d_inode) {
204 dput(xadir);
205 return ERR_PTR(-ENODATA);
206 }
207
208 xafile = lookup_one_len(name, xadir, strlen(name));
209 if (IS_ERR(xafile)) {
210 dput(xadir);
211 return ERR_PTR(PTR_ERR(xafile));
212 }
213
214 if (xafile->d_inode) { /* file exists */
215 if (flags & XATTR_CREATE) {
216 err = -EEXIST;
217 dput(xafile);
218 goto out;
219 }
220 } else if (flags & XATTR_REPLACE || flags & FL_READONLY) {
221 goto out;
222 } else {
1b1dcc1b 223 /* inode->i_mutex is down, so nothing else can try to create
bd4c625c
LT
224 * the same xattr */
225 err = xadir->d_inode->i_op->create(xadir->d_inode, xafile,
226 0700 | S_IFREG, NULL);
227
228 if (err) {
229 dput(xafile);
230 goto out;
231 }
232 }
1da177e4 233
bd4c625c
LT
234 out:
235 dput(xadir);
236 if (err)
237 xafile = ERR_PTR(err);
238 return xafile;
239}
1da177e4
LT
240
241/* Opens a file pointer to the attribute associated with inode */
bd4c625c
LT
242static struct file *open_xa_file(const struct inode *inode, const char *name,
243 int flags)
1da177e4 244{
bd4c625c
LT
245 struct dentry *xafile;
246 struct file *fp;
247
248 xafile = get_xa_file_dentry(inode, name, flags);
249 if (IS_ERR(xafile))
250 return ERR_PTR(PTR_ERR(xafile));
251 else if (!xafile->d_inode) {
252 dput(xafile);
253 return ERR_PTR(-ENODATA);
254 }
1da177e4 255
bd4c625c
LT
256 fp = dentry_open(xafile, NULL, O_RDWR);
257 /* dentry_open dputs the dentry if it fails */
1da177e4 258
bd4c625c 259 return fp;
1da177e4
LT
260}
261
1da177e4
LT
262/*
263 * this is very similar to fs/reiserfs/dir.c:reiserfs_readdir, but
264 * we need to drop the path before calling the filldir struct. That
265 * would be a big performance hit to the non-xattr case, so I've copied
266 * the whole thing for now. --clm
267 *
268 * the big difference is that I go backwards through the directory,
269 * and don't mess with f->f_pos, but the idea is the same. Do some
270 * action on each and every entry in the directory.
271 *
1b1dcc1b 272 * we're called with i_mutex held, so there are no worries about the directory
1da177e4
LT
273 * changing underneath us.
274 */
bd4c625c 275static int __xattr_readdir(struct file *filp, void *dirent, filldir_t filldir)
1da177e4 276{
bd4c625c
LT
277 struct inode *inode = filp->f_dentry->d_inode;
278 struct cpu_key pos_key; /* key of current position in the directory (key of directory entry) */
279 INITIALIZE_PATH(path_to_entry);
280 struct buffer_head *bh;
281 int entry_num;
282 struct item_head *ih, tmp_ih;
283 int search_res;
284 char *local_buf;
285 loff_t next_pos;
286 char small_buf[32]; /* avoid kmalloc if we can */
287 struct reiserfs_de_head *deh;
288 int d_reclen;
289 char *d_name;
290 off_t d_off;
291 ino_t d_ino;
292 struct reiserfs_dir_entry de;
293
294 /* form key for search the next directory entry using f_pos field of
295 file structure */
296 next_pos = max_reiserfs_offset(inode);
297
298 while (1) {
299 research:
300 if (next_pos <= DOT_DOT_OFFSET)
301 break;
302 make_cpu_key(&pos_key, inode, next_pos, TYPE_DIRENTRY, 3);
303
304 search_res =
305 search_by_entry_key(inode->i_sb, &pos_key, &path_to_entry,
306 &de);
307 if (search_res == IO_ERROR) {
308 // FIXME: we could just skip part of directory which could
309 // not be read
310 pathrelse(&path_to_entry);
311 return -EIO;
312 }
1da177e4 313
bd4c625c
LT
314 if (search_res == NAME_NOT_FOUND)
315 de.de_entry_num--;
1da177e4 316
bd4c625c
LT
317 set_de_name_and_namelen(&de);
318 entry_num = de.de_entry_num;
319 deh = &(de.de_deh[entry_num]);
1da177e4 320
bd4c625c
LT
321 bh = de.de_bh;
322 ih = de.de_ih;
1da177e4 323
bd4c625c
LT
324 if (!is_direntry_le_ih(ih)) {
325 reiserfs_warning(inode->i_sb, "not direntry %h", ih);
326 break;
327 }
328 copy_item_head(&tmp_ih, ih);
1da177e4 329
bd4c625c
LT
330 /* we must have found item, that is item of this directory, */
331 RFALSE(COMP_SHORT_KEYS(&(ih->ih_key), &pos_key),
332 "vs-9000: found item %h does not match to dir we readdir %K",
333 ih, &pos_key);
1da177e4 334
bd4c625c
LT
335 if (deh_offset(deh) <= DOT_DOT_OFFSET) {
336 break;
337 }
1da177e4 338
bd4c625c
LT
339 /* look for the previous entry in the directory */
340 next_pos = deh_offset(deh) - 1;
1da177e4 341
bd4c625c
LT
342 if (!de_visible(deh))
343 /* it is hidden entry */
344 continue;
1da177e4 345
bd4c625c
LT
346 d_reclen = entry_length(bh, ih, entry_num);
347 d_name = B_I_DEH_ENTRY_FILE_NAME(bh, ih, deh);
348 d_off = deh_offset(deh);
349 d_ino = deh_objectid(deh);
1da177e4 350
bd4c625c
LT
351 if (!d_name[d_reclen - 1])
352 d_reclen = strlen(d_name);
1da177e4 353
bd4c625c
LT
354 if (d_reclen > REISERFS_MAX_NAME(inode->i_sb->s_blocksize)) {
355 /* too big to send back to VFS */
356 continue;
357 }
1da177e4 358
bd4c625c
LT
359 /* Ignore the .reiserfs_priv entry */
360 if (reiserfs_xattrs(inode->i_sb) &&
361 !old_format_only(inode->i_sb) &&
362 deh_objectid(deh) ==
363 le32_to_cpu(INODE_PKEY
364 (REISERFS_SB(inode->i_sb)->priv_root->d_inode)->
365 k_objectid))
366 continue;
367
368 if (d_reclen <= 32) {
369 local_buf = small_buf;
370 } else {
371 local_buf =
372 reiserfs_kmalloc(d_reclen, GFP_NOFS, inode->i_sb);
373 if (!local_buf) {
374 pathrelse(&path_to_entry);
375 return -ENOMEM;
376 }
377 if (item_moved(&tmp_ih, &path_to_entry)) {
378 reiserfs_kfree(local_buf, d_reclen,
379 inode->i_sb);
380
381 /* sigh, must retry. Do this same offset again */
382 next_pos = d_off;
383 goto research;
384 }
385 }
1da177e4 386
bd4c625c
LT
387 // Note, that we copy name to user space via temporary
388 // buffer (local_buf) because filldir will block if
389 // user space buffer is swapped out. At that time
390 // entry can move to somewhere else
391 memcpy(local_buf, d_name, d_reclen);
1da177e4 392
bd4c625c
LT
393 /* the filldir function might need to start transactions,
394 * or do who knows what. Release the path now that we've
395 * copied all the important stuff out of the deh
396 */
397 pathrelse(&path_to_entry);
398
399 if (filldir(dirent, local_buf, d_reclen, d_off, d_ino,
400 DT_UNKNOWN) < 0) {
401 if (local_buf != small_buf) {
402 reiserfs_kfree(local_buf, d_reclen,
403 inode->i_sb);
404 }
405 goto end;
406 }
407 if (local_buf != small_buf) {
408 reiserfs_kfree(local_buf, d_reclen, inode->i_sb);
409 }
410 } /* while */
1da177e4 411
bd4c625c
LT
412 end:
413 pathrelse(&path_to_entry);
414 return 0;
1da177e4
LT
415}
416
417/*
418 * this could be done with dedicated readdir ops for the xattr files,
419 * but I want to get something working asap
420 * this is stolen from vfs_readdir
421 *
422 */
423static
424int xattr_readdir(struct file *file, filldir_t filler, void *buf)
425{
bd4c625c
LT
426 struct inode *inode = file->f_dentry->d_inode;
427 int res = -ENOTDIR;
428 if (!file->f_op || !file->f_op->readdir)
429 goto out;
1b1dcc1b 430 mutex_lock(&inode->i_mutex);
1da177e4 431// down(&inode->i_zombie);
bd4c625c
LT
432 res = -ENOENT;
433 if (!IS_DEADDIR(inode)) {
434 lock_kernel();
435 res = __xattr_readdir(file, buf, filler);
436 unlock_kernel();
437 }
1da177e4 438// up(&inode->i_zombie);
1b1dcc1b 439 mutex_unlock(&inode->i_mutex);
bd4c625c
LT
440 out:
441 return res;
1da177e4
LT
442}
443
1da177e4 444/* Internal operations on file data */
bd4c625c 445static inline void reiserfs_put_page(struct page *page)
1da177e4 446{
bd4c625c
LT
447 kunmap(page);
448 page_cache_release(page);
1da177e4
LT
449}
450
bd4c625c 451static struct page *reiserfs_get_page(struct inode *dir, unsigned long n)
1da177e4 452{
bd4c625c
LT
453 struct address_space *mapping = dir->i_mapping;
454 struct page *page;
455 /* We can deadlock if we try to free dentries,
456 and an unlink/rmdir has just occured - GFP_NOFS avoids this */
c4cdd038 457 mapping_set_gfp_mask(mapping, GFP_NOFS);
bd4c625c
LT
458 page = read_cache_page(mapping, n,
459 (filler_t *) mapping->a_ops->readpage, NULL);
460 if (!IS_ERR(page)) {
461 wait_on_page_locked(page);
462 kmap(page);
463 if (!PageUptodate(page))
464 goto fail;
465
466 if (PageError(page))
467 goto fail;
468 }
469 return page;
470
471 fail:
472 reiserfs_put_page(page);
473 return ERR_PTR(-EIO);
1da177e4
LT
474}
475
bd4c625c 476static inline __u32 xattr_hash(const char *msg, int len)
1da177e4 477{
bd4c625c 478 return csum_partial(msg, len, 0);
1da177e4
LT
479}
480
481/* Generic extended attribute operations that can be used by xa plugins */
482
483/*
1b1dcc1b 484 * inode->i_mutex: down
1da177e4
LT
485 */
486int
bd4c625c
LT
487reiserfs_xattr_set(struct inode *inode, const char *name, const void *buffer,
488 size_t buffer_size, int flags)
1da177e4 489{
bd4c625c
LT
490 int err = 0;
491 struct file *fp;
492 struct page *page;
493 char *data;
494 struct address_space *mapping;
495 size_t file_pos = 0;
496 size_t buffer_pos = 0;
497 struct inode *xinode;
498 struct iattr newattrs;
499 __u32 xahash = 0;
500
bd4c625c
LT
501 if (get_inode_sd_version(inode) == STAT_DATA_V1)
502 return -EOPNOTSUPP;
503
504 /* Empty xattrs are ok, they're just empty files, no hash */
505 if (buffer && buffer_size)
506 xahash = xattr_hash(buffer, buffer_size);
507
508 open_file:
509 fp = open_xa_file(inode, name, flags);
510 if (IS_ERR(fp)) {
511 err = PTR_ERR(fp);
512 goto out;
513 }
514
515 xinode = fp->f_dentry->d_inode;
516 REISERFS_I(inode)->i_flags |= i_has_xattr_dir;
517
518 /* we need to copy it off.. */
519 if (xinode->i_nlink > 1) {
520 fput(fp);
521 err = reiserfs_xattr_del(inode, name);
522 if (err < 0)
523 goto out;
524 /* We just killed the old one, we're not replacing anymore */
525 if (flags & XATTR_REPLACE)
526 flags &= ~XATTR_REPLACE;
527 goto open_file;
528 }
529
530 /* Resize it so we're ok to write there */
531 newattrs.ia_size = buffer_size;
532 newattrs.ia_valid = ATTR_SIZE | ATTR_CTIME;
1b1dcc1b 533 mutex_lock(&xinode->i_mutex);
bd4c625c
LT
534 err = notify_change(fp->f_dentry, &newattrs);
535 if (err)
536 goto out_filp;
537
538 mapping = xinode->i_mapping;
539 while (buffer_pos < buffer_size || buffer_pos == 0) {
540 size_t chunk;
541 size_t skip = 0;
542 size_t page_offset = (file_pos & (PAGE_CACHE_SIZE - 1));
543 if (buffer_size - buffer_pos > PAGE_CACHE_SIZE)
544 chunk = PAGE_CACHE_SIZE;
545 else
546 chunk = buffer_size - buffer_pos;
547
548 page = reiserfs_get_page(xinode, file_pos >> PAGE_CACHE_SHIFT);
549 if (IS_ERR(page)) {
550 err = PTR_ERR(page);
551 goto out_filp;
552 }
553
554 lock_page(page);
555 data = page_address(page);
556
557 if (file_pos == 0) {
558 struct reiserfs_xattr_header *rxh;
559 skip = file_pos = sizeof(struct reiserfs_xattr_header);
560 if (chunk + skip > PAGE_CACHE_SIZE)
561 chunk = PAGE_CACHE_SIZE - skip;
562 rxh = (struct reiserfs_xattr_header *)data;
563 rxh->h_magic = cpu_to_le32(REISERFS_XATTR_MAGIC);
564 rxh->h_hash = cpu_to_le32(xahash);
565 }
566
567 err = mapping->a_ops->prepare_write(fp, page, page_offset,
568 page_offset + chunk + skip);
569 if (!err) {
570 if (buffer)
571 memcpy(data + skip, buffer + buffer_pos, chunk);
572 err =
573 mapping->a_ops->commit_write(fp, page, page_offset,
574 page_offset + chunk +
575 skip);
576 }
577 unlock_page(page);
578 reiserfs_put_page(page);
579 buffer_pos += chunk;
580 file_pos += chunk;
581 skip = 0;
582 if (err || buffer_size == 0 || !buffer)
583 break;
584 }
585
586 /* We can't mark the inode dirty if it's not hashed. This is the case
587 * when we're inheriting the default ACL. If we dirty it, the inode
588 * gets marked dirty, but won't (ever) make it onto the dirty list until
589 * it's synced explicitly to clear I_DIRTY. This is bad. */
590 if (!hlist_unhashed(&inode->i_hash)) {
591 inode->i_ctime = CURRENT_TIME_SEC;
592 mark_inode_dirty(inode);
1da177e4 593 }
bd4c625c
LT
594
595 out_filp:
1b1dcc1b 596 mutex_unlock(&xinode->i_mutex);
bd4c625c
LT
597 fput(fp);
598
599 out:
600 return err;
1da177e4
LT
601}
602
603/*
1b1dcc1b 604 * inode->i_mutex: down
1da177e4
LT
605 */
606int
bd4c625c
LT
607reiserfs_xattr_get(const struct inode *inode, const char *name, void *buffer,
608 size_t buffer_size)
1da177e4 609{
bd4c625c
LT
610 ssize_t err = 0;
611 struct file *fp;
612 size_t isize;
613 size_t file_pos = 0;
614 size_t buffer_pos = 0;
615 struct page *page;
616 struct inode *xinode;
617 __u32 hash = 0;
618
619 if (name == NULL)
620 return -EINVAL;
621
622 /* We can't have xattrs attached to v1 items since they don't have
623 * generation numbers */
624 if (get_inode_sd_version(inode) == STAT_DATA_V1)
625 return -EOPNOTSUPP;
626
627 fp = open_xa_file(inode, name, FL_READONLY);
628 if (IS_ERR(fp)) {
629 err = PTR_ERR(fp);
630 goto out;
631 }
632
633 xinode = fp->f_dentry->d_inode;
634 isize = xinode->i_size;
635 REISERFS_I(inode)->i_flags |= i_has_xattr_dir;
636
637 /* Just return the size needed */
638 if (buffer == NULL) {
639 err = isize - sizeof(struct reiserfs_xattr_header);
640 goto out_dput;
641 }
642
643 if (buffer_size < isize - sizeof(struct reiserfs_xattr_header)) {
644 err = -ERANGE;
645 goto out_dput;
646 }
647
648 while (file_pos < isize) {
649 size_t chunk;
650 char *data;
651 size_t skip = 0;
652 if (isize - file_pos > PAGE_CACHE_SIZE)
653 chunk = PAGE_CACHE_SIZE;
654 else
655 chunk = isize - file_pos;
656
657 page = reiserfs_get_page(xinode, file_pos >> PAGE_CACHE_SHIFT);
658 if (IS_ERR(page)) {
659 err = PTR_ERR(page);
660 goto out_dput;
661 }
662
663 lock_page(page);
664 data = page_address(page);
665 if (file_pos == 0) {
666 struct reiserfs_xattr_header *rxh =
667 (struct reiserfs_xattr_header *)data;
668 skip = file_pos = sizeof(struct reiserfs_xattr_header);
669 chunk -= skip;
670 /* Magic doesn't match up.. */
671 if (rxh->h_magic != cpu_to_le32(REISERFS_XATTR_MAGIC)) {
672 unlock_page(page);
673 reiserfs_put_page(page);
674 reiserfs_warning(inode->i_sb,
675 "Invalid magic for xattr (%s) "
676 "associated with %k", name,
677 INODE_PKEY(inode));
678 err = -EIO;
679 goto out_dput;
680 }
681 hash = le32_to_cpu(rxh->h_hash);
682 }
683 memcpy(buffer + buffer_pos, data + skip, chunk);
684 unlock_page(page);
685 reiserfs_put_page(page);
686 file_pos += chunk;
687 buffer_pos += chunk;
688 skip = 0;
689 }
690 err = isize - sizeof(struct reiserfs_xattr_header);
691
692 if (xattr_hash(buffer, isize - sizeof(struct reiserfs_xattr_header)) !=
693 hash) {
694 reiserfs_warning(inode->i_sb,
695 "Invalid hash for xattr (%s) associated "
696 "with %k", name, INODE_PKEY(inode));
697 err = -EIO;
698 }
699
700 out_dput:
701 fput(fp);
702
703 out:
704 return err;
1da177e4
LT
705}
706
707static int
bd4c625c 708__reiserfs_xattr_del(struct dentry *xadir, const char *name, int namelen)
1da177e4 709{
bd4c625c
LT
710 struct dentry *dentry;
711 struct inode *dir = xadir->d_inode;
712 int err = 0;
713
714 dentry = lookup_one_len(name, xadir, namelen);
715 if (IS_ERR(dentry)) {
716 err = PTR_ERR(dentry);
717 goto out;
718 } else if (!dentry->d_inode) {
719 err = -ENODATA;
720 goto out_file;
721 }
722
723 /* Skip directories.. */
724 if (S_ISDIR(dentry->d_inode->i_mode))
725 goto out_file;
726
727 if (!is_reiserfs_priv_object(dentry->d_inode)) {
728 reiserfs_warning(dir->i_sb, "OID %08x [%.*s/%.*s] doesn't have "
729 "priv flag set [parent is %sset].",
730 le32_to_cpu(INODE_PKEY(dentry->d_inode)->
731 k_objectid), xadir->d_name.len,
732 xadir->d_name.name, namelen, name,
733 is_reiserfs_priv_object(xadir->
734 d_inode) ? "" :
735 "not ");
736 dput(dentry);
737 return -EIO;
738 }
1da177e4 739
bd4c625c
LT
740 err = dir->i_op->unlink(dir, dentry);
741 if (!err)
742 d_delete(dentry);
1da177e4 743
bd4c625c
LT
744 out_file:
745 dput(dentry);
746
747 out:
748 return err;
749}
750
751int reiserfs_xattr_del(struct inode *inode, const char *name)
1da177e4 752{
bd4c625c
LT
753 struct dentry *dir;
754 int err;
1da177e4 755
bd4c625c
LT
756 dir = open_xa_dir(inode, FL_READONLY);
757 if (IS_ERR(dir)) {
758 err = PTR_ERR(dir);
759 goto out;
760 }
1da177e4 761
bd4c625c
LT
762 err = __reiserfs_xattr_del(dir, name, strlen(name));
763 dput(dir);
1da177e4 764
bd4c625c
LT
765 if (!err) {
766 inode->i_ctime = CURRENT_TIME_SEC;
767 mark_inode_dirty(inode);
768 }
1da177e4 769
bd4c625c
LT
770 out:
771 return err;
1da177e4
LT
772}
773
774/* The following are side effects of other operations that aren't explicitly
775 * modifying extended attributes. This includes operations such as permissions
776 * or ownership changes, object deletions, etc. */
777
778static int
bd4c625c
LT
779reiserfs_delete_xattrs_filler(void *buf, const char *name, int namelen,
780 loff_t offset, ino_t ino, unsigned int d_type)
1da177e4 781{
bd4c625c 782 struct dentry *xadir = (struct dentry *)buf;
1da177e4 783
bd4c625c 784 return __reiserfs_xattr_del(xadir, name, namelen);
1da177e4
LT
785
786}
787
1b1dcc1b 788/* This is called w/ inode->i_mutex downed */
bd4c625c 789int reiserfs_delete_xattrs(struct inode *inode)
1da177e4 790{
bd4c625c
LT
791 struct file *fp;
792 struct dentry *dir, *root;
793 int err = 0;
794
795 /* Skip out, an xattr has no xattrs associated with it */
796 if (is_reiserfs_priv_object(inode) ||
797 get_inode_sd_version(inode) == STAT_DATA_V1 ||
798 !reiserfs_xattrs(inode->i_sb)) {
799 return 0;
800 }
801 reiserfs_read_lock_xattrs(inode->i_sb);
802 dir = open_xa_dir(inode, FL_READONLY);
803 reiserfs_read_unlock_xattrs(inode->i_sb);
804 if (IS_ERR(dir)) {
805 err = PTR_ERR(dir);
806 goto out;
807 } else if (!dir->d_inode) {
808 dput(dir);
809 return 0;
810 }
811
812 fp = dentry_open(dir, NULL, O_RDWR);
813 if (IS_ERR(fp)) {
814 err = PTR_ERR(fp);
815 /* dentry_open dputs the dentry if it fails */
816 goto out;
817 }
818
819 lock_kernel();
820 err = xattr_readdir(fp, reiserfs_delete_xattrs_filler, dir);
821 if (err) {
822 unlock_kernel();
823 goto out_dir;
824 }
825
826 /* Leftovers besides . and .. -- that's not good. */
827 if (dir->d_inode->i_nlink <= 2) {
828 root = get_xa_root(inode->i_sb);
829 reiserfs_write_lock_xattrs(inode->i_sb);
830 err = vfs_rmdir(root->d_inode, dir);
831 reiserfs_write_unlock_xattrs(inode->i_sb);
832 dput(root);
833 } else {
834 reiserfs_warning(inode->i_sb,
835 "Couldn't remove all entries in directory");
836 }
837 unlock_kernel();
838
839 out_dir:
840 fput(fp);
841
842 out:
843 if (!err)
844 REISERFS_I(inode)->i_flags =
845 REISERFS_I(inode)->i_flags & ~i_has_xattr_dir;
846 return err;
1da177e4
LT
847}
848
849struct reiserfs_chown_buf {
bd4c625c
LT
850 struct inode *inode;
851 struct dentry *xadir;
852 struct iattr *attrs;
1da177e4
LT
853};
854
855/* XXX: If there is a better way to do this, I'd love to hear about it */
856static int
bd4c625c
LT
857reiserfs_chown_xattrs_filler(void *buf, const char *name, int namelen,
858 loff_t offset, ino_t ino, unsigned int d_type)
1da177e4 859{
bd4c625c
LT
860 struct reiserfs_chown_buf *chown_buf = (struct reiserfs_chown_buf *)buf;
861 struct dentry *xafile, *xadir = chown_buf->xadir;
862 struct iattr *attrs = chown_buf->attrs;
863 int err = 0;
864
865 xafile = lookup_one_len(name, xadir, namelen);
866 if (IS_ERR(xafile))
867 return PTR_ERR(xafile);
868 else if (!xafile->d_inode) {
869 dput(xafile);
870 return -ENODATA;
871 }
872
873 if (!S_ISDIR(xafile->d_inode->i_mode))
874 err = notify_change(xafile, attrs);
875 dput(xafile);
876
877 return err;
1da177e4
LT
878}
879
bd4c625c 880int reiserfs_chown_xattrs(struct inode *inode, struct iattr *attrs)
1da177e4 881{
bd4c625c
LT
882 struct file *fp;
883 struct dentry *dir;
884 int err = 0;
885 struct reiserfs_chown_buf buf;
886 unsigned int ia_valid = attrs->ia_valid;
887
888 /* Skip out, an xattr has no xattrs associated with it */
889 if (is_reiserfs_priv_object(inode) ||
890 get_inode_sd_version(inode) == STAT_DATA_V1 ||
891 !reiserfs_xattrs(inode->i_sb)) {
892 return 0;
893 }
894 reiserfs_read_lock_xattrs(inode->i_sb);
895 dir = open_xa_dir(inode, FL_READONLY);
896 reiserfs_read_unlock_xattrs(inode->i_sb);
897 if (IS_ERR(dir)) {
898 if (PTR_ERR(dir) != -ENODATA)
899 err = PTR_ERR(dir);
900 goto out;
901 } else if (!dir->d_inode) {
902 dput(dir);
903 goto out;
904 }
905
906 fp = dentry_open(dir, NULL, O_RDWR);
907 if (IS_ERR(fp)) {
908 err = PTR_ERR(fp);
909 /* dentry_open dputs the dentry if it fails */
910 goto out;
911 }
1da177e4 912
bd4c625c
LT
913 lock_kernel();
914
915 attrs->ia_valid &= (ATTR_UID | ATTR_GID | ATTR_CTIME);
916 buf.xadir = dir;
917 buf.attrs = attrs;
918 buf.inode = inode;
919
920 err = xattr_readdir(fp, reiserfs_chown_xattrs_filler, &buf);
921 if (err) {
922 unlock_kernel();
923 goto out_dir;
924 }
925
926 err = notify_change(dir, attrs);
927 unlock_kernel();
928
929 out_dir:
930 fput(fp);
931
932 out:
933 attrs->ia_valid = ia_valid;
934 return err;
935}
1da177e4
LT
936
937/* Actual operations that are exported to VFS-land */
938
939/*
940 * Inode operation getxattr()
1b1dcc1b 941 * Preliminary locking: we down dentry->d_inode->i_mutex
1da177e4
LT
942 */
943ssize_t
bd4c625c
LT
944reiserfs_getxattr(struct dentry * dentry, const char *name, void *buffer,
945 size_t size)
1da177e4 946{
bd4c625c
LT
947 struct reiserfs_xattr_handler *xah = find_xattr_handler_prefix(name);
948 int err;
949
950 if (!xah || !reiserfs_xattrs(dentry->d_sb) ||
951 get_inode_sd_version(dentry->d_inode) == STAT_DATA_V1)
952 return -EOPNOTSUPP;
953
954 reiserfs_read_lock_xattr_i(dentry->d_inode);
955 reiserfs_read_lock_xattrs(dentry->d_sb);
956 err = xah->get(dentry->d_inode, name, buffer, size);
957 reiserfs_read_unlock_xattrs(dentry->d_sb);
958 reiserfs_read_unlock_xattr_i(dentry->d_inode);
959 return err;
1da177e4
LT
960}
961
1da177e4
LT
962/*
963 * Inode operation setxattr()
964 *
1b1dcc1b 965 * dentry->d_inode->i_mutex down
1da177e4
LT
966 */
967int
bd4c625c
LT
968reiserfs_setxattr(struct dentry *dentry, const char *name, const void *value,
969 size_t size, int flags)
1da177e4 970{
bd4c625c
LT
971 struct reiserfs_xattr_handler *xah = find_xattr_handler_prefix(name);
972 int err;
973 int lock;
974
975 if (!xah || !reiserfs_xattrs(dentry->d_sb) ||
976 get_inode_sd_version(dentry->d_inode) == STAT_DATA_V1)
977 return -EOPNOTSUPP;
978
bd4c625c
LT
979 reiserfs_write_lock_xattr_i(dentry->d_inode);
980 lock = !has_xattr_dir(dentry->d_inode);
981 if (lock)
982 reiserfs_write_lock_xattrs(dentry->d_sb);
983 else
984 reiserfs_read_lock_xattrs(dentry->d_sb);
985 err = xah->set(dentry->d_inode, name, value, size, flags);
986 if (lock)
987 reiserfs_write_unlock_xattrs(dentry->d_sb);
988 else
989 reiserfs_read_unlock_xattrs(dentry->d_sb);
990 reiserfs_write_unlock_xattr_i(dentry->d_inode);
991 return err;
1da177e4
LT
992}
993
994/*
995 * Inode operation removexattr()
996 *
1b1dcc1b 997 * dentry->d_inode->i_mutex down
1da177e4 998 */
bd4c625c 999int reiserfs_removexattr(struct dentry *dentry, const char *name)
1da177e4 1000{
bd4c625c
LT
1001 int err;
1002 struct reiserfs_xattr_handler *xah = find_xattr_handler_prefix(name);
1da177e4 1003
bd4c625c
LT
1004 if (!xah || !reiserfs_xattrs(dentry->d_sb) ||
1005 get_inode_sd_version(dentry->d_inode) == STAT_DATA_V1)
1006 return -EOPNOTSUPP;
1da177e4 1007
bd4c625c
LT
1008 reiserfs_write_lock_xattr_i(dentry->d_inode);
1009 reiserfs_read_lock_xattrs(dentry->d_sb);
1da177e4 1010
bd4c625c
LT
1011 /* Deletion pre-operation */
1012 if (xah->del) {
1013 err = xah->del(dentry->d_inode, name);
1014 if (err)
1015 goto out;
1016 }
1da177e4 1017
bd4c625c 1018 err = reiserfs_xattr_del(dentry->d_inode, name);
1da177e4 1019
bd4c625c
LT
1020 dentry->d_inode->i_ctime = CURRENT_TIME_SEC;
1021 mark_inode_dirty(dentry->d_inode);
1da177e4 1022
bd4c625c
LT
1023 out:
1024 reiserfs_read_unlock_xattrs(dentry->d_sb);
1025 reiserfs_write_unlock_xattr_i(dentry->d_inode);
1026 return err;
1da177e4
LT
1027}
1028
1da177e4
LT
1029/* This is what filldir will use:
1030 * r_pos will always contain the amount of space required for the entire
1031 * list. If r_pos becomes larger than r_size, we need more space and we
1032 * return an error indicating this. If r_pos is less than r_size, then we've
1033 * filled the buffer successfully and we return success */
1034struct reiserfs_listxattr_buf {
bd4c625c
LT
1035 int r_pos;
1036 int r_size;
1037 char *r_buf;
1038 struct inode *r_inode;
1da177e4
LT
1039};
1040
1041static int
bd4c625c
LT
1042reiserfs_listxattr_filler(void *buf, const char *name, int namelen,
1043 loff_t offset, ino_t ino, unsigned int d_type)
1da177e4 1044{
bd4c625c
LT
1045 struct reiserfs_listxattr_buf *b = (struct reiserfs_listxattr_buf *)buf;
1046 int len = 0;
1047 if (name[0] != '.'
1048 || (namelen != 1 && (name[1] != '.' || namelen != 2))) {
1049 struct reiserfs_xattr_handler *xah =
1050 find_xattr_handler_prefix(name);
1051 if (!xah)
1052 return 0; /* Unsupported xattr name, skip it */
1053
1054 /* We call ->list() twice because the operation isn't required to just
1055 * return the name back - we want to make sure we have enough space */
1056 len += xah->list(b->r_inode, name, namelen, NULL);
1057
1058 if (len) {
1059 if (b->r_pos + len + 1 <= b->r_size) {
1060 char *p = b->r_buf + b->r_pos;
1061 p += xah->list(b->r_inode, name, namelen, p);
1062 *p++ = '\0';
1063 }
1064 b->r_pos += len + 1;
1065 }
1066 }
1067
1068 return 0;
1da177e4 1069}
bd4c625c 1070
1da177e4
LT
1071/*
1072 * Inode operation listxattr()
1073 *
1b1dcc1b 1074 * Preliminary locking: we down dentry->d_inode->i_mutex
1da177e4 1075 */
bd4c625c 1076ssize_t reiserfs_listxattr(struct dentry * dentry, char *buffer, size_t size)
1da177e4 1077{
bd4c625c
LT
1078 struct file *fp;
1079 struct dentry *dir;
1080 int err = 0;
1081 struct reiserfs_listxattr_buf buf;
1082
1083 if (!dentry->d_inode)
1084 return -EINVAL;
1085
1086 if (!reiserfs_xattrs(dentry->d_sb) ||
1087 get_inode_sd_version(dentry->d_inode) == STAT_DATA_V1)
1088 return -EOPNOTSUPP;
1089
1090 reiserfs_read_lock_xattr_i(dentry->d_inode);
1091 reiserfs_read_lock_xattrs(dentry->d_sb);
1092 dir = open_xa_dir(dentry->d_inode, FL_READONLY);
1093 reiserfs_read_unlock_xattrs(dentry->d_sb);
1094 if (IS_ERR(dir)) {
1095 err = PTR_ERR(dir);
1096 if (err == -ENODATA)
1097 err = 0; /* Not an error if there aren't any xattrs */
1098 goto out;
1099 }
1100
1101 fp = dentry_open(dir, NULL, O_RDWR);
1102 if (IS_ERR(fp)) {
1103 err = PTR_ERR(fp);
1104 /* dentry_open dputs the dentry if it fails */
1105 goto out;
1106 }
1107
1108 buf.r_buf = buffer;
1109 buf.r_size = buffer ? size : 0;
1110 buf.r_pos = 0;
1111 buf.r_inode = dentry->d_inode;
1112
1113 REISERFS_I(dentry->d_inode)->i_flags |= i_has_xattr_dir;
1114
1115 err = xattr_readdir(fp, reiserfs_listxattr_filler, &buf);
1116 if (err)
1117 goto out_dir;
1118
1119 if (buf.r_pos > buf.r_size && buffer != NULL)
1120 err = -ERANGE;
1121 else
1122 err = buf.r_pos;
1123
1124 out_dir:
1125 fput(fp);
1126
1127 out:
1128 reiserfs_read_unlock_xattr_i(dentry->d_inode);
1129 return err;
1da177e4
LT
1130}
1131
1132/* This is the implementation for the xattr plugin infrastructure */
bd4c625c 1133static struct list_head xattr_handlers = LIST_HEAD_INIT(xattr_handlers);
1da177e4
LT
1134static DEFINE_RWLOCK(handler_lock);
1135
bd4c625c
LT
1136static struct reiserfs_xattr_handler *find_xattr_handler_prefix(const char
1137 *prefix)
1da177e4 1138{
bd4c625c
LT
1139 struct reiserfs_xattr_handler *xah = NULL;
1140 struct list_head *p;
1141
1142 read_lock(&handler_lock);
1143 list_for_each(p, &xattr_handlers) {
1144 xah = list_entry(p, struct reiserfs_xattr_handler, handlers);
1145 if (strncmp(xah->prefix, prefix, strlen(xah->prefix)) == 0)
1146 break;
1147 xah = NULL;
1148 }
1149
1150 read_unlock(&handler_lock);
1151 return xah;
1da177e4
LT
1152}
1153
bd4c625c 1154static void __unregister_handlers(void)
1da177e4 1155{
bd4c625c
LT
1156 struct reiserfs_xattr_handler *xah;
1157 struct list_head *p, *tmp;
1da177e4 1158
bd4c625c
LT
1159 list_for_each_safe(p, tmp, &xattr_handlers) {
1160 xah = list_entry(p, struct reiserfs_xattr_handler, handlers);
1161 if (xah->exit)
1162 xah->exit();
1da177e4 1163
bd4c625c
LT
1164 list_del_init(p);
1165 }
1166 INIT_LIST_HEAD(&xattr_handlers);
1da177e4
LT
1167}
1168
bd4c625c 1169int __init reiserfs_xattr_register_handlers(void)
1da177e4 1170{
bd4c625c
LT
1171 int err = 0;
1172 struct reiserfs_xattr_handler *xah;
1173 struct list_head *p;
1da177e4 1174
bd4c625c 1175 write_lock(&handler_lock);
1da177e4 1176
bd4c625c
LT
1177 /* If we're already initialized, nothing to do */
1178 if (!list_empty(&xattr_handlers)) {
1179 write_unlock(&handler_lock);
1180 return 0;
1181 }
1da177e4 1182
bd4c625c
LT
1183 /* Add the handlers */
1184 list_add_tail(&user_handler.handlers, &xattr_handlers);
1185 list_add_tail(&trusted_handler.handlers, &xattr_handlers);
1da177e4 1186#ifdef CONFIG_REISERFS_FS_SECURITY
bd4c625c 1187 list_add_tail(&security_handler.handlers, &xattr_handlers);
1da177e4
LT
1188#endif
1189#ifdef CONFIG_REISERFS_FS_POSIX_ACL
bd4c625c
LT
1190 list_add_tail(&posix_acl_access_handler.handlers, &xattr_handlers);
1191 list_add_tail(&posix_acl_default_handler.handlers, &xattr_handlers);
1da177e4
LT
1192#endif
1193
bd4c625c
LT
1194 /* Run initializers, if available */
1195 list_for_each(p, &xattr_handlers) {
1196 xah = list_entry(p, struct reiserfs_xattr_handler, handlers);
1197 if (xah->init) {
1198 err = xah->init();
1199 if (err) {
1200 list_del_init(p);
1201 break;
1202 }
1203 }
1204 }
1205
1206 /* Clean up other handlers, if any failed */
1207 if (err)
1208 __unregister_handlers();
1209
1210 write_unlock(&handler_lock);
1211 return err;
1da177e4
LT
1212}
1213
bd4c625c 1214void reiserfs_xattr_unregister_handlers(void)
1da177e4 1215{
bd4c625c
LT
1216 write_lock(&handler_lock);
1217 __unregister_handlers();
1218 write_unlock(&handler_lock);
1da177e4
LT
1219}
1220
1221/* This will catch lookups from the fs root to .reiserfs_priv */
1222static int
bd4c625c 1223xattr_lookup_poison(struct dentry *dentry, struct qstr *q1, struct qstr *name)
1da177e4 1224{
bd4c625c
LT
1225 struct dentry *priv_root = REISERFS_SB(dentry->d_sb)->priv_root;
1226 if (name->len == priv_root->d_name.len &&
1227 name->hash == priv_root->d_name.hash &&
1228 !memcmp(name->name, priv_root->d_name.name, name->len)) {
1229 return -ENOENT;
1230 } else if (q1->len == name->len &&
1231 !memcmp(q1->name, name->name, name->len))
1232 return 0;
1233 return 1;
1da177e4
LT
1234}
1235
1236static struct dentry_operations xattr_lookup_poison_ops = {
bd4c625c 1237 .d_compare = xattr_lookup_poison,
1da177e4
LT
1238};
1239
1da177e4
LT
1240/* We need to take a copy of the mount flags since things like
1241 * MS_RDONLY don't get set until *after* we're called.
1242 * mount_flags != mount_options */
bd4c625c 1243int reiserfs_xattr_init(struct super_block *s, int mount_flags)
1da177e4 1244{
bd4c625c
LT
1245 int err = 0;
1246
1247 /* We need generation numbers to ensure that the oid mapping is correct
1248 * v3.5 filesystems don't have them. */
1249 if (!old_format_only(s)) {
1250 set_bit(REISERFS_XATTRS, &(REISERFS_SB(s)->s_mount_opt));
1251 } else if (reiserfs_xattrs_optional(s)) {
1252 /* Old format filesystem, but optional xattrs have been enabled
1253 * at mount time. Error out. */
1254 reiserfs_warning(s, "xattrs/ACLs not supported on pre v3.6 "
1255 "format filesystem. Failing mount.");
1256 err = -EOPNOTSUPP;
1257 goto error;
1258 } else {
1259 /* Old format filesystem, but no optional xattrs have been enabled. This
1260 * means we silently disable xattrs on the filesystem. */
1261 clear_bit(REISERFS_XATTRS, &(REISERFS_SB(s)->s_mount_opt));
1262 }
1263
1264 /* If we don't have the privroot located yet - go find it */
1265 if (reiserfs_xattrs(s) && !REISERFS_SB(s)->priv_root) {
1266 struct dentry *dentry;
1267 dentry = lookup_one_len(PRIVROOT_NAME, s->s_root,
1268 strlen(PRIVROOT_NAME));
1269 if (!IS_ERR(dentry)) {
1270 if (!(mount_flags & MS_RDONLY) && !dentry->d_inode) {
1271 struct inode *inode = dentry->d_parent->d_inode;
1b1dcc1b 1272 mutex_lock(&inode->i_mutex);
bd4c625c 1273 err = inode->i_op->mkdir(inode, dentry, 0700);
1b1dcc1b 1274 mutex_unlock(&inode->i_mutex);
bd4c625c
LT
1275 if (err) {
1276 dput(dentry);
1277 dentry = NULL;
1278 }
1279
1280 if (dentry && dentry->d_inode)
1281 reiserfs_warning(s,
1282 "Created %s on %s - reserved for "
1283 "xattr storage.",
1284 PRIVROOT_NAME,
1285 reiserfs_bdevname
1286 (inode->i_sb));
1287 } else if (!dentry->d_inode) {
1288 dput(dentry);
1289 dentry = NULL;
1290 }
1291 } else
1292 err = PTR_ERR(dentry);
1293
1294 if (!err && dentry) {
1295 s->s_root->d_op = &xattr_lookup_poison_ops;
1296 reiserfs_mark_inode_private(dentry->d_inode);
1297 REISERFS_SB(s)->priv_root = dentry;
1298 } else if (!(mount_flags & MS_RDONLY)) { /* xattrs are unavailable */
1299 /* If we're read-only it just means that the dir hasn't been
1300 * created. Not an error -- just no xattrs on the fs. We'll
1301 * check again if we go read-write */
1302 reiserfs_warning(s, "xattrs/ACLs enabled and couldn't "
1303 "find/create .reiserfs_priv. Failing mount.");
1304 err = -EOPNOTSUPP;
1305 }
1306 }
1307
1308 error:
1309 /* This is only nonzero if there was an error initializing the xattr
1310 * directory or if there is a condition where we don't support them. */
1311 if (err) {
1312 clear_bit(REISERFS_XATTRS, &(REISERFS_SB(s)->s_mount_opt));
1313 clear_bit(REISERFS_XATTRS_USER, &(REISERFS_SB(s)->s_mount_opt));
1314 clear_bit(REISERFS_POSIXACL, &(REISERFS_SB(s)->s_mount_opt));
1315 }
1316
1317 /* The super_block MS_POSIXACL must mirror the (no)acl mount option. */
1318 s->s_flags = s->s_flags & ~MS_POSIXACL;
1319 if (reiserfs_posixacl(s))
1320 s->s_flags |= MS_POSIXACL;
1321
1322 return err;
1da177e4
LT
1323}
1324
1325static int
bd4c625c
LT
1326__reiserfs_permission(struct inode *inode, int mask, struct nameidata *nd,
1327 int need_lock)
1da177e4 1328{
bd4c625c 1329 umode_t mode = inode->i_mode;
1da177e4
LT
1330
1331 if (mask & MAY_WRITE) {
1332 /*
1333 * Nobody gets write access to a read-only fs.
1334 */
1335 if (IS_RDONLY(inode) &&
1336 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
1337 return -EROFS;
1338
1339 /*
1340 * Nobody gets write access to an immutable file.
1341 */
1342 if (IS_IMMUTABLE(inode))
1343 return -EACCES;
1344 }
1345
1346 /* We don't do permission checks on the internal objects.
bd4c625c
LT
1347 * Permissions are determined by the "owning" object. */
1348 if (is_reiserfs_priv_object(inode))
1da177e4
LT
1349 return 0;
1350
1351 if (current->fsuid == inode->i_uid) {
1352 mode >>= 6;
1353#ifdef CONFIG_REISERFS_FS_POSIX_ACL
1354 } else if (reiserfs_posixacl(inode->i_sb) &&
bd4c625c
LT
1355 get_inode_sd_version(inode) != STAT_DATA_V1) {
1356 struct posix_acl *acl;
1da177e4
LT
1357
1358 /* ACL can't contain additional permissions if
1359 the ACL_MASK entry is 0 */
1360 if (!(mode & S_IRWXG))
1361 goto check_groups;
1362
bd4c625c
LT
1363 if (need_lock) {
1364 reiserfs_read_lock_xattr_i(inode);
1365 reiserfs_read_lock_xattrs(inode->i_sb);
1366 }
1367 acl = reiserfs_get_acl(inode, ACL_TYPE_ACCESS);
1368 if (need_lock) {
1369 reiserfs_read_unlock_xattrs(inode->i_sb);
1370 reiserfs_read_unlock_xattr_i(inode);
1da177e4 1371 }
bd4c625c
LT
1372 if (IS_ERR(acl)) {
1373 if (PTR_ERR(acl) == -ENODATA)
1374 goto check_groups;
1375 return PTR_ERR(acl);
1da177e4 1376 }
bd4c625c
LT
1377
1378 if (acl) {
1379 int err = posix_acl_permission(inode, acl, mask);
1380 posix_acl_release(acl);
1381 if (err == -EACCES) {
1382 goto check_capabilities;
1383 }
1384 return err;
1da177e4
LT
1385 } else {
1386 goto check_groups;
bd4c625c 1387 }
1da177e4
LT
1388#endif
1389 } else {
bd4c625c 1390 check_groups:
1da177e4
LT
1391 if (in_group_p(inode->i_gid))
1392 mode >>= 3;
1393 }
1394
1395 /*
1396 * If the DACs are ok we don't need any capability check.
1397 */
bd4c625c 1398 if (((mode & mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == mask))
1da177e4
LT
1399 return 0;
1400
bd4c625c 1401 check_capabilities:
1da177e4
LT
1402 /*
1403 * Read/write DACs are always overridable.
1404 * Executable DACs are overridable if at least one exec bit is set.
1405 */
1406 if (!(mask & MAY_EXEC) ||
1407 (inode->i_mode & S_IXUGO) || S_ISDIR(inode->i_mode))
1408 if (capable(CAP_DAC_OVERRIDE))
1409 return 0;
1410
1411 /*
1412 * Searching includes executable on directories, else just read.
1413 */
1414 if (mask == MAY_READ || (S_ISDIR(inode->i_mode) && !(mask & MAY_WRITE)))
1415 if (capable(CAP_DAC_READ_SEARCH))
1416 return 0;
1417
1418 return -EACCES;
1419}
1420
bd4c625c 1421int reiserfs_permission(struct inode *inode, int mask, struct nameidata *nd)
1da177e4 1422{
bd4c625c 1423 return __reiserfs_permission(inode, mask, nd, 1);
1da177e4
LT
1424}
1425
1426int
bd4c625c 1427reiserfs_permission_locked(struct inode *inode, int mask, struct nameidata *nd)
1da177e4 1428{
bd4c625c 1429 return __reiserfs_permission(inode, mask, nd, 0);
1da177e4 1430}