blob: cc061bfd437b00b4c3aed01b3e0983fb83dba152 [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 {
371 local_buf =
372 reiserfs_kmalloc(d_reclen, GFP_NOFS, inode->i_sb);
373 if (!local_buf) {
374 pathrelse(&path_to_entry);
375 return -ENOMEM;
376 }
377 if (item_moved(&tmp_ih, &path_to_entry)) {
378 reiserfs_kfree(local_buf, d_reclen,
379 inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700381 /* sigh, must retry. Do this same offset again */
382 next_pos = d_off;
383 goto research;
384 }
385 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700387 // Note, that we copy name to user space via temporary
388 // buffer (local_buf) because filldir will block if
389 // user space buffer is swapped out. At that time
390 // entry can move to somewhere else
391 memcpy(local_buf, d_name, d_reclen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700393 /* the filldir function might need to start transactions,
394 * or do who knows what. Release the path now that we've
395 * copied all the important stuff out of the deh
396 */
397 pathrelse(&path_to_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700399 if (filldir(dirent, local_buf, d_reclen, d_off, d_ino,
400 DT_UNKNOWN) < 0) {
401 if (local_buf != small_buf) {
402 reiserfs_kfree(local_buf, d_reclen,
403 inode->i_sb);
404 }
405 goto end;
406 }
407 if (local_buf != small_buf) {
408 reiserfs_kfree(local_buf, d_reclen, inode->i_sb);
409 }
410 } /* while */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700412 end:
413 pathrelse(&path_to_entry);
414 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415}
416
417/*
418 * this could be done with dedicated readdir ops for the xattr files,
419 * but I want to get something working asap
420 * this is stolen from vfs_readdir
421 *
422 */
423static
424int xattr_readdir(struct file *file, filldir_t filler, void *buf)
425{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700426 struct inode *inode = file->f_dentry->d_inode;
427 int res = -ENOTDIR;
428 if (!file->f_op || !file->f_op->readdir)
429 goto out;
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800430 mutex_lock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431// down(&inode->i_zombie);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700432 res = -ENOENT;
433 if (!IS_DEADDIR(inode)) {
434 lock_kernel();
435 res = __xattr_readdir(file, buf, filler);
436 unlock_kernel();
437 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438// up(&inode->i_zombie);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800439 mutex_unlock(&inode->i_mutex);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700440 out:
441 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442}
443
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444/* Internal operations on file data */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700445static inline void reiserfs_put_page(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700447 kunmap(page);
448 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449}
450
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700451static struct page *reiserfs_get_page(struct inode *dir, unsigned long n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700453 struct address_space *mapping = dir->i_mapping;
454 struct page *page;
455 /* We can deadlock if we try to free dentries,
456 and an unlink/rmdir has just occured - GFP_NOFS avoids this */
Al Viroc4cdd032005-10-21 03:22:39 -0400457 mapping_set_gfp_mask(mapping, GFP_NOFS);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700458 page = read_cache_page(mapping, n,
459 (filler_t *) mapping->a_ops->readpage, NULL);
460 if (!IS_ERR(page)) {
461 wait_on_page_locked(page);
462 kmap(page);
463 if (!PageUptodate(page))
464 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700466 if (PageError(page))
467 goto fail;
468 }
469 return page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700471 fail:
472 reiserfs_put_page(page);
473 return ERR_PTR(-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474}
475
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700476static inline __u32 xattr_hash(const char *msg, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700478 return csum_partial(msg, len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479}
480
481/* Generic extended attribute operations that can be used by xa plugins */
482
483/*
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800484 * inode->i_mutex: down
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 */
486int
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700487reiserfs_xattr_set(struct inode *inode, const char *name, const void *buffer,
488 size_t buffer_size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700490 int err = 0;
491 struct file *fp;
492 struct page *page;
493 char *data;
494 struct address_space *mapping;
495 size_t file_pos = 0;
496 size_t buffer_pos = 0;
497 struct inode *xinode;
498 struct iattr newattrs;
499 __u32 xahash = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700501 if (get_inode_sd_version(inode) == STAT_DATA_V1)
502 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700504 /* Empty xattrs are ok, they're just empty files, no hash */
505 if (buffer && buffer_size)
506 xahash = xattr_hash(buffer, buffer_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700508 open_file:
509 fp = open_xa_file(inode, name, flags);
510 if (IS_ERR(fp)) {
511 err = PTR_ERR(fp);
512 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700515 xinode = fp->f_dentry->d_inode;
516 REISERFS_I(inode)->i_flags |= i_has_xattr_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700518 /* we need to copy it off.. */
519 if (xinode->i_nlink > 1) {
520 fput(fp);
521 err = reiserfs_xattr_del(inode, name);
522 if (err < 0)
523 goto out;
524 /* We just killed the old one, we're not replacing anymore */
525 if (flags & XATTR_REPLACE)
526 flags &= ~XATTR_REPLACE;
527 goto open_file;
528 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700530 /* Resize it so we're ok to write there */
531 newattrs.ia_size = buffer_size;
532 newattrs.ia_valid = ATTR_SIZE | ATTR_CTIME;
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800533 mutex_lock(&xinode->i_mutex);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700534 err = notify_change(fp->f_dentry, &newattrs);
535 if (err)
536 goto out_filp;
537
538 mapping = xinode->i_mapping;
539 while (buffer_pos < buffer_size || buffer_pos == 0) {
540 size_t chunk;
541 size_t skip = 0;
542 size_t page_offset = (file_pos & (PAGE_CACHE_SIZE - 1));
543 if (buffer_size - buffer_pos > PAGE_CACHE_SIZE)
544 chunk = PAGE_CACHE_SIZE;
545 else
546 chunk = buffer_size - buffer_pos;
547
548 page = reiserfs_get_page(xinode, file_pos >> PAGE_CACHE_SHIFT);
549 if (IS_ERR(page)) {
550 err = PTR_ERR(page);
551 goto out_filp;
552 }
553
554 lock_page(page);
555 data = page_address(page);
556
557 if (file_pos == 0) {
558 struct reiserfs_xattr_header *rxh;
559 skip = file_pos = sizeof(struct reiserfs_xattr_header);
560 if (chunk + skip > PAGE_CACHE_SIZE)
561 chunk = PAGE_CACHE_SIZE - skip;
562 rxh = (struct reiserfs_xattr_header *)data;
563 rxh->h_magic = cpu_to_le32(REISERFS_XATTR_MAGIC);
564 rxh->h_hash = cpu_to_le32(xahash);
565 }
566
567 err = mapping->a_ops->prepare_write(fp, page, page_offset,
568 page_offset + chunk + skip);
569 if (!err) {
570 if (buffer)
571 memcpy(data + skip, buffer + buffer_pos, chunk);
572 err =
573 mapping->a_ops->commit_write(fp, page, page_offset,
574 page_offset + chunk +
575 skip);
576 }
577 unlock_page(page);
578 reiserfs_put_page(page);
579 buffer_pos += chunk;
580 file_pos += chunk;
581 skip = 0;
582 if (err || buffer_size == 0 || !buffer)
583 break;
584 }
585
586 /* We can't mark the inode dirty if it's not hashed. This is the case
587 * when we're inheriting the default ACL. If we dirty it, the inode
588 * gets marked dirty, but won't (ever) make it onto the dirty list until
589 * it's synced explicitly to clear I_DIRTY. This is bad. */
590 if (!hlist_unhashed(&inode->i_hash)) {
591 inode->i_ctime = CURRENT_TIME_SEC;
592 mark_inode_dirty(inode);
593 }
594
595 out_filp:
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800596 mutex_unlock(&xinode->i_mutex);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700597 fput(fp);
598
599 out:
600 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601}
602
603/*
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800604 * inode->i_mutex: down
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 */
606int
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700607reiserfs_xattr_get(const struct inode *inode, const char *name, void *buffer,
608 size_t buffer_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700610 ssize_t err = 0;
611 struct file *fp;
612 size_t isize;
613 size_t file_pos = 0;
614 size_t buffer_pos = 0;
615 struct page *page;
616 struct inode *xinode;
617 __u32 hash = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700619 if (name == NULL)
620 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700622 /* We can't have xattrs attached to v1 items since they don't have
623 * generation numbers */
624 if (get_inode_sd_version(inode) == STAT_DATA_V1)
625 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700627 fp = open_xa_file(inode, name, FL_READONLY);
628 if (IS_ERR(fp)) {
629 err = PTR_ERR(fp);
630 goto out;
631 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700633 xinode = fp->f_dentry->d_inode;
634 isize = xinode->i_size;
635 REISERFS_I(inode)->i_flags |= i_has_xattr_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700637 /* Just return the size needed */
638 if (buffer == NULL) {
639 err = isize - sizeof(struct reiserfs_xattr_header);
640 goto out_dput;
641 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700643 if (buffer_size < isize - sizeof(struct reiserfs_xattr_header)) {
644 err = -ERANGE;
645 goto out_dput;
646 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700648 while (file_pos < isize) {
649 size_t chunk;
650 char *data;
651 size_t skip = 0;
652 if (isize - file_pos > PAGE_CACHE_SIZE)
653 chunk = PAGE_CACHE_SIZE;
654 else
655 chunk = isize - file_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700657 page = reiserfs_get_page(xinode, file_pos >> PAGE_CACHE_SHIFT);
658 if (IS_ERR(page)) {
659 err = PTR_ERR(page);
660 goto out_dput;
661 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700663 lock_page(page);
664 data = page_address(page);
665 if (file_pos == 0) {
666 struct reiserfs_xattr_header *rxh =
667 (struct reiserfs_xattr_header *)data;
668 skip = file_pos = sizeof(struct reiserfs_xattr_header);
669 chunk -= skip;
670 /* Magic doesn't match up.. */
671 if (rxh->h_magic != cpu_to_le32(REISERFS_XATTR_MAGIC)) {
672 unlock_page(page);
673 reiserfs_put_page(page);
674 reiserfs_warning(inode->i_sb,
675 "Invalid magic for xattr (%s) "
676 "associated with %k", name,
677 INODE_PKEY(inode));
678 err = -EIO;
679 goto out_dput;
680 }
681 hash = le32_to_cpu(rxh->h_hash);
682 }
683 memcpy(buffer + buffer_pos, data + skip, chunk);
684 unlock_page(page);
685 reiserfs_put_page(page);
686 file_pos += chunk;
687 buffer_pos += chunk;
688 skip = 0;
689 }
690 err = isize - sizeof(struct reiserfs_xattr_header);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700692 if (xattr_hash(buffer, isize - sizeof(struct reiserfs_xattr_header)) !=
693 hash) {
694 reiserfs_warning(inode->i_sb,
695 "Invalid hash for xattr (%s) associated "
696 "with %k", name, INODE_PKEY(inode));
697 err = -EIO;
698 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700700 out_dput:
701 fput(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700703 out:
704 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705}
706
707static int
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700708__reiserfs_xattr_del(struct dentry *xadir, const char *name, int namelen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700710 struct dentry *dentry;
711 struct inode *dir = xadir->d_inode;
712 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700714 dentry = lookup_one_len(name, xadir, namelen);
715 if (IS_ERR(dentry)) {
716 err = PTR_ERR(dentry);
717 goto out;
718 } else if (!dentry->d_inode) {
719 err = -ENODATA;
720 goto out_file;
721 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700723 /* Skip directories.. */
724 if (S_ISDIR(dentry->d_inode->i_mode))
725 goto out_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700727 if (!is_reiserfs_priv_object(dentry->d_inode)) {
728 reiserfs_warning(dir->i_sb, "OID %08x [%.*s/%.*s] doesn't have "
729 "priv flag set [parent is %sset].",
730 le32_to_cpu(INODE_PKEY(dentry->d_inode)->
731 k_objectid), xadir->d_name.len,
732 xadir->d_name.name, namelen, name,
733 is_reiserfs_priv_object(xadir->
734 d_inode) ? "" :
735 "not ");
736 dput(dentry);
737 return -EIO;
738 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700740 err = dir->i_op->unlink(dir, dentry);
741 if (!err)
742 d_delete(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700744 out_file:
745 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700747 out:
748 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749}
750
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700751int reiserfs_xattr_del(struct inode *inode, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700753 struct dentry *dir;
754 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700756 dir = open_xa_dir(inode, FL_READONLY);
757 if (IS_ERR(dir)) {
758 err = PTR_ERR(dir);
759 goto out;
760 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700762 err = __reiserfs_xattr_del(dir, name, strlen(name));
763 dput(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700765 if (!err) {
766 inode->i_ctime = CURRENT_TIME_SEC;
767 mark_inode_dirty(inode);
768 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700770 out:
771 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772}
773
774/* The following are side effects of other operations that aren't explicitly
775 * modifying extended attributes. This includes operations such as permissions
776 * or ownership changes, object deletions, etc. */
777
778static int
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700779reiserfs_delete_xattrs_filler(void *buf, const char *name, int namelen,
780 loff_t offset, ino_t ino, unsigned int d_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700782 struct dentry *xadir = (struct dentry *)buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700784 return __reiserfs_xattr_del(xadir, name, namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
786}
787
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800788/* This is called w/ inode->i_mutex downed */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700789int reiserfs_delete_xattrs(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700791 struct file *fp;
792 struct dentry *dir, *root;
793 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700795 /* Skip out, an xattr has no xattrs associated with it */
796 if (is_reiserfs_priv_object(inode) ||
797 get_inode_sd_version(inode) == STAT_DATA_V1 ||
798 !reiserfs_xattrs(inode->i_sb)) {
799 return 0;
800 }
801 reiserfs_read_lock_xattrs(inode->i_sb);
802 dir = open_xa_dir(inode, FL_READONLY);
803 reiserfs_read_unlock_xattrs(inode->i_sb);
804 if (IS_ERR(dir)) {
805 err = PTR_ERR(dir);
806 goto out;
807 } else if (!dir->d_inode) {
808 dput(dir);
809 return 0;
810 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700812 fp = dentry_open(dir, NULL, O_RDWR);
813 if (IS_ERR(fp)) {
814 err = PTR_ERR(fp);
815 /* dentry_open dputs the dentry if it fails */
816 goto out;
817 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700819 lock_kernel();
820 err = xattr_readdir(fp, reiserfs_delete_xattrs_filler, dir);
821 if (err) {
822 unlock_kernel();
823 goto out_dir;
824 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700826 /* Leftovers besides . and .. -- that's not good. */
827 if (dir->d_inode->i_nlink <= 2) {
828 root = get_xa_root(inode->i_sb);
829 reiserfs_write_lock_xattrs(inode->i_sb);
830 err = vfs_rmdir(root->d_inode, dir);
831 reiserfs_write_unlock_xattrs(inode->i_sb);
832 dput(root);
833 } else {
834 reiserfs_warning(inode->i_sb,
835 "Couldn't remove all entries in directory");
836 }
837 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700839 out_dir:
840 fput(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700842 out:
843 if (!err)
844 REISERFS_I(inode)->i_flags =
845 REISERFS_I(inode)->i_flags & ~i_has_xattr_dir;
846 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847}
848
849struct reiserfs_chown_buf {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700850 struct inode *inode;
851 struct dentry *xadir;
852 struct iattr *attrs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853};
854
855/* XXX: If there is a better way to do this, I'd love to hear about it */
856static int
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700857reiserfs_chown_xattrs_filler(void *buf, const char *name, int namelen,
858 loff_t offset, ino_t ino, unsigned int d_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700860 struct reiserfs_chown_buf *chown_buf = (struct reiserfs_chown_buf *)buf;
861 struct dentry *xafile, *xadir = chown_buf->xadir;
862 struct iattr *attrs = chown_buf->attrs;
863 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700865 xafile = lookup_one_len(name, xadir, namelen);
866 if (IS_ERR(xafile))
867 return PTR_ERR(xafile);
868 else if (!xafile->d_inode) {
869 dput(xafile);
870 return -ENODATA;
871 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700873 if (!S_ISDIR(xafile->d_inode->i_mode))
874 err = notify_change(xafile, attrs);
875 dput(xafile);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700877 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878}
879
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700880int reiserfs_chown_xattrs(struct inode *inode, struct iattr *attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700882 struct file *fp;
883 struct dentry *dir;
884 int err = 0;
885 struct reiserfs_chown_buf buf;
886 unsigned int ia_valid = attrs->ia_valid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700888 /* Skip out, an xattr has no xattrs associated with it */
889 if (is_reiserfs_priv_object(inode) ||
890 get_inode_sd_version(inode) == STAT_DATA_V1 ||
891 !reiserfs_xattrs(inode->i_sb)) {
892 return 0;
893 }
894 reiserfs_read_lock_xattrs(inode->i_sb);
895 dir = open_xa_dir(inode, FL_READONLY);
896 reiserfs_read_unlock_xattrs(inode->i_sb);
897 if (IS_ERR(dir)) {
898 if (PTR_ERR(dir) != -ENODATA)
899 err = PTR_ERR(dir);
900 goto out;
901 } else if (!dir->d_inode) {
902 dput(dir);
903 goto out;
904 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700906 fp = dentry_open(dir, NULL, O_RDWR);
907 if (IS_ERR(fp)) {
908 err = PTR_ERR(fp);
909 /* dentry_open dputs the dentry if it fails */
910 goto out;
911 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700913 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700915 attrs->ia_valid &= (ATTR_UID | ATTR_GID | ATTR_CTIME);
916 buf.xadir = dir;
917 buf.attrs = attrs;
918 buf.inode = inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700920 err = xattr_readdir(fp, reiserfs_chown_xattrs_filler, &buf);
921 if (err) {
922 unlock_kernel();
923 goto out_dir;
924 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700926 err = notify_change(dir, attrs);
927 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700929 out_dir:
930 fput(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700932 out:
933 attrs->ia_valid = ia_valid;
934 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935}
936
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937/* Actual operations that are exported to VFS-land */
938
939/*
940 * Inode operation getxattr()
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800941 * Preliminary locking: we down dentry->d_inode->i_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 */
943ssize_t
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700944reiserfs_getxattr(struct dentry * dentry, const char *name, void *buffer,
945 size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700947 struct reiserfs_xattr_handler *xah = find_xattr_handler_prefix(name);
948 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700950 if (!xah || !reiserfs_xattrs(dentry->d_sb) ||
951 get_inode_sd_version(dentry->d_inode) == STAT_DATA_V1)
952 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700954 reiserfs_read_lock_xattr_i(dentry->d_inode);
955 reiserfs_read_lock_xattrs(dentry->d_sb);
956 err = xah->get(dentry->d_inode, name, buffer, size);
957 reiserfs_read_unlock_xattrs(dentry->d_sb);
958 reiserfs_read_unlock_xattr_i(dentry->d_inode);
959 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960}
961
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962/*
963 * Inode operation setxattr()
964 *
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800965 * dentry->d_inode->i_mutex down
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 */
967int
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700968reiserfs_setxattr(struct dentry *dentry, const char *name, const void *value,
969 size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700971 struct reiserfs_xattr_handler *xah = find_xattr_handler_prefix(name);
972 int err;
973 int lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700975 if (!xah || !reiserfs_xattrs(dentry->d_sb) ||
976 get_inode_sd_version(dentry->d_inode) == STAT_DATA_V1)
977 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700979 reiserfs_write_lock_xattr_i(dentry->d_inode);
980 lock = !has_xattr_dir(dentry->d_inode);
981 if (lock)
982 reiserfs_write_lock_xattrs(dentry->d_sb);
983 else
984 reiserfs_read_lock_xattrs(dentry->d_sb);
985 err = xah->set(dentry->d_inode, name, value, size, flags);
986 if (lock)
987 reiserfs_write_unlock_xattrs(dentry->d_sb);
988 else
989 reiserfs_read_unlock_xattrs(dentry->d_sb);
990 reiserfs_write_unlock_xattr_i(dentry->d_inode);
991 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992}
993
994/*
995 * Inode operation removexattr()
996 *
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800997 * dentry->d_inode->i_mutex down
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700999int reiserfs_removexattr(struct dentry *dentry, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001001 int err;
1002 struct reiserfs_xattr_handler *xah = find_xattr_handler_prefix(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001004 if (!xah || !reiserfs_xattrs(dentry->d_sb) ||
1005 get_inode_sd_version(dentry->d_inode) == STAT_DATA_V1)
1006 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001008 reiserfs_write_lock_xattr_i(dentry->d_inode);
1009 reiserfs_read_lock_xattrs(dentry->d_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001011 /* Deletion pre-operation */
1012 if (xah->del) {
1013 err = xah->del(dentry->d_inode, name);
1014 if (err)
1015 goto out;
1016 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001018 err = reiserfs_xattr_del(dentry->d_inode, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001020 dentry->d_inode->i_ctime = CURRENT_TIME_SEC;
1021 mark_inode_dirty(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001023 out:
1024 reiserfs_read_unlock_xattrs(dentry->d_sb);
1025 reiserfs_write_unlock_xattr_i(dentry->d_inode);
1026 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027}
1028
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029/* This is what filldir will use:
1030 * r_pos will always contain the amount of space required for the entire
1031 * list. If r_pos becomes larger than r_size, we need more space and we
1032 * return an error indicating this. If r_pos is less than r_size, then we've
1033 * filled the buffer successfully and we return success */
1034struct reiserfs_listxattr_buf {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001035 int r_pos;
1036 int r_size;
1037 char *r_buf;
1038 struct inode *r_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039};
1040
1041static int
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001042reiserfs_listxattr_filler(void *buf, const char *name, int namelen,
1043 loff_t offset, ino_t ino, unsigned int d_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001045 struct reiserfs_listxattr_buf *b = (struct reiserfs_listxattr_buf *)buf;
1046 int len = 0;
1047 if (name[0] != '.'
1048 || (namelen != 1 && (name[1] != '.' || namelen != 2))) {
1049 struct reiserfs_xattr_handler *xah =
1050 find_xattr_handler_prefix(name);
1051 if (!xah)
1052 return 0; /* Unsupported xattr name, skip it */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001054 /* We call ->list() twice because the operation isn't required to just
1055 * return the name back - we want to make sure we have enough space */
1056 len += xah->list(b->r_inode, name, namelen, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001058 if (len) {
1059 if (b->r_pos + len + 1 <= b->r_size) {
1060 char *p = b->r_buf + b->r_pos;
1061 p += xah->list(b->r_inode, name, namelen, p);
1062 *p++ = '\0';
1063 }
1064 b->r_pos += len + 1;
1065 }
1066 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001068 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069}
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001070
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071/*
1072 * Inode operation listxattr()
1073 *
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001074 * Preliminary locking: we down dentry->d_inode->i_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001076ssize_t reiserfs_listxattr(struct dentry * dentry, char *buffer, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001078 struct file *fp;
1079 struct dentry *dir;
1080 int err = 0;
1081 struct reiserfs_listxattr_buf buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001083 if (!dentry->d_inode)
1084 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001086 if (!reiserfs_xattrs(dentry->d_sb) ||
1087 get_inode_sd_version(dentry->d_inode) == STAT_DATA_V1)
1088 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001090 reiserfs_read_lock_xattr_i(dentry->d_inode);
1091 reiserfs_read_lock_xattrs(dentry->d_sb);
1092 dir = open_xa_dir(dentry->d_inode, FL_READONLY);
1093 reiserfs_read_unlock_xattrs(dentry->d_sb);
1094 if (IS_ERR(dir)) {
1095 err = PTR_ERR(dir);
1096 if (err == -ENODATA)
1097 err = 0; /* Not an error if there aren't any xattrs */
1098 goto out;
1099 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001101 fp = dentry_open(dir, NULL, O_RDWR);
1102 if (IS_ERR(fp)) {
1103 err = PTR_ERR(fp);
1104 /* dentry_open dputs the dentry if it fails */
1105 goto out;
1106 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001108 buf.r_buf = buffer;
1109 buf.r_size = buffer ? size : 0;
1110 buf.r_pos = 0;
1111 buf.r_inode = dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001113 REISERFS_I(dentry->d_inode)->i_flags |= i_has_xattr_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001115 err = xattr_readdir(fp, reiserfs_listxattr_filler, &buf);
1116 if (err)
1117 goto out_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001119 if (buf.r_pos > buf.r_size && buffer != NULL)
1120 err = -ERANGE;
1121 else
1122 err = buf.r_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001124 out_dir:
1125 fput(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001127 out:
1128 reiserfs_read_unlock_xattr_i(dentry->d_inode);
1129 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130}
1131
1132/* This is the implementation for the xattr plugin infrastructure */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001133static struct list_head xattr_handlers = LIST_HEAD_INIT(xattr_handlers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134static DEFINE_RWLOCK(handler_lock);
1135
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001136static struct reiserfs_xattr_handler *find_xattr_handler_prefix(const char
1137 *prefix)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001139 struct reiserfs_xattr_handler *xah = NULL;
1140 struct list_head *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001142 read_lock(&handler_lock);
1143 list_for_each(p, &xattr_handlers) {
1144 xah = list_entry(p, struct reiserfs_xattr_handler, handlers);
1145 if (strncmp(xah->prefix, prefix, strlen(xah->prefix)) == 0)
1146 break;
1147 xah = NULL;
1148 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001150 read_unlock(&handler_lock);
1151 return xah;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152}
1153
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001154static void __unregister_handlers(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001156 struct reiserfs_xattr_handler *xah;
1157 struct list_head *p, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001159 list_for_each_safe(p, tmp, &xattr_handlers) {
1160 xah = list_entry(p, struct reiserfs_xattr_handler, handlers);
1161 if (xah->exit)
1162 xah->exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001164 list_del_init(p);
1165 }
1166 INIT_LIST_HEAD(&xattr_handlers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167}
1168
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001169int __init reiserfs_xattr_register_handlers(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001171 int err = 0;
1172 struct reiserfs_xattr_handler *xah;
1173 struct list_head *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001175 write_lock(&handler_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001177 /* If we're already initialized, nothing to do */
1178 if (!list_empty(&xattr_handlers)) {
1179 write_unlock(&handler_lock);
1180 return 0;
1181 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001183 /* Add the handlers */
1184 list_add_tail(&user_handler.handlers, &xattr_handlers);
1185 list_add_tail(&trusted_handler.handlers, &xattr_handlers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186#ifdef CONFIG_REISERFS_FS_SECURITY
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001187 list_add_tail(&security_handler.handlers, &xattr_handlers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188#endif
1189#ifdef CONFIG_REISERFS_FS_POSIX_ACL
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001190 list_add_tail(&posix_acl_access_handler.handlers, &xattr_handlers);
1191 list_add_tail(&posix_acl_default_handler.handlers, &xattr_handlers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192#endif
1193
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001194 /* Run initializers, if available */
1195 list_for_each(p, &xattr_handlers) {
1196 xah = list_entry(p, struct reiserfs_xattr_handler, handlers);
1197 if (xah->init) {
1198 err = xah->init();
1199 if (err) {
1200 list_del_init(p);
1201 break;
1202 }
1203 }
1204 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001206 /* Clean up other handlers, if any failed */
1207 if (err)
1208 __unregister_handlers();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001210 write_unlock(&handler_lock);
1211 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212}
1213
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001214void reiserfs_xattr_unregister_handlers(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001216 write_lock(&handler_lock);
1217 __unregister_handlers();
1218 write_unlock(&handler_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219}
1220
1221/* This will catch lookups from the fs root to .reiserfs_priv */
1222static int
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001223xattr_lookup_poison(struct dentry *dentry, struct qstr *q1, struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001225 struct dentry *priv_root = REISERFS_SB(dentry->d_sb)->priv_root;
1226 if (name->len == priv_root->d_name.len &&
1227 name->hash == priv_root->d_name.hash &&
1228 !memcmp(name->name, priv_root->d_name.name, name->len)) {
1229 return -ENOENT;
1230 } else if (q1->len == name->len &&
1231 !memcmp(q1->name, name->name, name->len))
1232 return 0;
1233 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234}
1235
1236static struct dentry_operations xattr_lookup_poison_ops = {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001237 .d_compare = xattr_lookup_poison,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238};
1239
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240/* We need to take a copy of the mount flags since things like
1241 * MS_RDONLY don't get set until *after* we're called.
1242 * mount_flags != mount_options */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001243int reiserfs_xattr_init(struct super_block *s, int mount_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001245 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001247 /* We need generation numbers to ensure that the oid mapping is correct
1248 * v3.5 filesystems don't have them. */
1249 if (!old_format_only(s)) {
1250 set_bit(REISERFS_XATTRS, &(REISERFS_SB(s)->s_mount_opt));
1251 } else if (reiserfs_xattrs_optional(s)) {
1252 /* Old format filesystem, but optional xattrs have been enabled
1253 * at mount time. Error out. */
1254 reiserfs_warning(s, "xattrs/ACLs not supported on pre v3.6 "
1255 "format filesystem. Failing mount.");
1256 err = -EOPNOTSUPP;
1257 goto error;
1258 } else {
1259 /* Old format filesystem, but no optional xattrs have been enabled. This
1260 * means we silently disable xattrs on the filesystem. */
1261 clear_bit(REISERFS_XATTRS, &(REISERFS_SB(s)->s_mount_opt));
1262 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001264 /* If we don't have the privroot located yet - go find it */
1265 if (reiserfs_xattrs(s) && !REISERFS_SB(s)->priv_root) {
1266 struct dentry *dentry;
1267 dentry = lookup_one_len(PRIVROOT_NAME, s->s_root,
1268 strlen(PRIVROOT_NAME));
1269 if (!IS_ERR(dentry)) {
1270 if (!(mount_flags & MS_RDONLY) && !dentry->d_inode) {
1271 struct inode *inode = dentry->d_parent->d_inode;
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001272 mutex_lock(&inode->i_mutex);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001273 err = inode->i_op->mkdir(inode, dentry, 0700);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001274 mutex_unlock(&inode->i_mutex);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001275 if (err) {
1276 dput(dentry);
1277 dentry = NULL;
1278 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001280 if (dentry && dentry->d_inode)
1281 reiserfs_warning(s,
1282 "Created %s on %s - reserved for "
1283 "xattr storage.",
1284 PRIVROOT_NAME,
1285 reiserfs_bdevname
1286 (inode->i_sb));
1287 } else if (!dentry->d_inode) {
1288 dput(dentry);
1289 dentry = NULL;
1290 }
1291 } else
1292 err = PTR_ERR(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001294 if (!err && dentry) {
1295 s->s_root->d_op = &xattr_lookup_poison_ops;
1296 reiserfs_mark_inode_private(dentry->d_inode);
1297 REISERFS_SB(s)->priv_root = dentry;
1298 } else if (!(mount_flags & MS_RDONLY)) { /* xattrs are unavailable */
1299 /* If we're read-only it just means that the dir hasn't been
1300 * created. Not an error -- just no xattrs on the fs. We'll
1301 * check again if we go read-write */
1302 reiserfs_warning(s, "xattrs/ACLs enabled and couldn't "
1303 "find/create .reiserfs_priv. Failing mount.");
1304 err = -EOPNOTSUPP;
1305 }
1306 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001308 error:
1309 /* This is only nonzero if there was an error initializing the xattr
1310 * directory or if there is a condition where we don't support them. */
1311 if (err) {
1312 clear_bit(REISERFS_XATTRS, &(REISERFS_SB(s)->s_mount_opt));
1313 clear_bit(REISERFS_XATTRS_USER, &(REISERFS_SB(s)->s_mount_opt));
1314 clear_bit(REISERFS_POSIXACL, &(REISERFS_SB(s)->s_mount_opt));
1315 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001317 /* The super_block MS_POSIXACL must mirror the (no)acl mount option. */
1318 s->s_flags = s->s_flags & ~MS_POSIXACL;
1319 if (reiserfs_posixacl(s))
1320 s->s_flags |= MS_POSIXACL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001322 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323}
1324
1325static int
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001326__reiserfs_permission(struct inode *inode, int mask, struct nameidata *nd,
1327 int need_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001329 umode_t mode = inode->i_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330
1331 if (mask & MAY_WRITE) {
1332 /*
1333 * Nobody gets write access to a read-only fs.
1334 */
1335 if (IS_RDONLY(inode) &&
1336 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
1337 return -EROFS;
1338
1339 /*
1340 * Nobody gets write access to an immutable file.
1341 */
1342 if (IS_IMMUTABLE(inode))
1343 return -EACCES;
1344 }
1345
1346 /* We don't do permission checks on the internal objects.
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001347 * Permissions are determined by the "owning" object. */
1348 if (is_reiserfs_priv_object(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 return 0;
1350
1351 if (current->fsuid == inode->i_uid) {
1352 mode >>= 6;
1353#ifdef CONFIG_REISERFS_FS_POSIX_ACL
1354 } else if (reiserfs_posixacl(inode->i_sb) &&
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001355 get_inode_sd_version(inode) != STAT_DATA_V1) {
1356 struct posix_acl *acl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357
1358 /* ACL can't contain additional permissions if
1359 the ACL_MASK entry is 0 */
1360 if (!(mode & S_IRWXG))
1361 goto check_groups;
1362
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001363 if (need_lock) {
1364 reiserfs_read_lock_xattr_i(inode);
1365 reiserfs_read_lock_xattrs(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001367 acl = reiserfs_get_acl(inode, ACL_TYPE_ACCESS);
1368 if (need_lock) {
1369 reiserfs_read_unlock_xattrs(inode->i_sb);
1370 reiserfs_read_unlock_xattr_i(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001372 if (IS_ERR(acl)) {
1373 if (PTR_ERR(acl) == -ENODATA)
1374 goto check_groups;
1375 return PTR_ERR(acl);
1376 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001378 if (acl) {
1379 int err = posix_acl_permission(inode, acl, mask);
1380 posix_acl_release(acl);
1381 if (err == -EACCES) {
1382 goto check_capabilities;
1383 }
1384 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 } else {
1386 goto check_groups;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001387 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388#endif
1389 } else {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001390 check_groups:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 if (in_group_p(inode->i_gid))
1392 mode >>= 3;
1393 }
1394
1395 /*
1396 * If the DACs are ok we don't need any capability check.
1397 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001398 if (((mode & mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == mask))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 return 0;
1400
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001401 check_capabilities:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 /*
1403 * Read/write DACs are always overridable.
1404 * Executable DACs are overridable if at least one exec bit is set.
1405 */
1406 if (!(mask & MAY_EXEC) ||
1407 (inode->i_mode & S_IXUGO) || S_ISDIR(inode->i_mode))
1408 if (capable(CAP_DAC_OVERRIDE))
1409 return 0;
1410
1411 /*
1412 * Searching includes executable on directories, else just read.
1413 */
1414 if (mask == MAY_READ || (S_ISDIR(inode->i_mode) && !(mask & MAY_WRITE)))
1415 if (capable(CAP_DAC_READ_SEARCH))
1416 return 0;
1417
1418 return -EACCES;
1419}
1420
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001421int reiserfs_permission(struct inode *inode, int mask, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001423 return __reiserfs_permission(inode, mask, nd, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424}
1425
1426int
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001427reiserfs_permission_locked(struct inode *inode, int mask, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001429 return __reiserfs_permission(inode, mask, nd, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430}