xattr.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  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 size_t f2fs_xattr_generic_list(const struct xattr_handler *handler,
  28. struct dentry *dentry, char *list, size_t list_size,
  29. const char *name, size_t len)
  30. {
  31. struct f2fs_sb_info *sbi = F2FS_SB(dentry->d_sb);
  32. int total_len, prefix_len;
  33. switch (handler->flags) {
  34. case F2FS_XATTR_INDEX_USER:
  35. if (!test_opt(sbi, XATTR_USER))
  36. return -EOPNOTSUPP;
  37. break;
  38. case F2FS_XATTR_INDEX_TRUSTED:
  39. if (!capable(CAP_SYS_ADMIN))
  40. return -EPERM;
  41. break;
  42. case F2FS_XATTR_INDEX_SECURITY:
  43. break;
  44. default:
  45. return -EINVAL;
  46. }
  47. prefix_len = strlen(handler->prefix);
  48. total_len = prefix_len + len + 1;
  49. if (list && total_len <= list_size) {
  50. memcpy(list, handler->prefix, prefix_len);
  51. memcpy(list + prefix_len, name, len);
  52. list[prefix_len + len] = '\0';
  53. }
  54. return total_len;
  55. }
  56. static int f2fs_xattr_generic_get(const struct xattr_handler *handler,
  57. struct dentry *dentry, const char *name, void *buffer,
  58. size_t size)
  59. {
  60. struct f2fs_sb_info *sbi = F2FS_SB(dentry->d_sb);
  61. switch (handler->flags) {
  62. case F2FS_XATTR_INDEX_USER:
  63. if (!test_opt(sbi, XATTR_USER))
  64. return -EOPNOTSUPP;
  65. break;
  66. case F2FS_XATTR_INDEX_TRUSTED:
  67. if (!capable(CAP_SYS_ADMIN))
  68. return -EPERM;
  69. break;
  70. case F2FS_XATTR_INDEX_SECURITY:
  71. break;
  72. default:
  73. return -EINVAL;
  74. }
  75. if (strcmp(name, "") == 0)
  76. return -EINVAL;
  77. return f2fs_getxattr(d_inode(dentry), handler->flags, name,
  78. buffer, size, NULL);
  79. }
  80. static int f2fs_xattr_generic_set(const struct xattr_handler *handler,
  81. struct dentry *dentry, const char *name, const void *value,
  82. size_t size, int flags)
  83. {
  84. struct f2fs_sb_info *sbi = F2FS_SB(dentry->d_sb);
  85. switch (handler->flags) {
  86. case F2FS_XATTR_INDEX_USER:
  87. if (!test_opt(sbi, XATTR_USER))
  88. return -EOPNOTSUPP;
  89. break;
  90. case F2FS_XATTR_INDEX_TRUSTED:
  91. if (!capable(CAP_SYS_ADMIN))
  92. return -EPERM;
  93. break;
  94. case F2FS_XATTR_INDEX_SECURITY:
  95. break;
  96. default:
  97. return -EINVAL;
  98. }
  99. if (strcmp(name, "") == 0)
  100. return -EINVAL;
  101. return f2fs_setxattr(d_inode(dentry), handler->flags, name,
  102. value, size, NULL, flags);
  103. }
  104. static size_t f2fs_xattr_advise_list(const struct xattr_handler *handler,
  105. struct dentry *dentry, char *list, size_t list_size,
  106. const char *name, size_t len)
  107. {
  108. const char *xname = F2FS_SYSTEM_ADVISE_PREFIX;
  109. size_t size;
  110. size = strlen(xname) + 1;
  111. if (list && size <= list_size)
  112. memcpy(list, xname, size);
  113. return size;
  114. }
  115. static int f2fs_xattr_advise_get(const struct xattr_handler *handler,
  116. struct dentry *dentry, const char *name, void *buffer,
  117. size_t size)
  118. {
  119. struct inode *inode = d_inode(dentry);
  120. if (strcmp(name, "") != 0)
  121. return -EINVAL;
  122. if (buffer)
  123. *((char *)buffer) = F2FS_I(inode)->i_advise;
  124. return sizeof(char);
  125. }
  126. static int f2fs_xattr_advise_set(const struct xattr_handler *handler,
  127. struct dentry *dentry, const char *name, const void *value,
  128. size_t size, int flags)
  129. {
  130. struct inode *inode = d_inode(dentry);
  131. if (strcmp(name, "") != 0)
  132. return -EINVAL;
  133. if (!inode_owner_or_capable(inode))
  134. return -EPERM;
  135. if (value == NULL)
  136. return -EINVAL;
  137. F2FS_I(inode)->i_advise |= *(char *)value;
  138. mark_inode_dirty(inode);
  139. return 0;
  140. }
  141. #ifdef CONFIG_F2FS_FS_SECURITY
  142. static int f2fs_initxattrs(struct inode *inode, const struct xattr *xattr_array,
  143. void *page)
  144. {
  145. const struct xattr *xattr;
  146. int err = 0;
  147. for (xattr = xattr_array; xattr->name != NULL; xattr++) {
  148. err = f2fs_setxattr(inode, F2FS_XATTR_INDEX_SECURITY,
  149. xattr->name, xattr->value,
  150. xattr->value_len, (struct page *)page, 0);
  151. if (err < 0)
  152. break;
  153. }
  154. return err;
  155. }
  156. int f2fs_init_security(struct inode *inode, struct inode *dir,
  157. const struct qstr *qstr, struct page *ipage)
  158. {
  159. return security_inode_init_security(inode, dir, qstr,
  160. &f2fs_initxattrs, ipage);
  161. }
  162. #endif
  163. const struct xattr_handler f2fs_xattr_user_handler = {
  164. .prefix = XATTR_USER_PREFIX,
  165. .flags = F2FS_XATTR_INDEX_USER,
  166. .list = f2fs_xattr_generic_list,
  167. .get = f2fs_xattr_generic_get,
  168. .set = f2fs_xattr_generic_set,
  169. };
  170. const struct xattr_handler f2fs_xattr_trusted_handler = {
  171. .prefix = XATTR_TRUSTED_PREFIX,
  172. .flags = F2FS_XATTR_INDEX_TRUSTED,
  173. .list = f2fs_xattr_generic_list,
  174. .get = f2fs_xattr_generic_get,
  175. .set = f2fs_xattr_generic_set,
  176. };
  177. const struct xattr_handler f2fs_xattr_advise_handler = {
  178. .prefix = F2FS_SYSTEM_ADVISE_PREFIX,
  179. .flags = F2FS_XATTR_INDEX_ADVISE,
  180. .list = f2fs_xattr_advise_list,
  181. .get = f2fs_xattr_advise_get,
  182. .set = f2fs_xattr_advise_set,
  183. };
  184. const struct xattr_handler f2fs_xattr_security_handler = {
  185. .prefix = XATTR_SECURITY_PREFIX,
  186. .flags = F2FS_XATTR_INDEX_SECURITY,
  187. .list = f2fs_xattr_generic_list,
  188. .get = f2fs_xattr_generic_get,
  189. .set = f2fs_xattr_generic_set,
  190. };
  191. static const struct xattr_handler *f2fs_xattr_handler_map[] = {
  192. [F2FS_XATTR_INDEX_USER] = &f2fs_xattr_user_handler,
  193. #ifdef CONFIG_F2FS_FS_POSIX_ACL
  194. [F2FS_XATTR_INDEX_POSIX_ACL_ACCESS] = &posix_acl_access_xattr_handler,
  195. [F2FS_XATTR_INDEX_POSIX_ACL_DEFAULT] = &posix_acl_default_xattr_handler,
  196. #endif
  197. [F2FS_XATTR_INDEX_TRUSTED] = &f2fs_xattr_trusted_handler,
  198. #ifdef CONFIG_F2FS_FS_SECURITY
  199. [F2FS_XATTR_INDEX_SECURITY] = &f2fs_xattr_security_handler,
  200. #endif
  201. [F2FS_XATTR_INDEX_ADVISE] = &f2fs_xattr_advise_handler,
  202. };
  203. const struct xattr_handler *f2fs_xattr_handlers[] = {
  204. &f2fs_xattr_user_handler,
  205. #ifdef CONFIG_F2FS_FS_POSIX_ACL
  206. &posix_acl_access_xattr_handler,
  207. &posix_acl_default_xattr_handler,
  208. #endif
  209. &f2fs_xattr_trusted_handler,
  210. #ifdef CONFIG_F2FS_FS_SECURITY
  211. &f2fs_xattr_security_handler,
  212. #endif
  213. &f2fs_xattr_advise_handler,
  214. NULL,
  215. };
  216. static inline const struct xattr_handler *f2fs_xattr_handler(int index)
  217. {
  218. const struct xattr_handler *handler = NULL;
  219. if (index > 0 && index < ARRAY_SIZE(f2fs_xattr_handler_map))
  220. handler = f2fs_xattr_handler_map[index];
  221. return handler;
  222. }
  223. static struct f2fs_xattr_entry *__find_xattr(void *base_addr, int index,
  224. size_t len, const char *name)
  225. {
  226. struct f2fs_xattr_entry *entry;
  227. list_for_each_xattr(entry, base_addr) {
  228. if (entry->e_name_index != index)
  229. continue;
  230. if (entry->e_name_len != len)
  231. continue;
  232. if (!memcmp(entry->e_name, name, len))
  233. break;
  234. }
  235. return entry;
  236. }
  237. static void *read_all_xattrs(struct inode *inode, struct page *ipage)
  238. {
  239. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  240. struct f2fs_xattr_header *header;
  241. size_t size = PAGE_SIZE, inline_size = 0;
  242. void *txattr_addr;
  243. inline_size = inline_xattr_size(inode);
  244. txattr_addr = kzalloc(inline_size + size, GFP_F2FS_ZERO);
  245. if (!txattr_addr)
  246. return NULL;
  247. /* read from inline xattr */
  248. if (inline_size) {
  249. struct page *page = NULL;
  250. void *inline_addr;
  251. if (ipage) {
  252. inline_addr = inline_xattr_addr(ipage);
  253. } else {
  254. page = get_node_page(sbi, inode->i_ino);
  255. if (IS_ERR(page))
  256. goto fail;
  257. inline_addr = inline_xattr_addr(page);
  258. }
  259. memcpy(txattr_addr, inline_addr, inline_size);
  260. f2fs_put_page(page, 1);
  261. }
  262. /* read from xattr node block */
  263. if (F2FS_I(inode)->i_xattr_nid) {
  264. struct page *xpage;
  265. void *xattr_addr;
  266. /* The inode already has an extended attribute block. */
  267. xpage = get_node_page(sbi, F2FS_I(inode)->i_xattr_nid);
  268. if (IS_ERR(xpage))
  269. goto fail;
  270. xattr_addr = page_address(xpage);
  271. memcpy(txattr_addr + inline_size, xattr_addr, PAGE_SIZE);
  272. f2fs_put_page(xpage, 1);
  273. }
  274. header = XATTR_HDR(txattr_addr);
  275. /* never been allocated xattrs */
  276. if (le32_to_cpu(header->h_magic) != F2FS_XATTR_MAGIC) {
  277. header->h_magic = cpu_to_le32(F2FS_XATTR_MAGIC);
  278. header->h_refcount = cpu_to_le32(1);
  279. }
  280. return txattr_addr;
  281. fail:
  282. kzfree(txattr_addr);
  283. return NULL;
  284. }
  285. static inline int write_all_xattrs(struct inode *inode, __u32 hsize,
  286. void *txattr_addr, struct page *ipage)
  287. {
  288. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  289. size_t inline_size = 0;
  290. void *xattr_addr;
  291. struct page *xpage;
  292. nid_t new_nid = 0;
  293. int err;
  294. inline_size = inline_xattr_size(inode);
  295. if (hsize > inline_size && !F2FS_I(inode)->i_xattr_nid)
  296. if (!alloc_nid(sbi, &new_nid))
  297. return -ENOSPC;
  298. /* write to inline xattr */
  299. if (inline_size) {
  300. struct page *page = NULL;
  301. void *inline_addr;
  302. if (ipage) {
  303. inline_addr = inline_xattr_addr(ipage);
  304. f2fs_wait_on_page_writeback(ipage, NODE);
  305. } else {
  306. page = get_node_page(sbi, inode->i_ino);
  307. if (IS_ERR(page)) {
  308. alloc_nid_failed(sbi, new_nid);
  309. return PTR_ERR(page);
  310. }
  311. inline_addr = inline_xattr_addr(page);
  312. f2fs_wait_on_page_writeback(page, NODE);
  313. }
  314. memcpy(inline_addr, txattr_addr, inline_size);
  315. f2fs_put_page(page, 1);
  316. /* no need to use xattr node block */
  317. if (hsize <= inline_size) {
  318. err = truncate_xattr_node(inode, ipage);
  319. alloc_nid_failed(sbi, new_nid);
  320. return err;
  321. }
  322. }
  323. /* write to xattr node block */
  324. if (F2FS_I(inode)->i_xattr_nid) {
  325. xpage = get_node_page(sbi, F2FS_I(inode)->i_xattr_nid);
  326. if (IS_ERR(xpage)) {
  327. alloc_nid_failed(sbi, new_nid);
  328. return PTR_ERR(xpage);
  329. }
  330. f2fs_bug_on(sbi, new_nid);
  331. f2fs_wait_on_page_writeback(xpage, NODE);
  332. } else {
  333. struct dnode_of_data dn;
  334. set_new_dnode(&dn, inode, NULL, NULL, new_nid);
  335. xpage = new_node_page(&dn, XATTR_NODE_OFFSET, ipage);
  336. if (IS_ERR(xpage)) {
  337. alloc_nid_failed(sbi, new_nid);
  338. return PTR_ERR(xpage);
  339. }
  340. alloc_nid_done(sbi, new_nid);
  341. }
  342. xattr_addr = page_address(xpage);
  343. memcpy(xattr_addr, txattr_addr + inline_size, PAGE_SIZE -
  344. sizeof(struct node_footer));
  345. set_page_dirty(xpage);
  346. f2fs_put_page(xpage, 1);
  347. /* need to checkpoint during fsync */
  348. F2FS_I(inode)->xattr_ver = cur_cp_version(F2FS_CKPT(sbi));
  349. return 0;
  350. }
  351. int f2fs_getxattr(struct inode *inode, int index, const char *name,
  352. void *buffer, size_t buffer_size, struct page *ipage)
  353. {
  354. struct f2fs_xattr_entry *entry;
  355. void *base_addr;
  356. int error = 0;
  357. size_t size, len;
  358. if (name == NULL)
  359. return -EINVAL;
  360. len = strlen(name);
  361. if (len > F2FS_NAME_LEN)
  362. return -ERANGE;
  363. base_addr = read_all_xattrs(inode, ipage);
  364. if (!base_addr)
  365. return -ENOMEM;
  366. entry = __find_xattr(base_addr, index, len, name);
  367. if (IS_XATTR_LAST_ENTRY(entry)) {
  368. error = -ENODATA;
  369. goto cleanup;
  370. }
  371. size = le16_to_cpu(entry->e_value_size);
  372. if (buffer && size > buffer_size) {
  373. error = -ERANGE;
  374. goto cleanup;
  375. }
  376. if (buffer) {
  377. char *pval = entry->e_name + entry->e_name_len;
  378. memcpy(buffer, pval, size);
  379. }
  380. error = size;
  381. cleanup:
  382. kzfree(base_addr);
  383. return error;
  384. }
  385. ssize_t f2fs_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
  386. {
  387. struct inode *inode = d_inode(dentry);
  388. struct f2fs_xattr_entry *entry;
  389. void *base_addr;
  390. int error = 0;
  391. size_t rest = buffer_size;
  392. base_addr = read_all_xattrs(inode, NULL);
  393. if (!base_addr)
  394. return -ENOMEM;
  395. list_for_each_xattr(entry, base_addr) {
  396. const struct xattr_handler *handler =
  397. f2fs_xattr_handler(entry->e_name_index);
  398. size_t size;
  399. if (!handler)
  400. continue;
  401. size = handler->list(handler, dentry, buffer, rest,
  402. entry->e_name, entry->e_name_len);
  403. if (buffer && size > rest) {
  404. error = -ERANGE;
  405. goto cleanup;
  406. }
  407. if (buffer)
  408. buffer += size;
  409. rest -= size;
  410. }
  411. error = buffer_size - rest;
  412. cleanup:
  413. kzfree(base_addr);
  414. return error;
  415. }
  416. static int __f2fs_setxattr(struct inode *inode, int index,
  417. const char *name, const void *value, size_t size,
  418. struct page *ipage, int flags)
  419. {
  420. struct f2fs_inode_info *fi = F2FS_I(inode);
  421. struct f2fs_xattr_entry *here, *last;
  422. void *base_addr;
  423. int found, newsize;
  424. size_t len;
  425. __u32 new_hsize;
  426. int error = -ENOMEM;
  427. if (name == NULL)
  428. return -EINVAL;
  429. if (value == NULL)
  430. size = 0;
  431. len = strlen(name);
  432. if (len > F2FS_NAME_LEN)
  433. return -ERANGE;
  434. if (size > MAX_VALUE_LEN(inode))
  435. return -E2BIG;
  436. base_addr = read_all_xattrs(inode, ipage);
  437. if (!base_addr)
  438. goto exit;
  439. /* find entry with wanted name. */
  440. here = __find_xattr(base_addr, index, len, name);
  441. found = IS_XATTR_LAST_ENTRY(here) ? 0 : 1;
  442. if ((flags & XATTR_REPLACE) && !found) {
  443. error = -ENODATA;
  444. goto exit;
  445. } else if ((flags & XATTR_CREATE) && found) {
  446. error = -EEXIST;
  447. goto exit;
  448. }
  449. last = here;
  450. while (!IS_XATTR_LAST_ENTRY(last))
  451. last = XATTR_NEXT_ENTRY(last);
  452. newsize = XATTR_ALIGN(sizeof(struct f2fs_xattr_entry) + len + size);
  453. /* 1. Check space */
  454. if (value) {
  455. int free;
  456. /*
  457. * If value is NULL, it is remove operation.
  458. * In case of update operation, we calculate free.
  459. */
  460. free = MIN_OFFSET(inode) - ((char *)last - (char *)base_addr);
  461. if (found)
  462. free = free + ENTRY_SIZE(here);
  463. if (unlikely(free < newsize)) {
  464. error = -ENOSPC;
  465. goto exit;
  466. }
  467. }
  468. /* 2. Remove old entry */
  469. if (found) {
  470. /*
  471. * If entry is found, remove old entry.
  472. * If not found, remove operation is not needed.
  473. */
  474. struct f2fs_xattr_entry *next = XATTR_NEXT_ENTRY(here);
  475. int oldsize = ENTRY_SIZE(here);
  476. memmove(here, next, (char *)last - (char *)next);
  477. last = (struct f2fs_xattr_entry *)((char *)last - oldsize);
  478. memset(last, 0, oldsize);
  479. }
  480. new_hsize = (char *)last - (char *)base_addr;
  481. /* 3. Write new entry */
  482. if (value) {
  483. char *pval;
  484. /*
  485. * Before we come here, old entry is removed.
  486. * We just write new entry.
  487. */
  488. memset(last, 0, newsize);
  489. last->e_name_index = index;
  490. last->e_name_len = len;
  491. memcpy(last->e_name, name, len);
  492. pval = last->e_name + len;
  493. memcpy(pval, value, size);
  494. last->e_value_size = cpu_to_le16(size);
  495. new_hsize += newsize;
  496. }
  497. error = write_all_xattrs(inode, new_hsize, base_addr, ipage);
  498. if (error)
  499. goto exit;
  500. if (is_inode_flag_set(fi, FI_ACL_MODE)) {
  501. inode->i_mode = fi->i_acl_mode;
  502. inode->i_ctime = CURRENT_TIME;
  503. clear_inode_flag(fi, FI_ACL_MODE);
  504. }
  505. if (index == F2FS_XATTR_INDEX_ENCRYPTION &&
  506. !strcmp(name, F2FS_XATTR_NAME_ENCRYPTION_CONTEXT))
  507. f2fs_set_encrypted_inode(inode);
  508. if (ipage)
  509. update_inode(inode, ipage);
  510. else
  511. update_inode_page(inode);
  512. exit:
  513. kzfree(base_addr);
  514. return error;
  515. }
  516. int f2fs_setxattr(struct inode *inode, int index, const char *name,
  517. const void *value, size_t size,
  518. struct page *ipage, int flags)
  519. {
  520. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  521. int err;
  522. /* this case is only from init_inode_metadata */
  523. if (ipage)
  524. return __f2fs_setxattr(inode, index, name, value,
  525. size, ipage, flags);
  526. f2fs_balance_fs(sbi);
  527. f2fs_lock_op(sbi);
  528. /* protect xattr_ver */
  529. down_write(&F2FS_I(inode)->i_sem);
  530. err = __f2fs_setxattr(inode, index, name, value, size, ipage, flags);
  531. up_write(&F2FS_I(inode)->i_sem);
  532. f2fs_unlock_op(sbi);
  533. return err;
  534. }