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