1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * linux/fs/reiserfs/xattr.c
4  *
5  * Copyright (c) 2002 by Jeff Mahoney, <jeffm@suse.com>
6  *
7  */
8 
9 /*
10  * In order to implement EA/ACLs in a clean, backwards compatible manner,
11  * they are implemented as files in a "private" directory.
12  * Each EA is in it's own file, with the directory layout like so (/ is assumed
13  * to be relative to fs root). Inside the /.reiserfs_priv/xattrs directory,
14  * directories named using the capital-hex form of the objectid and
15  * generation number are used. Inside each directory are individual files
16  * named with the name of the extended attribute.
17  *
18  * So, for objectid 12648430, we could have:
19  * /.reiserfs_priv/xattrs/C0FFEE.0/system.posix_acl_access
20  * /.reiserfs_priv/xattrs/C0FFEE.0/system.posix_acl_default
21  * /.reiserfs_priv/xattrs/C0FFEE.0/user.Content-Type
22  * .. or similar.
23  *
24  * The file contents are the text of the EA. The size is known based on the
25  * stat data describing the file.
26  *
27  * In the case of system.posix_acl_access and system.posix_acl_default, since
28  * these are special cases for filesystem ACLs, they are interpreted by the
29  * kernel, in addition, they are negatively and positively cached and attached
30  * to the inode so that unnecessary lookups are avoided.
31  *
32  * Locking works like so:
33  * Directory components (xattr root, xattr dir) are protectd by their i_mutex.
34  * The xattrs themselves are protected by the xattr_sem.
35  */
36 
37 #include "reiserfs.h"
38 #include <linux/capability.h>
39 #include <linux/dcache.h>
40 #include <linux/namei.h>
41 #include <linux/errno.h>
42 #include <linux/gfp.h>
43 #include <linux/fs.h>
44 #include <linux/file.h>
45 #include <linux/pagemap.h>
46 #include <linux/xattr.h>
47 #include "xattr.h"
48 #include "acl.h"
49 #include <linux/uaccess.h>
50 #include <net/checksum.h>
51 #include <linux/stat.h>
52 #include <linux/quotaops.h>
53 #include <linux/security.h>
54 #include <linux/posix_acl_xattr.h>
55 
56 #define PRIVROOT_NAME ".reiserfs_priv"
57 #define XAROOT_NAME   "xattrs"
58 
59 
60 /*
61  * Helpers for inode ops. We do this so that we don't have all the VFS
62  * overhead and also for proper i_mutex annotation.
63  * dir->i_mutex must be held for all of them.
64  */
65 #ifdef CONFIG_REISERFS_FS_XATTR
xattr_create(struct inode * dir,struct dentry * dentry,int mode)66 static int xattr_create(struct inode *dir, struct dentry *dentry, int mode)
67 {
68 	BUG_ON(!inode_is_locked(dir));
69 	return dir->i_op->create(dir, dentry, mode, true);
70 }
71 #endif
72 
xattr_mkdir(struct inode * dir,struct dentry * dentry,umode_t mode)73 static int xattr_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
74 {
75 	BUG_ON(!inode_is_locked(dir));
76 	return dir->i_op->mkdir(dir, dentry, mode);
77 }
78 
79 /*
80  * We use I_MUTEX_CHILD here to silence lockdep. It's safe because xattr
81  * mutation ops aren't called during rename or splace, which are the
82  * only other users of I_MUTEX_CHILD. It violates the ordering, but that's
83  * better than allocating another subclass just for this code.
84  */
xattr_unlink(struct inode * dir,struct dentry * dentry)85 static int xattr_unlink(struct inode *dir, struct dentry *dentry)
86 {
87 	int error;
88 
89 	BUG_ON(!inode_is_locked(dir));
90 
91 	inode_lock_nested(d_inode(dentry), I_MUTEX_CHILD);
92 	error = dir->i_op->unlink(dir, dentry);
93 	inode_unlock(d_inode(dentry));
94 
95 	if (!error)
96 		d_delete(dentry);
97 	return error;
98 }
99 
xattr_rmdir(struct inode * dir,struct dentry * dentry)100 static int xattr_rmdir(struct inode *dir, struct dentry *dentry)
101 {
102 	int error;
103 
104 	BUG_ON(!inode_is_locked(dir));
105 
106 	inode_lock_nested(d_inode(dentry), I_MUTEX_CHILD);
107 	error = dir->i_op->rmdir(dir, dentry);
108 	if (!error)
109 		d_inode(dentry)->i_flags |= S_DEAD;
110 	inode_unlock(d_inode(dentry));
111 	if (!error)
112 		d_delete(dentry);
113 
114 	return error;
115 }
116 
117 #define xattr_may_create(flags)	(!flags || flags & XATTR_CREATE)
118 
open_xa_root(struct super_block * sb,int flags)119 static struct dentry *open_xa_root(struct super_block *sb, int flags)
120 {
121 	struct dentry *privroot = REISERFS_SB(sb)->priv_root;
122 	struct dentry *xaroot;
123 
124 	if (d_really_is_negative(privroot))
125 		return ERR_PTR(-EOPNOTSUPP);
126 
127 	inode_lock_nested(d_inode(privroot), I_MUTEX_XATTR);
128 
129 	xaroot = dget(REISERFS_SB(sb)->xattr_root);
130 	if (!xaroot)
131 		xaroot = ERR_PTR(-EOPNOTSUPP);
132 	else if (d_really_is_negative(xaroot)) {
133 		int err = -ENODATA;
134 
135 		if (xattr_may_create(flags))
136 			err = xattr_mkdir(d_inode(privroot), xaroot, 0700);
137 		if (err) {
138 			dput(xaroot);
139 			xaroot = ERR_PTR(err);
140 		}
141 	}
142 
143 	inode_unlock(d_inode(privroot));
144 	return xaroot;
145 }
146 
open_xa_dir(const struct inode * inode,int flags)147 static struct dentry *open_xa_dir(const struct inode *inode, int flags)
148 {
149 	struct dentry *xaroot, *xadir;
150 	char namebuf[17];
151 
152 	xaroot = open_xa_root(inode->i_sb, flags);
153 	if (IS_ERR(xaroot))
154 		return xaroot;
155 
156 	snprintf(namebuf, sizeof(namebuf), "%X.%X",
157 		 le32_to_cpu(INODE_PKEY(inode)->k_objectid),
158 		 inode->i_generation);
159 
160 	inode_lock_nested(d_inode(xaroot), I_MUTEX_XATTR);
161 
162 	xadir = lookup_one_len(namebuf, xaroot, strlen(namebuf));
163 	if (!IS_ERR(xadir) && d_really_is_negative(xadir)) {
164 		int err = -ENODATA;
165 
166 		if (xattr_may_create(flags))
167 			err = xattr_mkdir(d_inode(xaroot), xadir, 0700);
168 		if (err) {
169 			dput(xadir);
170 			xadir = ERR_PTR(err);
171 		}
172 	}
173 
174 	inode_unlock(d_inode(xaroot));
175 	dput(xaroot);
176 	return xadir;
177 }
178 
179 /*
180  * The following are side effects of other operations that aren't explicitly
181  * modifying extended attributes. This includes operations such as permissions
182  * or ownership changes, object deletions, etc.
183  */
184 struct reiserfs_dentry_buf {
185 	struct dir_context ctx;
186 	struct dentry *xadir;
187 	int count;
188 	int err;
189 	struct dentry *dentries[8];
190 };
191 
192 static int
fill_with_dentries(struct dir_context * ctx,const char * name,int namelen,loff_t offset,u64 ino,unsigned int d_type)193 fill_with_dentries(struct dir_context *ctx, const char *name, int namelen,
194 		   loff_t offset, u64 ino, unsigned int d_type)
195 {
196 	struct reiserfs_dentry_buf *dbuf =
197 		container_of(ctx, struct reiserfs_dentry_buf, ctx);
198 	struct dentry *dentry;
199 
200 	WARN_ON_ONCE(!inode_is_locked(d_inode(dbuf->xadir)));
201 
202 	if (dbuf->count == ARRAY_SIZE(dbuf->dentries))
203 		return -ENOSPC;
204 
205 	if (name[0] == '.' && (namelen < 2 ||
206 			       (namelen == 2 && name[1] == '.')))
207 		return 0;
208 
209 	dentry = lookup_one_len(name, dbuf->xadir, namelen);
210 	if (IS_ERR(dentry)) {
211 		dbuf->err = PTR_ERR(dentry);
212 		return PTR_ERR(dentry);
213 	} else if (d_really_is_negative(dentry)) {
214 		/* A directory entry exists, but no file? */
215 		reiserfs_error(dentry->d_sb, "xattr-20003",
216 			       "Corrupted directory: xattr %pd listed but "
217 			       "not found for file %pd.\n",
218 			       dentry, dbuf->xadir);
219 		dput(dentry);
220 		dbuf->err = -EIO;
221 		return -EIO;
222 	}
223 
224 	dbuf->dentries[dbuf->count++] = dentry;
225 	return 0;
226 }
227 
228 static void
cleanup_dentry_buf(struct reiserfs_dentry_buf * buf)229 cleanup_dentry_buf(struct reiserfs_dentry_buf *buf)
230 {
231 	int i;
232 
233 	for (i = 0; i < buf->count; i++)
234 		if (buf->dentries[i])
235 			dput(buf->dentries[i]);
236 }
237 
reiserfs_for_each_xattr(struct inode * inode,int (* action)(struct dentry *,void *),void * data)238 static int reiserfs_for_each_xattr(struct inode *inode,
239 				   int (*action)(struct dentry *, void *),
240 				   void *data)
241 {
242 	struct dentry *dir;
243 	int i, err = 0;
244 	struct reiserfs_dentry_buf buf = {
245 		.ctx.actor = fill_with_dentries,
246 	};
247 
248 	/* Skip out, an xattr has no xattrs associated with it */
249 	if (IS_PRIVATE(inode) || get_inode_sd_version(inode) == STAT_DATA_V1)
250 		return 0;
251 
252 	dir = open_xa_dir(inode, XATTR_REPLACE);
253 	if (IS_ERR(dir)) {
254 		err = PTR_ERR(dir);
255 		goto out;
256 	} else if (d_really_is_negative(dir)) {
257 		err = 0;
258 		goto out_dir;
259 	}
260 
261 	inode_lock_nested(d_inode(dir), I_MUTEX_XATTR);
262 
263 	buf.xadir = dir;
264 	while (1) {
265 		err = reiserfs_readdir_inode(d_inode(dir), &buf.ctx);
266 		if (err)
267 			break;
268 		if (buf.err) {
269 			err = buf.err;
270 			break;
271 		}
272 		if (!buf.count)
273 			break;
274 		for (i = 0; !err && i < buf.count && buf.dentries[i]; i++) {
275 			struct dentry *dentry = buf.dentries[i];
276 
277 			if (!d_is_dir(dentry))
278 				err = action(dentry, data);
279 
280 			dput(dentry);
281 			buf.dentries[i] = NULL;
282 		}
283 		if (err)
284 			break;
285 		buf.count = 0;
286 	}
287 	inode_unlock(d_inode(dir));
288 
289 	cleanup_dentry_buf(&buf);
290 
291 	if (!err) {
292 		/*
293 		 * We start a transaction here to avoid a ABBA situation
294 		 * between the xattr root's i_mutex and the journal lock.
295 		 * This doesn't incur much additional overhead since the
296 		 * new transaction will just nest inside the
297 		 * outer transaction.
298 		 */
299 		int blocks = JOURNAL_PER_BALANCE_CNT * 2 + 2 +
300 			     4 * REISERFS_QUOTA_TRANS_BLOCKS(inode->i_sb);
301 		struct reiserfs_transaction_handle th;
302 
303 		reiserfs_write_lock(inode->i_sb);
304 		err = journal_begin(&th, inode->i_sb, blocks);
305 		reiserfs_write_unlock(inode->i_sb);
306 		if (!err) {
307 			int jerror;
308 
309 			inode_lock_nested(d_inode(dir->d_parent),
310 					  I_MUTEX_XATTR);
311 			err = action(dir, data);
312 			reiserfs_write_lock(inode->i_sb);
313 			jerror = journal_end(&th);
314 			reiserfs_write_unlock(inode->i_sb);
315 			inode_unlock(d_inode(dir->d_parent));
316 			err = jerror ?: err;
317 		}
318 	}
319 out_dir:
320 	dput(dir);
321 out:
322 	/*
323 	 * -ENODATA: this object doesn't have any xattrs
324 	 * -EOPNOTSUPP: this file system doesn't have xattrs enabled on disk.
325 	 * Neither are errors
326 	 */
327 	if (err == -ENODATA || err == -EOPNOTSUPP)
328 		err = 0;
329 	return err;
330 }
331 
delete_one_xattr(struct dentry * dentry,void * data)332 static int delete_one_xattr(struct dentry *dentry, void *data)
333 {
334 	struct inode *dir = d_inode(dentry->d_parent);
335 
336 	/* This is the xattr dir, handle specially. */
337 	if (d_is_dir(dentry))
338 		return xattr_rmdir(dir, dentry);
339 
340 	return xattr_unlink(dir, dentry);
341 }
342 
chown_one_xattr(struct dentry * dentry,void * data)343 static int chown_one_xattr(struct dentry *dentry, void *data)
344 {
345 	struct iattr *attrs = data;
346 	int ia_valid = attrs->ia_valid;
347 	int err;
348 
349 	/*
350 	 * We only want the ownership bits. Otherwise, we'll do
351 	 * things like change a directory to a regular file if
352 	 * ATTR_MODE is set.
353 	 */
354 	attrs->ia_valid &= (ATTR_UID|ATTR_GID);
355 	err = reiserfs_setattr(dentry, attrs);
356 	attrs->ia_valid = ia_valid;
357 
358 	return err;
359 }
360 
361 /* No i_mutex, but the inode is unconnected. */
reiserfs_delete_xattrs(struct inode * inode)362 int reiserfs_delete_xattrs(struct inode *inode)
363 {
364 	int err = reiserfs_for_each_xattr(inode, delete_one_xattr, NULL);
365 
366 	if (err)
367 		reiserfs_warning(inode->i_sb, "jdm-20004",
368 				 "Couldn't delete all xattrs (%d)\n", err);
369 	return err;
370 }
371 
372 /* inode->i_mutex: down */
reiserfs_chown_xattrs(struct inode * inode,struct iattr * attrs)373 int reiserfs_chown_xattrs(struct inode *inode, struct iattr *attrs)
374 {
375 	int err = reiserfs_for_each_xattr(inode, chown_one_xattr, attrs);
376 
377 	if (err)
378 		reiserfs_warning(inode->i_sb, "jdm-20007",
379 				 "Couldn't chown all xattrs (%d)\n", err);
380 	return err;
381 }
382 
383 #ifdef CONFIG_REISERFS_FS_XATTR
384 /*
385  * Returns a dentry corresponding to a specific extended attribute file
386  * for the inode. If flags allow, the file is created. Otherwise, a
387  * valid or negative dentry, or an error is returned.
388  */
xattr_lookup(struct inode * inode,const char * name,int flags)389 static struct dentry *xattr_lookup(struct inode *inode, const char *name,
390 				    int flags)
391 {
392 	struct dentry *xadir, *xafile;
393 	int err = 0;
394 
395 	xadir = open_xa_dir(inode, flags);
396 	if (IS_ERR(xadir))
397 		return ERR_CAST(xadir);
398 
399 	inode_lock_nested(d_inode(xadir), I_MUTEX_XATTR);
400 	xafile = lookup_one_len(name, xadir, strlen(name));
401 	if (IS_ERR(xafile)) {
402 		err = PTR_ERR(xafile);
403 		goto out;
404 	}
405 
406 	if (d_really_is_positive(xafile) && (flags & XATTR_CREATE))
407 		err = -EEXIST;
408 
409 	if (d_really_is_negative(xafile)) {
410 		err = -ENODATA;
411 		if (xattr_may_create(flags))
412 			err = xattr_create(d_inode(xadir), xafile,
413 					      0700|S_IFREG);
414 	}
415 
416 	if (err)
417 		dput(xafile);
418 out:
419 	inode_unlock(d_inode(xadir));
420 	dput(xadir);
421 	if (err)
422 		return ERR_PTR(err);
423 	return xafile;
424 }
425 
426 /* Internal operations on file data */
reiserfs_put_page(struct page * page)427 static inline void reiserfs_put_page(struct page *page)
428 {
429 	kunmap(page);
430 	put_page(page);
431 }
432 
reiserfs_get_page(struct inode * dir,size_t n)433 static struct page *reiserfs_get_page(struct inode *dir, size_t n)
434 {
435 	struct address_space *mapping = dir->i_mapping;
436 	struct page *page;
437 	/*
438 	 * We can deadlock if we try to free dentries,
439 	 * and an unlink/rmdir has just occurred - GFP_NOFS avoids this
440 	 */
441 	mapping_set_gfp_mask(mapping, GFP_NOFS);
442 	page = read_mapping_page(mapping, n >> PAGE_SHIFT, NULL);
443 	if (!IS_ERR(page)) {
444 		kmap(page);
445 		if (PageError(page))
446 			goto fail;
447 	}
448 	return page;
449 
450 fail:
451 	reiserfs_put_page(page);
452 	return ERR_PTR(-EIO);
453 }
454 
xattr_hash(const char * msg,int len)455 static inline __u32 xattr_hash(const char *msg, int len)
456 {
457 	return csum_partial(msg, len, 0);
458 }
459 
460 int reiserfs_commit_write(struct file *f, struct page *page,
461 			  unsigned from, unsigned to);
462 
update_ctime(struct inode * inode)463 static void update_ctime(struct inode *inode)
464 {
465 	struct timespec64 now = current_time(inode);
466 
467 	if (inode_unhashed(inode) || !inode->i_nlink ||
468 	    timespec64_equal(&inode->i_ctime, &now))
469 		return;
470 
471 	inode->i_ctime = current_time(inode);
472 	mark_inode_dirty(inode);
473 }
474 
lookup_and_delete_xattr(struct inode * inode,const char * name)475 static int lookup_and_delete_xattr(struct inode *inode, const char *name)
476 {
477 	int err = 0;
478 	struct dentry *dentry, *xadir;
479 
480 	xadir = open_xa_dir(inode, XATTR_REPLACE);
481 	if (IS_ERR(xadir))
482 		return PTR_ERR(xadir);
483 
484 	inode_lock_nested(d_inode(xadir), I_MUTEX_XATTR);
485 	dentry = lookup_one_len(name, xadir, strlen(name));
486 	if (IS_ERR(dentry)) {
487 		err = PTR_ERR(dentry);
488 		goto out_dput;
489 	}
490 
491 	if (d_really_is_positive(dentry)) {
492 		err = xattr_unlink(d_inode(xadir), dentry);
493 		update_ctime(inode);
494 	}
495 
496 	dput(dentry);
497 out_dput:
498 	inode_unlock(d_inode(xadir));
499 	dput(xadir);
500 	return err;
501 }
502 
503 
504 /* Generic extended attribute operations that can be used by xa plugins */
505 
506 /*
507  * inode->i_mutex: down
508  */
509 int
reiserfs_xattr_set_handle(struct reiserfs_transaction_handle * th,struct inode * inode,const char * name,const void * buffer,size_t buffer_size,int flags)510 reiserfs_xattr_set_handle(struct reiserfs_transaction_handle *th,
511 			  struct inode *inode, const char *name,
512 			  const void *buffer, size_t buffer_size, int flags)
513 {
514 	int err = 0;
515 	struct dentry *dentry;
516 	struct page *page;
517 	char *data;
518 	size_t file_pos = 0;
519 	size_t buffer_pos = 0;
520 	size_t new_size;
521 	__u32 xahash = 0;
522 
523 	if (get_inode_sd_version(inode) == STAT_DATA_V1)
524 		return -EOPNOTSUPP;
525 
526 	if (!buffer) {
527 		err = lookup_and_delete_xattr(inode, name);
528 		return err;
529 	}
530 
531 	dentry = xattr_lookup(inode, name, flags);
532 	if (IS_ERR(dentry))
533 		return PTR_ERR(dentry);
534 
535 	down_write(&REISERFS_I(inode)->i_xattr_sem);
536 
537 	xahash = xattr_hash(buffer, buffer_size);
538 	while (buffer_pos < buffer_size || buffer_pos == 0) {
539 		size_t chunk;
540 		size_t skip = 0;
541 		size_t page_offset = (file_pos & (PAGE_SIZE - 1));
542 
543 		if (buffer_size - buffer_pos > PAGE_SIZE)
544 			chunk = PAGE_SIZE;
545 		else
546 			chunk = buffer_size - buffer_pos;
547 
548 		page = reiserfs_get_page(d_inode(dentry), file_pos);
549 		if (IS_ERR(page)) {
550 			err = PTR_ERR(page);
551 			goto out_unlock;
552 		}
553 
554 		lock_page(page);
555 		data = page_address(page);
556 
557 		if (file_pos == 0) {
558 			struct reiserfs_xattr_header *rxh;
559 
560 			skip = file_pos = sizeof(struct reiserfs_xattr_header);
561 			if (chunk + skip > PAGE_SIZE)
562 				chunk = PAGE_SIZE - skip;
563 			rxh = (struct reiserfs_xattr_header *)data;
564 			rxh->h_magic = cpu_to_le32(REISERFS_XATTR_MAGIC);
565 			rxh->h_hash = cpu_to_le32(xahash);
566 		}
567 
568 		reiserfs_write_lock(inode->i_sb);
569 		err = __reiserfs_write_begin(page, page_offset, chunk + skip);
570 		if (!err) {
571 			if (buffer)
572 				memcpy(data + skip, buffer + buffer_pos, chunk);
573 			err = reiserfs_commit_write(NULL, page, page_offset,
574 						    page_offset + chunk +
575 						    skip);
576 		}
577 		reiserfs_write_unlock(inode->i_sb);
578 		unlock_page(page);
579 		reiserfs_put_page(page);
580 		buffer_pos += chunk;
581 		file_pos += chunk;
582 		skip = 0;
583 		if (err || buffer_size == 0 || !buffer)
584 			break;
585 	}
586 
587 	new_size = buffer_size + sizeof(struct reiserfs_xattr_header);
588 	if (!err && new_size < i_size_read(d_inode(dentry))) {
589 		struct iattr newattrs = {
590 			.ia_ctime = current_time(inode),
591 			.ia_size = new_size,
592 			.ia_valid = ATTR_SIZE | ATTR_CTIME,
593 		};
594 
595 		inode_lock_nested(d_inode(dentry), I_MUTEX_XATTR);
596 		inode_dio_wait(d_inode(dentry));
597 
598 		err = reiserfs_setattr(dentry, &newattrs);
599 		inode_unlock(d_inode(dentry));
600 	} else
601 		update_ctime(inode);
602 out_unlock:
603 	up_write(&REISERFS_I(inode)->i_xattr_sem);
604 	dput(dentry);
605 	return err;
606 }
607 
608 /* We need to start a transaction to maintain lock ordering */
reiserfs_xattr_set(struct inode * inode,const char * name,const void * buffer,size_t buffer_size,int flags)609 int reiserfs_xattr_set(struct inode *inode, const char *name,
610 		       const void *buffer, size_t buffer_size, int flags)
611 {
612 
613 	struct reiserfs_transaction_handle th;
614 	int error, error2;
615 	size_t jbegin_count = reiserfs_xattr_nblocks(inode, buffer_size);
616 
617 	/* Check before we start a transaction and then do nothing. */
618 	if (!d_really_is_positive(REISERFS_SB(inode->i_sb)->priv_root))
619 		return -EOPNOTSUPP;
620 
621 	if (!(flags & XATTR_REPLACE))
622 		jbegin_count += reiserfs_xattr_jcreate_nblocks(inode);
623 
624 	reiserfs_write_lock(inode->i_sb);
625 	error = journal_begin(&th, inode->i_sb, jbegin_count);
626 	reiserfs_write_unlock(inode->i_sb);
627 	if (error) {
628 		return error;
629 	}
630 
631 	error = reiserfs_xattr_set_handle(&th, inode, name,
632 					  buffer, buffer_size, flags);
633 
634 	reiserfs_write_lock(inode->i_sb);
635 	error2 = journal_end(&th);
636 	reiserfs_write_unlock(inode->i_sb);
637 	if (error == 0)
638 		error = error2;
639 
640 	return error;
641 }
642 
643 /*
644  * inode->i_mutex: down
645  */
646 int
reiserfs_xattr_get(struct inode * inode,const char * name,void * buffer,size_t buffer_size)647 reiserfs_xattr_get(struct inode *inode, const char *name, void *buffer,
648 		   size_t buffer_size)
649 {
650 	ssize_t err = 0;
651 	struct dentry *dentry;
652 	size_t isize;
653 	size_t file_pos = 0;
654 	size_t buffer_pos = 0;
655 	struct page *page;
656 	__u32 hash = 0;
657 
658 	if (name == NULL)
659 		return -EINVAL;
660 
661 	/*
662 	 * We can't have xattrs attached to v1 items since they don't have
663 	 * generation numbers
664 	 */
665 	if (get_inode_sd_version(inode) == STAT_DATA_V1)
666 		return -EOPNOTSUPP;
667 
668 	/*
669 	 * priv_root needn't be initialized during mount so allow initial
670 	 * lookups to succeed.
671 	 */
672 	if (!REISERFS_SB(inode->i_sb)->priv_root)
673 		return 0;
674 
675 	dentry = xattr_lookup(inode, name, XATTR_REPLACE);
676 	if (IS_ERR(dentry)) {
677 		err = PTR_ERR(dentry);
678 		goto out;
679 	}
680 
681 	down_read(&REISERFS_I(inode)->i_xattr_sem);
682 
683 	isize = i_size_read(d_inode(dentry));
684 
685 	/* Just return the size needed */
686 	if (buffer == NULL) {
687 		err = isize - sizeof(struct reiserfs_xattr_header);
688 		goto out_unlock;
689 	}
690 
691 	if (buffer_size < isize - sizeof(struct reiserfs_xattr_header)) {
692 		err = -ERANGE;
693 		goto out_unlock;
694 	}
695 
696 	while (file_pos < isize) {
697 		size_t chunk;
698 		char *data;
699 		size_t skip = 0;
700 
701 		if (isize - file_pos > PAGE_SIZE)
702 			chunk = PAGE_SIZE;
703 		else
704 			chunk = isize - file_pos;
705 
706 		page = reiserfs_get_page(d_inode(dentry), file_pos);
707 		if (IS_ERR(page)) {
708 			err = PTR_ERR(page);
709 			goto out_unlock;
710 		}
711 
712 		lock_page(page);
713 		data = page_address(page);
714 		if (file_pos == 0) {
715 			struct reiserfs_xattr_header *rxh =
716 			    (struct reiserfs_xattr_header *)data;
717 			skip = file_pos = sizeof(struct reiserfs_xattr_header);
718 			chunk -= skip;
719 			/* Magic doesn't match up.. */
720 			if (rxh->h_magic != cpu_to_le32(REISERFS_XATTR_MAGIC)) {
721 				unlock_page(page);
722 				reiserfs_put_page(page);
723 				reiserfs_warning(inode->i_sb, "jdm-20001",
724 						 "Invalid magic for xattr (%s) "
725 						 "associated with %k", name,
726 						 INODE_PKEY(inode));
727 				err = -EIO;
728 				goto out_unlock;
729 			}
730 			hash = le32_to_cpu(rxh->h_hash);
731 		}
732 		memcpy(buffer + buffer_pos, data + skip, chunk);
733 		unlock_page(page);
734 		reiserfs_put_page(page);
735 		file_pos += chunk;
736 		buffer_pos += chunk;
737 		skip = 0;
738 	}
739 	err = isize - sizeof(struct reiserfs_xattr_header);
740 
741 	if (xattr_hash(buffer, isize - sizeof(struct reiserfs_xattr_header)) !=
742 	    hash) {
743 		reiserfs_warning(inode->i_sb, "jdm-20002",
744 				 "Invalid hash for xattr (%s) associated "
745 				 "with %k", name, INODE_PKEY(inode));
746 		err = -EIO;
747 	}
748 
749 out_unlock:
750 	up_read(&REISERFS_I(inode)->i_xattr_sem);
751 	dput(dentry);
752 
753 out:
754 	return err;
755 }
756 
757 /*
758  * In order to implement different sets of xattr operations for each xattr
759  * prefix with the generic xattr API, a filesystem should create a
760  * null-terminated array of struct xattr_handler (one for each prefix) and
761  * hang a pointer to it off of the s_xattr field of the superblock.
762  *
763  * The generic_fooxattr() functions will use this list to dispatch xattr
764  * operations to the correct xattr_handler.
765  */
766 #define for_each_xattr_handler(handlers, handler)		\
767 		for ((handler) = *(handlers)++;			\
768 			(handler) != NULL;			\
769 			(handler) = *(handlers)++)
770 
771 /* This is the implementation for the xattr plugin infrastructure */
772 static inline const struct xattr_handler *
find_xattr_handler_prefix(const struct xattr_handler ** handlers,const char * name)773 find_xattr_handler_prefix(const struct xattr_handler **handlers,
774 			   const char *name)
775 {
776 	const struct xattr_handler *xah;
777 
778 	if (!handlers)
779 		return NULL;
780 
781 	for_each_xattr_handler(handlers, xah) {
782 		const char *prefix = xattr_prefix(xah);
783 		if (strncmp(prefix, name, strlen(prefix)) == 0)
784 			break;
785 	}
786 
787 	return xah;
788 }
789 
790 struct listxattr_buf {
791 	struct dir_context ctx;
792 	size_t size;
793 	size_t pos;
794 	char *buf;
795 	struct dentry *dentry;
796 };
797 
listxattr_filler(struct dir_context * ctx,const char * name,int namelen,loff_t offset,u64 ino,unsigned int d_type)798 static int listxattr_filler(struct dir_context *ctx, const char *name,
799 			    int namelen, loff_t offset, u64 ino,
800 			    unsigned int d_type)
801 {
802 	struct listxattr_buf *b =
803 		container_of(ctx, struct listxattr_buf, ctx);
804 	size_t size;
805 
806 	if (name[0] != '.' ||
807 	    (namelen != 1 && (name[1] != '.' || namelen != 2))) {
808 		const struct xattr_handler *handler;
809 
810 		handler = find_xattr_handler_prefix(b->dentry->d_sb->s_xattr,
811 						    name);
812 		if (!handler /* Unsupported xattr name */ ||
813 		    (handler->list && !handler->list(b->dentry)))
814 			return 0;
815 		size = namelen + 1;
816 		if (b->buf) {
817 			if (b->pos + size > b->size) {
818 				b->pos = -ERANGE;
819 				return -ERANGE;
820 			}
821 			memcpy(b->buf + b->pos, name, namelen);
822 			b->buf[b->pos + namelen] = 0;
823 		}
824 		b->pos += size;
825 	}
826 	return 0;
827 }
828 
829 /*
830  * Inode operation listxattr()
831  *
832  * We totally ignore the generic listxattr here because it would be stupid
833  * not to. Since the xattrs are organized in a directory, we can just
834  * readdir to find them.
835  */
reiserfs_listxattr(struct dentry * dentry,char * buffer,size_t size)836 ssize_t reiserfs_listxattr(struct dentry * dentry, char *buffer, size_t size)
837 {
838 	struct dentry *dir;
839 	int err = 0;
840 	struct listxattr_buf buf = {
841 		.ctx.actor = listxattr_filler,
842 		.dentry = dentry,
843 		.buf = buffer,
844 		.size = buffer ? size : 0,
845 	};
846 
847 	if (d_really_is_negative(dentry))
848 		return -EINVAL;
849 
850 	if (get_inode_sd_version(d_inode(dentry)) == STAT_DATA_V1)
851 		return -EOPNOTSUPP;
852 
853 	dir = open_xa_dir(d_inode(dentry), XATTR_REPLACE);
854 	if (IS_ERR(dir)) {
855 		err = PTR_ERR(dir);
856 		if (err == -ENODATA)
857 			err = 0;  /* Not an error if there aren't any xattrs */
858 		goto out;
859 	}
860 
861 	inode_lock_nested(d_inode(dir), I_MUTEX_XATTR);
862 	err = reiserfs_readdir_inode(d_inode(dir), &buf.ctx);
863 	inode_unlock(d_inode(dir));
864 
865 	if (!err)
866 		err = buf.pos;
867 
868 	dput(dir);
869 out:
870 	return err;
871 }
872 
create_privroot(struct dentry * dentry)873 static int create_privroot(struct dentry *dentry)
874 {
875 	int err;
876 	struct inode *inode = d_inode(dentry->d_parent);
877 
878 	WARN_ON_ONCE(!inode_is_locked(inode));
879 
880 	err = xattr_mkdir(inode, dentry, 0700);
881 	if (err || d_really_is_negative(dentry)) {
882 		reiserfs_warning(dentry->d_sb, "jdm-20006",
883 				 "xattrs/ACLs enabled and couldn't "
884 				 "find/create .reiserfs_priv. "
885 				 "Failing mount.");
886 		return -EOPNOTSUPP;
887 	}
888 
889 	d_inode(dentry)->i_flags |= S_PRIVATE;
890 	d_inode(dentry)->i_opflags &= ~IOP_XATTR;
891 	reiserfs_info(dentry->d_sb, "Created %s - reserved for xattr "
892 		      "storage.\n", PRIVROOT_NAME);
893 
894 	return 0;
895 }
896 
897 #else
reiserfs_xattr_register_handlers(void)898 int __init reiserfs_xattr_register_handlers(void) { return 0; }
reiserfs_xattr_unregister_handlers(void)899 void reiserfs_xattr_unregister_handlers(void) {}
create_privroot(struct dentry * dentry)900 static int create_privroot(struct dentry *dentry) { return 0; }
901 #endif
902 
903 /* Actual operations that are exported to VFS-land */
904 const struct xattr_handler *reiserfs_xattr_handlers[] = {
905 #ifdef CONFIG_REISERFS_FS_XATTR
906 	&reiserfs_xattr_user_handler,
907 	&reiserfs_xattr_trusted_handler,
908 #endif
909 #ifdef CONFIG_REISERFS_FS_SECURITY
910 	&reiserfs_xattr_security_handler,
911 #endif
912 #ifdef CONFIG_REISERFS_FS_POSIX_ACL
913 	&posix_acl_access_xattr_handler,
914 	&posix_acl_default_xattr_handler,
915 #endif
916 	NULL
917 };
918 
xattr_mount_check(struct super_block * s)919 static int xattr_mount_check(struct super_block *s)
920 {
921 	/*
922 	 * We need generation numbers to ensure that the oid mapping is correct
923 	 * v3.5 filesystems don't have them.
924 	 */
925 	if (old_format_only(s)) {
926 		if (reiserfs_xattrs_optional(s)) {
927 			/*
928 			 * Old format filesystem, but optional xattrs have
929 			 * been enabled. Error out.
930 			 */
931 			reiserfs_warning(s, "jdm-2005",
932 					 "xattrs/ACLs not supported "
933 					 "on pre-v3.6 format filesystems. "
934 					 "Failing mount.");
935 			return -EOPNOTSUPP;
936 		}
937 	}
938 
939 	return 0;
940 }
941 
reiserfs_permission(struct inode * inode,int mask)942 int reiserfs_permission(struct inode *inode, int mask)
943 {
944 	/*
945 	 * We don't do permission checks on the internal objects.
946 	 * Permissions are determined by the "owning" object.
947 	 */
948 	if (IS_PRIVATE(inode))
949 		return 0;
950 
951 	return generic_permission(inode, mask);
952 }
953 
xattr_hide_revalidate(struct dentry * dentry,unsigned int flags)954 static int xattr_hide_revalidate(struct dentry *dentry, unsigned int flags)
955 {
956 	return -EPERM;
957 }
958 
959 static const struct dentry_operations xattr_lookup_poison_ops = {
960 	.d_revalidate = xattr_hide_revalidate,
961 };
962 
reiserfs_lookup_privroot(struct super_block * s)963 int reiserfs_lookup_privroot(struct super_block *s)
964 {
965 	struct dentry *dentry;
966 	int err = 0;
967 
968 	/* If we don't have the privroot located yet - go find it */
969 	inode_lock(d_inode(s->s_root));
970 	dentry = lookup_one_len(PRIVROOT_NAME, s->s_root,
971 				strlen(PRIVROOT_NAME));
972 	if (!IS_ERR(dentry)) {
973 		REISERFS_SB(s)->priv_root = dentry;
974 		d_set_d_op(dentry, &xattr_lookup_poison_ops);
975 		if (d_really_is_positive(dentry)) {
976 			d_inode(dentry)->i_flags |= S_PRIVATE;
977 			d_inode(dentry)->i_opflags &= ~IOP_XATTR;
978 		}
979 	} else
980 		err = PTR_ERR(dentry);
981 	inode_unlock(d_inode(s->s_root));
982 
983 	return err;
984 }
985 
986 /*
987  * We need to take a copy of the mount flags since things like
988  * SB_RDONLY don't get set until *after* we're called.
989  * mount_flags != mount_options
990  */
reiserfs_xattr_init(struct super_block * s,int mount_flags)991 int reiserfs_xattr_init(struct super_block *s, int mount_flags)
992 {
993 	int err = 0;
994 	struct dentry *privroot = REISERFS_SB(s)->priv_root;
995 
996 	err = xattr_mount_check(s);
997 	if (err)
998 		goto error;
999 
1000 	if (d_really_is_negative(privroot) && !(mount_flags & SB_RDONLY)) {
1001 		inode_lock(d_inode(s->s_root));
1002 		err = create_privroot(REISERFS_SB(s)->priv_root);
1003 		inode_unlock(d_inode(s->s_root));
1004 	}
1005 
1006 	if (d_really_is_positive(privroot)) {
1007 		inode_lock(d_inode(privroot));
1008 		if (!REISERFS_SB(s)->xattr_root) {
1009 			struct dentry *dentry;
1010 
1011 			dentry = lookup_one_len(XAROOT_NAME, privroot,
1012 						strlen(XAROOT_NAME));
1013 			if (!IS_ERR(dentry))
1014 				REISERFS_SB(s)->xattr_root = dentry;
1015 			else
1016 				err = PTR_ERR(dentry);
1017 		}
1018 		inode_unlock(d_inode(privroot));
1019 	}
1020 
1021 error:
1022 	if (err) {
1023 		clear_bit(REISERFS_XATTRS_USER, &REISERFS_SB(s)->s_mount_opt);
1024 		clear_bit(REISERFS_POSIXACL, &REISERFS_SB(s)->s_mount_opt);
1025 	}
1026 
1027 	/* The super_block SB_POSIXACL must mirror the (no)acl mount option. */
1028 	if (reiserfs_posixacl(s))
1029 		s->s_flags |= SB_POSIXACL;
1030 	else
1031 		s->s_flags &= ~SB_POSIXACL;
1032 
1033 	return err;
1034 }
1035