xattr.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902
  1. /*
  2. * linux/fs/hfsplus/xattr.c
  3. *
  4. * Vyacheslav Dubeyko <slava@dubeyko.com>
  5. *
  6. * Logic of processing extended attributes
  7. */
  8. #include "hfsplus_fs.h"
  9. #include <linux/posix_acl_xattr.h>
  10. #include <linux/nls.h>
  11. #include "xattr.h"
  12. #include "acl.h"
  13. static int hfsplus_removexattr(struct inode *inode, const char *name);
  14. const struct xattr_handler *hfsplus_xattr_handlers[] = {
  15. &hfsplus_xattr_osx_handler,
  16. &hfsplus_xattr_user_handler,
  17. &hfsplus_xattr_trusted_handler,
  18. #ifdef CONFIG_HFSPLUS_FS_POSIX_ACL
  19. &posix_acl_access_xattr_handler,
  20. &posix_acl_default_xattr_handler,
  21. #endif
  22. &hfsplus_xattr_security_handler,
  23. NULL
  24. };
  25. static int strcmp_xattr_finder_info(const char *name)
  26. {
  27. if (name) {
  28. return strncmp(name, HFSPLUS_XATTR_FINDER_INFO_NAME,
  29. sizeof(HFSPLUS_XATTR_FINDER_INFO_NAME));
  30. }
  31. return -1;
  32. }
  33. static int strcmp_xattr_acl(const char *name)
  34. {
  35. if (name) {
  36. return strncmp(name, HFSPLUS_XATTR_ACL_NAME,
  37. sizeof(HFSPLUS_XATTR_ACL_NAME));
  38. }
  39. return -1;
  40. }
  41. static bool is_known_namespace(const char *name)
  42. {
  43. if (strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN) &&
  44. strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN) &&
  45. strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN) &&
  46. strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN))
  47. return false;
  48. return true;
  49. }
  50. static void hfsplus_init_header_node(struct inode *attr_file,
  51. u32 clump_size,
  52. char *buf, u16 node_size)
  53. {
  54. struct hfs_bnode_desc *desc;
  55. struct hfs_btree_header_rec *head;
  56. u16 offset;
  57. __be16 *rec_offsets;
  58. u32 hdr_node_map_rec_bits;
  59. char *bmp;
  60. u32 used_nodes;
  61. u32 used_bmp_bytes;
  62. u64 tmp;
  63. hfs_dbg(ATTR_MOD, "init_hdr_attr_file: clump %u, node_size %u\n",
  64. clump_size, node_size);
  65. /* The end of the node contains list of record offsets */
  66. rec_offsets = (__be16 *)(buf + node_size);
  67. desc = (struct hfs_bnode_desc *)buf;
  68. desc->type = HFS_NODE_HEADER;
  69. desc->num_recs = cpu_to_be16(HFSPLUS_BTREE_HDR_NODE_RECS_COUNT);
  70. offset = sizeof(struct hfs_bnode_desc);
  71. *--rec_offsets = cpu_to_be16(offset);
  72. head = (struct hfs_btree_header_rec *)(buf + offset);
  73. head->node_size = cpu_to_be16(node_size);
  74. tmp = i_size_read(attr_file);
  75. do_div(tmp, node_size);
  76. head->node_count = cpu_to_be32(tmp);
  77. head->free_nodes = cpu_to_be32(be32_to_cpu(head->node_count) - 1);
  78. head->clump_size = cpu_to_be32(clump_size);
  79. head->attributes |= cpu_to_be32(HFS_TREE_BIGKEYS | HFS_TREE_VARIDXKEYS);
  80. head->max_key_len = cpu_to_be16(HFSPLUS_ATTR_KEYLEN - sizeof(u16));
  81. offset += sizeof(struct hfs_btree_header_rec);
  82. *--rec_offsets = cpu_to_be16(offset);
  83. offset += HFSPLUS_BTREE_HDR_USER_BYTES;
  84. *--rec_offsets = cpu_to_be16(offset);
  85. hdr_node_map_rec_bits = 8 * (node_size - offset - (4 * sizeof(u16)));
  86. if (be32_to_cpu(head->node_count) > hdr_node_map_rec_bits) {
  87. u32 map_node_bits;
  88. u32 map_nodes;
  89. desc->next = cpu_to_be32(be32_to_cpu(head->leaf_tail) + 1);
  90. map_node_bits = 8 * (node_size - sizeof(struct hfs_bnode_desc) -
  91. (2 * sizeof(u16)) - 2);
  92. map_nodes = (be32_to_cpu(head->node_count) -
  93. hdr_node_map_rec_bits +
  94. (map_node_bits - 1)) / map_node_bits;
  95. be32_add_cpu(&head->free_nodes, 0 - map_nodes);
  96. }
  97. bmp = buf + offset;
  98. used_nodes =
  99. be32_to_cpu(head->node_count) - be32_to_cpu(head->free_nodes);
  100. used_bmp_bytes = used_nodes / 8;
  101. if (used_bmp_bytes) {
  102. memset(bmp, 0xFF, used_bmp_bytes);
  103. bmp += used_bmp_bytes;
  104. used_nodes %= 8;
  105. }
  106. *bmp = ~(0xFF >> used_nodes);
  107. offset += hdr_node_map_rec_bits / 8;
  108. *--rec_offsets = cpu_to_be16(offset);
  109. }
  110. static int hfsplus_create_attributes_file(struct super_block *sb)
  111. {
  112. int err = 0;
  113. struct hfsplus_sb_info *sbi = HFSPLUS_SB(sb);
  114. struct inode *attr_file;
  115. struct hfsplus_inode_info *hip;
  116. u32 clump_size;
  117. u16 node_size = HFSPLUS_ATTR_TREE_NODE_SIZE;
  118. char *buf;
  119. int index, written;
  120. struct address_space *mapping;
  121. struct page *page;
  122. int old_state = HFSPLUS_EMPTY_ATTR_TREE;
  123. hfs_dbg(ATTR_MOD, "create_attr_file: ino %d\n", HFSPLUS_ATTR_CNID);
  124. check_attr_tree_state_again:
  125. switch (atomic_read(&sbi->attr_tree_state)) {
  126. case HFSPLUS_EMPTY_ATTR_TREE:
  127. if (old_state != atomic_cmpxchg(&sbi->attr_tree_state,
  128. old_state,
  129. HFSPLUS_CREATING_ATTR_TREE))
  130. goto check_attr_tree_state_again;
  131. break;
  132. case HFSPLUS_CREATING_ATTR_TREE:
  133. /*
  134. * This state means that another thread is in process
  135. * of AttributesFile creation. Theoretically, it is
  136. * possible to be here. But really __setxattr() method
  137. * first of all calls hfs_find_init() for lookup in
  138. * B-tree of CatalogFile. This method locks mutex of
  139. * CatalogFile's B-tree. As a result, if some thread
  140. * is inside AttributedFile creation operation then
  141. * another threads will be waiting unlocking of
  142. * CatalogFile's B-tree's mutex. However, if code will
  143. * change then we will return error code (-EAGAIN) from
  144. * here. Really, it means that first try to set of xattr
  145. * fails with error but second attempt will have success.
  146. */
  147. return -EAGAIN;
  148. case HFSPLUS_VALID_ATTR_TREE:
  149. return 0;
  150. case HFSPLUS_FAILED_ATTR_TREE:
  151. return -EOPNOTSUPP;
  152. default:
  153. BUG();
  154. }
  155. attr_file = hfsplus_iget(sb, HFSPLUS_ATTR_CNID);
  156. if (IS_ERR(attr_file)) {
  157. pr_err("failed to load attributes file\n");
  158. return PTR_ERR(attr_file);
  159. }
  160. BUG_ON(i_size_read(attr_file) != 0);
  161. hip = HFSPLUS_I(attr_file);
  162. clump_size = hfsplus_calc_btree_clump_size(sb->s_blocksize,
  163. node_size,
  164. sbi->sect_count,
  165. HFSPLUS_ATTR_CNID);
  166. mutex_lock(&hip->extents_lock);
  167. hip->clump_blocks = clump_size >> sbi->alloc_blksz_shift;
  168. mutex_unlock(&hip->extents_lock);
  169. if (sbi->free_blocks <= (hip->clump_blocks << 1)) {
  170. err = -ENOSPC;
  171. goto end_attr_file_creation;
  172. }
  173. while (hip->alloc_blocks < hip->clump_blocks) {
  174. err = hfsplus_file_extend(attr_file, false);
  175. if (unlikely(err)) {
  176. pr_err("failed to extend attributes file\n");
  177. goto end_attr_file_creation;
  178. }
  179. hip->phys_size = attr_file->i_size =
  180. (loff_t)hip->alloc_blocks << sbi->alloc_blksz_shift;
  181. hip->fs_blocks = hip->alloc_blocks << sbi->fs_shift;
  182. inode_set_bytes(attr_file, attr_file->i_size);
  183. }
  184. buf = kzalloc(node_size, GFP_NOFS);
  185. if (!buf) {
  186. pr_err("failed to allocate memory for header node\n");
  187. err = -ENOMEM;
  188. goto end_attr_file_creation;
  189. }
  190. hfsplus_init_header_node(attr_file, clump_size, buf, node_size);
  191. mapping = attr_file->i_mapping;
  192. index = 0;
  193. written = 0;
  194. for (; written < node_size; index++, written += PAGE_CACHE_SIZE) {
  195. void *kaddr;
  196. page = read_mapping_page(mapping, index, NULL);
  197. if (IS_ERR(page)) {
  198. err = PTR_ERR(page);
  199. goto failed_header_node_init;
  200. }
  201. kaddr = kmap_atomic(page);
  202. memcpy(kaddr, buf + written,
  203. min_t(size_t, PAGE_CACHE_SIZE, node_size - written));
  204. kunmap_atomic(kaddr);
  205. set_page_dirty(page);
  206. page_cache_release(page);
  207. }
  208. hfsplus_mark_inode_dirty(attr_file, HFSPLUS_I_ATTR_DIRTY);
  209. sbi->attr_tree = hfs_btree_open(sb, HFSPLUS_ATTR_CNID);
  210. if (!sbi->attr_tree)
  211. pr_err("failed to load attributes file\n");
  212. failed_header_node_init:
  213. kfree(buf);
  214. end_attr_file_creation:
  215. iput(attr_file);
  216. if (!err)
  217. atomic_set(&sbi->attr_tree_state, HFSPLUS_VALID_ATTR_TREE);
  218. else if (err == -ENOSPC)
  219. atomic_set(&sbi->attr_tree_state, HFSPLUS_EMPTY_ATTR_TREE);
  220. else
  221. atomic_set(&sbi->attr_tree_state, HFSPLUS_FAILED_ATTR_TREE);
  222. return err;
  223. }
  224. int __hfsplus_setxattr(struct inode *inode, const char *name,
  225. const void *value, size_t size, int flags)
  226. {
  227. int err = 0;
  228. struct hfs_find_data cat_fd;
  229. hfsplus_cat_entry entry;
  230. u16 cat_entry_flags, cat_entry_type;
  231. u16 folder_finderinfo_len = sizeof(struct DInfo) +
  232. sizeof(struct DXInfo);
  233. u16 file_finderinfo_len = sizeof(struct FInfo) +
  234. sizeof(struct FXInfo);
  235. if ((!S_ISREG(inode->i_mode) &&
  236. !S_ISDIR(inode->i_mode)) ||
  237. HFSPLUS_IS_RSRC(inode))
  238. return -EOPNOTSUPP;
  239. if (value == NULL)
  240. return hfsplus_removexattr(inode, name);
  241. err = hfs_find_init(HFSPLUS_SB(inode->i_sb)->cat_tree, &cat_fd);
  242. if (err) {
  243. pr_err("can't init xattr find struct\n");
  244. return err;
  245. }
  246. err = hfsplus_find_cat(inode->i_sb, inode->i_ino, &cat_fd);
  247. if (err) {
  248. pr_err("catalog searching failed\n");
  249. goto end_setxattr;
  250. }
  251. if (!strcmp_xattr_finder_info(name)) {
  252. if (flags & XATTR_CREATE) {
  253. pr_err("xattr exists yet\n");
  254. err = -EOPNOTSUPP;
  255. goto end_setxattr;
  256. }
  257. hfs_bnode_read(cat_fd.bnode, &entry, cat_fd.entryoffset,
  258. sizeof(hfsplus_cat_entry));
  259. if (be16_to_cpu(entry.type) == HFSPLUS_FOLDER) {
  260. if (size == folder_finderinfo_len) {
  261. memcpy(&entry.folder.user_info, value,
  262. folder_finderinfo_len);
  263. hfs_bnode_write(cat_fd.bnode, &entry,
  264. cat_fd.entryoffset,
  265. sizeof(struct hfsplus_cat_folder));
  266. hfsplus_mark_inode_dirty(inode,
  267. HFSPLUS_I_CAT_DIRTY);
  268. } else {
  269. err = -ERANGE;
  270. goto end_setxattr;
  271. }
  272. } else if (be16_to_cpu(entry.type) == HFSPLUS_FILE) {
  273. if (size == file_finderinfo_len) {
  274. memcpy(&entry.file.user_info, value,
  275. file_finderinfo_len);
  276. hfs_bnode_write(cat_fd.bnode, &entry,
  277. cat_fd.entryoffset,
  278. sizeof(struct hfsplus_cat_file));
  279. hfsplus_mark_inode_dirty(inode,
  280. HFSPLUS_I_CAT_DIRTY);
  281. } else {
  282. err = -ERANGE;
  283. goto end_setxattr;
  284. }
  285. } else {
  286. err = -EOPNOTSUPP;
  287. goto end_setxattr;
  288. }
  289. goto end_setxattr;
  290. }
  291. if (!HFSPLUS_SB(inode->i_sb)->attr_tree) {
  292. err = hfsplus_create_attributes_file(inode->i_sb);
  293. if (unlikely(err))
  294. goto end_setxattr;
  295. }
  296. if (hfsplus_attr_exists(inode, name)) {
  297. if (flags & XATTR_CREATE) {
  298. pr_err("xattr exists yet\n");
  299. err = -EOPNOTSUPP;
  300. goto end_setxattr;
  301. }
  302. err = hfsplus_delete_attr(inode, name);
  303. if (err)
  304. goto end_setxattr;
  305. err = hfsplus_create_attr(inode, name, value, size);
  306. if (err)
  307. goto end_setxattr;
  308. } else {
  309. if (flags & XATTR_REPLACE) {
  310. pr_err("cannot replace xattr\n");
  311. err = -EOPNOTSUPP;
  312. goto end_setxattr;
  313. }
  314. err = hfsplus_create_attr(inode, name, value, size);
  315. if (err)
  316. goto end_setxattr;
  317. }
  318. cat_entry_type = hfs_bnode_read_u16(cat_fd.bnode, cat_fd.entryoffset);
  319. if (cat_entry_type == HFSPLUS_FOLDER) {
  320. cat_entry_flags = hfs_bnode_read_u16(cat_fd.bnode,
  321. cat_fd.entryoffset +
  322. offsetof(struct hfsplus_cat_folder, flags));
  323. cat_entry_flags |= HFSPLUS_XATTR_EXISTS;
  324. if (!strcmp_xattr_acl(name))
  325. cat_entry_flags |= HFSPLUS_ACL_EXISTS;
  326. hfs_bnode_write_u16(cat_fd.bnode, cat_fd.entryoffset +
  327. offsetof(struct hfsplus_cat_folder, flags),
  328. cat_entry_flags);
  329. hfsplus_mark_inode_dirty(inode, HFSPLUS_I_CAT_DIRTY);
  330. } else if (cat_entry_type == HFSPLUS_FILE) {
  331. cat_entry_flags = hfs_bnode_read_u16(cat_fd.bnode,
  332. cat_fd.entryoffset +
  333. offsetof(struct hfsplus_cat_file, flags));
  334. cat_entry_flags |= HFSPLUS_XATTR_EXISTS;
  335. if (!strcmp_xattr_acl(name))
  336. cat_entry_flags |= HFSPLUS_ACL_EXISTS;
  337. hfs_bnode_write_u16(cat_fd.bnode, cat_fd.entryoffset +
  338. offsetof(struct hfsplus_cat_file, flags),
  339. cat_entry_flags);
  340. hfsplus_mark_inode_dirty(inode, HFSPLUS_I_CAT_DIRTY);
  341. } else {
  342. pr_err("invalid catalog entry type\n");
  343. err = -EIO;
  344. goto end_setxattr;
  345. }
  346. end_setxattr:
  347. hfs_find_exit(&cat_fd);
  348. return err;
  349. }
  350. static int name_len(const char *xattr_name, int xattr_name_len)
  351. {
  352. int len = xattr_name_len + 1;
  353. if (!is_known_namespace(xattr_name))
  354. len += XATTR_MAC_OSX_PREFIX_LEN;
  355. return len;
  356. }
  357. static int copy_name(char *buffer, const char *xattr_name, int name_len)
  358. {
  359. int len = name_len;
  360. int offset = 0;
  361. if (!is_known_namespace(xattr_name)) {
  362. strncpy(buffer, XATTR_MAC_OSX_PREFIX, XATTR_MAC_OSX_PREFIX_LEN);
  363. offset += XATTR_MAC_OSX_PREFIX_LEN;
  364. len += XATTR_MAC_OSX_PREFIX_LEN;
  365. }
  366. strncpy(buffer + offset, xattr_name, name_len);
  367. memset(buffer + offset + name_len, 0, 1);
  368. len += 1;
  369. return len;
  370. }
  371. int hfsplus_setxattr(struct dentry *dentry, const char *name,
  372. const void *value, size_t size, int flags,
  373. const char *prefix, size_t prefixlen)
  374. {
  375. char *xattr_name;
  376. int res;
  377. if (!strcmp(name, ""))
  378. return -EINVAL;
  379. xattr_name = kmalloc(NLS_MAX_CHARSET_SIZE * HFSPLUS_ATTR_MAX_STRLEN + 1,
  380. GFP_KERNEL);
  381. if (!xattr_name)
  382. return -ENOMEM;
  383. strcpy(xattr_name, prefix);
  384. strcpy(xattr_name + prefixlen, name);
  385. res = __hfsplus_setxattr(d_inode(dentry), xattr_name, value, size,
  386. flags);
  387. kfree(xattr_name);
  388. return res;
  389. }
  390. static ssize_t hfsplus_getxattr_finder_info(struct inode *inode,
  391. void *value, size_t size)
  392. {
  393. ssize_t res = 0;
  394. struct hfs_find_data fd;
  395. u16 entry_type;
  396. u16 folder_rec_len = sizeof(struct DInfo) + sizeof(struct DXInfo);
  397. u16 file_rec_len = sizeof(struct FInfo) + sizeof(struct FXInfo);
  398. u16 record_len = max(folder_rec_len, file_rec_len);
  399. u8 folder_finder_info[sizeof(struct DInfo) + sizeof(struct DXInfo)];
  400. u8 file_finder_info[sizeof(struct FInfo) + sizeof(struct FXInfo)];
  401. if (size >= record_len) {
  402. res = hfs_find_init(HFSPLUS_SB(inode->i_sb)->cat_tree, &fd);
  403. if (res) {
  404. pr_err("can't init xattr find struct\n");
  405. return res;
  406. }
  407. res = hfsplus_find_cat(inode->i_sb, inode->i_ino, &fd);
  408. if (res)
  409. goto end_getxattr_finder_info;
  410. entry_type = hfs_bnode_read_u16(fd.bnode, fd.entryoffset);
  411. if (entry_type == HFSPLUS_FOLDER) {
  412. hfs_bnode_read(fd.bnode, folder_finder_info,
  413. fd.entryoffset +
  414. offsetof(struct hfsplus_cat_folder, user_info),
  415. folder_rec_len);
  416. memcpy(value, folder_finder_info, folder_rec_len);
  417. res = folder_rec_len;
  418. } else if (entry_type == HFSPLUS_FILE) {
  419. hfs_bnode_read(fd.bnode, file_finder_info,
  420. fd.entryoffset +
  421. offsetof(struct hfsplus_cat_file, user_info),
  422. file_rec_len);
  423. memcpy(value, file_finder_info, file_rec_len);
  424. res = file_rec_len;
  425. } else {
  426. res = -EOPNOTSUPP;
  427. goto end_getxattr_finder_info;
  428. }
  429. } else
  430. res = size ? -ERANGE : record_len;
  431. end_getxattr_finder_info:
  432. if (size >= record_len)
  433. hfs_find_exit(&fd);
  434. return res;
  435. }
  436. ssize_t __hfsplus_getxattr(struct inode *inode, const char *name,
  437. void *value, size_t size)
  438. {
  439. struct hfs_find_data fd;
  440. hfsplus_attr_entry *entry;
  441. __be32 xattr_record_type;
  442. u32 record_type;
  443. u16 record_length = 0;
  444. ssize_t res = 0;
  445. if ((!S_ISREG(inode->i_mode) &&
  446. !S_ISDIR(inode->i_mode)) ||
  447. HFSPLUS_IS_RSRC(inode))
  448. return -EOPNOTSUPP;
  449. if (!strcmp_xattr_finder_info(name))
  450. return hfsplus_getxattr_finder_info(inode, value, size);
  451. if (!HFSPLUS_SB(inode->i_sb)->attr_tree)
  452. return -EOPNOTSUPP;
  453. entry = hfsplus_alloc_attr_entry();
  454. if (!entry) {
  455. pr_err("can't allocate xattr entry\n");
  456. return -ENOMEM;
  457. }
  458. res = hfs_find_init(HFSPLUS_SB(inode->i_sb)->attr_tree, &fd);
  459. if (res) {
  460. pr_err("can't init xattr find struct\n");
  461. goto failed_getxattr_init;
  462. }
  463. res = hfsplus_find_attr(inode->i_sb, inode->i_ino, name, &fd);
  464. if (res) {
  465. if (res == -ENOENT)
  466. res = -ENODATA;
  467. else
  468. pr_err("xattr searching failed\n");
  469. goto out;
  470. }
  471. hfs_bnode_read(fd.bnode, &xattr_record_type,
  472. fd.entryoffset, sizeof(xattr_record_type));
  473. record_type = be32_to_cpu(xattr_record_type);
  474. if (record_type == HFSPLUS_ATTR_INLINE_DATA) {
  475. record_length = hfs_bnode_read_u16(fd.bnode,
  476. fd.entryoffset +
  477. offsetof(struct hfsplus_attr_inline_data,
  478. length));
  479. if (record_length > HFSPLUS_MAX_INLINE_DATA_SIZE) {
  480. pr_err("invalid xattr record size\n");
  481. res = -EIO;
  482. goto out;
  483. }
  484. } else if (record_type == HFSPLUS_ATTR_FORK_DATA ||
  485. record_type == HFSPLUS_ATTR_EXTENTS) {
  486. pr_err("only inline data xattr are supported\n");
  487. res = -EOPNOTSUPP;
  488. goto out;
  489. } else {
  490. pr_err("invalid xattr record\n");
  491. res = -EIO;
  492. goto out;
  493. }
  494. if (size) {
  495. hfs_bnode_read(fd.bnode, entry, fd.entryoffset,
  496. offsetof(struct hfsplus_attr_inline_data,
  497. raw_bytes) + record_length);
  498. }
  499. if (size >= record_length) {
  500. memcpy(value, entry->inline_data.raw_bytes, record_length);
  501. res = record_length;
  502. } else
  503. res = size ? -ERANGE : record_length;
  504. out:
  505. hfs_find_exit(&fd);
  506. failed_getxattr_init:
  507. hfsplus_destroy_attr_entry(entry);
  508. return res;
  509. }
  510. ssize_t hfsplus_getxattr(struct dentry *dentry, const char *name,
  511. void *value, size_t size,
  512. const char *prefix, size_t prefixlen)
  513. {
  514. int res;
  515. char *xattr_name;
  516. if (!strcmp(name, ""))
  517. return -EINVAL;
  518. xattr_name = kmalloc(NLS_MAX_CHARSET_SIZE * HFSPLUS_ATTR_MAX_STRLEN + 1,
  519. GFP_KERNEL);
  520. if (!xattr_name)
  521. return -ENOMEM;
  522. strcpy(xattr_name, prefix);
  523. strcpy(xattr_name + prefixlen, name);
  524. res = __hfsplus_getxattr(d_inode(dentry), xattr_name, value, size);
  525. kfree(xattr_name);
  526. return res;
  527. }
  528. static inline int can_list(const char *xattr_name)
  529. {
  530. if (!xattr_name)
  531. return 0;
  532. return strncmp(xattr_name, XATTR_TRUSTED_PREFIX,
  533. XATTR_TRUSTED_PREFIX_LEN) ||
  534. capable(CAP_SYS_ADMIN);
  535. }
  536. static ssize_t hfsplus_listxattr_finder_info(struct dentry *dentry,
  537. char *buffer, size_t size)
  538. {
  539. ssize_t res = 0;
  540. struct inode *inode = d_inode(dentry);
  541. struct hfs_find_data fd;
  542. u16 entry_type;
  543. u8 folder_finder_info[sizeof(struct DInfo) + sizeof(struct DXInfo)];
  544. u8 file_finder_info[sizeof(struct FInfo) + sizeof(struct FXInfo)];
  545. unsigned long len, found_bit;
  546. int xattr_name_len, symbols_count;
  547. res = hfs_find_init(HFSPLUS_SB(inode->i_sb)->cat_tree, &fd);
  548. if (res) {
  549. pr_err("can't init xattr find struct\n");
  550. return res;
  551. }
  552. res = hfsplus_find_cat(inode->i_sb, inode->i_ino, &fd);
  553. if (res)
  554. goto end_listxattr_finder_info;
  555. entry_type = hfs_bnode_read_u16(fd.bnode, fd.entryoffset);
  556. if (entry_type == HFSPLUS_FOLDER) {
  557. len = sizeof(struct DInfo) + sizeof(struct DXInfo);
  558. hfs_bnode_read(fd.bnode, folder_finder_info,
  559. fd.entryoffset +
  560. offsetof(struct hfsplus_cat_folder, user_info),
  561. len);
  562. found_bit = find_first_bit((void *)folder_finder_info, len*8);
  563. } else if (entry_type == HFSPLUS_FILE) {
  564. len = sizeof(struct FInfo) + sizeof(struct FXInfo);
  565. hfs_bnode_read(fd.bnode, file_finder_info,
  566. fd.entryoffset +
  567. offsetof(struct hfsplus_cat_file, user_info),
  568. len);
  569. found_bit = find_first_bit((void *)file_finder_info, len*8);
  570. } else {
  571. res = -EOPNOTSUPP;
  572. goto end_listxattr_finder_info;
  573. }
  574. if (found_bit >= (len*8))
  575. res = 0;
  576. else {
  577. symbols_count = sizeof(HFSPLUS_XATTR_FINDER_INFO_NAME) - 1;
  578. xattr_name_len =
  579. name_len(HFSPLUS_XATTR_FINDER_INFO_NAME, symbols_count);
  580. if (!buffer || !size) {
  581. if (can_list(HFSPLUS_XATTR_FINDER_INFO_NAME))
  582. res = xattr_name_len;
  583. } else if (can_list(HFSPLUS_XATTR_FINDER_INFO_NAME)) {
  584. if (size < xattr_name_len)
  585. res = -ERANGE;
  586. else {
  587. res = copy_name(buffer,
  588. HFSPLUS_XATTR_FINDER_INFO_NAME,
  589. symbols_count);
  590. }
  591. }
  592. }
  593. end_listxattr_finder_info:
  594. hfs_find_exit(&fd);
  595. return res;
  596. }
  597. ssize_t hfsplus_listxattr(struct dentry *dentry, char *buffer, size_t size)
  598. {
  599. ssize_t err;
  600. ssize_t res = 0;
  601. struct inode *inode = d_inode(dentry);
  602. struct hfs_find_data fd;
  603. u16 key_len = 0;
  604. struct hfsplus_attr_key attr_key;
  605. char *strbuf;
  606. int xattr_name_len;
  607. if ((!S_ISREG(inode->i_mode) &&
  608. !S_ISDIR(inode->i_mode)) ||
  609. HFSPLUS_IS_RSRC(inode))
  610. return -EOPNOTSUPP;
  611. res = hfsplus_listxattr_finder_info(dentry, buffer, size);
  612. if (res < 0)
  613. return res;
  614. else if (!HFSPLUS_SB(inode->i_sb)->attr_tree)
  615. return (res == 0) ? -EOPNOTSUPP : res;
  616. err = hfs_find_init(HFSPLUS_SB(inode->i_sb)->attr_tree, &fd);
  617. if (err) {
  618. pr_err("can't init xattr find struct\n");
  619. return err;
  620. }
  621. strbuf = kmalloc(NLS_MAX_CHARSET_SIZE * HFSPLUS_ATTR_MAX_STRLEN +
  622. XATTR_MAC_OSX_PREFIX_LEN + 1, GFP_KERNEL);
  623. if (!strbuf) {
  624. res = -ENOMEM;
  625. goto out;
  626. }
  627. err = hfsplus_find_attr(inode->i_sb, inode->i_ino, NULL, &fd);
  628. if (err) {
  629. if (err == -ENOENT) {
  630. if (res == 0)
  631. res = -ENODATA;
  632. goto end_listxattr;
  633. } else {
  634. res = err;
  635. goto end_listxattr;
  636. }
  637. }
  638. for (;;) {
  639. key_len = hfs_bnode_read_u16(fd.bnode, fd.keyoffset);
  640. if (key_len == 0 || key_len > fd.tree->max_key_len) {
  641. pr_err("invalid xattr key length: %d\n", key_len);
  642. res = -EIO;
  643. goto end_listxattr;
  644. }
  645. hfs_bnode_read(fd.bnode, &attr_key,
  646. fd.keyoffset, key_len + sizeof(key_len));
  647. if (be32_to_cpu(attr_key.cnid) != inode->i_ino)
  648. goto end_listxattr;
  649. xattr_name_len = NLS_MAX_CHARSET_SIZE * HFSPLUS_ATTR_MAX_STRLEN;
  650. if (hfsplus_uni2asc(inode->i_sb,
  651. (const struct hfsplus_unistr *)&fd.key->attr.key_name,
  652. strbuf, &xattr_name_len)) {
  653. pr_err("unicode conversion failed\n");
  654. res = -EIO;
  655. goto end_listxattr;
  656. }
  657. if (!buffer || !size) {
  658. if (can_list(strbuf))
  659. res += name_len(strbuf, xattr_name_len);
  660. } else if (can_list(strbuf)) {
  661. if (size < (res + name_len(strbuf, xattr_name_len))) {
  662. res = -ERANGE;
  663. goto end_listxattr;
  664. } else
  665. res += copy_name(buffer + res,
  666. strbuf, xattr_name_len);
  667. }
  668. if (hfs_brec_goto(&fd, 1))
  669. goto end_listxattr;
  670. }
  671. end_listxattr:
  672. kfree(strbuf);
  673. out:
  674. hfs_find_exit(&fd);
  675. return res;
  676. }
  677. static int hfsplus_removexattr(struct inode *inode, const char *name)
  678. {
  679. int err = 0;
  680. struct hfs_find_data cat_fd;
  681. u16 flags;
  682. u16 cat_entry_type;
  683. int is_xattr_acl_deleted = 0;
  684. int is_all_xattrs_deleted = 0;
  685. if (!HFSPLUS_SB(inode->i_sb)->attr_tree)
  686. return -EOPNOTSUPP;
  687. if (!strcmp_xattr_finder_info(name))
  688. return -EOPNOTSUPP;
  689. err = hfs_find_init(HFSPLUS_SB(inode->i_sb)->cat_tree, &cat_fd);
  690. if (err) {
  691. pr_err("can't init xattr find struct\n");
  692. return err;
  693. }
  694. err = hfsplus_find_cat(inode->i_sb, inode->i_ino, &cat_fd);
  695. if (err) {
  696. pr_err("catalog searching failed\n");
  697. goto end_removexattr;
  698. }
  699. err = hfsplus_delete_attr(inode, name);
  700. if (err)
  701. goto end_removexattr;
  702. is_xattr_acl_deleted = !strcmp_xattr_acl(name);
  703. is_all_xattrs_deleted = !hfsplus_attr_exists(inode, NULL);
  704. if (!is_xattr_acl_deleted && !is_all_xattrs_deleted)
  705. goto end_removexattr;
  706. cat_entry_type = hfs_bnode_read_u16(cat_fd.bnode, cat_fd.entryoffset);
  707. if (cat_entry_type == HFSPLUS_FOLDER) {
  708. flags = hfs_bnode_read_u16(cat_fd.bnode, cat_fd.entryoffset +
  709. offsetof(struct hfsplus_cat_folder, flags));
  710. if (is_xattr_acl_deleted)
  711. flags &= ~HFSPLUS_ACL_EXISTS;
  712. if (is_all_xattrs_deleted)
  713. flags &= ~HFSPLUS_XATTR_EXISTS;
  714. hfs_bnode_write_u16(cat_fd.bnode, cat_fd.entryoffset +
  715. offsetof(struct hfsplus_cat_folder, flags),
  716. flags);
  717. hfsplus_mark_inode_dirty(inode, HFSPLUS_I_CAT_DIRTY);
  718. } else if (cat_entry_type == HFSPLUS_FILE) {
  719. flags = hfs_bnode_read_u16(cat_fd.bnode, cat_fd.entryoffset +
  720. offsetof(struct hfsplus_cat_file, flags));
  721. if (is_xattr_acl_deleted)
  722. flags &= ~HFSPLUS_ACL_EXISTS;
  723. if (is_all_xattrs_deleted)
  724. flags &= ~HFSPLUS_XATTR_EXISTS;
  725. hfs_bnode_write_u16(cat_fd.bnode, cat_fd.entryoffset +
  726. offsetof(struct hfsplus_cat_file, flags),
  727. flags);
  728. hfsplus_mark_inode_dirty(inode, HFSPLUS_I_CAT_DIRTY);
  729. } else {
  730. pr_err("invalid catalog entry type\n");
  731. err = -EIO;
  732. goto end_removexattr;
  733. }
  734. end_removexattr:
  735. hfs_find_exit(&cat_fd);
  736. return err;
  737. }
  738. static int hfsplus_osx_getxattr(const struct xattr_handler *handler,
  739. struct dentry *dentry, const char *name,
  740. void *buffer, size_t size)
  741. {
  742. if (!strcmp(name, ""))
  743. return -EINVAL;
  744. /*
  745. * Don't allow retrieving properly prefixed attributes
  746. * by prepending them with "osx."
  747. */
  748. if (is_known_namespace(name))
  749. return -EOPNOTSUPP;
  750. /*
  751. * osx is the namespace we use to indicate an unprefixed
  752. * attribute on the filesystem (like the ones that OS X
  753. * creates), so we pass the name through unmodified (after
  754. * ensuring it doesn't conflict with another namespace).
  755. */
  756. return __hfsplus_getxattr(d_inode(dentry), name, buffer, size);
  757. }
  758. static int hfsplus_osx_setxattr(const struct xattr_handler *handler,
  759. struct dentry *dentry, const char *name,
  760. const void *buffer, size_t size, int flags)
  761. {
  762. if (!strcmp(name, ""))
  763. return -EINVAL;
  764. /*
  765. * Don't allow setting properly prefixed attributes
  766. * by prepending them with "osx."
  767. */
  768. if (is_known_namespace(name))
  769. return -EOPNOTSUPP;
  770. /*
  771. * osx is the namespace we use to indicate an unprefixed
  772. * attribute on the filesystem (like the ones that OS X
  773. * creates), so we pass the name through unmodified (after
  774. * ensuring it doesn't conflict with another namespace).
  775. */
  776. return __hfsplus_setxattr(d_inode(dentry), name, buffer, size, flags);
  777. }
  778. const struct xattr_handler hfsplus_xattr_osx_handler = {
  779. .prefix = XATTR_MAC_OSX_PREFIX,
  780. .get = hfsplus_osx_getxattr,
  781. .set = hfsplus_osx_setxattr,
  782. };