xattr.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  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 = kmalloc(size, GFP_NOFS);
  131. if (!ui->data) {
  132. err = -ENOMEM;
  133. goto out_free;
  134. }
  135. memcpy(ui->data, value, size);
  136. inode->i_size = ui->ui_size = size;
  137. ui->data_len = size;
  138. mutex_lock(&host_ui->ui_mutex);
  139. host->i_ctime = ubifs_current_time(host);
  140. host_ui->xattr_cnt += 1;
  141. host_ui->xattr_size += CALC_DENT_SIZE(nm->len);
  142. host_ui->xattr_size += CALC_XATTR_BYTES(size);
  143. host_ui->xattr_names += nm->len;
  144. err = ubifs_jnl_update(c, host, nm, inode, 0, 1);
  145. if (err)
  146. goto out_cancel;
  147. mutex_unlock(&host_ui->ui_mutex);
  148. ubifs_release_budget(c, &req);
  149. insert_inode_hash(inode);
  150. iput(inode);
  151. return 0;
  152. out_cancel:
  153. host_ui->xattr_cnt -= 1;
  154. host_ui->xattr_size -= CALC_DENT_SIZE(nm->len);
  155. host_ui->xattr_size -= CALC_XATTR_BYTES(size);
  156. mutex_unlock(&host_ui->ui_mutex);
  157. out_free:
  158. make_bad_inode(inode);
  159. iput(inode);
  160. out_budg:
  161. ubifs_release_budget(c, &req);
  162. return err;
  163. }
  164. /**
  165. * change_xattr - change an extended attribute.
  166. * @c: UBIFS file-system description object
  167. * @host: host inode
  168. * @inode: extended attribute inode
  169. * @value: extended attribute value
  170. * @size: size of extended attribute value
  171. *
  172. * This helper function changes the value of extended attribute @inode with new
  173. * data from @value. Returns zero in case of success and a negative error code
  174. * in case of failure.
  175. */
  176. static int change_xattr(struct ubifs_info *c, struct inode *host,
  177. struct inode *inode, const void *value, int size)
  178. {
  179. int err;
  180. struct ubifs_inode *host_ui = ubifs_inode(host);
  181. struct ubifs_inode *ui = ubifs_inode(inode);
  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(ui->data_len == inode->i_size);
  185. err = ubifs_budget_space(c, &req);
  186. if (err)
  187. return err;
  188. kfree(ui->data);
  189. ui->data = kmalloc(size, GFP_NOFS);
  190. if (!ui->data) {
  191. err = -ENOMEM;
  192. goto out_free;
  193. }
  194. memcpy(ui->data, value, size);
  195. inode->i_size = ui->ui_size = size;
  196. ui->data_len = size;
  197. mutex_lock(&host_ui->ui_mutex);
  198. host->i_ctime = ubifs_current_time(host);
  199. host_ui->xattr_size -= CALC_XATTR_BYTES(ui->data_len);
  200. host_ui->xattr_size += CALC_XATTR_BYTES(size);
  201. /*
  202. * It is important to write the host inode after the xattr inode
  203. * because if the host inode gets synchronized (via 'fsync()'), then
  204. * the extended attribute inode gets synchronized, because it goes
  205. * before the host inode in the write-buffer.
  206. */
  207. err = ubifs_jnl_change_xattr(c, inode, host);
  208. if (err)
  209. goto out_cancel;
  210. mutex_unlock(&host_ui->ui_mutex);
  211. ubifs_release_budget(c, &req);
  212. return 0;
  213. out_cancel:
  214. host_ui->xattr_size -= CALC_XATTR_BYTES(size);
  215. host_ui->xattr_size += CALC_XATTR_BYTES(ui->data_len);
  216. mutex_unlock(&host_ui->ui_mutex);
  217. make_bad_inode(inode);
  218. out_free:
  219. ubifs_release_budget(c, &req);
  220. return err;
  221. }
  222. /**
  223. * check_namespace - check extended attribute name-space.
  224. * @nm: extended attribute name
  225. *
  226. * This function makes sure the extended attribute name belongs to one of the
  227. * supported extended attribute name-spaces. Returns name-space index in case
  228. * of success and a negative error code in case of failure.
  229. */
  230. static int check_namespace(const struct qstr *nm)
  231. {
  232. int type;
  233. if (nm->len > UBIFS_MAX_NLEN)
  234. return -ENAMETOOLONG;
  235. if (!strncmp(nm->name, XATTR_TRUSTED_PREFIX,
  236. XATTR_TRUSTED_PREFIX_LEN)) {
  237. if (nm->name[sizeof(XATTR_TRUSTED_PREFIX) - 1] == '\0')
  238. return -EINVAL;
  239. type = TRUSTED_XATTR;
  240. } else if (!strncmp(nm->name, XATTR_USER_PREFIX,
  241. XATTR_USER_PREFIX_LEN)) {
  242. if (nm->name[XATTR_USER_PREFIX_LEN] == '\0')
  243. return -EINVAL;
  244. type = USER_XATTR;
  245. } else if (!strncmp(nm->name, XATTR_SECURITY_PREFIX,
  246. XATTR_SECURITY_PREFIX_LEN)) {
  247. if (nm->name[sizeof(XATTR_SECURITY_PREFIX) - 1] == '\0')
  248. return -EINVAL;
  249. type = SECURITY_XATTR;
  250. } else
  251. return -EOPNOTSUPP;
  252. return type;
  253. }
  254. static struct inode *iget_xattr(struct ubifs_info *c, ino_t inum)
  255. {
  256. struct inode *inode;
  257. inode = ubifs_iget(c->vfs_sb, inum);
  258. if (IS_ERR(inode)) {
  259. ubifs_err("dead extended attribute entry, error %d",
  260. (int)PTR_ERR(inode));
  261. return inode;
  262. }
  263. if (ubifs_inode(inode)->xattr)
  264. return inode;
  265. ubifs_err("corrupt extended attribute entry");
  266. iput(inode);
  267. return ERR_PTR(-EINVAL);
  268. }
  269. int ubifs_setxattr(struct dentry *dentry, const char *name,
  270. const void *value, size_t size, int flags)
  271. {
  272. struct inode *inode, *host = dentry->d_inode;
  273. struct ubifs_info *c = host->i_sb->s_fs_info;
  274. struct qstr nm = { .name = name, .len = strlen(name) };
  275. struct ubifs_dent_node *xent;
  276. union ubifs_key key;
  277. int err, type;
  278. dbg_gen("xattr '%s', host ino %lu ('%.*s'), size %zd", name,
  279. host->i_ino, dentry->d_name.len, dentry->d_name.name, size);
  280. ubifs_assert(mutex_is_locked(&host->i_mutex));
  281. if (size > UBIFS_MAX_INO_DATA)
  282. return -ERANGE;
  283. type = check_namespace(&nm);
  284. if (type < 0)
  285. return type;
  286. xent = kmalloc(UBIFS_MAX_XENT_NODE_SZ, GFP_NOFS);
  287. if (!xent)
  288. return -ENOMEM;
  289. /*
  290. * The extended attribute entries are stored in LNC, so multiple
  291. * look-ups do not involve reading the flash.
  292. */
  293. xent_key_init(c, &key, host->i_ino, &nm);
  294. err = ubifs_tnc_lookup_nm(c, &key, xent, &nm);
  295. if (err) {
  296. if (err != -ENOENT)
  297. goto out_free;
  298. if (flags & XATTR_REPLACE)
  299. /* We are asked not to create the xattr */
  300. err = -ENODATA;
  301. else
  302. err = create_xattr(c, host, &nm, value, size);
  303. goto out_free;
  304. }
  305. if (flags & XATTR_CREATE) {
  306. /* We are asked not to replace the xattr */
  307. err = -EEXIST;
  308. goto out_free;
  309. }
  310. inode = iget_xattr(c, le64_to_cpu(xent->inum));
  311. if (IS_ERR(inode)) {
  312. err = PTR_ERR(inode);
  313. goto out_free;
  314. }
  315. err = change_xattr(c, host, inode, value, size);
  316. iput(inode);
  317. out_free:
  318. kfree(xent);
  319. return err;
  320. }
  321. ssize_t ubifs_getxattr(struct dentry *dentry, const char *name, void *buf,
  322. size_t size)
  323. {
  324. struct inode *inode, *host = dentry->d_inode;
  325. struct ubifs_info *c = host->i_sb->s_fs_info;
  326. struct qstr nm = { .name = name, .len = strlen(name) };
  327. struct ubifs_inode *ui;
  328. struct ubifs_dent_node *xent;
  329. union ubifs_key key;
  330. int err;
  331. dbg_gen("xattr '%s', ino %lu ('%.*s'), buf size %zd", name,
  332. host->i_ino, dentry->d_name.len, dentry->d_name.name, size);
  333. err = check_namespace(&nm);
  334. if (err < 0)
  335. return err;
  336. xent = kmalloc(UBIFS_MAX_XENT_NODE_SZ, GFP_NOFS);
  337. if (!xent)
  338. return -ENOMEM;
  339. xent_key_init(c, &key, host->i_ino, &nm);
  340. err = ubifs_tnc_lookup_nm(c, &key, xent, &nm);
  341. if (err) {
  342. if (err == -ENOENT)
  343. err = -ENODATA;
  344. goto out_unlock;
  345. }
  346. inode = iget_xattr(c, le64_to_cpu(xent->inum));
  347. if (IS_ERR(inode)) {
  348. err = PTR_ERR(inode);
  349. goto out_unlock;
  350. }
  351. ui = ubifs_inode(inode);
  352. ubifs_assert(inode->i_size == ui->data_len);
  353. ubifs_assert(ubifs_inode(host)->xattr_size > ui->data_len);
  354. if (buf) {
  355. /* If @buf is %NULL we are supposed to return the length */
  356. if (ui->data_len > size) {
  357. dbg_err("buffer size %zd, xattr len %d",
  358. size, ui->data_len);
  359. err = -ERANGE;
  360. goto out_iput;
  361. }
  362. memcpy(buf, ui->data, ui->data_len);
  363. }
  364. err = ui->data_len;
  365. out_iput:
  366. iput(inode);
  367. out_unlock:
  368. kfree(xent);
  369. return err;
  370. }
  371. ssize_t ubifs_listxattr(struct dentry *dentry, char *buffer, size_t size)
  372. {
  373. union ubifs_key key;
  374. struct inode *host = dentry->d_inode;
  375. struct ubifs_info *c = host->i_sb->s_fs_info;
  376. struct ubifs_inode *host_ui = ubifs_inode(host);
  377. struct ubifs_dent_node *xent, *pxent = NULL;
  378. int err, len, written = 0;
  379. struct qstr nm = { .name = NULL };
  380. dbg_gen("ino %lu ('%.*s'), buffer size %zd", host->i_ino,
  381. dentry->d_name.len, dentry->d_name.name, size);
  382. len = host_ui->xattr_names + host_ui->xattr_cnt;
  383. if (!buffer)
  384. /*
  385. * We should return the minimum buffer size which will fit a
  386. * null-terminated list of all the extended attribute names.
  387. */
  388. return len;
  389. if (len > size)
  390. return -ERANGE;
  391. lowest_xent_key(c, &key, host->i_ino);
  392. while (1) {
  393. int type;
  394. xent = ubifs_tnc_next_ent(c, &key, &nm);
  395. if (IS_ERR(xent)) {
  396. err = PTR_ERR(xent);
  397. break;
  398. }
  399. nm.name = xent->name;
  400. nm.len = le16_to_cpu(xent->nlen);
  401. type = check_namespace(&nm);
  402. if (unlikely(type < 0)) {
  403. err = type;
  404. break;
  405. }
  406. /* Show trusted namespace only for "power" users */
  407. if (type != TRUSTED_XATTR || capable(CAP_SYS_ADMIN)) {
  408. memcpy(buffer + written, nm.name, nm.len + 1);
  409. written += nm.len + 1;
  410. }
  411. kfree(pxent);
  412. pxent = xent;
  413. key_read(c, &xent->key, &key);
  414. }
  415. kfree(pxent);
  416. if (err != -ENOENT) {
  417. ubifs_err("cannot find next direntry, error %d", err);
  418. return err;
  419. }
  420. ubifs_assert(written <= size);
  421. return written;
  422. }
  423. static int remove_xattr(struct ubifs_info *c, struct inode *host,
  424. struct inode *inode, const struct qstr *nm)
  425. {
  426. int err;
  427. struct ubifs_inode *host_ui = ubifs_inode(host);
  428. struct ubifs_inode *ui = ubifs_inode(inode);
  429. struct ubifs_budget_req req = { .dirtied_ino = 2, .mod_dent = 1,
  430. .dirtied_ino_d = ALIGN(host_ui->data_len, 8) };
  431. ubifs_assert(ui->data_len == inode->i_size);
  432. err = ubifs_budget_space(c, &req);
  433. if (err)
  434. return err;
  435. mutex_lock(&host_ui->ui_mutex);
  436. host->i_ctime = ubifs_current_time(host);
  437. host_ui->xattr_cnt -= 1;
  438. host_ui->xattr_size -= CALC_DENT_SIZE(nm->len);
  439. host_ui->xattr_size -= CALC_XATTR_BYTES(ui->data_len);
  440. host_ui->xattr_names -= nm->len;
  441. err = ubifs_jnl_delete_xattr(c, host, inode, nm);
  442. if (err)
  443. goto out_cancel;
  444. mutex_unlock(&host_ui->ui_mutex);
  445. ubifs_release_budget(c, &req);
  446. return 0;
  447. out_cancel:
  448. host_ui->xattr_cnt += 1;
  449. host_ui->xattr_size += CALC_DENT_SIZE(nm->len);
  450. host_ui->xattr_size += CALC_XATTR_BYTES(ui->data_len);
  451. mutex_unlock(&host_ui->ui_mutex);
  452. ubifs_release_budget(c, &req);
  453. make_bad_inode(inode);
  454. return err;
  455. }
  456. int ubifs_removexattr(struct dentry *dentry, const char *name)
  457. {
  458. struct inode *inode, *host = dentry->d_inode;
  459. struct ubifs_info *c = host->i_sb->s_fs_info;
  460. struct qstr nm = { .name = name, .len = strlen(name) };
  461. struct ubifs_dent_node *xent;
  462. union ubifs_key key;
  463. int err;
  464. dbg_gen("xattr '%s', ino %lu ('%.*s')", name,
  465. host->i_ino, dentry->d_name.len, dentry->d_name.name);
  466. ubifs_assert(mutex_is_locked(&host->i_mutex));
  467. err = check_namespace(&nm);
  468. if (err < 0)
  469. return err;
  470. xent = kmalloc(UBIFS_MAX_XENT_NODE_SZ, GFP_NOFS);
  471. if (!xent)
  472. return -ENOMEM;
  473. xent_key_init(c, &key, host->i_ino, &nm);
  474. err = ubifs_tnc_lookup_nm(c, &key, xent, &nm);
  475. if (err) {
  476. if (err == -ENOENT)
  477. err = -ENODATA;
  478. goto out_free;
  479. }
  480. inode = iget_xattr(c, le64_to_cpu(xent->inum));
  481. if (IS_ERR(inode)) {
  482. err = PTR_ERR(inode);
  483. goto out_free;
  484. }
  485. ubifs_assert(inode->i_nlink == 1);
  486. inode->i_nlink = 0;
  487. err = remove_xattr(c, host, inode, &nm);
  488. if (err)
  489. inode->i_nlink = 1;
  490. /* If @i_nlink is 0, 'iput()' will delete the inode */
  491. iput(inode);
  492. out_free:
  493. kfree(xent);
  494. return err;
  495. }