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