Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * linux/fs/reiserfs/xattr.c | |
3 | * | |
4 | * Copyright (c) 2002 by Jeff Mahoney, <jeffm@suse.com> | |
5 | * | |
6 | */ | |
7 | ||
8 | /* | |
9 | * In order to implement EA/ACLs in a clean, backwards compatible manner, | |
10 | * they are implemented as files in a "private" directory. | |
11 | * Each EA is in it's own file, with the directory layout like so (/ is assumed | |
12 | * to be relative to fs root). Inside the /.reiserfs_priv/xattrs directory, | |
13 | * directories named using the capital-hex form of the objectid and | |
14 | * generation number are used. Inside each directory are individual files | |
15 | * named with the name of the extended attribute. | |
16 | * | |
17 | * So, for objectid 12648430, we could have: | |
18 | * /.reiserfs_priv/xattrs/C0FFEE.0/system.posix_acl_access | |
19 | * /.reiserfs_priv/xattrs/C0FFEE.0/system.posix_acl_default | |
20 | * /.reiserfs_priv/xattrs/C0FFEE.0/user.Content-Type | |
21 | * .. or similar. | |
22 | * | |
23 | * The file contents are the text of the EA. The size is known based on the | |
24 | * stat data describing the file. | |
25 | * | |
26 | * In the case of system.posix_acl_access and system.posix_acl_default, since | |
27 | * these are special cases for filesystem ACLs, they are interpreted by the | |
28 | * kernel, in addition, they are negatively and positively cached and attached | |
29 | * to the inode so that unnecessary lookups are avoided. | |
d984561b JM |
30 | * |
31 | * Locking works like so: | |
8b6dd72a JM |
32 | * Directory components (xattr root, xattr dir) are protectd by their i_mutex. |
33 | * The xattrs themselves are protected by the xattr_sem. | |
1da177e4 LT |
34 | */ |
35 | ||
36 | #include <linux/reiserfs_fs.h> | |
16f7e0fe | 37 | #include <linux/capability.h> |
1da177e4 LT |
38 | #include <linux/dcache.h> |
39 | #include <linux/namei.h> | |
40 | #include <linux/errno.h> | |
41 | #include <linux/fs.h> | |
42 | #include <linux/file.h> | |
43 | #include <linux/pagemap.h> | |
44 | #include <linux/xattr.h> | |
45 | #include <linux/reiserfs_xattr.h> | |
46 | #include <linux/reiserfs_acl.h> | |
1da177e4 | 47 | #include <asm/uaccess.h> |
3277c39f | 48 | #include <net/checksum.h> |
1da177e4 LT |
49 | #include <linux/smp_lock.h> |
50 | #include <linux/stat.h> | |
6c17675e | 51 | #include <linux/quotaops.h> |
1da177e4 | 52 | |
1da177e4 LT |
53 | #define PRIVROOT_NAME ".reiserfs_priv" |
54 | #define XAROOT_NAME "xattrs" | |
55 | ||
8b6dd72a | 56 | |
6c17675e JM |
57 | /* Helpers for inode ops. We do this so that we don't have all the VFS |
58 | * overhead and also for proper i_mutex annotation. | |
59 | * dir->i_mutex must be held for all of them. */ | |
60 | static int xattr_create(struct inode *dir, struct dentry *dentry, int mode) | |
1da177e4 | 61 | { |
6c17675e JM |
62 | BUG_ON(!mutex_is_locked(&dir->i_mutex)); |
63 | DQUOT_INIT(dir); | |
64 | return dir->i_op->create(dir, dentry, mode, NULL); | |
65 | } | |
bd4c625c | 66 | |
6c17675e JM |
67 | static int xattr_mkdir(struct inode *dir, struct dentry *dentry, int mode) |
68 | { | |
69 | BUG_ON(!mutex_is_locked(&dir->i_mutex)); | |
70 | DQUOT_INIT(dir); | |
71 | return dir->i_op->mkdir(dir, dentry, mode); | |
72 | } | |
bd4c625c | 73 | |
6c17675e JM |
74 | /* We use I_MUTEX_CHILD here to silence lockdep. It's safe because xattr |
75 | * mutation ops aren't called during rename or splace, which are the | |
76 | * only other users of I_MUTEX_CHILD. It violates the ordering, but that's | |
77 | * better than allocating another subclass just for this code. */ | |
78 | static int xattr_unlink(struct inode *dir, struct dentry *dentry) | |
79 | { | |
80 | int error; | |
81 | BUG_ON(!mutex_is_locked(&dir->i_mutex)); | |
82 | DQUOT_INIT(dir); | |
bd4c625c | 83 | |
6c17675e JM |
84 | mutex_lock_nested(&dentry->d_inode->i_mutex, I_MUTEX_CHILD); |
85 | error = dir->i_op->unlink(dir, dentry); | |
86 | mutex_unlock(&dentry->d_inode->i_mutex); | |
87 | ||
88 | if (!error) | |
89 | d_delete(dentry); | |
90 | return error; | |
91 | } | |
92 | ||
93 | static int xattr_rmdir(struct inode *dir, struct dentry *dentry) | |
94 | { | |
95 | int error; | |
96 | BUG_ON(!mutex_is_locked(&dir->i_mutex)); | |
97 | DQUOT_INIT(dir); | |
98 | ||
99 | mutex_lock_nested(&dentry->d_inode->i_mutex, I_MUTEX_CHILD); | |
100 | dentry_unhash(dentry); | |
101 | error = dir->i_op->rmdir(dir, dentry); | |
102 | if (!error) | |
103 | dentry->d_inode->i_flags |= S_DEAD; | |
104 | mutex_unlock(&dentry->d_inode->i_mutex); | |
105 | if (!error) | |
106 | d_delete(dentry); | |
107 | dput(dentry); | |
108 | ||
109 | return error; | |
110 | } | |
111 | ||
6c17675e JM |
112 | #define xattr_may_create(flags) (!flags || flags & XATTR_CREATE) |
113 | ||
114 | /* Returns and possibly creates the xattr dir. */ | |
115 | static struct dentry *lookup_or_create_dir(struct dentry *parent, | |
116 | const char *name, int flags) | |
117 | { | |
118 | struct dentry *dentry; | |
119 | BUG_ON(!parent); | |
120 | ||
121 | dentry = lookup_one_len(name, parent, strlen(name)); | |
122 | if (IS_ERR(dentry)) | |
123 | return dentry; | |
124 | else if (!dentry->d_inode) { | |
9b7f3755 | 125 | int err = -ENODATA; |
6c17675e JM |
126 | |
127 | if (xattr_may_create(flags)) { | |
128 | mutex_lock_nested(&parent->d_inode->i_mutex, | |
129 | I_MUTEX_XATTR); | |
130 | err = xattr_mkdir(parent->d_inode, dentry, 0700); | |
131 | mutex_unlock(&parent->d_inode->i_mutex); | |
132 | } | |
133 | ||
9b7f3755 | 134 | if (err) { |
6c17675e JM |
135 | dput(dentry); |
136 | dentry = ERR_PTR(err); | |
9b7f3755 | 137 | } |
bd4c625c | 138 | } |
bd4c625c | 139 | |
6c17675e JM |
140 | return dentry; |
141 | } | |
142 | ||
143 | static struct dentry *open_xa_root(struct super_block *sb, int flags) | |
144 | { | |
145 | struct dentry *privroot = REISERFS_SB(sb)->priv_root; | |
146 | if (!privroot) | |
147 | return ERR_PTR(-ENODATA); | |
148 | return lookup_or_create_dir(privroot, XAROOT_NAME, flags); | |
1da177e4 LT |
149 | } |
150 | ||
bd4c625c | 151 | static struct dentry *open_xa_dir(const struct inode *inode, int flags) |
1da177e4 | 152 | { |
bd4c625c LT |
153 | struct dentry *xaroot, *xadir; |
154 | char namebuf[17]; | |
155 | ||
6c17675e | 156 | xaroot = open_xa_root(inode->i_sb, flags); |
9b7f3755 | 157 | if (IS_ERR(xaroot)) |
bd4c625c | 158 | return xaroot; |
bd4c625c | 159 | |
bd4c625c LT |
160 | snprintf(namebuf, sizeof(namebuf), "%X.%X", |
161 | le32_to_cpu(INODE_PKEY(inode)->k_objectid), | |
162 | inode->i_generation); | |
bd4c625c | 163 | |
6c17675e | 164 | xadir = lookup_or_create_dir(xaroot, namebuf, flags); |
bd4c625c LT |
165 | dput(xaroot); |
166 | return xadir; | |
6c17675e | 167 | |
1da177e4 LT |
168 | } |
169 | ||
1da177e4 LT |
170 | /* |
171 | * this is very similar to fs/reiserfs/dir.c:reiserfs_readdir, but | |
172 | * we need to drop the path before calling the filldir struct. That | |
173 | * would be a big performance hit to the non-xattr case, so I've copied | |
174 | * the whole thing for now. --clm | |
175 | * | |
176 | * the big difference is that I go backwards through the directory, | |
177 | * and don't mess with f->f_pos, but the idea is the same. Do some | |
178 | * action on each and every entry in the directory. | |
179 | * | |
1b1dcc1b | 180 | * we're called with i_mutex held, so there are no worries about the directory |
1da177e4 LT |
181 | * changing underneath us. |
182 | */ | |
3227e14c | 183 | static int __xattr_readdir(struct inode *inode, void *dirent, filldir_t filldir) |
1da177e4 | 184 | { |
bd4c625c LT |
185 | struct cpu_key pos_key; /* key of current position in the directory (key of directory entry) */ |
186 | INITIALIZE_PATH(path_to_entry); | |
187 | struct buffer_head *bh; | |
188 | int entry_num; | |
189 | struct item_head *ih, tmp_ih; | |
190 | int search_res; | |
191 | char *local_buf; | |
192 | loff_t next_pos; | |
193 | char small_buf[32]; /* avoid kmalloc if we can */ | |
194 | struct reiserfs_de_head *deh; | |
195 | int d_reclen; | |
196 | char *d_name; | |
197 | off_t d_off; | |
198 | ino_t d_ino; | |
199 | struct reiserfs_dir_entry de; | |
200 | ||
201 | /* form key for search the next directory entry using f_pos field of | |
202 | file structure */ | |
203 | next_pos = max_reiserfs_offset(inode); | |
204 | ||
205 | while (1) { | |
206 | research: | |
207 | if (next_pos <= DOT_DOT_OFFSET) | |
208 | break; | |
209 | make_cpu_key(&pos_key, inode, next_pos, TYPE_DIRENTRY, 3); | |
210 | ||
211 | search_res = | |
212 | search_by_entry_key(inode->i_sb, &pos_key, &path_to_entry, | |
213 | &de); | |
214 | if (search_res == IO_ERROR) { | |
215 | // FIXME: we could just skip part of directory which could | |
216 | // not be read | |
217 | pathrelse(&path_to_entry); | |
218 | return -EIO; | |
219 | } | |
1da177e4 | 220 | |
bd4c625c LT |
221 | if (search_res == NAME_NOT_FOUND) |
222 | de.de_entry_num--; | |
1da177e4 | 223 | |
bd4c625c LT |
224 | set_de_name_and_namelen(&de); |
225 | entry_num = de.de_entry_num; | |
226 | deh = &(de.de_deh[entry_num]); | |
1da177e4 | 227 | |
bd4c625c LT |
228 | bh = de.de_bh; |
229 | ih = de.de_ih; | |
1da177e4 | 230 | |
bd4c625c | 231 | if (!is_direntry_le_ih(ih)) { |
0030b645 JM |
232 | reiserfs_error(inode->i_sb, "jdm-20000", |
233 | "not direntry %h", ih); | |
bd4c625c LT |
234 | break; |
235 | } | |
236 | copy_item_head(&tmp_ih, ih); | |
1da177e4 | 237 | |
bd4c625c LT |
238 | /* we must have found item, that is item of this directory, */ |
239 | RFALSE(COMP_SHORT_KEYS(&(ih->ih_key), &pos_key), | |
240 | "vs-9000: found item %h does not match to dir we readdir %K", | |
241 | ih, &pos_key); | |
1da177e4 | 242 | |
bd4c625c LT |
243 | if (deh_offset(deh) <= DOT_DOT_OFFSET) { |
244 | break; | |
245 | } | |
1da177e4 | 246 | |
bd4c625c LT |
247 | /* look for the previous entry in the directory */ |
248 | next_pos = deh_offset(deh) - 1; | |
1da177e4 | 249 | |
bd4c625c LT |
250 | if (!de_visible(deh)) |
251 | /* it is hidden entry */ | |
252 | continue; | |
1da177e4 | 253 | |
bd4c625c LT |
254 | d_reclen = entry_length(bh, ih, entry_num); |
255 | d_name = B_I_DEH_ENTRY_FILE_NAME(bh, ih, deh); | |
256 | d_off = deh_offset(deh); | |
257 | d_ino = deh_objectid(deh); | |
1da177e4 | 258 | |
bd4c625c LT |
259 | if (!d_name[d_reclen - 1]) |
260 | d_reclen = strlen(d_name); | |
1da177e4 | 261 | |
bd4c625c LT |
262 | if (d_reclen > REISERFS_MAX_NAME(inode->i_sb->s_blocksize)) { |
263 | /* too big to send back to VFS */ | |
264 | continue; | |
265 | } | |
1da177e4 | 266 | |
bd4c625c LT |
267 | /* Ignore the .reiserfs_priv entry */ |
268 | if (reiserfs_xattrs(inode->i_sb) && | |
269 | !old_format_only(inode->i_sb) && | |
270 | deh_objectid(deh) == | |
271 | le32_to_cpu(INODE_PKEY | |
272 | (REISERFS_SB(inode->i_sb)->priv_root->d_inode)-> | |
273 | k_objectid)) | |
274 | continue; | |
275 | ||
276 | if (d_reclen <= 32) { | |
277 | local_buf = small_buf; | |
278 | } else { | |
d739b42b | 279 | local_buf = kmalloc(d_reclen, GFP_NOFS); |
bd4c625c LT |
280 | if (!local_buf) { |
281 | pathrelse(&path_to_entry); | |
282 | return -ENOMEM; | |
283 | } | |
284 | if (item_moved(&tmp_ih, &path_to_entry)) { | |
d739b42b | 285 | kfree(local_buf); |
bd4c625c LT |
286 | |
287 | /* sigh, must retry. Do this same offset again */ | |
288 | next_pos = d_off; | |
289 | goto research; | |
290 | } | |
291 | } | |
1da177e4 | 292 | |
bd4c625c LT |
293 | // Note, that we copy name to user space via temporary |
294 | // buffer (local_buf) because filldir will block if | |
295 | // user space buffer is swapped out. At that time | |
296 | // entry can move to somewhere else | |
297 | memcpy(local_buf, d_name, d_reclen); | |
1da177e4 | 298 | |
bd4c625c LT |
299 | /* the filldir function might need to start transactions, |
300 | * or do who knows what. Release the path now that we've | |
301 | * copied all the important stuff out of the deh | |
302 | */ | |
303 | pathrelse(&path_to_entry); | |
304 | ||
305 | if (filldir(dirent, local_buf, d_reclen, d_off, d_ino, | |
306 | DT_UNKNOWN) < 0) { | |
307 | if (local_buf != small_buf) { | |
d739b42b | 308 | kfree(local_buf); |
bd4c625c LT |
309 | } |
310 | goto end; | |
311 | } | |
312 | if (local_buf != small_buf) { | |
d739b42b | 313 | kfree(local_buf); |
bd4c625c LT |
314 | } |
315 | } /* while */ | |
1da177e4 | 316 | |
bd4c625c LT |
317 | end: |
318 | pathrelse(&path_to_entry); | |
319 | return 0; | |
1da177e4 LT |
320 | } |
321 | ||
322 | /* | |
323 | * this could be done with dedicated readdir ops for the xattr files, | |
324 | * but I want to get something working asap | |
325 | * this is stolen from vfs_readdir | |
326 | * | |
327 | */ | |
328 | static | |
3227e14c | 329 | int xattr_readdir(struct inode *inode, filldir_t filler, void *buf) |
1da177e4 | 330 | { |
3227e14c | 331 | int res = -ENOENT; |
bd4c625c LT |
332 | if (!IS_DEADDIR(inode)) { |
333 | lock_kernel(); | |
3227e14c | 334 | res = __xattr_readdir(inode, buf, filler); |
bd4c625c LT |
335 | unlock_kernel(); |
336 | } | |
bd4c625c | 337 | return res; |
1da177e4 LT |
338 | } |
339 | ||
48b32a35 JM |
340 | /* The following are side effects of other operations that aren't explicitly |
341 | * modifying extended attributes. This includes operations such as permissions | |
342 | * or ownership changes, object deletions, etc. */ | |
343 | ||
a72bdb1c | 344 | static int |
48b32a35 JM |
345 | reiserfs_delete_xattrs_filler(void *buf, const char *name, int namelen, |
346 | loff_t offset, u64 ino, unsigned int d_type) | |
a72bdb1c | 347 | { |
48b32a35 | 348 | struct dentry *xadir = (struct dentry *)buf; |
a72bdb1c | 349 | struct dentry *dentry; |
a72bdb1c JM |
350 | int err = 0; |
351 | ||
352 | dentry = lookup_one_len(name, xadir, namelen); | |
353 | if (IS_ERR(dentry)) { | |
354 | err = PTR_ERR(dentry); | |
355 | goto out; | |
356 | } else if (!dentry->d_inode) { | |
357 | err = -ENODATA; | |
358 | goto out_file; | |
359 | } | |
360 | ||
361 | /* Skip directories.. */ | |
362 | if (S_ISDIR(dentry->d_inode->i_mode)) | |
363 | goto out_file; | |
364 | ||
48b32a35 | 365 | err = xattr_unlink(xadir->d_inode, dentry); |
a72bdb1c JM |
366 | |
367 | out_file: | |
368 | dput(dentry); | |
369 | ||
370 | out: | |
371 | return err; | |
372 | } | |
373 | ||
a72bdb1c JM |
374 | /* This is called w/ inode->i_mutex downed */ |
375 | int reiserfs_delete_xattrs(struct inode *inode) | |
376 | { | |
8b6dd72a | 377 | int err = -ENODATA; |
d984561b JM |
378 | struct dentry *dir, *root; |
379 | struct reiserfs_transaction_handle th; | |
380 | int blocks = JOURNAL_PER_BALANCE_CNT * 2 + 2 + | |
381 | 4 * REISERFS_QUOTA_TRANS_BLOCKS(inode->i_sb); | |
a72bdb1c JM |
382 | |
383 | /* Skip out, an xattr has no xattrs associated with it */ | |
384 | if (IS_PRIVATE(inode) || get_inode_sd_version(inode) == STAT_DATA_V1) | |
385 | return 0; | |
386 | ||
6c17675e | 387 | dir = open_xa_dir(inode, XATTR_REPLACE); |
a72bdb1c JM |
388 | if (IS_ERR(dir)) { |
389 | err = PTR_ERR(dir); | |
390 | goto out; | |
391 | } else if (!dir->d_inode) { | |
392 | dput(dir); | |
8b6dd72a | 393 | goto out; |
a72bdb1c JM |
394 | } |
395 | ||
6c17675e | 396 | mutex_lock_nested(&dir->d_inode->i_mutex, I_MUTEX_XATTR); |
a72bdb1c | 397 | err = xattr_readdir(dir->d_inode, reiserfs_delete_xattrs_filler, dir); |
6c17675e | 398 | mutex_unlock(&dir->d_inode->i_mutex); |
8b6dd72a JM |
399 | if (err) { |
400 | dput(dir); | |
401 | goto out; | |
402 | } | |
403 | ||
404 | root = dget(dir->d_parent); | |
405 | dput(dir); | |
a72bdb1c | 406 | |
d984561b JM |
407 | /* We start a transaction here to avoid a ABBA situation |
408 | * between the xattr root's i_mutex and the journal lock. | |
409 | * Inode creation will inherit an ACL, which requires a | |
410 | * lookup. The lookup locks the xattr root i_mutex with a | |
411 | * transaction open. Inode deletion takes teh xattr root | |
412 | * i_mutex to delete the directory and then starts a | |
413 | * transaction inside it. Boom. This doesn't incur much | |
414 | * additional overhead since the reiserfs_rmdir transaction | |
415 | * will just nest inside the outer transaction. */ | |
416 | err = journal_begin(&th, inode->i_sb, blocks); | |
417 | if (!err) { | |
418 | int jerror; | |
6c17675e JM |
419 | mutex_lock_nested(&root->d_inode->i_mutex, I_MUTEX_XATTR); |
420 | err = xattr_rmdir(root->d_inode, dir); | |
d984561b | 421 | jerror = journal_end(&th, inode->i_sb, blocks); |
6c17675e | 422 | mutex_unlock(&root->d_inode->i_mutex); |
d984561b | 423 | err = jerror ?: err; |
a72bdb1c | 424 | } |
a72bdb1c | 425 | |
8b6dd72a | 426 | dput(root); |
a72bdb1c | 427 | out: |
8ecbe550 | 428 | if (err) |
d984561b JM |
429 | reiserfs_warning(inode->i_sb, "jdm-20004", |
430 | "Couldn't remove all xattrs (%d)\n", err); | |
a72bdb1c JM |
431 | return err; |
432 | } | |
433 | ||
434 | struct reiserfs_chown_buf { | |
435 | struct inode *inode; | |
436 | struct dentry *xadir; | |
437 | struct iattr *attrs; | |
438 | }; | |
439 | ||
440 | /* XXX: If there is a better way to do this, I'd love to hear about it */ | |
441 | static int | |
442 | reiserfs_chown_xattrs_filler(void *buf, const char *name, int namelen, | |
443 | loff_t offset, u64 ino, unsigned int d_type) | |
444 | { | |
445 | struct reiserfs_chown_buf *chown_buf = (struct reiserfs_chown_buf *)buf; | |
446 | struct dentry *xafile, *xadir = chown_buf->xadir; | |
447 | struct iattr *attrs = chown_buf->attrs; | |
448 | int err = 0; | |
449 | ||
450 | xafile = lookup_one_len(name, xadir, namelen); | |
451 | if (IS_ERR(xafile)) | |
452 | return PTR_ERR(xafile); | |
453 | else if (!xafile->d_inode) { | |
454 | dput(xafile); | |
455 | return -ENODATA; | |
456 | } | |
457 | ||
6c17675e JM |
458 | if (!S_ISDIR(xafile->d_inode->i_mode)) { |
459 | mutex_lock_nested(&xafile->d_inode->i_mutex, I_MUTEX_CHILD); | |
8b6dd72a | 460 | err = reiserfs_setattr(xafile, attrs); |
6c17675e JM |
461 | mutex_unlock(&xafile->d_inode->i_mutex); |
462 | } | |
a72bdb1c JM |
463 | dput(xafile); |
464 | ||
465 | return err; | |
466 | } | |
467 | ||
468 | int reiserfs_chown_xattrs(struct inode *inode, struct iattr *attrs) | |
469 | { | |
470 | struct dentry *dir; | |
471 | int err = 0; | |
472 | struct reiserfs_chown_buf buf; | |
473 | unsigned int ia_valid = attrs->ia_valid; | |
474 | ||
475 | /* Skip out, an xattr has no xattrs associated with it */ | |
476 | if (IS_PRIVATE(inode) || get_inode_sd_version(inode) == STAT_DATA_V1) | |
477 | return 0; | |
478 | ||
6c17675e | 479 | dir = open_xa_dir(inode, XATTR_REPLACE); |
a72bdb1c JM |
480 | if (IS_ERR(dir)) { |
481 | if (PTR_ERR(dir) != -ENODATA) | |
482 | err = PTR_ERR(dir); | |
483 | goto out; | |
6c17675e JM |
484 | } else if (!dir->d_inode) |
485 | goto out_dir; | |
a72bdb1c JM |
486 | |
487 | attrs->ia_valid &= (ATTR_UID | ATTR_GID | ATTR_CTIME); | |
488 | buf.xadir = dir; | |
489 | buf.attrs = attrs; | |
490 | buf.inode = inode; | |
491 | ||
6c17675e | 492 | mutex_lock_nested(&dir->d_inode->i_mutex, I_MUTEX_XATTR); |
a72bdb1c | 493 | err = xattr_readdir(dir->d_inode, reiserfs_chown_xattrs_filler, &buf); |
a72bdb1c | 494 | |
6c17675e | 495 | if (!err) |
8b6dd72a | 496 | err = reiserfs_setattr(dir, attrs); |
6c17675e | 497 | mutex_unlock(&dir->d_inode->i_mutex); |
a72bdb1c | 498 | |
6c17675e | 499 | attrs->ia_valid = ia_valid; |
a72bdb1c JM |
500 | out_dir: |
501 | dput(dir); | |
a72bdb1c | 502 | out: |
8b6dd72a JM |
503 | if (err) |
504 | reiserfs_warning(inode->i_sb, "jdm-20007", | |
505 | "Couldn't chown all xattrs (%d)\n", err); | |
a72bdb1c JM |
506 | return err; |
507 | } | |
508 | ||
509 | #ifdef CONFIG_REISERFS_FS_XATTR | |
a72bdb1c JM |
510 | /* Returns a dentry corresponding to a specific extended attribute file |
511 | * for the inode. If flags allow, the file is created. Otherwise, a | |
512 | * valid or negative dentry, or an error is returned. */ | |
48b32a35 JM |
513 | static struct dentry *xattr_lookup(struct inode *inode, const char *name, |
514 | int flags) | |
a72bdb1c JM |
515 | { |
516 | struct dentry *xadir, *xafile; | |
517 | int err = 0; | |
518 | ||
519 | xadir = open_xa_dir(inode, flags); | |
6c17675e | 520 | if (IS_ERR(xadir)) |
a72bdb1c | 521 | return ERR_CAST(xadir); |
a72bdb1c JM |
522 | |
523 | xafile = lookup_one_len(name, xadir, strlen(name)); | |
524 | if (IS_ERR(xafile)) { | |
6c17675e JM |
525 | err = PTR_ERR(xafile); |
526 | goto out; | |
a72bdb1c JM |
527 | } |
528 | ||
6c17675e JM |
529 | if (xafile->d_inode && (flags & XATTR_CREATE)) |
530 | err = -EEXIST; | |
a72bdb1c | 531 | |
6c17675e JM |
532 | if (!xafile->d_inode) { |
533 | err = -ENODATA; | |
534 | if (xattr_may_create(flags)) { | |
535 | mutex_lock_nested(&xadir->d_inode->i_mutex, | |
536 | I_MUTEX_XATTR); | |
537 | err = xattr_create(xadir->d_inode, xafile, | |
538 | 0700|S_IFREG); | |
539 | mutex_unlock(&xadir->d_inode->i_mutex); | |
a72bdb1c JM |
540 | } |
541 | } | |
542 | ||
6c17675e JM |
543 | if (err) |
544 | dput(xafile); | |
a72bdb1c JM |
545 | out: |
546 | dput(xadir); | |
547 | if (err) | |
6c17675e | 548 | return ERR_PTR(err); |
a72bdb1c JM |
549 | return xafile; |
550 | } | |
551 | ||
1da177e4 | 552 | /* Internal operations on file data */ |
bd4c625c | 553 | static inline void reiserfs_put_page(struct page *page) |
1da177e4 | 554 | { |
bd4c625c LT |
555 | kunmap(page); |
556 | page_cache_release(page); | |
1da177e4 LT |
557 | } |
558 | ||
ec6ea56b | 559 | static struct page *reiserfs_get_page(struct inode *dir, size_t n) |
1da177e4 | 560 | { |
bd4c625c LT |
561 | struct address_space *mapping = dir->i_mapping; |
562 | struct page *page; | |
563 | /* We can deadlock if we try to free dentries, | |
564 | and an unlink/rmdir has just occured - GFP_NOFS avoids this */ | |
c4cdd038 | 565 | mapping_set_gfp_mask(mapping, GFP_NOFS); |
ec6ea56b | 566 | page = read_mapping_page(mapping, n >> PAGE_CACHE_SHIFT, NULL); |
bd4c625c | 567 | if (!IS_ERR(page)) { |
bd4c625c | 568 | kmap(page); |
bd4c625c LT |
569 | if (PageError(page)) |
570 | goto fail; | |
571 | } | |
572 | return page; | |
573 | ||
574 | fail: | |
575 | reiserfs_put_page(page); | |
576 | return ERR_PTR(-EIO); | |
1da177e4 LT |
577 | } |
578 | ||
bd4c625c | 579 | static inline __u32 xattr_hash(const char *msg, int len) |
1da177e4 | 580 | { |
bd4c625c | 581 | return csum_partial(msg, len, 0); |
1da177e4 LT |
582 | } |
583 | ||
ba9d8cec VS |
584 | int reiserfs_commit_write(struct file *f, struct page *page, |
585 | unsigned from, unsigned to); | |
586 | int reiserfs_prepare_write(struct file *f, struct page *page, | |
587 | unsigned from, unsigned to); | |
588 | ||
48b32a35 JM |
589 | static void update_ctime(struct inode *inode) |
590 | { | |
591 | struct timespec now = current_fs_time(inode->i_sb); | |
592 | if (hlist_unhashed(&inode->i_hash) || !inode->i_nlink || | |
593 | timespec_equal(&inode->i_ctime, &now)) | |
594 | return; | |
595 | ||
596 | inode->i_ctime = CURRENT_TIME_SEC; | |
597 | mark_inode_dirty(inode); | |
598 | } | |
599 | ||
600 | static int lookup_and_delete_xattr(struct inode *inode, const char *name) | |
601 | { | |
602 | int err = 0; | |
603 | struct dentry *dentry, *xadir; | |
604 | ||
605 | xadir = open_xa_dir(inode, XATTR_REPLACE); | |
606 | if (IS_ERR(xadir)) | |
607 | return PTR_ERR(xadir); | |
608 | ||
609 | dentry = lookup_one_len(name, xadir, strlen(name)); | |
610 | if (IS_ERR(dentry)) { | |
611 | err = PTR_ERR(dentry); | |
612 | goto out_dput; | |
613 | } | |
614 | ||
615 | if (dentry->d_inode) { | |
616 | mutex_lock_nested(&xadir->d_inode->i_mutex, I_MUTEX_XATTR); | |
617 | err = xattr_unlink(xadir->d_inode, dentry); | |
618 | mutex_unlock(&xadir->d_inode->i_mutex); | |
619 | update_ctime(inode); | |
620 | } | |
621 | ||
622 | dput(dentry); | |
623 | out_dput: | |
624 | dput(xadir); | |
625 | return err; | |
626 | } | |
627 | ||
ba9d8cec | 628 | |
1da177e4 LT |
629 | /* Generic extended attribute operations that can be used by xa plugins */ |
630 | ||
631 | /* | |
1b1dcc1b | 632 | * inode->i_mutex: down |
1da177e4 LT |
633 | */ |
634 | int | |
0ab2621e JM |
635 | reiserfs_xattr_set_handle(struct reiserfs_transaction_handle *th, |
636 | struct inode *inode, const char *name, | |
637 | const void *buffer, size_t buffer_size, int flags) | |
1da177e4 | 638 | { |
bd4c625c | 639 | int err = 0; |
3227e14c | 640 | struct dentry *dentry; |
bd4c625c LT |
641 | struct page *page; |
642 | char *data; | |
bd4c625c LT |
643 | size_t file_pos = 0; |
644 | size_t buffer_pos = 0; | |
48b32a35 | 645 | size_t new_size; |
bd4c625c LT |
646 | __u32 xahash = 0; |
647 | ||
bd4c625c LT |
648 | if (get_inode_sd_version(inode) == STAT_DATA_V1) |
649 | return -EOPNOTSUPP; | |
650 | ||
8b6dd72a | 651 | if (!buffer) |
48b32a35 | 652 | return lookup_and_delete_xattr(inode, name); |
bd4c625c | 653 | |
48b32a35 JM |
654 | dentry = xattr_lookup(inode, name, flags); |
655 | if (IS_ERR(dentry)) | |
656 | return PTR_ERR(dentry); | |
bd4c625c | 657 | |
8b6dd72a JM |
658 | down_write(&REISERFS_I(inode)->i_xattr_sem); |
659 | ||
660 | xahash = xattr_hash(buffer, buffer_size); | |
bd4c625c LT |
661 | while (buffer_pos < buffer_size || buffer_pos == 0) { |
662 | size_t chunk; | |
663 | size_t skip = 0; | |
664 | size_t page_offset = (file_pos & (PAGE_CACHE_SIZE - 1)); | |
665 | if (buffer_size - buffer_pos > PAGE_CACHE_SIZE) | |
666 | chunk = PAGE_CACHE_SIZE; | |
667 | else | |
668 | chunk = buffer_size - buffer_pos; | |
669 | ||
ec6ea56b | 670 | page = reiserfs_get_page(dentry->d_inode, file_pos); |
bd4c625c LT |
671 | if (IS_ERR(page)) { |
672 | err = PTR_ERR(page); | |
48b32a35 | 673 | goto out_unlock; |
bd4c625c LT |
674 | } |
675 | ||
676 | lock_page(page); | |
677 | data = page_address(page); | |
678 | ||
679 | if (file_pos == 0) { | |
680 | struct reiserfs_xattr_header *rxh; | |
681 | skip = file_pos = sizeof(struct reiserfs_xattr_header); | |
682 | if (chunk + skip > PAGE_CACHE_SIZE) | |
683 | chunk = PAGE_CACHE_SIZE - skip; | |
684 | rxh = (struct reiserfs_xattr_header *)data; | |
685 | rxh->h_magic = cpu_to_le32(REISERFS_XATTR_MAGIC); | |
686 | rxh->h_hash = cpu_to_le32(xahash); | |
687 | } | |
688 | ||
3227e14c | 689 | err = reiserfs_prepare_write(NULL, page, page_offset, |
ba9d8cec | 690 | page_offset + chunk + skip); |
bd4c625c LT |
691 | if (!err) { |
692 | if (buffer) | |
693 | memcpy(data + skip, buffer + buffer_pos, chunk); | |
3227e14c JM |
694 | err = reiserfs_commit_write(NULL, page, page_offset, |
695 | page_offset + chunk + | |
696 | skip); | |
bd4c625c LT |
697 | } |
698 | unlock_page(page); | |
699 | reiserfs_put_page(page); | |
700 | buffer_pos += chunk; | |
701 | file_pos += chunk; | |
702 | skip = 0; | |
703 | if (err || buffer_size == 0 || !buffer) | |
704 | break; | |
705 | } | |
706 | ||
48b32a35 JM |
707 | new_size = buffer_size + sizeof(struct reiserfs_xattr_header); |
708 | if (!err && new_size < i_size_read(dentry->d_inode)) { | |
709 | struct iattr newattrs = { | |
710 | .ia_ctime = current_fs_time(inode->i_sb), | |
711 | .ia_size = buffer_size, | |
712 | .ia_valid = ATTR_SIZE | ATTR_CTIME, | |
713 | }; | |
714 | mutex_lock_nested(&dentry->d_inode->i_mutex, I_MUTEX_XATTR); | |
715 | down_write(&dentry->d_inode->i_alloc_sem); | |
716 | err = reiserfs_setattr(dentry, &newattrs); | |
717 | up_write(&dentry->d_inode->i_alloc_sem); | |
718 | mutex_unlock(&dentry->d_inode->i_mutex); | |
719 | } else | |
720 | update_ctime(inode); | |
721 | out_unlock: | |
8b6dd72a | 722 | up_write(&REISERFS_I(inode)->i_xattr_sem); |
3227e14c | 723 | dput(dentry); |
48b32a35 JM |
724 | return err; |
725 | } | |
bd4c625c | 726 | |
0ab2621e JM |
727 | /* We need to start a transaction to maintain lock ordering */ |
728 | int reiserfs_xattr_set(struct inode *inode, const char *name, | |
729 | const void *buffer, size_t buffer_size, int flags) | |
48b32a35 | 730 | { |
0ab2621e JM |
731 | |
732 | struct reiserfs_transaction_handle th; | |
733 | int error, error2; | |
734 | size_t jbegin_count = reiserfs_xattr_nblocks(inode, buffer_size); | |
735 | ||
736 | if (!(flags & XATTR_REPLACE)) | |
737 | jbegin_count += reiserfs_xattr_jcreate_nblocks(inode); | |
738 | ||
739 | reiserfs_write_lock(inode->i_sb); | |
740 | error = journal_begin(&th, inode->i_sb, jbegin_count); | |
741 | if (error) { | |
742 | reiserfs_write_unlock(inode->i_sb); | |
743 | return error; | |
744 | } | |
745 | ||
746 | error = reiserfs_xattr_set_handle(&th, inode, name, | |
747 | buffer, buffer_size, flags); | |
748 | ||
749 | error2 = journal_end(&th, inode->i_sb, jbegin_count); | |
750 | if (error == 0) | |
751 | error = error2; | |
752 | reiserfs_write_unlock(inode->i_sb); | |
753 | ||
754 | return error; | |
1da177e4 LT |
755 | } |
756 | ||
757 | /* | |
1b1dcc1b | 758 | * inode->i_mutex: down |
1da177e4 LT |
759 | */ |
760 | int | |
48b32a35 | 761 | reiserfs_xattr_get(struct inode *inode, const char *name, void *buffer, |
bd4c625c | 762 | size_t buffer_size) |
1da177e4 | 763 | { |
bd4c625c | 764 | ssize_t err = 0; |
3227e14c | 765 | struct dentry *dentry; |
bd4c625c LT |
766 | size_t isize; |
767 | size_t file_pos = 0; | |
768 | size_t buffer_pos = 0; | |
769 | struct page *page; | |
bd4c625c LT |
770 | __u32 hash = 0; |
771 | ||
772 | if (name == NULL) | |
773 | return -EINVAL; | |
774 | ||
775 | /* We can't have xattrs attached to v1 items since they don't have | |
776 | * generation numbers */ | |
777 | if (get_inode_sd_version(inode) == STAT_DATA_V1) | |
778 | return -EOPNOTSUPP; | |
779 | ||
48b32a35 | 780 | dentry = xattr_lookup(inode, name, XATTR_REPLACE); |
3227e14c JM |
781 | if (IS_ERR(dentry)) { |
782 | err = PTR_ERR(dentry); | |
bd4c625c LT |
783 | goto out; |
784 | } | |
785 | ||
8b6dd72a | 786 | down_read(&REISERFS_I(inode)->i_xattr_sem); |
d984561b | 787 | |
f437c529 | 788 | isize = i_size_read(dentry->d_inode); |
bd4c625c LT |
789 | |
790 | /* Just return the size needed */ | |
791 | if (buffer == NULL) { | |
792 | err = isize - sizeof(struct reiserfs_xattr_header); | |
8b6dd72a | 793 | goto out_unlock; |
bd4c625c LT |
794 | } |
795 | ||
796 | if (buffer_size < isize - sizeof(struct reiserfs_xattr_header)) { | |
797 | err = -ERANGE; | |
8b6dd72a | 798 | goto out_unlock; |
bd4c625c LT |
799 | } |
800 | ||
a72bdb1c JM |
801 | while (file_pos < isize) { |
802 | size_t chunk; | |
803 | char *data; | |
804 | size_t skip = 0; | |
805 | if (isize - file_pos > PAGE_CACHE_SIZE) | |
806 | chunk = PAGE_CACHE_SIZE; | |
807 | else | |
808 | chunk = isize - file_pos; | |
1da177e4 | 809 | |
a72bdb1c JM |
810 | page = reiserfs_get_page(dentry->d_inode, file_pos); |
811 | if (IS_ERR(page)) { | |
812 | err = PTR_ERR(page); | |
8b6dd72a | 813 | goto out_unlock; |
a72bdb1c | 814 | } |
1da177e4 | 815 | |
a72bdb1c JM |
816 | lock_page(page); |
817 | data = page_address(page); | |
818 | if (file_pos == 0) { | |
819 | struct reiserfs_xattr_header *rxh = | |
820 | (struct reiserfs_xattr_header *)data; | |
821 | skip = file_pos = sizeof(struct reiserfs_xattr_header); | |
822 | chunk -= skip; | |
823 | /* Magic doesn't match up.. */ | |
824 | if (rxh->h_magic != cpu_to_le32(REISERFS_XATTR_MAGIC)) { | |
825 | unlock_page(page); | |
826 | reiserfs_put_page(page); | |
827 | reiserfs_warning(inode->i_sb, "jdm-20001", | |
828 | "Invalid magic for xattr (%s) " | |
829 | "associated with %k", name, | |
830 | INODE_PKEY(inode)); | |
831 | err = -EIO; | |
8b6dd72a | 832 | goto out_unlock; |
a72bdb1c JM |
833 | } |
834 | hash = le32_to_cpu(rxh->h_hash); | |
835 | } | |
836 | memcpy(buffer + buffer_pos, data + skip, chunk); | |
837 | unlock_page(page); | |
838 | reiserfs_put_page(page); | |
839 | file_pos += chunk; | |
840 | buffer_pos += chunk; | |
841 | skip = 0; | |
842 | } | |
843 | err = isize - sizeof(struct reiserfs_xattr_header); | |
bd4c625c | 844 | |
a72bdb1c JM |
845 | if (xattr_hash(buffer, isize - sizeof(struct reiserfs_xattr_header)) != |
846 | hash) { | |
847 | reiserfs_warning(inode->i_sb, "jdm-20002", | |
848 | "Invalid hash for xattr (%s) associated " | |
849 | "with %k", name, INODE_PKEY(inode)); | |
850 | err = -EIO; | |
bd4c625c LT |
851 | } |
852 | ||
8b6dd72a JM |
853 | out_unlock: |
854 | up_read(&REISERFS_I(inode)->i_xattr_sem); | |
a72bdb1c | 855 | dput(dentry); |
bd4c625c | 856 | |
a72bdb1c | 857 | out: |
bd4c625c | 858 | return err; |
1da177e4 LT |
859 | } |
860 | ||
48b32a35 JM |
861 | /* Actual operations that are exported to VFS-land */ |
862 | struct xattr_handler *reiserfs_xattr_handlers[] = { | |
863 | &reiserfs_xattr_user_handler, | |
864 | &reiserfs_xattr_trusted_handler, | |
865 | #ifdef CONFIG_REISERFS_FS_SECURITY | |
866 | &reiserfs_xattr_security_handler, | |
867 | #endif | |
868 | #ifdef CONFIG_REISERFS_FS_POSIX_ACL | |
869 | &reiserfs_posix_acl_access_handler, | |
870 | &reiserfs_posix_acl_default_handler, | |
871 | #endif | |
872 | NULL | |
873 | }; | |
bd4c625c | 874 | |
48b32a35 JM |
875 | /* |
876 | * In order to implement different sets of xattr operations for each xattr | |
877 | * prefix with the generic xattr API, a filesystem should create a | |
878 | * null-terminated array of struct xattr_handler (one for each prefix) and | |
879 | * hang a pointer to it off of the s_xattr field of the superblock. | |
880 | * | |
881 | * The generic_fooxattr() functions will use this list to dispatch xattr | |
882 | * operations to the correct xattr_handler. | |
883 | */ | |
884 | #define for_each_xattr_handler(handlers, handler) \ | |
885 | for ((handler) = *(handlers)++; \ | |
886 | (handler) != NULL; \ | |
887 | (handler) = *(handlers)++) | |
bd4c625c | 888 | |
48b32a35 JM |
889 | /* This is the implementation for the xattr plugin infrastructure */ |
890 | static inline struct xattr_handler * | |
891 | find_xattr_handler_prefix(struct xattr_handler **handlers, | |
892 | const char *name) | |
893 | { | |
894 | struct xattr_handler *xah; | |
bd4c625c | 895 | |
48b32a35 JM |
896 | if (!handlers) |
897 | return NULL; | |
898 | ||
899 | for_each_xattr_handler(handlers, xah) { | |
900 | if (strncmp(xah->prefix, name, strlen(xah->prefix)) == 0) | |
901 | break; | |
bd4c625c LT |
902 | } |
903 | ||
48b32a35 | 904 | return xah; |
bd4c625c | 905 | } |
1da177e4 | 906 | |
48b32a35 | 907 | |
1da177e4 LT |
908 | /* |
909 | * Inode operation getxattr() | |
1da177e4 LT |
910 | */ |
911 | ssize_t | |
bd4c625c LT |
912 | reiserfs_getxattr(struct dentry * dentry, const char *name, void *buffer, |
913 | size_t size) | |
1da177e4 | 914 | { |
48b32a35 JM |
915 | struct inode *inode = dentry->d_inode; |
916 | struct xattr_handler *handler; | |
bd4c625c | 917 | |
48b32a35 JM |
918 | handler = find_xattr_handler_prefix(inode->i_sb->s_xattr, name); |
919 | ||
920 | if (!handler || get_inode_sd_version(inode) == STAT_DATA_V1) | |
bd4c625c LT |
921 | return -EOPNOTSUPP; |
922 | ||
48b32a35 | 923 | return handler->get(inode, name, buffer, size); |
1da177e4 LT |
924 | } |
925 | ||
1da177e4 LT |
926 | /* |
927 | * Inode operation setxattr() | |
928 | * | |
1b1dcc1b | 929 | * dentry->d_inode->i_mutex down |
1da177e4 LT |
930 | */ |
931 | int | |
bd4c625c LT |
932 | reiserfs_setxattr(struct dentry *dentry, const char *name, const void *value, |
933 | size_t size, int flags) | |
1da177e4 | 934 | { |
48b32a35 JM |
935 | struct inode *inode = dentry->d_inode; |
936 | struct xattr_handler *handler; | |
bd4c625c | 937 | |
48b32a35 JM |
938 | handler = find_xattr_handler_prefix(inode->i_sb->s_xattr, name); |
939 | ||
940 | if (!handler || get_inode_sd_version(inode) == STAT_DATA_V1) | |
bd4c625c LT |
941 | return -EOPNOTSUPP; |
942 | ||
48b32a35 | 943 | return handler->set(inode, name, value, size, flags); |
1da177e4 LT |
944 | } |
945 | ||
946 | /* | |
947 | * Inode operation removexattr() | |
948 | * | |
1b1dcc1b | 949 | * dentry->d_inode->i_mutex down |
1da177e4 | 950 | */ |
bd4c625c | 951 | int reiserfs_removexattr(struct dentry *dentry, const char *name) |
1da177e4 | 952 | { |
48b32a35 JM |
953 | struct inode *inode = dentry->d_inode; |
954 | struct xattr_handler *handler; | |
955 | handler = find_xattr_handler_prefix(inode->i_sb->s_xattr, name); | |
1da177e4 | 956 | |
48b32a35 | 957 | if (!handler || get_inode_sd_version(inode) == STAT_DATA_V1) |
bd4c625c | 958 | return -EOPNOTSUPP; |
1da177e4 | 959 | |
48b32a35 | 960 | return handler->set(inode, name, NULL, 0, XATTR_REPLACE); |
1da177e4 LT |
961 | } |
962 | ||
48b32a35 JM |
963 | struct listxattr_buf { |
964 | size_t size; | |
965 | size_t pos; | |
966 | char *buf; | |
967 | struct inode *inode; | |
1da177e4 LT |
968 | }; |
969 | ||
48b32a35 JM |
970 | static int listxattr_filler(void *buf, const char *name, int namelen, |
971 | loff_t offset, u64 ino, unsigned int d_type) | |
1da177e4 | 972 | { |
48b32a35 JM |
973 | struct listxattr_buf *b = (struct listxattr_buf *)buf; |
974 | size_t size; | |
975 | if (name[0] != '.' || | |
976 | (namelen != 1 && (name[1] != '.' || namelen != 2))) { | |
977 | struct xattr_handler *handler; | |
978 | handler = find_xattr_handler_prefix(b->inode->i_sb->s_xattr, | |
979 | name); | |
980 | if (!handler) /* Unsupported xattr name */ | |
981 | return 0; | |
982 | if (b->buf) { | |
983 | size = handler->list(b->inode, b->buf + b->pos, | |
984 | b->size, name, namelen); | |
985 | if (size > b->size) | |
986 | return -ERANGE; | |
987 | } else { | |
988 | size = handler->list(b->inode, NULL, 0, name, namelen); | |
bd4c625c | 989 | } |
bd4c625c | 990 | |
48b32a35 JM |
991 | b->pos += size; |
992 | } | |
bd4c625c | 993 | return 0; |
1da177e4 | 994 | } |
bd4c625c | 995 | |
1da177e4 LT |
996 | /* |
997 | * Inode operation listxattr() | |
48b32a35 JM |
998 | * |
999 | * We totally ignore the generic listxattr here because it would be stupid | |
1000 | * not to. Since the xattrs are organized in a directory, we can just | |
1001 | * readdir to find them. | |
1da177e4 | 1002 | */ |
bd4c625c | 1003 | ssize_t reiserfs_listxattr(struct dentry * dentry, char *buffer, size_t size) |
1da177e4 | 1004 | { |
bd4c625c LT |
1005 | struct dentry *dir; |
1006 | int err = 0; | |
48b32a35 JM |
1007 | struct listxattr_buf buf = { |
1008 | .inode = dentry->d_inode, | |
1009 | .buf = buffer, | |
1010 | .size = buffer ? size : 0, | |
1011 | }; | |
bd4c625c LT |
1012 | |
1013 | if (!dentry->d_inode) | |
1014 | return -EINVAL; | |
1015 | ||
1016 | if (!reiserfs_xattrs(dentry->d_sb) || | |
1017 | get_inode_sd_version(dentry->d_inode) == STAT_DATA_V1) | |
1018 | return -EOPNOTSUPP; | |
1019 | ||
6c17675e | 1020 | dir = open_xa_dir(dentry->d_inode, XATTR_REPLACE); |
bd4c625c LT |
1021 | if (IS_ERR(dir)) { |
1022 | err = PTR_ERR(dir); | |
1023 | if (err == -ENODATA) | |
48b32a35 | 1024 | err = 0; /* Not an error if there aren't any xattrs */ |
bd4c625c LT |
1025 | goto out; |
1026 | } | |
1027 | ||
6c17675e | 1028 | mutex_lock_nested(&dir->d_inode->i_mutex, I_MUTEX_XATTR); |
48b32a35 | 1029 | err = xattr_readdir(dir->d_inode, listxattr_filler, &buf); |
6c17675e | 1030 | mutex_unlock(&dir->d_inode->i_mutex); |
bd4c625c | 1031 | |
48b32a35 JM |
1032 | if (!err) |
1033 | err = buf.pos; | |
bd4c625c | 1034 | |
3227e14c | 1035 | dput(dir); |
8b6dd72a | 1036 | out: |
bd4c625c | 1037 | return err; |
1da177e4 LT |
1038 | } |
1039 | ||
a72bdb1c JM |
1040 | static int reiserfs_check_acl(struct inode *inode, int mask) |
1041 | { | |
1042 | struct posix_acl *acl; | |
1043 | int error = -EAGAIN; /* do regular unix permission checks by default */ | |
1044 | ||
a72bdb1c JM |
1045 | acl = reiserfs_get_acl(inode, ACL_TYPE_ACCESS); |
1046 | ||
a72bdb1c JM |
1047 | if (acl) { |
1048 | if (!IS_ERR(acl)) { | |
1049 | error = posix_acl_permission(inode, acl, mask); | |
1050 | posix_acl_release(acl); | |
1051 | } else if (PTR_ERR(acl) != -ENODATA) | |
1052 | error = PTR_ERR(acl); | |
1053 | } | |
1054 | ||
1055 | return error; | |
1056 | } | |
1057 | ||
1058 | int reiserfs_permission(struct inode *inode, int mask) | |
1059 | { | |
1060 | /* | |
1061 | * We don't do permission checks on the internal objects. | |
1062 | * Permissions are determined by the "owning" object. | |
1063 | */ | |
1064 | if (IS_PRIVATE(inode)) | |
1065 | return 0; | |
1066 | /* | |
1067 | * Stat data v1 doesn't support ACLs. | |
1068 | */ | |
1069 | if (get_inode_sd_version(inode) == STAT_DATA_V1) | |
1070 | return generic_permission(inode, mask, NULL); | |
1071 | else | |
1072 | return generic_permission(inode, mask, reiserfs_check_acl); | |
1073 | } | |
1074 | ||
1075 | static int create_privroot(struct dentry *dentry) | |
1076 | { | |
1077 | int err; | |
1078 | struct inode *inode = dentry->d_parent->d_inode; | |
1079 | mutex_lock_nested(&inode->i_mutex, I_MUTEX_XATTR); | |
6c17675e | 1080 | err = xattr_mkdir(inode, dentry, 0700); |
a72bdb1c JM |
1081 | mutex_unlock(&inode->i_mutex); |
1082 | if (err) { | |
1083 | dput(dentry); | |
1084 | dentry = NULL; | |
1085 | } | |
1086 | ||
1087 | if (dentry && dentry->d_inode) | |
1088 | reiserfs_info(dentry->d_sb, "Created %s - reserved for xattr " | |
1089 | "storage.\n", PRIVROOT_NAME); | |
1090 | ||
1091 | return err; | |
1092 | } | |
1093 | ||
1094 | static int xattr_mount_check(struct super_block *s) | |
1095 | { | |
1096 | /* We need generation numbers to ensure that the oid mapping is correct | |
1097 | * v3.5 filesystems don't have them. */ | |
48b32a35 JM |
1098 | if (old_format_only(s)) { |
1099 | if (reiserfs_xattrs_optional(s)) { | |
1100 | /* Old format filesystem, but optional xattrs have | |
1101 | * been enabled. Error out. */ | |
1102 | reiserfs_warning(s, "jdm-2005", | |
1103 | "xattrs/ACLs not supported " | |
1104 | "on pre-v3.6 format filesystems. " | |
1105 | "Failing mount."); | |
1106 | return -EOPNOTSUPP; | |
1107 | } | |
a72bdb1c JM |
1108 | } |
1109 | ||
1110 | return 0; | |
1111 | } | |
1112 | ||
1113 | #else | |
1114 | int __init reiserfs_xattr_register_handlers(void) { return 0; } | |
1115 | void reiserfs_xattr_unregister_handlers(void) {} | |
1116 | #endif | |
1117 | ||
1da177e4 LT |
1118 | /* This will catch lookups from the fs root to .reiserfs_priv */ |
1119 | static int | |
bd4c625c | 1120 | xattr_lookup_poison(struct dentry *dentry, struct qstr *q1, struct qstr *name) |
1da177e4 | 1121 | { |
bd4c625c LT |
1122 | struct dentry *priv_root = REISERFS_SB(dentry->d_sb)->priv_root; |
1123 | if (name->len == priv_root->d_name.len && | |
1124 | name->hash == priv_root->d_name.hash && | |
1125 | !memcmp(name->name, priv_root->d_name.name, name->len)) { | |
1126 | return -ENOENT; | |
1127 | } else if (q1->len == name->len && | |
1128 | !memcmp(q1->name, name->name, name->len)) | |
1129 | return 0; | |
1130 | return 1; | |
1da177e4 LT |
1131 | } |
1132 | ||
1133 | static struct dentry_operations xattr_lookup_poison_ops = { | |
bd4c625c | 1134 | .d_compare = xattr_lookup_poison, |
1da177e4 LT |
1135 | }; |
1136 | ||
1da177e4 LT |
1137 | /* We need to take a copy of the mount flags since things like |
1138 | * MS_RDONLY don't get set until *after* we're called. | |
1139 | * mount_flags != mount_options */ | |
bd4c625c | 1140 | int reiserfs_xattr_init(struct super_block *s, int mount_flags) |
1da177e4 | 1141 | { |
bd4c625c LT |
1142 | int err = 0; |
1143 | ||
a72bdb1c JM |
1144 | #ifdef CONFIG_REISERFS_FS_XATTR |
1145 | err = xattr_mount_check(s); | |
1146 | if (err) | |
bd4c625c | 1147 | goto error; |
a72bdb1c | 1148 | #endif |
bd4c625c LT |
1149 | |
1150 | /* If we don't have the privroot located yet - go find it */ | |
a72bdb1c | 1151 | if (!REISERFS_SB(s)->priv_root) { |
bd4c625c LT |
1152 | struct dentry *dentry; |
1153 | dentry = lookup_one_len(PRIVROOT_NAME, s->s_root, | |
1154 | strlen(PRIVROOT_NAME)); | |
1155 | if (!IS_ERR(dentry)) { | |
a72bdb1c JM |
1156 | #ifdef CONFIG_REISERFS_FS_XATTR |
1157 | if (!(mount_flags & MS_RDONLY) && !dentry->d_inode) | |
1158 | err = create_privroot(dentry); | |
1159 | #endif | |
1160 | if (!dentry->d_inode) { | |
bd4c625c LT |
1161 | dput(dentry); |
1162 | dentry = NULL; | |
1163 | } | |
1164 | } else | |
1165 | err = PTR_ERR(dentry); | |
1166 | ||
1167 | if (!err && dentry) { | |
1168 | s->s_root->d_op = &xattr_lookup_poison_ops; | |
6dfede69 | 1169 | dentry->d_inode->i_flags |= S_PRIVATE; |
bd4c625c | 1170 | REISERFS_SB(s)->priv_root = dentry; |
a72bdb1c JM |
1171 | #ifdef CONFIG_REISERFS_FS_XATTR |
1172 | /* xattrs are unavailable */ | |
1173 | } else if (!(mount_flags & MS_RDONLY)) { | |
1174 | /* If we're read-only it just means that the dir | |
1175 | * hasn't been created. Not an error -- just no | |
1176 | * xattrs on the fs. We'll check again if we | |
1177 | * go read-write */ | |
45b03d5e JM |
1178 | reiserfs_warning(s, "jdm-20006", |
1179 | "xattrs/ACLs enabled and couldn't " | |
1180 | "find/create .reiserfs_priv. " | |
1181 | "Failing mount."); | |
bd4c625c | 1182 | err = -EOPNOTSUPP; |
a72bdb1c | 1183 | #endif |
bd4c625c LT |
1184 | } |
1185 | } | |
1186 | ||
a72bdb1c | 1187 | #ifdef CONFIG_REISERFS_FS_XATTR |
48b32a35 JM |
1188 | if (!err) |
1189 | s->s_xattr = reiserfs_xattr_handlers; | |
1190 | ||
a72bdb1c | 1191 | error: |
bd4c625c | 1192 | if (err) { |
bd4c625c LT |
1193 | clear_bit(REISERFS_XATTRS_USER, &(REISERFS_SB(s)->s_mount_opt)); |
1194 | clear_bit(REISERFS_POSIXACL, &(REISERFS_SB(s)->s_mount_opt)); | |
1195 | } | |
a72bdb1c | 1196 | #endif |
bd4c625c LT |
1197 | |
1198 | /* The super_block MS_POSIXACL must mirror the (no)acl mount option. */ | |
1199 | s->s_flags = s->s_flags & ~MS_POSIXACL; | |
a72bdb1c | 1200 | #ifdef CONFIG_REISERFS_FS_POSIX_ACL |
bd4c625c LT |
1201 | if (reiserfs_posixacl(s)) |
1202 | s->s_flags |= MS_POSIXACL; | |
a72bdb1c | 1203 | #endif |
bd4c625c LT |
1204 | |
1205 | return err; | |
1da177e4 | 1206 | } |