xattr.c 16 KB

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