blob: f9bcdd5750f7be288ae7cd21fba2224a77c30f61 [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.
30 */
31
32#include <linux/reiserfs_fs.h>
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080033#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/dcache.h>
35#include <linux/namei.h>
36#include <linux/errno.h>
37#include <linux/fs.h>
38#include <linux/file.h>
39#include <linux/pagemap.h>
40#include <linux/xattr.h>
41#include <linux/reiserfs_xattr.h>
42#include <linux/reiserfs_acl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <asm/uaccess.h>
Al Viro3277c392006-11-14 21:13:53 -080044#include <net/checksum.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/smp_lock.h>
46#include <linux/stat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48#define FL_READONLY 128
49#define FL_DIR_SEM_HELD 256
50#define PRIVROOT_NAME ".reiserfs_priv"
51#define XAROOT_NAME "xattrs"
52
Jeff Mahoney9b7f3752007-04-23 14:41:17 -070053/* Returns the dentry referring to the root of the extended attribute
54 * directory tree. If it has already been retrieved, it is used. If it
55 * hasn't been created and the flags indicate creation is allowed, we
56 * attempt to create it. On error, we return a pointer-encoded error.
57 */
58static struct dentry *get_xa_root(struct super_block *sb, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070059{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070060 struct dentry *privroot = dget(REISERFS_SB(sb)->priv_root);
61 struct dentry *xaroot;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070063 /* This needs to be created at mount-time */
64 if (!privroot)
Jeff Mahoney9b7f3752007-04-23 14:41:17 -070065 return ERR_PTR(-ENODATA);
66
Jeff Mahoney1173a722007-04-30 15:09:50 -070067 mutex_lock_nested(&privroot->d_inode->i_mutex, I_MUTEX_XATTR);
Jeff Mahoney9b7f3752007-04-23 14:41:17 -070068 if (REISERFS_SB(sb)->xattr_root) {
69 xaroot = dget(REISERFS_SB(sb)->xattr_root);
70 goto out;
71 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070073 xaroot = lookup_one_len(XAROOT_NAME, privroot, strlen(XAROOT_NAME));
74 if (IS_ERR(xaroot)) {
75 goto out;
76 } else if (!xaroot->d_inode) {
Jeff Mahoney9b7f3752007-04-23 14:41:17 -070077 int err = -ENODATA;
78 if (flags == 0 || flags & XATTR_CREATE)
79 err = privroot->d_inode->i_op->mkdir(privroot->d_inode,
80 xaroot, 0700);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070081 if (err) {
82 dput(xaroot);
Jeff Mahoney9b7f3752007-04-23 14:41:17 -070083 xaroot = ERR_PTR(err);
84 goto out;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070085 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070086 }
Jeff Mahoney9b7f3752007-04-23 14:41:17 -070087 REISERFS_SB(sb)->xattr_root = dget(xaroot);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070089 out:
Jeff Mahoney9b7f3752007-04-23 14:41:17 -070090 mutex_unlock(&privroot->d_inode->i_mutex);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070091 dput(privroot);
92 return xaroot;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093}
94
Linus Torvalds1da177e2005-04-16 15:20:36 -070095/* Opens the directory corresponding to the inode's extended attribute store.
96 * If flags allow, the tree to the directory may be created. If creation is
97 * prohibited, -ENODATA is returned. */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070098static struct dentry *open_xa_dir(const struct inode *inode, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070099{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700100 struct dentry *xaroot, *xadir;
101 char namebuf[17];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
Jeff Mahoney9b7f3752007-04-23 14:41:17 -0700103 xaroot = get_xa_root(inode->i_sb, flags);
104 if (IS_ERR(xaroot))
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700105 return xaroot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700107 /* ok, we have xaroot open */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700108 snprintf(namebuf, sizeof(namebuf), "%X.%X",
109 le32_to_cpu(INODE_PKEY(inode)->k_objectid),
110 inode->i_generation);
111 xadir = lookup_one_len(namebuf, xaroot, strlen(namebuf));
112 if (IS_ERR(xadir)) {
113 dput(xaroot);
114 return xadir;
115 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700117 if (!xadir->d_inode) {
118 int err;
119 if (flags == 0 || flags & XATTR_CREATE) {
120 /* Although there is nothing else trying to create this directory,
121 * another directory with the same hash may be created, so we need
122 * to protect against that */
123 err =
124 xaroot->d_inode->i_op->mkdir(xaroot->d_inode, xadir,
125 0700);
126 if (err) {
127 dput(xaroot);
128 dput(xadir);
129 return ERR_PTR(err);
130 }
131 }
132 if (!xadir->d_inode) {
133 dput(xaroot);
134 dput(xadir);
135 return ERR_PTR(-ENODATA);
136 }
137 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700139 dput(xaroot);
140 return xadir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141}
142
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143/*
144 * this is very similar to fs/reiserfs/dir.c:reiserfs_readdir, but
145 * we need to drop the path before calling the filldir struct. That
146 * would be a big performance hit to the non-xattr case, so I've copied
147 * the whole thing for now. --clm
148 *
149 * the big difference is that I go backwards through the directory,
150 * and don't mess with f->f_pos, but the idea is the same. Do some
151 * action on each and every entry in the directory.
152 *
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800153 * we're called with i_mutex held, so there are no worries about the directory
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 * changing underneath us.
155 */
Jeff Mahoney3227e142008-02-15 14:37:22 -0800156static int __xattr_readdir(struct inode *inode, void *dirent, filldir_t filldir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700158 struct cpu_key pos_key; /* key of current position in the directory (key of directory entry) */
159 INITIALIZE_PATH(path_to_entry);
160 struct buffer_head *bh;
161 int entry_num;
162 struct item_head *ih, tmp_ih;
163 int search_res;
164 char *local_buf;
165 loff_t next_pos;
166 char small_buf[32]; /* avoid kmalloc if we can */
167 struct reiserfs_de_head *deh;
168 int d_reclen;
169 char *d_name;
170 off_t d_off;
171 ino_t d_ino;
172 struct reiserfs_dir_entry de;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700174 /* form key for search the next directory entry using f_pos field of
175 file structure */
176 next_pos = max_reiserfs_offset(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700178 while (1) {
179 research:
180 if (next_pos <= DOT_DOT_OFFSET)
181 break;
182 make_cpu_key(&pos_key, inode, next_pos, TYPE_DIRENTRY, 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700184 search_res =
185 search_by_entry_key(inode->i_sb, &pos_key, &path_to_entry,
186 &de);
187 if (search_res == IO_ERROR) {
188 // FIXME: we could just skip part of directory which could
189 // not be read
190 pathrelse(&path_to_entry);
191 return -EIO;
192 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700194 if (search_res == NAME_NOT_FOUND)
195 de.de_entry_num--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700197 set_de_name_and_namelen(&de);
198 entry_num = de.de_entry_num;
199 deh = &(de.de_deh[entry_num]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700201 bh = de.de_bh;
202 ih = de.de_ih;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700204 if (!is_direntry_le_ih(ih)) {
Jeff Mahoney0030b642009-03-30 14:02:28 -0400205 reiserfs_error(inode->i_sb, "jdm-20000",
206 "not direntry %h", ih);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700207 break;
208 }
209 copy_item_head(&tmp_ih, ih);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700211 /* we must have found item, that is item of this directory, */
212 RFALSE(COMP_SHORT_KEYS(&(ih->ih_key), &pos_key),
213 "vs-9000: found item %h does not match to dir we readdir %K",
214 ih, &pos_key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700216 if (deh_offset(deh) <= DOT_DOT_OFFSET) {
217 break;
218 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700220 /* look for the previous entry in the directory */
221 next_pos = deh_offset(deh) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700223 if (!de_visible(deh))
224 /* it is hidden entry */
225 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700227 d_reclen = entry_length(bh, ih, entry_num);
228 d_name = B_I_DEH_ENTRY_FILE_NAME(bh, ih, deh);
229 d_off = deh_offset(deh);
230 d_ino = deh_objectid(deh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700232 if (!d_name[d_reclen - 1])
233 d_reclen = strlen(d_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700235 if (d_reclen > REISERFS_MAX_NAME(inode->i_sb->s_blocksize)) {
236 /* too big to send back to VFS */
237 continue;
238 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700240 /* Ignore the .reiserfs_priv entry */
241 if (reiserfs_xattrs(inode->i_sb) &&
242 !old_format_only(inode->i_sb) &&
243 deh_objectid(deh) ==
244 le32_to_cpu(INODE_PKEY
245 (REISERFS_SB(inode->i_sb)->priv_root->d_inode)->
246 k_objectid))
247 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700249 if (d_reclen <= 32) {
250 local_buf = small_buf;
251 } else {
Pekka Enbergd739b422006-02-01 03:06:43 -0800252 local_buf = kmalloc(d_reclen, GFP_NOFS);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700253 if (!local_buf) {
254 pathrelse(&path_to_entry);
255 return -ENOMEM;
256 }
257 if (item_moved(&tmp_ih, &path_to_entry)) {
Pekka Enbergd739b422006-02-01 03:06:43 -0800258 kfree(local_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700260 /* sigh, must retry. Do this same offset again */
261 next_pos = d_off;
262 goto research;
263 }
264 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700266 // Note, that we copy name to user space via temporary
267 // buffer (local_buf) because filldir will block if
268 // user space buffer is swapped out. At that time
269 // entry can move to somewhere else
270 memcpy(local_buf, d_name, d_reclen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700272 /* the filldir function might need to start transactions,
273 * or do who knows what. Release the path now that we've
274 * copied all the important stuff out of the deh
275 */
276 pathrelse(&path_to_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700278 if (filldir(dirent, local_buf, d_reclen, d_off, d_ino,
279 DT_UNKNOWN) < 0) {
280 if (local_buf != small_buf) {
Pekka Enbergd739b422006-02-01 03:06:43 -0800281 kfree(local_buf);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700282 }
283 goto end;
284 }
285 if (local_buf != small_buf) {
Pekka Enbergd739b422006-02-01 03:06:43 -0800286 kfree(local_buf);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700287 }
288 } /* while */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700290 end:
291 pathrelse(&path_to_entry);
292 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293}
294
295/*
296 * this could be done with dedicated readdir ops for the xattr files,
297 * but I want to get something working asap
298 * this is stolen from vfs_readdir
299 *
300 */
301static
Jeff Mahoney3227e142008-02-15 14:37:22 -0800302int xattr_readdir(struct inode *inode, filldir_t filler, void *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303{
Jeff Mahoney3227e142008-02-15 14:37:22 -0800304 int res = -ENOENT;
Ingo Molnar4df46242006-08-27 01:23:56 -0700305 mutex_lock_nested(&inode->i_mutex, I_MUTEX_XATTR);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700306 if (!IS_DEADDIR(inode)) {
307 lock_kernel();
Jeff Mahoney3227e142008-02-15 14:37:22 -0800308 res = __xattr_readdir(inode, buf, filler);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700309 unlock_kernel();
310 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800311 mutex_unlock(&inode->i_mutex);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700312 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313}
314
Jeff Mahoneya72bdb12009-03-30 14:02:33 -0400315static int
316__reiserfs_xattr_del(struct dentry *xadir, const char *name, int namelen)
317{
318 struct dentry *dentry;
319 struct inode *dir = xadir->d_inode;
320 int err = 0;
321
322 dentry = lookup_one_len(name, xadir, namelen);
323 if (IS_ERR(dentry)) {
324 err = PTR_ERR(dentry);
325 goto out;
326 } else if (!dentry->d_inode) {
327 err = -ENODATA;
328 goto out_file;
329 }
330
331 /* Skip directories.. */
332 if (S_ISDIR(dentry->d_inode->i_mode))
333 goto out_file;
334
335 if (!IS_PRIVATE(dentry->d_inode)) {
336 reiserfs_error(dir->i_sb, "jdm-20003",
337 "OID %08x [%.*s/%.*s] doesn't have "
338 "priv flag set [parent is %sset].",
339 le32_to_cpu(INODE_PKEY(dentry->d_inode)->
340 k_objectid), xadir->d_name.len,
341 xadir->d_name.name, namelen, name,
342 IS_PRIVATE(xadir->d_inode) ? "" :
343 "not ");
344 dput(dentry);
345 return -EIO;
346 }
347
348 err = dir->i_op->unlink(dir, dentry);
349 if (!err)
350 d_delete(dentry);
351
352out_file:
353 dput(dentry);
354
355out:
356 return err;
357}
358
359/* The following are side effects of other operations that aren't explicitly
360 * modifying extended attributes. This includes operations such as permissions
361 * or ownership changes, object deletions, etc. */
362
363static int
364reiserfs_delete_xattrs_filler(void *buf, const char *name, int namelen,
365 loff_t offset, u64 ino, unsigned int d_type)
366{
367 struct dentry *xadir = (struct dentry *)buf;
368
369 return __reiserfs_xattr_del(xadir, name, namelen);
370
371}
372
373/* This is called w/ inode->i_mutex downed */
374int reiserfs_delete_xattrs(struct inode *inode)
375{
376 struct dentry *dir, *root;
377 int err = 0;
378
379 /* Skip out, an xattr has no xattrs associated with it */
380 if (IS_PRIVATE(inode) || get_inode_sd_version(inode) == STAT_DATA_V1)
381 return 0;
382
383 reiserfs_read_lock_xattrs(inode->i_sb);
384 dir = open_xa_dir(inode, FL_READONLY);
385 reiserfs_read_unlock_xattrs(inode->i_sb);
386 if (IS_ERR(dir)) {
387 err = PTR_ERR(dir);
388 goto out;
389 } else if (!dir->d_inode) {
390 dput(dir);
391 return 0;
392 }
393
394 lock_kernel();
395 err = xattr_readdir(dir->d_inode, reiserfs_delete_xattrs_filler, dir);
396 if (err) {
397 unlock_kernel();
398 goto out_dir;
399 }
400
401 /* Leftovers besides . and .. -- that's not good. */
402 if (dir->d_inode->i_nlink <= 2) {
403 root = get_xa_root(inode->i_sb, XATTR_REPLACE);
404 reiserfs_write_lock_xattrs(inode->i_sb);
405 err = vfs_rmdir(root->d_inode, dir);
406 reiserfs_write_unlock_xattrs(inode->i_sb);
407 dput(root);
408 } else {
409 reiserfs_warning(inode->i_sb, "jdm-20006",
410 "Couldn't remove all entries in directory");
411 }
412 unlock_kernel();
413
414out_dir:
415 dput(dir);
416
417out:
418 if (!err)
419 REISERFS_I(inode)->i_flags =
420 REISERFS_I(inode)->i_flags & ~i_has_xattr_dir;
421 return err;
422}
423
424struct reiserfs_chown_buf {
425 struct inode *inode;
426 struct dentry *xadir;
427 struct iattr *attrs;
428};
429
430/* XXX: If there is a better way to do this, I'd love to hear about it */
431static int
432reiserfs_chown_xattrs_filler(void *buf, const char *name, int namelen,
433 loff_t offset, u64 ino, unsigned int d_type)
434{
435 struct reiserfs_chown_buf *chown_buf = (struct reiserfs_chown_buf *)buf;
436 struct dentry *xafile, *xadir = chown_buf->xadir;
437 struct iattr *attrs = chown_buf->attrs;
438 int err = 0;
439
440 xafile = lookup_one_len(name, xadir, namelen);
441 if (IS_ERR(xafile))
442 return PTR_ERR(xafile);
443 else if (!xafile->d_inode) {
444 dput(xafile);
445 return -ENODATA;
446 }
447
448 if (!S_ISDIR(xafile->d_inode->i_mode))
449 err = notify_change(xafile, attrs);
450 dput(xafile);
451
452 return err;
453}
454
455int reiserfs_chown_xattrs(struct inode *inode, struct iattr *attrs)
456{
457 struct dentry *dir;
458 int err = 0;
459 struct reiserfs_chown_buf buf;
460 unsigned int ia_valid = attrs->ia_valid;
461
462 /* Skip out, an xattr has no xattrs associated with it */
463 if (IS_PRIVATE(inode) || get_inode_sd_version(inode) == STAT_DATA_V1)
464 return 0;
465
466 reiserfs_read_lock_xattrs(inode->i_sb);
467 dir = open_xa_dir(inode, FL_READONLY);
468 reiserfs_read_unlock_xattrs(inode->i_sb);
469 if (IS_ERR(dir)) {
470 if (PTR_ERR(dir) != -ENODATA)
471 err = PTR_ERR(dir);
472 goto out;
473 } else if (!dir->d_inode) {
474 dput(dir);
475 goto out;
476 }
477
478 lock_kernel();
479
480 attrs->ia_valid &= (ATTR_UID | ATTR_GID | ATTR_CTIME);
481 buf.xadir = dir;
482 buf.attrs = attrs;
483 buf.inode = inode;
484
485 err = xattr_readdir(dir->d_inode, reiserfs_chown_xattrs_filler, &buf);
486 if (err) {
487 unlock_kernel();
488 goto out_dir;
489 }
490
491 err = notify_change(dir, attrs);
492 unlock_kernel();
493
494out_dir:
495 dput(dir);
496
497out:
498 attrs->ia_valid = ia_valid;
499 return err;
500}
501
502#ifdef CONFIG_REISERFS_FS_XATTR
503static struct reiserfs_xattr_handler *find_xattr_handler_prefix(const char
504 *prefix);
505
506/* Returns a dentry corresponding to a specific extended attribute file
507 * for the inode. If flags allow, the file is created. Otherwise, a
508 * valid or negative dentry, or an error is returned. */
509static struct dentry *get_xa_file_dentry(const struct inode *inode,
510 const char *name, int flags)
511{
512 struct dentry *xadir, *xafile;
513 int err = 0;
514
515 xadir = open_xa_dir(inode, flags);
516 if (IS_ERR(xadir)) {
517 return ERR_CAST(xadir);
518 } else if (xadir && !xadir->d_inode) {
519 dput(xadir);
520 return ERR_PTR(-ENODATA);
521 }
522
523 xafile = lookup_one_len(name, xadir, strlen(name));
524 if (IS_ERR(xafile)) {
525 dput(xadir);
526 return ERR_CAST(xafile);
527 }
528
529 if (xafile->d_inode) { /* file exists */
530 if (flags & XATTR_CREATE) {
531 err = -EEXIST;
532 dput(xafile);
533 goto out;
534 }
535 } else if (flags & XATTR_REPLACE || flags & FL_READONLY) {
536 goto out;
537 } else {
538 /* inode->i_mutex is down, so nothing else can try to create
539 * the same xattr */
540 err = xadir->d_inode->i_op->create(xadir->d_inode, xafile,
541 0700 | S_IFREG, NULL);
542
543 if (err) {
544 dput(xafile);
545 goto out;
546 }
547 }
548
549out:
550 dput(xadir);
551 if (err)
552 xafile = ERR_PTR(err);
553 else if (!xafile->d_inode) {
554 dput(xafile);
555 xafile = ERR_PTR(-ENODATA);
556 }
557 return xafile;
558}
559
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560/* Internal operations on file data */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700561static inline void reiserfs_put_page(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700563 kunmap(page);
564 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565}
566
Jeff Mahoneyec6ea562009-03-30 14:02:30 -0400567static struct page *reiserfs_get_page(struct inode *dir, size_t n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700569 struct address_space *mapping = dir->i_mapping;
570 struct page *page;
571 /* We can deadlock if we try to free dentries,
572 and an unlink/rmdir has just occured - GFP_NOFS avoids this */
Al Viroc4cdd032005-10-21 03:22:39 -0400573 mapping_set_gfp_mask(mapping, GFP_NOFS);
Jeff Mahoneyec6ea562009-03-30 14:02:30 -0400574 page = read_mapping_page(mapping, n >> PAGE_CACHE_SHIFT, NULL);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700575 if (!IS_ERR(page)) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700576 kmap(page);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700577 if (PageError(page))
578 goto fail;
579 }
580 return page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700582 fail:
583 reiserfs_put_page(page);
584 return ERR_PTR(-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585}
586
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700587static inline __u32 xattr_hash(const char *msg, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700589 return csum_partial(msg, len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590}
591
Vladimir Savelievba9d8ce2007-10-16 01:25:14 -0700592int reiserfs_commit_write(struct file *f, struct page *page,
593 unsigned from, unsigned to);
594int reiserfs_prepare_write(struct file *f, struct page *page,
595 unsigned from, unsigned to);
596
597
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598/* Generic extended attribute operations that can be used by xa plugins */
599
600/*
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800601 * inode->i_mutex: down
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 */
603int
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700604reiserfs_xattr_set(struct inode *inode, const char *name, const void *buffer,
605 size_t buffer_size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700607 int err = 0;
Jeff Mahoney3227e142008-02-15 14:37:22 -0800608 struct dentry *dentry;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700609 struct page *page;
610 char *data;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700611 size_t file_pos = 0;
612 size_t buffer_pos = 0;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700613 struct iattr newattrs;
614 __u32 xahash = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700616 if (get_inode_sd_version(inode) == STAT_DATA_V1)
617 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700619 /* Empty xattrs are ok, they're just empty files, no hash */
620 if (buffer && buffer_size)
621 xahash = xattr_hash(buffer, buffer_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
Jeff Mahoney3227e142008-02-15 14:37:22 -0800623 dentry = get_xa_file_dentry(inode, name, flags);
624 if (IS_ERR(dentry)) {
625 err = PTR_ERR(dentry);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700626 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700629 REISERFS_I(inode)->i_flags |= i_has_xattr_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700631 /* Resize it so we're ok to write there */
632 newattrs.ia_size = buffer_size;
633 newattrs.ia_valid = ATTR_SIZE | ATTR_CTIME;
Jeff Mahoneyf437c522009-03-30 14:02:29 -0400634 mutex_lock_nested(&dentry->d_inode->i_mutex, I_MUTEX_XATTR);
Jeff Mahoney3227e142008-02-15 14:37:22 -0800635 err = notify_change(dentry, &newattrs);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700636 if (err)
637 goto out_filp;
638
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700639 while (buffer_pos < buffer_size || buffer_pos == 0) {
640 size_t chunk;
641 size_t skip = 0;
642 size_t page_offset = (file_pos & (PAGE_CACHE_SIZE - 1));
643 if (buffer_size - buffer_pos > PAGE_CACHE_SIZE)
644 chunk = PAGE_CACHE_SIZE;
645 else
646 chunk = buffer_size - buffer_pos;
647
Jeff Mahoneyec6ea562009-03-30 14:02:30 -0400648 page = reiserfs_get_page(dentry->d_inode, file_pos);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700649 if (IS_ERR(page)) {
650 err = PTR_ERR(page);
651 goto out_filp;
652 }
653
654 lock_page(page);
655 data = page_address(page);
656
657 if (file_pos == 0) {
658 struct reiserfs_xattr_header *rxh;
659 skip = file_pos = sizeof(struct reiserfs_xattr_header);
660 if (chunk + skip > PAGE_CACHE_SIZE)
661 chunk = PAGE_CACHE_SIZE - skip;
662 rxh = (struct reiserfs_xattr_header *)data;
663 rxh->h_magic = cpu_to_le32(REISERFS_XATTR_MAGIC);
664 rxh->h_hash = cpu_to_le32(xahash);
665 }
666
Jeff Mahoney3227e142008-02-15 14:37:22 -0800667 err = reiserfs_prepare_write(NULL, page, page_offset,
Vladimir Savelievba9d8ce2007-10-16 01:25:14 -0700668 page_offset + chunk + skip);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700669 if (!err) {
670 if (buffer)
671 memcpy(data + skip, buffer + buffer_pos, chunk);
Jeff Mahoney3227e142008-02-15 14:37:22 -0800672 err = reiserfs_commit_write(NULL, page, page_offset,
673 page_offset + chunk +
674 skip);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700675 }
676 unlock_page(page);
677 reiserfs_put_page(page);
678 buffer_pos += chunk;
679 file_pos += chunk;
680 skip = 0;
681 if (err || buffer_size == 0 || !buffer)
682 break;
683 }
684
685 /* We can't mark the inode dirty if it's not hashed. This is the case
686 * when we're inheriting the default ACL. If we dirty it, the inode
687 * gets marked dirty, but won't (ever) make it onto the dirty list until
688 * it's synced explicitly to clear I_DIRTY. This is bad. */
689 if (!hlist_unhashed(&inode->i_hash)) {
690 inode->i_ctime = CURRENT_TIME_SEC;
691 mark_inode_dirty(inode);
692 }
693
694 out_filp:
Jeff Mahoneyf437c522009-03-30 14:02:29 -0400695 mutex_unlock(&dentry->d_inode->i_mutex);
Jeff Mahoney3227e142008-02-15 14:37:22 -0800696 dput(dentry);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700697
698 out:
699 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700}
701
702/*
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800703 * inode->i_mutex: down
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 */
705int
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700706reiserfs_xattr_get(const struct inode *inode, const char *name, void *buffer,
707 size_t buffer_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700709 ssize_t err = 0;
Jeff Mahoney3227e142008-02-15 14:37:22 -0800710 struct dentry *dentry;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700711 size_t isize;
712 size_t file_pos = 0;
713 size_t buffer_pos = 0;
714 struct page *page;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700715 __u32 hash = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700717 if (name == NULL)
718 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700720 /* We can't have xattrs attached to v1 items since they don't have
721 * generation numbers */
722 if (get_inode_sd_version(inode) == STAT_DATA_V1)
723 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
Jeff Mahoney3227e142008-02-15 14:37:22 -0800725 dentry = get_xa_file_dentry(inode, name, FL_READONLY);
726 if (IS_ERR(dentry)) {
727 err = PTR_ERR(dentry);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700728 goto out;
729 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
Jeff Mahoneyf437c522009-03-30 14:02:29 -0400731 isize = i_size_read(dentry->d_inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700732 REISERFS_I(inode)->i_flags |= i_has_xattr_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700734 /* Just return the size needed */
735 if (buffer == NULL) {
736 err = isize - sizeof(struct reiserfs_xattr_header);
737 goto out_dput;
738 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700740 if (buffer_size < isize - sizeof(struct reiserfs_xattr_header)) {
741 err = -ERANGE;
742 goto out_dput;
743 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700745 while (file_pos < isize) {
746 size_t chunk;
747 char *data;
748 size_t skip = 0;
749 if (isize - file_pos > PAGE_CACHE_SIZE)
750 chunk = PAGE_CACHE_SIZE;
751 else
752 chunk = isize - file_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
Jeff Mahoneyec6ea562009-03-30 14:02:30 -0400754 page = reiserfs_get_page(dentry->d_inode, file_pos);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700755 if (IS_ERR(page)) {
756 err = PTR_ERR(page);
757 goto out_dput;
758 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700760 lock_page(page);
761 data = page_address(page);
762 if (file_pos == 0) {
763 struct reiserfs_xattr_header *rxh =
764 (struct reiserfs_xattr_header *)data;
765 skip = file_pos = sizeof(struct reiserfs_xattr_header);
766 chunk -= skip;
767 /* Magic doesn't match up.. */
768 if (rxh->h_magic != cpu_to_le32(REISERFS_XATTR_MAGIC)) {
769 unlock_page(page);
770 reiserfs_put_page(page);
Jeff Mahoney45b03d52009-03-30 14:02:21 -0400771 reiserfs_warning(inode->i_sb, "jdm-20001",
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700772 "Invalid magic for xattr (%s) "
773 "associated with %k", name,
774 INODE_PKEY(inode));
775 err = -EIO;
776 goto out_dput;
777 }
778 hash = le32_to_cpu(rxh->h_hash);
779 }
780 memcpy(buffer + buffer_pos, data + skip, chunk);
781 unlock_page(page);
782 reiserfs_put_page(page);
783 file_pos += chunk;
784 buffer_pos += chunk;
785 skip = 0;
786 }
787 err = isize - sizeof(struct reiserfs_xattr_header);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700789 if (xattr_hash(buffer, isize - sizeof(struct reiserfs_xattr_header)) !=
790 hash) {
Jeff Mahoney45b03d52009-03-30 14:02:21 -0400791 reiserfs_warning(inode->i_sb, "jdm-20002",
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700792 "Invalid hash for xattr (%s) associated "
793 "with %k", name, INODE_PKEY(inode));
794 err = -EIO;
795 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
Jeff Mahoneya72bdb12009-03-30 14:02:33 -0400797out_dput:
Jeff Mahoney3227e142008-02-15 14:37:22 -0800798 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
Jeff Mahoneya72bdb12009-03-30 14:02:33 -0400800out:
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700801 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802}
803
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700804int reiserfs_xattr_del(struct inode *inode, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700806 struct dentry *dir;
807 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700809 dir = open_xa_dir(inode, FL_READONLY);
810 if (IS_ERR(dir)) {
811 err = PTR_ERR(dir);
812 goto out;
813 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700815 err = __reiserfs_xattr_del(dir, name, strlen(name));
816 dput(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700818 if (!err) {
819 inode->i_ctime = CURRENT_TIME_SEC;
820 mark_inode_dirty(inode);
821 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700823 out:
824 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825}
826
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827/* Actual operations that are exported to VFS-land */
828
829/*
830 * Inode operation getxattr()
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800831 * Preliminary locking: we down dentry->d_inode->i_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 */
833ssize_t
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700834reiserfs_getxattr(struct dentry * dentry, const char *name, void *buffer,
835 size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700837 struct reiserfs_xattr_handler *xah = find_xattr_handler_prefix(name);
838 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700840 if (!xah || !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
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700844 reiserfs_read_lock_xattr_i(dentry->d_inode);
845 reiserfs_read_lock_xattrs(dentry->d_sb);
846 err = xah->get(dentry->d_inode, name, buffer, size);
847 reiserfs_read_unlock_xattrs(dentry->d_sb);
848 reiserfs_read_unlock_xattr_i(dentry->d_inode);
849 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850}
851
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852/*
853 * Inode operation setxattr()
854 *
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800855 * dentry->d_inode->i_mutex down
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 */
857int
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700858reiserfs_setxattr(struct dentry *dentry, const char *name, const void *value,
859 size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700861 struct reiserfs_xattr_handler *xah = find_xattr_handler_prefix(name);
862 int err;
863 int lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700865 if (!xah || !reiserfs_xattrs(dentry->d_sb) ||
866 get_inode_sd_version(dentry->d_inode) == STAT_DATA_V1)
867 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700869 reiserfs_write_lock_xattr_i(dentry->d_inode);
870 lock = !has_xattr_dir(dentry->d_inode);
871 if (lock)
872 reiserfs_write_lock_xattrs(dentry->d_sb);
873 else
874 reiserfs_read_lock_xattrs(dentry->d_sb);
875 err = xah->set(dentry->d_inode, name, value, size, flags);
876 if (lock)
877 reiserfs_write_unlock_xattrs(dentry->d_sb);
878 else
879 reiserfs_read_unlock_xattrs(dentry->d_sb);
880 reiserfs_write_unlock_xattr_i(dentry->d_inode);
881 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882}
883
884/*
885 * Inode operation removexattr()
886 *
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800887 * dentry->d_inode->i_mutex down
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700889int reiserfs_removexattr(struct dentry *dentry, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700891 int err;
892 struct reiserfs_xattr_handler *xah = find_xattr_handler_prefix(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700894 if (!xah || !reiserfs_xattrs(dentry->d_sb) ||
895 get_inode_sd_version(dentry->d_inode) == STAT_DATA_V1)
896 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700898 reiserfs_write_lock_xattr_i(dentry->d_inode);
899 reiserfs_read_lock_xattrs(dentry->d_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700901 /* Deletion pre-operation */
902 if (xah->del) {
903 err = xah->del(dentry->d_inode, name);
904 if (err)
905 goto out;
906 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700908 err = reiserfs_xattr_del(dentry->d_inode, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700910 dentry->d_inode->i_ctime = CURRENT_TIME_SEC;
911 mark_inode_dirty(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700913 out:
914 reiserfs_read_unlock_xattrs(dentry->d_sb);
915 reiserfs_write_unlock_xattr_i(dentry->d_inode);
916 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917}
918
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919/* This is what filldir will use:
920 * r_pos will always contain the amount of space required for the entire
921 * list. If r_pos becomes larger than r_size, we need more space and we
922 * return an error indicating this. If r_pos is less than r_size, then we've
923 * filled the buffer successfully and we return success */
924struct reiserfs_listxattr_buf {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700925 int r_pos;
926 int r_size;
927 char *r_buf;
928 struct inode *r_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929};
930
931static int
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700932reiserfs_listxattr_filler(void *buf, const char *name, int namelen,
David Howellsafefdbb2006-10-03 01:13:46 -0700933 loff_t offset, u64 ino, unsigned int d_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700935 struct reiserfs_listxattr_buf *b = (struct reiserfs_listxattr_buf *)buf;
936 int len = 0;
937 if (name[0] != '.'
938 || (namelen != 1 && (name[1] != '.' || namelen != 2))) {
939 struct reiserfs_xattr_handler *xah =
940 find_xattr_handler_prefix(name);
941 if (!xah)
942 return 0; /* Unsupported xattr name, skip it */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700944 /* We call ->list() twice because the operation isn't required to just
945 * return the name back - we want to make sure we have enough space */
946 len += xah->list(b->r_inode, name, namelen, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700948 if (len) {
949 if (b->r_pos + len + 1 <= b->r_size) {
950 char *p = b->r_buf + b->r_pos;
951 p += xah->list(b->r_inode, name, namelen, p);
952 *p++ = '\0';
953 }
954 b->r_pos += len + 1;
955 }
956 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700958 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959}
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700960
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961/*
962 * Inode operation listxattr()
963 *
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800964 * Preliminary locking: we down dentry->d_inode->i_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700966ssize_t reiserfs_listxattr(struct dentry * dentry, char *buffer, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700968 struct dentry *dir;
969 int err = 0;
970 struct reiserfs_listxattr_buf buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700972 if (!dentry->d_inode)
973 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700975 if (!reiserfs_xattrs(dentry->d_sb) ||
976 get_inode_sd_version(dentry->d_inode) == STAT_DATA_V1)
977 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700979 reiserfs_read_lock_xattr_i(dentry->d_inode);
980 reiserfs_read_lock_xattrs(dentry->d_sb);
981 dir = open_xa_dir(dentry->d_inode, FL_READONLY);
982 reiserfs_read_unlock_xattrs(dentry->d_sb);
983 if (IS_ERR(dir)) {
984 err = PTR_ERR(dir);
985 if (err == -ENODATA)
986 err = 0; /* Not an error if there aren't any xattrs */
987 goto out;
988 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700990 buf.r_buf = buffer;
991 buf.r_size = buffer ? size : 0;
992 buf.r_pos = 0;
993 buf.r_inode = dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700995 REISERFS_I(dentry->d_inode)->i_flags |= i_has_xattr_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996
Jeff Mahoney3227e142008-02-15 14:37:22 -0800997 err = xattr_readdir(dir->d_inode, reiserfs_listxattr_filler, &buf);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700998 if (err)
999 goto out_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001001 if (buf.r_pos > buf.r_size && buffer != NULL)
1002 err = -ERANGE;
1003 else
1004 err = buf.r_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001006 out_dir:
Jeff Mahoney3227e142008-02-15 14:37:22 -08001007 dput(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001009 out:
1010 reiserfs_read_unlock_xattr_i(dentry->d_inode);
1011 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012}
1013
1014/* This is the implementation for the xattr plugin infrastructure */
Denis Chengbcf11cb2008-02-06 01:37:40 -08001015static LIST_HEAD(xattr_handlers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016static DEFINE_RWLOCK(handler_lock);
1017
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001018static struct reiserfs_xattr_handler *find_xattr_handler_prefix(const char
1019 *prefix)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001021 struct reiserfs_xattr_handler *xah = NULL;
1022 struct list_head *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001024 read_lock(&handler_lock);
1025 list_for_each(p, &xattr_handlers) {
1026 xah = list_entry(p, struct reiserfs_xattr_handler, handlers);
1027 if (strncmp(xah->prefix, prefix, strlen(xah->prefix)) == 0)
1028 break;
1029 xah = NULL;
1030 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001032 read_unlock(&handler_lock);
1033 return xah;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034}
1035
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001036static void __unregister_handlers(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001038 struct reiserfs_xattr_handler *xah;
1039 struct list_head *p, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001041 list_for_each_safe(p, tmp, &xattr_handlers) {
1042 xah = list_entry(p, struct reiserfs_xattr_handler, handlers);
1043 if (xah->exit)
1044 xah->exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001046 list_del_init(p);
1047 }
1048 INIT_LIST_HEAD(&xattr_handlers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049}
1050
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001051int __init reiserfs_xattr_register_handlers(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001053 int err = 0;
1054 struct reiserfs_xattr_handler *xah;
1055 struct list_head *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001057 write_lock(&handler_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001059 /* If we're already initialized, nothing to do */
1060 if (!list_empty(&xattr_handlers)) {
1061 write_unlock(&handler_lock);
1062 return 0;
1063 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001065 /* Add the handlers */
1066 list_add_tail(&user_handler.handlers, &xattr_handlers);
1067 list_add_tail(&trusted_handler.handlers, &xattr_handlers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068#ifdef CONFIG_REISERFS_FS_SECURITY
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001069 list_add_tail(&security_handler.handlers, &xattr_handlers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070#endif
1071#ifdef CONFIG_REISERFS_FS_POSIX_ACL
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001072 list_add_tail(&posix_acl_access_handler.handlers, &xattr_handlers);
1073 list_add_tail(&posix_acl_default_handler.handlers, &xattr_handlers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074#endif
1075
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001076 /* Run initializers, if available */
1077 list_for_each(p, &xattr_handlers) {
1078 xah = list_entry(p, struct reiserfs_xattr_handler, handlers);
1079 if (xah->init) {
1080 err = xah->init();
1081 if (err) {
1082 list_del_init(p);
1083 break;
1084 }
1085 }
1086 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001088 /* Clean up other handlers, if any failed */
1089 if (err)
1090 __unregister_handlers();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001092 write_unlock(&handler_lock);
1093 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094}
1095
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001096void reiserfs_xattr_unregister_handlers(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001098 write_lock(&handler_lock);
1099 __unregister_handlers();
1100 write_unlock(&handler_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101}
1102
Christoph Hellwigec191572006-02-01 03:06:46 -08001103static int reiserfs_check_acl(struct inode *inode, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104{
Christoph Hellwigec191572006-02-01 03:06:46 -08001105 struct posix_acl *acl;
1106 int error = -EAGAIN; /* do regular unix permission checks by default */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
Christoph Hellwigec191572006-02-01 03:06:46 -08001108 reiserfs_read_lock_xattr_i(inode);
1109 reiserfs_read_lock_xattrs(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110
Christoph Hellwigec191572006-02-01 03:06:46 -08001111 acl = reiserfs_get_acl(inode, ACL_TYPE_ACCESS);
1112
1113 reiserfs_read_unlock_xattrs(inode->i_sb);
1114 reiserfs_read_unlock_xattr_i(inode);
1115
1116 if (acl) {
1117 if (!IS_ERR(acl)) {
1118 error = posix_acl_permission(inode, acl, mask);
1119 posix_acl_release(acl);
1120 } else if (PTR_ERR(acl) != -ENODATA)
1121 error = PTR_ERR(acl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 }
1123
Christoph Hellwigec191572006-02-01 03:06:46 -08001124 return error;
1125}
1126
Al Viroe6305c42008-07-15 21:03:57 -04001127int reiserfs_permission(struct inode *inode, int mask)
Christoph Hellwigec191572006-02-01 03:06:46 -08001128{
1129 /*
1130 * We don't do permission checks on the internal objects.
1131 * Permissions are determined by the "owning" object.
1132 */
Jeff Mahoney6dfede62009-03-30 14:02:32 -04001133 if (IS_PRIVATE(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 /*
Christoph Hellwigec191572006-02-01 03:06:46 -08001136 * Stat data v1 doesn't support ACLs.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 */
Christoph Hellwigec191572006-02-01 03:06:46 -08001138 if (get_inode_sd_version(inode) == STAT_DATA_V1)
1139 return generic_permission(inode, mask, NULL);
1140 else
1141 return generic_permission(inode, mask, reiserfs_check_acl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142}
Jeff Mahoneya72bdb12009-03-30 14:02:33 -04001143
1144static int create_privroot(struct dentry *dentry)
1145{
1146 int err;
1147 struct inode *inode = dentry->d_parent->d_inode;
1148 mutex_lock_nested(&inode->i_mutex, I_MUTEX_XATTR);
1149 err = inode->i_op->mkdir(inode, dentry, 0700);
1150 mutex_unlock(&inode->i_mutex);
1151 if (err) {
1152 dput(dentry);
1153 dentry = NULL;
1154 }
1155
1156 if (dentry && dentry->d_inode)
1157 reiserfs_info(dentry->d_sb, "Created %s - reserved for xattr "
1158 "storage.\n", PRIVROOT_NAME);
1159
1160 return err;
1161}
1162
1163static int xattr_mount_check(struct super_block *s)
1164{
1165 /* We need generation numbers to ensure that the oid mapping is correct
1166 * v3.5 filesystems don't have them. */
1167 if (!old_format_only(s)) {
1168 set_bit(REISERFS_XATTRS, &(REISERFS_SB(s)->s_mount_opt));
1169 } else if (reiserfs_xattrs_optional(s)) {
1170 /* Old format filesystem, but optional xattrs have been enabled
1171 * at mount time. Error out. */
1172 reiserfs_warning(s, "jdm-20005",
1173 "xattrs/ACLs not supported on pre v3.6 "
1174 "format filesystem. Failing mount.");
1175 return -EOPNOTSUPP;
1176 } else {
1177 /* Old format filesystem, but no optional xattrs have
1178 * been enabled. This means we silently disable xattrs
1179 * on the filesystem. */
1180 clear_bit(REISERFS_XATTRS, &(REISERFS_SB(s)->s_mount_opt));
1181 }
1182
1183 return 0;
1184}
1185
1186#else
1187int __init reiserfs_xattr_register_handlers(void) { return 0; }
1188void reiserfs_xattr_unregister_handlers(void) {}
1189#endif
1190
1191/* This will catch lookups from the fs root to .reiserfs_priv */
1192static int
1193xattr_lookup_poison(struct dentry *dentry, struct qstr *q1, struct qstr *name)
1194{
1195 struct dentry *priv_root = REISERFS_SB(dentry->d_sb)->priv_root;
1196 if (name->len == priv_root->d_name.len &&
1197 name->hash == priv_root->d_name.hash &&
1198 !memcmp(name->name, priv_root->d_name.name, name->len)) {
1199 return -ENOENT;
1200 } else if (q1->len == name->len &&
1201 !memcmp(q1->name, name->name, name->len))
1202 return 0;
1203 return 1;
1204}
1205
1206static struct dentry_operations xattr_lookup_poison_ops = {
1207 .d_compare = xattr_lookup_poison,
1208};
1209
1210/* We need to take a copy of the mount flags since things like
1211 * MS_RDONLY don't get set until *after* we're called.
1212 * mount_flags != mount_options */
1213int reiserfs_xattr_init(struct super_block *s, int mount_flags)
1214{
1215 int err = 0;
1216
1217#ifdef CONFIG_REISERFS_FS_XATTR
1218 err = xattr_mount_check(s);
1219 if (err)
1220 goto error;
1221#endif
1222
1223 /* If we don't have the privroot located yet - go find it */
1224 if (!REISERFS_SB(s)->priv_root) {
1225 struct dentry *dentry;
1226 dentry = lookup_one_len(PRIVROOT_NAME, s->s_root,
1227 strlen(PRIVROOT_NAME));
1228 if (!IS_ERR(dentry)) {
1229#ifdef CONFIG_REISERFS_FS_XATTR
1230 if (!(mount_flags & MS_RDONLY) && !dentry->d_inode)
1231 err = create_privroot(dentry);
1232#endif
1233 if (!dentry->d_inode) {
1234 dput(dentry);
1235 dentry = NULL;
1236 }
1237 } else
1238 err = PTR_ERR(dentry);
1239
1240 if (!err && dentry) {
1241 s->s_root->d_op = &xattr_lookup_poison_ops;
1242 dentry->d_inode->i_flags |= S_PRIVATE;
1243 REISERFS_SB(s)->priv_root = dentry;
1244#ifdef CONFIG_REISERFS_FS_XATTR
1245 /* xattrs are unavailable */
1246 } else if (!(mount_flags & MS_RDONLY)) {
1247 /* If we're read-only it just means that the dir
1248 * hasn't been created. Not an error -- just no
1249 * xattrs on the fs. We'll check again if we
1250 * go read-write */
1251 reiserfs_warning(s, "jdm-20006",
1252 "xattrs/ACLs enabled and couldn't "
1253 "find/create .reiserfs_priv. "
1254 "Failing mount.");
1255 err = -EOPNOTSUPP;
1256#endif
1257 }
1258 }
1259
1260#ifdef CONFIG_REISERFS_FS_XATTR
1261error:
1262 if (err) {
1263 clear_bit(REISERFS_XATTRS, &(REISERFS_SB(s)->s_mount_opt));
1264 clear_bit(REISERFS_XATTRS_USER, &(REISERFS_SB(s)->s_mount_opt));
1265 clear_bit(REISERFS_POSIXACL, &(REISERFS_SB(s)->s_mount_opt));
1266 }
1267#endif
1268
1269 /* The super_block MS_POSIXACL must mirror the (no)acl mount option. */
1270 s->s_flags = s->s_flags & ~MS_POSIXACL;
1271#ifdef CONFIG_REISERFS_FS_POSIX_ACL
1272 if (reiserfs_posixacl(s))
1273 s->s_flags |= MS_POSIXACL;
1274#endif
1275
1276 return err;
1277}