1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds * linux/fs/reiserfs/xattr.c
41da177e4SLinus Torvalds *
51da177e4SLinus Torvalds * Copyright (c) 2002 by Jeff Mahoney, <jeffm@suse.com>
61da177e4SLinus Torvalds *
71da177e4SLinus Torvalds */
81da177e4SLinus Torvalds
91da177e4SLinus Torvalds /*
101da177e4SLinus Torvalds * In order to implement EA/ACLs in a clean, backwards compatible manner,
111da177e4SLinus Torvalds * they are implemented as files in a "private" directory.
121da177e4SLinus Torvalds * Each EA is in it's own file, with the directory layout like so (/ is assumed
131da177e4SLinus Torvalds * to be relative to fs root). Inside the /.reiserfs_priv/xattrs directory,
141da177e4SLinus Torvalds * directories named using the capital-hex form of the objectid and
151da177e4SLinus Torvalds * generation number are used. Inside each directory are individual files
161da177e4SLinus Torvalds * named with the name of the extended attribute.
171da177e4SLinus Torvalds *
181da177e4SLinus Torvalds * So, for objectid 12648430, we could have:
191da177e4SLinus Torvalds * /.reiserfs_priv/xattrs/C0FFEE.0/system.posix_acl_access
201da177e4SLinus Torvalds * /.reiserfs_priv/xattrs/C0FFEE.0/system.posix_acl_default
211da177e4SLinus Torvalds * /.reiserfs_priv/xattrs/C0FFEE.0/user.Content-Type
221da177e4SLinus Torvalds * .. or similar.
231da177e4SLinus Torvalds *
241da177e4SLinus Torvalds * The file contents are the text of the EA. The size is known based on the
251da177e4SLinus Torvalds * stat data describing the file.
261da177e4SLinus Torvalds *
271da177e4SLinus Torvalds * In the case of system.posix_acl_access and system.posix_acl_default, since
281da177e4SLinus Torvalds * these are special cases for filesystem ACLs, they are interpreted by the
291da177e4SLinus Torvalds * kernel, in addition, they are negatively and positively cached and attached
301da177e4SLinus Torvalds * to the inode so that unnecessary lookups are avoided.
31d984561bSJeff Mahoney *
32d984561bSJeff Mahoney * Locking works like so:
338b6dd72aSJeff Mahoney * Directory components (xattr root, xattr dir) are protectd by their i_mutex.
348b6dd72aSJeff Mahoney * The xattrs themselves are protected by the xattr_sem.
351da177e4SLinus Torvalds */
361da177e4SLinus Torvalds
37f466c6fdSAl Viro #include "reiserfs.h"
3816f7e0feSRandy Dunlap #include <linux/capability.h>
391da177e4SLinus Torvalds #include <linux/dcache.h>
401da177e4SLinus Torvalds #include <linux/namei.h>
411da177e4SLinus Torvalds #include <linux/errno.h>
425a0e3ad6STejun Heo #include <linux/gfp.h>
431da177e4SLinus Torvalds #include <linux/fs.h>
441da177e4SLinus Torvalds #include <linux/file.h>
451da177e4SLinus Torvalds #include <linux/pagemap.h>
461da177e4SLinus Torvalds #include <linux/xattr.h>
47c45ac888SAl Viro #include "xattr.h"
48a3063ab8SAl Viro #include "acl.h"
4917093991SFabian Frederick #include <linux/uaccess.h>
503277c39fSAl Viro #include <net/checksum.h>
511da177e4SLinus Torvalds #include <linux/stat.h>
526c17675eSJeff Mahoney #include <linux/quotaops.h>
53431547b3SChristoph Hellwig #include <linux/security.h>
5447f70d08SChristoph Hellwig #include <linux/posix_acl_xattr.h>
55387b96a5SChristian Brauner #include <linux/xattr.h>
561da177e4SLinus Torvalds
571da177e4SLinus Torvalds #define PRIVROOT_NAME ".reiserfs_priv"
581da177e4SLinus Torvalds #define XAROOT_NAME "xattrs"
591da177e4SLinus Torvalds
601da177e4SLinus Torvalds
61098297b2SJeff Mahoney /*
62098297b2SJeff Mahoney * Helpers for inode ops. We do this so that we don't have all the VFS
636c17675eSJeff Mahoney * overhead and also for proper i_mutex annotation.
64098297b2SJeff Mahoney * dir->i_mutex must be held for all of them.
65098297b2SJeff Mahoney */
663a355cc6SJeff Mahoney #ifdef CONFIG_REISERFS_FS_XATTR
xattr_create(struct inode * dir,struct dentry * dentry,int mode)676c17675eSJeff Mahoney static int xattr_create(struct inode *dir, struct dentry *dentry, int mode)
681da177e4SLinus Torvalds {
695955102cSAl Viro BUG_ON(!inode_is_locked(dir));
706c960e68SChristian Brauner return dir->i_op->create(&nop_mnt_idmap, dir, dentry, mode, true);
716c17675eSJeff Mahoney }
723a355cc6SJeff Mahoney #endif
731da177e4SLinus Torvalds
xattr_mkdir(struct inode * dir,struct dentry * dentry,umode_t mode)7418bb1db3SAl Viro static int xattr_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
756c17675eSJeff Mahoney {
765955102cSAl Viro BUG_ON(!inode_is_locked(dir));
77c54bd91eSChristian Brauner return dir->i_op->mkdir(&nop_mnt_idmap, dir, dentry, mode);
786c17675eSJeff Mahoney }
796c17675eSJeff Mahoney
80098297b2SJeff Mahoney /*
81098297b2SJeff Mahoney * We use I_MUTEX_CHILD here to silence lockdep. It's safe because xattr
826c17675eSJeff Mahoney * mutation ops aren't called during rename or splace, which are the
836c17675eSJeff Mahoney * only other users of I_MUTEX_CHILD. It violates the ordering, but that's
84098297b2SJeff Mahoney * better than allocating another subclass just for this code.
85098297b2SJeff Mahoney */
xattr_unlink(struct inode * dir,struct dentry * dentry)866c17675eSJeff Mahoney static int xattr_unlink(struct inode *dir, struct dentry *dentry)
876c17675eSJeff Mahoney {
886c17675eSJeff Mahoney int error;
89f3fb9e27SFabian Frederick
905955102cSAl Viro BUG_ON(!inode_is_locked(dir));
916c17675eSJeff Mahoney
925955102cSAl Viro inode_lock_nested(d_inode(dentry), I_MUTEX_CHILD);
936c17675eSJeff Mahoney error = dir->i_op->unlink(dir, dentry);
945955102cSAl Viro inode_unlock(d_inode(dentry));
956c17675eSJeff Mahoney
966c17675eSJeff Mahoney if (!error)
976c17675eSJeff Mahoney d_delete(dentry);
986c17675eSJeff Mahoney return error;
996c17675eSJeff Mahoney }
1006c17675eSJeff Mahoney
xattr_rmdir(struct inode * dir,struct dentry * dentry)1016c17675eSJeff Mahoney static int xattr_rmdir(struct inode *dir, struct dentry *dentry)
1026c17675eSJeff Mahoney {
1036c17675eSJeff Mahoney int error;
104f3fb9e27SFabian Frederick
1055955102cSAl Viro BUG_ON(!inode_is_locked(dir));
1066c17675eSJeff Mahoney
1075955102cSAl Viro inode_lock_nested(d_inode(dentry), I_MUTEX_CHILD);
1086c17675eSJeff Mahoney error = dir->i_op->rmdir(dir, dentry);
1096c17675eSJeff Mahoney if (!error)
1102b0143b5SDavid Howells d_inode(dentry)->i_flags |= S_DEAD;
1115955102cSAl Viro inode_unlock(d_inode(dentry));
1126c17675eSJeff Mahoney if (!error)
1136c17675eSJeff Mahoney d_delete(dentry);
1146c17675eSJeff Mahoney
1156c17675eSJeff Mahoney return error;
1166c17675eSJeff Mahoney }
1176c17675eSJeff Mahoney
1186c17675eSJeff Mahoney #define xattr_may_create(flags) (!flags || flags & XATTR_CREATE)
1196c17675eSJeff Mahoney
open_xa_root(struct super_block * sb,int flags)1206c17675eSJeff Mahoney static struct dentry *open_xa_root(struct super_block *sb, int flags)
1216c17675eSJeff Mahoney {
1226c17675eSJeff Mahoney struct dentry *privroot = REISERFS_SB(sb)->priv_root;
123ab17c4f0SJeff Mahoney struct dentry *xaroot;
124f3fb9e27SFabian Frederick
1252b0143b5SDavid Howells if (d_really_is_negative(privroot))
12660e4cf67SJeff Mahoney return ERR_PTR(-EOPNOTSUPP);
127ab17c4f0SJeff Mahoney
1285955102cSAl Viro inode_lock_nested(d_inode(privroot), I_MUTEX_XATTR);
129ab17c4f0SJeff Mahoney
130ab17c4f0SJeff Mahoney xaroot = dget(REISERFS_SB(sb)->xattr_root);
131ceb5edc4SJeff Mahoney if (!xaroot)
13260e4cf67SJeff Mahoney xaroot = ERR_PTR(-EOPNOTSUPP);
1332b0143b5SDavid Howells else if (d_really_is_negative(xaroot)) {
134ab17c4f0SJeff Mahoney int err = -ENODATA;
135f3fb9e27SFabian Frederick
136ab17c4f0SJeff Mahoney if (xattr_may_create(flags))
1372b0143b5SDavid Howells err = xattr_mkdir(d_inode(privroot), xaroot, 0700);
138ab17c4f0SJeff Mahoney if (err) {
139ab17c4f0SJeff Mahoney dput(xaroot);
140ab17c4f0SJeff Mahoney xaroot = ERR_PTR(err);
141ab17c4f0SJeff Mahoney }
142ab17c4f0SJeff Mahoney }
143ab17c4f0SJeff Mahoney
1445955102cSAl Viro inode_unlock(d_inode(privroot));
145ab17c4f0SJeff Mahoney return xaroot;
1469b7f3755SJeff Mahoney }
1471da177e4SLinus Torvalds
open_xa_dir(const struct inode * inode,int flags)148bd4c625cSLinus Torvalds static struct dentry *open_xa_dir(const struct inode *inode, int flags)
1491da177e4SLinus Torvalds {
1501da177e4SLinus Torvalds struct dentry *xaroot, *xadir;
1511da177e4SLinus Torvalds char namebuf[17];
1521da177e4SLinus Torvalds
1536c17675eSJeff Mahoney xaroot = open_xa_root(inode->i_sb, flags);
1541da177e4SLinus Torvalds if (IS_ERR(xaroot))
1551da177e4SLinus Torvalds return xaroot;
1561da177e4SLinus Torvalds
1571da177e4SLinus Torvalds snprintf(namebuf, sizeof(namebuf), "%X.%X",
1581da177e4SLinus Torvalds le32_to_cpu(INODE_PKEY(inode)->k_objectid),
1591da177e4SLinus Torvalds inode->i_generation);
1606c17675eSJeff Mahoney
1615955102cSAl Viro inode_lock_nested(d_inode(xaroot), I_MUTEX_XATTR);
162ab17c4f0SJeff Mahoney
163ab17c4f0SJeff Mahoney xadir = lookup_one_len(namebuf, xaroot, strlen(namebuf));
1642b0143b5SDavid Howells if (!IS_ERR(xadir) && d_really_is_negative(xadir)) {
165ab17c4f0SJeff Mahoney int err = -ENODATA;
166f3fb9e27SFabian Frederick
167ab17c4f0SJeff Mahoney if (xattr_may_create(flags))
1682b0143b5SDavid Howells err = xattr_mkdir(d_inode(xaroot), xadir, 0700);
169ab17c4f0SJeff Mahoney if (err) {
170ab17c4f0SJeff Mahoney dput(xadir);
171ab17c4f0SJeff Mahoney xadir = ERR_PTR(err);
172ab17c4f0SJeff Mahoney }
173ab17c4f0SJeff Mahoney }
174ab17c4f0SJeff Mahoney
1755955102cSAl Viro inode_unlock(d_inode(xaroot));
1761da177e4SLinus Torvalds dput(xaroot);
1771da177e4SLinus Torvalds return xadir;
1781da177e4SLinus Torvalds }
1791da177e4SLinus Torvalds
180098297b2SJeff Mahoney /*
181098297b2SJeff Mahoney * The following are side effects of other operations that aren't explicitly
18248b32a35SJeff Mahoney * modifying extended attributes. This includes operations such as permissions
183098297b2SJeff Mahoney * or ownership changes, object deletions, etc.
184098297b2SJeff Mahoney */
185a41f1a47SJeff Mahoney struct reiserfs_dentry_buf {
1864acf381eSAl Viro struct dir_context ctx;
187a41f1a47SJeff Mahoney struct dentry *xadir;
188a41f1a47SJeff Mahoney int count;
189b10298d5SJann Horn int err;
190a41f1a47SJeff Mahoney struct dentry *dentries[8];
191a41f1a47SJeff Mahoney };
19248b32a35SJeff Mahoney
19325885a35SAl Viro static bool
fill_with_dentries(struct dir_context * ctx,const char * name,int namelen,loff_t offset,u64 ino,unsigned int d_type)194ac7576f4SMiklos Szeredi fill_with_dentries(struct dir_context *ctx, const char *name, int namelen,
195ac7576f4SMiklos Szeredi loff_t offset, u64 ino, unsigned int d_type)
196a72bdb1cSJeff Mahoney {
197ac7576f4SMiklos Szeredi struct reiserfs_dentry_buf *dbuf =
198ac7576f4SMiklos Szeredi container_of(ctx, struct reiserfs_dentry_buf, ctx);
199a72bdb1cSJeff Mahoney struct dentry *dentry;
200f3fb9e27SFabian Frederick
2015955102cSAl Viro WARN_ON_ONCE(!inode_is_locked(d_inode(dbuf->xadir)));
202a72bdb1cSJeff Mahoney
203a41f1a47SJeff Mahoney if (dbuf->count == ARRAY_SIZE(dbuf->dentries))
20425885a35SAl Viro return false;
205a41f1a47SJeff Mahoney
20635e5cbc0SJan Kara if (name[0] == '.' && (namelen < 2 ||
20735e5cbc0SJan Kara (namelen == 2 && name[1] == '.')))
20825885a35SAl Viro return true;
209a41f1a47SJeff Mahoney
210a41f1a47SJeff Mahoney dentry = lookup_one_len(name, dbuf->xadir, namelen);
211a72bdb1cSJeff Mahoney if (IS_ERR(dentry)) {
212b10298d5SJann Horn dbuf->err = PTR_ERR(dentry);
21325885a35SAl Viro return false;
2142b0143b5SDavid Howells } else if (d_really_is_negative(dentry)) {
215a41f1a47SJeff Mahoney /* A directory entry exists, but no file? */
216a41f1a47SJeff Mahoney reiserfs_error(dentry->d_sb, "xattr-20003",
217a455589fSAl Viro "Corrupted directory: xattr %pd listed but "
218a455589fSAl Viro "not found for file %pd.\n",
219a455589fSAl Viro dentry, dbuf->xadir);
220a72bdb1cSJeff Mahoney dput(dentry);
221b10298d5SJann Horn dbuf->err = -EIO;
22225885a35SAl Viro return false;
2231da177e4SLinus Torvalds }
2241da177e4SLinus Torvalds
225a41f1a47SJeff Mahoney dbuf->dentries[dbuf->count++] = dentry;
22625885a35SAl Viro return true;
2271da177e4SLinus Torvalds }
2281da177e4SLinus Torvalds
229a41f1a47SJeff Mahoney static void
cleanup_dentry_buf(struct reiserfs_dentry_buf * buf)230a41f1a47SJeff Mahoney cleanup_dentry_buf(struct reiserfs_dentry_buf *buf)
231a72bdb1cSJeff Mahoney {
232a41f1a47SJeff Mahoney int i;
233f3fb9e27SFabian Frederick
234a41f1a47SJeff Mahoney for (i = 0; i < buf->count; i++)
235a41f1a47SJeff Mahoney if (buf->dentries[i])
236a41f1a47SJeff Mahoney dput(buf->dentries[i]);
237a41f1a47SJeff Mahoney }
238a41f1a47SJeff Mahoney
reiserfs_for_each_xattr(struct inode * inode,int (* action)(struct dentry *,void *),void * data)239a41f1a47SJeff Mahoney static int reiserfs_for_each_xattr(struct inode *inode,
240a41f1a47SJeff Mahoney int (*action)(struct dentry *, void *),
241a41f1a47SJeff Mahoney void *data)
242a41f1a47SJeff Mahoney {
243a41f1a47SJeff Mahoney struct dentry *dir;
244a41f1a47SJeff Mahoney int i, err = 0;
245a41f1a47SJeff Mahoney struct reiserfs_dentry_buf buf = {
2464acf381eSAl Viro .ctx.actor = fill_with_dentries,
247a41f1a47SJeff Mahoney };
248a72bdb1cSJeff Mahoney
249a72bdb1cSJeff Mahoney /* Skip out, an xattr has no xattrs associated with it */
250a72bdb1cSJeff Mahoney if (IS_PRIVATE(inode) || get_inode_sd_version(inode) == STAT_DATA_V1)
251a72bdb1cSJeff Mahoney return 0;
252a72bdb1cSJeff Mahoney
2536c17675eSJeff Mahoney dir = open_xa_dir(inode, XATTR_REPLACE);
254a72bdb1cSJeff Mahoney if (IS_ERR(dir)) {
255a72bdb1cSJeff Mahoney err = PTR_ERR(dir);
256a72bdb1cSJeff Mahoney goto out;
2572b0143b5SDavid Howells } else if (d_really_is_negative(dir)) {
258a41f1a47SJeff Mahoney err = 0;
259a41f1a47SJeff Mahoney goto out_dir;
260a72bdb1cSJeff Mahoney }
261a72bdb1cSJeff Mahoney
2625955102cSAl Viro inode_lock_nested(d_inode(dir), I_MUTEX_XATTR);
26327026a05SFrederic Weisbecker
264a41f1a47SJeff Mahoney buf.xadir = dir;
265cd62cdaeSAl Viro while (1) {
2662b0143b5SDavid Howells err = reiserfs_readdir_inode(d_inode(dir), &buf.ctx);
267cd62cdaeSAl Viro if (err)
268cd62cdaeSAl Viro break;
269b10298d5SJann Horn if (buf.err) {
270b10298d5SJann Horn err = buf.err;
271b10298d5SJann Horn break;
272b10298d5SJann Horn }
273cd62cdaeSAl Viro if (!buf.count)
274cd62cdaeSAl Viro break;
275cd62cdaeSAl Viro for (i = 0; !err && i < buf.count && buf.dentries[i]; i++) {
276a41f1a47SJeff Mahoney struct dentry *dentry = buf.dentries[i];
277a41f1a47SJeff Mahoney
278e36cb0b8SDavid Howells if (!d_is_dir(dentry))
279cd62cdaeSAl Viro err = action(dentry, data);
280a41f1a47SJeff Mahoney
281a41f1a47SJeff Mahoney dput(dentry);
282a41f1a47SJeff Mahoney buf.dentries[i] = NULL;
2838b6dd72aSJeff Mahoney }
284cd62cdaeSAl Viro if (err)
285cd62cdaeSAl Viro break;
286a41f1a47SJeff Mahoney buf.count = 0;
287a41f1a47SJeff Mahoney }
2885955102cSAl Viro inode_unlock(d_inode(dir));
2898b6dd72aSJeff Mahoney
290a41f1a47SJeff Mahoney cleanup_dentry_buf(&buf);
291a72bdb1cSJeff Mahoney
292a41f1a47SJeff Mahoney if (!err) {
293098297b2SJeff Mahoney /*
294098297b2SJeff Mahoney * We start a transaction here to avoid a ABBA situation
295d984561bSJeff Mahoney * between the xattr root's i_mutex and the journal lock.
296a41f1a47SJeff Mahoney * This doesn't incur much additional overhead since the
297a41f1a47SJeff Mahoney * new transaction will just nest inside the
298098297b2SJeff Mahoney * outer transaction.
299098297b2SJeff Mahoney */
300a41f1a47SJeff Mahoney int blocks = JOURNAL_PER_BALANCE_CNT * 2 + 2 +
301a41f1a47SJeff Mahoney 4 * REISERFS_QUOTA_TRANS_BLOCKS(inode->i_sb);
302a41f1a47SJeff Mahoney struct reiserfs_transaction_handle th;
303f3fb9e27SFabian Frederick
3044c05141dSJeff Mahoney reiserfs_write_lock(inode->i_sb);
305d984561bSJeff Mahoney err = journal_begin(&th, inode->i_sb, blocks);
3064c05141dSJeff Mahoney reiserfs_write_unlock(inode->i_sb);
307d984561bSJeff Mahoney if (!err) {
308d984561bSJeff Mahoney int jerror;
309f3fb9e27SFabian Frederick
3105955102cSAl Viro inode_lock_nested(d_inode(dir->d_parent),
3114c05141dSJeff Mahoney I_MUTEX_XATTR);
312a41f1a47SJeff Mahoney err = action(dir, data);
3134c05141dSJeff Mahoney reiserfs_write_lock(inode->i_sb);
31458d85426SJeff Mahoney jerror = journal_end(&th);
3154c05141dSJeff Mahoney reiserfs_write_unlock(inode->i_sb);
3165955102cSAl Viro inode_unlock(d_inode(dir->d_parent));
317d984561bSJeff Mahoney err = jerror ?: err;
318a72bdb1cSJeff Mahoney }
319a72bdb1cSJeff Mahoney }
320a72bdb1cSJeff Mahoney out_dir:
321a72bdb1cSJeff Mahoney dput(dir);
322a72bdb1cSJeff Mahoney out:
323394440d4SJeff Mahoney /*
324394440d4SJeff Mahoney * -ENODATA: this object doesn't have any xattrs
325394440d4SJeff Mahoney * -EOPNOTSUPP: this file system doesn't have xattrs enabled on disk.
326394440d4SJeff Mahoney * Neither are errors
327394440d4SJeff Mahoney */
328394440d4SJeff Mahoney if (err == -ENODATA || err == -EOPNOTSUPP)
329a41f1a47SJeff Mahoney err = 0;
330a41f1a47SJeff Mahoney return err;
331a41f1a47SJeff Mahoney }
332a41f1a47SJeff Mahoney
delete_one_xattr(struct dentry * dentry,void * data)333a41f1a47SJeff Mahoney static int delete_one_xattr(struct dentry *dentry, void *data)
334a41f1a47SJeff Mahoney {
3352b0143b5SDavid Howells struct inode *dir = d_inode(dentry->d_parent);
336a41f1a47SJeff Mahoney
337a41f1a47SJeff Mahoney /* This is the xattr dir, handle specially. */
338e36cb0b8SDavid Howells if (d_is_dir(dentry))
339a41f1a47SJeff Mahoney return xattr_rmdir(dir, dentry);
340a41f1a47SJeff Mahoney
341a41f1a47SJeff Mahoney return xattr_unlink(dir, dentry);
342a41f1a47SJeff Mahoney }
343a41f1a47SJeff Mahoney
chown_one_xattr(struct dentry * dentry,void * data)344a41f1a47SJeff Mahoney static int chown_one_xattr(struct dentry *dentry, void *data)
345a41f1a47SJeff Mahoney {
346a41f1a47SJeff Mahoney struct iattr *attrs = data;
3474a857011SJeff Mahoney int ia_valid = attrs->ia_valid;
3484a857011SJeff Mahoney int err;
3494a857011SJeff Mahoney
3504a857011SJeff Mahoney /*
3514a857011SJeff Mahoney * We only want the ownership bits. Otherwise, we'll do
3524a857011SJeff Mahoney * things like change a directory to a regular file if
3534a857011SJeff Mahoney * ATTR_MODE is set.
3544a857011SJeff Mahoney */
3554a857011SJeff Mahoney attrs->ia_valid &= (ATTR_UID|ATTR_GID);
356c1632a0fSChristian Brauner err = reiserfs_setattr(&nop_mnt_idmap, dentry, attrs);
3574a857011SJeff Mahoney attrs->ia_valid = ia_valid;
3584a857011SJeff Mahoney
3594a857011SJeff Mahoney return err;
360a41f1a47SJeff Mahoney }
361a41f1a47SJeff Mahoney
362a41f1a47SJeff Mahoney /* No i_mutex, but the inode is unconnected. */
reiserfs_delete_xattrs(struct inode * inode)363a41f1a47SJeff Mahoney int reiserfs_delete_xattrs(struct inode *inode)
364a41f1a47SJeff Mahoney {
365a41f1a47SJeff Mahoney int err = reiserfs_for_each_xattr(inode, delete_one_xattr, NULL);
366f3fb9e27SFabian Frederick
367a41f1a47SJeff Mahoney if (err)
368a41f1a47SJeff Mahoney reiserfs_warning(inode->i_sb, "jdm-20004",
369a41f1a47SJeff Mahoney "Couldn't delete all xattrs (%d)\n", err);
370a41f1a47SJeff Mahoney return err;
371a41f1a47SJeff Mahoney }
372a41f1a47SJeff Mahoney
373a41f1a47SJeff Mahoney /* inode->i_mutex: down */
reiserfs_chown_xattrs(struct inode * inode,struct iattr * attrs)374a41f1a47SJeff Mahoney int reiserfs_chown_xattrs(struct inode *inode, struct iattr *attrs)
375a41f1a47SJeff Mahoney {
376a41f1a47SJeff Mahoney int err = reiserfs_for_each_xattr(inode, chown_one_xattr, attrs);
377f3fb9e27SFabian Frederick
3788b6dd72aSJeff Mahoney if (err)
3798b6dd72aSJeff Mahoney reiserfs_warning(inode->i_sb, "jdm-20007",
3808b6dd72aSJeff Mahoney "Couldn't chown all xattrs (%d)\n", err);
381a72bdb1cSJeff Mahoney return err;
382a72bdb1cSJeff Mahoney }
383a72bdb1cSJeff Mahoney
384a72bdb1cSJeff Mahoney #ifdef CONFIG_REISERFS_FS_XATTR
385098297b2SJeff Mahoney /*
386098297b2SJeff Mahoney * Returns a dentry corresponding to a specific extended attribute file
3871da177e4SLinus Torvalds * for the inode. If flags allow, the file is created. Otherwise, a
388098297b2SJeff Mahoney * valid or negative dentry, or an error is returned.
389098297b2SJeff Mahoney */
xattr_lookup(struct inode * inode,const char * name,int flags)39048b32a35SJeff Mahoney static struct dentry *xattr_lookup(struct inode *inode, const char *name,
39148b32a35SJeff Mahoney int flags)
3921da177e4SLinus Torvalds {
3931da177e4SLinus Torvalds struct dentry *xadir, *xafile;
3941da177e4SLinus Torvalds int err = 0;
3951da177e4SLinus Torvalds
3961da177e4SLinus Torvalds xadir = open_xa_dir(inode, flags);
3976c17675eSJeff Mahoney if (IS_ERR(xadir))
398e231c2eeSDavid Howells return ERR_CAST(xadir);
3991da177e4SLinus Torvalds
4005955102cSAl Viro inode_lock_nested(d_inode(xadir), I_MUTEX_XATTR);
4011da177e4SLinus Torvalds xafile = lookup_one_len(name, xadir, strlen(name));
4021da177e4SLinus Torvalds if (IS_ERR(xafile)) {
4036c17675eSJeff Mahoney err = PTR_ERR(xafile);
4046c17675eSJeff Mahoney goto out;
4051da177e4SLinus Torvalds }
4061da177e4SLinus Torvalds
4072b0143b5SDavid Howells if (d_really_is_positive(xafile) && (flags & XATTR_CREATE))
4081da177e4SLinus Torvalds err = -EEXIST;
4091da177e4SLinus Torvalds
4102b0143b5SDavid Howells if (d_really_is_negative(xafile)) {
4116c17675eSJeff Mahoney err = -ENODATA;
4125a6059c3SJeff Mahoney if (xattr_may_create(flags))
4132b0143b5SDavid Howells err = xattr_create(d_inode(xadir), xafile,
4146c17675eSJeff Mahoney 0700|S_IFREG);
4151da177e4SLinus Torvalds }
4161da177e4SLinus Torvalds
4176c17675eSJeff Mahoney if (err)
4186c17675eSJeff Mahoney dput(xafile);
4191da177e4SLinus Torvalds out:
4205955102cSAl Viro inode_unlock(d_inode(xadir));
4211da177e4SLinus Torvalds dput(xadir);
4221da177e4SLinus Torvalds if (err)
4236c17675eSJeff Mahoney return ERR_PTR(err);
4243227e14cSJeff Mahoney return xafile;
4251da177e4SLinus Torvalds }
4261da177e4SLinus Torvalds
4271da177e4SLinus Torvalds /* Internal operations on file data */
reiserfs_put_page(struct page * page)428bd4c625cSLinus Torvalds static inline void reiserfs_put_page(struct page *page)
4291da177e4SLinus Torvalds {
4301da177e4SLinus Torvalds kunmap(page);
43109cbfeafSKirill A. Shutemov put_page(page);
4321da177e4SLinus Torvalds }
4331da177e4SLinus Torvalds
reiserfs_get_page(struct inode * dir,size_t n)434ec6ea56bSJeff Mahoney static struct page *reiserfs_get_page(struct inode *dir, size_t n)
4351da177e4SLinus Torvalds {
4361da177e4SLinus Torvalds struct address_space *mapping = dir->i_mapping;
4371da177e4SLinus Torvalds struct page *page;
438098297b2SJeff Mahoney /*
439098297b2SJeff Mahoney * We can deadlock if we try to free dentries,
440098297b2SJeff Mahoney * and an unlink/rmdir has just occurred - GFP_NOFS avoids this
441098297b2SJeff Mahoney */
442c4cdd038SAl Viro mapping_set_gfp_mask(mapping, GFP_NOFS);
44309cbfeafSKirill A. Shutemov page = read_mapping_page(mapping, n >> PAGE_SHIFT, NULL);
444b0c971e7SMatthew Wilcox (Oracle) if (!IS_ERR(page))
4451da177e4SLinus Torvalds kmap(page);
4461da177e4SLinus Torvalds return page;
4471da177e4SLinus Torvalds }
4481da177e4SLinus Torvalds
xattr_hash(const char * msg,int len)449bd4c625cSLinus Torvalds static inline __u32 xattr_hash(const char *msg, int len)
4501da177e4SLinus Torvalds {
451672cdd56SBharath Vedartham /*
452672cdd56SBharath Vedartham * csum_partial() gives different results for little-endian and
453672cdd56SBharath Vedartham * big endian hosts. Images created on little-endian hosts and
454672cdd56SBharath Vedartham * mounted on big-endian hosts(and vice versa) will see csum mismatches
455672cdd56SBharath Vedartham * when trying to fetch xattrs. Treating the hash as __wsum_t would
456672cdd56SBharath Vedartham * lower the frequency of mismatch. This is an endianness bug in
457672cdd56SBharath Vedartham * reiserfs. The return statement would result in a sparse warning. Do
458672cdd56SBharath Vedartham * not fix the sparse warning so as to not hide a reminder of the bug.
459672cdd56SBharath Vedartham */
4601da177e4SLinus Torvalds return csum_partial(msg, len, 0);
4611da177e4SLinus Torvalds }
4621da177e4SLinus Torvalds
463ba9d8cecSVladimir Saveliev int reiserfs_commit_write(struct file *f, struct page *page,
464ba9d8cecSVladimir Saveliev unsigned from, unsigned to);
465ba9d8cecSVladimir Saveliev
update_ctime(struct inode * inode)46648b32a35SJeff Mahoney static void update_ctime(struct inode *inode)
46748b32a35SJeff Mahoney {
46895582b00SDeepa Dinamani struct timespec64 now = current_time(inode);
469*ae834901SJeff Layton struct timespec64 ctime = inode_get_ctime(inode);
470f3fb9e27SFabian Frederick
4711d3382cbSAl Viro if (inode_unhashed(inode) || !inode->i_nlink ||
472*ae834901SJeff Layton timespec64_equal(&ctime, &now))
47348b32a35SJeff Mahoney return;
47448b32a35SJeff Mahoney
475*ae834901SJeff Layton inode_set_ctime_to_ts(inode, now);
47648b32a35SJeff Mahoney mark_inode_dirty(inode);
47748b32a35SJeff Mahoney }
47848b32a35SJeff Mahoney
lookup_and_delete_xattr(struct inode * inode,const char * name)47948b32a35SJeff Mahoney static int lookup_and_delete_xattr(struct inode *inode, const char *name)
48048b32a35SJeff Mahoney {
48148b32a35SJeff Mahoney int err = 0;
48248b32a35SJeff Mahoney struct dentry *dentry, *xadir;
48348b32a35SJeff Mahoney
48448b32a35SJeff Mahoney xadir = open_xa_dir(inode, XATTR_REPLACE);
48548b32a35SJeff Mahoney if (IS_ERR(xadir))
48648b32a35SJeff Mahoney return PTR_ERR(xadir);
48748b32a35SJeff Mahoney
4885955102cSAl Viro inode_lock_nested(d_inode(xadir), I_MUTEX_XATTR);
48948b32a35SJeff Mahoney dentry = lookup_one_len(name, xadir, strlen(name));
49048b32a35SJeff Mahoney if (IS_ERR(dentry)) {
49148b32a35SJeff Mahoney err = PTR_ERR(dentry);
49248b32a35SJeff Mahoney goto out_dput;
49348b32a35SJeff Mahoney }
49448b32a35SJeff Mahoney
4952b0143b5SDavid Howells if (d_really_is_positive(dentry)) {
4962b0143b5SDavid Howells err = xattr_unlink(d_inode(xadir), dentry);
49748b32a35SJeff Mahoney update_ctime(inode);
49848b32a35SJeff Mahoney }
49948b32a35SJeff Mahoney
50048b32a35SJeff Mahoney dput(dentry);
50148b32a35SJeff Mahoney out_dput:
5025955102cSAl Viro inode_unlock(d_inode(xadir));
50348b32a35SJeff Mahoney dput(xadir);
50448b32a35SJeff Mahoney return err;
50548b32a35SJeff Mahoney }
50648b32a35SJeff Mahoney
507ba9d8cecSVladimir Saveliev
5081da177e4SLinus Torvalds /* Generic extended attribute operations that can be used by xa plugins */
5091da177e4SLinus Torvalds
5101da177e4SLinus Torvalds /*
5111b1dcc1bSJes Sorensen * inode->i_mutex: down
5121da177e4SLinus Torvalds */
5131da177e4SLinus Torvalds int
reiserfs_xattr_set_handle(struct reiserfs_transaction_handle * th,struct inode * inode,const char * name,const void * buffer,size_t buffer_size,int flags)5140ab2621eSJeff Mahoney reiserfs_xattr_set_handle(struct reiserfs_transaction_handle *th,
5150ab2621eSJeff Mahoney struct inode *inode, const char *name,
5160ab2621eSJeff Mahoney const void *buffer, size_t buffer_size, int flags)
5171da177e4SLinus Torvalds {
5181da177e4SLinus Torvalds int err = 0;
5193227e14cSJeff Mahoney struct dentry *dentry;
5201da177e4SLinus Torvalds struct page *page;
5211da177e4SLinus Torvalds char *data;
5221da177e4SLinus Torvalds size_t file_pos = 0;
5231da177e4SLinus Torvalds size_t buffer_pos = 0;
52448b32a35SJeff Mahoney size_t new_size;
5251da177e4SLinus Torvalds __u32 xahash = 0;
5261da177e4SLinus Torvalds
5271da177e4SLinus Torvalds if (get_inode_sd_version(inode) == STAT_DATA_V1)
5281da177e4SLinus Torvalds return -EOPNOTSUPP;
5291da177e4SLinus Torvalds
5304f3be1b5SFrederic Weisbecker if (!buffer) {
5314f3be1b5SFrederic Weisbecker err = lookup_and_delete_xattr(inode, name);
5324f3be1b5SFrederic Weisbecker return err;
5334f3be1b5SFrederic Weisbecker }
5344f3be1b5SFrederic Weisbecker
53548b32a35SJeff Mahoney dentry = xattr_lookup(inode, name, flags);
5364c05141dSJeff Mahoney if (IS_ERR(dentry))
53748b32a35SJeff Mahoney return PTR_ERR(dentry);
5381da177e4SLinus Torvalds
539f3e22f48SFrederic Weisbecker down_write(&REISERFS_I(inode)->i_xattr_sem);
5403f14fea6SFrederic Weisbecker
5411da177e4SLinus Torvalds xahash = xattr_hash(buffer, buffer_size);
5421da177e4SLinus Torvalds while (buffer_pos < buffer_size || buffer_pos == 0) {
5431da177e4SLinus Torvalds size_t chunk;
5441da177e4SLinus Torvalds size_t skip = 0;
54509cbfeafSKirill A. Shutemov size_t page_offset = (file_pos & (PAGE_SIZE - 1));
546f3fb9e27SFabian Frederick
54709cbfeafSKirill A. Shutemov if (buffer_size - buffer_pos > PAGE_SIZE)
54809cbfeafSKirill A. Shutemov chunk = PAGE_SIZE;
5491da177e4SLinus Torvalds else
5501da177e4SLinus Torvalds chunk = buffer_size - buffer_pos;
5511da177e4SLinus Torvalds
5522b0143b5SDavid Howells page = reiserfs_get_page(d_inode(dentry), file_pos);
5531da177e4SLinus Torvalds if (IS_ERR(page)) {
5541da177e4SLinus Torvalds err = PTR_ERR(page);
55548b32a35SJeff Mahoney goto out_unlock;
5561da177e4SLinus Torvalds }
5571da177e4SLinus Torvalds
5581da177e4SLinus Torvalds lock_page(page);
5591da177e4SLinus Torvalds data = page_address(page);
5601da177e4SLinus Torvalds
5611da177e4SLinus Torvalds if (file_pos == 0) {
5621da177e4SLinus Torvalds struct reiserfs_xattr_header *rxh;
563f3fb9e27SFabian Frederick
5641da177e4SLinus Torvalds skip = file_pos = sizeof(struct reiserfs_xattr_header);
56509cbfeafSKirill A. Shutemov if (chunk + skip > PAGE_SIZE)
56609cbfeafSKirill A. Shutemov chunk = PAGE_SIZE - skip;
5671da177e4SLinus Torvalds rxh = (struct reiserfs_xattr_header *)data;
5681da177e4SLinus Torvalds rxh->h_magic = cpu_to_le32(REISERFS_XATTR_MAGIC);
5691da177e4SLinus Torvalds rxh->h_hash = cpu_to_le32(xahash);
5701da177e4SLinus Torvalds }
5711da177e4SLinus Torvalds
5724c05141dSJeff Mahoney reiserfs_write_lock(inode->i_sb);
573ebdec241SChristoph Hellwig err = __reiserfs_write_begin(page, page_offset, chunk + skip);
5741da177e4SLinus Torvalds if (!err) {
5751da177e4SLinus Torvalds if (buffer)
5761da177e4SLinus Torvalds memcpy(data + skip, buffer + buffer_pos, chunk);
5773227e14cSJeff Mahoney err = reiserfs_commit_write(NULL, page, page_offset,
578bd4c625cSLinus Torvalds page_offset + chunk +
579bd4c625cSLinus Torvalds skip);
5801da177e4SLinus Torvalds }
5814c05141dSJeff Mahoney reiserfs_write_unlock(inode->i_sb);
5821da177e4SLinus Torvalds unlock_page(page);
5831da177e4SLinus Torvalds reiserfs_put_page(page);
5841da177e4SLinus Torvalds buffer_pos += chunk;
5851da177e4SLinus Torvalds file_pos += chunk;
5861da177e4SLinus Torvalds skip = 0;
5871da177e4SLinus Torvalds if (err || buffer_size == 0 || !buffer)
5881da177e4SLinus Torvalds break;
5891da177e4SLinus Torvalds }
5901da177e4SLinus Torvalds
59148b32a35SJeff Mahoney new_size = buffer_size + sizeof(struct reiserfs_xattr_header);
5922b0143b5SDavid Howells if (!err && new_size < i_size_read(d_inode(dentry))) {
59348b32a35SJeff Mahoney struct iattr newattrs = {
59402027d42SDeepa Dinamani .ia_ctime = current_time(inode),
595fb2162dfSJeff Mahoney .ia_size = new_size,
59648b32a35SJeff Mahoney .ia_valid = ATTR_SIZE | ATTR_CTIME,
59748b32a35SJeff Mahoney };
59831370f62SFrederic Weisbecker
5995955102cSAl Viro inode_lock_nested(d_inode(dentry), I_MUTEX_XATTR);
6002b0143b5SDavid Howells inode_dio_wait(d_inode(dentry));
60131370f62SFrederic Weisbecker
602c1632a0fSChristian Brauner err = reiserfs_setattr(&nop_mnt_idmap, dentry, &newattrs);
6035955102cSAl Viro inode_unlock(d_inode(dentry));
60448b32a35SJeff Mahoney } else
60548b32a35SJeff Mahoney update_ctime(inode);
60648b32a35SJeff Mahoney out_unlock:
6078b6dd72aSJeff Mahoney up_write(&REISERFS_I(inode)->i_xattr_sem);
6083227e14cSJeff Mahoney dput(dentry);
60948b32a35SJeff Mahoney return err;
6101da177e4SLinus Torvalds }
6111da177e4SLinus Torvalds
6120ab2621eSJeff Mahoney /* We need to start a transaction to maintain lock ordering */
reiserfs_xattr_set(struct inode * inode,const char * name,const void * buffer,size_t buffer_size,int flags)6130ab2621eSJeff Mahoney int reiserfs_xattr_set(struct inode *inode, const char *name,
6140ab2621eSJeff Mahoney const void *buffer, size_t buffer_size, int flags)
61548b32a35SJeff Mahoney {
6161da177e4SLinus Torvalds
6170ab2621eSJeff Mahoney struct reiserfs_transaction_handle th;
6180ab2621eSJeff Mahoney int error, error2;
6190ab2621eSJeff Mahoney size_t jbegin_count = reiserfs_xattr_nblocks(inode, buffer_size);
6200ab2621eSJeff Mahoney
62160e4cf67SJeff Mahoney /* Check before we start a transaction and then do nothing. */
62260e4cf67SJeff Mahoney if (!d_really_is_positive(REISERFS_SB(inode->i_sb)->priv_root))
62360e4cf67SJeff Mahoney return -EOPNOTSUPP;
62460e4cf67SJeff Mahoney
6250ab2621eSJeff Mahoney if (!(flags & XATTR_REPLACE))
6260ab2621eSJeff Mahoney jbegin_count += reiserfs_xattr_jcreate_nblocks(inode);
6270ab2621eSJeff Mahoney
6280ab2621eSJeff Mahoney reiserfs_write_lock(inode->i_sb);
6290ab2621eSJeff Mahoney error = journal_begin(&th, inode->i_sb, jbegin_count);
6300ab2621eSJeff Mahoney reiserfs_write_unlock(inode->i_sb);
6314c05141dSJeff Mahoney if (error) {
6320ab2621eSJeff Mahoney return error;
6330ab2621eSJeff Mahoney }
6340ab2621eSJeff Mahoney
6350ab2621eSJeff Mahoney error = reiserfs_xattr_set_handle(&th, inode, name,
6360ab2621eSJeff Mahoney buffer, buffer_size, flags);
6370ab2621eSJeff Mahoney
6384c05141dSJeff Mahoney reiserfs_write_lock(inode->i_sb);
63958d85426SJeff Mahoney error2 = journal_end(&th);
6404c05141dSJeff Mahoney reiserfs_write_unlock(inode->i_sb);
6410ab2621eSJeff Mahoney if (error == 0)
6420ab2621eSJeff Mahoney error = error2;
6430ab2621eSJeff Mahoney
6440ab2621eSJeff Mahoney return error;
6451da177e4SLinus Torvalds }
6461da177e4SLinus Torvalds
6471da177e4SLinus Torvalds /*
6481b1dcc1bSJes Sorensen * inode->i_mutex: down
6491da177e4SLinus Torvalds */
6501da177e4SLinus Torvalds int
reiserfs_xattr_get(struct inode * inode,const char * name,void * buffer,size_t buffer_size)65148b32a35SJeff Mahoney reiserfs_xattr_get(struct inode *inode, const char *name, void *buffer,
6521da177e4SLinus Torvalds size_t buffer_size)
6531da177e4SLinus Torvalds {
6541da177e4SLinus Torvalds ssize_t err = 0;
6553227e14cSJeff Mahoney struct dentry *dentry;
6561da177e4SLinus Torvalds size_t isize;
6571da177e4SLinus Torvalds size_t file_pos = 0;
6581da177e4SLinus Torvalds size_t buffer_pos = 0;
6591da177e4SLinus Torvalds struct page *page;
6601da177e4SLinus Torvalds __u32 hash = 0;
6611da177e4SLinus Torvalds
6621da177e4SLinus Torvalds if (name == NULL)
6631da177e4SLinus Torvalds return -EINVAL;
6641da177e4SLinus Torvalds
665098297b2SJeff Mahoney /*
666098297b2SJeff Mahoney * We can't have xattrs attached to v1 items since they don't have
667098297b2SJeff Mahoney * generation numbers
668098297b2SJeff Mahoney */
6691da177e4SLinus Torvalds if (get_inode_sd_version(inode) == STAT_DATA_V1)
6701da177e4SLinus Torvalds return -EOPNOTSUPP;
6711da177e4SLinus Torvalds
672c2bb80b8SJan Kara /*
673c2bb80b8SJan Kara * priv_root needn't be initialized during mount so allow initial
674c2bb80b8SJan Kara * lookups to succeed.
675c2bb80b8SJan Kara */
676c2bb80b8SJan Kara if (!REISERFS_SB(inode->i_sb)->priv_root)
677c2bb80b8SJan Kara return 0;
678c2bb80b8SJan Kara
67948b32a35SJeff Mahoney dentry = xattr_lookup(inode, name, XATTR_REPLACE);
6803227e14cSJeff Mahoney if (IS_ERR(dentry)) {
6813227e14cSJeff Mahoney err = PTR_ERR(dentry);
6821da177e4SLinus Torvalds goto out;
6831da177e4SLinus Torvalds }
6841da177e4SLinus Torvalds
6858b6dd72aSJeff Mahoney down_read(&REISERFS_I(inode)->i_xattr_sem);
686d984561bSJeff Mahoney
6872b0143b5SDavid Howells isize = i_size_read(d_inode(dentry));
6881da177e4SLinus Torvalds
6891da177e4SLinus Torvalds /* Just return the size needed */
6901da177e4SLinus Torvalds if (buffer == NULL) {
6911da177e4SLinus Torvalds err = isize - sizeof(struct reiserfs_xattr_header);
6928b6dd72aSJeff Mahoney goto out_unlock;
6931da177e4SLinus Torvalds }
6941da177e4SLinus Torvalds
6951da177e4SLinus Torvalds if (buffer_size < isize - sizeof(struct reiserfs_xattr_header)) {
6961da177e4SLinus Torvalds err = -ERANGE;
6978b6dd72aSJeff Mahoney goto out_unlock;
6981da177e4SLinus Torvalds }
6991da177e4SLinus Torvalds
7001da177e4SLinus Torvalds while (file_pos < isize) {
7011da177e4SLinus Torvalds size_t chunk;
7021da177e4SLinus Torvalds char *data;
7031da177e4SLinus Torvalds size_t skip = 0;
704f3fb9e27SFabian Frederick
70509cbfeafSKirill A. Shutemov if (isize - file_pos > PAGE_SIZE)
70609cbfeafSKirill A. Shutemov chunk = PAGE_SIZE;
7071da177e4SLinus Torvalds else
7081da177e4SLinus Torvalds chunk = isize - file_pos;
7091da177e4SLinus Torvalds
7102b0143b5SDavid Howells page = reiserfs_get_page(d_inode(dentry), file_pos);
7111da177e4SLinus Torvalds if (IS_ERR(page)) {
7121da177e4SLinus Torvalds err = PTR_ERR(page);
7138b6dd72aSJeff Mahoney goto out_unlock;
7141da177e4SLinus Torvalds }
7151da177e4SLinus Torvalds
7161da177e4SLinus Torvalds lock_page(page);
7171da177e4SLinus Torvalds data = page_address(page);
7181da177e4SLinus Torvalds if (file_pos == 0) {
7191da177e4SLinus Torvalds struct reiserfs_xattr_header *rxh =
7201da177e4SLinus Torvalds (struct reiserfs_xattr_header *)data;
7211da177e4SLinus Torvalds skip = file_pos = sizeof(struct reiserfs_xattr_header);
7221da177e4SLinus Torvalds chunk -= skip;
7231da177e4SLinus Torvalds /* Magic doesn't match up.. */
7241da177e4SLinus Torvalds if (rxh->h_magic != cpu_to_le32(REISERFS_XATTR_MAGIC)) {
7251da177e4SLinus Torvalds unlock_page(page);
7261da177e4SLinus Torvalds reiserfs_put_page(page);
72745b03d5eSJeff Mahoney reiserfs_warning(inode->i_sb, "jdm-20001",
728bd4c625cSLinus Torvalds "Invalid magic for xattr (%s) "
7291da177e4SLinus Torvalds "associated with %k", name,
7301da177e4SLinus Torvalds INODE_PKEY(inode));
7311da177e4SLinus Torvalds err = -EIO;
7328b6dd72aSJeff Mahoney goto out_unlock;
7331da177e4SLinus Torvalds }
7341da177e4SLinus Torvalds hash = le32_to_cpu(rxh->h_hash);
7351da177e4SLinus Torvalds }
7361da177e4SLinus Torvalds memcpy(buffer + buffer_pos, data + skip, chunk);
7371da177e4SLinus Torvalds unlock_page(page);
7381da177e4SLinus Torvalds reiserfs_put_page(page);
7391da177e4SLinus Torvalds file_pos += chunk;
7401da177e4SLinus Torvalds buffer_pos += chunk;
7411da177e4SLinus Torvalds skip = 0;
7421da177e4SLinus Torvalds }
7431da177e4SLinus Torvalds err = isize - sizeof(struct reiserfs_xattr_header);
7441da177e4SLinus Torvalds
745bd4c625cSLinus Torvalds if (xattr_hash(buffer, isize - sizeof(struct reiserfs_xattr_header)) !=
746bd4c625cSLinus Torvalds hash) {
74745b03d5eSJeff Mahoney reiserfs_warning(inode->i_sb, "jdm-20002",
748bd4c625cSLinus Torvalds "Invalid hash for xattr (%s) associated "
7491da177e4SLinus Torvalds "with %k", name, INODE_PKEY(inode));
7501da177e4SLinus Torvalds err = -EIO;
7511da177e4SLinus Torvalds }
7521da177e4SLinus Torvalds
7538b6dd72aSJeff Mahoney out_unlock:
7548b6dd72aSJeff Mahoney up_read(&REISERFS_I(inode)->i_xattr_sem);
7553227e14cSJeff Mahoney dput(dentry);
7561da177e4SLinus Torvalds
7571da177e4SLinus Torvalds out:
7581da177e4SLinus Torvalds return err;
7591da177e4SLinus Torvalds }
7601da177e4SLinus Torvalds
76148b32a35SJeff Mahoney /*
76248b32a35SJeff Mahoney * In order to implement different sets of xattr operations for each xattr
76348b32a35SJeff Mahoney * prefix with the generic xattr API, a filesystem should create a
76448b32a35SJeff Mahoney * null-terminated array of struct xattr_handler (one for each prefix) and
76548b32a35SJeff Mahoney * hang a pointer to it off of the s_xattr field of the superblock.
76648b32a35SJeff Mahoney *
76748b32a35SJeff Mahoney * The generic_fooxattr() functions will use this list to dispatch xattr
76848b32a35SJeff Mahoney * operations to the correct xattr_handler.
76948b32a35SJeff Mahoney */
77048b32a35SJeff Mahoney #define for_each_xattr_handler(handlers, handler) \
77148b32a35SJeff Mahoney for ((handler) = *(handlers)++; \
77248b32a35SJeff Mahoney (handler) != NULL; \
77348b32a35SJeff Mahoney (handler) = *(handlers)++)
77448b32a35SJeff Mahoney
reiserfs_posix_acl_list(const char * name,struct dentry * dentry)775387b96a5SChristian Brauner static inline bool reiserfs_posix_acl_list(const char *name,
776387b96a5SChristian Brauner struct dentry *dentry)
77748b32a35SJeff Mahoney {
778387b96a5SChristian Brauner return (posix_acl_type(name) >= 0) &&
779387b96a5SChristian Brauner IS_POSIXACL(d_backing_inode(dentry));
780387b96a5SChristian Brauner }
78148b32a35SJeff Mahoney
782387b96a5SChristian Brauner /* This is the implementation for the xattr plugin infrastructure */
reiserfs_xattr_list(const struct xattr_handler ** handlers,const char * name,struct dentry * dentry)783387b96a5SChristian Brauner static inline bool reiserfs_xattr_list(const struct xattr_handler **handlers,
784387b96a5SChristian Brauner const char *name, struct dentry *dentry)
785387b96a5SChristian Brauner {
786387b96a5SChristian Brauner if (handlers) {
787387b96a5SChristian Brauner const struct xattr_handler *xah = NULL;
78848b32a35SJeff Mahoney
78948b32a35SJeff Mahoney for_each_xattr_handler(handlers, xah) {
79098e9cb57SAndreas Gruenbacher const char *prefix = xattr_prefix(xah);
791387b96a5SChristian Brauner
792387b96a5SChristian Brauner if (strncmp(prefix, name, strlen(prefix)))
793387b96a5SChristian Brauner continue;
794387b96a5SChristian Brauner
795387b96a5SChristian Brauner if (!xattr_handler_can_list(xah, dentry))
796387b96a5SChristian Brauner return false;
797387b96a5SChristian Brauner
798387b96a5SChristian Brauner return true;
799387b96a5SChristian Brauner }
80048b32a35SJeff Mahoney }
80148b32a35SJeff Mahoney
802387b96a5SChristian Brauner return reiserfs_posix_acl_list(name, dentry);
80348b32a35SJeff Mahoney }
80448b32a35SJeff Mahoney
80548b32a35SJeff Mahoney struct listxattr_buf {
8064acf381eSAl Viro struct dir_context ctx;
80748b32a35SJeff Mahoney size_t size;
80848b32a35SJeff Mahoney size_t pos;
80948b32a35SJeff Mahoney char *buf;
810431547b3SChristoph Hellwig struct dentry *dentry;
8111da177e4SLinus Torvalds };
8121da177e4SLinus Torvalds
listxattr_filler(struct dir_context * ctx,const char * name,int namelen,loff_t offset,u64 ino,unsigned int d_type)81325885a35SAl Viro static bool listxattr_filler(struct dir_context *ctx, const char *name,
814ac7576f4SMiklos Szeredi int namelen, loff_t offset, u64 ino,
815ac7576f4SMiklos Szeredi unsigned int d_type)
8161da177e4SLinus Torvalds {
817ac7576f4SMiklos Szeredi struct listxattr_buf *b =
818ac7576f4SMiklos Szeredi container_of(ctx, struct listxattr_buf, ctx);
81948b32a35SJeff Mahoney size_t size;
820f3fb9e27SFabian Frederick
82148b32a35SJeff Mahoney if (name[0] != '.' ||
82248b32a35SJeff Mahoney (namelen != 1 && (name[1] != '.' || namelen != 2))) {
823387b96a5SChristian Brauner if (!reiserfs_xattr_list(b->dentry->d_sb->s_xattr, name,
824387b96a5SChristian Brauner b->dentry))
82525885a35SAl Viro return true;
826764a5c6bSAndreas Gruenbacher size = namelen + 1;
82748b32a35SJeff Mahoney if (b->buf) {
828a13f085dSJann Horn if (b->pos + size > b->size) {
829a13f085dSJann Horn b->pos = -ERANGE;
83025885a35SAl Viro return false;
831a13f085dSJann Horn }
832764a5c6bSAndreas Gruenbacher memcpy(b->buf + b->pos, name, namelen);
833764a5c6bSAndreas Gruenbacher b->buf[b->pos + namelen] = 0;
8341da177e4SLinus Torvalds }
83548b32a35SJeff Mahoney b->pos += size;
83648b32a35SJeff Mahoney }
83725885a35SAl Viro return true;
8381da177e4SLinus Torvalds }
839bd4c625cSLinus Torvalds
8401da177e4SLinus Torvalds /*
8411da177e4SLinus Torvalds * Inode operation listxattr()
8421da177e4SLinus Torvalds *
84348b32a35SJeff Mahoney * We totally ignore the generic listxattr here because it would be stupid
84448b32a35SJeff Mahoney * not to. Since the xattrs are organized in a directory, we can just
84548b32a35SJeff Mahoney * readdir to find them.
8461da177e4SLinus Torvalds */
reiserfs_listxattr(struct dentry * dentry,char * buffer,size_t size)847bd4c625cSLinus Torvalds ssize_t reiserfs_listxattr(struct dentry * dentry, char *buffer, size_t size)
8481da177e4SLinus Torvalds {
8491da177e4SLinus Torvalds struct dentry *dir;
8501da177e4SLinus Torvalds int err = 0;
85148b32a35SJeff Mahoney struct listxattr_buf buf = {
8524acf381eSAl Viro .ctx.actor = listxattr_filler,
853431547b3SChristoph Hellwig .dentry = dentry,
85448b32a35SJeff Mahoney .buf = buffer,
85548b32a35SJeff Mahoney .size = buffer ? size : 0,
85648b32a35SJeff Mahoney };
8571da177e4SLinus Torvalds
8582b0143b5SDavid Howells if (d_really_is_negative(dentry))
8591da177e4SLinus Torvalds return -EINVAL;
8601da177e4SLinus Torvalds
86160e4cf67SJeff Mahoney if (get_inode_sd_version(d_inode(dentry)) == STAT_DATA_V1)
8621da177e4SLinus Torvalds return -EOPNOTSUPP;
8631da177e4SLinus Torvalds
8642b0143b5SDavid Howells dir = open_xa_dir(d_inode(dentry), XATTR_REPLACE);
8651da177e4SLinus Torvalds if (IS_ERR(dir)) {
8661da177e4SLinus Torvalds err = PTR_ERR(dir);
8671da177e4SLinus Torvalds if (err == -ENODATA)
8681da177e4SLinus Torvalds err = 0; /* Not an error if there aren't any xattrs */
8691da177e4SLinus Torvalds goto out;
8701da177e4SLinus Torvalds }
8711da177e4SLinus Torvalds
8725955102cSAl Viro inode_lock_nested(d_inode(dir), I_MUTEX_XATTR);
8732b0143b5SDavid Howells err = reiserfs_readdir_inode(d_inode(dir), &buf.ctx);
8745955102cSAl Viro inode_unlock(d_inode(dir));
8751da177e4SLinus Torvalds
87648b32a35SJeff Mahoney if (!err)
87748b32a35SJeff Mahoney err = buf.pos;
8781da177e4SLinus Torvalds
8793227e14cSJeff Mahoney dput(dir);
8801da177e4SLinus Torvalds out:
8811da177e4SLinus Torvalds return err;
8821da177e4SLinus Torvalds }
8831da177e4SLinus Torvalds
create_privroot(struct dentry * dentry)884a72bdb1cSJeff Mahoney static int create_privroot(struct dentry *dentry)
8851da177e4SLinus Torvalds {
886a72bdb1cSJeff Mahoney int err;
8872b0143b5SDavid Howells struct inode *inode = d_inode(dentry->d_parent);
888f3fb9e27SFabian Frederick
8895955102cSAl Viro WARN_ON_ONCE(!inode_is_locked(inode));
8905a6059c3SJeff Mahoney
8916c17675eSJeff Mahoney err = xattr_mkdir(inode, dentry, 0700);
8922b0143b5SDavid Howells if (err || d_really_is_negative(dentry)) {
893edcc37a0SAl Viro reiserfs_warning(dentry->d_sb, "jdm-20006",
894edcc37a0SAl Viro "xattrs/ACLs enabled and couldn't "
895edcc37a0SAl Viro "find/create .reiserfs_priv. "
896edcc37a0SAl Viro "Failing mount.");
897edcc37a0SAl Viro return -EOPNOTSUPP;
898a72bdb1cSJeff Mahoney }
8991da177e4SLinus Torvalds
900d9f892b9SChristian Brauner reiserfs_init_priv_inode(d_inode(dentry));
901a72bdb1cSJeff Mahoney reiserfs_info(dentry->d_sb, "Created %s - reserved for xattr "
902a72bdb1cSJeff Mahoney "storage.\n", PRIVROOT_NAME);
9031da177e4SLinus Torvalds
904edcc37a0SAl Viro return 0;
905a72bdb1cSJeff Mahoney }
906a72bdb1cSJeff Mahoney
90712abb35aSJeff Mahoney #else
reiserfs_xattr_register_handlers(void)90812abb35aSJeff Mahoney int __init reiserfs_xattr_register_handlers(void) { return 0; }
reiserfs_xattr_unregister_handlers(void)90912abb35aSJeff Mahoney void reiserfs_xattr_unregister_handlers(void) {}
create_privroot(struct dentry * dentry)91012abb35aSJeff Mahoney static int create_privroot(struct dentry *dentry) { return 0; }
91112abb35aSJeff Mahoney #endif
91212abb35aSJeff Mahoney
91312abb35aSJeff Mahoney /* Actual operations that are exported to VFS-land */
91460e4cf67SJeff Mahoney const struct xattr_handler *reiserfs_xattr_handlers[] = {
91512abb35aSJeff Mahoney #ifdef CONFIG_REISERFS_FS_XATTR
91612abb35aSJeff Mahoney &reiserfs_xattr_user_handler,
91712abb35aSJeff Mahoney &reiserfs_xattr_trusted_handler,
91812abb35aSJeff Mahoney #endif
91912abb35aSJeff Mahoney #ifdef CONFIG_REISERFS_FS_SECURITY
92012abb35aSJeff Mahoney &reiserfs_xattr_security_handler,
92112abb35aSJeff Mahoney #endif
92212abb35aSJeff Mahoney NULL
92312abb35aSJeff Mahoney };
92412abb35aSJeff Mahoney
xattr_mount_check(struct super_block * s)925a72bdb1cSJeff Mahoney static int xattr_mount_check(struct super_block *s)
926a72bdb1cSJeff Mahoney {
927098297b2SJeff Mahoney /*
928098297b2SJeff Mahoney * We need generation numbers to ensure that the oid mapping is correct
929098297b2SJeff Mahoney * v3.5 filesystems don't have them.
930098297b2SJeff Mahoney */
93148b32a35SJeff Mahoney if (old_format_only(s)) {
93248b32a35SJeff Mahoney if (reiserfs_xattrs_optional(s)) {
933098297b2SJeff Mahoney /*
934098297b2SJeff Mahoney * Old format filesystem, but optional xattrs have
935098297b2SJeff Mahoney * been enabled. Error out.
936098297b2SJeff Mahoney */
93748b32a35SJeff Mahoney reiserfs_warning(s, "jdm-2005",
93848b32a35SJeff Mahoney "xattrs/ACLs not supported "
93948b32a35SJeff Mahoney "on pre-v3.6 format filesystems. "
94048b32a35SJeff Mahoney "Failing mount.");
941a72bdb1cSJeff Mahoney return -EOPNOTSUPP;
94248b32a35SJeff Mahoney }
943a72bdb1cSJeff Mahoney }
944a72bdb1cSJeff Mahoney
9451da177e4SLinus Torvalds return 0;
9461da177e4SLinus Torvalds }
9471da177e4SLinus Torvalds
reiserfs_permission(struct mnt_idmap * idmap,struct inode * inode,int mask)9484609e1f1SChristian Brauner int reiserfs_permission(struct mnt_idmap *idmap, struct inode *inode,
949549c7297SChristian Brauner int mask)
950b83674c0SJeff Mahoney {
951b83674c0SJeff Mahoney /*
952b83674c0SJeff Mahoney * We don't do permission checks on the internal objects.
953b83674c0SJeff Mahoney * Permissions are determined by the "owning" object.
954b83674c0SJeff Mahoney */
955b83674c0SJeff Mahoney if (IS_PRIVATE(inode))
956b83674c0SJeff Mahoney return 0;
957b83674c0SJeff Mahoney
9584609e1f1SChristian Brauner return generic_permission(&nop_mnt_idmap, inode, mask);
959b83674c0SJeff Mahoney }
960b83674c0SJeff Mahoney
xattr_hide_revalidate(struct dentry * dentry,unsigned int flags)9610b728e19SAl Viro static int xattr_hide_revalidate(struct dentry *dentry, unsigned int flags)
9621da177e4SLinus Torvalds {
963cac36f70SJeff Mahoney return -EPERM;
9641da177e4SLinus Torvalds }
9651da177e4SLinus Torvalds
966e16404edSAl Viro static const struct dentry_operations xattr_lookup_poison_ops = {
967cac36f70SJeff Mahoney .d_revalidate = xattr_hide_revalidate,
9681da177e4SLinus Torvalds };
9691da177e4SLinus Torvalds
reiserfs_lookup_privroot(struct super_block * s)970edcc37a0SAl Viro int reiserfs_lookup_privroot(struct super_block *s)
971edcc37a0SAl Viro {
972edcc37a0SAl Viro struct dentry *dentry;
973edcc37a0SAl Viro int err = 0;
974edcc37a0SAl Viro
975edcc37a0SAl Viro /* If we don't have the privroot located yet - go find it */
9765955102cSAl Viro inode_lock(d_inode(s->s_root));
977edcc37a0SAl Viro dentry = lookup_one_len(PRIVROOT_NAME, s->s_root,
978edcc37a0SAl Viro strlen(PRIVROOT_NAME));
979edcc37a0SAl Viro if (!IS_ERR(dentry)) {
980edcc37a0SAl Viro REISERFS_SB(s)->priv_root = dentry;
981fb045adbSNick Piggin d_set_d_op(dentry, &xattr_lookup_poison_ops);
982d9f892b9SChristian Brauner if (d_really_is_positive(dentry))
983d9f892b9SChristian Brauner reiserfs_init_priv_inode(d_inode(dentry));
984edcc37a0SAl Viro } else
985edcc37a0SAl Viro err = PTR_ERR(dentry);
9865955102cSAl Viro inode_unlock(d_inode(s->s_root));
987edcc37a0SAl Viro
988edcc37a0SAl Viro return err;
989edcc37a0SAl Viro }
990edcc37a0SAl Viro
991098297b2SJeff Mahoney /*
992098297b2SJeff Mahoney * We need to take a copy of the mount flags since things like
9931751e8a6SLinus Torvalds * SB_RDONLY don't get set until *after* we're called.
994098297b2SJeff Mahoney * mount_flags != mount_options
995098297b2SJeff Mahoney */
reiserfs_xattr_init(struct super_block * s,int mount_flags)996bd4c625cSLinus Torvalds int reiserfs_xattr_init(struct super_block *s, int mount_flags)
9971da177e4SLinus Torvalds {
9981da177e4SLinus Torvalds int err = 0;
999ab17c4f0SJeff Mahoney struct dentry *privroot = REISERFS_SB(s)->priv_root;
10001da177e4SLinus Torvalds
1001a72bdb1cSJeff Mahoney err = xattr_mount_check(s);
1002a72bdb1cSJeff Mahoney if (err)
10031da177e4SLinus Torvalds goto error;
10041da177e4SLinus Torvalds
10051751e8a6SLinus Torvalds if (d_really_is_negative(privroot) && !(mount_flags & SB_RDONLY)) {
10065955102cSAl Viro inode_lock(d_inode(s->s_root));
1007edcc37a0SAl Viro err = create_privroot(REISERFS_SB(s)->priv_root);
10085955102cSAl Viro inode_unlock(d_inode(s->s_root));
10091da177e4SLinus Torvalds }
1010ab17c4f0SJeff Mahoney
10112b0143b5SDavid Howells if (d_really_is_positive(privroot)) {
10125955102cSAl Viro inode_lock(d_inode(privroot));
1013ab17c4f0SJeff Mahoney if (!REISERFS_SB(s)->xattr_root) {
1014ab17c4f0SJeff Mahoney struct dentry *dentry;
1015f3fb9e27SFabian Frederick
1016ab17c4f0SJeff Mahoney dentry = lookup_one_len(XAROOT_NAME, privroot,
1017ab17c4f0SJeff Mahoney strlen(XAROOT_NAME));
1018ab17c4f0SJeff Mahoney if (!IS_ERR(dentry))
1019ab17c4f0SJeff Mahoney REISERFS_SB(s)->xattr_root = dentry;
1020ab17c4f0SJeff Mahoney else
1021ab17c4f0SJeff Mahoney err = PTR_ERR(dentry);
1022ab17c4f0SJeff Mahoney }
10235955102cSAl Viro inode_unlock(d_inode(privroot));
1024ab17c4f0SJeff Mahoney }
102548b32a35SJeff Mahoney
10261da177e4SLinus Torvalds error:
10271da177e4SLinus Torvalds if (err) {
1028a228bf8fSJeff Mahoney clear_bit(REISERFS_XATTRS_USER, &REISERFS_SB(s)->s_mount_opt);
1029a228bf8fSJeff Mahoney clear_bit(REISERFS_POSIXACL, &REISERFS_SB(s)->s_mount_opt);
10301da177e4SLinus Torvalds }
10311da177e4SLinus Torvalds
10321751e8a6SLinus Torvalds /* The super_block SB_POSIXACL must mirror the (no)acl mount option. */
10331da177e4SLinus Torvalds if (reiserfs_posixacl(s))
10341751e8a6SLinus Torvalds s->s_flags |= SB_POSIXACL;
1035ab17c4f0SJeff Mahoney else
10361751e8a6SLinus Torvalds s->s_flags &= ~SB_POSIXACL;
10371da177e4SLinus Torvalds
10381da177e4SLinus Torvalds return err;
10391da177e4SLinus Torvalds }
1040