xattr.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701
  1. /*
  2. * fs/f2fs/xattr.c
  3. *
  4. * Copyright (c) 2012 Samsung Electronics Co., Ltd.
  5. * http://www.samsung.com/
  6. *
  7. * Portions of this code from linux/fs/ext2/xattr.c
  8. *
  9. * Copyright (C) 2001-2003 Andreas Gruenbacher <agruen@suse.de>
  10. *
  11. * Fix by Harrison Xing <harrison@mountainviewdata.com>.
  12. * Extended attributes for symlinks and special files added per
  13. * suggestion of Luka Renko <luka.renko@hermes.si>.
  14. * xattr consolidation Copyright (c) 2004 James Morris <jmorris@redhat.com>,
  15. * Red Hat Inc.
  16. *
  17. * This program is free software; you can redistribute it and/or modify
  18. * it under the terms of the GNU General Public License version 2 as
  19. * published by the Free Software Foundation.
  20. */
  21. #include <linux/rwsem.h>
  22. #include <linux/f2fs_fs.h>
  23. #include <linux/security.h>
  24. #include <linux/posix_acl_xattr.h>
  25. #include "f2fs.h"
  26. #include "xattr.h"
  27. static int f2fs_xattr_generic_get(const struct xattr_handler *handler,
  28. struct dentry *unused, struct inode *inode,
  29. const char *name, void *buffer, size_t size)
  30. {
  31. struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
  32. switch (handler->flags) {
  33. case F2FS_XATTR_INDEX_USER:
  34. if (!test_opt(sbi, XATTR_USER))
  35. return -EOPNOTSUPP;
  36. break;
  37. case F2FS_XATTR_INDEX_TRUSTED:
  38. if (!capable(CAP_SYS_ADMIN))
  39. return -EPERM;
  40. break;
  41. case F2FS_XATTR_INDEX_SECURITY:
  42. break;
  43. default:
  44. return -EINVAL;
  45. }
  46. return f2fs_getxattr(inode, handler->flags, name,
  47. buffer, size, NULL);
  48. }
  49. static int f2fs_xattr_generic_set(const struct xattr_handler *handler,
  50. struct dentry *unused, struct inode *inode,
  51. const char *name, const void *value,
  52. size_t size, int flags)
  53. {
  54. struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
  55. switch (handler->flags) {
  56. case F2FS_XATTR_INDEX_USER:
  57. if (!test_opt(sbi, XATTR_USER))
  58. return -EOPNOTSUPP;
  59. break;
  60. case F2FS_XATTR_INDEX_TRUSTED:
  61. if (!capable(CAP_SYS_ADMIN))
  62. return -EPERM;
  63. break;
  64. case F2FS_XATTR_INDEX_SECURITY:
  65. break;
  66. default:
  67. return -EINVAL;
  68. }
  69. return f2fs_setxattr(inode, handler->flags, name,
  70. value, size, NULL, flags);
  71. }
  72. static bool f2fs_xattr_user_list(struct dentry *dentry)
  73. {
  74. struct f2fs_sb_info *sbi = F2FS_SB(dentry->d_sb);
  75. return test_opt(sbi, XATTR_USER);
  76. }
  77. static bool f2fs_xattr_trusted_list(struct dentry *dentry)
  78. {
  79. return capable(CAP_SYS_ADMIN);
  80. }
  81. static int f2fs_xattr_advise_get(const struct xattr_handler *handler,
  82. struct dentry *unused, struct inode *inode,
  83. const char *name, void *buffer, size_t size)
  84. {
  85. if (buffer)
  86. *((char *)buffer) = F2FS_I(inode)->i_advise;
  87. return sizeof(char);
  88. }
  89. static int f2fs_xattr_advise_set(const struct xattr_handler *handler,
  90. struct dentry *unused, struct inode *inode,
  91. const char *name, const void *value,
  92. size_t size, int flags)
  93. {
  94. if (!inode_owner_or_capable(inode))
  95. return -EPERM;
  96. if (value == NULL)
  97. return -EINVAL;
  98. F2FS_I(inode)->i_advise |= *(char *)value;
  99. f2fs_mark_inode_dirty_sync(inode, true);
  100. return 0;
  101. }
  102. #ifdef CONFIG_F2FS_FS_SECURITY
  103. static int f2fs_initxattrs(struct inode *inode, const struct xattr *xattr_array,
  104. void *page)
  105. {
  106. const struct xattr *xattr;
  107. int err = 0;
  108. for (xattr = xattr_array; xattr->name != NULL; xattr++) {
  109. err = f2fs_setxattr(inode, F2FS_XATTR_INDEX_SECURITY,
  110. xattr->name, xattr->value,
  111. xattr->value_len, (struct page *)page, 0);
  112. if (err < 0)
  113. break;
  114. }
  115. return err;
  116. }
  117. int f2fs_init_security(struct inode *inode, struct inode *dir,
  118. const struct qstr *qstr, struct page *ipage)
  119. {
  120. return security_inode_init_security(inode, dir, qstr,
  121. &f2fs_initxattrs, ipage);
  122. }
  123. #endif
  124. const struct xattr_handler f2fs_xattr_user_handler = {
  125. .prefix = XATTR_USER_PREFIX,
  126. .flags = F2FS_XATTR_INDEX_USER,
  127. .list = f2fs_xattr_user_list,
  128. .get = f2fs_xattr_generic_get,
  129. .set = f2fs_xattr_generic_set,
  130. };
  131. const struct xattr_handler f2fs_xattr_trusted_handler = {
  132. .prefix = XATTR_TRUSTED_PREFIX,
  133. .flags = F2FS_XATTR_INDEX_TRUSTED,
  134. .list = f2fs_xattr_trusted_list,
  135. .get = f2fs_xattr_generic_get,
  136. .set = f2fs_xattr_generic_set,
  137. };
  138. const struct xattr_handler f2fs_xattr_advise_handler = {
  139. .name = F2FS_SYSTEM_ADVISE_NAME,
  140. .flags = F2FS_XATTR_INDEX_ADVISE,
  141. .get = f2fs_xattr_advise_get,
  142. .set = f2fs_xattr_advise_set,
  143. };
  144. const struct xattr_handler f2fs_xattr_security_handler = {
  145. .prefix = XATTR_SECURITY_PREFIX,
  146. .flags = F2FS_XATTR_INDEX_SECURITY,
  147. .get = f2fs_xattr_generic_get,
  148. .set = f2fs_xattr_generic_set,
  149. };
  150. static const struct xattr_handler *f2fs_xattr_handler_map[] = {
  151. [F2FS_XATTR_INDEX_USER] = &f2fs_xattr_user_handler,
  152. #ifdef CONFIG_F2FS_FS_POSIX_ACL
  153. [F2FS_XATTR_INDEX_POSIX_ACL_ACCESS] = &posix_acl_access_xattr_handler,
  154. [F2FS_XATTR_INDEX_POSIX_ACL_DEFAULT] = &posix_acl_default_xattr_handler,
  155. #endif
  156. [F2FS_XATTR_INDEX_TRUSTED] = &f2fs_xattr_trusted_handler,
  157. #ifdef CONFIG_F2FS_FS_SECURITY
  158. [F2FS_XATTR_INDEX_SECURITY] = &f2fs_xattr_security_handler,
  159. #endif
  160. [F2FS_XATTR_INDEX_ADVISE] = &f2fs_xattr_advise_handler,
  161. };
  162. const struct xattr_handler *f2fs_xattr_handlers[] = {
  163. &f2fs_xattr_user_handler,
  164. #ifdef CONFIG_F2FS_FS_POSIX_ACL
  165. &posix_acl_access_xattr_handler,
  166. &posix_acl_default_xattr_handler,
  167. #endif
  168. &f2fs_xattr_trusted_handler,
  169. #ifdef CONFIG_F2FS_FS_SECURITY
  170. &f2fs_xattr_security_handler,
  171. #endif
  172. &f2fs_xattr_advise_handler,
  173. NULL,
  174. };
  175. static inline const struct xattr_handler *f2fs_xattr_handler(int index)
  176. {
  177. const struct xattr_handler *handler = NULL;
  178. if (index > 0 && index < ARRAY_SIZE(f2fs_xattr_handler_map))
  179. handler = f2fs_xattr_handler_map[index];
  180. return handler;
  181. }
  182. static struct f2fs_xattr_entry *__find_xattr(void *base_addr, int index,
  183. size_t len, const char *name)
  184. {
  185. struct f2fs_xattr_entry *entry;
  186. list_for_each_xattr(entry, base_addr) {
  187. if (entry->e_name_index != index)
  188. continue;
  189. if (entry->e_name_len != len)
  190. continue;
  191. if (!memcmp(entry->e_name, name, len))
  192. break;
  193. }
  194. return entry;
  195. }
  196. static struct f2fs_xattr_entry *__find_inline_xattr(void *base_addr,
  197. void **last_addr, int index,
  198. size_t len, const char *name)
  199. {
  200. struct f2fs_xattr_entry *entry;
  201. unsigned int inline_size = F2FS_INLINE_XATTR_ADDRS << 2;
  202. list_for_each_xattr(entry, base_addr) {
  203. if ((void *)entry + sizeof(__u32) > base_addr + inline_size ||
  204. (void *)XATTR_NEXT_ENTRY(entry) + sizeof(__u32) >
  205. base_addr + inline_size) {
  206. *last_addr = entry;
  207. return NULL;
  208. }
  209. if (entry->e_name_index != index)
  210. continue;
  211. if (entry->e_name_len != len)
  212. continue;
  213. if (!memcmp(entry->e_name, name, len))
  214. break;
  215. }
  216. return entry;
  217. }
  218. static int lookup_all_xattrs(struct inode *inode, struct page *ipage,
  219. unsigned int index, unsigned int len,
  220. const char *name, struct f2fs_xattr_entry **xe,
  221. void **base_addr)
  222. {
  223. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  224. void *cur_addr, *txattr_addr, *last_addr = NULL;
  225. nid_t xnid = F2FS_I(inode)->i_xattr_nid;
  226. unsigned int size = xnid ? VALID_XATTR_BLOCK_SIZE : 0;
  227. unsigned int inline_size = inline_xattr_size(inode);
  228. int err = 0;
  229. if (!size && !inline_size)
  230. return -ENODATA;
  231. txattr_addr = kzalloc(inline_size + size + XATTR_PADDING_SIZE,
  232. GFP_F2FS_ZERO);
  233. if (!txattr_addr)
  234. return -ENOMEM;
  235. /* read from inline xattr */
  236. if (inline_size) {
  237. struct page *page = NULL;
  238. void *inline_addr;
  239. if (ipage) {
  240. inline_addr = inline_xattr_addr(ipage);
  241. } else {
  242. page = get_node_page(sbi, inode->i_ino);
  243. if (IS_ERR(page)) {
  244. err = PTR_ERR(page);
  245. goto out;
  246. }
  247. inline_addr = inline_xattr_addr(page);
  248. }
  249. memcpy(txattr_addr, inline_addr, inline_size);
  250. f2fs_put_page(page, 1);
  251. *xe = __find_inline_xattr(txattr_addr, &last_addr,
  252. index, len, name);
  253. if (*xe)
  254. goto check;
  255. }
  256. /* read from xattr node block */
  257. if (xnid) {
  258. struct page *xpage;
  259. void *xattr_addr;
  260. /* The inode already has an extended attribute block. */
  261. xpage = get_node_page(sbi, xnid);
  262. if (IS_ERR(xpage)) {
  263. err = PTR_ERR(xpage);
  264. goto out;
  265. }
  266. xattr_addr = page_address(xpage);
  267. memcpy(txattr_addr + inline_size, xattr_addr, size);
  268. f2fs_put_page(xpage, 1);
  269. }
  270. if (last_addr)
  271. cur_addr = XATTR_HDR(last_addr) - 1;
  272. else
  273. cur_addr = txattr_addr;
  274. *xe = __find_xattr(cur_addr, index, len, name);
  275. check:
  276. if (IS_XATTR_LAST_ENTRY(*xe)) {
  277. err = -ENODATA;
  278. goto out;
  279. }
  280. *base_addr = txattr_addr;
  281. return 0;
  282. out:
  283. kzfree(txattr_addr);
  284. return err;
  285. }
  286. static int read_all_xattrs(struct inode *inode, struct page *ipage,
  287. void **base_addr)
  288. {
  289. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  290. struct f2fs_xattr_header *header;
  291. nid_t xnid = F2FS_I(inode)->i_xattr_nid;
  292. unsigned int size = VALID_XATTR_BLOCK_SIZE;
  293. unsigned int inline_size = inline_xattr_size(inode);
  294. void *txattr_addr;
  295. int err;
  296. txattr_addr = kzalloc(inline_size + size + XATTR_PADDING_SIZE,
  297. GFP_F2FS_ZERO);
  298. if (!txattr_addr)
  299. return -ENOMEM;
  300. /* read from inline xattr */
  301. if (inline_size) {
  302. struct page *page = NULL;
  303. void *inline_addr;
  304. if (ipage) {
  305. inline_addr = inline_xattr_addr(ipage);
  306. } else {
  307. page = get_node_page(sbi, inode->i_ino);
  308. if (IS_ERR(page)) {
  309. err = PTR_ERR(page);
  310. goto fail;
  311. }
  312. inline_addr = inline_xattr_addr(page);
  313. }
  314. memcpy(txattr_addr, inline_addr, inline_size);
  315. f2fs_put_page(page, 1);
  316. }
  317. /* read from xattr node block */
  318. if (xnid) {
  319. struct page *xpage;
  320. void *xattr_addr;
  321. /* The inode already has an extended attribute block. */
  322. xpage = get_node_page(sbi, xnid);
  323. if (IS_ERR(xpage)) {
  324. err = PTR_ERR(xpage);
  325. goto fail;
  326. }
  327. xattr_addr = page_address(xpage);
  328. memcpy(txattr_addr + inline_size, xattr_addr, size);
  329. f2fs_put_page(xpage, 1);
  330. }
  331. header = XATTR_HDR(txattr_addr);
  332. /* never been allocated xattrs */
  333. if (le32_to_cpu(header->h_magic) != F2FS_XATTR_MAGIC) {
  334. header->h_magic = cpu_to_le32(F2FS_XATTR_MAGIC);
  335. header->h_refcount = cpu_to_le32(1);
  336. }
  337. *base_addr = txattr_addr;
  338. return 0;
  339. fail:
  340. kzfree(txattr_addr);
  341. return err;
  342. }
  343. static inline int write_all_xattrs(struct inode *inode, __u32 hsize,
  344. void *txattr_addr, struct page *ipage)
  345. {
  346. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  347. size_t inline_size = inline_xattr_size(inode);
  348. void *xattr_addr;
  349. struct page *xpage;
  350. nid_t new_nid = 0;
  351. int err;
  352. if (hsize > inline_size && !F2FS_I(inode)->i_xattr_nid)
  353. if (!alloc_nid(sbi, &new_nid))
  354. return -ENOSPC;
  355. /* write to inline xattr */
  356. if (inline_size) {
  357. struct page *page = NULL;
  358. void *inline_addr;
  359. if (ipage) {
  360. inline_addr = inline_xattr_addr(ipage);
  361. f2fs_wait_on_page_writeback(ipage, NODE, true);
  362. set_page_dirty(ipage);
  363. } else {
  364. page = get_node_page(sbi, inode->i_ino);
  365. if (IS_ERR(page)) {
  366. alloc_nid_failed(sbi, new_nid);
  367. return PTR_ERR(page);
  368. }
  369. inline_addr = inline_xattr_addr(page);
  370. f2fs_wait_on_page_writeback(page, NODE, true);
  371. }
  372. memcpy(inline_addr, txattr_addr, inline_size);
  373. f2fs_put_page(page, 1);
  374. /* no need to use xattr node block */
  375. if (hsize <= inline_size) {
  376. err = truncate_xattr_node(inode, ipage);
  377. alloc_nid_failed(sbi, new_nid);
  378. return err;
  379. }
  380. }
  381. /* write to xattr node block */
  382. if (F2FS_I(inode)->i_xattr_nid) {
  383. xpage = get_node_page(sbi, F2FS_I(inode)->i_xattr_nid);
  384. if (IS_ERR(xpage)) {
  385. alloc_nid_failed(sbi, new_nid);
  386. return PTR_ERR(xpage);
  387. }
  388. f2fs_bug_on(sbi, new_nid);
  389. f2fs_wait_on_page_writeback(xpage, NODE, true);
  390. } else {
  391. struct dnode_of_data dn;
  392. set_new_dnode(&dn, inode, NULL, NULL, new_nid);
  393. xpage = new_node_page(&dn, XATTR_NODE_OFFSET);
  394. if (IS_ERR(xpage)) {
  395. alloc_nid_failed(sbi, new_nid);
  396. return PTR_ERR(xpage);
  397. }
  398. alloc_nid_done(sbi, new_nid);
  399. }
  400. xattr_addr = page_address(xpage);
  401. memcpy(xattr_addr, txattr_addr + inline_size, VALID_XATTR_BLOCK_SIZE);
  402. set_page_dirty(xpage);
  403. f2fs_put_page(xpage, 1);
  404. return 0;
  405. }
  406. int f2fs_getxattr(struct inode *inode, int index, const char *name,
  407. void *buffer, size_t buffer_size, struct page *ipage)
  408. {
  409. struct f2fs_xattr_entry *entry = NULL;
  410. int error = 0;
  411. unsigned int size, len;
  412. void *base_addr = NULL;
  413. if (name == NULL)
  414. return -EINVAL;
  415. len = strlen(name);
  416. if (len > F2FS_NAME_LEN)
  417. return -ERANGE;
  418. down_read(&F2FS_I(inode)->i_xattr_sem);
  419. error = lookup_all_xattrs(inode, ipage, index, len, name,
  420. &entry, &base_addr);
  421. up_read(&F2FS_I(inode)->i_xattr_sem);
  422. if (error)
  423. return error;
  424. size = le16_to_cpu(entry->e_value_size);
  425. if (buffer && size > buffer_size) {
  426. error = -ERANGE;
  427. goto out;
  428. }
  429. if (buffer) {
  430. char *pval = entry->e_name + entry->e_name_len;
  431. memcpy(buffer, pval, size);
  432. }
  433. error = size;
  434. out:
  435. kzfree(base_addr);
  436. return error;
  437. }
  438. ssize_t f2fs_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
  439. {
  440. struct inode *inode = d_inode(dentry);
  441. struct f2fs_xattr_entry *entry;
  442. void *base_addr;
  443. int error = 0;
  444. size_t rest = buffer_size;
  445. down_read(&F2FS_I(inode)->i_xattr_sem);
  446. error = read_all_xattrs(inode, NULL, &base_addr);
  447. up_read(&F2FS_I(inode)->i_xattr_sem);
  448. if (error)
  449. return error;
  450. list_for_each_xattr(entry, base_addr) {
  451. const struct xattr_handler *handler =
  452. f2fs_xattr_handler(entry->e_name_index);
  453. const char *prefix;
  454. size_t prefix_len;
  455. size_t size;
  456. if (!handler || (handler->list && !handler->list(dentry)))
  457. continue;
  458. prefix = handler->prefix ?: handler->name;
  459. prefix_len = strlen(prefix);
  460. size = prefix_len + entry->e_name_len + 1;
  461. if (buffer) {
  462. if (size > rest) {
  463. error = -ERANGE;
  464. goto cleanup;
  465. }
  466. memcpy(buffer, prefix, prefix_len);
  467. buffer += prefix_len;
  468. memcpy(buffer, entry->e_name, entry->e_name_len);
  469. buffer += entry->e_name_len;
  470. *buffer++ = 0;
  471. }
  472. rest -= size;
  473. }
  474. error = buffer_size - rest;
  475. cleanup:
  476. kzfree(base_addr);
  477. return error;
  478. }
  479. static bool f2fs_xattr_value_same(struct f2fs_xattr_entry *entry,
  480. const void *value, size_t size)
  481. {
  482. void *pval = entry->e_name + entry->e_name_len;
  483. return (le16_to_cpu(entry->e_value_size) == size) &&
  484. !memcmp(pval, value, size);
  485. }
  486. static int __f2fs_setxattr(struct inode *inode, int index,
  487. const char *name, const void *value, size_t size,
  488. struct page *ipage, int flags)
  489. {
  490. struct f2fs_xattr_entry *here, *last;
  491. void *base_addr;
  492. int found, newsize;
  493. size_t len;
  494. __u32 new_hsize;
  495. int error = 0;
  496. if (name == NULL)
  497. return -EINVAL;
  498. if (value == NULL)
  499. size = 0;
  500. len = strlen(name);
  501. if (len > F2FS_NAME_LEN)
  502. return -ERANGE;
  503. if (size > MAX_VALUE_LEN(inode))
  504. return -E2BIG;
  505. error = read_all_xattrs(inode, ipage, &base_addr);
  506. if (error)
  507. return error;
  508. /* find entry with wanted name. */
  509. here = __find_xattr(base_addr, index, len, name);
  510. found = IS_XATTR_LAST_ENTRY(here) ? 0 : 1;
  511. if (found) {
  512. if ((flags & XATTR_CREATE)) {
  513. error = -EEXIST;
  514. goto exit;
  515. }
  516. if (f2fs_xattr_value_same(here, value, size))
  517. goto exit;
  518. } else if ((flags & XATTR_REPLACE)) {
  519. error = -ENODATA;
  520. goto exit;
  521. }
  522. last = here;
  523. while (!IS_XATTR_LAST_ENTRY(last))
  524. last = XATTR_NEXT_ENTRY(last);
  525. newsize = XATTR_ALIGN(sizeof(struct f2fs_xattr_entry) + len + size);
  526. /* 1. Check space */
  527. if (value) {
  528. int free;
  529. /*
  530. * If value is NULL, it is remove operation.
  531. * In case of update operation, we calculate free.
  532. */
  533. free = MIN_OFFSET(inode) - ((char *)last - (char *)base_addr);
  534. if (found)
  535. free = free + ENTRY_SIZE(here);
  536. if (unlikely(free < newsize)) {
  537. error = -E2BIG;
  538. goto exit;
  539. }
  540. }
  541. /* 2. Remove old entry */
  542. if (found) {
  543. /*
  544. * If entry is found, remove old entry.
  545. * If not found, remove operation is not needed.
  546. */
  547. struct f2fs_xattr_entry *next = XATTR_NEXT_ENTRY(here);
  548. int oldsize = ENTRY_SIZE(here);
  549. memmove(here, next, (char *)last - (char *)next);
  550. last = (struct f2fs_xattr_entry *)((char *)last - oldsize);
  551. memset(last, 0, oldsize);
  552. }
  553. new_hsize = (char *)last - (char *)base_addr;
  554. /* 3. Write new entry */
  555. if (value) {
  556. char *pval;
  557. /*
  558. * Before we come here, old entry is removed.
  559. * We just write new entry.
  560. */
  561. last->e_name_index = index;
  562. last->e_name_len = len;
  563. memcpy(last->e_name, name, len);
  564. pval = last->e_name + len;
  565. memcpy(pval, value, size);
  566. last->e_value_size = cpu_to_le16(size);
  567. new_hsize += newsize;
  568. }
  569. error = write_all_xattrs(inode, new_hsize, base_addr, ipage);
  570. if (error)
  571. goto exit;
  572. if (is_inode_flag_set(inode, FI_ACL_MODE)) {
  573. inode->i_mode = F2FS_I(inode)->i_acl_mode;
  574. inode->i_ctime = current_time(inode);
  575. clear_inode_flag(inode, FI_ACL_MODE);
  576. }
  577. if (index == F2FS_XATTR_INDEX_ENCRYPTION &&
  578. !strcmp(name, F2FS_XATTR_NAME_ENCRYPTION_CONTEXT))
  579. f2fs_set_encrypted_inode(inode);
  580. f2fs_mark_inode_dirty_sync(inode, true);
  581. if (!error && S_ISDIR(inode->i_mode))
  582. set_sbi_flag(F2FS_I_SB(inode), SBI_NEED_CP);
  583. exit:
  584. kzfree(base_addr);
  585. return error;
  586. }
  587. int f2fs_setxattr(struct inode *inode, int index, const char *name,
  588. const void *value, size_t size,
  589. struct page *ipage, int flags)
  590. {
  591. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  592. int err;
  593. /* this case is only from init_inode_metadata */
  594. if (ipage)
  595. return __f2fs_setxattr(inode, index, name, value,
  596. size, ipage, flags);
  597. f2fs_balance_fs(sbi, true);
  598. f2fs_lock_op(sbi);
  599. /* protect xattr_ver */
  600. down_write(&F2FS_I(inode)->i_sem);
  601. down_write(&F2FS_I(inode)->i_xattr_sem);
  602. err = __f2fs_setxattr(inode, index, name, value, size, ipage, flags);
  603. up_write(&F2FS_I(inode)->i_xattr_sem);
  604. up_write(&F2FS_I(inode)->i_sem);
  605. f2fs_unlock_op(sbi);
  606. f2fs_update_time(sbi, REQ_TIME);
  607. return err;
  608. }