[PATCH] move capable() to capability.h
[deliverable/linux.git] / fs / reiserfs / xattr.c
CommitLineData
1da177e4
LT
1/*
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>
1da177e4
LT
42#include <asm/uaccess.h>
43#include <asm/checksum.h>
44#include <linux/smp_lock.h>
45#include <linux/stat.h>
46#include <asm/semaphore.h>
47
48#define FL_READONLY 128
49#define FL_DIR_SEM_HELD 256
50#define PRIVROOT_NAME ".reiserfs_priv"
51#define XAROOT_NAME "xattrs"
52
bd4c625c
LT
53static struct reiserfs_xattr_handler *find_xattr_handler_prefix(const char
54 *prefix);
1da177e4 55
bd4c625c 56static struct dentry *create_xa_root(struct super_block *sb)
1da177e4 57{
bd4c625c
LT
58 struct dentry *privroot = dget(REISERFS_SB(sb)->priv_root);
59 struct dentry *xaroot;
60
61 /* This needs to be created at mount-time */
62 if (!privroot)
63 return ERR_PTR(-EOPNOTSUPP);
64
65 xaroot = lookup_one_len(XAROOT_NAME, privroot, strlen(XAROOT_NAME));
66 if (IS_ERR(xaroot)) {
67 goto out;
68 } else if (!xaroot->d_inode) {
69 int err;
1b1dcc1b 70 mutex_lock(&privroot->d_inode->i_mutex);
bd4c625c
LT
71 err =
72 privroot->d_inode->i_op->mkdir(privroot->d_inode, xaroot,
73 0700);
1b1dcc1b 74 mutex_unlock(&privroot->d_inode->i_mutex);
bd4c625c
LT
75
76 if (err) {
77 dput(xaroot);
78 dput(privroot);
79 return ERR_PTR(err);
80 }
81 REISERFS_SB(sb)->xattr_root = dget(xaroot);
82 }
83
84 out:
85 dput(privroot);
86 return xaroot;
1da177e4
LT
87}
88
89/* This will return a dentry, or error, refering to the xa root directory.
90 * If the xa root doesn't exist yet, the dentry will be returned without
91 * an associated inode. This dentry can be used with ->mkdir to create
92 * the xa directory. */
bd4c625c 93static struct dentry *__get_xa_root(struct super_block *s)
1da177e4 94{
bd4c625c
LT
95 struct dentry *privroot = dget(REISERFS_SB(s)->priv_root);
96 struct dentry *xaroot = NULL;
97
98 if (IS_ERR(privroot) || !privroot)
99 return privroot;
100
101 xaroot = lookup_one_len(XAROOT_NAME, privroot, strlen(XAROOT_NAME));
102 if (IS_ERR(xaroot)) {
103 goto out;
104 } else if (!xaroot->d_inode) {
105 dput(xaroot);
106 xaroot = NULL;
107 goto out;
108 }
109
110 REISERFS_SB(s)->xattr_root = dget(xaroot);
111
112 out:
113 dput(privroot);
114 return xaroot;
1da177e4
LT
115}
116
117/* Returns the dentry (or NULL) referring to the root of the extended
4a4efbde
MM
118 * attribute directory tree. If it has already been retrieved, it is used.
119 * Otherwise, we attempt to retrieve it from disk. It may also return
1da177e4
LT
120 * a pointer-encoded error.
121 */
bd4c625c 122static inline struct dentry *get_xa_root(struct super_block *s)
1da177e4 123{
bd4c625c 124 struct dentry *dentry = dget(REISERFS_SB(s)->xattr_root);
1da177e4 125
bd4c625c
LT
126 if (!dentry)
127 dentry = __get_xa_root(s);
1da177e4 128
bd4c625c 129 return dentry;
1da177e4
LT
130}
131
132/* Opens the directory corresponding to the inode's extended attribute store.
133 * If flags allow, the tree to the directory may be created. If creation is
134 * prohibited, -ENODATA is returned. */
bd4c625c 135static struct dentry *open_xa_dir(const struct inode *inode, int flags)
1da177e4 136{
bd4c625c
LT
137 struct dentry *xaroot, *xadir;
138 char namebuf[17];
139
140 xaroot = get_xa_root(inode->i_sb);
141 if (IS_ERR(xaroot)) {
142 return xaroot;
143 } else if (!xaroot) {
144 if (flags == 0 || flags & XATTR_CREATE) {
145 xaroot = create_xa_root(inode->i_sb);
146 if (IS_ERR(xaroot))
147 return xaroot;
148 }
149 if (!xaroot)
150 return ERR_PTR(-ENODATA);
151 }
152
153 /* ok, we have xaroot open */
154
155 snprintf(namebuf, sizeof(namebuf), "%X.%X",
156 le32_to_cpu(INODE_PKEY(inode)->k_objectid),
157 inode->i_generation);
158 xadir = lookup_one_len(namebuf, xaroot, strlen(namebuf));
159 if (IS_ERR(xadir)) {
160 dput(xaroot);
161 return xadir;
162 }
163
164 if (!xadir->d_inode) {
165 int err;
166 if (flags == 0 || flags & XATTR_CREATE) {
167 /* Although there is nothing else trying to create this directory,
168 * another directory with the same hash may be created, so we need
169 * to protect against that */
170 err =
171 xaroot->d_inode->i_op->mkdir(xaroot->d_inode, xadir,
172 0700);
173 if (err) {
174 dput(xaroot);
175 dput(xadir);
176 return ERR_PTR(err);
177 }
178 }
179 if (!xadir->d_inode) {
180 dput(xaroot);
181 dput(xadir);
182 return ERR_PTR(-ENODATA);
183 }
184 }
185
186 dput(xaroot);
187 return xadir;
1da177e4
LT
188}
189
190/* Returns a dentry corresponding to a specific extended attribute file
191 * for the inode. If flags allow, the file is created. Otherwise, a
192 * valid or negative dentry, or an error is returned. */
bd4c625c
LT
193static struct dentry *get_xa_file_dentry(const struct inode *inode,
194 const char *name, int flags)
1da177e4 195{
bd4c625c
LT
196 struct dentry *xadir, *xafile;
197 int err = 0;
198
199 xadir = open_xa_dir(inode, flags);
200 if (IS_ERR(xadir)) {
201 return ERR_PTR(PTR_ERR(xadir));
202 } else if (xadir && !xadir->d_inode) {
203 dput(xadir);
204 return ERR_PTR(-ENODATA);
205 }
206
207 xafile = lookup_one_len(name, xadir, strlen(name));
208 if (IS_ERR(xafile)) {
209 dput(xadir);
210 return ERR_PTR(PTR_ERR(xafile));
211 }
212
213 if (xafile->d_inode) { /* file exists */
214 if (flags & XATTR_CREATE) {
215 err = -EEXIST;
216 dput(xafile);
217 goto out;
218 }
219 } else if (flags & XATTR_REPLACE || flags & FL_READONLY) {
220 goto out;
221 } else {
1b1dcc1b 222 /* inode->i_mutex is down, so nothing else can try to create
bd4c625c
LT
223 * the same xattr */
224 err = xadir->d_inode->i_op->create(xadir->d_inode, xafile,
225 0700 | S_IFREG, NULL);
226
227 if (err) {
228 dput(xafile);
229 goto out;
230 }
231 }
1da177e4 232
bd4c625c
LT
233 out:
234 dput(xadir);
235 if (err)
236 xafile = ERR_PTR(err);
237 return xafile;
238}
1da177e4
LT
239
240/* Opens a file pointer to the attribute associated with inode */
bd4c625c
LT
241static struct file *open_xa_file(const struct inode *inode, const char *name,
242 int flags)
1da177e4 243{
bd4c625c
LT
244 struct dentry *xafile;
245 struct file *fp;
246
247 xafile = get_xa_file_dentry(inode, name, flags);
248 if (IS_ERR(xafile))
249 return ERR_PTR(PTR_ERR(xafile));
250 else if (!xafile->d_inode) {
251 dput(xafile);
252 return ERR_PTR(-ENODATA);
253 }
1da177e4 254
bd4c625c
LT
255 fp = dentry_open(xafile, NULL, O_RDWR);
256 /* dentry_open dputs the dentry if it fails */
1da177e4 257
bd4c625c 258 return fp;
1da177e4
LT
259}
260
1da177e4
LT
261/*
262 * this is very similar to fs/reiserfs/dir.c:reiserfs_readdir, but
263 * we need to drop the path before calling the filldir struct. That
264 * would be a big performance hit to the non-xattr case, so I've copied
265 * the whole thing for now. --clm
266 *
267 * the big difference is that I go backwards through the directory,
268 * and don't mess with f->f_pos, but the idea is the same. Do some
269 * action on each and every entry in the directory.
270 *
1b1dcc1b 271 * we're called with i_mutex held, so there are no worries about the directory
1da177e4
LT
272 * changing underneath us.
273 */
bd4c625c 274static int __xattr_readdir(struct file *filp, void *dirent, filldir_t filldir)
1da177e4 275{
bd4c625c
LT
276 struct inode *inode = filp->f_dentry->d_inode;
277 struct cpu_key pos_key; /* key of current position in the directory (key of directory entry) */
278 INITIALIZE_PATH(path_to_entry);
279 struct buffer_head *bh;
280 int entry_num;
281 struct item_head *ih, tmp_ih;
282 int search_res;
283 char *local_buf;
284 loff_t next_pos;
285 char small_buf[32]; /* avoid kmalloc if we can */
286 struct reiserfs_de_head *deh;
287 int d_reclen;
288 char *d_name;
289 off_t d_off;
290 ino_t d_ino;
291 struct reiserfs_dir_entry de;
292
293 /* form key for search the next directory entry using f_pos field of
294 file structure */
295 next_pos = max_reiserfs_offset(inode);
296
297 while (1) {
298 research:
299 if (next_pos <= DOT_DOT_OFFSET)
300 break;
301 make_cpu_key(&pos_key, inode, next_pos, TYPE_DIRENTRY, 3);
302
303 search_res =
304 search_by_entry_key(inode->i_sb, &pos_key, &path_to_entry,
305 &de);
306 if (search_res == IO_ERROR) {
307 // FIXME: we could just skip part of directory which could
308 // not be read
309 pathrelse(&path_to_entry);
310 return -EIO;
311 }
1da177e4 312
bd4c625c
LT
313 if (search_res == NAME_NOT_FOUND)
314 de.de_entry_num--;
1da177e4 315
bd4c625c
LT
316 set_de_name_and_namelen(&de);
317 entry_num = de.de_entry_num;
318 deh = &(de.de_deh[entry_num]);
1da177e4 319
bd4c625c
LT
320 bh = de.de_bh;
321 ih = de.de_ih;
1da177e4 322
bd4c625c
LT
323 if (!is_direntry_le_ih(ih)) {
324 reiserfs_warning(inode->i_sb, "not direntry %h", ih);
325 break;
326 }
327 copy_item_head(&tmp_ih, ih);
1da177e4 328
bd4c625c
LT
329 /* we must have found item, that is item of this directory, */
330 RFALSE(COMP_SHORT_KEYS(&(ih->ih_key), &pos_key),
331 "vs-9000: found item %h does not match to dir we readdir %K",
332 ih, &pos_key);
1da177e4 333
bd4c625c
LT
334 if (deh_offset(deh) <= DOT_DOT_OFFSET) {
335 break;
336 }
1da177e4 337
bd4c625c
LT
338 /* look for the previous entry in the directory */
339 next_pos = deh_offset(deh) - 1;
1da177e4 340
bd4c625c
LT
341 if (!de_visible(deh))
342 /* it is hidden entry */
343 continue;
1da177e4 344
bd4c625c
LT
345 d_reclen = entry_length(bh, ih, entry_num);
346 d_name = B_I_DEH_ENTRY_FILE_NAME(bh, ih, deh);
347 d_off = deh_offset(deh);
348 d_ino = deh_objectid(deh);
1da177e4 349
bd4c625c
LT
350 if (!d_name[d_reclen - 1])
351 d_reclen = strlen(d_name);
1da177e4 352
bd4c625c
LT
353 if (d_reclen > REISERFS_MAX_NAME(inode->i_sb->s_blocksize)) {
354 /* too big to send back to VFS */
355 continue;
356 }
1da177e4 357
bd4c625c
LT
358 /* Ignore the .reiserfs_priv entry */
359 if (reiserfs_xattrs(inode->i_sb) &&
360 !old_format_only(inode->i_sb) &&
361 deh_objectid(deh) ==
362 le32_to_cpu(INODE_PKEY
363 (REISERFS_SB(inode->i_sb)->priv_root->d_inode)->
364 k_objectid))
365 continue;
366
367 if (d_reclen <= 32) {
368 local_buf = small_buf;
369 } else {
370 local_buf =
371 reiserfs_kmalloc(d_reclen, GFP_NOFS, inode->i_sb);
372 if (!local_buf) {
373 pathrelse(&path_to_entry);
374 return -ENOMEM;
375 }
376 if (item_moved(&tmp_ih, &path_to_entry)) {
377 reiserfs_kfree(local_buf, d_reclen,
378 inode->i_sb);
379
380 /* sigh, must retry. Do this same offset again */
381 next_pos = d_off;
382 goto research;
383 }
384 }
1da177e4 385
bd4c625c
LT
386 // Note, that we copy name to user space via temporary
387 // buffer (local_buf) because filldir will block if
388 // user space buffer is swapped out. At that time
389 // entry can move to somewhere else
390 memcpy(local_buf, d_name, d_reclen);
1da177e4 391
bd4c625c
LT
392 /* the filldir function might need to start transactions,
393 * or do who knows what. Release the path now that we've
394 * copied all the important stuff out of the deh
395 */
396 pathrelse(&path_to_entry);
397
398 if (filldir(dirent, local_buf, d_reclen, d_off, d_ino,
399 DT_UNKNOWN) < 0) {
400 if (local_buf != small_buf) {
401 reiserfs_kfree(local_buf, d_reclen,
402 inode->i_sb);
403 }
404 goto end;
405 }
406 if (local_buf != small_buf) {
407 reiserfs_kfree(local_buf, d_reclen, inode->i_sb);
408 }
409 } /* while */
1da177e4 410
bd4c625c
LT
411 end:
412 pathrelse(&path_to_entry);
413 return 0;
1da177e4
LT
414}
415
416/*
417 * this could be done with dedicated readdir ops for the xattr files,
418 * but I want to get something working asap
419 * this is stolen from vfs_readdir
420 *
421 */
422static
423int xattr_readdir(struct file *file, filldir_t filler, void *buf)
424{
bd4c625c
LT
425 struct inode *inode = file->f_dentry->d_inode;
426 int res = -ENOTDIR;
427 if (!file->f_op || !file->f_op->readdir)
428 goto out;
1b1dcc1b 429 mutex_lock(&inode->i_mutex);
1da177e4 430// down(&inode->i_zombie);
bd4c625c
LT
431 res = -ENOENT;
432 if (!IS_DEADDIR(inode)) {
433 lock_kernel();
434 res = __xattr_readdir(file, buf, filler);
435 unlock_kernel();
436 }
1da177e4 437// up(&inode->i_zombie);
1b1dcc1b 438 mutex_unlock(&inode->i_mutex);
bd4c625c
LT
439 out:
440 return res;
1da177e4
LT
441}
442
1da177e4 443/* Internal operations on file data */
bd4c625c 444static inline void reiserfs_put_page(struct page *page)
1da177e4 445{
bd4c625c
LT
446 kunmap(page);
447 page_cache_release(page);
1da177e4
LT
448}
449
bd4c625c 450static struct page *reiserfs_get_page(struct inode *dir, unsigned long n)
1da177e4 451{
bd4c625c
LT
452 struct address_space *mapping = dir->i_mapping;
453 struct page *page;
454 /* We can deadlock if we try to free dentries,
455 and an unlink/rmdir has just occured - GFP_NOFS avoids this */
c4cdd038 456 mapping_set_gfp_mask(mapping, GFP_NOFS);
bd4c625c
LT
457 page = read_cache_page(mapping, n,
458 (filler_t *) mapping->a_ops->readpage, NULL);
459 if (!IS_ERR(page)) {
460 wait_on_page_locked(page);
461 kmap(page);
462 if (!PageUptodate(page))
463 goto fail;
464
465 if (PageError(page))
466 goto fail;
467 }
468 return page;
469
470 fail:
471 reiserfs_put_page(page);
472 return ERR_PTR(-EIO);
1da177e4
LT
473}
474
bd4c625c 475static inline __u32 xattr_hash(const char *msg, int len)
1da177e4 476{
bd4c625c 477 return csum_partial(msg, len, 0);
1da177e4
LT
478}
479
480/* Generic extended attribute operations that can be used by xa plugins */
481
482/*
1b1dcc1b 483 * inode->i_mutex: down
1da177e4
LT
484 */
485int
bd4c625c
LT
486reiserfs_xattr_set(struct inode *inode, const char *name, const void *buffer,
487 size_t buffer_size, int flags)
1da177e4 488{
bd4c625c
LT
489 int err = 0;
490 struct file *fp;
491 struct page *page;
492 char *data;
493 struct address_space *mapping;
494 size_t file_pos = 0;
495 size_t buffer_pos = 0;
496 struct inode *xinode;
497 struct iattr newattrs;
498 __u32 xahash = 0;
499
bd4c625c
LT
500 if (get_inode_sd_version(inode) == STAT_DATA_V1)
501 return -EOPNOTSUPP;
502
503 /* Empty xattrs are ok, they're just empty files, no hash */
504 if (buffer && buffer_size)
505 xahash = xattr_hash(buffer, buffer_size);
506
507 open_file:
508 fp = open_xa_file(inode, name, flags);
509 if (IS_ERR(fp)) {
510 err = PTR_ERR(fp);
511 goto out;
512 }
513
514 xinode = fp->f_dentry->d_inode;
515 REISERFS_I(inode)->i_flags |= i_has_xattr_dir;
516
517 /* we need to copy it off.. */
518 if (xinode->i_nlink > 1) {
519 fput(fp);
520 err = reiserfs_xattr_del(inode, name);
521 if (err < 0)
522 goto out;
523 /* We just killed the old one, we're not replacing anymore */
524 if (flags & XATTR_REPLACE)
525 flags &= ~XATTR_REPLACE;
526 goto open_file;
527 }
528
529 /* Resize it so we're ok to write there */
530 newattrs.ia_size = buffer_size;
531 newattrs.ia_valid = ATTR_SIZE | ATTR_CTIME;
1b1dcc1b 532 mutex_lock(&xinode->i_mutex);
bd4c625c
LT
533 err = notify_change(fp->f_dentry, &newattrs);
534 if (err)
535 goto out_filp;
536
537 mapping = xinode->i_mapping;
538 while (buffer_pos < buffer_size || buffer_pos == 0) {
539 size_t chunk;
540 size_t skip = 0;
541 size_t page_offset = (file_pos & (PAGE_CACHE_SIZE - 1));
542 if (buffer_size - buffer_pos > PAGE_CACHE_SIZE)
543 chunk = PAGE_CACHE_SIZE;
544 else
545 chunk = buffer_size - buffer_pos;
546
547 page = reiserfs_get_page(xinode, file_pos >> PAGE_CACHE_SHIFT);
548 if (IS_ERR(page)) {
549 err = PTR_ERR(page);
550 goto out_filp;
551 }
552
553 lock_page(page);
554 data = page_address(page);
555
556 if (file_pos == 0) {
557 struct reiserfs_xattr_header *rxh;
558 skip = file_pos = sizeof(struct reiserfs_xattr_header);
559 if (chunk + skip > PAGE_CACHE_SIZE)
560 chunk = PAGE_CACHE_SIZE - skip;
561 rxh = (struct reiserfs_xattr_header *)data;
562 rxh->h_magic = cpu_to_le32(REISERFS_XATTR_MAGIC);
563 rxh->h_hash = cpu_to_le32(xahash);
564 }
565
566 err = mapping->a_ops->prepare_write(fp, page, page_offset,
567 page_offset + chunk + skip);
568 if (!err) {
569 if (buffer)
570 memcpy(data + skip, buffer + buffer_pos, chunk);
571 err =
572 mapping->a_ops->commit_write(fp, page, page_offset,
573 page_offset + chunk +
574 skip);
575 }
576 unlock_page(page);
577 reiserfs_put_page(page);
578 buffer_pos += chunk;
579 file_pos += chunk;
580 skip = 0;
581 if (err || buffer_size == 0 || !buffer)
582 break;
583 }
584
585 /* We can't mark the inode dirty if it's not hashed. This is the case
586 * when we're inheriting the default ACL. If we dirty it, the inode
587 * gets marked dirty, but won't (ever) make it onto the dirty list until
588 * it's synced explicitly to clear I_DIRTY. This is bad. */
589 if (!hlist_unhashed(&inode->i_hash)) {
590 inode->i_ctime = CURRENT_TIME_SEC;
591 mark_inode_dirty(inode);
1da177e4 592 }
bd4c625c
LT
593
594 out_filp:
1b1dcc1b 595 mutex_unlock(&xinode->i_mutex);
bd4c625c
LT
596 fput(fp);
597
598 out:
599 return err;
1da177e4
LT
600}
601
602/*
1b1dcc1b 603 * inode->i_mutex: down
1da177e4
LT
604 */
605int
bd4c625c
LT
606reiserfs_xattr_get(const struct inode *inode, const char *name, void *buffer,
607 size_t buffer_size)
1da177e4 608{
bd4c625c
LT
609 ssize_t err = 0;
610 struct file *fp;
611 size_t isize;
612 size_t file_pos = 0;
613 size_t buffer_pos = 0;
614 struct page *page;
615 struct inode *xinode;
616 __u32 hash = 0;
617
618 if (name == NULL)
619 return -EINVAL;
620
621 /* We can't have xattrs attached to v1 items since they don't have
622 * generation numbers */
623 if (get_inode_sd_version(inode) == STAT_DATA_V1)
624 return -EOPNOTSUPP;
625
626 fp = open_xa_file(inode, name, FL_READONLY);
627 if (IS_ERR(fp)) {
628 err = PTR_ERR(fp);
629 goto out;
630 }
631
632 xinode = fp->f_dentry->d_inode;
633 isize = xinode->i_size;
634 REISERFS_I(inode)->i_flags |= i_has_xattr_dir;
635
636 /* Just return the size needed */
637 if (buffer == NULL) {
638 err = isize - sizeof(struct reiserfs_xattr_header);
639 goto out_dput;
640 }
641
642 if (buffer_size < isize - sizeof(struct reiserfs_xattr_header)) {
643 err = -ERANGE;
644 goto out_dput;
645 }
646
647 while (file_pos < isize) {
648 size_t chunk;
649 char *data;
650 size_t skip = 0;
651 if (isize - file_pos > PAGE_CACHE_SIZE)
652 chunk = PAGE_CACHE_SIZE;
653 else
654 chunk = isize - file_pos;
655
656 page = reiserfs_get_page(xinode, file_pos >> PAGE_CACHE_SHIFT);
657 if (IS_ERR(page)) {
658 err = PTR_ERR(page);
659 goto out_dput;
660 }
661
662 lock_page(page);
663 data = page_address(page);
664 if (file_pos == 0) {
665 struct reiserfs_xattr_header *rxh =
666 (struct reiserfs_xattr_header *)data;
667 skip = file_pos = sizeof(struct reiserfs_xattr_header);
668 chunk -= skip;
669 /* Magic doesn't match up.. */
670 if (rxh->h_magic != cpu_to_le32(REISERFS_XATTR_MAGIC)) {
671 unlock_page(page);
672 reiserfs_put_page(page);
673 reiserfs_warning(inode->i_sb,
674 "Invalid magic for xattr (%s) "
675 "associated with %k", name,
676 INODE_PKEY(inode));
677 err = -EIO;
678 goto out_dput;
679 }
680 hash = le32_to_cpu(rxh->h_hash);
681 }
682 memcpy(buffer + buffer_pos, data + skip, chunk);
683 unlock_page(page);
684 reiserfs_put_page(page);
685 file_pos += chunk;
686 buffer_pos += chunk;
687 skip = 0;
688 }
689 err = isize - sizeof(struct reiserfs_xattr_header);
690
691 if (xattr_hash(buffer, isize - sizeof(struct reiserfs_xattr_header)) !=
692 hash) {
693 reiserfs_warning(inode->i_sb,
694 "Invalid hash for xattr (%s) associated "
695 "with %k", name, INODE_PKEY(inode));
696 err = -EIO;
697 }
698
699 out_dput:
700 fput(fp);
701
702 out:
703 return err;
1da177e4
LT
704}
705
706static int
bd4c625c 707__reiserfs_xattr_del(struct dentry *xadir, const char *name, int namelen)
1da177e4 708{
bd4c625c
LT
709 struct dentry *dentry;
710 struct inode *dir = xadir->d_inode;
711 int err = 0;
712
713 dentry = lookup_one_len(name, xadir, namelen);
714 if (IS_ERR(dentry)) {
715 err = PTR_ERR(dentry);
716 goto out;
717 } else if (!dentry->d_inode) {
718 err = -ENODATA;
719 goto out_file;
720 }
721
722 /* Skip directories.. */
723 if (S_ISDIR(dentry->d_inode->i_mode))
724 goto out_file;
725
726 if (!is_reiserfs_priv_object(dentry->d_inode)) {
727 reiserfs_warning(dir->i_sb, "OID %08x [%.*s/%.*s] doesn't have "
728 "priv flag set [parent is %sset].",
729 le32_to_cpu(INODE_PKEY(dentry->d_inode)->
730 k_objectid), xadir->d_name.len,
731 xadir->d_name.name, namelen, name,
732 is_reiserfs_priv_object(xadir->
733 d_inode) ? "" :
734 "not ");
735 dput(dentry);
736 return -EIO;
737 }
1da177e4 738
bd4c625c
LT
739 err = dir->i_op->unlink(dir, dentry);
740 if (!err)
741 d_delete(dentry);
1da177e4 742
bd4c625c
LT
743 out_file:
744 dput(dentry);
745
746 out:
747 return err;
748}
749
750int reiserfs_xattr_del(struct inode *inode, const char *name)
1da177e4 751{
bd4c625c
LT
752 struct dentry *dir;
753 int err;
1da177e4 754
bd4c625c
LT
755 dir = open_xa_dir(inode, FL_READONLY);
756 if (IS_ERR(dir)) {
757 err = PTR_ERR(dir);
758 goto out;
759 }
1da177e4 760
bd4c625c
LT
761 err = __reiserfs_xattr_del(dir, name, strlen(name));
762 dput(dir);
1da177e4 763
bd4c625c
LT
764 if (!err) {
765 inode->i_ctime = CURRENT_TIME_SEC;
766 mark_inode_dirty(inode);
767 }
1da177e4 768
bd4c625c
LT
769 out:
770 return err;
1da177e4
LT
771}
772
773/* The following are side effects of other operations that aren't explicitly
774 * modifying extended attributes. This includes operations such as permissions
775 * or ownership changes, object deletions, etc. */
776
777static int
bd4c625c
LT
778reiserfs_delete_xattrs_filler(void *buf, const char *name, int namelen,
779 loff_t offset, ino_t ino, unsigned int d_type)
1da177e4 780{
bd4c625c 781 struct dentry *xadir = (struct dentry *)buf;
1da177e4 782
bd4c625c 783 return __reiserfs_xattr_del(xadir, name, namelen);
1da177e4
LT
784
785}
786
1b1dcc1b 787/* This is called w/ inode->i_mutex downed */
bd4c625c 788int reiserfs_delete_xattrs(struct inode *inode)
1da177e4 789{
bd4c625c
LT
790 struct file *fp;
791 struct dentry *dir, *root;
792 int err = 0;
793
794 /* Skip out, an xattr has no xattrs associated with it */
795 if (is_reiserfs_priv_object(inode) ||
796 get_inode_sd_version(inode) == STAT_DATA_V1 ||
797 !reiserfs_xattrs(inode->i_sb)) {
798 return 0;
799 }
800 reiserfs_read_lock_xattrs(inode->i_sb);
801 dir = open_xa_dir(inode, FL_READONLY);
802 reiserfs_read_unlock_xattrs(inode->i_sb);
803 if (IS_ERR(dir)) {
804 err = PTR_ERR(dir);
805 goto out;
806 } else if (!dir->d_inode) {
807 dput(dir);
808 return 0;
809 }
810
811 fp = dentry_open(dir, NULL, O_RDWR);
812 if (IS_ERR(fp)) {
813 err = PTR_ERR(fp);
814 /* dentry_open dputs the dentry if it fails */
815 goto out;
816 }
817
818 lock_kernel();
819 err = xattr_readdir(fp, reiserfs_delete_xattrs_filler, dir);
820 if (err) {
821 unlock_kernel();
822 goto out_dir;
823 }
824
825 /* Leftovers besides . and .. -- that's not good. */
826 if (dir->d_inode->i_nlink <= 2) {
827 root = get_xa_root(inode->i_sb);
828 reiserfs_write_lock_xattrs(inode->i_sb);
829 err = vfs_rmdir(root->d_inode, dir);
830 reiserfs_write_unlock_xattrs(inode->i_sb);
831 dput(root);
832 } else {
833 reiserfs_warning(inode->i_sb,
834 "Couldn't remove all entries in directory");
835 }
836 unlock_kernel();
837
838 out_dir:
839 fput(fp);
840
841 out:
842 if (!err)
843 REISERFS_I(inode)->i_flags =
844 REISERFS_I(inode)->i_flags & ~i_has_xattr_dir;
845 return err;
1da177e4
LT
846}
847
848struct reiserfs_chown_buf {
bd4c625c
LT
849 struct inode *inode;
850 struct dentry *xadir;
851 struct iattr *attrs;
1da177e4
LT
852};
853
854/* XXX: If there is a better way to do this, I'd love to hear about it */
855static int
bd4c625c
LT
856reiserfs_chown_xattrs_filler(void *buf, const char *name, int namelen,
857 loff_t offset, ino_t ino, unsigned int d_type)
1da177e4 858{
bd4c625c
LT
859 struct reiserfs_chown_buf *chown_buf = (struct reiserfs_chown_buf *)buf;
860 struct dentry *xafile, *xadir = chown_buf->xadir;
861 struct iattr *attrs = chown_buf->attrs;
862 int err = 0;
863
864 xafile = lookup_one_len(name, xadir, namelen);
865 if (IS_ERR(xafile))
866 return PTR_ERR(xafile);
867 else if (!xafile->d_inode) {
868 dput(xafile);
869 return -ENODATA;
870 }
871
872 if (!S_ISDIR(xafile->d_inode->i_mode))
873 err = notify_change(xafile, attrs);
874 dput(xafile);
875
876 return err;
1da177e4
LT
877}
878
bd4c625c 879int reiserfs_chown_xattrs(struct inode *inode, struct iattr *attrs)
1da177e4 880{
bd4c625c
LT
881 struct file *fp;
882 struct dentry *dir;
883 int err = 0;
884 struct reiserfs_chown_buf buf;
885 unsigned int ia_valid = attrs->ia_valid;
886
887 /* Skip out, an xattr has no xattrs associated with it */
888 if (is_reiserfs_priv_object(inode) ||
889 get_inode_sd_version(inode) == STAT_DATA_V1 ||
890 !reiserfs_xattrs(inode->i_sb)) {
891 return 0;
892 }
893 reiserfs_read_lock_xattrs(inode->i_sb);
894 dir = open_xa_dir(inode, FL_READONLY);
895 reiserfs_read_unlock_xattrs(inode->i_sb);
896 if (IS_ERR(dir)) {
897 if (PTR_ERR(dir) != -ENODATA)
898 err = PTR_ERR(dir);
899 goto out;
900 } else if (!dir->d_inode) {
901 dput(dir);
902 goto out;
903 }
904
905 fp = dentry_open(dir, NULL, O_RDWR);
906 if (IS_ERR(fp)) {
907 err = PTR_ERR(fp);
908 /* dentry_open dputs the dentry if it fails */
909 goto out;
910 }
1da177e4 911
bd4c625c
LT
912 lock_kernel();
913
914 attrs->ia_valid &= (ATTR_UID | ATTR_GID | ATTR_CTIME);
915 buf.xadir = dir;
916 buf.attrs = attrs;
917 buf.inode = inode;
918
919 err = xattr_readdir(fp, reiserfs_chown_xattrs_filler, &buf);
920 if (err) {
921 unlock_kernel();
922 goto out_dir;
923 }
924
925 err = notify_change(dir, attrs);
926 unlock_kernel();
927
928 out_dir:
929 fput(fp);
930
931 out:
932 attrs->ia_valid = ia_valid;
933 return err;
934}
1da177e4
LT
935
936/* Actual operations that are exported to VFS-land */
937
938/*
939 * Inode operation getxattr()
1b1dcc1b 940 * Preliminary locking: we down dentry->d_inode->i_mutex
1da177e4
LT
941 */
942ssize_t
bd4c625c
LT
943reiserfs_getxattr(struct dentry * dentry, const char *name, void *buffer,
944 size_t size)
1da177e4 945{
bd4c625c
LT
946 struct reiserfs_xattr_handler *xah = find_xattr_handler_prefix(name);
947 int err;
948
949 if (!xah || !reiserfs_xattrs(dentry->d_sb) ||
950 get_inode_sd_version(dentry->d_inode) == STAT_DATA_V1)
951 return -EOPNOTSUPP;
952
953 reiserfs_read_lock_xattr_i(dentry->d_inode);
954 reiserfs_read_lock_xattrs(dentry->d_sb);
955 err = xah->get(dentry->d_inode, name, buffer, size);
956 reiserfs_read_unlock_xattrs(dentry->d_sb);
957 reiserfs_read_unlock_xattr_i(dentry->d_inode);
958 return err;
1da177e4
LT
959}
960
1da177e4
LT
961/*
962 * Inode operation setxattr()
963 *
1b1dcc1b 964 * dentry->d_inode->i_mutex down
1da177e4
LT
965 */
966int
bd4c625c
LT
967reiserfs_setxattr(struct dentry *dentry, const char *name, const void *value,
968 size_t size, int flags)
1da177e4 969{
bd4c625c
LT
970 struct reiserfs_xattr_handler *xah = find_xattr_handler_prefix(name);
971 int err;
972 int lock;
973
974 if (!xah || !reiserfs_xattrs(dentry->d_sb) ||
975 get_inode_sd_version(dentry->d_inode) == STAT_DATA_V1)
976 return -EOPNOTSUPP;
977
bd4c625c
LT
978 reiserfs_write_lock_xattr_i(dentry->d_inode);
979 lock = !has_xattr_dir(dentry->d_inode);
980 if (lock)
981 reiserfs_write_lock_xattrs(dentry->d_sb);
982 else
983 reiserfs_read_lock_xattrs(dentry->d_sb);
984 err = xah->set(dentry->d_inode, name, value, size, flags);
985 if (lock)
986 reiserfs_write_unlock_xattrs(dentry->d_sb);
987 else
988 reiserfs_read_unlock_xattrs(dentry->d_sb);
989 reiserfs_write_unlock_xattr_i(dentry->d_inode);
990 return err;
1da177e4
LT
991}
992
993/*
994 * Inode operation removexattr()
995 *
1b1dcc1b 996 * dentry->d_inode->i_mutex down
1da177e4 997 */
bd4c625c 998int reiserfs_removexattr(struct dentry *dentry, const char *name)
1da177e4 999{
bd4c625c
LT
1000 int err;
1001 struct reiserfs_xattr_handler *xah = find_xattr_handler_prefix(name);
1da177e4 1002
bd4c625c
LT
1003 if (!xah || !reiserfs_xattrs(dentry->d_sb) ||
1004 get_inode_sd_version(dentry->d_inode) == STAT_DATA_V1)
1005 return -EOPNOTSUPP;
1da177e4 1006
bd4c625c
LT
1007 reiserfs_write_lock_xattr_i(dentry->d_inode);
1008 reiserfs_read_lock_xattrs(dentry->d_sb);
1da177e4 1009
bd4c625c
LT
1010 /* Deletion pre-operation */
1011 if (xah->del) {
1012 err = xah->del(dentry->d_inode, name);
1013 if (err)
1014 goto out;
1015 }
1da177e4 1016
bd4c625c 1017 err = reiserfs_xattr_del(dentry->d_inode, name);
1da177e4 1018
bd4c625c
LT
1019 dentry->d_inode->i_ctime = CURRENT_TIME_SEC;
1020 mark_inode_dirty(dentry->d_inode);
1da177e4 1021
bd4c625c
LT
1022 out:
1023 reiserfs_read_unlock_xattrs(dentry->d_sb);
1024 reiserfs_write_unlock_xattr_i(dentry->d_inode);
1025 return err;
1da177e4
LT
1026}
1027
1da177e4
LT
1028/* This is what filldir will use:
1029 * r_pos will always contain the amount of space required for the entire
1030 * list. If r_pos becomes larger than r_size, we need more space and we
1031 * return an error indicating this. If r_pos is less than r_size, then we've
1032 * filled the buffer successfully and we return success */
1033struct reiserfs_listxattr_buf {
bd4c625c
LT
1034 int r_pos;
1035 int r_size;
1036 char *r_buf;
1037 struct inode *r_inode;
1da177e4
LT
1038};
1039
1040static int
bd4c625c
LT
1041reiserfs_listxattr_filler(void *buf, const char *name, int namelen,
1042 loff_t offset, ino_t ino, unsigned int d_type)
1da177e4 1043{
bd4c625c
LT
1044 struct reiserfs_listxattr_buf *b = (struct reiserfs_listxattr_buf *)buf;
1045 int len = 0;
1046 if (name[0] != '.'
1047 || (namelen != 1 && (name[1] != '.' || namelen != 2))) {
1048 struct reiserfs_xattr_handler *xah =
1049 find_xattr_handler_prefix(name);
1050 if (!xah)
1051 return 0; /* Unsupported xattr name, skip it */
1052
1053 /* We call ->list() twice because the operation isn't required to just
1054 * return the name back - we want to make sure we have enough space */
1055 len += xah->list(b->r_inode, name, namelen, NULL);
1056
1057 if (len) {
1058 if (b->r_pos + len + 1 <= b->r_size) {
1059 char *p = b->r_buf + b->r_pos;
1060 p += xah->list(b->r_inode, name, namelen, p);
1061 *p++ = '\0';
1062 }
1063 b->r_pos += len + 1;
1064 }
1065 }
1066
1067 return 0;
1da177e4 1068}
bd4c625c 1069
1da177e4
LT
1070/*
1071 * Inode operation listxattr()
1072 *
1b1dcc1b 1073 * Preliminary locking: we down dentry->d_inode->i_mutex
1da177e4 1074 */
bd4c625c 1075ssize_t reiserfs_listxattr(struct dentry * dentry, char *buffer, size_t size)
1da177e4 1076{
bd4c625c
LT
1077 struct file *fp;
1078 struct dentry *dir;
1079 int err = 0;
1080 struct reiserfs_listxattr_buf buf;
1081
1082 if (!dentry->d_inode)
1083 return -EINVAL;
1084
1085 if (!reiserfs_xattrs(dentry->d_sb) ||
1086 get_inode_sd_version(dentry->d_inode) == STAT_DATA_V1)
1087 return -EOPNOTSUPP;
1088
1089 reiserfs_read_lock_xattr_i(dentry->d_inode);
1090 reiserfs_read_lock_xattrs(dentry->d_sb);
1091 dir = open_xa_dir(dentry->d_inode, FL_READONLY);
1092 reiserfs_read_unlock_xattrs(dentry->d_sb);
1093 if (IS_ERR(dir)) {
1094 err = PTR_ERR(dir);
1095 if (err == -ENODATA)
1096 err = 0; /* Not an error if there aren't any xattrs */
1097 goto out;
1098 }
1099
1100 fp = dentry_open(dir, NULL, O_RDWR);
1101 if (IS_ERR(fp)) {
1102 err = PTR_ERR(fp);
1103 /* dentry_open dputs the dentry if it fails */
1104 goto out;
1105 }
1106
1107 buf.r_buf = buffer;
1108 buf.r_size = buffer ? size : 0;
1109 buf.r_pos = 0;
1110 buf.r_inode = dentry->d_inode;
1111
1112 REISERFS_I(dentry->d_inode)->i_flags |= i_has_xattr_dir;
1113
1114 err = xattr_readdir(fp, reiserfs_listxattr_filler, &buf);
1115 if (err)
1116 goto out_dir;
1117
1118 if (buf.r_pos > buf.r_size && buffer != NULL)
1119 err = -ERANGE;
1120 else
1121 err = buf.r_pos;
1122
1123 out_dir:
1124 fput(fp);
1125
1126 out:
1127 reiserfs_read_unlock_xattr_i(dentry->d_inode);
1128 return err;
1da177e4
LT
1129}
1130
1131/* This is the implementation for the xattr plugin infrastructure */
bd4c625c 1132static struct list_head xattr_handlers = LIST_HEAD_INIT(xattr_handlers);
1da177e4
LT
1133static DEFINE_RWLOCK(handler_lock);
1134
bd4c625c
LT
1135static struct reiserfs_xattr_handler *find_xattr_handler_prefix(const char
1136 *prefix)
1da177e4 1137{
bd4c625c
LT
1138 struct reiserfs_xattr_handler *xah = NULL;
1139 struct list_head *p;
1140
1141 read_lock(&handler_lock);
1142 list_for_each(p, &xattr_handlers) {
1143 xah = list_entry(p, struct reiserfs_xattr_handler, handlers);
1144 if (strncmp(xah->prefix, prefix, strlen(xah->prefix)) == 0)
1145 break;
1146 xah = NULL;
1147 }
1148
1149 read_unlock(&handler_lock);
1150 return xah;
1da177e4
LT
1151}
1152
bd4c625c 1153static void __unregister_handlers(void)
1da177e4 1154{
bd4c625c
LT
1155 struct reiserfs_xattr_handler *xah;
1156 struct list_head *p, *tmp;
1da177e4 1157
bd4c625c
LT
1158 list_for_each_safe(p, tmp, &xattr_handlers) {
1159 xah = list_entry(p, struct reiserfs_xattr_handler, handlers);
1160 if (xah->exit)
1161 xah->exit();
1da177e4 1162
bd4c625c
LT
1163 list_del_init(p);
1164 }
1165 INIT_LIST_HEAD(&xattr_handlers);
1da177e4
LT
1166}
1167
bd4c625c 1168int __init reiserfs_xattr_register_handlers(void)
1da177e4 1169{
bd4c625c
LT
1170 int err = 0;
1171 struct reiserfs_xattr_handler *xah;
1172 struct list_head *p;
1da177e4 1173
bd4c625c 1174 write_lock(&handler_lock);
1da177e4 1175
bd4c625c
LT
1176 /* If we're already initialized, nothing to do */
1177 if (!list_empty(&xattr_handlers)) {
1178 write_unlock(&handler_lock);
1179 return 0;
1180 }
1da177e4 1181
bd4c625c
LT
1182 /* Add the handlers */
1183 list_add_tail(&user_handler.handlers, &xattr_handlers);
1184 list_add_tail(&trusted_handler.handlers, &xattr_handlers);
1da177e4 1185#ifdef CONFIG_REISERFS_FS_SECURITY
bd4c625c 1186 list_add_tail(&security_handler.handlers, &xattr_handlers);
1da177e4
LT
1187#endif
1188#ifdef CONFIG_REISERFS_FS_POSIX_ACL
bd4c625c
LT
1189 list_add_tail(&posix_acl_access_handler.handlers, &xattr_handlers);
1190 list_add_tail(&posix_acl_default_handler.handlers, &xattr_handlers);
1da177e4
LT
1191#endif
1192
bd4c625c
LT
1193 /* Run initializers, if available */
1194 list_for_each(p, &xattr_handlers) {
1195 xah = list_entry(p, struct reiserfs_xattr_handler, handlers);
1196 if (xah->init) {
1197 err = xah->init();
1198 if (err) {
1199 list_del_init(p);
1200 break;
1201 }
1202 }
1203 }
1204
1205 /* Clean up other handlers, if any failed */
1206 if (err)
1207 __unregister_handlers();
1208
1209 write_unlock(&handler_lock);
1210 return err;
1da177e4
LT
1211}
1212
bd4c625c 1213void reiserfs_xattr_unregister_handlers(void)
1da177e4 1214{
bd4c625c
LT
1215 write_lock(&handler_lock);
1216 __unregister_handlers();
1217 write_unlock(&handler_lock);
1da177e4
LT
1218}
1219
1220/* This will catch lookups from the fs root to .reiserfs_priv */
1221static int
bd4c625c 1222xattr_lookup_poison(struct dentry *dentry, struct qstr *q1, struct qstr *name)
1da177e4 1223{
bd4c625c
LT
1224 struct dentry *priv_root = REISERFS_SB(dentry->d_sb)->priv_root;
1225 if (name->len == priv_root->d_name.len &&
1226 name->hash == priv_root->d_name.hash &&
1227 !memcmp(name->name, priv_root->d_name.name, name->len)) {
1228 return -ENOENT;
1229 } else if (q1->len == name->len &&
1230 !memcmp(q1->name, name->name, name->len))
1231 return 0;
1232 return 1;
1da177e4
LT
1233}
1234
1235static struct dentry_operations xattr_lookup_poison_ops = {
bd4c625c 1236 .d_compare = xattr_lookup_poison,
1da177e4
LT
1237};
1238
1da177e4
LT
1239/* We need to take a copy of the mount flags since things like
1240 * MS_RDONLY don't get set until *after* we're called.
1241 * mount_flags != mount_options */
bd4c625c 1242int reiserfs_xattr_init(struct super_block *s, int mount_flags)
1da177e4 1243{
bd4c625c
LT
1244 int err = 0;
1245
1246 /* We need generation numbers to ensure that the oid mapping is correct
1247 * v3.5 filesystems don't have them. */
1248 if (!old_format_only(s)) {
1249 set_bit(REISERFS_XATTRS, &(REISERFS_SB(s)->s_mount_opt));
1250 } else if (reiserfs_xattrs_optional(s)) {
1251 /* Old format filesystem, but optional xattrs have been enabled
1252 * at mount time. Error out. */
1253 reiserfs_warning(s, "xattrs/ACLs not supported on pre v3.6 "
1254 "format filesystem. Failing mount.");
1255 err = -EOPNOTSUPP;
1256 goto error;
1257 } else {
1258 /* Old format filesystem, but no optional xattrs have been enabled. This
1259 * means we silently disable xattrs on the filesystem. */
1260 clear_bit(REISERFS_XATTRS, &(REISERFS_SB(s)->s_mount_opt));
1261 }
1262
1263 /* If we don't have the privroot located yet - go find it */
1264 if (reiserfs_xattrs(s) && !REISERFS_SB(s)->priv_root) {
1265 struct dentry *dentry;
1266 dentry = lookup_one_len(PRIVROOT_NAME, s->s_root,
1267 strlen(PRIVROOT_NAME));
1268 if (!IS_ERR(dentry)) {
1269 if (!(mount_flags & MS_RDONLY) && !dentry->d_inode) {
1270 struct inode *inode = dentry->d_parent->d_inode;
1b1dcc1b 1271 mutex_lock(&inode->i_mutex);
bd4c625c 1272 err = inode->i_op->mkdir(inode, dentry, 0700);
1b1dcc1b 1273 mutex_unlock(&inode->i_mutex);
bd4c625c
LT
1274 if (err) {
1275 dput(dentry);
1276 dentry = NULL;
1277 }
1278
1279 if (dentry && dentry->d_inode)
1280 reiserfs_warning(s,
1281 "Created %s on %s - reserved for "
1282 "xattr storage.",
1283 PRIVROOT_NAME,
1284 reiserfs_bdevname
1285 (inode->i_sb));
1286 } else if (!dentry->d_inode) {
1287 dput(dentry);
1288 dentry = NULL;
1289 }
1290 } else
1291 err = PTR_ERR(dentry);
1292
1293 if (!err && dentry) {
1294 s->s_root->d_op = &xattr_lookup_poison_ops;
1295 reiserfs_mark_inode_private(dentry->d_inode);
1296 REISERFS_SB(s)->priv_root = dentry;
1297 } else if (!(mount_flags & MS_RDONLY)) { /* xattrs are unavailable */
1298 /* If we're read-only it just means that the dir hasn't been
1299 * created. Not an error -- just no xattrs on the fs. We'll
1300 * check again if we go read-write */
1301 reiserfs_warning(s, "xattrs/ACLs enabled and couldn't "
1302 "find/create .reiserfs_priv. Failing mount.");
1303 err = -EOPNOTSUPP;
1304 }
1305 }
1306
1307 error:
1308 /* This is only nonzero if there was an error initializing the xattr
1309 * directory or if there is a condition where we don't support them. */
1310 if (err) {
1311 clear_bit(REISERFS_XATTRS, &(REISERFS_SB(s)->s_mount_opt));
1312 clear_bit(REISERFS_XATTRS_USER, &(REISERFS_SB(s)->s_mount_opt));
1313 clear_bit(REISERFS_POSIXACL, &(REISERFS_SB(s)->s_mount_opt));
1314 }
1315
1316 /* The super_block MS_POSIXACL must mirror the (no)acl mount option. */
1317 s->s_flags = s->s_flags & ~MS_POSIXACL;
1318 if (reiserfs_posixacl(s))
1319 s->s_flags |= MS_POSIXACL;
1320
1321 return err;
1da177e4
LT
1322}
1323
1324static int
bd4c625c
LT
1325__reiserfs_permission(struct inode *inode, int mask, struct nameidata *nd,
1326 int need_lock)
1da177e4 1327{
bd4c625c 1328 umode_t mode = inode->i_mode;
1da177e4
LT
1329
1330 if (mask & MAY_WRITE) {
1331 /*
1332 * Nobody gets write access to a read-only fs.
1333 */
1334 if (IS_RDONLY(inode) &&
1335 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
1336 return -EROFS;
1337
1338 /*
1339 * Nobody gets write access to an immutable file.
1340 */
1341 if (IS_IMMUTABLE(inode))
1342 return -EACCES;
1343 }
1344
1345 /* We don't do permission checks on the internal objects.
bd4c625c
LT
1346 * Permissions are determined by the "owning" object. */
1347 if (is_reiserfs_priv_object(inode))
1da177e4
LT
1348 return 0;
1349
1350 if (current->fsuid == inode->i_uid) {
1351 mode >>= 6;
1352#ifdef CONFIG_REISERFS_FS_POSIX_ACL
1353 } else if (reiserfs_posixacl(inode->i_sb) &&
bd4c625c
LT
1354 get_inode_sd_version(inode) != STAT_DATA_V1) {
1355 struct posix_acl *acl;
1da177e4
LT
1356
1357 /* ACL can't contain additional permissions if
1358 the ACL_MASK entry is 0 */
1359 if (!(mode & S_IRWXG))
1360 goto check_groups;
1361
bd4c625c
LT
1362 if (need_lock) {
1363 reiserfs_read_lock_xattr_i(inode);
1364 reiserfs_read_lock_xattrs(inode->i_sb);
1365 }
1366 acl = reiserfs_get_acl(inode, ACL_TYPE_ACCESS);
1367 if (need_lock) {
1368 reiserfs_read_unlock_xattrs(inode->i_sb);
1369 reiserfs_read_unlock_xattr_i(inode);
1da177e4 1370 }
bd4c625c
LT
1371 if (IS_ERR(acl)) {
1372 if (PTR_ERR(acl) == -ENODATA)
1373 goto check_groups;
1374 return PTR_ERR(acl);
1da177e4 1375 }
bd4c625c
LT
1376
1377 if (acl) {
1378 int err = posix_acl_permission(inode, acl, mask);
1379 posix_acl_release(acl);
1380 if (err == -EACCES) {
1381 goto check_capabilities;
1382 }
1383 return err;
1da177e4
LT
1384 } else {
1385 goto check_groups;
bd4c625c 1386 }
1da177e4
LT
1387#endif
1388 } else {
bd4c625c 1389 check_groups:
1da177e4
LT
1390 if (in_group_p(inode->i_gid))
1391 mode >>= 3;
1392 }
1393
1394 /*
1395 * If the DACs are ok we don't need any capability check.
1396 */
bd4c625c 1397 if (((mode & mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == mask))
1da177e4
LT
1398 return 0;
1399
bd4c625c 1400 check_capabilities:
1da177e4
LT
1401 /*
1402 * Read/write DACs are always overridable.
1403 * Executable DACs are overridable if at least one exec bit is set.
1404 */
1405 if (!(mask & MAY_EXEC) ||
1406 (inode->i_mode & S_IXUGO) || S_ISDIR(inode->i_mode))
1407 if (capable(CAP_DAC_OVERRIDE))
1408 return 0;
1409
1410 /*
1411 * Searching includes executable on directories, else just read.
1412 */
1413 if (mask == MAY_READ || (S_ISDIR(inode->i_mode) && !(mask & MAY_WRITE)))
1414 if (capable(CAP_DAC_READ_SEARCH))
1415 return 0;
1416
1417 return -EACCES;
1418}
1419
bd4c625c 1420int reiserfs_permission(struct inode *inode, int mask, struct nameidata *nd)
1da177e4 1421{
bd4c625c 1422 return __reiserfs_permission(inode, mask, nd, 1);
1da177e4
LT
1423}
1424
1425int
bd4c625c 1426reiserfs_permission_locked(struct inode *inode, int mask, struct nameidata *nd)
1da177e4 1427{
bd4c625c 1428 return __reiserfs_permission(inode, mask, nd, 0);
1da177e4 1429}
This page took 0.171645 seconds and 5 git commands to generate.