blob: 31a3dbb120e117072a286bac95ab8190fff632ed [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/smp_lock.h>
50#include <linux/stat.h>
Jeff Mahoney6c176752009-03-30 14:02:34 -040051#include <linux/quotaops.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
86 mutex_lock_nested(&dentry->d_inode->i_mutex, I_MUTEX_CHILD);
87 error = dir->i_op->unlink(dir, dentry);
88 mutex_unlock(&dentry->d_inode->i_mutex);
89
90 if (!error)
91 d_delete(dentry);
92 return error;
93}
94
95static int xattr_rmdir(struct inode *dir, struct dentry *dentry)
96{
97 int error;
98 BUG_ON(!mutex_is_locked(&dir->i_mutex));
Linus Torvaldse1c50242009-03-30 12:29:21 -070099 vfs_dq_init(dir);
Jeff Mahoney6c176752009-03-30 14:02:34 -0400100
101 mutex_lock_nested(&dentry->d_inode->i_mutex, I_MUTEX_CHILD);
102 dentry_unhash(dentry);
103 error = dir->i_op->rmdir(dir, dentry);
104 if (!error)
105 dentry->d_inode->i_flags |= S_DEAD;
106 mutex_unlock(&dentry->d_inode->i_mutex);
107 if (!error)
108 d_delete(dentry);
109 dput(dentry);
110
111 return error;
112}
113
Jeff Mahoney6c176752009-03-30 14:02:34 -0400114#define xattr_may_create(flags) (!flags || flags & XATTR_CREATE)
115
116/* Returns and possibly creates the xattr dir. */
117static struct dentry *lookup_or_create_dir(struct dentry *parent,
118 const char *name, int flags)
119{
120 struct dentry *dentry;
121 BUG_ON(!parent);
122
Jeff Mahoney5a6059c2009-05-01 12:11:12 -0400123 mutex_lock_nested(&parent->d_inode->i_mutex, I_MUTEX_XATTR);
Jeff Mahoney6c176752009-03-30 14:02:34 -0400124 dentry = lookup_one_len(name, parent, strlen(name));
Jeff Mahoney5a6059c2009-05-01 12:11:12 -0400125 if (!IS_ERR(dentry) && !dentry->d_inode) {
Jeff Mahoney6c176752009-03-30 14:02:34 -0400126 int err = -ENODATA;
127
Jeff Mahoney5a6059c2009-05-01 12:11:12 -0400128 if (xattr_may_create(flags))
Jeff Mahoney6c176752009-03-30 14:02:34 -0400129 err = xattr_mkdir(parent->d_inode, dentry, 0700);
Jeff Mahoney6c176752009-03-30 14:02:34 -0400130
131 if (err) {
132 dput(dentry);
133 dentry = ERR_PTR(err);
134 }
135 }
Jeff Mahoney5a6059c2009-05-01 12:11:12 -0400136 mutex_unlock(&parent->d_inode->i_mutex);
Jeff Mahoney6c176752009-03-30 14:02:34 -0400137 return dentry;
138}
139
140static struct dentry *open_xa_root(struct super_block *sb, int flags)
141{
142 struct dentry *privroot = REISERFS_SB(sb)->priv_root;
143 if (!privroot)
144 return ERR_PTR(-ENODATA);
145 return lookup_or_create_dir(privroot, XAROOT_NAME, flags);
146}
147
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700148static struct dentry *open_xa_dir(const struct inode *inode, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700150 struct dentry *xaroot, *xadir;
151 char namebuf[17];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
Jeff Mahoney6c176752009-03-30 14:02:34 -0400153 xaroot = open_xa_root(inode->i_sb, flags);
Jeff Mahoney9b7f3752007-04-23 14:41:17 -0700154 if (IS_ERR(xaroot))
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700155 return xaroot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700157 snprintf(namebuf, sizeof(namebuf), "%X.%X",
158 le32_to_cpu(INODE_PKEY(inode)->k_objectid),
159 inode->i_generation);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
Jeff Mahoney6c176752009-03-30 14:02:34 -0400161 xadir = lookup_or_create_dir(xaroot, namebuf, flags);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700162 dput(xaroot);
163 return xadir;
Jeff Mahoney6c176752009-03-30 14:02:34 -0400164
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165}
166
Jeff Mahoney48b32a32009-03-30 14:02:38 -0400167/* The following are side effects of other operations that aren't explicitly
168 * modifying extended attributes. This includes operations such as permissions
169 * or ownership changes, object deletions, etc. */
Jeff Mahoneya41f1a42009-03-30 14:02:40 -0400170struct reiserfs_dentry_buf {
171 struct dentry *xadir;
172 int count;
173 struct dentry *dentries[8];
174};
Jeff Mahoney48b32a32009-03-30 14:02:38 -0400175
Jeff Mahoneya72bdb12009-03-30 14:02:33 -0400176static int
Jeff Mahoneya41f1a42009-03-30 14:02:40 -0400177fill_with_dentries(void *buf, const char *name, int namelen, loff_t offset,
178 u64 ino, unsigned int d_type)
Jeff Mahoneya72bdb12009-03-30 14:02:33 -0400179{
Jeff Mahoneya41f1a42009-03-30 14:02:40 -0400180 struct reiserfs_dentry_buf *dbuf = buf;
Jeff Mahoneya72bdb12009-03-30 14:02:33 -0400181 struct dentry *dentry;
Jeff Mahoney5a6059c2009-05-01 12:11:12 -0400182 WARN_ON_ONCE(!mutex_is_locked(&dbuf->xadir->d_inode->i_mutex));
Jeff Mahoneya72bdb12009-03-30 14:02:33 -0400183
Jeff Mahoneya41f1a42009-03-30 14:02:40 -0400184 if (dbuf->count == ARRAY_SIZE(dbuf->dentries))
185 return -ENOSPC;
186
187 if (name[0] == '.' && (name[1] == '\0' ||
188 (name[1] == '.' && name[2] == '\0')))
189 return 0;
190
191 dentry = lookup_one_len(name, dbuf->xadir, namelen);
Jeff Mahoneya72bdb12009-03-30 14:02:33 -0400192 if (IS_ERR(dentry)) {
Jeff Mahoneya41f1a42009-03-30 14:02:40 -0400193 return PTR_ERR(dentry);
Jeff Mahoneya72bdb12009-03-30 14:02:33 -0400194 } else if (!dentry->d_inode) {
Jeff Mahoneya41f1a42009-03-30 14:02:40 -0400195 /* A directory entry exists, but no file? */
196 reiserfs_error(dentry->d_sb, "xattr-20003",
197 "Corrupted directory: xattr %s listed but "
198 "not found for file %s.\n",
199 dentry->d_name.name, dbuf->xadir->d_name.name);
200 dput(dentry);
201 return -EIO;
Jeff Mahoneya72bdb12009-03-30 14:02:33 -0400202 }
203
Jeff Mahoneya41f1a42009-03-30 14:02:40 -0400204 dbuf->dentries[dbuf->count++] = dentry;
205 return 0;
Jeff Mahoneya72bdb12009-03-30 14:02:33 -0400206}
207
Jeff Mahoneya41f1a42009-03-30 14:02:40 -0400208static void
209cleanup_dentry_buf(struct reiserfs_dentry_buf *buf)
Jeff Mahoneya72bdb12009-03-30 14:02:33 -0400210{
Jeff Mahoneya41f1a42009-03-30 14:02:40 -0400211 int i;
212 for (i = 0; i < buf->count; i++)
213 if (buf->dentries[i])
214 dput(buf->dentries[i]);
215}
216
217static int reiserfs_for_each_xattr(struct inode *inode,
218 int (*action)(struct dentry *, void *),
219 void *data)
220{
221 struct dentry *dir;
222 int i, err = 0;
223 loff_t pos = 0;
224 struct reiserfs_dentry_buf buf = {
225 .count = 0,
226 };
Jeff Mahoneya72bdb12009-03-30 14:02:33 -0400227
228 /* Skip out, an xattr has no xattrs associated with it */
229 if (IS_PRIVATE(inode) || get_inode_sd_version(inode) == STAT_DATA_V1)
230 return 0;
231
Jeff Mahoney6c176752009-03-30 14:02:34 -0400232 dir = open_xa_dir(inode, XATTR_REPLACE);
Jeff Mahoneya72bdb12009-03-30 14:02:33 -0400233 if (IS_ERR(dir)) {
234 err = PTR_ERR(dir);
235 goto out;
236 } else if (!dir->d_inode) {
Jeff Mahoneya41f1a42009-03-30 14:02:40 -0400237 err = 0;
Jeff Mahoney6c176752009-03-30 14:02:34 -0400238 goto out_dir;
Jeff Mahoneya41f1a42009-03-30 14:02:40 -0400239 }
Jeff Mahoneya72bdb12009-03-30 14:02:33 -0400240
Jeff Mahoney6c176752009-03-30 14:02:34 -0400241 mutex_lock_nested(&dir->d_inode->i_mutex, I_MUTEX_XATTR);
Jeff Mahoneya41f1a42009-03-30 14:02:40 -0400242 buf.xadir = dir;
243 err = reiserfs_readdir_dentry(dir, &buf, fill_with_dentries, &pos);
244 while ((err == 0 || err == -ENOSPC) && buf.count) {
245 err = 0;
Jeff Mahoneya72bdb12009-03-30 14:02:33 -0400246
Jeff Mahoneya41f1a42009-03-30 14:02:40 -0400247 for (i = 0; i < buf.count && buf.dentries[i]; i++) {
248 int lerr = 0;
249 struct dentry *dentry = buf.dentries[i];
250
251 if (err == 0 && !S_ISDIR(dentry->d_inode->i_mode))
252 lerr = action(dentry, data);
253
254 dput(dentry);
255 buf.dentries[i] = NULL;
256 err = lerr ?: err;
257 }
258 buf.count = 0;
259 if (!err)
260 err = reiserfs_readdir_dentry(dir, &buf,
261 fill_with_dentries, &pos);
262 }
Jeff Mahoney6c176752009-03-30 14:02:34 -0400263 mutex_unlock(&dir->d_inode->i_mutex);
Jeff Mahoneya72bdb12009-03-30 14:02:33 -0400264
Jeff Mahoneya41f1a42009-03-30 14:02:40 -0400265 /* Clean up after a failed readdir */
266 cleanup_dentry_buf(&buf);
267
268 if (!err) {
269 /* We start a transaction here to avoid a ABBA situation
270 * between the xattr root's i_mutex and the journal lock.
271 * This doesn't incur much additional overhead since the
272 * new transaction will just nest inside the
273 * outer transaction. */
274 int blocks = JOURNAL_PER_BALANCE_CNT * 2 + 2 +
275 4 * REISERFS_QUOTA_TRANS_BLOCKS(inode->i_sb);
276 struct reiserfs_transaction_handle th;
277 err = journal_begin(&th, inode->i_sb, blocks);
278 if (!err) {
279 int jerror;
280 mutex_lock_nested(&dir->d_parent->d_inode->i_mutex,
281 I_MUTEX_XATTR);
282 err = action(dir, data);
283 jerror = journal_end(&th, inode->i_sb, blocks);
284 mutex_unlock(&dir->d_parent->d_inode->i_mutex);
285 err = jerror ?: err;
286 }
287 }
Jeff Mahoneya72bdb12009-03-30 14:02:33 -0400288out_dir:
289 dput(dir);
Jeff Mahoneya72bdb12009-03-30 14:02:33 -0400290out:
Jeff Mahoneya41f1a42009-03-30 14:02:40 -0400291 /* -ENODATA isn't an error */
292 if (err == -ENODATA)
293 err = 0;
294 return err;
295}
296
297static int delete_one_xattr(struct dentry *dentry, void *data)
298{
299 struct inode *dir = dentry->d_parent->d_inode;
300
301 /* This is the xattr dir, handle specially. */
302 if (S_ISDIR(dentry->d_inode->i_mode))
303 return xattr_rmdir(dir, dentry);
304
305 return xattr_unlink(dir, dentry);
306}
307
308static int chown_one_xattr(struct dentry *dentry, void *data)
309{
310 struct iattr *attrs = data;
311 return reiserfs_setattr(dentry, attrs);
312}
313
314/* No i_mutex, but the inode is unconnected. */
315int reiserfs_delete_xattrs(struct inode *inode)
316{
317 int err = reiserfs_for_each_xattr(inode, delete_one_xattr, NULL);
318 if (err)
319 reiserfs_warning(inode->i_sb, "jdm-20004",
320 "Couldn't delete all xattrs (%d)\n", err);
321 return err;
322}
323
324/* inode->i_mutex: down */
325int reiserfs_chown_xattrs(struct inode *inode, struct iattr *attrs)
326{
327 int err = reiserfs_for_each_xattr(inode, chown_one_xattr, attrs);
Jeff Mahoney8b6dd722009-03-30 14:02:36 -0400328 if (err)
329 reiserfs_warning(inode->i_sb, "jdm-20007",
330 "Couldn't chown all xattrs (%d)\n", err);
Jeff Mahoneya72bdb12009-03-30 14:02:33 -0400331 return err;
332}
333
334#ifdef CONFIG_REISERFS_FS_XATTR
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335/* Returns a dentry corresponding to a specific extended attribute file
336 * for the inode. If flags allow, the file is created. Otherwise, a
337 * valid or negative dentry, or an error is returned. */
Jeff Mahoney48b32a32009-03-30 14:02:38 -0400338static struct dentry *xattr_lookup(struct inode *inode, const char *name,
339 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700341 struct dentry *xadir, *xafile;
342 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700344 xadir = open_xa_dir(inode, flags);
Jeff Mahoney6c176752009-03-30 14:02:34 -0400345 if (IS_ERR(xadir))
David Howellse231c2e2008-02-07 00:15:26 -0800346 return ERR_CAST(xadir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
Jeff Mahoney5a6059c2009-05-01 12:11:12 -0400348 mutex_lock_nested(&xadir->d_inode->i_mutex, I_MUTEX_XATTR);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700349 xafile = lookup_one_len(name, xadir, strlen(name));
350 if (IS_ERR(xafile)) {
Jeff Mahoney6c176752009-03-30 14:02:34 -0400351 err = PTR_ERR(xafile);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700352 goto out;
Jeff Mahoney6c176752009-03-30 14:02:34 -0400353 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
Jeff Mahoney6c176752009-03-30 14:02:34 -0400355 if (xafile->d_inode && (flags & XATTR_CREATE))
356 err = -EEXIST;
357
358 if (!xafile->d_inode) {
359 err = -ENODATA;
Jeff Mahoney5a6059c2009-05-01 12:11:12 -0400360 if (xattr_may_create(flags))
Jeff Mahoney6c176752009-03-30 14:02:34 -0400361 err = xattr_create(xadir->d_inode, xafile,
362 0700|S_IFREG);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700363 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
Jeff Mahoney6c176752009-03-30 14:02:34 -0400365 if (err)
366 dput(xafile);
Jeff Mahoneya72bdb12009-03-30 14:02:33 -0400367out:
Jeff Mahoney5a6059c2009-05-01 12:11:12 -0400368 mutex_unlock(&xadir->d_inode->i_mutex);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700369 dput(xadir);
370 if (err)
Jeff Mahoney6c176752009-03-30 14:02:34 -0400371 return ERR_PTR(err);
Jeff Mahoney3227e142008-02-15 14:37:22 -0800372 return xafile;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373}
374
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375/* Internal operations on file data */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700376static inline void reiserfs_put_page(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700378 kunmap(page);
379 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380}
381
Jeff Mahoneyec6ea562009-03-30 14:02:30 -0400382static struct page *reiserfs_get_page(struct inode *dir, size_t n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700384 struct address_space *mapping = dir->i_mapping;
385 struct page *page;
386 /* We can deadlock if we try to free dentries,
387 and an unlink/rmdir has just occured - GFP_NOFS avoids this */
Al Viroc4cdd032005-10-21 03:22:39 -0400388 mapping_set_gfp_mask(mapping, GFP_NOFS);
Jeff Mahoneyec6ea562009-03-30 14:02:30 -0400389 page = read_mapping_page(mapping, n >> PAGE_CACHE_SHIFT, NULL);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700390 if (!IS_ERR(page)) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700391 kmap(page);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700392 if (PageError(page))
393 goto fail;
394 }
395 return page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700397 fail:
398 reiserfs_put_page(page);
399 return ERR_PTR(-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400}
401
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700402static inline __u32 xattr_hash(const char *msg, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700404 return csum_partial(msg, len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405}
406
Vladimir Savelievba9d8ce2007-10-16 01:25:14 -0700407int reiserfs_commit_write(struct file *f, struct page *page,
408 unsigned from, unsigned to);
409int reiserfs_prepare_write(struct file *f, struct page *page,
410 unsigned from, unsigned to);
411
Jeff Mahoney48b32a32009-03-30 14:02:38 -0400412static void update_ctime(struct inode *inode)
413{
414 struct timespec now = current_fs_time(inode->i_sb);
415 if (hlist_unhashed(&inode->i_hash) || !inode->i_nlink ||
416 timespec_equal(&inode->i_ctime, &now))
417 return;
418
419 inode->i_ctime = CURRENT_TIME_SEC;
420 mark_inode_dirty(inode);
421}
422
423static int lookup_and_delete_xattr(struct inode *inode, const char *name)
424{
425 int err = 0;
426 struct dentry *dentry, *xadir;
427
428 xadir = open_xa_dir(inode, XATTR_REPLACE);
429 if (IS_ERR(xadir))
430 return PTR_ERR(xadir);
431
Jeff Mahoney5a6059c2009-05-01 12:11:12 -0400432 mutex_lock_nested(&xadir->d_inode->i_mutex, I_MUTEX_XATTR);
Jeff Mahoney48b32a32009-03-30 14:02:38 -0400433 dentry = lookup_one_len(name, xadir, strlen(name));
434 if (IS_ERR(dentry)) {
435 err = PTR_ERR(dentry);
436 goto out_dput;
437 }
438
439 if (dentry->d_inode) {
Jeff Mahoney48b32a32009-03-30 14:02:38 -0400440 err = xattr_unlink(xadir->d_inode, dentry);
Jeff Mahoney48b32a32009-03-30 14:02:38 -0400441 update_ctime(inode);
442 }
443
444 dput(dentry);
445out_dput:
Jeff Mahoney5a6059c2009-05-01 12:11:12 -0400446 mutex_unlock(&xadir->d_inode->i_mutex);
Jeff Mahoney48b32a32009-03-30 14:02:38 -0400447 dput(xadir);
448 return err;
449}
450
Vladimir Savelievba9d8ce2007-10-16 01:25:14 -0700451
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452/* Generic extended attribute operations that can be used by xa plugins */
453
454/*
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800455 * inode->i_mutex: down
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 */
457int
Jeff Mahoney0ab26212009-03-30 14:02:39 -0400458reiserfs_xattr_set_handle(struct reiserfs_transaction_handle *th,
459 struct inode *inode, const char *name,
460 const void *buffer, size_t buffer_size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700462 int err = 0;
Jeff Mahoney3227e142008-02-15 14:37:22 -0800463 struct dentry *dentry;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700464 struct page *page;
465 char *data;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700466 size_t file_pos = 0;
467 size_t buffer_pos = 0;
Jeff Mahoney48b32a32009-03-30 14:02:38 -0400468 size_t new_size;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700469 __u32 xahash = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700471 if (get_inode_sd_version(inode) == STAT_DATA_V1)
472 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
Jeff Mahoney8b6dd722009-03-30 14:02:36 -0400474 if (!buffer)
Jeff Mahoney48b32a32009-03-30 14:02:38 -0400475 return lookup_and_delete_xattr(inode, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
Jeff Mahoney48b32a32009-03-30 14:02:38 -0400477 dentry = xattr_lookup(inode, name, flags);
478 if (IS_ERR(dentry))
479 return PTR_ERR(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
Jeff Mahoney8b6dd722009-03-30 14:02:36 -0400481 down_write(&REISERFS_I(inode)->i_xattr_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
Jeff Mahoney8b6dd722009-03-30 14:02:36 -0400483 xahash = xattr_hash(buffer, buffer_size);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700484 while (buffer_pos < buffer_size || buffer_pos == 0) {
485 size_t chunk;
486 size_t skip = 0;
487 size_t page_offset = (file_pos & (PAGE_CACHE_SIZE - 1));
488 if (buffer_size - buffer_pos > PAGE_CACHE_SIZE)
489 chunk = PAGE_CACHE_SIZE;
490 else
491 chunk = buffer_size - buffer_pos;
492
Jeff Mahoneyec6ea562009-03-30 14:02:30 -0400493 page = reiserfs_get_page(dentry->d_inode, file_pos);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700494 if (IS_ERR(page)) {
495 err = PTR_ERR(page);
Jeff Mahoney48b32a32009-03-30 14:02:38 -0400496 goto out_unlock;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700497 }
498
499 lock_page(page);
500 data = page_address(page);
501
502 if (file_pos == 0) {
503 struct reiserfs_xattr_header *rxh;
504 skip = file_pos = sizeof(struct reiserfs_xattr_header);
505 if (chunk + skip > PAGE_CACHE_SIZE)
506 chunk = PAGE_CACHE_SIZE - skip;
507 rxh = (struct reiserfs_xattr_header *)data;
508 rxh->h_magic = cpu_to_le32(REISERFS_XATTR_MAGIC);
509 rxh->h_hash = cpu_to_le32(xahash);
510 }
511
Jeff Mahoney3227e142008-02-15 14:37:22 -0800512 err = reiserfs_prepare_write(NULL, page, page_offset,
Vladimir Savelievba9d8ce2007-10-16 01:25:14 -0700513 page_offset + chunk + skip);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700514 if (!err) {
515 if (buffer)
516 memcpy(data + skip, buffer + buffer_pos, chunk);
Jeff Mahoney3227e142008-02-15 14:37:22 -0800517 err = reiserfs_commit_write(NULL, page, page_offset,
518 page_offset + chunk +
519 skip);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700520 }
521 unlock_page(page);
522 reiserfs_put_page(page);
523 buffer_pos += chunk;
524 file_pos += chunk;
525 skip = 0;
526 if (err || buffer_size == 0 || !buffer)
527 break;
528 }
529
Jeff Mahoney48b32a32009-03-30 14:02:38 -0400530 new_size = buffer_size + sizeof(struct reiserfs_xattr_header);
531 if (!err && new_size < i_size_read(dentry->d_inode)) {
532 struct iattr newattrs = {
533 .ia_ctime = current_fs_time(inode->i_sb),
534 .ia_size = buffer_size,
535 .ia_valid = ATTR_SIZE | ATTR_CTIME,
536 };
537 mutex_lock_nested(&dentry->d_inode->i_mutex, I_MUTEX_XATTR);
538 down_write(&dentry->d_inode->i_alloc_sem);
539 err = reiserfs_setattr(dentry, &newattrs);
540 up_write(&dentry->d_inode->i_alloc_sem);
541 mutex_unlock(&dentry->d_inode->i_mutex);
542 } else
543 update_ctime(inode);
544out_unlock:
Jeff Mahoney8b6dd722009-03-30 14:02:36 -0400545 up_write(&REISERFS_I(inode)->i_xattr_sem);
Jeff Mahoney3227e142008-02-15 14:37:22 -0800546 dput(dentry);
Jeff Mahoney48b32a32009-03-30 14:02:38 -0400547 return err;
548}
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700549
Jeff Mahoney0ab26212009-03-30 14:02:39 -0400550/* We need to start a transaction to maintain lock ordering */
551int reiserfs_xattr_set(struct inode *inode, const char *name,
552 const void *buffer, size_t buffer_size, int flags)
Jeff Mahoney48b32a32009-03-30 14:02:38 -0400553{
Jeff Mahoney0ab26212009-03-30 14:02:39 -0400554
555 struct reiserfs_transaction_handle th;
556 int error, error2;
557 size_t jbegin_count = reiserfs_xattr_nblocks(inode, buffer_size);
558
559 if (!(flags & XATTR_REPLACE))
560 jbegin_count += reiserfs_xattr_jcreate_nblocks(inode);
561
562 reiserfs_write_lock(inode->i_sb);
563 error = journal_begin(&th, inode->i_sb, jbegin_count);
564 if (error) {
565 reiserfs_write_unlock(inode->i_sb);
566 return error;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700567 }
568
Jeff Mahoney0ab26212009-03-30 14:02:39 -0400569 error = reiserfs_xattr_set_handle(&th, inode, name,
570 buffer, buffer_size, flags);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700571
Jeff Mahoney0ab26212009-03-30 14:02:39 -0400572 error2 = journal_end(&th, inode->i_sb, jbegin_count);
573 if (error == 0)
574 error = error2;
575 reiserfs_write_unlock(inode->i_sb);
576
577 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578}
579
580/*
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800581 * inode->i_mutex: down
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 */
583int
Jeff Mahoney48b32a32009-03-30 14:02:38 -0400584reiserfs_xattr_get(struct inode *inode, const char *name, void *buffer,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700585 size_t buffer_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700587 ssize_t err = 0;
Jeff Mahoney3227e142008-02-15 14:37:22 -0800588 struct dentry *dentry;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700589 size_t isize;
590 size_t file_pos = 0;
591 size_t buffer_pos = 0;
592 struct page *page;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700593 __u32 hash = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700595 if (name == NULL)
596 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700598 /* We can't have xattrs attached to v1 items since they don't have
599 * generation numbers */
600 if (get_inode_sd_version(inode) == STAT_DATA_V1)
601 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
Jeff Mahoney48b32a32009-03-30 14:02:38 -0400603 dentry = xattr_lookup(inode, name, XATTR_REPLACE);
Jeff Mahoney3227e142008-02-15 14:37:22 -0800604 if (IS_ERR(dentry)) {
605 err = PTR_ERR(dentry);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700606 goto out;
607 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
Jeff Mahoney8b6dd722009-03-30 14:02:36 -0400609 down_read(&REISERFS_I(inode)->i_xattr_sem);
Jeff Mahoneyd9845612009-03-30 14:02:35 -0400610
Jeff Mahoneyf437c522009-03-30 14:02:29 -0400611 isize = i_size_read(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700613 /* Just return the size needed */
614 if (buffer == NULL) {
615 err = isize - sizeof(struct reiserfs_xattr_header);
Jeff Mahoney8b6dd722009-03-30 14:02:36 -0400616 goto out_unlock;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700617 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700619 if (buffer_size < isize - sizeof(struct reiserfs_xattr_header)) {
620 err = -ERANGE;
Jeff Mahoney8b6dd722009-03-30 14:02:36 -0400621 goto out_unlock;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700622 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700624 while (file_pos < isize) {
625 size_t chunk;
626 char *data;
627 size_t skip = 0;
628 if (isize - file_pos > PAGE_CACHE_SIZE)
629 chunk = PAGE_CACHE_SIZE;
630 else
631 chunk = isize - file_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
Jeff Mahoneyec6ea562009-03-30 14:02:30 -0400633 page = reiserfs_get_page(dentry->d_inode, file_pos);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700634 if (IS_ERR(page)) {
635 err = PTR_ERR(page);
Jeff Mahoney8b6dd722009-03-30 14:02:36 -0400636 goto out_unlock;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700637 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700639 lock_page(page);
640 data = page_address(page);
641 if (file_pos == 0) {
642 struct reiserfs_xattr_header *rxh =
643 (struct reiserfs_xattr_header *)data;
644 skip = file_pos = sizeof(struct reiserfs_xattr_header);
645 chunk -= skip;
646 /* Magic doesn't match up.. */
647 if (rxh->h_magic != cpu_to_le32(REISERFS_XATTR_MAGIC)) {
648 unlock_page(page);
649 reiserfs_put_page(page);
Jeff Mahoney45b03d52009-03-30 14:02:21 -0400650 reiserfs_warning(inode->i_sb, "jdm-20001",
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700651 "Invalid magic for xattr (%s) "
652 "associated with %k", name,
653 INODE_PKEY(inode));
654 err = -EIO;
Jeff Mahoney8b6dd722009-03-30 14:02:36 -0400655 goto out_unlock;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700656 }
657 hash = le32_to_cpu(rxh->h_hash);
658 }
659 memcpy(buffer + buffer_pos, data + skip, chunk);
660 unlock_page(page);
661 reiserfs_put_page(page);
662 file_pos += chunk;
663 buffer_pos += chunk;
664 skip = 0;
665 }
666 err = isize - sizeof(struct reiserfs_xattr_header);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700668 if (xattr_hash(buffer, isize - sizeof(struct reiserfs_xattr_header)) !=
669 hash) {
Jeff Mahoney45b03d52009-03-30 14:02:21 -0400670 reiserfs_warning(inode->i_sb, "jdm-20002",
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700671 "Invalid hash for xattr (%s) associated "
672 "with %k", name, INODE_PKEY(inode));
673 err = -EIO;
674 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
Jeff Mahoney8b6dd722009-03-30 14:02:36 -0400676out_unlock:
677 up_read(&REISERFS_I(inode)->i_xattr_sem);
Jeff Mahoney3227e142008-02-15 14:37:22 -0800678 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
Jeff Mahoneya72bdb12009-03-30 14:02:33 -0400680out:
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700681 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682}
683
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684/* Actual operations that are exported to VFS-land */
Jeff Mahoney48b32a32009-03-30 14:02:38 -0400685struct xattr_handler *reiserfs_xattr_handlers[] = {
686 &reiserfs_xattr_user_handler,
687 &reiserfs_xattr_trusted_handler,
688#ifdef CONFIG_REISERFS_FS_SECURITY
689 &reiserfs_xattr_security_handler,
690#endif
691#ifdef CONFIG_REISERFS_FS_POSIX_ACL
692 &reiserfs_posix_acl_access_handler,
693 &reiserfs_posix_acl_default_handler,
694#endif
695 NULL
696};
697
698/*
699 * In order to implement different sets of xattr operations for each xattr
700 * prefix with the generic xattr API, a filesystem should create a
701 * null-terminated array of struct xattr_handler (one for each prefix) and
702 * hang a pointer to it off of the s_xattr field of the superblock.
703 *
704 * The generic_fooxattr() functions will use this list to dispatch xattr
705 * operations to the correct xattr_handler.
706 */
707#define for_each_xattr_handler(handlers, handler) \
708 for ((handler) = *(handlers)++; \
709 (handler) != NULL; \
710 (handler) = *(handlers)++)
711
712/* This is the implementation for the xattr plugin infrastructure */
713static inline struct xattr_handler *
714find_xattr_handler_prefix(struct xattr_handler **handlers,
715 const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716{
Jeff Mahoney48b32a32009-03-30 14:02:38 -0400717 struct xattr_handler *xah;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
Jeff Mahoney48b32a32009-03-30 14:02:38 -0400719 if (!handlers)
720 return NULL;
721
722 for_each_xattr_handler(handlers, xah) {
723 if (strncmp(xah->prefix, name, strlen(xah->prefix)) == 0)
724 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 }
726
Jeff Mahoney48b32a32009-03-30 14:02:38 -0400727 return xah;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728}
729
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
731/*
732 * Inode operation getxattr()
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 */
734ssize_t
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700735reiserfs_getxattr(struct dentry * dentry, const char *name, void *buffer,
736 size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737{
Jeff Mahoney48b32a32009-03-30 14:02:38 -0400738 struct inode *inode = dentry->d_inode;
739 struct xattr_handler *handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
Jeff Mahoney48b32a32009-03-30 14:02:38 -0400741 handler = find_xattr_handler_prefix(inode->i_sb->s_xattr, name);
742
743 if (!handler || get_inode_sd_version(inode) == STAT_DATA_V1)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700744 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
Jeff Mahoney48b32a32009-03-30 14:02:38 -0400746 return handler->get(inode, name, buffer, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747}
748
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749/*
750 * Inode operation setxattr()
751 *
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800752 * dentry->d_inode->i_mutex down
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 */
754int
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700755reiserfs_setxattr(struct dentry *dentry, const char *name, const void *value,
756 size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757{
Jeff Mahoney48b32a32009-03-30 14:02:38 -0400758 struct inode *inode = dentry->d_inode;
759 struct xattr_handler *handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
Jeff Mahoney48b32a32009-03-30 14:02:38 -0400761 handler = find_xattr_handler_prefix(inode->i_sb->s_xattr, name);
762
763 if (!handler || get_inode_sd_version(inode) == STAT_DATA_V1)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700764 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
Jeff Mahoney48b32a32009-03-30 14:02:38 -0400766 return handler->set(inode, name, value, size, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767}
768
769/*
770 * Inode operation removexattr()
771 *
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800772 * dentry->d_inode->i_mutex down
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700774int reiserfs_removexattr(struct dentry *dentry, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775{
Jeff Mahoney48b32a32009-03-30 14:02:38 -0400776 struct inode *inode = dentry->d_inode;
777 struct xattr_handler *handler;
778 handler = find_xattr_handler_prefix(inode->i_sb->s_xattr, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
Jeff Mahoney48b32a32009-03-30 14:02:38 -0400780 if (!handler || get_inode_sd_version(inode) == STAT_DATA_V1)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700781 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
Jeff Mahoney48b32a32009-03-30 14:02:38 -0400783 return handler->set(inode, name, NULL, 0, XATTR_REPLACE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784}
785
Jeff Mahoney48b32a32009-03-30 14:02:38 -0400786struct listxattr_buf {
787 size_t size;
788 size_t pos;
789 char *buf;
790 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791};
792
Jeff Mahoney48b32a32009-03-30 14:02:38 -0400793static int listxattr_filler(void *buf, const char *name, int namelen,
794 loff_t offset, u64 ino, unsigned int d_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795{
Jeff Mahoney48b32a32009-03-30 14:02:38 -0400796 struct listxattr_buf *b = (struct listxattr_buf *)buf;
797 size_t size;
798 if (name[0] != '.' ||
799 (namelen != 1 && (name[1] != '.' || namelen != 2))) {
800 struct xattr_handler *handler;
801 handler = find_xattr_handler_prefix(b->inode->i_sb->s_xattr,
802 name);
803 if (!handler) /* Unsupported xattr name */
804 return 0;
805 if (b->buf) {
806 size = handler->list(b->inode, b->buf + b->pos,
807 b->size, name, namelen);
808 if (size > b->size)
809 return -ERANGE;
810 } else {
811 size = handler->list(b->inode, NULL, 0, name, namelen);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700812 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
Jeff Mahoney48b32a32009-03-30 14:02:38 -0400814 b->pos += size;
815 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700816 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817}
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700818
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819/*
820 * Inode operation listxattr()
821 *
Jeff Mahoney48b32a32009-03-30 14:02:38 -0400822 * We totally ignore the generic listxattr here because it would be stupid
823 * not to. Since the xattrs are organized in a directory, we can just
824 * readdir to find them.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700826ssize_t reiserfs_listxattr(struct dentry * dentry, char *buffer, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700828 struct dentry *dir;
829 int err = 0;
Jeff Mahoneya41f1a42009-03-30 14:02:40 -0400830 loff_t pos = 0;
Jeff Mahoney48b32a32009-03-30 14:02:38 -0400831 struct listxattr_buf buf = {
832 .inode = dentry->d_inode,
833 .buf = buffer,
834 .size = buffer ? size : 0,
835 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700837 if (!dentry->d_inode)
838 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700840 if (!reiserfs_xattrs(dentry->d_sb) ||
841 get_inode_sd_version(dentry->d_inode) == STAT_DATA_V1)
842 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
Jeff Mahoney6c176752009-03-30 14:02:34 -0400844 dir = open_xa_dir(dentry->d_inode, XATTR_REPLACE);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700845 if (IS_ERR(dir)) {
846 err = PTR_ERR(dir);
847 if (err == -ENODATA)
Jeff Mahoney48b32a32009-03-30 14:02:38 -0400848 err = 0; /* Not an error if there aren't any xattrs */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700849 goto out;
850 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
Jeff Mahoney6c176752009-03-30 14:02:34 -0400852 mutex_lock_nested(&dir->d_inode->i_mutex, I_MUTEX_XATTR);
Jeff Mahoneya41f1a42009-03-30 14:02:40 -0400853 err = reiserfs_readdir_dentry(dir, &buf, listxattr_filler, &pos);
Jeff Mahoney6c176752009-03-30 14:02:34 -0400854 mutex_unlock(&dir->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
Jeff Mahoney48b32a32009-03-30 14:02:38 -0400856 if (!err)
857 err = buf.pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
Jeff Mahoney3227e142008-02-15 14:37:22 -0800859 dput(dir);
Jeff Mahoney8b6dd722009-03-30 14:02:36 -0400860out:
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700861 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862}
863
Christoph Hellwigec191572006-02-01 03:06:46 -0800864static int reiserfs_check_acl(struct inode *inode, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865{
Christoph Hellwigec191572006-02-01 03:06:46 -0800866 struct posix_acl *acl;
867 int error = -EAGAIN; /* do regular unix permission checks by default */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700868
Christoph Hellwigec191572006-02-01 03:06:46 -0800869 acl = reiserfs_get_acl(inode, ACL_TYPE_ACCESS);
870
Christoph Hellwigec191572006-02-01 03:06:46 -0800871 if (acl) {
872 if (!IS_ERR(acl)) {
873 error = posix_acl_permission(inode, acl, mask);
874 posix_acl_release(acl);
875 } else if (PTR_ERR(acl) != -ENODATA)
876 error = PTR_ERR(acl);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700877 }
878
Christoph Hellwigec191572006-02-01 03:06:46 -0800879 return error;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700880}
881
Al Viroe6305c42008-07-15 21:03:57 -0400882int reiserfs_permission(struct inode *inode, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883{
Christoph Hellwigec191572006-02-01 03:06:46 -0800884 /*
885 * We don't do permission checks on the internal objects.
886 * Permissions are determined by the "owning" object.
887 */
Jeff Mahoney6dfede62009-03-30 14:02:32 -0400888 if (IS_PRIVATE(inode))
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700889 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 /*
Christoph Hellwigec191572006-02-01 03:06:46 -0800891 * Stat data v1 doesn't support ACLs.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 */
Christoph Hellwigec191572006-02-01 03:06:46 -0800893 if (get_inode_sd_version(inode) == STAT_DATA_V1)
894 return generic_permission(inode, mask, NULL);
895 else
896 return generic_permission(inode, mask, reiserfs_check_acl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897}
Jeff Mahoneya72bdb12009-03-30 14:02:33 -0400898
899static int create_privroot(struct dentry *dentry)
900{
901 int err;
902 struct inode *inode = dentry->d_parent->d_inode;
Jeff Mahoney5a6059c2009-05-01 12:11:12 -0400903 WARN_ON_ONCE(!mutex_is_locked(&inode->i_mutex));
904
Jeff Mahoney6c176752009-03-30 14:02:34 -0400905 err = xattr_mkdir(inode, dentry, 0700);
Jeff Mahoneya72bdb12009-03-30 14:02:33 -0400906 if (err) {
907 dput(dentry);
908 dentry = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 }
910
Jeff Mahoneya72bdb12009-03-30 14:02:33 -0400911 if (dentry && dentry->d_inode)
912 reiserfs_info(dentry->d_sb, "Created %s - reserved for xattr "
913 "storage.\n", PRIVROOT_NAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
Jeff Mahoneya72bdb12009-03-30 14:02:33 -0400915 return err;
916}
917
918static int xattr_mount_check(struct super_block *s)
919{
920 /* We need generation numbers to ensure that the oid mapping is correct
921 * v3.5 filesystems don't have them. */
Jeff Mahoney48b32a32009-03-30 14:02:38 -0400922 if (old_format_only(s)) {
923 if (reiserfs_xattrs_optional(s)) {
924 /* Old format filesystem, but optional xattrs have
925 * been enabled. Error out. */
926 reiserfs_warning(s, "jdm-2005",
927 "xattrs/ACLs not supported "
928 "on pre-v3.6 format filesystems. "
929 "Failing mount.");
930 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 }
932 }
933
Jeff Mahoneya72bdb12009-03-30 14:02:33 -0400934 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935}
936
Jeff Mahoneya72bdb12009-03-30 14:02:33 -0400937#else
938int __init reiserfs_xattr_register_handlers(void) { return 0; }
939void reiserfs_xattr_unregister_handlers(void) {}
940#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
942/* This will catch lookups from the fs root to .reiserfs_priv */
943static int
944xattr_lookup_poison(struct dentry *dentry, struct qstr *q1, struct qstr *name)
945{
946 struct dentry *priv_root = REISERFS_SB(dentry->d_sb)->priv_root;
947 if (name->len == priv_root->d_name.len &&
948 name->hash == priv_root->d_name.hash &&
949 !memcmp(name->name, priv_root->d_name.name, name->len)) {
950 return -ENOENT;
951 } else if (q1->len == name->len &&
952 !memcmp(q1->name, name->name, name->len))
953 return 0;
954 return 1;
955}
956
Al Viroe16404e2009-02-20 05:55:13 +0000957static const struct dentry_operations xattr_lookup_poison_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 .d_compare = xattr_lookup_poison,
959};
960
961/* We need to take a copy of the mount flags since things like
962 * MS_RDONLY don't get set until *after* we're called.
963 * mount_flags != mount_options */
964int reiserfs_xattr_init(struct super_block *s, int mount_flags)
965{
966 int err = 0;
967
Jeff Mahoneya72bdb12009-03-30 14:02:33 -0400968#ifdef CONFIG_REISERFS_FS_XATTR
969 err = xattr_mount_check(s);
970 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 goto error;
Jeff Mahoneya72bdb12009-03-30 14:02:33 -0400972#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973
974 /* If we don't have the privroot located yet - go find it */
Jeff Mahoneya72bdb12009-03-30 14:02:33 -0400975 if (!REISERFS_SB(s)->priv_root) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 struct dentry *dentry;
Jeff Mahoney5a6059c2009-05-01 12:11:12 -0400977 mutex_lock_nested(&s->s_root->d_inode->i_mutex, I_MUTEX_CHILD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 dentry = lookup_one_len(PRIVROOT_NAME, s->s_root,
979 strlen(PRIVROOT_NAME));
980 if (!IS_ERR(dentry)) {
Jeff Mahoneya72bdb12009-03-30 14:02:33 -0400981#ifdef CONFIG_REISERFS_FS_XATTR
982 if (!(mount_flags & MS_RDONLY) && !dentry->d_inode)
983 err = create_privroot(dentry);
984#endif
985 if (!dentry->d_inode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 dput(dentry);
987 dentry = NULL;
988 }
989 } else
990 err = PTR_ERR(dentry);
Jeff Mahoney5a6059c2009-05-01 12:11:12 -0400991 mutex_unlock(&s->s_root->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992
993 if (!err && dentry) {
994 s->s_root->d_op = &xattr_lookup_poison_ops;
Jeff Mahoneya72bdb12009-03-30 14:02:33 -0400995 dentry->d_inode->i_flags |= S_PRIVATE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 REISERFS_SB(s)->priv_root = dentry;
Jeff Mahoneya72bdb12009-03-30 14:02:33 -0400997#ifdef CONFIG_REISERFS_FS_XATTR
998 /* xattrs are unavailable */
999 } else if (!(mount_flags & MS_RDONLY)) {
1000 /* If we're read-only it just means that the dir
1001 * hasn't been created. Not an error -- just no
1002 * xattrs on the fs. We'll check again if we
1003 * go read-write */
1004 reiserfs_warning(s, "jdm-20006",
1005 "xattrs/ACLs enabled and couldn't "
1006 "find/create .reiserfs_priv. "
1007 "Failing mount.");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 err = -EOPNOTSUPP;
Jeff Mahoneya72bdb12009-03-30 14:02:33 -04001009#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 }
1011 }
1012
Jeff Mahoneya72bdb12009-03-30 14:02:33 -04001013#ifdef CONFIG_REISERFS_FS_XATTR
Jeff Mahoney48b32a32009-03-30 14:02:38 -04001014 if (!err)
1015 s->s_xattr = reiserfs_xattr_handlers;
1016
Jeff Mahoneya72bdb12009-03-30 14:02:33 -04001017error:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 clear_bit(REISERFS_XATTRS_USER, &(REISERFS_SB(s)->s_mount_opt));
1020 clear_bit(REISERFS_POSIXACL, &(REISERFS_SB(s)->s_mount_opt));
1021 }
Jeff Mahoneya72bdb12009-03-30 14:02:33 -04001022#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023
1024 /* The super_block MS_POSIXACL must mirror the (no)acl mount option. */
1025 s->s_flags = s->s_flags & ~MS_POSIXACL;
Jeff Mahoneya72bdb12009-03-30 14:02:33 -04001026#ifdef CONFIG_REISERFS_FS_POSIX_ACL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 if (reiserfs_posixacl(s))
1028 s->s_flags |= MS_POSIXACL;
Jeff Mahoneya72bdb12009-03-30 14:02:33 -04001029#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030
1031 return err;
1032}