xattr.c 19 KB

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