attributes.c 8.2 KB

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