attributes.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  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. hfs_dbg("value size %zu is too big\n", size);
  104. return HFSPLUS_INVALID_ATTR_RECORD;
  105. }
  106. entry->inline_data.length = cpu_to_be16(len);
  107. memcpy(entry->inline_data.raw_bytes, value, len);
  108. /*
  109. * Align len on two-byte boundary.
  110. * It needs to add pad byte if we have odd len.
  111. */
  112. len = round_up(len, 2);
  113. return offsetof(struct hfsplus_attr_inline_data, raw_bytes) +
  114. len;
  115. } else /* invalid input */
  116. memset(entry, 0, sizeof(*entry));
  117. return HFSPLUS_INVALID_ATTR_RECORD;
  118. }
  119. int hfsplus_find_attr(struct super_block *sb, u32 cnid,
  120. const char *name, struct hfs_find_data *fd)
  121. {
  122. int err = 0;
  123. hfs_dbg("name %s, cnid %d\n", name ? name : NULL, cnid);
  124. if (!HFSPLUS_SB(sb)->attr_tree) {
  125. pr_err("attributes file doesn't exist\n");
  126. return -EINVAL;
  127. }
  128. if (name) {
  129. err = hfsplus_attr_build_key(sb, fd->search_key, cnid, name);
  130. if (err)
  131. goto failed_find_attr;
  132. err = hfs_brec_find(fd, hfs_find_rec_by_key);
  133. if (err)
  134. goto failed_find_attr;
  135. } else {
  136. err = hfsplus_attr_build_key(sb, fd->search_key, cnid, NULL);
  137. if (err)
  138. goto failed_find_attr;
  139. err = hfs_brec_find(fd, hfs_find_1st_rec_by_cnid);
  140. if (err)
  141. goto failed_find_attr;
  142. }
  143. failed_find_attr:
  144. return err;
  145. }
  146. int hfsplus_attr_exists(struct inode *inode, const char *name)
  147. {
  148. int err = 0;
  149. struct super_block *sb = inode->i_sb;
  150. struct hfs_find_data fd;
  151. if (!HFSPLUS_SB(sb)->attr_tree)
  152. return 0;
  153. err = hfs_find_init(HFSPLUS_SB(sb)->attr_tree, &fd);
  154. if (err)
  155. return 0;
  156. err = hfsplus_find_attr(sb, inode->i_ino, name, &fd);
  157. if (err)
  158. goto attr_not_found;
  159. hfs_find_exit(&fd);
  160. return 1;
  161. attr_not_found:
  162. hfs_find_exit(&fd);
  163. return 0;
  164. }
  165. static
  166. int hfsplus_create_attr_nolock(struct inode *inode, const char *name,
  167. const void *value, size_t size,
  168. struct hfs_find_data *fd,
  169. hfsplus_attr_entry *entry_ptr)
  170. {
  171. struct super_block *sb = inode->i_sb;
  172. int entry_size;
  173. int err;
  174. hfs_dbg("name %s, ino %ld\n",
  175. name ? name : NULL, inode->i_ino);
  176. if (name) {
  177. err = hfsplus_attr_build_key(sb, fd->search_key,
  178. inode->i_ino, name);
  179. if (err)
  180. return err;
  181. } else
  182. return -EINVAL;
  183. /* Mac OS X supports only inline data attributes. */
  184. entry_size = hfsplus_attr_build_record(entry_ptr,
  185. HFSPLUS_ATTR_INLINE_DATA,
  186. inode->i_ino,
  187. value, size);
  188. if (entry_size == HFSPLUS_INVALID_ATTR_RECORD) {
  189. if (size > HFSPLUS_MAX_INLINE_DATA_SIZE)
  190. err = -E2BIG;
  191. else
  192. err = -EINVAL;
  193. hfs_dbg("unable to store value: err %d\n", err);
  194. return err;
  195. }
  196. err = hfs_brec_find(fd, hfs_find_rec_by_key);
  197. if (err != -ENOENT) {
  198. if (!err)
  199. err = -EEXIST;
  200. return err;
  201. }
  202. err = hfs_brec_insert(fd, entry_ptr, entry_size);
  203. if (err) {
  204. hfs_dbg("unable to store value: err %d\n", err);
  205. return err;
  206. }
  207. hfsplus_mark_inode_dirty(inode, HFSPLUS_I_ATTR_DIRTY);
  208. return 0;
  209. }
  210. int hfsplus_create_attr(struct inode *inode,
  211. const char *name,
  212. const void *value, size_t size)
  213. {
  214. struct super_block *sb = inode->i_sb;
  215. struct hfs_find_data fd;
  216. hfsplus_attr_entry *entry_ptr;
  217. int err;
  218. hfs_dbg("name %s, ino %ld\n",
  219. name ? name : NULL, inode->i_ino);
  220. if (!HFSPLUS_SB(sb)->attr_tree) {
  221. pr_err("attributes file doesn't exist\n");
  222. return -EINVAL;
  223. }
  224. entry_ptr = hfsplus_alloc_attr_entry();
  225. if (!entry_ptr)
  226. return -ENOMEM;
  227. err = hfs_find_init(HFSPLUS_SB(sb)->attr_tree, &fd);
  228. if (err)
  229. goto failed_init_create_attr;
  230. /* Fail early and avoid ENOSPC during the btree operation */
  231. err = hfs_bmap_reserve(fd.tree, fd.tree->depth + 1);
  232. if (err)
  233. goto failed_create_attr;
  234. err = hfsplus_create_attr_nolock(inode, name, value, size,
  235. &fd, entry_ptr);
  236. if (err)
  237. goto failed_create_attr;
  238. failed_create_attr:
  239. hfs_find_exit(&fd);
  240. failed_init_create_attr:
  241. hfsplus_destroy_attr_entry(entry_ptr);
  242. return err;
  243. }
  244. static int __hfsplus_delete_attr(struct inode *inode, u32 cnid,
  245. struct hfs_find_data *fd)
  246. {
  247. int err = 0;
  248. __be32 found_cnid, record_type;
  249. hfs_bnode_read(fd->bnode, &found_cnid,
  250. fd->keyoffset +
  251. offsetof(struct hfsplus_attr_key, cnid),
  252. sizeof(__be32));
  253. if (cnid != be32_to_cpu(found_cnid))
  254. return -ENOENT;
  255. hfs_bnode_read(fd->bnode, &record_type,
  256. fd->entryoffset, sizeof(record_type));
  257. switch (be32_to_cpu(record_type)) {
  258. case HFSPLUS_ATTR_INLINE_DATA:
  259. /* All is OK. Do nothing. */
  260. break;
  261. case HFSPLUS_ATTR_FORK_DATA:
  262. case HFSPLUS_ATTR_EXTENTS:
  263. pr_err("only inline data xattr are supported\n");
  264. return -EOPNOTSUPP;
  265. default:
  266. pr_err("invalid extended attribute record\n");
  267. return -ENOENT;
  268. }
  269. /* Avoid btree corruption */
  270. hfs_bnode_read(fd->bnode, fd->search_key,
  271. fd->keyoffset, fd->keylength);
  272. err = hfs_brec_remove(fd);
  273. if (err)
  274. return err;
  275. hfsplus_mark_inode_dirty(inode, HFSPLUS_I_ATTR_DIRTY);
  276. return err;
  277. }
  278. static
  279. int hfsplus_delete_attr_nolock(struct inode *inode, const char *name,
  280. struct hfs_find_data *fd)
  281. {
  282. struct super_block *sb = inode->i_sb;
  283. int err;
  284. hfs_dbg("name %s, ino %ld\n",
  285. name ? name : NULL, inode->i_ino);
  286. if (name) {
  287. err = hfsplus_attr_build_key(sb, fd->search_key,
  288. inode->i_ino, name);
  289. if (err)
  290. return err;
  291. } else {
  292. pr_err("invalid extended attribute name\n");
  293. return -EINVAL;
  294. }
  295. err = hfs_brec_find(fd, hfs_find_rec_by_key);
  296. if (err)
  297. return err;
  298. err = __hfsplus_delete_attr(inode, inode->i_ino, fd);
  299. if (err)
  300. return err;
  301. return 0;
  302. }
  303. int hfsplus_delete_attr(struct inode *inode, const char *name)
  304. {
  305. int err = 0;
  306. struct super_block *sb = inode->i_sb;
  307. struct hfs_find_data fd;
  308. hfs_dbg("name %s, ino %ld\n",
  309. name ? name : NULL, inode->i_ino);
  310. if (!HFSPLUS_SB(sb)->attr_tree) {
  311. pr_err("attributes file doesn't exist\n");
  312. return -EINVAL;
  313. }
  314. err = hfs_find_init(HFSPLUS_SB(sb)->attr_tree, &fd);
  315. if (err)
  316. return err;
  317. /* Fail early and avoid ENOSPC during the btree operation */
  318. err = hfs_bmap_reserve(fd.tree, fd.tree->depth);
  319. if (err)
  320. goto out;
  321. err = hfsplus_delete_attr_nolock(inode, name, &fd);
  322. if (err)
  323. goto out;
  324. out:
  325. hfs_find_exit(&fd);
  326. return err;
  327. }
  328. int hfsplus_delete_all_attrs(struct inode *dir, u32 cnid)
  329. {
  330. int err = 0;
  331. struct hfs_find_data fd;
  332. hfs_dbg("cnid %d\n", cnid);
  333. if (!HFSPLUS_SB(dir->i_sb)->attr_tree) {
  334. pr_err("attributes file doesn't exist\n");
  335. return -EINVAL;
  336. }
  337. err = hfs_find_init(HFSPLUS_SB(dir->i_sb)->attr_tree, &fd);
  338. if (err)
  339. return err;
  340. for (;;) {
  341. err = hfsplus_find_attr(dir->i_sb, cnid, NULL, &fd);
  342. if (err) {
  343. if (err != -ENOENT)
  344. pr_err("xattr search failed\n");
  345. goto end_delete_all;
  346. }
  347. err = __hfsplus_delete_attr(dir, cnid, &fd);
  348. if (err)
  349. goto end_delete_all;
  350. }
  351. end_delete_all:
  352. hfs_find_exit(&fd);
  353. return err;
  354. }
  355. int hfsplus_replace_attr(struct inode *inode,
  356. const char *name,
  357. const void *value, size_t size)
  358. {
  359. struct super_block *sb = inode->i_sb;
  360. struct hfs_find_data fd;
  361. hfsplus_attr_entry *entry_ptr;
  362. int err = 0;
  363. hfs_dbg("name %s, ino %ld\n",
  364. name ? name : NULL, inode->i_ino);
  365. if (!HFSPLUS_SB(sb)->attr_tree) {
  366. pr_err("attributes file doesn't exist\n");
  367. return -EINVAL;
  368. }
  369. entry_ptr = hfsplus_alloc_attr_entry();
  370. if (!entry_ptr)
  371. return -ENOMEM;
  372. err = hfs_find_init(HFSPLUS_SB(sb)->attr_tree, &fd);
  373. if (err)
  374. goto failed_init_replace_attr;
  375. /* Fail early and avoid ENOSPC during the btree operation */
  376. err = hfs_bmap_reserve(fd.tree, fd.tree->depth + 1);
  377. if (err)
  378. goto failed_replace_attr;
  379. err = hfsplus_delete_attr_nolock(inode, name, &fd);
  380. if (err)
  381. goto failed_replace_attr;
  382. err = hfsplus_create_attr_nolock(inode, name, value, size,
  383. &fd, entry_ptr);
  384. if (err)
  385. goto failed_replace_attr;
  386. failed_replace_attr:
  387. hfs_find_exit(&fd);
  388. failed_init_replace_attr:
  389. hfsplus_destroy_attr_entry(entry_ptr);
  390. return err;
  391. }