xattr.c 19 KB

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