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