xattr.c 15 KB

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