Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * linux/fs/hfsplus/xattr.c
0004  *
0005  * Vyacheslav Dubeyko <slava@dubeyko.com>
0006  *
0007  * Logic of processing extended attributes
0008  */
0009 
0010 #include "hfsplus_fs.h"
0011 #include <linux/nls.h>
0012 #include "xattr.h"
0013 
0014 static int hfsplus_removexattr(struct inode *inode, const char *name);
0015 
0016 const struct xattr_handler *hfsplus_xattr_handlers[] = {
0017     &hfsplus_xattr_osx_handler,
0018     &hfsplus_xattr_user_handler,
0019     &hfsplus_xattr_trusted_handler,
0020     &hfsplus_xattr_security_handler,
0021     NULL
0022 };
0023 
0024 static int strcmp_xattr_finder_info(const char *name)
0025 {
0026     if (name) {
0027         return strncmp(name, HFSPLUS_XATTR_FINDER_INFO_NAME,
0028                 sizeof(HFSPLUS_XATTR_FINDER_INFO_NAME));
0029     }
0030     return -1;
0031 }
0032 
0033 static int strcmp_xattr_acl(const char *name)
0034 {
0035     if (name) {
0036         return strncmp(name, HFSPLUS_XATTR_ACL_NAME,
0037                 sizeof(HFSPLUS_XATTR_ACL_NAME));
0038     }
0039     return -1;
0040 }
0041 
0042 static bool is_known_namespace(const char *name)
0043 {
0044     if (strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN) &&
0045         strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN) &&
0046         strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN) &&
0047         strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN))
0048         return false;
0049 
0050     return true;
0051 }
0052 
0053 static void hfsplus_init_header_node(struct inode *attr_file,
0054                     u32 clump_size,
0055                     char *buf, u16 node_size)
0056 {
0057     struct hfs_bnode_desc *desc;
0058     struct hfs_btree_header_rec *head;
0059     u16 offset;
0060     __be16 *rec_offsets;
0061     u32 hdr_node_map_rec_bits;
0062     char *bmp;
0063     u32 used_nodes;
0064     u32 used_bmp_bytes;
0065     u64 tmp;
0066 
0067     hfs_dbg(ATTR_MOD, "init_hdr_attr_file: clump %u, node_size %u\n",
0068         clump_size, node_size);
0069 
0070     /* The end of the node contains list of record offsets */
0071     rec_offsets = (__be16 *)(buf + node_size);
0072 
0073     desc = (struct hfs_bnode_desc *)buf;
0074     desc->type = HFS_NODE_HEADER;
0075     desc->num_recs = cpu_to_be16(HFSPLUS_BTREE_HDR_NODE_RECS_COUNT);
0076     offset = sizeof(struct hfs_bnode_desc);
0077     *--rec_offsets = cpu_to_be16(offset);
0078 
0079     head = (struct hfs_btree_header_rec *)(buf + offset);
0080     head->node_size = cpu_to_be16(node_size);
0081     tmp = i_size_read(attr_file);
0082     do_div(tmp, node_size);
0083     head->node_count = cpu_to_be32(tmp);
0084     head->free_nodes = cpu_to_be32(be32_to_cpu(head->node_count) - 1);
0085     head->clump_size = cpu_to_be32(clump_size);
0086     head->attributes |= cpu_to_be32(HFS_TREE_BIGKEYS | HFS_TREE_VARIDXKEYS);
0087     head->max_key_len = cpu_to_be16(HFSPLUS_ATTR_KEYLEN - sizeof(u16));
0088     offset += sizeof(struct hfs_btree_header_rec);
0089     *--rec_offsets = cpu_to_be16(offset);
0090     offset += HFSPLUS_BTREE_HDR_USER_BYTES;
0091     *--rec_offsets = cpu_to_be16(offset);
0092 
0093     hdr_node_map_rec_bits = 8 * (node_size - offset - (4 * sizeof(u16)));
0094     if (be32_to_cpu(head->node_count) > hdr_node_map_rec_bits) {
0095         u32 map_node_bits;
0096         u32 map_nodes;
0097 
0098         desc->next = cpu_to_be32(be32_to_cpu(head->leaf_tail) + 1);
0099         map_node_bits = 8 * (node_size - sizeof(struct hfs_bnode_desc) -
0100                     (2 * sizeof(u16)) - 2);
0101         map_nodes = (be32_to_cpu(head->node_count) -
0102                 hdr_node_map_rec_bits +
0103                 (map_node_bits - 1)) / map_node_bits;
0104         be32_add_cpu(&head->free_nodes, 0 - map_nodes);
0105     }
0106 
0107     bmp = buf + offset;
0108     used_nodes =
0109         be32_to_cpu(head->node_count) - be32_to_cpu(head->free_nodes);
0110     used_bmp_bytes = used_nodes / 8;
0111     if (used_bmp_bytes) {
0112         memset(bmp, 0xFF, used_bmp_bytes);
0113         bmp += used_bmp_bytes;
0114         used_nodes %= 8;
0115     }
0116     *bmp = ~(0xFF >> used_nodes);
0117     offset += hdr_node_map_rec_bits / 8;
0118     *--rec_offsets = cpu_to_be16(offset);
0119 }
0120 
0121 static int hfsplus_create_attributes_file(struct super_block *sb)
0122 {
0123     int err = 0;
0124     struct hfsplus_sb_info *sbi = HFSPLUS_SB(sb);
0125     struct inode *attr_file;
0126     struct hfsplus_inode_info *hip;
0127     u32 clump_size;
0128     u16 node_size = HFSPLUS_ATTR_TREE_NODE_SIZE;
0129     char *buf;
0130     int index, written;
0131     struct address_space *mapping;
0132     struct page *page;
0133     int old_state = HFSPLUS_EMPTY_ATTR_TREE;
0134 
0135     hfs_dbg(ATTR_MOD, "create_attr_file: ino %d\n", HFSPLUS_ATTR_CNID);
0136 
0137 check_attr_tree_state_again:
0138     switch (atomic_read(&sbi->attr_tree_state)) {
0139     case HFSPLUS_EMPTY_ATTR_TREE:
0140         if (old_state != atomic_cmpxchg(&sbi->attr_tree_state,
0141                         old_state,
0142                         HFSPLUS_CREATING_ATTR_TREE))
0143             goto check_attr_tree_state_again;
0144         break;
0145     case HFSPLUS_CREATING_ATTR_TREE:
0146         /*
0147          * This state means that another thread is in process
0148          * of AttributesFile creation. Theoretically, it is
0149          * possible to be here. But really __setxattr() method
0150          * first of all calls hfs_find_init() for lookup in
0151          * B-tree of CatalogFile. This method locks mutex of
0152          * CatalogFile's B-tree. As a result, if some thread
0153          * is inside AttributedFile creation operation then
0154          * another threads will be waiting unlocking of
0155          * CatalogFile's B-tree's mutex. However, if code will
0156          * change then we will return error code (-EAGAIN) from
0157          * here. Really, it means that first try to set of xattr
0158          * fails with error but second attempt will have success.
0159          */
0160         return -EAGAIN;
0161     case HFSPLUS_VALID_ATTR_TREE:
0162         return 0;
0163     case HFSPLUS_FAILED_ATTR_TREE:
0164         return -EOPNOTSUPP;
0165     default:
0166         BUG();
0167     }
0168 
0169     attr_file = hfsplus_iget(sb, HFSPLUS_ATTR_CNID);
0170     if (IS_ERR(attr_file)) {
0171         pr_err("failed to load attributes file\n");
0172         return PTR_ERR(attr_file);
0173     }
0174 
0175     BUG_ON(i_size_read(attr_file) != 0);
0176 
0177     hip = HFSPLUS_I(attr_file);
0178 
0179     clump_size = hfsplus_calc_btree_clump_size(sb->s_blocksize,
0180                             node_size,
0181                             sbi->sect_count,
0182                             HFSPLUS_ATTR_CNID);
0183 
0184     mutex_lock(&hip->extents_lock);
0185     hip->clump_blocks = clump_size >> sbi->alloc_blksz_shift;
0186     mutex_unlock(&hip->extents_lock);
0187 
0188     if (sbi->free_blocks <= (hip->clump_blocks << 1)) {
0189         err = -ENOSPC;
0190         goto end_attr_file_creation;
0191     }
0192 
0193     while (hip->alloc_blocks < hip->clump_blocks) {
0194         err = hfsplus_file_extend(attr_file, false);
0195         if (unlikely(err)) {
0196             pr_err("failed to extend attributes file\n");
0197             goto end_attr_file_creation;
0198         }
0199         hip->phys_size = attr_file->i_size =
0200             (loff_t)hip->alloc_blocks << sbi->alloc_blksz_shift;
0201         hip->fs_blocks = hip->alloc_blocks << sbi->fs_shift;
0202         inode_set_bytes(attr_file, attr_file->i_size);
0203     }
0204 
0205     buf = kzalloc(node_size, GFP_NOFS);
0206     if (!buf) {
0207         err = -ENOMEM;
0208         goto end_attr_file_creation;
0209     }
0210 
0211     hfsplus_init_header_node(attr_file, clump_size, buf, node_size);
0212 
0213     mapping = attr_file->i_mapping;
0214 
0215     index = 0;
0216     written = 0;
0217     for (; written < node_size; index++, written += PAGE_SIZE) {
0218         void *kaddr;
0219 
0220         page = read_mapping_page(mapping, index, NULL);
0221         if (IS_ERR(page)) {
0222             err = PTR_ERR(page);
0223             goto failed_header_node_init;
0224         }
0225 
0226         kaddr = kmap_atomic(page);
0227         memcpy(kaddr, buf + written,
0228             min_t(size_t, PAGE_SIZE, node_size - written));
0229         kunmap_atomic(kaddr);
0230 
0231         set_page_dirty(page);
0232         put_page(page);
0233     }
0234 
0235     hfsplus_mark_inode_dirty(attr_file, HFSPLUS_I_ATTR_DIRTY);
0236 
0237     sbi->attr_tree = hfs_btree_open(sb, HFSPLUS_ATTR_CNID);
0238     if (!sbi->attr_tree)
0239         pr_err("failed to load attributes file\n");
0240 
0241 failed_header_node_init:
0242     kfree(buf);
0243 
0244 end_attr_file_creation:
0245     iput(attr_file);
0246 
0247     if (!err)
0248         atomic_set(&sbi->attr_tree_state, HFSPLUS_VALID_ATTR_TREE);
0249     else if (err == -ENOSPC)
0250         atomic_set(&sbi->attr_tree_state, HFSPLUS_EMPTY_ATTR_TREE);
0251     else
0252         atomic_set(&sbi->attr_tree_state, HFSPLUS_FAILED_ATTR_TREE);
0253 
0254     return err;
0255 }
0256 
0257 int __hfsplus_setxattr(struct inode *inode, const char *name,
0258             const void *value, size_t size, int flags)
0259 {
0260     int err = 0;
0261     struct hfs_find_data cat_fd;
0262     hfsplus_cat_entry entry;
0263     u16 cat_entry_flags, cat_entry_type;
0264     u16 folder_finderinfo_len = sizeof(struct DInfo) +
0265                     sizeof(struct DXInfo);
0266     u16 file_finderinfo_len = sizeof(struct FInfo) +
0267                     sizeof(struct FXInfo);
0268 
0269     if ((!S_ISREG(inode->i_mode) &&
0270             !S_ISDIR(inode->i_mode)) ||
0271                 HFSPLUS_IS_RSRC(inode))
0272         return -EOPNOTSUPP;
0273 
0274     if (value == NULL)
0275         return hfsplus_removexattr(inode, name);
0276 
0277     err = hfs_find_init(HFSPLUS_SB(inode->i_sb)->cat_tree, &cat_fd);
0278     if (err) {
0279         pr_err("can't init xattr find struct\n");
0280         return err;
0281     }
0282 
0283     err = hfsplus_find_cat(inode->i_sb, inode->i_ino, &cat_fd);
0284     if (err) {
0285         pr_err("catalog searching failed\n");
0286         goto end_setxattr;
0287     }
0288 
0289     if (!strcmp_xattr_finder_info(name)) {
0290         if (flags & XATTR_CREATE) {
0291             pr_err("xattr exists yet\n");
0292             err = -EOPNOTSUPP;
0293             goto end_setxattr;
0294         }
0295         hfs_bnode_read(cat_fd.bnode, &entry, cat_fd.entryoffset,
0296                     sizeof(hfsplus_cat_entry));
0297         if (be16_to_cpu(entry.type) == HFSPLUS_FOLDER) {
0298             if (size == folder_finderinfo_len) {
0299                 memcpy(&entry.folder.info, value,
0300                         folder_finderinfo_len);
0301                 hfs_bnode_write(cat_fd.bnode, &entry,
0302                     cat_fd.entryoffset,
0303                     sizeof(struct hfsplus_cat_folder));
0304                 hfsplus_mark_inode_dirty(inode,
0305                         HFSPLUS_I_CAT_DIRTY);
0306             } else {
0307                 err = -ERANGE;
0308                 goto end_setxattr;
0309             }
0310         } else if (be16_to_cpu(entry.type) == HFSPLUS_FILE) {
0311             if (size == file_finderinfo_len) {
0312                 memcpy(&entry.file.info, value,
0313                         file_finderinfo_len);
0314                 hfs_bnode_write(cat_fd.bnode, &entry,
0315                     cat_fd.entryoffset,
0316                     sizeof(struct hfsplus_cat_file));
0317                 hfsplus_mark_inode_dirty(inode,
0318                         HFSPLUS_I_CAT_DIRTY);
0319             } else {
0320                 err = -ERANGE;
0321                 goto end_setxattr;
0322             }
0323         } else {
0324             err = -EOPNOTSUPP;
0325             goto end_setxattr;
0326         }
0327         goto end_setxattr;
0328     }
0329 
0330     if (!HFSPLUS_SB(inode->i_sb)->attr_tree) {
0331         err = hfsplus_create_attributes_file(inode->i_sb);
0332         if (unlikely(err))
0333             goto end_setxattr;
0334     }
0335 
0336     if (hfsplus_attr_exists(inode, name)) {
0337         if (flags & XATTR_CREATE) {
0338             pr_err("xattr exists yet\n");
0339             err = -EOPNOTSUPP;
0340             goto end_setxattr;
0341         }
0342         err = hfsplus_delete_attr(inode, name);
0343         if (err)
0344             goto end_setxattr;
0345         err = hfsplus_create_attr(inode, name, value, size);
0346         if (err)
0347             goto end_setxattr;
0348     } else {
0349         if (flags & XATTR_REPLACE) {
0350             pr_err("cannot replace xattr\n");
0351             err = -EOPNOTSUPP;
0352             goto end_setxattr;
0353         }
0354         err = hfsplus_create_attr(inode, name, value, size);
0355         if (err)
0356             goto end_setxattr;
0357     }
0358 
0359     cat_entry_type = hfs_bnode_read_u16(cat_fd.bnode, cat_fd.entryoffset);
0360     if (cat_entry_type == HFSPLUS_FOLDER) {
0361         cat_entry_flags = hfs_bnode_read_u16(cat_fd.bnode,
0362                     cat_fd.entryoffset +
0363                     offsetof(struct hfsplus_cat_folder, flags));
0364         cat_entry_flags |= HFSPLUS_XATTR_EXISTS;
0365         if (!strcmp_xattr_acl(name))
0366             cat_entry_flags |= HFSPLUS_ACL_EXISTS;
0367         hfs_bnode_write_u16(cat_fd.bnode, cat_fd.entryoffset +
0368                 offsetof(struct hfsplus_cat_folder, flags),
0369                 cat_entry_flags);
0370         hfsplus_mark_inode_dirty(inode, HFSPLUS_I_CAT_DIRTY);
0371     } else if (cat_entry_type == HFSPLUS_FILE) {
0372         cat_entry_flags = hfs_bnode_read_u16(cat_fd.bnode,
0373                     cat_fd.entryoffset +
0374                     offsetof(struct hfsplus_cat_file, flags));
0375         cat_entry_flags |= HFSPLUS_XATTR_EXISTS;
0376         if (!strcmp_xattr_acl(name))
0377             cat_entry_flags |= HFSPLUS_ACL_EXISTS;
0378         hfs_bnode_write_u16(cat_fd.bnode, cat_fd.entryoffset +
0379                     offsetof(struct hfsplus_cat_file, flags),
0380                     cat_entry_flags);
0381         hfsplus_mark_inode_dirty(inode, HFSPLUS_I_CAT_DIRTY);
0382     } else {
0383         pr_err("invalid catalog entry type\n");
0384         err = -EIO;
0385         goto end_setxattr;
0386     }
0387 
0388 end_setxattr:
0389     hfs_find_exit(&cat_fd);
0390     return err;
0391 }
0392 
0393 static int name_len(const char *xattr_name, int xattr_name_len)
0394 {
0395     int len = xattr_name_len + 1;
0396 
0397     if (!is_known_namespace(xattr_name))
0398         len += XATTR_MAC_OSX_PREFIX_LEN;
0399 
0400     return len;
0401 }
0402 
0403 static int copy_name(char *buffer, const char *xattr_name, int name_len)
0404 {
0405     int len = name_len;
0406     int offset = 0;
0407 
0408     if (!is_known_namespace(xattr_name)) {
0409         memcpy(buffer, XATTR_MAC_OSX_PREFIX, XATTR_MAC_OSX_PREFIX_LEN);
0410         offset += XATTR_MAC_OSX_PREFIX_LEN;
0411         len += XATTR_MAC_OSX_PREFIX_LEN;
0412     }
0413 
0414     strncpy(buffer + offset, xattr_name, name_len);
0415     memset(buffer + offset + name_len, 0, 1);
0416     len += 1;
0417 
0418     return len;
0419 }
0420 
0421 int hfsplus_setxattr(struct inode *inode, const char *name,
0422              const void *value, size_t size, int flags,
0423              const char *prefix, size_t prefixlen)
0424 {
0425     char *xattr_name;
0426     int res;
0427 
0428     xattr_name = kmalloc(NLS_MAX_CHARSET_SIZE * HFSPLUS_ATTR_MAX_STRLEN + 1,
0429         GFP_KERNEL);
0430     if (!xattr_name)
0431         return -ENOMEM;
0432     strcpy(xattr_name, prefix);
0433     strcpy(xattr_name + prefixlen, name);
0434     res = __hfsplus_setxattr(inode, xattr_name, value, size, flags);
0435     kfree(xattr_name);
0436     return res;
0437 }
0438 
0439 static ssize_t hfsplus_getxattr_finder_info(struct inode *inode,
0440                         void *value, size_t size)
0441 {
0442     ssize_t res = 0;
0443     struct hfs_find_data fd;
0444     u16 entry_type;
0445     u16 folder_rec_len = sizeof(struct DInfo) + sizeof(struct DXInfo);
0446     u16 file_rec_len = sizeof(struct FInfo) + sizeof(struct FXInfo);
0447     u16 record_len = max(folder_rec_len, file_rec_len);
0448     u8 folder_finder_info[sizeof(struct DInfo) + sizeof(struct DXInfo)];
0449     u8 file_finder_info[sizeof(struct FInfo) + sizeof(struct FXInfo)];
0450 
0451     if (size >= record_len) {
0452         res = hfs_find_init(HFSPLUS_SB(inode->i_sb)->cat_tree, &fd);
0453         if (res) {
0454             pr_err("can't init xattr find struct\n");
0455             return res;
0456         }
0457         res = hfsplus_find_cat(inode->i_sb, inode->i_ino, &fd);
0458         if (res)
0459             goto end_getxattr_finder_info;
0460         entry_type = hfs_bnode_read_u16(fd.bnode, fd.entryoffset);
0461 
0462         if (entry_type == HFSPLUS_FOLDER) {
0463             hfs_bnode_read(fd.bnode, folder_finder_info,
0464                 fd.entryoffset +
0465                 offsetof(struct hfsplus_cat_folder, user_info),
0466                 folder_rec_len);
0467             memcpy(value, folder_finder_info, folder_rec_len);
0468             res = folder_rec_len;
0469         } else if (entry_type == HFSPLUS_FILE) {
0470             hfs_bnode_read(fd.bnode, file_finder_info,
0471                 fd.entryoffset +
0472                 offsetof(struct hfsplus_cat_file, user_info),
0473                 file_rec_len);
0474             memcpy(value, file_finder_info, file_rec_len);
0475             res = file_rec_len;
0476         } else {
0477             res = -EOPNOTSUPP;
0478             goto end_getxattr_finder_info;
0479         }
0480     } else
0481         res = size ? -ERANGE : record_len;
0482 
0483 end_getxattr_finder_info:
0484     if (size >= record_len)
0485         hfs_find_exit(&fd);
0486     return res;
0487 }
0488 
0489 ssize_t __hfsplus_getxattr(struct inode *inode, const char *name,
0490              void *value, size_t size)
0491 {
0492     struct hfs_find_data fd;
0493     hfsplus_attr_entry *entry;
0494     __be32 xattr_record_type;
0495     u32 record_type;
0496     u16 record_length = 0;
0497     ssize_t res = 0;
0498 
0499     if ((!S_ISREG(inode->i_mode) &&
0500             !S_ISDIR(inode->i_mode)) ||
0501                 HFSPLUS_IS_RSRC(inode))
0502         return -EOPNOTSUPP;
0503 
0504     if (!strcmp_xattr_finder_info(name))
0505         return hfsplus_getxattr_finder_info(inode, value, size);
0506 
0507     if (!HFSPLUS_SB(inode->i_sb)->attr_tree)
0508         return -EOPNOTSUPP;
0509 
0510     entry = hfsplus_alloc_attr_entry();
0511     if (!entry) {
0512         pr_err("can't allocate xattr entry\n");
0513         return -ENOMEM;
0514     }
0515 
0516     res = hfs_find_init(HFSPLUS_SB(inode->i_sb)->attr_tree, &fd);
0517     if (res) {
0518         pr_err("can't init xattr find struct\n");
0519         goto failed_getxattr_init;
0520     }
0521 
0522     res = hfsplus_find_attr(inode->i_sb, inode->i_ino, name, &fd);
0523     if (res) {
0524         if (res == -ENOENT)
0525             res = -ENODATA;
0526         else
0527             pr_err("xattr searching failed\n");
0528         goto out;
0529     }
0530 
0531     hfs_bnode_read(fd.bnode, &xattr_record_type,
0532             fd.entryoffset, sizeof(xattr_record_type));
0533     record_type = be32_to_cpu(xattr_record_type);
0534     if (record_type == HFSPLUS_ATTR_INLINE_DATA) {
0535         record_length = hfs_bnode_read_u16(fd.bnode,
0536                 fd.entryoffset +
0537                 offsetof(struct hfsplus_attr_inline_data,
0538                 length));
0539         if (record_length > HFSPLUS_MAX_INLINE_DATA_SIZE) {
0540             pr_err("invalid xattr record size\n");
0541             res = -EIO;
0542             goto out;
0543         }
0544     } else if (record_type == HFSPLUS_ATTR_FORK_DATA ||
0545             record_type == HFSPLUS_ATTR_EXTENTS) {
0546         pr_err("only inline data xattr are supported\n");
0547         res = -EOPNOTSUPP;
0548         goto out;
0549     } else {
0550         pr_err("invalid xattr record\n");
0551         res = -EIO;
0552         goto out;
0553     }
0554 
0555     if (size) {
0556         hfs_bnode_read(fd.bnode, entry, fd.entryoffset,
0557                 offsetof(struct hfsplus_attr_inline_data,
0558                     raw_bytes) + record_length);
0559     }
0560 
0561     if (size >= record_length) {
0562         memcpy(value, entry->inline_data.raw_bytes, record_length);
0563         res = record_length;
0564     } else
0565         res = size ? -ERANGE : record_length;
0566 
0567 out:
0568     hfs_find_exit(&fd);
0569 
0570 failed_getxattr_init:
0571     hfsplus_destroy_attr_entry(entry);
0572     return res;
0573 }
0574 
0575 ssize_t hfsplus_getxattr(struct inode *inode, const char *name,
0576              void *value, size_t size,
0577              const char *prefix, size_t prefixlen)
0578 {
0579     int res;
0580     char *xattr_name;
0581 
0582     xattr_name = kmalloc(NLS_MAX_CHARSET_SIZE * HFSPLUS_ATTR_MAX_STRLEN + 1,
0583                  GFP_KERNEL);
0584     if (!xattr_name)
0585         return -ENOMEM;
0586 
0587     strcpy(xattr_name, prefix);
0588     strcpy(xattr_name + prefixlen, name);
0589 
0590     res = __hfsplus_getxattr(inode, xattr_name, value, size);
0591     kfree(xattr_name);
0592     return res;
0593 
0594 }
0595 
0596 static inline int can_list(const char *xattr_name)
0597 {
0598     if (!xattr_name)
0599         return 0;
0600 
0601     return strncmp(xattr_name, XATTR_TRUSTED_PREFIX,
0602             XATTR_TRUSTED_PREFIX_LEN) ||
0603                 capable(CAP_SYS_ADMIN);
0604 }
0605 
0606 static ssize_t hfsplus_listxattr_finder_info(struct dentry *dentry,
0607                         char *buffer, size_t size)
0608 {
0609     ssize_t res = 0;
0610     struct inode *inode = d_inode(dentry);
0611     struct hfs_find_data fd;
0612     u16 entry_type;
0613     u8 folder_finder_info[sizeof(struct DInfo) + sizeof(struct DXInfo)];
0614     u8 file_finder_info[sizeof(struct FInfo) + sizeof(struct FXInfo)];
0615     unsigned long len, found_bit;
0616     int xattr_name_len, symbols_count;
0617 
0618     res = hfs_find_init(HFSPLUS_SB(inode->i_sb)->cat_tree, &fd);
0619     if (res) {
0620         pr_err("can't init xattr find struct\n");
0621         return res;
0622     }
0623 
0624     res = hfsplus_find_cat(inode->i_sb, inode->i_ino, &fd);
0625     if (res)
0626         goto end_listxattr_finder_info;
0627 
0628     entry_type = hfs_bnode_read_u16(fd.bnode, fd.entryoffset);
0629     if (entry_type == HFSPLUS_FOLDER) {
0630         len = sizeof(struct DInfo) + sizeof(struct DXInfo);
0631         hfs_bnode_read(fd.bnode, folder_finder_info,
0632                 fd.entryoffset +
0633                 offsetof(struct hfsplus_cat_folder, user_info),
0634                 len);
0635         found_bit = find_first_bit((void *)folder_finder_info, len*8);
0636     } else if (entry_type == HFSPLUS_FILE) {
0637         len = sizeof(struct FInfo) + sizeof(struct FXInfo);
0638         hfs_bnode_read(fd.bnode, file_finder_info,
0639                 fd.entryoffset +
0640                 offsetof(struct hfsplus_cat_file, user_info),
0641                 len);
0642         found_bit = find_first_bit((void *)file_finder_info, len*8);
0643     } else {
0644         res = -EOPNOTSUPP;
0645         goto end_listxattr_finder_info;
0646     }
0647 
0648     if (found_bit >= (len*8))
0649         res = 0;
0650     else {
0651         symbols_count = sizeof(HFSPLUS_XATTR_FINDER_INFO_NAME) - 1;
0652         xattr_name_len =
0653             name_len(HFSPLUS_XATTR_FINDER_INFO_NAME, symbols_count);
0654         if (!buffer || !size) {
0655             if (can_list(HFSPLUS_XATTR_FINDER_INFO_NAME))
0656                 res = xattr_name_len;
0657         } else if (can_list(HFSPLUS_XATTR_FINDER_INFO_NAME)) {
0658             if (size < xattr_name_len)
0659                 res = -ERANGE;
0660             else {
0661                 res = copy_name(buffer,
0662                         HFSPLUS_XATTR_FINDER_INFO_NAME,
0663                         symbols_count);
0664             }
0665         }
0666     }
0667 
0668 end_listxattr_finder_info:
0669     hfs_find_exit(&fd);
0670 
0671     return res;
0672 }
0673 
0674 ssize_t hfsplus_listxattr(struct dentry *dentry, char *buffer, size_t size)
0675 {
0676     ssize_t err;
0677     ssize_t res = 0;
0678     struct inode *inode = d_inode(dentry);
0679     struct hfs_find_data fd;
0680     u16 key_len = 0;
0681     struct hfsplus_attr_key attr_key;
0682     char *strbuf;
0683     int xattr_name_len;
0684 
0685     if ((!S_ISREG(inode->i_mode) &&
0686             !S_ISDIR(inode->i_mode)) ||
0687                 HFSPLUS_IS_RSRC(inode))
0688         return -EOPNOTSUPP;
0689 
0690     res = hfsplus_listxattr_finder_info(dentry, buffer, size);
0691     if (res < 0)
0692         return res;
0693     else if (!HFSPLUS_SB(inode->i_sb)->attr_tree)
0694         return (res == 0) ? -EOPNOTSUPP : res;
0695 
0696     err = hfs_find_init(HFSPLUS_SB(inode->i_sb)->attr_tree, &fd);
0697     if (err) {
0698         pr_err("can't init xattr find struct\n");
0699         return err;
0700     }
0701 
0702     strbuf = kmalloc(NLS_MAX_CHARSET_SIZE * HFSPLUS_ATTR_MAX_STRLEN +
0703             XATTR_MAC_OSX_PREFIX_LEN + 1, GFP_KERNEL);
0704     if (!strbuf) {
0705         res = -ENOMEM;
0706         goto out;
0707     }
0708 
0709     err = hfsplus_find_attr(inode->i_sb, inode->i_ino, NULL, &fd);
0710     if (err) {
0711         if (err == -ENOENT) {
0712             if (res == 0)
0713                 res = -ENODATA;
0714             goto end_listxattr;
0715         } else {
0716             res = err;
0717             goto end_listxattr;
0718         }
0719     }
0720 
0721     for (;;) {
0722         key_len = hfs_bnode_read_u16(fd.bnode, fd.keyoffset);
0723         if (key_len == 0 || key_len > fd.tree->max_key_len) {
0724             pr_err("invalid xattr key length: %d\n", key_len);
0725             res = -EIO;
0726             goto end_listxattr;
0727         }
0728 
0729         hfs_bnode_read(fd.bnode, &attr_key,
0730                 fd.keyoffset, key_len + sizeof(key_len));
0731 
0732         if (be32_to_cpu(attr_key.cnid) != inode->i_ino)
0733             goto end_listxattr;
0734 
0735         xattr_name_len = NLS_MAX_CHARSET_SIZE * HFSPLUS_ATTR_MAX_STRLEN;
0736         if (hfsplus_uni2asc(inode->i_sb,
0737             (const struct hfsplus_unistr *)&fd.key->attr.key_name,
0738                     strbuf, &xattr_name_len)) {
0739             pr_err("unicode conversion failed\n");
0740             res = -EIO;
0741             goto end_listxattr;
0742         }
0743 
0744         if (!buffer || !size) {
0745             if (can_list(strbuf))
0746                 res += name_len(strbuf, xattr_name_len);
0747         } else if (can_list(strbuf)) {
0748             if (size < (res + name_len(strbuf, xattr_name_len))) {
0749                 res = -ERANGE;
0750                 goto end_listxattr;
0751             } else
0752                 res += copy_name(buffer + res,
0753                         strbuf, xattr_name_len);
0754         }
0755 
0756         if (hfs_brec_goto(&fd, 1))
0757             goto end_listxattr;
0758     }
0759 
0760 end_listxattr:
0761     kfree(strbuf);
0762 out:
0763     hfs_find_exit(&fd);
0764     return res;
0765 }
0766 
0767 static int hfsplus_removexattr(struct inode *inode, const char *name)
0768 {
0769     int err = 0;
0770     struct hfs_find_data cat_fd;
0771     u16 flags;
0772     u16 cat_entry_type;
0773     int is_xattr_acl_deleted = 0;
0774     int is_all_xattrs_deleted = 0;
0775 
0776     if (!HFSPLUS_SB(inode->i_sb)->attr_tree)
0777         return -EOPNOTSUPP;
0778 
0779     if (!strcmp_xattr_finder_info(name))
0780         return -EOPNOTSUPP;
0781 
0782     err = hfs_find_init(HFSPLUS_SB(inode->i_sb)->cat_tree, &cat_fd);
0783     if (err) {
0784         pr_err("can't init xattr find struct\n");
0785         return err;
0786     }
0787 
0788     err = hfsplus_find_cat(inode->i_sb, inode->i_ino, &cat_fd);
0789     if (err) {
0790         pr_err("catalog searching failed\n");
0791         goto end_removexattr;
0792     }
0793 
0794     err = hfsplus_delete_attr(inode, name);
0795     if (err)
0796         goto end_removexattr;
0797 
0798     is_xattr_acl_deleted = !strcmp_xattr_acl(name);
0799     is_all_xattrs_deleted = !hfsplus_attr_exists(inode, NULL);
0800 
0801     if (!is_xattr_acl_deleted && !is_all_xattrs_deleted)
0802         goto end_removexattr;
0803 
0804     cat_entry_type = hfs_bnode_read_u16(cat_fd.bnode, cat_fd.entryoffset);
0805 
0806     if (cat_entry_type == HFSPLUS_FOLDER) {
0807         flags = hfs_bnode_read_u16(cat_fd.bnode, cat_fd.entryoffset +
0808                 offsetof(struct hfsplus_cat_folder, flags));
0809         if (is_xattr_acl_deleted)
0810             flags &= ~HFSPLUS_ACL_EXISTS;
0811         if (is_all_xattrs_deleted)
0812             flags &= ~HFSPLUS_XATTR_EXISTS;
0813         hfs_bnode_write_u16(cat_fd.bnode, cat_fd.entryoffset +
0814                 offsetof(struct hfsplus_cat_folder, flags),
0815                 flags);
0816         hfsplus_mark_inode_dirty(inode, HFSPLUS_I_CAT_DIRTY);
0817     } else if (cat_entry_type == HFSPLUS_FILE) {
0818         flags = hfs_bnode_read_u16(cat_fd.bnode, cat_fd.entryoffset +
0819                 offsetof(struct hfsplus_cat_file, flags));
0820         if (is_xattr_acl_deleted)
0821             flags &= ~HFSPLUS_ACL_EXISTS;
0822         if (is_all_xattrs_deleted)
0823             flags &= ~HFSPLUS_XATTR_EXISTS;
0824         hfs_bnode_write_u16(cat_fd.bnode, cat_fd.entryoffset +
0825                 offsetof(struct hfsplus_cat_file, flags),
0826                 flags);
0827         hfsplus_mark_inode_dirty(inode, HFSPLUS_I_CAT_DIRTY);
0828     } else {
0829         pr_err("invalid catalog entry type\n");
0830         err = -EIO;
0831         goto end_removexattr;
0832     }
0833 
0834 end_removexattr:
0835     hfs_find_exit(&cat_fd);
0836     return err;
0837 }
0838 
0839 static int hfsplus_osx_getxattr(const struct xattr_handler *handler,
0840                 struct dentry *unused, struct inode *inode,
0841                 const char *name, void *buffer, size_t size)
0842 {
0843     /*
0844      * Don't allow retrieving properly prefixed attributes
0845      * by prepending them with "osx."
0846      */
0847     if (is_known_namespace(name))
0848         return -EOPNOTSUPP;
0849 
0850     /*
0851      * osx is the namespace we use to indicate an unprefixed
0852      * attribute on the filesystem (like the ones that OS X
0853      * creates), so we pass the name through unmodified (after
0854      * ensuring it doesn't conflict with another namespace).
0855      */
0856     return __hfsplus_getxattr(inode, name, buffer, size);
0857 }
0858 
0859 static int hfsplus_osx_setxattr(const struct xattr_handler *handler,
0860                 struct user_namespace *mnt_userns,
0861                 struct dentry *unused, struct inode *inode,
0862                 const char *name, const void *buffer,
0863                 size_t size, int flags)
0864 {
0865     /*
0866      * Don't allow setting properly prefixed attributes
0867      * by prepending them with "osx."
0868      */
0869     if (is_known_namespace(name))
0870         return -EOPNOTSUPP;
0871 
0872     /*
0873      * osx is the namespace we use to indicate an unprefixed
0874      * attribute on the filesystem (like the ones that OS X
0875      * creates), so we pass the name through unmodified (after
0876      * ensuring it doesn't conflict with another namespace).
0877      */
0878     return __hfsplus_setxattr(inode, name, buffer, size, flags);
0879 }
0880 
0881 const struct xattr_handler hfsplus_xattr_osx_handler = {
0882     .prefix = XATTR_MAC_OSX_PREFIX,
0883     .get    = hfsplus_osx_getxattr,
0884     .set    = hfsplus_osx_setxattr,
0885 };