xattr.c 14 KB

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