xattr.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854
  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 <agruen@suse.de>
  11. *
  12. * Fix by Harrison Xing <harrison@mountainviewdata.com>.
  13. * Extended attributes for symlinks and special files added per
  14. * suggestion of Luka Renko <luka.renko@hermes.si>.
  15. * xattr consolidation Copyright (c) 2004 James Morris <jmorris@redhat.com>,
  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 mnt_idmap *idmap,
  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 mnt_idmap *idmap,
  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(&nop_mnt_idmap, 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 * const 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] = &nop_posix_acl_access,
  173. [F2FS_XATTR_INDEX_POSIX_ACL_DEFAULT] = &nop_posix_acl_default,
  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 * const f2fs_xattr_handlers[] = {
  182. &f2fs_xattr_user_handler,
  183. &f2fs_xattr_trusted_handler,
  184. #ifdef CONFIG_F2FS_FS_SECURITY
  185. &f2fs_xattr_security_handler,
  186. #endif
  187. &f2fs_xattr_advise_handler,
  188. NULL,
  189. };
  190. static inline const char *f2fs_xattr_prefix(int index,
  191. struct dentry *dentry)
  192. {
  193. const struct xattr_handler *handler = NULL;
  194. if (index > 0 && index < ARRAY_SIZE(f2fs_xattr_handler_map))
  195. handler = f2fs_xattr_handler_map[index];
  196. if (!xattr_handler_can_list(handler, dentry))
  197. return NULL;
  198. return xattr_prefix(handler);
  199. }
  200. static struct f2fs_xattr_entry *__find_xattr(void *base_addr,
  201. void *last_base_addr, void **last_addr,
  202. int index, size_t len, const char *name)
  203. {
  204. struct f2fs_xattr_entry *entry;
  205. list_for_each_xattr(entry, base_addr) {
  206. if ((void *)(entry) + sizeof(__u32) > last_base_addr ||
  207. (void *)XATTR_NEXT_ENTRY(entry) > last_base_addr) {
  208. if (last_addr)
  209. *last_addr = entry;
  210. return NULL;
  211. }
  212. if (entry->e_name_index != index)
  213. continue;
  214. if (entry->e_name_len != len)
  215. continue;
  216. if (!memcmp(entry->e_name, name, len))
  217. break;
  218. }
  219. return entry;
  220. }
  221. static struct f2fs_xattr_entry *__find_inline_xattr(struct inode *inode,
  222. void *base_addr, void **last_addr, int index,
  223. size_t len, const char *name)
  224. {
  225. struct f2fs_xattr_entry *entry;
  226. unsigned int inline_size = inline_xattr_size(inode);
  227. void *max_addr = base_addr + inline_size;
  228. entry = __find_xattr(base_addr, max_addr, last_addr, index, len, name);
  229. if (!entry)
  230. return NULL;
  231. /* inline xattr header or entry across max inline xattr size */
  232. if (IS_XATTR_LAST_ENTRY(entry) &&
  233. (void *)entry + sizeof(__u32) > max_addr) {
  234. *last_addr = entry;
  235. return NULL;
  236. }
  237. return entry;
  238. }
  239. static int read_inline_xattr(struct inode *inode, struct page *ipage,
  240. void *txattr_addr)
  241. {
  242. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  243. unsigned int inline_size = inline_xattr_size(inode);
  244. struct page *page = NULL;
  245. void *inline_addr;
  246. if (ipage) {
  247. inline_addr = inline_xattr_addr(inode, ipage);
  248. } else {
  249. page = f2fs_get_node_page(sbi, inode->i_ino);
  250. if (IS_ERR(page))
  251. return PTR_ERR(page);
  252. inline_addr = inline_xattr_addr(inode, page);
  253. }
  254. memcpy(txattr_addr, inline_addr, inline_size);
  255. f2fs_put_page(page, 1);
  256. return 0;
  257. }
  258. static int read_xattr_block(struct inode *inode, void *txattr_addr)
  259. {
  260. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  261. nid_t xnid = F2FS_I(inode)->i_xattr_nid;
  262. unsigned int inline_size = inline_xattr_size(inode);
  263. struct page *xpage;
  264. void *xattr_addr;
  265. /* The inode already has an extended attribute block. */
  266. xpage = f2fs_get_node_page(sbi, xnid);
  267. if (IS_ERR(xpage))
  268. return PTR_ERR(xpage);
  269. xattr_addr = page_address(xpage);
  270. memcpy(txattr_addr + inline_size, xattr_addr, VALID_XATTR_BLOCK_SIZE);
  271. f2fs_put_page(xpage, 1);
  272. return 0;
  273. }
  274. static int lookup_all_xattrs(struct inode *inode, struct page *ipage,
  275. unsigned int index, unsigned int len,
  276. const char *name, struct f2fs_xattr_entry **xe,
  277. void **base_addr, int *base_size,
  278. bool *is_inline)
  279. {
  280. void *cur_addr, *txattr_addr, *last_txattr_addr;
  281. void *last_addr = NULL;
  282. nid_t xnid = F2FS_I(inode)->i_xattr_nid;
  283. unsigned int inline_size = inline_xattr_size(inode);
  284. int err;
  285. if (!xnid && !inline_size)
  286. return -ENODATA;
  287. *base_size = XATTR_SIZE(inode) + XATTR_PADDING_SIZE;
  288. txattr_addr = xattr_alloc(F2FS_I_SB(inode), *base_size, is_inline);
  289. if (!txattr_addr)
  290. return -ENOMEM;
  291. last_txattr_addr = (void *)txattr_addr + XATTR_SIZE(inode);
  292. /* read from inline xattr */
  293. if (inline_size) {
  294. err = read_inline_xattr(inode, ipage, txattr_addr);
  295. if (err)
  296. goto out;
  297. *xe = __find_inline_xattr(inode, txattr_addr, &last_addr,
  298. index, len, name);
  299. if (*xe) {
  300. *base_size = inline_size;
  301. goto check;
  302. }
  303. }
  304. /* read from xattr node block */
  305. if (xnid) {
  306. err = read_xattr_block(inode, txattr_addr);
  307. if (err)
  308. goto out;
  309. }
  310. if (last_addr)
  311. cur_addr = XATTR_HDR(last_addr) - 1;
  312. else
  313. cur_addr = txattr_addr;
  314. *xe = __find_xattr(cur_addr, last_txattr_addr, NULL, index, len, name);
  315. if (!*xe) {
  316. f2fs_err(F2FS_I_SB(inode), "lookup inode (%lu) has corrupted xattr",
  317. inode->i_ino);
  318. set_sbi_flag(F2FS_I_SB(inode), SBI_NEED_FSCK);
  319. err = -ENODATA;
  320. f2fs_handle_error(F2FS_I_SB(inode),
  321. ERROR_CORRUPTED_XATTR);
  322. goto out;
  323. }
  324. check:
  325. if (IS_XATTR_LAST_ENTRY(*xe)) {
  326. err = -ENODATA;
  327. goto out;
  328. }
  329. *base_addr = txattr_addr;
  330. return 0;
  331. out:
  332. xattr_free(F2FS_I_SB(inode), txattr_addr, *is_inline);
  333. return err;
  334. }
  335. static int read_all_xattrs(struct inode *inode, struct page *ipage,
  336. void **base_addr)
  337. {
  338. struct f2fs_xattr_header *header;
  339. nid_t xnid = F2FS_I(inode)->i_xattr_nid;
  340. unsigned int size = VALID_XATTR_BLOCK_SIZE;
  341. unsigned int inline_size = inline_xattr_size(inode);
  342. void *txattr_addr;
  343. int err;
  344. txattr_addr = f2fs_kzalloc(F2FS_I_SB(inode),
  345. inline_size + size + XATTR_PADDING_SIZE, GFP_NOFS);
  346. if (!txattr_addr)
  347. return -ENOMEM;
  348. /* read from inline xattr */
  349. if (inline_size) {
  350. err = read_inline_xattr(inode, ipage, txattr_addr);
  351. if (err)
  352. goto fail;
  353. }
  354. /* read from xattr node block */
  355. if (xnid) {
  356. err = read_xattr_block(inode, txattr_addr);
  357. if (err)
  358. goto fail;
  359. }
  360. header = XATTR_HDR(txattr_addr);
  361. /* never been allocated xattrs */
  362. if (le32_to_cpu(header->h_magic) != F2FS_XATTR_MAGIC) {
  363. header->h_magic = cpu_to_le32(F2FS_XATTR_MAGIC);
  364. header->h_refcount = cpu_to_le32(1);
  365. }
  366. *base_addr = txattr_addr;
  367. return 0;
  368. fail:
  369. kfree(txattr_addr);
  370. return err;
  371. }
  372. static inline int write_all_xattrs(struct inode *inode, __u32 hsize,
  373. void *txattr_addr, struct page *ipage)
  374. {
  375. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  376. size_t inline_size = inline_xattr_size(inode);
  377. struct page *in_page = NULL;
  378. void *xattr_addr;
  379. void *inline_addr = NULL;
  380. struct page *xpage;
  381. nid_t new_nid = 0;
  382. int err = 0;
  383. if (hsize > inline_size && !F2FS_I(inode)->i_xattr_nid)
  384. if (!f2fs_alloc_nid(sbi, &new_nid))
  385. return -ENOSPC;
  386. /* write to inline xattr */
  387. if (inline_size) {
  388. if (ipage) {
  389. inline_addr = inline_xattr_addr(inode, ipage);
  390. } else {
  391. in_page = f2fs_get_node_page(sbi, inode->i_ino);
  392. if (IS_ERR(in_page)) {
  393. f2fs_alloc_nid_failed(sbi, new_nid);
  394. return PTR_ERR(in_page);
  395. }
  396. inline_addr = inline_xattr_addr(inode, in_page);
  397. }
  398. f2fs_wait_on_page_writeback(ipage ? ipage : in_page,
  399. NODE, true, true);
  400. /* no need to use xattr node block */
  401. if (hsize <= inline_size) {
  402. err = f2fs_truncate_xattr_node(inode);
  403. f2fs_alloc_nid_failed(sbi, new_nid);
  404. if (err) {
  405. f2fs_put_page(in_page, 1);
  406. return err;
  407. }
  408. memcpy(inline_addr, txattr_addr, inline_size);
  409. set_page_dirty(ipage ? ipage : in_page);
  410. goto in_page_out;
  411. }
  412. }
  413. /* write to xattr node block */
  414. if (F2FS_I(inode)->i_xattr_nid) {
  415. xpage = f2fs_get_node_page(sbi, F2FS_I(inode)->i_xattr_nid);
  416. if (IS_ERR(xpage)) {
  417. err = PTR_ERR(xpage);
  418. f2fs_alloc_nid_failed(sbi, new_nid);
  419. goto in_page_out;
  420. }
  421. f2fs_bug_on(sbi, new_nid);
  422. f2fs_wait_on_page_writeback(xpage, NODE, true, true);
  423. } else {
  424. struct dnode_of_data dn;
  425. set_new_dnode(&dn, inode, NULL, NULL, new_nid);
  426. xpage = f2fs_new_node_page(&dn, XATTR_NODE_OFFSET);
  427. if (IS_ERR(xpage)) {
  428. err = PTR_ERR(xpage);
  429. f2fs_alloc_nid_failed(sbi, new_nid);
  430. goto in_page_out;
  431. }
  432. f2fs_alloc_nid_done(sbi, new_nid);
  433. }
  434. xattr_addr = page_address(xpage);
  435. if (inline_size)
  436. memcpy(inline_addr, txattr_addr, inline_size);
  437. memcpy(xattr_addr, txattr_addr + inline_size, VALID_XATTR_BLOCK_SIZE);
  438. if (inline_size)
  439. set_page_dirty(ipage ? ipage : in_page);
  440. set_page_dirty(xpage);
  441. f2fs_put_page(xpage, 1);
  442. in_page_out:
  443. f2fs_put_page(in_page, 1);
  444. return err;
  445. }
  446. int f2fs_getxattr(struct inode *inode, int index, const char *name,
  447. void *buffer, size_t buffer_size, struct page *ipage)
  448. {
  449. struct f2fs_xattr_entry *entry = NULL;
  450. int error;
  451. unsigned int size, len;
  452. void *base_addr = NULL;
  453. int base_size;
  454. bool is_inline;
  455. if (name == NULL)
  456. return -EINVAL;
  457. len = strlen(name);
  458. if (len > F2FS_NAME_LEN)
  459. return -ERANGE;
  460. if (!ipage)
  461. f2fs_down_read(&F2FS_I(inode)->i_xattr_sem);
  462. error = lookup_all_xattrs(inode, ipage, index, len, name,
  463. &entry, &base_addr, &base_size, &is_inline);
  464. if (!ipage)
  465. f2fs_up_read(&F2FS_I(inode)->i_xattr_sem);
  466. if (error)
  467. return error;
  468. size = le16_to_cpu(entry->e_value_size);
  469. if (buffer && size > buffer_size) {
  470. error = -ERANGE;
  471. goto out;
  472. }
  473. if (buffer) {
  474. char *pval = entry->e_name + entry->e_name_len;
  475. if (base_size - (pval - (char *)base_addr) < size) {
  476. error = -ERANGE;
  477. goto out;
  478. }
  479. memcpy(buffer, pval, size);
  480. }
  481. error = size;
  482. out:
  483. xattr_free(F2FS_I_SB(inode), base_addr, is_inline);
  484. return error;
  485. }
  486. ssize_t f2fs_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
  487. {
  488. struct inode *inode = d_inode(dentry);
  489. struct f2fs_xattr_entry *entry;
  490. void *base_addr, *last_base_addr;
  491. int error;
  492. size_t rest = buffer_size;
  493. f2fs_down_read(&F2FS_I(inode)->i_xattr_sem);
  494. error = read_all_xattrs(inode, NULL, &base_addr);
  495. f2fs_up_read(&F2FS_I(inode)->i_xattr_sem);
  496. if (error)
  497. return error;
  498. last_base_addr = (void *)base_addr + XATTR_SIZE(inode);
  499. list_for_each_xattr(entry, base_addr) {
  500. const char *prefix;
  501. size_t prefix_len;
  502. size_t size;
  503. prefix = f2fs_xattr_prefix(entry->e_name_index, dentry);
  504. if ((void *)(entry) + sizeof(__u32) > last_base_addr ||
  505. (void *)XATTR_NEXT_ENTRY(entry) > last_base_addr) {
  506. f2fs_err(F2FS_I_SB(inode), "list inode (%lu) has corrupted xattr",
  507. inode->i_ino);
  508. set_sbi_flag(F2FS_I_SB(inode), SBI_NEED_FSCK);
  509. f2fs_handle_error(F2FS_I_SB(inode),
  510. ERROR_CORRUPTED_XATTR);
  511. break;
  512. }
  513. if (!prefix)
  514. continue;
  515. prefix_len = strlen(prefix);
  516. size = prefix_len + entry->e_name_len + 1;
  517. if (buffer) {
  518. if (size > rest) {
  519. error = -ERANGE;
  520. goto cleanup;
  521. }
  522. memcpy(buffer, prefix, prefix_len);
  523. buffer += prefix_len;
  524. memcpy(buffer, entry->e_name, entry->e_name_len);
  525. buffer += entry->e_name_len;
  526. *buffer++ = 0;
  527. }
  528. rest -= size;
  529. }
  530. error = buffer_size - rest;
  531. cleanup:
  532. kfree(base_addr);
  533. return error;
  534. }
  535. static bool f2fs_xattr_value_same(struct f2fs_xattr_entry *entry,
  536. const void *value, size_t size)
  537. {
  538. void *pval = entry->e_name + entry->e_name_len;
  539. return (le16_to_cpu(entry->e_value_size) == size) &&
  540. !memcmp(pval, value, size);
  541. }
  542. static int __f2fs_setxattr(struct inode *inode, int index,
  543. const char *name, const void *value, size_t size,
  544. struct page *ipage, int flags)
  545. {
  546. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  547. struct f2fs_xattr_entry *here, *last;
  548. void *base_addr, *last_base_addr;
  549. int found, newsize;
  550. size_t len;
  551. __u32 new_hsize;
  552. int error;
  553. if (name == NULL)
  554. return -EINVAL;
  555. if (value == NULL)
  556. size = 0;
  557. len = strlen(name);
  558. if (len > F2FS_NAME_LEN)
  559. return -ERANGE;
  560. if (size > MAX_VALUE_LEN(inode))
  561. return -E2BIG;
  562. retry:
  563. error = read_all_xattrs(inode, ipage, &base_addr);
  564. if (error)
  565. return error;
  566. last_base_addr = (void *)base_addr + XATTR_SIZE(inode);
  567. /* find entry with wanted name. */
  568. here = __find_xattr(base_addr, last_base_addr, NULL, index, len, name);
  569. if (!here) {
  570. if (!F2FS_I(inode)->i_xattr_nid) {
  571. error = f2fs_recover_xattr_data(inode, NULL);
  572. f2fs_notice(F2FS_I_SB(inode),
  573. "recover xattr in inode (%lu), error(%d)",
  574. inode->i_ino, error);
  575. if (!error) {
  576. kfree(base_addr);
  577. goto retry;
  578. }
  579. }
  580. f2fs_err(F2FS_I_SB(inode), "set inode (%lu) has corrupted xattr",
  581. inode->i_ino);
  582. set_sbi_flag(F2FS_I_SB(inode), SBI_NEED_FSCK);
  583. error = -EFSCORRUPTED;
  584. f2fs_handle_error(F2FS_I_SB(inode),
  585. ERROR_CORRUPTED_XATTR);
  586. goto exit;
  587. }
  588. found = IS_XATTR_LAST_ENTRY(here) ? 0 : 1;
  589. if (found) {
  590. if ((flags & XATTR_CREATE)) {
  591. error = -EEXIST;
  592. goto exit;
  593. }
  594. if (value && f2fs_xattr_value_same(here, value, size))
  595. goto same;
  596. } else if ((flags & XATTR_REPLACE)) {
  597. error = -ENODATA;
  598. goto exit;
  599. }
  600. last = here;
  601. while (!IS_XATTR_LAST_ENTRY(last)) {
  602. if ((void *)(last) + sizeof(__u32) > last_base_addr ||
  603. (void *)XATTR_NEXT_ENTRY(last) > last_base_addr) {
  604. f2fs_err(F2FS_I_SB(inode), "inode (%lu) has invalid last xattr entry, entry_size: %zu",
  605. inode->i_ino, ENTRY_SIZE(last));
  606. set_sbi_flag(F2FS_I_SB(inode), SBI_NEED_FSCK);
  607. error = -EFSCORRUPTED;
  608. f2fs_handle_error(F2FS_I_SB(inode),
  609. ERROR_CORRUPTED_XATTR);
  610. goto exit;
  611. }
  612. last = XATTR_NEXT_ENTRY(last);
  613. }
  614. newsize = XATTR_ALIGN(sizeof(struct f2fs_xattr_entry) + len + size);
  615. /* 1. Check space */
  616. if (value) {
  617. int free;
  618. /*
  619. * If value is NULL, it is remove operation.
  620. * In case of update operation, we calculate free.
  621. */
  622. free = MIN_OFFSET(inode) - ((char *)last - (char *)base_addr);
  623. if (found)
  624. free = free + ENTRY_SIZE(here);
  625. if (unlikely(free < newsize)) {
  626. error = -E2BIG;
  627. goto exit;
  628. }
  629. }
  630. /* 2. Remove old entry */
  631. if (found) {
  632. /*
  633. * If entry is found, remove old entry.
  634. * If not found, remove operation is not needed.
  635. */
  636. struct f2fs_xattr_entry *next = XATTR_NEXT_ENTRY(here);
  637. int oldsize = ENTRY_SIZE(here);
  638. memmove(here, next, (char *)last - (char *)next);
  639. last = (struct f2fs_xattr_entry *)((char *)last - oldsize);
  640. memset(last, 0, oldsize);
  641. }
  642. new_hsize = (char *)last - (char *)base_addr;
  643. /* 3. Write new entry */
  644. if (value) {
  645. char *pval;
  646. /*
  647. * Before we come here, old entry is removed.
  648. * We just write new entry.
  649. */
  650. last->e_name_index = index;
  651. last->e_name_len = len;
  652. memcpy(last->e_name, name, len);
  653. pval = last->e_name + len;
  654. memcpy(pval, value, size);
  655. last->e_value_size = cpu_to_le16(size);
  656. new_hsize += newsize;
  657. /*
  658. * Explicitly add the null terminator. The unused xattr space
  659. * is supposed to always be zeroed, which would make this
  660. * unnecessary, but don't depend on that.
  661. */
  662. *(u32 *)((u8 *)last + newsize) = 0;
  663. }
  664. error = write_all_xattrs(inode, new_hsize, base_addr, ipage);
  665. if (error)
  666. goto exit;
  667. if (index == F2FS_XATTR_INDEX_ENCRYPTION &&
  668. !strcmp(name, F2FS_XATTR_NAME_ENCRYPTION_CONTEXT))
  669. f2fs_set_encrypted_inode(inode);
  670. if (!S_ISDIR(inode->i_mode))
  671. goto same;
  672. /*
  673. * In restrict mode, fsync() always try to trigger checkpoint for all
  674. * metadata consistency, in other mode, it triggers checkpoint when
  675. * parent's xattr metadata was updated.
  676. */
  677. if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_STRICT)
  678. set_sbi_flag(sbi, SBI_NEED_CP);
  679. else
  680. f2fs_add_ino_entry(sbi, inode->i_ino, XATTR_DIR_INO);
  681. same:
  682. if (is_inode_flag_set(inode, FI_ACL_MODE)) {
  683. inode->i_mode = F2FS_I(inode)->i_acl_mode;
  684. clear_inode_flag(inode, FI_ACL_MODE);
  685. }
  686. inode_set_ctime_current(inode);
  687. f2fs_mark_inode_dirty_sync(inode, true);
  688. exit:
  689. kfree(base_addr);
  690. return error;
  691. }
  692. int f2fs_setxattr(struct inode *inode, int index, const char *name,
  693. const void *value, size_t size,
  694. struct page *ipage, int flags)
  695. {
  696. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  697. int err;
  698. if (unlikely(f2fs_cp_error(sbi)))
  699. return -EIO;
  700. if (!f2fs_is_checkpoint_ready(sbi))
  701. return -ENOSPC;
  702. err = f2fs_dquot_initialize(inode);
  703. if (err)
  704. return err;
  705. /* this case is only from f2fs_init_inode_metadata */
  706. if (ipage)
  707. return __f2fs_setxattr(inode, index, name, value,
  708. size, ipage, flags);
  709. f2fs_balance_fs(sbi, true);
  710. f2fs_lock_op(sbi);
  711. f2fs_down_write(&F2FS_I(inode)->i_xattr_sem);
  712. err = __f2fs_setxattr(inode, index, name, value, size, ipage, flags);
  713. f2fs_up_write(&F2FS_I(inode)->i_xattr_sem);
  714. f2fs_unlock_op(sbi);
  715. f2fs_update_time(sbi, REQ_TIME);
  716. return err;
  717. }
  718. int f2fs_init_xattr_caches(struct f2fs_sb_info *sbi)
  719. {
  720. dev_t dev = sbi->sb->s_bdev->bd_dev;
  721. char slab_name[32];
  722. sprintf(slab_name, "f2fs_xattr_entry-%u:%u", MAJOR(dev), MINOR(dev));
  723. sbi->inline_xattr_slab_size = F2FS_OPTION(sbi).inline_xattr_size *
  724. sizeof(__le32) + XATTR_PADDING_SIZE;
  725. sbi->inline_xattr_slab = f2fs_kmem_cache_create(slab_name,
  726. sbi->inline_xattr_slab_size);
  727. if (!sbi->inline_xattr_slab)
  728. return -ENOMEM;
  729. return 0;
  730. }
  731. void f2fs_destroy_xattr_caches(struct f2fs_sb_info *sbi)
  732. {
  733. kmem_cache_destroy(sbi->inline_xattr_slab);
  734. }