blob: 344b9b96cc56ecd10f0612300e844948b0892d90 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/reiserfs/xattr.c
3 *
4 * Copyright (c) 2002 by Jeff Mahoney, <jeffm@suse.com>
5 *
6 */
7
8/*
9 * In order to implement EA/ACLs in a clean, backwards compatible manner,
10 * they are implemented as files in a "private" directory.
11 * Each EA is in it's own file, with the directory layout like so (/ is assumed
12 * to be relative to fs root). Inside the /.reiserfs_priv/xattrs directory,
13 * directories named using the capital-hex form of the objectid and
14 * generation number are used. Inside each directory are individual files
15 * named with the name of the extended attribute.
16 *
17 * So, for objectid 12648430, we could have:
18 * /.reiserfs_priv/xattrs/C0FFEE.0/system.posix_acl_access
19 * /.reiserfs_priv/xattrs/C0FFEE.0/system.posix_acl_default
20 * /.reiserfs_priv/xattrs/C0FFEE.0/user.Content-Type
21 * .. or similar.
22 *
23 * The file contents are the text of the EA. The size is known based on the
24 * stat data describing the file.
25 *
26 * In the case of system.posix_acl_access and system.posix_acl_default, since
27 * these are special cases for filesystem ACLs, they are interpreted by the
28 * kernel, in addition, they are negatively and positively cached and attached
29 * to the inode so that unnecessary lookups are avoided.
30 */
31
32#include <linux/reiserfs_fs.h>
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080033#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/dcache.h>
35#include <linux/namei.h>
36#include <linux/errno.h>
37#include <linux/fs.h>
38#include <linux/file.h>
39#include <linux/pagemap.h>
40#include <linux/xattr.h>
41#include <linux/reiserfs_xattr.h>
42#include <linux/reiserfs_acl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <asm/uaccess.h>
Al Viro3277c392006-11-14 21:13:53 -080044#include <net/checksum.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/smp_lock.h>
46#include <linux/stat.h>
47#include <asm/semaphore.h>
48
49#define FL_READONLY 128
50#define FL_DIR_SEM_HELD 256
51#define PRIVROOT_NAME ".reiserfs_priv"
52#define XAROOT_NAME "xattrs"
53
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070054static struct reiserfs_xattr_handler *find_xattr_handler_prefix(const char
55 *prefix);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Jeff Mahoney9b7f3752007-04-23 14:41:17 -070057/* Returns the dentry referring to the root of the extended attribute
58 * directory tree. If it has already been retrieved, it is used. If it
59 * hasn't been created and the flags indicate creation is allowed, we
60 * attempt to create it. On error, we return a pointer-encoded error.
61 */
62static struct dentry *get_xa_root(struct super_block *sb, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070063{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070064 struct dentry *privroot = dget(REISERFS_SB(sb)->priv_root);
65 struct dentry *xaroot;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070067 /* This needs to be created at mount-time */
68 if (!privroot)
Jeff Mahoney9b7f3752007-04-23 14:41:17 -070069 return ERR_PTR(-ENODATA);
70
Jeff Mahoney1173a722007-04-30 15:09:50 -070071 mutex_lock_nested(&privroot->d_inode->i_mutex, I_MUTEX_XATTR);
Jeff Mahoney9b7f3752007-04-23 14:41:17 -070072 if (REISERFS_SB(sb)->xattr_root) {
73 xaroot = dget(REISERFS_SB(sb)->xattr_root);
74 goto out;
75 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070077 xaroot = lookup_one_len(XAROOT_NAME, privroot, strlen(XAROOT_NAME));
78 if (IS_ERR(xaroot)) {
79 goto out;
80 } else if (!xaroot->d_inode) {
Jeff Mahoney9b7f3752007-04-23 14:41:17 -070081 int err = -ENODATA;
82 if (flags == 0 || flags & XATTR_CREATE)
83 err = privroot->d_inode->i_op->mkdir(privroot->d_inode,
84 xaroot, 0700);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070085 if (err) {
86 dput(xaroot);
Jeff Mahoney9b7f3752007-04-23 14:41:17 -070087 xaroot = ERR_PTR(err);
88 goto out;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070089 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070090 }
Jeff Mahoney9b7f3752007-04-23 14:41:17 -070091 REISERFS_SB(sb)->xattr_root = dget(xaroot);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070093 out:
Jeff Mahoney9b7f3752007-04-23 14:41:17 -070094 mutex_unlock(&privroot->d_inode->i_mutex);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070095 dput(privroot);
96 return xaroot;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097}
98
Linus Torvalds1da177e2005-04-16 15:20:36 -070099/* Opens the directory corresponding to the inode's extended attribute store.
100 * If flags allow, the tree to the directory may be created. If creation is
101 * prohibited, -ENODATA is returned. */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700102static struct dentry *open_xa_dir(const struct inode *inode, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700104 struct dentry *xaroot, *xadir;
105 char namebuf[17];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
Jeff Mahoney9b7f3752007-04-23 14:41:17 -0700107 xaroot = get_xa_root(inode->i_sb, flags);
108 if (IS_ERR(xaroot))
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700109 return xaroot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700111 /* ok, we have xaroot open */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700112 snprintf(namebuf, sizeof(namebuf), "%X.%X",
113 le32_to_cpu(INODE_PKEY(inode)->k_objectid),
114 inode->i_generation);
115 xadir = lookup_one_len(namebuf, xaroot, strlen(namebuf));
116 if (IS_ERR(xadir)) {
117 dput(xaroot);
118 return xadir;
119 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700121 if (!xadir->d_inode) {
122 int err;
123 if (flags == 0 || flags & XATTR_CREATE) {
124 /* Although there is nothing else trying to create this directory,
125 * another directory with the same hash may be created, so we need
126 * to protect against that */
127 err =
128 xaroot->d_inode->i_op->mkdir(xaroot->d_inode, xadir,
129 0700);
130 if (err) {
131 dput(xaroot);
132 dput(xadir);
133 return ERR_PTR(err);
134 }
135 }
136 if (!xadir->d_inode) {
137 dput(xaroot);
138 dput(xadir);
139 return ERR_PTR(-ENODATA);
140 }
141 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700143 dput(xaroot);
144 return xadir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145}
146
147/* Returns a dentry corresponding to a specific extended attribute file
148 * for the inode. If flags allow, the file is created. Otherwise, a
149 * valid or negative dentry, or an error is returned. */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700150static struct dentry *get_xa_file_dentry(const struct inode *inode,
151 const char *name, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700153 struct dentry *xadir, *xafile;
154 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700156 xadir = open_xa_dir(inode, flags);
157 if (IS_ERR(xadir)) {
David Howellse231c2e2008-02-07 00:15:26 -0800158 return ERR_CAST(xadir);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700159 } else if (xadir && !xadir->d_inode) {
160 dput(xadir);
161 return ERR_PTR(-ENODATA);
162 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700164 xafile = lookup_one_len(name, xadir, strlen(name));
165 if (IS_ERR(xafile)) {
166 dput(xadir);
David Howellse231c2e2008-02-07 00:15:26 -0800167 return ERR_CAST(xafile);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700168 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700170 if (xafile->d_inode) { /* file exists */
171 if (flags & XATTR_CREATE) {
172 err = -EEXIST;
173 dput(xafile);
174 goto out;
175 }
176 } else if (flags & XATTR_REPLACE || flags & FL_READONLY) {
177 goto out;
178 } else {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800179 /* inode->i_mutex is down, so nothing else can try to create
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700180 * the same xattr */
181 err = xadir->d_inode->i_op->create(xadir->d_inode, xafile,
182 0700 | S_IFREG, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700184 if (err) {
185 dput(xafile);
186 goto out;
187 }
188 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700190 out:
191 dput(xadir);
192 if (err)
193 xafile = ERR_PTR(err);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700194 else if (!xafile->d_inode) {
195 dput(xafile);
Jeff Mahoney3227e142008-02-15 14:37:22 -0800196 xafile = ERR_PTR(-ENODATA);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700197 }
Jeff Mahoney3227e142008-02-15 14:37:22 -0800198 return xafile;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199}
200
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201/*
202 * this is very similar to fs/reiserfs/dir.c:reiserfs_readdir, but
203 * we need to drop the path before calling the filldir struct. That
204 * would be a big performance hit to the non-xattr case, so I've copied
205 * the whole thing for now. --clm
206 *
207 * the big difference is that I go backwards through the directory,
208 * and don't mess with f->f_pos, but the idea is the same. Do some
209 * action on each and every entry in the directory.
210 *
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800211 * we're called with i_mutex held, so there are no worries about the directory
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 * changing underneath us.
213 */
Jeff Mahoney3227e142008-02-15 14:37:22 -0800214static int __xattr_readdir(struct inode *inode, void *dirent, filldir_t filldir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700216 struct cpu_key pos_key; /* key of current position in the directory (key of directory entry) */
217 INITIALIZE_PATH(path_to_entry);
218 struct buffer_head *bh;
219 int entry_num;
220 struct item_head *ih, tmp_ih;
221 int search_res;
222 char *local_buf;
223 loff_t next_pos;
224 char small_buf[32]; /* avoid kmalloc if we can */
225 struct reiserfs_de_head *deh;
226 int d_reclen;
227 char *d_name;
228 off_t d_off;
229 ino_t d_ino;
230 struct reiserfs_dir_entry de;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700232 /* form key for search the next directory entry using f_pos field of
233 file structure */
234 next_pos = max_reiserfs_offset(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700236 while (1) {
237 research:
238 if (next_pos <= DOT_DOT_OFFSET)
239 break;
240 make_cpu_key(&pos_key, inode, next_pos, TYPE_DIRENTRY, 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700242 search_res =
243 search_by_entry_key(inode->i_sb, &pos_key, &path_to_entry,
244 &de);
245 if (search_res == IO_ERROR) {
246 // FIXME: we could just skip part of directory which could
247 // not be read
248 pathrelse(&path_to_entry);
249 return -EIO;
250 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700252 if (search_res == NAME_NOT_FOUND)
253 de.de_entry_num--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700255 set_de_name_and_namelen(&de);
256 entry_num = de.de_entry_num;
257 deh = &(de.de_deh[entry_num]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700259 bh = de.de_bh;
260 ih = de.de_ih;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700262 if (!is_direntry_le_ih(ih)) {
263 reiserfs_warning(inode->i_sb, "not direntry %h", ih);
264 break;
265 }
266 copy_item_head(&tmp_ih, ih);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700268 /* we must have found item, that is item of this directory, */
269 RFALSE(COMP_SHORT_KEYS(&(ih->ih_key), &pos_key),
270 "vs-9000: found item %h does not match to dir we readdir %K",
271 ih, &pos_key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700273 if (deh_offset(deh) <= DOT_DOT_OFFSET) {
274 break;
275 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700277 /* look for the previous entry in the directory */
278 next_pos = deh_offset(deh) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700280 if (!de_visible(deh))
281 /* it is hidden entry */
282 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700284 d_reclen = entry_length(bh, ih, entry_num);
285 d_name = B_I_DEH_ENTRY_FILE_NAME(bh, ih, deh);
286 d_off = deh_offset(deh);
287 d_ino = deh_objectid(deh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700289 if (!d_name[d_reclen - 1])
290 d_reclen = strlen(d_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700292 if (d_reclen > REISERFS_MAX_NAME(inode->i_sb->s_blocksize)) {
293 /* too big to send back to VFS */
294 continue;
295 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700297 /* Ignore the .reiserfs_priv entry */
298 if (reiserfs_xattrs(inode->i_sb) &&
299 !old_format_only(inode->i_sb) &&
300 deh_objectid(deh) ==
301 le32_to_cpu(INODE_PKEY
302 (REISERFS_SB(inode->i_sb)->priv_root->d_inode)->
303 k_objectid))
304 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700306 if (d_reclen <= 32) {
307 local_buf = small_buf;
308 } else {
Pekka Enbergd739b422006-02-01 03:06:43 -0800309 local_buf = kmalloc(d_reclen, GFP_NOFS);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700310 if (!local_buf) {
311 pathrelse(&path_to_entry);
312 return -ENOMEM;
313 }
314 if (item_moved(&tmp_ih, &path_to_entry)) {
Pekka Enbergd739b422006-02-01 03:06:43 -0800315 kfree(local_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700317 /* sigh, must retry. Do this same offset again */
318 next_pos = d_off;
319 goto research;
320 }
321 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700323 // Note, that we copy name to user space via temporary
324 // buffer (local_buf) because filldir will block if
325 // user space buffer is swapped out. At that time
326 // entry can move to somewhere else
327 memcpy(local_buf, d_name, d_reclen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700329 /* the filldir function might need to start transactions,
330 * or do who knows what. Release the path now that we've
331 * copied all the important stuff out of the deh
332 */
333 pathrelse(&path_to_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700335 if (filldir(dirent, local_buf, d_reclen, d_off, d_ino,
336 DT_UNKNOWN) < 0) {
337 if (local_buf != small_buf) {
Pekka Enbergd739b422006-02-01 03:06:43 -0800338 kfree(local_buf);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700339 }
340 goto end;
341 }
342 if (local_buf != small_buf) {
Pekka Enbergd739b422006-02-01 03:06:43 -0800343 kfree(local_buf);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700344 }
345 } /* while */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700347 end:
348 pathrelse(&path_to_entry);
349 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350}
351
352/*
353 * this could be done with dedicated readdir ops for the xattr files,
354 * but I want to get something working asap
355 * this is stolen from vfs_readdir
356 *
357 */
358static
Jeff Mahoney3227e142008-02-15 14:37:22 -0800359int xattr_readdir(struct inode *inode, filldir_t filler, void *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360{
Jeff Mahoney3227e142008-02-15 14:37:22 -0800361 int res = -ENOENT;
Ingo Molnar4df46242006-08-27 01:23:56 -0700362 mutex_lock_nested(&inode->i_mutex, I_MUTEX_XATTR);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700363 if (!IS_DEADDIR(inode)) {
364 lock_kernel();
Jeff Mahoney3227e142008-02-15 14:37:22 -0800365 res = __xattr_readdir(inode, buf, filler);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700366 unlock_kernel();
367 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800368 mutex_unlock(&inode->i_mutex);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700369 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370}
371
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372/* Internal operations on file data */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700373static inline void reiserfs_put_page(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700375 kunmap(page);
376 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377}
378
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700379static struct page *reiserfs_get_page(struct inode *dir, unsigned long n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700381 struct address_space *mapping = dir->i_mapping;
382 struct page *page;
383 /* We can deadlock if we try to free dentries,
384 and an unlink/rmdir has just occured - GFP_NOFS avoids this */
Al Viroc4cdd032005-10-21 03:22:39 -0400385 mapping_set_gfp_mask(mapping, GFP_NOFS);
Pekka Enberg090d2b12006-06-23 02:05:08 -0700386 page = read_mapping_page(mapping, n, NULL);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700387 if (!IS_ERR(page)) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700388 kmap(page);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700389 if (PageError(page))
390 goto fail;
391 }
392 return page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700394 fail:
395 reiserfs_put_page(page);
396 return ERR_PTR(-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397}
398
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700399static inline __u32 xattr_hash(const char *msg, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700401 return csum_partial(msg, len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402}
403
Vladimir Savelievba9d8ce2007-10-16 01:25:14 -0700404int reiserfs_commit_write(struct file *f, struct page *page,
405 unsigned from, unsigned to);
406int reiserfs_prepare_write(struct file *f, struct page *page,
407 unsigned from, unsigned to);
408
409
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410/* Generic extended attribute operations that can be used by xa plugins */
411
412/*
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800413 * inode->i_mutex: down
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 */
415int
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700416reiserfs_xattr_set(struct inode *inode, const char *name, const void *buffer,
417 size_t buffer_size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700419 int err = 0;
Jeff Mahoney3227e142008-02-15 14:37:22 -0800420 struct dentry *dentry;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700421 struct page *page;
422 char *data;
423 struct address_space *mapping;
424 size_t file_pos = 0;
425 size_t buffer_pos = 0;
426 struct inode *xinode;
427 struct iattr newattrs;
428 __u32 xahash = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700430 if (get_inode_sd_version(inode) == STAT_DATA_V1)
431 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700433 /* Empty xattrs are ok, they're just empty files, no hash */
434 if (buffer && buffer_size)
435 xahash = xattr_hash(buffer, buffer_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700437 open_file:
Jeff Mahoney3227e142008-02-15 14:37:22 -0800438 dentry = get_xa_file_dentry(inode, name, flags);
439 if (IS_ERR(dentry)) {
440 err = PTR_ERR(dentry);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700441 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
Jeff Mahoney3227e142008-02-15 14:37:22 -0800444 xinode = dentry->d_inode;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700445 REISERFS_I(inode)->i_flags |= i_has_xattr_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700447 /* we need to copy it off.. */
448 if (xinode->i_nlink > 1) {
Jeff Mahoney3227e142008-02-15 14:37:22 -0800449 dput(dentry);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700450 err = reiserfs_xattr_del(inode, name);
451 if (err < 0)
452 goto out;
453 /* We just killed the old one, we're not replacing anymore */
454 if (flags & XATTR_REPLACE)
455 flags &= ~XATTR_REPLACE;
456 goto open_file;
457 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700459 /* Resize it so we're ok to write there */
460 newattrs.ia_size = buffer_size;
461 newattrs.ia_valid = ATTR_SIZE | ATTR_CTIME;
Jeff Mahoney75983922007-10-18 23:39:23 -0700462 mutex_lock_nested(&xinode->i_mutex, I_MUTEX_XATTR);
Jeff Mahoney3227e142008-02-15 14:37:22 -0800463 err = notify_change(dentry, &newattrs);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700464 if (err)
465 goto out_filp;
466
467 mapping = xinode->i_mapping;
468 while (buffer_pos < buffer_size || buffer_pos == 0) {
469 size_t chunk;
470 size_t skip = 0;
471 size_t page_offset = (file_pos & (PAGE_CACHE_SIZE - 1));
472 if (buffer_size - buffer_pos > PAGE_CACHE_SIZE)
473 chunk = PAGE_CACHE_SIZE;
474 else
475 chunk = buffer_size - buffer_pos;
476
477 page = reiserfs_get_page(xinode, file_pos >> PAGE_CACHE_SHIFT);
478 if (IS_ERR(page)) {
479 err = PTR_ERR(page);
480 goto out_filp;
481 }
482
483 lock_page(page);
484 data = page_address(page);
485
486 if (file_pos == 0) {
487 struct reiserfs_xattr_header *rxh;
488 skip = file_pos = sizeof(struct reiserfs_xattr_header);
489 if (chunk + skip > PAGE_CACHE_SIZE)
490 chunk = PAGE_CACHE_SIZE - skip;
491 rxh = (struct reiserfs_xattr_header *)data;
492 rxh->h_magic = cpu_to_le32(REISERFS_XATTR_MAGIC);
493 rxh->h_hash = cpu_to_le32(xahash);
494 }
495
Jeff Mahoney3227e142008-02-15 14:37:22 -0800496 err = reiserfs_prepare_write(NULL, page, page_offset,
Vladimir Savelievba9d8ce2007-10-16 01:25:14 -0700497 page_offset + chunk + skip);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700498 if (!err) {
499 if (buffer)
500 memcpy(data + skip, buffer + buffer_pos, chunk);
Jeff Mahoney3227e142008-02-15 14:37:22 -0800501 err = reiserfs_commit_write(NULL, page, page_offset,
502 page_offset + chunk +
503 skip);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700504 }
505 unlock_page(page);
506 reiserfs_put_page(page);
507 buffer_pos += chunk;
508 file_pos += chunk;
509 skip = 0;
510 if (err || buffer_size == 0 || !buffer)
511 break;
512 }
513
514 /* We can't mark the inode dirty if it's not hashed. This is the case
515 * when we're inheriting the default ACL. If we dirty it, the inode
516 * gets marked dirty, but won't (ever) make it onto the dirty list until
517 * it's synced explicitly to clear I_DIRTY. This is bad. */
518 if (!hlist_unhashed(&inode->i_hash)) {
519 inode->i_ctime = CURRENT_TIME_SEC;
520 mark_inode_dirty(inode);
521 }
522
523 out_filp:
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800524 mutex_unlock(&xinode->i_mutex);
Jeff Mahoney3227e142008-02-15 14:37:22 -0800525 dput(dentry);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700526
527 out:
528 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529}
530
531/*
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800532 * inode->i_mutex: down
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 */
534int
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700535reiserfs_xattr_get(const struct inode *inode, const char *name, void *buffer,
536 size_t buffer_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700538 ssize_t err = 0;
Jeff Mahoney3227e142008-02-15 14:37:22 -0800539 struct dentry *dentry;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700540 size_t isize;
541 size_t file_pos = 0;
542 size_t buffer_pos = 0;
543 struct page *page;
544 struct inode *xinode;
545 __u32 hash = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700547 if (name == NULL)
548 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700550 /* We can't have xattrs attached to v1 items since they don't have
551 * generation numbers */
552 if (get_inode_sd_version(inode) == STAT_DATA_V1)
553 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
Jeff Mahoney3227e142008-02-15 14:37:22 -0800555 dentry = get_xa_file_dentry(inode, name, FL_READONLY);
556 if (IS_ERR(dentry)) {
557 err = PTR_ERR(dentry);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700558 goto out;
559 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
Jeff Mahoney3227e142008-02-15 14:37:22 -0800561 xinode = dentry->d_inode;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700562 isize = xinode->i_size;
563 REISERFS_I(inode)->i_flags |= i_has_xattr_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700565 /* Just return the size needed */
566 if (buffer == NULL) {
567 err = isize - sizeof(struct reiserfs_xattr_header);
568 goto out_dput;
569 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700571 if (buffer_size < isize - sizeof(struct reiserfs_xattr_header)) {
572 err = -ERANGE;
573 goto out_dput;
574 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700576 while (file_pos < isize) {
577 size_t chunk;
578 char *data;
579 size_t skip = 0;
580 if (isize - file_pos > PAGE_CACHE_SIZE)
581 chunk = PAGE_CACHE_SIZE;
582 else
583 chunk = isize - file_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700585 page = reiserfs_get_page(xinode, file_pos >> PAGE_CACHE_SHIFT);
586 if (IS_ERR(page)) {
587 err = PTR_ERR(page);
588 goto out_dput;
589 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700591 lock_page(page);
592 data = page_address(page);
593 if (file_pos == 0) {
594 struct reiserfs_xattr_header *rxh =
595 (struct reiserfs_xattr_header *)data;
596 skip = file_pos = sizeof(struct reiserfs_xattr_header);
597 chunk -= skip;
598 /* Magic doesn't match up.. */
599 if (rxh->h_magic != cpu_to_le32(REISERFS_XATTR_MAGIC)) {
600 unlock_page(page);
601 reiserfs_put_page(page);
602 reiserfs_warning(inode->i_sb,
603 "Invalid magic for xattr (%s) "
604 "associated with %k", name,
605 INODE_PKEY(inode));
606 err = -EIO;
607 goto out_dput;
608 }
609 hash = le32_to_cpu(rxh->h_hash);
610 }
611 memcpy(buffer + buffer_pos, data + skip, chunk);
612 unlock_page(page);
613 reiserfs_put_page(page);
614 file_pos += chunk;
615 buffer_pos += chunk;
616 skip = 0;
617 }
618 err = isize - sizeof(struct reiserfs_xattr_header);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700620 if (xattr_hash(buffer, isize - sizeof(struct reiserfs_xattr_header)) !=
621 hash) {
622 reiserfs_warning(inode->i_sb,
623 "Invalid hash for xattr (%s) associated "
624 "with %k", name, INODE_PKEY(inode));
625 err = -EIO;
626 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700628 out_dput:
Jeff Mahoney3227e142008-02-15 14:37:22 -0800629 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700631 out:
632 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633}
634
635static int
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700636__reiserfs_xattr_del(struct dentry *xadir, const char *name, int namelen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700638 struct dentry *dentry;
639 struct inode *dir = xadir->d_inode;
640 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700642 dentry = lookup_one_len(name, xadir, namelen);
643 if (IS_ERR(dentry)) {
644 err = PTR_ERR(dentry);
645 goto out;
646 } else if (!dentry->d_inode) {
647 err = -ENODATA;
648 goto out_file;
649 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700651 /* Skip directories.. */
652 if (S_ISDIR(dentry->d_inode->i_mode))
653 goto out_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700655 if (!is_reiserfs_priv_object(dentry->d_inode)) {
656 reiserfs_warning(dir->i_sb, "OID %08x [%.*s/%.*s] doesn't have "
657 "priv flag set [parent is %sset].",
658 le32_to_cpu(INODE_PKEY(dentry->d_inode)->
659 k_objectid), xadir->d_name.len,
660 xadir->d_name.name, namelen, name,
661 is_reiserfs_priv_object(xadir->
662 d_inode) ? "" :
663 "not ");
664 dput(dentry);
665 return -EIO;
666 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700668 err = dir->i_op->unlink(dir, dentry);
669 if (!err)
670 d_delete(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700672 out_file:
673 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700675 out:
676 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677}
678
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700679int reiserfs_xattr_del(struct inode *inode, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700681 struct dentry *dir;
682 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700684 dir = open_xa_dir(inode, FL_READONLY);
685 if (IS_ERR(dir)) {
686 err = PTR_ERR(dir);
687 goto out;
688 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700690 err = __reiserfs_xattr_del(dir, name, strlen(name));
691 dput(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700693 if (!err) {
694 inode->i_ctime = CURRENT_TIME_SEC;
695 mark_inode_dirty(inode);
696 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700698 out:
699 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700}
701
702/* The following are side effects of other operations that aren't explicitly
703 * modifying extended attributes. This includes operations such as permissions
704 * or ownership changes, object deletions, etc. */
705
706static int
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700707reiserfs_delete_xattrs_filler(void *buf, const char *name, int namelen,
David Howellsafefdbb2006-10-03 01:13:46 -0700708 loff_t offset, u64 ino, unsigned int d_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700710 struct dentry *xadir = (struct dentry *)buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700712 return __reiserfs_xattr_del(xadir, name, namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
714}
715
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800716/* This is called w/ inode->i_mutex downed */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700717int reiserfs_delete_xattrs(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700719 struct dentry *dir, *root;
720 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700722 /* Skip out, an xattr has no xattrs associated with it */
723 if (is_reiserfs_priv_object(inode) ||
724 get_inode_sd_version(inode) == STAT_DATA_V1 ||
725 !reiserfs_xattrs(inode->i_sb)) {
726 return 0;
727 }
728 reiserfs_read_lock_xattrs(inode->i_sb);
729 dir = open_xa_dir(inode, FL_READONLY);
730 reiserfs_read_unlock_xattrs(inode->i_sb);
731 if (IS_ERR(dir)) {
732 err = PTR_ERR(dir);
733 goto out;
734 } else if (!dir->d_inode) {
735 dput(dir);
736 return 0;
737 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700739 lock_kernel();
Jeff Mahoney3227e142008-02-15 14:37:22 -0800740 err = xattr_readdir(dir->d_inode, reiserfs_delete_xattrs_filler, dir);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700741 if (err) {
742 unlock_kernel();
743 goto out_dir;
744 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700746 /* Leftovers besides . and .. -- that's not good. */
747 if (dir->d_inode->i_nlink <= 2) {
Jeff Mahoney9b7f3752007-04-23 14:41:17 -0700748 root = get_xa_root(inode->i_sb, XATTR_REPLACE);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700749 reiserfs_write_lock_xattrs(inode->i_sb);
750 err = vfs_rmdir(root->d_inode, dir);
751 reiserfs_write_unlock_xattrs(inode->i_sb);
752 dput(root);
753 } else {
754 reiserfs_warning(inode->i_sb,
755 "Couldn't remove all entries in directory");
756 }
757 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700759 out_dir:
Jeff Mahoney3227e142008-02-15 14:37:22 -0800760 dput(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700762 out:
763 if (!err)
764 REISERFS_I(inode)->i_flags =
765 REISERFS_I(inode)->i_flags & ~i_has_xattr_dir;
766 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767}
768
769struct reiserfs_chown_buf {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700770 struct inode *inode;
771 struct dentry *xadir;
772 struct iattr *attrs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773};
774
775/* XXX: If there is a better way to do this, I'd love to hear about it */
776static int
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700777reiserfs_chown_xattrs_filler(void *buf, const char *name, int namelen,
David Howellsafefdbb2006-10-03 01:13:46 -0700778 loff_t offset, u64 ino, unsigned int d_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700780 struct reiserfs_chown_buf *chown_buf = (struct reiserfs_chown_buf *)buf;
781 struct dentry *xafile, *xadir = chown_buf->xadir;
782 struct iattr *attrs = chown_buf->attrs;
783 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700785 xafile = lookup_one_len(name, xadir, namelen);
786 if (IS_ERR(xafile))
787 return PTR_ERR(xafile);
788 else if (!xafile->d_inode) {
789 dput(xafile);
790 return -ENODATA;
791 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700793 if (!S_ISDIR(xafile->d_inode->i_mode))
794 err = notify_change(xafile, attrs);
795 dput(xafile);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700797 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798}
799
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700800int reiserfs_chown_xattrs(struct inode *inode, struct iattr *attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700802 struct dentry *dir;
803 int err = 0;
804 struct reiserfs_chown_buf buf;
805 unsigned int ia_valid = attrs->ia_valid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700807 /* Skip out, an xattr has no xattrs associated with it */
808 if (is_reiserfs_priv_object(inode) ||
809 get_inode_sd_version(inode) == STAT_DATA_V1 ||
810 !reiserfs_xattrs(inode->i_sb)) {
811 return 0;
812 }
813 reiserfs_read_lock_xattrs(inode->i_sb);
814 dir = open_xa_dir(inode, FL_READONLY);
815 reiserfs_read_unlock_xattrs(inode->i_sb);
816 if (IS_ERR(dir)) {
817 if (PTR_ERR(dir) != -ENODATA)
818 err = PTR_ERR(dir);
819 goto out;
820 } else if (!dir->d_inode) {
821 dput(dir);
822 goto out;
823 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700825 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700827 attrs->ia_valid &= (ATTR_UID | ATTR_GID | ATTR_CTIME);
828 buf.xadir = dir;
829 buf.attrs = attrs;
830 buf.inode = inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831
Jeff Mahoney3227e142008-02-15 14:37:22 -0800832 err = xattr_readdir(dir->d_inode, reiserfs_chown_xattrs_filler, &buf);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700833 if (err) {
834 unlock_kernel();
835 goto out_dir;
836 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700838 err = notify_change(dir, attrs);
839 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700841 out_dir:
Jeff Mahoney3227e142008-02-15 14:37:22 -0800842 dput(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700844 out:
845 attrs->ia_valid = ia_valid;
846 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847}
848
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849/* Actual operations that are exported to VFS-land */
850
851/*
852 * Inode operation getxattr()
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800853 * Preliminary locking: we down dentry->d_inode->i_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 */
855ssize_t
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700856reiserfs_getxattr(struct dentry * dentry, const char *name, void *buffer,
857 size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700859 struct reiserfs_xattr_handler *xah = find_xattr_handler_prefix(name);
860 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700862 if (!xah || !reiserfs_xattrs(dentry->d_sb) ||
863 get_inode_sd_version(dentry->d_inode) == STAT_DATA_V1)
864 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700866 reiserfs_read_lock_xattr_i(dentry->d_inode);
867 reiserfs_read_lock_xattrs(dentry->d_sb);
868 err = xah->get(dentry->d_inode, name, buffer, size);
869 reiserfs_read_unlock_xattrs(dentry->d_sb);
870 reiserfs_read_unlock_xattr_i(dentry->d_inode);
871 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872}
873
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874/*
875 * Inode operation setxattr()
876 *
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800877 * dentry->d_inode->i_mutex down
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 */
879int
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700880reiserfs_setxattr(struct dentry *dentry, const char *name, const void *value,
881 size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700883 struct reiserfs_xattr_handler *xah = find_xattr_handler_prefix(name);
884 int err;
885 int lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700887 if (!xah || !reiserfs_xattrs(dentry->d_sb) ||
888 get_inode_sd_version(dentry->d_inode) == STAT_DATA_V1)
889 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700891 reiserfs_write_lock_xattr_i(dentry->d_inode);
892 lock = !has_xattr_dir(dentry->d_inode);
893 if (lock)
894 reiserfs_write_lock_xattrs(dentry->d_sb);
895 else
896 reiserfs_read_lock_xattrs(dentry->d_sb);
897 err = xah->set(dentry->d_inode, name, value, size, flags);
898 if (lock)
899 reiserfs_write_unlock_xattrs(dentry->d_sb);
900 else
901 reiserfs_read_unlock_xattrs(dentry->d_sb);
902 reiserfs_write_unlock_xattr_i(dentry->d_inode);
903 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904}
905
906/*
907 * Inode operation removexattr()
908 *
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800909 * dentry->d_inode->i_mutex down
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700911int reiserfs_removexattr(struct dentry *dentry, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700913 int err;
914 struct reiserfs_xattr_handler *xah = find_xattr_handler_prefix(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700916 if (!xah || !reiserfs_xattrs(dentry->d_sb) ||
917 get_inode_sd_version(dentry->d_inode) == STAT_DATA_V1)
918 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700920 reiserfs_write_lock_xattr_i(dentry->d_inode);
921 reiserfs_read_lock_xattrs(dentry->d_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700923 /* Deletion pre-operation */
924 if (xah->del) {
925 err = xah->del(dentry->d_inode, name);
926 if (err)
927 goto out;
928 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700930 err = reiserfs_xattr_del(dentry->d_inode, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700932 dentry->d_inode->i_ctime = CURRENT_TIME_SEC;
933 mark_inode_dirty(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700935 out:
936 reiserfs_read_unlock_xattrs(dentry->d_sb);
937 reiserfs_write_unlock_xattr_i(dentry->d_inode);
938 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939}
940
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941/* This is what filldir will use:
942 * r_pos will always contain the amount of space required for the entire
943 * list. If r_pos becomes larger than r_size, we need more space and we
944 * return an error indicating this. If r_pos is less than r_size, then we've
945 * filled the buffer successfully and we return success */
946struct reiserfs_listxattr_buf {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700947 int r_pos;
948 int r_size;
949 char *r_buf;
950 struct inode *r_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951};
952
953static int
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700954reiserfs_listxattr_filler(void *buf, const char *name, int namelen,
David Howellsafefdbb2006-10-03 01:13:46 -0700955 loff_t offset, u64 ino, unsigned int d_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700957 struct reiserfs_listxattr_buf *b = (struct reiserfs_listxattr_buf *)buf;
958 int len = 0;
959 if (name[0] != '.'
960 || (namelen != 1 && (name[1] != '.' || namelen != 2))) {
961 struct reiserfs_xattr_handler *xah =
962 find_xattr_handler_prefix(name);
963 if (!xah)
964 return 0; /* Unsupported xattr name, skip it */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700966 /* We call ->list() twice because the operation isn't required to just
967 * return the name back - we want to make sure we have enough space */
968 len += xah->list(b->r_inode, name, namelen, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700970 if (len) {
971 if (b->r_pos + len + 1 <= b->r_size) {
972 char *p = b->r_buf + b->r_pos;
973 p += xah->list(b->r_inode, name, namelen, p);
974 *p++ = '\0';
975 }
976 b->r_pos += len + 1;
977 }
978 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700980 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981}
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700982
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983/*
984 * Inode operation listxattr()
985 *
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800986 * Preliminary locking: we down dentry->d_inode->i_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700988ssize_t reiserfs_listxattr(struct dentry * dentry, char *buffer, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700990 struct dentry *dir;
991 int err = 0;
992 struct reiserfs_listxattr_buf buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700994 if (!dentry->d_inode)
995 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700997 if (!reiserfs_xattrs(dentry->d_sb) ||
998 get_inode_sd_version(dentry->d_inode) == STAT_DATA_V1)
999 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001001 reiserfs_read_lock_xattr_i(dentry->d_inode);
1002 reiserfs_read_lock_xattrs(dentry->d_sb);
1003 dir = open_xa_dir(dentry->d_inode, FL_READONLY);
1004 reiserfs_read_unlock_xattrs(dentry->d_sb);
1005 if (IS_ERR(dir)) {
1006 err = PTR_ERR(dir);
1007 if (err == -ENODATA)
1008 err = 0; /* Not an error if there aren't any xattrs */
1009 goto out;
1010 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001012 buf.r_buf = buffer;
1013 buf.r_size = buffer ? size : 0;
1014 buf.r_pos = 0;
1015 buf.r_inode = dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001017 REISERFS_I(dentry->d_inode)->i_flags |= i_has_xattr_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
Jeff Mahoney3227e142008-02-15 14:37:22 -08001019 err = xattr_readdir(dir->d_inode, reiserfs_listxattr_filler, &buf);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001020 if (err)
1021 goto out_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001023 if (buf.r_pos > buf.r_size && buffer != NULL)
1024 err = -ERANGE;
1025 else
1026 err = buf.r_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001028 out_dir:
Jeff Mahoney3227e142008-02-15 14:37:22 -08001029 dput(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001031 out:
1032 reiserfs_read_unlock_xattr_i(dentry->d_inode);
1033 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034}
1035
1036/* This is the implementation for the xattr plugin infrastructure */
Denis Chengbcf11cb2008-02-06 01:37:40 -08001037static LIST_HEAD(xattr_handlers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038static DEFINE_RWLOCK(handler_lock);
1039
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001040static struct reiserfs_xattr_handler *find_xattr_handler_prefix(const char
1041 *prefix)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001043 struct reiserfs_xattr_handler *xah = NULL;
1044 struct list_head *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001046 read_lock(&handler_lock);
1047 list_for_each(p, &xattr_handlers) {
1048 xah = list_entry(p, struct reiserfs_xattr_handler, handlers);
1049 if (strncmp(xah->prefix, prefix, strlen(xah->prefix)) == 0)
1050 break;
1051 xah = NULL;
1052 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001054 read_unlock(&handler_lock);
1055 return xah;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056}
1057
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001058static void __unregister_handlers(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001060 struct reiserfs_xattr_handler *xah;
1061 struct list_head *p, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001063 list_for_each_safe(p, tmp, &xattr_handlers) {
1064 xah = list_entry(p, struct reiserfs_xattr_handler, handlers);
1065 if (xah->exit)
1066 xah->exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001068 list_del_init(p);
1069 }
1070 INIT_LIST_HEAD(&xattr_handlers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071}
1072
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001073int __init reiserfs_xattr_register_handlers(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001075 int err = 0;
1076 struct reiserfs_xattr_handler *xah;
1077 struct list_head *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001079 write_lock(&handler_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001081 /* If we're already initialized, nothing to do */
1082 if (!list_empty(&xattr_handlers)) {
1083 write_unlock(&handler_lock);
1084 return 0;
1085 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001087 /* Add the handlers */
1088 list_add_tail(&user_handler.handlers, &xattr_handlers);
1089 list_add_tail(&trusted_handler.handlers, &xattr_handlers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090#ifdef CONFIG_REISERFS_FS_SECURITY
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001091 list_add_tail(&security_handler.handlers, &xattr_handlers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092#endif
1093#ifdef CONFIG_REISERFS_FS_POSIX_ACL
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001094 list_add_tail(&posix_acl_access_handler.handlers, &xattr_handlers);
1095 list_add_tail(&posix_acl_default_handler.handlers, &xattr_handlers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096#endif
1097
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001098 /* Run initializers, if available */
1099 list_for_each(p, &xattr_handlers) {
1100 xah = list_entry(p, struct reiserfs_xattr_handler, handlers);
1101 if (xah->init) {
1102 err = xah->init();
1103 if (err) {
1104 list_del_init(p);
1105 break;
1106 }
1107 }
1108 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001110 /* Clean up other handlers, if any failed */
1111 if (err)
1112 __unregister_handlers();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001114 write_unlock(&handler_lock);
1115 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116}
1117
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001118void reiserfs_xattr_unregister_handlers(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001120 write_lock(&handler_lock);
1121 __unregister_handlers();
1122 write_unlock(&handler_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123}
1124
1125/* This will catch lookups from the fs root to .reiserfs_priv */
1126static int
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001127xattr_lookup_poison(struct dentry *dentry, struct qstr *q1, struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001129 struct dentry *priv_root = REISERFS_SB(dentry->d_sb)->priv_root;
1130 if (name->len == priv_root->d_name.len &&
1131 name->hash == priv_root->d_name.hash &&
1132 !memcmp(name->name, priv_root->d_name.name, name->len)) {
1133 return -ENOENT;
1134 } else if (q1->len == name->len &&
1135 !memcmp(q1->name, name->name, name->len))
1136 return 0;
1137 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138}
1139
1140static struct dentry_operations xattr_lookup_poison_ops = {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001141 .d_compare = xattr_lookup_poison,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142};
1143
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144/* We need to take a copy of the mount flags since things like
1145 * MS_RDONLY don't get set until *after* we're called.
1146 * mount_flags != mount_options */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001147int reiserfs_xattr_init(struct super_block *s, int mount_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001149 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001151 /* We need generation numbers to ensure that the oid mapping is correct
1152 * v3.5 filesystems don't have them. */
1153 if (!old_format_only(s)) {
1154 set_bit(REISERFS_XATTRS, &(REISERFS_SB(s)->s_mount_opt));
1155 } else if (reiserfs_xattrs_optional(s)) {
1156 /* Old format filesystem, but optional xattrs have been enabled
1157 * at mount time. Error out. */
1158 reiserfs_warning(s, "xattrs/ACLs not supported on pre v3.6 "
1159 "format filesystem. Failing mount.");
1160 err = -EOPNOTSUPP;
1161 goto error;
1162 } else {
1163 /* Old format filesystem, but no optional xattrs have been enabled. This
1164 * means we silently disable xattrs on the filesystem. */
1165 clear_bit(REISERFS_XATTRS, &(REISERFS_SB(s)->s_mount_opt));
1166 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001168 /* If we don't have the privroot located yet - go find it */
1169 if (reiserfs_xattrs(s) && !REISERFS_SB(s)->priv_root) {
1170 struct dentry *dentry;
1171 dentry = lookup_one_len(PRIVROOT_NAME, s->s_root,
1172 strlen(PRIVROOT_NAME));
1173 if (!IS_ERR(dentry)) {
1174 if (!(mount_flags & MS_RDONLY) && !dentry->d_inode) {
1175 struct inode *inode = dentry->d_parent->d_inode;
Jeff Mahoney75983922007-10-18 23:39:23 -07001176 mutex_lock_nested(&inode->i_mutex,
1177 I_MUTEX_XATTR);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001178 err = inode->i_op->mkdir(inode, dentry, 0700);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001179 mutex_unlock(&inode->i_mutex);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001180 if (err) {
1181 dput(dentry);
1182 dentry = NULL;
1183 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001185 if (dentry && dentry->d_inode)
1186 reiserfs_warning(s,
1187 "Created %s on %s - reserved for "
1188 "xattr storage.",
1189 PRIVROOT_NAME,
1190 reiserfs_bdevname
1191 (inode->i_sb));
1192 } else if (!dentry->d_inode) {
1193 dput(dentry);
1194 dentry = NULL;
1195 }
1196 } else
1197 err = PTR_ERR(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001199 if (!err && dentry) {
1200 s->s_root->d_op = &xattr_lookup_poison_ops;
1201 reiserfs_mark_inode_private(dentry->d_inode);
1202 REISERFS_SB(s)->priv_root = dentry;
1203 } else if (!(mount_flags & MS_RDONLY)) { /* xattrs are unavailable */
1204 /* If we're read-only it just means that the dir hasn't been
1205 * created. Not an error -- just no xattrs on the fs. We'll
1206 * check again if we go read-write */
1207 reiserfs_warning(s, "xattrs/ACLs enabled and couldn't "
1208 "find/create .reiserfs_priv. Failing mount.");
1209 err = -EOPNOTSUPP;
1210 }
1211 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001213 error:
1214 /* This is only nonzero if there was an error initializing the xattr
1215 * directory or if there is a condition where we don't support them. */
1216 if (err) {
1217 clear_bit(REISERFS_XATTRS, &(REISERFS_SB(s)->s_mount_opt));
1218 clear_bit(REISERFS_XATTRS_USER, &(REISERFS_SB(s)->s_mount_opt));
1219 clear_bit(REISERFS_POSIXACL, &(REISERFS_SB(s)->s_mount_opt));
1220 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001222 /* The super_block MS_POSIXACL must mirror the (no)acl mount option. */
1223 s->s_flags = s->s_flags & ~MS_POSIXACL;
1224 if (reiserfs_posixacl(s))
1225 s->s_flags |= MS_POSIXACL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001227 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228}
1229
Christoph Hellwigec191572006-02-01 03:06:46 -08001230static int reiserfs_check_acl(struct inode *inode, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231{
Christoph Hellwigec191572006-02-01 03:06:46 -08001232 struct posix_acl *acl;
1233 int error = -EAGAIN; /* do regular unix permission checks by default */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234
Christoph Hellwigec191572006-02-01 03:06:46 -08001235 reiserfs_read_lock_xattr_i(inode);
1236 reiserfs_read_lock_xattrs(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237
Christoph Hellwigec191572006-02-01 03:06:46 -08001238 acl = reiserfs_get_acl(inode, ACL_TYPE_ACCESS);
1239
1240 reiserfs_read_unlock_xattrs(inode->i_sb);
1241 reiserfs_read_unlock_xattr_i(inode);
1242
1243 if (acl) {
1244 if (!IS_ERR(acl)) {
1245 error = posix_acl_permission(inode, acl, mask);
1246 posix_acl_release(acl);
1247 } else if (PTR_ERR(acl) != -ENODATA)
1248 error = PTR_ERR(acl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 }
1250
Christoph Hellwigec191572006-02-01 03:06:46 -08001251 return error;
1252}
1253
1254int reiserfs_permission(struct inode *inode, int mask, struct nameidata *nd)
1255{
1256 /*
1257 * We don't do permission checks on the internal objects.
1258 * Permissions are determined by the "owning" object.
1259 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001260 if (is_reiserfs_priv_object(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 return 0;
1262
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 /*
Christoph Hellwigec191572006-02-01 03:06:46 -08001264 * Stat data v1 doesn't support ACLs.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 */
Christoph Hellwigec191572006-02-01 03:06:46 -08001266 if (get_inode_sd_version(inode) == STAT_DATA_V1)
1267 return generic_permission(inode, mask, NULL);
1268 else
1269 return generic_permission(inode, mask, reiserfs_check_acl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270}