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