^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * linux/fs/hfs/attr.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * (C) 2003 Ardis Technologies <roman@ardistech.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Export hfs data via xattr
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/xattr.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include "hfs_fs.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include "btree.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) enum hfs_xattr_type {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) HFS_TYPE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) HFS_CREATOR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) static int __hfs_setxattr(struct inode *inode, enum hfs_xattr_type type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) const void *value, size_t size, int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) struct hfs_find_data fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) hfs_cat_rec rec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) struct hfs_cat_file *file;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) int res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) if (!S_ISREG(inode->i_mode) || HFS_IS_RSRC(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) res = hfs_find_init(HFS_SB(inode->i_sb)->cat_tree, &fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) if (res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) fd.search_key->cat = HFS_I(inode)->cat_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) res = hfs_brec_find(&fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) if (res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) hfs_bnode_read(fd.bnode, &rec, fd.entryoffset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) sizeof(struct hfs_cat_file));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) file = &rec.file;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) switch (type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) case HFS_TYPE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) if (size == 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) memcpy(&file->UsrWds.fdType, value, 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) res = -ERANGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) case HFS_CREATOR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) if (size == 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) memcpy(&file->UsrWds.fdCreator, value, 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) res = -ERANGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) if (!res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) hfs_bnode_write(fd.bnode, &rec, fd.entryoffset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) sizeof(struct hfs_cat_file));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) hfs_find_exit(&fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) static ssize_t __hfs_getxattr(struct inode *inode, enum hfs_xattr_type type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) void *value, size_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) struct hfs_find_data fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) hfs_cat_rec rec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) struct hfs_cat_file *file;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) ssize_t res = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) if (!S_ISREG(inode->i_mode) || HFS_IS_RSRC(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) if (size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) res = hfs_find_init(HFS_SB(inode->i_sb)->cat_tree, &fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) if (res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) fd.search_key->cat = HFS_I(inode)->cat_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) res = hfs_brec_find(&fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) if (res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) hfs_bnode_read(fd.bnode, &rec, fd.entryoffset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) sizeof(struct hfs_cat_file));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) file = &rec.file;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) switch (type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) case HFS_TYPE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) if (size >= 4) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) memcpy(value, &file->UsrWds.fdType, 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) res = 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) res = size ? -ERANGE : 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) case HFS_CREATOR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) if (size >= 4) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) memcpy(value, &file->UsrWds.fdCreator, 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) res = 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) res = size ? -ERANGE : 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) if (size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) hfs_find_exit(&fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) static int hfs_xattr_get(const struct xattr_handler *handler,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) struct dentry *unused, struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) const char *name, void *value, size_t size, int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) return __hfs_getxattr(inode, handler->flags, value, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) static int hfs_xattr_set(const struct xattr_handler *handler,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) struct dentry *unused, struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) const char *name, const void *value, size_t size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) if (!value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) return __hfs_setxattr(inode, handler->flags, value, size, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) static const struct xattr_handler hfs_creator_handler = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) .name = "hfs.creator",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) .flags = HFS_CREATOR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) .get = hfs_xattr_get,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) .set = hfs_xattr_set,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) static const struct xattr_handler hfs_type_handler = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) .name = "hfs.type",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) .flags = HFS_TYPE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) .get = hfs_xattr_get,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) .set = hfs_xattr_set,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) const struct xattr_handler *hfs_xattr_handlers[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) &hfs_creator_handler,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) &hfs_type_handler,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) };