blob: eba037b3338fe8c2c480318d38441d3de65aab51 [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>
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
Jeff Mahoney9b7f3752007-04-23 14:41:17 -070057/* Returns the dentry referring to the root of the extended attribute
58 * directory tree. If it has already been retrieved, it is used. If it
59 * hasn't been created and the flags indicate creation is allowed, we
60 * attempt to create it. On error, we return a pointer-encoded error.
61 */
62static struct dentry *get_xa_root(struct super_block *sb, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070063{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070064 struct dentry *privroot = dget(REISERFS_SB(sb)->priv_root);
65 struct dentry *xaroot;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070067 /* This needs to be created at mount-time */
68 if (!privroot)
Jeff Mahoney9b7f3752007-04-23 14:41:17 -070069 return ERR_PTR(-ENODATA);
70
Jeff Mahoney1173a722007-04-30 15:09:50 -070071 mutex_lock_nested(&privroot->d_inode->i_mutex, I_MUTEX_XATTR);
Jeff Mahoney9b7f3752007-04-23 14:41:17 -070072 if (REISERFS_SB(sb)->xattr_root) {
73 xaroot = dget(REISERFS_SB(sb)->xattr_root);
74 goto out;
75 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070077 xaroot = lookup_one_len(XAROOT_NAME, privroot, strlen(XAROOT_NAME));
78 if (IS_ERR(xaroot)) {
79 goto out;
80 } else if (!xaroot->d_inode) {
Jeff Mahoney9b7f3752007-04-23 14:41:17 -070081 int err = -ENODATA;
82 if (flags == 0 || flags & XATTR_CREATE)
83 err = privroot->d_inode->i_op->mkdir(privroot->d_inode,
84 xaroot, 0700);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070085 if (err) {
86 dput(xaroot);
Jeff Mahoney9b7f3752007-04-23 14:41:17 -070087 xaroot = ERR_PTR(err);
88 goto out;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070089 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070090 }
Jeff Mahoney9b7f3752007-04-23 14:41:17 -070091 REISERFS_SB(sb)->xattr_root = dget(xaroot);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070093 out:
Jeff Mahoney9b7f3752007-04-23 14:41:17 -070094 mutex_unlock(&privroot->d_inode->i_mutex);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070095 dput(privroot);
96 return xaroot;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097}
98
Linus Torvalds1da177e2005-04-16 15:20:36 -070099/* Opens the directory corresponding to the inode's extended attribute store.
100 * If flags allow, the tree to the directory may be created. If creation is
101 * prohibited, -ENODATA is returned. */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700102static struct dentry *open_xa_dir(const struct inode *inode, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700104 struct dentry *xaroot, *xadir;
105 char namebuf[17];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
Jeff Mahoney9b7f3752007-04-23 14:41:17 -0700107 xaroot = get_xa_root(inode->i_sb, flags);
108 if (IS_ERR(xaroot))
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700109 return xaroot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700111 /* ok, we have xaroot open */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700112 snprintf(namebuf, sizeof(namebuf), "%X.%X",
113 le32_to_cpu(INODE_PKEY(inode)->k_objectid),
114 inode->i_generation);
115 xadir = lookup_one_len(namebuf, xaroot, strlen(namebuf));
116 if (IS_ERR(xadir)) {
117 dput(xaroot);
118 return xadir;
119 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700121 if (!xadir->d_inode) {
122 int err;
123 if (flags == 0 || flags & XATTR_CREATE) {
124 /* Although there is nothing else trying to create this directory,
125 * another directory with the same hash may be created, so we need
126 * to protect against that */
127 err =
128 xaroot->d_inode->i_op->mkdir(xaroot->d_inode, xadir,
129 0700);
130 if (err) {
131 dput(xaroot);
132 dput(xadir);
133 return ERR_PTR(err);
134 }
135 }
136 if (!xadir->d_inode) {
137 dput(xaroot);
138 dput(xadir);
139 return ERR_PTR(-ENODATA);
140 }
141 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700143 dput(xaroot);
144 return xadir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145}
146
147/* Returns a dentry corresponding to a specific extended attribute file
148 * for the inode. If flags allow, the file is created. Otherwise, a
149 * valid or negative dentry, or an error is returned. */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700150static struct dentry *get_xa_file_dentry(const struct inode *inode,
151 const char *name, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700153 struct dentry *xadir, *xafile;
154 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700156 xadir = open_xa_dir(inode, flags);
157 if (IS_ERR(xadir)) {
David Howellse231c2e2008-02-07 00:15:26 -0800158 return ERR_CAST(xadir);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700159 } else if (xadir && !xadir->d_inode) {
160 dput(xadir);
161 return ERR_PTR(-ENODATA);
162 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700164 xafile = lookup_one_len(name, xadir, strlen(name));
165 if (IS_ERR(xafile)) {
166 dput(xadir);
David Howellse231c2e2008-02-07 00:15:26 -0800167 return ERR_CAST(xafile);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700168 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700170 if (xafile->d_inode) { /* file exists */
171 if (flags & XATTR_CREATE) {
172 err = -EEXIST;
173 dput(xafile);
174 goto out;
175 }
176 } else if (flags & XATTR_REPLACE || flags & FL_READONLY) {
177 goto out;
178 } else {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800179 /* inode->i_mutex is down, so nothing else can try to create
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700180 * the same xattr */
181 err = xadir->d_inode->i_op->create(xadir->d_inode, xafile,
182 0700 | S_IFREG, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700184 if (err) {
185 dput(xafile);
186 goto out;
187 }
188 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700190 out:
191 dput(xadir);
192 if (err)
193 xafile = ERR_PTR(err);
194 return xafile;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195}
196
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197/* Opens a file pointer to the attribute associated with inode */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700198static struct file *open_xa_file(const struct inode *inode, const char *name,
199 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700201 struct dentry *xafile;
202 struct file *fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700204 xafile = get_xa_file_dentry(inode, name, flags);
205 if (IS_ERR(xafile))
206 return ERR_PTR(PTR_ERR(xafile));
207 else if (!xafile->d_inode) {
208 dput(xafile);
209 return ERR_PTR(-ENODATA);
210 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700212 fp = dentry_open(xafile, NULL, O_RDWR);
213 /* dentry_open dputs the dentry if it fails */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700215 return fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216}
217
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218/*
219 * this is very similar to fs/reiserfs/dir.c:reiserfs_readdir, but
220 * we need to drop the path before calling the filldir struct. That
221 * would be a big performance hit to the non-xattr case, so I've copied
222 * the whole thing for now. --clm
223 *
224 * the big difference is that I go backwards through the directory,
225 * and don't mess with f->f_pos, but the idea is the same. Do some
226 * action on each and every entry in the directory.
227 *
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800228 * we're called with i_mutex held, so there are no worries about the directory
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 * changing underneath us.
230 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700231static int __xattr_readdir(struct file *filp, void *dirent, filldir_t filldir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232{
Josef Sipek1fc5adb2006-12-08 02:37:33 -0800233 struct inode *inode = filp->f_path.dentry->d_inode;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700234 struct cpu_key pos_key; /* key of current position in the directory (key of directory entry) */
235 INITIALIZE_PATH(path_to_entry);
236 struct buffer_head *bh;
237 int entry_num;
238 struct item_head *ih, tmp_ih;
239 int search_res;
240 char *local_buf;
241 loff_t next_pos;
242 char small_buf[32]; /* avoid kmalloc if we can */
243 struct reiserfs_de_head *deh;
244 int d_reclen;
245 char *d_name;
246 off_t d_off;
247 ino_t d_ino;
248 struct reiserfs_dir_entry de;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700250 /* form key for search the next directory entry using f_pos field of
251 file structure */
252 next_pos = max_reiserfs_offset(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700254 while (1) {
255 research:
256 if (next_pos <= DOT_DOT_OFFSET)
257 break;
258 make_cpu_key(&pos_key, inode, next_pos, TYPE_DIRENTRY, 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700260 search_res =
261 search_by_entry_key(inode->i_sb, &pos_key, &path_to_entry,
262 &de);
263 if (search_res == IO_ERROR) {
264 // FIXME: we could just skip part of directory which could
265 // not be read
266 pathrelse(&path_to_entry);
267 return -EIO;
268 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700270 if (search_res == NAME_NOT_FOUND)
271 de.de_entry_num--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700273 set_de_name_and_namelen(&de);
274 entry_num = de.de_entry_num;
275 deh = &(de.de_deh[entry_num]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700277 bh = de.de_bh;
278 ih = de.de_ih;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700280 if (!is_direntry_le_ih(ih)) {
281 reiserfs_warning(inode->i_sb, "not direntry %h", ih);
282 break;
283 }
284 copy_item_head(&tmp_ih, ih);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700286 /* we must have found item, that is item of this directory, */
287 RFALSE(COMP_SHORT_KEYS(&(ih->ih_key), &pos_key),
288 "vs-9000: found item %h does not match to dir we readdir %K",
289 ih, &pos_key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700291 if (deh_offset(deh) <= DOT_DOT_OFFSET) {
292 break;
293 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700295 /* look for the previous entry in the directory */
296 next_pos = deh_offset(deh) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700298 if (!de_visible(deh))
299 /* it is hidden entry */
300 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700302 d_reclen = entry_length(bh, ih, entry_num);
303 d_name = B_I_DEH_ENTRY_FILE_NAME(bh, ih, deh);
304 d_off = deh_offset(deh);
305 d_ino = deh_objectid(deh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700307 if (!d_name[d_reclen - 1])
308 d_reclen = strlen(d_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700310 if (d_reclen > REISERFS_MAX_NAME(inode->i_sb->s_blocksize)) {
311 /* too big to send back to VFS */
312 continue;
313 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700315 /* Ignore the .reiserfs_priv entry */
316 if (reiserfs_xattrs(inode->i_sb) &&
317 !old_format_only(inode->i_sb) &&
318 deh_objectid(deh) ==
319 le32_to_cpu(INODE_PKEY
320 (REISERFS_SB(inode->i_sb)->priv_root->d_inode)->
321 k_objectid))
322 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700324 if (d_reclen <= 32) {
325 local_buf = small_buf;
326 } else {
Pekka Enbergd739b422006-02-01 03:06:43 -0800327 local_buf = kmalloc(d_reclen, GFP_NOFS);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700328 if (!local_buf) {
329 pathrelse(&path_to_entry);
330 return -ENOMEM;
331 }
332 if (item_moved(&tmp_ih, &path_to_entry)) {
Pekka Enbergd739b422006-02-01 03:06:43 -0800333 kfree(local_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700335 /* sigh, must retry. Do this same offset again */
336 next_pos = d_off;
337 goto research;
338 }
339 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700341 // Note, that we copy name to user space via temporary
342 // buffer (local_buf) because filldir will block if
343 // user space buffer is swapped out. At that time
344 // entry can move to somewhere else
345 memcpy(local_buf, d_name, d_reclen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700347 /* the filldir function might need to start transactions,
348 * or do who knows what. Release the path now that we've
349 * copied all the important stuff out of the deh
350 */
351 pathrelse(&path_to_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700353 if (filldir(dirent, local_buf, d_reclen, d_off, d_ino,
354 DT_UNKNOWN) < 0) {
355 if (local_buf != small_buf) {
Pekka Enbergd739b422006-02-01 03:06:43 -0800356 kfree(local_buf);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700357 }
358 goto end;
359 }
360 if (local_buf != small_buf) {
Pekka Enbergd739b422006-02-01 03:06:43 -0800361 kfree(local_buf);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700362 }
363 } /* while */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700365 end:
366 pathrelse(&path_to_entry);
367 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368}
369
370/*
371 * this could be done with dedicated readdir ops for the xattr files,
372 * but I want to get something working asap
373 * this is stolen from vfs_readdir
374 *
375 */
376static
377int xattr_readdir(struct file *file, filldir_t filler, void *buf)
378{
Josef Sipek1fc5adb2006-12-08 02:37:33 -0800379 struct inode *inode = file->f_path.dentry->d_inode;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700380 int res = -ENOTDIR;
381 if (!file->f_op || !file->f_op->readdir)
382 goto out;
Ingo Molnar4df46242006-08-27 01:23:56 -0700383 mutex_lock_nested(&inode->i_mutex, I_MUTEX_XATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384// down(&inode->i_zombie);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700385 res = -ENOENT;
386 if (!IS_DEADDIR(inode)) {
387 lock_kernel();
388 res = __xattr_readdir(file, buf, filler);
389 unlock_kernel();
390 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391// up(&inode->i_zombie);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800392 mutex_unlock(&inode->i_mutex);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700393 out:
394 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395}
396
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397/* Internal operations on file data */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700398static inline void reiserfs_put_page(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700400 kunmap(page);
401 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402}
403
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700404static struct page *reiserfs_get_page(struct inode *dir, unsigned long n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700406 struct address_space *mapping = dir->i_mapping;
407 struct page *page;
408 /* We can deadlock if we try to free dentries,
409 and an unlink/rmdir has just occured - GFP_NOFS avoids this */
Al Viroc4cdd032005-10-21 03:22:39 -0400410 mapping_set_gfp_mask(mapping, GFP_NOFS);
Pekka Enberg090d2b12006-06-23 02:05:08 -0700411 page = read_mapping_page(mapping, n, NULL);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700412 if (!IS_ERR(page)) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700413 kmap(page);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700414 if (PageError(page))
415 goto fail;
416 }
417 return page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700419 fail:
420 reiserfs_put_page(page);
421 return ERR_PTR(-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422}
423
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700424static inline __u32 xattr_hash(const char *msg, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700426 return csum_partial(msg, len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427}
428
Vladimir Savelievba9d8ce2007-10-16 01:25:14 -0700429int reiserfs_commit_write(struct file *f, struct page *page,
430 unsigned from, unsigned to);
431int reiserfs_prepare_write(struct file *f, struct page *page,
432 unsigned from, unsigned to);
433
434
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435/* Generic extended attribute operations that can be used by xa plugins */
436
437/*
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800438 * inode->i_mutex: down
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 */
440int
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700441reiserfs_xattr_set(struct inode *inode, const char *name, const void *buffer,
442 size_t buffer_size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700444 int err = 0;
445 struct file *fp;
446 struct page *page;
447 char *data;
448 struct address_space *mapping;
449 size_t file_pos = 0;
450 size_t buffer_pos = 0;
451 struct inode *xinode;
452 struct iattr newattrs;
453 __u32 xahash = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700455 if (get_inode_sd_version(inode) == STAT_DATA_V1)
456 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700458 /* Empty xattrs are ok, they're just empty files, no hash */
459 if (buffer && buffer_size)
460 xahash = xattr_hash(buffer, buffer_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700462 open_file:
463 fp = open_xa_file(inode, name, flags);
464 if (IS_ERR(fp)) {
465 err = PTR_ERR(fp);
466 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
Josef Sipek1fc5adb2006-12-08 02:37:33 -0800469 xinode = fp->f_path.dentry->d_inode;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700470 REISERFS_I(inode)->i_flags |= i_has_xattr_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700472 /* we need to copy it off.. */
473 if (xinode->i_nlink > 1) {
474 fput(fp);
475 err = reiserfs_xattr_del(inode, name);
476 if (err < 0)
477 goto out;
478 /* We just killed the old one, we're not replacing anymore */
479 if (flags & XATTR_REPLACE)
480 flags &= ~XATTR_REPLACE;
481 goto open_file;
482 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700484 /* Resize it so we're ok to write there */
485 newattrs.ia_size = buffer_size;
486 newattrs.ia_valid = ATTR_SIZE | ATTR_CTIME;
Jeff Mahoney75983922007-10-18 23:39:23 -0700487 mutex_lock_nested(&xinode->i_mutex, I_MUTEX_XATTR);
Josef Sipek1fc5adb2006-12-08 02:37:33 -0800488 err = notify_change(fp->f_path.dentry, &newattrs);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700489 if (err)
490 goto out_filp;
491
492 mapping = xinode->i_mapping;
493 while (buffer_pos < buffer_size || buffer_pos == 0) {
494 size_t chunk;
495 size_t skip = 0;
496 size_t page_offset = (file_pos & (PAGE_CACHE_SIZE - 1));
497 if (buffer_size - buffer_pos > PAGE_CACHE_SIZE)
498 chunk = PAGE_CACHE_SIZE;
499 else
500 chunk = buffer_size - buffer_pos;
501
502 page = reiserfs_get_page(xinode, file_pos >> PAGE_CACHE_SHIFT);
503 if (IS_ERR(page)) {
504 err = PTR_ERR(page);
505 goto out_filp;
506 }
507
508 lock_page(page);
509 data = page_address(page);
510
511 if (file_pos == 0) {
512 struct reiserfs_xattr_header *rxh;
513 skip = file_pos = sizeof(struct reiserfs_xattr_header);
514 if (chunk + skip > PAGE_CACHE_SIZE)
515 chunk = PAGE_CACHE_SIZE - skip;
516 rxh = (struct reiserfs_xattr_header *)data;
517 rxh->h_magic = cpu_to_le32(REISERFS_XATTR_MAGIC);
518 rxh->h_hash = cpu_to_le32(xahash);
519 }
520
Vladimir Savelievba9d8ce2007-10-16 01:25:14 -0700521 err = reiserfs_prepare_write(fp, page, page_offset,
522 page_offset + chunk + skip);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700523 if (!err) {
524 if (buffer)
525 memcpy(data + skip, buffer + buffer_pos, chunk);
526 err =
Vladimir Savelievba9d8ce2007-10-16 01:25:14 -0700527 reiserfs_commit_write(fp, page, page_offset,
528 page_offset + chunk +
529 skip);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700530 }
531 unlock_page(page);
532 reiserfs_put_page(page);
533 buffer_pos += chunk;
534 file_pos += chunk;
535 skip = 0;
536 if (err || buffer_size == 0 || !buffer)
537 break;
538 }
539
540 /* We can't mark the inode dirty if it's not hashed. This is the case
541 * when we're inheriting the default ACL. If we dirty it, the inode
542 * gets marked dirty, but won't (ever) make it onto the dirty list until
543 * it's synced explicitly to clear I_DIRTY. This is bad. */
544 if (!hlist_unhashed(&inode->i_hash)) {
545 inode->i_ctime = CURRENT_TIME_SEC;
546 mark_inode_dirty(inode);
547 }
548
549 out_filp:
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800550 mutex_unlock(&xinode->i_mutex);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700551 fput(fp);
552
553 out:
554 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555}
556
557/*
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800558 * inode->i_mutex: down
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 */
560int
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700561reiserfs_xattr_get(const struct inode *inode, const char *name, void *buffer,
562 size_t buffer_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700564 ssize_t err = 0;
565 struct file *fp;
566 size_t isize;
567 size_t file_pos = 0;
568 size_t buffer_pos = 0;
569 struct page *page;
570 struct inode *xinode;
571 __u32 hash = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700573 if (name == NULL)
574 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700576 /* We can't have xattrs attached to v1 items since they don't have
577 * generation numbers */
578 if (get_inode_sd_version(inode) == STAT_DATA_V1)
579 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700581 fp = open_xa_file(inode, name, FL_READONLY);
582 if (IS_ERR(fp)) {
583 err = PTR_ERR(fp);
584 goto out;
585 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
Josef Sipek1fc5adb2006-12-08 02:37:33 -0800587 xinode = fp->f_path.dentry->d_inode;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700588 isize = xinode->i_size;
589 REISERFS_I(inode)->i_flags |= i_has_xattr_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700591 /* Just return the size needed */
592 if (buffer == NULL) {
593 err = isize - sizeof(struct reiserfs_xattr_header);
594 goto out_dput;
595 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700597 if (buffer_size < isize - sizeof(struct reiserfs_xattr_header)) {
598 err = -ERANGE;
599 goto out_dput;
600 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700602 while (file_pos < isize) {
603 size_t chunk;
604 char *data;
605 size_t skip = 0;
606 if (isize - file_pos > PAGE_CACHE_SIZE)
607 chunk = PAGE_CACHE_SIZE;
608 else
609 chunk = isize - file_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700611 page = reiserfs_get_page(xinode, file_pos >> PAGE_CACHE_SHIFT);
612 if (IS_ERR(page)) {
613 err = PTR_ERR(page);
614 goto out_dput;
615 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700617 lock_page(page);
618 data = page_address(page);
619 if (file_pos == 0) {
620 struct reiserfs_xattr_header *rxh =
621 (struct reiserfs_xattr_header *)data;
622 skip = file_pos = sizeof(struct reiserfs_xattr_header);
623 chunk -= skip;
624 /* Magic doesn't match up.. */
625 if (rxh->h_magic != cpu_to_le32(REISERFS_XATTR_MAGIC)) {
626 unlock_page(page);
627 reiserfs_put_page(page);
628 reiserfs_warning(inode->i_sb,
629 "Invalid magic for xattr (%s) "
630 "associated with %k", name,
631 INODE_PKEY(inode));
632 err = -EIO;
633 goto out_dput;
634 }
635 hash = le32_to_cpu(rxh->h_hash);
636 }
637 memcpy(buffer + buffer_pos, data + skip, chunk);
638 unlock_page(page);
639 reiserfs_put_page(page);
640 file_pos += chunk;
641 buffer_pos += chunk;
642 skip = 0;
643 }
644 err = isize - sizeof(struct reiserfs_xattr_header);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700646 if (xattr_hash(buffer, isize - sizeof(struct reiserfs_xattr_header)) !=
647 hash) {
648 reiserfs_warning(inode->i_sb,
649 "Invalid hash for xattr (%s) associated "
650 "with %k", name, INODE_PKEY(inode));
651 err = -EIO;
652 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700654 out_dput:
655 fput(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700657 out:
658 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659}
660
661static int
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700662__reiserfs_xattr_del(struct dentry *xadir, const char *name, int namelen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700664 struct dentry *dentry;
665 struct inode *dir = xadir->d_inode;
666 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700668 dentry = lookup_one_len(name, xadir, namelen);
669 if (IS_ERR(dentry)) {
670 err = PTR_ERR(dentry);
671 goto out;
672 } else if (!dentry->d_inode) {
673 err = -ENODATA;
674 goto out_file;
675 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700677 /* Skip directories.. */
678 if (S_ISDIR(dentry->d_inode->i_mode))
679 goto out_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700681 if (!is_reiserfs_priv_object(dentry->d_inode)) {
682 reiserfs_warning(dir->i_sb, "OID %08x [%.*s/%.*s] doesn't have "
683 "priv flag set [parent is %sset].",
684 le32_to_cpu(INODE_PKEY(dentry->d_inode)->
685 k_objectid), xadir->d_name.len,
686 xadir->d_name.name, namelen, name,
687 is_reiserfs_priv_object(xadir->
688 d_inode) ? "" :
689 "not ");
690 dput(dentry);
691 return -EIO;
692 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700694 err = dir->i_op->unlink(dir, dentry);
695 if (!err)
696 d_delete(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700698 out_file:
699 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700701 out:
702 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703}
704
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700705int reiserfs_xattr_del(struct inode *inode, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700707 struct dentry *dir;
708 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700710 dir = open_xa_dir(inode, FL_READONLY);
711 if (IS_ERR(dir)) {
712 err = PTR_ERR(dir);
713 goto out;
714 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700716 err = __reiserfs_xattr_del(dir, name, strlen(name));
717 dput(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700719 if (!err) {
720 inode->i_ctime = CURRENT_TIME_SEC;
721 mark_inode_dirty(inode);
722 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700724 out:
725 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726}
727
728/* The following are side effects of other operations that aren't explicitly
729 * modifying extended attributes. This includes operations such as permissions
730 * or ownership changes, object deletions, etc. */
731
732static int
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700733reiserfs_delete_xattrs_filler(void *buf, const char *name, int namelen,
David Howellsafefdbb2006-10-03 01:13:46 -0700734 loff_t offset, u64 ino, unsigned int d_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700736 struct dentry *xadir = (struct dentry *)buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700738 return __reiserfs_xattr_del(xadir, name, namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
740}
741
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800742/* This is called w/ inode->i_mutex downed */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700743int reiserfs_delete_xattrs(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700745 struct file *fp;
746 struct dentry *dir, *root;
747 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700749 /* Skip out, an xattr has no xattrs associated with it */
750 if (is_reiserfs_priv_object(inode) ||
751 get_inode_sd_version(inode) == STAT_DATA_V1 ||
752 !reiserfs_xattrs(inode->i_sb)) {
753 return 0;
754 }
755 reiserfs_read_lock_xattrs(inode->i_sb);
756 dir = open_xa_dir(inode, FL_READONLY);
757 reiserfs_read_unlock_xattrs(inode->i_sb);
758 if (IS_ERR(dir)) {
759 err = PTR_ERR(dir);
760 goto out;
761 } else if (!dir->d_inode) {
762 dput(dir);
763 return 0;
764 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700766 fp = dentry_open(dir, NULL, O_RDWR);
767 if (IS_ERR(fp)) {
768 err = PTR_ERR(fp);
769 /* dentry_open dputs the dentry if it fails */
770 goto out;
771 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700773 lock_kernel();
774 err = xattr_readdir(fp, reiserfs_delete_xattrs_filler, dir);
775 if (err) {
776 unlock_kernel();
777 goto out_dir;
778 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700780 /* Leftovers besides . and .. -- that's not good. */
781 if (dir->d_inode->i_nlink <= 2) {
Jeff Mahoney9b7f3752007-04-23 14:41:17 -0700782 root = get_xa_root(inode->i_sb, XATTR_REPLACE);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700783 reiserfs_write_lock_xattrs(inode->i_sb);
784 err = vfs_rmdir(root->d_inode, dir);
785 reiserfs_write_unlock_xattrs(inode->i_sb);
786 dput(root);
787 } else {
788 reiserfs_warning(inode->i_sb,
789 "Couldn't remove all entries in directory");
790 }
791 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700793 out_dir:
794 fput(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700796 out:
797 if (!err)
798 REISERFS_I(inode)->i_flags =
799 REISERFS_I(inode)->i_flags & ~i_has_xattr_dir;
800 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801}
802
803struct reiserfs_chown_buf {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700804 struct inode *inode;
805 struct dentry *xadir;
806 struct iattr *attrs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807};
808
809/* XXX: If there is a better way to do this, I'd love to hear about it */
810static int
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700811reiserfs_chown_xattrs_filler(void *buf, const char *name, int namelen,
David Howellsafefdbb2006-10-03 01:13:46 -0700812 loff_t offset, u64 ino, unsigned int d_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700814 struct reiserfs_chown_buf *chown_buf = (struct reiserfs_chown_buf *)buf;
815 struct dentry *xafile, *xadir = chown_buf->xadir;
816 struct iattr *attrs = chown_buf->attrs;
817 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700819 xafile = lookup_one_len(name, xadir, namelen);
820 if (IS_ERR(xafile))
821 return PTR_ERR(xafile);
822 else if (!xafile->d_inode) {
823 dput(xafile);
824 return -ENODATA;
825 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700827 if (!S_ISDIR(xafile->d_inode->i_mode))
828 err = notify_change(xafile, attrs);
829 dput(xafile);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700831 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832}
833
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700834int reiserfs_chown_xattrs(struct inode *inode, struct iattr *attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700836 struct file *fp;
837 struct dentry *dir;
838 int err = 0;
839 struct reiserfs_chown_buf buf;
840 unsigned int ia_valid = attrs->ia_valid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700842 /* Skip out, an xattr has no xattrs associated with it */
843 if (is_reiserfs_priv_object(inode) ||
844 get_inode_sd_version(inode) == STAT_DATA_V1 ||
845 !reiserfs_xattrs(inode->i_sb)) {
846 return 0;
847 }
848 reiserfs_read_lock_xattrs(inode->i_sb);
849 dir = open_xa_dir(inode, FL_READONLY);
850 reiserfs_read_unlock_xattrs(inode->i_sb);
851 if (IS_ERR(dir)) {
852 if (PTR_ERR(dir) != -ENODATA)
853 err = PTR_ERR(dir);
854 goto out;
855 } else if (!dir->d_inode) {
856 dput(dir);
857 goto out;
858 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700860 fp = dentry_open(dir, NULL, O_RDWR);
861 if (IS_ERR(fp)) {
862 err = PTR_ERR(fp);
863 /* dentry_open dputs the dentry if it fails */
864 goto out;
865 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700867 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700869 attrs->ia_valid &= (ATTR_UID | ATTR_GID | ATTR_CTIME);
870 buf.xadir = dir;
871 buf.attrs = attrs;
872 buf.inode = inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700874 err = xattr_readdir(fp, reiserfs_chown_xattrs_filler, &buf);
875 if (err) {
876 unlock_kernel();
877 goto out_dir;
878 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700880 err = notify_change(dir, attrs);
881 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700883 out_dir:
884 fput(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700886 out:
887 attrs->ia_valid = ia_valid;
888 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889}
890
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891/* Actual operations that are exported to VFS-land */
892
893/*
894 * Inode operation getxattr()
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800895 * Preliminary locking: we down dentry->d_inode->i_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 */
897ssize_t
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700898reiserfs_getxattr(struct dentry * dentry, const char *name, void *buffer,
899 size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700901 struct reiserfs_xattr_handler *xah = find_xattr_handler_prefix(name);
902 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700904 if (!xah || !reiserfs_xattrs(dentry->d_sb) ||
905 get_inode_sd_version(dentry->d_inode) == STAT_DATA_V1)
906 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700908 reiserfs_read_lock_xattr_i(dentry->d_inode);
909 reiserfs_read_lock_xattrs(dentry->d_sb);
910 err = xah->get(dentry->d_inode, name, buffer, size);
911 reiserfs_read_unlock_xattrs(dentry->d_sb);
912 reiserfs_read_unlock_xattr_i(dentry->d_inode);
913 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914}
915
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916/*
917 * Inode operation setxattr()
918 *
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800919 * dentry->d_inode->i_mutex down
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 */
921int
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700922reiserfs_setxattr(struct dentry *dentry, const char *name, const void *value,
923 size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700925 struct reiserfs_xattr_handler *xah = find_xattr_handler_prefix(name);
926 int err;
927 int lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700929 if (!xah || !reiserfs_xattrs(dentry->d_sb) ||
930 get_inode_sd_version(dentry->d_inode) == STAT_DATA_V1)
931 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700933 reiserfs_write_lock_xattr_i(dentry->d_inode);
934 lock = !has_xattr_dir(dentry->d_inode);
935 if (lock)
936 reiserfs_write_lock_xattrs(dentry->d_sb);
937 else
938 reiserfs_read_lock_xattrs(dentry->d_sb);
939 err = xah->set(dentry->d_inode, name, value, size, flags);
940 if (lock)
941 reiserfs_write_unlock_xattrs(dentry->d_sb);
942 else
943 reiserfs_read_unlock_xattrs(dentry->d_sb);
944 reiserfs_write_unlock_xattr_i(dentry->d_inode);
945 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946}
947
948/*
949 * Inode operation removexattr()
950 *
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800951 * dentry->d_inode->i_mutex down
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700953int reiserfs_removexattr(struct dentry *dentry, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700955 int err;
956 struct reiserfs_xattr_handler *xah = find_xattr_handler_prefix(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700958 if (!xah || !reiserfs_xattrs(dentry->d_sb) ||
959 get_inode_sd_version(dentry->d_inode) == STAT_DATA_V1)
960 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700962 reiserfs_write_lock_xattr_i(dentry->d_inode);
963 reiserfs_read_lock_xattrs(dentry->d_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700965 /* Deletion pre-operation */
966 if (xah->del) {
967 err = xah->del(dentry->d_inode, name);
968 if (err)
969 goto out;
970 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700972 err = reiserfs_xattr_del(dentry->d_inode, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700974 dentry->d_inode->i_ctime = CURRENT_TIME_SEC;
975 mark_inode_dirty(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700977 out:
978 reiserfs_read_unlock_xattrs(dentry->d_sb);
979 reiserfs_write_unlock_xattr_i(dentry->d_inode);
980 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981}
982
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983/* This is what filldir will use:
984 * r_pos will always contain the amount of space required for the entire
985 * list. If r_pos becomes larger than r_size, we need more space and we
986 * return an error indicating this. If r_pos is less than r_size, then we've
987 * filled the buffer successfully and we return success */
988struct reiserfs_listxattr_buf {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700989 int r_pos;
990 int r_size;
991 char *r_buf;
992 struct inode *r_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993};
994
995static int
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700996reiserfs_listxattr_filler(void *buf, const char *name, int namelen,
David Howellsafefdbb2006-10-03 01:13:46 -0700997 loff_t offset, u64 ino, unsigned int d_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700999 struct reiserfs_listxattr_buf *b = (struct reiserfs_listxattr_buf *)buf;
1000 int len = 0;
1001 if (name[0] != '.'
1002 || (namelen != 1 && (name[1] != '.' || namelen != 2))) {
1003 struct reiserfs_xattr_handler *xah =
1004 find_xattr_handler_prefix(name);
1005 if (!xah)
1006 return 0; /* Unsupported xattr name, skip it */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001008 /* We call ->list() twice because the operation isn't required to just
1009 * return the name back - we want to make sure we have enough space */
1010 len += xah->list(b->r_inode, name, namelen, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001012 if (len) {
1013 if (b->r_pos + len + 1 <= b->r_size) {
1014 char *p = b->r_buf + b->r_pos;
1015 p += xah->list(b->r_inode, name, namelen, p);
1016 *p++ = '\0';
1017 }
1018 b->r_pos += len + 1;
1019 }
1020 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001022 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023}
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001024
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025/*
1026 * Inode operation listxattr()
1027 *
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001028 * Preliminary locking: we down dentry->d_inode->i_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001030ssize_t reiserfs_listxattr(struct dentry * dentry, char *buffer, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001032 struct file *fp;
1033 struct dentry *dir;
1034 int err = 0;
1035 struct reiserfs_listxattr_buf buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001037 if (!dentry->d_inode)
1038 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001040 if (!reiserfs_xattrs(dentry->d_sb) ||
1041 get_inode_sd_version(dentry->d_inode) == STAT_DATA_V1)
1042 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001044 reiserfs_read_lock_xattr_i(dentry->d_inode);
1045 reiserfs_read_lock_xattrs(dentry->d_sb);
1046 dir = open_xa_dir(dentry->d_inode, FL_READONLY);
1047 reiserfs_read_unlock_xattrs(dentry->d_sb);
1048 if (IS_ERR(dir)) {
1049 err = PTR_ERR(dir);
1050 if (err == -ENODATA)
1051 err = 0; /* Not an error if there aren't any xattrs */
1052 goto out;
1053 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001055 fp = dentry_open(dir, NULL, O_RDWR);
1056 if (IS_ERR(fp)) {
1057 err = PTR_ERR(fp);
1058 /* dentry_open dputs the dentry if it fails */
1059 goto out;
1060 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001062 buf.r_buf = buffer;
1063 buf.r_size = buffer ? size : 0;
1064 buf.r_pos = 0;
1065 buf.r_inode = dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001067 REISERFS_I(dentry->d_inode)->i_flags |= i_has_xattr_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001069 err = xattr_readdir(fp, reiserfs_listxattr_filler, &buf);
1070 if (err)
1071 goto out_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001073 if (buf.r_pos > buf.r_size && buffer != NULL)
1074 err = -ERANGE;
1075 else
1076 err = buf.r_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001078 out_dir:
1079 fput(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001081 out:
1082 reiserfs_read_unlock_xattr_i(dentry->d_inode);
1083 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084}
1085
1086/* This is the implementation for the xattr plugin infrastructure */
Denis Chengbcf11cb2008-02-06 01:37:40 -08001087static LIST_HEAD(xattr_handlers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088static DEFINE_RWLOCK(handler_lock);
1089
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001090static struct reiserfs_xattr_handler *find_xattr_handler_prefix(const char
1091 *prefix)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001093 struct reiserfs_xattr_handler *xah = NULL;
1094 struct list_head *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001096 read_lock(&handler_lock);
1097 list_for_each(p, &xattr_handlers) {
1098 xah = list_entry(p, struct reiserfs_xattr_handler, handlers);
1099 if (strncmp(xah->prefix, prefix, strlen(xah->prefix)) == 0)
1100 break;
1101 xah = NULL;
1102 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001104 read_unlock(&handler_lock);
1105 return xah;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106}
1107
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001108static void __unregister_handlers(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001110 struct reiserfs_xattr_handler *xah;
1111 struct list_head *p, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001113 list_for_each_safe(p, tmp, &xattr_handlers) {
1114 xah = list_entry(p, struct reiserfs_xattr_handler, handlers);
1115 if (xah->exit)
1116 xah->exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001118 list_del_init(p);
1119 }
1120 INIT_LIST_HEAD(&xattr_handlers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121}
1122
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001123int __init reiserfs_xattr_register_handlers(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001125 int err = 0;
1126 struct reiserfs_xattr_handler *xah;
1127 struct list_head *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001129 write_lock(&handler_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001131 /* If we're already initialized, nothing to do */
1132 if (!list_empty(&xattr_handlers)) {
1133 write_unlock(&handler_lock);
1134 return 0;
1135 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001137 /* Add the handlers */
1138 list_add_tail(&user_handler.handlers, &xattr_handlers);
1139 list_add_tail(&trusted_handler.handlers, &xattr_handlers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140#ifdef CONFIG_REISERFS_FS_SECURITY
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001141 list_add_tail(&security_handler.handlers, &xattr_handlers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142#endif
1143#ifdef CONFIG_REISERFS_FS_POSIX_ACL
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001144 list_add_tail(&posix_acl_access_handler.handlers, &xattr_handlers);
1145 list_add_tail(&posix_acl_default_handler.handlers, &xattr_handlers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146#endif
1147
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001148 /* Run initializers, if available */
1149 list_for_each(p, &xattr_handlers) {
1150 xah = list_entry(p, struct reiserfs_xattr_handler, handlers);
1151 if (xah->init) {
1152 err = xah->init();
1153 if (err) {
1154 list_del_init(p);
1155 break;
1156 }
1157 }
1158 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001160 /* Clean up other handlers, if any failed */
1161 if (err)
1162 __unregister_handlers();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001164 write_unlock(&handler_lock);
1165 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166}
1167
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001168void reiserfs_xattr_unregister_handlers(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001170 write_lock(&handler_lock);
1171 __unregister_handlers();
1172 write_unlock(&handler_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173}
1174
1175/* This will catch lookups from the fs root to .reiserfs_priv */
1176static int
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001177xattr_lookup_poison(struct dentry *dentry, struct qstr *q1, struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001179 struct dentry *priv_root = REISERFS_SB(dentry->d_sb)->priv_root;
1180 if (name->len == priv_root->d_name.len &&
1181 name->hash == priv_root->d_name.hash &&
1182 !memcmp(name->name, priv_root->d_name.name, name->len)) {
1183 return -ENOENT;
1184 } else if (q1->len == name->len &&
1185 !memcmp(q1->name, name->name, name->len))
1186 return 0;
1187 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188}
1189
1190static struct dentry_operations xattr_lookup_poison_ops = {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001191 .d_compare = xattr_lookup_poison,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192};
1193
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194/* We need to take a copy of the mount flags since things like
1195 * MS_RDONLY don't get set until *after* we're called.
1196 * mount_flags != mount_options */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001197int reiserfs_xattr_init(struct super_block *s, int mount_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001199 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001201 /* We need generation numbers to ensure that the oid mapping is correct
1202 * v3.5 filesystems don't have them. */
1203 if (!old_format_only(s)) {
1204 set_bit(REISERFS_XATTRS, &(REISERFS_SB(s)->s_mount_opt));
1205 } else if (reiserfs_xattrs_optional(s)) {
1206 /* Old format filesystem, but optional xattrs have been enabled
1207 * at mount time. Error out. */
1208 reiserfs_warning(s, "xattrs/ACLs not supported on pre v3.6 "
1209 "format filesystem. Failing mount.");
1210 err = -EOPNOTSUPP;
1211 goto error;
1212 } else {
1213 /* Old format filesystem, but no optional xattrs have been enabled. This
1214 * means we silently disable xattrs on the filesystem. */
1215 clear_bit(REISERFS_XATTRS, &(REISERFS_SB(s)->s_mount_opt));
1216 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001218 /* If we don't have the privroot located yet - go find it */
1219 if (reiserfs_xattrs(s) && !REISERFS_SB(s)->priv_root) {
1220 struct dentry *dentry;
1221 dentry = lookup_one_len(PRIVROOT_NAME, s->s_root,
1222 strlen(PRIVROOT_NAME));
1223 if (!IS_ERR(dentry)) {
1224 if (!(mount_flags & MS_RDONLY) && !dentry->d_inode) {
1225 struct inode *inode = dentry->d_parent->d_inode;
Jeff Mahoney75983922007-10-18 23:39:23 -07001226 mutex_lock_nested(&inode->i_mutex,
1227 I_MUTEX_XATTR);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001228 err = inode->i_op->mkdir(inode, dentry, 0700);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001229 mutex_unlock(&inode->i_mutex);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001230 if (err) {
1231 dput(dentry);
1232 dentry = NULL;
1233 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001235 if (dentry && dentry->d_inode)
1236 reiserfs_warning(s,
1237 "Created %s on %s - reserved for "
1238 "xattr storage.",
1239 PRIVROOT_NAME,
1240 reiserfs_bdevname
1241 (inode->i_sb));
1242 } else if (!dentry->d_inode) {
1243 dput(dentry);
1244 dentry = NULL;
1245 }
1246 } else
1247 err = PTR_ERR(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001249 if (!err && dentry) {
1250 s->s_root->d_op = &xattr_lookup_poison_ops;
1251 reiserfs_mark_inode_private(dentry->d_inode);
1252 REISERFS_SB(s)->priv_root = dentry;
1253 } else if (!(mount_flags & MS_RDONLY)) { /* xattrs are unavailable */
1254 /* If we're read-only it just means that the dir hasn't been
1255 * created. Not an error -- just no xattrs on the fs. We'll
1256 * check again if we go read-write */
1257 reiserfs_warning(s, "xattrs/ACLs enabled and couldn't "
1258 "find/create .reiserfs_priv. Failing mount.");
1259 err = -EOPNOTSUPP;
1260 }
1261 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001263 error:
1264 /* This is only nonzero if there was an error initializing the xattr
1265 * directory or if there is a condition where we don't support them. */
1266 if (err) {
1267 clear_bit(REISERFS_XATTRS, &(REISERFS_SB(s)->s_mount_opt));
1268 clear_bit(REISERFS_XATTRS_USER, &(REISERFS_SB(s)->s_mount_opt));
1269 clear_bit(REISERFS_POSIXACL, &(REISERFS_SB(s)->s_mount_opt));
1270 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001272 /* The super_block MS_POSIXACL must mirror the (no)acl mount option. */
1273 s->s_flags = s->s_flags & ~MS_POSIXACL;
1274 if (reiserfs_posixacl(s))
1275 s->s_flags |= MS_POSIXACL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001277 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278}
1279
Christoph Hellwigec191572006-02-01 03:06:46 -08001280static int reiserfs_check_acl(struct inode *inode, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281{
Christoph Hellwigec191572006-02-01 03:06:46 -08001282 struct posix_acl *acl;
1283 int error = -EAGAIN; /* do regular unix permission checks by default */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284
Christoph Hellwigec191572006-02-01 03:06:46 -08001285 reiserfs_read_lock_xattr_i(inode);
1286 reiserfs_read_lock_xattrs(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287
Christoph Hellwigec191572006-02-01 03:06:46 -08001288 acl = reiserfs_get_acl(inode, ACL_TYPE_ACCESS);
1289
1290 reiserfs_read_unlock_xattrs(inode->i_sb);
1291 reiserfs_read_unlock_xattr_i(inode);
1292
1293 if (acl) {
1294 if (!IS_ERR(acl)) {
1295 error = posix_acl_permission(inode, acl, mask);
1296 posix_acl_release(acl);
1297 } else if (PTR_ERR(acl) != -ENODATA)
1298 error = PTR_ERR(acl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 }
1300
Christoph Hellwigec191572006-02-01 03:06:46 -08001301 return error;
1302}
1303
1304int reiserfs_permission(struct inode *inode, int mask, struct nameidata *nd)
1305{
1306 /*
1307 * We don't do permission checks on the internal objects.
1308 * Permissions are determined by the "owning" object.
1309 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001310 if (is_reiserfs_priv_object(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 return 0;
1312
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 /*
Christoph Hellwigec191572006-02-01 03:06:46 -08001314 * Stat data v1 doesn't support ACLs.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 */
Christoph Hellwigec191572006-02-01 03:06:46 -08001316 if (get_inode_sd_version(inode) == STAT_DATA_V1)
1317 return generic_permission(inode, mask, NULL);
1318 else
1319 return generic_permission(inode, mask, reiserfs_check_acl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320}