blob: e386d3db30513c90ebf9aac18ad9e6a0f3e8adac [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>
33#include <linux/dcache.h>
34#include <linux/namei.h>
35#include <linux/errno.h>
36#include <linux/fs.h>
37#include <linux/file.h>
38#include <linux/pagemap.h>
39#include <linux/xattr.h>
40#include <linux/reiserfs_xattr.h>
41#include <linux/reiserfs_acl.h>
42#include <linux/mbcache.h>
43#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;
71 down(&privroot->d_inode->i_sem);
72 err =
73 privroot->d_inode->i_op->mkdir(privroot->d_inode, xaroot,
74 0700);
75 up(&privroot->d_inode->i_sem);
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
119 * attribute directory tree. If it has already been retreived, it is used.
120 * Otherwise, we attempt to retreive it from disk. It may also return
121 * 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 {
223 /* inode->i_sem is down, so nothing else can try to create
224 * 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 *
272 * we're called with i_sem held, so there are no worries about the directory
273 * 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;
430 down(&inode->i_sem);
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);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700439 up(&inode->i_sem);
440 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 */
457 mapping->flags = (mapping->flags & ~__GFP_BITS_MASK) | GFP_NOFS;
458 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/*
484 * inode->i_sem: down
485 */
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 (IS_RDONLY(inode))
502 return -EROFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700504 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
505 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700507 if (get_inode_sd_version(inode) == STAT_DATA_V1)
508 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700510 /* Empty xattrs are ok, they're just empty files, no hash */
511 if (buffer && buffer_size)
512 xahash = xattr_hash(buffer, buffer_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700514 open_file:
515 fp = open_xa_file(inode, name, flags);
516 if (IS_ERR(fp)) {
517 err = PTR_ERR(fp);
518 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700521 xinode = fp->f_dentry->d_inode;
522 REISERFS_I(inode)->i_flags |= i_has_xattr_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700524 /* we need to copy it off.. */
525 if (xinode->i_nlink > 1) {
526 fput(fp);
527 err = reiserfs_xattr_del(inode, name);
528 if (err < 0)
529 goto out;
530 /* We just killed the old one, we're not replacing anymore */
531 if (flags & XATTR_REPLACE)
532 flags &= ~XATTR_REPLACE;
533 goto open_file;
534 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700536 /* Resize it so we're ok to write there */
537 newattrs.ia_size = buffer_size;
538 newattrs.ia_valid = ATTR_SIZE | ATTR_CTIME;
539 down(&xinode->i_sem);
540 err = notify_change(fp->f_dentry, &newattrs);
541 if (err)
542 goto out_filp;
543
544 mapping = xinode->i_mapping;
545 while (buffer_pos < buffer_size || buffer_pos == 0) {
546 size_t chunk;
547 size_t skip = 0;
548 size_t page_offset = (file_pos & (PAGE_CACHE_SIZE - 1));
549 if (buffer_size - buffer_pos > PAGE_CACHE_SIZE)
550 chunk = PAGE_CACHE_SIZE;
551 else
552 chunk = buffer_size - buffer_pos;
553
554 page = reiserfs_get_page(xinode, file_pos >> PAGE_CACHE_SHIFT);
555 if (IS_ERR(page)) {
556 err = PTR_ERR(page);
557 goto out_filp;
558 }
559
560 lock_page(page);
561 data = page_address(page);
562
563 if (file_pos == 0) {
564 struct reiserfs_xattr_header *rxh;
565 skip = file_pos = sizeof(struct reiserfs_xattr_header);
566 if (chunk + skip > PAGE_CACHE_SIZE)
567 chunk = PAGE_CACHE_SIZE - skip;
568 rxh = (struct reiserfs_xattr_header *)data;
569 rxh->h_magic = cpu_to_le32(REISERFS_XATTR_MAGIC);
570 rxh->h_hash = cpu_to_le32(xahash);
571 }
572
573 err = mapping->a_ops->prepare_write(fp, page, page_offset,
574 page_offset + chunk + skip);
575 if (!err) {
576 if (buffer)
577 memcpy(data + skip, buffer + buffer_pos, chunk);
578 err =
579 mapping->a_ops->commit_write(fp, page, page_offset,
580 page_offset + chunk +
581 skip);
582 }
583 unlock_page(page);
584 reiserfs_put_page(page);
585 buffer_pos += chunk;
586 file_pos += chunk;
587 skip = 0;
588 if (err || buffer_size == 0 || !buffer)
589 break;
590 }
591
592 /* We can't mark the inode dirty if it's not hashed. This is the case
593 * when we're inheriting the default ACL. If we dirty it, the inode
594 * gets marked dirty, but won't (ever) make it onto the dirty list until
595 * it's synced explicitly to clear I_DIRTY. This is bad. */
596 if (!hlist_unhashed(&inode->i_hash)) {
597 inode->i_ctime = CURRENT_TIME_SEC;
598 mark_inode_dirty(inode);
599 }
600
601 out_filp:
602 up(&xinode->i_sem);
603 fput(fp);
604
605 out:
606 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607}
608
609/*
610 * inode->i_sem: down
611 */
612int
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700613reiserfs_xattr_get(const struct inode *inode, const char *name, void *buffer,
614 size_t buffer_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700616 ssize_t err = 0;
617 struct file *fp;
618 size_t isize;
619 size_t file_pos = 0;
620 size_t buffer_pos = 0;
621 struct page *page;
622 struct inode *xinode;
623 __u32 hash = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700625 if (name == NULL)
626 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700628 /* We can't have xattrs attached to v1 items since they don't have
629 * generation numbers */
630 if (get_inode_sd_version(inode) == STAT_DATA_V1)
631 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700633 fp = open_xa_file(inode, name, FL_READONLY);
634 if (IS_ERR(fp)) {
635 err = PTR_ERR(fp);
636 goto out;
637 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700639 xinode = fp->f_dentry->d_inode;
640 isize = xinode->i_size;
641 REISERFS_I(inode)->i_flags |= i_has_xattr_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700643 /* Just return the size needed */
644 if (buffer == NULL) {
645 err = isize - sizeof(struct reiserfs_xattr_header);
646 goto out_dput;
647 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700649 if (buffer_size < isize - sizeof(struct reiserfs_xattr_header)) {
650 err = -ERANGE;
651 goto out_dput;
652 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700654 while (file_pos < isize) {
655 size_t chunk;
656 char *data;
657 size_t skip = 0;
658 if (isize - file_pos > PAGE_CACHE_SIZE)
659 chunk = PAGE_CACHE_SIZE;
660 else
661 chunk = isize - file_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700663 page = reiserfs_get_page(xinode, file_pos >> PAGE_CACHE_SHIFT);
664 if (IS_ERR(page)) {
665 err = PTR_ERR(page);
666 goto out_dput;
667 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700669 lock_page(page);
670 data = page_address(page);
671 if (file_pos == 0) {
672 struct reiserfs_xattr_header *rxh =
673 (struct reiserfs_xattr_header *)data;
674 skip = file_pos = sizeof(struct reiserfs_xattr_header);
675 chunk -= skip;
676 /* Magic doesn't match up.. */
677 if (rxh->h_magic != cpu_to_le32(REISERFS_XATTR_MAGIC)) {
678 unlock_page(page);
679 reiserfs_put_page(page);
680 reiserfs_warning(inode->i_sb,
681 "Invalid magic for xattr (%s) "
682 "associated with %k", name,
683 INODE_PKEY(inode));
684 err = -EIO;
685 goto out_dput;
686 }
687 hash = le32_to_cpu(rxh->h_hash);
688 }
689 memcpy(buffer + buffer_pos, data + skip, chunk);
690 unlock_page(page);
691 reiserfs_put_page(page);
692 file_pos += chunk;
693 buffer_pos += chunk;
694 skip = 0;
695 }
696 err = isize - sizeof(struct reiserfs_xattr_header);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700698 if (xattr_hash(buffer, isize - sizeof(struct reiserfs_xattr_header)) !=
699 hash) {
700 reiserfs_warning(inode->i_sb,
701 "Invalid hash for xattr (%s) associated "
702 "with %k", name, INODE_PKEY(inode));
703 err = -EIO;
704 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700706 out_dput:
707 fput(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700709 out:
710 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711}
712
713static int
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700714__reiserfs_xattr_del(struct dentry *xadir, const char *name, int namelen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700716 struct dentry *dentry;
717 struct inode *dir = xadir->d_inode;
718 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700720 dentry = lookup_one_len(name, xadir, namelen);
721 if (IS_ERR(dentry)) {
722 err = PTR_ERR(dentry);
723 goto out;
724 } else if (!dentry->d_inode) {
725 err = -ENODATA;
726 goto out_file;
727 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700729 /* Skip directories.. */
730 if (S_ISDIR(dentry->d_inode->i_mode))
731 goto out_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700733 if (!is_reiserfs_priv_object(dentry->d_inode)) {
734 reiserfs_warning(dir->i_sb, "OID %08x [%.*s/%.*s] doesn't have "
735 "priv flag set [parent is %sset].",
736 le32_to_cpu(INODE_PKEY(dentry->d_inode)->
737 k_objectid), xadir->d_name.len,
738 xadir->d_name.name, namelen, name,
739 is_reiserfs_priv_object(xadir->
740 d_inode) ? "" :
741 "not ");
742 dput(dentry);
743 return -EIO;
744 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700746 err = dir->i_op->unlink(dir, dentry);
747 if (!err)
748 d_delete(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700750 out_file:
751 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700753 out:
754 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755}
756
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700757int reiserfs_xattr_del(struct inode *inode, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700759 struct dentry *dir;
760 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700762 if (IS_RDONLY(inode))
763 return -EROFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700765 dir = open_xa_dir(inode, FL_READONLY);
766 if (IS_ERR(dir)) {
767 err = PTR_ERR(dir);
768 goto out;
769 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700771 err = __reiserfs_xattr_del(dir, name, strlen(name));
772 dput(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700774 if (!err) {
775 inode->i_ctime = CURRENT_TIME_SEC;
776 mark_inode_dirty(inode);
777 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700779 out:
780 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781}
782
783/* The following are side effects of other operations that aren't explicitly
784 * modifying extended attributes. This includes operations such as permissions
785 * or ownership changes, object deletions, etc. */
786
787static int
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700788reiserfs_delete_xattrs_filler(void *buf, const char *name, int namelen,
789 loff_t offset, ino_t ino, unsigned int d_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700791 struct dentry *xadir = (struct dentry *)buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700793 return __reiserfs_xattr_del(xadir, name, namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
795}
796
797/* This is called w/ inode->i_sem downed */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700798int reiserfs_delete_xattrs(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700800 struct file *fp;
801 struct dentry *dir, *root;
802 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700804 /* Skip out, an xattr has no xattrs associated with it */
805 if (is_reiserfs_priv_object(inode) ||
806 get_inode_sd_version(inode) == STAT_DATA_V1 ||
807 !reiserfs_xattrs(inode->i_sb)) {
808 return 0;
809 }
810 reiserfs_read_lock_xattrs(inode->i_sb);
811 dir = open_xa_dir(inode, FL_READONLY);
812 reiserfs_read_unlock_xattrs(inode->i_sb);
813 if (IS_ERR(dir)) {
814 err = PTR_ERR(dir);
815 goto out;
816 } else if (!dir->d_inode) {
817 dput(dir);
818 return 0;
819 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700821 fp = dentry_open(dir, NULL, O_RDWR);
822 if (IS_ERR(fp)) {
823 err = PTR_ERR(fp);
824 /* dentry_open dputs the dentry if it fails */
825 goto out;
826 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700828 lock_kernel();
829 err = xattr_readdir(fp, reiserfs_delete_xattrs_filler, dir);
830 if (err) {
831 unlock_kernel();
832 goto out_dir;
833 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700835 /* Leftovers besides . and .. -- that's not good. */
836 if (dir->d_inode->i_nlink <= 2) {
837 root = get_xa_root(inode->i_sb);
838 reiserfs_write_lock_xattrs(inode->i_sb);
839 err = vfs_rmdir(root->d_inode, dir);
840 reiserfs_write_unlock_xattrs(inode->i_sb);
841 dput(root);
842 } else {
843 reiserfs_warning(inode->i_sb,
844 "Couldn't remove all entries in directory");
845 }
846 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700848 out_dir:
849 fput(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700851 out:
852 if (!err)
853 REISERFS_I(inode)->i_flags =
854 REISERFS_I(inode)->i_flags & ~i_has_xattr_dir;
855 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856}
857
858struct reiserfs_chown_buf {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700859 struct inode *inode;
860 struct dentry *xadir;
861 struct iattr *attrs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862};
863
864/* XXX: If there is a better way to do this, I'd love to hear about it */
865static int
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700866reiserfs_chown_xattrs_filler(void *buf, const char *name, int namelen,
867 loff_t offset, ino_t ino, unsigned int d_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700869 struct reiserfs_chown_buf *chown_buf = (struct reiserfs_chown_buf *)buf;
870 struct dentry *xafile, *xadir = chown_buf->xadir;
871 struct iattr *attrs = chown_buf->attrs;
872 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700874 xafile = lookup_one_len(name, xadir, namelen);
875 if (IS_ERR(xafile))
876 return PTR_ERR(xafile);
877 else if (!xafile->d_inode) {
878 dput(xafile);
879 return -ENODATA;
880 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700882 if (!S_ISDIR(xafile->d_inode->i_mode))
883 err = notify_change(xafile, attrs);
884 dput(xafile);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700886 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887}
888
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700889int reiserfs_chown_xattrs(struct inode *inode, struct iattr *attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700891 struct file *fp;
892 struct dentry *dir;
893 int err = 0;
894 struct reiserfs_chown_buf buf;
895 unsigned int ia_valid = attrs->ia_valid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700897 /* Skip out, an xattr has no xattrs associated with it */
898 if (is_reiserfs_priv_object(inode) ||
899 get_inode_sd_version(inode) == STAT_DATA_V1 ||
900 !reiserfs_xattrs(inode->i_sb)) {
901 return 0;
902 }
903 reiserfs_read_lock_xattrs(inode->i_sb);
904 dir = open_xa_dir(inode, FL_READONLY);
905 reiserfs_read_unlock_xattrs(inode->i_sb);
906 if (IS_ERR(dir)) {
907 if (PTR_ERR(dir) != -ENODATA)
908 err = PTR_ERR(dir);
909 goto out;
910 } else if (!dir->d_inode) {
911 dput(dir);
912 goto out;
913 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700915 fp = dentry_open(dir, NULL, O_RDWR);
916 if (IS_ERR(fp)) {
917 err = PTR_ERR(fp);
918 /* dentry_open dputs the dentry if it fails */
919 goto out;
920 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700922 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700924 attrs->ia_valid &= (ATTR_UID | ATTR_GID | ATTR_CTIME);
925 buf.xadir = dir;
926 buf.attrs = attrs;
927 buf.inode = inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700929 err = xattr_readdir(fp, reiserfs_chown_xattrs_filler, &buf);
930 if (err) {
931 unlock_kernel();
932 goto out_dir;
933 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700935 err = notify_change(dir, attrs);
936 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700938 out_dir:
939 fput(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700941 out:
942 attrs->ia_valid = ia_valid;
943 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944}
945
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946/* Actual operations that are exported to VFS-land */
947
948/*
949 * Inode operation getxattr()
950 * Preliminary locking: we down dentry->d_inode->i_sem
951 */
952ssize_t
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700953reiserfs_getxattr(struct dentry * dentry, const char *name, void *buffer,
954 size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700956 struct reiserfs_xattr_handler *xah = find_xattr_handler_prefix(name);
957 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700959 if (!xah || !reiserfs_xattrs(dentry->d_sb) ||
960 get_inode_sd_version(dentry->d_inode) == STAT_DATA_V1)
961 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700963 reiserfs_read_lock_xattr_i(dentry->d_inode);
964 reiserfs_read_lock_xattrs(dentry->d_sb);
965 err = xah->get(dentry->d_inode, name, buffer, size);
966 reiserfs_read_unlock_xattrs(dentry->d_sb);
967 reiserfs_read_unlock_xattr_i(dentry->d_inode);
968 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969}
970
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971/*
972 * Inode operation setxattr()
973 *
974 * dentry->d_inode->i_sem down
975 */
976int
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700977reiserfs_setxattr(struct dentry *dentry, const char *name, const void *value,
978 size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700980 struct reiserfs_xattr_handler *xah = find_xattr_handler_prefix(name);
981 int err;
982 int lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700984 if (!xah || !reiserfs_xattrs(dentry->d_sb) ||
985 get_inode_sd_version(dentry->d_inode) == STAT_DATA_V1)
986 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700988 if (IS_RDONLY(dentry->d_inode))
989 return -EROFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700991 if (IS_IMMUTABLE(dentry->d_inode) || IS_APPEND(dentry->d_inode))
992 return -EROFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700994 reiserfs_write_lock_xattr_i(dentry->d_inode);
995 lock = !has_xattr_dir(dentry->d_inode);
996 if (lock)
997 reiserfs_write_lock_xattrs(dentry->d_sb);
998 else
999 reiserfs_read_lock_xattrs(dentry->d_sb);
1000 err = xah->set(dentry->d_inode, name, value, size, flags);
1001 if (lock)
1002 reiserfs_write_unlock_xattrs(dentry->d_sb);
1003 else
1004 reiserfs_read_unlock_xattrs(dentry->d_sb);
1005 reiserfs_write_unlock_xattr_i(dentry->d_inode);
1006 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007}
1008
1009/*
1010 * Inode operation removexattr()
1011 *
1012 * dentry->d_inode->i_sem down
1013 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001014int reiserfs_removexattr(struct dentry *dentry, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001016 int err;
1017 struct reiserfs_xattr_handler *xah = find_xattr_handler_prefix(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001019 if (!xah || !reiserfs_xattrs(dentry->d_sb) ||
1020 get_inode_sd_version(dentry->d_inode) == STAT_DATA_V1)
1021 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001023 if (IS_RDONLY(dentry->d_inode))
1024 return -EROFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001026 if (IS_IMMUTABLE(dentry->d_inode) || IS_APPEND(dentry->d_inode))
1027 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001029 reiserfs_write_lock_xattr_i(dentry->d_inode);
1030 reiserfs_read_lock_xattrs(dentry->d_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001032 /* Deletion pre-operation */
1033 if (xah->del) {
1034 err = xah->del(dentry->d_inode, name);
1035 if (err)
1036 goto out;
1037 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001039 err = reiserfs_xattr_del(dentry->d_inode, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001041 dentry->d_inode->i_ctime = CURRENT_TIME_SEC;
1042 mark_inode_dirty(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001044 out:
1045 reiserfs_read_unlock_xattrs(dentry->d_sb);
1046 reiserfs_write_unlock_xattr_i(dentry->d_inode);
1047 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048}
1049
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050/* This is what filldir will use:
1051 * r_pos will always contain the amount of space required for the entire
1052 * list. If r_pos becomes larger than r_size, we need more space and we
1053 * return an error indicating this. If r_pos is less than r_size, then we've
1054 * filled the buffer successfully and we return success */
1055struct reiserfs_listxattr_buf {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001056 int r_pos;
1057 int r_size;
1058 char *r_buf;
1059 struct inode *r_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060};
1061
1062static int
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001063reiserfs_listxattr_filler(void *buf, const char *name, int namelen,
1064 loff_t offset, ino_t ino, unsigned int d_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001066 struct reiserfs_listxattr_buf *b = (struct reiserfs_listxattr_buf *)buf;
1067 int len = 0;
1068 if (name[0] != '.'
1069 || (namelen != 1 && (name[1] != '.' || namelen != 2))) {
1070 struct reiserfs_xattr_handler *xah =
1071 find_xattr_handler_prefix(name);
1072 if (!xah)
1073 return 0; /* Unsupported xattr name, skip it */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001075 /* We call ->list() twice because the operation isn't required to just
1076 * return the name back - we want to make sure we have enough space */
1077 len += xah->list(b->r_inode, name, namelen, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001079 if (len) {
1080 if (b->r_pos + len + 1 <= b->r_size) {
1081 char *p = b->r_buf + b->r_pos;
1082 p += xah->list(b->r_inode, name, namelen, p);
1083 *p++ = '\0';
1084 }
1085 b->r_pos += len + 1;
1086 }
1087 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001089 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090}
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001091
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092/*
1093 * Inode operation listxattr()
1094 *
1095 * Preliminary locking: we down dentry->d_inode->i_sem
1096 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001097ssize_t reiserfs_listxattr(struct dentry * dentry, char *buffer, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001099 struct file *fp;
1100 struct dentry *dir;
1101 int err = 0;
1102 struct reiserfs_listxattr_buf buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001104 if (!dentry->d_inode)
1105 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001107 if (!reiserfs_xattrs(dentry->d_sb) ||
1108 get_inode_sd_version(dentry->d_inode) == STAT_DATA_V1)
1109 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001111 reiserfs_read_lock_xattr_i(dentry->d_inode);
1112 reiserfs_read_lock_xattrs(dentry->d_sb);
1113 dir = open_xa_dir(dentry->d_inode, FL_READONLY);
1114 reiserfs_read_unlock_xattrs(dentry->d_sb);
1115 if (IS_ERR(dir)) {
1116 err = PTR_ERR(dir);
1117 if (err == -ENODATA)
1118 err = 0; /* Not an error if there aren't any xattrs */
1119 goto out;
1120 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001122 fp = dentry_open(dir, NULL, O_RDWR);
1123 if (IS_ERR(fp)) {
1124 err = PTR_ERR(fp);
1125 /* dentry_open dputs the dentry if it fails */
1126 goto out;
1127 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001129 buf.r_buf = buffer;
1130 buf.r_size = buffer ? size : 0;
1131 buf.r_pos = 0;
1132 buf.r_inode = dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001134 REISERFS_I(dentry->d_inode)->i_flags |= i_has_xattr_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001136 err = xattr_readdir(fp, reiserfs_listxattr_filler, &buf);
1137 if (err)
1138 goto out_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001140 if (buf.r_pos > buf.r_size && buffer != NULL)
1141 err = -ERANGE;
1142 else
1143 err = buf.r_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001145 out_dir:
1146 fput(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001148 out:
1149 reiserfs_read_unlock_xattr_i(dentry->d_inode);
1150 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151}
1152
1153/* This is the implementation for the xattr plugin infrastructure */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001154static struct list_head xattr_handlers = LIST_HEAD_INIT(xattr_handlers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155static DEFINE_RWLOCK(handler_lock);
1156
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001157static struct reiserfs_xattr_handler *find_xattr_handler_prefix(const char
1158 *prefix)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001160 struct reiserfs_xattr_handler *xah = NULL;
1161 struct list_head *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001163 read_lock(&handler_lock);
1164 list_for_each(p, &xattr_handlers) {
1165 xah = list_entry(p, struct reiserfs_xattr_handler, handlers);
1166 if (strncmp(xah->prefix, prefix, strlen(xah->prefix)) == 0)
1167 break;
1168 xah = NULL;
1169 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001171 read_unlock(&handler_lock);
1172 return xah;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173}
1174
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001175static void __unregister_handlers(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001177 struct reiserfs_xattr_handler *xah;
1178 struct list_head *p, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001180 list_for_each_safe(p, tmp, &xattr_handlers) {
1181 xah = list_entry(p, struct reiserfs_xattr_handler, handlers);
1182 if (xah->exit)
1183 xah->exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001185 list_del_init(p);
1186 }
1187 INIT_LIST_HEAD(&xattr_handlers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188}
1189
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001190int __init reiserfs_xattr_register_handlers(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001192 int err = 0;
1193 struct reiserfs_xattr_handler *xah;
1194 struct list_head *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001196 write_lock(&handler_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001198 /* If we're already initialized, nothing to do */
1199 if (!list_empty(&xattr_handlers)) {
1200 write_unlock(&handler_lock);
1201 return 0;
1202 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001204 /* Add the handlers */
1205 list_add_tail(&user_handler.handlers, &xattr_handlers);
1206 list_add_tail(&trusted_handler.handlers, &xattr_handlers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207#ifdef CONFIG_REISERFS_FS_SECURITY
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001208 list_add_tail(&security_handler.handlers, &xattr_handlers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209#endif
1210#ifdef CONFIG_REISERFS_FS_POSIX_ACL
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001211 list_add_tail(&posix_acl_access_handler.handlers, &xattr_handlers);
1212 list_add_tail(&posix_acl_default_handler.handlers, &xattr_handlers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213#endif
1214
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001215 /* Run initializers, if available */
1216 list_for_each(p, &xattr_handlers) {
1217 xah = list_entry(p, struct reiserfs_xattr_handler, handlers);
1218 if (xah->init) {
1219 err = xah->init();
1220 if (err) {
1221 list_del_init(p);
1222 break;
1223 }
1224 }
1225 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001227 /* Clean up other handlers, if any failed */
1228 if (err)
1229 __unregister_handlers();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001231 write_unlock(&handler_lock);
1232 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233}
1234
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001235void reiserfs_xattr_unregister_handlers(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001237 write_lock(&handler_lock);
1238 __unregister_handlers();
1239 write_unlock(&handler_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240}
1241
1242/* This will catch lookups from the fs root to .reiserfs_priv */
1243static int
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001244xattr_lookup_poison(struct dentry *dentry, struct qstr *q1, struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001246 struct dentry *priv_root = REISERFS_SB(dentry->d_sb)->priv_root;
1247 if (name->len == priv_root->d_name.len &&
1248 name->hash == priv_root->d_name.hash &&
1249 !memcmp(name->name, priv_root->d_name.name, name->len)) {
1250 return -ENOENT;
1251 } else if (q1->len == name->len &&
1252 !memcmp(q1->name, name->name, name->len))
1253 return 0;
1254 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255}
1256
1257static struct dentry_operations xattr_lookup_poison_ops = {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001258 .d_compare = xattr_lookup_poison,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259};
1260
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261/* We need to take a copy of the mount flags since things like
1262 * MS_RDONLY don't get set until *after* we're called.
1263 * mount_flags != mount_options */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001264int reiserfs_xattr_init(struct super_block *s, int mount_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001266 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001268 /* We need generation numbers to ensure that the oid mapping is correct
1269 * v3.5 filesystems don't have them. */
1270 if (!old_format_only(s)) {
1271 set_bit(REISERFS_XATTRS, &(REISERFS_SB(s)->s_mount_opt));
1272 } else if (reiserfs_xattrs_optional(s)) {
1273 /* Old format filesystem, but optional xattrs have been enabled
1274 * at mount time. Error out. */
1275 reiserfs_warning(s, "xattrs/ACLs not supported on pre v3.6 "
1276 "format filesystem. Failing mount.");
1277 err = -EOPNOTSUPP;
1278 goto error;
1279 } else {
1280 /* Old format filesystem, but no optional xattrs have been enabled. This
1281 * means we silently disable xattrs on the filesystem. */
1282 clear_bit(REISERFS_XATTRS, &(REISERFS_SB(s)->s_mount_opt));
1283 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001285 /* If we don't have the privroot located yet - go find it */
1286 if (reiserfs_xattrs(s) && !REISERFS_SB(s)->priv_root) {
1287 struct dentry *dentry;
1288 dentry = lookup_one_len(PRIVROOT_NAME, s->s_root,
1289 strlen(PRIVROOT_NAME));
1290 if (!IS_ERR(dentry)) {
1291 if (!(mount_flags & MS_RDONLY) && !dentry->d_inode) {
1292 struct inode *inode = dentry->d_parent->d_inode;
1293 down(&inode->i_sem);
1294 err = inode->i_op->mkdir(inode, dentry, 0700);
1295 up(&inode->i_sem);
1296 if (err) {
1297 dput(dentry);
1298 dentry = NULL;
1299 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001301 if (dentry && dentry->d_inode)
1302 reiserfs_warning(s,
1303 "Created %s on %s - reserved for "
1304 "xattr storage.",
1305 PRIVROOT_NAME,
1306 reiserfs_bdevname
1307 (inode->i_sb));
1308 } else if (!dentry->d_inode) {
1309 dput(dentry);
1310 dentry = NULL;
1311 }
1312 } else
1313 err = PTR_ERR(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001315 if (!err && dentry) {
1316 s->s_root->d_op = &xattr_lookup_poison_ops;
1317 reiserfs_mark_inode_private(dentry->d_inode);
1318 REISERFS_SB(s)->priv_root = dentry;
1319 } else if (!(mount_flags & MS_RDONLY)) { /* xattrs are unavailable */
1320 /* If we're read-only it just means that the dir hasn't been
1321 * created. Not an error -- just no xattrs on the fs. We'll
1322 * check again if we go read-write */
1323 reiserfs_warning(s, "xattrs/ACLs enabled and couldn't "
1324 "find/create .reiserfs_priv. Failing mount.");
1325 err = -EOPNOTSUPP;
1326 }
1327 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001329 error:
1330 /* This is only nonzero if there was an error initializing the xattr
1331 * directory or if there is a condition where we don't support them. */
1332 if (err) {
1333 clear_bit(REISERFS_XATTRS, &(REISERFS_SB(s)->s_mount_opt));
1334 clear_bit(REISERFS_XATTRS_USER, &(REISERFS_SB(s)->s_mount_opt));
1335 clear_bit(REISERFS_POSIXACL, &(REISERFS_SB(s)->s_mount_opt));
1336 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001338 /* The super_block MS_POSIXACL must mirror the (no)acl mount option. */
1339 s->s_flags = s->s_flags & ~MS_POSIXACL;
1340 if (reiserfs_posixacl(s))
1341 s->s_flags |= MS_POSIXACL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001343 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344}
1345
1346static int
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001347__reiserfs_permission(struct inode *inode, int mask, struct nameidata *nd,
1348 int need_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001350 umode_t mode = inode->i_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351
1352 if (mask & MAY_WRITE) {
1353 /*
1354 * Nobody gets write access to a read-only fs.
1355 */
1356 if (IS_RDONLY(inode) &&
1357 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
1358 return -EROFS;
1359
1360 /*
1361 * Nobody gets write access to an immutable file.
1362 */
1363 if (IS_IMMUTABLE(inode))
1364 return -EACCES;
1365 }
1366
1367 /* We don't do permission checks on the internal objects.
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001368 * Permissions are determined by the "owning" object. */
1369 if (is_reiserfs_priv_object(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 return 0;
1371
1372 if (current->fsuid == inode->i_uid) {
1373 mode >>= 6;
1374#ifdef CONFIG_REISERFS_FS_POSIX_ACL
1375 } else if (reiserfs_posixacl(inode->i_sb) &&
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001376 get_inode_sd_version(inode) != STAT_DATA_V1) {
1377 struct posix_acl *acl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378
1379 /* ACL can't contain additional permissions if
1380 the ACL_MASK entry is 0 */
1381 if (!(mode & S_IRWXG))
1382 goto check_groups;
1383
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001384 if (need_lock) {
1385 reiserfs_read_lock_xattr_i(inode);
1386 reiserfs_read_lock_xattrs(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001388 acl = reiserfs_get_acl(inode, ACL_TYPE_ACCESS);
1389 if (need_lock) {
1390 reiserfs_read_unlock_xattrs(inode->i_sb);
1391 reiserfs_read_unlock_xattr_i(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001393 if (IS_ERR(acl)) {
1394 if (PTR_ERR(acl) == -ENODATA)
1395 goto check_groups;
1396 return PTR_ERR(acl);
1397 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001399 if (acl) {
1400 int err = posix_acl_permission(inode, acl, mask);
1401 posix_acl_release(acl);
1402 if (err == -EACCES) {
1403 goto check_capabilities;
1404 }
1405 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 } else {
1407 goto check_groups;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001408 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409#endif
1410 } else {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001411 check_groups:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 if (in_group_p(inode->i_gid))
1413 mode >>= 3;
1414 }
1415
1416 /*
1417 * If the DACs are ok we don't need any capability check.
1418 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001419 if (((mode & mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == mask))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 return 0;
1421
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001422 check_capabilities:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 /*
1424 * Read/write DACs are always overridable.
1425 * Executable DACs are overridable if at least one exec bit is set.
1426 */
1427 if (!(mask & MAY_EXEC) ||
1428 (inode->i_mode & S_IXUGO) || S_ISDIR(inode->i_mode))
1429 if (capable(CAP_DAC_OVERRIDE))
1430 return 0;
1431
1432 /*
1433 * Searching includes executable on directories, else just read.
1434 */
1435 if (mask == MAY_READ || (S_ISDIR(inode->i_mode) && !(mask & MAY_WRITE)))
1436 if (capable(CAP_DAC_READ_SEARCH))
1437 return 0;
1438
1439 return -EACCES;
1440}
1441
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001442int reiserfs_permission(struct inode *inode, int mask, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001444 return __reiserfs_permission(inode, mask, nd, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445}
1446
1447int
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001448reiserfs_permission_locked(struct inode *inode, int mask, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001450 return __reiserfs_permission(inode, mask, nd, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451}