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