xattr.c 18 KB

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