xref: /openbmc/linux/fs/jfs/xattr.c (revision 59301226)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *   Copyright (C) International Business Machines  Corp., 2000-2004
31da177e4SLinus Torvalds  *   Copyright (C) Christoph Hellwig, 2002
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  *   This program is free software;  you can redistribute it and/or modify
61da177e4SLinus Torvalds  *   it under the terms of the GNU General Public License as published by
71da177e4SLinus Torvalds  *   the Free Software Foundation; either version 2 of the License, or
81da177e4SLinus Torvalds  *   (at your option) any later version.
91da177e4SLinus Torvalds  *
101da177e4SLinus Torvalds  *   This program is distributed in the hope that it will be useful,
111da177e4SLinus Torvalds  *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
121da177e4SLinus Torvalds  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
131da177e4SLinus Torvalds  *   the GNU General Public License for more details.
141da177e4SLinus Torvalds  *
151da177e4SLinus Torvalds  *   You should have received a copy of the GNU General Public License
161da177e4SLinus Torvalds  *   along with this program;  if not, write to the Free Software
171da177e4SLinus Torvalds  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
181da177e4SLinus Torvalds  */
191da177e4SLinus Torvalds 
2016f7e0feSRandy Dunlap #include <linux/capability.h>
211da177e4SLinus Torvalds #include <linux/fs.h>
221da177e4SLinus Torvalds #include <linux/xattr.h>
239a59f452SChristoph Hellwig #include <linux/posix_acl_xattr.h>
245a0e3ad6STejun Heo #include <linux/slab.h>
251da177e4SLinus Torvalds #include <linux/quotaops.h>
261d15b10fSDave Kleikamp #include <linux/security.h>
271da177e4SLinus Torvalds #include "jfs_incore.h"
281da177e4SLinus Torvalds #include "jfs_superblock.h"
291da177e4SLinus Torvalds #include "jfs_dmap.h"
301da177e4SLinus Torvalds #include "jfs_debug.h"
311da177e4SLinus Torvalds #include "jfs_dinode.h"
321da177e4SLinus Torvalds #include "jfs_extent.h"
331da177e4SLinus Torvalds #include "jfs_metapage.h"
341da177e4SLinus Torvalds #include "jfs_xattr.h"
351da177e4SLinus Torvalds #include "jfs_acl.h"
361da177e4SLinus Torvalds 
371da177e4SLinus Torvalds /*
381da177e4SLinus Torvalds  *	jfs_xattr.c: extended attribute service
391da177e4SLinus Torvalds  *
401da177e4SLinus Torvalds  * Overall design --
411da177e4SLinus Torvalds  *
421da177e4SLinus Torvalds  * Format:
431da177e4SLinus Torvalds  *
441da177e4SLinus Torvalds  *   Extended attribute lists (jfs_ea_list) consist of an overall size (32 bit
451da177e4SLinus Torvalds  *   value) and a variable (0 or more) number of extended attribute
461da177e4SLinus Torvalds  *   entries.  Each extended attribute entry (jfs_ea) is a <name,value> double
471da177e4SLinus Torvalds  *   where <name> is constructed from a null-terminated ascii string
481da177e4SLinus Torvalds  *   (1 ... 255 bytes in the name) and <value> is arbitrary 8 bit data
491da177e4SLinus Torvalds  *   (1 ... 65535 bytes).  The in-memory format is
501da177e4SLinus Torvalds  *
511da177e4SLinus Torvalds  *   0       1        2        4                4 + namelen + 1
521da177e4SLinus Torvalds  *   +-------+--------+--------+----------------+-------------------+
531da177e4SLinus Torvalds  *   | Flags | Name   | Value  | Name String \0 | Data . . . .      |
541da177e4SLinus Torvalds  *   |       | Length | Length |                |                   |
551da177e4SLinus Torvalds  *   +-------+--------+--------+----------------+-------------------+
561da177e4SLinus Torvalds  *
571da177e4SLinus Torvalds  *   A jfs_ea_list then is structured as
581da177e4SLinus Torvalds  *
591da177e4SLinus Torvalds  *   0            4                   4 + EA_SIZE(ea1)
601da177e4SLinus Torvalds  *   +------------+-------------------+--------------------+-----
611da177e4SLinus Torvalds  *   | Overall EA | First FEA Element | Second FEA Element | .....
621da177e4SLinus Torvalds  *   | List Size  |                   |                    |
631da177e4SLinus Torvalds  *   +------------+-------------------+--------------------+-----
641da177e4SLinus Torvalds  *
651da177e4SLinus Torvalds  *   On-disk:
661da177e4SLinus Torvalds  *
671da177e4SLinus Torvalds  *	FEALISTs are stored on disk using blocks allocated by dbAlloc() and
681da177e4SLinus Torvalds  *	written directly. An EA list may be in-lined in the inode if there is
691da177e4SLinus Torvalds  *	sufficient room available.
701da177e4SLinus Torvalds  */
711da177e4SLinus Torvalds 
721da177e4SLinus Torvalds struct ea_buffer {
731da177e4SLinus Torvalds 	int flag;		/* Indicates what storage xattr points to */
741da177e4SLinus Torvalds 	int max_size;		/* largest xattr that fits in current buffer */
751da177e4SLinus Torvalds 	dxd_t new_ea;		/* dxd to replace ea when modifying xattr */
761da177e4SLinus Torvalds 	struct metapage *mp;	/* metapage containing ea list */
771da177e4SLinus Torvalds 	struct jfs_ea_list *xattr;	/* buffer containing ea list */
781da177e4SLinus Torvalds };
791da177e4SLinus Torvalds 
801da177e4SLinus Torvalds /*
811da177e4SLinus Torvalds  * ea_buffer.flag values
821da177e4SLinus Torvalds  */
831da177e4SLinus Torvalds #define EA_INLINE	0x0001
841da177e4SLinus Torvalds #define EA_EXTENT	0x0002
851da177e4SLinus Torvalds #define EA_NEW		0x0004
861da177e4SLinus Torvalds #define EA_MALLOC	0x0008
871da177e4SLinus Torvalds 
881da177e4SLinus Torvalds 
896c8f980cSAndreas Gruenbacher /*
906c8f980cSAndreas Gruenbacher  * Mapping of on-disk attribute names: for on-disk attribute names with an
916c8f980cSAndreas Gruenbacher  * unknown prefix (not "system.", "user.", "security.", or "trusted."), the
926c8f980cSAndreas Gruenbacher  * prefix "os2." is prepended.  On the way back to disk, "os2." prefixes are
936c8f980cSAndreas Gruenbacher  * stripped and we make sure that the remaining name does not start with one
946c8f980cSAndreas Gruenbacher  * of the know prefixes.
956c8f980cSAndreas Gruenbacher  */
966c8f980cSAndreas Gruenbacher 
97aca0fa34SDave Kleikamp static int is_known_namespace(const char *name)
98aca0fa34SDave Kleikamp {
99aca0fa34SDave Kleikamp 	if (strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN) &&
100aca0fa34SDave Kleikamp 	    strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN) &&
101aca0fa34SDave Kleikamp 	    strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN) &&
102aca0fa34SDave Kleikamp 	    strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN))
103aca0fa34SDave Kleikamp 		return false;
104aca0fa34SDave Kleikamp 
105aca0fa34SDave Kleikamp 	return true;
106aca0fa34SDave Kleikamp }
107aca0fa34SDave Kleikamp 
1081da177e4SLinus Torvalds static inline int name_size(struct jfs_ea *ea)
1091da177e4SLinus Torvalds {
1106c8f980cSAndreas Gruenbacher 	if (is_known_namespace(ea->name))
1111da177e4SLinus Torvalds 		return ea->namelen;
1126c8f980cSAndreas Gruenbacher 	else
1136c8f980cSAndreas Gruenbacher 		return ea->namelen + XATTR_OS2_PREFIX_LEN;
1141da177e4SLinus Torvalds }
1151da177e4SLinus Torvalds 
1161da177e4SLinus Torvalds static inline int copy_name(char *buffer, struct jfs_ea *ea)
1171da177e4SLinus Torvalds {
1181da177e4SLinus Torvalds 	int len = ea->namelen;
1191da177e4SLinus Torvalds 
1206c8f980cSAndreas Gruenbacher 	if (!is_known_namespace(ea->name)) {
1211da177e4SLinus Torvalds 		memcpy(buffer, XATTR_OS2_PREFIX, XATTR_OS2_PREFIX_LEN);
1221da177e4SLinus Torvalds 		buffer += XATTR_OS2_PREFIX_LEN;
1231da177e4SLinus Torvalds 		len += XATTR_OS2_PREFIX_LEN;
1241da177e4SLinus Torvalds 	}
1251da177e4SLinus Torvalds 	memcpy(buffer, ea->name, ea->namelen);
1261da177e4SLinus Torvalds 	buffer[ea->namelen] = 0;
1271da177e4SLinus Torvalds 
1281da177e4SLinus Torvalds 	return len;
1291da177e4SLinus Torvalds }
1301da177e4SLinus Torvalds 
1311da177e4SLinus Torvalds /* Forward references */
1321da177e4SLinus Torvalds static void ea_release(struct inode *inode, struct ea_buffer *ea_buf);
1331da177e4SLinus Torvalds 
1341da177e4SLinus Torvalds /*
1351da177e4SLinus Torvalds  * NAME: ea_write_inline
1361da177e4SLinus Torvalds  *
1371da177e4SLinus Torvalds  * FUNCTION: Attempt to write an EA inline if area is available
1381da177e4SLinus Torvalds  *
1391da177e4SLinus Torvalds  * PRE CONDITIONS:
1401da177e4SLinus Torvalds  *	Already verified that the specified EA is small enough to fit inline
1411da177e4SLinus Torvalds  *
1421da177e4SLinus Torvalds  * PARAMETERS:
1431da177e4SLinus Torvalds  *	ip	- Inode pointer
1441da177e4SLinus Torvalds  *	ealist	- EA list pointer
1451da177e4SLinus Torvalds  *	size	- size of ealist in bytes
1461da177e4SLinus Torvalds  *	ea	- dxd_t structure to be filled in with necessary EA information
1471da177e4SLinus Torvalds  *		  if we successfully copy the EA inline
1481da177e4SLinus Torvalds  *
1491da177e4SLinus Torvalds  * NOTES:
1501da177e4SLinus Torvalds  *	Checks if the inode's inline area is available.  If so, copies EA inline
1511da177e4SLinus Torvalds  *	and sets <ea> fields appropriately.  Otherwise, returns failure, EA will
1521da177e4SLinus Torvalds  *	have to be put into an extent.
1531da177e4SLinus Torvalds  *
1541da177e4SLinus Torvalds  * RETURNS: 0 for successful copy to inline area; -1 if area not available
1551da177e4SLinus Torvalds  */
1561da177e4SLinus Torvalds static int ea_write_inline(struct inode *ip, struct jfs_ea_list *ealist,
1571da177e4SLinus Torvalds 			   int size, dxd_t * ea)
1581da177e4SLinus Torvalds {
1591da177e4SLinus Torvalds 	struct jfs_inode_info *ji = JFS_IP(ip);
1601da177e4SLinus Torvalds 
1611da177e4SLinus Torvalds 	/*
1621da177e4SLinus Torvalds 	 * Make sure we have an EA -- the NULL EA list is valid, but you
1631da177e4SLinus Torvalds 	 * can't copy it!
1641da177e4SLinus Torvalds 	 */
1651da177e4SLinus Torvalds 	if (ealist && size > sizeof (struct jfs_ea_list)) {
1661da177e4SLinus Torvalds 		assert(size <= sizeof (ji->i_inline_ea));
1671da177e4SLinus Torvalds 
1681da177e4SLinus Torvalds 		/*
1691da177e4SLinus Torvalds 		 * See if the space is available or if it is already being
1701da177e4SLinus Torvalds 		 * used for an inline EA.
1711da177e4SLinus Torvalds 		 */
1721da177e4SLinus Torvalds 		if (!(ji->mode2 & INLINEEA) && !(ji->ea.flag & DXD_INLINE))
1731da177e4SLinus Torvalds 			return -EPERM;
1741da177e4SLinus Torvalds 
1751da177e4SLinus Torvalds 		DXDsize(ea, size);
1761da177e4SLinus Torvalds 		DXDlength(ea, 0);
1771da177e4SLinus Torvalds 		DXDaddress(ea, 0);
1781da177e4SLinus Torvalds 		memcpy(ji->i_inline_ea, ealist, size);
1791da177e4SLinus Torvalds 		ea->flag = DXD_INLINE;
1801da177e4SLinus Torvalds 		ji->mode2 &= ~INLINEEA;
1811da177e4SLinus Torvalds 	} else {
1821da177e4SLinus Torvalds 		ea->flag = 0;
1831da177e4SLinus Torvalds 		DXDsize(ea, 0);
1841da177e4SLinus Torvalds 		DXDlength(ea, 0);
1851da177e4SLinus Torvalds 		DXDaddress(ea, 0);
1861da177e4SLinus Torvalds 
1871da177e4SLinus Torvalds 		/* Free up INLINE area */
1881da177e4SLinus Torvalds 		if (ji->ea.flag & DXD_INLINE)
1891da177e4SLinus Torvalds 			ji->mode2 |= INLINEEA;
1901da177e4SLinus Torvalds 	}
1911da177e4SLinus Torvalds 
1921da177e4SLinus Torvalds 	return 0;
1931da177e4SLinus Torvalds }
1941da177e4SLinus Torvalds 
1951da177e4SLinus Torvalds /*
1961da177e4SLinus Torvalds  * NAME: ea_write
1971da177e4SLinus Torvalds  *
1981da177e4SLinus Torvalds  * FUNCTION: Write an EA for an inode
1991da177e4SLinus Torvalds  *
2001da177e4SLinus Torvalds  * PRE CONDITIONS: EA has been verified
2011da177e4SLinus Torvalds  *
2021da177e4SLinus Torvalds  * PARAMETERS:
2031da177e4SLinus Torvalds  *	ip	- Inode pointer
2041da177e4SLinus Torvalds  *	ealist	- EA list pointer
2051da177e4SLinus Torvalds  *	size	- size of ealist in bytes
2061da177e4SLinus Torvalds  *	ea	- dxd_t structure to be filled in appropriately with where the
2071da177e4SLinus Torvalds  *		  EA was copied
2081da177e4SLinus Torvalds  *
2091da177e4SLinus Torvalds  * NOTES: Will write EA inline if able to, otherwise allocates blocks for an
2101da177e4SLinus Torvalds  *	extent and synchronously writes it to those blocks.
2111da177e4SLinus Torvalds  *
2121da177e4SLinus Torvalds  * RETURNS: 0 for success; Anything else indicates failure
2131da177e4SLinus Torvalds  */
2141da177e4SLinus Torvalds static int ea_write(struct inode *ip, struct jfs_ea_list *ealist, int size,
2151da177e4SLinus Torvalds 		       dxd_t * ea)
2161da177e4SLinus Torvalds {
2171da177e4SLinus Torvalds 	struct super_block *sb = ip->i_sb;
2181da177e4SLinus Torvalds 	struct jfs_inode_info *ji = JFS_IP(ip);
2191da177e4SLinus Torvalds 	struct jfs_sb_info *sbi = JFS_SBI(sb);
2201da177e4SLinus Torvalds 	int nblocks;
2211da177e4SLinus Torvalds 	s64 blkno;
2221da177e4SLinus Torvalds 	int rc = 0, i;
2231da177e4SLinus Torvalds 	char *cp;
2241da177e4SLinus Torvalds 	s32 nbytes, nb;
2251da177e4SLinus Torvalds 	s32 bytes_to_write;
2261da177e4SLinus Torvalds 	struct metapage *mp;
2271da177e4SLinus Torvalds 
2281da177e4SLinus Torvalds 	/*
2291da177e4SLinus Torvalds 	 * Quick check to see if this is an in-linable EA.  Short EAs
2301da177e4SLinus Torvalds 	 * and empty EAs are all in-linable, provided the space exists.
2311da177e4SLinus Torvalds 	 */
2321da177e4SLinus Torvalds 	if (!ealist || size <= sizeof (ji->i_inline_ea)) {
2331da177e4SLinus Torvalds 		if (!ea_write_inline(ip, ealist, size, ea))
2341da177e4SLinus Torvalds 			return 0;
2351da177e4SLinus Torvalds 	}
2361da177e4SLinus Torvalds 
2371da177e4SLinus Torvalds 	/* figure out how many blocks we need */
2381da177e4SLinus Torvalds 	nblocks = (size + (sb->s_blocksize - 1)) >> sb->s_blocksize_bits;
2391da177e4SLinus Torvalds 
2401da177e4SLinus Torvalds 	/* Allocate new blocks to quota. */
2415dd4056dSChristoph Hellwig 	rc = dquot_alloc_block(ip, nblocks);
2425dd4056dSChristoph Hellwig 	if (rc)
2435dd4056dSChristoph Hellwig 		return rc;
2441da177e4SLinus Torvalds 
2451da177e4SLinus Torvalds 	rc = dbAlloc(ip, INOHINT(ip), nblocks, &blkno);
2461da177e4SLinus Torvalds 	if (rc) {
2471da177e4SLinus Torvalds 		/*Rollback quota allocation. */
2485dd4056dSChristoph Hellwig 		dquot_free_block(ip, nblocks);
2491da177e4SLinus Torvalds 		return rc;
2501da177e4SLinus Torvalds 	}
2511da177e4SLinus Torvalds 
2521da177e4SLinus Torvalds 	/*
2531da177e4SLinus Torvalds 	 * Now have nblocks worth of storage to stuff into the FEALIST.
2541da177e4SLinus Torvalds 	 * loop over the FEALIST copying data into the buffer one page at
2551da177e4SLinus Torvalds 	 * a time.
2561da177e4SLinus Torvalds 	 */
2571da177e4SLinus Torvalds 	cp = (char *) ealist;
2581da177e4SLinus Torvalds 	nbytes = size;
2591da177e4SLinus Torvalds 	for (i = 0; i < nblocks; i += sbi->nbperpage) {
2601da177e4SLinus Torvalds 		/*
2611da177e4SLinus Torvalds 		 * Determine how many bytes for this request, and round up to
2621da177e4SLinus Torvalds 		 * the nearest aggregate block size
2631da177e4SLinus Torvalds 		 */
2641da177e4SLinus Torvalds 		nb = min(PSIZE, nbytes);
2651da177e4SLinus Torvalds 		bytes_to_write =
2661da177e4SLinus Torvalds 		    ((((nb + sb->s_blocksize - 1)) >> sb->s_blocksize_bits))
2671da177e4SLinus Torvalds 		    << sb->s_blocksize_bits;
2681da177e4SLinus Torvalds 
2691da177e4SLinus Torvalds 		if (!(mp = get_metapage(ip, blkno + i, bytes_to_write, 1))) {
2701da177e4SLinus Torvalds 			rc = -EIO;
2711da177e4SLinus Torvalds 			goto failed;
2721da177e4SLinus Torvalds 		}
2731da177e4SLinus Torvalds 
2741da177e4SLinus Torvalds 		memcpy(mp->data, cp, nb);
2751da177e4SLinus Torvalds 
2761da177e4SLinus Torvalds 		/*
2771da177e4SLinus Torvalds 		 * We really need a way to propagate errors for
2781da177e4SLinus Torvalds 		 * forced writes like this one.  --hch
2791da177e4SLinus Torvalds 		 *
2801da177e4SLinus Torvalds 		 * (__write_metapage => release_metapage => flush_metapage)
2811da177e4SLinus Torvalds 		 */
2821da177e4SLinus Torvalds #ifdef _JFS_FIXME
2831da177e4SLinus Torvalds 		if ((rc = flush_metapage(mp))) {
2841da177e4SLinus Torvalds 			/*
2851da177e4SLinus Torvalds 			 * the write failed -- this means that the buffer
2861da177e4SLinus Torvalds 			 * is still assigned and the blocks are not being
2871da177e4SLinus Torvalds 			 * used.  this seems like the best error recovery
2881da177e4SLinus Torvalds 			 * we can get ...
2891da177e4SLinus Torvalds 			 */
2901da177e4SLinus Torvalds 			goto failed;
2911da177e4SLinus Torvalds 		}
2921da177e4SLinus Torvalds #else
2931da177e4SLinus Torvalds 		flush_metapage(mp);
2941da177e4SLinus Torvalds #endif
2951da177e4SLinus Torvalds 
2961da177e4SLinus Torvalds 		cp += PSIZE;
2971da177e4SLinus Torvalds 		nbytes -= nb;
2981da177e4SLinus Torvalds 	}
2991da177e4SLinus Torvalds 
3001da177e4SLinus Torvalds 	ea->flag = DXD_EXTENT;
3011da177e4SLinus Torvalds 	DXDsize(ea, le32_to_cpu(ealist->size));
3021da177e4SLinus Torvalds 	DXDlength(ea, nblocks);
3031da177e4SLinus Torvalds 	DXDaddress(ea, blkno);
3041da177e4SLinus Torvalds 
3051da177e4SLinus Torvalds 	/* Free up INLINE area */
3061da177e4SLinus Torvalds 	if (ji->ea.flag & DXD_INLINE)
3071da177e4SLinus Torvalds 		ji->mode2 |= INLINEEA;
3081da177e4SLinus Torvalds 
3091da177e4SLinus Torvalds 	return 0;
3101da177e4SLinus Torvalds 
3111da177e4SLinus Torvalds       failed:
3121da177e4SLinus Torvalds 	/* Rollback quota allocation. */
3135dd4056dSChristoph Hellwig 	dquot_free_block(ip, nblocks);
3141da177e4SLinus Torvalds 
3151da177e4SLinus Torvalds 	dbFree(ip, blkno, nblocks);
3161da177e4SLinus Torvalds 	return rc;
3171da177e4SLinus Torvalds }
3181da177e4SLinus Torvalds 
3191da177e4SLinus Torvalds /*
3201da177e4SLinus Torvalds  * NAME: ea_read_inline
3211da177e4SLinus Torvalds  *
3221da177e4SLinus Torvalds  * FUNCTION: Read an inlined EA into user's buffer
3231da177e4SLinus Torvalds  *
3241da177e4SLinus Torvalds  * PARAMETERS:
3251da177e4SLinus Torvalds  *	ip	- Inode pointer
3261da177e4SLinus Torvalds  *	ealist	- Pointer to buffer to fill in with EA
3271da177e4SLinus Torvalds  *
3281da177e4SLinus Torvalds  * RETURNS: 0
3291da177e4SLinus Torvalds  */
3301da177e4SLinus Torvalds static int ea_read_inline(struct inode *ip, struct jfs_ea_list *ealist)
3311da177e4SLinus Torvalds {
3321da177e4SLinus Torvalds 	struct jfs_inode_info *ji = JFS_IP(ip);
3331da177e4SLinus Torvalds 	int ea_size = sizeDXD(&ji->ea);
3341da177e4SLinus Torvalds 
3351da177e4SLinus Torvalds 	if (ea_size == 0) {
3361da177e4SLinus Torvalds 		ealist->size = 0;
3371da177e4SLinus Torvalds 		return 0;
3381da177e4SLinus Torvalds 	}
3391da177e4SLinus Torvalds 
3401da177e4SLinus Torvalds 	/* Sanity Check */
3411da177e4SLinus Torvalds 	if ((sizeDXD(&ji->ea) > sizeof (ji->i_inline_ea)))
3421da177e4SLinus Torvalds 		return -EIO;
3431da177e4SLinus Torvalds 	if (le32_to_cpu(((struct jfs_ea_list *) &ji->i_inline_ea)->size)
3441da177e4SLinus Torvalds 	    != ea_size)
3451da177e4SLinus Torvalds 		return -EIO;
3461da177e4SLinus Torvalds 
3471da177e4SLinus Torvalds 	memcpy(ealist, ji->i_inline_ea, ea_size);
3481da177e4SLinus Torvalds 	return 0;
3491da177e4SLinus Torvalds }
3501da177e4SLinus Torvalds 
3511da177e4SLinus Torvalds /*
3521da177e4SLinus Torvalds  * NAME: ea_read
3531da177e4SLinus Torvalds  *
3541da177e4SLinus Torvalds  * FUNCTION: copy EA data into user's buffer
3551da177e4SLinus Torvalds  *
3561da177e4SLinus Torvalds  * PARAMETERS:
3571da177e4SLinus Torvalds  *	ip	- Inode pointer
3581da177e4SLinus Torvalds  *	ealist	- Pointer to buffer to fill in with EA
3591da177e4SLinus Torvalds  *
3601da177e4SLinus Torvalds  * NOTES:  If EA is inline calls ea_read_inline() to copy EA.
3611da177e4SLinus Torvalds  *
3621da177e4SLinus Torvalds  * RETURNS: 0 for success; other indicates failure
3631da177e4SLinus Torvalds  */
3641da177e4SLinus Torvalds static int ea_read(struct inode *ip, struct jfs_ea_list *ealist)
3651da177e4SLinus Torvalds {
3661da177e4SLinus Torvalds 	struct super_block *sb = ip->i_sb;
3671da177e4SLinus Torvalds 	struct jfs_inode_info *ji = JFS_IP(ip);
3681da177e4SLinus Torvalds 	struct jfs_sb_info *sbi = JFS_SBI(sb);
3691da177e4SLinus Torvalds 	int nblocks;
3701da177e4SLinus Torvalds 	s64 blkno;
3711da177e4SLinus Torvalds 	char *cp = (char *) ealist;
3721da177e4SLinus Torvalds 	int i;
3731da177e4SLinus Torvalds 	int nbytes, nb;
3741da177e4SLinus Torvalds 	s32 bytes_to_read;
3751da177e4SLinus Torvalds 	struct metapage *mp;
3761da177e4SLinus Torvalds 
3771da177e4SLinus Torvalds 	/* quick check for in-line EA */
3781da177e4SLinus Torvalds 	if (ji->ea.flag & DXD_INLINE)
3791da177e4SLinus Torvalds 		return ea_read_inline(ip, ealist);
3801da177e4SLinus Torvalds 
3811da177e4SLinus Torvalds 	nbytes = sizeDXD(&ji->ea);
3821da177e4SLinus Torvalds 	if (!nbytes) {
383eb8630d7SJoe Perches 		jfs_error(sb, "nbytes is 0\n");
3841da177e4SLinus Torvalds 		return -EIO;
3851da177e4SLinus Torvalds 	}
3861da177e4SLinus Torvalds 
3871da177e4SLinus Torvalds 	/*
3881da177e4SLinus Torvalds 	 * Figure out how many blocks were allocated when this EA list was
3891da177e4SLinus Torvalds 	 * originally written to disk.
3901da177e4SLinus Torvalds 	 */
3911da177e4SLinus Torvalds 	nblocks = lengthDXD(&ji->ea) << sbi->l2nbperpage;
3921da177e4SLinus Torvalds 	blkno = addressDXD(&ji->ea) << sbi->l2nbperpage;
3931da177e4SLinus Torvalds 
3941da177e4SLinus Torvalds 	/*
3951da177e4SLinus Torvalds 	 * I have found the disk blocks which were originally used to store
3961da177e4SLinus Torvalds 	 * the FEALIST.  now i loop over each contiguous block copying the
3971da177e4SLinus Torvalds 	 * data into the buffer.
3981da177e4SLinus Torvalds 	 */
3991da177e4SLinus Torvalds 	for (i = 0; i < nblocks; i += sbi->nbperpage) {
4001da177e4SLinus Torvalds 		/*
4011da177e4SLinus Torvalds 		 * Determine how many bytes for this request, and round up to
4021da177e4SLinus Torvalds 		 * the nearest aggregate block size
4031da177e4SLinus Torvalds 		 */
4041da177e4SLinus Torvalds 		nb = min(PSIZE, nbytes);
4051da177e4SLinus Torvalds 		bytes_to_read =
4061da177e4SLinus Torvalds 		    ((((nb + sb->s_blocksize - 1)) >> sb->s_blocksize_bits))
4071da177e4SLinus Torvalds 		    << sb->s_blocksize_bits;
4081da177e4SLinus Torvalds 
4091da177e4SLinus Torvalds 		if (!(mp = read_metapage(ip, blkno + i, bytes_to_read, 1)))
4101da177e4SLinus Torvalds 			return -EIO;
4111da177e4SLinus Torvalds 
4121da177e4SLinus Torvalds 		memcpy(cp, mp->data, nb);
4131da177e4SLinus Torvalds 		release_metapage(mp);
4141da177e4SLinus Torvalds 
4151da177e4SLinus Torvalds 		cp += PSIZE;
4161da177e4SLinus Torvalds 		nbytes -= nb;
4171da177e4SLinus Torvalds 	}
4181da177e4SLinus Torvalds 
4191da177e4SLinus Torvalds 	return 0;
4201da177e4SLinus Torvalds }
4211da177e4SLinus Torvalds 
4221da177e4SLinus Torvalds /*
4231da177e4SLinus Torvalds  * NAME: ea_get
4241da177e4SLinus Torvalds  *
4251da177e4SLinus Torvalds  * FUNCTION: Returns buffer containing existing extended attributes.
4261da177e4SLinus Torvalds  *	     The size of the buffer will be the larger of the existing
4271da177e4SLinus Torvalds  *	     attributes size, or min_size.
4281da177e4SLinus Torvalds  *
4291da177e4SLinus Torvalds  *	     The buffer, which may be inlined in the inode or in the
4301da177e4SLinus Torvalds  *	     page cache must be release by calling ea_release or ea_put
4311da177e4SLinus Torvalds  *
4321da177e4SLinus Torvalds  * PARAMETERS:
4331da177e4SLinus Torvalds  *	inode	- Inode pointer
4341da177e4SLinus Torvalds  *	ea_buf	- Structure to be populated with ealist and its metadata
4351da177e4SLinus Torvalds  *	min_size- minimum size of buffer to be returned
4361da177e4SLinus Torvalds  *
4371da177e4SLinus Torvalds  * RETURNS: 0 for success; Other indicates failure
4381da177e4SLinus Torvalds  */
4391da177e4SLinus Torvalds static int ea_get(struct inode *inode, struct ea_buffer *ea_buf, int min_size)
4401da177e4SLinus Torvalds {
4411da177e4SLinus Torvalds 	struct jfs_inode_info *ji = JFS_IP(inode);
4421da177e4SLinus Torvalds 	struct super_block *sb = inode->i_sb;
4431da177e4SLinus Torvalds 	int size;
4441da177e4SLinus Torvalds 	int ea_size = sizeDXD(&ji->ea);
4451da177e4SLinus Torvalds 	int blocks_needed, current_blocks;
4461da177e4SLinus Torvalds 	s64 blkno;
4471da177e4SLinus Torvalds 	int rc;
4481da177e4SLinus Torvalds 	int quota_allocation = 0;
4491da177e4SLinus Torvalds 
4501da177e4SLinus Torvalds 	/* When fsck.jfs clears a bad ea, it doesn't clear the size */
4511da177e4SLinus Torvalds 	if (ji->ea.flag == 0)
4521da177e4SLinus Torvalds 		ea_size = 0;
4531da177e4SLinus Torvalds 
4541da177e4SLinus Torvalds 	if (ea_size == 0) {
4551da177e4SLinus Torvalds 		if (min_size == 0) {
4561da177e4SLinus Torvalds 			ea_buf->flag = 0;
4571da177e4SLinus Torvalds 			ea_buf->max_size = 0;
4581da177e4SLinus Torvalds 			ea_buf->xattr = NULL;
4591da177e4SLinus Torvalds 			return 0;
4601da177e4SLinus Torvalds 		}
4611da177e4SLinus Torvalds 		if ((min_size <= sizeof (ji->i_inline_ea)) &&
4621da177e4SLinus Torvalds 		    (ji->mode2 & INLINEEA)) {
4631da177e4SLinus Torvalds 			ea_buf->flag = EA_INLINE | EA_NEW;
4641da177e4SLinus Torvalds 			ea_buf->max_size = sizeof (ji->i_inline_ea);
4651da177e4SLinus Torvalds 			ea_buf->xattr = (struct jfs_ea_list *) ji->i_inline_ea;
4661da177e4SLinus Torvalds 			DXDlength(&ea_buf->new_ea, 0);
4671da177e4SLinus Torvalds 			DXDaddress(&ea_buf->new_ea, 0);
4681da177e4SLinus Torvalds 			ea_buf->new_ea.flag = DXD_INLINE;
4691da177e4SLinus Torvalds 			DXDsize(&ea_buf->new_ea, min_size);
4701da177e4SLinus Torvalds 			return 0;
4711da177e4SLinus Torvalds 		}
4721da177e4SLinus Torvalds 		current_blocks = 0;
4731da177e4SLinus Torvalds 	} else if (ji->ea.flag & DXD_INLINE) {
4741da177e4SLinus Torvalds 		if (min_size <= sizeof (ji->i_inline_ea)) {
4751da177e4SLinus Torvalds 			ea_buf->flag = EA_INLINE;
4761da177e4SLinus Torvalds 			ea_buf->max_size = sizeof (ji->i_inline_ea);
4771da177e4SLinus Torvalds 			ea_buf->xattr = (struct jfs_ea_list *) ji->i_inline_ea;
4781da177e4SLinus Torvalds 			goto size_check;
4791da177e4SLinus Torvalds 		}
4801da177e4SLinus Torvalds 		current_blocks = 0;
4811da177e4SLinus Torvalds 	} else {
4821da177e4SLinus Torvalds 		if (!(ji->ea.flag & DXD_EXTENT)) {
483eb8630d7SJoe Perches 			jfs_error(sb, "invalid ea.flag\n");
4841da177e4SLinus Torvalds 			return -EIO;
4851da177e4SLinus Torvalds 		}
4861da177e4SLinus Torvalds 		current_blocks = (ea_size + sb->s_blocksize - 1) >>
4871da177e4SLinus Torvalds 		    sb->s_blocksize_bits;
4881da177e4SLinus Torvalds 	}
4891da177e4SLinus Torvalds 	size = max(min_size, ea_size);
4901da177e4SLinus Torvalds 
4911da177e4SLinus Torvalds 	if (size > PSIZE) {
4921da177e4SLinus Torvalds 		/*
4931da177e4SLinus Torvalds 		 * To keep the rest of the code simple.  Allocate a
4941da177e4SLinus Torvalds 		 * contiguous buffer to work with
4951da177e4SLinus Torvalds 		 */
4961da177e4SLinus Torvalds 		ea_buf->xattr = kmalloc(size, GFP_KERNEL);
4971da177e4SLinus Torvalds 		if (ea_buf->xattr == NULL)
4981da177e4SLinus Torvalds 			return -ENOMEM;
4991da177e4SLinus Torvalds 
5001da177e4SLinus Torvalds 		ea_buf->flag = EA_MALLOC;
5011da177e4SLinus Torvalds 		ea_buf->max_size = (size + sb->s_blocksize - 1) &
5021da177e4SLinus Torvalds 		    ~(sb->s_blocksize - 1);
5031da177e4SLinus Torvalds 
5041da177e4SLinus Torvalds 		if (ea_size == 0)
5051da177e4SLinus Torvalds 			return 0;
5061da177e4SLinus Torvalds 
5071da177e4SLinus Torvalds 		if ((rc = ea_read(inode, ea_buf->xattr))) {
5081da177e4SLinus Torvalds 			kfree(ea_buf->xattr);
5091da177e4SLinus Torvalds 			ea_buf->xattr = NULL;
5101da177e4SLinus Torvalds 			return rc;
5111da177e4SLinus Torvalds 		}
5121da177e4SLinus Torvalds 		goto size_check;
5131da177e4SLinus Torvalds 	}
5141da177e4SLinus Torvalds 	blocks_needed = (min_size + sb->s_blocksize - 1) >>
5151da177e4SLinus Torvalds 	    sb->s_blocksize_bits;
5161da177e4SLinus Torvalds 
5171da177e4SLinus Torvalds 	if (blocks_needed > current_blocks) {
5181da177e4SLinus Torvalds 		/* Allocate new blocks to quota. */
5195dd4056dSChristoph Hellwig 		rc = dquot_alloc_block(inode, blocks_needed);
5205dd4056dSChristoph Hellwig 		if (rc)
5211da177e4SLinus Torvalds 			return -EDQUOT;
5221da177e4SLinus Torvalds 
5231da177e4SLinus Torvalds 		quota_allocation = blocks_needed;
5241da177e4SLinus Torvalds 
5251da177e4SLinus Torvalds 		rc = dbAlloc(inode, INOHINT(inode), (s64) blocks_needed,
5261da177e4SLinus Torvalds 			     &blkno);
5271da177e4SLinus Torvalds 		if (rc)
5281da177e4SLinus Torvalds 			goto clean_up;
5291da177e4SLinus Torvalds 
5301da177e4SLinus Torvalds 		DXDlength(&ea_buf->new_ea, blocks_needed);
5311da177e4SLinus Torvalds 		DXDaddress(&ea_buf->new_ea, blkno);
5321da177e4SLinus Torvalds 		ea_buf->new_ea.flag = DXD_EXTENT;
5331da177e4SLinus Torvalds 		DXDsize(&ea_buf->new_ea, min_size);
5341da177e4SLinus Torvalds 
5351da177e4SLinus Torvalds 		ea_buf->flag = EA_EXTENT | EA_NEW;
5361da177e4SLinus Torvalds 
5371da177e4SLinus Torvalds 		ea_buf->mp = get_metapage(inode, blkno,
5381da177e4SLinus Torvalds 					  blocks_needed << sb->s_blocksize_bits,
5391da177e4SLinus Torvalds 					  1);
5401da177e4SLinus Torvalds 		if (ea_buf->mp == NULL) {
5411da177e4SLinus Torvalds 			dbFree(inode, blkno, (s64) blocks_needed);
5421da177e4SLinus Torvalds 			rc = -EIO;
5431da177e4SLinus Torvalds 			goto clean_up;
5441da177e4SLinus Torvalds 		}
5451da177e4SLinus Torvalds 		ea_buf->xattr = ea_buf->mp->data;
5461da177e4SLinus Torvalds 		ea_buf->max_size = (min_size + sb->s_blocksize - 1) &
5471da177e4SLinus Torvalds 		    ~(sb->s_blocksize - 1);
5481da177e4SLinus Torvalds 		if (ea_size == 0)
5491da177e4SLinus Torvalds 			return 0;
5501da177e4SLinus Torvalds 		if ((rc = ea_read(inode, ea_buf->xattr))) {
5511da177e4SLinus Torvalds 			discard_metapage(ea_buf->mp);
5521da177e4SLinus Torvalds 			dbFree(inode, blkno, (s64) blocks_needed);
5531da177e4SLinus Torvalds 			goto clean_up;
5541da177e4SLinus Torvalds 		}
5551da177e4SLinus Torvalds 		goto size_check;
5561da177e4SLinus Torvalds 	}
5571da177e4SLinus Torvalds 	ea_buf->flag = EA_EXTENT;
5581da177e4SLinus Torvalds 	ea_buf->mp = read_metapage(inode, addressDXD(&ji->ea),
5591da177e4SLinus Torvalds 				   lengthDXD(&ji->ea) << sb->s_blocksize_bits,
5601da177e4SLinus Torvalds 				   1);
5611da177e4SLinus Torvalds 	if (ea_buf->mp == NULL) {
5621da177e4SLinus Torvalds 		rc = -EIO;
5631da177e4SLinus Torvalds 		goto clean_up;
5641da177e4SLinus Torvalds 	}
5651da177e4SLinus Torvalds 	ea_buf->xattr = ea_buf->mp->data;
5661da177e4SLinus Torvalds 	ea_buf->max_size = (ea_size + sb->s_blocksize - 1) &
5671da177e4SLinus Torvalds 	    ~(sb->s_blocksize - 1);
5681da177e4SLinus Torvalds 
5691da177e4SLinus Torvalds       size_check:
5701da177e4SLinus Torvalds 	if (EALIST_SIZE(ea_buf->xattr) != ea_size) {
5711da177e4SLinus Torvalds 		printk(KERN_ERR "ea_get: invalid extended attribute\n");
572288e4d83SDave Kleikamp 		print_hex_dump(KERN_ERR, "", DUMP_PREFIX_ADDRESS, 16, 1,
573288e4d83SDave Kleikamp 				     ea_buf->xattr, ea_size, 1);
5741da177e4SLinus Torvalds 		ea_release(inode, ea_buf);
5751da177e4SLinus Torvalds 		rc = -EIO;
5761da177e4SLinus Torvalds 		goto clean_up;
5771da177e4SLinus Torvalds 	}
5781da177e4SLinus Torvalds 
5791da177e4SLinus Torvalds 	return ea_size;
5801da177e4SLinus Torvalds 
5811da177e4SLinus Torvalds       clean_up:
5821da177e4SLinus Torvalds 	/* Rollback quota allocation */
5831da177e4SLinus Torvalds 	if (quota_allocation)
5845dd4056dSChristoph Hellwig 		dquot_free_block(inode, quota_allocation);
5851da177e4SLinus Torvalds 
5861da177e4SLinus Torvalds 	return (rc);
5871da177e4SLinus Torvalds }
5881da177e4SLinus Torvalds 
5891da177e4SLinus Torvalds static void ea_release(struct inode *inode, struct ea_buffer *ea_buf)
5901da177e4SLinus Torvalds {
5911da177e4SLinus Torvalds 	if (ea_buf->flag & EA_MALLOC)
5921da177e4SLinus Torvalds 		kfree(ea_buf->xattr);
5931da177e4SLinus Torvalds 	else if (ea_buf->flag & EA_EXTENT) {
5941da177e4SLinus Torvalds 		assert(ea_buf->mp);
5951da177e4SLinus Torvalds 		release_metapage(ea_buf->mp);
5961da177e4SLinus Torvalds 
5971da177e4SLinus Torvalds 		if (ea_buf->flag & EA_NEW)
5981da177e4SLinus Torvalds 			dbFree(inode, addressDXD(&ea_buf->new_ea),
5991da177e4SLinus Torvalds 			       lengthDXD(&ea_buf->new_ea));
6001da177e4SLinus Torvalds 	}
6011da177e4SLinus Torvalds }
6021da177e4SLinus Torvalds 
6034f4b401bSDave Kleikamp static int ea_put(tid_t tid, struct inode *inode, struct ea_buffer *ea_buf,
6044f4b401bSDave Kleikamp 		  int new_size)
6051da177e4SLinus Torvalds {
6061da177e4SLinus Torvalds 	struct jfs_inode_info *ji = JFS_IP(inode);
6071da177e4SLinus Torvalds 	unsigned long old_blocks, new_blocks;
6081da177e4SLinus Torvalds 	int rc = 0;
6091da177e4SLinus Torvalds 
6101da177e4SLinus Torvalds 	if (new_size == 0) {
6111da177e4SLinus Torvalds 		ea_release(inode, ea_buf);
6121da177e4SLinus Torvalds 		ea_buf = NULL;
6131da177e4SLinus Torvalds 	} else if (ea_buf->flag & EA_INLINE) {
6141da177e4SLinus Torvalds 		assert(new_size <= sizeof (ji->i_inline_ea));
6151da177e4SLinus Torvalds 		ji->mode2 &= ~INLINEEA;
6161da177e4SLinus Torvalds 		ea_buf->new_ea.flag = DXD_INLINE;
6171da177e4SLinus Torvalds 		DXDsize(&ea_buf->new_ea, new_size);
6181da177e4SLinus Torvalds 		DXDaddress(&ea_buf->new_ea, 0);
6191da177e4SLinus Torvalds 		DXDlength(&ea_buf->new_ea, 0);
6201da177e4SLinus Torvalds 	} else if (ea_buf->flag & EA_MALLOC) {
6211da177e4SLinus Torvalds 		rc = ea_write(inode, ea_buf->xattr, new_size, &ea_buf->new_ea);
6221da177e4SLinus Torvalds 		kfree(ea_buf->xattr);
6231da177e4SLinus Torvalds 	} else if (ea_buf->flag & EA_NEW) {
6241da177e4SLinus Torvalds 		/* We have already allocated a new dxd */
6251da177e4SLinus Torvalds 		flush_metapage(ea_buf->mp);
6261da177e4SLinus Torvalds 	} else {
6271da177e4SLinus Torvalds 		/* ->xattr must point to original ea's metapage */
6281da177e4SLinus Torvalds 		rc = ea_write(inode, ea_buf->xattr, new_size, &ea_buf->new_ea);
6291da177e4SLinus Torvalds 		discard_metapage(ea_buf->mp);
6301da177e4SLinus Torvalds 	}
6311da177e4SLinus Torvalds 	if (rc)
6321da177e4SLinus Torvalds 		return rc;
6331da177e4SLinus Torvalds 
6341da177e4SLinus Torvalds 	old_blocks = new_blocks = 0;
6351da177e4SLinus Torvalds 
6361da177e4SLinus Torvalds 	if (ji->ea.flag & DXD_EXTENT) {
6371da177e4SLinus Torvalds 		invalidate_dxd_metapages(inode, ji->ea);
6381da177e4SLinus Torvalds 		old_blocks = lengthDXD(&ji->ea);
6391da177e4SLinus Torvalds 	}
6401da177e4SLinus Torvalds 
6411da177e4SLinus Torvalds 	if (ea_buf) {
6421da177e4SLinus Torvalds 		txEA(tid, inode, &ji->ea, &ea_buf->new_ea);
6431da177e4SLinus Torvalds 		if (ea_buf->new_ea.flag & DXD_EXTENT) {
6441da177e4SLinus Torvalds 			new_blocks = lengthDXD(&ea_buf->new_ea);
6451da177e4SLinus Torvalds 			if (ji->ea.flag & DXD_INLINE)
6461da177e4SLinus Torvalds 				ji->mode2 |= INLINEEA;
6471da177e4SLinus Torvalds 		}
6481da177e4SLinus Torvalds 		ji->ea = ea_buf->new_ea;
6491da177e4SLinus Torvalds 	} else {
6501da177e4SLinus Torvalds 		txEA(tid, inode, &ji->ea, NULL);
6511da177e4SLinus Torvalds 		if (ji->ea.flag & DXD_INLINE)
6521da177e4SLinus Torvalds 			ji->mode2 |= INLINEEA;
6531da177e4SLinus Torvalds 		ji->ea.flag = 0;
6541da177e4SLinus Torvalds 		ji->ea.size = 0;
6551da177e4SLinus Torvalds 	}
6561da177e4SLinus Torvalds 
6571da177e4SLinus Torvalds 	/* If old blocks exist, they must be removed from quota allocation. */
6581da177e4SLinus Torvalds 	if (old_blocks)
6595dd4056dSChristoph Hellwig 		dquot_free_block(inode, old_blocks);
6601da177e4SLinus Torvalds 
6611da177e4SLinus Torvalds 	inode->i_ctime = CURRENT_TIME;
6621da177e4SLinus Torvalds 
6634f4b401bSDave Kleikamp 	return 0;
6641da177e4SLinus Torvalds }
6651da177e4SLinus Torvalds 
6664f4b401bSDave Kleikamp int __jfs_setxattr(tid_t tid, struct inode *inode, const char *name,
6674f4b401bSDave Kleikamp 		   const void *value, size_t value_len, int flags)
6681da177e4SLinus Torvalds {
6691da177e4SLinus Torvalds 	struct jfs_ea_list *ealist;
6701da177e4SLinus Torvalds 	struct jfs_ea *ea, *old_ea = NULL, *next_ea = NULL;
6711da177e4SLinus Torvalds 	struct ea_buffer ea_buf;
6721da177e4SLinus Torvalds 	int old_ea_size = 0;
6731da177e4SLinus Torvalds 	int xattr_size;
6741da177e4SLinus Torvalds 	int new_size;
6751da177e4SLinus Torvalds 	int namelen = strlen(name);
6761da177e4SLinus Torvalds 	int found = 0;
6771da177e4SLinus Torvalds 	int rc;
6781da177e4SLinus Torvalds 	int length;
6791da177e4SLinus Torvalds 
6801da177e4SLinus Torvalds 	down_write(&JFS_IP(inode)->xattr_sem);
6811da177e4SLinus Torvalds 
6821da177e4SLinus Torvalds 	xattr_size = ea_get(inode, &ea_buf, 0);
6831da177e4SLinus Torvalds 	if (xattr_size < 0) {
6841da177e4SLinus Torvalds 		rc = xattr_size;
6851da177e4SLinus Torvalds 		goto out;
6861da177e4SLinus Torvalds 	}
6871da177e4SLinus Torvalds 
6881da177e4SLinus Torvalds       again:
6891da177e4SLinus Torvalds 	ealist = (struct jfs_ea_list *) ea_buf.xattr;
6901da177e4SLinus Torvalds 	new_size = sizeof (struct jfs_ea_list);
6911da177e4SLinus Torvalds 
6921da177e4SLinus Torvalds 	if (xattr_size) {
6931da177e4SLinus Torvalds 		for (ea = FIRST_EA(ealist); ea < END_EALIST(ealist);
6941da177e4SLinus Torvalds 		     ea = NEXT_EA(ea)) {
6951da177e4SLinus Torvalds 			if ((namelen == ea->namelen) &&
6961da177e4SLinus Torvalds 			    (memcmp(name, ea->name, namelen) == 0)) {
6971da177e4SLinus Torvalds 				found = 1;
6981da177e4SLinus Torvalds 				if (flags & XATTR_CREATE) {
6991da177e4SLinus Torvalds 					rc = -EEXIST;
7001da177e4SLinus Torvalds 					goto release;
7011da177e4SLinus Torvalds 				}
7021da177e4SLinus Torvalds 				old_ea = ea;
7031da177e4SLinus Torvalds 				old_ea_size = EA_SIZE(ea);
7041da177e4SLinus Torvalds 				next_ea = NEXT_EA(ea);
7051da177e4SLinus Torvalds 			} else
7061da177e4SLinus Torvalds 				new_size += EA_SIZE(ea);
7071da177e4SLinus Torvalds 		}
7081da177e4SLinus Torvalds 	}
7091da177e4SLinus Torvalds 
7101da177e4SLinus Torvalds 	if (!found) {
7111da177e4SLinus Torvalds 		if (flags & XATTR_REPLACE) {
7121da177e4SLinus Torvalds 			rc = -ENODATA;
7131da177e4SLinus Torvalds 			goto release;
7141da177e4SLinus Torvalds 		}
7151da177e4SLinus Torvalds 		if (value == NULL) {
7161da177e4SLinus Torvalds 			rc = 0;
7171da177e4SLinus Torvalds 			goto release;
7181da177e4SLinus Torvalds 		}
7191da177e4SLinus Torvalds 	}
7201da177e4SLinus Torvalds 	if (value)
7211da177e4SLinus Torvalds 		new_size += sizeof (struct jfs_ea) + namelen + 1 + value_len;
7221da177e4SLinus Torvalds 
7231da177e4SLinus Torvalds 	if (new_size > ea_buf.max_size) {
7241da177e4SLinus Torvalds 		/*
7251da177e4SLinus Torvalds 		 * We need to allocate more space for merged ea list.
7261da177e4SLinus Torvalds 		 * We should only have loop to again: once.
7271da177e4SLinus Torvalds 		 */
7281da177e4SLinus Torvalds 		ea_release(inode, &ea_buf);
7291da177e4SLinus Torvalds 		xattr_size = ea_get(inode, &ea_buf, new_size);
7301da177e4SLinus Torvalds 		if (xattr_size < 0) {
7311da177e4SLinus Torvalds 			rc = xattr_size;
7321da177e4SLinus Torvalds 			goto out;
7331da177e4SLinus Torvalds 		}
7341da177e4SLinus Torvalds 		goto again;
7351da177e4SLinus Torvalds 	}
7361da177e4SLinus Torvalds 
7371da177e4SLinus Torvalds 	/* Remove old ea of the same name */
7381da177e4SLinus Torvalds 	if (found) {
7391da177e4SLinus Torvalds 		/* number of bytes following target EA */
7401da177e4SLinus Torvalds 		length = (char *) END_EALIST(ealist) - (char *) next_ea;
7411da177e4SLinus Torvalds 		if (length > 0)
7421da177e4SLinus Torvalds 			memmove(old_ea, next_ea, length);
7431da177e4SLinus Torvalds 		xattr_size -= old_ea_size;
7441da177e4SLinus Torvalds 	}
7451da177e4SLinus Torvalds 
7461da177e4SLinus Torvalds 	/* Add new entry to the end */
7471da177e4SLinus Torvalds 	if (value) {
7481da177e4SLinus Torvalds 		if (xattr_size == 0)
7491da177e4SLinus Torvalds 			/* Completely new ea list */
7501da177e4SLinus Torvalds 			xattr_size = sizeof (struct jfs_ea_list);
7511da177e4SLinus Torvalds 
7520439e091SJie Liu 		/*
7530439e091SJie Liu 		 * The size of EA value is limitted by on-disk format up to
7540439e091SJie Liu 		 *  __le16, there would be an overflow if the size is equal
7550439e091SJie Liu 		 * to XATTR_SIZE_MAX (65536).  In order to avoid this issue,
7560439e091SJie Liu 		 * we can pre-checkup the value size against USHRT_MAX, and
7570439e091SJie Liu 		 * return -E2BIG in this case, which is consistent with the
7580439e091SJie Liu 		 * VFS setxattr interface.
7590439e091SJie Liu 		 */
7600439e091SJie Liu 		if (value_len >= USHRT_MAX) {
7610439e091SJie Liu 			rc = -E2BIG;
7620439e091SJie Liu 			goto release;
7630439e091SJie Liu 		}
7640439e091SJie Liu 
7651da177e4SLinus Torvalds 		ea = (struct jfs_ea *) ((char *) ealist + xattr_size);
7661da177e4SLinus Torvalds 		ea->flag = 0;
7671da177e4SLinus Torvalds 		ea->namelen = namelen;
7681da177e4SLinus Torvalds 		ea->valuelen = (cpu_to_le16(value_len));
7691da177e4SLinus Torvalds 		memcpy(ea->name, name, namelen);
7701da177e4SLinus Torvalds 		ea->name[namelen] = 0;
7711da177e4SLinus Torvalds 		if (value_len)
7721da177e4SLinus Torvalds 			memcpy(&ea->name[namelen + 1], value, value_len);
7731da177e4SLinus Torvalds 		xattr_size += EA_SIZE(ea);
7741da177e4SLinus Torvalds 	}
7751da177e4SLinus Torvalds 
7761da177e4SLinus Torvalds 	/* DEBUG - If we did this right, these number match */
7771da177e4SLinus Torvalds 	if (xattr_size != new_size) {
7781da177e4SLinus Torvalds 		printk(KERN_ERR
7790439e091SJie Liu 		       "__jfs_setxattr: xattr_size = %d, new_size = %d\n",
7801da177e4SLinus Torvalds 		       xattr_size, new_size);
7811da177e4SLinus Torvalds 
7821da177e4SLinus Torvalds 		rc = -EINVAL;
7831da177e4SLinus Torvalds 		goto release;
7841da177e4SLinus Torvalds 	}
7851da177e4SLinus Torvalds 
7861da177e4SLinus Torvalds 	/*
7871da177e4SLinus Torvalds 	 * If we're left with an empty list, there's no ea
7881da177e4SLinus Torvalds 	 */
7891da177e4SLinus Torvalds 	if (new_size == sizeof (struct jfs_ea_list))
7901da177e4SLinus Torvalds 		new_size = 0;
7911da177e4SLinus Torvalds 
7921da177e4SLinus Torvalds 	ealist->size = cpu_to_le32(new_size);
7931da177e4SLinus Torvalds 
7944f4b401bSDave Kleikamp 	rc = ea_put(tid, inode, &ea_buf, new_size);
7951da177e4SLinus Torvalds 
7961da177e4SLinus Torvalds 	goto out;
7971da177e4SLinus Torvalds       release:
7981da177e4SLinus Torvalds 	ea_release(inode, &ea_buf);
7991da177e4SLinus Torvalds       out:
8001da177e4SLinus Torvalds 	up_write(&JFS_IP(inode)->xattr_sem);
8011da177e4SLinus Torvalds 
8021da177e4SLinus Torvalds 	return rc;
8031da177e4SLinus Torvalds }
8041da177e4SLinus Torvalds 
8051da177e4SLinus Torvalds ssize_t __jfs_getxattr(struct inode *inode, const char *name, void *data,
8061da177e4SLinus Torvalds 		       size_t buf_size)
8071da177e4SLinus Torvalds {
8081da177e4SLinus Torvalds 	struct jfs_ea_list *ealist;
8091da177e4SLinus Torvalds 	struct jfs_ea *ea;
8101da177e4SLinus Torvalds 	struct ea_buffer ea_buf;
8111da177e4SLinus Torvalds 	int xattr_size;
8121da177e4SLinus Torvalds 	ssize_t size;
8131da177e4SLinus Torvalds 	int namelen = strlen(name);
8141da177e4SLinus Torvalds 	char *value;
8151da177e4SLinus Torvalds 
8161da177e4SLinus Torvalds 	down_read(&JFS_IP(inode)->xattr_sem);
8171da177e4SLinus Torvalds 
8181da177e4SLinus Torvalds 	xattr_size = ea_get(inode, &ea_buf, 0);
8191da177e4SLinus Torvalds 
8201da177e4SLinus Torvalds 	if (xattr_size < 0) {
8211da177e4SLinus Torvalds 		size = xattr_size;
8221da177e4SLinus Torvalds 		goto out;
8231da177e4SLinus Torvalds 	}
8241da177e4SLinus Torvalds 
8251da177e4SLinus Torvalds 	if (xattr_size == 0)
8261da177e4SLinus Torvalds 		goto not_found;
8271da177e4SLinus Torvalds 
8281da177e4SLinus Torvalds 	ealist = (struct jfs_ea_list *) ea_buf.xattr;
8291da177e4SLinus Torvalds 
8301da177e4SLinus Torvalds 	/* Find the named attribute */
8311da177e4SLinus Torvalds 	for (ea = FIRST_EA(ealist); ea < END_EALIST(ealist); ea = NEXT_EA(ea))
8321da177e4SLinus Torvalds 		if ((namelen == ea->namelen) &&
8331da177e4SLinus Torvalds 		    memcmp(name, ea->name, namelen) == 0) {
8341da177e4SLinus Torvalds 			/* Found it */
8351da177e4SLinus Torvalds 			size = le16_to_cpu(ea->valuelen);
8361da177e4SLinus Torvalds 			if (!data)
8371da177e4SLinus Torvalds 				goto release;
8381da177e4SLinus Torvalds 			else if (size > buf_size) {
8391da177e4SLinus Torvalds 				size = -ERANGE;
8401da177e4SLinus Torvalds 				goto release;
8411da177e4SLinus Torvalds 			}
8421da177e4SLinus Torvalds 			value = ((char *) &ea->name) + ea->namelen + 1;
8431da177e4SLinus Torvalds 			memcpy(data, value, size);
8441da177e4SLinus Torvalds 			goto release;
8451da177e4SLinus Torvalds 		}
8461da177e4SLinus Torvalds       not_found:
8471da177e4SLinus Torvalds 	size = -ENODATA;
8481da177e4SLinus Torvalds       release:
8491da177e4SLinus Torvalds 	ea_release(inode, &ea_buf);
8501da177e4SLinus Torvalds       out:
8511da177e4SLinus Torvalds 	up_read(&JFS_IP(inode)->xattr_sem);
8521da177e4SLinus Torvalds 
8531da177e4SLinus Torvalds 	return size;
8541da177e4SLinus Torvalds }
8551da177e4SLinus Torvalds 
8561da177e4SLinus Torvalds /*
8571da177e4SLinus Torvalds  * No special permissions are needed to list attributes except for trusted.*
8581da177e4SLinus Torvalds  */
8591da177e4SLinus Torvalds static inline int can_list(struct jfs_ea *ea)
8601da177e4SLinus Torvalds {
8611da177e4SLinus Torvalds 	return (strncmp(ea->name, XATTR_TRUSTED_PREFIX,
8621da177e4SLinus Torvalds 			    XATTR_TRUSTED_PREFIX_LEN) ||
8631da177e4SLinus Torvalds 		capable(CAP_SYS_ADMIN));
8641da177e4SLinus Torvalds }
8651da177e4SLinus Torvalds 
8661da177e4SLinus Torvalds ssize_t jfs_listxattr(struct dentry * dentry, char *data, size_t buf_size)
8671da177e4SLinus Torvalds {
8682b0143b5SDavid Howells 	struct inode *inode = d_inode(dentry);
8691da177e4SLinus Torvalds 	char *buffer;
8701da177e4SLinus Torvalds 	ssize_t size = 0;
8711da177e4SLinus Torvalds 	int xattr_size;
8721da177e4SLinus Torvalds 	struct jfs_ea_list *ealist;
8731da177e4SLinus Torvalds 	struct jfs_ea *ea;
8741da177e4SLinus Torvalds 	struct ea_buffer ea_buf;
8751da177e4SLinus Torvalds 
8761da177e4SLinus Torvalds 	down_read(&JFS_IP(inode)->xattr_sem);
8771da177e4SLinus Torvalds 
8781da177e4SLinus Torvalds 	xattr_size = ea_get(inode, &ea_buf, 0);
8791da177e4SLinus Torvalds 	if (xattr_size < 0) {
8801da177e4SLinus Torvalds 		size = xattr_size;
8811da177e4SLinus Torvalds 		goto out;
8821da177e4SLinus Torvalds 	}
8831da177e4SLinus Torvalds 
8841da177e4SLinus Torvalds 	if (xattr_size == 0)
8851da177e4SLinus Torvalds 		goto release;
8861da177e4SLinus Torvalds 
8871da177e4SLinus Torvalds 	ealist = (struct jfs_ea_list *) ea_buf.xattr;
8881da177e4SLinus Torvalds 
8891da177e4SLinus Torvalds 	/* compute required size of list */
8901da177e4SLinus Torvalds 	for (ea = FIRST_EA(ealist); ea < END_EALIST(ealist); ea = NEXT_EA(ea)) {
8911da177e4SLinus Torvalds 		if (can_list(ea))
8921da177e4SLinus Torvalds 			size += name_size(ea) + 1;
8931da177e4SLinus Torvalds 	}
8941da177e4SLinus Torvalds 
8951da177e4SLinus Torvalds 	if (!data)
8961da177e4SLinus Torvalds 		goto release;
8971da177e4SLinus Torvalds 
8981da177e4SLinus Torvalds 	if (size > buf_size) {
8991da177e4SLinus Torvalds 		size = -ERANGE;
9001da177e4SLinus Torvalds 		goto release;
9011da177e4SLinus Torvalds 	}
9021da177e4SLinus Torvalds 
9031da177e4SLinus Torvalds 	/* Copy attribute names to buffer */
9041da177e4SLinus Torvalds 	buffer = data;
9051da177e4SLinus Torvalds 	for (ea = FIRST_EA(ealist); ea < END_EALIST(ealist); ea = NEXT_EA(ea)) {
9061da177e4SLinus Torvalds 		if (can_list(ea)) {
9071da177e4SLinus Torvalds 			int namelen = copy_name(buffer, ea);
9081da177e4SLinus Torvalds 			buffer += namelen + 1;
9091da177e4SLinus Torvalds 		}
9101da177e4SLinus Torvalds 	}
9111da177e4SLinus Torvalds 
9121da177e4SLinus Torvalds       release:
9131da177e4SLinus Torvalds 	ea_release(inode, &ea_buf);
9141da177e4SLinus Torvalds       out:
9151da177e4SLinus Torvalds 	up_read(&JFS_IP(inode)->xattr_sem);
9161da177e4SLinus Torvalds 	return size;
9171da177e4SLinus Torvalds }
9181da177e4SLinus Torvalds 
919c8b6056aSAndreas Gruenbacher static int __jfs_xattr_set(struct inode *inode, const char *name,
920c8b6056aSAndreas Gruenbacher 			   const void *value, size_t size, int flags)
9211da177e4SLinus Torvalds {
9224f4b401bSDave Kleikamp 	struct jfs_inode_info *ji = JFS_IP(inode);
9234f4b401bSDave Kleikamp 	tid_t tid;
924c8b6056aSAndreas Gruenbacher 	int rc;
925c18f7b51SDave Kleikamp 
9264f4b401bSDave Kleikamp 	tid = txBegin(inode->i_sb, 0);
9271de87444SIngo Molnar 	mutex_lock(&ji->commit_mutex);
928c8b6056aSAndreas Gruenbacher 	rc = __jfs_setxattr(tid, inode, name, value, size, flags);
9294f4b401bSDave Kleikamp 	if (!rc)
9304f4b401bSDave Kleikamp 		rc = txCommit(tid, 1, &inode, 0);
9314f4b401bSDave Kleikamp 	txEnd(tid);
9321de87444SIngo Molnar 	mutex_unlock(&ji->commit_mutex);
9334f4b401bSDave Kleikamp 
9344f4b401bSDave Kleikamp 	return rc;
9351da177e4SLinus Torvalds }
9361d15b10fSDave Kleikamp 
937c8b6056aSAndreas Gruenbacher static int jfs_xattr_get(const struct xattr_handler *handler,
938c8b6056aSAndreas Gruenbacher 			 struct dentry *unused, struct inode *inode,
939c8b6056aSAndreas Gruenbacher 			 const char *name, void *value, size_t size)
940c8b6056aSAndreas Gruenbacher {
941c8b6056aSAndreas Gruenbacher 	name = xattr_full_name(handler, name);
942c8b6056aSAndreas Gruenbacher 	return __jfs_getxattr(inode, name, value, size);
943c8b6056aSAndreas Gruenbacher }
944c8b6056aSAndreas Gruenbacher 
945c8b6056aSAndreas Gruenbacher static int jfs_xattr_set(const struct xattr_handler *handler,
94659301226SAl Viro 			 struct dentry *unused, struct inode *inode,
94759301226SAl Viro 			 const char *name, const void *value,
94859301226SAl Viro 			 size_t size, int flags)
949c8b6056aSAndreas Gruenbacher {
950c8b6056aSAndreas Gruenbacher 	name = xattr_full_name(handler, name);
951c8b6056aSAndreas Gruenbacher 	return __jfs_xattr_set(inode, name, value, size, flags);
952c8b6056aSAndreas Gruenbacher }
953c8b6056aSAndreas Gruenbacher 
954c8b6056aSAndreas Gruenbacher static int jfs_xattr_get_os2(const struct xattr_handler *handler,
955c8b6056aSAndreas Gruenbacher 			     struct dentry *unused, struct inode *inode,
956c8b6056aSAndreas Gruenbacher 			     const char *name, void *value, size_t size)
957c8b6056aSAndreas Gruenbacher {
958c8b6056aSAndreas Gruenbacher 	if (is_known_namespace(name))
959c8b6056aSAndreas Gruenbacher 		return -EOPNOTSUPP;
960c8b6056aSAndreas Gruenbacher 	return __jfs_getxattr(inode, name, value, size);
961c8b6056aSAndreas Gruenbacher }
962c8b6056aSAndreas Gruenbacher 
963c8b6056aSAndreas Gruenbacher static int jfs_xattr_set_os2(const struct xattr_handler *handler,
96459301226SAl Viro 			     struct dentry *unused, struct inode *inode,
96559301226SAl Viro 			     const char *name, const void *value,
96659301226SAl Viro 			     size_t size, int flags)
967c8b6056aSAndreas Gruenbacher {
968c8b6056aSAndreas Gruenbacher 	if (is_known_namespace(name))
969c8b6056aSAndreas Gruenbacher 		return -EOPNOTSUPP;
970c8b6056aSAndreas Gruenbacher 	return __jfs_xattr_set(inode, name, value, size, flags);
971c8b6056aSAndreas Gruenbacher }
972c8b6056aSAndreas Gruenbacher 
973c8b6056aSAndreas Gruenbacher static const struct xattr_handler jfs_user_xattr_handler = {
974c8b6056aSAndreas Gruenbacher 	.prefix = XATTR_USER_PREFIX,
975c8b6056aSAndreas Gruenbacher 	.get = jfs_xattr_get,
976c8b6056aSAndreas Gruenbacher 	.set = jfs_xattr_set,
977c8b6056aSAndreas Gruenbacher };
978c8b6056aSAndreas Gruenbacher 
979c8b6056aSAndreas Gruenbacher static const struct xattr_handler jfs_os2_xattr_handler = {
980c8b6056aSAndreas Gruenbacher 	.prefix = XATTR_OS2_PREFIX,
981c8b6056aSAndreas Gruenbacher 	.get = jfs_xattr_get_os2,
982c8b6056aSAndreas Gruenbacher 	.set = jfs_xattr_set_os2,
983c8b6056aSAndreas Gruenbacher };
984c8b6056aSAndreas Gruenbacher 
985c8b6056aSAndreas Gruenbacher static const struct xattr_handler jfs_security_xattr_handler = {
986c8b6056aSAndreas Gruenbacher 	.prefix = XATTR_SECURITY_PREFIX,
987c8b6056aSAndreas Gruenbacher 	.get = jfs_xattr_get,
988c8b6056aSAndreas Gruenbacher 	.set = jfs_xattr_set,
989c8b6056aSAndreas Gruenbacher };
990c8b6056aSAndreas Gruenbacher 
991c8b6056aSAndreas Gruenbacher static const struct xattr_handler jfs_trusted_xattr_handler = {
992c8b6056aSAndreas Gruenbacher 	.prefix = XATTR_TRUSTED_PREFIX,
993c8b6056aSAndreas Gruenbacher 	.get = jfs_xattr_get,
994c8b6056aSAndreas Gruenbacher 	.set = jfs_xattr_set,
995c8b6056aSAndreas Gruenbacher };
996c8b6056aSAndreas Gruenbacher 
9972cc6a5a0SChristoph Hellwig const struct xattr_handler *jfs_xattr_handlers[] = {
998c18f7b51SDave Kleikamp #ifdef CONFIG_JFS_POSIX_ACL
9992cc6a5a0SChristoph Hellwig 	&posix_acl_access_xattr_handler,
10002cc6a5a0SChristoph Hellwig 	&posix_acl_default_xattr_handler,
10012cc6a5a0SChristoph Hellwig #endif
1002c8b6056aSAndreas Gruenbacher 	&jfs_os2_xattr_handler,
1003c8b6056aSAndreas Gruenbacher 	&jfs_user_xattr_handler,
1004c8b6056aSAndreas Gruenbacher 	&jfs_security_xattr_handler,
1005c8b6056aSAndreas Gruenbacher 	&jfs_trusted_xattr_handler,
10062cc6a5a0SChristoph Hellwig 	NULL,
10072cc6a5a0SChristoph Hellwig };
10082cc6a5a0SChristoph Hellwig 
10092cc6a5a0SChristoph Hellwig 
10101d15b10fSDave Kleikamp #ifdef CONFIG_JFS_SECURITY
101121d1101fSDave Kleikamp static int jfs_initxattrs(struct inode *inode, const struct xattr *xattr_array,
10129d8f13baSMimi Zohar 			  void *fs_info)
10139d8f13baSMimi Zohar {
10149d8f13baSMimi Zohar 	const struct xattr *xattr;
10159d8f13baSMimi Zohar 	tid_t *tid = fs_info;
10169d8f13baSMimi Zohar 	char *name;
10179d8f13baSMimi Zohar 	int err = 0;
10189d8f13baSMimi Zohar 
10199d8f13baSMimi Zohar 	for (xattr = xattr_array; xattr->name != NULL; xattr++) {
10209d8f13baSMimi Zohar 		name = kmalloc(XATTR_SECURITY_PREFIX_LEN +
10219d8f13baSMimi Zohar 			       strlen(xattr->name) + 1, GFP_NOFS);
10229d8f13baSMimi Zohar 		if (!name) {
10239d8f13baSMimi Zohar 			err = -ENOMEM;
10249d8f13baSMimi Zohar 			break;
10259d8f13baSMimi Zohar 		}
10269d8f13baSMimi Zohar 		strcpy(name, XATTR_SECURITY_PREFIX);
10279d8f13baSMimi Zohar 		strcpy(name + XATTR_SECURITY_PREFIX_LEN, xattr->name);
10289d8f13baSMimi Zohar 
10299d8f13baSMimi Zohar 		err = __jfs_setxattr(*tid, inode, name,
10309d8f13baSMimi Zohar 				     xattr->value, xattr->value_len, 0);
10319d8f13baSMimi Zohar 		kfree(name);
10329d8f13baSMimi Zohar 		if (err < 0)
10339d8f13baSMimi Zohar 			break;
10349d8f13baSMimi Zohar 	}
10359d8f13baSMimi Zohar 	return err;
10369d8f13baSMimi Zohar }
10379d8f13baSMimi Zohar 
10382a7dba39SEric Paris int jfs_init_security(tid_t tid, struct inode *inode, struct inode *dir,
10392a7dba39SEric Paris 		      const struct qstr *qstr)
10401d15b10fSDave Kleikamp {
10419d8f13baSMimi Zohar 	return security_inode_init_security(inode, dir, qstr,
10429d8f13baSMimi Zohar 					    &jfs_initxattrs, &tid);
10431d15b10fSDave Kleikamp }
10441d15b10fSDave Kleikamp #endif
1045