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