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