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 | S_NOQUOTA;
  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. mutex_unlock(&host_ui->ui_mutex);
  160. ubifs_release_budget(c, &req);
  161. insert_inode_hash(inode);
  162. iput(inode);
  163. return 0;
  164. out_cancel:
  165. host_ui->xattr_cnt -= 1;
  166. host_ui->xattr_size -= CALC_DENT_SIZE(fname_len(nm));
  167. host_ui->xattr_size -= CALC_XATTR_BYTES(size);
  168. host_ui->xattr_names -= fname_len(nm);
  169. host_ui->flags &= ~UBIFS_CRYPT_FL;
  170. mutex_unlock(&host_ui->ui_mutex);
  171. out_free:
  172. make_bad_inode(inode);
  173. iput(inode);
  174. out_budg:
  175. ubifs_release_budget(c, &req);
  176. return err;
  177. }
  178. /**
  179. * change_xattr - change an extended attribute.
  180. * @c: UBIFS file-system description object
  181. * @host: host inode
  182. * @inode: extended attribute inode
  183. * @value: extended attribute value
  184. * @size: size of extended attribute value
  185. *
  186. * This helper function changes the value of extended attribute @inode with new
  187. * data from @value. Returns zero in case of success and a negative error code
  188. * in case of failure.
  189. */
  190. static int change_xattr(struct ubifs_info *c, struct inode *host,
  191. struct inode *inode, const void *value, int size)
  192. {
  193. int err;
  194. struct ubifs_inode *host_ui = ubifs_inode(host);
  195. struct ubifs_inode *ui = ubifs_inode(inode);
  196. void *buf = NULL;
  197. int old_size;
  198. struct ubifs_budget_req req = { .dirtied_ino = 2,
  199. .dirtied_ino_d = ALIGN(size, 8) + ALIGN(host_ui->data_len, 8) };
  200. ubifs_assert(ui->data_len == inode->i_size);
  201. err = ubifs_budget_space(c, &req);
  202. if (err)
  203. return err;
  204. buf = kmemdup(value, size, GFP_NOFS);
  205. if (!buf) {
  206. err = -ENOMEM;
  207. goto out_free;
  208. }
  209. mutex_lock(&ui->ui_mutex);
  210. kfree(ui->data);
  211. ui->data = buf;
  212. inode->i_size = ui->ui_size = size;
  213. old_size = ui->data_len;
  214. ui->data_len = size;
  215. mutex_unlock(&ui->ui_mutex);
  216. mutex_lock(&host_ui->ui_mutex);
  217. host->i_ctime = current_time(host);
  218. host_ui->xattr_size -= CALC_XATTR_BYTES(old_size);
  219. host_ui->xattr_size += CALC_XATTR_BYTES(size);
  220. /*
  221. * It is important to write the host inode after the xattr inode
  222. * because if the host inode gets synchronized (via 'fsync()'), then
  223. * the extended attribute inode gets synchronized, because it goes
  224. * before the host inode in the write-buffer.
  225. */
  226. err = ubifs_jnl_change_xattr(c, inode, host);
  227. if (err)
  228. goto out_cancel;
  229. mutex_unlock(&host_ui->ui_mutex);
  230. ubifs_release_budget(c, &req);
  231. return 0;
  232. out_cancel:
  233. host_ui->xattr_size -= CALC_XATTR_BYTES(size);
  234. host_ui->xattr_size += CALC_XATTR_BYTES(old_size);
  235. mutex_unlock(&host_ui->ui_mutex);
  236. make_bad_inode(inode);
  237. out_free:
  238. ubifs_release_budget(c, &req);
  239. return err;
  240. }
  241. static struct inode *iget_xattr(struct ubifs_info *c, ino_t inum)
  242. {
  243. struct inode *inode;
  244. inode = ubifs_iget(c->vfs_sb, inum);
  245. if (IS_ERR(inode)) {
  246. ubifs_err(c, "dead extended attribute entry, error %d",
  247. (int)PTR_ERR(inode));
  248. return inode;
  249. }
  250. if (ubifs_inode(inode)->xattr)
  251. return inode;
  252. ubifs_err(c, "corrupt extended attribute entry");
  253. iput(inode);
  254. return ERR_PTR(-EINVAL);
  255. }
  256. int ubifs_xattr_set(struct inode *host, const char *name, const void *value,
  257. size_t size, int flags, bool check_lock)
  258. {
  259. struct inode *inode;
  260. struct ubifs_info *c = host->i_sb->s_fs_info;
  261. struct fscrypt_name nm = { .disk_name = FSTR_INIT((char *)name, strlen(name))};
  262. struct ubifs_dent_node *xent;
  263. union ubifs_key key;
  264. int err;
  265. if (check_lock)
  266. ubifs_assert(inode_is_locked(host));
  267. if (size > UBIFS_MAX_INO_DATA)
  268. return -ERANGE;
  269. if (fname_len(&nm) > UBIFS_MAX_NLEN)
  270. return -ENAMETOOLONG;
  271. xent = kmalloc(UBIFS_MAX_XENT_NODE_SZ, GFP_NOFS);
  272. if (!xent)
  273. return -ENOMEM;
  274. /*
  275. * The extended attribute entries are stored in LNC, so multiple
  276. * look-ups do not involve reading the flash.
  277. */
  278. xent_key_init(c, &key, host->i_ino, &nm);
  279. err = ubifs_tnc_lookup_nm(c, &key, xent, &nm);
  280. if (err) {
  281. if (err != -ENOENT)
  282. goto out_free;
  283. if (flags & XATTR_REPLACE)
  284. /* We are asked not to create the xattr */
  285. err = -ENODATA;
  286. else
  287. err = create_xattr(c, host, &nm, value, size);
  288. goto out_free;
  289. }
  290. if (flags & XATTR_CREATE) {
  291. /* We are asked not to replace the xattr */
  292. err = -EEXIST;
  293. goto out_free;
  294. }
  295. inode = iget_xattr(c, le64_to_cpu(xent->inum));
  296. if (IS_ERR(inode)) {
  297. err = PTR_ERR(inode);
  298. goto out_free;
  299. }
  300. err = change_xattr(c, host, inode, value, size);
  301. iput(inode);
  302. out_free:
  303. kfree(xent);
  304. return err;
  305. }
  306. ssize_t ubifs_xattr_get(struct inode *host, const char *name, void *buf,
  307. size_t size)
  308. {
  309. struct inode *inode;
  310. struct ubifs_info *c = host->i_sb->s_fs_info;
  311. struct fscrypt_name nm = { .disk_name = FSTR_INIT((char *)name, strlen(name))};
  312. struct ubifs_inode *ui;
  313. struct ubifs_dent_node *xent;
  314. union ubifs_key key;
  315. int err;
  316. if (fname_len(&nm) > UBIFS_MAX_NLEN)
  317. return -ENAMETOOLONG;
  318. xent = kmalloc(UBIFS_MAX_XENT_NODE_SZ, GFP_NOFS);
  319. if (!xent)
  320. return -ENOMEM;
  321. xent_key_init(c, &key, host->i_ino, &nm);
  322. err = ubifs_tnc_lookup_nm(c, &key, xent, &nm);
  323. if (err) {
  324. if (err == -ENOENT)
  325. err = -ENODATA;
  326. goto out_unlock;
  327. }
  328. inode = iget_xattr(c, le64_to_cpu(xent->inum));
  329. if (IS_ERR(inode)) {
  330. err = PTR_ERR(inode);
  331. goto out_unlock;
  332. }
  333. ui = ubifs_inode(inode);
  334. ubifs_assert(inode->i_size == ui->data_len);
  335. ubifs_assert(ubifs_inode(host)->xattr_size > ui->data_len);
  336. mutex_lock(&ui->ui_mutex);
  337. if (buf) {
  338. /* If @buf is %NULL we are supposed to return the length */
  339. if (ui->data_len > size) {
  340. ubifs_err(c, "buffer size %zd, xattr len %d",
  341. size, ui->data_len);
  342. err = -ERANGE;
  343. goto out_iput;
  344. }
  345. memcpy(buf, ui->data, ui->data_len);
  346. }
  347. err = ui->data_len;
  348. out_iput:
  349. mutex_unlock(&ui->ui_mutex);
  350. iput(inode);
  351. out_unlock:
  352. kfree(xent);
  353. return err;
  354. }
  355. static bool xattr_visible(const char *name)
  356. {
  357. /* File encryption related xattrs are for internal use only */
  358. if (strcmp(name, UBIFS_XATTR_NAME_ENCRYPTION_CONTEXT) == 0)
  359. return false;
  360. /* Show trusted namespace only for "power" users */
  361. if (strncmp(name, XATTR_TRUSTED_PREFIX,
  362. XATTR_TRUSTED_PREFIX_LEN) == 0 && !capable(CAP_SYS_ADMIN))
  363. return false;
  364. return true;
  365. }
  366. ssize_t ubifs_listxattr(struct dentry *dentry, char *buffer, size_t size)
  367. {
  368. union ubifs_key key;
  369. struct inode *host = d_inode(dentry);
  370. struct ubifs_info *c = host->i_sb->s_fs_info;
  371. struct ubifs_inode *host_ui = ubifs_inode(host);
  372. struct ubifs_dent_node *xent, *pxent = NULL;
  373. int err, len, written = 0;
  374. struct fscrypt_name nm = {0};
  375. dbg_gen("ino %lu ('%pd'), buffer size %zd", host->i_ino,
  376. dentry, size);
  377. len = host_ui->xattr_names + host_ui->xattr_cnt;
  378. if (!buffer)
  379. /*
  380. * We should return the minimum buffer size which will fit a
  381. * null-terminated list of all the extended attribute names.
  382. */
  383. return len;
  384. if (len > size)
  385. return -ERANGE;
  386. lowest_xent_key(c, &key, host->i_ino);
  387. while (1) {
  388. xent = ubifs_tnc_next_ent(c, &key, &nm);
  389. if (IS_ERR(xent)) {
  390. err = PTR_ERR(xent);
  391. break;
  392. }
  393. fname_name(&nm) = xent->name;
  394. fname_len(&nm) = le16_to_cpu(xent->nlen);
  395. if (xattr_visible(xent->name)) {
  396. memcpy(buffer + written, fname_name(&nm), fname_len(&nm) + 1);
  397. written += fname_len(&nm) + 1;
  398. }
  399. kfree(pxent);
  400. pxent = xent;
  401. key_read(c, &xent->key, &key);
  402. }
  403. kfree(pxent);
  404. if (err != -ENOENT) {
  405. ubifs_err(c, "cannot find next direntry, error %d", err);
  406. return err;
  407. }
  408. ubifs_assert(written <= size);
  409. return written;
  410. }
  411. static int remove_xattr(struct ubifs_info *c, struct inode *host,
  412. struct inode *inode, const struct fscrypt_name *nm)
  413. {
  414. int err;
  415. struct ubifs_inode *host_ui = ubifs_inode(host);
  416. struct ubifs_inode *ui = ubifs_inode(inode);
  417. struct ubifs_budget_req req = { .dirtied_ino = 2, .mod_dent = 1,
  418. .dirtied_ino_d = ALIGN(host_ui->data_len, 8) };
  419. ubifs_assert(ui->data_len == inode->i_size);
  420. err = ubifs_budget_space(c, &req);
  421. if (err)
  422. return err;
  423. mutex_lock(&host_ui->ui_mutex);
  424. host->i_ctime = current_time(host);
  425. host_ui->xattr_cnt -= 1;
  426. host_ui->xattr_size -= CALC_DENT_SIZE(fname_len(nm));
  427. host_ui->xattr_size -= CALC_XATTR_BYTES(ui->data_len);
  428. host_ui->xattr_names -= fname_len(nm);
  429. err = ubifs_jnl_delete_xattr(c, host, inode, nm);
  430. if (err)
  431. goto out_cancel;
  432. mutex_unlock(&host_ui->ui_mutex);
  433. ubifs_release_budget(c, &req);
  434. return 0;
  435. out_cancel:
  436. host_ui->xattr_cnt += 1;
  437. host_ui->xattr_size += CALC_DENT_SIZE(fname_len(nm));
  438. host_ui->xattr_size += CALC_XATTR_BYTES(ui->data_len);
  439. host_ui->xattr_names += fname_len(nm);
  440. mutex_unlock(&host_ui->ui_mutex);
  441. ubifs_release_budget(c, &req);
  442. make_bad_inode(inode);
  443. return err;
  444. }
  445. /**
  446. * ubifs_evict_xattr_inode - Evict an xattr inode.
  447. * @c: UBIFS file-system description object
  448. * @xattr_inum: xattr inode number
  449. *
  450. * When an inode that hosts xattrs is being removed we have to make sure
  451. * that cached inodes of the xattrs also get removed from the inode cache
  452. * otherwise we'd waste memory. This function looks up an inode from the
  453. * inode cache and clears the link counter such that iput() will evict
  454. * the inode.
  455. */
  456. void ubifs_evict_xattr_inode(struct ubifs_info *c, ino_t xattr_inum)
  457. {
  458. struct inode *inode;
  459. inode = ilookup(c->vfs_sb, xattr_inum);
  460. if (inode) {
  461. clear_nlink(inode);
  462. iput(inode);
  463. }
  464. }
  465. static int ubifs_xattr_remove(struct inode *host, const char *name)
  466. {
  467. struct inode *inode;
  468. struct ubifs_info *c = host->i_sb->s_fs_info;
  469. struct fscrypt_name nm = { .disk_name = FSTR_INIT((char *)name, strlen(name))};
  470. struct ubifs_dent_node *xent;
  471. union ubifs_key key;
  472. int err;
  473. ubifs_assert(inode_is_locked(host));
  474. if (fname_len(&nm) > UBIFS_MAX_NLEN)
  475. return -ENAMETOOLONG;
  476. xent = kmalloc(UBIFS_MAX_XENT_NODE_SZ, GFP_NOFS);
  477. if (!xent)
  478. return -ENOMEM;
  479. xent_key_init(c, &key, host->i_ino, &nm);
  480. err = ubifs_tnc_lookup_nm(c, &key, xent, &nm);
  481. if (err) {
  482. if (err == -ENOENT)
  483. err = -ENODATA;
  484. goto out_free;
  485. }
  486. inode = iget_xattr(c, le64_to_cpu(xent->inum));
  487. if (IS_ERR(inode)) {
  488. err = PTR_ERR(inode);
  489. goto out_free;
  490. }
  491. ubifs_assert(inode->i_nlink == 1);
  492. clear_nlink(inode);
  493. err = remove_xattr(c, host, inode, &nm);
  494. if (err)
  495. set_nlink(inode, 1);
  496. /* If @i_nlink is 0, 'iput()' will delete the inode */
  497. iput(inode);
  498. out_free:
  499. kfree(xent);
  500. return err;
  501. }
  502. #ifdef CONFIG_UBIFS_FS_SECURITY
  503. static int init_xattrs(struct inode *inode, const struct xattr *xattr_array,
  504. void *fs_info)
  505. {
  506. const struct xattr *xattr;
  507. char *name;
  508. int err = 0;
  509. for (xattr = xattr_array; xattr->name != NULL; xattr++) {
  510. name = kmalloc(XATTR_SECURITY_PREFIX_LEN +
  511. strlen(xattr->name) + 1, GFP_NOFS);
  512. if (!name) {
  513. err = -ENOMEM;
  514. break;
  515. }
  516. strcpy(name, XATTR_SECURITY_PREFIX);
  517. strcpy(name + XATTR_SECURITY_PREFIX_LEN, xattr->name);
  518. /*
  519. * creating a new inode without holding the inode rwsem,
  520. * no need to check whether inode is locked.
  521. */
  522. err = ubifs_xattr_set(inode, name, xattr->value,
  523. xattr->value_len, 0, false);
  524. kfree(name);
  525. if (err < 0)
  526. break;
  527. }
  528. return err;
  529. }
  530. int ubifs_init_security(struct inode *dentry, struct inode *inode,
  531. const struct qstr *qstr)
  532. {
  533. int err;
  534. err = security_inode_init_security(inode, dentry, qstr,
  535. &init_xattrs, 0);
  536. if (err) {
  537. struct ubifs_info *c = dentry->i_sb->s_fs_info;
  538. ubifs_err(c, "cannot initialize security for inode %lu, error %d",
  539. inode->i_ino, err);
  540. }
  541. return err;
  542. }
  543. #endif
  544. static int xattr_get(const struct xattr_handler *handler,
  545. struct dentry *dentry, struct inode *inode,
  546. const char *name, void *buffer, size_t size)
  547. {
  548. dbg_gen("xattr '%s', ino %lu ('%pd'), buf size %zd", name,
  549. inode->i_ino, dentry, size);
  550. name = xattr_full_name(handler, name);
  551. return ubifs_xattr_get(inode, name, buffer, size);
  552. }
  553. static int xattr_set(const struct xattr_handler *handler,
  554. struct dentry *dentry, struct inode *inode,
  555. const char *name, const void *value,
  556. size_t size, int flags)
  557. {
  558. dbg_gen("xattr '%s', host ino %lu ('%pd'), size %zd",
  559. name, inode->i_ino, dentry, size);
  560. name = xattr_full_name(handler, name);
  561. if (value)
  562. return ubifs_xattr_set(inode, name, value, size, flags, true);
  563. else
  564. return ubifs_xattr_remove(inode, name);
  565. }
  566. static const struct xattr_handler ubifs_user_xattr_handler = {
  567. .prefix = XATTR_USER_PREFIX,
  568. .get = xattr_get,
  569. .set = xattr_set,
  570. };
  571. static const struct xattr_handler ubifs_trusted_xattr_handler = {
  572. .prefix = XATTR_TRUSTED_PREFIX,
  573. .get = xattr_get,
  574. .set = xattr_set,
  575. };
  576. #ifdef CONFIG_UBIFS_FS_SECURITY
  577. static const struct xattr_handler ubifs_security_xattr_handler = {
  578. .prefix = XATTR_SECURITY_PREFIX,
  579. .get = xattr_get,
  580. .set = xattr_set,
  581. };
  582. #endif
  583. const struct xattr_handler *ubifs_xattr_handlers[] = {
  584. &ubifs_user_xattr_handler,
  585. &ubifs_trusted_xattr_handler,
  586. #ifdef CONFIG_UBIFS_FS_SECURITY
  587. &ubifs_security_xattr_handler,
  588. #endif
  589. NULL
  590. };