xattr.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  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 <linux/security.h>
  24. #include <linux/posix_acl_xattr.h>
  25. #include "f2fs.h"
  26. #include "xattr.h"
  27. static int f2fs_xattr_generic_get(const struct xattr_handler *handler,
  28. struct dentry *unused, struct inode *inode,
  29. const char *name, void *buffer, size_t size)
  30. {
  31. struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
  32. switch (handler->flags) {
  33. case F2FS_XATTR_INDEX_USER:
  34. if (!test_opt(sbi, XATTR_USER))
  35. return -EOPNOTSUPP;
  36. break;
  37. case F2FS_XATTR_INDEX_TRUSTED:
  38. if (!capable(CAP_SYS_ADMIN))
  39. return -EPERM;
  40. break;
  41. case F2FS_XATTR_INDEX_SECURITY:
  42. break;
  43. default:
  44. return -EINVAL;
  45. }
  46. return f2fs_getxattr(inode, handler->flags, name,
  47. buffer, size, NULL);
  48. }
  49. static int f2fs_xattr_generic_set(const struct xattr_handler *handler,
  50. struct dentry *unused, struct inode *inode,
  51. const char *name, const void *value,
  52. size_t size, int flags)
  53. {
  54. struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
  55. switch (handler->flags) {
  56. case F2FS_XATTR_INDEX_USER:
  57. if (!test_opt(sbi, XATTR_USER))
  58. return -EOPNOTSUPP;
  59. break;
  60. case F2FS_XATTR_INDEX_TRUSTED:
  61. if (!capable(CAP_SYS_ADMIN))
  62. return -EPERM;
  63. break;
  64. case F2FS_XATTR_INDEX_SECURITY:
  65. break;
  66. default:
  67. return -EINVAL;
  68. }
  69. return f2fs_setxattr(inode, handler->flags, name,
  70. value, size, NULL, flags);
  71. }
  72. static bool f2fs_xattr_user_list(struct dentry *dentry)
  73. {
  74. struct f2fs_sb_info *sbi = F2FS_SB(dentry->d_sb);
  75. return test_opt(sbi, XATTR_USER);
  76. }
  77. static bool f2fs_xattr_trusted_list(struct dentry *dentry)
  78. {
  79. return capable(CAP_SYS_ADMIN);
  80. }
  81. static int f2fs_xattr_advise_get(const struct xattr_handler *handler,
  82. struct dentry *unused, struct inode *inode,
  83. const char *name, void *buffer, size_t size)
  84. {
  85. if (buffer)
  86. *((char *)buffer) = F2FS_I(inode)->i_advise;
  87. return sizeof(char);
  88. }
  89. static int f2fs_xattr_advise_set(const struct xattr_handler *handler,
  90. struct dentry *unused, struct inode *inode,
  91. const char *name, const void *value,
  92. size_t size, int flags)
  93. {
  94. if (!inode_owner_or_capable(inode))
  95. return -EPERM;
  96. if (value == NULL)
  97. return -EINVAL;
  98. F2FS_I(inode)->i_advise |= *(char *)value;
  99. f2fs_mark_inode_dirty_sync(inode);
  100. return 0;
  101. }
  102. #ifdef CONFIG_F2FS_FS_SECURITY
  103. static int f2fs_initxattrs(struct inode *inode, const struct xattr *xattr_array,
  104. void *page)
  105. {
  106. const struct xattr *xattr;
  107. int err = 0;
  108. for (xattr = xattr_array; xattr->name != NULL; xattr++) {
  109. err = f2fs_setxattr(inode, F2FS_XATTR_INDEX_SECURITY,
  110. xattr->name, xattr->value,
  111. xattr->value_len, (struct page *)page, 0);
  112. if (err < 0)
  113. break;
  114. }
  115. return err;
  116. }
  117. int f2fs_init_security(struct inode *inode, struct inode *dir,
  118. const struct qstr *qstr, struct page *ipage)
  119. {
  120. return security_inode_init_security(inode, dir, qstr,
  121. &f2fs_initxattrs, ipage);
  122. }
  123. #endif
  124. const struct xattr_handler f2fs_xattr_user_handler = {
  125. .prefix = XATTR_USER_PREFIX,
  126. .flags = F2FS_XATTR_INDEX_USER,
  127. .list = f2fs_xattr_user_list,
  128. .get = f2fs_xattr_generic_get,
  129. .set = f2fs_xattr_generic_set,
  130. };
  131. const struct xattr_handler f2fs_xattr_trusted_handler = {
  132. .prefix = XATTR_TRUSTED_PREFIX,
  133. .flags = F2FS_XATTR_INDEX_TRUSTED,
  134. .list = f2fs_xattr_trusted_list,
  135. .get = f2fs_xattr_generic_get,
  136. .set = f2fs_xattr_generic_set,
  137. };
  138. const struct xattr_handler f2fs_xattr_advise_handler = {
  139. .name = F2FS_SYSTEM_ADVISE_NAME,
  140. .flags = F2FS_XATTR_INDEX_ADVISE,
  141. .get = f2fs_xattr_advise_get,
  142. .set = f2fs_xattr_advise_set,
  143. };
  144. const struct xattr_handler f2fs_xattr_security_handler = {
  145. .prefix = XATTR_SECURITY_PREFIX,
  146. .flags = F2FS_XATTR_INDEX_SECURITY,
  147. .get = f2fs_xattr_generic_get,
  148. .set = f2fs_xattr_generic_set,
  149. };
  150. static const struct xattr_handler *f2fs_xattr_handler_map[] = {
  151. [F2FS_XATTR_INDEX_USER] = &f2fs_xattr_user_handler,
  152. #ifdef CONFIG_F2FS_FS_POSIX_ACL
  153. [F2FS_XATTR_INDEX_POSIX_ACL_ACCESS] = &posix_acl_access_xattr_handler,
  154. [F2FS_XATTR_INDEX_POSIX_ACL_DEFAULT] = &posix_acl_default_xattr_handler,
  155. #endif
  156. [F2FS_XATTR_INDEX_TRUSTED] = &f2fs_xattr_trusted_handler,
  157. #ifdef CONFIG_F2FS_FS_SECURITY
  158. [F2FS_XATTR_INDEX_SECURITY] = &f2fs_xattr_security_handler,
  159. #endif
  160. [F2FS_XATTR_INDEX_ADVISE] = &f2fs_xattr_advise_handler,
  161. };
  162. const struct xattr_handler *f2fs_xattr_handlers[] = {
  163. &f2fs_xattr_user_handler,
  164. #ifdef CONFIG_F2FS_FS_POSIX_ACL
  165. &posix_acl_access_xattr_handler,
  166. &posix_acl_default_xattr_handler,
  167. #endif
  168. &f2fs_xattr_trusted_handler,
  169. #ifdef CONFIG_F2FS_FS_SECURITY
  170. &f2fs_xattr_security_handler,
  171. #endif
  172. &f2fs_xattr_advise_handler,
  173. NULL,
  174. };
  175. static inline const struct xattr_handler *f2fs_xattr_handler(int index)
  176. {
  177. const struct xattr_handler *handler = NULL;
  178. if (index > 0 && index < ARRAY_SIZE(f2fs_xattr_handler_map))
  179. handler = f2fs_xattr_handler_map[index];
  180. return handler;
  181. }
  182. static struct f2fs_xattr_entry *__find_xattr(void *base_addr, int index,
  183. size_t len, const char *name)
  184. {
  185. struct f2fs_xattr_entry *entry;
  186. list_for_each_xattr(entry, base_addr) {
  187. if (entry->e_name_index != index)
  188. continue;
  189. if (entry->e_name_len != len)
  190. continue;
  191. if (!memcmp(entry->e_name, name, len))
  192. break;
  193. }
  194. return entry;
  195. }
  196. static int read_all_xattrs(struct inode *inode, struct page *ipage,
  197. void **base_addr)
  198. {
  199. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  200. struct f2fs_xattr_header *header;
  201. size_t size = PAGE_SIZE, inline_size = 0;
  202. void *txattr_addr;
  203. int err;
  204. inline_size = inline_xattr_size(inode);
  205. txattr_addr = kzalloc(inline_size + size, GFP_F2FS_ZERO);
  206. if (!txattr_addr)
  207. return -ENOMEM;
  208. /* read from inline xattr */
  209. if (inline_size) {
  210. struct page *page = NULL;
  211. void *inline_addr;
  212. if (ipage) {
  213. inline_addr = inline_xattr_addr(ipage);
  214. } else {
  215. page = get_node_page(sbi, inode->i_ino);
  216. if (IS_ERR(page)) {
  217. err = PTR_ERR(page);
  218. goto fail;
  219. }
  220. inline_addr = inline_xattr_addr(page);
  221. }
  222. memcpy(txattr_addr, inline_addr, inline_size);
  223. f2fs_put_page(page, 1);
  224. }
  225. /* read from xattr node block */
  226. if (F2FS_I(inode)->i_xattr_nid) {
  227. struct page *xpage;
  228. void *xattr_addr;
  229. /* The inode already has an extended attribute block. */
  230. xpage = get_node_page(sbi, F2FS_I(inode)->i_xattr_nid);
  231. if (IS_ERR(xpage)) {
  232. err = PTR_ERR(xpage);
  233. goto fail;
  234. }
  235. xattr_addr = page_address(xpage);
  236. memcpy(txattr_addr + inline_size, xattr_addr, PAGE_SIZE);
  237. f2fs_put_page(xpage, 1);
  238. }
  239. header = XATTR_HDR(txattr_addr);
  240. /* never been allocated xattrs */
  241. if (le32_to_cpu(header->h_magic) != F2FS_XATTR_MAGIC) {
  242. header->h_magic = cpu_to_le32(F2FS_XATTR_MAGIC);
  243. header->h_refcount = cpu_to_le32(1);
  244. }
  245. *base_addr = txattr_addr;
  246. return 0;
  247. fail:
  248. kzfree(txattr_addr);
  249. return err;
  250. }
  251. static inline int write_all_xattrs(struct inode *inode, __u32 hsize,
  252. void *txattr_addr, struct page *ipage)
  253. {
  254. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  255. size_t inline_size = 0;
  256. void *xattr_addr;
  257. struct page *xpage;
  258. nid_t new_nid = 0;
  259. int err;
  260. inline_size = inline_xattr_size(inode);
  261. if (hsize > inline_size && !F2FS_I(inode)->i_xattr_nid)
  262. if (!alloc_nid(sbi, &new_nid))
  263. return -ENOSPC;
  264. /* write to inline xattr */
  265. if (inline_size) {
  266. struct page *page = NULL;
  267. void *inline_addr;
  268. if (ipage) {
  269. inline_addr = inline_xattr_addr(ipage);
  270. f2fs_wait_on_page_writeback(ipage, NODE, true);
  271. set_page_dirty(ipage);
  272. } else {
  273. page = get_node_page(sbi, inode->i_ino);
  274. if (IS_ERR(page)) {
  275. alloc_nid_failed(sbi, new_nid);
  276. return PTR_ERR(page);
  277. }
  278. inline_addr = inline_xattr_addr(page);
  279. f2fs_wait_on_page_writeback(page, NODE, true);
  280. }
  281. memcpy(inline_addr, txattr_addr, inline_size);
  282. f2fs_put_page(page, 1);
  283. /* no need to use xattr node block */
  284. if (hsize <= inline_size) {
  285. err = truncate_xattr_node(inode, ipage);
  286. alloc_nid_failed(sbi, new_nid);
  287. return err;
  288. }
  289. }
  290. /* write to xattr node block */
  291. if (F2FS_I(inode)->i_xattr_nid) {
  292. xpage = get_node_page(sbi, F2FS_I(inode)->i_xattr_nid);
  293. if (IS_ERR(xpage)) {
  294. alloc_nid_failed(sbi, new_nid);
  295. return PTR_ERR(xpage);
  296. }
  297. f2fs_bug_on(sbi, new_nid);
  298. f2fs_wait_on_page_writeback(xpage, NODE, true);
  299. } else {
  300. struct dnode_of_data dn;
  301. set_new_dnode(&dn, inode, NULL, NULL, new_nid);
  302. xpage = new_node_page(&dn, XATTR_NODE_OFFSET, ipage);
  303. if (IS_ERR(xpage)) {
  304. alloc_nid_failed(sbi, new_nid);
  305. return PTR_ERR(xpage);
  306. }
  307. alloc_nid_done(sbi, new_nid);
  308. }
  309. xattr_addr = page_address(xpage);
  310. memcpy(xattr_addr, txattr_addr + inline_size, PAGE_SIZE -
  311. sizeof(struct node_footer));
  312. set_page_dirty(xpage);
  313. f2fs_put_page(xpage, 1);
  314. /* need to checkpoint during fsync */
  315. F2FS_I(inode)->xattr_ver = cur_cp_version(F2FS_CKPT(sbi));
  316. return 0;
  317. }
  318. int f2fs_getxattr(struct inode *inode, int index, const char *name,
  319. void *buffer, size_t buffer_size, struct page *ipage)
  320. {
  321. struct f2fs_xattr_entry *entry;
  322. void *base_addr;
  323. int error = 0;
  324. size_t size, len;
  325. if (name == NULL)
  326. return -EINVAL;
  327. len = strlen(name);
  328. if (len > F2FS_NAME_LEN)
  329. return -ERANGE;
  330. error = read_all_xattrs(inode, ipage, &base_addr);
  331. if (error)
  332. return error;
  333. entry = __find_xattr(base_addr, index, len, name);
  334. if (IS_XATTR_LAST_ENTRY(entry)) {
  335. error = -ENODATA;
  336. goto cleanup;
  337. }
  338. size = le16_to_cpu(entry->e_value_size);
  339. if (buffer && size > buffer_size) {
  340. error = -ERANGE;
  341. goto cleanup;
  342. }
  343. if (buffer) {
  344. char *pval = entry->e_name + entry->e_name_len;
  345. memcpy(buffer, pval, size);
  346. }
  347. error = size;
  348. cleanup:
  349. kzfree(base_addr);
  350. return error;
  351. }
  352. ssize_t f2fs_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
  353. {
  354. struct inode *inode = d_inode(dentry);
  355. struct f2fs_xattr_entry *entry;
  356. void *base_addr;
  357. int error = 0;
  358. size_t rest = buffer_size;
  359. error = read_all_xattrs(inode, NULL, &base_addr);
  360. if (error)
  361. return error;
  362. list_for_each_xattr(entry, base_addr) {
  363. const struct xattr_handler *handler =
  364. f2fs_xattr_handler(entry->e_name_index);
  365. const char *prefix;
  366. size_t prefix_len;
  367. size_t size;
  368. if (!handler || (handler->list && !handler->list(dentry)))
  369. continue;
  370. prefix = handler->prefix ?: handler->name;
  371. prefix_len = strlen(prefix);
  372. size = prefix_len + entry->e_name_len + 1;
  373. if (buffer) {
  374. if (size > rest) {
  375. error = -ERANGE;
  376. goto cleanup;
  377. }
  378. memcpy(buffer, prefix, prefix_len);
  379. buffer += prefix_len;
  380. memcpy(buffer, entry->e_name, entry->e_name_len);
  381. buffer += entry->e_name_len;
  382. *buffer++ = 0;
  383. }
  384. rest -= size;
  385. }
  386. error = buffer_size - rest;
  387. cleanup:
  388. kzfree(base_addr);
  389. return error;
  390. }
  391. static int __f2fs_setxattr(struct inode *inode, int index,
  392. const char *name, const void *value, size_t size,
  393. struct page *ipage, int flags)
  394. {
  395. struct f2fs_xattr_entry *here, *last;
  396. void *base_addr;
  397. int found, newsize;
  398. size_t len;
  399. __u32 new_hsize;
  400. int error = 0;
  401. if (name == NULL)
  402. return -EINVAL;
  403. if (value == NULL)
  404. size = 0;
  405. len = strlen(name);
  406. if (len > F2FS_NAME_LEN)
  407. return -ERANGE;
  408. if (size > MAX_VALUE_LEN(inode))
  409. return -E2BIG;
  410. error = read_all_xattrs(inode, ipage, &base_addr);
  411. if (error)
  412. return error;
  413. /* find entry with wanted name. */
  414. here = __find_xattr(base_addr, index, len, name);
  415. found = IS_XATTR_LAST_ENTRY(here) ? 0 : 1;
  416. if ((flags & XATTR_REPLACE) && !found) {
  417. error = -ENODATA;
  418. goto exit;
  419. } else if ((flags & XATTR_CREATE) && found) {
  420. error = -EEXIST;
  421. goto exit;
  422. }
  423. last = here;
  424. while (!IS_XATTR_LAST_ENTRY(last))
  425. last = XATTR_NEXT_ENTRY(last);
  426. newsize = XATTR_ALIGN(sizeof(struct f2fs_xattr_entry) + len + size);
  427. /* 1. Check space */
  428. if (value) {
  429. int free;
  430. /*
  431. * If value is NULL, it is remove operation.
  432. * In case of update operation, we calculate free.
  433. */
  434. free = MIN_OFFSET(inode) - ((char *)last - (char *)base_addr);
  435. if (found)
  436. free = free + ENTRY_SIZE(here);
  437. if (unlikely(free < newsize)) {
  438. error = -E2BIG;
  439. goto exit;
  440. }
  441. }
  442. /* 2. Remove old entry */
  443. if (found) {
  444. /*
  445. * If entry is found, remove old entry.
  446. * If not found, remove operation is not needed.
  447. */
  448. struct f2fs_xattr_entry *next = XATTR_NEXT_ENTRY(here);
  449. int oldsize = ENTRY_SIZE(here);
  450. memmove(here, next, (char *)last - (char *)next);
  451. last = (struct f2fs_xattr_entry *)((char *)last - oldsize);
  452. memset(last, 0, oldsize);
  453. }
  454. new_hsize = (char *)last - (char *)base_addr;
  455. /* 3. Write new entry */
  456. if (value) {
  457. char *pval;
  458. /*
  459. * Before we come here, old entry is removed.
  460. * We just write new entry.
  461. */
  462. last->e_name_index = index;
  463. last->e_name_len = len;
  464. memcpy(last->e_name, name, len);
  465. pval = last->e_name + len;
  466. memcpy(pval, value, size);
  467. last->e_value_size = cpu_to_le16(size);
  468. new_hsize += newsize;
  469. }
  470. error = write_all_xattrs(inode, new_hsize, base_addr, ipage);
  471. if (error)
  472. goto exit;
  473. if (is_inode_flag_set(inode, FI_ACL_MODE)) {
  474. inode->i_mode = F2FS_I(inode)->i_acl_mode;
  475. inode->i_ctime = current_time(inode);
  476. clear_inode_flag(inode, FI_ACL_MODE);
  477. }
  478. if (index == F2FS_XATTR_INDEX_ENCRYPTION &&
  479. !strcmp(name, F2FS_XATTR_NAME_ENCRYPTION_CONTEXT))
  480. f2fs_set_encrypted_inode(inode);
  481. f2fs_mark_inode_dirty_sync(inode);
  482. if (!error && S_ISDIR(inode->i_mode))
  483. set_sbi_flag(F2FS_I_SB(inode), SBI_NEED_CP);
  484. exit:
  485. kzfree(base_addr);
  486. return error;
  487. }
  488. int f2fs_setxattr(struct inode *inode, int index, const char *name,
  489. const void *value, size_t size,
  490. struct page *ipage, int flags)
  491. {
  492. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  493. int err;
  494. /* this case is only from init_inode_metadata */
  495. if (ipage)
  496. return __f2fs_setxattr(inode, index, name, value,
  497. size, ipage, flags);
  498. f2fs_balance_fs(sbi, true);
  499. f2fs_lock_op(sbi);
  500. /* protect xattr_ver */
  501. down_write(&F2FS_I(inode)->i_sem);
  502. err = __f2fs_setxattr(inode, index, name, value, size, ipage, flags);
  503. up_write(&F2FS_I(inode)->i_sem);
  504. f2fs_unlock_op(sbi);
  505. f2fs_update_time(sbi, REQ_TIME);
  506. return err;
  507. }