xattr.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664
  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. #include <linux/posix_acl_xattr.h>
  61. /*
  62. * Limit the number of extended attributes per inode so that the total size
  63. * (@xattr_size) is guaranteeded to fit in an 'unsigned int'.
  64. */
  65. #define MAX_XATTRS_PER_INODE 65535
  66. /*
  67. * Extended attribute type constants.
  68. *
  69. * USER_XATTR: user extended attribute ("user.*")
  70. * TRUSTED_XATTR: trusted extended attribute ("trusted.*)
  71. * SECURITY_XATTR: security extended attribute ("security.*")
  72. */
  73. enum {
  74. USER_XATTR,
  75. TRUSTED_XATTR,
  76. SECURITY_XATTR,
  77. };
  78. static const struct inode_operations empty_iops;
  79. static const struct file_operations empty_fops;
  80. /**
  81. * create_xattr - create an extended attribute.
  82. * @c: UBIFS file-system description object
  83. * @host: host inode
  84. * @nm: extended attribute name
  85. * @value: extended attribute value
  86. * @size: size of extended attribute value
  87. *
  88. * This is a helper function which creates an extended attribute of name @nm
  89. * and value @value for inode @host. The host inode is also updated on flash
  90. * because the ctime and extended attribute accounting data changes. This
  91. * function returns zero in case of success and a negative error code in case
  92. * of failure.
  93. */
  94. static int create_xattr(struct ubifs_info *c, struct inode *host,
  95. const struct qstr *nm, const void *value, int size)
  96. {
  97. int err, names_len;
  98. struct inode *inode;
  99. struct ubifs_inode *ui, *host_ui = ubifs_inode(host);
  100. struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1,
  101. .new_ino_d = ALIGN(size, 8), .dirtied_ino = 1,
  102. .dirtied_ino_d = ALIGN(host_ui->data_len, 8) };
  103. if (host_ui->xattr_cnt >= MAX_XATTRS_PER_INODE) {
  104. ubifs_err(c, "inode %lu already has too many xattrs (%d), cannot create more",
  105. host->i_ino, host_ui->xattr_cnt);
  106. return -ENOSPC;
  107. }
  108. /*
  109. * Linux limits the maximum size of the extended attribute names list
  110. * to %XATTR_LIST_MAX. This means we should not allow creating more
  111. * extended attributes if the name list becomes larger. This limitation
  112. * is artificial for UBIFS, though.
  113. */
  114. names_len = host_ui->xattr_names + host_ui->xattr_cnt + nm->len + 1;
  115. if (names_len > XATTR_LIST_MAX) {
  116. ubifs_err(c, "cannot add one more xattr name to inode %lu, total names length would become %d, max. is %d",
  117. host->i_ino, names_len, XATTR_LIST_MAX);
  118. return -ENOSPC;
  119. }
  120. err = ubifs_budget_space(c, &req);
  121. if (err)
  122. return err;
  123. inode = ubifs_new_inode(c, host, S_IFREG | S_IRWXUGO);
  124. if (IS_ERR(inode)) {
  125. err = PTR_ERR(inode);
  126. goto out_budg;
  127. }
  128. /* Re-define all operations to be "nothing" */
  129. inode->i_mapping->a_ops = &empty_aops;
  130. inode->i_op = &empty_iops;
  131. inode->i_fop = &empty_fops;
  132. inode->i_flags |= S_SYNC | S_NOATIME | S_NOCMTIME | S_NOQUOTA;
  133. ui = ubifs_inode(inode);
  134. ui->xattr = 1;
  135. ui->flags |= UBIFS_XATTR_FL;
  136. ui->data = kmemdup(value, size, GFP_NOFS);
  137. if (!ui->data) {
  138. err = -ENOMEM;
  139. goto out_free;
  140. }
  141. inode->i_size = ui->ui_size = size;
  142. ui->data_len = size;
  143. mutex_lock(&host_ui->ui_mutex);
  144. host->i_ctime = ubifs_current_time(host);
  145. host_ui->xattr_cnt += 1;
  146. host_ui->xattr_size += CALC_DENT_SIZE(nm->len);
  147. host_ui->xattr_size += CALC_XATTR_BYTES(size);
  148. host_ui->xattr_names += nm->len;
  149. err = ubifs_jnl_update(c, host, nm, inode, 0, 1);
  150. if (err)
  151. goto out_cancel;
  152. mutex_unlock(&host_ui->ui_mutex);
  153. ubifs_release_budget(c, &req);
  154. insert_inode_hash(inode);
  155. iput(inode);
  156. return 0;
  157. out_cancel:
  158. host_ui->xattr_cnt -= 1;
  159. host_ui->xattr_size -= CALC_DENT_SIZE(nm->len);
  160. host_ui->xattr_size -= CALC_XATTR_BYTES(size);
  161. mutex_unlock(&host_ui->ui_mutex);
  162. out_free:
  163. make_bad_inode(inode);
  164. iput(inode);
  165. out_budg:
  166. ubifs_release_budget(c, &req);
  167. return err;
  168. }
  169. /**
  170. * change_xattr - change an extended attribute.
  171. * @c: UBIFS file-system description object
  172. * @host: host inode
  173. * @inode: extended attribute inode
  174. * @value: extended attribute value
  175. * @size: size of extended attribute value
  176. *
  177. * This helper function changes the value of extended attribute @inode with new
  178. * data from @value. Returns zero in case of success and a negative error code
  179. * in case of failure.
  180. */
  181. static int change_xattr(struct ubifs_info *c, struct inode *host,
  182. struct inode *inode, const void *value, int size)
  183. {
  184. int err;
  185. struct ubifs_inode *host_ui = ubifs_inode(host);
  186. struct ubifs_inode *ui = ubifs_inode(inode);
  187. struct ubifs_budget_req req = { .dirtied_ino = 2,
  188. .dirtied_ino_d = ALIGN(size, 8) + ALIGN(host_ui->data_len, 8) };
  189. ubifs_assert(ui->data_len == inode->i_size);
  190. err = ubifs_budget_space(c, &req);
  191. if (err)
  192. return err;
  193. kfree(ui->data);
  194. ui->data = kmemdup(value, size, GFP_NOFS);
  195. if (!ui->data) {
  196. err = -ENOMEM;
  197. goto out_free;
  198. }
  199. inode->i_size = ui->ui_size = size;
  200. ui->data_len = size;
  201. mutex_lock(&host_ui->ui_mutex);
  202. host->i_ctime = ubifs_current_time(host);
  203. host_ui->xattr_size -= CALC_XATTR_BYTES(ui->data_len);
  204. host_ui->xattr_size += CALC_XATTR_BYTES(size);
  205. /*
  206. * It is important to write the host inode after the xattr inode
  207. * because if the host inode gets synchronized (via 'fsync()'), then
  208. * the extended attribute inode gets synchronized, because it goes
  209. * before the host inode in the write-buffer.
  210. */
  211. err = ubifs_jnl_change_xattr(c, inode, host);
  212. if (err)
  213. goto out_cancel;
  214. mutex_unlock(&host_ui->ui_mutex);
  215. ubifs_release_budget(c, &req);
  216. return 0;
  217. out_cancel:
  218. host_ui->xattr_size -= CALC_XATTR_BYTES(size);
  219. host_ui->xattr_size += CALC_XATTR_BYTES(ui->data_len);
  220. mutex_unlock(&host_ui->ui_mutex);
  221. make_bad_inode(inode);
  222. out_free:
  223. ubifs_release_budget(c, &req);
  224. return err;
  225. }
  226. /**
  227. * check_namespace - check extended attribute name-space.
  228. * @nm: extended attribute name
  229. *
  230. * This function makes sure the extended attribute name belongs to one of the
  231. * supported extended attribute name-spaces. Returns name-space index in case
  232. * of success and a negative error code in case of failure.
  233. */
  234. static int check_namespace(const struct qstr *nm)
  235. {
  236. int type;
  237. if (nm->len > UBIFS_MAX_NLEN)
  238. return -ENAMETOOLONG;
  239. if (!strncmp(nm->name, XATTR_TRUSTED_PREFIX,
  240. XATTR_TRUSTED_PREFIX_LEN)) {
  241. if (nm->name[sizeof(XATTR_TRUSTED_PREFIX) - 1] == '\0')
  242. return -EINVAL;
  243. type = TRUSTED_XATTR;
  244. } else if (!strncmp(nm->name, XATTR_USER_PREFIX,
  245. XATTR_USER_PREFIX_LEN)) {
  246. if (nm->name[XATTR_USER_PREFIX_LEN] == '\0')
  247. return -EINVAL;
  248. type = USER_XATTR;
  249. } else if (!strncmp(nm->name, XATTR_SECURITY_PREFIX,
  250. XATTR_SECURITY_PREFIX_LEN)) {
  251. if (nm->name[sizeof(XATTR_SECURITY_PREFIX) - 1] == '\0')
  252. return -EINVAL;
  253. type = SECURITY_XATTR;
  254. } else
  255. return -EOPNOTSUPP;
  256. return type;
  257. }
  258. static struct inode *iget_xattr(struct ubifs_info *c, ino_t inum)
  259. {
  260. struct inode *inode;
  261. inode = ubifs_iget(c->vfs_sb, inum);
  262. if (IS_ERR(inode)) {
  263. ubifs_err(c, "dead extended attribute entry, error %d",
  264. (int)PTR_ERR(inode));
  265. return inode;
  266. }
  267. if (ubifs_inode(inode)->xattr)
  268. return inode;
  269. ubifs_err(c, "corrupt extended attribute entry");
  270. iput(inode);
  271. return ERR_PTR(-EINVAL);
  272. }
  273. static int setxattr(struct inode *host, const char *name, const void *value,
  274. size_t size, int flags)
  275. {
  276. struct inode *inode;
  277. struct ubifs_info *c = host->i_sb->s_fs_info;
  278. struct qstr nm = QSTR_INIT(name, strlen(name));
  279. struct ubifs_dent_node *xent;
  280. union ubifs_key key;
  281. int err, type;
  282. ubifs_assert(mutex_is_locked(&host->i_mutex));
  283. if (size > UBIFS_MAX_INO_DATA)
  284. return -ERANGE;
  285. type = check_namespace(&nm);
  286. if (type < 0)
  287. return type;
  288. xent = kmalloc(UBIFS_MAX_XENT_NODE_SZ, GFP_NOFS);
  289. if (!xent)
  290. return -ENOMEM;
  291. /*
  292. * The extended attribute entries are stored in LNC, so multiple
  293. * look-ups do not involve reading the flash.
  294. */
  295. xent_key_init(c, &key, host->i_ino, &nm);
  296. err = ubifs_tnc_lookup_nm(c, &key, xent, &nm);
  297. if (err) {
  298. if (err != -ENOENT)
  299. goto out_free;
  300. if (flags & XATTR_REPLACE)
  301. /* We are asked not to create the xattr */
  302. err = -ENODATA;
  303. else
  304. err = create_xattr(c, host, &nm, value, size);
  305. goto out_free;
  306. }
  307. if (flags & XATTR_CREATE) {
  308. /* We are asked not to replace the xattr */
  309. err = -EEXIST;
  310. goto out_free;
  311. }
  312. inode = iget_xattr(c, le64_to_cpu(xent->inum));
  313. if (IS_ERR(inode)) {
  314. err = PTR_ERR(inode);
  315. goto out_free;
  316. }
  317. err = change_xattr(c, host, inode, value, size);
  318. iput(inode);
  319. out_free:
  320. kfree(xent);
  321. return err;
  322. }
  323. int ubifs_setxattr(struct dentry *dentry, const char *name,
  324. const void *value, size_t size, int flags)
  325. {
  326. dbg_gen("xattr '%s', host ino %lu ('%pd'), size %zd",
  327. name, d_inode(dentry)->i_ino, dentry, size);
  328. return setxattr(d_inode(dentry), name, value, size, flags);
  329. }
  330. ssize_t ubifs_getxattr(struct dentry *dentry, const char *name, void *buf,
  331. size_t size)
  332. {
  333. struct inode *inode, *host = d_inode(dentry);
  334. struct ubifs_info *c = host->i_sb->s_fs_info;
  335. struct qstr nm = QSTR_INIT(name, strlen(name));
  336. struct ubifs_inode *ui;
  337. struct ubifs_dent_node *xent;
  338. union ubifs_key key;
  339. int err;
  340. dbg_gen("xattr '%s', ino %lu ('%pd'), buf size %zd", name,
  341. host->i_ino, dentry, size);
  342. err = check_namespace(&nm);
  343. if (err < 0)
  344. return err;
  345. xent = kmalloc(UBIFS_MAX_XENT_NODE_SZ, GFP_NOFS);
  346. if (!xent)
  347. return -ENOMEM;
  348. xent_key_init(c, &key, host->i_ino, &nm);
  349. err = ubifs_tnc_lookup_nm(c, &key, xent, &nm);
  350. if (err) {
  351. if (err == -ENOENT)
  352. err = -ENODATA;
  353. goto out_unlock;
  354. }
  355. inode = iget_xattr(c, le64_to_cpu(xent->inum));
  356. if (IS_ERR(inode)) {
  357. err = PTR_ERR(inode);
  358. goto out_unlock;
  359. }
  360. ui = ubifs_inode(inode);
  361. ubifs_assert(inode->i_size == ui->data_len);
  362. ubifs_assert(ubifs_inode(host)->xattr_size > ui->data_len);
  363. if (buf) {
  364. /* If @buf is %NULL we are supposed to return the length */
  365. if (ui->data_len > size) {
  366. ubifs_err(c, "buffer size %zd, xattr len %d",
  367. size, ui->data_len);
  368. err = -ERANGE;
  369. goto out_iput;
  370. }
  371. memcpy(buf, ui->data, ui->data_len);
  372. }
  373. err = ui->data_len;
  374. out_iput:
  375. iput(inode);
  376. out_unlock:
  377. kfree(xent);
  378. return err;
  379. }
  380. ssize_t ubifs_listxattr(struct dentry *dentry, char *buffer, size_t size)
  381. {
  382. union ubifs_key key;
  383. struct inode *host = d_inode(dentry);
  384. struct ubifs_info *c = host->i_sb->s_fs_info;
  385. struct ubifs_inode *host_ui = ubifs_inode(host);
  386. struct ubifs_dent_node *xent, *pxent = NULL;
  387. int err, len, written = 0;
  388. struct qstr nm = { .name = NULL };
  389. dbg_gen("ino %lu ('%pd'), buffer size %zd", host->i_ino,
  390. dentry, size);
  391. len = host_ui->xattr_names + host_ui->xattr_cnt;
  392. if (!buffer)
  393. /*
  394. * We should return the minimum buffer size which will fit a
  395. * null-terminated list of all the extended attribute names.
  396. */
  397. return len;
  398. if (len > size)
  399. return -ERANGE;
  400. lowest_xent_key(c, &key, host->i_ino);
  401. while (1) {
  402. int type;
  403. xent = ubifs_tnc_next_ent(c, &key, &nm);
  404. if (IS_ERR(xent)) {
  405. err = PTR_ERR(xent);
  406. break;
  407. }
  408. nm.name = xent->name;
  409. nm.len = le16_to_cpu(xent->nlen);
  410. type = check_namespace(&nm);
  411. if (unlikely(type < 0)) {
  412. err = type;
  413. break;
  414. }
  415. /* Show trusted namespace only for "power" users */
  416. if (type != TRUSTED_XATTR || capable(CAP_SYS_ADMIN)) {
  417. memcpy(buffer + written, nm.name, nm.len + 1);
  418. written += nm.len + 1;
  419. }
  420. kfree(pxent);
  421. pxent = xent;
  422. key_read(c, &xent->key, &key);
  423. }
  424. kfree(pxent);
  425. if (err != -ENOENT) {
  426. ubifs_err(c, "cannot find next direntry, error %d", err);
  427. return err;
  428. }
  429. ubifs_assert(written <= size);
  430. return written;
  431. }
  432. static int remove_xattr(struct ubifs_info *c, struct inode *host,
  433. struct inode *inode, const struct qstr *nm)
  434. {
  435. int err;
  436. struct ubifs_inode *host_ui = ubifs_inode(host);
  437. struct ubifs_inode *ui = ubifs_inode(inode);
  438. struct ubifs_budget_req req = { .dirtied_ino = 2, .mod_dent = 1,
  439. .dirtied_ino_d = ALIGN(host_ui->data_len, 8) };
  440. ubifs_assert(ui->data_len == inode->i_size);
  441. err = ubifs_budget_space(c, &req);
  442. if (err)
  443. return err;
  444. mutex_lock(&host_ui->ui_mutex);
  445. host->i_ctime = ubifs_current_time(host);
  446. host_ui->xattr_cnt -= 1;
  447. host_ui->xattr_size -= CALC_DENT_SIZE(nm->len);
  448. host_ui->xattr_size -= CALC_XATTR_BYTES(ui->data_len);
  449. host_ui->xattr_names -= nm->len;
  450. err = ubifs_jnl_delete_xattr(c, host, inode, nm);
  451. if (err)
  452. goto out_cancel;
  453. mutex_unlock(&host_ui->ui_mutex);
  454. ubifs_release_budget(c, &req);
  455. return 0;
  456. out_cancel:
  457. host_ui->xattr_cnt += 1;
  458. host_ui->xattr_size += CALC_DENT_SIZE(nm->len);
  459. host_ui->xattr_size += CALC_XATTR_BYTES(ui->data_len);
  460. mutex_unlock(&host_ui->ui_mutex);
  461. ubifs_release_budget(c, &req);
  462. make_bad_inode(inode);
  463. return err;
  464. }
  465. int ubifs_removexattr(struct dentry *dentry, const char *name)
  466. {
  467. struct inode *inode, *host = d_inode(dentry);
  468. struct ubifs_info *c = host->i_sb->s_fs_info;
  469. struct qstr nm = QSTR_INIT(name, strlen(name));
  470. struct ubifs_dent_node *xent;
  471. union ubifs_key key;
  472. int err;
  473. dbg_gen("xattr '%s', ino %lu ('%pd')", name,
  474. host->i_ino, dentry);
  475. ubifs_assert(mutex_is_locked(&host->i_mutex));
  476. err = check_namespace(&nm);
  477. if (err < 0)
  478. return err;
  479. xent = kmalloc(UBIFS_MAX_XENT_NODE_SZ, GFP_NOFS);
  480. if (!xent)
  481. return -ENOMEM;
  482. xent_key_init(c, &key, host->i_ino, &nm);
  483. err = ubifs_tnc_lookup_nm(c, &key, xent, &nm);
  484. if (err) {
  485. if (err == -ENOENT)
  486. err = -ENODATA;
  487. goto out_free;
  488. }
  489. inode = iget_xattr(c, le64_to_cpu(xent->inum));
  490. if (IS_ERR(inode)) {
  491. err = PTR_ERR(inode);
  492. goto out_free;
  493. }
  494. ubifs_assert(inode->i_nlink == 1);
  495. clear_nlink(inode);
  496. err = remove_xattr(c, host, inode, &nm);
  497. if (err)
  498. set_nlink(inode, 1);
  499. /* If @i_nlink is 0, 'iput()' will delete the inode */
  500. iput(inode);
  501. out_free:
  502. kfree(xent);
  503. return err;
  504. }
  505. static size_t security_listxattr(struct dentry *d, char *list, size_t list_size,
  506. const char *name, size_t name_len, int flags)
  507. {
  508. const int prefix_len = XATTR_SECURITY_PREFIX_LEN;
  509. const size_t total_len = prefix_len + name_len + 1;
  510. if (list && total_len <= list_size) {
  511. memcpy(list, XATTR_SECURITY_PREFIX, prefix_len);
  512. memcpy(list + prefix_len, name, name_len);
  513. list[prefix_len + name_len] = '\0';
  514. }
  515. return total_len;
  516. }
  517. static int security_getxattr(struct dentry *d, const char *name, void *buffer,
  518. size_t size, int flags)
  519. {
  520. return ubifs_getxattr(d, name, buffer, size);
  521. }
  522. static int security_setxattr(struct dentry *d, const char *name,
  523. const void *value, size_t size, int flags,
  524. int handler_flags)
  525. {
  526. return ubifs_setxattr(d, name, value, size, flags);
  527. }
  528. static const struct xattr_handler ubifs_xattr_security_handler = {
  529. .prefix = XATTR_SECURITY_PREFIX,
  530. .list = security_listxattr,
  531. .get = security_getxattr,
  532. .set = security_setxattr,
  533. };
  534. const struct xattr_handler *ubifs_xattr_handlers[] = {
  535. &ubifs_xattr_security_handler,
  536. NULL,
  537. };
  538. static int init_xattrs(struct inode *inode, const struct xattr *xattr_array,
  539. void *fs_info)
  540. {
  541. const struct xattr *xattr;
  542. char *name;
  543. int err = 0;
  544. for (xattr = xattr_array; xattr->name != NULL; xattr++) {
  545. name = kmalloc(XATTR_SECURITY_PREFIX_LEN +
  546. strlen(xattr->name) + 1, GFP_NOFS);
  547. if (!name) {
  548. err = -ENOMEM;
  549. break;
  550. }
  551. strcpy(name, XATTR_SECURITY_PREFIX);
  552. strcpy(name + XATTR_SECURITY_PREFIX_LEN, xattr->name);
  553. err = setxattr(inode, name, xattr->value, xattr->value_len, 0);
  554. kfree(name);
  555. if (err < 0)
  556. break;
  557. }
  558. return err;
  559. }
  560. int ubifs_init_security(struct inode *dentry, struct inode *inode,
  561. const struct qstr *qstr)
  562. {
  563. int err;
  564. err = security_inode_init_security(inode, dentry, qstr,
  565. &init_xattrs, 0);
  566. if (err) {
  567. struct ubifs_info *c = dentry->i_sb->s_fs_info;
  568. ubifs_err(c, "cannot initialize security for inode %lu, error %d",
  569. inode->i_ino, err);
  570. }
  571. return err;
  572. }