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