xref: /openbmc/linux/fs/jfs/xattr.c (revision b694e3c604e999343258c49e574abd7be012e726)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *   Copyright (C) International Business Machines  Corp., 2000-2004
4  *   Copyright (C) Christoph Hellwig, 2002
5  */
6 
7 #include <linux/capability.h>
8 #include <linux/fs.h>
9 #include <linux/xattr.h>
10 #include <linux/posix_acl_xattr.h>
11 #include <linux/slab.h>
12 #include <linux/quotaops.h>
13 #include <linux/security.h>
14 #include "jfs_incore.h"
15 #include "jfs_superblock.h"
16 #include "jfs_dmap.h"
17 #include "jfs_debug.h"
18 #include "jfs_dinode.h"
19 #include "jfs_extent.h"
20 #include "jfs_metapage.h"
21 #include "jfs_xattr.h"
22 #include "jfs_acl.h"
23 
24 /*
25  *	jfs_xattr.c: extended attribute service
26  *
27  * Overall design --
28  *
29  * Format:
30  *
31  *   Extended attribute lists (jfs_ea_list) consist of an overall size (32 bit
32  *   value) and a variable (0 or more) number of extended attribute
33  *   entries.  Each extended attribute entry (jfs_ea) is a <name,value> double
34  *   where <name> is constructed from a null-terminated ascii string
35  *   (1 ... 255 bytes in the name) and <value> is arbitrary 8 bit data
36  *   (1 ... 65535 bytes).  The in-memory format is
37  *
38  *   0       1        2        4                4 + namelen + 1
39  *   +-------+--------+--------+----------------+-------------------+
40  *   | Flags | Name   | Value  | Name String \0 | Data . . . .      |
41  *   |       | Length | Length |                |                   |
42  *   +-------+--------+--------+----------------+-------------------+
43  *
44  *   A jfs_ea_list then is structured as
45  *
46  *   0            4                   4 + EA_SIZE(ea1)
47  *   +------------+-------------------+--------------------+-----
48  *   | Overall EA | First FEA Element | Second FEA Element | .....
49  *   | List Size  |                   |                    |
50  *   +------------+-------------------+--------------------+-----
51  *
52  *   On-disk:
53  *
54  *	FEALISTs are stored on disk using blocks allocated by dbAlloc() and
55  *	written directly. An EA list may be in-lined in the inode if there is
56  *	sufficient room available.
57  */
58 
59 struct ea_buffer {
60 	int flag;		/* Indicates what storage xattr points to */
61 	int max_size;		/* largest xattr that fits in current buffer */
62 	dxd_t new_ea;		/* dxd to replace ea when modifying xattr */
63 	struct metapage *mp;	/* metapage containing ea list */
64 	struct jfs_ea_list *xattr;	/* buffer containing ea list */
65 };
66 
67 /*
68  * ea_buffer.flag values
69  */
70 #define EA_INLINE	0x0001
71 #define EA_EXTENT	0x0002
72 #define EA_NEW		0x0004
73 #define EA_MALLOC	0x0008
74 
75 
76 /*
77  * Mapping of on-disk attribute names: for on-disk attribute names with an
78  * unknown prefix (not "system.", "user.", "security.", or "trusted."), the
79  * prefix "os2." is prepended.  On the way back to disk, "os2." prefixes are
80  * stripped and we make sure that the remaining name does not start with one
81  * of the know prefixes.
82  */
83 
is_known_namespace(const char * name)84 static int is_known_namespace(const char *name)
85 {
86 	if (strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN) &&
87 	    strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN) &&
88 	    strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN) &&
89 	    strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN))
90 		return false;
91 
92 	return true;
93 }
94 
name_size(struct jfs_ea * ea)95 static inline int name_size(struct jfs_ea *ea)
96 {
97 	if (is_known_namespace(ea->name))
98 		return ea->namelen;
99 	else
100 		return ea->namelen + XATTR_OS2_PREFIX_LEN;
101 }
102 
copy_name(char * buffer,struct jfs_ea * ea)103 static inline int copy_name(char *buffer, struct jfs_ea *ea)
104 {
105 	int len = ea->namelen;
106 
107 	if (!is_known_namespace(ea->name)) {
108 		memcpy(buffer, XATTR_OS2_PREFIX, XATTR_OS2_PREFIX_LEN);
109 		buffer += XATTR_OS2_PREFIX_LEN;
110 		len += XATTR_OS2_PREFIX_LEN;
111 	}
112 	memcpy(buffer, ea->name, ea->namelen);
113 	buffer[ea->namelen] = 0;
114 
115 	return len;
116 }
117 
118 /* Forward references */
119 static void ea_release(struct inode *inode, struct ea_buffer *ea_buf);
120 
121 /*
122  * NAME: ea_write_inline
123  *
124  * FUNCTION: Attempt to write an EA inline if area is available
125  *
126  * PRE CONDITIONS:
127  *	Already verified that the specified EA is small enough to fit inline
128  *
129  * PARAMETERS:
130  *	ip	- Inode pointer
131  *	ealist	- EA list pointer
132  *	size	- size of ealist in bytes
133  *	ea	- dxd_t structure to be filled in with necessary EA information
134  *		  if we successfully copy the EA inline
135  *
136  * NOTES:
137  *	Checks if the inode's inline area is available.  If so, copies EA inline
138  *	and sets <ea> fields appropriately.  Otherwise, returns failure, EA will
139  *	have to be put into an extent.
140  *
141  * RETURNS: 0 for successful copy to inline area; -1 if area not available
142  */
ea_write_inline(struct inode * ip,struct jfs_ea_list * ealist,int size,dxd_t * ea)143 static int ea_write_inline(struct inode *ip, struct jfs_ea_list *ealist,
144 			   int size, dxd_t * ea)
145 {
146 	struct jfs_inode_info *ji = JFS_IP(ip);
147 
148 	/*
149 	 * Make sure we have an EA -- the NULL EA list is valid, but you
150 	 * can't copy it!
151 	 */
152 	if (ealist && size > sizeof (struct jfs_ea_list)) {
153 		assert(size <= sizeof (ji->i_inline_ea));
154 
155 		/*
156 		 * See if the space is available or if it is already being
157 		 * used for an inline EA.
158 		 */
159 		if (!(ji->mode2 & INLINEEA) && !(ji->ea.flag & DXD_INLINE))
160 			return -EPERM;
161 
162 		DXDsize(ea, size);
163 		DXDlength(ea, 0);
164 		DXDaddress(ea, 0);
165 		memcpy(ji->i_inline_ea, ealist, size);
166 		ea->flag = DXD_INLINE;
167 		ji->mode2 &= ~INLINEEA;
168 	} else {
169 		ea->flag = 0;
170 		DXDsize(ea, 0);
171 		DXDlength(ea, 0);
172 		DXDaddress(ea, 0);
173 
174 		/* Free up INLINE area */
175 		if (ji->ea.flag & DXD_INLINE)
176 			ji->mode2 |= INLINEEA;
177 	}
178 
179 	return 0;
180 }
181 
182 /*
183  * NAME: ea_write
184  *
185  * FUNCTION: Write an EA for an inode
186  *
187  * PRE CONDITIONS: EA has been verified
188  *
189  * PARAMETERS:
190  *	ip	- Inode pointer
191  *	ealist	- EA list pointer
192  *	size	- size of ealist in bytes
193  *	ea	- dxd_t structure to be filled in appropriately with where the
194  *		  EA was copied
195  *
196  * NOTES: Will write EA inline if able to, otherwise allocates blocks for an
197  *	extent and synchronously writes it to those blocks.
198  *
199  * RETURNS: 0 for success; Anything else indicates failure
200  */
ea_write(struct inode * ip,struct jfs_ea_list * ealist,int size,dxd_t * ea)201 static int ea_write(struct inode *ip, struct jfs_ea_list *ealist, int size,
202 		       dxd_t * ea)
203 {
204 	struct super_block *sb = ip->i_sb;
205 	struct jfs_inode_info *ji = JFS_IP(ip);
206 	struct jfs_sb_info *sbi = JFS_SBI(sb);
207 	int nblocks;
208 	s64 blkno;
209 	int rc = 0, i;
210 	char *cp;
211 	s32 nbytes, nb;
212 	s32 bytes_to_write;
213 	struct metapage *mp;
214 
215 	/*
216 	 * Quick check to see if this is an in-linable EA.  Short EAs
217 	 * and empty EAs are all in-linable, provided the space exists.
218 	 */
219 	if (!ealist || size <= sizeof (ji->i_inline_ea)) {
220 		if (!ea_write_inline(ip, ealist, size, ea))
221 			return 0;
222 	}
223 
224 	/* figure out how many blocks we need */
225 	nblocks = (size + (sb->s_blocksize - 1)) >> sb->s_blocksize_bits;
226 
227 	/* Allocate new blocks to quota. */
228 	rc = dquot_alloc_block(ip, nblocks);
229 	if (rc)
230 		return rc;
231 
232 	rc = dbAlloc(ip, INOHINT(ip), nblocks, &blkno);
233 	if (rc) {
234 		/*Rollback quota allocation. */
235 		dquot_free_block(ip, nblocks);
236 		return rc;
237 	}
238 
239 	/*
240 	 * Now have nblocks worth of storage to stuff into the FEALIST.
241 	 * loop over the FEALIST copying data into the buffer one page at
242 	 * a time.
243 	 */
244 	cp = (char *) ealist;
245 	nbytes = size;
246 	for (i = 0; i < nblocks; i += sbi->nbperpage) {
247 		/*
248 		 * Determine how many bytes for this request, and round up to
249 		 * the nearest aggregate block size
250 		 */
251 		nb = min(PSIZE, nbytes);
252 		bytes_to_write =
253 		    ((((nb + sb->s_blocksize - 1)) >> sb->s_blocksize_bits))
254 		    << sb->s_blocksize_bits;
255 
256 		if (!(mp = get_metapage(ip, blkno + i, bytes_to_write, 1))) {
257 			rc = -EIO;
258 			goto failed;
259 		}
260 
261 		memcpy(mp->data, cp, nb);
262 
263 		/*
264 		 * We really need a way to propagate errors for
265 		 * forced writes like this one.  --hch
266 		 *
267 		 * (__write_metapage => release_metapage => flush_metapage)
268 		 */
269 #ifdef _JFS_FIXME
270 		if ((rc = flush_metapage(mp))) {
271 			/*
272 			 * the write failed -- this means that the buffer
273 			 * is still assigned and the blocks are not being
274 			 * used.  this seems like the best error recovery
275 			 * we can get ...
276 			 */
277 			goto failed;
278 		}
279 #else
280 		flush_metapage(mp);
281 #endif
282 
283 		cp += PSIZE;
284 		nbytes -= nb;
285 	}
286 
287 	ea->flag = DXD_EXTENT;
288 	DXDsize(ea, le32_to_cpu(ealist->size));
289 	DXDlength(ea, nblocks);
290 	DXDaddress(ea, blkno);
291 
292 	/* Free up INLINE area */
293 	if (ji->ea.flag & DXD_INLINE)
294 		ji->mode2 |= INLINEEA;
295 
296 	return 0;
297 
298       failed:
299 	/* Rollback quota allocation. */
300 	dquot_free_block(ip, nblocks);
301 
302 	dbFree(ip, blkno, nblocks);
303 	return rc;
304 }
305 
306 /*
307  * NAME: ea_read_inline
308  *
309  * FUNCTION: Read an inlined EA into user's buffer
310  *
311  * PARAMETERS:
312  *	ip	- Inode pointer
313  *	ealist	- Pointer to buffer to fill in with EA
314  *
315  * RETURNS: 0
316  */
ea_read_inline(struct inode * ip,struct jfs_ea_list * ealist)317 static int ea_read_inline(struct inode *ip, struct jfs_ea_list *ealist)
318 {
319 	struct jfs_inode_info *ji = JFS_IP(ip);
320 	int ea_size = sizeDXD(&ji->ea);
321 
322 	if (ea_size == 0) {
323 		ealist->size = 0;
324 		return 0;
325 	}
326 
327 	/* Sanity Check */
328 	if ((sizeDXD(&ji->ea) > sizeof (ji->i_inline_ea)))
329 		return -EIO;
330 	if (le32_to_cpu(((struct jfs_ea_list *) &ji->i_inline_ea)->size)
331 	    != ea_size)
332 		return -EIO;
333 
334 	memcpy(ealist, ji->i_inline_ea, ea_size);
335 	return 0;
336 }
337 
338 /*
339  * NAME: ea_read
340  *
341  * FUNCTION: copy EA data into user's buffer
342  *
343  * PARAMETERS:
344  *	ip	- Inode pointer
345  *	ealist	- Pointer to buffer to fill in with EA
346  *
347  * NOTES:  If EA is inline calls ea_read_inline() to copy EA.
348  *
349  * RETURNS: 0 for success; other indicates failure
350  */
ea_read(struct inode * ip,struct jfs_ea_list * ealist)351 static int ea_read(struct inode *ip, struct jfs_ea_list *ealist)
352 {
353 	struct super_block *sb = ip->i_sb;
354 	struct jfs_inode_info *ji = JFS_IP(ip);
355 	struct jfs_sb_info *sbi = JFS_SBI(sb);
356 	int nblocks;
357 	s64 blkno;
358 	char *cp = (char *) ealist;
359 	int i;
360 	int nbytes, nb;
361 	s32 bytes_to_read;
362 	struct metapage *mp;
363 
364 	/* quick check for in-line EA */
365 	if (ji->ea.flag & DXD_INLINE)
366 		return ea_read_inline(ip, ealist);
367 
368 	nbytes = sizeDXD(&ji->ea);
369 	if (!nbytes) {
370 		jfs_error(sb, "nbytes is 0\n");
371 		return -EIO;
372 	}
373 
374 	/*
375 	 * Figure out how many blocks were allocated when this EA list was
376 	 * originally written to disk.
377 	 */
378 	nblocks = lengthDXD(&ji->ea) << sbi->l2nbperpage;
379 	blkno = addressDXD(&ji->ea) << sbi->l2nbperpage;
380 
381 	/*
382 	 * I have found the disk blocks which were originally used to store
383 	 * the FEALIST.  now i loop over each contiguous block copying the
384 	 * data into the buffer.
385 	 */
386 	for (i = 0; i < nblocks; i += sbi->nbperpage) {
387 		/*
388 		 * Determine how many bytes for this request, and round up to
389 		 * the nearest aggregate block size
390 		 */
391 		nb = min(PSIZE, nbytes);
392 		bytes_to_read =
393 		    ((((nb + sb->s_blocksize - 1)) >> sb->s_blocksize_bits))
394 		    << sb->s_blocksize_bits;
395 
396 		if (!(mp = read_metapage(ip, blkno + i, bytes_to_read, 1)))
397 			return -EIO;
398 
399 		memcpy(cp, mp->data, nb);
400 		release_metapage(mp);
401 
402 		cp += PSIZE;
403 		nbytes -= nb;
404 	}
405 
406 	return 0;
407 }
408 
409 /*
410  * NAME: ea_get
411  *
412  * FUNCTION: Returns buffer containing existing extended attributes.
413  *	     The size of the buffer will be the larger of the existing
414  *	     attributes size, or min_size.
415  *
416  *	     The buffer, which may be inlined in the inode or in the
417  *	     page cache must be release by calling ea_release or ea_put
418  *
419  * PARAMETERS:
420  *	inode	- Inode pointer
421  *	ea_buf	- Structure to be populated with ealist and its metadata
422  *	min_size- minimum size of buffer to be returned
423  *
424  * RETURNS: 0 for success; Other indicates failure
425  */
ea_get(struct inode * inode,struct ea_buffer * ea_buf,int min_size)426 static int ea_get(struct inode *inode, struct ea_buffer *ea_buf, int min_size)
427 {
428 	struct jfs_inode_info *ji = JFS_IP(inode);
429 	struct super_block *sb = inode->i_sb;
430 	int size;
431 	int ea_size = sizeDXD(&ji->ea);
432 	int blocks_needed, current_blocks;
433 	s64 blkno;
434 	int rc;
435 	int quota_allocation = 0;
436 
437 	memset(&ea_buf->new_ea, 0, sizeof(ea_buf->new_ea));
438 
439 	/* When fsck.jfs clears a bad ea, it doesn't clear the size */
440 	if (ji->ea.flag == 0)
441 		ea_size = 0;
442 
443 	if (ea_size == 0) {
444 		if (min_size == 0) {
445 			ea_buf->flag = 0;
446 			ea_buf->max_size = 0;
447 			ea_buf->xattr = NULL;
448 			return 0;
449 		}
450 		if ((min_size <= sizeof (ji->i_inline_ea)) &&
451 		    (ji->mode2 & INLINEEA)) {
452 			ea_buf->flag = EA_INLINE | EA_NEW;
453 			ea_buf->max_size = sizeof (ji->i_inline_ea);
454 			ea_buf->xattr = (struct jfs_ea_list *) ji->i_inline_ea;
455 			DXDlength(&ea_buf->new_ea, 0);
456 			DXDaddress(&ea_buf->new_ea, 0);
457 			ea_buf->new_ea.flag = DXD_INLINE;
458 			DXDsize(&ea_buf->new_ea, min_size);
459 			return 0;
460 		}
461 		current_blocks = 0;
462 	} else if (ji->ea.flag & DXD_INLINE) {
463 		if (min_size <= sizeof (ji->i_inline_ea)) {
464 			ea_buf->flag = EA_INLINE;
465 			ea_buf->max_size = sizeof (ji->i_inline_ea);
466 			ea_buf->xattr = (struct jfs_ea_list *) ji->i_inline_ea;
467 			goto size_check;
468 		}
469 		current_blocks = 0;
470 	} else {
471 		if (!(ji->ea.flag & DXD_EXTENT)) {
472 			jfs_error(sb, "invalid ea.flag\n");
473 			return -EIO;
474 		}
475 		current_blocks = (ea_size + sb->s_blocksize - 1) >>
476 		    sb->s_blocksize_bits;
477 	}
478 	size = max(min_size, ea_size);
479 
480 	if (size > PSIZE) {
481 		/*
482 		 * To keep the rest of the code simple.  Allocate a
483 		 * contiguous buffer to work with. Make the buffer large
484 		 * enough to make use of the whole extent.
485 		 */
486 		ea_buf->max_size = (size + sb->s_blocksize - 1) &
487 		    ~(sb->s_blocksize - 1);
488 
489 		ea_buf->xattr = kmalloc(ea_buf->max_size, GFP_KERNEL);
490 		if (ea_buf->xattr == NULL)
491 			return -ENOMEM;
492 
493 		ea_buf->flag = EA_MALLOC;
494 
495 		if (ea_size == 0)
496 			return 0;
497 
498 		if ((rc = ea_read(inode, ea_buf->xattr))) {
499 			kfree(ea_buf->xattr);
500 			ea_buf->xattr = NULL;
501 			return rc;
502 		}
503 		goto size_check;
504 	}
505 	blocks_needed = (min_size + sb->s_blocksize - 1) >>
506 	    sb->s_blocksize_bits;
507 
508 	if (blocks_needed > current_blocks) {
509 		/* Allocate new blocks to quota. */
510 		rc = dquot_alloc_block(inode, blocks_needed);
511 		if (rc)
512 			return -EDQUOT;
513 
514 		quota_allocation = blocks_needed;
515 
516 		rc = dbAlloc(inode, INOHINT(inode), (s64) blocks_needed,
517 			     &blkno);
518 		if (rc)
519 			goto clean_up;
520 
521 		DXDlength(&ea_buf->new_ea, blocks_needed);
522 		DXDaddress(&ea_buf->new_ea, blkno);
523 		ea_buf->new_ea.flag = DXD_EXTENT;
524 		DXDsize(&ea_buf->new_ea, min_size);
525 
526 		ea_buf->flag = EA_EXTENT | EA_NEW;
527 
528 		ea_buf->mp = get_metapage(inode, blkno,
529 					  blocks_needed << sb->s_blocksize_bits,
530 					  1);
531 		if (ea_buf->mp == NULL) {
532 			dbFree(inode, blkno, (s64) blocks_needed);
533 			rc = -EIO;
534 			goto clean_up;
535 		}
536 		ea_buf->xattr = ea_buf->mp->data;
537 		ea_buf->max_size = (min_size + sb->s_blocksize - 1) &
538 		    ~(sb->s_blocksize - 1);
539 		if (ea_size == 0)
540 			return 0;
541 		if ((rc = ea_read(inode, ea_buf->xattr))) {
542 			discard_metapage(ea_buf->mp);
543 			dbFree(inode, blkno, (s64) blocks_needed);
544 			goto clean_up;
545 		}
546 		goto size_check;
547 	}
548 	ea_buf->flag = EA_EXTENT;
549 	ea_buf->mp = read_metapage(inode, addressDXD(&ji->ea),
550 				   lengthDXD(&ji->ea) << sb->s_blocksize_bits,
551 				   1);
552 	if (ea_buf->mp == NULL) {
553 		rc = -EIO;
554 		goto clean_up;
555 	}
556 	ea_buf->xattr = ea_buf->mp->data;
557 	ea_buf->max_size = (ea_size + sb->s_blocksize - 1) &
558 	    ~(sb->s_blocksize - 1);
559 
560       size_check:
561 	if (EALIST_SIZE(ea_buf->xattr) != ea_size) {
562 		if (unlikely(EALIST_SIZE(ea_buf->xattr) > INT_MAX)) {
563 			printk(KERN_ERR "ea_get: extended attribute size too large: %u > INT_MAX\n",
564 			       EALIST_SIZE(ea_buf->xattr));
565 		} else {
566 			int size = clamp_t(int, ea_size, 0, EALIST_SIZE(ea_buf->xattr));
567 
568 			printk(KERN_ERR "ea_get: invalid extended attribute\n");
569 			print_hex_dump(KERN_ERR, "", DUMP_PREFIX_ADDRESS, 16, 1,
570 				       ea_buf->xattr, size, 1);
571 		}
572 		ea_release(inode, ea_buf);
573 		rc = -EIO;
574 		goto clean_up;
575 	}
576 
577 	return ea_size;
578 
579       clean_up:
580 	/* Rollback quota allocation */
581 	if (quota_allocation)
582 		dquot_free_block(inode, quota_allocation);
583 
584 	return (rc);
585 }
586 
ea_release(struct inode * inode,struct ea_buffer * ea_buf)587 static void ea_release(struct inode *inode, struct ea_buffer *ea_buf)
588 {
589 	if (ea_buf->flag & EA_MALLOC)
590 		kfree(ea_buf->xattr);
591 	else if (ea_buf->flag & EA_EXTENT) {
592 		assert(ea_buf->mp);
593 		release_metapage(ea_buf->mp);
594 
595 		if (ea_buf->flag & EA_NEW)
596 			dbFree(inode, addressDXD(&ea_buf->new_ea),
597 			       lengthDXD(&ea_buf->new_ea));
598 	}
599 }
600 
ea_put(tid_t tid,struct inode * inode,struct ea_buffer * ea_buf,int new_size)601 static int ea_put(tid_t tid, struct inode *inode, struct ea_buffer *ea_buf,
602 		  int new_size)
603 {
604 	struct jfs_inode_info *ji = JFS_IP(inode);
605 	unsigned long old_blocks, new_blocks;
606 	int rc = 0;
607 
608 	if (new_size == 0) {
609 		ea_release(inode, ea_buf);
610 		ea_buf = NULL;
611 	} else if (ea_buf->flag & EA_INLINE) {
612 		assert(new_size <= sizeof (ji->i_inline_ea));
613 		ji->mode2 &= ~INLINEEA;
614 		ea_buf->new_ea.flag = DXD_INLINE;
615 		DXDsize(&ea_buf->new_ea, new_size);
616 		DXDaddress(&ea_buf->new_ea, 0);
617 		DXDlength(&ea_buf->new_ea, 0);
618 	} else if (ea_buf->flag & EA_MALLOC) {
619 		rc = ea_write(inode, ea_buf->xattr, new_size, &ea_buf->new_ea);
620 		kfree(ea_buf->xattr);
621 	} else if (ea_buf->flag & EA_NEW) {
622 		/* We have already allocated a new dxd */
623 		flush_metapage(ea_buf->mp);
624 	} else {
625 		/* ->xattr must point to original ea's metapage */
626 		rc = ea_write(inode, ea_buf->xattr, new_size, &ea_buf->new_ea);
627 		discard_metapage(ea_buf->mp);
628 	}
629 	if (rc)
630 		return rc;
631 
632 	old_blocks = new_blocks = 0;
633 
634 	if (ji->ea.flag & DXD_EXTENT) {
635 		invalidate_dxd_metapages(inode, ji->ea);
636 		old_blocks = lengthDXD(&ji->ea);
637 	}
638 
639 	if (ea_buf) {
640 		txEA(tid, inode, &ji->ea, &ea_buf->new_ea);
641 		if (ea_buf->new_ea.flag & DXD_EXTENT) {
642 			new_blocks = lengthDXD(&ea_buf->new_ea);
643 			if (ji->ea.flag & DXD_INLINE)
644 				ji->mode2 |= INLINEEA;
645 		}
646 		ji->ea = ea_buf->new_ea;
647 	} else {
648 		txEA(tid, inode, &ji->ea, NULL);
649 		if (ji->ea.flag & DXD_INLINE)
650 			ji->mode2 |= INLINEEA;
651 		ji->ea.flag = 0;
652 		ji->ea.size = 0;
653 	}
654 
655 	/* If old blocks exist, they must be removed from quota allocation. */
656 	if (old_blocks)
657 		dquot_free_block(inode, old_blocks);
658 
659 	inode_set_ctime_current(inode);
660 
661 	return 0;
662 }
663 
__jfs_setxattr(tid_t tid,struct inode * inode,const char * name,const void * value,size_t value_len,int flags)664 int __jfs_setxattr(tid_t tid, struct inode *inode, const char *name,
665 		   const void *value, size_t value_len, int flags)
666 {
667 	struct jfs_ea_list *ealist;
668 	struct jfs_ea *ea, *old_ea = NULL, *next_ea = NULL;
669 	struct ea_buffer ea_buf;
670 	int old_ea_size = 0;
671 	int xattr_size;
672 	int new_size;
673 	int namelen = strlen(name);
674 	int found = 0;
675 	int rc;
676 	int length;
677 
678 	down_write(&JFS_IP(inode)->xattr_sem);
679 
680 	xattr_size = ea_get(inode, &ea_buf, 0);
681 	if (xattr_size < 0) {
682 		rc = xattr_size;
683 		goto out;
684 	}
685 
686       again:
687 	ealist = (struct jfs_ea_list *) ea_buf.xattr;
688 	new_size = sizeof (struct jfs_ea_list);
689 
690 	if (xattr_size) {
691 		for (ea = FIRST_EA(ealist); ea < END_EALIST(ealist);
692 		     ea = NEXT_EA(ea)) {
693 			if ((namelen == ea->namelen) &&
694 			    (memcmp(name, ea->name, namelen) == 0)) {
695 				found = 1;
696 				if (flags & XATTR_CREATE) {
697 					rc = -EEXIST;
698 					goto release;
699 				}
700 				old_ea = ea;
701 				old_ea_size = EA_SIZE(ea);
702 				next_ea = NEXT_EA(ea);
703 			} else
704 				new_size += EA_SIZE(ea);
705 		}
706 	}
707 
708 	if (!found) {
709 		if (flags & XATTR_REPLACE) {
710 			rc = -ENODATA;
711 			goto release;
712 		}
713 		if (value == NULL) {
714 			rc = 0;
715 			goto release;
716 		}
717 	}
718 	if (value)
719 		new_size += sizeof (struct jfs_ea) + namelen + 1 + value_len;
720 
721 	if (new_size > ea_buf.max_size) {
722 		/*
723 		 * We need to allocate more space for merged ea list.
724 		 * We should only have loop to again: once.
725 		 */
726 		ea_release(inode, &ea_buf);
727 		xattr_size = ea_get(inode, &ea_buf, new_size);
728 		if (xattr_size < 0) {
729 			rc = xattr_size;
730 			goto out;
731 		}
732 		goto again;
733 	}
734 
735 	/* Remove old ea of the same name */
736 	if (found) {
737 		/* number of bytes following target EA */
738 		length = (char *) END_EALIST(ealist) - (char *) next_ea;
739 		if (length > 0)
740 			memmove(old_ea, next_ea, length);
741 		xattr_size -= old_ea_size;
742 	}
743 
744 	/* Add new entry to the end */
745 	if (value) {
746 		if (xattr_size == 0)
747 			/* Completely new ea list */
748 			xattr_size = sizeof (struct jfs_ea_list);
749 
750 		/*
751 		 * The size of EA value is limitted by on-disk format up to
752 		 *  __le16, there would be an overflow if the size is equal
753 		 * to XATTR_SIZE_MAX (65536).  In order to avoid this issue,
754 		 * we can pre-checkup the value size against USHRT_MAX, and
755 		 * return -E2BIG in this case, which is consistent with the
756 		 * VFS setxattr interface.
757 		 */
758 		if (value_len >= USHRT_MAX) {
759 			rc = -E2BIG;
760 			goto release;
761 		}
762 
763 		ea = (struct jfs_ea *) ((char *) ealist + xattr_size);
764 		ea->flag = 0;
765 		ea->namelen = namelen;
766 		ea->valuelen = (cpu_to_le16(value_len));
767 		memcpy(ea->name, name, namelen);
768 		ea->name[namelen] = 0;
769 		if (value_len)
770 			memcpy(&ea->name[namelen + 1], value, value_len);
771 		xattr_size += EA_SIZE(ea);
772 	}
773 
774 	/* DEBUG - If we did this right, these number match */
775 	if (xattr_size != new_size) {
776 		printk(KERN_ERR
777 		       "__jfs_setxattr: xattr_size = %d, new_size = %d\n",
778 		       xattr_size, new_size);
779 
780 		rc = -EINVAL;
781 		goto release;
782 	}
783 
784 	/*
785 	 * If we're left with an empty list, there's no ea
786 	 */
787 	if (new_size == sizeof (struct jfs_ea_list))
788 		new_size = 0;
789 
790 	ealist->size = cpu_to_le32(new_size);
791 
792 	rc = ea_put(tid, inode, &ea_buf, new_size);
793 
794 	goto out;
795       release:
796 	ea_release(inode, &ea_buf);
797       out:
798 	up_write(&JFS_IP(inode)->xattr_sem);
799 
800 	return rc;
801 }
802 
__jfs_getxattr(struct inode * inode,const char * name,void * data,size_t buf_size)803 ssize_t __jfs_getxattr(struct inode *inode, const char *name, void *data,
804 		       size_t buf_size)
805 {
806 	struct jfs_ea_list *ealist;
807 	struct jfs_ea *ea, *ealist_end;
808 	struct ea_buffer ea_buf;
809 	int xattr_size;
810 	ssize_t size;
811 	int namelen = strlen(name);
812 	char *value;
813 
814 	down_read(&JFS_IP(inode)->xattr_sem);
815 
816 	xattr_size = ea_get(inode, &ea_buf, 0);
817 
818 	if (xattr_size < 0) {
819 		size = xattr_size;
820 		goto out;
821 	}
822 
823 	if (xattr_size == 0)
824 		goto not_found;
825 
826 	ealist = (struct jfs_ea_list *) ea_buf.xattr;
827 	ealist_end = END_EALIST(ealist);
828 
829 	/* Find the named attribute */
830 	for (ea = FIRST_EA(ealist); ea < ealist_end; ea = NEXT_EA(ea)) {
831 		if (unlikely(ea + 1 > ealist_end) ||
832 		    unlikely(NEXT_EA(ea) > ealist_end)) {
833 			size = -EUCLEAN;
834 			goto release;
835 		}
836 
837 		if ((namelen == ea->namelen) &&
838 		    memcmp(name, ea->name, namelen) == 0) {
839 			/* Found it */
840 			size = le16_to_cpu(ea->valuelen);
841 			if (!data)
842 				goto release;
843 			else if (size > buf_size) {
844 				size = -ERANGE;
845 				goto release;
846 			}
847 			value = ((char *) &ea->name) + ea->namelen + 1;
848 			memcpy(data, value, size);
849 			goto release;
850 		}
851 	}
852       not_found:
853 	size = -ENODATA;
854       release:
855 	ea_release(inode, &ea_buf);
856       out:
857 	up_read(&JFS_IP(inode)->xattr_sem);
858 
859 	return size;
860 }
861 
862 /*
863  * No special permissions are needed to list attributes except for trusted.*
864  */
can_list(struct jfs_ea * ea)865 static inline int can_list(struct jfs_ea *ea)
866 {
867 	return (strncmp(ea->name, XATTR_TRUSTED_PREFIX,
868 			    XATTR_TRUSTED_PREFIX_LEN) ||
869 		capable(CAP_SYS_ADMIN));
870 }
871 
jfs_listxattr(struct dentry * dentry,char * data,size_t buf_size)872 ssize_t jfs_listxattr(struct dentry * dentry, char *data, size_t buf_size)
873 {
874 	struct inode *inode = d_inode(dentry);
875 	char *buffer;
876 	ssize_t size = 0;
877 	int xattr_size;
878 	struct jfs_ea_list *ealist;
879 	struct jfs_ea *ea, *ealist_end;
880 	struct ea_buffer ea_buf;
881 
882 	down_read(&JFS_IP(inode)->xattr_sem);
883 
884 	xattr_size = ea_get(inode, &ea_buf, 0);
885 	if (xattr_size < 0) {
886 		size = xattr_size;
887 		goto out;
888 	}
889 
890 	if (xattr_size == 0)
891 		goto release;
892 
893 	ealist = (struct jfs_ea_list *) ea_buf.xattr;
894 	ealist_end = END_EALIST(ealist);
895 
896 	/* compute required size of list */
897 	for (ea = FIRST_EA(ealist); ea < ealist_end; ea = NEXT_EA(ea)) {
898 		if (unlikely(ea + 1 > ealist_end) ||
899 		    unlikely(NEXT_EA(ea) > ealist_end)) {
900 			size = -EUCLEAN;
901 			goto release;
902 		}
903 
904 		if (can_list(ea))
905 			size += name_size(ea) + 1;
906 	}
907 
908 	if (!data)
909 		goto release;
910 
911 	if (size > buf_size) {
912 		size = -ERANGE;
913 		goto release;
914 	}
915 
916 	/* Copy attribute names to buffer */
917 	buffer = data;
918 	for (ea = FIRST_EA(ealist); ea < END_EALIST(ealist); ea = NEXT_EA(ea)) {
919 		if (can_list(ea)) {
920 			int namelen = copy_name(buffer, ea);
921 			buffer += namelen + 1;
922 		}
923 	}
924 
925       release:
926 	ea_release(inode, &ea_buf);
927       out:
928 	up_read(&JFS_IP(inode)->xattr_sem);
929 	return size;
930 }
931 
__jfs_xattr_set(struct inode * inode,const char * name,const void * value,size_t size,int flags)932 static int __jfs_xattr_set(struct inode *inode, const char *name,
933 			   const void *value, size_t size, int flags)
934 {
935 	struct jfs_inode_info *ji = JFS_IP(inode);
936 	tid_t tid;
937 	int rc;
938 
939 	tid = txBegin(inode->i_sb, 0);
940 	mutex_lock(&ji->commit_mutex);
941 	rc = __jfs_setxattr(tid, inode, name, value, size, flags);
942 	if (!rc)
943 		rc = txCommit(tid, 1, &inode, 0);
944 	txEnd(tid);
945 	mutex_unlock(&ji->commit_mutex);
946 
947 	return rc;
948 }
949 
jfs_xattr_get(const struct xattr_handler * handler,struct dentry * unused,struct inode * inode,const char * name,void * value,size_t size)950 static int jfs_xattr_get(const struct xattr_handler *handler,
951 			 struct dentry *unused, struct inode *inode,
952 			 const char *name, void *value, size_t size)
953 {
954 	name = xattr_full_name(handler, name);
955 	return __jfs_getxattr(inode, name, value, size);
956 }
957 
jfs_xattr_set(const struct xattr_handler * handler,struct mnt_idmap * idmap,struct dentry * unused,struct inode * inode,const char * name,const void * value,size_t size,int flags)958 static int jfs_xattr_set(const struct xattr_handler *handler,
959 			 struct mnt_idmap *idmap,
960 			 struct dentry *unused, struct inode *inode,
961 			 const char *name, const void *value,
962 			 size_t size, int flags)
963 {
964 	name = xattr_full_name(handler, name);
965 	return __jfs_xattr_set(inode, name, value, size, flags);
966 }
967 
jfs_xattr_get_os2(const struct xattr_handler * handler,struct dentry * unused,struct inode * inode,const char * name,void * value,size_t size)968 static int jfs_xattr_get_os2(const struct xattr_handler *handler,
969 			     struct dentry *unused, struct inode *inode,
970 			     const char *name, void *value, size_t size)
971 {
972 	if (is_known_namespace(name))
973 		return -EOPNOTSUPP;
974 	return __jfs_getxattr(inode, name, value, size);
975 }
976 
jfs_xattr_set_os2(const struct xattr_handler * handler,struct mnt_idmap * idmap,struct dentry * unused,struct inode * inode,const char * name,const void * value,size_t size,int flags)977 static int jfs_xattr_set_os2(const struct xattr_handler *handler,
978 			     struct mnt_idmap *idmap,
979 			     struct dentry *unused, struct inode *inode,
980 			     const char *name, const void *value,
981 			     size_t size, int flags)
982 {
983 	if (is_known_namespace(name))
984 		return -EOPNOTSUPP;
985 	return __jfs_xattr_set(inode, name, value, size, flags);
986 }
987 
988 static const struct xattr_handler jfs_user_xattr_handler = {
989 	.prefix = XATTR_USER_PREFIX,
990 	.get = jfs_xattr_get,
991 	.set = jfs_xattr_set,
992 };
993 
994 static const struct xattr_handler jfs_os2_xattr_handler = {
995 	.prefix = XATTR_OS2_PREFIX,
996 	.get = jfs_xattr_get_os2,
997 	.set = jfs_xattr_set_os2,
998 };
999 
1000 static const struct xattr_handler jfs_security_xattr_handler = {
1001 	.prefix = XATTR_SECURITY_PREFIX,
1002 	.get = jfs_xattr_get,
1003 	.set = jfs_xattr_set,
1004 };
1005 
1006 static const struct xattr_handler jfs_trusted_xattr_handler = {
1007 	.prefix = XATTR_TRUSTED_PREFIX,
1008 	.get = jfs_xattr_get,
1009 	.set = jfs_xattr_set,
1010 };
1011 
1012 const struct xattr_handler *jfs_xattr_handlers[] = {
1013 	&jfs_os2_xattr_handler,
1014 	&jfs_user_xattr_handler,
1015 	&jfs_security_xattr_handler,
1016 	&jfs_trusted_xattr_handler,
1017 	NULL,
1018 };
1019 
1020 
1021 #ifdef CONFIG_JFS_SECURITY
jfs_initxattrs(struct inode * inode,const struct xattr * xattr_array,void * fs_info)1022 static int jfs_initxattrs(struct inode *inode, const struct xattr *xattr_array,
1023 			  void *fs_info)
1024 {
1025 	const struct xattr *xattr;
1026 	tid_t *tid = fs_info;
1027 	char *name;
1028 	int err = 0;
1029 
1030 	for (xattr = xattr_array; xattr->name != NULL; xattr++) {
1031 		name = kmalloc(XATTR_SECURITY_PREFIX_LEN +
1032 			       strlen(xattr->name) + 1, GFP_NOFS);
1033 		if (!name) {
1034 			err = -ENOMEM;
1035 			break;
1036 		}
1037 		strcpy(name, XATTR_SECURITY_PREFIX);
1038 		strcpy(name + XATTR_SECURITY_PREFIX_LEN, xattr->name);
1039 
1040 		err = __jfs_setxattr(*tid, inode, name,
1041 				     xattr->value, xattr->value_len, 0);
1042 		kfree(name);
1043 		if (err < 0)
1044 			break;
1045 	}
1046 	return err;
1047 }
1048 
jfs_init_security(tid_t tid,struct inode * inode,struct inode * dir,const struct qstr * qstr)1049 int jfs_init_security(tid_t tid, struct inode *inode, struct inode *dir,
1050 		      const struct qstr *qstr)
1051 {
1052 	return security_inode_init_security(inode, dir, qstr,
1053 					    &jfs_initxattrs, &tid);
1054 }
1055 #endif
1056