xattr.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680
  1. /*
  2. * This file is part of UBIFS.
  3. *
  4. * Copyright (C) 2006-2008 Nokia Corporation.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License version 2 as published by
  8. * the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along with
  16. * this program; if not, write to the Free Software Foundation, Inc., 51
  17. * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  18. *
  19. * Authors: Artem Bityutskiy (Битюцкий Артём)
  20. * Adrian Hunter
  21. */
  22. /*
  23. * This file implements UBIFS extended attributes support.
  24. *
  25. * Extended attributes are implemented as regular inodes with attached data,
  26. * which limits extended attribute size to UBIFS block size (4KiB). Names of
  27. * extended attributes are described by extended attribute entries (xentries),
  28. * which are almost identical to directory entries, but have different key type.
  29. *
  30. * In other words, the situation with extended attributes is very similar to
  31. * directories. Indeed, any inode (but of course not xattr inodes) may have a
  32. * number of associated xentries, just like directory inodes have associated
  33. * directory entries. Extended attribute entries store the name of the extended
  34. * attribute, the host inode number, and the extended attribute inode number.
  35. * Similarly, direntries store the name, the parent and the target inode
  36. * numbers. Thus, most of the common UBIFS mechanisms may be re-used for
  37. * extended attributes.
  38. *
  39. * The number of extended attributes is not limited, but there is Linux
  40. * limitation on the maximum possible size of the list of all extended
  41. * attributes associated with an inode (%XATTR_LIST_MAX), so UBIFS makes sure
  42. * the sum of all extended attribute names of the inode does not exceed that
  43. * limit.
  44. *
  45. * Extended attributes are synchronous, which means they are written to the
  46. * flash media synchronously and there is no write-back for extended attribute
  47. * inodes. The extended attribute values are not stored in compressed form on
  48. * the media.
  49. *
  50. * Since extended attributes are represented by regular inodes, they are cached
  51. * in the VFS inode cache. The xentries are cached in the LNC cache (see
  52. * tnc.c).
  53. *
  54. * ACL support is not implemented.
  55. */
  56. #include "ubifs.h"
  57. #include <linux/fs.h>
  58. #include <linux/slab.h>
  59. #include <linux/xattr.h>
  60. /*
  61. * Limit the number of extended attributes per inode so that the total size
  62. * (@xattr_size) is guaranteeded to fit in an 'unsigned int'.
  63. */
  64. #define MAX_XATTRS_PER_INODE 65535
  65. /*
  66. * Extended attribute type constants.
  67. *
  68. * USER_XATTR: user extended attribute ("user.*")
  69. * TRUSTED_XATTR: trusted extended attribute ("trusted.*)
  70. * SECURITY_XATTR: security extended attribute ("security.*")
  71. */
  72. enum {
  73. USER_XATTR,
  74. TRUSTED_XATTR,
  75. SECURITY_XATTR,
  76. };
  77. static const struct inode_operations empty_iops;
  78. static const struct file_operations empty_fops;
  79. /**
  80. * create_xattr - create an extended attribute.
  81. * @c: UBIFS file-system description object
  82. * @host: host inode
  83. * @nm: extended attribute name
  84. * @value: extended attribute value
  85. * @size: size of extended attribute value
  86. *
  87. * This is a helper function which creates an extended attribute of name @nm
  88. * and value @value for inode @host. The host inode is also updated on flash
  89. * because the ctime and extended attribute accounting data changes. This
  90. * function returns zero in case of success and a negative error code in case
  91. * of failure.
  92. */
  93. static int create_xattr(struct ubifs_info *c, struct inode *host,
  94. const struct fscrypt_name *nm, const void *value, int size)
  95. {
  96. int err, names_len;
  97. struct inode *inode;
  98. struct ubifs_inode *ui, *host_ui = ubifs_inode(host);
  99. struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1,
  100. .new_ino_d = ALIGN(size, 8), .dirtied_ino = 1,
  101. .dirtied_ino_d = ALIGN(host_ui->data_len, 8) };
  102. if (host_ui->xattr_cnt >= MAX_XATTRS_PER_INODE) {
  103. ubifs_err(c, "inode %lu already has too many xattrs (%d), cannot create more",
  104. host->i_ino, host_ui->xattr_cnt);
  105. return -ENOSPC;
  106. }
  107. /*
  108. * Linux limits the maximum size of the extended attribute names list
  109. * to %XATTR_LIST_MAX. This means we should not allow creating more
  110. * extended attributes if the name list becomes larger. This limitation
  111. * is artificial for UBIFS, though.
  112. */
  113. names_len = host_ui->xattr_names + host_ui->xattr_cnt + fname_len(nm) + 1;
  114. if (names_len > XATTR_LIST_MAX) {
  115. ubifs_err(c, "cannot add one more xattr name to inode %lu, total names length would become %d, max. is %d",
  116. host->i_ino, names_len, XATTR_LIST_MAX);
  117. return -ENOSPC;
  118. }
  119. err = ubifs_budget_space(c, &req);
  120. if (err)
  121. return err;
  122. inode = ubifs_new_inode(c, host, S_IFREG | S_IRWXUGO);
  123. if (IS_ERR(inode)) {
  124. err = PTR_ERR(inode);
  125. goto out_budg;
  126. }
  127. /* Re-define all operations to be "nothing" */
  128. inode->i_mapping->a_ops = &empty_aops;
  129. inode->i_op = &empty_iops;
  130. inode->i_fop = &empty_fops;
  131. inode->i_flags |= S_SYNC | S_NOATIME | S_NOCMTIME;
  132. ui = ubifs_inode(inode);
  133. ui->xattr = 1;
  134. ui->flags |= UBIFS_XATTR_FL;
  135. ui->data = kmemdup(value, size, GFP_NOFS);
  136. if (!ui->data) {
  137. err = -ENOMEM;
  138. goto out_free;
  139. }
  140. inode->i_size = ui->ui_size = size;
  141. ui->data_len = size;
  142. mutex_lock(&host_ui->ui_mutex);
  143. host->i_ctime = current_time(host);
  144. host_ui->xattr_cnt += 1;
  145. host_ui->xattr_size += CALC_DENT_SIZE(fname_len(nm));
  146. host_ui->xattr_size += CALC_XATTR_BYTES(size);
  147. host_ui->xattr_names += fname_len(nm);
  148. /*
  149. * We handle UBIFS_XATTR_NAME_ENCRYPTION_CONTEXT here because we
  150. * have to set the UBIFS_CRYPT_FL flag on the host inode.
  151. * To avoid multiple updates of the same inode in the same operation,
  152. * let's do it here.
  153. */
  154. if (strcmp(fname_name(nm), UBIFS_XATTR_NAME_ENCRYPTION_CONTEXT) == 0)
  155. host_ui->flags |= UBIFS_CRYPT_FL;
  156. err = ubifs_jnl_update(c, host, nm, inode, 0, 1);
  157. if (err)
  158. goto out_cancel;
  159. ubifs_set_inode_flags(host);
  160. mutex_unlock(&host_ui->ui_mutex);
  161. ubifs_release_budget(c, &req);
  162. insert_inode_hash(inode);
  163. iput(inode);
  164. return 0;
  165. out_cancel:
  166. host_ui->xattr_cnt -= 1;
  167. host_ui->xattr_size -= CALC_DENT_SIZE(fname_len(nm));
  168. host_ui->xattr_size -= CALC_XATTR_BYTES(size);
  169. host_ui->xattr_names -= fname_len(nm);
  170. host_ui->flags &= ~UBIFS_CRYPT_FL;
  171. mutex_unlock(&host_ui->ui_mutex);
  172. out_free:
  173. make_bad_inode(inode);
  174. iput(inode);
  175. out_budg:
  176. ubifs_release_budget(c, &req);
  177. return err;
  178. }
  179. /**
  180. * change_xattr - change an extended attribute.
  181. * @c: UBIFS file-system description object
  182. * @host: host inode
  183. * @inode: extended attribute inode
  184. * @value: extended attribute value
  185. * @size: size of extended attribute value
  186. *
  187. * This helper function changes the value of extended attribute @inode with new
  188. * data from @value. Returns zero in case of success and a negative error code
  189. * in case of failure.
  190. */
  191. static int change_xattr(struct ubifs_info *c, struct inode *host,
  192. struct inode *inode, const void *value, int size)
  193. {
  194. int err;
  195. struct ubifs_inode *host_ui = ubifs_inode(host);
  196. struct ubifs_inode *ui = ubifs_inode(inode);
  197. void *buf = NULL;
  198. int old_size;
  199. struct ubifs_budget_req req = { .dirtied_ino = 2,
  200. .dirtied_ino_d = ALIGN(size, 8) + ALIGN(host_ui->data_len, 8) };
  201. ubifs_assert(c, ui->data_len == inode->i_size);
  202. err = ubifs_budget_space(c, &req);
  203. if (err)
  204. return err;
  205. buf = kmemdup(value, size, GFP_NOFS);
  206. if (!buf) {
  207. err = -ENOMEM;
  208. goto out_free;
  209. }
  210. mutex_lock(&ui->ui_mutex);
  211. kfree(ui->data);
  212. ui->data = buf;
  213. inode->i_size = ui->ui_size = size;
  214. old_size = ui->data_len;
  215. ui->data_len = size;
  216. mutex_unlock(&ui->ui_mutex);
  217. mutex_lock(&host_ui->ui_mutex);
  218. host->i_ctime = current_time(host);
  219. host_ui->xattr_size -= CALC_XATTR_BYTES(old_size);
  220. host_ui->xattr_size += CALC_XATTR_BYTES(size);
  221. /*
  222. * It is important to write the host inode after the xattr inode
  223. * because if the host inode gets synchronized (via 'fsync()'), then
  224. * the extended attribute inode gets synchronized, because it goes
  225. * before the host inode in the write-buffer.
  226. */
  227. err = ubifs_jnl_change_xattr(c, inode, host);
  228. if (err)
  229. goto out_cancel;
  230. mutex_unlock(&host_ui->ui_mutex);
  231. ubifs_release_budget(c, &req);
  232. return 0;
  233. out_cancel:
  234. host_ui->xattr_size -= CALC_XATTR_BYTES(size);
  235. host_ui->xattr_size += CALC_XATTR_BYTES(old_size);
  236. mutex_unlock(&host_ui->ui_mutex);
  237. make_bad_inode(inode);
  238. out_free:
  239. ubifs_release_budget(c, &req);
  240. return err;
  241. }
  242. static struct inode *iget_xattr(struct ubifs_info *c, ino_t inum)
  243. {
  244. struct inode *inode;
  245. inode = ubifs_iget(c->vfs_sb, inum);
  246. if (IS_ERR(inode)) {
  247. ubifs_err(c, "dead extended attribute entry, error %d",
  248. (int)PTR_ERR(inode));
  249. return inode;
  250. }
  251. if (ubifs_inode(inode)->xattr)
  252. return inode;
  253. ubifs_err(c, "corrupt extended attribute entry");
  254. iput(inode);
  255. return ERR_PTR(-EINVAL);
  256. }
  257. int ubifs_xattr_set(struct inode *host, const char *name, const void *value,
  258. size_t size, int flags, bool check_lock)
  259. {
  260. struct inode *inode;
  261. struct ubifs_info *c = host->i_sb->s_fs_info;
  262. struct fscrypt_name nm = { .disk_name = FSTR_INIT((char *)name, strlen(name))};
  263. struct ubifs_dent_node *xent;
  264. union ubifs_key key;
  265. int err;
  266. if (check_lock)
  267. ubifs_assert(c, inode_is_locked(host));
  268. if (size > UBIFS_MAX_INO_DATA)
  269. return -ERANGE;
  270. if (fname_len(&nm) > UBIFS_MAX_NLEN)
  271. return -ENAMETOOLONG;
  272. xent = kmalloc(UBIFS_MAX_XENT_NODE_SZ, GFP_NOFS);
  273. if (!xent)
  274. return -ENOMEM;
  275. /*
  276. * The extended attribute entries are stored in LNC, so multiple
  277. * look-ups do not involve reading the flash.
  278. */
  279. xent_key_init(c, &key, host->i_ino, &nm);
  280. err = ubifs_tnc_lookup_nm(c, &key, xent, &nm);
  281. if (err) {
  282. if (err != -ENOENT)
  283. goto out_free;
  284. if (flags & XATTR_REPLACE)
  285. /* We are asked not to create the xattr */
  286. err = -ENODATA;
  287. else
  288. err = create_xattr(c, host, &nm, value, size);
  289. goto out_free;
  290. }
  291. if (flags & XATTR_CREATE) {
  292. /* We are asked not to replace the xattr */
  293. err = -EEXIST;
  294. goto out_free;
  295. }
  296. inode = iget_xattr(c, le64_to_cpu(xent->inum));
  297. if (IS_ERR(inode)) {
  298. err = PTR_ERR(inode);
  299. goto out_free;
  300. }
  301. err = change_xattr(c, host, inode, value, size);
  302. iput(inode);
  303. out_free:
  304. kfree(xent);
  305. return err;
  306. }
  307. ssize_t ubifs_xattr_get(struct inode *host, const char *name, void *buf,
  308. size_t size)
  309. {
  310. struct inode *inode;
  311. struct ubifs_info *c = host->i_sb->s_fs_info;
  312. struct fscrypt_name nm = { .disk_name = FSTR_INIT((char *)name, strlen(name))};
  313. struct ubifs_inode *ui;
  314. struct ubifs_dent_node *xent;
  315. union ubifs_key key;
  316. int err;
  317. if (fname_len(&nm) > UBIFS_MAX_NLEN)
  318. return -ENAMETOOLONG;
  319. xent = kmalloc(UBIFS_MAX_XENT_NODE_SZ, GFP_NOFS);
  320. if (!xent)
  321. return -ENOMEM;
  322. xent_key_init(c, &key, host->i_ino, &nm);
  323. err = ubifs_tnc_lookup_nm(c, &key, xent, &nm);
  324. if (err) {
  325. if (err == -ENOENT)
  326. err = -ENODATA;
  327. goto out_unlock;
  328. }
  329. inode = iget_xattr(c, le64_to_cpu(xent->inum));
  330. if (IS_ERR(inode)) {
  331. err = PTR_ERR(inode);
  332. goto out_unlock;
  333. }
  334. ui = ubifs_inode(inode);
  335. ubifs_assert(c, inode->i_size == ui->data_len);
  336. ubifs_assert(c, ubifs_inode(host)->xattr_size > ui->data_len);
  337. mutex_lock(&ui->ui_mutex);
  338. if (buf) {
  339. /* If @buf is %NULL we are supposed to return the length */
  340. if (ui->data_len > size) {
  341. err = -ERANGE;
  342. goto out_iput;
  343. }
  344. memcpy(buf, ui->data, ui->data_len);
  345. }
  346. err = ui->data_len;
  347. out_iput:
  348. mutex_unlock(&ui->ui_mutex);
  349. iput(inode);
  350. out_unlock:
  351. kfree(xent);
  352. return err;
  353. }
  354. static bool xattr_visible(const char *name)
  355. {
  356. /* File encryption related xattrs are for internal use only */
  357. if (strcmp(name, UBIFS_XATTR_NAME_ENCRYPTION_CONTEXT) == 0)
  358. return false;
  359. /* Show trusted namespace only for "power" users */
  360. if (strncmp(name, XATTR_TRUSTED_PREFIX,
  361. XATTR_TRUSTED_PREFIX_LEN) == 0 && !capable(CAP_SYS_ADMIN))
  362. return false;
  363. return true;
  364. }
  365. ssize_t ubifs_listxattr(struct dentry *dentry, char *buffer, size_t size)
  366. {
  367. union ubifs_key key;
  368. struct inode *host = d_inode(dentry);
  369. struct ubifs_info *c = host->i_sb->s_fs_info;
  370. struct ubifs_inode *host_ui = ubifs_inode(host);
  371. struct ubifs_dent_node *xent, *pxent = NULL;
  372. int err, len, written = 0;
  373. struct fscrypt_name nm = {0};
  374. dbg_gen("ino %lu ('%pd'), buffer size %zd", host->i_ino,
  375. dentry, size);
  376. len = host_ui->xattr_names + host_ui->xattr_cnt;
  377. if (!buffer)
  378. /*
  379. * We should return the minimum buffer size which will fit a
  380. * null-terminated list of all the extended attribute names.
  381. */
  382. return len;
  383. if (len > size)
  384. return -ERANGE;
  385. lowest_xent_key(c, &key, host->i_ino);
  386. while (1) {
  387. xent = ubifs_tnc_next_ent(c, &key, &nm);
  388. if (IS_ERR(xent)) {
  389. err = PTR_ERR(xent);
  390. break;
  391. }
  392. fname_name(&nm) = xent->name;
  393. fname_len(&nm) = le16_to_cpu(xent->nlen);
  394. if (xattr_visible(xent->name)) {
  395. memcpy(buffer + written, fname_name(&nm), fname_len(&nm) + 1);
  396. written += fname_len(&nm) + 1;
  397. }
  398. kfree(pxent);
  399. pxent = xent;
  400. key_read(c, &xent->key, &key);
  401. }
  402. kfree(pxent);
  403. if (err != -ENOENT) {
  404. ubifs_err(c, "cannot find next direntry, error %d", err);
  405. return err;
  406. }
  407. ubifs_assert(c, written <= size);
  408. return written;
  409. }
  410. static int remove_xattr(struct ubifs_info *c, struct inode *host,
  411. struct inode *inode, const struct fscrypt_name *nm)
  412. {
  413. int err;
  414. struct ubifs_inode *host_ui = ubifs_inode(host);
  415. struct ubifs_inode *ui = ubifs_inode(inode);
  416. struct ubifs_budget_req req = { .dirtied_ino = 2, .mod_dent = 1,
  417. .dirtied_ino_d = ALIGN(host_ui->data_len, 8) };
  418. ubifs_assert(c, ui->data_len == inode->i_size);
  419. err = ubifs_budget_space(c, &req);
  420. if (err)
  421. return err;
  422. mutex_lock(&host_ui->ui_mutex);
  423. host->i_ctime = current_time(host);
  424. host_ui->xattr_cnt -= 1;
  425. host_ui->xattr_size -= CALC_DENT_SIZE(fname_len(nm));
  426. host_ui->xattr_size -= CALC_XATTR_BYTES(ui->data_len);
  427. host_ui->xattr_names -= fname_len(nm);
  428. err = ubifs_jnl_delete_xattr(c, host, inode, nm);
  429. if (err)
  430. goto out_cancel;
  431. mutex_unlock(&host_ui->ui_mutex);
  432. ubifs_release_budget(c, &req);
  433. return 0;
  434. out_cancel:
  435. host_ui->xattr_cnt += 1;
  436. host_ui->xattr_size += CALC_DENT_SIZE(fname_len(nm));
  437. host_ui->xattr_size += CALC_XATTR_BYTES(ui->data_len);
  438. host_ui->xattr_names += fname_len(nm);
  439. mutex_unlock(&host_ui->ui_mutex);
  440. ubifs_release_budget(c, &req);
  441. make_bad_inode(inode);
  442. return err;
  443. }
  444. /**
  445. * ubifs_evict_xattr_inode - Evict an xattr inode.
  446. * @c: UBIFS file-system description object
  447. * @xattr_inum: xattr inode number
  448. *
  449. * When an inode that hosts xattrs is being removed we have to make sure
  450. * that cached inodes of the xattrs also get removed from the inode cache
  451. * otherwise we'd waste memory. This function looks up an inode from the
  452. * inode cache and clears the link counter such that iput() will evict
  453. * the inode.
  454. */
  455. void ubifs_evict_xattr_inode(struct ubifs_info *c, ino_t xattr_inum)
  456. {
  457. struct inode *inode;
  458. inode = ilookup(c->vfs_sb, xattr_inum);
  459. if (inode) {
  460. clear_nlink(inode);
  461. iput(inode);
  462. }
  463. }
  464. static int ubifs_xattr_remove(struct inode *host, const char *name)
  465. {
  466. struct inode *inode;
  467. struct ubifs_info *c = host->i_sb->s_fs_info;
  468. struct fscrypt_name nm = { .disk_name = FSTR_INIT((char *)name, strlen(name))};
  469. struct ubifs_dent_node *xent;
  470. union ubifs_key key;
  471. int err;
  472. ubifs_assert(c, inode_is_locked(host));
  473. if (fname_len(&nm) > UBIFS_MAX_NLEN)
  474. return -ENAMETOOLONG;
  475. xent = kmalloc(UBIFS_MAX_XENT_NODE_SZ, GFP_NOFS);
  476. if (!xent)
  477. return -ENOMEM;
  478. xent_key_init(c, &key, host->i_ino, &nm);
  479. err = ubifs_tnc_lookup_nm(c, &key, xent, &nm);
  480. if (err) {
  481. if (err == -ENOENT)
  482. err = -ENODATA;
  483. goto out_free;
  484. }
  485. inode = iget_xattr(c, le64_to_cpu(xent->inum));
  486. if (IS_ERR(inode)) {
  487. err = PTR_ERR(inode);
  488. goto out_free;
  489. }
  490. ubifs_assert(c, inode->i_nlink == 1);
  491. clear_nlink(inode);
  492. err = remove_xattr(c, host, inode, &nm);
  493. if (err)
  494. set_nlink(inode, 1);
  495. /* If @i_nlink is 0, 'iput()' will delete the inode */
  496. iput(inode);
  497. out_free:
  498. kfree(xent);
  499. return err;
  500. }
  501. #ifdef CONFIG_UBIFS_FS_SECURITY
  502. static int init_xattrs(struct inode *inode, const struct xattr *xattr_array,
  503. void *fs_info)
  504. {
  505. const struct xattr *xattr;
  506. char *name;
  507. int err = 0;
  508. for (xattr = xattr_array; xattr->name != NULL; xattr++) {
  509. name = kmalloc(XATTR_SECURITY_PREFIX_LEN +
  510. strlen(xattr->name) + 1, GFP_NOFS);
  511. if (!name) {
  512. err = -ENOMEM;
  513. break;
  514. }
  515. strcpy(name, XATTR_SECURITY_PREFIX);
  516. strcpy(name + XATTR_SECURITY_PREFIX_LEN, xattr->name);
  517. /*
  518. * creating a new inode without holding the inode rwsem,
  519. * no need to check whether inode is locked.
  520. */
  521. err = ubifs_xattr_set(inode, name, xattr->value,
  522. xattr->value_len, 0, false);
  523. kfree(name);
  524. if (err < 0)
  525. break;
  526. }
  527. return err;
  528. }
  529. int ubifs_init_security(struct inode *dentry, struct inode *inode,
  530. const struct qstr *qstr)
  531. {
  532. int err;
  533. err = security_inode_init_security(inode, dentry, qstr,
  534. &init_xattrs, 0);
  535. if (err) {
  536. struct ubifs_info *c = dentry->i_sb->s_fs_info;
  537. ubifs_err(c, "cannot initialize security for inode %lu, error %d",
  538. inode->i_ino, err);
  539. }
  540. return err;
  541. }
  542. #endif
  543. static int xattr_get(const struct xattr_handler *handler,
  544. struct dentry *dentry, struct inode *inode,
  545. const char *name, void *buffer, size_t size)
  546. {
  547. dbg_gen("xattr '%s', ino %lu ('%pd'), buf size %zd", name,
  548. inode->i_ino, dentry, size);
  549. name = xattr_full_name(handler, name);
  550. return ubifs_xattr_get(inode, name, buffer, size);
  551. }
  552. static int xattr_set(const struct xattr_handler *handler,
  553. struct dentry *dentry, struct inode *inode,
  554. const char *name, const void *value,
  555. size_t size, int flags)
  556. {
  557. dbg_gen("xattr '%s', host ino %lu ('%pd'), size %zd",
  558. name, inode->i_ino, dentry, size);
  559. name = xattr_full_name(handler, name);
  560. if (value)
  561. return ubifs_xattr_set(inode, name, value, size, flags, true);
  562. else
  563. return ubifs_xattr_remove(inode, name);
  564. }
  565. static const struct xattr_handler ubifs_user_xattr_handler = {
  566. .prefix = XATTR_USER_PREFIX,
  567. .get = xattr_get,
  568. .set = xattr_set,
  569. };
  570. static const struct xattr_handler ubifs_trusted_xattr_handler = {
  571. .prefix = XATTR_TRUSTED_PREFIX,
  572. .get = xattr_get,
  573. .set = xattr_set,
  574. };
  575. #ifdef CONFIG_UBIFS_FS_SECURITY
  576. static const struct xattr_handler ubifs_security_xattr_handler = {
  577. .prefix = XATTR_SECURITY_PREFIX,
  578. .get = xattr_get,
  579. .set = xattr_set,
  580. };
  581. #endif
  582. const struct xattr_handler *ubifs_xattr_handlers[] = {
  583. &ubifs_user_xattr_handler,
  584. &ubifs_trusted_xattr_handler,
  585. #ifdef CONFIG_UBIFS_FS_SECURITY
  586. &ubifs_security_xattr_handler,
  587. #endif
  588. NULL
  589. };