Index: sys/fs/ext2fs/ext2_alloc.c =================================================================== --- sys/fs/ext2fs/ext2_alloc.c +++ sys/fs/ext2fs/ext2_alloc.c @@ -52,7 +52,6 @@ #include #include -static daddr_t ext2_alloccg(struct inode *, int, daddr_t, int); static daddr_t ext2_clusteralloc(struct inode *, int, daddr_t, int); static u_long ext2_dirpref(struct inode *); static void ext2_fserr(struct m_ext2fs *, uid_t, char *); @@ -644,7 +643,7 @@ * Check to see if a block of the appropriate size is available, * and if it is, allocate it. */ -static daddr_t +daddr_t ext2_alloccg(struct inode *ip, int cg, daddr_t bpref, int size) { struct m_ext2fs *fs; Index: sys/fs/ext2fs/ext2_extattr.h =================================================================== --- sys/fs/ext2fs/ext2_extattr.h +++ sys/fs/ext2fs/ext2_extattr.h @@ -30,25 +30,33 @@ #define _FS_EXT2FS_EXT2_EXTARTTR_H_ /* Linux xattr name indexes */ -#define EXT4_XATTR_INDEX_USER 1 +#define EXT4_XATTR_INDEX_USER 1 #define EXT4_XATTR_INDEX_POSIX_ACL_ACCESS 2 #define EXT4_XATTR_INDEX_POSIX_ACL_DEFAULT 3 -#define EXT4_XATTR_INDEX_TRUSTED 4 -#define EXT4_XATTR_INDEX_LUSTRE 5 -#define EXT4_XATTR_INDEX_SECURITY 6 -#define EXT4_XATTR_INDEX_SYSTEM 7 -#define EXT4_XATTR_INDEX_RICHACL 8 -#define EXT4_XATTR_INDEX_ENCRYPTION 9 +#define EXT4_XATTR_INDEX_TRUSTED 4 +#define EXT4_XATTR_INDEX_LUSTRE 5 +#define EXT4_XATTR_INDEX_SECURITY 6 +#define EXT4_XATTR_INDEX_SYSTEM 7 +#define EXT4_XATTR_INDEX_RICHACL 8 +#define EXT4_XATTR_INDEX_ENCRYPTION 9 /* Magic value in attribute blocks */ #define EXTATTR_MAGIC 0xEA020000 +/* Max EA name length */ +#define EXT2_EXTATTR_NAMELEN_MAX 255 + +/* EA hash constants */ +#define EXT2_EXTATTR_NAME_HASH_SHIFT 5 +#define EXT2_EXTATTR_VALUE_HASH_SHIFT 16 +#define EXT2_EXTATTR_BLOCK_HASH_SHIFT 16 + struct ext2fs_extattr_header { int32_t h_magic; /* magic number for identification */ int32_t h_refcount; /* reference count */ int32_t h_blocks; /* number of disk blocks used */ int32_t h_hash; /* hash value of all attributes */ - uint32_t h_reserved[4]; /* zero right now */ + uint32_t h_reserved[4]; /* zero right now */ }; struct ext2fs_extattr_dinode_header { @@ -56,21 +64,15 @@ }; struct ext2fs_extattr_entry { - uint8_t e_name_len; /* length of name */ + uint8_t e_name_len; /* length of name */ uint8_t e_name_index; /* attribute name index */ uint16_t e_value_offs; /* offset in disk block of value */ uint32_t e_value_block; /* disk block attribute is stored on (n/i) */ uint32_t e_value_size; /* size of attribute value */ - uint32_t e_hash; /* hash value of name and value */ - char e_name[0]; /* attribute name */ + uint32_t e_hash; /* hash value of name and value */ + char e_name[0]; /* attribute name */ }; -#define EXT2_IHDR(inode, raw_inode) \ - ((struct ext4_xattr_ibody_header *) \ - ((void *)raw_inode + \ - EXT4_GOOD_OLD_INODE_SIZE + \ - EXT4_I(inode)->i_extra_isize)) - #define EXT2_IFIRST(hdr) ((struct ext2fs_extattr_entry *)((hdr)+1)) #define EXT2_HDR(bh) ((struct ext2fs_extattr_header *)((bh)->b_data)) @@ -85,20 +87,54 @@ (((name_len) + EXT2_EXTATTR_ROUND + \ sizeof(struct ext2fs_extattr_entry)) & ~EXT2_EXTATTR_ROUND) +#define EXT2_EXTATTR_SIZE(size) \ + (((size) + EXT2_EXTATTR_ROUND) & ~EXT2_EXTATTR_ROUND) + #define EXT2_EXTATTR_NEXT(entry) \ ( (struct ext2fs_extattr_entry *)( \ (char *)(entry) + EXT2_EXTATTR_LEN((entry)->e_name_len)) ) -int ext2_extattr_inode_list(struct inode *ip, int attrnamespace, - struct uio *uio, size_t *size); -int ext2_extattr_block_list(struct inode *ip, int attrnamespace, - struct uio *uio, size_t *size); +#define EXT2_EXTATTR_INODE(ip) \ + (EXT2_INODE_SIZE(ip->i_e2fs) != E2FS_REV0_INODE_SIZE && \ + ip->i_extra_isize) + +#define EXT2_EXTATTR_BLOCK(ip) (1) +#define EXT2_EXTATTR_INODE_SIZE(ip) (EXT2_EXTATTR_INODE(ip) ? \ + (EXT2_INODE_SIZE(ip->i_e2fs) - E2FS_REV0_INODE_SIZE - \ + ip->i_extra_isize) : 0) + +#define EXT2_EXTATTR_BLOCK_SIZE(ip) (ip->i_e2fs->e2fs_bsize) +#define EXT2_EXTATTR_INODE_HDR(ip, addr) \ + (EXT2_EXTATTR_INODE(ip) ? \ + ((struct ext2fs_extattr_dinode_header *)addr) : NULL) + +#define EXT2_EXTATTR_BLOCK_HDR(ip, addr) (EXT2_EXTATTR_BLOCK(ip) ? \ + (EXT2_EXTATTR_INODE(ip) ? \ + ((struct ext2fs_extattr_header *)(addr + \ + EXT2_EXTATTR_INODE_SIZE(ip))) : ((struct ext2fs_extattr_header *) \ + addr)) : NULL) + +int ext2_extattr_check(struct ext2fs_extattr_entry *eae, char *end); +void ext2_extattr_delete_entry(char *off, struct ext2fs_extattr_entry *feae, + struct ext2fs_extattr_entry *eae, char *end); + +int ext2_extattr_free(struct inode *ip); +int ext2_extattr_get_size(struct ext2fs_extattr_entry *feae, + struct ext2fs_extattr_entry *eeae, int hdr_size, + int name_len, int new_size); + +void ext2_extattr_rehash(struct ext2fs_extattr_header *hdr, + struct ext2fs_extattr_entry *eae); + +void ext2_extattr_set_exist_entry(char *off, struct ext2fs_extattr_entry *feae, + struct ext2fs_extattr_entry *eae, char *end, struct uio *uio); -int ext2_extattr_inode_get(struct inode *ip, int attrnamespace, - const char *name, struct uio *uio, size_t *size); +struct ext2fs_extattr_entry *ext2_extattr_set_new_entry(char *off, + struct ext2fs_extattr_entry *feae, const char *name, + int attrnamespace, char *end, struct uio *uio); -int ext2_extattr_block_get(struct inode *ip, int attrnamespace, - const char *name, struct uio *uio, size_t *size); +int ext2_extattr_index_to_bsd(int index); +int ext2_extattr_valid_attrname(int attrnamespace, const char *attrname); #endif /* !_FS_EXT2FS_EXT2_EXTARTTR_H_ */ Index: sys/fs/ext2fs/ext2_extattr.c =================================================================== --- sys/fs/ext2fs/ext2_extattr.c +++ sys/fs/ext2fs/ext2_extattr.c @@ -1,4 +1,4 @@ -/*- +/*- * Copyright (c) 2017, Fedor Uporov * All rights reserved. * @@ -44,239 +44,321 @@ #include #include #include +#include - -static int +int ext2_extattr_index_to_bsd(int index) { switch (index) { + case EXT4_XATTR_INDEX_SYSTEM: + return EXTATTR_NAMESPACE_SYSTEM; + case EXT4_XATTR_INDEX_USER: return EXTATTR_NAMESPACE_USER; + } - case EXT4_XATTR_INDEX_SYSTEM: - return EXTATTR_NAMESPACE_SYSTEM; + return EXTATTR_NAMESPACE_EMPTY; +} + +static int +ext2_extattr_index_to_linux(int index) +{ + switch (index) { + case EXTATTR_NAMESPACE_SYSTEM: + return EXT4_XATTR_INDEX_SYSTEM; - default: - return EXTATTR_NAMESPACE_EMPTY; + case EXTATTR_NAMESPACE_USER: + return EXT4_XATTR_INDEX_USER; } + + return (-1); } int -ext2_extattr_inode_list(struct inode *ip, int attrnamespace, - struct uio *uio, size_t *size) +ext2_extattr_valid_attrname(int attrnamespace, const char *attrname) +{ + if (attrnamespace == EXTATTR_NAMESPACE_EMPTY) + return (EINVAL); + + if (strlen(attrname) == 0) + return (EINVAL); + + if (strlen(attrname) + 1 > EXT2_EXTATTR_NAMELEN_MAX) + return (ENAMETOOLONG); + + return (0); +} + +int +ext2_extattr_check(struct ext2fs_extattr_entry *eae, char *end) { - struct m_ext2fs *fs; - struct buf *bp; - struct ext2fs_extattr_dinode_header *header; - struct ext2fs_extattr_entry *entry; struct ext2fs_extattr_entry *next; - char *end; - int error; - fs = ip->i_e2fs; + while (!EXT2_IS_LAST_ENTRY(eae)) { + next = EXT2_EXTATTR_NEXT(eae); + if ((char *)next >= end) + return (EIO); - if ((error = bread(ip->i_devvp, - fsbtodb(fs, ino_to_fsba(fs, ip->i_number)), - (int)fs->e2fs_bsize, NOCRED, &bp)) != 0) { - brelse(bp); - return (error); + eae = next; } - struct ext2fs_dinode *dinode = (struct ext2fs_dinode *) - ((char *)bp->b_data + - EXT2_INODE_SIZE(fs) * ino_to_fsbo(fs, ip->i_number)); + return (0); +} - /* Check attributes magic value */ - header = (struct ext2fs_extattr_dinode_header *)((char *)dinode + - E2FS_REV0_INODE_SIZE + dinode->e2di_extra_isize); +static uint16_t +ext2_extattr_delete_value(char *off, + struct ext2fs_extattr_entry *feae, + struct ext2fs_extattr_entry *eae, char *end) +{ + uint16_t min_offs; + struct ext2fs_extattr_entry *next; - if (header->h_magic != EXTATTR_MAGIC) { - brelse(bp); - return (0); + min_offs = end - off; + next = feae; + while (!EXT2_IS_LAST_ENTRY(next)) { + if (min_offs > next->e_value_offs && next->e_value_offs > 0) + min_offs = next->e_value_offs; + + next = EXT2_EXTATTR_NEXT(next); } - /* Check attributes integrity */ - entry = EXT2_IFIRST(header); - end = (char *)dinode + EXT2_INODE_SIZE(fs); - while (!EXT2_IS_LAST_ENTRY(entry)) { - next = EXT2_EXTATTR_NEXT(entry); - if ((char *)next >= end) { - brelse(bp); - return (EIO); - } + if (eae->e_value_size == 0) + return min_offs; - entry = next; - } + memmove(off + min_offs + EXT2_EXTATTR_SIZE(eae->e_value_size), + off + min_offs, eae->e_value_offs - min_offs); - for (entry = EXT2_IFIRST(header); !EXT2_IS_LAST_ENTRY(entry); - entry = EXT2_EXTATTR_NEXT(entry)) { - if (ext2_extattr_index_to_bsd(entry->e_name_index) != attrnamespace) - continue; - - if (uio == NULL) - *size += entry->e_name_len + 1; - else { - char *attr_name = malloc(entry->e_name_len + 1, M_TEMP, M_WAITOK); - attr_name[0] = entry->e_name_len; - memcpy(&attr_name[1], entry->e_name, entry->e_name_len); - error = uiomove(attr_name, entry->e_name_len + 1, uio); - free(attr_name, M_TEMP); - } + /* Adjust all value offsets */ + next = feae; + while (!EXT2_IS_LAST_ENTRY(next)) { + if (next->e_value_offs > 0 && + next->e_value_offs < eae->e_value_offs) + next->e_value_offs += + EXT2_EXTATTR_SIZE(eae->e_value_size); + + next = EXT2_EXTATTR_NEXT(next); } - brelse(bp); + min_offs += EXT2_EXTATTR_SIZE(eae->e_value_size); - return (0); + return min_offs; } -int -ext2_extattr_block_list(struct inode *ip, int attrnamespace, - struct uio *uio, size_t *size) +void +ext2_extattr_delete_entry(char *off, + struct ext2fs_extattr_entry *feae, + struct ext2fs_extattr_entry *eae, char *end) { - struct m_ext2fs *fs; - struct buf *bp; - struct ext2fs_extattr_header *header; - struct ext2fs_extattr_entry *entry; + char *pad; struct ext2fs_extattr_entry *next; - char *end; - int error; - fs = ip->i_e2fs; + /* Clean entry value */ + ext2_extattr_delete_value(off, feae, eae, end); - error = bread(ip->i_devvp, fsbtodb(fs, ip->i_facl), - fs->e2fs_bsize, NOCRED, &bp); - if (error) { - brelse(bp); - return (error); - } + /* Clean the entry */ + next = feae; + while (!EXT2_IS_LAST_ENTRY(next)) + next = EXT2_EXTATTR_NEXT(next); - /* Check attributes magic value */ - header = EXT2_HDR(bp); - if (header->h_magic != EXTATTR_MAGIC || header->h_blocks != 1) { - brelse(bp); - return (EINVAL); - } + pad = (char*)next + sizeof(uint32_t); - /* Check attributes integrity */ - end = bp->b_data + bp->b_bufsize; - entry = EXT2_FIRST_ENTRY(bp); - while (!EXT2_IS_LAST_ENTRY(entry)) { - next = EXT2_EXTATTR_NEXT(entry); - if ((char *)next >= end) { - brelse(bp); - return (EIO); - } + memmove(eae, (char *)eae + EXT2_EXTATTR_LEN(eae->e_name_len), + pad - ((char *)eae + EXT2_EXTATTR_LEN(eae->e_name_len))); +} + +static struct ext2fs_extattr_entry * +allocate_entry(const char *name, int attrnamespace, uint16_t offs, + uint32_t size, uint32_t hash) +{ + size_t name_len; + struct ext2fs_extattr_entry *eae; + + name_len = strlen(name); + eae = malloc(sizeof(struct ext2fs_extattr_entry) + name_len, + M_TEMP, M_WAITOK); + + eae->e_name_len = name_len; + eae->e_name_index = ext2_extattr_index_to_linux(attrnamespace); + eae->e_value_offs = offs; + eae->e_value_block = 0; + eae->e_value_size = size; + eae->e_hash = hash; + memcpy(eae->e_name, name, name_len); + + return eae; +} + +static void +free_entry(struct ext2fs_extattr_entry *eae) +{ - entry = next; + free(eae, M_TEMP); +} + +int +ext2_extattr_get_size(struct ext2fs_extattr_entry *feae, + struct ext2fs_extattr_entry *eeae, int header_size, + int name_len, int new_size) +{ + struct ext2fs_extattr_entry *eae; + int size; + + size = header_size; + size += sizeof(uint32_t); + + if (!eeae) { + size += EXT2_EXTATTR_LEN(name_len); + size += EXT2_EXTATTR_SIZE(new_size); } - for (entry = EXT2_FIRST_ENTRY(bp); !EXT2_IS_LAST_ENTRY(entry); - entry = EXT2_EXTATTR_NEXT(entry)) { - if (ext2_extattr_index_to_bsd(entry->e_name_index) != attrnamespace) - continue; - - if (uio == NULL) - *size += entry->e_name_len + 1; - else { - char *attr_name = malloc(entry->e_name_len + 1, M_TEMP, M_WAITOK); - attr_name[0] = entry->e_name_len; - memcpy(&attr_name[1], entry->e_name, entry->e_name_len); - error = uiomove(attr_name, entry->e_name_len + 1, uio); - free(attr_name, M_TEMP); + if (feae) + for (eae = feae; !EXT2_IS_LAST_ENTRY(eae); + eae = EXT2_EXTATTR_NEXT(eae)) { + if (eae != eeae) + size += EXT2_EXTATTR_LEN(eae->e_name_len) + + EXT2_EXTATTR_SIZE(eae->e_value_size); + else + size += EXT2_EXTATTR_LEN(eae->e_name_len) + + EXT2_EXTATTR_SIZE(new_size); } - } - brelse(bp); + return (size); +} - return (0); +void +ext2_extattr_set_exist_entry(char *off, + struct ext2fs_extattr_entry *feae, + struct ext2fs_extattr_entry *eae, + char *end, struct uio *uio) +{ + uint16_t min_offs; + + min_offs = ext2_extattr_delete_value(off, feae, eae, end); + + eae->e_value_size = uio->uio_resid; + if (eae->e_value_size) + eae->e_value_offs = min_offs - + EXT2_EXTATTR_SIZE(uio->uio_resid); + else + eae->e_value_offs = 0; } -int -ext2_extattr_inode_get(struct inode *ip, int attrnamespace, - const char *name, struct uio *uio, size_t *size) +struct ext2fs_extattr_entry * +ext2_extattr_set_new_entry(char *off, struct ext2fs_extattr_entry *feae, + const char *name, int attrnamespace, char *end, struct uio *uio) { - struct m_ext2fs *fs; - struct buf *bp; - struct ext2fs_extattr_dinode_header *header; - struct ext2fs_extattr_entry *entry; - struct ext2fs_extattr_entry *next; - char *end; - int error; + int name_len; + char *pad; + uint16_t min_offs; + struct ext2fs_extattr_entry *eae; + struct ext2fs_extattr_entry *new_eae; + + /* Find pad's */ + min_offs = end - off; + eae = feae; + while (!EXT2_IS_LAST_ENTRY(eae)) { + if (min_offs > eae->e_value_offs && eae->e_value_offs > 0) + min_offs = eae->e_value_offs; + + eae = EXT2_EXTATTR_NEXT(eae); + } - fs = ip->i_e2fs; + pad = (char*)eae + sizeof(uint32_t); - if ((error = bread(ip->i_devvp, - fsbtodb(fs, ino_to_fsba(fs, ip->i_number)), - (int)fs->e2fs_bsize, NOCRED, &bp)) != 0) { - brelse(bp); - return (error); + /* Find entry insert position */ + name_len = strlen(name); + eae = feae; + while (!EXT2_IS_LAST_ENTRY(eae)) { + if (!(attrnamespace - eae->e_name_index) && + !(name_len - eae->e_name_len)) + if (memcmp(name, eae->e_name, name_len) <= 0) + break; + + eae = EXT2_EXTATTR_NEXT(eae); } - struct ext2fs_dinode *dinode = (struct ext2fs_dinode *) - ((char *)bp->b_data + - EXT2_INODE_SIZE(fs) * ino_to_fsbo(fs, ip->i_number)); + /* Create new entry and insert it */ + new_eae = allocate_entry(name, attrnamespace, 0, uio->uio_resid, 0); + memmove((char *)eae + EXT2_EXTATTR_LEN(new_eae->e_name_len), eae, + pad - (char*)eae); - /* Check attributes magic value */ - header = (struct ext2fs_extattr_dinode_header *)((char *)dinode + - E2FS_REV0_INODE_SIZE + dinode->e2di_extra_isize); + memcpy(eae, new_eae, EXT2_EXTATTR_LEN(new_eae->e_name_len)); + free_entry(new_eae); - if (header->h_magic != EXTATTR_MAGIC) { - brelse(bp); - return (0); + new_eae = eae; + if (new_eae->e_value_size > 0) + new_eae->e_value_offs = min_offs - + EXT2_EXTATTR_SIZE(new_eae->e_value_size); + + return new_eae; +} + +static void +ext2_extattr_hash_entry(struct ext2fs_extattr_header *hdr, + struct ext2fs_extattr_entry *eae) +{ + uint32_t hash = 0; + char *name = eae->e_name; + int n; + + for (n=0; n < eae->e_name_len; n++) { + hash = (hash << EXT2_EXTATTR_NAME_HASH_SHIFT) ^ + (hash >> (8*sizeof(hash) - EXT2_EXTATTR_NAME_HASH_SHIFT)) ^ + (*name++); } - /* Check attributes integrity */ - entry = EXT2_IFIRST(header); - end = (char *)dinode + EXT2_INODE_SIZE(fs); - while (!EXT2_IS_LAST_ENTRY(entry)) { - next = EXT2_EXTATTR_NEXT(entry); - if ((char *)next >= end) { - brelse(bp); - return (EIO); + if (eae->e_value_block == 0 && eae->e_value_size != 0) { + uint32_t *value = (uint32_t *)((char *)hdr + eae->e_value_offs); + for (n = (eae->e_value_size + + EXT2_EXTATTR_ROUND) >> EXT2_EXTATTR_PAD_BITS; n; n--) { + hash = (hash << EXT2_EXTATTR_VALUE_HASH_SHIFT) ^ + (hash >> (8*sizeof(hash) - EXT2_EXTATTR_VALUE_HASH_SHIFT)) ^ + (*value++); } - - entry = next; } - for (entry = EXT2_IFIRST(header); !EXT2_IS_LAST_ENTRY(entry); - entry = EXT2_EXTATTR_NEXT(entry)) { - if (ext2_extattr_index_to_bsd(entry->e_name_index) != attrnamespace) - continue; - - if (strlen(name) == entry->e_name_len && - 0 == strncmp(entry->e_name, name, entry->e_name_len)) { - if (uio == NULL) - *size += entry->e_value_size; - else { - error = uiomove(((char *)EXT2_IFIRST(header)) + entry->e_value_offs, - entry->e_value_size, uio); - if (error) { - brelse(bp); - return (error); - } - } + eae->e_hash = hash; +} + +void +ext2_extattr_rehash(struct ext2fs_extattr_header *hdr, + struct ext2fs_extattr_entry *eae) +{ + struct ext2fs_extattr_entry *here; + uint32_t hash = 0; + + ext2_extattr_hash_entry(hdr, eae); + + here = EXT2_ENTRY(hdr+1); + while (!EXT2_IS_LAST_ENTRY(here)) { + if (!here->e_hash) { + /* Block is not shared if an entry's hash value == 0 */ + hash = 0; + break; } - } - brelse(bp); + hash = (hash << EXT2_EXTATTR_BLOCK_HASH_SHIFT) ^ + (hash >> (8*sizeof(hash) - EXT2_EXTATTR_BLOCK_HASH_SHIFT)) ^ + here->e_hash; - return (0); + here = EXT2_EXTATTR_NEXT(here); + } + + hdr->h_hash = hash; } -int -ext2_extattr_block_get(struct inode *ip, int attrnamespace, - const char *name, struct uio *uio, size_t *size) +int ext2_extattr_free(struct inode *ip) { struct m_ext2fs *fs; struct buf *bp; - struct ext2fs_extattr_header *header; - struct ext2fs_extattr_entry *entry; - struct ext2fs_extattr_entry *next; - char *end; + struct ext2fs_extattr_header *hdr; int error; fs = ip->i_e2fs; + if (!ip->i_facl) + return (0); + error = bread(ip->i_devvp, fsbtodb(fs, ip->i_facl), fs->e2fs_bsize, NOCRED, &bp); if (error) { @@ -285,46 +367,29 @@ } /* Check attributes magic value */ - header = EXT2_HDR(bp); - if (header->h_magic != EXTATTR_MAGIC || header->h_blocks != 1) { + hdr = EXT2_HDR(bp); + if (hdr->h_magic != EXTATTR_MAGIC || hdr->h_blocks != 1) { brelse(bp); return (EINVAL); } - /* Check attributes integrity */ - end = bp->b_data + bp->b_bufsize; - entry = EXT2_FIRST_ENTRY(bp); - while (!EXT2_IS_LAST_ENTRY(entry)) { - next = EXT2_EXTATTR_NEXT(entry); - if ((char *)next >= end) { - brelse(bp); - return (EIO); - } - - entry = next; + error = ext2_extattr_check(EXT2_FIRST_ENTRY(bp), bp->b_data + bp->b_bufsize); + if (error) { + brelse(bp); + return (error); } - for (entry = EXT2_FIRST_ENTRY(bp); !EXT2_IS_LAST_ENTRY(entry); - entry = EXT2_EXTATTR_NEXT(entry)) { - if (ext2_extattr_index_to_bsd(entry->e_name_index) != attrnamespace) - continue; - - if (strlen(name) == entry->e_name_len && - 0 == strncmp(entry->e_name, name, entry->e_name_len)) { - if (uio == NULL) - *size += entry->e_value_size; - else { - error = uiomove(bp->b_data + entry->e_value_offs, - entry->e_value_size, uio); - if (error) { - brelse(bp); - return (error); - } - } - } - } + if (hdr->h_refcount > 1) { + hdr->h_refcount--; + bwrite(bp); + } else { + ext2_blkfree(ip, ip->i_facl, ip->i_e2fs->e2fs_bsize); + brelse(bp); + } - brelse(bp); + ip->i_blocks -= btodb(ip->i_e2fs->e2fs_bsize); + ip->i_facl = 0; + ext2_update(ip->i_vnode, 1); return (0); } Index: sys/fs/ext2fs/ext2_extern.h =================================================================== --- sys/fs/ext2fs/ext2_extern.h +++ sys/fs/ext2fs/ext2_extern.h @@ -51,6 +51,7 @@ int ext2_add_entry(struct vnode *, struct ext2fs_direct_2 *); int ext2_alloc(struct inode *, daddr_t, e4fs_daddr_t, int, struct ucred *, e4fs_daddr_t *); +daddr_t ext2_alloccg(struct inode *ip, int cg, daddr_t bpref, int size); int ext2_balloc(struct inode *, e2fs_lbn_t, int, struct ucred *, struct buf **, int); int ext2_blkatoff(struct vnode *, off_t, char **, struct buf **); Index: sys/fs/ext2fs/ext2_inode.c =================================================================== --- sys/fs/ext2fs/ext2_inode.c +++ sys/fs/ext2fs/ext2_inode.c @@ -53,6 +53,7 @@ #include #include #include +#include static int ext2_indirtrunc(struct inode *, daddr_t, daddr_t, daddr_t, int, e4fs_daddr_t *); @@ -488,6 +489,7 @@ if (ip->i_mode == 0) goto out; if (ip->i_nlink <= 0) { + ext2_extattr_free(ip); error = ext2_truncate(vp, (off_t)0, 0, NOCRED, td); ip->i_rdev = 0; mode = ip->i_mode; Index: sys/fs/ext2fs/ext2_inode_cnv.c =================================================================== --- sys/fs/ext2fs/ext2_inode_cnv.c +++ sys/fs/ext2fs/ext2_inode_cnv.c @@ -51,8 +51,8 @@ printf("Inode: %5ju", (uintmax_t)in->i_number); printf( /* "Inode: %5d" */ - " Type: %10s Mode: 0x%o Flags: 0x%x Version: %d\n", - "n/a", in->i_mode, in->i_flags, in->i_gen); + " Type: %10s Mode: 0x%o Flags: 0x%x Version: %d acl: 0x%llx\n", + "n/a", in->i_mode, in->i_flags, in->i_gen, in->i_facl); printf("User: %5u Group: %5u Size: %ju\n", in->i_uid, in->i_gid, (uintmax_t)in->i_size); printf("Links: %3d Blockcount: %ju\n", @@ -62,6 +62,8 @@ printf("mtime: 0x%x", in->i_mtime); if (E2DI_HAS_XTIME(in)) printf("crtime %#x ", in->i_birthtime); + if (EXT2_INODE_SIZE(in->i_e2fs) != E2FS_REV0_INODE_SIZE) + printf("extra_size %d\n", in->i_extra_isize); printf("BLOCKS:"); for (i = 0; i < (in->i_blocks <= 24 ? (in->i_blocks + 1) / 2 : 12); i++) printf(" %d", in->i_db[i]); @@ -129,6 +131,11 @@ ip->i_db[i] = ei->e2di_blocks[i]; for (i = 0; i < EXT2_NIADDR; i++) ip->i_ib[i] = ei->e2di_blocks[EXT2_NDIR_BLOCKS + i]; + + if (EXT2_INODE_SIZE(ip->i_e2fs) != E2FS_REV0_INODE_SIZE) + ip->i_extra_isize = ei->e2di_extra_isize; + else + ip->i_extra_isize = 0; } /* @@ -167,6 +174,8 @@ ei->e2di_flags |= (ip->i_flag & IN_E4EXTENTS) ? EXT4_EXTENTS : 0; ei->e2di_nblock = ip->i_blocks & 0xffffffff; ei->e2di_nblock_high = ip->i_blocks >> 32 & 0xffff; + ei->e2di_facl = ip->i_facl & 0xffffffff; + ei->e2di_facl_high = ip->i_facl >> 32 & 0xffff; ei->e2di_gen = ip->i_gen; ei->e2di_uid = ip->i_uid; ei->e2di_gid = ip->i_gid; Index: sys/fs/ext2fs/ext2_vnops.c =================================================================== --- sys/fs/ext2fs/ext2_vnops.c +++ sys/fs/ext2fs/ext2_vnops.c @@ -117,8 +117,12 @@ static vop_strategy_t ext2_strategy; static vop_symlink_t ext2_symlink; static vop_write_t ext2_write; -static vop_getextattr_t ext2_getextattr; +static vop_closeextattr_t ext2_closeextattr; +static vop_deleteextattr_t ext2_deleteextattr; +static vop_getextattr_t ext2_getextattr; static vop_listextattr_t ext2_listextattr; +static vop_openextattr_t ext2_openextattr; +static vop_setextattr_t ext2_setextattr; static vop_vptofh_t ext2_vptofh; static vop_close_t ext2fifo_close; static vop_kqfilter_t ext2fifo_kqfilter; @@ -157,8 +161,12 @@ .vop_strategy = ext2_strategy, .vop_symlink = ext2_symlink, .vop_write = ext2_write, + .vop_closeextattr = ext2_closeextattr, + .vop_deleteextattr = ext2_deleteextattr, .vop_getextattr = ext2_getextattr, .vop_listextattr = ext2_listextattr, + .vop_openextattr = ext2_openextattr, + .vop_setextattr = ext2_setextattr, .vop_vptofh = ext2_vptofh, }; @@ -1486,14 +1494,447 @@ } /* - * Vnode operation to retrieve a named extended attribute. + * Extended attribute area reading. */ static int -ext2_getextattr(struct vop_getextattr_args *ap) +ext2_extread(struct vnode *vp, struct uio *uio, int ioflag) +{ + struct inode *ip; + struct m_ext2fs *fs; + struct buf *bp; + struct ext2fs_extattr_header *hdr; + int error; + + ip = VTOI(vp); + fs = ip->i_e2fs; + + if (EXT2_INODE_SIZE(fs) != E2FS_REV0_INODE_SIZE && ip->i_extra_isize) { + if ((error = bread(ip->i_devvp, + fsbtodb(fs, ino_to_fsba(fs, ip->i_number)), + (int)fs->e2fs_bsize, NOCRED, &bp)) != 0) { + brelse(bp); + return (error); + } + + error = uiomove((char *)bp->b_data + EXT2_INODE_SIZE(fs) * + ino_to_fsbo(fs, ip->i_number) + + E2FS_REV0_INODE_SIZE + ip->i_extra_isize, + EXT2_EXTATTR_INODE_SIZE(ip), uio); + vfs_bio_brelse(bp, ioflag); + if (error) + return (error); + } + + if (ip->i_facl) { + error = bread(ip->i_devvp, fsbtodb(fs, ip->i_facl), + fs->e2fs_bsize, NOCRED, &bp); + if (error) { + brelse(bp); + return (error); + } + + error = uiomove(bp->b_data, fs->e2fs_bsize, uio); + vfs_bio_brelse(bp, ioflag); + } else { + hdr = (struct ext2fs_extattr_header *)uio->uio_iov->iov_base; + hdr->h_magic = EXTATTR_MAGIC; + hdr->h_refcount = 1; + hdr->h_blocks = 1; + hdr->h_hash = 0; + memset(hdr->h_reserved, 0, sizeof(hdr->h_reserved)); + memset(EXT2_IFIRST(hdr), 0, sizeof(uint32_t)); + } + + return (error); +} + +static int +ext2_extattr_block_clone(struct inode *ip) +{ + struct m_ext2fs *fs; + struct buf *sbp; + struct ext2fs_extattr_header *hdr; + uint64_t facl; + int error; + + fs = ip->i_e2fs; + + hdr = EXT2_EXTATTR_BLOCK_HDR(ip, ip->i_ea_area); + if (hdr->h_magic != EXTATTR_MAGIC || hdr->h_blocks != 1) + return (EINVAL); + + if (hdr->h_refcount == 1) + return (0); + + /* Increment refcount on original block and sync it */ + error = bread(ip->i_devvp, fsbtodb(fs, ip->i_facl), + fs->e2fs_bsize, NOCRED, &sbp); + if (error) { + brelse(sbp); + return (error); + } + + hdr = EXT2_HDR(sbp); + hdr->h_refcount--; + bwrite(sbp); + + EXT2_LOCK(ip->i_ump); + facl = ext2_alloccg(ip, ino_to_cg(fs, ip->i_number), 0, fs->e2fs_bsize); + if (0 == facl) { + EXT2_UNLOCK(ip->i_ump); + return (ENOSPC); + } + + ip->i_facl = facl; + ext2_update(ip->i_vnode, 1); + + /* Restore cloned header */ + hdr = EXT2_EXTATTR_BLOCK_HDR(ip, ip->i_ea_area); + hdr->h_refcount = 1; + + return (0); +} + +/* + * Extended attribute area writing. + */ +static int +ext2_extwrite(struct vnode *vp, struct uio *uio, int ioflag, struct ucred *ucred) +{ + struct inode *ip; + struct m_ext2fs *fs; + struct buf *bp; + struct ext2fs_extattr_header *hdr; + struct ext2fs_extattr_entry *eap; + ssize_t resid; + int error; + + ip = VTOI(vp); + fs = ip->i_e2fs; + resid = uio->uio_resid; + + if (EXT2_EXTATTR_INODE(ip)) { + if ((error = bread(ip->i_devvp, + fsbtodb(fs, ino_to_fsba(fs, ip->i_number)), + (int)fs->e2fs_bsize, NOCRED, &bp)) != 0) { + brelse(bp); + return (error); + } + + error = uiomove((char *)bp->b_data + EXT2_INODE_SIZE(fs) * + ino_to_fsbo(fs, ip->i_number) + + E2FS_REV0_INODE_SIZE + ip->i_extra_isize, + EXT2_EXTATTR_INODE_SIZE(ip), uio); + if (error) { + brelse(bp); + return (error); + } + + vfs_bio_set_flags(bp, ioflag); + + if (ioflag & IO_SYNC) { + (void)bwrite(bp); + } else if (vm_page_count_severe() || + buf_dirty_count_severe() || + (ioflag & (IO_ASYNC | IO_DIRECT))) + bawrite(bp); + else + bdwrite(bp); + } + + if (EXT2_EXTATTR_BLOCK(ip)) { + /* Allocate block if was not allocated yet */ + if (!ip->i_facl) { + EXT2_LOCK(ip->i_ump); + ip->i_facl = ext2_alloccg(ip, + ino_to_cg(fs, ip->i_number), 0, fs->e2fs_bsize); + if (0 == ip->i_facl) { + EXT2_UNLOCK(ip->i_ump); + return (ENOSPC); + } + + ip->i_blocks += btodb(fs->e2fs_bsize); + if (ioflag & IO_SYNC) + error = ext2_update(ip->i_vnode, 1); + } + + /* Check if block cloning required */ + hdr = EXT2_EXTATTR_BLOCK_HDR(ip, ip->i_ea_area); + if (hdr->h_refcount != 1) { + error = ext2_extattr_block_clone(ip); + if (error) + return error; + } + + /* Free block if EA is empty */ + eap = EXT2_IFIRST(hdr); + if (EXT2_IS_LAST_ENTRY(eap)) { + ip->i_blocks -= btodb(fs->e2fs_bsize); + ext2_blkfree(ip, ip->i_facl, fs->e2fs_bsize); + ip->i_facl = 0; + if (ioflag & IO_SYNC) + error = ext2_update(ip->i_vnode, 1); + return (error); + } + + bp = getblk(ip->i_devvp, fsbtodb(fs, ip->i_facl), + fs->e2fs_bsize, 0, 0, 0); + if (!bp) + return (EIO); + + error = uiomove(bp->b_data, fs->e2fs_bsize, uio); + if (error) { + brelse(bp); + return (error); + } + + vfs_bio_set_flags(bp, ioflag); + + if (ioflag & IO_SYNC) { + (void)bwrite(bp); + } else if (vm_page_count_severe() || + buf_dirty_count_severe() || + (ioflag & (IO_ASYNC | IO_DIRECT))) + bawrite(bp); + else + bdwrite(bp); + } + + return (0); +} + +/* + * Vnode operating to retrieve a named extended attribute. + * + * Locate a particular EA (nspace:name) using first entry pointer, and return + * the length of the EA, and possibly the pointer to the entry and to the data. + */ +static int +ext2_findextattr(int nspace, const char *name, char *off, + struct ext2fs_extattr_entry *eae, struct ext2fs_extattr_entry **eaepp, + u_char **eac) +{ + + while (!EXT2_IS_LAST_ENTRY(eae)) { + if (ext2_extattr_index_to_bsd(eae->e_name_index) != + nspace) + continue; + + if (strlen(name) == eae->e_name_len && + 0 == strncmp(eae->e_name, name, eae->e_name_len)) { + if (eac) + *eac = off + eae->e_value_offs; + if (eaepp) + *eaepp = eae; + + return eae->e_value_size; + } + + eae = EXT2_EXTATTR_NEXT(eae); + } + + return (-1); +} + +static int +ext2_rdextattr(u_char **p, struct vnode *vp, struct thread *td, int extra) { struct inode *ip; struct m_ext2fs *fs; + struct uio luio; + struct iovec liovec; + u_int easize; int error; + u_char *eae; + + ip = VTOI(vp); + fs = ip->i_e2fs; + easize = 0; + + if (EXT2_INODE_SIZE(fs) != E2FS_REV0_INODE_SIZE && ip->i_extra_isize) + easize += EXT2_EXTATTR_INODE_SIZE(ip); + easize += fs->e2fs_bsize; + + eae = malloc(easize, M_TEMP, M_WAITOK); + + liovec.iov_base = eae; + liovec.iov_len = easize; + luio.uio_iov = &liovec; + luio.uio_iovcnt = 1; + luio.uio_offset = 0; + luio.uio_resid = easize; + luio.uio_segflg = UIO_SYSSPACE; + luio.uio_rw = UIO_READ; + luio.uio_td = td; + + error = ext2_extread(vp, &luio, IO_EXT | IO_SYNC); + if (error) { + free(eae, M_TEMP); + return(error); + } + + ip->i_ea_len = easize; + *p = eae; + + return (0); +} + +static void +ext2_lock_ea(struct vnode *vp) +{ + struct inode *ip; + + ip = VTOI(vp); + + VI_LOCK(vp); + while (ip->i_flag & IN_EA_LOCKED) { + ip->i_flag |= IN_EA_LOCKWAIT; + msleep(&ip->i_ea_refs, &vp->v_interlock, PINOD + 2, "ext2_ea", + 0); + } + + ip->i_flag |= IN_EA_LOCKED; + VI_UNLOCK(vp); +} + +static void +ext2_unlock_ea(struct vnode *vp) +{ + struct inode *ip; + + ip = VTOI(vp); + + VI_LOCK(vp); + if (ip->i_flag & IN_EA_LOCKWAIT) + wakeup(&ip->i_ea_refs); + ip->i_flag &= ~(IN_EA_LOCKED | IN_EA_LOCKWAIT); + VI_UNLOCK(vp); +} + +static int +ext2_open_ea(struct vnode *vp, struct ucred *cred, struct thread *td) +{ + struct inode *ip; + int error; + + ip = VTOI(vp); + + ext2_lock_ea(vp); + if (ip->i_ea_area != NULL) { + ip->i_ea_refs++; + ext2_unlock_ea(vp); + return (0); + } + error = ext2_rdextattr(&ip->i_ea_area, vp, td, 0); + if (error) { + ext2_unlock_ea(vp); + return (error); + } + ip->i_ea_error = 0; + ip->i_ea_refs++; + ext2_unlock_ea(vp); + return (0); +} + +/* + * Vnode extattr transaction commit/abort + */ +static int +ext2_close_ea(struct vnode *vp, int commit, struct ucred *cred, + struct thread *td) +{ + struct inode *ip; + struct uio luio; + struct iovec liovec; + int error; + + ip = VTOI(vp); + + ext2_lock_ea(vp); + if (ip->i_ea_area == NULL) { + ext2_unlock_ea(vp); + return (EINVAL); + } + + error = ip->i_ea_error; + if (commit && error == 0) { + ASSERT_VOP_ELOCKED(vp, "ext2_close_ea commit"); + if (cred == NOCRED) + cred = vp->v_mount->mnt_cred; + liovec.iov_base = ip->i_ea_area; + liovec.iov_len = ip->i_ea_len; + luio.uio_iov = &liovec; + luio.uio_iovcnt = 1; + luio.uio_offset = 0; + luio.uio_resid = ip->i_ea_len; + luio.uio_segflg = UIO_SYSSPACE; + luio.uio_rw = UIO_WRITE; + luio.uio_td = td; + error = ext2_extwrite(vp, &luio, IO_EXT | IO_SYNC, cred); + } + if (--ip->i_ea_refs == 0) { + free(ip->i_ea_area, M_TEMP); + ip->i_ea_area = NULL; + ip->i_ea_len = 0; + ip->i_ea_error = 0; + } + ext2_unlock_ea(vp); + return (error); +} + +static int +ext2_openextattr(struct vop_openextattr_args *ap) +{ + struct inode *ip; + + ip = VTOI(ap->a_vp); + + if (!EXT2_HAS_COMPAT_FEATURE(ip->i_e2fs, EXT2F_COMPAT_EXT_ATTR)) + return (EOPNOTSUPP); + + if (ap->a_vp->v_type == VCHR || ap->a_vp->v_type == VBLK) + return (EOPNOTSUPP); + + return (ext2_open_ea(ap->a_vp, ap->a_cred, ap->a_td)); +} + +/* + * Vnode extattr transaction commit/abort + */ +static int +ext2_closeextattr(struct vop_closeextattr_args *ap) +{ + struct inode *ip; + + ip = VTOI(ap->a_vp); + + if (!EXT2_HAS_COMPAT_FEATURE(ip->i_e2fs, EXT2F_COMPAT_EXT_ATTR)) + return (EOPNOTSUPP); + + if (ap->a_vp->v_type == VCHR || ap->a_vp->v_type == VBLK) + return (EOPNOTSUPP); + + if (ap->a_commit && (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY)) + return (EROFS); + + return (ext2_close_ea(ap->a_vp, ap->a_commit, ap->a_cred, ap->a_td)); +} + +/* + * Vnode operation to remove a named attribute. + */ +static int +ext2_deleteextattr(struct vop_deleteextattr_args *ap) +{ + struct inode *ip; + struct m_ext2fs *fs; + struct ext2fs_extattr_dinode_header *ihdr; + struct ext2fs_extattr_header *hdr; + struct ext2fs_extattr_entry *eae; + int olen, error, easize; + u_char *eap; + char *eaend; + void *tmp; ip = VTOI(ap->a_vp); fs = ip->i_e2fs; @@ -1504,6 +1945,119 @@ if (ap->a_vp->v_type == VCHR || ap->a_vp->v_type == VBLK) return (EOPNOTSUPP); + if (strlen(ap->a_name) == 0) + return (EINVAL); + + if (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY) + return (EROFS); + + error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace, + ap->a_cred, ap->a_td, VWRITE); + if (error) { + if (ip->i_ea_area != NULL && ip->i_ea_error == 0) + ip->i_ea_error = error; + return (error); + } + + error = ext2_open_ea(ap->a_vp, ap->a_cred, ap->a_td); + if (error) + return (error); + + eap = malloc(ip->i_ea_len, M_TEMP, M_WAITOK); + bcopy(ip->i_ea_area, eap, ip->i_ea_len); + + if (EXT2_EXTATTR_INODE(ip)) { + ihdr = EXT2_EXTATTR_INODE_HDR(ip, eap); + easize = EXT2_EXTATTR_INODE_SIZE(ip); + eae = EXT2_IFIRST(ihdr); + eaend = (char *)ihdr + easize; + + if (ihdr->h_magic != EXTATTR_MAGIC) { + error = ENOATTR; + goto block; + } + + error = ext2_extattr_check(EXT2_IFIRST(ihdr), eaend); + if (error) + goto out; + + olen = ext2_findextattr(ap->a_attrnamespace, ap->a_name, + (char *)EXT2_IFIRST(ihdr), eae, &eae, NULL); + if (olen >= 0) { + ext2_extattr_delete_entry((char *)EXT2_IFIRST(ihdr), + EXT2_IFIRST(ihdr), eae, eaend); + goto out; + } + + error = ENOATTR; + } + +block: + if (EXT2_EXTATTR_BLOCK(ip)) { + hdr = EXT2_EXTATTR_BLOCK_HDR(ip, eap); + easize = EXT2_EXTATTR_BLOCK_SIZE(ip); + eae = EXT2_IFIRST(hdr); + eaend = (char *)hdr + easize; + + if (hdr->h_magic != EXTATTR_MAGIC) { + error = EIO; + goto out; + } + + error = ext2_extattr_check(EXT2_IFIRST(hdr), eaend); + if (error) + goto out; + + olen = ext2_findextattr(ap->a_attrnamespace, ap->a_name, + (char *)hdr, eae, &eae, NULL); + if (olen >= 0) { + ext2_extattr_delete_entry((char *)hdr, + EXT2_IFIRST(hdr), eae, eaend); + goto out; + } + + error = ENOATTR; + } + +out: + if (error) { + free(eap, M_TEMP); + ext2_close_ea(ap->a_vp, 0, ap->a_cred, ap->a_td); + if (ip->i_ea_area != NULL && ip->i_ea_error == 0) + ip->i_ea_error = error; + return (error); + } + + tmp = ip->i_ea_area; + ip->i_ea_area = eap; + free(tmp, M_TEMP); + error = ext2_close_ea(ap->a_vp, 1, ap->a_cred, ap->a_td); + + return (error); +} + +/* + * Vnode operation to retrieve a named extended attribute. + */ +static int +ext2_getextattr(struct vop_getextattr_args *ap) +{ + struct inode *ip; + struct m_ext2fs *fs; + struct ext2fs_extattr_dinode_header *ihdr; + struct ext2fs_extattr_header *hdr; + struct ext2fs_extattr_entry *eae; + u_char *eap, *p; + unsigned easize; + int error, ealen; + char *eaend; + + ip = VTOI(ap->a_vp); + fs = ip->i_e2fs; + + if (ap->a_vp->v_type == VCHR || ap->a_vp->v_type == VBLK) + return (EOPNOTSUPP); + error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace, ap->a_cred, ap->a_td, VREAD); if (error) @@ -1512,17 +2066,73 @@ if (ap->a_size != NULL) *ap->a_size = 0; - if (EXT2_INODE_SIZE(fs) != E2FS_REV0_INODE_SIZE) { - error = ext2_extattr_inode_get(ip, ap->a_attrnamespace, - ap->a_name, ap->a_uio, ap->a_size); + error = ext2_open_ea(ap->a_vp, ap->a_cred, ap->a_td); + if (error) + return (error); + + if (EXT2_EXTATTR_INODE(ip)) { + ihdr = EXT2_EXTATTR_INODE_HDR(ip, ip->i_ea_area); + eap = (char *)ihdr; + easize = EXT2_EXTATTR_INODE_SIZE(ip); + eae = EXT2_IFIRST(ihdr); + eaend = (char *)ihdr + easize; + + if (ihdr->h_magic != EXTATTR_MAGIC) { + error = ENOATTR; + goto block; + } + + error = ext2_extattr_check(EXT2_IFIRST(ihdr), eaend); if (error) - return (error); + goto out; + + ealen = ext2_findextattr(ap->a_attrnamespace, ap->a_name, + (char *)eae, eae, NULL, &p); + + if (ealen >= 0) { + if (ap->a_size != NULL) + *ap->a_size += ealen; + else if (ap->a_uio != NULL) + error = uiomove(p, ealen, ap->a_uio); + + goto out; + } + error = ENOATTR; } - if (ip->i_facl) - error = ext2_extattr_block_get(ip, ap->a_attrnamespace, - ap->a_name, ap->a_uio, ap->a_size); +block: + if (EXT2_EXTATTR_BLOCK(ip)) { + hdr = EXT2_EXTATTR_BLOCK_HDR(ip, ip->i_ea_area); + eap = (char *)hdr; + easize = EXT2_EXTATTR_BLOCK_SIZE(ip); + eae = EXT2_IFIRST(hdr); + eaend = (char *)hdr + easize; + + if (hdr->h_magic != EXTATTR_MAGIC) { + error = EIO; + goto out; + } + + error = ext2_extattr_check(EXT2_IFIRST(hdr), eaend); + if (error) + goto out; + + ealen = ext2_findextattr(ap->a_attrnamespace, ap->a_name, + (char *)hdr, eae, NULL, &p); + if (ealen >= 0) { + if (ap->a_size != NULL) + *ap->a_size += ealen; + else if (ap->a_uio != NULL) { + error = uiomove(p, ealen, ap->a_uio); + if (error) + goto out; + } + } else + error = ENOATTR; + } +out: + ext2_close_ea(ap->a_vp, 0, ap->a_cred, ap->a_td); return (error); } @@ -1534,6 +2144,9 @@ { struct inode *ip; struct m_ext2fs *fs; + struct ext2fs_extattr_dinode_header *ihdr; + struct ext2fs_extattr_header *hdr; + struct ext2fs_extattr_entry *eae; int error; ip = VTOI(ap->a_vp); @@ -1553,16 +2166,268 @@ if (ap->a_size != NULL) *ap->a_size = 0; - if (EXT2_INODE_SIZE(fs) != E2FS_REV0_INODE_SIZE) { - error = ext2_extattr_inode_list(ip, ap->a_attrnamespace, - ap->a_uio, ap->a_size); - if(error) - return (error); + error = ext2_open_ea(ap->a_vp, ap->a_cred, ap->a_td); + if (error) + return (error); + + if (EXT2_EXTATTR_INODE(ip)) { + ihdr = EXT2_EXTATTR_INODE_HDR(ip, ip->i_ea_area); + if (ihdr->h_magic != EXTATTR_MAGIC) { + error = ENOATTR; + goto block; + } + + error = ext2_extattr_check(EXT2_IFIRST(ihdr), + (char *)ihdr + EXT2_EXTATTR_INODE_SIZE(ip)); + if (error) + goto out; + + eae = EXT2_IFIRST(ihdr); + while (!EXT2_IS_LAST_ENTRY(eae)) { + if (ext2_extattr_index_to_bsd(eae->e_name_index) != + ap->a_attrnamespace) + continue; + + if (ap->a_uio == NULL) + *ap->a_size += eae->e_name_len + 1; + else { + char *ea_name = malloc(eae->e_name_len + + 1, M_TEMP, M_WAITOK); + ea_name[0] = eae->e_name_len; + memcpy(&ea_name[1], eae->e_name, + eae->e_name_len); + error = uiomove(ea_name, eae->e_name_len + 1, + ap->a_uio); + free(ea_name, M_TEMP); + if (error) + goto out; + } + + eae = EXT2_EXTATTR_NEXT(eae); + } + } + +block: + if (EXT2_EXTATTR_BLOCK(ip)) { + hdr = EXT2_EXTATTR_BLOCK_HDR(ip, ip->i_ea_area); + if (hdr->h_magic != EXTATTR_MAGIC) { + error = EIO; + goto out; + } + + error = ext2_extattr_check(EXT2_IFIRST(hdr), + (char *)hdr + EXT2_EXTATTR_BLOCK_SIZE(ip)); + if (error) + goto out; + + eae = EXT2_IFIRST(hdr); + while (!EXT2_IS_LAST_ENTRY(eae)) { + if (ext2_extattr_index_to_bsd(eae->e_name_index) != + ap->a_attrnamespace) + continue; + + if (ap->a_uio == NULL) + *ap->a_size += eae->e_name_len + 1; + else { + char *ea_name = malloc(eae->e_name_len + + 1, M_TEMP, M_WAITOK); + ea_name[0] = eae->e_name_len; + memcpy(&ea_name[1], eae->e_name, + eae->e_name_len); + error = uiomove(ea_name, eae->e_name_len + 1, + ap->a_uio); + free(ea_name, M_TEMP); + if (error) + goto out; + } + + eae = EXT2_EXTATTR_NEXT(eae); + } + } + +out: + ext2_close_ea(ap->a_vp, 0, ap->a_cred, ap->a_td); + return (error); +} + +/* + * Vnode operation to set a named attribute. + */ +static int +ext2_setextattr(struct vop_setextattr_args *ap) +{ + struct inode *ip; + struct m_ext2fs *fs; + struct ext2fs_extattr_dinode_header *ihdr; + struct ext2fs_extattr_header *hdr; + struct ext2fs_extattr_entry *eae; + struct ext2fs_extattr_entry *ieae; + int olen, ealen, error, size, easize; + u_char *eap; + char *eaend; + void *tmp; + + ip = VTOI(ap->a_vp); + fs = ip->i_e2fs; + ieae = NULL; + + if (!EXT2_HAS_COMPAT_FEATURE(ip->i_e2fs, EXT2F_COMPAT_EXT_ATTR)) + return (EOPNOTSUPP); + + if (ap->a_vp->v_type == VCHR || ap->a_vp->v_type == VBLK) + return (EOPNOTSUPP); + + if (strlen(ap->a_name) == 0) + return (EINVAL); + + /* XXX Now unsupported API to delete EAs using NULL uio. */ + if (ap->a_uio == NULL) + return (EOPNOTSUPP); + + if (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY) + return (EROFS); + + ealen = ap->a_uio->uio_resid; + if (ealen < 0 || ealen > fs->e2fs_bsize) + return (EINVAL); + + error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace, + ap->a_cred, ap->a_td, VWRITE); + if (error) { + goto out; + } + + error = ext2_extattr_valid_attrname(ap->a_attrnamespace, ap->a_name); + if (error) + return (error); + + error = ext2_open_ea(ap->a_vp, ap->a_cred, ap->a_td); + if (error) + return (error); + + eap = malloc(ip->i_ea_len, M_TEMP, M_WAITOK); + bcopy(ip->i_ea_area, eap, ip->i_ea_len); + + if (EXT2_EXTATTR_INODE(ip)) { + ihdr = EXT2_EXTATTR_INODE_HDR(ip, eap); + easize = EXT2_EXTATTR_INODE_SIZE(ip); + eae = EXT2_IFIRST(ihdr); + eaend = (char *)ihdr + easize; + + if (ihdr->h_magic != EXTATTR_MAGIC) { + error = ENOATTR; + goto block; + } + + error = ext2_extattr_check(EXT2_IFIRST(ihdr), eaend); + if (error) + goto out; + + olen = ext2_findextattr(ap->a_attrnamespace, ap->a_name, + (char *)EXT2_IFIRST(ihdr), eae, &eae, NULL); + if (olen == -1) { + size = ext2_extattr_get_size(EXT2_IFIRST(ihdr), + NULL, sizeof(struct ext2fs_extattr_dinode_header), + strlen(ap->a_name), ap->a_uio->uio_resid); + if (size > EXT2_EXTATTR_INODE_SIZE(ip)) { + error = ENOSPC; + goto block; + } + + eae = ext2_extattr_set_new_entry((char *) + EXT2_IFIRST(ihdr), + EXT2_IFIRST(ihdr), ap->a_name, + ap->a_attrnamespace, eaend, ap->a_uio); + } else { + size = ext2_extattr_get_size(EXT2_IFIRST(ihdr), + eae, sizeof(struct ext2fs_extattr_dinode_header), + eae->e_name_len, ap->a_uio->uio_resid); + if (size > EXT2_EXTATTR_INODE_SIZE(ip)) { + ieae= eae; + error = ENOSPC; + goto block; + } + + ext2_extattr_set_exist_entry((char *)EXT2_IFIRST(ihdr), + EXT2_IFIRST(ihdr), eae, eaend, ap->a_uio); + } + + error = uiomove((char *)EXT2_IFIRST(ihdr) + eae->e_value_offs, + eae->e_value_size, ap->a_uio); + + goto out; + } + +block: + if (EXT2_EXTATTR_BLOCK(ip)) { + hdr = EXT2_EXTATTR_BLOCK_HDR(ip, eap); + easize = EXT2_EXTATTR_BLOCK_SIZE(ip); + eae = EXT2_IFIRST(hdr); + eaend = (char *)hdr + easize; + + if (hdr->h_magic != EXTATTR_MAGIC) { + error = EIO; + goto out; + } + + error = ext2_extattr_check(EXT2_IFIRST(hdr), eaend); + if (error) + goto out; + + olen = ext2_findextattr(ap->a_attrnamespace, ap->a_name, + (char *)hdr, eae, &eae, NULL); + if (olen == -1) { + size = ext2_extattr_get_size(EXT2_IFIRST(hdr), + NULL, sizeof(struct ext2fs_extattr_header), + strlen(ap->a_name), ap->a_uio->uio_resid); + if (size > EXT2_EXTATTR_BLOCK_SIZE(ip)) { + error = ENOSPC; + goto out; + } + + if (ieae) { + ext2_extattr_delete_entry((char *) + EXT2_IFIRST(ihdr), + EXT2_IFIRST(ihdr), ieae, + (char *)EXT2_IFIRST(ihdr) + + EXT2_EXTATTR_INODE_SIZE(ip)); + } + + eae = ext2_extattr_set_new_entry((char *)hdr, + EXT2_IFIRST(hdr), ap->a_name, + ap->a_attrnamespace, eaend, ap->a_uio); + } else { + size = ext2_extattr_get_size(EXT2_IFIRST(hdr), + eae, sizeof(struct ext2fs_extattr_header), + eae->e_name_len, ap->a_uio->uio_resid); + if (size > EXT2_EXTATTR_BLOCK_SIZE(ip)) { + error = ENOSPC; + goto out; + } + + ext2_extattr_set_exist_entry((char *)hdr, + EXT2_IFIRST(hdr), eae, eaend, ap->a_uio); + } + + error = uiomove((char *)hdr + eae->e_value_offs, + eae->e_value_size, ap->a_uio); + + ext2_extattr_rehash(hdr, eae); + } + +out: + if (error) { + free(eap, M_TEMP); + ext2_close_ea(ap->a_vp, 0, ap->a_cred, ap->a_td); + if (ip->i_ea_area != NULL && ip->i_ea_error == 0) + ip->i_ea_error = error; + return (error); } - if(ip->i_facl) - error = ext2_extattr_block_list(ip, ap->a_attrnamespace, - ap->a_uio, ap->a_size); + tmp = ip->i_ea_area; + ip->i_ea_area = eap; + free(tmp, M_TEMP); + error = ext2_close_ea(ap->a_vp, 1, ap->a_cred, ap->a_td); return (error); } Index: sys/fs/ext2fs/inode.h =================================================================== --- sys/fs/ext2fs/inode.h +++ sys/fs/ext2fs/inode.h @@ -89,6 +89,16 @@ uint32_t i_next_alloc_block; uint32_t i_next_alloc_goal; + /* + * Data for extended attribute modification. + */ + u_char *i_ea_area; /* Pointer to copy of EA area */ + unsigned i_ea_len; /* Length of i_ea_area */ + int i_ea_error; /* First errno in transaction */ + int i_ea_refs; /* Number of users of EA area */ + + uint16_t i_extra_isize; /* Extra size of the inode */ + /* Fields from struct dinode in UFS. */ uint16_t i_mode; /* IFMT, permissions; see below. */ int16_t i_nlink; /* File link count. */ @@ -153,6 +163,8 @@ #define IN_SPACECOUNTED 0x0080 /* Blocks to be freed in free count. */ #define IN_LAZYACCESS 0x0100 /* Process IN_ACCESS after the suspension finished */ +#define IN_EA_LOCKED 0x0200 +#define IN_EA_LOCKWAIT 0x0400 /* * These are translation flags for some attributes that Ext4