attributes.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * linux/fs/hfsplus/attributes.c
  4. *
  5. * Vyacheslav Dubeyko <[email protected]>
  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. /* Fail early and avoid ENOSPC during the btree operation */
  185. err = hfs_bmap_reserve(fd.tree, fd.tree->depth + 1);
  186. if (err)
  187. goto failed_create_attr;
  188. if (name) {
  189. err = hfsplus_attr_build_key(sb, fd.search_key,
  190. inode->i_ino, name);
  191. if (err)
  192. goto failed_create_attr;
  193. } else {
  194. err = -EINVAL;
  195. goto failed_create_attr;
  196. }
  197. /* Mac OS X supports only inline data attributes. */
  198. entry_size = hfsplus_attr_build_record(entry_ptr,
  199. HFSPLUS_ATTR_INLINE_DATA,
  200. inode->i_ino,
  201. value, size);
  202. if (entry_size == HFSPLUS_INVALID_ATTR_RECORD) {
  203. err = -EINVAL;
  204. goto failed_create_attr;
  205. }
  206. err = hfs_brec_find(&fd, hfs_find_rec_by_key);
  207. if (err != -ENOENT) {
  208. if (!err)
  209. err = -EEXIST;
  210. goto failed_create_attr;
  211. }
  212. err = hfs_brec_insert(&fd, entry_ptr, entry_size);
  213. if (err)
  214. goto failed_create_attr;
  215. hfsplus_mark_inode_dirty(inode, HFSPLUS_I_ATTR_DIRTY);
  216. failed_create_attr:
  217. hfs_find_exit(&fd);
  218. failed_init_create_attr:
  219. hfsplus_destroy_attr_entry(entry_ptr);
  220. return err;
  221. }
  222. static int __hfsplus_delete_attr(struct inode *inode, u32 cnid,
  223. struct hfs_find_data *fd)
  224. {
  225. int err = 0;
  226. __be32 found_cnid, record_type;
  227. hfs_bnode_read(fd->bnode, &found_cnid,
  228. fd->keyoffset +
  229. offsetof(struct hfsplus_attr_key, cnid),
  230. sizeof(__be32));
  231. if (cnid != be32_to_cpu(found_cnid))
  232. return -ENOENT;
  233. hfs_bnode_read(fd->bnode, &record_type,
  234. fd->entryoffset, sizeof(record_type));
  235. switch (be32_to_cpu(record_type)) {
  236. case HFSPLUS_ATTR_INLINE_DATA:
  237. /* All is OK. Do nothing. */
  238. break;
  239. case HFSPLUS_ATTR_FORK_DATA:
  240. case HFSPLUS_ATTR_EXTENTS:
  241. pr_err("only inline data xattr are supported\n");
  242. return -EOPNOTSUPP;
  243. default:
  244. pr_err("invalid extended attribute record\n");
  245. return -ENOENT;
  246. }
  247. /* Avoid btree corruption */
  248. hfs_bnode_read(fd->bnode, fd->search_key,
  249. fd->keyoffset, fd->keylength);
  250. err = hfs_brec_remove(fd);
  251. if (err)
  252. return err;
  253. hfsplus_mark_inode_dirty(inode, HFSPLUS_I_ATTR_DIRTY);
  254. return err;
  255. }
  256. int hfsplus_delete_attr(struct inode *inode, const char *name)
  257. {
  258. int err = 0;
  259. struct super_block *sb = inode->i_sb;
  260. struct hfs_find_data fd;
  261. hfs_dbg(ATTR_MOD, "delete_attr: %s,%ld\n",
  262. name ? name : NULL, inode->i_ino);
  263. if (!HFSPLUS_SB(sb)->attr_tree) {
  264. pr_err("attributes file doesn't exist\n");
  265. return -EINVAL;
  266. }
  267. err = hfs_find_init(HFSPLUS_SB(sb)->attr_tree, &fd);
  268. if (err)
  269. return err;
  270. /* Fail early and avoid ENOSPC during the btree operation */
  271. err = hfs_bmap_reserve(fd.tree, fd.tree->depth);
  272. if (err)
  273. goto out;
  274. if (name) {
  275. err = hfsplus_attr_build_key(sb, fd.search_key,
  276. inode->i_ino, name);
  277. if (err)
  278. goto out;
  279. } else {
  280. pr_err("invalid extended attribute name\n");
  281. err = -EINVAL;
  282. goto out;
  283. }
  284. err = hfs_brec_find(&fd, hfs_find_rec_by_key);
  285. if (err)
  286. goto out;
  287. err = __hfsplus_delete_attr(inode, inode->i_ino, &fd);
  288. if (err)
  289. goto out;
  290. out:
  291. hfs_find_exit(&fd);
  292. return err;
  293. }
  294. int hfsplus_delete_all_attrs(struct inode *dir, u32 cnid)
  295. {
  296. int err = 0;
  297. struct hfs_find_data fd;
  298. hfs_dbg(ATTR_MOD, "delete_all_attrs: %d\n", cnid);
  299. if (!HFSPLUS_SB(dir->i_sb)->attr_tree) {
  300. pr_err("attributes file doesn't exist\n");
  301. return -EINVAL;
  302. }
  303. err = hfs_find_init(HFSPLUS_SB(dir->i_sb)->attr_tree, &fd);
  304. if (err)
  305. return err;
  306. for (;;) {
  307. err = hfsplus_find_attr(dir->i_sb, cnid, NULL, &fd);
  308. if (err) {
  309. if (err != -ENOENT)
  310. pr_err("xattr search failed\n");
  311. goto end_delete_all;
  312. }
  313. err = __hfsplus_delete_attr(dir, cnid, &fd);
  314. if (err)
  315. goto end_delete_all;
  316. }
  317. end_delete_all:
  318. hfs_find_exit(&fd);
  319. return err;
  320. }