Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * linux/fs/reiserfs/xattr.c
0004  *
0005  * Copyright (c) 2002 by Jeff Mahoney, <jeffm@suse.com>
0006  *
0007  */
0008 
0009 /*
0010  * In order to implement EA/ACLs in a clean, backwards compatible manner,
0011  * they are implemented as files in a "private" directory.
0012  * Each EA is in it's own file, with the directory layout like so (/ is assumed
0013  * to be relative to fs root). Inside the /.reiserfs_priv/xattrs directory,
0014  * directories named using the capital-hex form of the objectid and
0015  * generation number are used. Inside each directory are individual files
0016  * named with the name of the extended attribute.
0017  *
0018  * So, for objectid 12648430, we could have:
0019  * /.reiserfs_priv/xattrs/C0FFEE.0/system.posix_acl_access
0020  * /.reiserfs_priv/xattrs/C0FFEE.0/system.posix_acl_default
0021  * /.reiserfs_priv/xattrs/C0FFEE.0/user.Content-Type
0022  * .. or similar.
0023  *
0024  * The file contents are the text of the EA. The size is known based on the
0025  * stat data describing the file.
0026  *
0027  * In the case of system.posix_acl_access and system.posix_acl_default, since
0028  * these are special cases for filesystem ACLs, they are interpreted by the
0029  * kernel, in addition, they are negatively and positively cached and attached
0030  * to the inode so that unnecessary lookups are avoided.
0031  *
0032  * Locking works like so:
0033  * Directory components (xattr root, xattr dir) are protectd by their i_mutex.
0034  * The xattrs themselves are protected by the xattr_sem.
0035  */
0036 
0037 #include "reiserfs.h"
0038 #include <linux/capability.h>
0039 #include <linux/dcache.h>
0040 #include <linux/namei.h>
0041 #include <linux/errno.h>
0042 #include <linux/gfp.h>
0043 #include <linux/fs.h>
0044 #include <linux/file.h>
0045 #include <linux/pagemap.h>
0046 #include <linux/xattr.h>
0047 #include "xattr.h"
0048 #include "acl.h"
0049 #include <linux/uaccess.h>
0050 #include <net/checksum.h>
0051 #include <linux/stat.h>
0052 #include <linux/quotaops.h>
0053 #include <linux/security.h>
0054 #include <linux/posix_acl_xattr.h>
0055 
0056 #define PRIVROOT_NAME ".reiserfs_priv"
0057 #define XAROOT_NAME   "xattrs"
0058 
0059 
0060 /*
0061  * Helpers for inode ops. We do this so that we don't have all the VFS
0062  * overhead and also for proper i_mutex annotation.
0063  * dir->i_mutex must be held for all of them.
0064  */
0065 #ifdef CONFIG_REISERFS_FS_XATTR
0066 static int xattr_create(struct inode *dir, struct dentry *dentry, int mode)
0067 {
0068     BUG_ON(!inode_is_locked(dir));
0069     return dir->i_op->create(&init_user_ns, dir, dentry, mode, true);
0070 }
0071 #endif
0072 
0073 static int xattr_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
0074 {
0075     BUG_ON(!inode_is_locked(dir));
0076     return dir->i_op->mkdir(&init_user_ns, dir, dentry, mode);
0077 }
0078 
0079 /*
0080  * We use I_MUTEX_CHILD here to silence lockdep. It's safe because xattr
0081  * mutation ops aren't called during rename or splace, which are the
0082  * only other users of I_MUTEX_CHILD. It violates the ordering, but that's
0083  * better than allocating another subclass just for this code.
0084  */
0085 static int xattr_unlink(struct inode *dir, struct dentry *dentry)
0086 {
0087     int error;
0088 
0089     BUG_ON(!inode_is_locked(dir));
0090 
0091     inode_lock_nested(d_inode(dentry), I_MUTEX_CHILD);
0092     error = dir->i_op->unlink(dir, dentry);
0093     inode_unlock(d_inode(dentry));
0094 
0095     if (!error)
0096         d_delete(dentry);
0097     return error;
0098 }
0099 
0100 static int xattr_rmdir(struct inode *dir, struct dentry *dentry)
0101 {
0102     int error;
0103 
0104     BUG_ON(!inode_is_locked(dir));
0105 
0106     inode_lock_nested(d_inode(dentry), I_MUTEX_CHILD);
0107     error = dir->i_op->rmdir(dir, dentry);
0108     if (!error)
0109         d_inode(dentry)->i_flags |= S_DEAD;
0110     inode_unlock(d_inode(dentry));
0111     if (!error)
0112         d_delete(dentry);
0113 
0114     return error;
0115 }
0116 
0117 #define xattr_may_create(flags) (!flags || flags & XATTR_CREATE)
0118 
0119 static struct dentry *open_xa_root(struct super_block *sb, int flags)
0120 {
0121     struct dentry *privroot = REISERFS_SB(sb)->priv_root;
0122     struct dentry *xaroot;
0123 
0124     if (d_really_is_negative(privroot))
0125         return ERR_PTR(-EOPNOTSUPP);
0126 
0127     inode_lock_nested(d_inode(privroot), I_MUTEX_XATTR);
0128 
0129     xaroot = dget(REISERFS_SB(sb)->xattr_root);
0130     if (!xaroot)
0131         xaroot = ERR_PTR(-EOPNOTSUPP);
0132     else if (d_really_is_negative(xaroot)) {
0133         int err = -ENODATA;
0134 
0135         if (xattr_may_create(flags))
0136             err = xattr_mkdir(d_inode(privroot), xaroot, 0700);
0137         if (err) {
0138             dput(xaroot);
0139             xaroot = ERR_PTR(err);
0140         }
0141     }
0142 
0143     inode_unlock(d_inode(privroot));
0144     return xaroot;
0145 }
0146 
0147 static struct dentry *open_xa_dir(const struct inode *inode, int flags)
0148 {
0149     struct dentry *xaroot, *xadir;
0150     char namebuf[17];
0151 
0152     xaroot = open_xa_root(inode->i_sb, flags);
0153     if (IS_ERR(xaroot))
0154         return xaroot;
0155 
0156     snprintf(namebuf, sizeof(namebuf), "%X.%X",
0157          le32_to_cpu(INODE_PKEY(inode)->k_objectid),
0158          inode->i_generation);
0159 
0160     inode_lock_nested(d_inode(xaroot), I_MUTEX_XATTR);
0161 
0162     xadir = lookup_one_len(namebuf, xaroot, strlen(namebuf));
0163     if (!IS_ERR(xadir) && d_really_is_negative(xadir)) {
0164         int err = -ENODATA;
0165 
0166         if (xattr_may_create(flags))
0167             err = xattr_mkdir(d_inode(xaroot), xadir, 0700);
0168         if (err) {
0169             dput(xadir);
0170             xadir = ERR_PTR(err);
0171         }
0172     }
0173 
0174     inode_unlock(d_inode(xaroot));
0175     dput(xaroot);
0176     return xadir;
0177 }
0178 
0179 /*
0180  * The following are side effects of other operations that aren't explicitly
0181  * modifying extended attributes. This includes operations such as permissions
0182  * or ownership changes, object deletions, etc.
0183  */
0184 struct reiserfs_dentry_buf {
0185     struct dir_context ctx;
0186     struct dentry *xadir;
0187     int count;
0188     int err;
0189     struct dentry *dentries[8];
0190 };
0191 
0192 static int
0193 fill_with_dentries(struct dir_context *ctx, const char *name, int namelen,
0194            loff_t offset, u64 ino, unsigned int d_type)
0195 {
0196     struct reiserfs_dentry_buf *dbuf =
0197         container_of(ctx, struct reiserfs_dentry_buf, ctx);
0198     struct dentry *dentry;
0199 
0200     WARN_ON_ONCE(!inode_is_locked(d_inode(dbuf->xadir)));
0201 
0202     if (dbuf->count == ARRAY_SIZE(dbuf->dentries))
0203         return -ENOSPC;
0204 
0205     if (name[0] == '.' && (namelen < 2 ||
0206                    (namelen == 2 && name[1] == '.')))
0207         return 0;
0208 
0209     dentry = lookup_one_len(name, dbuf->xadir, namelen);
0210     if (IS_ERR(dentry)) {
0211         dbuf->err = PTR_ERR(dentry);
0212         return PTR_ERR(dentry);
0213     } else if (d_really_is_negative(dentry)) {
0214         /* A directory entry exists, but no file? */
0215         reiserfs_error(dentry->d_sb, "xattr-20003",
0216                    "Corrupted directory: xattr %pd listed but "
0217                    "not found for file %pd.\n",
0218                    dentry, dbuf->xadir);
0219         dput(dentry);
0220         dbuf->err = -EIO;
0221         return -EIO;
0222     }
0223 
0224     dbuf->dentries[dbuf->count++] = dentry;
0225     return 0;
0226 }
0227 
0228 static void
0229 cleanup_dentry_buf(struct reiserfs_dentry_buf *buf)
0230 {
0231     int i;
0232 
0233     for (i = 0; i < buf->count; i++)
0234         if (buf->dentries[i])
0235             dput(buf->dentries[i]);
0236 }
0237 
0238 static int reiserfs_for_each_xattr(struct inode *inode,
0239                    int (*action)(struct dentry *, void *),
0240                    void *data)
0241 {
0242     struct dentry *dir;
0243     int i, err = 0;
0244     struct reiserfs_dentry_buf buf = {
0245         .ctx.actor = fill_with_dentries,
0246     };
0247 
0248     /* Skip out, an xattr has no xattrs associated with it */
0249     if (IS_PRIVATE(inode) || get_inode_sd_version(inode) == STAT_DATA_V1)
0250         return 0;
0251 
0252     dir = open_xa_dir(inode, XATTR_REPLACE);
0253     if (IS_ERR(dir)) {
0254         err = PTR_ERR(dir);
0255         goto out;
0256     } else if (d_really_is_negative(dir)) {
0257         err = 0;
0258         goto out_dir;
0259     }
0260 
0261     inode_lock_nested(d_inode(dir), I_MUTEX_XATTR);
0262 
0263     buf.xadir = dir;
0264     while (1) {
0265         err = reiserfs_readdir_inode(d_inode(dir), &buf.ctx);
0266         if (err)
0267             break;
0268         if (buf.err) {
0269             err = buf.err;
0270             break;
0271         }
0272         if (!buf.count)
0273             break;
0274         for (i = 0; !err && i < buf.count && buf.dentries[i]; i++) {
0275             struct dentry *dentry = buf.dentries[i];
0276 
0277             if (!d_is_dir(dentry))
0278                 err = action(dentry, data);
0279 
0280             dput(dentry);
0281             buf.dentries[i] = NULL;
0282         }
0283         if (err)
0284             break;
0285         buf.count = 0;
0286     }
0287     inode_unlock(d_inode(dir));
0288 
0289     cleanup_dentry_buf(&buf);
0290 
0291     if (!err) {
0292         /*
0293          * We start a transaction here to avoid a ABBA situation
0294          * between the xattr root's i_mutex and the journal lock.
0295          * This doesn't incur much additional overhead since the
0296          * new transaction will just nest inside the
0297          * outer transaction.
0298          */
0299         int blocks = JOURNAL_PER_BALANCE_CNT * 2 + 2 +
0300                  4 * REISERFS_QUOTA_TRANS_BLOCKS(inode->i_sb);
0301         struct reiserfs_transaction_handle th;
0302 
0303         reiserfs_write_lock(inode->i_sb);
0304         err = journal_begin(&th, inode->i_sb, blocks);
0305         reiserfs_write_unlock(inode->i_sb);
0306         if (!err) {
0307             int jerror;
0308 
0309             inode_lock_nested(d_inode(dir->d_parent),
0310                       I_MUTEX_XATTR);
0311             err = action(dir, data);
0312             reiserfs_write_lock(inode->i_sb);
0313             jerror = journal_end(&th);
0314             reiserfs_write_unlock(inode->i_sb);
0315             inode_unlock(d_inode(dir->d_parent));
0316             err = jerror ?: err;
0317         }
0318     }
0319 out_dir:
0320     dput(dir);
0321 out:
0322     /*
0323      * -ENODATA: this object doesn't have any xattrs
0324      * -EOPNOTSUPP: this file system doesn't have xattrs enabled on disk.
0325      * Neither are errors
0326      */
0327     if (err == -ENODATA || err == -EOPNOTSUPP)
0328         err = 0;
0329     return err;
0330 }
0331 
0332 static int delete_one_xattr(struct dentry *dentry, void *data)
0333 {
0334     struct inode *dir = d_inode(dentry->d_parent);
0335 
0336     /* This is the xattr dir, handle specially. */
0337     if (d_is_dir(dentry))
0338         return xattr_rmdir(dir, dentry);
0339 
0340     return xattr_unlink(dir, dentry);
0341 }
0342 
0343 static int chown_one_xattr(struct dentry *dentry, void *data)
0344 {
0345     struct iattr *attrs = data;
0346     int ia_valid = attrs->ia_valid;
0347     int err;
0348 
0349     /*
0350      * We only want the ownership bits. Otherwise, we'll do
0351      * things like change a directory to a regular file if
0352      * ATTR_MODE is set.
0353      */
0354     attrs->ia_valid &= (ATTR_UID|ATTR_GID);
0355     err = reiserfs_setattr(&init_user_ns, dentry, attrs);
0356     attrs->ia_valid = ia_valid;
0357 
0358     return err;
0359 }
0360 
0361 /* No i_mutex, but the inode is unconnected. */
0362 int reiserfs_delete_xattrs(struct inode *inode)
0363 {
0364     int err = reiserfs_for_each_xattr(inode, delete_one_xattr, NULL);
0365 
0366     if (err)
0367         reiserfs_warning(inode->i_sb, "jdm-20004",
0368                  "Couldn't delete all xattrs (%d)\n", err);
0369     return err;
0370 }
0371 
0372 /* inode->i_mutex: down */
0373 int reiserfs_chown_xattrs(struct inode *inode, struct iattr *attrs)
0374 {
0375     int err = reiserfs_for_each_xattr(inode, chown_one_xattr, attrs);
0376 
0377     if (err)
0378         reiserfs_warning(inode->i_sb, "jdm-20007",
0379                  "Couldn't chown all xattrs (%d)\n", err);
0380     return err;
0381 }
0382 
0383 #ifdef CONFIG_REISERFS_FS_XATTR
0384 /*
0385  * Returns a dentry corresponding to a specific extended attribute file
0386  * for the inode. If flags allow, the file is created. Otherwise, a
0387  * valid or negative dentry, or an error is returned.
0388  */
0389 static struct dentry *xattr_lookup(struct inode *inode, const char *name,
0390                     int flags)
0391 {
0392     struct dentry *xadir, *xafile;
0393     int err = 0;
0394 
0395     xadir = open_xa_dir(inode, flags);
0396     if (IS_ERR(xadir))
0397         return ERR_CAST(xadir);
0398 
0399     inode_lock_nested(d_inode(xadir), I_MUTEX_XATTR);
0400     xafile = lookup_one_len(name, xadir, strlen(name));
0401     if (IS_ERR(xafile)) {
0402         err = PTR_ERR(xafile);
0403         goto out;
0404     }
0405 
0406     if (d_really_is_positive(xafile) && (flags & XATTR_CREATE))
0407         err = -EEXIST;
0408 
0409     if (d_really_is_negative(xafile)) {
0410         err = -ENODATA;
0411         if (xattr_may_create(flags))
0412             err = xattr_create(d_inode(xadir), xafile,
0413                           0700|S_IFREG);
0414     }
0415 
0416     if (err)
0417         dput(xafile);
0418 out:
0419     inode_unlock(d_inode(xadir));
0420     dput(xadir);
0421     if (err)
0422         return ERR_PTR(err);
0423     return xafile;
0424 }
0425 
0426 /* Internal operations on file data */
0427 static inline void reiserfs_put_page(struct page *page)
0428 {
0429     kunmap(page);
0430     put_page(page);
0431 }
0432 
0433 static struct page *reiserfs_get_page(struct inode *dir, size_t n)
0434 {
0435     struct address_space *mapping = dir->i_mapping;
0436     struct page *page;
0437     /*
0438      * We can deadlock if we try to free dentries,
0439      * and an unlink/rmdir has just occurred - GFP_NOFS avoids this
0440      */
0441     mapping_set_gfp_mask(mapping, GFP_NOFS);
0442     page = read_mapping_page(mapping, n >> PAGE_SHIFT, NULL);
0443     if (!IS_ERR(page))
0444         kmap(page);
0445     return page;
0446 }
0447 
0448 static inline __u32 xattr_hash(const char *msg, int len)
0449 {
0450     /*
0451      * csum_partial() gives different results for little-endian and
0452      * big endian hosts. Images created on little-endian hosts and
0453      * mounted on big-endian hosts(and vice versa) will see csum mismatches
0454      * when trying to fetch xattrs. Treating the hash as __wsum_t would
0455      * lower the frequency of mismatch.  This is an endianness bug in
0456      * reiserfs.  The return statement would result in a sparse warning. Do
0457      * not fix the sparse warning so as to not hide a reminder of the bug.
0458      */
0459     return csum_partial(msg, len, 0);
0460 }
0461 
0462 int reiserfs_commit_write(struct file *f, struct page *page,
0463               unsigned from, unsigned to);
0464 
0465 static void update_ctime(struct inode *inode)
0466 {
0467     struct timespec64 now = current_time(inode);
0468 
0469     if (inode_unhashed(inode) || !inode->i_nlink ||
0470         timespec64_equal(&inode->i_ctime, &now))
0471         return;
0472 
0473     inode->i_ctime = current_time(inode);
0474     mark_inode_dirty(inode);
0475 }
0476 
0477 static int lookup_and_delete_xattr(struct inode *inode, const char *name)
0478 {
0479     int err = 0;
0480     struct dentry *dentry, *xadir;
0481 
0482     xadir = open_xa_dir(inode, XATTR_REPLACE);
0483     if (IS_ERR(xadir))
0484         return PTR_ERR(xadir);
0485 
0486     inode_lock_nested(d_inode(xadir), I_MUTEX_XATTR);
0487     dentry = lookup_one_len(name, xadir, strlen(name));
0488     if (IS_ERR(dentry)) {
0489         err = PTR_ERR(dentry);
0490         goto out_dput;
0491     }
0492 
0493     if (d_really_is_positive(dentry)) {
0494         err = xattr_unlink(d_inode(xadir), dentry);
0495         update_ctime(inode);
0496     }
0497 
0498     dput(dentry);
0499 out_dput:
0500     inode_unlock(d_inode(xadir));
0501     dput(xadir);
0502     return err;
0503 }
0504 
0505 
0506 /* Generic extended attribute operations that can be used by xa plugins */
0507 
0508 /*
0509  * inode->i_mutex: down
0510  */
0511 int
0512 reiserfs_xattr_set_handle(struct reiserfs_transaction_handle *th,
0513               struct inode *inode, const char *name,
0514               const void *buffer, size_t buffer_size, int flags)
0515 {
0516     int err = 0;
0517     struct dentry *dentry;
0518     struct page *page;
0519     char *data;
0520     size_t file_pos = 0;
0521     size_t buffer_pos = 0;
0522     size_t new_size;
0523     __u32 xahash = 0;
0524 
0525     if (get_inode_sd_version(inode) == STAT_DATA_V1)
0526         return -EOPNOTSUPP;
0527 
0528     if (!buffer) {
0529         err = lookup_and_delete_xattr(inode, name);
0530         return err;
0531     }
0532 
0533     dentry = xattr_lookup(inode, name, flags);
0534     if (IS_ERR(dentry))
0535         return PTR_ERR(dentry);
0536 
0537     down_write(&REISERFS_I(inode)->i_xattr_sem);
0538 
0539     xahash = xattr_hash(buffer, buffer_size);
0540     while (buffer_pos < buffer_size || buffer_pos == 0) {
0541         size_t chunk;
0542         size_t skip = 0;
0543         size_t page_offset = (file_pos & (PAGE_SIZE - 1));
0544 
0545         if (buffer_size - buffer_pos > PAGE_SIZE)
0546             chunk = PAGE_SIZE;
0547         else
0548             chunk = buffer_size - buffer_pos;
0549 
0550         page = reiserfs_get_page(d_inode(dentry), file_pos);
0551         if (IS_ERR(page)) {
0552             err = PTR_ERR(page);
0553             goto out_unlock;
0554         }
0555 
0556         lock_page(page);
0557         data = page_address(page);
0558 
0559         if (file_pos == 0) {
0560             struct reiserfs_xattr_header *rxh;
0561 
0562             skip = file_pos = sizeof(struct reiserfs_xattr_header);
0563             if (chunk + skip > PAGE_SIZE)
0564                 chunk = PAGE_SIZE - skip;
0565             rxh = (struct reiserfs_xattr_header *)data;
0566             rxh->h_magic = cpu_to_le32(REISERFS_XATTR_MAGIC);
0567             rxh->h_hash = cpu_to_le32(xahash);
0568         }
0569 
0570         reiserfs_write_lock(inode->i_sb);
0571         err = __reiserfs_write_begin(page, page_offset, chunk + skip);
0572         if (!err) {
0573             if (buffer)
0574                 memcpy(data + skip, buffer + buffer_pos, chunk);
0575             err = reiserfs_commit_write(NULL, page, page_offset,
0576                             page_offset + chunk +
0577                             skip);
0578         }
0579         reiserfs_write_unlock(inode->i_sb);
0580         unlock_page(page);
0581         reiserfs_put_page(page);
0582         buffer_pos += chunk;
0583         file_pos += chunk;
0584         skip = 0;
0585         if (err || buffer_size == 0 || !buffer)
0586             break;
0587     }
0588 
0589     new_size = buffer_size + sizeof(struct reiserfs_xattr_header);
0590     if (!err && new_size < i_size_read(d_inode(dentry))) {
0591         struct iattr newattrs = {
0592             .ia_ctime = current_time(inode),
0593             .ia_size = new_size,
0594             .ia_valid = ATTR_SIZE | ATTR_CTIME,
0595         };
0596 
0597         inode_lock_nested(d_inode(dentry), I_MUTEX_XATTR);
0598         inode_dio_wait(d_inode(dentry));
0599 
0600         err = reiserfs_setattr(&init_user_ns, dentry, &newattrs);
0601         inode_unlock(d_inode(dentry));
0602     } else
0603         update_ctime(inode);
0604 out_unlock:
0605     up_write(&REISERFS_I(inode)->i_xattr_sem);
0606     dput(dentry);
0607     return err;
0608 }
0609 
0610 /* We need to start a transaction to maintain lock ordering */
0611 int reiserfs_xattr_set(struct inode *inode, const char *name,
0612                const void *buffer, size_t buffer_size, int flags)
0613 {
0614 
0615     struct reiserfs_transaction_handle th;
0616     int error, error2;
0617     size_t jbegin_count = reiserfs_xattr_nblocks(inode, buffer_size);
0618 
0619     /* Check before we start a transaction and then do nothing. */
0620     if (!d_really_is_positive(REISERFS_SB(inode->i_sb)->priv_root))
0621         return -EOPNOTSUPP;
0622 
0623     if (!(flags & XATTR_REPLACE))
0624         jbegin_count += reiserfs_xattr_jcreate_nblocks(inode);
0625 
0626     reiserfs_write_lock(inode->i_sb);
0627     error = journal_begin(&th, inode->i_sb, jbegin_count);
0628     reiserfs_write_unlock(inode->i_sb);
0629     if (error) {
0630         return error;
0631     }
0632 
0633     error = reiserfs_xattr_set_handle(&th, inode, name,
0634                       buffer, buffer_size, flags);
0635 
0636     reiserfs_write_lock(inode->i_sb);
0637     error2 = journal_end(&th);
0638     reiserfs_write_unlock(inode->i_sb);
0639     if (error == 0)
0640         error = error2;
0641 
0642     return error;
0643 }
0644 
0645 /*
0646  * inode->i_mutex: down
0647  */
0648 int
0649 reiserfs_xattr_get(struct inode *inode, const char *name, void *buffer,
0650            size_t buffer_size)
0651 {
0652     ssize_t err = 0;
0653     struct dentry *dentry;
0654     size_t isize;
0655     size_t file_pos = 0;
0656     size_t buffer_pos = 0;
0657     struct page *page;
0658     __u32 hash = 0;
0659 
0660     if (name == NULL)
0661         return -EINVAL;
0662 
0663     /*
0664      * We can't have xattrs attached to v1 items since they don't have
0665      * generation numbers
0666      */
0667     if (get_inode_sd_version(inode) == STAT_DATA_V1)
0668         return -EOPNOTSUPP;
0669 
0670     /*
0671      * priv_root needn't be initialized during mount so allow initial
0672      * lookups to succeed.
0673      */
0674     if (!REISERFS_SB(inode->i_sb)->priv_root)
0675         return 0;
0676 
0677     dentry = xattr_lookup(inode, name, XATTR_REPLACE);
0678     if (IS_ERR(dentry)) {
0679         err = PTR_ERR(dentry);
0680         goto out;
0681     }
0682 
0683     down_read(&REISERFS_I(inode)->i_xattr_sem);
0684 
0685     isize = i_size_read(d_inode(dentry));
0686 
0687     /* Just return the size needed */
0688     if (buffer == NULL) {
0689         err = isize - sizeof(struct reiserfs_xattr_header);
0690         goto out_unlock;
0691     }
0692 
0693     if (buffer_size < isize - sizeof(struct reiserfs_xattr_header)) {
0694         err = -ERANGE;
0695         goto out_unlock;
0696     }
0697 
0698     while (file_pos < isize) {
0699         size_t chunk;
0700         char *data;
0701         size_t skip = 0;
0702 
0703         if (isize - file_pos > PAGE_SIZE)
0704             chunk = PAGE_SIZE;
0705         else
0706             chunk = isize - file_pos;
0707 
0708         page = reiserfs_get_page(d_inode(dentry), file_pos);
0709         if (IS_ERR(page)) {
0710             err = PTR_ERR(page);
0711             goto out_unlock;
0712         }
0713 
0714         lock_page(page);
0715         data = page_address(page);
0716         if (file_pos == 0) {
0717             struct reiserfs_xattr_header *rxh =
0718                 (struct reiserfs_xattr_header *)data;
0719             skip = file_pos = sizeof(struct reiserfs_xattr_header);
0720             chunk -= skip;
0721             /* Magic doesn't match up.. */
0722             if (rxh->h_magic != cpu_to_le32(REISERFS_XATTR_MAGIC)) {
0723                 unlock_page(page);
0724                 reiserfs_put_page(page);
0725                 reiserfs_warning(inode->i_sb, "jdm-20001",
0726                          "Invalid magic for xattr (%s) "
0727                          "associated with %k", name,
0728                          INODE_PKEY(inode));
0729                 err = -EIO;
0730                 goto out_unlock;
0731             }
0732             hash = le32_to_cpu(rxh->h_hash);
0733         }
0734         memcpy(buffer + buffer_pos, data + skip, chunk);
0735         unlock_page(page);
0736         reiserfs_put_page(page);
0737         file_pos += chunk;
0738         buffer_pos += chunk;
0739         skip = 0;
0740     }
0741     err = isize - sizeof(struct reiserfs_xattr_header);
0742 
0743     if (xattr_hash(buffer, isize - sizeof(struct reiserfs_xattr_header)) !=
0744         hash) {
0745         reiserfs_warning(inode->i_sb, "jdm-20002",
0746                  "Invalid hash for xattr (%s) associated "
0747                  "with %k", name, INODE_PKEY(inode));
0748         err = -EIO;
0749     }
0750 
0751 out_unlock:
0752     up_read(&REISERFS_I(inode)->i_xattr_sem);
0753     dput(dentry);
0754 
0755 out:
0756     return err;
0757 }
0758 
0759 /*
0760  * In order to implement different sets of xattr operations for each xattr
0761  * prefix with the generic xattr API, a filesystem should create a
0762  * null-terminated array of struct xattr_handler (one for each prefix) and
0763  * hang a pointer to it off of the s_xattr field of the superblock.
0764  *
0765  * The generic_fooxattr() functions will use this list to dispatch xattr
0766  * operations to the correct xattr_handler.
0767  */
0768 #define for_each_xattr_handler(handlers, handler)       \
0769         for ((handler) = *(handlers)++;         \
0770             (handler) != NULL;          \
0771             (handler) = *(handlers)++)
0772 
0773 /* This is the implementation for the xattr plugin infrastructure */
0774 static inline const struct xattr_handler *
0775 find_xattr_handler_prefix(const struct xattr_handler **handlers,
0776                const char *name)
0777 {
0778     const struct xattr_handler *xah;
0779 
0780     if (!handlers)
0781         return NULL;
0782 
0783     for_each_xattr_handler(handlers, xah) {
0784         const char *prefix = xattr_prefix(xah);
0785         if (strncmp(prefix, name, strlen(prefix)) == 0)
0786             break;
0787     }
0788 
0789     return xah;
0790 }
0791 
0792 struct listxattr_buf {
0793     struct dir_context ctx;
0794     size_t size;
0795     size_t pos;
0796     char *buf;
0797     struct dentry *dentry;
0798 };
0799 
0800 static int listxattr_filler(struct dir_context *ctx, const char *name,
0801                 int namelen, loff_t offset, u64 ino,
0802                 unsigned int d_type)
0803 {
0804     struct listxattr_buf *b =
0805         container_of(ctx, struct listxattr_buf, ctx);
0806     size_t size;
0807 
0808     if (name[0] != '.' ||
0809         (namelen != 1 && (name[1] != '.' || namelen != 2))) {
0810         const struct xattr_handler *handler;
0811 
0812         handler = find_xattr_handler_prefix(b->dentry->d_sb->s_xattr,
0813                             name);
0814         if (!handler /* Unsupported xattr name */ ||
0815             (handler->list && !handler->list(b->dentry)))
0816             return 0;
0817         size = namelen + 1;
0818         if (b->buf) {
0819             if (b->pos + size > b->size) {
0820                 b->pos = -ERANGE;
0821                 return -ERANGE;
0822             }
0823             memcpy(b->buf + b->pos, name, namelen);
0824             b->buf[b->pos + namelen] = 0;
0825         }
0826         b->pos += size;
0827     }
0828     return 0;
0829 }
0830 
0831 /*
0832  * Inode operation listxattr()
0833  *
0834  * We totally ignore the generic listxattr here because it would be stupid
0835  * not to. Since the xattrs are organized in a directory, we can just
0836  * readdir to find them.
0837  */
0838 ssize_t reiserfs_listxattr(struct dentry * dentry, char *buffer, size_t size)
0839 {
0840     struct dentry *dir;
0841     int err = 0;
0842     struct listxattr_buf buf = {
0843         .ctx.actor = listxattr_filler,
0844         .dentry = dentry,
0845         .buf = buffer,
0846         .size = buffer ? size : 0,
0847     };
0848 
0849     if (d_really_is_negative(dentry))
0850         return -EINVAL;
0851 
0852     if (get_inode_sd_version(d_inode(dentry)) == STAT_DATA_V1)
0853         return -EOPNOTSUPP;
0854 
0855     dir = open_xa_dir(d_inode(dentry), XATTR_REPLACE);
0856     if (IS_ERR(dir)) {
0857         err = PTR_ERR(dir);
0858         if (err == -ENODATA)
0859             err = 0;  /* Not an error if there aren't any xattrs */
0860         goto out;
0861     }
0862 
0863     inode_lock_nested(d_inode(dir), I_MUTEX_XATTR);
0864     err = reiserfs_readdir_inode(d_inode(dir), &buf.ctx);
0865     inode_unlock(d_inode(dir));
0866 
0867     if (!err)
0868         err = buf.pos;
0869 
0870     dput(dir);
0871 out:
0872     return err;
0873 }
0874 
0875 static int create_privroot(struct dentry *dentry)
0876 {
0877     int err;
0878     struct inode *inode = d_inode(dentry->d_parent);
0879 
0880     WARN_ON_ONCE(!inode_is_locked(inode));
0881 
0882     err = xattr_mkdir(inode, dentry, 0700);
0883     if (err || d_really_is_negative(dentry)) {
0884         reiserfs_warning(dentry->d_sb, "jdm-20006",
0885                  "xattrs/ACLs enabled and couldn't "
0886                  "find/create .reiserfs_priv. "
0887                  "Failing mount.");
0888         return -EOPNOTSUPP;
0889     }
0890 
0891     d_inode(dentry)->i_flags |= S_PRIVATE;
0892     d_inode(dentry)->i_opflags &= ~IOP_XATTR;
0893     reiserfs_info(dentry->d_sb, "Created %s - reserved for xattr "
0894               "storage.\n", PRIVROOT_NAME);
0895 
0896     return 0;
0897 }
0898 
0899 #else
0900 int __init reiserfs_xattr_register_handlers(void) { return 0; }
0901 void reiserfs_xattr_unregister_handlers(void) {}
0902 static int create_privroot(struct dentry *dentry) { return 0; }
0903 #endif
0904 
0905 /* Actual operations that are exported to VFS-land */
0906 const struct xattr_handler *reiserfs_xattr_handlers[] = {
0907 #ifdef CONFIG_REISERFS_FS_XATTR
0908     &reiserfs_xattr_user_handler,
0909     &reiserfs_xattr_trusted_handler,
0910 #endif
0911 #ifdef CONFIG_REISERFS_FS_SECURITY
0912     &reiserfs_xattr_security_handler,
0913 #endif
0914 #ifdef CONFIG_REISERFS_FS_POSIX_ACL
0915     &posix_acl_access_xattr_handler,
0916     &posix_acl_default_xattr_handler,
0917 #endif
0918     NULL
0919 };
0920 
0921 static int xattr_mount_check(struct super_block *s)
0922 {
0923     /*
0924      * We need generation numbers to ensure that the oid mapping is correct
0925      * v3.5 filesystems don't have them.
0926      */
0927     if (old_format_only(s)) {
0928         if (reiserfs_xattrs_optional(s)) {
0929             /*
0930              * Old format filesystem, but optional xattrs have
0931              * been enabled. Error out.
0932              */
0933             reiserfs_warning(s, "jdm-2005",
0934                      "xattrs/ACLs not supported "
0935                      "on pre-v3.6 format filesystems. "
0936                      "Failing mount.");
0937             return -EOPNOTSUPP;
0938         }
0939     }
0940 
0941     return 0;
0942 }
0943 
0944 int reiserfs_permission(struct user_namespace *mnt_userns, struct inode *inode,
0945             int mask)
0946 {
0947     /*
0948      * We don't do permission checks on the internal objects.
0949      * Permissions are determined by the "owning" object.
0950      */
0951     if (IS_PRIVATE(inode))
0952         return 0;
0953 
0954     return generic_permission(&init_user_ns, inode, mask);
0955 }
0956 
0957 static int xattr_hide_revalidate(struct dentry *dentry, unsigned int flags)
0958 {
0959     return -EPERM;
0960 }
0961 
0962 static const struct dentry_operations xattr_lookup_poison_ops = {
0963     .d_revalidate = xattr_hide_revalidate,
0964 };
0965 
0966 int reiserfs_lookup_privroot(struct super_block *s)
0967 {
0968     struct dentry *dentry;
0969     int err = 0;
0970 
0971     /* If we don't have the privroot located yet - go find it */
0972     inode_lock(d_inode(s->s_root));
0973     dentry = lookup_one_len(PRIVROOT_NAME, s->s_root,
0974                 strlen(PRIVROOT_NAME));
0975     if (!IS_ERR(dentry)) {
0976         REISERFS_SB(s)->priv_root = dentry;
0977         d_set_d_op(dentry, &xattr_lookup_poison_ops);
0978         if (d_really_is_positive(dentry)) {
0979             d_inode(dentry)->i_flags |= S_PRIVATE;
0980             d_inode(dentry)->i_opflags &= ~IOP_XATTR;
0981         }
0982     } else
0983         err = PTR_ERR(dentry);
0984     inode_unlock(d_inode(s->s_root));
0985 
0986     return err;
0987 }
0988 
0989 /*
0990  * We need to take a copy of the mount flags since things like
0991  * SB_RDONLY don't get set until *after* we're called.
0992  * mount_flags != mount_options
0993  */
0994 int reiserfs_xattr_init(struct super_block *s, int mount_flags)
0995 {
0996     int err = 0;
0997     struct dentry *privroot = REISERFS_SB(s)->priv_root;
0998 
0999     err = xattr_mount_check(s);
1000     if (err)
1001         goto error;
1002 
1003     if (d_really_is_negative(privroot) && !(mount_flags & SB_RDONLY)) {
1004         inode_lock(d_inode(s->s_root));
1005         err = create_privroot(REISERFS_SB(s)->priv_root);
1006         inode_unlock(d_inode(s->s_root));
1007     }
1008 
1009     if (d_really_is_positive(privroot)) {
1010         inode_lock(d_inode(privroot));
1011         if (!REISERFS_SB(s)->xattr_root) {
1012             struct dentry *dentry;
1013 
1014             dentry = lookup_one_len(XAROOT_NAME, privroot,
1015                         strlen(XAROOT_NAME));
1016             if (!IS_ERR(dentry))
1017                 REISERFS_SB(s)->xattr_root = dentry;
1018             else
1019                 err = PTR_ERR(dentry);
1020         }
1021         inode_unlock(d_inode(privroot));
1022     }
1023 
1024 error:
1025     if (err) {
1026         clear_bit(REISERFS_XATTRS_USER, &REISERFS_SB(s)->s_mount_opt);
1027         clear_bit(REISERFS_POSIXACL, &REISERFS_SB(s)->s_mount_opt);
1028     }
1029 
1030     /* The super_block SB_POSIXACL must mirror the (no)acl mount option. */
1031     if (reiserfs_posixacl(s))
1032         s->s_flags |= SB_POSIXACL;
1033     else
1034         s->s_flags &= ~SB_POSIXACL;
1035 
1036     return err;
1037 }