xattr.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  1. /*
  2. * This file is part of UBIFS.
  3. *
  4. * Copyright (C) 2006-2008 Nokia Corporation.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License version 2 as published by
  8. * the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along with
  16. * this program; if not, write to the Free Software Foundation, Inc., 51
  17. * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  18. *
  19. * Authors: Artem Bityutskiy (Битюцкий Артём)
  20. * Adrian Hunter
  21. */
  22. /*
  23. * This file implements UBIFS extended attributes support.
  24. *
  25. * Extended attributes are implemented as regular inodes with attached data,
  26. * which limits extended attribute size to UBIFS block size (4KiB). Names of
  27. * extended attributes are described by extended attribute entries (xentries),
  28. * which are almost identical to directory entries, but have different key type.
  29. *
  30. * In other words, the situation with extended attributes is very similar to
  31. * directories. Indeed, any inode (but of course not xattr inodes) may have a
  32. * number of associated xentries, just like directory inodes have associated
  33. * directory entries. Extended attribute entries store the name of the extended
  34. * attribute, the host inode number, and the extended attribute inode number.
  35. * Similarly, direntries store the name, the parent and the target inode
  36. * numbers. Thus, most of the common UBIFS mechanisms may be re-used for
  37. * extended attributes.
  38. *
  39. * The number of extended attributes is not limited, but there is Linux
  40. * limitation on the maximum possible size of the list of all extended
  41. * attributes associated with an inode (%XATTR_LIST_MAX), so UBIFS makes sure
  42. * the sum of all extended attribute names of the inode does not exceed that
  43. * limit.
  44. *
  45. * Extended attributes are synchronous, which means they are written to the
  46. * flash media synchronously and there is no write-back for extended attribute
  47. * inodes. The extended attribute values are not stored in compressed form on
  48. * the media.
  49. *
  50. * Since extended attributes are represented by regular inodes, they are cached
  51. * in the VFS inode cache. The xentries are cached in the LNC cache (see
  52. * tnc.c).
  53. *
  54. * ACL support is not implemented.
  55. */
  56. #include "ubifs.h"
  57. #include <linux/fs.h>
  58. #include <linux/slab.h>
  59. #include <linux/xattr.h>
  60. /*
  61. * Limit the number of extended attributes per inode so that the total size
  62. * (@xattr_size) is guaranteeded to fit in an 'unsigned int'.
  63. */
  64. #define MAX_XATTRS_PER_INODE 65535
  65. /*
  66. * Extended attribute type constants.
  67. *
  68. * USER_XATTR: user extended attribute ("user.*")
  69. * TRUSTED_XATTR: trusted extended attribute ("trusted.*)
  70. * SECURITY_XATTR: security extended attribute ("security.*")
  71. */
  72. enum {
  73. USER_XATTR,
  74. TRUSTED_XATTR,
  75. SECURITY_XATTR,
  76. };
  77. static const struct inode_operations empty_iops;
  78. static const struct file_operations empty_fops;
  79. /**
  80. * create_xattr - create an extended attribute.
  81. * @c: UBIFS file-system description object
  82. * @host: host inode
  83. * @nm: extended attribute name
  84. * @value: extended attribute value
  85. * @size: size of extended attribute value
  86. *
  87. * This is a helper function which creates an extended attribute of name @nm
  88. * and value @value for inode @host. The host inode is also updated on flash
  89. * because the ctime and extended attribute accounting data changes. This
  90. * function returns zero in case of success and a negative error code in case
  91. * of failure.
  92. */
  93. static int create_xattr(struct ubifs_info *c, struct inode *host,
  94. const struct qstr *nm, const void *value, int size)
  95. {
  96. int err, names_len;
  97. struct inode *inode;
  98. struct ubifs_inode *ui, *host_ui = ubifs_inode(host);
  99. struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1,
  100. .new_ino_d = ALIGN(size, 8), .dirtied_ino = 1,
  101. .dirtied_ino_d = ALIGN(host_ui->data_len, 8) };
  102. if (host_ui->xattr_cnt >= MAX_XATTRS_PER_INODE) {
  103. ubifs_err(c, "inode %lu already has too many xattrs (%d), cannot create more",
  104. host->i_ino, host_ui->xattr_cnt);
  105. return -ENOSPC;
  106. }
  107. /*
  108. * Linux limits the maximum size of the extended attribute names list
  109. * to %XATTR_LIST_MAX. This means we should not allow creating more
  110. * extended attributes if the name list becomes larger. This limitation
  111. * is artificial for UBIFS, though.
  112. */
  113. names_len = host_ui->xattr_names + host_ui->xattr_cnt + nm->len + 1;
  114. if (names_len > XATTR_LIST_MAX) {
  115. ubifs_err(c, "cannot add one more xattr name to inode %lu, total names length would become %d, max. is %d",
  116. host->i_ino, names_len, XATTR_LIST_MAX);
  117. return -ENOSPC;
  118. }
  119. err = ubifs_budget_space(c, &req);
  120. if (err)
  121. return err;
  122. inode = ubifs_new_inode(c, host, S_IFREG | S_IRWXUGO);
  123. if (IS_ERR(inode)) {
  124. err = PTR_ERR(inode);
  125. goto out_budg;
  126. }
  127. /* Re-define all operations to be "nothing" */
  128. inode->i_mapping->a_ops = &empty_aops;
  129. inode->i_op = &empty_iops;
  130. inode->i_fop = &empty_fops;
  131. inode->i_flags |= S_SYNC | S_NOATIME | S_NOCMTIME | S_NOQUOTA;
  132. ui = ubifs_inode(inode);
  133. ui->xattr = 1;
  134. ui->flags |= UBIFS_XATTR_FL;
  135. ui->data = kmemdup(value, size, GFP_NOFS);
  136. if (!ui->data) {
  137. err = -ENOMEM;
  138. goto out_free;
  139. }
  140. inode->i_size = ui->ui_size = size;
  141. ui->data_len = size;
  142. mutex_lock(&host_ui->ui_mutex);
  143. host->i_ctime = ubifs_current_time(host);
  144. host_ui->xattr_cnt += 1;
  145. host_ui->xattr_size += CALC_DENT_SIZE(nm->len);
  146. host_ui->xattr_size += CALC_XATTR_BYTES(size);
  147. host_ui->xattr_names += nm->len;
  148. err = ubifs_jnl_update(c, host, nm, inode, 0, 1);
  149. if (err)
  150. goto out_cancel;
  151. mutex_unlock(&host_ui->ui_mutex);
  152. ubifs_release_budget(c, &req);
  153. insert_inode_hash(inode);
  154. iput(inode);
  155. return 0;
  156. out_cancel:
  157. host_ui->xattr_cnt -= 1;
  158. host_ui->xattr_size -= CALC_DENT_SIZE(nm->len);
  159. host_ui->xattr_size -= CALC_XATTR_BYTES(size);
  160. host_ui->xattr_names -= nm->len;
  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. void *buf = NULL;
  188. int old_size;
  189. struct ubifs_budget_req req = { .dirtied_ino = 2,
  190. .dirtied_ino_d = ALIGN(size, 8) + ALIGN(host_ui->data_len, 8) };
  191. ubifs_assert(ui->data_len == inode->i_size);
  192. err = ubifs_budget_space(c, &req);
  193. if (err)
  194. return err;
  195. buf = kmemdup(value, size, GFP_NOFS);
  196. if (!buf) {
  197. err = -ENOMEM;
  198. goto out_free;
  199. }
  200. mutex_lock(&ui->ui_mutex);
  201. kfree(ui->data);
  202. ui->data = buf;
  203. inode->i_size = ui->ui_size = size;
  204. old_size = ui->data_len;
  205. ui->data_len = size;
  206. mutex_unlock(&ui->ui_mutex);
  207. mutex_lock(&host_ui->ui_mutex);
  208. host->i_ctime = ubifs_current_time(host);
  209. host_ui->xattr_size -= CALC_XATTR_BYTES(old_size);
  210. host_ui->xattr_size += CALC_XATTR_BYTES(size);
  211. /*
  212. * It is important to write the host inode after the xattr inode
  213. * because if the host inode gets synchronized (via 'fsync()'), then
  214. * the extended attribute inode gets synchronized, because it goes
  215. * before the host inode in the write-buffer.
  216. */
  217. err = ubifs_jnl_change_xattr(c, inode, host);
  218. if (err)
  219. goto out_cancel;
  220. mutex_unlock(&host_ui->ui_mutex);
  221. ubifs_release_budget(c, &req);
  222. return 0;
  223. out_cancel:
  224. host_ui->xattr_size -= CALC_XATTR_BYTES(size);
  225. host_ui->xattr_size += CALC_XATTR_BYTES(old_size);
  226. mutex_unlock(&host_ui->ui_mutex);
  227. make_bad_inode(inode);
  228. out_free:
  229. ubifs_release_budget(c, &req);
  230. return err;
  231. }
  232. static struct inode *iget_xattr(struct ubifs_info *c, ino_t inum)
  233. {
  234. struct inode *inode;
  235. inode = ubifs_iget(c->vfs_sb, inum);
  236. if (IS_ERR(inode)) {
  237. ubifs_err(c, "dead extended attribute entry, error %d",
  238. (int)PTR_ERR(inode));
  239. return inode;
  240. }
  241. if (ubifs_inode(inode)->xattr)
  242. return inode;
  243. ubifs_err(c, "corrupt extended attribute entry");
  244. iput(inode);
  245. return ERR_PTR(-EINVAL);
  246. }
  247. static int __ubifs_setxattr(struct inode *host, const char *name,
  248. const void *value, size_t size, int flags,
  249. bool check_lock)
  250. {
  251. struct inode *inode;
  252. struct ubifs_info *c = host->i_sb->s_fs_info;
  253. struct qstr nm = QSTR_INIT(name, strlen(name));
  254. struct ubifs_dent_node *xent;
  255. union ubifs_key key;
  256. int err;
  257. if (check_lock)
  258. ubifs_assert(inode_is_locked(host));
  259. if (size > UBIFS_MAX_INO_DATA)
  260. return -ERANGE;
  261. if (nm.len > UBIFS_MAX_NLEN)
  262. return -ENAMETOOLONG;
  263. xent = kmalloc(UBIFS_MAX_XENT_NODE_SZ, GFP_NOFS);
  264. if (!xent)
  265. return -ENOMEM;
  266. /*
  267. * The extended attribute entries are stored in LNC, so multiple
  268. * look-ups do not involve reading the flash.
  269. */
  270. xent_key_init(c, &key, host->i_ino, &nm);
  271. err = ubifs_tnc_lookup_nm(c, &key, xent, &nm);
  272. if (err) {
  273. if (err != -ENOENT)
  274. goto out_free;
  275. if (flags & XATTR_REPLACE)
  276. /* We are asked not to create the xattr */
  277. err = -ENODATA;
  278. else
  279. err = create_xattr(c, host, &nm, value, size);
  280. goto out_free;
  281. }
  282. if (flags & XATTR_CREATE) {
  283. /* We are asked not to replace the xattr */
  284. err = -EEXIST;
  285. goto out_free;
  286. }
  287. inode = iget_xattr(c, le64_to_cpu(xent->inum));
  288. if (IS_ERR(inode)) {
  289. err = PTR_ERR(inode);
  290. goto out_free;
  291. }
  292. err = change_xattr(c, host, inode, value, size);
  293. iput(inode);
  294. out_free:
  295. kfree(xent);
  296. return err;
  297. }
  298. static ssize_t __ubifs_getxattr(struct inode *host, const char *name,
  299. void *buf, size_t size)
  300. {
  301. struct inode *inode;
  302. struct ubifs_info *c = host->i_sb->s_fs_info;
  303. struct qstr nm = QSTR_INIT(name, strlen(name));
  304. struct ubifs_inode *ui;
  305. struct ubifs_dent_node *xent;
  306. union ubifs_key key;
  307. int err;
  308. if (nm.len > UBIFS_MAX_NLEN)
  309. return -ENAMETOOLONG;
  310. xent = kmalloc(UBIFS_MAX_XENT_NODE_SZ, GFP_NOFS);
  311. if (!xent)
  312. return -ENOMEM;
  313. xent_key_init(c, &key, host->i_ino, &nm);
  314. err = ubifs_tnc_lookup_nm(c, &key, xent, &nm);
  315. if (err) {
  316. if (err == -ENOENT)
  317. err = -ENODATA;
  318. goto out_unlock;
  319. }
  320. inode = iget_xattr(c, le64_to_cpu(xent->inum));
  321. if (IS_ERR(inode)) {
  322. err = PTR_ERR(inode);
  323. goto out_unlock;
  324. }
  325. ui = ubifs_inode(inode);
  326. ubifs_assert(inode->i_size == ui->data_len);
  327. ubifs_assert(ubifs_inode(host)->xattr_size > ui->data_len);
  328. mutex_lock(&ui->ui_mutex);
  329. if (buf) {
  330. /* If @buf is %NULL we are supposed to return the length */
  331. if (ui->data_len > size) {
  332. ubifs_err(c, "buffer size %zd, xattr len %d",
  333. size, ui->data_len);
  334. err = -ERANGE;
  335. goto out_iput;
  336. }
  337. memcpy(buf, ui->data, ui->data_len);
  338. }
  339. err = ui->data_len;
  340. out_iput:
  341. mutex_unlock(&ui->ui_mutex);
  342. iput(inode);
  343. out_unlock:
  344. kfree(xent);
  345. return err;
  346. }
  347. ssize_t ubifs_listxattr(struct dentry *dentry, char *buffer, size_t size)
  348. {
  349. union ubifs_key key;
  350. struct inode *host = d_inode(dentry);
  351. struct ubifs_info *c = host->i_sb->s_fs_info;
  352. struct ubifs_inode *host_ui = ubifs_inode(host);
  353. struct ubifs_dent_node *xent, *pxent = NULL;
  354. int err, len, written = 0;
  355. struct qstr nm = { .name = NULL };
  356. dbg_gen("ino %lu ('%pd'), buffer size %zd", host->i_ino,
  357. dentry, size);
  358. len = host_ui->xattr_names + host_ui->xattr_cnt;
  359. if (!buffer)
  360. /*
  361. * We should return the minimum buffer size which will fit a
  362. * null-terminated list of all the extended attribute names.
  363. */
  364. return len;
  365. if (len > size)
  366. return -ERANGE;
  367. lowest_xent_key(c, &key, host->i_ino);
  368. while (1) {
  369. xent = ubifs_tnc_next_ent(c, &key, &nm);
  370. if (IS_ERR(xent)) {
  371. err = PTR_ERR(xent);
  372. break;
  373. }
  374. nm.name = xent->name;
  375. nm.len = le16_to_cpu(xent->nlen);
  376. /* Show trusted namespace only for "power" users */
  377. if (strncmp(xent->name, XATTR_TRUSTED_PREFIX,
  378. XATTR_TRUSTED_PREFIX_LEN) ||
  379. capable(CAP_SYS_ADMIN)) {
  380. memcpy(buffer + written, nm.name, nm.len + 1);
  381. written += nm.len + 1;
  382. }
  383. kfree(pxent);
  384. pxent = xent;
  385. key_read(c, &xent->key, &key);
  386. }
  387. kfree(pxent);
  388. if (err != -ENOENT) {
  389. ubifs_err(c, "cannot find next direntry, error %d", err);
  390. return err;
  391. }
  392. ubifs_assert(written <= size);
  393. return written;
  394. }
  395. static int remove_xattr(struct ubifs_info *c, struct inode *host,
  396. struct inode *inode, const struct qstr *nm)
  397. {
  398. int err;
  399. struct ubifs_inode *host_ui = ubifs_inode(host);
  400. struct ubifs_inode *ui = ubifs_inode(inode);
  401. struct ubifs_budget_req req = { .dirtied_ino = 2, .mod_dent = 1,
  402. .dirtied_ino_d = ALIGN(host_ui->data_len, 8) };
  403. ubifs_assert(ui->data_len == inode->i_size);
  404. err = ubifs_budget_space(c, &req);
  405. if (err)
  406. return err;
  407. mutex_lock(&host_ui->ui_mutex);
  408. host->i_ctime = ubifs_current_time(host);
  409. host_ui->xattr_cnt -= 1;
  410. host_ui->xattr_size -= CALC_DENT_SIZE(nm->len);
  411. host_ui->xattr_size -= CALC_XATTR_BYTES(ui->data_len);
  412. host_ui->xattr_names -= nm->len;
  413. err = ubifs_jnl_delete_xattr(c, host, inode, nm);
  414. if (err)
  415. goto out_cancel;
  416. mutex_unlock(&host_ui->ui_mutex);
  417. ubifs_release_budget(c, &req);
  418. return 0;
  419. out_cancel:
  420. host_ui->xattr_cnt += 1;
  421. host_ui->xattr_size += CALC_DENT_SIZE(nm->len);
  422. host_ui->xattr_size += CALC_XATTR_BYTES(ui->data_len);
  423. host_ui->xattr_names += nm->len;
  424. mutex_unlock(&host_ui->ui_mutex);
  425. ubifs_release_budget(c, &req);
  426. make_bad_inode(inode);
  427. return err;
  428. }
  429. static int __ubifs_removexattr(struct inode *host, const char *name)
  430. {
  431. struct inode *inode;
  432. struct ubifs_info *c = host->i_sb->s_fs_info;
  433. struct qstr nm = QSTR_INIT(name, strlen(name));
  434. struct ubifs_dent_node *xent;
  435. union ubifs_key key;
  436. int err;
  437. ubifs_assert(inode_is_locked(host));
  438. if (nm.len > UBIFS_MAX_NLEN)
  439. return -ENAMETOOLONG;
  440. xent = kmalloc(UBIFS_MAX_XENT_NODE_SZ, GFP_NOFS);
  441. if (!xent)
  442. return -ENOMEM;
  443. xent_key_init(c, &key, host->i_ino, &nm);
  444. err = ubifs_tnc_lookup_nm(c, &key, xent, &nm);
  445. if (err) {
  446. if (err == -ENOENT)
  447. err = -ENODATA;
  448. goto out_free;
  449. }
  450. inode = iget_xattr(c, le64_to_cpu(xent->inum));
  451. if (IS_ERR(inode)) {
  452. err = PTR_ERR(inode);
  453. goto out_free;
  454. }
  455. ubifs_assert(inode->i_nlink == 1);
  456. clear_nlink(inode);
  457. err = remove_xattr(c, host, inode, &nm);
  458. if (err)
  459. set_nlink(inode, 1);
  460. /* If @i_nlink is 0, 'iput()' will delete the inode */
  461. iput(inode);
  462. out_free:
  463. kfree(xent);
  464. return err;
  465. }
  466. static int init_xattrs(struct inode *inode, const struct xattr *xattr_array,
  467. void *fs_info)
  468. {
  469. const struct xattr *xattr;
  470. char *name;
  471. int err = 0;
  472. for (xattr = xattr_array; xattr->name != NULL; xattr++) {
  473. name = kmalloc(XATTR_SECURITY_PREFIX_LEN +
  474. strlen(xattr->name) + 1, GFP_NOFS);
  475. if (!name) {
  476. err = -ENOMEM;
  477. break;
  478. }
  479. strcpy(name, XATTR_SECURITY_PREFIX);
  480. strcpy(name + XATTR_SECURITY_PREFIX_LEN, xattr->name);
  481. err = __ubifs_setxattr(inode, name, xattr->value,
  482. xattr->value_len, 0, false);
  483. kfree(name);
  484. if (err < 0)
  485. break;
  486. }
  487. return err;
  488. }
  489. int ubifs_init_security(struct inode *dentry, struct inode *inode,
  490. const struct qstr *qstr)
  491. {
  492. int err;
  493. err = security_inode_init_security(inode, dentry, qstr,
  494. &init_xattrs, 0);
  495. if (err) {
  496. struct ubifs_info *c = dentry->i_sb->s_fs_info;
  497. ubifs_err(c, "cannot initialize security for inode %lu, error %d",
  498. inode->i_ino, err);
  499. }
  500. return err;
  501. }
  502. static int ubifs_xattr_get(const struct xattr_handler *handler,
  503. struct dentry *dentry, struct inode *inode,
  504. const char *name, void *buffer, size_t size)
  505. {
  506. dbg_gen("xattr '%s', ino %lu ('%pd'), buf size %zd", name,
  507. inode->i_ino, dentry, size);
  508. name = xattr_full_name(handler, name);
  509. return __ubifs_getxattr(inode, name, buffer, size);
  510. }
  511. static int ubifs_xattr_set(const struct xattr_handler *handler,
  512. struct dentry *dentry, struct inode *inode,
  513. const char *name, const void *value,
  514. size_t size, int flags)
  515. {
  516. dbg_gen("xattr '%s', host ino %lu ('%pd'), size %zd",
  517. name, inode->i_ino, dentry, size);
  518. name = xattr_full_name(handler, name);
  519. if (value)
  520. return __ubifs_setxattr(inode, name, value, size, flags,
  521. true);
  522. else
  523. return __ubifs_removexattr(inode, name);
  524. }
  525. static const struct xattr_handler ubifs_user_xattr_handler = {
  526. .prefix = XATTR_USER_PREFIX,
  527. .get = ubifs_xattr_get,
  528. .set = ubifs_xattr_set,
  529. };
  530. static const struct xattr_handler ubifs_trusted_xattr_handler = {
  531. .prefix = XATTR_TRUSTED_PREFIX,
  532. .get = ubifs_xattr_get,
  533. .set = ubifs_xattr_set,
  534. };
  535. static const struct xattr_handler ubifs_security_xattr_handler = {
  536. .prefix = XATTR_SECURITY_PREFIX,
  537. .get = ubifs_xattr_get,
  538. .set = ubifs_xattr_set,
  539. };
  540. const struct xattr_handler *ubifs_xattr_handlers[] = {
  541. &ubifs_user_xattr_handler,
  542. &ubifs_trusted_xattr_handler,
  543. &ubifs_security_xattr_handler,
  544. NULL
  545. };