Linux Audio

Check our new training course

Linux debugging, profiling, tracing and performance analysis training

Apr 14-17, 2025
Register
Loading...
v6.9.4
   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	/* When fsck.jfs clears a bad ea, it doesn't clear the size */
 438	if (ji->ea.flag == 0)
 439		ea_size = 0;
 440
 441	if (ea_size == 0) {
 442		if (min_size == 0) {
 443			ea_buf->flag = 0;
 444			ea_buf->max_size = 0;
 445			ea_buf->xattr = NULL;
 446			return 0;
 447		}
 448		if ((min_size <= sizeof (ji->i_inline_ea)) &&
 449		    (ji->mode2 & INLINEEA)) {
 450			ea_buf->flag = EA_INLINE | EA_NEW;
 451			ea_buf->max_size = sizeof (ji->i_inline_ea);
 452			ea_buf->xattr = (struct jfs_ea_list *) ji->i_inline_ea;
 453			DXDlength(&ea_buf->new_ea, 0);
 454			DXDaddress(&ea_buf->new_ea, 0);
 455			ea_buf->new_ea.flag = DXD_INLINE;
 456			DXDsize(&ea_buf->new_ea, min_size);
 457			return 0;
 458		}
 459		current_blocks = 0;
 460	} else if (ji->ea.flag & DXD_INLINE) {
 461		if (min_size <= sizeof (ji->i_inline_ea)) {
 462			ea_buf->flag = EA_INLINE;
 463			ea_buf->max_size = sizeof (ji->i_inline_ea);
 464			ea_buf->xattr = (struct jfs_ea_list *) ji->i_inline_ea;
 465			goto size_check;
 466		}
 467		current_blocks = 0;
 468	} else {
 469		if (!(ji->ea.flag & DXD_EXTENT)) {
 470			jfs_error(sb, "invalid ea.flag\n");
 471			return -EIO;
 472		}
 473		current_blocks = (ea_size + sb->s_blocksize - 1) >>
 474		    sb->s_blocksize_bits;
 475	}
 476	size = max(min_size, ea_size);
 477
 478	if (size > PSIZE) {
 479		/*
 480		 * To keep the rest of the code simple.  Allocate a
 481		 * contiguous buffer to work with. Make the buffer large
 482		 * enough to make use of the whole extent.
 483		 */
 484		ea_buf->max_size = (size + sb->s_blocksize - 1) &
 485		    ~(sb->s_blocksize - 1);
 486
 487		ea_buf->xattr = kmalloc(ea_buf->max_size, GFP_KERNEL);
 488		if (ea_buf->xattr == NULL)
 489			return -ENOMEM;
 490
 491		ea_buf->flag = EA_MALLOC;
 492
 493		if (ea_size == 0)
 494			return 0;
 495
 496		if ((rc = ea_read(inode, ea_buf->xattr))) {
 497			kfree(ea_buf->xattr);
 498			ea_buf->xattr = NULL;
 499			return rc;
 500		}
 501		goto size_check;
 502	}
 503	blocks_needed = (min_size + sb->s_blocksize - 1) >>
 504	    sb->s_blocksize_bits;
 505
 506	if (blocks_needed > current_blocks) {
 507		/* Allocate new blocks to quota. */
 508		rc = dquot_alloc_block(inode, blocks_needed);
 509		if (rc)
 510			return -EDQUOT;
 511
 512		quota_allocation = blocks_needed;
 513
 514		rc = dbAlloc(inode, INOHINT(inode), (s64) blocks_needed,
 515			     &blkno);
 516		if (rc)
 517			goto clean_up;
 518
 519		DXDlength(&ea_buf->new_ea, blocks_needed);
 520		DXDaddress(&ea_buf->new_ea, blkno);
 521		ea_buf->new_ea.flag = DXD_EXTENT;
 522		DXDsize(&ea_buf->new_ea, min_size);
 523
 524		ea_buf->flag = EA_EXTENT | EA_NEW;
 525
 526		ea_buf->mp = get_metapage(inode, blkno,
 527					  blocks_needed << sb->s_blocksize_bits,
 528					  1);
 529		if (ea_buf->mp == NULL) {
 530			dbFree(inode, blkno, (s64) blocks_needed);
 531			rc = -EIO;
 532			goto clean_up;
 533		}
 534		ea_buf->xattr = ea_buf->mp->data;
 535		ea_buf->max_size = (min_size + sb->s_blocksize - 1) &
 536		    ~(sb->s_blocksize - 1);
 537		if (ea_size == 0)
 538			return 0;
 539		if ((rc = ea_read(inode, ea_buf->xattr))) {
 540			discard_metapage(ea_buf->mp);
 541			dbFree(inode, blkno, (s64) blocks_needed);
 542			goto clean_up;
 543		}
 544		goto size_check;
 545	}
 546	ea_buf->flag = EA_EXTENT;
 547	ea_buf->mp = read_metapage(inode, addressDXD(&ji->ea),
 548				   lengthDXD(&ji->ea) << sb->s_blocksize_bits,
 549				   1);
 550	if (ea_buf->mp == NULL) {
 551		rc = -EIO;
 552		goto clean_up;
 553	}
 554	ea_buf->xattr = ea_buf->mp->data;
 555	ea_buf->max_size = (ea_size + sb->s_blocksize - 1) &
 556	    ~(sb->s_blocksize - 1);
 557
 558      size_check:
 559	if (EALIST_SIZE(ea_buf->xattr) != ea_size) {
 
 
 560		printk(KERN_ERR "ea_get: invalid extended attribute\n");
 561		print_hex_dump(KERN_ERR, "", DUMP_PREFIX_ADDRESS, 16, 1,
 562				     ea_buf->xattr, ea_size, 1);
 563		ea_release(inode, ea_buf);
 564		rc = -EIO;
 565		goto clean_up;
 566	}
 567
 568	return ea_size;
 569
 570      clean_up:
 571	/* Rollback quota allocation */
 572	if (quota_allocation)
 573		dquot_free_block(inode, quota_allocation);
 574
 575	return (rc);
 576}
 577
 578static void ea_release(struct inode *inode, struct ea_buffer *ea_buf)
 579{
 580	if (ea_buf->flag & EA_MALLOC)
 581		kfree(ea_buf->xattr);
 582	else if (ea_buf->flag & EA_EXTENT) {
 583		assert(ea_buf->mp);
 584		release_metapage(ea_buf->mp);
 585
 586		if (ea_buf->flag & EA_NEW)
 587			dbFree(inode, addressDXD(&ea_buf->new_ea),
 588			       lengthDXD(&ea_buf->new_ea));
 589	}
 590}
 591
 592static int ea_put(tid_t tid, struct inode *inode, struct ea_buffer *ea_buf,
 593		  int new_size)
 594{
 595	struct jfs_inode_info *ji = JFS_IP(inode);
 596	unsigned long old_blocks, new_blocks;
 597	int rc = 0;
 598
 599	if (new_size == 0) {
 600		ea_release(inode, ea_buf);
 601		ea_buf = NULL;
 602	} else if (ea_buf->flag & EA_INLINE) {
 603		assert(new_size <= sizeof (ji->i_inline_ea));
 604		ji->mode2 &= ~INLINEEA;
 605		ea_buf->new_ea.flag = DXD_INLINE;
 606		DXDsize(&ea_buf->new_ea, new_size);
 607		DXDaddress(&ea_buf->new_ea, 0);
 608		DXDlength(&ea_buf->new_ea, 0);
 609	} else if (ea_buf->flag & EA_MALLOC) {
 610		rc = ea_write(inode, ea_buf->xattr, new_size, &ea_buf->new_ea);
 611		kfree(ea_buf->xattr);
 612	} else if (ea_buf->flag & EA_NEW) {
 613		/* We have already allocated a new dxd */
 614		flush_metapage(ea_buf->mp);
 615	} else {
 616		/* ->xattr must point to original ea's metapage */
 617		rc = ea_write(inode, ea_buf->xattr, new_size, &ea_buf->new_ea);
 618		discard_metapage(ea_buf->mp);
 619	}
 620	if (rc)
 621		return rc;
 622
 623	old_blocks = new_blocks = 0;
 624
 625	if (ji->ea.flag & DXD_EXTENT) {
 626		invalidate_dxd_metapages(inode, ji->ea);
 627		old_blocks = lengthDXD(&ji->ea);
 628	}
 629
 630	if (ea_buf) {
 631		txEA(tid, inode, &ji->ea, &ea_buf->new_ea);
 632		if (ea_buf->new_ea.flag & DXD_EXTENT) {
 633			new_blocks = lengthDXD(&ea_buf->new_ea);
 634			if (ji->ea.flag & DXD_INLINE)
 635				ji->mode2 |= INLINEEA;
 636		}
 637		ji->ea = ea_buf->new_ea;
 638	} else {
 639		txEA(tid, inode, &ji->ea, NULL);
 640		if (ji->ea.flag & DXD_INLINE)
 641			ji->mode2 |= INLINEEA;
 642		ji->ea.flag = 0;
 643		ji->ea.size = 0;
 644	}
 645
 646	/* If old blocks exist, they must be removed from quota allocation. */
 647	if (old_blocks)
 648		dquot_free_block(inode, old_blocks);
 649
 650	inode_set_ctime_current(inode);
 651
 652	return 0;
 653}
 654
 655int __jfs_setxattr(tid_t tid, struct inode *inode, const char *name,
 656		   const void *value, size_t value_len, int flags)
 657{
 658	struct jfs_ea_list *ealist;
 659	struct jfs_ea *ea, *old_ea = NULL, *next_ea = NULL;
 660	struct ea_buffer ea_buf;
 661	int old_ea_size = 0;
 662	int xattr_size;
 663	int new_size;
 664	int namelen = strlen(name);
 665	int found = 0;
 666	int rc;
 667	int length;
 668
 669	down_write(&JFS_IP(inode)->xattr_sem);
 670
 671	xattr_size = ea_get(inode, &ea_buf, 0);
 672	if (xattr_size < 0) {
 673		rc = xattr_size;
 674		goto out;
 675	}
 676
 677      again:
 678	ealist = (struct jfs_ea_list *) ea_buf.xattr;
 679	new_size = sizeof (struct jfs_ea_list);
 680
 681	if (xattr_size) {
 682		for (ea = FIRST_EA(ealist); ea < END_EALIST(ealist);
 683		     ea = NEXT_EA(ea)) {
 684			if ((namelen == ea->namelen) &&
 685			    (memcmp(name, ea->name, namelen) == 0)) {
 686				found = 1;
 687				if (flags & XATTR_CREATE) {
 688					rc = -EEXIST;
 689					goto release;
 690				}
 691				old_ea = ea;
 692				old_ea_size = EA_SIZE(ea);
 693				next_ea = NEXT_EA(ea);
 694			} else
 695				new_size += EA_SIZE(ea);
 696		}
 697	}
 698
 699	if (!found) {
 700		if (flags & XATTR_REPLACE) {
 701			rc = -ENODATA;
 702			goto release;
 703		}
 704		if (value == NULL) {
 705			rc = 0;
 706			goto release;
 707		}
 708	}
 709	if (value)
 710		new_size += sizeof (struct jfs_ea) + namelen + 1 + value_len;
 711
 712	if (new_size > ea_buf.max_size) {
 713		/*
 714		 * We need to allocate more space for merged ea list.
 715		 * We should only have loop to again: once.
 716		 */
 717		ea_release(inode, &ea_buf);
 718		xattr_size = ea_get(inode, &ea_buf, new_size);
 719		if (xattr_size < 0) {
 720			rc = xattr_size;
 721			goto out;
 722		}
 723		goto again;
 724	}
 725
 726	/* Remove old ea of the same name */
 727	if (found) {
 728		/* number of bytes following target EA */
 729		length = (char *) END_EALIST(ealist) - (char *) next_ea;
 730		if (length > 0)
 731			memmove(old_ea, next_ea, length);
 732		xattr_size -= old_ea_size;
 733	}
 734
 735	/* Add new entry to the end */
 736	if (value) {
 737		if (xattr_size == 0)
 738			/* Completely new ea list */
 739			xattr_size = sizeof (struct jfs_ea_list);
 740
 741		/*
 742		 * The size of EA value is limitted by on-disk format up to
 743		 *  __le16, there would be an overflow if the size is equal
 744		 * to XATTR_SIZE_MAX (65536).  In order to avoid this issue,
 745		 * we can pre-checkup the value size against USHRT_MAX, and
 746		 * return -E2BIG in this case, which is consistent with the
 747		 * VFS setxattr interface.
 748		 */
 749		if (value_len >= USHRT_MAX) {
 750			rc = -E2BIG;
 751			goto release;
 752		}
 753
 754		ea = (struct jfs_ea *) ((char *) ealist + xattr_size);
 755		ea->flag = 0;
 756		ea->namelen = namelen;
 757		ea->valuelen = (cpu_to_le16(value_len));
 758		memcpy(ea->name, name, namelen);
 759		ea->name[namelen] = 0;
 760		if (value_len)
 761			memcpy(&ea->name[namelen + 1], value, value_len);
 762		xattr_size += EA_SIZE(ea);
 763	}
 764
 765	/* DEBUG - If we did this right, these number match */
 766	if (xattr_size != new_size) {
 767		printk(KERN_ERR
 768		       "__jfs_setxattr: xattr_size = %d, new_size = %d\n",
 769		       xattr_size, new_size);
 770
 771		rc = -EINVAL;
 772		goto release;
 773	}
 774
 775	/*
 776	 * If we're left with an empty list, there's no ea
 777	 */
 778	if (new_size == sizeof (struct jfs_ea_list))
 779		new_size = 0;
 780
 781	ealist->size = cpu_to_le32(new_size);
 782
 783	rc = ea_put(tid, inode, &ea_buf, new_size);
 784
 785	goto out;
 786      release:
 787	ea_release(inode, &ea_buf);
 788      out:
 789	up_write(&JFS_IP(inode)->xattr_sem);
 790
 791	return rc;
 792}
 793
 794ssize_t __jfs_getxattr(struct inode *inode, const char *name, void *data,
 795		       size_t buf_size)
 796{
 797	struct jfs_ea_list *ealist;
 798	struct jfs_ea *ea;
 799	struct ea_buffer ea_buf;
 800	int xattr_size;
 801	ssize_t size;
 802	int namelen = strlen(name);
 803	char *value;
 804
 805	down_read(&JFS_IP(inode)->xattr_sem);
 806
 807	xattr_size = ea_get(inode, &ea_buf, 0);
 808
 809	if (xattr_size < 0) {
 810		size = xattr_size;
 811		goto out;
 812	}
 813
 814	if (xattr_size == 0)
 815		goto not_found;
 816
 817	ealist = (struct jfs_ea_list *) ea_buf.xattr;
 
 818
 819	/* Find the named attribute */
 820	for (ea = FIRST_EA(ealist); ea < END_EALIST(ealist); ea = NEXT_EA(ea))
 
 
 
 
 
 
 821		if ((namelen == ea->namelen) &&
 822		    memcmp(name, ea->name, namelen) == 0) {
 823			/* Found it */
 824			size = le16_to_cpu(ea->valuelen);
 825			if (!data)
 826				goto release;
 827			else if (size > buf_size) {
 828				size = -ERANGE;
 829				goto release;
 830			}
 831			value = ((char *) &ea->name) + ea->namelen + 1;
 832			memcpy(data, value, size);
 833			goto release;
 834		}
 
 835      not_found:
 836	size = -ENODATA;
 837      release:
 838	ea_release(inode, &ea_buf);
 839      out:
 840	up_read(&JFS_IP(inode)->xattr_sem);
 841
 842	return size;
 843}
 844
 845/*
 846 * No special permissions are needed to list attributes except for trusted.*
 847 */
 848static inline int can_list(struct jfs_ea *ea)
 849{
 850	return (strncmp(ea->name, XATTR_TRUSTED_PREFIX,
 851			    XATTR_TRUSTED_PREFIX_LEN) ||
 852		capable(CAP_SYS_ADMIN));
 853}
 854
 855ssize_t jfs_listxattr(struct dentry * dentry, char *data, size_t buf_size)
 856{
 857	struct inode *inode = d_inode(dentry);
 858	char *buffer;
 859	ssize_t size = 0;
 860	int xattr_size;
 861	struct jfs_ea_list *ealist;
 862	struct jfs_ea *ea;
 863	struct ea_buffer ea_buf;
 864
 865	down_read(&JFS_IP(inode)->xattr_sem);
 866
 867	xattr_size = ea_get(inode, &ea_buf, 0);
 868	if (xattr_size < 0) {
 869		size = xattr_size;
 870		goto out;
 871	}
 872
 873	if (xattr_size == 0)
 874		goto release;
 875
 876	ealist = (struct jfs_ea_list *) ea_buf.xattr;
 
 877
 878	/* compute required size of list */
 879	for (ea = FIRST_EA(ealist); ea < END_EALIST(ealist); ea = NEXT_EA(ea)) {
 
 
 
 
 
 
 880		if (can_list(ea))
 881			size += name_size(ea) + 1;
 882	}
 883
 884	if (!data)
 885		goto release;
 886
 887	if (size > buf_size) {
 888		size = -ERANGE;
 889		goto release;
 890	}
 891
 892	/* Copy attribute names to buffer */
 893	buffer = data;
 894	for (ea = FIRST_EA(ealist); ea < END_EALIST(ealist); ea = NEXT_EA(ea)) {
 895		if (can_list(ea)) {
 896			int namelen = copy_name(buffer, ea);
 897			buffer += namelen + 1;
 898		}
 899	}
 900
 901      release:
 902	ea_release(inode, &ea_buf);
 903      out:
 904	up_read(&JFS_IP(inode)->xattr_sem);
 905	return size;
 906}
 907
 908static int __jfs_xattr_set(struct inode *inode, const char *name,
 909			   const void *value, size_t size, int flags)
 910{
 911	struct jfs_inode_info *ji = JFS_IP(inode);
 912	tid_t tid;
 913	int rc;
 914
 915	tid = txBegin(inode->i_sb, 0);
 916	mutex_lock(&ji->commit_mutex);
 917	rc = __jfs_setxattr(tid, inode, name, value, size, flags);
 918	if (!rc)
 919		rc = txCommit(tid, 1, &inode, 0);
 920	txEnd(tid);
 921	mutex_unlock(&ji->commit_mutex);
 922
 923	return rc;
 924}
 925
 926static int jfs_xattr_get(const struct xattr_handler *handler,
 927			 struct dentry *unused, struct inode *inode,
 928			 const char *name, void *value, size_t size)
 929{
 930	name = xattr_full_name(handler, name);
 931	return __jfs_getxattr(inode, name, value, size);
 932}
 933
 934static int jfs_xattr_set(const struct xattr_handler *handler,
 935			 struct mnt_idmap *idmap,
 936			 struct dentry *unused, struct inode *inode,
 937			 const char *name, const void *value,
 938			 size_t size, int flags)
 939{
 940	name = xattr_full_name(handler, name);
 941	return __jfs_xattr_set(inode, name, value, size, flags);
 942}
 943
 944static int jfs_xattr_get_os2(const struct xattr_handler *handler,
 945			     struct dentry *unused, struct inode *inode,
 946			     const char *name, void *value, size_t size)
 947{
 948	if (is_known_namespace(name))
 949		return -EOPNOTSUPP;
 950	return __jfs_getxattr(inode, name, value, size);
 951}
 952
 953static int jfs_xattr_set_os2(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	if (is_known_namespace(name))
 960		return -EOPNOTSUPP;
 961	return __jfs_xattr_set(inode, name, value, size, flags);
 962}
 963
 964static const struct xattr_handler jfs_user_xattr_handler = {
 965	.prefix = XATTR_USER_PREFIX,
 966	.get = jfs_xattr_get,
 967	.set = jfs_xattr_set,
 968};
 969
 970static const struct xattr_handler jfs_os2_xattr_handler = {
 971	.prefix = XATTR_OS2_PREFIX,
 972	.get = jfs_xattr_get_os2,
 973	.set = jfs_xattr_set_os2,
 974};
 975
 976static const struct xattr_handler jfs_security_xattr_handler = {
 977	.prefix = XATTR_SECURITY_PREFIX,
 978	.get = jfs_xattr_get,
 979	.set = jfs_xattr_set,
 980};
 981
 982static const struct xattr_handler jfs_trusted_xattr_handler = {
 983	.prefix = XATTR_TRUSTED_PREFIX,
 984	.get = jfs_xattr_get,
 985	.set = jfs_xattr_set,
 986};
 987
 988const struct xattr_handler * const jfs_xattr_handlers[] = {
 989	&jfs_os2_xattr_handler,
 990	&jfs_user_xattr_handler,
 991	&jfs_security_xattr_handler,
 992	&jfs_trusted_xattr_handler,
 993	NULL,
 994};
 995
 996
 997#ifdef CONFIG_JFS_SECURITY
 998static int jfs_initxattrs(struct inode *inode, const struct xattr *xattr_array,
 999			  void *fs_info)
1000{
1001	const struct xattr *xattr;
1002	tid_t *tid = fs_info;
1003	char *name;
1004	int err = 0;
1005
1006	for (xattr = xattr_array; xattr->name != NULL; xattr++) {
1007		name = kmalloc(XATTR_SECURITY_PREFIX_LEN +
1008			       strlen(xattr->name) + 1, GFP_NOFS);
1009		if (!name) {
1010			err = -ENOMEM;
1011			break;
1012		}
1013		strcpy(name, XATTR_SECURITY_PREFIX);
1014		strcpy(name + XATTR_SECURITY_PREFIX_LEN, xattr->name);
1015
1016		err = __jfs_setxattr(*tid, inode, name,
1017				     xattr->value, xattr->value_len, 0);
1018		kfree(name);
1019		if (err < 0)
1020			break;
1021	}
1022	return err;
1023}
1024
1025int jfs_init_security(tid_t tid, struct inode *inode, struct inode *dir,
1026		      const struct qstr *qstr)
1027{
1028	return security_inode_init_security(inode, dir, qstr,
1029					    &jfs_initxattrs, &tid);
1030}
1031#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