xattr.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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. static size_t f2fs_xattr_advise_list(struct dentry *dentry, char *list,
  97. size_t list_size, const char *name, size_t name_len, int type)
  98. {
  99. const char *xname = F2FS_SYSTEM_ADVISE_PREFIX;
  100. size_t size;
  101. if (type != F2FS_XATTR_INDEX_ADVISE)
  102. return 0;
  103. size = strlen(xname) + 1;
  104. if (list && size <= list_size)
  105. memcpy(list, xname, size);
  106. return size;
  107. }
  108. static int f2fs_xattr_advise_get(struct dentry *dentry, const char *name,
  109. void *buffer, size_t size, int type)
  110. {
  111. struct inode *inode = dentry->d_inode;
  112. if (strcmp(name, "") != 0)
  113. return -EINVAL;
  114. *((char *)buffer) = F2FS_I(inode)->i_advise;
  115. return sizeof(char);
  116. }
  117. static int f2fs_xattr_advise_set(struct dentry *dentry, const char *name,
  118. const void *value, size_t size, int flags, int type)
  119. {
  120. struct inode *inode = dentry->d_inode;
  121. if (strcmp(name, "") != 0)
  122. return -EINVAL;
  123. if (!inode_owner_or_capable(inode))
  124. return -EPERM;
  125. if (value == NULL)
  126. return -EINVAL;
  127. F2FS_I(inode)->i_advise |= *(char *)value;
  128. return 0;
  129. }
  130. const struct xattr_handler f2fs_xattr_user_handler = {
  131. .prefix = XATTR_USER_PREFIX,
  132. .flags = F2FS_XATTR_INDEX_USER,
  133. .list = f2fs_xattr_generic_list,
  134. .get = f2fs_xattr_generic_get,
  135. .set = f2fs_xattr_generic_set,
  136. };
  137. const struct xattr_handler f2fs_xattr_trusted_handler = {
  138. .prefix = XATTR_TRUSTED_PREFIX,
  139. .flags = F2FS_XATTR_INDEX_TRUSTED,
  140. .list = f2fs_xattr_generic_list,
  141. .get = f2fs_xattr_generic_get,
  142. .set = f2fs_xattr_generic_set,
  143. };
  144. const struct xattr_handler f2fs_xattr_advise_handler = {
  145. .prefix = F2FS_SYSTEM_ADVISE_PREFIX,
  146. .flags = F2FS_XATTR_INDEX_ADVISE,
  147. .list = f2fs_xattr_advise_list,
  148. .get = f2fs_xattr_advise_get,
  149. .set = f2fs_xattr_advise_set,
  150. };
  151. static const struct xattr_handler *f2fs_xattr_handler_map[] = {
  152. [F2FS_XATTR_INDEX_USER] = &f2fs_xattr_user_handler,
  153. #ifdef CONFIG_F2FS_FS_POSIX_ACL
  154. [F2FS_XATTR_INDEX_POSIX_ACL_ACCESS] = &f2fs_xattr_acl_access_handler,
  155. [F2FS_XATTR_INDEX_POSIX_ACL_DEFAULT] = &f2fs_xattr_acl_default_handler,
  156. #endif
  157. [F2FS_XATTR_INDEX_TRUSTED] = &f2fs_xattr_trusted_handler,
  158. [F2FS_XATTR_INDEX_ADVISE] = &f2fs_xattr_advise_handler,
  159. };
  160. const struct xattr_handler *f2fs_xattr_handlers[] = {
  161. &f2fs_xattr_user_handler,
  162. #ifdef CONFIG_F2FS_FS_POSIX_ACL
  163. &f2fs_xattr_acl_access_handler,
  164. &f2fs_xattr_acl_default_handler,
  165. #endif
  166. &f2fs_xattr_trusted_handler,
  167. &f2fs_xattr_advise_handler,
  168. NULL,
  169. };
  170. static inline const struct xattr_handler *f2fs_xattr_handler(int name_index)
  171. {
  172. const struct xattr_handler *handler = NULL;
  173. if (name_index > 0 && name_index < ARRAY_SIZE(f2fs_xattr_handler_map))
  174. handler = f2fs_xattr_handler_map[name_index];
  175. return handler;
  176. }
  177. int f2fs_getxattr(struct inode *inode, int name_index, const char *name,
  178. void *buffer, size_t buffer_size)
  179. {
  180. struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
  181. struct f2fs_inode_info *fi = F2FS_I(inode);
  182. struct f2fs_xattr_entry *entry;
  183. struct page *page;
  184. void *base_addr;
  185. int error = 0, found = 0;
  186. size_t value_len, name_len;
  187. if (name == NULL)
  188. return -EINVAL;
  189. name_len = strlen(name);
  190. if (!fi->i_xattr_nid)
  191. return -ENODATA;
  192. page = get_node_page(sbi, fi->i_xattr_nid);
  193. base_addr = page_address(page);
  194. list_for_each_xattr(entry, base_addr) {
  195. if (entry->e_name_index != name_index)
  196. continue;
  197. if (entry->e_name_len != name_len)
  198. continue;
  199. if (!memcmp(entry->e_name, name, name_len)) {
  200. found = 1;
  201. break;
  202. }
  203. }
  204. if (!found) {
  205. error = -ENODATA;
  206. goto cleanup;
  207. }
  208. value_len = le16_to_cpu(entry->e_value_size);
  209. if (buffer && value_len > buffer_size) {
  210. error = -ERANGE;
  211. goto cleanup;
  212. }
  213. if (buffer) {
  214. char *pval = entry->e_name + entry->e_name_len;
  215. memcpy(buffer, pval, value_len);
  216. }
  217. error = value_len;
  218. cleanup:
  219. f2fs_put_page(page, 1);
  220. return error;
  221. }
  222. ssize_t f2fs_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
  223. {
  224. struct inode *inode = dentry->d_inode;
  225. struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
  226. struct f2fs_inode_info *fi = F2FS_I(inode);
  227. struct f2fs_xattr_entry *entry;
  228. struct page *page;
  229. void *base_addr;
  230. int error = 0;
  231. size_t rest = buffer_size;
  232. if (!fi->i_xattr_nid)
  233. return 0;
  234. page = get_node_page(sbi, fi->i_xattr_nid);
  235. base_addr = page_address(page);
  236. list_for_each_xattr(entry, base_addr) {
  237. const struct xattr_handler *handler =
  238. f2fs_xattr_handler(entry->e_name_index);
  239. size_t size;
  240. if (!handler)
  241. continue;
  242. size = handler->list(dentry, buffer, rest, entry->e_name,
  243. entry->e_name_len, handler->flags);
  244. if (buffer && size > rest) {
  245. error = -ERANGE;
  246. goto cleanup;
  247. }
  248. if (buffer)
  249. buffer += size;
  250. rest -= size;
  251. }
  252. error = buffer_size - rest;
  253. cleanup:
  254. f2fs_put_page(page, 1);
  255. return error;
  256. }
  257. int f2fs_setxattr(struct inode *inode, int name_index, const char *name,
  258. const void *value, size_t value_len)
  259. {
  260. struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
  261. struct f2fs_inode_info *fi = F2FS_I(inode);
  262. struct f2fs_xattr_header *header = NULL;
  263. struct f2fs_xattr_entry *here, *last;
  264. struct page *page;
  265. void *base_addr;
  266. int error, found, free, newsize;
  267. size_t name_len;
  268. char *pval;
  269. if (name == NULL)
  270. return -EINVAL;
  271. name_len = strlen(name);
  272. if (value == NULL)
  273. value_len = 0;
  274. if (name_len > 255 || value_len > MAX_VALUE_LEN)
  275. return -ERANGE;
  276. f2fs_balance_fs(sbi);
  277. mutex_lock_op(sbi, NODE_NEW);
  278. if (!fi->i_xattr_nid) {
  279. /* Allocate new attribute block */
  280. struct dnode_of_data dn;
  281. if (!alloc_nid(sbi, &fi->i_xattr_nid)) {
  282. mutex_unlock_op(sbi, NODE_NEW);
  283. return -ENOSPC;
  284. }
  285. set_new_dnode(&dn, inode, NULL, NULL, fi->i_xattr_nid);
  286. mark_inode_dirty(inode);
  287. page = new_node_page(&dn, XATTR_NODE_OFFSET);
  288. if (IS_ERR(page)) {
  289. alloc_nid_failed(sbi, fi->i_xattr_nid);
  290. fi->i_xattr_nid = 0;
  291. mutex_unlock_op(sbi, NODE_NEW);
  292. return PTR_ERR(page);
  293. }
  294. alloc_nid_done(sbi, fi->i_xattr_nid);
  295. base_addr = page_address(page);
  296. header = XATTR_HDR(base_addr);
  297. header->h_magic = cpu_to_le32(F2FS_XATTR_MAGIC);
  298. header->h_refcount = cpu_to_le32(1);
  299. } else {
  300. /* The inode already has an extended attribute block. */
  301. page = get_node_page(sbi, fi->i_xattr_nid);
  302. if (IS_ERR(page)) {
  303. mutex_unlock_op(sbi, NODE_NEW);
  304. return PTR_ERR(page);
  305. }
  306. base_addr = page_address(page);
  307. header = XATTR_HDR(base_addr);
  308. }
  309. if (le32_to_cpu(header->h_magic) != F2FS_XATTR_MAGIC) {
  310. error = -EIO;
  311. goto cleanup;
  312. }
  313. /* find entry with wanted name. */
  314. found = 0;
  315. list_for_each_xattr(here, base_addr) {
  316. if (here->e_name_index != name_index)
  317. continue;
  318. if (here->e_name_len != name_len)
  319. continue;
  320. if (!memcmp(here->e_name, name, name_len)) {
  321. found = 1;
  322. break;
  323. }
  324. }
  325. last = here;
  326. while (!IS_XATTR_LAST_ENTRY(last))
  327. last = XATTR_NEXT_ENTRY(last);
  328. newsize = XATTR_ALIGN(sizeof(struct f2fs_xattr_entry) +
  329. name_len + value_len);
  330. /* 1. Check space */
  331. if (value) {
  332. /* If value is NULL, it is remove operation.
  333. * In case of update operation, we caculate free.
  334. */
  335. free = MIN_OFFSET - ((char *)last - (char *)header);
  336. if (found)
  337. free = free - ENTRY_SIZE(here);
  338. if (free < newsize) {
  339. error = -ENOSPC;
  340. goto cleanup;
  341. }
  342. }
  343. /* 2. Remove old entry */
  344. if (found) {
  345. /* If entry is found, remove old entry.
  346. * If not found, remove operation is not needed.
  347. */
  348. struct f2fs_xattr_entry *next = XATTR_NEXT_ENTRY(here);
  349. int oldsize = ENTRY_SIZE(here);
  350. memmove(here, next, (char *)last - (char *)next);
  351. last = (struct f2fs_xattr_entry *)((char *)last - oldsize);
  352. memset(last, 0, oldsize);
  353. }
  354. /* 3. Write new entry */
  355. if (value) {
  356. /* Before we come here, old entry is removed.
  357. * We just write new entry. */
  358. memset(last, 0, newsize);
  359. last->e_name_index = name_index;
  360. last->e_name_len = name_len;
  361. memcpy(last->e_name, name, name_len);
  362. pval = last->e_name + name_len;
  363. memcpy(pval, value, value_len);
  364. last->e_value_size = cpu_to_le16(value_len);
  365. }
  366. set_page_dirty(page);
  367. f2fs_put_page(page, 1);
  368. if (is_inode_flag_set(fi, FI_ACL_MODE)) {
  369. inode->i_mode = fi->i_acl_mode;
  370. inode->i_ctime = CURRENT_TIME;
  371. clear_inode_flag(fi, FI_ACL_MODE);
  372. }
  373. f2fs_write_inode(inode, NULL);
  374. mutex_unlock_op(sbi, NODE_NEW);
  375. return 0;
  376. cleanup:
  377. f2fs_put_page(page, 1);
  378. mutex_unlock_op(sbi, NODE_NEW);
  379. return error;
  380. }