attributes.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * linux/fs/hfsplus/attributes.c
  4. *
  5. * Vyacheslav Dubeyko <slava@dubeyko.com>
  6. *
  7. * Handling of records in attributes tree
  8. */
  9. #include "hfsplus_fs.h"
  10. #include "hfsplus_raw.h"
  11. static struct kmem_cache *hfsplus_attr_tree_cachep;
  12. int __init hfsplus_create_attr_tree_cache(void)
  13. {
  14. if (hfsplus_attr_tree_cachep)
  15. return -EEXIST;
  16. hfsplus_attr_tree_cachep =
  17. kmem_cache_create("hfsplus_attr_cache",
  18. sizeof(hfsplus_attr_entry), 0,
  19. SLAB_HWCACHE_ALIGN, NULL);
  20. if (!hfsplus_attr_tree_cachep)
  21. return -ENOMEM;
  22. return 0;
  23. }
  24. void hfsplus_destroy_attr_tree_cache(void)
  25. {
  26. kmem_cache_destroy(hfsplus_attr_tree_cachep);
  27. }
  28. int hfsplus_attr_bin_cmp_key(const hfsplus_btree_key *k1,
  29. const hfsplus_btree_key *k2)
  30. {
  31. __be32 k1_cnid, k2_cnid;
  32. k1_cnid = k1->attr.cnid;
  33. k2_cnid = k2->attr.cnid;
  34. if (k1_cnid != k2_cnid)
  35. return be32_to_cpu(k1_cnid) < be32_to_cpu(k2_cnid) ? -1 : 1;
  36. return hfsplus_strcmp(
  37. (const struct hfsplus_unistr *)&k1->attr.key_name,
  38. (const struct hfsplus_unistr *)&k2->attr.key_name);
  39. }
  40. int hfsplus_attr_build_key(struct super_block *sb, hfsplus_btree_key *key,
  41. u32 cnid, const char *name)
  42. {
  43. int len;
  44. memset(key, 0, sizeof(struct hfsplus_attr_key));
  45. key->attr.cnid = cpu_to_be32(cnid);
  46. if (name) {
  47. int res = hfsplus_asc2uni(sb,
  48. (struct hfsplus_unistr *)&key->attr.key_name,
  49. HFSPLUS_ATTR_MAX_STRLEN, name, strlen(name));
  50. if (res)
  51. return res;
  52. len = be16_to_cpu(key->attr.key_name.length);
  53. } else {
  54. key->attr.key_name.length = 0;
  55. len = 0;
  56. }
  57. /* The length of the key, as stored in key_len field, does not include
  58. * the size of the key_len field itself.
  59. * So, offsetof(hfsplus_attr_key, key_name) is a trick because
  60. * it takes into consideration key_len field (__be16) of
  61. * hfsplus_attr_key structure instead of length field (__be16) of
  62. * hfsplus_attr_unistr structure.
  63. */
  64. key->key_len =
  65. cpu_to_be16(offsetof(struct hfsplus_attr_key, key_name) +
  66. 2 * len);
  67. return 0;
  68. }
  69. hfsplus_attr_entry *hfsplus_alloc_attr_entry(void)
  70. {
  71. return kmem_cache_alloc(hfsplus_attr_tree_cachep, GFP_KERNEL);
  72. }
  73. void hfsplus_destroy_attr_entry(hfsplus_attr_entry *entry)
  74. {
  75. if (entry)
  76. kmem_cache_free(hfsplus_attr_tree_cachep, entry);
  77. }
  78. #define HFSPLUS_INVALID_ATTR_RECORD -1
  79. static int hfsplus_attr_build_record(hfsplus_attr_entry *entry, int record_type,
  80. u32 cnid, const void *value, size_t size)
  81. {
  82. if (record_type == HFSPLUS_ATTR_FORK_DATA) {
  83. /*
  84. * Mac OS X supports only inline data attributes.
  85. * Do nothing
  86. */
  87. memset(entry, 0, sizeof(*entry));
  88. return sizeof(struct hfsplus_attr_fork_data);
  89. } else if (record_type == HFSPLUS_ATTR_EXTENTS) {
  90. /*
  91. * Mac OS X supports only inline data attributes.
  92. * Do nothing.
  93. */
  94. memset(entry, 0, sizeof(*entry));
  95. return sizeof(struct hfsplus_attr_extents);
  96. } else if (record_type == HFSPLUS_ATTR_INLINE_DATA) {
  97. u16 len;
  98. memset(entry, 0, sizeof(struct hfsplus_attr_inline_data));
  99. entry->inline_data.record_type = cpu_to_be32(record_type);
  100. if (size <= HFSPLUS_MAX_INLINE_DATA_SIZE)
  101. len = size;
  102. else
  103. return HFSPLUS_INVALID_ATTR_RECORD;
  104. entry->inline_data.length = cpu_to_be16(len);
  105. memcpy(entry->inline_data.raw_bytes, value, len);
  106. /*
  107. * Align len on two-byte boundary.
  108. * It needs to add pad byte if we have odd len.
  109. */
  110. len = round_up(len, 2);
  111. return offsetof(struct hfsplus_attr_inline_data, raw_bytes) +
  112. len;
  113. } else /* invalid input */
  114. memset(entry, 0, sizeof(*entry));
  115. return HFSPLUS_INVALID_ATTR_RECORD;
  116. }
  117. int hfsplus_find_attr(struct super_block *sb, u32 cnid,
  118. const char *name, struct hfs_find_data *fd)
  119. {
  120. int err = 0;
  121. hfs_dbg(ATTR_MOD, "find_attr: %s,%d\n", name ? name : NULL, cnid);
  122. if (!HFSPLUS_SB(sb)->attr_tree) {
  123. pr_err("attributes file doesn't exist\n");
  124. return -EINVAL;
  125. }
  126. if (name) {
  127. err = hfsplus_attr_build_key(sb, fd->search_key, cnid, name);
  128. if (err)
  129. goto failed_find_attr;
  130. err = hfs_brec_find(fd, hfs_find_rec_by_key);
  131. if (err)
  132. goto failed_find_attr;
  133. } else {
  134. err = hfsplus_attr_build_key(sb, fd->search_key, cnid, NULL);
  135. if (err)
  136. goto failed_find_attr;
  137. err = hfs_brec_find(fd, hfs_find_1st_rec_by_cnid);
  138. if (err)
  139. goto failed_find_attr;
  140. }
  141. failed_find_attr:
  142. return err;
  143. }
  144. int hfsplus_attr_exists(struct inode *inode, const char *name)
  145. {
  146. int err = 0;
  147. struct super_block *sb = inode->i_sb;
  148. struct hfs_find_data fd;
  149. if (!HFSPLUS_SB(sb)->attr_tree)
  150. return 0;
  151. err = hfs_find_init(HFSPLUS_SB(sb)->attr_tree, &fd);
  152. if (err)
  153. return 0;
  154. err = hfsplus_find_attr(sb, inode->i_ino, name, &fd);
  155. if (err)
  156. goto attr_not_found;
  157. hfs_find_exit(&fd);
  158. return 1;
  159. attr_not_found:
  160. hfs_find_exit(&fd);
  161. return 0;
  162. }
  163. int hfsplus_create_attr(struct inode *inode,
  164. const char *name,
  165. const void *value, size_t size)
  166. {
  167. struct super_block *sb = inode->i_sb;
  168. struct hfs_find_data fd;
  169. hfsplus_attr_entry *entry_ptr;
  170. int entry_size;
  171. int err;
  172. hfs_dbg(ATTR_MOD, "create_attr: %s,%ld\n",
  173. name ? name : NULL, inode->i_ino);
  174. if (!HFSPLUS_SB(sb)->attr_tree) {
  175. pr_err("attributes file doesn't exist\n");
  176. return -EINVAL;
  177. }
  178. entry_ptr = hfsplus_alloc_attr_entry();
  179. if (!entry_ptr)
  180. return -ENOMEM;
  181. err = hfs_find_init(HFSPLUS_SB(sb)->attr_tree, &fd);
  182. if (err)
  183. goto failed_init_create_attr;
  184. if (name) {
  185. err = hfsplus_attr_build_key(sb, fd.search_key,
  186. inode->i_ino, name);
  187. if (err)
  188. goto failed_create_attr;
  189. } else {
  190. err = -EINVAL;
  191. goto failed_create_attr;
  192. }
  193. /* Mac OS X supports only inline data attributes. */
  194. entry_size = hfsplus_attr_build_record(entry_ptr,
  195. HFSPLUS_ATTR_INLINE_DATA,
  196. inode->i_ino,
  197. value, size);
  198. if (entry_size == HFSPLUS_INVALID_ATTR_RECORD) {
  199. err = -EINVAL;
  200. goto failed_create_attr;
  201. }
  202. err = hfs_brec_find(&fd, hfs_find_rec_by_key);
  203. if (err != -ENOENT) {
  204. if (!err)
  205. err = -EEXIST;
  206. goto failed_create_attr;
  207. }
  208. err = hfs_brec_insert(&fd, entry_ptr, entry_size);
  209. if (err)
  210. goto failed_create_attr;
  211. hfsplus_mark_inode_dirty(inode, HFSPLUS_I_ATTR_DIRTY);
  212. failed_create_attr:
  213. hfs_find_exit(&fd);
  214. failed_init_create_attr:
  215. hfsplus_destroy_attr_entry(entry_ptr);
  216. return err;
  217. }
  218. static int __hfsplus_delete_attr(struct inode *inode, u32 cnid,
  219. struct hfs_find_data *fd)
  220. {
  221. int err = 0;
  222. __be32 found_cnid, record_type;
  223. hfs_bnode_read(fd->bnode, &found_cnid,
  224. fd->keyoffset +
  225. offsetof(struct hfsplus_attr_key, cnid),
  226. sizeof(__be32));
  227. if (cnid != be32_to_cpu(found_cnid))
  228. return -ENOENT;
  229. hfs_bnode_read(fd->bnode, &record_type,
  230. fd->entryoffset, sizeof(record_type));
  231. switch (be32_to_cpu(record_type)) {
  232. case HFSPLUS_ATTR_INLINE_DATA:
  233. /* All is OK. Do nothing. */
  234. break;
  235. case HFSPLUS_ATTR_FORK_DATA:
  236. case HFSPLUS_ATTR_EXTENTS:
  237. pr_err("only inline data xattr are supported\n");
  238. return -EOPNOTSUPP;
  239. default:
  240. pr_err("invalid extended attribute record\n");
  241. return -ENOENT;
  242. }
  243. err = hfs_brec_remove(fd);
  244. if (err)
  245. return err;
  246. hfsplus_mark_inode_dirty(inode, HFSPLUS_I_ATTR_DIRTY);
  247. return err;
  248. }
  249. int hfsplus_delete_attr(struct inode *inode, const char *name)
  250. {
  251. int err = 0;
  252. struct super_block *sb = inode->i_sb;
  253. struct hfs_find_data fd;
  254. hfs_dbg(ATTR_MOD, "delete_attr: %s,%ld\n",
  255. name ? name : NULL, inode->i_ino);
  256. if (!HFSPLUS_SB(sb)->attr_tree) {
  257. pr_err("attributes file doesn't exist\n");
  258. return -EINVAL;
  259. }
  260. err = hfs_find_init(HFSPLUS_SB(sb)->attr_tree, &fd);
  261. if (err)
  262. return err;
  263. if (name) {
  264. err = hfsplus_attr_build_key(sb, fd.search_key,
  265. inode->i_ino, name);
  266. if (err)
  267. goto out;
  268. } else {
  269. pr_err("invalid extended attribute name\n");
  270. err = -EINVAL;
  271. goto out;
  272. }
  273. err = hfs_brec_find(&fd, hfs_find_rec_by_key);
  274. if (err)
  275. goto out;
  276. err = __hfsplus_delete_attr(inode, inode->i_ino, &fd);
  277. if (err)
  278. goto out;
  279. out:
  280. hfs_find_exit(&fd);
  281. return err;
  282. }
  283. int hfsplus_delete_all_attrs(struct inode *dir, u32 cnid)
  284. {
  285. int err = 0;
  286. struct hfs_find_data fd;
  287. hfs_dbg(ATTR_MOD, "delete_all_attrs: %d\n", cnid);
  288. if (!HFSPLUS_SB(dir->i_sb)->attr_tree) {
  289. pr_err("attributes file doesn't exist\n");
  290. return -EINVAL;
  291. }
  292. err = hfs_find_init(HFSPLUS_SB(dir->i_sb)->attr_tree, &fd);
  293. if (err)
  294. return err;
  295. for (;;) {
  296. err = hfsplus_find_attr(dir->i_sb, cnid, NULL, &fd);
  297. if (err) {
  298. if (err != -ENOENT)
  299. pr_err("xattr search failed\n");
  300. goto end_delete_all;
  301. }
  302. err = __hfsplus_delete_attr(dir, cnid, &fd);
  303. if (err)
  304. goto end_delete_all;
  305. }
  306. end_delete_all:
  307. hfs_find_exit(&fd);
  308. return err;
  309. }