xattr.c 20 KB

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