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