blob: ffb79c48c5bf974fb6953fe6b761e64f16bbe8fa [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>
44#include <asm/checksum.h>
45#include <linux/smp_lock.h>
46#include <linux/stat.h>
47#include <asm/semaphore.h>
48
49#define FL_READONLY 128
50#define FL_DIR_SEM_HELD 256
51#define PRIVROOT_NAME ".reiserfs_priv"
52#define XAROOT_NAME "xattrs"
53
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070054static struct reiserfs_xattr_handler *find_xattr_handler_prefix(const char
55 *prefix);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070057static struct dentry *create_xa_root(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -070058{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070059 struct dentry *privroot = dget(REISERFS_SB(sb)->priv_root);
60 struct dentry *xaroot;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070062 /* This needs to be created at mount-time */
63 if (!privroot)
64 return ERR_PTR(-EOPNOTSUPP);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070066 xaroot = lookup_one_len(XAROOT_NAME, privroot, strlen(XAROOT_NAME));
67 if (IS_ERR(xaroot)) {
68 goto out;
69 } else if (!xaroot->d_inode) {
70 int err;
Jes Sorensen1b1dcc12006-01-09 15:59:24 -080071 mutex_lock(&privroot->d_inode->i_mutex);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070072 err =
73 privroot->d_inode->i_op->mkdir(privroot->d_inode, xaroot,
74 0700);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -080075 mutex_unlock(&privroot->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070077 if (err) {
78 dput(xaroot);
79 dput(privroot);
80 return ERR_PTR(err);
81 }
82 REISERFS_SB(sb)->xattr_root = dget(xaroot);
83 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070085 out:
86 dput(privroot);
87 return xaroot;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088}
89
90/* This will return a dentry, or error, refering to the xa root directory.
91 * If the xa root doesn't exist yet, the dentry will be returned without
92 * an associated inode. This dentry can be used with ->mkdir to create
93 * the xa directory. */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070094static struct dentry *__get_xa_root(struct super_block *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070096 struct dentry *privroot = dget(REISERFS_SB(s)->priv_root);
97 struct dentry *xaroot = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070099 if (IS_ERR(privroot) || !privroot)
100 return privroot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700102 xaroot = lookup_one_len(XAROOT_NAME, privroot, strlen(XAROOT_NAME));
103 if (IS_ERR(xaroot)) {
104 goto out;
105 } else if (!xaroot->d_inode) {
106 dput(xaroot);
107 xaroot = NULL;
108 goto out;
109 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700111 REISERFS_SB(s)->xattr_root = dget(xaroot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700113 out:
114 dput(privroot);
115 return xaroot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116}
117
118/* Returns the dentry (or NULL) referring to the root of the extended
Matt Mackall4a4efbd2006-01-03 13:27:11 +0100119 * attribute directory tree. If it has already been retrieved, it is used.
120 * Otherwise, we attempt to retrieve it from disk. It may also return
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 * a pointer-encoded error.
122 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700123static inline struct dentry *get_xa_root(struct super_block *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700125 struct dentry *dentry = dget(REISERFS_SB(s)->xattr_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700127 if (!dentry)
128 dentry = __get_xa_root(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700130 return dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131}
132
133/* Opens the directory corresponding to the inode's extended attribute store.
134 * If flags allow, the tree to the directory may be created. If creation is
135 * prohibited, -ENODATA is returned. */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700136static struct dentry *open_xa_dir(const struct inode *inode, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700138 struct dentry *xaroot, *xadir;
139 char namebuf[17];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700141 xaroot = get_xa_root(inode->i_sb);
142 if (IS_ERR(xaroot)) {
143 return xaroot;
144 } else if (!xaroot) {
145 if (flags == 0 || flags & XATTR_CREATE) {
146 xaroot = create_xa_root(inode->i_sb);
147 if (IS_ERR(xaroot))
148 return xaroot;
149 }
150 if (!xaroot)
151 return ERR_PTR(-ENODATA);
152 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700154 /* ok, we have xaroot open */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700156 snprintf(namebuf, sizeof(namebuf), "%X.%X",
157 le32_to_cpu(INODE_PKEY(inode)->k_objectid),
158 inode->i_generation);
159 xadir = lookup_one_len(namebuf, xaroot, strlen(namebuf));
160 if (IS_ERR(xadir)) {
161 dput(xaroot);
162 return xadir;
163 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700165 if (!xadir->d_inode) {
166 int err;
167 if (flags == 0 || flags & XATTR_CREATE) {
168 /* Although there is nothing else trying to create this directory,
169 * another directory with the same hash may be created, so we need
170 * to protect against that */
171 err =
172 xaroot->d_inode->i_op->mkdir(xaroot->d_inode, xadir,
173 0700);
174 if (err) {
175 dput(xaroot);
176 dput(xadir);
177 return ERR_PTR(err);
178 }
179 }
180 if (!xadir->d_inode) {
181 dput(xaroot);
182 dput(xadir);
183 return ERR_PTR(-ENODATA);
184 }
185 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700187 dput(xaroot);
188 return xadir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189}
190
191/* Returns a dentry corresponding to a specific extended attribute file
192 * for the inode. If flags allow, the file is created. Otherwise, a
193 * valid or negative dentry, or an error is returned. */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700194static struct dentry *get_xa_file_dentry(const struct inode *inode,
195 const char *name, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700197 struct dentry *xadir, *xafile;
198 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700200 xadir = open_xa_dir(inode, flags);
201 if (IS_ERR(xadir)) {
202 return ERR_PTR(PTR_ERR(xadir));
203 } else if (xadir && !xadir->d_inode) {
204 dput(xadir);
205 return ERR_PTR(-ENODATA);
206 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700208 xafile = lookup_one_len(name, xadir, strlen(name));
209 if (IS_ERR(xafile)) {
210 dput(xadir);
211 return ERR_PTR(PTR_ERR(xafile));
212 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700214 if (xafile->d_inode) { /* file exists */
215 if (flags & XATTR_CREATE) {
216 err = -EEXIST;
217 dput(xafile);
218 goto out;
219 }
220 } else if (flags & XATTR_REPLACE || flags & FL_READONLY) {
221 goto out;
222 } else {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800223 /* inode->i_mutex is down, so nothing else can try to create
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700224 * the same xattr */
225 err = xadir->d_inode->i_op->create(xadir->d_inode, xafile,
226 0700 | S_IFREG, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700228 if (err) {
229 dput(xafile);
230 goto out;
231 }
232 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700234 out:
235 dput(xadir);
236 if (err)
237 xafile = ERR_PTR(err);
238 return xafile;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239}
240
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241/* Opens a file pointer to the attribute associated with inode */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700242static struct file *open_xa_file(const struct inode *inode, const char *name,
243 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700245 struct dentry *xafile;
246 struct file *fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700248 xafile = get_xa_file_dentry(inode, name, flags);
249 if (IS_ERR(xafile))
250 return ERR_PTR(PTR_ERR(xafile));
251 else if (!xafile->d_inode) {
252 dput(xafile);
253 return ERR_PTR(-ENODATA);
254 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700256 fp = dentry_open(xafile, NULL, O_RDWR);
257 /* dentry_open dputs the dentry if it fails */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700259 return fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260}
261
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262/*
263 * this is very similar to fs/reiserfs/dir.c:reiserfs_readdir, but
264 * we need to drop the path before calling the filldir struct. That
265 * would be a big performance hit to the non-xattr case, so I've copied
266 * the whole thing for now. --clm
267 *
268 * the big difference is that I go backwards through the directory,
269 * and don't mess with f->f_pos, but the idea is the same. Do some
270 * action on each and every entry in the directory.
271 *
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800272 * we're called with i_mutex held, so there are no worries about the directory
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 * changing underneath us.
274 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700275static int __xattr_readdir(struct file *filp, void *dirent, filldir_t filldir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700277 struct inode *inode = filp->f_dentry->d_inode;
278 struct cpu_key pos_key; /* key of current position in the directory (key of directory entry) */
279 INITIALIZE_PATH(path_to_entry);
280 struct buffer_head *bh;
281 int entry_num;
282 struct item_head *ih, tmp_ih;
283 int search_res;
284 char *local_buf;
285 loff_t next_pos;
286 char small_buf[32]; /* avoid kmalloc if we can */
287 struct reiserfs_de_head *deh;
288 int d_reclen;
289 char *d_name;
290 off_t d_off;
291 ino_t d_ino;
292 struct reiserfs_dir_entry de;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700294 /* form key for search the next directory entry using f_pos field of
295 file structure */
296 next_pos = max_reiserfs_offset(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700298 while (1) {
299 research:
300 if (next_pos <= DOT_DOT_OFFSET)
301 break;
302 make_cpu_key(&pos_key, inode, next_pos, TYPE_DIRENTRY, 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700304 search_res =
305 search_by_entry_key(inode->i_sb, &pos_key, &path_to_entry,
306 &de);
307 if (search_res == IO_ERROR) {
308 // FIXME: we could just skip part of directory which could
309 // not be read
310 pathrelse(&path_to_entry);
311 return -EIO;
312 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700314 if (search_res == NAME_NOT_FOUND)
315 de.de_entry_num--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700317 set_de_name_and_namelen(&de);
318 entry_num = de.de_entry_num;
319 deh = &(de.de_deh[entry_num]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700321 bh = de.de_bh;
322 ih = de.de_ih;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700324 if (!is_direntry_le_ih(ih)) {
325 reiserfs_warning(inode->i_sb, "not direntry %h", ih);
326 break;
327 }
328 copy_item_head(&tmp_ih, ih);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700330 /* we must have found item, that is item of this directory, */
331 RFALSE(COMP_SHORT_KEYS(&(ih->ih_key), &pos_key),
332 "vs-9000: found item %h does not match to dir we readdir %K",
333 ih, &pos_key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700335 if (deh_offset(deh) <= DOT_DOT_OFFSET) {
336 break;
337 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700339 /* look for the previous entry in the directory */
340 next_pos = deh_offset(deh) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700342 if (!de_visible(deh))
343 /* it is hidden entry */
344 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700346 d_reclen = entry_length(bh, ih, entry_num);
347 d_name = B_I_DEH_ENTRY_FILE_NAME(bh, ih, deh);
348 d_off = deh_offset(deh);
349 d_ino = deh_objectid(deh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700351 if (!d_name[d_reclen - 1])
352 d_reclen = strlen(d_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700354 if (d_reclen > REISERFS_MAX_NAME(inode->i_sb->s_blocksize)) {
355 /* too big to send back to VFS */
356 continue;
357 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700359 /* Ignore the .reiserfs_priv entry */
360 if (reiserfs_xattrs(inode->i_sb) &&
361 !old_format_only(inode->i_sb) &&
362 deh_objectid(deh) ==
363 le32_to_cpu(INODE_PKEY
364 (REISERFS_SB(inode->i_sb)->priv_root->d_inode)->
365 k_objectid))
366 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700368 if (d_reclen <= 32) {
369 local_buf = small_buf;
370 } else {
Pekka Enbergd739b422006-02-01 03:06:43 -0800371 local_buf = kmalloc(d_reclen, GFP_NOFS);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700372 if (!local_buf) {
373 pathrelse(&path_to_entry);
374 return -ENOMEM;
375 }
376 if (item_moved(&tmp_ih, &path_to_entry)) {
Pekka Enbergd739b422006-02-01 03:06:43 -0800377 kfree(local_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700379 /* sigh, must retry. Do this same offset again */
380 next_pos = d_off;
381 goto research;
382 }
383 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700385 // Note, that we copy name to user space via temporary
386 // buffer (local_buf) because filldir will block if
387 // user space buffer is swapped out. At that time
388 // entry can move to somewhere else
389 memcpy(local_buf, d_name, d_reclen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700391 /* the filldir function might need to start transactions,
392 * or do who knows what. Release the path now that we've
393 * copied all the important stuff out of the deh
394 */
395 pathrelse(&path_to_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700397 if (filldir(dirent, local_buf, d_reclen, d_off, d_ino,
398 DT_UNKNOWN) < 0) {
399 if (local_buf != small_buf) {
Pekka Enbergd739b422006-02-01 03:06:43 -0800400 kfree(local_buf);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700401 }
402 goto end;
403 }
404 if (local_buf != small_buf) {
Pekka Enbergd739b422006-02-01 03:06:43 -0800405 kfree(local_buf);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700406 }
407 } /* while */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700409 end:
410 pathrelse(&path_to_entry);
411 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412}
413
414/*
415 * this could be done with dedicated readdir ops for the xattr files,
416 * but I want to get something working asap
417 * this is stolen from vfs_readdir
418 *
419 */
420static
421int xattr_readdir(struct file *file, filldir_t filler, void *buf)
422{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700423 struct inode *inode = file->f_dentry->d_inode;
424 int res = -ENOTDIR;
425 if (!file->f_op || !file->f_op->readdir)
426 goto out;
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800427 mutex_lock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428// down(&inode->i_zombie);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700429 res = -ENOENT;
430 if (!IS_DEADDIR(inode)) {
431 lock_kernel();
432 res = __xattr_readdir(file, buf, filler);
433 unlock_kernel();
434 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435// up(&inode->i_zombie);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800436 mutex_unlock(&inode->i_mutex);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700437 out:
438 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439}
440
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441/* Internal operations on file data */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700442static inline void reiserfs_put_page(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700444 kunmap(page);
445 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446}
447
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700448static struct page *reiserfs_get_page(struct inode *dir, unsigned long n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700450 struct address_space *mapping = dir->i_mapping;
451 struct page *page;
452 /* We can deadlock if we try to free dentries,
453 and an unlink/rmdir has just occured - GFP_NOFS avoids this */
Al Viroc4cdd032005-10-21 03:22:39 -0400454 mapping_set_gfp_mask(mapping, GFP_NOFS);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700455 page = read_cache_page(mapping, n,
456 (filler_t *) mapping->a_ops->readpage, NULL);
457 if (!IS_ERR(page)) {
458 wait_on_page_locked(page);
459 kmap(page);
460 if (!PageUptodate(page))
461 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700463 if (PageError(page))
464 goto fail;
465 }
466 return page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700468 fail:
469 reiserfs_put_page(page);
470 return ERR_PTR(-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471}
472
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700473static inline __u32 xattr_hash(const char *msg, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700475 return csum_partial(msg, len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476}
477
478/* Generic extended attribute operations that can be used by xa plugins */
479
480/*
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800481 * inode->i_mutex: down
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 */
483int
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700484reiserfs_xattr_set(struct inode *inode, const char *name, const void *buffer,
485 size_t buffer_size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700487 int err = 0;
488 struct file *fp;
489 struct page *page;
490 char *data;
491 struct address_space *mapping;
492 size_t file_pos = 0;
493 size_t buffer_pos = 0;
494 struct inode *xinode;
495 struct iattr newattrs;
496 __u32 xahash = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700498 if (get_inode_sd_version(inode) == STAT_DATA_V1)
499 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700501 /* Empty xattrs are ok, they're just empty files, no hash */
502 if (buffer && buffer_size)
503 xahash = xattr_hash(buffer, buffer_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700505 open_file:
506 fp = open_xa_file(inode, name, flags);
507 if (IS_ERR(fp)) {
508 err = PTR_ERR(fp);
509 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700512 xinode = fp->f_dentry->d_inode;
513 REISERFS_I(inode)->i_flags |= i_has_xattr_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700515 /* we need to copy it off.. */
516 if (xinode->i_nlink > 1) {
517 fput(fp);
518 err = reiserfs_xattr_del(inode, name);
519 if (err < 0)
520 goto out;
521 /* We just killed the old one, we're not replacing anymore */
522 if (flags & XATTR_REPLACE)
523 flags &= ~XATTR_REPLACE;
524 goto open_file;
525 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700527 /* Resize it so we're ok to write there */
528 newattrs.ia_size = buffer_size;
529 newattrs.ia_valid = ATTR_SIZE | ATTR_CTIME;
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800530 mutex_lock(&xinode->i_mutex);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700531 err = notify_change(fp->f_dentry, &newattrs);
532 if (err)
533 goto out_filp;
534
535 mapping = xinode->i_mapping;
536 while (buffer_pos < buffer_size || buffer_pos == 0) {
537 size_t chunk;
538 size_t skip = 0;
539 size_t page_offset = (file_pos & (PAGE_CACHE_SIZE - 1));
540 if (buffer_size - buffer_pos > PAGE_CACHE_SIZE)
541 chunk = PAGE_CACHE_SIZE;
542 else
543 chunk = buffer_size - buffer_pos;
544
545 page = reiserfs_get_page(xinode, file_pos >> PAGE_CACHE_SHIFT);
546 if (IS_ERR(page)) {
547 err = PTR_ERR(page);
548 goto out_filp;
549 }
550
551 lock_page(page);
552 data = page_address(page);
553
554 if (file_pos == 0) {
555 struct reiserfs_xattr_header *rxh;
556 skip = file_pos = sizeof(struct reiserfs_xattr_header);
557 if (chunk + skip > PAGE_CACHE_SIZE)
558 chunk = PAGE_CACHE_SIZE - skip;
559 rxh = (struct reiserfs_xattr_header *)data;
560 rxh->h_magic = cpu_to_le32(REISERFS_XATTR_MAGIC);
561 rxh->h_hash = cpu_to_le32(xahash);
562 }
563
564 err = mapping->a_ops->prepare_write(fp, page, page_offset,
565 page_offset + chunk + skip);
566 if (!err) {
567 if (buffer)
568 memcpy(data + skip, buffer + buffer_pos, chunk);
569 err =
570 mapping->a_ops->commit_write(fp, page, page_offset,
571 page_offset + chunk +
572 skip);
573 }
574 unlock_page(page);
575 reiserfs_put_page(page);
576 buffer_pos += chunk;
577 file_pos += chunk;
578 skip = 0;
579 if (err || buffer_size == 0 || !buffer)
580 break;
581 }
582
583 /* We can't mark the inode dirty if it's not hashed. This is the case
584 * when we're inheriting the default ACL. If we dirty it, the inode
585 * gets marked dirty, but won't (ever) make it onto the dirty list until
586 * it's synced explicitly to clear I_DIRTY. This is bad. */
587 if (!hlist_unhashed(&inode->i_hash)) {
588 inode->i_ctime = CURRENT_TIME_SEC;
589 mark_inode_dirty(inode);
590 }
591
592 out_filp:
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800593 mutex_unlock(&xinode->i_mutex);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700594 fput(fp);
595
596 out:
597 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598}
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_get(const struct inode *inode, const char *name, void *buffer,
605 size_t buffer_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700607 ssize_t err = 0;
608 struct file *fp;
609 size_t isize;
610 size_t file_pos = 0;
611 size_t buffer_pos = 0;
612 struct page *page;
613 struct inode *xinode;
614 __u32 hash = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700616 if (name == NULL)
617 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700619 /* We can't have xattrs attached to v1 items since they don't have
620 * generation numbers */
621 if (get_inode_sd_version(inode) == STAT_DATA_V1)
622 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700624 fp = open_xa_file(inode, name, FL_READONLY);
625 if (IS_ERR(fp)) {
626 err = PTR_ERR(fp);
627 goto out;
628 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700630 xinode = fp->f_dentry->d_inode;
631 isize = xinode->i_size;
632 REISERFS_I(inode)->i_flags |= i_has_xattr_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700634 /* Just return the size needed */
635 if (buffer == NULL) {
636 err = isize - sizeof(struct reiserfs_xattr_header);
637 goto out_dput;
638 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700640 if (buffer_size < isize - sizeof(struct reiserfs_xattr_header)) {
641 err = -ERANGE;
642 goto out_dput;
643 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700645 while (file_pos < isize) {
646 size_t chunk;
647 char *data;
648 size_t skip = 0;
649 if (isize - file_pos > PAGE_CACHE_SIZE)
650 chunk = PAGE_CACHE_SIZE;
651 else
652 chunk = isize - file_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700654 page = reiserfs_get_page(xinode, file_pos >> PAGE_CACHE_SHIFT);
655 if (IS_ERR(page)) {
656 err = PTR_ERR(page);
657 goto out_dput;
658 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700660 lock_page(page);
661 data = page_address(page);
662 if (file_pos == 0) {
663 struct reiserfs_xattr_header *rxh =
664 (struct reiserfs_xattr_header *)data;
665 skip = file_pos = sizeof(struct reiserfs_xattr_header);
666 chunk -= skip;
667 /* Magic doesn't match up.. */
668 if (rxh->h_magic != cpu_to_le32(REISERFS_XATTR_MAGIC)) {
669 unlock_page(page);
670 reiserfs_put_page(page);
671 reiserfs_warning(inode->i_sb,
672 "Invalid magic for xattr (%s) "
673 "associated with %k", name,
674 INODE_PKEY(inode));
675 err = -EIO;
676 goto out_dput;
677 }
678 hash = le32_to_cpu(rxh->h_hash);
679 }
680 memcpy(buffer + buffer_pos, data + skip, chunk);
681 unlock_page(page);
682 reiserfs_put_page(page);
683 file_pos += chunk;
684 buffer_pos += chunk;
685 skip = 0;
686 }
687 err = isize - sizeof(struct reiserfs_xattr_header);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700689 if (xattr_hash(buffer, isize - sizeof(struct reiserfs_xattr_header)) !=
690 hash) {
691 reiserfs_warning(inode->i_sb,
692 "Invalid hash for xattr (%s) associated "
693 "with %k", name, INODE_PKEY(inode));
694 err = -EIO;
695 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700697 out_dput:
698 fput(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700700 out:
701 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702}
703
704static int
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700705__reiserfs_xattr_del(struct dentry *xadir, const char *name, int namelen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700707 struct dentry *dentry;
708 struct inode *dir = xadir->d_inode;
709 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700711 dentry = lookup_one_len(name, xadir, namelen);
712 if (IS_ERR(dentry)) {
713 err = PTR_ERR(dentry);
714 goto out;
715 } else if (!dentry->d_inode) {
716 err = -ENODATA;
717 goto out_file;
718 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700720 /* Skip directories.. */
721 if (S_ISDIR(dentry->d_inode->i_mode))
722 goto out_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700724 if (!is_reiserfs_priv_object(dentry->d_inode)) {
725 reiserfs_warning(dir->i_sb, "OID %08x [%.*s/%.*s] doesn't have "
726 "priv flag set [parent is %sset].",
727 le32_to_cpu(INODE_PKEY(dentry->d_inode)->
728 k_objectid), xadir->d_name.len,
729 xadir->d_name.name, namelen, name,
730 is_reiserfs_priv_object(xadir->
731 d_inode) ? "" :
732 "not ");
733 dput(dentry);
734 return -EIO;
735 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700737 err = dir->i_op->unlink(dir, dentry);
738 if (!err)
739 d_delete(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700741 out_file:
742 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700744 out:
745 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746}
747
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700748int reiserfs_xattr_del(struct inode *inode, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700750 struct dentry *dir;
751 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700753 dir = open_xa_dir(inode, FL_READONLY);
754 if (IS_ERR(dir)) {
755 err = PTR_ERR(dir);
756 goto out;
757 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700759 err = __reiserfs_xattr_del(dir, name, strlen(name));
760 dput(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700762 if (!err) {
763 inode->i_ctime = CURRENT_TIME_SEC;
764 mark_inode_dirty(inode);
765 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700767 out:
768 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769}
770
771/* The following are side effects of other operations that aren't explicitly
772 * modifying extended attributes. This includes operations such as permissions
773 * or ownership changes, object deletions, etc. */
774
775static int
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700776reiserfs_delete_xattrs_filler(void *buf, const char *name, int namelen,
777 loff_t offset, ino_t ino, unsigned int d_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700779 struct dentry *xadir = (struct dentry *)buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700781 return __reiserfs_xattr_del(xadir, name, namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
783}
784
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800785/* This is called w/ inode->i_mutex downed */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700786int reiserfs_delete_xattrs(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700788 struct file *fp;
789 struct dentry *dir, *root;
790 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700792 /* Skip out, an xattr has no xattrs associated with it */
793 if (is_reiserfs_priv_object(inode) ||
794 get_inode_sd_version(inode) == STAT_DATA_V1 ||
795 !reiserfs_xattrs(inode->i_sb)) {
796 return 0;
797 }
798 reiserfs_read_lock_xattrs(inode->i_sb);
799 dir = open_xa_dir(inode, FL_READONLY);
800 reiserfs_read_unlock_xattrs(inode->i_sb);
801 if (IS_ERR(dir)) {
802 err = PTR_ERR(dir);
803 goto out;
804 } else if (!dir->d_inode) {
805 dput(dir);
806 return 0;
807 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700809 fp = dentry_open(dir, NULL, O_RDWR);
810 if (IS_ERR(fp)) {
811 err = PTR_ERR(fp);
812 /* dentry_open dputs the dentry if it fails */
813 goto out;
814 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700816 lock_kernel();
817 err = xattr_readdir(fp, reiserfs_delete_xattrs_filler, dir);
818 if (err) {
819 unlock_kernel();
820 goto out_dir;
821 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700823 /* Leftovers besides . and .. -- that's not good. */
824 if (dir->d_inode->i_nlink <= 2) {
825 root = get_xa_root(inode->i_sb);
826 reiserfs_write_lock_xattrs(inode->i_sb);
827 err = vfs_rmdir(root->d_inode, dir);
828 reiserfs_write_unlock_xattrs(inode->i_sb);
829 dput(root);
830 } else {
831 reiserfs_warning(inode->i_sb,
832 "Couldn't remove all entries in directory");
833 }
834 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700836 out_dir:
837 fput(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700839 out:
840 if (!err)
841 REISERFS_I(inode)->i_flags =
842 REISERFS_I(inode)->i_flags & ~i_has_xattr_dir;
843 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844}
845
846struct reiserfs_chown_buf {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700847 struct inode *inode;
848 struct dentry *xadir;
849 struct iattr *attrs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850};
851
852/* XXX: If there is a better way to do this, I'd love to hear about it */
853static int
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700854reiserfs_chown_xattrs_filler(void *buf, const char *name, int namelen,
855 loff_t offset, ino_t ino, unsigned int d_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700857 struct reiserfs_chown_buf *chown_buf = (struct reiserfs_chown_buf *)buf;
858 struct dentry *xafile, *xadir = chown_buf->xadir;
859 struct iattr *attrs = chown_buf->attrs;
860 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700862 xafile = lookup_one_len(name, xadir, namelen);
863 if (IS_ERR(xafile))
864 return PTR_ERR(xafile);
865 else if (!xafile->d_inode) {
866 dput(xafile);
867 return -ENODATA;
868 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700870 if (!S_ISDIR(xafile->d_inode->i_mode))
871 err = notify_change(xafile, attrs);
872 dput(xafile);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700874 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875}
876
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700877int reiserfs_chown_xattrs(struct inode *inode, struct iattr *attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700879 struct file *fp;
880 struct dentry *dir;
881 int err = 0;
882 struct reiserfs_chown_buf buf;
883 unsigned int ia_valid = attrs->ia_valid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700885 /* Skip out, an xattr has no xattrs associated with it */
886 if (is_reiserfs_priv_object(inode) ||
887 get_inode_sd_version(inode) == STAT_DATA_V1 ||
888 !reiserfs_xattrs(inode->i_sb)) {
889 return 0;
890 }
891 reiserfs_read_lock_xattrs(inode->i_sb);
892 dir = open_xa_dir(inode, FL_READONLY);
893 reiserfs_read_unlock_xattrs(inode->i_sb);
894 if (IS_ERR(dir)) {
895 if (PTR_ERR(dir) != -ENODATA)
896 err = PTR_ERR(dir);
897 goto out;
898 } else if (!dir->d_inode) {
899 dput(dir);
900 goto out;
901 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700903 fp = dentry_open(dir, NULL, O_RDWR);
904 if (IS_ERR(fp)) {
905 err = PTR_ERR(fp);
906 /* dentry_open dputs the dentry if it fails */
907 goto out;
908 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700910 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700912 attrs->ia_valid &= (ATTR_UID | ATTR_GID | ATTR_CTIME);
913 buf.xadir = dir;
914 buf.attrs = attrs;
915 buf.inode = inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700917 err = xattr_readdir(fp, reiserfs_chown_xattrs_filler, &buf);
918 if (err) {
919 unlock_kernel();
920 goto out_dir;
921 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700923 err = notify_change(dir, attrs);
924 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700926 out_dir:
927 fput(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700929 out:
930 attrs->ia_valid = ia_valid;
931 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932}
933
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934/* Actual operations that are exported to VFS-land */
935
936/*
937 * Inode operation getxattr()
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800938 * Preliminary locking: we down dentry->d_inode->i_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 */
940ssize_t
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700941reiserfs_getxattr(struct dentry * dentry, const char *name, void *buffer,
942 size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700944 struct reiserfs_xattr_handler *xah = find_xattr_handler_prefix(name);
945 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700947 if (!xah || !reiserfs_xattrs(dentry->d_sb) ||
948 get_inode_sd_version(dentry->d_inode) == STAT_DATA_V1)
949 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700951 reiserfs_read_lock_xattr_i(dentry->d_inode);
952 reiserfs_read_lock_xattrs(dentry->d_sb);
953 err = xah->get(dentry->d_inode, name, buffer, size);
954 reiserfs_read_unlock_xattrs(dentry->d_sb);
955 reiserfs_read_unlock_xattr_i(dentry->d_inode);
956 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957}
958
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959/*
960 * Inode operation setxattr()
961 *
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800962 * dentry->d_inode->i_mutex down
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 */
964int
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700965reiserfs_setxattr(struct dentry *dentry, const char *name, const void *value,
966 size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700968 struct reiserfs_xattr_handler *xah = find_xattr_handler_prefix(name);
969 int err;
970 int lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700972 if (!xah || !reiserfs_xattrs(dentry->d_sb) ||
973 get_inode_sd_version(dentry->d_inode) == STAT_DATA_V1)
974 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700976 reiserfs_write_lock_xattr_i(dentry->d_inode);
977 lock = !has_xattr_dir(dentry->d_inode);
978 if (lock)
979 reiserfs_write_lock_xattrs(dentry->d_sb);
980 else
981 reiserfs_read_lock_xattrs(dentry->d_sb);
982 err = xah->set(dentry->d_inode, name, value, size, flags);
983 if (lock)
984 reiserfs_write_unlock_xattrs(dentry->d_sb);
985 else
986 reiserfs_read_unlock_xattrs(dentry->d_sb);
987 reiserfs_write_unlock_xattr_i(dentry->d_inode);
988 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989}
990
991/*
992 * Inode operation removexattr()
993 *
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800994 * dentry->d_inode->i_mutex down
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700996int reiserfs_removexattr(struct dentry *dentry, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700998 int err;
999 struct reiserfs_xattr_handler *xah = find_xattr_handler_prefix(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001001 if (!xah || !reiserfs_xattrs(dentry->d_sb) ||
1002 get_inode_sd_version(dentry->d_inode) == STAT_DATA_V1)
1003 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001005 reiserfs_write_lock_xattr_i(dentry->d_inode);
1006 reiserfs_read_lock_xattrs(dentry->d_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001008 /* Deletion pre-operation */
1009 if (xah->del) {
1010 err = xah->del(dentry->d_inode, name);
1011 if (err)
1012 goto out;
1013 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001015 err = reiserfs_xattr_del(dentry->d_inode, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001017 dentry->d_inode->i_ctime = CURRENT_TIME_SEC;
1018 mark_inode_dirty(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001020 out:
1021 reiserfs_read_unlock_xattrs(dentry->d_sb);
1022 reiserfs_write_unlock_xattr_i(dentry->d_inode);
1023 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024}
1025
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026/* This is what filldir will use:
1027 * r_pos will always contain the amount of space required for the entire
1028 * list. If r_pos becomes larger than r_size, we need more space and we
1029 * return an error indicating this. If r_pos is less than r_size, then we've
1030 * filled the buffer successfully and we return success */
1031struct reiserfs_listxattr_buf {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001032 int r_pos;
1033 int r_size;
1034 char *r_buf;
1035 struct inode *r_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036};
1037
1038static int
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001039reiserfs_listxattr_filler(void *buf, const char *name, int namelen,
1040 loff_t offset, ino_t ino, unsigned int d_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001042 struct reiserfs_listxattr_buf *b = (struct reiserfs_listxattr_buf *)buf;
1043 int len = 0;
1044 if (name[0] != '.'
1045 || (namelen != 1 && (name[1] != '.' || namelen != 2))) {
1046 struct reiserfs_xattr_handler *xah =
1047 find_xattr_handler_prefix(name);
1048 if (!xah)
1049 return 0; /* Unsupported xattr name, skip it */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001051 /* We call ->list() twice because the operation isn't required to just
1052 * return the name back - we want to make sure we have enough space */
1053 len += xah->list(b->r_inode, name, namelen, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001055 if (len) {
1056 if (b->r_pos + len + 1 <= b->r_size) {
1057 char *p = b->r_buf + b->r_pos;
1058 p += xah->list(b->r_inode, name, namelen, p);
1059 *p++ = '\0';
1060 }
1061 b->r_pos += len + 1;
1062 }
1063 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001065 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066}
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001067
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068/*
1069 * Inode operation listxattr()
1070 *
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001071 * Preliminary locking: we down dentry->d_inode->i_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001073ssize_t reiserfs_listxattr(struct dentry * dentry, char *buffer, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001075 struct file *fp;
1076 struct dentry *dir;
1077 int err = 0;
1078 struct reiserfs_listxattr_buf buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001080 if (!dentry->d_inode)
1081 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001083 if (!reiserfs_xattrs(dentry->d_sb) ||
1084 get_inode_sd_version(dentry->d_inode) == STAT_DATA_V1)
1085 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001087 reiserfs_read_lock_xattr_i(dentry->d_inode);
1088 reiserfs_read_lock_xattrs(dentry->d_sb);
1089 dir = open_xa_dir(dentry->d_inode, FL_READONLY);
1090 reiserfs_read_unlock_xattrs(dentry->d_sb);
1091 if (IS_ERR(dir)) {
1092 err = PTR_ERR(dir);
1093 if (err == -ENODATA)
1094 err = 0; /* Not an error if there aren't any xattrs */
1095 goto out;
1096 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001098 fp = dentry_open(dir, NULL, O_RDWR);
1099 if (IS_ERR(fp)) {
1100 err = PTR_ERR(fp);
1101 /* dentry_open dputs the dentry if it fails */
1102 goto out;
1103 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001105 buf.r_buf = buffer;
1106 buf.r_size = buffer ? size : 0;
1107 buf.r_pos = 0;
1108 buf.r_inode = dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001110 REISERFS_I(dentry->d_inode)->i_flags |= i_has_xattr_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001112 err = xattr_readdir(fp, reiserfs_listxattr_filler, &buf);
1113 if (err)
1114 goto out_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001116 if (buf.r_pos > buf.r_size && buffer != NULL)
1117 err = -ERANGE;
1118 else
1119 err = buf.r_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001121 out_dir:
1122 fput(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001124 out:
1125 reiserfs_read_unlock_xattr_i(dentry->d_inode);
1126 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127}
1128
1129/* This is the implementation for the xattr plugin infrastructure */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001130static struct list_head xattr_handlers = LIST_HEAD_INIT(xattr_handlers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131static DEFINE_RWLOCK(handler_lock);
1132
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001133static struct reiserfs_xattr_handler *find_xattr_handler_prefix(const char
1134 *prefix)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001136 struct reiserfs_xattr_handler *xah = NULL;
1137 struct list_head *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001139 read_lock(&handler_lock);
1140 list_for_each(p, &xattr_handlers) {
1141 xah = list_entry(p, struct reiserfs_xattr_handler, handlers);
1142 if (strncmp(xah->prefix, prefix, strlen(xah->prefix)) == 0)
1143 break;
1144 xah = NULL;
1145 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001147 read_unlock(&handler_lock);
1148 return xah;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149}
1150
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001151static void __unregister_handlers(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001153 struct reiserfs_xattr_handler *xah;
1154 struct list_head *p, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001156 list_for_each_safe(p, tmp, &xattr_handlers) {
1157 xah = list_entry(p, struct reiserfs_xattr_handler, handlers);
1158 if (xah->exit)
1159 xah->exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001161 list_del_init(p);
1162 }
1163 INIT_LIST_HEAD(&xattr_handlers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164}
1165
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001166int __init reiserfs_xattr_register_handlers(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001168 int err = 0;
1169 struct reiserfs_xattr_handler *xah;
1170 struct list_head *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001172 write_lock(&handler_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001174 /* If we're already initialized, nothing to do */
1175 if (!list_empty(&xattr_handlers)) {
1176 write_unlock(&handler_lock);
1177 return 0;
1178 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001180 /* Add the handlers */
1181 list_add_tail(&user_handler.handlers, &xattr_handlers);
1182 list_add_tail(&trusted_handler.handlers, &xattr_handlers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183#ifdef CONFIG_REISERFS_FS_SECURITY
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001184 list_add_tail(&security_handler.handlers, &xattr_handlers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185#endif
1186#ifdef CONFIG_REISERFS_FS_POSIX_ACL
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001187 list_add_tail(&posix_acl_access_handler.handlers, &xattr_handlers);
1188 list_add_tail(&posix_acl_default_handler.handlers, &xattr_handlers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189#endif
1190
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001191 /* Run initializers, if available */
1192 list_for_each(p, &xattr_handlers) {
1193 xah = list_entry(p, struct reiserfs_xattr_handler, handlers);
1194 if (xah->init) {
1195 err = xah->init();
1196 if (err) {
1197 list_del_init(p);
1198 break;
1199 }
1200 }
1201 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001203 /* Clean up other handlers, if any failed */
1204 if (err)
1205 __unregister_handlers();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001207 write_unlock(&handler_lock);
1208 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209}
1210
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001211void reiserfs_xattr_unregister_handlers(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001213 write_lock(&handler_lock);
1214 __unregister_handlers();
1215 write_unlock(&handler_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216}
1217
1218/* This will catch lookups from the fs root to .reiserfs_priv */
1219static int
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001220xattr_lookup_poison(struct dentry *dentry, struct qstr *q1, struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001222 struct dentry *priv_root = REISERFS_SB(dentry->d_sb)->priv_root;
1223 if (name->len == priv_root->d_name.len &&
1224 name->hash == priv_root->d_name.hash &&
1225 !memcmp(name->name, priv_root->d_name.name, name->len)) {
1226 return -ENOENT;
1227 } else if (q1->len == name->len &&
1228 !memcmp(q1->name, name->name, name->len))
1229 return 0;
1230 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231}
1232
1233static struct dentry_operations xattr_lookup_poison_ops = {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001234 .d_compare = xattr_lookup_poison,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235};
1236
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237/* We need to take a copy of the mount flags since things like
1238 * MS_RDONLY don't get set until *after* we're called.
1239 * mount_flags != mount_options */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001240int reiserfs_xattr_init(struct super_block *s, int mount_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001242 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001244 /* We need generation numbers to ensure that the oid mapping is correct
1245 * v3.5 filesystems don't have them. */
1246 if (!old_format_only(s)) {
1247 set_bit(REISERFS_XATTRS, &(REISERFS_SB(s)->s_mount_opt));
1248 } else if (reiserfs_xattrs_optional(s)) {
1249 /* Old format filesystem, but optional xattrs have been enabled
1250 * at mount time. Error out. */
1251 reiserfs_warning(s, "xattrs/ACLs not supported on pre v3.6 "
1252 "format filesystem. Failing mount.");
1253 err = -EOPNOTSUPP;
1254 goto error;
1255 } else {
1256 /* Old format filesystem, but no optional xattrs have been enabled. This
1257 * means we silently disable xattrs on the filesystem. */
1258 clear_bit(REISERFS_XATTRS, &(REISERFS_SB(s)->s_mount_opt));
1259 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001261 /* If we don't have the privroot located yet - go find it */
1262 if (reiserfs_xattrs(s) && !REISERFS_SB(s)->priv_root) {
1263 struct dentry *dentry;
1264 dentry = lookup_one_len(PRIVROOT_NAME, s->s_root,
1265 strlen(PRIVROOT_NAME));
1266 if (!IS_ERR(dentry)) {
1267 if (!(mount_flags & MS_RDONLY) && !dentry->d_inode) {
1268 struct inode *inode = dentry->d_parent->d_inode;
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001269 mutex_lock(&inode->i_mutex);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001270 err = inode->i_op->mkdir(inode, dentry, 0700);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001271 mutex_unlock(&inode->i_mutex);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001272 if (err) {
1273 dput(dentry);
1274 dentry = NULL;
1275 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001277 if (dentry && dentry->d_inode)
1278 reiserfs_warning(s,
1279 "Created %s on %s - reserved for "
1280 "xattr storage.",
1281 PRIVROOT_NAME,
1282 reiserfs_bdevname
1283 (inode->i_sb));
1284 } else if (!dentry->d_inode) {
1285 dput(dentry);
1286 dentry = NULL;
1287 }
1288 } else
1289 err = PTR_ERR(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001291 if (!err && dentry) {
1292 s->s_root->d_op = &xattr_lookup_poison_ops;
1293 reiserfs_mark_inode_private(dentry->d_inode);
1294 REISERFS_SB(s)->priv_root = dentry;
1295 } else if (!(mount_flags & MS_RDONLY)) { /* xattrs are unavailable */
1296 /* If we're read-only it just means that the dir hasn't been
1297 * created. Not an error -- just no xattrs on the fs. We'll
1298 * check again if we go read-write */
1299 reiserfs_warning(s, "xattrs/ACLs enabled and couldn't "
1300 "find/create .reiserfs_priv. Failing mount.");
1301 err = -EOPNOTSUPP;
1302 }
1303 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001305 error:
1306 /* This is only nonzero if there was an error initializing the xattr
1307 * directory or if there is a condition where we don't support them. */
1308 if (err) {
1309 clear_bit(REISERFS_XATTRS, &(REISERFS_SB(s)->s_mount_opt));
1310 clear_bit(REISERFS_XATTRS_USER, &(REISERFS_SB(s)->s_mount_opt));
1311 clear_bit(REISERFS_POSIXACL, &(REISERFS_SB(s)->s_mount_opt));
1312 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001314 /* The super_block MS_POSIXACL must mirror the (no)acl mount option. */
1315 s->s_flags = s->s_flags & ~MS_POSIXACL;
1316 if (reiserfs_posixacl(s))
1317 s->s_flags |= MS_POSIXACL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001319 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320}
1321
Christoph Hellwigec191572006-02-01 03:06:46 -08001322static int reiserfs_check_acl(struct inode *inode, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323{
Christoph Hellwigec191572006-02-01 03:06:46 -08001324 struct posix_acl *acl;
1325 int error = -EAGAIN; /* do regular unix permission checks by default */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326
Christoph Hellwigec191572006-02-01 03:06:46 -08001327 reiserfs_read_lock_xattr_i(inode);
1328 reiserfs_read_lock_xattrs(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329
Christoph Hellwigec191572006-02-01 03:06:46 -08001330 acl = reiserfs_get_acl(inode, ACL_TYPE_ACCESS);
1331
1332 reiserfs_read_unlock_xattrs(inode->i_sb);
1333 reiserfs_read_unlock_xattr_i(inode);
1334
1335 if (acl) {
1336 if (!IS_ERR(acl)) {
1337 error = posix_acl_permission(inode, acl, mask);
1338 posix_acl_release(acl);
1339 } else if (PTR_ERR(acl) != -ENODATA)
1340 error = PTR_ERR(acl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 }
1342
Christoph Hellwigec191572006-02-01 03:06:46 -08001343 return error;
1344}
1345
1346int reiserfs_permission(struct inode *inode, int mask, struct nameidata *nd)
1347{
1348 /*
1349 * We don't do permission checks on the internal objects.
1350 * Permissions are determined by the "owning" object.
1351 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001352 if (is_reiserfs_priv_object(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 return 0;
1354
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 /*
Christoph Hellwigec191572006-02-01 03:06:46 -08001356 * Stat data v1 doesn't support ACLs.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 */
Christoph Hellwigec191572006-02-01 03:06:46 -08001358 if (get_inode_sd_version(inode) == STAT_DATA_V1)
1359 return generic_permission(inode, mask, NULL);
1360 else
1361 return generic_permission(inode, mask, reiserfs_check_acl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362}