xattr.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891
  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_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_SIZE, node_size - written));
  204. kunmap_atomic(kaddr);
  205. set_page_dirty(page);
  206. put_page(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 inode *inode, 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. xattr_name = kmalloc(NLS_MAX_CHARSET_SIZE * HFSPLUS_ATTR_MAX_STRLEN + 1,
  378. GFP_KERNEL);
  379. if (!xattr_name)
  380. return -ENOMEM;
  381. strcpy(xattr_name, prefix);
  382. strcpy(xattr_name + prefixlen, name);
  383. res = __hfsplus_setxattr(inode, xattr_name, value, size, flags);
  384. kfree(xattr_name);
  385. return res;
  386. }
  387. static ssize_t hfsplus_getxattr_finder_info(struct inode *inode,
  388. void *value, size_t size)
  389. {
  390. ssize_t res = 0;
  391. struct hfs_find_data fd;
  392. u16 entry_type;
  393. u16 folder_rec_len = sizeof(struct DInfo) + sizeof(struct DXInfo);
  394. u16 file_rec_len = sizeof(struct FInfo) + sizeof(struct FXInfo);
  395. u16 record_len = max(folder_rec_len, file_rec_len);
  396. u8 folder_finder_info[sizeof(struct DInfo) + sizeof(struct DXInfo)];
  397. u8 file_finder_info[sizeof(struct FInfo) + sizeof(struct FXInfo)];
  398. if (size >= record_len) {
  399. res = hfs_find_init(HFSPLUS_SB(inode->i_sb)->cat_tree, &fd);
  400. if (res) {
  401. pr_err("can't init xattr find struct\n");
  402. return res;
  403. }
  404. res = hfsplus_find_cat(inode->i_sb, inode->i_ino, &fd);
  405. if (res)
  406. goto end_getxattr_finder_info;
  407. entry_type = hfs_bnode_read_u16(fd.bnode, fd.entryoffset);
  408. if (entry_type == HFSPLUS_FOLDER) {
  409. hfs_bnode_read(fd.bnode, folder_finder_info,
  410. fd.entryoffset +
  411. offsetof(struct hfsplus_cat_folder, user_info),
  412. folder_rec_len);
  413. memcpy(value, folder_finder_info, folder_rec_len);
  414. res = folder_rec_len;
  415. } else if (entry_type == HFSPLUS_FILE) {
  416. hfs_bnode_read(fd.bnode, file_finder_info,
  417. fd.entryoffset +
  418. offsetof(struct hfsplus_cat_file, user_info),
  419. file_rec_len);
  420. memcpy(value, file_finder_info, file_rec_len);
  421. res = file_rec_len;
  422. } else {
  423. res = -EOPNOTSUPP;
  424. goto end_getxattr_finder_info;
  425. }
  426. } else
  427. res = size ? -ERANGE : record_len;
  428. end_getxattr_finder_info:
  429. if (size >= record_len)
  430. hfs_find_exit(&fd);
  431. return res;
  432. }
  433. ssize_t __hfsplus_getxattr(struct inode *inode, const char *name,
  434. void *value, size_t size)
  435. {
  436. struct hfs_find_data fd;
  437. hfsplus_attr_entry *entry;
  438. __be32 xattr_record_type;
  439. u32 record_type;
  440. u16 record_length = 0;
  441. ssize_t res = 0;
  442. if ((!S_ISREG(inode->i_mode) &&
  443. !S_ISDIR(inode->i_mode)) ||
  444. HFSPLUS_IS_RSRC(inode))
  445. return -EOPNOTSUPP;
  446. if (!strcmp_xattr_finder_info(name))
  447. return hfsplus_getxattr_finder_info(inode, value, size);
  448. if (!HFSPLUS_SB(inode->i_sb)->attr_tree)
  449. return -EOPNOTSUPP;
  450. entry = hfsplus_alloc_attr_entry();
  451. if (!entry) {
  452. pr_err("can't allocate xattr entry\n");
  453. return -ENOMEM;
  454. }
  455. res = hfs_find_init(HFSPLUS_SB(inode->i_sb)->attr_tree, &fd);
  456. if (res) {
  457. pr_err("can't init xattr find struct\n");
  458. goto failed_getxattr_init;
  459. }
  460. res = hfsplus_find_attr(inode->i_sb, inode->i_ino, name, &fd);
  461. if (res) {
  462. if (res == -ENOENT)
  463. res = -ENODATA;
  464. else
  465. pr_err("xattr searching failed\n");
  466. goto out;
  467. }
  468. hfs_bnode_read(fd.bnode, &xattr_record_type,
  469. fd.entryoffset, sizeof(xattr_record_type));
  470. record_type = be32_to_cpu(xattr_record_type);
  471. if (record_type == HFSPLUS_ATTR_INLINE_DATA) {
  472. record_length = hfs_bnode_read_u16(fd.bnode,
  473. fd.entryoffset +
  474. offsetof(struct hfsplus_attr_inline_data,
  475. length));
  476. if (record_length > HFSPLUS_MAX_INLINE_DATA_SIZE) {
  477. pr_err("invalid xattr record size\n");
  478. res = -EIO;
  479. goto out;
  480. }
  481. } else if (record_type == HFSPLUS_ATTR_FORK_DATA ||
  482. record_type == HFSPLUS_ATTR_EXTENTS) {
  483. pr_err("only inline data xattr are supported\n");
  484. res = -EOPNOTSUPP;
  485. goto out;
  486. } else {
  487. pr_err("invalid xattr record\n");
  488. res = -EIO;
  489. goto out;
  490. }
  491. if (size) {
  492. hfs_bnode_read(fd.bnode, entry, fd.entryoffset,
  493. offsetof(struct hfsplus_attr_inline_data,
  494. raw_bytes) + record_length);
  495. }
  496. if (size >= record_length) {
  497. memcpy(value, entry->inline_data.raw_bytes, record_length);
  498. res = record_length;
  499. } else
  500. res = size ? -ERANGE : record_length;
  501. out:
  502. hfs_find_exit(&fd);
  503. failed_getxattr_init:
  504. hfsplus_destroy_attr_entry(entry);
  505. return res;
  506. }
  507. ssize_t hfsplus_getxattr(struct inode *inode, const char *name,
  508. void *value, size_t size,
  509. const char *prefix, size_t prefixlen)
  510. {
  511. int res;
  512. char *xattr_name;
  513. xattr_name = kmalloc(NLS_MAX_CHARSET_SIZE * HFSPLUS_ATTR_MAX_STRLEN + 1,
  514. GFP_KERNEL);
  515. if (!xattr_name)
  516. return -ENOMEM;
  517. strcpy(xattr_name, prefix);
  518. strcpy(xattr_name + prefixlen, name);
  519. res = __hfsplus_getxattr(inode, xattr_name, value, size);
  520. kfree(xattr_name);
  521. return res;
  522. }
  523. static inline int can_list(const char *xattr_name)
  524. {
  525. if (!xattr_name)
  526. return 0;
  527. return strncmp(xattr_name, XATTR_TRUSTED_PREFIX,
  528. XATTR_TRUSTED_PREFIX_LEN) ||
  529. capable(CAP_SYS_ADMIN);
  530. }
  531. static ssize_t hfsplus_listxattr_finder_info(struct dentry *dentry,
  532. char *buffer, size_t size)
  533. {
  534. ssize_t res = 0;
  535. struct inode *inode = d_inode(dentry);
  536. struct hfs_find_data fd;
  537. u16 entry_type;
  538. u8 folder_finder_info[sizeof(struct DInfo) + sizeof(struct DXInfo)];
  539. u8 file_finder_info[sizeof(struct FInfo) + sizeof(struct FXInfo)];
  540. unsigned long len, found_bit;
  541. int xattr_name_len, symbols_count;
  542. res = hfs_find_init(HFSPLUS_SB(inode->i_sb)->cat_tree, &fd);
  543. if (res) {
  544. pr_err("can't init xattr find struct\n");
  545. return res;
  546. }
  547. res = hfsplus_find_cat(inode->i_sb, inode->i_ino, &fd);
  548. if (res)
  549. goto end_listxattr_finder_info;
  550. entry_type = hfs_bnode_read_u16(fd.bnode, fd.entryoffset);
  551. if (entry_type == HFSPLUS_FOLDER) {
  552. len = sizeof(struct DInfo) + sizeof(struct DXInfo);
  553. hfs_bnode_read(fd.bnode, folder_finder_info,
  554. fd.entryoffset +
  555. offsetof(struct hfsplus_cat_folder, user_info),
  556. len);
  557. found_bit = find_first_bit((void *)folder_finder_info, len*8);
  558. } else if (entry_type == HFSPLUS_FILE) {
  559. len = sizeof(struct FInfo) + sizeof(struct FXInfo);
  560. hfs_bnode_read(fd.bnode, file_finder_info,
  561. fd.entryoffset +
  562. offsetof(struct hfsplus_cat_file, user_info),
  563. len);
  564. found_bit = find_first_bit((void *)file_finder_info, len*8);
  565. } else {
  566. res = -EOPNOTSUPP;
  567. goto end_listxattr_finder_info;
  568. }
  569. if (found_bit >= (len*8))
  570. res = 0;
  571. else {
  572. symbols_count = sizeof(HFSPLUS_XATTR_FINDER_INFO_NAME) - 1;
  573. xattr_name_len =
  574. name_len(HFSPLUS_XATTR_FINDER_INFO_NAME, symbols_count);
  575. if (!buffer || !size) {
  576. if (can_list(HFSPLUS_XATTR_FINDER_INFO_NAME))
  577. res = xattr_name_len;
  578. } else if (can_list(HFSPLUS_XATTR_FINDER_INFO_NAME)) {
  579. if (size < xattr_name_len)
  580. res = -ERANGE;
  581. else {
  582. res = copy_name(buffer,
  583. HFSPLUS_XATTR_FINDER_INFO_NAME,
  584. symbols_count);
  585. }
  586. }
  587. }
  588. end_listxattr_finder_info:
  589. hfs_find_exit(&fd);
  590. return res;
  591. }
  592. ssize_t hfsplus_listxattr(struct dentry *dentry, char *buffer, size_t size)
  593. {
  594. ssize_t err;
  595. ssize_t res = 0;
  596. struct inode *inode = d_inode(dentry);
  597. struct hfs_find_data fd;
  598. u16 key_len = 0;
  599. struct hfsplus_attr_key attr_key;
  600. char *strbuf;
  601. int xattr_name_len;
  602. if ((!S_ISREG(inode->i_mode) &&
  603. !S_ISDIR(inode->i_mode)) ||
  604. HFSPLUS_IS_RSRC(inode))
  605. return -EOPNOTSUPP;
  606. res = hfsplus_listxattr_finder_info(dentry, buffer, size);
  607. if (res < 0)
  608. return res;
  609. else if (!HFSPLUS_SB(inode->i_sb)->attr_tree)
  610. return (res == 0) ? -EOPNOTSUPP : res;
  611. err = hfs_find_init(HFSPLUS_SB(inode->i_sb)->attr_tree, &fd);
  612. if (err) {
  613. pr_err("can't init xattr find struct\n");
  614. return err;
  615. }
  616. strbuf = kmalloc(NLS_MAX_CHARSET_SIZE * HFSPLUS_ATTR_MAX_STRLEN +
  617. XATTR_MAC_OSX_PREFIX_LEN + 1, GFP_KERNEL);
  618. if (!strbuf) {
  619. res = -ENOMEM;
  620. goto out;
  621. }
  622. err = hfsplus_find_attr(inode->i_sb, inode->i_ino, NULL, &fd);
  623. if (err) {
  624. if (err == -ENOENT) {
  625. if (res == 0)
  626. res = -ENODATA;
  627. goto end_listxattr;
  628. } else {
  629. res = err;
  630. goto end_listxattr;
  631. }
  632. }
  633. for (;;) {
  634. key_len = hfs_bnode_read_u16(fd.bnode, fd.keyoffset);
  635. if (key_len == 0 || key_len > fd.tree->max_key_len) {
  636. pr_err("invalid xattr key length: %d\n", key_len);
  637. res = -EIO;
  638. goto end_listxattr;
  639. }
  640. hfs_bnode_read(fd.bnode, &attr_key,
  641. fd.keyoffset, key_len + sizeof(key_len));
  642. if (be32_to_cpu(attr_key.cnid) != inode->i_ino)
  643. goto end_listxattr;
  644. xattr_name_len = NLS_MAX_CHARSET_SIZE * HFSPLUS_ATTR_MAX_STRLEN;
  645. if (hfsplus_uni2asc(inode->i_sb,
  646. (const struct hfsplus_unistr *)&fd.key->attr.key_name,
  647. strbuf, &xattr_name_len)) {
  648. pr_err("unicode conversion failed\n");
  649. res = -EIO;
  650. goto end_listxattr;
  651. }
  652. if (!buffer || !size) {
  653. if (can_list(strbuf))
  654. res += name_len(strbuf, xattr_name_len);
  655. } else if (can_list(strbuf)) {
  656. if (size < (res + name_len(strbuf, xattr_name_len))) {
  657. res = -ERANGE;
  658. goto end_listxattr;
  659. } else
  660. res += copy_name(buffer + res,
  661. strbuf, xattr_name_len);
  662. }
  663. if (hfs_brec_goto(&fd, 1))
  664. goto end_listxattr;
  665. }
  666. end_listxattr:
  667. kfree(strbuf);
  668. out:
  669. hfs_find_exit(&fd);
  670. return res;
  671. }
  672. static int hfsplus_removexattr(struct inode *inode, const char *name)
  673. {
  674. int err = 0;
  675. struct hfs_find_data cat_fd;
  676. u16 flags;
  677. u16 cat_entry_type;
  678. int is_xattr_acl_deleted = 0;
  679. int is_all_xattrs_deleted = 0;
  680. if (!HFSPLUS_SB(inode->i_sb)->attr_tree)
  681. return -EOPNOTSUPP;
  682. if (!strcmp_xattr_finder_info(name))
  683. return -EOPNOTSUPP;
  684. err = hfs_find_init(HFSPLUS_SB(inode->i_sb)->cat_tree, &cat_fd);
  685. if (err) {
  686. pr_err("can't init xattr find struct\n");
  687. return err;
  688. }
  689. err = hfsplus_find_cat(inode->i_sb, inode->i_ino, &cat_fd);
  690. if (err) {
  691. pr_err("catalog searching failed\n");
  692. goto end_removexattr;
  693. }
  694. err = hfsplus_delete_attr(inode, name);
  695. if (err)
  696. goto end_removexattr;
  697. is_xattr_acl_deleted = !strcmp_xattr_acl(name);
  698. is_all_xattrs_deleted = !hfsplus_attr_exists(inode, NULL);
  699. if (!is_xattr_acl_deleted && !is_all_xattrs_deleted)
  700. goto end_removexattr;
  701. cat_entry_type = hfs_bnode_read_u16(cat_fd.bnode, cat_fd.entryoffset);
  702. if (cat_entry_type == HFSPLUS_FOLDER) {
  703. flags = hfs_bnode_read_u16(cat_fd.bnode, cat_fd.entryoffset +
  704. offsetof(struct hfsplus_cat_folder, flags));
  705. if (is_xattr_acl_deleted)
  706. flags &= ~HFSPLUS_ACL_EXISTS;
  707. if (is_all_xattrs_deleted)
  708. flags &= ~HFSPLUS_XATTR_EXISTS;
  709. hfs_bnode_write_u16(cat_fd.bnode, cat_fd.entryoffset +
  710. offsetof(struct hfsplus_cat_folder, flags),
  711. flags);
  712. hfsplus_mark_inode_dirty(inode, HFSPLUS_I_CAT_DIRTY);
  713. } else if (cat_entry_type == HFSPLUS_FILE) {
  714. flags = hfs_bnode_read_u16(cat_fd.bnode, cat_fd.entryoffset +
  715. offsetof(struct hfsplus_cat_file, flags));
  716. if (is_xattr_acl_deleted)
  717. flags &= ~HFSPLUS_ACL_EXISTS;
  718. if (is_all_xattrs_deleted)
  719. flags &= ~HFSPLUS_XATTR_EXISTS;
  720. hfs_bnode_write_u16(cat_fd.bnode, cat_fd.entryoffset +
  721. offsetof(struct hfsplus_cat_file, flags),
  722. flags);
  723. hfsplus_mark_inode_dirty(inode, HFSPLUS_I_CAT_DIRTY);
  724. } else {
  725. pr_err("invalid catalog entry type\n");
  726. err = -EIO;
  727. goto end_removexattr;
  728. }
  729. end_removexattr:
  730. hfs_find_exit(&cat_fd);
  731. return err;
  732. }
  733. static int hfsplus_osx_getxattr(const struct xattr_handler *handler,
  734. struct dentry *unused, struct inode *inode,
  735. const char *name, void *buffer, size_t size)
  736. {
  737. /*
  738. * Don't allow retrieving properly prefixed attributes
  739. * by prepending them with "osx."
  740. */
  741. if (is_known_namespace(name))
  742. return -EOPNOTSUPP;
  743. /*
  744. * osx is the namespace we use to indicate an unprefixed
  745. * attribute on the filesystem (like the ones that OS X
  746. * creates), so we pass the name through unmodified (after
  747. * ensuring it doesn't conflict with another namespace).
  748. */
  749. return __hfsplus_getxattr(inode, name, buffer, size);
  750. }
  751. static int hfsplus_osx_setxattr(const struct xattr_handler *handler,
  752. struct dentry *unused, struct inode *inode,
  753. const char *name, const void *buffer,
  754. size_t size, int flags)
  755. {
  756. /*
  757. * Don't allow setting properly prefixed attributes
  758. * by prepending them with "osx."
  759. */
  760. if (is_known_namespace(name))
  761. return -EOPNOTSUPP;
  762. /*
  763. * osx is the namespace we use to indicate an unprefixed
  764. * attribute on the filesystem (like the ones that OS X
  765. * creates), so we pass the name through unmodified (after
  766. * ensuring it doesn't conflict with another namespace).
  767. */
  768. return __hfsplus_setxattr(inode, name, buffer, size, flags);
  769. }
  770. const struct xattr_handler hfsplus_xattr_osx_handler = {
  771. .prefix = XATTR_MAC_OSX_PREFIX,
  772. .get = hfsplus_osx_getxattr,
  773. .set = hfsplus_osx_setxattr,
  774. };