xattr.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  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 *dentry, const char *name, void *buffer,
  29. size_t size)
  30. {
  31. struct f2fs_sb_info *sbi = F2FS_SB(dentry->d_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(d_inode(dentry), handler->flags, name,
  47. buffer, size, NULL);
  48. }
  49. static int f2fs_xattr_generic_set(const struct xattr_handler *handler,
  50. struct dentry *dentry, const char *name, const void *value,
  51. size_t size, int flags)
  52. {
  53. struct f2fs_sb_info *sbi = F2FS_SB(dentry->d_sb);
  54. switch (handler->flags) {
  55. case F2FS_XATTR_INDEX_USER:
  56. if (!test_opt(sbi, XATTR_USER))
  57. return -EOPNOTSUPP;
  58. break;
  59. case F2FS_XATTR_INDEX_TRUSTED:
  60. if (!capable(CAP_SYS_ADMIN))
  61. return -EPERM;
  62. break;
  63. case F2FS_XATTR_INDEX_SECURITY:
  64. break;
  65. default:
  66. return -EINVAL;
  67. }
  68. return f2fs_setxattr(d_inode(dentry), handler->flags, name,
  69. value, size, NULL, flags);
  70. }
  71. static bool f2fs_xattr_user_list(struct dentry *dentry)
  72. {
  73. struct f2fs_sb_info *sbi = F2FS_SB(dentry->d_sb);
  74. return test_opt(sbi, XATTR_USER);
  75. }
  76. static bool f2fs_xattr_trusted_list(struct dentry *dentry)
  77. {
  78. return capable(CAP_SYS_ADMIN);
  79. }
  80. static int f2fs_xattr_advise_get(const struct xattr_handler *handler,
  81. struct dentry *dentry, const char *name, void *buffer,
  82. size_t size)
  83. {
  84. struct inode *inode = d_inode(dentry);
  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 *dentry, const char *name, const void *value,
  91. size_t size, int flags)
  92. {
  93. struct inode *inode = d_inode(dentry);
  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. mark_inode_dirty(inode);
  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 void *read_all_xattrs(struct inode *inode, struct page *ipage)
  197. {
  198. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  199. struct f2fs_xattr_header *header;
  200. size_t size = PAGE_SIZE, inline_size = 0;
  201. void *txattr_addr;
  202. inline_size = inline_xattr_size(inode);
  203. txattr_addr = kzalloc(inline_size + size, GFP_F2FS_ZERO);
  204. if (!txattr_addr)
  205. return NULL;
  206. /* read from inline xattr */
  207. if (inline_size) {
  208. struct page *page = NULL;
  209. void *inline_addr;
  210. if (ipage) {
  211. inline_addr = inline_xattr_addr(ipage);
  212. } else {
  213. page = get_node_page(sbi, inode->i_ino);
  214. if (IS_ERR(page))
  215. goto fail;
  216. inline_addr = inline_xattr_addr(page);
  217. }
  218. memcpy(txattr_addr, inline_addr, inline_size);
  219. f2fs_put_page(page, 1);
  220. }
  221. /* read from xattr node block */
  222. if (F2FS_I(inode)->i_xattr_nid) {
  223. struct page *xpage;
  224. void *xattr_addr;
  225. /* The inode already has an extended attribute block. */
  226. xpage = get_node_page(sbi, F2FS_I(inode)->i_xattr_nid);
  227. if (IS_ERR(xpage))
  228. goto fail;
  229. xattr_addr = page_address(xpage);
  230. memcpy(txattr_addr + inline_size, xattr_addr, PAGE_SIZE);
  231. f2fs_put_page(xpage, 1);
  232. }
  233. header = XATTR_HDR(txattr_addr);
  234. /* never been allocated xattrs */
  235. if (le32_to_cpu(header->h_magic) != F2FS_XATTR_MAGIC) {
  236. header->h_magic = cpu_to_le32(F2FS_XATTR_MAGIC);
  237. header->h_refcount = cpu_to_le32(1);
  238. }
  239. return txattr_addr;
  240. fail:
  241. kzfree(txattr_addr);
  242. return NULL;
  243. }
  244. static inline int write_all_xattrs(struct inode *inode, __u32 hsize,
  245. void *txattr_addr, struct page *ipage)
  246. {
  247. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  248. size_t inline_size = 0;
  249. void *xattr_addr;
  250. struct page *xpage;
  251. nid_t new_nid = 0;
  252. int err;
  253. inline_size = inline_xattr_size(inode);
  254. if (hsize > inline_size && !F2FS_I(inode)->i_xattr_nid)
  255. if (!alloc_nid(sbi, &new_nid))
  256. return -ENOSPC;
  257. /* write to inline xattr */
  258. if (inline_size) {
  259. struct page *page = NULL;
  260. void *inline_addr;
  261. if (ipage) {
  262. inline_addr = inline_xattr_addr(ipage);
  263. f2fs_wait_on_page_writeback(ipage, NODE);
  264. } else {
  265. page = get_node_page(sbi, inode->i_ino);
  266. if (IS_ERR(page)) {
  267. alloc_nid_failed(sbi, new_nid);
  268. return PTR_ERR(page);
  269. }
  270. inline_addr = inline_xattr_addr(page);
  271. f2fs_wait_on_page_writeback(page, NODE);
  272. }
  273. memcpy(inline_addr, txattr_addr, inline_size);
  274. f2fs_put_page(page, 1);
  275. /* no need to use xattr node block */
  276. if (hsize <= inline_size) {
  277. err = truncate_xattr_node(inode, ipage);
  278. alloc_nid_failed(sbi, new_nid);
  279. return err;
  280. }
  281. }
  282. /* write to xattr node block */
  283. if (F2FS_I(inode)->i_xattr_nid) {
  284. xpage = get_node_page(sbi, F2FS_I(inode)->i_xattr_nid);
  285. if (IS_ERR(xpage)) {
  286. alloc_nid_failed(sbi, new_nid);
  287. return PTR_ERR(xpage);
  288. }
  289. f2fs_bug_on(sbi, new_nid);
  290. f2fs_wait_on_page_writeback(xpage, NODE);
  291. } else {
  292. struct dnode_of_data dn;
  293. set_new_dnode(&dn, inode, NULL, NULL, new_nid);
  294. xpage = new_node_page(&dn, XATTR_NODE_OFFSET, ipage);
  295. if (IS_ERR(xpage)) {
  296. alloc_nid_failed(sbi, new_nid);
  297. return PTR_ERR(xpage);
  298. }
  299. alloc_nid_done(sbi, new_nid);
  300. }
  301. xattr_addr = page_address(xpage);
  302. memcpy(xattr_addr, txattr_addr + inline_size, PAGE_SIZE -
  303. sizeof(struct node_footer));
  304. set_page_dirty(xpage);
  305. f2fs_put_page(xpage, 1);
  306. /* need to checkpoint during fsync */
  307. F2FS_I(inode)->xattr_ver = cur_cp_version(F2FS_CKPT(sbi));
  308. return 0;
  309. }
  310. int f2fs_getxattr(struct inode *inode, int index, const char *name,
  311. void *buffer, size_t buffer_size, struct page *ipage)
  312. {
  313. struct f2fs_xattr_entry *entry;
  314. void *base_addr;
  315. int error = 0;
  316. size_t size, len;
  317. if (name == NULL)
  318. return -EINVAL;
  319. len = strlen(name);
  320. if (len > F2FS_NAME_LEN)
  321. return -ERANGE;
  322. base_addr = read_all_xattrs(inode, ipage);
  323. if (!base_addr)
  324. return -ENOMEM;
  325. entry = __find_xattr(base_addr, index, len, name);
  326. if (IS_XATTR_LAST_ENTRY(entry)) {
  327. error = -ENODATA;
  328. goto cleanup;
  329. }
  330. size = le16_to_cpu(entry->e_value_size);
  331. if (buffer && size > buffer_size) {
  332. error = -ERANGE;
  333. goto cleanup;
  334. }
  335. if (buffer) {
  336. char *pval = entry->e_name + entry->e_name_len;
  337. memcpy(buffer, pval, size);
  338. }
  339. error = size;
  340. cleanup:
  341. kzfree(base_addr);
  342. return error;
  343. }
  344. ssize_t f2fs_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
  345. {
  346. struct inode *inode = d_inode(dentry);
  347. struct f2fs_xattr_entry *entry;
  348. void *base_addr;
  349. int error = 0;
  350. size_t rest = buffer_size;
  351. base_addr = read_all_xattrs(inode, NULL);
  352. if (!base_addr)
  353. return -ENOMEM;
  354. list_for_each_xattr(entry, base_addr) {
  355. const struct xattr_handler *handler =
  356. f2fs_xattr_handler(entry->e_name_index);
  357. const char *prefix;
  358. size_t prefix_len;
  359. size_t size;
  360. if (!handler || (handler->list && !handler->list(dentry)))
  361. continue;
  362. prefix = handler->prefix ?: handler->name;
  363. prefix_len = strlen(prefix);
  364. size = prefix_len + entry->e_name_len + 1;
  365. if (buffer) {
  366. if (size > rest) {
  367. error = -ERANGE;
  368. goto cleanup;
  369. }
  370. memcpy(buffer, prefix, prefix_len);
  371. buffer += prefix_len;
  372. memcpy(buffer, entry->e_name, entry->e_name_len);
  373. buffer += entry->e_name_len;
  374. *buffer++ = 0;
  375. }
  376. rest -= size;
  377. }
  378. error = buffer_size - rest;
  379. cleanup:
  380. kzfree(base_addr);
  381. return error;
  382. }
  383. static int __f2fs_setxattr(struct inode *inode, int index,
  384. const char *name, const void *value, size_t size,
  385. struct page *ipage, int flags)
  386. {
  387. struct f2fs_inode_info *fi = F2FS_I(inode);
  388. struct f2fs_xattr_entry *here, *last;
  389. void *base_addr;
  390. int found, newsize;
  391. size_t len;
  392. __u32 new_hsize;
  393. int error = -ENOMEM;
  394. if (name == NULL)
  395. return -EINVAL;
  396. if (value == NULL)
  397. size = 0;
  398. len = strlen(name);
  399. if (len > F2FS_NAME_LEN)
  400. return -ERANGE;
  401. if (size > MAX_VALUE_LEN(inode))
  402. return -E2BIG;
  403. base_addr = read_all_xattrs(inode, ipage);
  404. if (!base_addr)
  405. goto exit;
  406. /* find entry with wanted name. */
  407. here = __find_xattr(base_addr, index, len, name);
  408. found = IS_XATTR_LAST_ENTRY(here) ? 0 : 1;
  409. if ((flags & XATTR_REPLACE) && !found) {
  410. error = -ENODATA;
  411. goto exit;
  412. } else if ((flags & XATTR_CREATE) && found) {
  413. error = -EEXIST;
  414. goto exit;
  415. }
  416. last = here;
  417. while (!IS_XATTR_LAST_ENTRY(last))
  418. last = XATTR_NEXT_ENTRY(last);
  419. newsize = XATTR_ALIGN(sizeof(struct f2fs_xattr_entry) + len + size);
  420. /* 1. Check space */
  421. if (value) {
  422. int free;
  423. /*
  424. * If value is NULL, it is remove operation.
  425. * In case of update operation, we calculate free.
  426. */
  427. free = MIN_OFFSET(inode) - ((char *)last - (char *)base_addr);
  428. if (found)
  429. free = free + ENTRY_SIZE(here);
  430. if (unlikely(free < newsize)) {
  431. error = -ENOSPC;
  432. goto exit;
  433. }
  434. }
  435. /* 2. Remove old entry */
  436. if (found) {
  437. /*
  438. * If entry is found, remove old entry.
  439. * If not found, remove operation is not needed.
  440. */
  441. struct f2fs_xattr_entry *next = XATTR_NEXT_ENTRY(here);
  442. int oldsize = ENTRY_SIZE(here);
  443. memmove(here, next, (char *)last - (char *)next);
  444. last = (struct f2fs_xattr_entry *)((char *)last - oldsize);
  445. memset(last, 0, oldsize);
  446. }
  447. new_hsize = (char *)last - (char *)base_addr;
  448. /* 3. Write new entry */
  449. if (value) {
  450. char *pval;
  451. /*
  452. * Before we come here, old entry is removed.
  453. * We just write new entry.
  454. */
  455. memset(last, 0, newsize);
  456. last->e_name_index = index;
  457. last->e_name_len = len;
  458. memcpy(last->e_name, name, len);
  459. pval = last->e_name + len;
  460. memcpy(pval, value, size);
  461. last->e_value_size = cpu_to_le16(size);
  462. new_hsize += newsize;
  463. }
  464. error = write_all_xattrs(inode, new_hsize, base_addr, ipage);
  465. if (error)
  466. goto exit;
  467. if (is_inode_flag_set(fi, FI_ACL_MODE)) {
  468. inode->i_mode = fi->i_acl_mode;
  469. inode->i_ctime = CURRENT_TIME;
  470. clear_inode_flag(fi, FI_ACL_MODE);
  471. }
  472. if (index == F2FS_XATTR_INDEX_ENCRYPTION &&
  473. !strcmp(name, F2FS_XATTR_NAME_ENCRYPTION_CONTEXT))
  474. f2fs_set_encrypted_inode(inode);
  475. if (ipage)
  476. update_inode(inode, ipage);
  477. else
  478. update_inode_page(inode);
  479. exit:
  480. kzfree(base_addr);
  481. return error;
  482. }
  483. int f2fs_setxattr(struct inode *inode, int index, const char *name,
  484. const void *value, size_t size,
  485. struct page *ipage, int flags)
  486. {
  487. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  488. int err;
  489. /* this case is only from init_inode_metadata */
  490. if (ipage)
  491. return __f2fs_setxattr(inode, index, name, value,
  492. size, ipage, flags);
  493. f2fs_balance_fs(sbi, true);
  494. f2fs_lock_op(sbi);
  495. /* protect xattr_ver */
  496. down_write(&F2FS_I(inode)->i_sem);
  497. err = __f2fs_setxattr(inode, index, name, value, size, ipage, flags);
  498. up_write(&F2FS_I(inode)->i_sem);
  499. f2fs_unlock_op(sbi);
  500. f2fs_update_time(sbi, REQ_TIME);
  501. return err;
  502. }