blob: 81f09fab8ae476ccbdb9547d226e397e1b8cb6d1 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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.
Jeff Mahoneyd9845612009-03-30 14:02:35 -040030 *
31 * Locking works like so:
Jeff Mahoney8b6dd722009-03-30 14:02:36 -040032 * Directory components (xattr root, xattr dir) are protectd by their i_mutex.
33 * The xattrs themselves are protected by the xattr_sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 */
35
36#include <linux/reiserfs_fs.h>
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080037#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/dcache.h>
39#include <linux/namei.h>
40#include <linux/errno.h>
41#include <linux/fs.h>
42#include <linux/file.h>
43#include <linux/pagemap.h>
44#include <linux/xattr.h>
45#include <linux/reiserfs_xattr.h>
46#include <linux/reiserfs_acl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <asm/uaccess.h>
Al Viro3277c392006-11-14 21:13:53 -080048#include <net/checksum.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <linux/stat.h>
Jeff Mahoney6c176752009-03-30 14:02:34 -040050#include <linux/quotaops.h>
Christoph Hellwig431547b2009-11-13 09:52:56 +000051#include <linux/security.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#define PRIVROOT_NAME ".reiserfs_priv"
54#define XAROOT_NAME "xattrs"
55
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Jeff Mahoney6c176752009-03-30 14:02:34 -040057/* Helpers for inode ops. We do this so that we don't have all the VFS
58 * overhead and also for proper i_mutex annotation.
59 * dir->i_mutex must be held for all of them. */
Jeff Mahoney3a355cc2009-03-30 16:49:58 -040060#ifdef CONFIG_REISERFS_FS_XATTR
Jeff Mahoney6c176752009-03-30 14:02:34 -040061static int xattr_create(struct inode *dir, struct dentry *dentry, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -070062{
Jeff Mahoney6c176752009-03-30 14:02:34 -040063 BUG_ON(!mutex_is_locked(&dir->i_mutex));
Linus Torvaldse1c50242009-03-30 12:29:21 -070064 vfs_dq_init(dir);
Jeff Mahoney6c176752009-03-30 14:02:34 -040065 return dir->i_op->create(dir, dentry, mode, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066}
Jeff Mahoney3a355cc2009-03-30 16:49:58 -040067#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Jeff Mahoney6c176752009-03-30 14:02:34 -040069static int xattr_mkdir(struct inode *dir, struct dentry *dentry, int mode)
70{
71 BUG_ON(!mutex_is_locked(&dir->i_mutex));
Linus Torvaldse1c50242009-03-30 12:29:21 -070072 vfs_dq_init(dir);
Jeff Mahoney6c176752009-03-30 14:02:34 -040073 return dir->i_op->mkdir(dir, dentry, mode);
74}
75
76/* We use I_MUTEX_CHILD here to silence lockdep. It's safe because xattr
77 * mutation ops aren't called during rename or splace, which are the
78 * only other users of I_MUTEX_CHILD. It violates the ordering, but that's
79 * better than allocating another subclass just for this code. */
80static int xattr_unlink(struct inode *dir, struct dentry *dentry)
81{
82 int error;
83 BUG_ON(!mutex_is_locked(&dir->i_mutex));
Linus Torvaldse1c50242009-03-30 12:29:21 -070084 vfs_dq_init(dir);
Jeff Mahoney6c176752009-03-30 14:02:34 -040085
Frederic Weisbecker4dd85962009-12-30 07:26:28 +010086 reiserfs_mutex_lock_nested_safe(&dentry->d_inode->i_mutex,
87 I_MUTEX_CHILD, dir->i_sb);
Jeff Mahoney6c176752009-03-30 14:02:34 -040088 error = dir->i_op->unlink(dir, dentry);
89 mutex_unlock(&dentry->d_inode->i_mutex);
90
91 if (!error)
92 d_delete(dentry);
93 return error;
94}
95
96static int xattr_rmdir(struct inode *dir, struct dentry *dentry)
97{
98 int error;
99 BUG_ON(!mutex_is_locked(&dir->i_mutex));
Linus Torvaldse1c50242009-03-30 12:29:21 -0700100 vfs_dq_init(dir);
Jeff Mahoney6c176752009-03-30 14:02:34 -0400101
Frederic Weisbecker835d5242009-12-30 07:40:39 +0100102 reiserfs_mutex_lock_nested_safe(&dentry->d_inode->i_mutex,
103 I_MUTEX_CHILD, dir->i_sb);
Jeff Mahoney6c176752009-03-30 14:02:34 -0400104 dentry_unhash(dentry);
105 error = dir->i_op->rmdir(dir, dentry);
106 if (!error)
107 dentry->d_inode->i_flags |= S_DEAD;
108 mutex_unlock(&dentry->d_inode->i_mutex);
109 if (!error)
110 d_delete(dentry);
111 dput(dentry);
112
113 return error;
114}
115
Jeff Mahoney6c176752009-03-30 14:02:34 -0400116#define xattr_may_create(flags) (!flags || flags & XATTR_CREATE)
117
Jeff Mahoney6c176752009-03-30 14:02:34 -0400118static struct dentry *open_xa_root(struct super_block *sb, int flags)
119{
120 struct dentry *privroot = REISERFS_SB(sb)->priv_root;
Jeff Mahoneyab17c4f2009-05-05 15:30:15 -0400121 struct dentry *xaroot;
122 if (!privroot->d_inode)
Jeff Mahoney6c176752009-03-30 14:02:34 -0400123 return ERR_PTR(-ENODATA);
Jeff Mahoneyab17c4f2009-05-05 15:30:15 -0400124
125 mutex_lock_nested(&privroot->d_inode->i_mutex, I_MUTEX_XATTR);
126
127 xaroot = dget(REISERFS_SB(sb)->xattr_root);
Jeff Mahoneyceb5edc2009-05-17 01:02:02 -0400128 if (!xaroot)
129 xaroot = ERR_PTR(-ENODATA);
130 else if (!xaroot->d_inode) {
Jeff Mahoneyab17c4f2009-05-05 15:30:15 -0400131 int err = -ENODATA;
132 if (xattr_may_create(flags))
133 err = xattr_mkdir(privroot->d_inode, xaroot, 0700);
134 if (err) {
135 dput(xaroot);
136 xaroot = ERR_PTR(err);
137 }
138 }
139
140 mutex_unlock(&privroot->d_inode->i_mutex);
141 return xaroot;
Jeff Mahoney6c176752009-03-30 14:02:34 -0400142}
143
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700144static struct dentry *open_xa_dir(const struct inode *inode, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700146 struct dentry *xaroot, *xadir;
147 char namebuf[17];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
Jeff Mahoney6c176752009-03-30 14:02:34 -0400149 xaroot = open_xa_root(inode->i_sb, flags);
Jeff Mahoney9b7f3752007-04-23 14:41:17 -0700150 if (IS_ERR(xaroot))
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700151 return xaroot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700153 snprintf(namebuf, sizeof(namebuf), "%X.%X",
154 le32_to_cpu(INODE_PKEY(inode)->k_objectid),
155 inode->i_generation);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
Jeff Mahoneyab17c4f2009-05-05 15:30:15 -0400157 mutex_lock_nested(&xaroot->d_inode->i_mutex, I_MUTEX_XATTR);
158
159 xadir = lookup_one_len(namebuf, xaroot, strlen(namebuf));
160 if (!IS_ERR(xadir) && !xadir->d_inode) {
161 int err = -ENODATA;
162 if (xattr_may_create(flags))
163 err = xattr_mkdir(xaroot->d_inode, xadir, 0700);
164 if (err) {
165 dput(xadir);
166 xadir = ERR_PTR(err);
167 }
168 }
169
170 mutex_unlock(&xaroot->d_inode->i_mutex);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700171 dput(xaroot);
172 return xadir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173}
174
Jeff Mahoney48b32a32009-03-30 14:02:38 -0400175/* The following are side effects of other operations that aren't explicitly
176 * modifying extended attributes. This includes operations such as permissions
177 * or ownership changes, object deletions, etc. */
Jeff Mahoneya41f1a42009-03-30 14:02:40 -0400178struct reiserfs_dentry_buf {
179 struct dentry *xadir;
180 int count;
181 struct dentry *dentries[8];
182};
Jeff Mahoney48b32a32009-03-30 14:02:38 -0400183
Jeff Mahoneya72bdb12009-03-30 14:02:33 -0400184static int
Jeff Mahoneya41f1a42009-03-30 14:02:40 -0400185fill_with_dentries(void *buf, const char *name, int namelen, loff_t offset,
186 u64 ino, unsigned int d_type)
Jeff Mahoneya72bdb12009-03-30 14:02:33 -0400187{
Jeff Mahoneya41f1a42009-03-30 14:02:40 -0400188 struct reiserfs_dentry_buf *dbuf = buf;
Jeff Mahoneya72bdb12009-03-30 14:02:33 -0400189 struct dentry *dentry;
Jeff Mahoney5a6059c2009-05-01 12:11:12 -0400190 WARN_ON_ONCE(!mutex_is_locked(&dbuf->xadir->d_inode->i_mutex));
Jeff Mahoneya72bdb12009-03-30 14:02:33 -0400191
Jeff Mahoneya41f1a42009-03-30 14:02:40 -0400192 if (dbuf->count == ARRAY_SIZE(dbuf->dentries))
193 return -ENOSPC;
194
195 if (name[0] == '.' && (name[1] == '\0' ||
196 (name[1] == '.' && name[2] == '\0')))
197 return 0;
198
199 dentry = lookup_one_len(name, dbuf->xadir, namelen);
Jeff Mahoneya72bdb12009-03-30 14:02:33 -0400200 if (IS_ERR(dentry)) {
Jeff Mahoneya41f1a42009-03-30 14:02:40 -0400201 return PTR_ERR(dentry);
Jeff Mahoneya72bdb12009-03-30 14:02:33 -0400202 } else if (!dentry->d_inode) {
Jeff Mahoneya41f1a42009-03-30 14:02:40 -0400203 /* A directory entry exists, but no file? */
204 reiserfs_error(dentry->d_sb, "xattr-20003",
205 "Corrupted directory: xattr %s listed but "
206 "not found for file %s.\n",
207 dentry->d_name.name, dbuf->xadir->d_name.name);
208 dput(dentry);
209 return -EIO;
Jeff Mahoneya72bdb12009-03-30 14:02:33 -0400210 }
211
Jeff Mahoneya41f1a42009-03-30 14:02:40 -0400212 dbuf->dentries[dbuf->count++] = dentry;
213 return 0;
Jeff Mahoneya72bdb12009-03-30 14:02:33 -0400214}
215
Jeff Mahoneya41f1a42009-03-30 14:02:40 -0400216static void
217cleanup_dentry_buf(struct reiserfs_dentry_buf *buf)
Jeff Mahoneya72bdb12009-03-30 14:02:33 -0400218{
Jeff Mahoneya41f1a42009-03-30 14:02:40 -0400219 int i;
220 for (i = 0; i < buf->count; i++)
221 if (buf->dentries[i])
222 dput(buf->dentries[i]);
223}
224
225static int reiserfs_for_each_xattr(struct inode *inode,
226 int (*action)(struct dentry *, void *),
227 void *data)
228{
229 struct dentry *dir;
230 int i, err = 0;
231 loff_t pos = 0;
232 struct reiserfs_dentry_buf buf = {
233 .count = 0,
234 };
Jeff Mahoneya72bdb12009-03-30 14:02:33 -0400235
236 /* Skip out, an xattr has no xattrs associated with it */
237 if (IS_PRIVATE(inode) || get_inode_sd_version(inode) == STAT_DATA_V1)
238 return 0;
239
Frederic Weisbecker27026a02009-12-30 05:06:21 +0100240 reiserfs_write_unlock(inode->i_sb);
Jeff Mahoney6c176752009-03-30 14:02:34 -0400241 dir = open_xa_dir(inode, XATTR_REPLACE);
Jeff Mahoneya72bdb12009-03-30 14:02:33 -0400242 if (IS_ERR(dir)) {
243 err = PTR_ERR(dir);
Frederic Weisbecker27026a02009-12-30 05:06:21 +0100244 reiserfs_write_lock(inode->i_sb);
Jeff Mahoneya72bdb12009-03-30 14:02:33 -0400245 goto out;
246 } else if (!dir->d_inode) {
Jeff Mahoneya41f1a42009-03-30 14:02:40 -0400247 err = 0;
Frederic Weisbecker27026a02009-12-30 05:06:21 +0100248 reiserfs_write_lock(inode->i_sb);
Jeff Mahoney6c176752009-03-30 14:02:34 -0400249 goto out_dir;
Jeff Mahoneya41f1a42009-03-30 14:02:40 -0400250 }
Jeff Mahoneya72bdb12009-03-30 14:02:33 -0400251
Jeff Mahoney6c176752009-03-30 14:02:34 -0400252 mutex_lock_nested(&dir->d_inode->i_mutex, I_MUTEX_XATTR);
Frederic Weisbecker27026a02009-12-30 05:06:21 +0100253
254 reiserfs_write_lock(inode->i_sb);
255
Jeff Mahoneya41f1a42009-03-30 14:02:40 -0400256 buf.xadir = dir;
257 err = reiserfs_readdir_dentry(dir, &buf, fill_with_dentries, &pos);
258 while ((err == 0 || err == -ENOSPC) && buf.count) {
259 err = 0;
Jeff Mahoneya72bdb12009-03-30 14:02:33 -0400260
Jeff Mahoneya41f1a42009-03-30 14:02:40 -0400261 for (i = 0; i < buf.count && buf.dentries[i]; i++) {
262 int lerr = 0;
263 struct dentry *dentry = buf.dentries[i];
264
265 if (err == 0 && !S_ISDIR(dentry->d_inode->i_mode))
266 lerr = action(dentry, data);
267
268 dput(dentry);
269 buf.dentries[i] = NULL;
270 err = lerr ?: err;
271 }
272 buf.count = 0;
273 if (!err)
274 err = reiserfs_readdir_dentry(dir, &buf,
275 fill_with_dentries, &pos);
276 }
Jeff Mahoney6c176752009-03-30 14:02:34 -0400277 mutex_unlock(&dir->d_inode->i_mutex);
Jeff Mahoneya72bdb12009-03-30 14:02:33 -0400278
Jeff Mahoneya41f1a42009-03-30 14:02:40 -0400279 /* Clean up after a failed readdir */
280 cleanup_dentry_buf(&buf);
281
282 if (!err) {
283 /* We start a transaction here to avoid a ABBA situation
284 * between the xattr root's i_mutex and the journal lock.
285 * This doesn't incur much additional overhead since the
286 * new transaction will just nest inside the
287 * outer transaction. */
288 int blocks = JOURNAL_PER_BALANCE_CNT * 2 + 2 +
289 4 * REISERFS_QUOTA_TRANS_BLOCKS(inode->i_sb);
290 struct reiserfs_transaction_handle th;
291 err = journal_begin(&th, inode->i_sb, blocks);
292 if (!err) {
293 int jerror;
Frederic Weisbecker8b513f52009-12-30 07:28:58 +0100294 reiserfs_mutex_lock_nested_safe(
295 &dir->d_parent->d_inode->i_mutex,
296 I_MUTEX_XATTR, inode->i_sb);
Jeff Mahoneya41f1a42009-03-30 14:02:40 -0400297 err = action(dir, data);
298 jerror = journal_end(&th, inode->i_sb, blocks);
299 mutex_unlock(&dir->d_parent->d_inode->i_mutex);
300 err = jerror ?: err;
301 }
302 }
Jeff Mahoneya72bdb12009-03-30 14:02:33 -0400303out_dir:
304 dput(dir);
Jeff Mahoneya72bdb12009-03-30 14:02:33 -0400305out:
Jeff Mahoneya41f1a42009-03-30 14:02:40 -0400306 /* -ENODATA isn't an error */
307 if (err == -ENODATA)
308 err = 0;
309 return err;
310}
311
312static int delete_one_xattr(struct dentry *dentry, void *data)
313{
314 struct inode *dir = dentry->d_parent->d_inode;
315
316 /* This is the xattr dir, handle specially. */
317 if (S_ISDIR(dentry->d_inode->i_mode))
318 return xattr_rmdir(dir, dentry);
319
320 return xattr_unlink(dir, dentry);
321}
322
323static int chown_one_xattr(struct dentry *dentry, void *data)
324{
325 struct iattr *attrs = data;
326 return reiserfs_setattr(dentry, attrs);
327}
328
329/* No i_mutex, but the inode is unconnected. */
330int reiserfs_delete_xattrs(struct inode *inode)
331{
332 int err = reiserfs_for_each_xattr(inode, delete_one_xattr, NULL);
333 if (err)
334 reiserfs_warning(inode->i_sb, "jdm-20004",
335 "Couldn't delete all xattrs (%d)\n", err);
336 return err;
337}
338
339/* inode->i_mutex: down */
340int reiserfs_chown_xattrs(struct inode *inode, struct iattr *attrs)
341{
342 int err = reiserfs_for_each_xattr(inode, chown_one_xattr, attrs);
Jeff Mahoney8b6dd722009-03-30 14:02:36 -0400343 if (err)
344 reiserfs_warning(inode->i_sb, "jdm-20007",
345 "Couldn't chown all xattrs (%d)\n", err);
Jeff Mahoneya72bdb12009-03-30 14:02:33 -0400346 return err;
347}
348
349#ifdef CONFIG_REISERFS_FS_XATTR
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350/* Returns a dentry corresponding to a specific extended attribute file
351 * for the inode. If flags allow, the file is created. Otherwise, a
352 * valid or negative dentry, or an error is returned. */
Jeff Mahoney48b32a32009-03-30 14:02:38 -0400353static struct dentry *xattr_lookup(struct inode *inode, const char *name,
354 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700356 struct dentry *xadir, *xafile;
357 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700359 xadir = open_xa_dir(inode, flags);
Jeff Mahoney6c176752009-03-30 14:02:34 -0400360 if (IS_ERR(xadir))
David Howellse231c2e2008-02-07 00:15:26 -0800361 return ERR_CAST(xadir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
Jeff Mahoney5a6059c2009-05-01 12:11:12 -0400363 mutex_lock_nested(&xadir->d_inode->i_mutex, I_MUTEX_XATTR);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700364 xafile = lookup_one_len(name, xadir, strlen(name));
365 if (IS_ERR(xafile)) {
Jeff Mahoney6c176752009-03-30 14:02:34 -0400366 err = PTR_ERR(xafile);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700367 goto out;
Jeff Mahoney6c176752009-03-30 14:02:34 -0400368 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
Jeff Mahoney6c176752009-03-30 14:02:34 -0400370 if (xafile->d_inode && (flags & XATTR_CREATE))
371 err = -EEXIST;
372
373 if (!xafile->d_inode) {
374 err = -ENODATA;
Jeff Mahoney5a6059c2009-05-01 12:11:12 -0400375 if (xattr_may_create(flags))
Jeff Mahoney6c176752009-03-30 14:02:34 -0400376 err = xattr_create(xadir->d_inode, xafile,
377 0700|S_IFREG);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700378 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
Jeff Mahoney6c176752009-03-30 14:02:34 -0400380 if (err)
381 dput(xafile);
Jeff Mahoneya72bdb12009-03-30 14:02:33 -0400382out:
Jeff Mahoney5a6059c2009-05-01 12:11:12 -0400383 mutex_unlock(&xadir->d_inode->i_mutex);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700384 dput(xadir);
385 if (err)
Jeff Mahoney6c176752009-03-30 14:02:34 -0400386 return ERR_PTR(err);
Jeff Mahoney3227e142008-02-15 14:37:22 -0800387 return xafile;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388}
389
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390/* Internal operations on file data */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700391static inline void reiserfs_put_page(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700393 kunmap(page);
394 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395}
396
Jeff Mahoneyec6ea562009-03-30 14:02:30 -0400397static struct page *reiserfs_get_page(struct inode *dir, size_t n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700399 struct address_space *mapping = dir->i_mapping;
400 struct page *page;
401 /* We can deadlock if we try to free dentries,
402 and an unlink/rmdir has just occured - GFP_NOFS avoids this */
Al Viroc4cdd032005-10-21 03:22:39 -0400403 mapping_set_gfp_mask(mapping, GFP_NOFS);
Jeff Mahoneyec6ea562009-03-30 14:02:30 -0400404 page = read_mapping_page(mapping, n >> PAGE_CACHE_SHIFT, NULL);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700405 if (!IS_ERR(page)) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700406 kmap(page);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700407 if (PageError(page))
408 goto fail;
409 }
410 return page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700412 fail:
413 reiserfs_put_page(page);
414 return ERR_PTR(-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415}
416
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700417static inline __u32 xattr_hash(const char *msg, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700419 return csum_partial(msg, len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420}
421
Vladimir Savelievba9d8ce2007-10-16 01:25:14 -0700422int reiserfs_commit_write(struct file *f, struct page *page,
423 unsigned from, unsigned to);
424int reiserfs_prepare_write(struct file *f, struct page *page,
425 unsigned from, unsigned to);
426
Jeff Mahoney48b32a32009-03-30 14:02:38 -0400427static void update_ctime(struct inode *inode)
428{
429 struct timespec now = current_fs_time(inode->i_sb);
430 if (hlist_unhashed(&inode->i_hash) || !inode->i_nlink ||
431 timespec_equal(&inode->i_ctime, &now))
432 return;
433
434 inode->i_ctime = CURRENT_TIME_SEC;
435 mark_inode_dirty(inode);
436}
437
438static int lookup_and_delete_xattr(struct inode *inode, const char *name)
439{
440 int err = 0;
441 struct dentry *dentry, *xadir;
442
443 xadir = open_xa_dir(inode, XATTR_REPLACE);
444 if (IS_ERR(xadir))
445 return PTR_ERR(xadir);
446
Jeff Mahoney5a6059c2009-05-01 12:11:12 -0400447 mutex_lock_nested(&xadir->d_inode->i_mutex, I_MUTEX_XATTR);
Jeff Mahoney48b32a32009-03-30 14:02:38 -0400448 dentry = lookup_one_len(name, xadir, strlen(name));
449 if (IS_ERR(dentry)) {
450 err = PTR_ERR(dentry);
451 goto out_dput;
452 }
453
454 if (dentry->d_inode) {
Frederic Weisbecker4f3be1b2010-01-05 02:14:30 +0100455 reiserfs_write_lock(inode->i_sb);
Jeff Mahoney48b32a32009-03-30 14:02:38 -0400456 err = xattr_unlink(xadir->d_inode, dentry);
Frederic Weisbecker4f3be1b2010-01-05 02:14:30 +0100457 reiserfs_write_unlock(inode->i_sb);
Jeff Mahoney48b32a32009-03-30 14:02:38 -0400458 update_ctime(inode);
459 }
460
461 dput(dentry);
462out_dput:
Jeff Mahoney5a6059c2009-05-01 12:11:12 -0400463 mutex_unlock(&xadir->d_inode->i_mutex);
Jeff Mahoney48b32a32009-03-30 14:02:38 -0400464 dput(xadir);
465 return err;
466}
467
Vladimir Savelievba9d8ce2007-10-16 01:25:14 -0700468
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469/* Generic extended attribute operations that can be used by xa plugins */
470
471/*
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800472 * inode->i_mutex: down
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 */
474int
Jeff Mahoney0ab26212009-03-30 14:02:39 -0400475reiserfs_xattr_set_handle(struct reiserfs_transaction_handle *th,
476 struct inode *inode, const char *name,
477 const void *buffer, size_t buffer_size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700479 int err = 0;
Jeff Mahoney3227e142008-02-15 14:37:22 -0800480 struct dentry *dentry;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700481 struct page *page;
482 char *data;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700483 size_t file_pos = 0;
484 size_t buffer_pos = 0;
Jeff Mahoney48b32a32009-03-30 14:02:38 -0400485 size_t new_size;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700486 __u32 xahash = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700488 if (get_inode_sd_version(inode) == STAT_DATA_V1)
489 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
Frederic Weisbecker3f14fea2009-12-30 07:03:53 +0100491 reiserfs_write_unlock(inode->i_sb);
Frederic Weisbecker4f3be1b2010-01-05 02:14:30 +0100492
493 if (!buffer) {
494 err = lookup_and_delete_xattr(inode, name);
495 reiserfs_write_lock(inode->i_sb);
496 return err;
497 }
498
Jeff Mahoney48b32a32009-03-30 14:02:38 -0400499 dentry = xattr_lookup(inode, name, flags);
Frederic Weisbecker3f14fea2009-12-30 07:03:53 +0100500 if (IS_ERR(dentry)) {
501 reiserfs_write_lock(inode->i_sb);
Jeff Mahoney48b32a32009-03-30 14:02:38 -0400502 return PTR_ERR(dentry);
Frederic Weisbecker3f14fea2009-12-30 07:03:53 +0100503 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
Frederic Weisbeckerf3e22f42010-01-03 03:44:53 +0100505 down_write(&REISERFS_I(inode)->i_xattr_sem);
Frederic Weisbecker3f14fea2009-12-30 07:03:53 +0100506
507 reiserfs_write_lock(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
Jeff Mahoney8b6dd722009-03-30 14:02:36 -0400509 xahash = xattr_hash(buffer, buffer_size);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700510 while (buffer_pos < buffer_size || buffer_pos == 0) {
511 size_t chunk;
512 size_t skip = 0;
513 size_t page_offset = (file_pos & (PAGE_CACHE_SIZE - 1));
514 if (buffer_size - buffer_pos > PAGE_CACHE_SIZE)
515 chunk = PAGE_CACHE_SIZE;
516 else
517 chunk = buffer_size - buffer_pos;
518
Jeff Mahoneyec6ea562009-03-30 14:02:30 -0400519 page = reiserfs_get_page(dentry->d_inode, file_pos);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700520 if (IS_ERR(page)) {
521 err = PTR_ERR(page);
Jeff Mahoney48b32a32009-03-30 14:02:38 -0400522 goto out_unlock;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700523 }
524
525 lock_page(page);
526 data = page_address(page);
527
528 if (file_pos == 0) {
529 struct reiserfs_xattr_header *rxh;
530 skip = file_pos = sizeof(struct reiserfs_xattr_header);
531 if (chunk + skip > PAGE_CACHE_SIZE)
532 chunk = PAGE_CACHE_SIZE - skip;
533 rxh = (struct reiserfs_xattr_header *)data;
534 rxh->h_magic = cpu_to_le32(REISERFS_XATTR_MAGIC);
535 rxh->h_hash = cpu_to_le32(xahash);
536 }
537
Jeff Mahoney3227e142008-02-15 14:37:22 -0800538 err = reiserfs_prepare_write(NULL, page, page_offset,
Vladimir Savelievba9d8ce2007-10-16 01:25:14 -0700539 page_offset + chunk + skip);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700540 if (!err) {
541 if (buffer)
542 memcpy(data + skip, buffer + buffer_pos, chunk);
Jeff Mahoney3227e142008-02-15 14:37:22 -0800543 err = reiserfs_commit_write(NULL, page, page_offset,
544 page_offset + chunk +
545 skip);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700546 }
547 unlock_page(page);
548 reiserfs_put_page(page);
549 buffer_pos += chunk;
550 file_pos += chunk;
551 skip = 0;
552 if (err || buffer_size == 0 || !buffer)
553 break;
554 }
555
Jeff Mahoney48b32a32009-03-30 14:02:38 -0400556 new_size = buffer_size + sizeof(struct reiserfs_xattr_header);
557 if (!err && new_size < i_size_read(dentry->d_inode)) {
558 struct iattr newattrs = {
559 .ia_ctime = current_fs_time(inode->i_sb),
560 .ia_size = buffer_size,
561 .ia_valid = ATTR_SIZE | ATTR_CTIME,
562 };
Frederic Weisbecker31370f62010-01-07 15:55:31 +0100563
564 reiserfs_write_unlock(inode->i_sb);
Jeff Mahoney48b32a32009-03-30 14:02:38 -0400565 mutex_lock_nested(&dentry->d_inode->i_mutex, I_MUTEX_XATTR);
566 down_write(&dentry->d_inode->i_alloc_sem);
Frederic Weisbecker31370f62010-01-07 15:55:31 +0100567 reiserfs_write_lock(inode->i_sb);
568
Jeff Mahoney48b32a32009-03-30 14:02:38 -0400569 err = reiserfs_setattr(dentry, &newattrs);
570 up_write(&dentry->d_inode->i_alloc_sem);
571 mutex_unlock(&dentry->d_inode->i_mutex);
572 } else
573 update_ctime(inode);
574out_unlock:
Jeff Mahoney8b6dd722009-03-30 14:02:36 -0400575 up_write(&REISERFS_I(inode)->i_xattr_sem);
Jeff Mahoney3227e142008-02-15 14:37:22 -0800576 dput(dentry);
Jeff Mahoney48b32a32009-03-30 14:02:38 -0400577 return err;
578}
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700579
Jeff Mahoney0ab26212009-03-30 14:02:39 -0400580/* We need to start a transaction to maintain lock ordering */
581int reiserfs_xattr_set(struct inode *inode, const char *name,
582 const void *buffer, size_t buffer_size, int flags)
Jeff Mahoney48b32a32009-03-30 14:02:38 -0400583{
Jeff Mahoney0ab26212009-03-30 14:02:39 -0400584
585 struct reiserfs_transaction_handle th;
586 int error, error2;
587 size_t jbegin_count = reiserfs_xattr_nblocks(inode, buffer_size);
588
589 if (!(flags & XATTR_REPLACE))
590 jbegin_count += reiserfs_xattr_jcreate_nblocks(inode);
591
592 reiserfs_write_lock(inode->i_sb);
593 error = journal_begin(&th, inode->i_sb, jbegin_count);
594 if (error) {
595 reiserfs_write_unlock(inode->i_sb);
596 return error;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700597 }
598
Jeff Mahoney0ab26212009-03-30 14:02:39 -0400599 error = reiserfs_xattr_set_handle(&th, inode, name,
600 buffer, buffer_size, flags);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700601
Jeff Mahoney0ab26212009-03-30 14:02:39 -0400602 error2 = journal_end(&th, inode->i_sb, jbegin_count);
603 if (error == 0)
604 error = error2;
605 reiserfs_write_unlock(inode->i_sb);
606
607 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608}
609
610/*
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800611 * inode->i_mutex: down
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 */
613int
Jeff Mahoney48b32a32009-03-30 14:02:38 -0400614reiserfs_xattr_get(struct inode *inode, const char *name, void *buffer,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700615 size_t buffer_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700617 ssize_t err = 0;
Jeff Mahoney3227e142008-02-15 14:37:22 -0800618 struct dentry *dentry;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700619 size_t isize;
620 size_t file_pos = 0;
621 size_t buffer_pos = 0;
622 struct page *page;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700623 __u32 hash = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700625 if (name == NULL)
626 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700628 /* 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
Jeff Mahoney48b32a32009-03-30 14:02:38 -0400633 dentry = xattr_lookup(inode, name, XATTR_REPLACE);
Jeff Mahoney3227e142008-02-15 14:37:22 -0800634 if (IS_ERR(dentry)) {
635 err = PTR_ERR(dentry);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700636 goto out;
637 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
Jeff Mahoney8b6dd722009-03-30 14:02:36 -0400639 down_read(&REISERFS_I(inode)->i_xattr_sem);
Jeff Mahoneyd9845612009-03-30 14:02:35 -0400640
Jeff Mahoneyf437c522009-03-30 14:02:29 -0400641 isize = i_size_read(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700643 /* Just return the size needed */
644 if (buffer == NULL) {
645 err = isize - sizeof(struct reiserfs_xattr_header);
Jeff Mahoney8b6dd722009-03-30 14:02:36 -0400646 goto out_unlock;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700647 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700649 if (buffer_size < isize - sizeof(struct reiserfs_xattr_header)) {
650 err = -ERANGE;
Jeff Mahoney8b6dd722009-03-30 14:02:36 -0400651 goto out_unlock;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700652 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700654 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
Jeff Mahoneyec6ea562009-03-30 14:02:30 -0400663 page = reiserfs_get_page(dentry->d_inode, file_pos);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700664 if (IS_ERR(page)) {
665 err = PTR_ERR(page);
Jeff Mahoney8b6dd722009-03-30 14:02:36 -0400666 goto out_unlock;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700667 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700669 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);
Jeff Mahoney45b03d52009-03-30 14:02:21 -0400680 reiserfs_warning(inode->i_sb, "jdm-20001",
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700681 "Invalid magic for xattr (%s) "
682 "associated with %k", name,
683 INODE_PKEY(inode));
684 err = -EIO;
Jeff Mahoney8b6dd722009-03-30 14:02:36 -0400685 goto out_unlock;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700686 }
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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700698 if (xattr_hash(buffer, isize - sizeof(struct reiserfs_xattr_header)) !=
699 hash) {
Jeff Mahoney45b03d52009-03-30 14:02:21 -0400700 reiserfs_warning(inode->i_sb, "jdm-20002",
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700701 "Invalid hash for xattr (%s) associated "
702 "with %k", name, INODE_PKEY(inode));
703 err = -EIO;
704 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
Jeff Mahoney8b6dd722009-03-30 14:02:36 -0400706out_unlock:
707 up_read(&REISERFS_I(inode)->i_xattr_sem);
Jeff Mahoney3227e142008-02-15 14:37:22 -0800708 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
Jeff Mahoneya72bdb12009-03-30 14:02:33 -0400710out:
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700711 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712}
713
Jeff Mahoney48b32a32009-03-30 14:02:38 -0400714/*
715 * In order to implement different sets of xattr operations for each xattr
716 * prefix with the generic xattr API, a filesystem should create a
717 * null-terminated array of struct xattr_handler (one for each prefix) and
718 * hang a pointer to it off of the s_xattr field of the superblock.
719 *
720 * The generic_fooxattr() functions will use this list to dispatch xattr
721 * operations to the correct xattr_handler.
722 */
723#define for_each_xattr_handler(handlers, handler) \
724 for ((handler) = *(handlers)++; \
725 (handler) != NULL; \
726 (handler) = *(handlers)++)
727
728/* This is the implementation for the xattr plugin infrastructure */
729static inline struct xattr_handler *
730find_xattr_handler_prefix(struct xattr_handler **handlers,
731 const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732{
Jeff Mahoney48b32a32009-03-30 14:02:38 -0400733 struct xattr_handler *xah;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
Jeff Mahoney48b32a32009-03-30 14:02:38 -0400735 if (!handlers)
736 return NULL;
737
738 for_each_xattr_handler(handlers, xah) {
739 if (strncmp(xah->prefix, name, strlen(xah->prefix)) == 0)
740 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 }
742
Jeff Mahoney48b32a32009-03-30 14:02:38 -0400743 return xah;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744}
745
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
747/*
748 * Inode operation getxattr()
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 */
750ssize_t
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700751reiserfs_getxattr(struct dentry * dentry, const char *name, void *buffer,
752 size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753{
Jeff Mahoney48b32a32009-03-30 14:02:38 -0400754 struct xattr_handler *handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
Christoph Hellwig431547b2009-11-13 09:52:56 +0000756 handler = find_xattr_handler_prefix(dentry->d_sb->s_xattr, name);
Jeff Mahoney48b32a32009-03-30 14:02:38 -0400757
Christoph Hellwig431547b2009-11-13 09:52:56 +0000758 if (!handler || get_inode_sd_version(dentry->d_inode) == STAT_DATA_V1)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700759 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
Christoph Hellwig431547b2009-11-13 09:52:56 +0000761 return handler->get(dentry, name, buffer, size, handler->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762}
763
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764/*
765 * Inode operation setxattr()
766 *
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800767 * dentry->d_inode->i_mutex down
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 */
769int
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700770reiserfs_setxattr(struct dentry *dentry, const char *name, const void *value,
771 size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772{
Jeff Mahoney48b32a32009-03-30 14:02:38 -0400773 struct xattr_handler *handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
Christoph Hellwig431547b2009-11-13 09:52:56 +0000775 handler = find_xattr_handler_prefix(dentry->d_sb->s_xattr, name);
Jeff Mahoney48b32a32009-03-30 14:02:38 -0400776
Christoph Hellwig431547b2009-11-13 09:52:56 +0000777 if (!handler || get_inode_sd_version(dentry->d_inode) == STAT_DATA_V1)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700778 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
Christoph Hellwig431547b2009-11-13 09:52:56 +0000780 return handler->set(dentry, name, value, size, flags, handler->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781}
782
783/*
784 * Inode operation removexattr()
785 *
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800786 * dentry->d_inode->i_mutex down
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700788int reiserfs_removexattr(struct dentry *dentry, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789{
Jeff Mahoney48b32a32009-03-30 14:02:38 -0400790 struct xattr_handler *handler;
Christoph Hellwig431547b2009-11-13 09:52:56 +0000791 handler = find_xattr_handler_prefix(dentry->d_sb->s_xattr, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
Christoph Hellwig431547b2009-11-13 09:52:56 +0000793 if (!handler || get_inode_sd_version(dentry->d_inode) == STAT_DATA_V1)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700794 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
Christoph Hellwig431547b2009-11-13 09:52:56 +0000796 return handler->set(dentry, name, NULL, 0, XATTR_REPLACE, handler->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797}
798
Jeff Mahoney48b32a32009-03-30 14:02:38 -0400799struct listxattr_buf {
800 size_t size;
801 size_t pos;
802 char *buf;
Christoph Hellwig431547b2009-11-13 09:52:56 +0000803 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804};
805
Jeff Mahoney48b32a32009-03-30 14:02:38 -0400806static int listxattr_filler(void *buf, const char *name, int namelen,
807 loff_t offset, u64 ino, unsigned int d_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808{
Jeff Mahoney48b32a32009-03-30 14:02:38 -0400809 struct listxattr_buf *b = (struct listxattr_buf *)buf;
810 size_t size;
811 if (name[0] != '.' ||
812 (namelen != 1 && (name[1] != '.' || namelen != 2))) {
813 struct xattr_handler *handler;
Christoph Hellwig431547b2009-11-13 09:52:56 +0000814 handler = find_xattr_handler_prefix(b->dentry->d_sb->s_xattr,
Jeff Mahoney48b32a32009-03-30 14:02:38 -0400815 name);
816 if (!handler) /* Unsupported xattr name */
817 return 0;
818 if (b->buf) {
Christoph Hellwig431547b2009-11-13 09:52:56 +0000819 size = handler->list(b->dentry, b->buf + b->pos,
820 b->size, name, namelen,
821 handler->flags);
Jeff Mahoney48b32a32009-03-30 14:02:38 -0400822 if (size > b->size)
823 return -ERANGE;
824 } else {
Christoph Hellwig431547b2009-11-13 09:52:56 +0000825 size = handler->list(b->dentry, NULL, 0, name,
826 namelen, handler->flags);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700827 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
Jeff Mahoney48b32a32009-03-30 14:02:38 -0400829 b->pos += size;
830 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700831 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832}
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700833
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834/*
835 * Inode operation listxattr()
836 *
Jeff Mahoney48b32a32009-03-30 14:02:38 -0400837 * We totally ignore the generic listxattr here because it would be stupid
838 * not to. Since the xattrs are organized in a directory, we can just
839 * readdir to find them.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700841ssize_t reiserfs_listxattr(struct dentry * dentry, char *buffer, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700843 struct dentry *dir;
844 int err = 0;
Jeff Mahoneya41f1a42009-03-30 14:02:40 -0400845 loff_t pos = 0;
Jeff Mahoney48b32a32009-03-30 14:02:38 -0400846 struct listxattr_buf buf = {
Christoph Hellwig431547b2009-11-13 09:52:56 +0000847 .dentry = dentry,
Jeff Mahoney48b32a32009-03-30 14:02:38 -0400848 .buf = buffer,
849 .size = buffer ? size : 0,
850 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700852 if (!dentry->d_inode)
853 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854
Jeff Mahoney677c9b22009-05-05 15:30:17 -0400855 if (!dentry->d_sb->s_xattr ||
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700856 get_inode_sd_version(dentry->d_inode) == STAT_DATA_V1)
857 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
Jeff Mahoney6c176752009-03-30 14:02:34 -0400859 dir = open_xa_dir(dentry->d_inode, XATTR_REPLACE);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700860 if (IS_ERR(dir)) {
861 err = PTR_ERR(dir);
862 if (err == -ENODATA)
Jeff Mahoney48b32a32009-03-30 14:02:38 -0400863 err = 0; /* Not an error if there aren't any xattrs */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700864 goto out;
865 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
Jeff Mahoney6c176752009-03-30 14:02:34 -0400867 mutex_lock_nested(&dir->d_inode->i_mutex, I_MUTEX_XATTR);
Jeff Mahoneya41f1a42009-03-30 14:02:40 -0400868 err = reiserfs_readdir_dentry(dir, &buf, listxattr_filler, &pos);
Jeff Mahoney6c176752009-03-30 14:02:34 -0400869 mutex_unlock(&dir->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
Jeff Mahoney48b32a32009-03-30 14:02:38 -0400871 if (!err)
872 err = buf.pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873
Jeff Mahoney3227e142008-02-15 14:37:22 -0800874 dput(dir);
Jeff Mahoney8b6dd722009-03-30 14:02:36 -0400875out:
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700876 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877}
878
Christoph Hellwigec191572006-02-01 03:06:46 -0800879static int reiserfs_check_acl(struct inode *inode, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880{
Christoph Hellwigec191572006-02-01 03:06:46 -0800881 struct posix_acl *acl;
882 int error = -EAGAIN; /* do regular unix permission checks by default */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700883
Christoph Hellwigec191572006-02-01 03:06:46 -0800884 acl = reiserfs_get_acl(inode, ACL_TYPE_ACCESS);
885
Christoph Hellwigec191572006-02-01 03:06:46 -0800886 if (acl) {
887 if (!IS_ERR(acl)) {
888 error = posix_acl_permission(inode, acl, mask);
889 posix_acl_release(acl);
890 } else if (PTR_ERR(acl) != -ENODATA)
891 error = PTR_ERR(acl);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700892 }
893
Christoph Hellwigec191572006-02-01 03:06:46 -0800894 return error;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700895}
896
Jeff Mahoneya72bdb12009-03-30 14:02:33 -0400897static int create_privroot(struct dentry *dentry)
898{
899 int err;
900 struct inode *inode = dentry->d_parent->d_inode;
Jeff Mahoney5a6059c2009-05-01 12:11:12 -0400901 WARN_ON_ONCE(!mutex_is_locked(&inode->i_mutex));
902
Jeff Mahoney6c176752009-03-30 14:02:34 -0400903 err = xattr_mkdir(inode, dentry, 0700);
Al Viroedcc37a2009-05-03 06:00:05 -0400904 if (err || !dentry->d_inode) {
905 reiserfs_warning(dentry->d_sb, "jdm-20006",
906 "xattrs/ACLs enabled and couldn't "
907 "find/create .reiserfs_priv. "
908 "Failing mount.");
909 return -EOPNOTSUPP;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700910 }
911
Al Viroedcc37a2009-05-03 06:00:05 -0400912 dentry->d_inode->i_flags |= S_PRIVATE;
913 reiserfs_info(dentry->d_sb, "Created %s - reserved for xattr "
914 "storage.\n", PRIVROOT_NAME);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700915
Al Viroedcc37a2009-05-03 06:00:05 -0400916 return 0;
Jeff Mahoneya72bdb12009-03-30 14:02:33 -0400917}
918
Jeff Mahoney12abb352009-05-17 01:02:01 -0400919#else
920int __init reiserfs_xattr_register_handlers(void) { return 0; }
921void reiserfs_xattr_unregister_handlers(void) {}
922static int create_privroot(struct dentry *dentry) { return 0; }
923#endif
924
925/* Actual operations that are exported to VFS-land */
926struct xattr_handler *reiserfs_xattr_handlers[] = {
927#ifdef CONFIG_REISERFS_FS_XATTR
928 &reiserfs_xattr_user_handler,
929 &reiserfs_xattr_trusted_handler,
930#endif
931#ifdef CONFIG_REISERFS_FS_SECURITY
932 &reiserfs_xattr_security_handler,
933#endif
934#ifdef CONFIG_REISERFS_FS_POSIX_ACL
935 &reiserfs_posix_acl_access_handler,
936 &reiserfs_posix_acl_default_handler,
937#endif
938 NULL
939};
940
Jeff Mahoneya72bdb12009-03-30 14:02:33 -0400941static int xattr_mount_check(struct super_block *s)
942{
943 /* We need generation numbers to ensure that the oid mapping is correct
944 * v3.5 filesystems don't have them. */
Jeff Mahoney48b32a32009-03-30 14:02:38 -0400945 if (old_format_only(s)) {
946 if (reiserfs_xattrs_optional(s)) {
947 /* Old format filesystem, but optional xattrs have
948 * been enabled. Error out. */
949 reiserfs_warning(s, "jdm-2005",
950 "xattrs/ACLs not supported "
951 "on pre-v3.6 format filesystems. "
952 "Failing mount.");
953 return -EOPNOTSUPP;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700954 }
955 }
956
Jeff Mahoneya72bdb12009-03-30 14:02:33 -0400957 return 0;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700958}
959
Jeff Mahoneyb83674c2009-05-17 01:02:03 -0400960int reiserfs_permission(struct inode *inode, int mask)
961{
962 /*
963 * We don't do permission checks on the internal objects.
964 * Permissions are determined by the "owning" object.
965 */
966 if (IS_PRIVATE(inode))
967 return 0;
968
969#ifdef CONFIG_REISERFS_FS_XATTR
970 /*
971 * Stat data v1 doesn't support ACLs.
972 */
973 if (get_inode_sd_version(inode) != STAT_DATA_V1)
974 return generic_permission(inode, mask, reiserfs_check_acl);
975#endif
976 return generic_permission(inode, mask, NULL);
977}
978
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979/* This will catch lookups from the fs root to .reiserfs_priv */
980static int
981xattr_lookup_poison(struct dentry *dentry, struct qstr *q1, struct qstr *name)
982{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700983 struct dentry *priv_root = REISERFS_SB(dentry->d_sb)->priv_root;
Al Viroedcc37a2009-05-03 06:00:05 -0400984 if (container_of(q1, struct dentry, d_name) == priv_root)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700985 return -ENOENT;
Al Viroedcc37a2009-05-03 06:00:05 -0400986 if (q1->len == name->len &&
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700987 !memcmp(q1->name, name->name, name->len))
988 return 0;
989 return 1;
990}
991
Al Viroe16404e2009-02-20 05:55:13 +0000992static const struct dentry_operations xattr_lookup_poison_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 .d_compare = xattr_lookup_poison,
994};
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700995
Al Viroedcc37a2009-05-03 06:00:05 -0400996int reiserfs_lookup_privroot(struct super_block *s)
997{
998 struct dentry *dentry;
999 int err = 0;
1000
1001 /* If we don't have the privroot located yet - go find it */
Frederic Weisbeckerc72e0572009-05-16 18:12:08 +02001002 reiserfs_mutex_lock_safe(&s->s_root->d_inode->i_mutex, s);
Al Viroedcc37a2009-05-03 06:00:05 -04001003 dentry = lookup_one_len(PRIVROOT_NAME, s->s_root,
1004 strlen(PRIVROOT_NAME));
1005 if (!IS_ERR(dentry)) {
1006 REISERFS_SB(s)->priv_root = dentry;
Jeff Mahoney73422812009-05-10 16:05:39 -04001007 if (!reiserfs_expose_privroot(s))
1008 s->s_root->d_op = &xattr_lookup_poison_ops;
Al Viroedcc37a2009-05-03 06:00:05 -04001009 if (dentry->d_inode)
1010 dentry->d_inode->i_flags |= S_PRIVATE;
1011 } else
1012 err = PTR_ERR(dentry);
1013 mutex_unlock(&s->s_root->d_inode->i_mutex);
1014
1015 return err;
1016}
1017
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018/* We need to take a copy of the mount flags since things like
1019 * MS_RDONLY don't get set until *after* we're called.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 * mount_flags != mount_options */
1021int reiserfs_xattr_init(struct super_block *s, int mount_flags)
1022{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001023 int err = 0;
Jeff Mahoneyab17c4f2009-05-05 15:30:15 -04001024 struct dentry *privroot = REISERFS_SB(s)->priv_root;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
Jeff Mahoneya72bdb12009-03-30 14:02:33 -04001026 err = xattr_mount_check(s);
1027 if (err)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001028 goto error;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001029
Jeff Mahoneyab17c4f2009-05-05 15:30:15 -04001030 if (!privroot->d_inode && !(mount_flags & MS_RDONLY)) {
Frederic Weisbeckerae635c02009-08-25 02:24:45 +02001031 reiserfs_mutex_lock_safe(&s->s_root->d_inode->i_mutex, s);
Al Viroedcc37a2009-05-03 06:00:05 -04001032 err = create_privroot(REISERFS_SB(s)->priv_root);
Jeff Mahoney5a6059c2009-05-01 12:11:12 -04001033 mutex_unlock(&s->s_root->d_inode->i_mutex);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001034 }
Jeff Mahoneyab17c4f2009-05-05 15:30:15 -04001035
1036 if (privroot->d_inode) {
Jeff Mahoney48b32a32009-03-30 14:02:38 -04001037 s->s_xattr = reiserfs_xattr_handlers;
Frederic Weisbeckerc72e0572009-05-16 18:12:08 +02001038 reiserfs_mutex_lock_safe(&privroot->d_inode->i_mutex, s);
Jeff Mahoneyab17c4f2009-05-05 15:30:15 -04001039 if (!REISERFS_SB(s)->xattr_root) {
1040 struct dentry *dentry;
1041 dentry = lookup_one_len(XAROOT_NAME, privroot,
1042 strlen(XAROOT_NAME));
1043 if (!IS_ERR(dentry))
1044 REISERFS_SB(s)->xattr_root = dentry;
1045 else
1046 err = PTR_ERR(dentry);
1047 }
1048 mutex_unlock(&privroot->d_inode->i_mutex);
1049 }
Jeff Mahoney48b32a32009-03-30 14:02:38 -04001050
Jeff Mahoneya72bdb12009-03-30 14:02:33 -04001051error:
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001052 if (err) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001053 clear_bit(REISERFS_XATTRS_USER, &(REISERFS_SB(s)->s_mount_opt));
1054 clear_bit(REISERFS_POSIXACL, &(REISERFS_SB(s)->s_mount_opt));
1055 }
1056
1057 /* The super_block MS_POSIXACL must mirror the (no)acl mount option. */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001058 if (reiserfs_posixacl(s))
1059 s->s_flags |= MS_POSIXACL;
Jeff Mahoneyab17c4f2009-05-05 15:30:15 -04001060 else
Jeff Mahoneyab17c4f2009-05-05 15:30:15 -04001061 s->s_flags &= ~MS_POSIXACL;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001062
1063 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064}