xattr.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  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 name_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 + name_len + 1;
  54. if (list && total_len <= list_size) {
  55. memcpy(list, prefix, prefix_len);
  56. memcpy(list + prefix_len, name, name_len);
  57. list[prefix_len + name_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(dentry->d_inode, type, name, buffer, size);
  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(dentry->d_inode, type, name, value, size, NULL);
  104. }
  105. static size_t f2fs_xattr_advise_list(struct dentry *dentry, char *list,
  106. size_t list_size, const char *name, size_t name_len, int type)
  107. {
  108. const char *xname = F2FS_SYSTEM_ADVISE_PREFIX;
  109. size_t size;
  110. if (type != F2FS_XATTR_INDEX_ADVISE)
  111. return 0;
  112. size = strlen(xname) + 1;
  113. if (list && size <= list_size)
  114. memcpy(list, xname, size);
  115. return size;
  116. }
  117. static int f2fs_xattr_advise_get(struct dentry *dentry, const char *name,
  118. void *buffer, size_t size, int type)
  119. {
  120. struct inode *inode = dentry->d_inode;
  121. if (strcmp(name, "") != 0)
  122. return -EINVAL;
  123. *((char *)buffer) = F2FS_I(inode)->i_advise;
  124. return sizeof(char);
  125. }
  126. static int f2fs_xattr_advise_set(struct dentry *dentry, const char *name,
  127. const void *value, size_t size, int flags, int type)
  128. {
  129. struct inode *inode = dentry->d_inode;
  130. if (strcmp(name, "") != 0)
  131. return -EINVAL;
  132. if (!inode_owner_or_capable(inode))
  133. return -EPERM;
  134. if (value == NULL)
  135. return -EINVAL;
  136. F2FS_I(inode)->i_advise |= *(char *)value;
  137. return 0;
  138. }
  139. #ifdef CONFIG_F2FS_FS_SECURITY
  140. static int __f2fs_setxattr(struct inode *inode, int name_index,
  141. const char *name, const void *value, size_t value_len,
  142. struct page *ipage);
  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);
  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 name_index)
  218. {
  219. const struct xattr_handler *handler = NULL;
  220. if (name_index > 0 && name_index < ARRAY_SIZE(f2fs_xattr_handler_map))
  221. handler = f2fs_xattr_handler_map[name_index];
  222. return handler;
  223. }
  224. static struct f2fs_xattr_entry *__find_xattr(void *base_addr, int name_index,
  225. size_t name_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 != name_index)
  230. continue;
  231. if (entry->e_name_len != name_len)
  232. continue;
  233. if (!memcmp(entry->e_name, 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_SB(inode->i_sb);
  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_SB(inode->i_sb);
  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. } 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. }
  313. memcpy(inline_addr, txattr_addr, inline_size);
  314. f2fs_put_page(page, 1);
  315. /* no need to use xattr node block */
  316. if (hsize <= inline_size) {
  317. err = truncate_xattr_node(inode, ipage);
  318. alloc_nid_failed(sbi, new_nid);
  319. return err;
  320. }
  321. }
  322. /* write to xattr node block */
  323. if (F2FS_I(inode)->i_xattr_nid) {
  324. xpage = get_node_page(sbi, F2FS_I(inode)->i_xattr_nid);
  325. if (IS_ERR(xpage)) {
  326. alloc_nid_failed(sbi, new_nid);
  327. return PTR_ERR(xpage);
  328. }
  329. f2fs_bug_on(new_nid);
  330. } else {
  331. struct dnode_of_data dn;
  332. set_new_dnode(&dn, inode, NULL, NULL, new_nid);
  333. xpage = new_node_page(&dn, XATTR_NODE_OFFSET, ipage);
  334. if (IS_ERR(xpage)) {
  335. alloc_nid_failed(sbi, new_nid);
  336. return PTR_ERR(xpage);
  337. }
  338. alloc_nid_done(sbi, new_nid);
  339. }
  340. xattr_addr = page_address(xpage);
  341. memcpy(xattr_addr, txattr_addr + inline_size, PAGE_SIZE -
  342. sizeof(struct node_footer));
  343. set_page_dirty(xpage);
  344. f2fs_put_page(xpage, 1);
  345. /* need to checkpoint during fsync */
  346. F2FS_I(inode)->xattr_ver = cur_cp_version(F2FS_CKPT(sbi));
  347. return 0;
  348. }
  349. int f2fs_getxattr(struct inode *inode, int name_index, const char *name,
  350. void *buffer, size_t buffer_size)
  351. {
  352. struct f2fs_xattr_entry *entry;
  353. void *base_addr;
  354. int error = 0;
  355. size_t value_len, name_len;
  356. if (name == NULL)
  357. return -EINVAL;
  358. name_len = strlen(name);
  359. if (name_len > F2FS_NAME_LEN)
  360. return -ERANGE;
  361. base_addr = read_all_xattrs(inode, NULL);
  362. if (!base_addr)
  363. return -ENOMEM;
  364. entry = __find_xattr(base_addr, name_index, name_len, name);
  365. if (IS_XATTR_LAST_ENTRY(entry)) {
  366. error = -ENODATA;
  367. goto cleanup;
  368. }
  369. value_len = le16_to_cpu(entry->e_value_size);
  370. if (buffer && value_len > buffer_size) {
  371. error = -ERANGE;
  372. goto cleanup;
  373. }
  374. if (buffer) {
  375. char *pval = entry->e_name + entry->e_name_len;
  376. memcpy(buffer, pval, value_len);
  377. }
  378. error = value_len;
  379. cleanup:
  380. kzfree(base_addr);
  381. return error;
  382. }
  383. ssize_t f2fs_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
  384. {
  385. struct inode *inode = dentry->d_inode;
  386. struct f2fs_xattr_entry *entry;
  387. void *base_addr;
  388. int error = 0;
  389. size_t rest = buffer_size;
  390. base_addr = read_all_xattrs(inode, NULL);
  391. if (!base_addr)
  392. return -ENOMEM;
  393. list_for_each_xattr(entry, base_addr) {
  394. const struct xattr_handler *handler =
  395. f2fs_xattr_handler(entry->e_name_index);
  396. size_t size;
  397. if (!handler)
  398. continue;
  399. size = handler->list(dentry, buffer, rest, entry->e_name,
  400. entry->e_name_len, handler->flags);
  401. if (buffer && size > rest) {
  402. error = -ERANGE;
  403. goto cleanup;
  404. }
  405. if (buffer)
  406. buffer += size;
  407. rest -= size;
  408. }
  409. error = buffer_size - rest;
  410. cleanup:
  411. kzfree(base_addr);
  412. return error;
  413. }
  414. static int __f2fs_setxattr(struct inode *inode, int name_index,
  415. const char *name, const void *value, size_t value_len,
  416. struct page *ipage)
  417. {
  418. struct f2fs_inode_info *fi = F2FS_I(inode);
  419. struct f2fs_xattr_entry *here, *last;
  420. void *base_addr;
  421. int found, newsize;
  422. size_t name_len;
  423. __u32 new_hsize;
  424. int error = -ENOMEM;
  425. if (name == NULL)
  426. return -EINVAL;
  427. if (value == NULL)
  428. value_len = 0;
  429. name_len = strlen(name);
  430. if (name_len > F2FS_NAME_LEN || value_len > MAX_VALUE_LEN(inode))
  431. return -ERANGE;
  432. base_addr = read_all_xattrs(inode, ipage);
  433. if (!base_addr)
  434. goto exit;
  435. /* find entry with wanted name. */
  436. here = __find_xattr(base_addr, name_index, name_len, name);
  437. found = IS_XATTR_LAST_ENTRY(here) ? 0 : 1;
  438. last = here;
  439. while (!IS_XATTR_LAST_ENTRY(last))
  440. last = XATTR_NEXT_ENTRY(last);
  441. newsize = XATTR_ALIGN(sizeof(struct f2fs_xattr_entry) +
  442. name_len + value_len);
  443. /* 1. Check space */
  444. if (value) {
  445. int free;
  446. /*
  447. * If value is NULL, it is remove operation.
  448. * In case of update operation, we caculate free.
  449. */
  450. free = MIN_OFFSET(inode) - ((char *)last - (char *)base_addr);
  451. if (found)
  452. free = free + ENTRY_SIZE(here);
  453. if (unlikely(free < newsize)) {
  454. error = -ENOSPC;
  455. goto exit;
  456. }
  457. }
  458. /* 2. Remove old entry */
  459. if (found) {
  460. /*
  461. * If entry is found, remove old entry.
  462. * If not found, remove operation is not needed.
  463. */
  464. struct f2fs_xattr_entry *next = XATTR_NEXT_ENTRY(here);
  465. int oldsize = ENTRY_SIZE(here);
  466. memmove(here, next, (char *)last - (char *)next);
  467. last = (struct f2fs_xattr_entry *)((char *)last - oldsize);
  468. memset(last, 0, oldsize);
  469. }
  470. new_hsize = (char *)last - (char *)base_addr;
  471. /* 3. Write new entry */
  472. if (value) {
  473. char *pval;
  474. /*
  475. * Before we come here, old entry is removed.
  476. * We just write new entry.
  477. */
  478. memset(last, 0, newsize);
  479. last->e_name_index = name_index;
  480. last->e_name_len = name_len;
  481. memcpy(last->e_name, name, name_len);
  482. pval = last->e_name + name_len;
  483. memcpy(pval, value, value_len);
  484. last->e_value_size = cpu_to_le16(value_len);
  485. new_hsize += newsize;
  486. }
  487. error = write_all_xattrs(inode, new_hsize, base_addr, ipage);
  488. if (error)
  489. goto exit;
  490. if (is_inode_flag_set(fi, FI_ACL_MODE)) {
  491. inode->i_mode = fi->i_acl_mode;
  492. inode->i_ctime = CURRENT_TIME;
  493. clear_inode_flag(fi, FI_ACL_MODE);
  494. }
  495. if (ipage)
  496. update_inode(inode, ipage);
  497. else
  498. update_inode_page(inode);
  499. exit:
  500. kzfree(base_addr);
  501. return error;
  502. }
  503. int f2fs_setxattr(struct inode *inode, int name_index, const char *name,
  504. const void *value, size_t value_len, struct page *ipage)
  505. {
  506. struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
  507. int err;
  508. f2fs_balance_fs(sbi);
  509. f2fs_lock_op(sbi);
  510. /* protect xattr_ver */
  511. down_write(&F2FS_I(inode)->i_sem);
  512. err = __f2fs_setxattr(inode, name_index, name, value, value_len, ipage);
  513. up_write(&F2FS_I(inode)->i_sem);
  514. f2fs_unlock_op(sbi);
  515. return err;
  516. }