xattr.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. /**
  2. * fs/f2fs/xattr.c
  3. *
  4. * Copyright (c) 2012 Samsung Electronics Co., Ltd.
  5. * http://www.samsung.com/
  6. *
  7. * Portions of this code from linux/fs/ext2/xattr.c
  8. *
  9. * Copyright (C) 2001-2003 Andreas Gruenbacher <agruen@suse.de>
  10. *
  11. * Fix by Harrison Xing <harrison@mountainviewdata.com>.
  12. * Extended attributes for symlinks and special files added per
  13. * suggestion of Luka Renko <luka.renko@hermes.si>.
  14. * xattr consolidation Copyright (c) 2004 James Morris <jmorris@redhat.com>,
  15. * Red Hat Inc.
  16. *
  17. * This program is free software; you can redistribute it and/or modify
  18. * it under the terms of the GNU General Public License version 2 as
  19. * published by the Free Software Foundation.
  20. */
  21. #include <linux/rwsem.h>
  22. #include <linux/f2fs_fs.h>
  23. #include "f2fs.h"
  24. #include "xattr.h"
  25. static size_t f2fs_xattr_generic_list(struct dentry *dentry, char *list,
  26. size_t list_size, const char *name, size_t name_len, int type)
  27. {
  28. struct f2fs_sb_info *sbi = F2FS_SB(dentry->d_sb);
  29. int total_len, prefix_len = 0;
  30. const char *prefix = NULL;
  31. switch (type) {
  32. case F2FS_XATTR_INDEX_USER:
  33. if (!test_opt(sbi, XATTR_USER))
  34. return -EOPNOTSUPP;
  35. prefix = XATTR_USER_PREFIX;
  36. prefix_len = XATTR_USER_PREFIX_LEN;
  37. break;
  38. case F2FS_XATTR_INDEX_TRUSTED:
  39. if (!capable(CAP_SYS_ADMIN))
  40. return -EPERM;
  41. prefix = XATTR_TRUSTED_PREFIX;
  42. prefix_len = XATTR_TRUSTED_PREFIX_LEN;
  43. break;
  44. default:
  45. return -EINVAL;
  46. }
  47. total_len = prefix_len + name_len + 1;
  48. if (list && total_len <= list_size) {
  49. memcpy(list, prefix, prefix_len);
  50. memcpy(list+prefix_len, name, name_len);
  51. list[prefix_len + name_len] = '\0';
  52. }
  53. return total_len;
  54. }
  55. static int f2fs_xattr_generic_get(struct dentry *dentry, const char *name,
  56. void *buffer, size_t size, int type)
  57. {
  58. struct f2fs_sb_info *sbi = F2FS_SB(dentry->d_sb);
  59. switch (type) {
  60. case F2FS_XATTR_INDEX_USER:
  61. if (!test_opt(sbi, XATTR_USER))
  62. return -EOPNOTSUPP;
  63. break;
  64. case F2FS_XATTR_INDEX_TRUSTED:
  65. if (!capable(CAP_SYS_ADMIN))
  66. return -EPERM;
  67. break;
  68. default:
  69. return -EINVAL;
  70. }
  71. if (strcmp(name, "") == 0)
  72. return -EINVAL;
  73. return f2fs_getxattr(dentry->d_inode, type, name,
  74. buffer, size);
  75. }
  76. static int f2fs_xattr_generic_set(struct dentry *dentry, const char *name,
  77. const void *value, size_t size, int flags, int type)
  78. {
  79. struct f2fs_sb_info *sbi = F2FS_SB(dentry->d_sb);
  80. switch (type) {
  81. case F2FS_XATTR_INDEX_USER:
  82. if (!test_opt(sbi, XATTR_USER))
  83. return -EOPNOTSUPP;
  84. break;
  85. case F2FS_XATTR_INDEX_TRUSTED:
  86. if (!capable(CAP_SYS_ADMIN))
  87. return -EPERM;
  88. break;
  89. default:
  90. return -EINVAL;
  91. }
  92. if (strcmp(name, "") == 0)
  93. return -EINVAL;
  94. return f2fs_setxattr(dentry->d_inode, type, name, value, size);
  95. }
  96. const struct xattr_handler f2fs_xattr_user_handler = {
  97. .prefix = XATTR_USER_PREFIX,
  98. .flags = F2FS_XATTR_INDEX_USER,
  99. .list = f2fs_xattr_generic_list,
  100. .get = f2fs_xattr_generic_get,
  101. .set = f2fs_xattr_generic_set,
  102. };
  103. const struct xattr_handler f2fs_xattr_trusted_handler = {
  104. .prefix = XATTR_TRUSTED_PREFIX,
  105. .flags = F2FS_XATTR_INDEX_TRUSTED,
  106. .list = f2fs_xattr_generic_list,
  107. .get = f2fs_xattr_generic_get,
  108. .set = f2fs_xattr_generic_set,
  109. };
  110. static const struct xattr_handler *f2fs_xattr_handler_map[] = {
  111. [F2FS_XATTR_INDEX_USER] = &f2fs_xattr_user_handler,
  112. #ifdef CONFIG_F2FS_FS_POSIX_ACL
  113. [F2FS_XATTR_INDEX_POSIX_ACL_ACCESS] = &f2fs_xattr_acl_access_handler,
  114. [F2FS_XATTR_INDEX_POSIX_ACL_DEFAULT] = &f2fs_xattr_acl_default_handler,
  115. #endif
  116. [F2FS_XATTR_INDEX_TRUSTED] = &f2fs_xattr_trusted_handler,
  117. [F2FS_XATTR_INDEX_ADVISE] = &f2fs_xattr_advise_handler,
  118. };
  119. const struct xattr_handler *f2fs_xattr_handlers[] = {
  120. &f2fs_xattr_user_handler,
  121. #ifdef CONFIG_F2FS_FS_POSIX_ACL
  122. &f2fs_xattr_acl_access_handler,
  123. &f2fs_xattr_acl_default_handler,
  124. #endif
  125. &f2fs_xattr_trusted_handler,
  126. &f2fs_xattr_advise_handler,
  127. NULL,
  128. };
  129. static inline const struct xattr_handler *f2fs_xattr_handler(int name_index)
  130. {
  131. const struct xattr_handler *handler = NULL;
  132. if (name_index > 0 && name_index < ARRAY_SIZE(f2fs_xattr_handler_map))
  133. handler = f2fs_xattr_handler_map[name_index];
  134. return handler;
  135. }
  136. int f2fs_getxattr(struct inode *inode, int name_index, const char *name,
  137. void *buffer, size_t buffer_size)
  138. {
  139. struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
  140. struct f2fs_inode_info *fi = F2FS_I(inode);
  141. struct f2fs_xattr_entry *entry;
  142. struct page *page;
  143. void *base_addr;
  144. int error = 0, found = 0;
  145. int value_len, name_len;
  146. if (name == NULL)
  147. return -EINVAL;
  148. name_len = strlen(name);
  149. if (!fi->i_xattr_nid)
  150. return -ENODATA;
  151. page = get_node_page(sbi, fi->i_xattr_nid);
  152. base_addr = page_address(page);
  153. list_for_each_xattr(entry, base_addr) {
  154. if (entry->e_name_index != name_index)
  155. continue;
  156. if (entry->e_name_len != name_len)
  157. continue;
  158. if (!memcmp(entry->e_name, name, name_len)) {
  159. found = 1;
  160. break;
  161. }
  162. }
  163. if (!found) {
  164. error = -ENODATA;
  165. goto cleanup;
  166. }
  167. value_len = le16_to_cpu(entry->e_value_size);
  168. if (buffer && value_len > buffer_size) {
  169. error = -ERANGE;
  170. goto cleanup;
  171. }
  172. if (buffer) {
  173. char *pval = entry->e_name + entry->e_name_len;
  174. memcpy(buffer, pval, value_len);
  175. }
  176. error = value_len;
  177. cleanup:
  178. f2fs_put_page(page, 1);
  179. return error;
  180. }
  181. ssize_t f2fs_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
  182. {
  183. struct inode *inode = dentry->d_inode;
  184. struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
  185. struct f2fs_inode_info *fi = F2FS_I(inode);
  186. struct f2fs_xattr_entry *entry;
  187. struct page *page;
  188. void *base_addr;
  189. int error = 0;
  190. size_t rest = buffer_size;
  191. if (!fi->i_xattr_nid)
  192. return 0;
  193. page = get_node_page(sbi, fi->i_xattr_nid);
  194. base_addr = page_address(page);
  195. list_for_each_xattr(entry, base_addr) {
  196. const struct xattr_handler *handler =
  197. f2fs_xattr_handler(entry->e_name_index);
  198. size_t size;
  199. if (!handler)
  200. continue;
  201. size = handler->list(dentry, buffer, rest, entry->e_name,
  202. entry->e_name_len, handler->flags);
  203. if (buffer && size > rest) {
  204. error = -ERANGE;
  205. goto cleanup;
  206. }
  207. if (buffer)
  208. buffer += size;
  209. rest -= size;
  210. }
  211. error = buffer_size - rest;
  212. cleanup:
  213. f2fs_put_page(page, 1);
  214. return error;
  215. }
  216. int f2fs_setxattr(struct inode *inode, int name_index, const char *name,
  217. const void *value, size_t value_len)
  218. {
  219. struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
  220. struct f2fs_inode_info *fi = F2FS_I(inode);
  221. struct f2fs_xattr_header *header = NULL;
  222. struct f2fs_xattr_entry *here, *last;
  223. struct page *page;
  224. void *base_addr;
  225. int error, found, free, name_len, newsize;
  226. char *pval;
  227. if (name == NULL)
  228. return -EINVAL;
  229. name_len = strlen(name);
  230. if (value == NULL)
  231. value_len = 0;
  232. if (name_len > 255 || value_len > MAX_VALUE_LEN)
  233. return -ERANGE;
  234. mutex_lock_op(sbi, NODE_NEW);
  235. if (!fi->i_xattr_nid) {
  236. /* Allocate new attribute block */
  237. struct dnode_of_data dn;
  238. if (!alloc_nid(sbi, &fi->i_xattr_nid)) {
  239. mutex_unlock_op(sbi, NODE_NEW);
  240. return -ENOSPC;
  241. }
  242. set_new_dnode(&dn, inode, NULL, NULL, fi->i_xattr_nid);
  243. mark_inode_dirty(inode);
  244. page = new_node_page(&dn, XATTR_NODE_OFFSET);
  245. if (IS_ERR(page)) {
  246. alloc_nid_failed(sbi, fi->i_xattr_nid);
  247. fi->i_xattr_nid = 0;
  248. mutex_unlock_op(sbi, NODE_NEW);
  249. return PTR_ERR(page);
  250. }
  251. alloc_nid_done(sbi, fi->i_xattr_nid);
  252. base_addr = page_address(page);
  253. header = XATTR_HDR(base_addr);
  254. header->h_magic = cpu_to_le32(F2FS_XATTR_MAGIC);
  255. header->h_refcount = cpu_to_le32(1);
  256. } else {
  257. /* The inode already has an extended attribute block. */
  258. page = get_node_page(sbi, fi->i_xattr_nid);
  259. if (IS_ERR(page)) {
  260. mutex_unlock_op(sbi, NODE_NEW);
  261. return PTR_ERR(page);
  262. }
  263. base_addr = page_address(page);
  264. header = XATTR_HDR(base_addr);
  265. }
  266. if (le32_to_cpu(header->h_magic) != F2FS_XATTR_MAGIC) {
  267. error = -EIO;
  268. goto cleanup;
  269. }
  270. /* find entry with wanted name. */
  271. found = 0;
  272. list_for_each_xattr(here, base_addr) {
  273. if (here->e_name_index != name_index)
  274. continue;
  275. if (here->e_name_len != name_len)
  276. continue;
  277. if (!memcmp(here->e_name, name, name_len)) {
  278. found = 1;
  279. break;
  280. }
  281. }
  282. last = here;
  283. while (!IS_XATTR_LAST_ENTRY(last))
  284. last = XATTR_NEXT_ENTRY(last);
  285. newsize = XATTR_ALIGN(sizeof(struct f2fs_xattr_entry) +
  286. name_len + value_len);
  287. /* 1. Check space */
  288. if (value) {
  289. /* If value is NULL, it is remove operation.
  290. * In case of update operation, we caculate free.
  291. */
  292. free = MIN_OFFSET - ((char *)last - (char *)header);
  293. if (found)
  294. free = free - ENTRY_SIZE(here);
  295. if (free < newsize) {
  296. error = -ENOSPC;
  297. goto cleanup;
  298. }
  299. }
  300. /* 2. Remove old entry */
  301. if (found) {
  302. /* If entry is found, remove old entry.
  303. * If not found, remove operation is not needed.
  304. */
  305. struct f2fs_xattr_entry *next = XATTR_NEXT_ENTRY(here);
  306. int oldsize = ENTRY_SIZE(here);
  307. memmove(here, next, (char *)last - (char *)next);
  308. last = (struct f2fs_xattr_entry *)((char *)last - oldsize);
  309. memset(last, 0, oldsize);
  310. }
  311. /* 3. Write new entry */
  312. if (value) {
  313. /* Before we come here, old entry is removed.
  314. * We just write new entry. */
  315. memset(last, 0, newsize);
  316. last->e_name_index = name_index;
  317. last->e_name_len = name_len;
  318. memcpy(last->e_name, name, name_len);
  319. pval = last->e_name + name_len;
  320. memcpy(pval, value, value_len);
  321. last->e_value_size = cpu_to_le16(value_len);
  322. }
  323. set_page_dirty(page);
  324. f2fs_put_page(page, 1);
  325. if (is_inode_flag_set(fi, FI_ACL_MODE)) {
  326. inode->i_mode = fi->i_acl_mode;
  327. inode->i_ctime = CURRENT_TIME;
  328. clear_inode_flag(fi, FI_ACL_MODE);
  329. }
  330. f2fs_write_inode(inode, NULL);
  331. mutex_unlock_op(sbi, NODE_NEW);
  332. return 0;
  333. cleanup:
  334. f2fs_put_page(page, 1);
  335. mutex_unlock_op(sbi, NODE_NEW);
  336. return error;
  337. }