reiserfs: run scripts/Lindent on reiserfs code
[pandora-kernel.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
55                                                                 *prefix);
56
57 static struct dentry *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 =
73                     privroot->d_inode->i_op->mkdir(privroot->d_inode, xaroot,
74                                                    0700);
75                 up(&privroot->d_inode->i_sem);
76
77                 if (err) {
78                         dput(xaroot);
79                         dput(privroot);
80                         return ERR_PTR(err);
81                 }
82                 REISERFS_SB(sb)->xattr_root = dget(xaroot);
83         }
84
85       out:
86         dput(privroot);
87         return xaroot;
88 }
89
90 /* This will return a dentry, or error, refering to the xa root directory.
91  * If the xa root doesn't exist yet, the dentry will be returned without
92  * an associated inode. This dentry can be used with ->mkdir to create
93  * the xa directory. */
94 static struct dentry *__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 *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 *open_xa_dir(const struct inode *inode, int flags)
137 {
138         struct dentry *xaroot, *xadir;
139         char namebuf[17];
140
141         xaroot = get_xa_root(inode->i_sb);
142         if (IS_ERR(xaroot)) {
143                 return xaroot;
144         } else if (!xaroot) {
145                 if (flags == 0 || flags & XATTR_CREATE) {
146                         xaroot = create_xa_root(inode->i_sb);
147                         if (IS_ERR(xaroot))
148                                 return xaroot;
149                 }
150                 if (!xaroot)
151                         return ERR_PTR(-ENODATA);
152         }
153
154         /* ok, we have xaroot open */
155
156         snprintf(namebuf, sizeof(namebuf), "%X.%X",
157                  le32_to_cpu(INODE_PKEY(inode)->k_objectid),
158                  inode->i_generation);
159         xadir = lookup_one_len(namebuf, xaroot, strlen(namebuf));
160         if (IS_ERR(xadir)) {
161                 dput(xaroot);
162                 return xadir;
163         }
164
165         if (!xadir->d_inode) {
166                 int err;
167                 if (flags == 0 || flags & XATTR_CREATE) {
168                         /* Although there is nothing else trying to create this directory,
169                          * another directory with the same hash may be created, so we need
170                          * to protect against that */
171                         err =
172                             xaroot->d_inode->i_op->mkdir(xaroot->d_inode, xadir,
173                                                          0700);
174                         if (err) {
175                                 dput(xaroot);
176                                 dput(xadir);
177                                 return ERR_PTR(err);
178                         }
179                 }
180                 if (!xadir->d_inode) {
181                         dput(xaroot);
182                         dput(xadir);
183                         return ERR_PTR(-ENODATA);
184                 }
185         }
186
187         dput(xaroot);
188         return xadir;
189 }
190
191 /* Returns a dentry corresponding to a specific extended attribute file
192  * for the inode. If flags allow, the file is created. Otherwise, a
193  * valid or negative dentry, or an error is returned. */
194 static struct dentry *get_xa_file_dentry(const struct inode *inode,
195                                          const char *name, int flags)
196 {
197         struct dentry *xadir, *xafile;
198         int err = 0;
199
200         xadir = open_xa_dir(inode, flags);
201         if (IS_ERR(xadir)) {
202                 return ERR_PTR(PTR_ERR(xadir));
203         } else if (xadir && !xadir->d_inode) {
204                 dput(xadir);
205                 return ERR_PTR(-ENODATA);
206         }
207
208         xafile = lookup_one_len(name, xadir, strlen(name));
209         if (IS_ERR(xafile)) {
210                 dput(xadir);
211                 return ERR_PTR(PTR_ERR(xafile));
212         }
213
214         if (xafile->d_inode) {  /* file exists */
215                 if (flags & XATTR_CREATE) {
216                         err = -EEXIST;
217                         dput(xafile);
218                         goto out;
219                 }
220         } else if (flags & XATTR_REPLACE || flags & FL_READONLY) {
221                 goto out;
222         } else {
223                 /* inode->i_sem is down, so nothing else can try to create
224                  * the same xattr */
225                 err = xadir->d_inode->i_op->create(xadir->d_inode, xafile,
226                                                    0700 | S_IFREG, NULL);
227
228                 if (err) {
229                         dput(xafile);
230                         goto out;
231                 }
232         }
233
234       out:
235         dput(xadir);
236         if (err)
237                 xafile = ERR_PTR(err);
238         return xafile;
239 }
240
241 /* Opens a file pointer to the attribute associated with inode */
242 static struct file *open_xa_file(const struct inode *inode, const char *name,
243                                  int flags)
244 {
245         struct dentry *xafile;
246         struct file *fp;
247
248         xafile = get_xa_file_dentry(inode, name, flags);
249         if (IS_ERR(xafile))
250                 return ERR_PTR(PTR_ERR(xafile));
251         else if (!xafile->d_inode) {
252                 dput(xafile);
253                 return ERR_PTR(-ENODATA);
254         }
255
256         fp = dentry_open(xafile, NULL, O_RDWR);
257         /* dentry_open dputs the dentry if it fails */
258
259         return fp;
260 }
261
262 /*
263  * this is very similar to fs/reiserfs/dir.c:reiserfs_readdir, but
264  * we need to drop the path before calling the filldir struct.  That
265  * would be a big performance hit to the non-xattr case, so I've copied
266  * the whole thing for now. --clm
267  *
268  * the big difference is that I go backwards through the directory,
269  * and don't mess with f->f_pos, but the idea is the same.  Do some
270  * action on each and every entry in the directory.
271  *
272  * we're called with i_sem held, so there are no worries about the directory
273  * changing underneath us.
274  */
275 static int __xattr_readdir(struct file *filp, void *dirent, filldir_t filldir)
276 {
277         struct inode *inode = filp->f_dentry->d_inode;
278         struct cpu_key pos_key; /* key of current position in the directory (key of directory entry) */
279         INITIALIZE_PATH(path_to_entry);
280         struct buffer_head *bh;
281         int entry_num;
282         struct item_head *ih, tmp_ih;
283         int search_res;
284         char *local_buf;
285         loff_t next_pos;
286         char small_buf[32];     /* avoid kmalloc if we can */
287         struct reiserfs_de_head *deh;
288         int d_reclen;
289         char *d_name;
290         off_t d_off;
291         ino_t d_ino;
292         struct reiserfs_dir_entry de;
293
294         /* form key for search the next directory entry using f_pos field of
295            file structure */
296         next_pos = max_reiserfs_offset(inode);
297
298         while (1) {
299               research:
300                 if (next_pos <= DOT_DOT_OFFSET)
301                         break;
302                 make_cpu_key(&pos_key, inode, next_pos, TYPE_DIRENTRY, 3);
303
304                 search_res =
305                     search_by_entry_key(inode->i_sb, &pos_key, &path_to_entry,
306                                         &de);
307                 if (search_res == IO_ERROR) {
308                         // FIXME: we could just skip part of directory which could
309                         // not be read
310                         pathrelse(&path_to_entry);
311                         return -EIO;
312                 }
313
314                 if (search_res == NAME_NOT_FOUND)
315                         de.de_entry_num--;
316
317                 set_de_name_and_namelen(&de);
318                 entry_num = de.de_entry_num;
319                 deh = &(de.de_deh[entry_num]);
320
321                 bh = de.de_bh;
322                 ih = de.de_ih;
323
324                 if (!is_direntry_le_ih(ih)) {
325                         reiserfs_warning(inode->i_sb, "not direntry %h", ih);
326                         break;
327                 }
328                 copy_item_head(&tmp_ih, ih);
329
330                 /* we must have found item, that is item of this directory, */
331                 RFALSE(COMP_SHORT_KEYS(&(ih->ih_key), &pos_key),
332                        "vs-9000: found item %h does not match to dir we readdir %K",
333                        ih, &pos_key);
334
335                 if (deh_offset(deh) <= DOT_DOT_OFFSET) {
336                         break;
337                 }
338
339                 /* look for the previous entry in the directory */
340                 next_pos = deh_offset(deh) - 1;
341
342                 if (!de_visible(deh))
343                         /* it is hidden entry */
344                         continue;
345
346                 d_reclen = entry_length(bh, ih, entry_num);
347                 d_name = B_I_DEH_ENTRY_FILE_NAME(bh, ih, deh);
348                 d_off = deh_offset(deh);
349                 d_ino = deh_objectid(deh);
350
351                 if (!d_name[d_reclen - 1])
352                         d_reclen = strlen(d_name);
353
354                 if (d_reclen > REISERFS_MAX_NAME(inode->i_sb->s_blocksize)) {
355                         /* too big to send back to VFS */
356                         continue;
357                 }
358
359                 /* Ignore the .reiserfs_priv entry */
360                 if (reiserfs_xattrs(inode->i_sb) &&
361                     !old_format_only(inode->i_sb) &&
362                     deh_objectid(deh) ==
363                     le32_to_cpu(INODE_PKEY
364                                 (REISERFS_SB(inode->i_sb)->priv_root->d_inode)->
365                                 k_objectid))
366                         continue;
367
368                 if (d_reclen <= 32) {
369                         local_buf = small_buf;
370                 } else {
371                         local_buf =
372                             reiserfs_kmalloc(d_reclen, GFP_NOFS, inode->i_sb);
373                         if (!local_buf) {
374                                 pathrelse(&path_to_entry);
375                                 return -ENOMEM;
376                         }
377                         if (item_moved(&tmp_ih, &path_to_entry)) {
378                                 reiserfs_kfree(local_buf, d_reclen,
379                                                inode->i_sb);
380
381                                 /* sigh, must retry.  Do this same offset again */
382                                 next_pos = d_off;
383                                 goto research;
384                         }
385                 }
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,
403                                                inode->i_sb);
404                         }
405                         goto end;
406                 }
407                 if (local_buf != small_buf) {
408                         reiserfs_kfree(local_buf, d_reclen, inode->i_sb);
409                 }
410         }                       /* while */
411
412       end:
413         pathrelse(&path_to_entry);
414         return 0;
415 }
416
417 /*
418  * this could be done with dedicated readdir ops for the xattr files,
419  * but I want to get something working asap
420  * this is stolen from vfs_readdir
421  *
422  */
423 static
424 int xattr_readdir(struct file *file, filldir_t filler, void *buf)
425 {
426         struct inode *inode = file->f_dentry->d_inode;
427         int res = -ENOTDIR;
428         if (!file->f_op || !file->f_op->readdir)
429                 goto out;
430         down(&inode->i_sem);
431 //        down(&inode->i_zombie);
432         res = -ENOENT;
433         if (!IS_DEADDIR(inode)) {
434                 lock_kernel();
435                 res = __xattr_readdir(file, buf, filler);
436                 unlock_kernel();
437         }
438 //        up(&inode->i_zombie);
439         up(&inode->i_sem);
440       out:
441         return res;
442 }
443
444 /* Internal operations on file data */
445 static inline void reiserfs_put_page(struct page *page)
446 {
447         kunmap(page);
448         page_cache_release(page);
449 }
450
451 static struct page *reiserfs_get_page(struct inode *dir, unsigned long n)
452 {
453         struct address_space *mapping = dir->i_mapping;
454         struct page *page;
455         /* We can deadlock if we try to free dentries,
456            and an unlink/rmdir has just occured - GFP_NOFS avoids this */
457         mapping->flags = (mapping->flags & ~__GFP_BITS_MASK) | GFP_NOFS;
458         page = read_cache_page(mapping, n,
459                                (filler_t *) mapping->a_ops->readpage, NULL);
460         if (!IS_ERR(page)) {
461                 wait_on_page_locked(page);
462                 kmap(page);
463                 if (!PageUptodate(page))
464                         goto fail;
465
466                 if (PageError(page))
467                         goto fail;
468         }
469         return page;
470
471       fail:
472         reiserfs_put_page(page);
473         return ERR_PTR(-EIO);
474 }
475
476 static inline __u32 xattr_hash(const char *msg, int len)
477 {
478         return csum_partial(msg, len, 0);
479 }
480
481 /* Generic extended attribute operations that can be used by xa plugins */
482
483 /*
484  * inode->i_sem: down
485  */
486 int
487 reiserfs_xattr_set(struct inode *inode, const char *name, const void *buffer,
488                    size_t buffer_size, int flags)
489 {
490         int err = 0;
491         struct file *fp;
492         struct page *page;
493         char *data;
494         struct address_space *mapping;
495         size_t file_pos = 0;
496         size_t buffer_pos = 0;
497         struct inode *xinode;
498         struct iattr newattrs;
499         __u32 xahash = 0;
500
501         if (IS_RDONLY(inode))
502                 return -EROFS;
503
504         if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
505                 return -EPERM;
506
507         if (get_inode_sd_version(inode) == STAT_DATA_V1)
508                 return -EOPNOTSUPP;
509
510         /* Empty xattrs are ok, they're just empty files, no hash */
511         if (buffer && buffer_size)
512                 xahash = xattr_hash(buffer, buffer_size);
513
514       open_file:
515         fp = open_xa_file(inode, name, flags);
516         if (IS_ERR(fp)) {
517                 err = PTR_ERR(fp);
518                 goto out;
519         }
520
521         xinode = fp->f_dentry->d_inode;
522         REISERFS_I(inode)->i_flags |= i_has_xattr_dir;
523
524         /* we need to copy it off.. */
525         if (xinode->i_nlink > 1) {
526                 fput(fp);
527                 err = reiserfs_xattr_del(inode, name);
528                 if (err < 0)
529                         goto out;
530                 /* We just killed the old one, we're not replacing anymore */
531                 if (flags & XATTR_REPLACE)
532                         flags &= ~XATTR_REPLACE;
533                 goto open_file;
534         }
535
536         /* Resize it so we're ok to write there */
537         newattrs.ia_size = buffer_size;
538         newattrs.ia_valid = ATTR_SIZE | ATTR_CTIME;
539         down(&xinode->i_sem);
540         err = notify_change(fp->f_dentry, &newattrs);
541         if (err)
542                 goto out_filp;
543
544         mapping = xinode->i_mapping;
545         while (buffer_pos < buffer_size || buffer_pos == 0) {
546                 size_t chunk;
547                 size_t skip = 0;
548                 size_t page_offset = (file_pos & (PAGE_CACHE_SIZE - 1));
549                 if (buffer_size - buffer_pos > PAGE_CACHE_SIZE)
550                         chunk = PAGE_CACHE_SIZE;
551                 else
552                         chunk = buffer_size - buffer_pos;
553
554                 page = reiserfs_get_page(xinode, file_pos >> PAGE_CACHE_SHIFT);
555                 if (IS_ERR(page)) {
556                         err = PTR_ERR(page);
557                         goto out_filp;
558                 }
559
560                 lock_page(page);
561                 data = page_address(page);
562
563                 if (file_pos == 0) {
564                         struct reiserfs_xattr_header *rxh;
565                         skip = file_pos = sizeof(struct reiserfs_xattr_header);
566                         if (chunk + skip > PAGE_CACHE_SIZE)
567                                 chunk = PAGE_CACHE_SIZE - skip;
568                         rxh = (struct reiserfs_xattr_header *)data;
569                         rxh->h_magic = cpu_to_le32(REISERFS_XATTR_MAGIC);
570                         rxh->h_hash = cpu_to_le32(xahash);
571                 }
572
573                 err = mapping->a_ops->prepare_write(fp, page, page_offset,
574                                                     page_offset + chunk + skip);
575                 if (!err) {
576                         if (buffer)
577                                 memcpy(data + skip, buffer + buffer_pos, chunk);
578                         err =
579                             mapping->a_ops->commit_write(fp, page, page_offset,
580                                                          page_offset + chunk +
581                                                          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,
681                                                  "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)) !=
699             hash) {
700                 reiserfs_warning(inode->i_sb,
701                                  "Invalid hash for xattr (%s) associated "
702                                  "with %k", name, INODE_PKEY(inode));
703                 err = -EIO;
704         }
705
706       out_dput:
707         fput(fp);
708
709       out:
710         return err;
711 }
712
713 static int
714 __reiserfs_xattr_del(struct dentry *xadir, const char *name, int namelen)
715 {
716         struct dentry *dentry;
717         struct inode *dir = xadir->d_inode;
718         int err = 0;
719
720         dentry = lookup_one_len(name, xadir, namelen);
721         if (IS_ERR(dentry)) {
722                 err = PTR_ERR(dentry);
723                 goto out;
724         } else if (!dentry->d_inode) {
725                 err = -ENODATA;
726                 goto out_file;
727         }
728
729         /* Skip directories.. */
730         if (S_ISDIR(dentry->d_inode->i_mode))
731                 goto out_file;
732
733         if (!is_reiserfs_priv_object(dentry->d_inode)) {
734                 reiserfs_warning(dir->i_sb, "OID %08x [%.*s/%.*s] doesn't have "
735                                  "priv flag set [parent is %sset].",
736                                  le32_to_cpu(INODE_PKEY(dentry->d_inode)->
737                                              k_objectid), xadir->d_name.len,
738                                  xadir->d_name.name, namelen, name,
739                                  is_reiserfs_priv_object(xadir->
740                                                          d_inode) ? "" :
741                                  "not ");
742                 dput(dentry);
743                 return -EIO;
744         }
745
746         err = dir->i_op->unlink(dir, dentry);
747         if (!err)
748                 d_delete(dentry);
749
750       out_file:
751         dput(dentry);
752
753       out:
754         return err;
755 }
756
757 int reiserfs_xattr_del(struct inode *inode, const char *name)
758 {
759         struct dentry *dir;
760         int err;
761
762         if (IS_RDONLY(inode))
763                 return -EROFS;
764
765         dir = open_xa_dir(inode, FL_READONLY);
766         if (IS_ERR(dir)) {
767                 err = PTR_ERR(dir);
768                 goto out;
769         }
770
771         err = __reiserfs_xattr_del(dir, name, strlen(name));
772         dput(dir);
773
774         if (!err) {
775                 inode->i_ctime = CURRENT_TIME_SEC;
776                 mark_inode_dirty(inode);
777         }
778
779       out:
780         return err;
781 }
782
783 /* The following are side effects of other operations that aren't explicitly
784  * modifying extended attributes. This includes operations such as permissions
785  * or ownership changes, object deletions, etc. */
786
787 static int
788 reiserfs_delete_xattrs_filler(void *buf, const char *name, int namelen,
789                               loff_t offset, ino_t ino, unsigned int d_type)
790 {
791         struct dentry *xadir = (struct dentry *)buf;
792
793         return __reiserfs_xattr_del(xadir, name, namelen);
794
795 }
796
797 /* This is called w/ inode->i_sem downed */
798 int reiserfs_delete_xattrs(struct inode *inode)
799 {
800         struct file *fp;
801         struct dentry *dir, *root;
802         int err = 0;
803
804         /* Skip out, an xattr has no xattrs associated with it */
805         if (is_reiserfs_priv_object(inode) ||
806             get_inode_sd_version(inode) == STAT_DATA_V1 ||
807             !reiserfs_xattrs(inode->i_sb)) {
808                 return 0;
809         }
810         reiserfs_read_lock_xattrs(inode->i_sb);
811         dir = open_xa_dir(inode, FL_READONLY);
812         reiserfs_read_unlock_xattrs(inode->i_sb);
813         if (IS_ERR(dir)) {
814                 err = PTR_ERR(dir);
815                 goto out;
816         } else if (!dir->d_inode) {
817                 dput(dir);
818                 return 0;
819         }
820
821         fp = dentry_open(dir, NULL, O_RDWR);
822         if (IS_ERR(fp)) {
823                 err = PTR_ERR(fp);
824                 /* dentry_open dputs the dentry if it fails */
825                 goto out;
826         }
827
828         lock_kernel();
829         err = xattr_readdir(fp, reiserfs_delete_xattrs_filler, dir);
830         if (err) {
831                 unlock_kernel();
832                 goto out_dir;
833         }
834
835         /* Leftovers besides . and .. -- that's not good. */
836         if (dir->d_inode->i_nlink <= 2) {
837                 root = get_xa_root(inode->i_sb);
838                 reiserfs_write_lock_xattrs(inode->i_sb);
839                 err = vfs_rmdir(root->d_inode, dir);
840                 reiserfs_write_unlock_xattrs(inode->i_sb);
841                 dput(root);
842         } else {
843                 reiserfs_warning(inode->i_sb,
844                                  "Couldn't remove all entries in directory");
845         }
846         unlock_kernel();
847
848       out_dir:
849         fput(fp);
850
851       out:
852         if (!err)
853                 REISERFS_I(inode)->i_flags =
854                     REISERFS_I(inode)->i_flags & ~i_has_xattr_dir;
855         return err;
856 }
857
858 struct reiserfs_chown_buf {
859         struct inode *inode;
860         struct dentry *xadir;
861         struct iattr *attrs;
862 };
863
864 /* XXX: If there is a better way to do this, I'd love to hear about it */
865 static int
866 reiserfs_chown_xattrs_filler(void *buf, const char *name, int namelen,
867                              loff_t offset, ino_t ino, unsigned int d_type)
868 {
869         struct reiserfs_chown_buf *chown_buf = (struct reiserfs_chown_buf *)buf;
870         struct dentry *xafile, *xadir = chown_buf->xadir;
871         struct iattr *attrs = chown_buf->attrs;
872         int err = 0;
873
874         xafile = lookup_one_len(name, xadir, namelen);
875         if (IS_ERR(xafile))
876                 return PTR_ERR(xafile);
877         else if (!xafile->d_inode) {
878                 dput(xafile);
879                 return -ENODATA;
880         }
881
882         if (!S_ISDIR(xafile->d_inode->i_mode))
883                 err = notify_change(xafile, attrs);
884         dput(xafile);
885
886         return err;
887 }
888
889 int reiserfs_chown_xattrs(struct inode *inode, struct iattr *attrs)
890 {
891         struct file *fp;
892         struct dentry *dir;
893         int err = 0;
894         struct reiserfs_chown_buf buf;
895         unsigned int ia_valid = attrs->ia_valid;
896
897         /* Skip out, an xattr has no xattrs associated with it */
898         if (is_reiserfs_priv_object(inode) ||
899             get_inode_sd_version(inode) == STAT_DATA_V1 ||
900             !reiserfs_xattrs(inode->i_sb)) {
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 /* 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  * Inode operation setxattr()
973  *
974  * dentry->d_inode->i_sem down
975  */
976 int
977 reiserfs_setxattr(struct dentry *dentry, const char *name, const void *value,
978                   size_t size, int flags)
979 {
980         struct reiserfs_xattr_handler *xah = find_xattr_handler_prefix(name);
981         int err;
982         int lock;
983
984         if (!xah || !reiserfs_xattrs(dentry->d_sb) ||
985             get_inode_sd_version(dentry->d_inode) == STAT_DATA_V1)
986                 return -EOPNOTSUPP;
987
988         if (IS_RDONLY(dentry->d_inode))
989                 return -EROFS;
990
991         if (IS_IMMUTABLE(dentry->d_inode) || IS_APPEND(dentry->d_inode))
992                 return -EROFS;
993
994         reiserfs_write_lock_xattr_i(dentry->d_inode);
995         lock = !has_xattr_dir(dentry->d_inode);
996         if (lock)
997                 reiserfs_write_lock_xattrs(dentry->d_sb);
998         else
999                 reiserfs_read_lock_xattrs(dentry->d_sb);
1000         err = xah->set(dentry->d_inode, name, value, size, flags);
1001         if (lock)
1002                 reiserfs_write_unlock_xattrs(dentry->d_sb);
1003         else
1004                 reiserfs_read_unlock_xattrs(dentry->d_sb);
1005         reiserfs_write_unlock_xattr_i(dentry->d_inode);
1006         return err;
1007 }
1008
1009 /*
1010  * Inode operation removexattr()
1011  *
1012  * dentry->d_inode->i_sem down
1013  */
1014 int reiserfs_removexattr(struct dentry *dentry, const char *name)
1015 {
1016         int err;
1017         struct reiserfs_xattr_handler *xah = find_xattr_handler_prefix(name);
1018
1019         if (!xah || !reiserfs_xattrs(dentry->d_sb) ||
1020             get_inode_sd_version(dentry->d_inode) == STAT_DATA_V1)
1021                 return -EOPNOTSUPP;
1022
1023         if (IS_RDONLY(dentry->d_inode))
1024                 return -EROFS;
1025
1026         if (IS_IMMUTABLE(dentry->d_inode) || IS_APPEND(dentry->d_inode))
1027                 return -EPERM;
1028
1029         reiserfs_write_lock_xattr_i(dentry->d_inode);
1030         reiserfs_read_lock_xattrs(dentry->d_sb);
1031
1032         /* Deletion pre-operation */
1033         if (xah->del) {
1034                 err = xah->del(dentry->d_inode, name);
1035                 if (err)
1036                         goto out;
1037         }
1038
1039         err = reiserfs_xattr_del(dentry->d_inode, name);
1040
1041         dentry->d_inode->i_ctime = CURRENT_TIME_SEC;
1042         mark_inode_dirty(dentry->d_inode);
1043
1044       out:
1045         reiserfs_read_unlock_xattrs(dentry->d_sb);
1046         reiserfs_write_unlock_xattr_i(dentry->d_inode);
1047         return err;
1048 }
1049
1050 /* This is what filldir will use:
1051  * r_pos will always contain the amount of space required for the entire
1052  * list. If r_pos becomes larger than r_size, we need more space and we
1053  * return an error indicating this. If r_pos is less than r_size, then we've
1054  * filled the buffer successfully and we return success */
1055 struct reiserfs_listxattr_buf {
1056         int r_pos;
1057         int r_size;
1058         char *r_buf;
1059         struct inode *r_inode;
1060 };
1061
1062 static int
1063 reiserfs_listxattr_filler(void *buf, const char *name, int namelen,
1064                           loff_t offset, ino_t ino, unsigned int d_type)
1065 {
1066         struct reiserfs_listxattr_buf *b = (struct reiserfs_listxattr_buf *)buf;
1067         int len = 0;
1068         if (name[0] != '.'
1069             || (namelen != 1 && (name[1] != '.' || namelen != 2))) {
1070                 struct reiserfs_xattr_handler *xah =
1071                     find_xattr_handler_prefix(name);
1072                 if (!xah)
1073                         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 /*
1093  * Inode operation listxattr()
1094  *
1095  * Preliminary locking: we down dentry->d_inode->i_sem
1096  */
1097 ssize_t 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 *find_xattr_handler_prefix(const char
1158                                                                 *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 __unregister_handlers(void)
1176 {
1177         struct reiserfs_xattr_handler *xah;
1178         struct list_head *p, *tmp;
1179
1180         list_for_each_safe(p, tmp, &xattr_handlers) {
1181                 xah = list_entry(p, struct reiserfs_xattr_handler, handlers);
1182                 if (xah->exit)
1183                         xah->exit();
1184
1185                 list_del_init(p);
1186         }
1187         INIT_LIST_HEAD(&xattr_handlers);
1188 }
1189
1190 int __init reiserfs_xattr_register_handlers(void)
1191 {
1192         int err = 0;
1193         struct reiserfs_xattr_handler *xah;
1194         struct list_head *p;
1195
1196         write_lock(&handler_lock);
1197
1198         /* If we're already initialized, nothing to do */
1199         if (!list_empty(&xattr_handlers)) {
1200                 write_unlock(&handler_lock);
1201                 return 0;
1202         }
1203
1204         /* Add the handlers */
1205         list_add_tail(&user_handler.handlers, &xattr_handlers);
1206         list_add_tail(&trusted_handler.handlers, &xattr_handlers);
1207 #ifdef CONFIG_REISERFS_FS_SECURITY
1208         list_add_tail(&security_handler.handlers, &xattr_handlers);
1209 #endif
1210 #ifdef CONFIG_REISERFS_FS_POSIX_ACL
1211         list_add_tail(&posix_acl_access_handler.handlers, &xattr_handlers);
1212         list_add_tail(&posix_acl_default_handler.handlers, &xattr_handlers);
1213 #endif
1214
1215         /* Run initializers, if available */
1216         list_for_each(p, &xattr_handlers) {
1217                 xah = list_entry(p, struct reiserfs_xattr_handler, handlers);
1218                 if (xah->init) {
1219                         err = xah->init();
1220                         if (err) {
1221                                 list_del_init(p);
1222                                 break;
1223                         }
1224                 }
1225         }
1226
1227         /* Clean up other handlers, if any failed */
1228         if (err)
1229                 __unregister_handlers();
1230
1231         write_unlock(&handler_lock);
1232         return err;
1233 }
1234
1235 void reiserfs_xattr_unregister_handlers(void)
1236 {
1237         write_lock(&handler_lock);
1238         __unregister_handlers();
1239         write_unlock(&handler_lock);
1240 }
1241
1242 /* This will catch lookups from the fs root to .reiserfs_priv */
1243 static int
1244 xattr_lookup_poison(struct dentry *dentry, struct qstr *q1, struct qstr *name)
1245 {
1246         struct dentry *priv_root = REISERFS_SB(dentry->d_sb)->priv_root;
1247         if (name->len == priv_root->d_name.len &&
1248             name->hash == priv_root->d_name.hash &&
1249             !memcmp(name->name, priv_root->d_name.name, name->len)) {
1250                 return -ENOENT;
1251         } else if (q1->len == name->len &&
1252                    !memcmp(q1->name, name->name, name->len))
1253                 return 0;
1254         return 1;
1255 }
1256
1257 static struct dentry_operations xattr_lookup_poison_ops = {
1258         .d_compare = xattr_lookup_poison,
1259 };
1260
1261 /* We need to take a copy of the mount flags since things like
1262  * MS_RDONLY don't get set until *after* we're called.
1263  * mount_flags != mount_options */
1264 int reiserfs_xattr_init(struct super_block *s, int mount_flags)
1265 {
1266         int err = 0;
1267
1268         /* We need generation numbers to ensure that the oid mapping is correct
1269          * v3.5 filesystems don't have them. */
1270         if (!old_format_only(s)) {
1271                 set_bit(REISERFS_XATTRS, &(REISERFS_SB(s)->s_mount_opt));
1272         } else if (reiserfs_xattrs_optional(s)) {
1273                 /* Old format filesystem, but optional xattrs have been enabled
1274                  * at mount time. Error out. */
1275                 reiserfs_warning(s, "xattrs/ACLs not supported on pre v3.6 "
1276                                  "format filesystem. Failing mount.");
1277                 err = -EOPNOTSUPP;
1278                 goto error;
1279         } else {
1280                 /* Old format filesystem, but no optional xattrs have been enabled. This
1281                  * means we silently disable xattrs on the filesystem. */
1282                 clear_bit(REISERFS_XATTRS, &(REISERFS_SB(s)->s_mount_opt));
1283         }
1284
1285         /* If we don't have the privroot located yet - go find it */
1286         if (reiserfs_xattrs(s) && !REISERFS_SB(s)->priv_root) {
1287                 struct dentry *dentry;
1288                 dentry = lookup_one_len(PRIVROOT_NAME, s->s_root,
1289                                         strlen(PRIVROOT_NAME));
1290                 if (!IS_ERR(dentry)) {
1291                         if (!(mount_flags & MS_RDONLY) && !dentry->d_inode) {
1292                                 struct inode *inode = dentry->d_parent->d_inode;
1293                                 down(&inode->i_sem);
1294                                 err = inode->i_op->mkdir(inode, dentry, 0700);
1295                                 up(&inode->i_sem);
1296                                 if (err) {
1297                                         dput(dentry);
1298                                         dentry = NULL;
1299                                 }
1300
1301                                 if (dentry && dentry->d_inode)
1302                                         reiserfs_warning(s,
1303                                                          "Created %s on %s - reserved for "
1304                                                          "xattr storage.",
1305                                                          PRIVROOT_NAME,
1306                                                          reiserfs_bdevname
1307                                                          (inode->i_sb));
1308                         } else if (!dentry->d_inode) {
1309                                 dput(dentry);
1310                                 dentry = NULL;
1311                         }
1312                 } else
1313                         err = PTR_ERR(dentry);
1314
1315                 if (!err && dentry) {
1316                         s->s_root->d_op = &xattr_lookup_poison_ops;
1317                         reiserfs_mark_inode_private(dentry->d_inode);
1318                         REISERFS_SB(s)->priv_root = dentry;
1319                 } else if (!(mount_flags & MS_RDONLY)) {        /* xattrs are unavailable */
1320                         /* If we're read-only it just means that the dir hasn't been
1321                          * created. Not an error -- just no xattrs on the fs. We'll
1322                          * check again if we go read-write */
1323                         reiserfs_warning(s, "xattrs/ACLs enabled and couldn't "
1324                                          "find/create .reiserfs_priv. Failing mount.");
1325                         err = -EOPNOTSUPP;
1326                 }
1327         }
1328
1329       error:
1330         /* This is only nonzero if there was an error initializing the xattr
1331          * directory or if there is a condition where we don't support them. */
1332         if (err) {
1333                 clear_bit(REISERFS_XATTRS, &(REISERFS_SB(s)->s_mount_opt));
1334                 clear_bit(REISERFS_XATTRS_USER, &(REISERFS_SB(s)->s_mount_opt));
1335                 clear_bit(REISERFS_POSIXACL, &(REISERFS_SB(s)->s_mount_opt));
1336         }
1337
1338         /* The super_block MS_POSIXACL must mirror the (no)acl mount option. */
1339         s->s_flags = s->s_flags & ~MS_POSIXACL;
1340         if (reiserfs_posixacl(s))
1341                 s->s_flags |= MS_POSIXACL;
1342
1343         return err;
1344 }
1345
1346 static int
1347 __reiserfs_permission(struct inode *inode, int mask, struct nameidata *nd,
1348                       int need_lock)
1349 {
1350         umode_t mode = inode->i_mode;
1351
1352         if (mask & MAY_WRITE) {
1353                 /*
1354                  * Nobody gets write access to a read-only fs.
1355                  */
1356                 if (IS_RDONLY(inode) &&
1357                     (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
1358                         return -EROFS;
1359
1360                 /*
1361                  * Nobody gets write access to an immutable file.
1362                  */
1363                 if (IS_IMMUTABLE(inode))
1364                         return -EACCES;
1365         }
1366
1367         /* We don't do permission checks on the internal objects.
1368          * Permissions are determined by the "owning" object. */
1369         if (is_reiserfs_priv_object(inode))
1370                 return 0;
1371
1372         if (current->fsuid == inode->i_uid) {
1373                 mode >>= 6;
1374 #ifdef CONFIG_REISERFS_FS_POSIX_ACL
1375         } else if (reiserfs_posixacl(inode->i_sb) &&
1376                    get_inode_sd_version(inode) != STAT_DATA_V1) {
1377                 struct posix_acl *acl;
1378
1379                 /* ACL can't contain additional permissions if
1380                    the ACL_MASK entry is 0 */
1381                 if (!(mode & S_IRWXG))
1382                         goto check_groups;
1383
1384                 if (need_lock) {
1385                         reiserfs_read_lock_xattr_i(inode);
1386                         reiserfs_read_lock_xattrs(inode->i_sb);
1387                 }
1388                 acl = reiserfs_get_acl(inode, ACL_TYPE_ACCESS);
1389                 if (need_lock) {
1390                         reiserfs_read_unlock_xattrs(inode->i_sb);
1391                         reiserfs_read_unlock_xattr_i(inode);
1392                 }
1393                 if (IS_ERR(acl)) {
1394                         if (PTR_ERR(acl) == -ENODATA)
1395                                 goto check_groups;
1396                         return PTR_ERR(acl);
1397                 }
1398
1399                 if (acl) {
1400                         int err = posix_acl_permission(inode, acl, mask);
1401                         posix_acl_release(acl);
1402                         if (err == -EACCES) {
1403                                 goto check_capabilities;
1404                         }
1405                         return err;
1406                 } else {
1407                         goto check_groups;
1408                 }
1409 #endif
1410         } else {
1411               check_groups:
1412                 if (in_group_p(inode->i_gid))
1413                         mode >>= 3;
1414         }
1415
1416         /*
1417          * If the DACs are ok we don't need any capability check.
1418          */
1419         if (((mode & mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == mask))
1420                 return 0;
1421
1422       check_capabilities:
1423         /*
1424          * Read/write DACs are always overridable.
1425          * Executable DACs are overridable if at least one exec bit is set.
1426          */
1427         if (!(mask & MAY_EXEC) ||
1428             (inode->i_mode & S_IXUGO) || S_ISDIR(inode->i_mode))
1429                 if (capable(CAP_DAC_OVERRIDE))
1430                         return 0;
1431
1432         /*
1433          * Searching includes executable on directories, else just read.
1434          */
1435         if (mask == MAY_READ || (S_ISDIR(inode->i_mode) && !(mask & MAY_WRITE)))
1436                 if (capable(CAP_DAC_READ_SEARCH))
1437                         return 0;
1438
1439         return -EACCES;
1440 }
1441
1442 int reiserfs_permission(struct inode *inode, int mask, struct nameidata *nd)
1443 {
1444         return __reiserfs_permission(inode, mask, nd, 1);
1445 }
1446
1447 int
1448 reiserfs_permission_locked(struct inode *inode, int mask, struct nameidata *nd)
1449 {
1450         return __reiserfs_permission(inode, mask, nd, 0);
1451 }