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 <linux/xattr.h>
  57. #include <linux/posix_acl_xattr.h>
  58. #include "ubifs.h"
  59. /*
  60. * Limit the number of extended attributes per inode so that the total size
  61. * (@xattr_size) is guaranteeded to fit in an 'unsigned int'.
  62. */
  63. #define MAX_XATTRS_PER_INODE 65535
  64. /*
  65. * Extended attribute type constants.
  66. *
  67. * USER_XATTR: user extended attribute ("user.*")
  68. * TRUSTED_XATTR: trusted extended attribute ("trusted.*)
  69. * SECURITY_XATTR: security extended attribute ("security.*")
  70. */
  71. enum {
  72. USER_XATTR,
  73. TRUSTED_XATTR,
  74. SECURITY_XATTR,
  75. };
  76. static struct inode_operations none_inode_operations;
  77. static struct address_space_operations none_address_operations;
  78. static struct file_operations none_file_operations;
  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;
  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. return -ENOSPC;
  104. /*
  105. * Linux limits the maximum size of the extended attribute names list
  106. * to %XATTR_LIST_MAX. This means we should not allow creating more
  107. * extended attributes if the name list becomes larger. This limitation
  108. * is artificial for UBIFS, though.
  109. */
  110. if (host_ui->xattr_names + host_ui->xattr_cnt +
  111. nm->len + 1 > XATTR_LIST_MAX)
  112. return -ENOSPC;
  113. err = ubifs_budget_space(c, &req);
  114. if (err)
  115. return err;
  116. inode = ubifs_new_inode(c, host, S_IFREG | S_IRWXUGO);
  117. if (IS_ERR(inode)) {
  118. err = PTR_ERR(inode);
  119. goto out_budg;
  120. }
  121. /* Re-define all operations to be "nothing" */
  122. inode->i_mapping->a_ops = &none_address_operations;
  123. inode->i_op = &none_inode_operations;
  124. inode->i_fop = &none_file_operations;
  125. inode->i_flags |= S_SYNC | S_NOATIME | S_NOCMTIME | S_NOQUOTA;
  126. ui = ubifs_inode(inode);
  127. ui->xattr = 1;
  128. ui->flags |= UBIFS_XATTR_FL;
  129. ui->data = kmalloc(size, GFP_NOFS);
  130. if (!ui->data) {
  131. err = -ENOMEM;
  132. goto out_free;
  133. }
  134. memcpy(ui->data, value, size);
  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 = kmalloc(size, GFP_NOFS);
  189. if (!ui->data) {
  190. err = -ENOMEM;
  191. goto out_free;
  192. }
  193. memcpy(ui->data, value, size);
  194. inode->i_size = ui->ui_size = size;
  195. ui->data_len = size;
  196. mutex_lock(&host_ui->ui_mutex);
  197. host->i_ctime = ubifs_current_time(host);
  198. host_ui->xattr_size -= CALC_XATTR_BYTES(ui->data_len);
  199. host_ui->xattr_size += CALC_XATTR_BYTES(size);
  200. /*
  201. * It is important to write the host inode after the xattr inode
  202. * because if the host inode gets synchronized (via 'fsync()'), then
  203. * the extended attribute inode gets synchronized, because it goes
  204. * before the host inode in the write-buffer.
  205. */
  206. err = ubifs_jnl_change_xattr(c, inode, host);
  207. if (err)
  208. goto out_cancel;
  209. mutex_unlock(&host_ui->ui_mutex);
  210. ubifs_release_budget(c, &req);
  211. return 0;
  212. out_cancel:
  213. host_ui->xattr_size -= CALC_XATTR_BYTES(size);
  214. host_ui->xattr_size += CALC_XATTR_BYTES(ui->data_len);
  215. mutex_unlock(&host_ui->ui_mutex);
  216. make_bad_inode(inode);
  217. out_free:
  218. ubifs_release_budget(c, &req);
  219. return err;
  220. }
  221. /**
  222. * check_namespace - check extended attribute name-space.
  223. * @nm: extended attribute name
  224. *
  225. * This function makes sure the extended attribute name belongs to one of the
  226. * supported extended attribute name-spaces. Returns name-space index in case
  227. * of success and a negative error code in case of failure.
  228. */
  229. static int check_namespace(const struct qstr *nm)
  230. {
  231. int type;
  232. if (nm->len > UBIFS_MAX_NLEN)
  233. return -ENAMETOOLONG;
  234. if (!strncmp(nm->name, XATTR_TRUSTED_PREFIX,
  235. XATTR_TRUSTED_PREFIX_LEN)) {
  236. if (nm->name[sizeof(XATTR_TRUSTED_PREFIX) - 1] == '\0')
  237. return -EINVAL;
  238. type = TRUSTED_XATTR;
  239. } else if (!strncmp(nm->name, XATTR_USER_PREFIX,
  240. XATTR_USER_PREFIX_LEN)) {
  241. if (nm->name[XATTR_USER_PREFIX_LEN] == '\0')
  242. return -EINVAL;
  243. type = USER_XATTR;
  244. } else if (!strncmp(nm->name, XATTR_SECURITY_PREFIX,
  245. XATTR_SECURITY_PREFIX_LEN)) {
  246. if (nm->name[sizeof(XATTR_SECURITY_PREFIX) - 1] == '\0')
  247. return -EINVAL;
  248. type = SECURITY_XATTR;
  249. } else
  250. return -EOPNOTSUPP;
  251. return type;
  252. }
  253. static struct inode *iget_xattr(struct ubifs_info *c, ino_t inum)
  254. {
  255. struct inode *inode;
  256. inode = ubifs_iget(c->vfs_sb, inum);
  257. if (IS_ERR(inode)) {
  258. ubifs_err("dead extended attribute entry, error %d",
  259. (int)PTR_ERR(inode));
  260. return inode;
  261. }
  262. if (ubifs_inode(inode)->xattr)
  263. return inode;
  264. ubifs_err("corrupt extended attribute entry");
  265. iput(inode);
  266. return ERR_PTR(-EINVAL);
  267. }
  268. int ubifs_setxattr(struct dentry *dentry, const char *name,
  269. const void *value, size_t size, int flags)
  270. {
  271. struct inode *inode, *host = dentry->d_inode;
  272. struct ubifs_info *c = host->i_sb->s_fs_info;
  273. struct qstr nm = { .name = name, .len = strlen(name) };
  274. struct ubifs_dent_node *xent;
  275. union ubifs_key key;
  276. int err, type;
  277. dbg_gen("xattr '%s', host ino %lu ('%.*s'), size %zd", name,
  278. host->i_ino, dentry->d_name.len, dentry->d_name.name, size);
  279. ubifs_assert(mutex_is_locked(&host->i_mutex));
  280. if (size > UBIFS_MAX_INO_DATA)
  281. return -ERANGE;
  282. type = check_namespace(&nm);
  283. if (type < 0)
  284. return type;
  285. xent = kmalloc(UBIFS_MAX_XENT_NODE_SZ, GFP_NOFS);
  286. if (!xent)
  287. return -ENOMEM;
  288. /*
  289. * The extended attribute entries are stored in LNC, so multiple
  290. * look-ups do not involve reading the flash.
  291. */
  292. xent_key_init(c, &key, host->i_ino, &nm);
  293. err = ubifs_tnc_lookup_nm(c, &key, xent, &nm);
  294. if (err) {
  295. if (err != -ENOENT)
  296. goto out_free;
  297. if (flags & XATTR_REPLACE)
  298. /* We are asked not to create the xattr */
  299. err = -ENODATA;
  300. else
  301. err = create_xattr(c, host, &nm, value, size);
  302. goto out_free;
  303. }
  304. if (flags & XATTR_CREATE) {
  305. /* We are asked not to replace the xattr */
  306. err = -EEXIST;
  307. goto out_free;
  308. }
  309. inode = iget_xattr(c, le64_to_cpu(xent->inum));
  310. if (IS_ERR(inode)) {
  311. err = PTR_ERR(inode);
  312. goto out_free;
  313. }
  314. err = change_xattr(c, host, inode, value, size);
  315. iput(inode);
  316. out_free:
  317. kfree(xent);
  318. return err;
  319. }
  320. ssize_t ubifs_getxattr(struct dentry *dentry, const char *name, void *buf,
  321. size_t size)
  322. {
  323. struct inode *inode, *host = dentry->d_inode;
  324. struct ubifs_info *c = host->i_sb->s_fs_info;
  325. struct qstr nm = { .name = name, .len = strlen(name) };
  326. struct ubifs_inode *ui;
  327. struct ubifs_dent_node *xent;
  328. union ubifs_key key;
  329. int err;
  330. dbg_gen("xattr '%s', ino %lu ('%.*s'), buf size %zd", name,
  331. host->i_ino, dentry->d_name.len, dentry->d_name.name, size);
  332. err = check_namespace(&nm);
  333. if (err < 0)
  334. return err;
  335. xent = kmalloc(UBIFS_MAX_XENT_NODE_SZ, GFP_NOFS);
  336. if (!xent)
  337. return -ENOMEM;
  338. xent_key_init(c, &key, host->i_ino, &nm);
  339. err = ubifs_tnc_lookup_nm(c, &key, xent, &nm);
  340. if (err) {
  341. if (err == -ENOENT)
  342. err = -ENODATA;
  343. goto out_unlock;
  344. }
  345. inode = iget_xattr(c, le64_to_cpu(xent->inum));
  346. if (IS_ERR(inode)) {
  347. err = PTR_ERR(inode);
  348. goto out_unlock;
  349. }
  350. ui = ubifs_inode(inode);
  351. ubifs_assert(inode->i_size == ui->data_len);
  352. ubifs_assert(ubifs_inode(host)->xattr_size > ui->data_len);
  353. if (buf) {
  354. /* If @buf is %NULL we are supposed to return the length */
  355. if (ui->data_len > size) {
  356. dbg_err("buffer size %zd, xattr len %d",
  357. size, ui->data_len);
  358. err = -ERANGE;
  359. goto out_iput;
  360. }
  361. memcpy(buf, ui->data, ui->data_len);
  362. }
  363. err = ui->data_len;
  364. out_iput:
  365. iput(inode);
  366. out_unlock:
  367. kfree(xent);
  368. return err;
  369. }
  370. ssize_t ubifs_listxattr(struct dentry *dentry, char *buffer, size_t size)
  371. {
  372. union ubifs_key key;
  373. struct inode *host = dentry->d_inode;
  374. struct ubifs_info *c = host->i_sb->s_fs_info;
  375. struct ubifs_inode *host_ui = ubifs_inode(host);
  376. struct ubifs_dent_node *xent, *pxent = NULL;
  377. int err, len, written = 0;
  378. struct qstr nm = { .name = NULL };
  379. dbg_gen("ino %lu ('%.*s'), buffer size %zd", host->i_ino,
  380. dentry->d_name.len, dentry->d_name.name, size);
  381. len = host_ui->xattr_names + host_ui->xattr_cnt;
  382. if (!buffer)
  383. /*
  384. * We should return the minimum buffer size which will fit a
  385. * null-terminated list of all the extended attribute names.
  386. */
  387. return len;
  388. if (len > size)
  389. return -ERANGE;
  390. lowest_xent_key(c, &key, host->i_ino);
  391. while (1) {
  392. int type;
  393. xent = ubifs_tnc_next_ent(c, &key, &nm);
  394. if (IS_ERR(xent)) {
  395. err = PTR_ERR(xent);
  396. break;
  397. }
  398. nm.name = xent->name;
  399. nm.len = le16_to_cpu(xent->nlen);
  400. type = check_namespace(&nm);
  401. if (unlikely(type < 0)) {
  402. err = type;
  403. break;
  404. }
  405. /* Show trusted namespace only for "power" users */
  406. if (type != TRUSTED_XATTR || capable(CAP_SYS_ADMIN)) {
  407. memcpy(buffer + written, nm.name, nm.len + 1);
  408. written += nm.len + 1;
  409. }
  410. kfree(pxent);
  411. pxent = xent;
  412. key_read(c, &xent->key, &key);
  413. }
  414. kfree(pxent);
  415. if (err != -ENOENT) {
  416. ubifs_err("cannot find next direntry, error %d", err);
  417. return err;
  418. }
  419. ubifs_assert(written <= size);
  420. return written;
  421. }
  422. static int remove_xattr(struct ubifs_info *c, struct inode *host,
  423. struct inode *inode, const struct qstr *nm)
  424. {
  425. int err;
  426. struct ubifs_inode *host_ui = ubifs_inode(host);
  427. struct ubifs_inode *ui = ubifs_inode(inode);
  428. struct ubifs_budget_req req = { .dirtied_ino = 2, .mod_dent = 1,
  429. .dirtied_ino_d = ALIGN(host_ui->data_len, 8) };
  430. ubifs_assert(ui->data_len == inode->i_size);
  431. err = ubifs_budget_space(c, &req);
  432. if (err)
  433. return err;
  434. mutex_lock(&host_ui->ui_mutex);
  435. host->i_ctime = ubifs_current_time(host);
  436. host_ui->xattr_cnt -= 1;
  437. host_ui->xattr_size -= CALC_DENT_SIZE(nm->len);
  438. host_ui->xattr_size -= CALC_XATTR_BYTES(ui->data_len);
  439. host_ui->xattr_names -= nm->len;
  440. err = ubifs_jnl_delete_xattr(c, host, inode, nm);
  441. if (err)
  442. goto out_cancel;
  443. mutex_unlock(&host_ui->ui_mutex);
  444. ubifs_release_budget(c, &req);
  445. return 0;
  446. out_cancel:
  447. host_ui->xattr_cnt += 1;
  448. host_ui->xattr_size += CALC_DENT_SIZE(nm->len);
  449. host_ui->xattr_size += CALC_XATTR_BYTES(ui->data_len);
  450. mutex_unlock(&host_ui->ui_mutex);
  451. ubifs_release_budget(c, &req);
  452. make_bad_inode(inode);
  453. return err;
  454. }
  455. int ubifs_removexattr(struct dentry *dentry, const char *name)
  456. {
  457. struct inode *inode, *host = dentry->d_inode;
  458. struct ubifs_info *c = host->i_sb->s_fs_info;
  459. struct qstr nm = { .name = name, .len = strlen(name) };
  460. struct ubifs_dent_node *xent;
  461. union ubifs_key key;
  462. int err;
  463. dbg_gen("xattr '%s', ino %lu ('%.*s')", name,
  464. host->i_ino, dentry->d_name.len, dentry->d_name.name);
  465. ubifs_assert(mutex_is_locked(&host->i_mutex));
  466. err = check_namespace(&nm);
  467. if (err < 0)
  468. return err;
  469. xent = kmalloc(UBIFS_MAX_XENT_NODE_SZ, GFP_NOFS);
  470. if (!xent)
  471. return -ENOMEM;
  472. xent_key_init(c, &key, host->i_ino, &nm);
  473. err = ubifs_tnc_lookup_nm(c, &key, xent, &nm);
  474. if (err) {
  475. if (err == -ENOENT)
  476. err = -ENODATA;
  477. goto out_free;
  478. }
  479. inode = iget_xattr(c, le64_to_cpu(xent->inum));
  480. if (IS_ERR(inode)) {
  481. err = PTR_ERR(inode);
  482. goto out_free;
  483. }
  484. ubifs_assert(inode->i_nlink == 1);
  485. inode->i_nlink = 0;
  486. err = remove_xattr(c, host, inode, &nm);
  487. if (err)
  488. inode->i_nlink = 1;
  489. /* If @i_nlink is 0, 'iput()' will delete the inode */
  490. iput(inode);
  491. out_free:
  492. kfree(xent);
  493. return err;
  494. }