xattr.c 18 KB

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