xattr.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * fs/f2fs/xattr.c
  4. *
  5. * Copyright (c) 2012 Samsung Electronics Co., Ltd.
  6. * http://www.samsung.com/
  7. *
  8. * Portions of this code from linux/fs/ext2/xattr.c
  9. *
  10. * Copyright (C) 2001-2003 Andreas Gruenbacher <[email protected]>
  11. *
  12. * Fix by Harrison Xing <[email protected]>.
  13. * Extended attributes for symlinks and special files added per
  14. * suggestion of Luka Renko <[email protected]>.
  15. * xattr consolidation Copyright (c) 2004 James Morris <[email protected]>,
  16. * Red Hat Inc.
  17. */
  18. #include <linux/rwsem.h>
  19. #include <linux/f2fs_fs.h>
  20. #include <linux/security.h>
  21. #include <linux/posix_acl_xattr.h>
  22. #include "f2fs.h"
  23. #include "xattr.h"
  24. #include "segment.h"
  25. static void *xattr_alloc(struct f2fs_sb_info *sbi, int size, bool *is_inline)
  26. {
  27. if (likely(size == sbi->inline_xattr_slab_size)) {
  28. *is_inline = true;
  29. return f2fs_kmem_cache_alloc(sbi->inline_xattr_slab,
  30. GFP_F2FS_ZERO, false, sbi);
  31. }
  32. *is_inline = false;
  33. return f2fs_kzalloc(sbi, size, GFP_NOFS);
  34. }
  35. static void xattr_free(struct f2fs_sb_info *sbi, void *xattr_addr,
  36. bool is_inline)
  37. {
  38. if (is_inline)
  39. kmem_cache_free(sbi->inline_xattr_slab, xattr_addr);
  40. else
  41. kfree(xattr_addr);
  42. }
  43. static int f2fs_xattr_generic_get(const struct xattr_handler *handler,
  44. struct dentry *unused, struct inode *inode,
  45. const char *name, void *buffer, size_t size)
  46. {
  47. struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
  48. switch (handler->flags) {
  49. case F2FS_XATTR_INDEX_USER:
  50. if (!test_opt(sbi, XATTR_USER))
  51. return -EOPNOTSUPP;
  52. break;
  53. case F2FS_XATTR_INDEX_TRUSTED:
  54. case F2FS_XATTR_INDEX_SECURITY:
  55. break;
  56. default:
  57. return -EINVAL;
  58. }
  59. return f2fs_getxattr(inode, handler->flags, name,
  60. buffer, size, NULL);
  61. }
  62. static int f2fs_xattr_generic_set(const struct xattr_handler *handler,
  63. struct user_namespace *mnt_userns,
  64. struct dentry *unused, struct inode *inode,
  65. const char *name, const void *value,
  66. size_t size, int flags)
  67. {
  68. struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
  69. switch (handler->flags) {
  70. case F2FS_XATTR_INDEX_USER:
  71. if (!test_opt(sbi, XATTR_USER))
  72. return -EOPNOTSUPP;
  73. break;
  74. case F2FS_XATTR_INDEX_TRUSTED:
  75. case F2FS_XATTR_INDEX_SECURITY:
  76. break;
  77. default:
  78. return -EINVAL;
  79. }
  80. return f2fs_setxattr(inode, handler->flags, name,
  81. value, size, NULL, flags);
  82. }
  83. static bool f2fs_xattr_user_list(struct dentry *dentry)
  84. {
  85. struct f2fs_sb_info *sbi = F2FS_SB(dentry->d_sb);
  86. return test_opt(sbi, XATTR_USER);
  87. }
  88. static bool f2fs_xattr_trusted_list(struct dentry *dentry)
  89. {
  90. return capable(CAP_SYS_ADMIN);
  91. }
  92. static int f2fs_xattr_advise_get(const struct xattr_handler *handler,
  93. struct dentry *unused, struct inode *inode,
  94. const char *name, void *buffer, size_t size)
  95. {
  96. if (buffer)
  97. *((char *)buffer) = F2FS_I(inode)->i_advise;
  98. return sizeof(char);
  99. }
  100. static int f2fs_xattr_advise_set(const struct xattr_handler *handler,
  101. struct user_namespace *mnt_userns,
  102. struct dentry *unused, struct inode *inode,
  103. const char *name, const void *value,
  104. size_t size, int flags)
  105. {
  106. unsigned char old_advise = F2FS_I(inode)->i_advise;
  107. unsigned char new_advise;
  108. if (!inode_owner_or_capable(&init_user_ns, inode))
  109. return -EPERM;
  110. if (value == NULL)
  111. return -EINVAL;
  112. new_advise = *(char *)value;
  113. if (new_advise & ~FADVISE_MODIFIABLE_BITS)
  114. return -EINVAL;
  115. new_advise = new_advise & FADVISE_MODIFIABLE_BITS;
  116. new_advise |= old_advise & ~FADVISE_MODIFIABLE_BITS;
  117. F2FS_I(inode)->i_advise = new_advise;
  118. f2fs_mark_inode_dirty_sync(inode, true);
  119. return 0;
  120. }
  121. #ifdef CONFIG_F2FS_FS_SECURITY
  122. static int f2fs_initxattrs(struct inode *inode, const struct xattr *xattr_array,
  123. void *page)
  124. {
  125. const struct xattr *xattr;
  126. int err = 0;
  127. for (xattr = xattr_array; xattr->name != NULL; xattr++) {
  128. err = f2fs_setxattr(inode, F2FS_XATTR_INDEX_SECURITY,
  129. xattr->name, xattr->value,
  130. xattr->value_len, (struct page *)page, 0);
  131. if (err < 0)
  132. break;
  133. }
  134. return err;
  135. }
  136. int f2fs_init_security(struct inode *inode, struct inode *dir,
  137. const struct qstr *qstr, struct page *ipage)
  138. {
  139. return security_inode_init_security(inode, dir, qstr,
  140. &f2fs_initxattrs, ipage);
  141. }
  142. #endif
  143. const struct xattr_handler f2fs_xattr_user_handler = {
  144. .prefix = XATTR_USER_PREFIX,
  145. .flags = F2FS_XATTR_INDEX_USER,
  146. .list = f2fs_xattr_user_list,
  147. .get = f2fs_xattr_generic_get,
  148. .set = f2fs_xattr_generic_set,
  149. };
  150. const struct xattr_handler f2fs_xattr_trusted_handler = {
  151. .prefix = XATTR_TRUSTED_PREFIX,
  152. .flags = F2FS_XATTR_INDEX_TRUSTED,
  153. .list = f2fs_xattr_trusted_list,
  154. .get = f2fs_xattr_generic_get,
  155. .set = f2fs_xattr_generic_set,
  156. };
  157. const struct xattr_handler f2fs_xattr_advise_handler = {
  158. .name = F2FS_SYSTEM_ADVISE_NAME,
  159. .flags = F2FS_XATTR_INDEX_ADVISE,
  160. .get = f2fs_xattr_advise_get,
  161. .set = f2fs_xattr_advise_set,
  162. };
  163. const struct xattr_handler f2fs_xattr_security_handler = {
  164. .prefix = XATTR_SECURITY_PREFIX,
  165. .flags = F2FS_XATTR_INDEX_SECURITY,
  166. .get = f2fs_xattr_generic_get,
  167. .set = f2fs_xattr_generic_set,
  168. };
  169. static const struct xattr_handler *f2fs_xattr_handler_map[] = {
  170. [F2FS_XATTR_INDEX_USER] = &f2fs_xattr_user_handler,
  171. #ifdef CONFIG_F2FS_FS_POSIX_ACL
  172. [F2FS_XATTR_INDEX_POSIX_ACL_ACCESS] = &posix_acl_access_xattr_handler,
  173. [F2FS_XATTR_INDEX_POSIX_ACL_DEFAULT] = &posix_acl_default_xattr_handler,
  174. #endif
  175. [F2FS_XATTR_INDEX_TRUSTED] = &f2fs_xattr_trusted_handler,
  176. #ifdef CONFIG_F2FS_FS_SECURITY
  177. [F2FS_XATTR_INDEX_SECURITY] = &f2fs_xattr_security_handler,
  178. #endif
  179. [F2FS_XATTR_INDEX_ADVISE] = &f2fs_xattr_advise_handler,
  180. };
  181. const struct xattr_handler *f2fs_xattr_handlers[] = {
  182. &f2fs_xattr_user_handler,
  183. #ifdef CONFIG_F2FS_FS_POSIX_ACL
  184. &posix_acl_access_xattr_handler,
  185. &posix_acl_default_xattr_handler,
  186. #endif
  187. &f2fs_xattr_trusted_handler,
  188. #ifdef CONFIG_F2FS_FS_SECURITY
  189. &f2fs_xattr_security_handler,
  190. #endif
  191. &f2fs_xattr_advise_handler,
  192. NULL,
  193. };
  194. static inline const struct xattr_handler *f2fs_xattr_handler(int index)
  195. {
  196. const struct xattr_handler *handler = NULL;
  197. if (index > 0 && index < ARRAY_SIZE(f2fs_xattr_handler_map))
  198. handler = f2fs_xattr_handler_map[index];
  199. return handler;
  200. }
  201. static struct f2fs_xattr_entry *__find_xattr(void *base_addr,
  202. void *last_base_addr, void **last_addr,
  203. int index, size_t len, const char *name)
  204. {
  205. struct f2fs_xattr_entry *entry;
  206. list_for_each_xattr(entry, base_addr) {
  207. if ((void *)(entry) + sizeof(__u32) > last_base_addr ||
  208. (void *)XATTR_NEXT_ENTRY(entry) > last_base_addr) {
  209. if (last_addr)
  210. *last_addr = entry;
  211. return NULL;
  212. }
  213. if (entry->e_name_index != index)
  214. continue;
  215. if (entry->e_name_len != len)
  216. continue;
  217. if (!memcmp(entry->e_name, name, len))
  218. break;
  219. }
  220. return entry;
  221. }
  222. static struct f2fs_xattr_entry *__find_inline_xattr(struct inode *inode,
  223. void *base_addr, void **last_addr, int index,
  224. size_t len, const char *name)
  225. {
  226. struct f2fs_xattr_entry *entry;
  227. unsigned int inline_size = inline_xattr_size(inode);
  228. void *max_addr = base_addr + inline_size;
  229. entry = __find_xattr(base_addr, max_addr, last_addr, index, len, name);
  230. if (!entry)
  231. return NULL;
  232. /* inline xattr header or entry across max inline xattr size */
  233. if (IS_XATTR_LAST_ENTRY(entry) &&
  234. (void *)entry + sizeof(__u32) > max_addr) {
  235. *last_addr = entry;
  236. return NULL;
  237. }
  238. return entry;
  239. }
  240. static int read_inline_xattr(struct inode *inode, struct page *ipage,
  241. void *txattr_addr)
  242. {
  243. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  244. unsigned int inline_size = inline_xattr_size(inode);
  245. struct page *page = NULL;
  246. void *inline_addr;
  247. if (ipage) {
  248. inline_addr = inline_xattr_addr(inode, ipage);
  249. } else {
  250. page = f2fs_get_node_page(sbi, inode->i_ino);
  251. if (IS_ERR(page))
  252. return PTR_ERR(page);
  253. inline_addr = inline_xattr_addr(inode, page);
  254. }
  255. memcpy(txattr_addr, inline_addr, inline_size);
  256. f2fs_put_page(page, 1);
  257. return 0;
  258. }
  259. static int read_xattr_block(struct inode *inode, void *txattr_addr)
  260. {
  261. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  262. nid_t xnid = F2FS_I(inode)->i_xattr_nid;
  263. unsigned int inline_size = inline_xattr_size(inode);
  264. struct page *xpage;
  265. void *xattr_addr;
  266. /* The inode already has an extended attribute block. */
  267. xpage = f2fs_get_node_page(sbi, xnid);
  268. if (IS_ERR(xpage))
  269. return PTR_ERR(xpage);
  270. xattr_addr = page_address(xpage);
  271. memcpy(txattr_addr + inline_size, xattr_addr, VALID_XATTR_BLOCK_SIZE);
  272. f2fs_put_page(xpage, 1);
  273. return 0;
  274. }
  275. static int lookup_all_xattrs(struct inode *inode, struct page *ipage,
  276. unsigned int index, unsigned int len,
  277. const char *name, struct f2fs_xattr_entry **xe,
  278. void **base_addr, int *base_size,
  279. bool *is_inline)
  280. {
  281. void *cur_addr, *txattr_addr, *last_txattr_addr;
  282. void *last_addr = NULL;
  283. nid_t xnid = F2FS_I(inode)->i_xattr_nid;
  284. unsigned int inline_size = inline_xattr_size(inode);
  285. int err;
  286. if (!xnid && !inline_size)
  287. return -ENODATA;
  288. *base_size = XATTR_SIZE(inode) + XATTR_PADDING_SIZE;
  289. txattr_addr = xattr_alloc(F2FS_I_SB(inode), *base_size, is_inline);
  290. if (!txattr_addr)
  291. return -ENOMEM;
  292. last_txattr_addr = (void *)txattr_addr + XATTR_SIZE(inode);
  293. /* read from inline xattr */
  294. if (inline_size) {
  295. err = read_inline_xattr(inode, ipage, txattr_addr);
  296. if (err)
  297. goto out;
  298. *xe = __find_inline_xattr(inode, txattr_addr, &last_addr,
  299. index, len, name);
  300. if (*xe) {
  301. *base_size = inline_size;
  302. goto check;
  303. }
  304. }
  305. /* read from xattr node block */
  306. if (xnid) {
  307. err = read_xattr_block(inode, txattr_addr);
  308. if (err)
  309. goto out;
  310. }
  311. if (last_addr)
  312. cur_addr = XATTR_HDR(last_addr) - 1;
  313. else
  314. cur_addr = txattr_addr;
  315. *xe = __find_xattr(cur_addr, last_txattr_addr, NULL, index, len, name);
  316. if (!*xe) {
  317. f2fs_err(F2FS_I_SB(inode), "lookup inode (%lu) has corrupted xattr",
  318. inode->i_ino);
  319. set_sbi_flag(F2FS_I_SB(inode), SBI_NEED_FSCK);
  320. err = -ENODATA;
  321. f2fs_handle_error(F2FS_I_SB(inode),
  322. ERROR_CORRUPTED_XATTR);
  323. goto out;
  324. }
  325. check:
  326. if (IS_XATTR_LAST_ENTRY(*xe)) {
  327. err = -ENODATA;
  328. goto out;
  329. }
  330. *base_addr = txattr_addr;
  331. return 0;
  332. out:
  333. xattr_free(F2FS_I_SB(inode), txattr_addr, *is_inline);
  334. return err;
  335. }
  336. static int read_all_xattrs(struct inode *inode, struct page *ipage,
  337. void **base_addr)
  338. {
  339. struct f2fs_xattr_header *header;
  340. nid_t xnid = F2FS_I(inode)->i_xattr_nid;
  341. unsigned int size = VALID_XATTR_BLOCK_SIZE;
  342. unsigned int inline_size = inline_xattr_size(inode);
  343. void *txattr_addr;
  344. int err;
  345. txattr_addr = f2fs_kzalloc(F2FS_I_SB(inode),
  346. inline_size + size + XATTR_PADDING_SIZE, GFP_NOFS);
  347. if (!txattr_addr)
  348. return -ENOMEM;
  349. /* read from inline xattr */
  350. if (inline_size) {
  351. err = read_inline_xattr(inode, ipage, txattr_addr);
  352. if (err)
  353. goto fail;
  354. }
  355. /* read from xattr node block */
  356. if (xnid) {
  357. err = read_xattr_block(inode, txattr_addr);
  358. if (err)
  359. goto fail;
  360. }
  361. header = XATTR_HDR(txattr_addr);
  362. /* never been allocated xattrs */
  363. if (le32_to_cpu(header->h_magic) != F2FS_XATTR_MAGIC) {
  364. header->h_magic = cpu_to_le32(F2FS_XATTR_MAGIC);
  365. header->h_refcount = cpu_to_le32(1);
  366. }
  367. *base_addr = txattr_addr;
  368. return 0;
  369. fail:
  370. kfree(txattr_addr);
  371. return err;
  372. }
  373. static inline int write_all_xattrs(struct inode *inode, __u32 hsize,
  374. void *txattr_addr, struct page *ipage)
  375. {
  376. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  377. size_t inline_size = inline_xattr_size(inode);
  378. struct page *in_page = NULL;
  379. void *xattr_addr;
  380. void *inline_addr = NULL;
  381. struct page *xpage;
  382. nid_t new_nid = 0;
  383. int err = 0;
  384. if (hsize > inline_size && !F2FS_I(inode)->i_xattr_nid)
  385. if (!f2fs_alloc_nid(sbi, &new_nid))
  386. return -ENOSPC;
  387. /* write to inline xattr */
  388. if (inline_size) {
  389. if (ipage) {
  390. inline_addr = inline_xattr_addr(inode, ipage);
  391. } else {
  392. in_page = f2fs_get_node_page(sbi, inode->i_ino);
  393. if (IS_ERR(in_page)) {
  394. f2fs_alloc_nid_failed(sbi, new_nid);
  395. return PTR_ERR(in_page);
  396. }
  397. inline_addr = inline_xattr_addr(inode, in_page);
  398. }
  399. f2fs_wait_on_page_writeback(ipage ? ipage : in_page,
  400. NODE, true, true);
  401. /* no need to use xattr node block */
  402. if (hsize <= inline_size) {
  403. err = f2fs_truncate_xattr_node(inode);
  404. f2fs_alloc_nid_failed(sbi, new_nid);
  405. if (err) {
  406. f2fs_put_page(in_page, 1);
  407. return err;
  408. }
  409. memcpy(inline_addr, txattr_addr, inline_size);
  410. set_page_dirty(ipage ? ipage : in_page);
  411. goto in_page_out;
  412. }
  413. }
  414. /* write to xattr node block */
  415. if (F2FS_I(inode)->i_xattr_nid) {
  416. xpage = f2fs_get_node_page(sbi, F2FS_I(inode)->i_xattr_nid);
  417. if (IS_ERR(xpage)) {
  418. err = PTR_ERR(xpage);
  419. f2fs_alloc_nid_failed(sbi, new_nid);
  420. goto in_page_out;
  421. }
  422. f2fs_bug_on(sbi, new_nid);
  423. f2fs_wait_on_page_writeback(xpage, NODE, true, true);
  424. } else {
  425. struct dnode_of_data dn;
  426. set_new_dnode(&dn, inode, NULL, NULL, new_nid);
  427. xpage = f2fs_new_node_page(&dn, XATTR_NODE_OFFSET);
  428. if (IS_ERR(xpage)) {
  429. err = PTR_ERR(xpage);
  430. f2fs_alloc_nid_failed(sbi, new_nid);
  431. goto in_page_out;
  432. }
  433. f2fs_alloc_nid_done(sbi, new_nid);
  434. }
  435. xattr_addr = page_address(xpage);
  436. if (inline_size)
  437. memcpy(inline_addr, txattr_addr, inline_size);
  438. memcpy(xattr_addr, txattr_addr + inline_size, VALID_XATTR_BLOCK_SIZE);
  439. if (inline_size)
  440. set_page_dirty(ipage ? ipage : in_page);
  441. set_page_dirty(xpage);
  442. f2fs_put_page(xpage, 1);
  443. in_page_out:
  444. f2fs_put_page(in_page, 1);
  445. return err;
  446. }
  447. int f2fs_getxattr(struct inode *inode, int index, const char *name,
  448. void *buffer, size_t buffer_size, struct page *ipage)
  449. {
  450. struct f2fs_xattr_entry *entry = NULL;
  451. int error;
  452. unsigned int size, len;
  453. void *base_addr = NULL;
  454. int base_size;
  455. bool is_inline;
  456. if (name == NULL)
  457. return -EINVAL;
  458. len = strlen(name);
  459. if (len > F2FS_NAME_LEN)
  460. return -ERANGE;
  461. if (!ipage)
  462. f2fs_down_read(&F2FS_I(inode)->i_xattr_sem);
  463. error = lookup_all_xattrs(inode, ipage, index, len, name,
  464. &entry, &base_addr, &base_size, &is_inline);
  465. if (!ipage)
  466. f2fs_up_read(&F2FS_I(inode)->i_xattr_sem);
  467. if (error)
  468. return error;
  469. size = le16_to_cpu(entry->e_value_size);
  470. if (buffer && size > buffer_size) {
  471. error = -ERANGE;
  472. goto out;
  473. }
  474. if (buffer) {
  475. char *pval = entry->e_name + entry->e_name_len;
  476. if (base_size - (pval - (char *)base_addr) < size) {
  477. error = -ERANGE;
  478. goto out;
  479. }
  480. memcpy(buffer, pval, size);
  481. }
  482. error = size;
  483. out:
  484. xattr_free(F2FS_I_SB(inode), base_addr, is_inline);
  485. return error;
  486. }
  487. ssize_t f2fs_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
  488. {
  489. struct inode *inode = d_inode(dentry);
  490. struct f2fs_xattr_entry *entry;
  491. void *base_addr, *last_base_addr;
  492. int error;
  493. size_t rest = buffer_size;
  494. f2fs_down_read(&F2FS_I(inode)->i_xattr_sem);
  495. error = read_all_xattrs(inode, NULL, &base_addr);
  496. f2fs_up_read(&F2FS_I(inode)->i_xattr_sem);
  497. if (error)
  498. return error;
  499. last_base_addr = (void *)base_addr + XATTR_SIZE(inode);
  500. list_for_each_xattr(entry, base_addr) {
  501. const struct xattr_handler *handler =
  502. f2fs_xattr_handler(entry->e_name_index);
  503. const char *prefix;
  504. size_t prefix_len;
  505. size_t size;
  506. if ((void *)(entry) + sizeof(__u32) > last_base_addr ||
  507. (void *)XATTR_NEXT_ENTRY(entry) > last_base_addr) {
  508. f2fs_err(F2FS_I_SB(inode), "list inode (%lu) has corrupted xattr",
  509. inode->i_ino);
  510. set_sbi_flag(F2FS_I_SB(inode), SBI_NEED_FSCK);
  511. f2fs_handle_error(F2FS_I_SB(inode),
  512. ERROR_CORRUPTED_XATTR);
  513. break;
  514. }
  515. if (!handler || (handler->list && !handler->list(dentry)))
  516. continue;
  517. prefix = xattr_prefix(handler);
  518. prefix_len = strlen(prefix);
  519. size = prefix_len + entry->e_name_len + 1;
  520. if (buffer) {
  521. if (size > rest) {
  522. error = -ERANGE;
  523. goto cleanup;
  524. }
  525. memcpy(buffer, prefix, prefix_len);
  526. buffer += prefix_len;
  527. memcpy(buffer, entry->e_name, entry->e_name_len);
  528. buffer += entry->e_name_len;
  529. *buffer++ = 0;
  530. }
  531. rest -= size;
  532. }
  533. error = buffer_size - rest;
  534. cleanup:
  535. kfree(base_addr);
  536. return error;
  537. }
  538. static bool f2fs_xattr_value_same(struct f2fs_xattr_entry *entry,
  539. const void *value, size_t size)
  540. {
  541. void *pval = entry->e_name + entry->e_name_len;
  542. return (le16_to_cpu(entry->e_value_size) == size) &&
  543. !memcmp(pval, value, size);
  544. }
  545. static int __f2fs_setxattr(struct inode *inode, int index,
  546. const char *name, const void *value, size_t size,
  547. struct page *ipage, int flags)
  548. {
  549. struct f2fs_xattr_entry *here, *last;
  550. void *base_addr, *last_base_addr;
  551. int found, newsize;
  552. size_t len;
  553. __u32 new_hsize;
  554. int error;
  555. if (name == NULL)
  556. return -EINVAL;
  557. if (value == NULL)
  558. size = 0;
  559. len = strlen(name);
  560. if (len > F2FS_NAME_LEN)
  561. return -ERANGE;
  562. if (size > MAX_VALUE_LEN(inode))
  563. return -E2BIG;
  564. retry:
  565. error = read_all_xattrs(inode, ipage, &base_addr);
  566. if (error)
  567. return error;
  568. last_base_addr = (void *)base_addr + XATTR_SIZE(inode);
  569. /* find entry with wanted name. */
  570. here = __find_xattr(base_addr, last_base_addr, NULL, index, len, name);
  571. if (!here) {
  572. if (!F2FS_I(inode)->i_xattr_nid) {
  573. f2fs_notice(F2FS_I_SB(inode),
  574. "recover xattr in inode (%lu)", inode->i_ino);
  575. f2fs_recover_xattr_data(inode, NULL);
  576. kfree(base_addr);
  577. goto retry;
  578. }
  579. f2fs_err(F2FS_I_SB(inode), "set inode (%lu) has corrupted xattr",
  580. inode->i_ino);
  581. set_sbi_flag(F2FS_I_SB(inode), SBI_NEED_FSCK);
  582. error = -EFSCORRUPTED;
  583. f2fs_handle_error(F2FS_I_SB(inode),
  584. ERROR_CORRUPTED_XATTR);
  585. goto exit;
  586. }
  587. found = IS_XATTR_LAST_ENTRY(here) ? 0 : 1;
  588. if (found) {
  589. if ((flags & XATTR_CREATE)) {
  590. error = -EEXIST;
  591. goto exit;
  592. }
  593. if (value && f2fs_xattr_value_same(here, value, size))
  594. goto same;
  595. } else if ((flags & XATTR_REPLACE)) {
  596. error = -ENODATA;
  597. goto exit;
  598. }
  599. last = here;
  600. while (!IS_XATTR_LAST_ENTRY(last)) {
  601. if ((void *)(last) + sizeof(__u32) > last_base_addr ||
  602. (void *)XATTR_NEXT_ENTRY(last) > last_base_addr) {
  603. f2fs_err(F2FS_I_SB(inode), "inode (%lu) has invalid last xattr entry, entry_size: %zu",
  604. inode->i_ino, ENTRY_SIZE(last));
  605. set_sbi_flag(F2FS_I_SB(inode), SBI_NEED_FSCK);
  606. error = -EFSCORRUPTED;
  607. f2fs_handle_error(F2FS_I_SB(inode),
  608. ERROR_CORRUPTED_XATTR);
  609. goto exit;
  610. }
  611. last = XATTR_NEXT_ENTRY(last);
  612. }
  613. newsize = XATTR_ALIGN(sizeof(struct f2fs_xattr_entry) + len + size);
  614. /* 1. Check space */
  615. if (value) {
  616. int free;
  617. /*
  618. * If value is NULL, it is remove operation.
  619. * In case of update operation, we calculate free.
  620. */
  621. free = MIN_OFFSET(inode) - ((char *)last - (char *)base_addr);
  622. if (found)
  623. free = free + ENTRY_SIZE(here);
  624. if (unlikely(free < newsize)) {
  625. error = -E2BIG;
  626. goto exit;
  627. }
  628. }
  629. /* 2. Remove old entry */
  630. if (found) {
  631. /*
  632. * If entry is found, remove old entry.
  633. * If not found, remove operation is not needed.
  634. */
  635. struct f2fs_xattr_entry *next = XATTR_NEXT_ENTRY(here);
  636. int oldsize = ENTRY_SIZE(here);
  637. memmove(here, next, (char *)last - (char *)next);
  638. last = (struct f2fs_xattr_entry *)((char *)last - oldsize);
  639. memset(last, 0, oldsize);
  640. }
  641. new_hsize = (char *)last - (char *)base_addr;
  642. /* 3. Write new entry */
  643. if (value) {
  644. char *pval;
  645. /*
  646. * Before we come here, old entry is removed.
  647. * We just write new entry.
  648. */
  649. last->e_name_index = index;
  650. last->e_name_len = len;
  651. memcpy(last->e_name, name, len);
  652. pval = last->e_name + len;
  653. memcpy(pval, value, size);
  654. last->e_value_size = cpu_to_le16(size);
  655. new_hsize += newsize;
  656. }
  657. error = write_all_xattrs(inode, new_hsize, base_addr, ipage);
  658. if (error)
  659. goto exit;
  660. if (index == F2FS_XATTR_INDEX_ENCRYPTION &&
  661. !strcmp(name, F2FS_XATTR_NAME_ENCRYPTION_CONTEXT))
  662. f2fs_set_encrypted_inode(inode);
  663. f2fs_mark_inode_dirty_sync(inode, true);
  664. if (!error && S_ISDIR(inode->i_mode))
  665. set_sbi_flag(F2FS_I_SB(inode), SBI_NEED_CP);
  666. same:
  667. if (is_inode_flag_set(inode, FI_ACL_MODE)) {
  668. inode->i_mode = F2FS_I(inode)->i_acl_mode;
  669. inode->i_ctime = current_time(inode);
  670. clear_inode_flag(inode, FI_ACL_MODE);
  671. }
  672. exit:
  673. kfree(base_addr);
  674. return error;
  675. }
  676. int f2fs_setxattr(struct inode *inode, int index, const char *name,
  677. const void *value, size_t size,
  678. struct page *ipage, int flags)
  679. {
  680. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  681. int err;
  682. if (unlikely(f2fs_cp_error(sbi)))
  683. return -EIO;
  684. if (!f2fs_is_checkpoint_ready(sbi))
  685. return -ENOSPC;
  686. err = f2fs_dquot_initialize(inode);
  687. if (err)
  688. return err;
  689. /* this case is only from f2fs_init_inode_metadata */
  690. if (ipage)
  691. return __f2fs_setxattr(inode, index, name, value,
  692. size, ipage, flags);
  693. f2fs_balance_fs(sbi, true);
  694. f2fs_lock_op(sbi);
  695. f2fs_down_write(&F2FS_I(inode)->i_xattr_sem);
  696. err = __f2fs_setxattr(inode, index, name, value, size, ipage, flags);
  697. f2fs_up_write(&F2FS_I(inode)->i_xattr_sem);
  698. f2fs_unlock_op(sbi);
  699. f2fs_update_time(sbi, REQ_TIME);
  700. return err;
  701. }
  702. int f2fs_init_xattr_caches(struct f2fs_sb_info *sbi)
  703. {
  704. dev_t dev = sbi->sb->s_bdev->bd_dev;
  705. char slab_name[32];
  706. sprintf(slab_name, "f2fs_xattr_entry-%u:%u", MAJOR(dev), MINOR(dev));
  707. sbi->inline_xattr_slab_size = F2FS_OPTION(sbi).inline_xattr_size *
  708. sizeof(__le32) + XATTR_PADDING_SIZE;
  709. sbi->inline_xattr_slab = f2fs_kmem_cache_create(slab_name,
  710. sbi->inline_xattr_slab_size);
  711. if (!sbi->inline_xattr_slab)
  712. return -ENOMEM;
  713. return 0;
  714. }
  715. void f2fs_destroy_xattr_caches(struct f2fs_sb_info *sbi)
  716. {
  717. kmem_cache_destroy(sbi->inline_xattr_slab);
  718. }