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