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