xattr.c 15 KB

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