xattr.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713
  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(struct inode *inode,
  197. void *base_addr, void **last_addr, int index,
  198. size_t len, const char *name)
  199. {
  200. struct f2fs_xattr_entry *entry;
  201. unsigned int inline_size = inline_xattr_size(inode);
  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 read_inline_xattr(struct inode *inode, struct page *ipage,
  219. void *txattr_addr)
  220. {
  221. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  222. unsigned int inline_size = inline_xattr_size(inode);
  223. struct page *page = NULL;
  224. void *inline_addr;
  225. if (ipage) {
  226. inline_addr = inline_xattr_addr(inode, ipage);
  227. } else {
  228. page = f2fs_get_node_page(sbi, inode->i_ino);
  229. if (IS_ERR(page))
  230. return PTR_ERR(page);
  231. inline_addr = inline_xattr_addr(inode, page);
  232. }
  233. memcpy(txattr_addr, inline_addr, inline_size);
  234. f2fs_put_page(page, 1);
  235. return 0;
  236. }
  237. static int read_xattr_block(struct inode *inode, void *txattr_addr)
  238. {
  239. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  240. nid_t xnid = F2FS_I(inode)->i_xattr_nid;
  241. unsigned int inline_size = inline_xattr_size(inode);
  242. struct page *xpage;
  243. void *xattr_addr;
  244. /* The inode already has an extended attribute block. */
  245. xpage = f2fs_get_node_page(sbi, xnid);
  246. if (IS_ERR(xpage))
  247. return PTR_ERR(xpage);
  248. xattr_addr = page_address(xpage);
  249. memcpy(txattr_addr + inline_size, xattr_addr, VALID_XATTR_BLOCK_SIZE);
  250. f2fs_put_page(xpage, 1);
  251. return 0;
  252. }
  253. static int lookup_all_xattrs(struct inode *inode, struct page *ipage,
  254. unsigned int index, unsigned int len,
  255. const char *name, struct f2fs_xattr_entry **xe,
  256. void **base_addr)
  257. {
  258. void *cur_addr, *txattr_addr, *last_addr = NULL;
  259. nid_t xnid = F2FS_I(inode)->i_xattr_nid;
  260. unsigned int size = xnid ? VALID_XATTR_BLOCK_SIZE : 0;
  261. unsigned int inline_size = inline_xattr_size(inode);
  262. int err = 0;
  263. if (!size && !inline_size)
  264. return -ENODATA;
  265. txattr_addr = f2fs_kzalloc(F2FS_I_SB(inode),
  266. inline_size + size + XATTR_PADDING_SIZE, GFP_NOFS);
  267. if (!txattr_addr)
  268. return -ENOMEM;
  269. /* read from inline xattr */
  270. if (inline_size) {
  271. err = read_inline_xattr(inode, ipage, txattr_addr);
  272. if (err)
  273. goto out;
  274. *xe = __find_inline_xattr(inode, txattr_addr, &last_addr,
  275. index, len, name);
  276. if (*xe)
  277. goto check;
  278. }
  279. /* read from xattr node block */
  280. if (xnid) {
  281. err = read_xattr_block(inode, txattr_addr);
  282. if (err)
  283. goto out;
  284. }
  285. if (last_addr)
  286. cur_addr = XATTR_HDR(last_addr) - 1;
  287. else
  288. cur_addr = txattr_addr;
  289. *xe = __find_xattr(cur_addr, index, len, name);
  290. check:
  291. if (IS_XATTR_LAST_ENTRY(*xe)) {
  292. err = -ENODATA;
  293. goto out;
  294. }
  295. *base_addr = txattr_addr;
  296. return 0;
  297. out:
  298. kzfree(txattr_addr);
  299. return err;
  300. }
  301. static int read_all_xattrs(struct inode *inode, struct page *ipage,
  302. void **base_addr)
  303. {
  304. struct f2fs_xattr_header *header;
  305. nid_t xnid = F2FS_I(inode)->i_xattr_nid;
  306. unsigned int size = VALID_XATTR_BLOCK_SIZE;
  307. unsigned int inline_size = inline_xattr_size(inode);
  308. void *txattr_addr;
  309. int err;
  310. txattr_addr = f2fs_kzalloc(F2FS_I_SB(inode),
  311. inline_size + size + XATTR_PADDING_SIZE, GFP_NOFS);
  312. if (!txattr_addr)
  313. return -ENOMEM;
  314. /* read from inline xattr */
  315. if (inline_size) {
  316. err = read_inline_xattr(inode, ipage, txattr_addr);
  317. if (err)
  318. goto fail;
  319. }
  320. /* read from xattr node block */
  321. if (xnid) {
  322. err = read_xattr_block(inode, txattr_addr);
  323. if (err)
  324. goto fail;
  325. }
  326. header = XATTR_HDR(txattr_addr);
  327. /* never been allocated xattrs */
  328. if (le32_to_cpu(header->h_magic) != F2FS_XATTR_MAGIC) {
  329. header->h_magic = cpu_to_le32(F2FS_XATTR_MAGIC);
  330. header->h_refcount = cpu_to_le32(1);
  331. }
  332. *base_addr = txattr_addr;
  333. return 0;
  334. fail:
  335. kzfree(txattr_addr);
  336. return err;
  337. }
  338. static inline int write_all_xattrs(struct inode *inode, __u32 hsize,
  339. void *txattr_addr, struct page *ipage)
  340. {
  341. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  342. size_t inline_size = inline_xattr_size(inode);
  343. struct page *in_page = NULL;
  344. void *xattr_addr;
  345. void *inline_addr = NULL;
  346. struct page *xpage;
  347. nid_t new_nid = 0;
  348. int err = 0;
  349. if (hsize > inline_size && !F2FS_I(inode)->i_xattr_nid)
  350. if (!f2fs_alloc_nid(sbi, &new_nid))
  351. return -ENOSPC;
  352. /* write to inline xattr */
  353. if (inline_size) {
  354. if (ipage) {
  355. inline_addr = inline_xattr_addr(inode, ipage);
  356. } else {
  357. in_page = f2fs_get_node_page(sbi, inode->i_ino);
  358. if (IS_ERR(in_page)) {
  359. f2fs_alloc_nid_failed(sbi, new_nid);
  360. return PTR_ERR(in_page);
  361. }
  362. inline_addr = inline_xattr_addr(inode, in_page);
  363. }
  364. f2fs_wait_on_page_writeback(ipage ? ipage : in_page,
  365. NODE, true);
  366. /* no need to use xattr node block */
  367. if (hsize <= inline_size) {
  368. err = f2fs_truncate_xattr_node(inode);
  369. f2fs_alloc_nid_failed(sbi, new_nid);
  370. if (err) {
  371. f2fs_put_page(in_page, 1);
  372. return err;
  373. }
  374. memcpy(inline_addr, txattr_addr, inline_size);
  375. set_page_dirty(ipage ? ipage : in_page);
  376. goto in_page_out;
  377. }
  378. }
  379. /* write to xattr node block */
  380. if (F2FS_I(inode)->i_xattr_nid) {
  381. xpage = f2fs_get_node_page(sbi, F2FS_I(inode)->i_xattr_nid);
  382. if (IS_ERR(xpage)) {
  383. err = PTR_ERR(xpage);
  384. f2fs_alloc_nid_failed(sbi, new_nid);
  385. goto in_page_out;
  386. }
  387. f2fs_bug_on(sbi, new_nid);
  388. f2fs_wait_on_page_writeback(xpage, NODE, true);
  389. } else {
  390. struct dnode_of_data dn;
  391. set_new_dnode(&dn, inode, NULL, NULL, new_nid);
  392. xpage = f2fs_new_node_page(&dn, XATTR_NODE_OFFSET);
  393. if (IS_ERR(xpage)) {
  394. err = PTR_ERR(xpage);
  395. f2fs_alloc_nid_failed(sbi, new_nid);
  396. goto in_page_out;
  397. }
  398. f2fs_alloc_nid_done(sbi, new_nid);
  399. }
  400. xattr_addr = page_address(xpage);
  401. if (inline_size)
  402. memcpy(inline_addr, txattr_addr, inline_size);
  403. memcpy(xattr_addr, txattr_addr + inline_size, VALID_XATTR_BLOCK_SIZE);
  404. if (inline_size)
  405. set_page_dirty(ipage ? ipage : in_page);
  406. set_page_dirty(xpage);
  407. f2fs_put_page(xpage, 1);
  408. in_page_out:
  409. f2fs_put_page(in_page, 1);
  410. return err;
  411. }
  412. int f2fs_getxattr(struct inode *inode, int index, const char *name,
  413. void *buffer, size_t buffer_size, struct page *ipage)
  414. {
  415. struct f2fs_xattr_entry *entry = NULL;
  416. int error = 0;
  417. unsigned int size, len;
  418. void *base_addr = NULL;
  419. if (name == NULL)
  420. return -EINVAL;
  421. len = strlen(name);
  422. if (len > F2FS_NAME_LEN)
  423. return -ERANGE;
  424. down_read(&F2FS_I(inode)->i_xattr_sem);
  425. error = lookup_all_xattrs(inode, ipage, index, len, name,
  426. &entry, &base_addr);
  427. up_read(&F2FS_I(inode)->i_xattr_sem);
  428. if (error)
  429. return error;
  430. size = le16_to_cpu(entry->e_value_size);
  431. if (buffer && size > buffer_size) {
  432. error = -ERANGE;
  433. goto out;
  434. }
  435. if (buffer) {
  436. char *pval = entry->e_name + entry->e_name_len;
  437. memcpy(buffer, pval, size);
  438. }
  439. error = size;
  440. out:
  441. kzfree(base_addr);
  442. return error;
  443. }
  444. ssize_t f2fs_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
  445. {
  446. struct inode *inode = d_inode(dentry);
  447. struct f2fs_xattr_entry *entry;
  448. void *base_addr;
  449. int error = 0;
  450. size_t rest = buffer_size;
  451. down_read(&F2FS_I(inode)->i_xattr_sem);
  452. error = read_all_xattrs(inode, NULL, &base_addr);
  453. up_read(&F2FS_I(inode)->i_xattr_sem);
  454. if (error)
  455. return error;
  456. list_for_each_xattr(entry, base_addr) {
  457. const struct xattr_handler *handler =
  458. f2fs_xattr_handler(entry->e_name_index);
  459. const char *prefix;
  460. size_t prefix_len;
  461. size_t size;
  462. if (!handler || (handler->list && !handler->list(dentry)))
  463. continue;
  464. prefix = handler->prefix ?: handler->name;
  465. prefix_len = strlen(prefix);
  466. size = prefix_len + entry->e_name_len + 1;
  467. if (buffer) {
  468. if (size > rest) {
  469. error = -ERANGE;
  470. goto cleanup;
  471. }
  472. memcpy(buffer, prefix, prefix_len);
  473. buffer += prefix_len;
  474. memcpy(buffer, entry->e_name, entry->e_name_len);
  475. buffer += entry->e_name_len;
  476. *buffer++ = 0;
  477. }
  478. rest -= size;
  479. }
  480. error = buffer_size - rest;
  481. cleanup:
  482. kzfree(base_addr);
  483. return error;
  484. }
  485. static bool f2fs_xattr_value_same(struct f2fs_xattr_entry *entry,
  486. const void *value, size_t size)
  487. {
  488. void *pval = entry->e_name + entry->e_name_len;
  489. return (le16_to_cpu(entry->e_value_size) == size) &&
  490. !memcmp(pval, value, size);
  491. }
  492. static int __f2fs_setxattr(struct inode *inode, int index,
  493. const char *name, const void *value, size_t size,
  494. struct page *ipage, int flags)
  495. {
  496. struct f2fs_xattr_entry *here, *last;
  497. void *base_addr;
  498. int found, newsize;
  499. size_t len;
  500. __u32 new_hsize;
  501. int error = 0;
  502. if (name == NULL)
  503. return -EINVAL;
  504. if (value == NULL)
  505. size = 0;
  506. len = strlen(name);
  507. if (len > F2FS_NAME_LEN)
  508. return -ERANGE;
  509. if (size > MAX_VALUE_LEN(inode))
  510. return -E2BIG;
  511. error = read_all_xattrs(inode, ipage, &base_addr);
  512. if (error)
  513. return error;
  514. /* find entry with wanted name. */
  515. here = __find_xattr(base_addr, index, len, name);
  516. found = IS_XATTR_LAST_ENTRY(here) ? 0 : 1;
  517. if (found) {
  518. if ((flags & XATTR_CREATE)) {
  519. error = -EEXIST;
  520. goto exit;
  521. }
  522. if (value && f2fs_xattr_value_same(here, value, size))
  523. goto exit;
  524. } else if ((flags & XATTR_REPLACE)) {
  525. error = -ENODATA;
  526. goto exit;
  527. }
  528. last = here;
  529. while (!IS_XATTR_LAST_ENTRY(last))
  530. last = XATTR_NEXT_ENTRY(last);
  531. newsize = XATTR_ALIGN(sizeof(struct f2fs_xattr_entry) + len + size);
  532. /* 1. Check space */
  533. if (value) {
  534. int free;
  535. /*
  536. * If value is NULL, it is remove operation.
  537. * In case of update operation, we calculate free.
  538. */
  539. free = MIN_OFFSET(inode) - ((char *)last - (char *)base_addr);
  540. if (found)
  541. free = free + ENTRY_SIZE(here);
  542. if (unlikely(free < newsize)) {
  543. error = -E2BIG;
  544. goto exit;
  545. }
  546. }
  547. /* 2. Remove old entry */
  548. if (found) {
  549. /*
  550. * If entry is found, remove old entry.
  551. * If not found, remove operation is not needed.
  552. */
  553. struct f2fs_xattr_entry *next = XATTR_NEXT_ENTRY(here);
  554. int oldsize = ENTRY_SIZE(here);
  555. memmove(here, next, (char *)last - (char *)next);
  556. last = (struct f2fs_xattr_entry *)((char *)last - oldsize);
  557. memset(last, 0, oldsize);
  558. }
  559. new_hsize = (char *)last - (char *)base_addr;
  560. /* 3. Write new entry */
  561. if (value) {
  562. char *pval;
  563. /*
  564. * Before we come here, old entry is removed.
  565. * We just write new entry.
  566. */
  567. last->e_name_index = index;
  568. last->e_name_len = len;
  569. memcpy(last->e_name, name, len);
  570. pval = last->e_name + len;
  571. memcpy(pval, value, size);
  572. last->e_value_size = cpu_to_le16(size);
  573. new_hsize += newsize;
  574. }
  575. error = write_all_xattrs(inode, new_hsize, base_addr, ipage);
  576. if (error)
  577. goto exit;
  578. if (is_inode_flag_set(inode, FI_ACL_MODE)) {
  579. inode->i_mode = F2FS_I(inode)->i_acl_mode;
  580. inode->i_ctime = current_time(inode);
  581. clear_inode_flag(inode, FI_ACL_MODE);
  582. }
  583. if (index == F2FS_XATTR_INDEX_ENCRYPTION &&
  584. !strcmp(name, F2FS_XATTR_NAME_ENCRYPTION_CONTEXT))
  585. f2fs_set_encrypted_inode(inode);
  586. f2fs_mark_inode_dirty_sync(inode, true);
  587. if (!error && S_ISDIR(inode->i_mode))
  588. set_sbi_flag(F2FS_I_SB(inode), SBI_NEED_CP);
  589. exit:
  590. kzfree(base_addr);
  591. return error;
  592. }
  593. int f2fs_setxattr(struct inode *inode, int index, const char *name,
  594. const void *value, size_t size,
  595. struct page *ipage, int flags)
  596. {
  597. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  598. int err;
  599. err = dquot_initialize(inode);
  600. if (err)
  601. return err;
  602. /* this case is only from f2fs_init_inode_metadata */
  603. if (ipage)
  604. return __f2fs_setxattr(inode, index, name, value,
  605. size, ipage, flags);
  606. f2fs_balance_fs(sbi, true);
  607. f2fs_lock_op(sbi);
  608. /* protect xattr_ver */
  609. down_write(&F2FS_I(inode)->i_sem);
  610. down_write(&F2FS_I(inode)->i_xattr_sem);
  611. err = __f2fs_setxattr(inode, index, name, value, size, ipage, flags);
  612. up_write(&F2FS_I(inode)->i_xattr_sem);
  613. up_write(&F2FS_I(inode)->i_sem);
  614. f2fs_unlock_op(sbi);
  615. f2fs_update_time(sbi, REQ_TIME);
  616. return err;
  617. }