xattr.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625
  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. {
  250. struct inode *inode;
  251. struct ubifs_info *c = host->i_sb->s_fs_info;
  252. struct qstr nm = QSTR_INIT(name, strlen(name));
  253. struct ubifs_dent_node *xent;
  254. union ubifs_key key;
  255. int err;
  256. ubifs_assert(inode_is_locked(host));
  257. if (size > UBIFS_MAX_INO_DATA)
  258. return -ERANGE;
  259. if (nm.len > UBIFS_MAX_NLEN)
  260. return -ENAMETOOLONG;
  261. xent = kmalloc(UBIFS_MAX_XENT_NODE_SZ, GFP_NOFS);
  262. if (!xent)
  263. return -ENOMEM;
  264. /*
  265. * The extended attribute entries are stored in LNC, so multiple
  266. * look-ups do not involve reading the flash.
  267. */
  268. xent_key_init(c, &key, host->i_ino, &nm);
  269. err = ubifs_tnc_lookup_nm(c, &key, xent, &nm);
  270. if (err) {
  271. if (err != -ENOENT)
  272. goto out_free;
  273. if (flags & XATTR_REPLACE)
  274. /* We are asked not to create the xattr */
  275. err = -ENODATA;
  276. else
  277. err = create_xattr(c, host, &nm, value, size);
  278. goto out_free;
  279. }
  280. if (flags & XATTR_CREATE) {
  281. /* We are asked not to replace the xattr */
  282. err = -EEXIST;
  283. goto out_free;
  284. }
  285. inode = iget_xattr(c, le64_to_cpu(xent->inum));
  286. if (IS_ERR(inode)) {
  287. err = PTR_ERR(inode);
  288. goto out_free;
  289. }
  290. err = change_xattr(c, host, inode, value, size);
  291. iput(inode);
  292. out_free:
  293. kfree(xent);
  294. return err;
  295. }
  296. static ssize_t __ubifs_getxattr(struct inode *host, const char *name,
  297. void *buf, size_t size)
  298. {
  299. struct inode *inode;
  300. struct ubifs_info *c = host->i_sb->s_fs_info;
  301. struct qstr nm = QSTR_INIT(name, strlen(name));
  302. struct ubifs_inode *ui;
  303. struct ubifs_dent_node *xent;
  304. union ubifs_key key;
  305. int err;
  306. if (nm.len > UBIFS_MAX_NLEN)
  307. return -ENAMETOOLONG;
  308. xent = kmalloc(UBIFS_MAX_XENT_NODE_SZ, GFP_NOFS);
  309. if (!xent)
  310. return -ENOMEM;
  311. xent_key_init(c, &key, host->i_ino, &nm);
  312. err = ubifs_tnc_lookup_nm(c, &key, xent, &nm);
  313. if (err) {
  314. if (err == -ENOENT)
  315. err = -ENODATA;
  316. goto out_unlock;
  317. }
  318. inode = iget_xattr(c, le64_to_cpu(xent->inum));
  319. if (IS_ERR(inode)) {
  320. err = PTR_ERR(inode);
  321. goto out_unlock;
  322. }
  323. ui = ubifs_inode(inode);
  324. ubifs_assert(inode->i_size == ui->data_len);
  325. ubifs_assert(ubifs_inode(host)->xattr_size > ui->data_len);
  326. mutex_lock(&ui->ui_mutex);
  327. if (buf) {
  328. /* If @buf is %NULL we are supposed to return the length */
  329. if (ui->data_len > size) {
  330. ubifs_err(c, "buffer size %zd, xattr len %d",
  331. size, ui->data_len);
  332. err = -ERANGE;
  333. goto out_iput;
  334. }
  335. memcpy(buf, ui->data, ui->data_len);
  336. }
  337. err = ui->data_len;
  338. out_iput:
  339. mutex_unlock(&ui->ui_mutex);
  340. iput(inode);
  341. out_unlock:
  342. kfree(xent);
  343. return err;
  344. }
  345. ssize_t ubifs_listxattr(struct dentry *dentry, char *buffer, size_t size)
  346. {
  347. union ubifs_key key;
  348. struct inode *host = d_inode(dentry);
  349. struct ubifs_info *c = host->i_sb->s_fs_info;
  350. struct ubifs_inode *host_ui = ubifs_inode(host);
  351. struct ubifs_dent_node *xent, *pxent = NULL;
  352. int err, len, written = 0;
  353. struct qstr nm = { .name = NULL };
  354. dbg_gen("ino %lu ('%pd'), buffer size %zd", host->i_ino,
  355. dentry, size);
  356. len = host_ui->xattr_names + host_ui->xattr_cnt;
  357. if (!buffer)
  358. /*
  359. * We should return the minimum buffer size which will fit a
  360. * null-terminated list of all the extended attribute names.
  361. */
  362. return len;
  363. if (len > size)
  364. return -ERANGE;
  365. lowest_xent_key(c, &key, host->i_ino);
  366. while (1) {
  367. xent = ubifs_tnc_next_ent(c, &key, &nm);
  368. if (IS_ERR(xent)) {
  369. err = PTR_ERR(xent);
  370. break;
  371. }
  372. nm.name = xent->name;
  373. nm.len = le16_to_cpu(xent->nlen);
  374. /* Show trusted namespace only for "power" users */
  375. if (strncmp(xent->name, XATTR_TRUSTED_PREFIX,
  376. XATTR_TRUSTED_PREFIX_LEN) ||
  377. capable(CAP_SYS_ADMIN)) {
  378. memcpy(buffer + written, nm.name, nm.len + 1);
  379. written += nm.len + 1;
  380. }
  381. kfree(pxent);
  382. pxent = xent;
  383. key_read(c, &xent->key, &key);
  384. }
  385. kfree(pxent);
  386. if (err != -ENOENT) {
  387. ubifs_err(c, "cannot find next direntry, error %d", err);
  388. return err;
  389. }
  390. ubifs_assert(written <= size);
  391. return written;
  392. }
  393. static int remove_xattr(struct ubifs_info *c, struct inode *host,
  394. struct inode *inode, const struct qstr *nm)
  395. {
  396. int err;
  397. struct ubifs_inode *host_ui = ubifs_inode(host);
  398. struct ubifs_inode *ui = ubifs_inode(inode);
  399. struct ubifs_budget_req req = { .dirtied_ino = 2, .mod_dent = 1,
  400. .dirtied_ino_d = ALIGN(host_ui->data_len, 8) };
  401. ubifs_assert(ui->data_len == inode->i_size);
  402. err = ubifs_budget_space(c, &req);
  403. if (err)
  404. return err;
  405. mutex_lock(&host_ui->ui_mutex);
  406. host->i_ctime = ubifs_current_time(host);
  407. host_ui->xattr_cnt -= 1;
  408. host_ui->xattr_size -= CALC_DENT_SIZE(nm->len);
  409. host_ui->xattr_size -= CALC_XATTR_BYTES(ui->data_len);
  410. host_ui->xattr_names -= nm->len;
  411. err = ubifs_jnl_delete_xattr(c, host, inode, nm);
  412. if (err)
  413. goto out_cancel;
  414. mutex_unlock(&host_ui->ui_mutex);
  415. ubifs_release_budget(c, &req);
  416. return 0;
  417. out_cancel:
  418. host_ui->xattr_cnt += 1;
  419. host_ui->xattr_size += CALC_DENT_SIZE(nm->len);
  420. host_ui->xattr_size += CALC_XATTR_BYTES(ui->data_len);
  421. host_ui->xattr_names += nm->len;
  422. mutex_unlock(&host_ui->ui_mutex);
  423. ubifs_release_budget(c, &req);
  424. make_bad_inode(inode);
  425. return err;
  426. }
  427. static int __ubifs_removexattr(struct inode *host, const char *name)
  428. {
  429. struct inode *inode;
  430. struct ubifs_info *c = host->i_sb->s_fs_info;
  431. struct qstr nm = QSTR_INIT(name, strlen(name));
  432. struct ubifs_dent_node *xent;
  433. union ubifs_key key;
  434. int err;
  435. ubifs_assert(inode_is_locked(host));
  436. if (nm.len > UBIFS_MAX_NLEN)
  437. return -ENAMETOOLONG;
  438. xent = kmalloc(UBIFS_MAX_XENT_NODE_SZ, GFP_NOFS);
  439. if (!xent)
  440. return -ENOMEM;
  441. xent_key_init(c, &key, host->i_ino, &nm);
  442. err = ubifs_tnc_lookup_nm(c, &key, xent, &nm);
  443. if (err) {
  444. if (err == -ENOENT)
  445. err = -ENODATA;
  446. goto out_free;
  447. }
  448. inode = iget_xattr(c, le64_to_cpu(xent->inum));
  449. if (IS_ERR(inode)) {
  450. err = PTR_ERR(inode);
  451. goto out_free;
  452. }
  453. ubifs_assert(inode->i_nlink == 1);
  454. clear_nlink(inode);
  455. err = remove_xattr(c, host, inode, &nm);
  456. if (err)
  457. set_nlink(inode, 1);
  458. /* If @i_nlink is 0, 'iput()' will delete the inode */
  459. iput(inode);
  460. out_free:
  461. kfree(xent);
  462. return err;
  463. }
  464. static int init_xattrs(struct inode *inode, const struct xattr *xattr_array,
  465. void *fs_info)
  466. {
  467. const struct xattr *xattr;
  468. char *name;
  469. int err = 0;
  470. for (xattr = xattr_array; xattr->name != NULL; xattr++) {
  471. name = kmalloc(XATTR_SECURITY_PREFIX_LEN +
  472. strlen(xattr->name) + 1, GFP_NOFS);
  473. if (!name) {
  474. err = -ENOMEM;
  475. break;
  476. }
  477. strcpy(name, XATTR_SECURITY_PREFIX);
  478. strcpy(name + XATTR_SECURITY_PREFIX_LEN, xattr->name);
  479. err = __ubifs_setxattr(inode, name, xattr->value, xattr->value_len, 0);
  480. kfree(name);
  481. if (err < 0)
  482. break;
  483. }
  484. return err;
  485. }
  486. int ubifs_init_security(struct inode *dentry, struct inode *inode,
  487. const struct qstr *qstr)
  488. {
  489. int err;
  490. err = security_inode_init_security(inode, dentry, qstr,
  491. &init_xattrs, 0);
  492. if (err) {
  493. struct ubifs_info *c = dentry->i_sb->s_fs_info;
  494. ubifs_err(c, "cannot initialize security for inode %lu, error %d",
  495. inode->i_ino, err);
  496. }
  497. return err;
  498. }
  499. static int ubifs_xattr_get(const struct xattr_handler *handler,
  500. struct dentry *dentry, struct inode *inode,
  501. const char *name, void *buffer, size_t size)
  502. {
  503. dbg_gen("xattr '%s', ino %lu ('%pd'), buf size %zd", name,
  504. inode->i_ino, dentry, size);
  505. name = xattr_full_name(handler, name);
  506. return __ubifs_getxattr(inode, name, buffer, size);
  507. }
  508. static int ubifs_xattr_set(const struct xattr_handler *handler,
  509. struct dentry *dentry, struct inode *inode,
  510. const char *name, const void *value,
  511. size_t size, int flags)
  512. {
  513. dbg_gen("xattr '%s', host ino %lu ('%pd'), size %zd",
  514. name, inode->i_ino, dentry, size);
  515. name = xattr_full_name(handler, name);
  516. if (value)
  517. return __ubifs_setxattr(inode, name, value, size, flags);
  518. else
  519. return __ubifs_removexattr(inode, name);
  520. }
  521. static const struct xattr_handler ubifs_user_xattr_handler = {
  522. .prefix = XATTR_USER_PREFIX,
  523. .get = ubifs_xattr_get,
  524. .set = ubifs_xattr_set,
  525. };
  526. static const struct xattr_handler ubifs_trusted_xattr_handler = {
  527. .prefix = XATTR_TRUSTED_PREFIX,
  528. .get = ubifs_xattr_get,
  529. .set = ubifs_xattr_set,
  530. };
  531. static const struct xattr_handler ubifs_security_xattr_handler = {
  532. .prefix = XATTR_SECURITY_PREFIX,
  533. .get = ubifs_xattr_get,
  534. .set = ubifs_xattr_set,
  535. };
  536. const struct xattr_handler *ubifs_xattr_handlers[] = {
  537. &ubifs_user_xattr_handler,
  538. &ubifs_trusted_xattr_handler,
  539. &ubifs_security_xattr_handler,
  540. NULL
  541. };