Linux Audio

Check our new training course

In-person Linux kernel drivers training

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