reiserfs: run scripts/Lindent on reiserfs code
[linux-2.6/verdex.git] / fs / reiserfs / xattr.c
blobe386d3db30513c90ebf9aac18ad9e6a0f3e8adac
1 /*
2 * linux/fs/reiserfs/xattr.c
4 * Copyright (c) 2002 by Jeff Mahoney, <jeffm@suse.com>
6 */
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.
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.
23 * The file contents are the text of the EA. The size is known based on the
24 * stat data describing the file.
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.
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>
49 #define FL_READONLY 128
50 #define FL_DIR_SEM_HELD 256
51 #define PRIVROOT_NAME ".reiserfs_priv"
52 #define XAROOT_NAME "xattrs"
54 static struct reiserfs_xattr_handler *find_xattr_handler_prefix(const char
55 *prefix);
57 static struct dentry *create_xa_root(struct super_block *sb)
59 struct dentry *privroot = dget(REISERFS_SB(sb)->priv_root);
60 struct dentry *xaroot;
62 /* This needs to be created at mount-time */
63 if (!privroot)
64 return ERR_PTR(-EOPNOTSUPP);
66 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);
77 if (err) {
78 dput(xaroot);
79 dput(privroot);
80 return ERR_PTR(err);
82 REISERFS_SB(sb)->xattr_root = dget(xaroot);
85 out:
86 dput(privroot);
87 return xaroot;
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. */
94 static struct dentry *__get_xa_root(struct super_block *s)
96 struct dentry *privroot = dget(REISERFS_SB(s)->priv_root);
97 struct dentry *xaroot = NULL;
99 if (IS_ERR(privroot) || !privroot)
100 return privroot;
102 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;
111 REISERFS_SB(s)->xattr_root = dget(xaroot);
113 out:
114 dput(privroot);
115 return xaroot;
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.
123 static inline struct dentry *get_xa_root(struct super_block *s)
125 struct dentry *dentry = dget(REISERFS_SB(s)->xattr_root);
127 if (!dentry)
128 dentry = __get_xa_root(s);
130 return dentry;
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. */
136 static struct dentry *open_xa_dir(const struct inode *inode, int flags)
138 struct dentry *xaroot, *xadir;
139 char namebuf[17];
141 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;
150 if (!xaroot)
151 return ERR_PTR(-ENODATA);
154 /* ok, we have xaroot open */
156 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;
165 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);
180 if (!xadir->d_inode) {
181 dput(xaroot);
182 dput(xadir);
183 return ERR_PTR(-ENODATA);
187 dput(xaroot);
188 return xadir;
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. */
194 static struct dentry *get_xa_file_dentry(const struct inode *inode,
195 const char *name, int flags)
197 struct dentry *xadir, *xafile;
198 int err = 0;
200 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);
208 xafile = lookup_one_len(name, xadir, strlen(name));
209 if (IS_ERR(xafile)) {
210 dput(xadir);
211 return ERR_PTR(PTR_ERR(xafile));
214 if (xafile->d_inode) { /* file exists */
215 if (flags & XATTR_CREATE) {
216 err = -EEXIST;
217 dput(xafile);
218 goto out;
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);
228 if (err) {
229 dput(xafile);
230 goto out;
234 out:
235 dput(xadir);
236 if (err)
237 xafile = ERR_PTR(err);
238 return xafile;
241 /* Opens a file pointer to the attribute associated with inode */
242 static struct file *open_xa_file(const struct inode *inode, const char *name,
243 int flags)
245 struct dentry *xafile;
246 struct file *fp;
248 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);
256 fp = dentry_open(xafile, NULL, O_RDWR);
257 /* dentry_open dputs the dentry if it fails */
259 return fp;
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
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.
272 * we're called with i_sem held, so there are no worries about the directory
273 * changing underneath us.
275 static int __xattr_readdir(struct file *filp, void *dirent, filldir_t filldir)
277 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;
294 /* form key for search the next directory entry using f_pos field of
295 file structure */
296 next_pos = max_reiserfs_offset(inode);
298 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);
304 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;
314 if (search_res == NAME_NOT_FOUND)
315 de.de_entry_num--;
317 set_de_name_and_namelen(&de);
318 entry_num = de.de_entry_num;
319 deh = &(de.de_deh[entry_num]);
321 bh = de.de_bh;
322 ih = de.de_ih;
324 if (!is_direntry_le_ih(ih)) {
325 reiserfs_warning(inode->i_sb, "not direntry %h", ih);
326 break;
328 copy_item_head(&tmp_ih, ih);
330 /* 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);
335 if (deh_offset(deh) <= DOT_DOT_OFFSET) {
336 break;
339 /* look for the previous entry in the directory */
340 next_pos = deh_offset(deh) - 1;
342 if (!de_visible(deh))
343 /* it is hidden entry */
344 continue;
346 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);
351 if (!d_name[d_reclen - 1])
352 d_reclen = strlen(d_name);
354 if (d_reclen > REISERFS_MAX_NAME(inode->i_sb->s_blocksize)) {
355 /* too big to send back to VFS */
356 continue;
359 /* 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;
368 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;
377 if (item_moved(&tmp_ih, &path_to_entry)) {
378 reiserfs_kfree(local_buf, d_reclen,
379 inode->i_sb);
381 /* sigh, must retry. Do this same offset again */
382 next_pos = d_off;
383 goto research;
387 // 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);
393 /* 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
397 pathrelse(&path_to_entry);
399 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);
405 goto end;
407 if (local_buf != small_buf) {
408 reiserfs_kfree(local_buf, d_reclen, inode->i_sb);
410 } /* while */
412 end:
413 pathrelse(&path_to_entry);
414 return 0;
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
423 static
424 int xattr_readdir(struct file *file, filldir_t filler, void *buf)
426 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);
431 // down(&inode->i_zombie);
432 res = -ENOENT;
433 if (!IS_DEADDIR(inode)) {
434 lock_kernel();
435 res = __xattr_readdir(file, buf, filler);
436 unlock_kernel();
438 // up(&inode->i_zombie);
439 up(&inode->i_sem);
440 out:
441 return res;
444 /* Internal operations on file data */
445 static inline void reiserfs_put_page(struct page *page)
447 kunmap(page);
448 page_cache_release(page);
451 static struct page *reiserfs_get_page(struct inode *dir, unsigned long n)
453 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;
466 if (PageError(page))
467 goto fail;
469 return page;
471 fail:
472 reiserfs_put_page(page);
473 return ERR_PTR(-EIO);
476 static inline __u32 xattr_hash(const char *msg, int len)
478 return csum_partial(msg, len, 0);
481 /* Generic extended attribute operations that can be used by xa plugins */
484 * inode->i_sem: down
487 reiserfs_xattr_set(struct inode *inode, const char *name, const void *buffer,
488 size_t buffer_size, int flags)
490 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;
501 if (IS_RDONLY(inode))
502 return -EROFS;
504 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
505 return -EPERM;
507 if (get_inode_sd_version(inode) == STAT_DATA_V1)
508 return -EOPNOTSUPP;
510 /* Empty xattrs are ok, they're just empty files, no hash */
511 if (buffer && buffer_size)
512 xahash = xattr_hash(buffer, buffer_size);
514 open_file:
515 fp = open_xa_file(inode, name, flags);
516 if (IS_ERR(fp)) {
517 err = PTR_ERR(fp);
518 goto out;
521 xinode = fp->f_dentry->d_inode;
522 REISERFS_I(inode)->i_flags |= i_has_xattr_dir;
524 /* 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;
536 /* 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;
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;
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;
560 lock_page(page);
561 data = page_address(page);
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);
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);
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;
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);
601 out_filp:
602 up(&xinode->i_sem);
603 fput(fp);
605 out:
606 return err;
610 * inode->i_sem: down
613 reiserfs_xattr_get(const struct inode *inode, const char *name, void *buffer,
614 size_t buffer_size)
616 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;
625 if (name == NULL)
626 return -EINVAL;
628 /* 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;
633 fp = open_xa_file(inode, name, FL_READONLY);
634 if (IS_ERR(fp)) {
635 err = PTR_ERR(fp);
636 goto out;
639 xinode = fp->f_dentry->d_inode;
640 isize = xinode->i_size;
641 REISERFS_I(inode)->i_flags |= i_has_xattr_dir;
643 /* Just return the size needed */
644 if (buffer == NULL) {
645 err = isize - sizeof(struct reiserfs_xattr_header);
646 goto out_dput;
649 if (buffer_size < isize - sizeof(struct reiserfs_xattr_header)) {
650 err = -ERANGE;
651 goto out_dput;
654 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;
663 page = reiserfs_get_page(xinode, file_pos >> PAGE_CACHE_SHIFT);
664 if (IS_ERR(page)) {
665 err = PTR_ERR(page);
666 goto out_dput;
669 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;
687 hash = le32_to_cpu(rxh->h_hash);
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;
696 err = isize - sizeof(struct reiserfs_xattr_header);
698 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;
706 out_dput:
707 fput(fp);
709 out:
710 return err;
713 static int
714 __reiserfs_xattr_del(struct dentry *xadir, const char *name, int namelen)
716 struct dentry *dentry;
717 struct inode *dir = xadir->d_inode;
718 int err = 0;
720 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;
729 /* Skip directories.. */
730 if (S_ISDIR(dentry->d_inode->i_mode))
731 goto out_file;
733 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;
746 err = dir->i_op->unlink(dir, dentry);
747 if (!err)
748 d_delete(dentry);
750 out_file:
751 dput(dentry);
753 out:
754 return err;
757 int reiserfs_xattr_del(struct inode *inode, const char *name)
759 struct dentry *dir;
760 int err;
762 if (IS_RDONLY(inode))
763 return -EROFS;
765 dir = open_xa_dir(inode, FL_READONLY);
766 if (IS_ERR(dir)) {
767 err = PTR_ERR(dir);
768 goto out;
771 err = __reiserfs_xattr_del(dir, name, strlen(name));
772 dput(dir);
774 if (!err) {
775 inode->i_ctime = CURRENT_TIME_SEC;
776 mark_inode_dirty(inode);
779 out:
780 return err;
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. */
787 static int
788 reiserfs_delete_xattrs_filler(void *buf, const char *name, int namelen,
789 loff_t offset, ino_t ino, unsigned int d_type)
791 struct dentry *xadir = (struct dentry *)buf;
793 return __reiserfs_xattr_del(xadir, name, namelen);
797 /* This is called w/ inode->i_sem downed */
798 int reiserfs_delete_xattrs(struct inode *inode)
800 struct file *fp;
801 struct dentry *dir, *root;
802 int err = 0;
804 /* 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;
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;
821 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;
828 lock_kernel();
829 err = xattr_readdir(fp, reiserfs_delete_xattrs_filler, dir);
830 if (err) {
831 unlock_kernel();
832 goto out_dir;
835 /* 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");
846 unlock_kernel();
848 out_dir:
849 fput(fp);
851 out:
852 if (!err)
853 REISERFS_I(inode)->i_flags =
854 REISERFS_I(inode)->i_flags & ~i_has_xattr_dir;
855 return err;
858 struct reiserfs_chown_buf {
859 struct inode *inode;
860 struct dentry *xadir;
861 struct iattr *attrs;
864 /* XXX: If there is a better way to do this, I'd love to hear about it */
865 static int
866 reiserfs_chown_xattrs_filler(void *buf, const char *name, int namelen,
867 loff_t offset, ino_t ino, unsigned int d_type)
869 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;
874 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;
882 if (!S_ISDIR(xafile->d_inode->i_mode))
883 err = notify_change(xafile, attrs);
884 dput(xafile);
886 return err;
889 int reiserfs_chown_xattrs(struct inode *inode, struct iattr *attrs)
891 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;
897 /* 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;
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;
915 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;
922 lock_kernel();
924 attrs->ia_valid &= (ATTR_UID | ATTR_GID | ATTR_CTIME);
925 buf.xadir = dir;
926 buf.attrs = attrs;
927 buf.inode = inode;
929 err = xattr_readdir(fp, reiserfs_chown_xattrs_filler, &buf);
930 if (err) {
931 unlock_kernel();
932 goto out_dir;
935 err = notify_change(dir, attrs);
936 unlock_kernel();
938 out_dir:
939 fput(fp);
941 out:
942 attrs->ia_valid = ia_valid;
943 return err;
946 /* Actual operations that are exported to VFS-land */
949 * Inode operation getxattr()
950 * Preliminary locking: we down dentry->d_inode->i_sem
952 ssize_t
953 reiserfs_getxattr(struct dentry * dentry, const char *name, void *buffer,
954 size_t size)
956 struct reiserfs_xattr_handler *xah = find_xattr_handler_prefix(name);
957 int err;
959 if (!xah || !reiserfs_xattrs(dentry->d_sb) ||
960 get_inode_sd_version(dentry->d_inode) == STAT_DATA_V1)
961 return -EOPNOTSUPP;
963 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;
972 * Inode operation setxattr()
974 * dentry->d_inode->i_sem down
977 reiserfs_setxattr(struct dentry *dentry, const char *name, const void *value,
978 size_t size, int flags)
980 struct reiserfs_xattr_handler *xah = find_xattr_handler_prefix(name);
981 int err;
982 int lock;
984 if (!xah || !reiserfs_xattrs(dentry->d_sb) ||
985 get_inode_sd_version(dentry->d_inode) == STAT_DATA_V1)
986 return -EOPNOTSUPP;
988 if (IS_RDONLY(dentry->d_inode))
989 return -EROFS;
991 if (IS_IMMUTABLE(dentry->d_inode) || IS_APPEND(dentry->d_inode))
992 return -EROFS;
994 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;
1010 * Inode operation removexattr()
1012 * dentry->d_inode->i_sem down
1014 int reiserfs_removexattr(struct dentry *dentry, const char *name)
1016 int err;
1017 struct reiserfs_xattr_handler *xah = find_xattr_handler_prefix(name);
1019 if (!xah || !reiserfs_xattrs(dentry->d_sb) ||
1020 get_inode_sd_version(dentry->d_inode) == STAT_DATA_V1)
1021 return -EOPNOTSUPP;
1023 if (IS_RDONLY(dentry->d_inode))
1024 return -EROFS;
1026 if (IS_IMMUTABLE(dentry->d_inode) || IS_APPEND(dentry->d_inode))
1027 return -EPERM;
1029 reiserfs_write_lock_xattr_i(dentry->d_inode);
1030 reiserfs_read_lock_xattrs(dentry->d_sb);
1032 /* Deletion pre-operation */
1033 if (xah->del) {
1034 err = xah->del(dentry->d_inode, name);
1035 if (err)
1036 goto out;
1039 err = reiserfs_xattr_del(dentry->d_inode, name);
1041 dentry->d_inode->i_ctime = CURRENT_TIME_SEC;
1042 mark_inode_dirty(dentry->d_inode);
1044 out:
1045 reiserfs_read_unlock_xattrs(dentry->d_sb);
1046 reiserfs_write_unlock_xattr_i(dentry->d_inode);
1047 return err;
1050 /* 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 */
1055 struct reiserfs_listxattr_buf {
1056 int r_pos;
1057 int r_size;
1058 char *r_buf;
1059 struct inode *r_inode;
1062 static int
1063 reiserfs_listxattr_filler(void *buf, const char *name, int namelen,
1064 loff_t offset, ino_t ino, unsigned int d_type)
1066 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 */
1075 /* 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);
1079 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';
1085 b->r_pos += len + 1;
1089 return 0;
1093 * Inode operation listxattr()
1095 * Preliminary locking: we down dentry->d_inode->i_sem
1097 ssize_t reiserfs_listxattr(struct dentry * dentry, char *buffer, size_t size)
1099 struct file *fp;
1100 struct dentry *dir;
1101 int err = 0;
1102 struct reiserfs_listxattr_buf buf;
1104 if (!dentry->d_inode)
1105 return -EINVAL;
1107 if (!reiserfs_xattrs(dentry->d_sb) ||
1108 get_inode_sd_version(dentry->d_inode) == STAT_DATA_V1)
1109 return -EOPNOTSUPP;
1111 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;
1122 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;
1129 buf.r_buf = buffer;
1130 buf.r_size = buffer ? size : 0;
1131 buf.r_pos = 0;
1132 buf.r_inode = dentry->d_inode;
1134 REISERFS_I(dentry->d_inode)->i_flags |= i_has_xattr_dir;
1136 err = xattr_readdir(fp, reiserfs_listxattr_filler, &buf);
1137 if (err)
1138 goto out_dir;
1140 if (buf.r_pos > buf.r_size && buffer != NULL)
1141 err = -ERANGE;
1142 else
1143 err = buf.r_pos;
1145 out_dir:
1146 fput(fp);
1148 out:
1149 reiserfs_read_unlock_xattr_i(dentry->d_inode);
1150 return err;
1153 /* This is the implementation for the xattr plugin infrastructure */
1154 static struct list_head xattr_handlers = LIST_HEAD_INIT(xattr_handlers);
1155 static DEFINE_RWLOCK(handler_lock);
1157 static struct reiserfs_xattr_handler *find_xattr_handler_prefix(const char
1158 *prefix)
1160 struct reiserfs_xattr_handler *xah = NULL;
1161 struct list_head *p;
1163 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;
1171 read_unlock(&handler_lock);
1172 return xah;
1175 static void __unregister_handlers(void)
1177 struct reiserfs_xattr_handler *xah;
1178 struct list_head *p, *tmp;
1180 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();
1185 list_del_init(p);
1187 INIT_LIST_HEAD(&xattr_handlers);
1190 int __init reiserfs_xattr_register_handlers(void)
1192 int err = 0;
1193 struct reiserfs_xattr_handler *xah;
1194 struct list_head *p;
1196 write_lock(&handler_lock);
1198 /* If we're already initialized, nothing to do */
1199 if (!list_empty(&xattr_handlers)) {
1200 write_unlock(&handler_lock);
1201 return 0;
1204 /* Add the handlers */
1205 list_add_tail(&user_handler.handlers, &xattr_handlers);
1206 list_add_tail(&trusted_handler.handlers, &xattr_handlers);
1207 #ifdef CONFIG_REISERFS_FS_SECURITY
1208 list_add_tail(&security_handler.handlers, &xattr_handlers);
1209 #endif
1210 #ifdef CONFIG_REISERFS_FS_POSIX_ACL
1211 list_add_tail(&posix_acl_access_handler.handlers, &xattr_handlers);
1212 list_add_tail(&posix_acl_default_handler.handlers, &xattr_handlers);
1213 #endif
1215 /* 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;
1227 /* Clean up other handlers, if any failed */
1228 if (err)
1229 __unregister_handlers();
1231 write_unlock(&handler_lock);
1232 return err;
1235 void reiserfs_xattr_unregister_handlers(void)
1237 write_lock(&handler_lock);
1238 __unregister_handlers();
1239 write_unlock(&handler_lock);
1242 /* This will catch lookups from the fs root to .reiserfs_priv */
1243 static int
1244 xattr_lookup_poison(struct dentry *dentry, struct qstr *q1, struct qstr *name)
1246 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;
1257 static struct dentry_operations xattr_lookup_poison_ops = {
1258 .d_compare = xattr_lookup_poison,
1261 /* 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 */
1264 int reiserfs_xattr_init(struct super_block *s, int mount_flags)
1266 int err = 0;
1268 /* 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));
1285 /* 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;
1301 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;
1312 } else
1313 err = PTR_ERR(dentry);
1315 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;
1329 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));
1338 /* 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;
1343 return err;
1346 static int
1347 __reiserfs_permission(struct inode *inode, int mask, struct nameidata *nd,
1348 int need_lock)
1350 umode_t mode = inode->i_mode;
1352 if (mask & MAY_WRITE) {
1354 * Nobody gets write access to a read-only fs.
1356 if (IS_RDONLY(inode) &&
1357 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
1358 return -EROFS;
1361 * Nobody gets write access to an immutable file.
1363 if (IS_IMMUTABLE(inode))
1364 return -EACCES;
1367 /* We don't do permission checks on the internal objects.
1368 * Permissions are determined by the "owning" object. */
1369 if (is_reiserfs_priv_object(inode))
1370 return 0;
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) &&
1376 get_inode_sd_version(inode) != STAT_DATA_V1) {
1377 struct posix_acl *acl;
1379 /* ACL can't contain additional permissions if
1380 the ACL_MASK entry is 0 */
1381 if (!(mode & S_IRWXG))
1382 goto check_groups;
1384 if (need_lock) {
1385 reiserfs_read_lock_xattr_i(inode);
1386 reiserfs_read_lock_xattrs(inode->i_sb);
1388 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);
1393 if (IS_ERR(acl)) {
1394 if (PTR_ERR(acl) == -ENODATA)
1395 goto check_groups;
1396 return PTR_ERR(acl);
1399 if (acl) {
1400 int err = posix_acl_permission(inode, acl, mask);
1401 posix_acl_release(acl);
1402 if (err == -EACCES) {
1403 goto check_capabilities;
1405 return err;
1406 } else {
1407 goto check_groups;
1409 #endif
1410 } else {
1411 check_groups:
1412 if (in_group_p(inode->i_gid))
1413 mode >>= 3;
1417 * If the DACs are ok we don't need any capability check.
1419 if (((mode & mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == mask))
1420 return 0;
1422 check_capabilities:
1424 * Read/write DACs are always overridable.
1425 * Executable DACs are overridable if at least one exec bit is set.
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;
1433 * Searching includes executable on directories, else just read.
1435 if (mask == MAY_READ || (S_ISDIR(inode->i_mode) && !(mask & MAY_WRITE)))
1436 if (capable(CAP_DAC_READ_SEARCH))
1437 return 0;
1439 return -EACCES;
1442 int reiserfs_permission(struct inode *inode, int mask, struct nameidata *nd)
1444 return __reiserfs_permission(inode, mask, nd, 1);
1448 reiserfs_permission_locked(struct inode *inode, int mask, struct nameidata *nd)
1450 return __reiserfs_permission(inode, mask, nd, 0);