xattr.c 15 KB

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