blob: 3e9e82ca3ba2a8fbd077323d4934fd70485f6dee [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48#define FL_READONLY 128
49#define FL_DIR_SEM_HELD 256
50#define PRIVROOT_NAME ".reiserfs_priv"
51#define XAROOT_NAME "xattrs"
52
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070053static struct reiserfs_xattr_handler *find_xattr_handler_prefix(const char
54 *prefix);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Jeff Mahoney9b7f3752007-04-23 14:41:17 -070056/* Returns the dentry referring to the root of the extended attribute
57 * directory tree. If it has already been retrieved, it is used. If it
58 * hasn't been created and the flags indicate creation is allowed, we
59 * attempt to create it. On error, we return a pointer-encoded error.
60 */
61static struct dentry *get_xa_root(struct super_block *sb, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070062{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070063 struct dentry *privroot = dget(REISERFS_SB(sb)->priv_root);
64 struct dentry *xaroot;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070066 /* This needs to be created at mount-time */
67 if (!privroot)
Jeff Mahoney9b7f3752007-04-23 14:41:17 -070068 return ERR_PTR(-ENODATA);
69
Jeff Mahoney1173a722007-04-30 15:09:50 -070070 mutex_lock_nested(&privroot->d_inode->i_mutex, I_MUTEX_XATTR);
Jeff Mahoney9b7f3752007-04-23 14:41:17 -070071 if (REISERFS_SB(sb)->xattr_root) {
72 xaroot = dget(REISERFS_SB(sb)->xattr_root);
73 goto out;
74 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070076 xaroot = lookup_one_len(XAROOT_NAME, privroot, strlen(XAROOT_NAME));
77 if (IS_ERR(xaroot)) {
78 goto out;
79 } else if (!xaroot->d_inode) {
Jeff Mahoney9b7f3752007-04-23 14:41:17 -070080 int err = -ENODATA;
81 if (flags == 0 || flags & XATTR_CREATE)
82 err = privroot->d_inode->i_op->mkdir(privroot->d_inode,
83 xaroot, 0700);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070084 if (err) {
85 dput(xaroot);
Jeff Mahoney9b7f3752007-04-23 14:41:17 -070086 xaroot = ERR_PTR(err);
87 goto out;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070088 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070089 }
Jeff Mahoney9b7f3752007-04-23 14:41:17 -070090 REISERFS_SB(sb)->xattr_root = dget(xaroot);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070092 out:
Jeff Mahoney9b7f3752007-04-23 14:41:17 -070093 mutex_unlock(&privroot->d_inode->i_mutex);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070094 dput(privroot);
95 return xaroot;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096}
97
Linus Torvalds1da177e2005-04-16 15:20:36 -070098/* Opens the directory corresponding to the inode's extended attribute store.
99 * If flags allow, the tree to the directory may be created. If creation is
100 * prohibited, -ENODATA is returned. */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700101static struct dentry *open_xa_dir(const struct inode *inode, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700103 struct dentry *xaroot, *xadir;
104 char namebuf[17];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
Jeff Mahoney9b7f3752007-04-23 14:41:17 -0700106 xaroot = get_xa_root(inode->i_sb, flags);
107 if (IS_ERR(xaroot))
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700108 return xaroot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700110 /* ok, we have xaroot open */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700111 snprintf(namebuf, sizeof(namebuf), "%X.%X",
112 le32_to_cpu(INODE_PKEY(inode)->k_objectid),
113 inode->i_generation);
114 xadir = lookup_one_len(namebuf, xaroot, strlen(namebuf));
115 if (IS_ERR(xadir)) {
116 dput(xaroot);
117 return xadir;
118 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700120 if (!xadir->d_inode) {
121 int err;
122 if (flags == 0 || flags & XATTR_CREATE) {
123 /* Although there is nothing else trying to create this directory,
124 * another directory with the same hash may be created, so we need
125 * to protect against that */
126 err =
127 xaroot->d_inode->i_op->mkdir(xaroot->d_inode, xadir,
128 0700);
129 if (err) {
130 dput(xaroot);
131 dput(xadir);
132 return ERR_PTR(err);
133 }
134 }
135 if (!xadir->d_inode) {
136 dput(xaroot);
137 dput(xadir);
138 return ERR_PTR(-ENODATA);
139 }
140 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700142 dput(xaroot);
143 return xadir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144}
145
146/* Returns a dentry corresponding to a specific extended attribute file
147 * for the inode. If flags allow, the file is created. Otherwise, a
148 * valid or negative dentry, or an error is returned. */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700149static struct dentry *get_xa_file_dentry(const struct inode *inode,
150 const char *name, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700152 struct dentry *xadir, *xafile;
153 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700155 xadir = open_xa_dir(inode, flags);
156 if (IS_ERR(xadir)) {
David Howellse231c2e2008-02-07 00:15:26 -0800157 return ERR_CAST(xadir);
Julien Brunel67b172c2008-10-15 22:04:12 -0700158 } else if (!xadir->d_inode) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700159 dput(xadir);
160 return ERR_PTR(-ENODATA);
161 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700163 xafile = lookup_one_len(name, xadir, strlen(name));
164 if (IS_ERR(xafile)) {
165 dput(xadir);
David Howellse231c2e2008-02-07 00:15:26 -0800166 return ERR_CAST(xafile);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700167 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700169 if (xafile->d_inode) { /* file exists */
170 if (flags & XATTR_CREATE) {
171 err = -EEXIST;
172 dput(xafile);
173 goto out;
174 }
175 } else if (flags & XATTR_REPLACE || flags & FL_READONLY) {
176 goto out;
177 } else {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800178 /* inode->i_mutex is down, so nothing else can try to create
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700179 * the same xattr */
180 err = xadir->d_inode->i_op->create(xadir->d_inode, xafile,
181 0700 | S_IFREG, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700183 if (err) {
184 dput(xafile);
185 goto out;
186 }
187 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700189 out:
190 dput(xadir);
191 if (err)
192 xafile = ERR_PTR(err);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700193 else if (!xafile->d_inode) {
194 dput(xafile);
Jeff Mahoney3227e142008-02-15 14:37:22 -0800195 xafile = ERR_PTR(-ENODATA);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700196 }
Jeff Mahoney3227e142008-02-15 14:37:22 -0800197 return xafile;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198}
199
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200/*
201 * this is very similar to fs/reiserfs/dir.c:reiserfs_readdir, but
202 * we need to drop the path before calling the filldir struct. That
203 * would be a big performance hit to the non-xattr case, so I've copied
204 * the whole thing for now. --clm
205 *
206 * the big difference is that I go backwards through the directory,
207 * and don't mess with f->f_pos, but the idea is the same. Do some
208 * action on each and every entry in the directory.
209 *
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800210 * we're called with i_mutex held, so there are no worries about the directory
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 * changing underneath us.
212 */
Jeff Mahoney3227e142008-02-15 14:37:22 -0800213static int __xattr_readdir(struct inode *inode, void *dirent, filldir_t filldir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700215 struct cpu_key pos_key; /* key of current position in the directory (key of directory entry) */
216 INITIALIZE_PATH(path_to_entry);
217 struct buffer_head *bh;
218 int entry_num;
219 struct item_head *ih, tmp_ih;
220 int search_res;
221 char *local_buf;
222 loff_t next_pos;
223 char small_buf[32]; /* avoid kmalloc if we can */
224 struct reiserfs_de_head *deh;
225 int d_reclen;
226 char *d_name;
227 off_t d_off;
228 ino_t d_ino;
229 struct reiserfs_dir_entry de;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700231 /* form key for search the next directory entry using f_pos field of
232 file structure */
233 next_pos = max_reiserfs_offset(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700235 while (1) {
236 research:
237 if (next_pos <= DOT_DOT_OFFSET)
238 break;
239 make_cpu_key(&pos_key, inode, next_pos, TYPE_DIRENTRY, 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700241 search_res =
242 search_by_entry_key(inode->i_sb, &pos_key, &path_to_entry,
243 &de);
244 if (search_res == IO_ERROR) {
245 // FIXME: we could just skip part of directory which could
246 // not be read
247 pathrelse(&path_to_entry);
248 return -EIO;
249 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700251 if (search_res == NAME_NOT_FOUND)
252 de.de_entry_num--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700254 set_de_name_and_namelen(&de);
255 entry_num = de.de_entry_num;
256 deh = &(de.de_deh[entry_num]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700258 bh = de.de_bh;
259 ih = de.de_ih;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700261 if (!is_direntry_le_ih(ih)) {
Jeff Mahoney0030b642009-03-30 14:02:28 -0400262 reiserfs_error(inode->i_sb, "jdm-20000",
263 "not direntry %h", ih);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700264 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
Jeff Mahoneyec6ea562009-03-30 14:02:30 -0400379static struct page *reiserfs_get_page(struct inode *dir, size_t 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);
Jeff Mahoneyec6ea562009-03-30 14:02:30 -0400386 page = read_mapping_page(mapping, n >> PAGE_CACHE_SHIFT, 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;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700423 size_t file_pos = 0;
424 size_t buffer_pos = 0;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700425 struct iattr newattrs;
426 __u32 xahash = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700428 if (get_inode_sd_version(inode) == STAT_DATA_V1)
429 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700431 /* Empty xattrs are ok, they're just empty files, no hash */
432 if (buffer && buffer_size)
433 xahash = xattr_hash(buffer, buffer_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
Jeff Mahoney3227e142008-02-15 14:37:22 -0800435 dentry = get_xa_file_dentry(inode, name, flags);
436 if (IS_ERR(dentry)) {
437 err = PTR_ERR(dentry);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700438 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700441 REISERFS_I(inode)->i_flags |= i_has_xattr_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700443 /* Resize it so we're ok to write there */
444 newattrs.ia_size = buffer_size;
445 newattrs.ia_valid = ATTR_SIZE | ATTR_CTIME;
Jeff Mahoneyf437c522009-03-30 14:02:29 -0400446 mutex_lock_nested(&dentry->d_inode->i_mutex, I_MUTEX_XATTR);
Jeff Mahoney3227e142008-02-15 14:37:22 -0800447 err = notify_change(dentry, &newattrs);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700448 if (err)
449 goto out_filp;
450
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700451 while (buffer_pos < buffer_size || buffer_pos == 0) {
452 size_t chunk;
453 size_t skip = 0;
454 size_t page_offset = (file_pos & (PAGE_CACHE_SIZE - 1));
455 if (buffer_size - buffer_pos > PAGE_CACHE_SIZE)
456 chunk = PAGE_CACHE_SIZE;
457 else
458 chunk = buffer_size - buffer_pos;
459
Jeff Mahoneyec6ea562009-03-30 14:02:30 -0400460 page = reiserfs_get_page(dentry->d_inode, file_pos);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700461 if (IS_ERR(page)) {
462 err = PTR_ERR(page);
463 goto out_filp;
464 }
465
466 lock_page(page);
467 data = page_address(page);
468
469 if (file_pos == 0) {
470 struct reiserfs_xattr_header *rxh;
471 skip = file_pos = sizeof(struct reiserfs_xattr_header);
472 if (chunk + skip > PAGE_CACHE_SIZE)
473 chunk = PAGE_CACHE_SIZE - skip;
474 rxh = (struct reiserfs_xattr_header *)data;
475 rxh->h_magic = cpu_to_le32(REISERFS_XATTR_MAGIC);
476 rxh->h_hash = cpu_to_le32(xahash);
477 }
478
Jeff Mahoney3227e142008-02-15 14:37:22 -0800479 err = reiserfs_prepare_write(NULL, page, page_offset,
Vladimir Savelievba9d8ce2007-10-16 01:25:14 -0700480 page_offset + chunk + skip);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700481 if (!err) {
482 if (buffer)
483 memcpy(data + skip, buffer + buffer_pos, chunk);
Jeff Mahoney3227e142008-02-15 14:37:22 -0800484 err = reiserfs_commit_write(NULL, page, page_offset,
485 page_offset + chunk +
486 skip);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700487 }
488 unlock_page(page);
489 reiserfs_put_page(page);
490 buffer_pos += chunk;
491 file_pos += chunk;
492 skip = 0;
493 if (err || buffer_size == 0 || !buffer)
494 break;
495 }
496
497 /* We can't mark the inode dirty if it's not hashed. This is the case
498 * when we're inheriting the default ACL. If we dirty it, the inode
499 * gets marked dirty, but won't (ever) make it onto the dirty list until
500 * it's synced explicitly to clear I_DIRTY. This is bad. */
501 if (!hlist_unhashed(&inode->i_hash)) {
502 inode->i_ctime = CURRENT_TIME_SEC;
503 mark_inode_dirty(inode);
504 }
505
506 out_filp:
Jeff Mahoneyf437c522009-03-30 14:02:29 -0400507 mutex_unlock(&dentry->d_inode->i_mutex);
Jeff Mahoney3227e142008-02-15 14:37:22 -0800508 dput(dentry);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700509
510 out:
511 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512}
513
514/*
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800515 * inode->i_mutex: down
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 */
517int
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700518reiserfs_xattr_get(const struct inode *inode, const char *name, void *buffer,
519 size_t buffer_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700521 ssize_t err = 0;
Jeff Mahoney3227e142008-02-15 14:37:22 -0800522 struct dentry *dentry;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700523 size_t isize;
524 size_t file_pos = 0;
525 size_t buffer_pos = 0;
526 struct page *page;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700527 __u32 hash = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700529 if (name == NULL)
530 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700532 /* We can't have xattrs attached to v1 items since they don't have
533 * generation numbers */
534 if (get_inode_sd_version(inode) == STAT_DATA_V1)
535 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
Jeff Mahoney3227e142008-02-15 14:37:22 -0800537 dentry = get_xa_file_dentry(inode, name, FL_READONLY);
538 if (IS_ERR(dentry)) {
539 err = PTR_ERR(dentry);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700540 goto out;
541 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
Jeff Mahoneyf437c522009-03-30 14:02:29 -0400543 isize = i_size_read(dentry->d_inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700544 REISERFS_I(inode)->i_flags |= i_has_xattr_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700546 /* Just return the size needed */
547 if (buffer == NULL) {
548 err = isize - sizeof(struct reiserfs_xattr_header);
549 goto out_dput;
550 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700552 if (buffer_size < isize - sizeof(struct reiserfs_xattr_header)) {
553 err = -ERANGE;
554 goto out_dput;
555 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700557 while (file_pos < isize) {
558 size_t chunk;
559 char *data;
560 size_t skip = 0;
561 if (isize - file_pos > PAGE_CACHE_SIZE)
562 chunk = PAGE_CACHE_SIZE;
563 else
564 chunk = isize - file_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
Jeff Mahoneyec6ea562009-03-30 14:02:30 -0400566 page = reiserfs_get_page(dentry->d_inode, file_pos);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700567 if (IS_ERR(page)) {
568 err = PTR_ERR(page);
569 goto out_dput;
570 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700572 lock_page(page);
573 data = page_address(page);
574 if (file_pos == 0) {
575 struct reiserfs_xattr_header *rxh =
576 (struct reiserfs_xattr_header *)data;
577 skip = file_pos = sizeof(struct reiserfs_xattr_header);
578 chunk -= skip;
579 /* Magic doesn't match up.. */
580 if (rxh->h_magic != cpu_to_le32(REISERFS_XATTR_MAGIC)) {
581 unlock_page(page);
582 reiserfs_put_page(page);
Jeff Mahoney45b03d52009-03-30 14:02:21 -0400583 reiserfs_warning(inode->i_sb, "jdm-20001",
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700584 "Invalid magic for xattr (%s) "
585 "associated with %k", name,
586 INODE_PKEY(inode));
587 err = -EIO;
588 goto out_dput;
589 }
590 hash = le32_to_cpu(rxh->h_hash);
591 }
592 memcpy(buffer + buffer_pos, data + skip, chunk);
593 unlock_page(page);
594 reiserfs_put_page(page);
595 file_pos += chunk;
596 buffer_pos += chunk;
597 skip = 0;
598 }
599 err = isize - sizeof(struct reiserfs_xattr_header);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700601 if (xattr_hash(buffer, isize - sizeof(struct reiserfs_xattr_header)) !=
602 hash) {
Jeff Mahoney45b03d52009-03-30 14:02:21 -0400603 reiserfs_warning(inode->i_sb, "jdm-20002",
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700604 "Invalid hash for xattr (%s) associated "
605 "with %k", name, INODE_PKEY(inode));
606 err = -EIO;
607 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700609 out_dput:
Jeff Mahoney3227e142008-02-15 14:37:22 -0800610 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700612 out:
613 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614}
615
616static int
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700617__reiserfs_xattr_del(struct dentry *xadir, const char *name, int namelen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700619 struct dentry *dentry;
620 struct inode *dir = xadir->d_inode;
621 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700623 dentry = lookup_one_len(name, xadir, namelen);
624 if (IS_ERR(dentry)) {
625 err = PTR_ERR(dentry);
626 goto out;
627 } else if (!dentry->d_inode) {
628 err = -ENODATA;
629 goto out_file;
630 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700632 /* Skip directories.. */
633 if (S_ISDIR(dentry->d_inode->i_mode))
634 goto out_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700636 if (!is_reiserfs_priv_object(dentry->d_inode)) {
Jeff Mahoney0030b642009-03-30 14:02:28 -0400637 reiserfs_error(dir->i_sb, "jdm-20003",
638 "OID %08x [%.*s/%.*s] doesn't have "
639 "priv flag set [parent is %sset].",
640 le32_to_cpu(INODE_PKEY(dentry->d_inode)->
641 k_objectid), xadir->d_name.len,
642 xadir->d_name.name, namelen, name,
643 is_reiserfs_priv_object(xadir->d_inode) ? "" :
644 "not ");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700645 dput(dentry);
646 return -EIO;
647 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700649 err = dir->i_op->unlink(dir, dentry);
650 if (!err)
651 d_delete(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700653 out_file:
654 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700656 out:
657 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658}
659
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700660int reiserfs_xattr_del(struct inode *inode, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700662 struct dentry *dir;
663 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700665 dir = open_xa_dir(inode, FL_READONLY);
666 if (IS_ERR(dir)) {
667 err = PTR_ERR(dir);
668 goto out;
669 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700671 err = __reiserfs_xattr_del(dir, name, strlen(name));
672 dput(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700674 if (!err) {
675 inode->i_ctime = CURRENT_TIME_SEC;
676 mark_inode_dirty(inode);
677 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700679 out:
680 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681}
682
683/* The following are side effects of other operations that aren't explicitly
684 * modifying extended attributes. This includes operations such as permissions
685 * or ownership changes, object deletions, etc. */
686
687static int
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700688reiserfs_delete_xattrs_filler(void *buf, const char *name, int namelen,
David Howellsafefdbb2006-10-03 01:13:46 -0700689 loff_t offset, u64 ino, unsigned int d_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700691 struct dentry *xadir = (struct dentry *)buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700693 return __reiserfs_xattr_del(xadir, name, namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
695}
696
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800697/* This is called w/ inode->i_mutex downed */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700698int reiserfs_delete_xattrs(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700700 struct dentry *dir, *root;
701 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700703 /* Skip out, an xattr has no xattrs associated with it */
704 if (is_reiserfs_priv_object(inode) ||
705 get_inode_sd_version(inode) == STAT_DATA_V1 ||
706 !reiserfs_xattrs(inode->i_sb)) {
707 return 0;
708 }
709 reiserfs_read_lock_xattrs(inode->i_sb);
710 dir = open_xa_dir(inode, FL_READONLY);
711 reiserfs_read_unlock_xattrs(inode->i_sb);
712 if (IS_ERR(dir)) {
713 err = PTR_ERR(dir);
714 goto out;
715 } else if (!dir->d_inode) {
716 dput(dir);
717 return 0;
718 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700720 lock_kernel();
Jeff Mahoney3227e142008-02-15 14:37:22 -0800721 err = xattr_readdir(dir->d_inode, reiserfs_delete_xattrs_filler, dir);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700722 if (err) {
723 unlock_kernel();
724 goto out_dir;
725 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700727 /* Leftovers besides . and .. -- that's not good. */
728 if (dir->d_inode->i_nlink <= 2) {
Jeff Mahoney9b7f3752007-04-23 14:41:17 -0700729 root = get_xa_root(inode->i_sb, XATTR_REPLACE);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700730 reiserfs_write_lock_xattrs(inode->i_sb);
731 err = vfs_rmdir(root->d_inode, dir);
732 reiserfs_write_unlock_xattrs(inode->i_sb);
733 dput(root);
734 } else {
Jeff Mahoney45b03d52009-03-30 14:02:21 -0400735 reiserfs_warning(inode->i_sb, "jdm-20006",
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700736 "Couldn't remove all entries in directory");
737 }
738 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700740 out_dir:
Jeff Mahoney3227e142008-02-15 14:37:22 -0800741 dput(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700743 out:
744 if (!err)
745 REISERFS_I(inode)->i_flags =
746 REISERFS_I(inode)->i_flags & ~i_has_xattr_dir;
747 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748}
749
750struct reiserfs_chown_buf {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700751 struct inode *inode;
752 struct dentry *xadir;
753 struct iattr *attrs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754};
755
756/* XXX: If there is a better way to do this, I'd love to hear about it */
757static int
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700758reiserfs_chown_xattrs_filler(void *buf, const char *name, int namelen,
David Howellsafefdbb2006-10-03 01:13:46 -0700759 loff_t offset, u64 ino, unsigned int d_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700761 struct reiserfs_chown_buf *chown_buf = (struct reiserfs_chown_buf *)buf;
762 struct dentry *xafile, *xadir = chown_buf->xadir;
763 struct iattr *attrs = chown_buf->attrs;
764 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700766 xafile = lookup_one_len(name, xadir, namelen);
767 if (IS_ERR(xafile))
768 return PTR_ERR(xafile);
769 else if (!xafile->d_inode) {
770 dput(xafile);
771 return -ENODATA;
772 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700774 if (!S_ISDIR(xafile->d_inode->i_mode))
775 err = notify_change(xafile, attrs);
776 dput(xafile);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700778 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779}
780
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700781int reiserfs_chown_xattrs(struct inode *inode, struct iattr *attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700783 struct dentry *dir;
784 int err = 0;
785 struct reiserfs_chown_buf buf;
786 unsigned int ia_valid = attrs->ia_valid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700788 /* Skip out, an xattr has no xattrs associated with it */
789 if (is_reiserfs_priv_object(inode) ||
790 get_inode_sd_version(inode) == STAT_DATA_V1 ||
791 !reiserfs_xattrs(inode->i_sb)) {
792 return 0;
793 }
794 reiserfs_read_lock_xattrs(inode->i_sb);
795 dir = open_xa_dir(inode, FL_READONLY);
796 reiserfs_read_unlock_xattrs(inode->i_sb);
797 if (IS_ERR(dir)) {
798 if (PTR_ERR(dir) != -ENODATA)
799 err = PTR_ERR(dir);
800 goto out;
801 } else if (!dir->d_inode) {
802 dput(dir);
803 goto out;
804 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700806 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700808 attrs->ia_valid &= (ATTR_UID | ATTR_GID | ATTR_CTIME);
809 buf.xadir = dir;
810 buf.attrs = attrs;
811 buf.inode = inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
Jeff Mahoney3227e142008-02-15 14:37:22 -0800813 err = xattr_readdir(dir->d_inode, reiserfs_chown_xattrs_filler, &buf);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700814 if (err) {
815 unlock_kernel();
816 goto out_dir;
817 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700819 err = notify_change(dir, attrs);
820 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700822 out_dir:
Jeff Mahoney3227e142008-02-15 14:37:22 -0800823 dput(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700825 out:
826 attrs->ia_valid = ia_valid;
827 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828}
829
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830/* Actual operations that are exported to VFS-land */
831
832/*
833 * Inode operation getxattr()
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800834 * Preliminary locking: we down dentry->d_inode->i_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 */
836ssize_t
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700837reiserfs_getxattr(struct dentry * dentry, const char *name, void *buffer,
838 size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700840 struct reiserfs_xattr_handler *xah = find_xattr_handler_prefix(name);
841 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700843 if (!xah || !reiserfs_xattrs(dentry->d_sb) ||
844 get_inode_sd_version(dentry->d_inode) == STAT_DATA_V1)
845 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700847 reiserfs_read_lock_xattr_i(dentry->d_inode);
848 reiserfs_read_lock_xattrs(dentry->d_sb);
849 err = xah->get(dentry->d_inode, name, buffer, size);
850 reiserfs_read_unlock_xattrs(dentry->d_sb);
851 reiserfs_read_unlock_xattr_i(dentry->d_inode);
852 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853}
854
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855/*
856 * Inode operation setxattr()
857 *
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800858 * dentry->d_inode->i_mutex down
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 */
860int
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700861reiserfs_setxattr(struct dentry *dentry, const char *name, const void *value,
862 size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700864 struct reiserfs_xattr_handler *xah = find_xattr_handler_prefix(name);
865 int err;
866 int lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700868 if (!xah || !reiserfs_xattrs(dentry->d_sb) ||
869 get_inode_sd_version(dentry->d_inode) == STAT_DATA_V1)
870 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700872 reiserfs_write_lock_xattr_i(dentry->d_inode);
873 lock = !has_xattr_dir(dentry->d_inode);
874 if (lock)
875 reiserfs_write_lock_xattrs(dentry->d_sb);
876 else
877 reiserfs_read_lock_xattrs(dentry->d_sb);
878 err = xah->set(dentry->d_inode, name, value, size, flags);
879 if (lock)
880 reiserfs_write_unlock_xattrs(dentry->d_sb);
881 else
882 reiserfs_read_unlock_xattrs(dentry->d_sb);
883 reiserfs_write_unlock_xattr_i(dentry->d_inode);
884 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885}
886
887/*
888 * Inode operation removexattr()
889 *
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800890 * dentry->d_inode->i_mutex down
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700892int reiserfs_removexattr(struct dentry *dentry, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700894 int err;
895 struct reiserfs_xattr_handler *xah = find_xattr_handler_prefix(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700897 if (!xah || !reiserfs_xattrs(dentry->d_sb) ||
898 get_inode_sd_version(dentry->d_inode) == STAT_DATA_V1)
899 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700901 reiserfs_write_lock_xattr_i(dentry->d_inode);
902 reiserfs_read_lock_xattrs(dentry->d_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700904 /* Deletion pre-operation */
905 if (xah->del) {
906 err = xah->del(dentry->d_inode, name);
907 if (err)
908 goto out;
909 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700911 err = reiserfs_xattr_del(dentry->d_inode, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700913 dentry->d_inode->i_ctime = CURRENT_TIME_SEC;
914 mark_inode_dirty(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700916 out:
917 reiserfs_read_unlock_xattrs(dentry->d_sb);
918 reiserfs_write_unlock_xattr_i(dentry->d_inode);
919 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920}
921
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922/* This is what filldir will use:
923 * r_pos will always contain the amount of space required for the entire
924 * list. If r_pos becomes larger than r_size, we need more space and we
925 * return an error indicating this. If r_pos is less than r_size, then we've
926 * filled the buffer successfully and we return success */
927struct reiserfs_listxattr_buf {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700928 int r_pos;
929 int r_size;
930 char *r_buf;
931 struct inode *r_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932};
933
934static int
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700935reiserfs_listxattr_filler(void *buf, const char *name, int namelen,
David Howellsafefdbb2006-10-03 01:13:46 -0700936 loff_t offset, u64 ino, unsigned int d_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700938 struct reiserfs_listxattr_buf *b = (struct reiserfs_listxattr_buf *)buf;
939 int len = 0;
940 if (name[0] != '.'
941 || (namelen != 1 && (name[1] != '.' || namelen != 2))) {
942 struct reiserfs_xattr_handler *xah =
943 find_xattr_handler_prefix(name);
944 if (!xah)
945 return 0; /* Unsupported xattr name, skip it */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700947 /* We call ->list() twice because the operation isn't required to just
948 * return the name back - we want to make sure we have enough space */
949 len += xah->list(b->r_inode, name, namelen, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700951 if (len) {
952 if (b->r_pos + len + 1 <= b->r_size) {
953 char *p = b->r_buf + b->r_pos;
954 p += xah->list(b->r_inode, name, namelen, p);
955 *p++ = '\0';
956 }
957 b->r_pos += len + 1;
958 }
959 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700961 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962}
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700963
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964/*
965 * Inode operation listxattr()
966 *
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800967 * Preliminary locking: we down dentry->d_inode->i_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700969ssize_t reiserfs_listxattr(struct dentry * dentry, char *buffer, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700971 struct dentry *dir;
972 int err = 0;
973 struct reiserfs_listxattr_buf buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700975 if (!dentry->d_inode)
976 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700978 if (!reiserfs_xattrs(dentry->d_sb) ||
979 get_inode_sd_version(dentry->d_inode) == STAT_DATA_V1)
980 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700982 reiserfs_read_lock_xattr_i(dentry->d_inode);
983 reiserfs_read_lock_xattrs(dentry->d_sb);
984 dir = open_xa_dir(dentry->d_inode, FL_READONLY);
985 reiserfs_read_unlock_xattrs(dentry->d_sb);
986 if (IS_ERR(dir)) {
987 err = PTR_ERR(dir);
988 if (err == -ENODATA)
989 err = 0; /* Not an error if there aren't any xattrs */
990 goto out;
991 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700993 buf.r_buf = buffer;
994 buf.r_size = buffer ? size : 0;
995 buf.r_pos = 0;
996 buf.r_inode = dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700998 REISERFS_I(dentry->d_inode)->i_flags |= i_has_xattr_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999
Jeff Mahoney3227e142008-02-15 14:37:22 -08001000 err = xattr_readdir(dir->d_inode, reiserfs_listxattr_filler, &buf);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001001 if (err)
1002 goto out_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001004 if (buf.r_pos > buf.r_size && buffer != NULL)
1005 err = -ERANGE;
1006 else
1007 err = buf.r_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001009 out_dir:
Jeff Mahoney3227e142008-02-15 14:37:22 -08001010 dput(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001012 out:
1013 reiserfs_read_unlock_xattr_i(dentry->d_inode);
1014 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015}
1016
1017/* This is the implementation for the xattr plugin infrastructure */
Denis Chengbcf11cb2008-02-06 01:37:40 -08001018static LIST_HEAD(xattr_handlers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019static DEFINE_RWLOCK(handler_lock);
1020
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001021static struct reiserfs_xattr_handler *find_xattr_handler_prefix(const char
1022 *prefix)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001024 struct reiserfs_xattr_handler *xah = NULL;
1025 struct list_head *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001027 read_lock(&handler_lock);
1028 list_for_each(p, &xattr_handlers) {
1029 xah = list_entry(p, struct reiserfs_xattr_handler, handlers);
1030 if (strncmp(xah->prefix, prefix, strlen(xah->prefix)) == 0)
1031 break;
1032 xah = NULL;
1033 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001035 read_unlock(&handler_lock);
1036 return xah;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037}
1038
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001039static void __unregister_handlers(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001041 struct reiserfs_xattr_handler *xah;
1042 struct list_head *p, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001044 list_for_each_safe(p, tmp, &xattr_handlers) {
1045 xah = list_entry(p, struct reiserfs_xattr_handler, handlers);
1046 if (xah->exit)
1047 xah->exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001049 list_del_init(p);
1050 }
1051 INIT_LIST_HEAD(&xattr_handlers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052}
1053
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001054int __init reiserfs_xattr_register_handlers(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001056 int err = 0;
1057 struct reiserfs_xattr_handler *xah;
1058 struct list_head *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001060 write_lock(&handler_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001062 /* If we're already initialized, nothing to do */
1063 if (!list_empty(&xattr_handlers)) {
1064 write_unlock(&handler_lock);
1065 return 0;
1066 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001068 /* Add the handlers */
1069 list_add_tail(&user_handler.handlers, &xattr_handlers);
1070 list_add_tail(&trusted_handler.handlers, &xattr_handlers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071#ifdef CONFIG_REISERFS_FS_SECURITY
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001072 list_add_tail(&security_handler.handlers, &xattr_handlers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073#endif
1074#ifdef CONFIG_REISERFS_FS_POSIX_ACL
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001075 list_add_tail(&posix_acl_access_handler.handlers, &xattr_handlers);
1076 list_add_tail(&posix_acl_default_handler.handlers, &xattr_handlers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077#endif
1078
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001079 /* Run initializers, if available */
1080 list_for_each(p, &xattr_handlers) {
1081 xah = list_entry(p, struct reiserfs_xattr_handler, handlers);
1082 if (xah->init) {
1083 err = xah->init();
1084 if (err) {
1085 list_del_init(p);
1086 break;
1087 }
1088 }
1089 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001091 /* Clean up other handlers, if any failed */
1092 if (err)
1093 __unregister_handlers();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001095 write_unlock(&handler_lock);
1096 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097}
1098
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001099void reiserfs_xattr_unregister_handlers(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001101 write_lock(&handler_lock);
1102 __unregister_handlers();
1103 write_unlock(&handler_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104}
1105
1106/* This will catch lookups from the fs root to .reiserfs_priv */
1107static int
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001108xattr_lookup_poison(struct dentry *dentry, struct qstr *q1, struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001110 struct dentry *priv_root = REISERFS_SB(dentry->d_sb)->priv_root;
1111 if (name->len == priv_root->d_name.len &&
1112 name->hash == priv_root->d_name.hash &&
1113 !memcmp(name->name, priv_root->d_name.name, name->len)) {
1114 return -ENOENT;
1115 } else if (q1->len == name->len &&
1116 !memcmp(q1->name, name->name, name->len))
1117 return 0;
1118 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119}
1120
1121static struct dentry_operations xattr_lookup_poison_ops = {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001122 .d_compare = xattr_lookup_poison,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123};
1124
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125/* We need to take a copy of the mount flags since things like
1126 * MS_RDONLY don't get set until *after* we're called.
1127 * mount_flags != mount_options */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001128int reiserfs_xattr_init(struct super_block *s, int mount_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001130 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001132 /* We need generation numbers to ensure that the oid mapping is correct
1133 * v3.5 filesystems don't have them. */
1134 if (!old_format_only(s)) {
1135 set_bit(REISERFS_XATTRS, &(REISERFS_SB(s)->s_mount_opt));
1136 } else if (reiserfs_xattrs_optional(s)) {
1137 /* Old format filesystem, but optional xattrs have been enabled
1138 * at mount time. Error out. */
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001139 reiserfs_warning(s, "jdm-20005",
1140 "xattrs/ACLs not supported on pre v3.6 "
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001141 "format filesystem. Failing mount.");
1142 err = -EOPNOTSUPP;
1143 goto error;
1144 } else {
1145 /* Old format filesystem, but no optional xattrs have been enabled. This
1146 * means we silently disable xattrs on the filesystem. */
1147 clear_bit(REISERFS_XATTRS, &(REISERFS_SB(s)->s_mount_opt));
1148 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001150 /* If we don't have the privroot located yet - go find it */
1151 if (reiserfs_xattrs(s) && !REISERFS_SB(s)->priv_root) {
1152 struct dentry *dentry;
1153 dentry = lookup_one_len(PRIVROOT_NAME, s->s_root,
1154 strlen(PRIVROOT_NAME));
1155 if (!IS_ERR(dentry)) {
1156 if (!(mount_flags & MS_RDONLY) && !dentry->d_inode) {
1157 struct inode *inode = dentry->d_parent->d_inode;
Jeff Mahoney75983922007-10-18 23:39:23 -07001158 mutex_lock_nested(&inode->i_mutex,
1159 I_MUTEX_XATTR);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001160 err = inode->i_op->mkdir(inode, dentry, 0700);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001161 mutex_unlock(&inode->i_mutex);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001162 if (err) {
1163 dput(dentry);
1164 dentry = NULL;
1165 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001167 if (dentry && dentry->d_inode)
Jeff Mahoney1d889d92009-03-30 14:02:20 -04001168 reiserfs_info(s, "Created %s - "
1169 "reserved for xattr "
1170 "storage.\n",
1171 PRIVROOT_NAME);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001172 } else if (!dentry->d_inode) {
1173 dput(dentry);
1174 dentry = NULL;
1175 }
1176 } else
1177 err = PTR_ERR(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001179 if (!err && dentry) {
1180 s->s_root->d_op = &xattr_lookup_poison_ops;
1181 reiserfs_mark_inode_private(dentry->d_inode);
1182 REISERFS_SB(s)->priv_root = dentry;
1183 } else if (!(mount_flags & MS_RDONLY)) { /* xattrs are unavailable */
1184 /* If we're read-only it just means that the dir hasn't been
1185 * created. Not an error -- just no xattrs on the fs. We'll
1186 * check again if we go read-write */
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001187 reiserfs_warning(s, "jdm-20006",
1188 "xattrs/ACLs enabled and couldn't "
1189 "find/create .reiserfs_priv. "
1190 "Failing mount.");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001191 err = -EOPNOTSUPP;
1192 }
1193 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001195 error:
1196 /* This is only nonzero if there was an error initializing the xattr
1197 * directory or if there is a condition where we don't support them. */
1198 if (err) {
1199 clear_bit(REISERFS_XATTRS, &(REISERFS_SB(s)->s_mount_opt));
1200 clear_bit(REISERFS_XATTRS_USER, &(REISERFS_SB(s)->s_mount_opt));
1201 clear_bit(REISERFS_POSIXACL, &(REISERFS_SB(s)->s_mount_opt));
1202 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001204 /* The super_block MS_POSIXACL must mirror the (no)acl mount option. */
1205 s->s_flags = s->s_flags & ~MS_POSIXACL;
1206 if (reiserfs_posixacl(s))
1207 s->s_flags |= MS_POSIXACL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001209 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210}
1211
Christoph Hellwigec191572006-02-01 03:06:46 -08001212static int reiserfs_check_acl(struct inode *inode, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213{
Christoph Hellwigec191572006-02-01 03:06:46 -08001214 struct posix_acl *acl;
1215 int error = -EAGAIN; /* do regular unix permission checks by default */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216
Christoph Hellwigec191572006-02-01 03:06:46 -08001217 reiserfs_read_lock_xattr_i(inode);
1218 reiserfs_read_lock_xattrs(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219
Christoph Hellwigec191572006-02-01 03:06:46 -08001220 acl = reiserfs_get_acl(inode, ACL_TYPE_ACCESS);
1221
1222 reiserfs_read_unlock_xattrs(inode->i_sb);
1223 reiserfs_read_unlock_xattr_i(inode);
1224
1225 if (acl) {
1226 if (!IS_ERR(acl)) {
1227 error = posix_acl_permission(inode, acl, mask);
1228 posix_acl_release(acl);
1229 } else if (PTR_ERR(acl) != -ENODATA)
1230 error = PTR_ERR(acl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 }
1232
Christoph Hellwigec191572006-02-01 03:06:46 -08001233 return error;
1234}
1235
Al Viroe6305c42008-07-15 21:03:57 -04001236int reiserfs_permission(struct inode *inode, int mask)
Christoph Hellwigec191572006-02-01 03:06:46 -08001237{
1238 /*
1239 * We don't do permission checks on the internal objects.
1240 * Permissions are determined by the "owning" object.
1241 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001242 if (is_reiserfs_priv_object(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 return 0;
1244
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 /*
Christoph Hellwigec191572006-02-01 03:06:46 -08001246 * Stat data v1 doesn't support ACLs.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 */
Christoph Hellwigec191572006-02-01 03:06:46 -08001248 if (get_inode_sd_version(inode) == STAT_DATA_V1)
1249 return generic_permission(inode, mask, NULL);
1250 else
1251 return generic_permission(inode, mask, reiserfs_check_acl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252}