PageRenderTime 31ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/fs/ext4/xattr.c

https://bitbucket.org/zarboz/ville-upstream-test-branch
C | 1608 lines | 1259 code | 143 blank | 206 comment | 275 complexity | 7614be387b4fc15f2d6c054af5d7adc1 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, AGPL-1.0
  1. /*
  2. * linux/fs/ext4/xattr.c
  3. *
  4. * Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de>
  5. *
  6. * Fix by Harrison Xing <harrison@mountainviewdata.com>.
  7. * Ext4 code with a lot of help from Eric Jarman <ejarman@acm.org>.
  8. * Extended attributes for symlinks and special files added per
  9. * suggestion of Luka Renko <luka.renko@hermes.si>.
  10. * xattr consolidation Copyright (c) 2004 James Morris <jmorris@redhat.com>,
  11. * Red Hat Inc.
  12. * ea-in-inode support by Alex Tomas <alex@clusterfs.com> aka bzzz
  13. * and Andreas Gruenbacher <agruen@suse.de>.
  14. */
  15. /*
  16. * Extended attributes are stored directly in inodes (on file systems with
  17. * inodes bigger than 128 bytes) and on additional disk blocks. The i_file_acl
  18. * field contains the block number if an inode uses an additional block. All
  19. * attributes must fit in the inode and one additional block. Blocks that
  20. * contain the identical set of attributes may be shared among several inodes.
  21. * Identical blocks are detected by keeping a cache of blocks that have
  22. * recently been accessed.
  23. *
  24. * The attributes in inodes and on blocks have a different header; the entries
  25. * are stored in the same format:
  26. *
  27. * +------------------+
  28. * | header |
  29. * | entry 1 | |
  30. * | entry 2 | | growing downwards
  31. * | entry 3 | v
  32. * | four null bytes |
  33. * | . . . |
  34. * | value 1 | ^
  35. * | value 3 | | growing upwards
  36. * | value 2 | |
  37. * +------------------+
  38. *
  39. * The header is followed by multiple entry descriptors. In disk blocks, the
  40. * entry descriptors are kept sorted. In inodes, they are unsorted. The
  41. * attribute values are aligned to the end of the block in no specific order.
  42. *
  43. * Locking strategy
  44. * ----------------
  45. * EXT4_I(inode)->i_file_acl is protected by EXT4_I(inode)->xattr_sem.
  46. * EA blocks are only changed if they are exclusive to an inode, so
  47. * holding xattr_sem also means that nothing but the EA block's reference
  48. * count can change. Multiple writers to the same block are synchronized
  49. * by the buffer lock.
  50. */
  51. #include <linux/init.h>
  52. #include <linux/fs.h>
  53. #include <linux/slab.h>
  54. #include <linux/mbcache.h>
  55. #include <linux/quotaops.h>
  56. #include <linux/rwsem.h>
  57. #include "ext4_jbd2.h"
  58. #include "ext4.h"
  59. #include "xattr.h"
  60. #include "acl.h"
  61. #define BHDR(bh) ((struct ext4_xattr_header *)((bh)->b_data))
  62. #define ENTRY(ptr) ((struct ext4_xattr_entry *)(ptr))
  63. #define BFIRST(bh) ENTRY(BHDR(bh)+1)
  64. #define IS_LAST_ENTRY(entry) (*(__u32 *)(entry) == 0)
  65. #ifdef EXT4_XATTR_DEBUG
  66. # define ea_idebug(inode, f...) do { \
  67. printk(KERN_DEBUG "inode %s:%lu: ", \
  68. inode->i_sb->s_id, inode->i_ino); \
  69. printk(f); \
  70. printk("\n"); \
  71. } while (0)
  72. # define ea_bdebug(bh, f...) do { \
  73. char b[BDEVNAME_SIZE]; \
  74. printk(KERN_DEBUG "block %s:%lu: ", \
  75. bdevname(bh->b_bdev, b), \
  76. (unsigned long) bh->b_blocknr); \
  77. printk(f); \
  78. printk("\n"); \
  79. } while (0)
  80. #else
  81. # define ea_idebug(f...)
  82. # define ea_bdebug(f...)
  83. #endif
  84. static void ext4_xattr_cache_insert(struct buffer_head *);
  85. static struct buffer_head *ext4_xattr_cache_find(struct inode *,
  86. struct ext4_xattr_header *,
  87. struct mb_cache_entry **);
  88. static void ext4_xattr_rehash(struct ext4_xattr_header *,
  89. struct ext4_xattr_entry *);
  90. static int ext4_xattr_list(struct dentry *dentry, char *buffer,
  91. size_t buffer_size);
  92. static struct mb_cache *ext4_xattr_cache;
  93. static const struct xattr_handler *ext4_xattr_handler_map[] = {
  94. [EXT4_XATTR_INDEX_USER] = &ext4_xattr_user_handler,
  95. #ifdef CONFIG_EXT4_FS_POSIX_ACL
  96. [EXT4_XATTR_INDEX_POSIX_ACL_ACCESS] = &ext4_xattr_acl_access_handler,
  97. [EXT4_XATTR_INDEX_POSIX_ACL_DEFAULT] = &ext4_xattr_acl_default_handler,
  98. #endif
  99. [EXT4_XATTR_INDEX_TRUSTED] = &ext4_xattr_trusted_handler,
  100. #ifdef CONFIG_EXT4_FS_SECURITY
  101. [EXT4_XATTR_INDEX_SECURITY] = &ext4_xattr_security_handler,
  102. #endif
  103. };
  104. const struct xattr_handler *ext4_xattr_handlers[] = {
  105. &ext4_xattr_user_handler,
  106. &ext4_xattr_trusted_handler,
  107. #ifdef CONFIG_EXT4_FS_POSIX_ACL
  108. &ext4_xattr_acl_access_handler,
  109. &ext4_xattr_acl_default_handler,
  110. #endif
  111. #ifdef CONFIG_EXT4_FS_SECURITY
  112. &ext4_xattr_security_handler,
  113. #endif
  114. NULL
  115. };
  116. static inline const struct xattr_handler *
  117. ext4_xattr_handler(int name_index)
  118. {
  119. const struct xattr_handler *handler = NULL;
  120. if (name_index > 0 && name_index < ARRAY_SIZE(ext4_xattr_handler_map))
  121. handler = ext4_xattr_handler_map[name_index];
  122. return handler;
  123. }
  124. /*
  125. * Inode operation listxattr()
  126. *
  127. * dentry->d_inode->i_mutex: don't care
  128. */
  129. ssize_t
  130. ext4_listxattr(struct dentry *dentry, char *buffer, size_t size)
  131. {
  132. return ext4_xattr_list(dentry, buffer, size);
  133. }
  134. static int
  135. ext4_xattr_check_names(struct ext4_xattr_entry *entry, void *end)
  136. {
  137. while (!IS_LAST_ENTRY(entry)) {
  138. struct ext4_xattr_entry *next = EXT4_XATTR_NEXT(entry);
  139. if ((void *)next >= end)
  140. return -EIO;
  141. entry = next;
  142. }
  143. return 0;
  144. }
  145. static inline int
  146. ext4_xattr_check_block(struct buffer_head *bh)
  147. {
  148. int error;
  149. if (BHDR(bh)->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC) ||
  150. BHDR(bh)->h_blocks != cpu_to_le32(1))
  151. return -EIO;
  152. error = ext4_xattr_check_names(BFIRST(bh), bh->b_data + bh->b_size);
  153. return error;
  154. }
  155. static inline int
  156. ext4_xattr_check_entry(struct ext4_xattr_entry *entry, size_t size)
  157. {
  158. size_t value_size = le32_to_cpu(entry->e_value_size);
  159. if (entry->e_value_block != 0 || value_size > size ||
  160. le16_to_cpu(entry->e_value_offs) + value_size > size)
  161. return -EIO;
  162. return 0;
  163. }
  164. static int
  165. ext4_xattr_find_entry(struct ext4_xattr_entry **pentry, int name_index,
  166. const char *name, size_t size, int sorted)
  167. {
  168. struct ext4_xattr_entry *entry;
  169. size_t name_len;
  170. int cmp = 1;
  171. if (name == NULL)
  172. return -EINVAL;
  173. name_len = strlen(name);
  174. entry = *pentry;
  175. for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
  176. cmp = name_index - entry->e_name_index;
  177. if (!cmp)
  178. cmp = name_len - entry->e_name_len;
  179. if (!cmp)
  180. cmp = memcmp(name, entry->e_name, name_len);
  181. if (cmp <= 0 && (sorted || cmp == 0))
  182. break;
  183. }
  184. *pentry = entry;
  185. if (!cmp && ext4_xattr_check_entry(entry, size))
  186. return -EIO;
  187. return cmp ? -ENODATA : 0;
  188. }
  189. static int
  190. ext4_xattr_block_get(struct inode *inode, int name_index, const char *name,
  191. void *buffer, size_t buffer_size)
  192. {
  193. struct buffer_head *bh = NULL;
  194. struct ext4_xattr_entry *entry;
  195. size_t size;
  196. int error;
  197. ea_idebug(inode, "name=%d.%s, buffer=%p, buffer_size=%ld",
  198. name_index, name, buffer, (long)buffer_size);
  199. error = -ENODATA;
  200. if (!EXT4_I(inode)->i_file_acl)
  201. goto cleanup;
  202. ea_idebug(inode, "reading block %u", EXT4_I(inode)->i_file_acl);
  203. bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
  204. if (!bh)
  205. goto cleanup;
  206. ea_bdebug(bh, "b_count=%d, refcount=%d",
  207. atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
  208. if (ext4_xattr_check_block(bh)) {
  209. bad_block:
  210. EXT4_ERROR_INODE(inode, "bad block %llu",
  211. EXT4_I(inode)->i_file_acl);
  212. error = -EIO;
  213. goto cleanup;
  214. }
  215. ext4_xattr_cache_insert(bh);
  216. entry = BFIRST(bh);
  217. error = ext4_xattr_find_entry(&entry, name_index, name, bh->b_size, 1);
  218. if (error == -EIO)
  219. goto bad_block;
  220. if (error)
  221. goto cleanup;
  222. size = le32_to_cpu(entry->e_value_size);
  223. if (buffer) {
  224. error = -ERANGE;
  225. if (size > buffer_size)
  226. goto cleanup;
  227. memcpy(buffer, bh->b_data + le16_to_cpu(entry->e_value_offs),
  228. size);
  229. }
  230. error = size;
  231. cleanup:
  232. brelse(bh);
  233. return error;
  234. }
  235. static int
  236. ext4_xattr_ibody_get(struct inode *inode, int name_index, const char *name,
  237. void *buffer, size_t buffer_size)
  238. {
  239. struct ext4_xattr_ibody_header *header;
  240. struct ext4_xattr_entry *entry;
  241. struct ext4_inode *raw_inode;
  242. struct ext4_iloc iloc;
  243. size_t size;
  244. void *end;
  245. int error;
  246. if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
  247. return -ENODATA;
  248. error = ext4_get_inode_loc(inode, &iloc);
  249. if (error)
  250. return error;
  251. raw_inode = ext4_raw_inode(&iloc);
  252. header = IHDR(inode, raw_inode);
  253. entry = IFIRST(header);
  254. end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
  255. error = ext4_xattr_check_names(entry, end);
  256. if (error)
  257. goto cleanup;
  258. error = ext4_xattr_find_entry(&entry, name_index, name,
  259. end - (void *)entry, 0);
  260. if (error)
  261. goto cleanup;
  262. size = le32_to_cpu(entry->e_value_size);
  263. if (buffer) {
  264. error = -ERANGE;
  265. if (size > buffer_size)
  266. goto cleanup;
  267. memcpy(buffer, (void *)IFIRST(header) +
  268. le16_to_cpu(entry->e_value_offs), size);
  269. }
  270. error = size;
  271. cleanup:
  272. brelse(iloc.bh);
  273. return error;
  274. }
  275. /*
  276. * ext4_xattr_get()
  277. *
  278. * Copy an extended attribute into the buffer
  279. * provided, or compute the buffer size required.
  280. * Buffer is NULL to compute the size of the buffer required.
  281. *
  282. * Returns a negative error number on failure, or the number of bytes
  283. * used / required on success.
  284. */
  285. int
  286. ext4_xattr_get(struct inode *inode, int name_index, const char *name,
  287. void *buffer, size_t buffer_size)
  288. {
  289. int error;
  290. down_read(&EXT4_I(inode)->xattr_sem);
  291. error = ext4_xattr_ibody_get(inode, name_index, name, buffer,
  292. buffer_size);
  293. if (error == -ENODATA)
  294. error = ext4_xattr_block_get(inode, name_index, name, buffer,
  295. buffer_size);
  296. up_read(&EXT4_I(inode)->xattr_sem);
  297. return error;
  298. }
  299. static int
  300. ext4_xattr_list_entries(struct dentry *dentry, struct ext4_xattr_entry *entry,
  301. char *buffer, size_t buffer_size)
  302. {
  303. size_t rest = buffer_size;
  304. for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
  305. const struct xattr_handler *handler =
  306. ext4_xattr_handler(entry->e_name_index);
  307. if (handler) {
  308. size_t size = handler->list(dentry, buffer, rest,
  309. entry->e_name,
  310. entry->e_name_len,
  311. handler->flags);
  312. if (buffer) {
  313. if (size > rest)
  314. return -ERANGE;
  315. buffer += size;
  316. }
  317. rest -= size;
  318. }
  319. }
  320. return buffer_size - rest;
  321. }
  322. static int
  323. ext4_xattr_block_list(struct dentry *dentry, char *buffer, size_t buffer_size)
  324. {
  325. struct inode *inode = dentry->d_inode;
  326. struct buffer_head *bh = NULL;
  327. int error;
  328. ea_idebug(inode, "buffer=%p, buffer_size=%ld",
  329. buffer, (long)buffer_size);
  330. error = 0;
  331. if (!EXT4_I(inode)->i_file_acl)
  332. goto cleanup;
  333. ea_idebug(inode, "reading block %u", EXT4_I(inode)->i_file_acl);
  334. bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
  335. error = -EIO;
  336. if (!bh)
  337. goto cleanup;
  338. ea_bdebug(bh, "b_count=%d, refcount=%d",
  339. atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
  340. if (ext4_xattr_check_block(bh)) {
  341. EXT4_ERROR_INODE(inode, "bad block %llu",
  342. EXT4_I(inode)->i_file_acl);
  343. error = -EIO;
  344. goto cleanup;
  345. }
  346. ext4_xattr_cache_insert(bh);
  347. error = ext4_xattr_list_entries(dentry, BFIRST(bh), buffer, buffer_size);
  348. cleanup:
  349. brelse(bh);
  350. return error;
  351. }
  352. static int
  353. ext4_xattr_ibody_list(struct dentry *dentry, char *buffer, size_t buffer_size)
  354. {
  355. struct inode *inode = dentry->d_inode;
  356. struct ext4_xattr_ibody_header *header;
  357. struct ext4_inode *raw_inode;
  358. struct ext4_iloc iloc;
  359. void *end;
  360. int error;
  361. if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
  362. return 0;
  363. error = ext4_get_inode_loc(inode, &iloc);
  364. if (error)
  365. return error;
  366. raw_inode = ext4_raw_inode(&iloc);
  367. header = IHDR(inode, raw_inode);
  368. end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
  369. error = ext4_xattr_check_names(IFIRST(header), end);
  370. if (error)
  371. goto cleanup;
  372. error = ext4_xattr_list_entries(dentry, IFIRST(header),
  373. buffer, buffer_size);
  374. cleanup:
  375. brelse(iloc.bh);
  376. return error;
  377. }
  378. /*
  379. * ext4_xattr_list()
  380. *
  381. * Copy a list of attribute names into the buffer
  382. * provided, or compute the buffer size required.
  383. * Buffer is NULL to compute the size of the buffer required.
  384. *
  385. * Returns a negative error number on failure, or the number of bytes
  386. * used / required on success.
  387. */
  388. static int
  389. ext4_xattr_list(struct dentry *dentry, char *buffer, size_t buffer_size)
  390. {
  391. int ret, ret2;
  392. down_read(&EXT4_I(dentry->d_inode)->xattr_sem);
  393. ret = ret2 = ext4_xattr_ibody_list(dentry, buffer, buffer_size);
  394. if (ret < 0)
  395. goto errout;
  396. if (buffer) {
  397. buffer += ret;
  398. buffer_size -= ret;
  399. }
  400. ret = ext4_xattr_block_list(dentry, buffer, buffer_size);
  401. if (ret < 0)
  402. goto errout;
  403. ret += ret2;
  404. errout:
  405. up_read(&EXT4_I(dentry->d_inode)->xattr_sem);
  406. return ret;
  407. }
  408. /*
  409. * If the EXT4_FEATURE_COMPAT_EXT_ATTR feature of this file system is
  410. * not set, set it.
  411. */
  412. static void ext4_xattr_update_super_block(handle_t *handle,
  413. struct super_block *sb)
  414. {
  415. if (EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_EXT_ATTR))
  416. return;
  417. if (ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh) == 0) {
  418. EXT4_SET_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_EXT_ATTR);
  419. ext4_handle_dirty_super(handle, sb);
  420. }
  421. }
  422. /*
  423. * Release the xattr block BH: If the reference count is > 1, decrement
  424. * it; otherwise free the block.
  425. */
  426. static void
  427. ext4_xattr_release_block(handle_t *handle, struct inode *inode,
  428. struct buffer_head *bh)
  429. {
  430. struct mb_cache_entry *ce = NULL;
  431. int error = 0;
  432. ce = mb_cache_entry_get(ext4_xattr_cache, bh->b_bdev, bh->b_blocknr);
  433. error = ext4_journal_get_write_access(handle, bh);
  434. if (error)
  435. goto out;
  436. lock_buffer(bh);
  437. if (BHDR(bh)->h_refcount == cpu_to_le32(1)) {
  438. ea_bdebug(bh, "refcount now=0; freeing");
  439. if (ce)
  440. mb_cache_entry_free(ce);
  441. get_bh(bh);
  442. ext4_free_blocks(handle, inode, bh, 0, 1,
  443. EXT4_FREE_BLOCKS_METADATA |
  444. EXT4_FREE_BLOCKS_FORGET);
  445. unlock_buffer(bh);
  446. } else {
  447. le32_add_cpu(&BHDR(bh)->h_refcount, -1);
  448. if (ce)
  449. mb_cache_entry_release(ce);
  450. unlock_buffer(bh);
  451. error = ext4_handle_dirty_metadata(handle, inode, bh);
  452. if (IS_SYNC(inode))
  453. ext4_handle_sync(handle);
  454. dquot_free_block(inode, 1);
  455. ea_bdebug(bh, "refcount now=%d; releasing",
  456. le32_to_cpu(BHDR(bh)->h_refcount));
  457. }
  458. out:
  459. ext4_std_error(inode->i_sb, error);
  460. return;
  461. }
  462. /*
  463. * Find the available free space for EAs. This also returns the total number of
  464. * bytes used by EA entries.
  465. */
  466. static size_t ext4_xattr_free_space(struct ext4_xattr_entry *last,
  467. size_t *min_offs, void *base, int *total)
  468. {
  469. for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
  470. *total += EXT4_XATTR_LEN(last->e_name_len);
  471. if (!last->e_value_block && last->e_value_size) {
  472. size_t offs = le16_to_cpu(last->e_value_offs);
  473. if (offs < *min_offs)
  474. *min_offs = offs;
  475. }
  476. }
  477. return (*min_offs - ((void *)last - base) - sizeof(__u32));
  478. }
  479. struct ext4_xattr_info {
  480. int name_index;
  481. const char *name;
  482. const void *value;
  483. size_t value_len;
  484. };
  485. struct ext4_xattr_search {
  486. struct ext4_xattr_entry *first;
  487. void *base;
  488. void *end;
  489. struct ext4_xattr_entry *here;
  490. int not_found;
  491. };
  492. static int
  493. ext4_xattr_set_entry(struct ext4_xattr_info *i, struct ext4_xattr_search *s)
  494. {
  495. struct ext4_xattr_entry *last;
  496. size_t free, min_offs = s->end - s->base, name_len = strlen(i->name);
  497. /* Compute min_offs and last. */
  498. last = s->first;
  499. for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
  500. if (!last->e_value_block && last->e_value_size) {
  501. size_t offs = le16_to_cpu(last->e_value_offs);
  502. if (offs < min_offs)
  503. min_offs = offs;
  504. }
  505. }
  506. free = min_offs - ((void *)last - s->base) - sizeof(__u32);
  507. if (!s->not_found) {
  508. if (!s->here->e_value_block && s->here->e_value_size) {
  509. size_t size = le32_to_cpu(s->here->e_value_size);
  510. free += EXT4_XATTR_SIZE(size);
  511. }
  512. free += EXT4_XATTR_LEN(name_len);
  513. }
  514. if (i->value) {
  515. if (free < EXT4_XATTR_SIZE(i->value_len) ||
  516. free < EXT4_XATTR_LEN(name_len) +
  517. EXT4_XATTR_SIZE(i->value_len))
  518. return -ENOSPC;
  519. }
  520. if (i->value && s->not_found) {
  521. /* Insert the new name. */
  522. size_t size = EXT4_XATTR_LEN(name_len);
  523. size_t rest = (void *)last - (void *)s->here + sizeof(__u32);
  524. memmove((void *)s->here + size, s->here, rest);
  525. memset(s->here, 0, size);
  526. s->here->e_name_index = i->name_index;
  527. s->here->e_name_len = name_len;
  528. memcpy(s->here->e_name, i->name, name_len);
  529. } else {
  530. if (!s->here->e_value_block && s->here->e_value_size) {
  531. void *first_val = s->base + min_offs;
  532. size_t offs = le16_to_cpu(s->here->e_value_offs);
  533. void *val = s->base + offs;
  534. size_t size = EXT4_XATTR_SIZE(
  535. le32_to_cpu(s->here->e_value_size));
  536. if (i->value && size == EXT4_XATTR_SIZE(i->value_len)) {
  537. /* The old and the new value have the same
  538. size. Just replace. */
  539. s->here->e_value_size =
  540. cpu_to_le32(i->value_len);
  541. memset(val + size - EXT4_XATTR_PAD, 0,
  542. EXT4_XATTR_PAD); /* Clear pad bytes. */
  543. memcpy(val, i->value, i->value_len);
  544. return 0;
  545. }
  546. /* Remove the old value. */
  547. memmove(first_val + size, first_val, val - first_val);
  548. memset(first_val, 0, size);
  549. s->here->e_value_size = 0;
  550. s->here->e_value_offs = 0;
  551. min_offs += size;
  552. /* Adjust all value offsets. */
  553. last = s->first;
  554. while (!IS_LAST_ENTRY(last)) {
  555. size_t o = le16_to_cpu(last->e_value_offs);
  556. if (!last->e_value_block &&
  557. last->e_value_size && o < offs)
  558. last->e_value_offs =
  559. cpu_to_le16(o + size);
  560. last = EXT4_XATTR_NEXT(last);
  561. }
  562. }
  563. if (!i->value) {
  564. /* Remove the old name. */
  565. size_t size = EXT4_XATTR_LEN(name_len);
  566. last = ENTRY((void *)last - size);
  567. memmove(s->here, (void *)s->here + size,
  568. (void *)last - (void *)s->here + sizeof(__u32));
  569. memset(last, 0, size);
  570. }
  571. }
  572. if (i->value) {
  573. /* Insert the new value. */
  574. s->here->e_value_size = cpu_to_le32(i->value_len);
  575. if (i->value_len) {
  576. size_t size = EXT4_XATTR_SIZE(i->value_len);
  577. void *val = s->base + min_offs - size;
  578. s->here->e_value_offs = cpu_to_le16(min_offs - size);
  579. memset(val + size - EXT4_XATTR_PAD, 0,
  580. EXT4_XATTR_PAD); /* Clear the pad bytes. */
  581. memcpy(val, i->value, i->value_len);
  582. }
  583. }
  584. return 0;
  585. }
  586. struct ext4_xattr_block_find {
  587. struct ext4_xattr_search s;
  588. struct buffer_head *bh;
  589. };
  590. static int
  591. ext4_xattr_block_find(struct inode *inode, struct ext4_xattr_info *i,
  592. struct ext4_xattr_block_find *bs)
  593. {
  594. struct super_block *sb = inode->i_sb;
  595. int error;
  596. ea_idebug(inode, "name=%d.%s, value=%p, value_len=%ld",
  597. i->name_index, i->name, i->value, (long)i->value_len);
  598. if (EXT4_I(inode)->i_file_acl) {
  599. /* The inode already has an extended attribute block. */
  600. bs->bh = sb_bread(sb, EXT4_I(inode)->i_file_acl);
  601. error = -EIO;
  602. if (!bs->bh)
  603. goto cleanup;
  604. ea_bdebug(bs->bh, "b_count=%d, refcount=%d",
  605. atomic_read(&(bs->bh->b_count)),
  606. le32_to_cpu(BHDR(bs->bh)->h_refcount));
  607. if (ext4_xattr_check_block(bs->bh)) {
  608. EXT4_ERROR_INODE(inode, "bad block %llu",
  609. EXT4_I(inode)->i_file_acl);
  610. error = -EIO;
  611. goto cleanup;
  612. }
  613. /* Find the named attribute. */
  614. bs->s.base = BHDR(bs->bh);
  615. bs->s.first = BFIRST(bs->bh);
  616. bs->s.end = bs->bh->b_data + bs->bh->b_size;
  617. bs->s.here = bs->s.first;
  618. error = ext4_xattr_find_entry(&bs->s.here, i->name_index,
  619. i->name, bs->bh->b_size, 1);
  620. if (error && error != -ENODATA)
  621. goto cleanup;
  622. bs->s.not_found = error;
  623. }
  624. error = 0;
  625. cleanup:
  626. return error;
  627. }
  628. static int
  629. ext4_xattr_block_set(handle_t *handle, struct inode *inode,
  630. struct ext4_xattr_info *i,
  631. struct ext4_xattr_block_find *bs)
  632. {
  633. struct super_block *sb = inode->i_sb;
  634. struct buffer_head *new_bh = NULL;
  635. struct ext4_xattr_search *s = &bs->s;
  636. struct mb_cache_entry *ce = NULL;
  637. int error = 0;
  638. #define header(x) ((struct ext4_xattr_header *)(x))
  639. if (i->value && i->value_len > sb->s_blocksize)
  640. return -ENOSPC;
  641. if (s->base) {
  642. ce = mb_cache_entry_get(ext4_xattr_cache, bs->bh->b_bdev,
  643. bs->bh->b_blocknr);
  644. error = ext4_journal_get_write_access(handle, bs->bh);
  645. if (error)
  646. goto cleanup;
  647. lock_buffer(bs->bh);
  648. if (header(s->base)->h_refcount == cpu_to_le32(1)) {
  649. if (ce) {
  650. mb_cache_entry_free(ce);
  651. ce = NULL;
  652. }
  653. ea_bdebug(bs->bh, "modifying in-place");
  654. error = ext4_xattr_set_entry(i, s);
  655. if (!error) {
  656. if (!IS_LAST_ENTRY(s->first))
  657. ext4_xattr_rehash(header(s->base),
  658. s->here);
  659. ext4_xattr_cache_insert(bs->bh);
  660. }
  661. unlock_buffer(bs->bh);
  662. if (error == -EIO)
  663. goto bad_block;
  664. if (!error)
  665. error = ext4_handle_dirty_metadata(handle,
  666. inode,
  667. bs->bh);
  668. if (error)
  669. goto cleanup;
  670. goto inserted;
  671. } else {
  672. int offset = (char *)s->here - bs->bh->b_data;
  673. unlock_buffer(bs->bh);
  674. ext4_handle_release_buffer(handle, bs->bh);
  675. if (ce) {
  676. mb_cache_entry_release(ce);
  677. ce = NULL;
  678. }
  679. ea_bdebug(bs->bh, "cloning");
  680. s->base = kmalloc(bs->bh->b_size, GFP_NOFS);
  681. error = -ENOMEM;
  682. if (s->base == NULL)
  683. goto cleanup;
  684. memcpy(s->base, BHDR(bs->bh), bs->bh->b_size);
  685. s->first = ENTRY(header(s->base)+1);
  686. header(s->base)->h_refcount = cpu_to_le32(1);
  687. s->here = ENTRY(s->base + offset);
  688. s->end = s->base + bs->bh->b_size;
  689. }
  690. } else {
  691. /* Allocate a buffer where we construct the new block. */
  692. s->base = kzalloc(sb->s_blocksize, GFP_NOFS);
  693. /* assert(header == s->base) */
  694. error = -ENOMEM;
  695. if (s->base == NULL)
  696. goto cleanup;
  697. header(s->base)->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
  698. header(s->base)->h_blocks = cpu_to_le32(1);
  699. header(s->base)->h_refcount = cpu_to_le32(1);
  700. s->first = ENTRY(header(s->base)+1);
  701. s->here = ENTRY(header(s->base)+1);
  702. s->end = s->base + sb->s_blocksize;
  703. }
  704. error = ext4_xattr_set_entry(i, s);
  705. if (error == -EIO)
  706. goto bad_block;
  707. if (error)
  708. goto cleanup;
  709. if (!IS_LAST_ENTRY(s->first))
  710. ext4_xattr_rehash(header(s->base), s->here);
  711. inserted:
  712. if (!IS_LAST_ENTRY(s->first)) {
  713. new_bh = ext4_xattr_cache_find(inode, header(s->base), &ce);
  714. if (new_bh) {
  715. /* We found an identical block in the cache. */
  716. if (new_bh == bs->bh)
  717. ea_bdebug(new_bh, "keeping");
  718. else {
  719. /* The old block is released after updating
  720. the inode. */
  721. error = dquot_alloc_block(inode, 1);
  722. if (error)
  723. goto cleanup;
  724. error = ext4_journal_get_write_access(handle,
  725. new_bh);
  726. if (error)
  727. goto cleanup_dquot;
  728. lock_buffer(new_bh);
  729. le32_add_cpu(&BHDR(new_bh)->h_refcount, 1);
  730. ea_bdebug(new_bh, "reusing; refcount now=%d",
  731. le32_to_cpu(BHDR(new_bh)->h_refcount));
  732. unlock_buffer(new_bh);
  733. error = ext4_handle_dirty_metadata(handle,
  734. inode,
  735. new_bh);
  736. if (error)
  737. goto cleanup_dquot;
  738. }
  739. mb_cache_entry_release(ce);
  740. ce = NULL;
  741. } else if (bs->bh && s->base == bs->bh->b_data) {
  742. /* We were modifying this block in-place. */
  743. ea_bdebug(bs->bh, "keeping this block");
  744. new_bh = bs->bh;
  745. get_bh(new_bh);
  746. } else {
  747. /* We need to allocate a new block */
  748. ext4_fsblk_t goal, block;
  749. goal = ext4_group_first_block_no(sb,
  750. EXT4_I(inode)->i_block_group);
  751. /* non-extent files can't have physical blocks past 2^32 */
  752. if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
  753. goal = goal & EXT4_MAX_BLOCK_FILE_PHYS;
  754. /*
  755. * take i_data_sem because we will test
  756. * i_delalloc_reserved_flag in ext4_mb_new_blocks
  757. */
  758. down_read((&EXT4_I(inode)->i_data_sem));
  759. block = ext4_new_meta_blocks(handle, inode, goal, 0,
  760. NULL, &error);
  761. up_read((&EXT4_I(inode)->i_data_sem));
  762. if (error)
  763. goto cleanup;
  764. if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
  765. BUG_ON(block > EXT4_MAX_BLOCK_FILE_PHYS);
  766. ea_idebug(inode, "creating block %d", block);
  767. new_bh = sb_getblk(sb, block);
  768. if (!new_bh) {
  769. getblk_failed:
  770. ext4_free_blocks(handle, inode, NULL, block, 1,
  771. EXT4_FREE_BLOCKS_METADATA);
  772. error = -EIO;
  773. goto cleanup;
  774. }
  775. lock_buffer(new_bh);
  776. error = ext4_journal_get_create_access(handle, new_bh);
  777. if (error) {
  778. unlock_buffer(new_bh);
  779. goto getblk_failed;
  780. }
  781. memcpy(new_bh->b_data, s->base, new_bh->b_size);
  782. set_buffer_uptodate(new_bh);
  783. unlock_buffer(new_bh);
  784. ext4_xattr_cache_insert(new_bh);
  785. error = ext4_handle_dirty_metadata(handle,
  786. inode, new_bh);
  787. if (error)
  788. goto cleanup;
  789. }
  790. }
  791. /* Update the inode. */
  792. EXT4_I(inode)->i_file_acl = new_bh ? new_bh->b_blocknr : 0;
  793. /* Drop the previous xattr block. */
  794. if (bs->bh && bs->bh != new_bh)
  795. ext4_xattr_release_block(handle, inode, bs->bh);
  796. error = 0;
  797. cleanup:
  798. if (ce)
  799. mb_cache_entry_release(ce);
  800. brelse(new_bh);
  801. if (!(bs->bh && s->base == bs->bh->b_data))
  802. kfree(s->base);
  803. return error;
  804. cleanup_dquot:
  805. dquot_free_block(inode, 1);
  806. goto cleanup;
  807. bad_block:
  808. EXT4_ERROR_INODE(inode, "bad block %llu",
  809. EXT4_I(inode)->i_file_acl);
  810. goto cleanup;
  811. #undef header
  812. }
  813. struct ext4_xattr_ibody_find {
  814. struct ext4_xattr_search s;
  815. struct ext4_iloc iloc;
  816. };
  817. static int
  818. ext4_xattr_ibody_find(struct inode *inode, struct ext4_xattr_info *i,
  819. struct ext4_xattr_ibody_find *is)
  820. {
  821. struct ext4_xattr_ibody_header *header;
  822. struct ext4_inode *raw_inode;
  823. int error;
  824. if (EXT4_I(inode)->i_extra_isize == 0)
  825. return 0;
  826. raw_inode = ext4_raw_inode(&is->iloc);
  827. header = IHDR(inode, raw_inode);
  828. is->s.base = is->s.first = IFIRST(header);
  829. is->s.here = is->s.first;
  830. is->s.end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
  831. if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
  832. error = ext4_xattr_check_names(IFIRST(header), is->s.end);
  833. if (error)
  834. return error;
  835. /* Find the named attribute. */
  836. error = ext4_xattr_find_entry(&is->s.here, i->name_index,
  837. i->name, is->s.end -
  838. (void *)is->s.base, 0);
  839. if (error && error != -ENODATA)
  840. return error;
  841. is->s.not_found = error;
  842. }
  843. return 0;
  844. }
  845. static int
  846. ext4_xattr_ibody_set(handle_t *handle, struct inode *inode,
  847. struct ext4_xattr_info *i,
  848. struct ext4_xattr_ibody_find *is)
  849. {
  850. struct ext4_xattr_ibody_header *header;
  851. struct ext4_xattr_search *s = &is->s;
  852. int error;
  853. if (EXT4_I(inode)->i_extra_isize == 0)
  854. return -ENOSPC;
  855. error = ext4_xattr_set_entry(i, s);
  856. if (error)
  857. return error;
  858. header = IHDR(inode, ext4_raw_inode(&is->iloc));
  859. if (!IS_LAST_ENTRY(s->first)) {
  860. header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
  861. ext4_set_inode_state(inode, EXT4_STATE_XATTR);
  862. } else {
  863. header->h_magic = cpu_to_le32(0);
  864. ext4_clear_inode_state(inode, EXT4_STATE_XATTR);
  865. }
  866. return 0;
  867. }
  868. /*
  869. * ext4_xattr_set_handle()
  870. *
  871. * Create, replace or remove an extended attribute for this inode. Value
  872. * is NULL to remove an existing extended attribute, and non-NULL to
  873. * either replace an existing extended attribute, or create a new extended
  874. * attribute. The flags XATTR_REPLACE and XATTR_CREATE
  875. * specify that an extended attribute must exist and must not exist
  876. * previous to the call, respectively.
  877. *
  878. * Returns 0, or a negative error number on failure.
  879. */
  880. int
  881. ext4_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index,
  882. const char *name, const void *value, size_t value_len,
  883. int flags)
  884. {
  885. struct ext4_xattr_info i = {
  886. .name_index = name_index,
  887. .name = name,
  888. .value = value,
  889. .value_len = value_len,
  890. };
  891. struct ext4_xattr_ibody_find is = {
  892. .s = { .not_found = -ENODATA, },
  893. };
  894. struct ext4_xattr_block_find bs = {
  895. .s = { .not_found = -ENODATA, },
  896. };
  897. unsigned long no_expand;
  898. int error;
  899. if (!name)
  900. return -EINVAL;
  901. if (strlen(name) > 255)
  902. return -ERANGE;
  903. down_write(&EXT4_I(inode)->xattr_sem);
  904. no_expand = ext4_test_inode_state(inode, EXT4_STATE_NO_EXPAND);
  905. ext4_set_inode_state(inode, EXT4_STATE_NO_EXPAND);
  906. error = ext4_reserve_inode_write(handle, inode, &is.iloc);
  907. if (error)
  908. goto cleanup;
  909. if (ext4_test_inode_state(inode, EXT4_STATE_NEW)) {
  910. struct ext4_inode *raw_inode = ext4_raw_inode(&is.iloc);
  911. memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size);
  912. ext4_clear_inode_state(inode, EXT4_STATE_NEW);
  913. }
  914. error = ext4_xattr_ibody_find(inode, &i, &is);
  915. if (error)
  916. goto cleanup;
  917. if (is.s.not_found)
  918. error = ext4_xattr_block_find(inode, &i, &bs);
  919. if (error)
  920. goto cleanup;
  921. if (is.s.not_found && bs.s.not_found) {
  922. error = -ENODATA;
  923. if (flags & XATTR_REPLACE)
  924. goto cleanup;
  925. error = 0;
  926. if (!value)
  927. goto cleanup;
  928. } else {
  929. error = -EEXIST;
  930. if (flags & XATTR_CREATE)
  931. goto cleanup;
  932. }
  933. if (!value) {
  934. if (!is.s.not_found)
  935. error = ext4_xattr_ibody_set(handle, inode, &i, &is);
  936. else if (!bs.s.not_found)
  937. error = ext4_xattr_block_set(handle, inode, &i, &bs);
  938. } else {
  939. error = ext4_xattr_ibody_set(handle, inode, &i, &is);
  940. if (!error && !bs.s.not_found) {
  941. i.value = NULL;
  942. error = ext4_xattr_block_set(handle, inode, &i, &bs);
  943. } else if (error == -ENOSPC) {
  944. if (EXT4_I(inode)->i_file_acl && !bs.s.base) {
  945. error = ext4_xattr_block_find(inode, &i, &bs);
  946. if (error)
  947. goto cleanup;
  948. }
  949. error = ext4_xattr_block_set(handle, inode, &i, &bs);
  950. if (error)
  951. goto cleanup;
  952. if (!is.s.not_found) {
  953. i.value = NULL;
  954. error = ext4_xattr_ibody_set(handle, inode, &i,
  955. &is);
  956. }
  957. }
  958. }
  959. if (!error) {
  960. ext4_xattr_update_super_block(handle, inode->i_sb);
  961. inode->i_ctime = ext4_current_time(inode);
  962. if (!value)
  963. ext4_clear_inode_state(inode, EXT4_STATE_NO_EXPAND);
  964. error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
  965. /*
  966. * The bh is consumed by ext4_mark_iloc_dirty, even with
  967. * error != 0.
  968. */
  969. is.iloc.bh = NULL;
  970. if (IS_SYNC(inode))
  971. ext4_handle_sync(handle);
  972. }
  973. cleanup:
  974. brelse(is.iloc.bh);
  975. brelse(bs.bh);
  976. if (no_expand == 0)
  977. ext4_clear_inode_state(inode, EXT4_STATE_NO_EXPAND);
  978. up_write(&EXT4_I(inode)->xattr_sem);
  979. return error;
  980. }
  981. /*
  982. * ext4_xattr_set()
  983. *
  984. * Like ext4_xattr_set_handle, but start from an inode. This extended
  985. * attribute modification is a filesystem transaction by itself.
  986. *
  987. * Returns 0, or a negative error number on failure.
  988. */
  989. int
  990. ext4_xattr_set(struct inode *inode, int name_index, const char *name,
  991. const void *value, size_t value_len, int flags)
  992. {
  993. handle_t *handle;
  994. int error, retries = 0;
  995. retry:
  996. handle = ext4_journal_start(inode, EXT4_DATA_TRANS_BLOCKS(inode->i_sb));
  997. if (IS_ERR(handle)) {
  998. error = PTR_ERR(handle);
  999. } else {
  1000. int error2;
  1001. error = ext4_xattr_set_handle(handle, inode, name_index, name,
  1002. value, value_len, flags);
  1003. error2 = ext4_journal_stop(handle);
  1004. if (error == -ENOSPC &&
  1005. ext4_should_retry_alloc(inode->i_sb, &retries))
  1006. goto retry;
  1007. if (error == 0)
  1008. error = error2;
  1009. }
  1010. return error;
  1011. }
  1012. /*
  1013. * Shift the EA entries in the inode to create space for the increased
  1014. * i_extra_isize.
  1015. */
  1016. static void ext4_xattr_shift_entries(struct ext4_xattr_entry *entry,
  1017. int value_offs_shift, void *to,
  1018. void *from, size_t n, int blocksize)
  1019. {
  1020. struct ext4_xattr_entry *last = entry;
  1021. int new_offs;
  1022. /* Adjust the value offsets of the entries */
  1023. for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
  1024. if (!last->e_value_block && last->e_value_size) {
  1025. new_offs = le16_to_cpu(last->e_value_offs) +
  1026. value_offs_shift;
  1027. BUG_ON(new_offs + le32_to_cpu(last->e_value_size)
  1028. > blocksize);
  1029. last->e_value_offs = cpu_to_le16(new_offs);
  1030. }
  1031. }
  1032. /* Shift the entries by n bytes */
  1033. memmove(to, from, n);
  1034. }
  1035. /*
  1036. * Expand an inode by new_extra_isize bytes when EAs are present.
  1037. * Returns 0 on success or negative error number on failure.
  1038. */
  1039. int ext4_expand_extra_isize_ea(struct inode *inode, int new_extra_isize,
  1040. struct ext4_inode *raw_inode, handle_t *handle)
  1041. {
  1042. struct ext4_xattr_ibody_header *header;
  1043. struct ext4_xattr_entry *entry, *last, *first;
  1044. struct buffer_head *bh = NULL;
  1045. struct ext4_xattr_ibody_find *is = NULL;
  1046. struct ext4_xattr_block_find *bs = NULL;
  1047. char *buffer = NULL, *b_entry_name = NULL;
  1048. size_t min_offs, free;
  1049. int total_ino, total_blk;
  1050. void *base, *start, *end;
  1051. int extra_isize = 0, error = 0, tried_min_extra_isize = 0;
  1052. int s_min_extra_isize = le16_to_cpu(EXT4_SB(inode->i_sb)->s_es->s_min_extra_isize);
  1053. down_write(&EXT4_I(inode)->xattr_sem);
  1054. retry:
  1055. if (EXT4_I(inode)->i_extra_isize >= new_extra_isize) {
  1056. up_write(&EXT4_I(inode)->xattr_sem);
  1057. return 0;
  1058. }
  1059. header = IHDR(inode, raw_inode);
  1060. entry = IFIRST(header);
  1061. /*
  1062. * Check if enough free space is available in the inode to shift the
  1063. * entries ahead by new_extra_isize.
  1064. */
  1065. base = start = entry;
  1066. end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
  1067. min_offs = end - base;
  1068. last = entry;
  1069. total_ino = sizeof(struct ext4_xattr_ibody_header);
  1070. free = ext4_xattr_free_space(last, &min_offs, base, &total_ino);
  1071. if (free >= new_extra_isize) {
  1072. entry = IFIRST(header);
  1073. ext4_xattr_shift_entries(entry, EXT4_I(inode)->i_extra_isize
  1074. - new_extra_isize, (void *)raw_inode +
  1075. EXT4_GOOD_OLD_INODE_SIZE + new_extra_isize,
  1076. (void *)header, total_ino,
  1077. inode->i_sb->s_blocksize);
  1078. EXT4_I(inode)->i_extra_isize = new_extra_isize;
  1079. error = 0;
  1080. goto cleanup;
  1081. }
  1082. /*
  1083. * Enough free space isn't available in the inode, check if
  1084. * EA block can hold new_extra_isize bytes.
  1085. */
  1086. if (EXT4_I(inode)->i_file_acl) {
  1087. bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
  1088. error = -EIO;
  1089. if (!bh)
  1090. goto cleanup;
  1091. if (ext4_xattr_check_block(bh)) {
  1092. EXT4_ERROR_INODE(inode, "bad block %llu",
  1093. EXT4_I(inode)->i_file_acl);
  1094. error = -EIO;
  1095. goto cleanup;
  1096. }
  1097. base = BHDR(bh);
  1098. first = BFIRST(bh);
  1099. end = bh->b_data + bh->b_size;
  1100. min_offs = end - base;
  1101. free = ext4_xattr_free_space(first, &min_offs, base,
  1102. &total_blk);
  1103. if (free < new_extra_isize) {
  1104. if (!tried_min_extra_isize && s_min_extra_isize) {
  1105. tried_min_extra_isize++;
  1106. new_extra_isize = s_min_extra_isize;
  1107. brelse(bh);
  1108. goto retry;
  1109. }
  1110. error = -1;
  1111. goto cleanup;
  1112. }
  1113. } else {
  1114. free = inode->i_sb->s_blocksize;
  1115. }
  1116. while (new_extra_isize > 0) {
  1117. size_t offs, size, entry_size;
  1118. struct ext4_xattr_entry *small_entry = NULL;
  1119. struct ext4_xattr_info i = {
  1120. .value = NULL,
  1121. .value_len = 0,
  1122. };
  1123. unsigned int total_size; /* EA entry size + value size */
  1124. unsigned int shift_bytes; /* No. of bytes to shift EAs by? */
  1125. unsigned int min_total_size = ~0U;
  1126. is = kzalloc(sizeof(struct ext4_xattr_ibody_find), GFP_NOFS);
  1127. bs = kzalloc(sizeof(struct ext4_xattr_block_find), GFP_NOFS);
  1128. if (!is || !bs) {
  1129. error = -ENOMEM;
  1130. goto cleanup;
  1131. }
  1132. is->s.not_found = -ENODATA;
  1133. bs->s.not_found = -ENODATA;
  1134. is->iloc.bh = NULL;
  1135. bs->bh = NULL;
  1136. last = IFIRST(header);
  1137. /* Find the entry best suited to be pushed into EA block */
  1138. entry = NULL;
  1139. for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
  1140. total_size =
  1141. EXT4_XATTR_SIZE(le32_to_cpu(last->e_value_size)) +
  1142. EXT4_XATTR_LEN(last->e_name_len);
  1143. if (total_size <= free && total_size < min_total_size) {
  1144. if (total_size < new_extra_isize) {
  1145. small_entry = last;
  1146. } else {
  1147. entry = last;
  1148. min_total_size = total_size;
  1149. }
  1150. }
  1151. }
  1152. if (entry == NULL) {
  1153. if (small_entry) {
  1154. entry = small_entry;
  1155. } else {
  1156. if (!tried_min_extra_isize &&
  1157. s_min_extra_isize) {
  1158. tried_min_extra_isize++;
  1159. new_extra_isize = s_min_extra_isize;
  1160. goto retry;
  1161. }
  1162. error = -1;
  1163. goto cleanup;
  1164. }
  1165. }
  1166. offs = le16_to_cpu(entry->e_value_offs);
  1167. size = le32_to_cpu(entry->e_value_size);
  1168. entry_size = EXT4_XATTR_LEN(entry->e_name_len);
  1169. i.name_index = entry->e_name_index,
  1170. buffer = kmalloc(EXT4_XATTR_SIZE(size), GFP_NOFS);
  1171. b_entry_name = kmalloc(entry->e_name_len + 1, GFP_NOFS);
  1172. if (!buffer || !b_entry_name) {
  1173. error = -ENOMEM;
  1174. goto cleanup;
  1175. }
  1176. /* Save the entry name and the entry value */
  1177. memcpy(buffer, (void *)IFIRST(header) + offs,
  1178. EXT4_XATTR_SIZE(size));
  1179. memcpy(b_entry_name, entry->e_name, entry->e_name_len);
  1180. b_entry_name[entry->e_name_len] = '\0';
  1181. i.name = b_entry_name;
  1182. error = ext4_get_inode_loc(inode, &is->iloc);
  1183. if (error)
  1184. goto cleanup;
  1185. error = ext4_xattr_ibody_find(inode, &i, is);
  1186. if (error)
  1187. goto cleanup;
  1188. /* Remove the chosen entry from the inode */
  1189. error = ext4_xattr_ibody_set(handle, inode, &i, is);
  1190. if (error)
  1191. goto cleanup;
  1192. entry = IFIRST(header);
  1193. if (entry_size + EXT4_XATTR_SIZE(size) >= new_extra_isize)
  1194. shift_bytes = new_extra_isize;
  1195. else
  1196. shift_bytes = entry_size + size;
  1197. /* Adjust the offsets and shift the remaining entries ahead */
  1198. ext4_xattr_shift_entries(entry, EXT4_I(inode)->i_extra_isize -
  1199. shift_bytes, (void *)raw_inode +
  1200. EXT4_GOOD_OLD_INODE_SIZE + extra_isize + shift_bytes,
  1201. (void *)header, total_ino - entry_size,
  1202. inode->i_sb->s_blocksize);
  1203. extra_isize += shift_bytes;
  1204. new_extra_isize -= shift_bytes;
  1205. EXT4_I(inode)->i_extra_isize = extra_isize;
  1206. i.name = b_entry_name;
  1207. i.value = buffer;
  1208. i.value_len = size;
  1209. error = ext4_xattr_block_find(inode, &i, bs);
  1210. if (error)
  1211. goto cleanup;
  1212. /* Add entry which was removed from the inode into the block */
  1213. error = ext4_xattr_block_set(handle, inode, &i, bs);
  1214. if (error)
  1215. goto cleanup;
  1216. kfree(b_entry_name);
  1217. kfree(buffer);
  1218. b_entry_name = NULL;
  1219. buffer = NULL;
  1220. brelse(is->iloc.bh);
  1221. kfree(is);
  1222. kfree(bs);
  1223. }
  1224. brelse(bh);
  1225. up_write(&EXT4_I(inode)->xattr_sem);
  1226. return 0;
  1227. cleanup:
  1228. kfree(b_entry_name);
  1229. kfree(buffer);
  1230. if (is)
  1231. brelse(is->iloc.bh);
  1232. kfree(is);
  1233. kfree(bs);
  1234. brelse(bh);
  1235. up_write(&EXT4_I(inode)->xattr_sem);
  1236. return error;
  1237. }
  1238. /*
  1239. * ext4_xattr_delete_inode()
  1240. *
  1241. * Free extended attribute resources associated with this inode. This
  1242. * is called immediately before an inode is freed. We have exclusive
  1243. * access to the inode.
  1244. */
  1245. void
  1246. ext4_xattr_delete_inode(handle_t *handle, struct inode *inode)
  1247. {
  1248. struct buffer_head *bh = NULL;
  1249. if (!EXT4_I(inode)->i_file_acl)
  1250. goto cleanup;
  1251. bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
  1252. if (!bh) {
  1253. EXT4_ERROR_INODE(inode, "block %llu read error",
  1254. EXT4_I(inode)->i_file_acl);
  1255. goto cleanup;
  1256. }
  1257. if (BHDR(bh)->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC) ||
  1258. BHDR(bh)->h_blocks != cpu_to_le32(1)) {
  1259. EXT4_ERROR_INODE(inode, "bad block %llu",
  1260. EXT4_I(inode)->i_file_acl);
  1261. goto cleanup;
  1262. }
  1263. ext4_xattr_release_block(handle, inode, bh);
  1264. EXT4_I(inode)->i_file_acl = 0;
  1265. cleanup:
  1266. brelse(bh);
  1267. }
  1268. /*
  1269. * ext4_xattr_put_super()
  1270. *
  1271. * This is called when a file system is unmounted.
  1272. */
  1273. void
  1274. ext4_xattr_put_super(struct super_block *sb)
  1275. {
  1276. mb_cache_shrink(sb->s_bdev);
  1277. }
  1278. /*
  1279. * ext4_xattr_cache_insert()
  1280. *
  1281. * Create a new entry in the extended attribute cache, and insert
  1282. * it unless such an entry is already in the cache.
  1283. *
  1284. * Returns 0, or a negative error number on failure.
  1285. */
  1286. static void
  1287. ext4_xattr_cache_insert(struct buffer_head *bh)
  1288. {
  1289. __u32 hash = le32_to_cpu(BHDR(bh)->h_hash);
  1290. struct mb_cache_entry *ce;
  1291. int error;
  1292. ce = mb_cache_entry_alloc(ext4_xattr_cache, GFP_NOFS);
  1293. if (!ce) {
  1294. ea_bdebug(bh, "out of memory");
  1295. return;
  1296. }
  1297. error = mb_cache_entry_insert(ce, bh->b_bdev, bh->b_blocknr, hash);
  1298. if (error) {
  1299. mb_cache_entry_free(ce);
  1300. if (error == -EBUSY) {
  1301. ea_bdebug(bh, "already in cache");
  1302. error = 0;
  1303. }
  1304. } else {
  1305. ea_bdebug(bh, "inserting [%x]", (int)hash);
  1306. mb_cache_entry_release(ce);
  1307. }
  1308. }
  1309. /*
  1310. * ext4_xattr_cmp()
  1311. *
  1312. * Compare two extended attribute blocks for equality.
  1313. *
  1314. * Returns 0 if the blocks are equal, 1 if they differ, and
  1315. * a negative error number on errors.
  1316. */
  1317. static int
  1318. ext4_xattr_cmp(struct ext4_xattr_header *header1,
  1319. struct ext4_xattr_header *header2)
  1320. {
  1321. struct ext4_xattr_entry *entry1, *entry2;
  1322. entry1 = ENTRY(header1+1);
  1323. entry2 = ENTRY(header2+1);
  1324. while (!IS_LAST_ENTRY(entry1)) {
  1325. if (IS_LAST_ENTRY(entry2))
  1326. return 1;
  1327. if (entry1->e_hash != entry2->e_hash ||
  1328. entry1->e_name_index != entry2->e_name_index ||
  1329. entry1->e_name_len != entry2->e_name_len ||
  1330. entry1->e_value_size != entry2->e_value_size ||
  1331. memcmp(entry1->e_name, entry2->e_name, entry1->e_name_len))
  1332. return 1;
  1333. if (entry1->e_value_block != 0 || entry2->e_value_block != 0)
  1334. return -EIO;
  1335. if (memcmp((char *)header1 + le16_to_cpu(entry1->e_value_offs),
  1336. (char *)header2 + le16_to_cpu(entry2->e_value_offs),
  1337. le32_to_cpu(entry1->e_value_size)))
  1338. return 1;
  1339. entry1 = EXT4_XATTR_NEXT(entry1);
  1340. entry2 = EXT4_XATTR_NEXT(entry2);
  1341. }
  1342. if (!IS_LAST_ENTRY(entry2))
  1343. return 1;
  1344. return 0;
  1345. }
  1346. /*
  1347. * ext4_xattr_cache_find()
  1348. *
  1349. * Find an identical extended attribute block.
  1350. *
  1351. * Returns a pointer to the block found, or NULL if such a block was
  1352. * not found or an error occurred.
  1353. */
  1354. static struct buffer_head *
  1355. ext4_xattr_cache_find(struct inode *inode, struct ext4_xattr_header *header,
  1356. struct mb_cache_entry **pce)
  1357. {
  1358. __u32 hash = le32_to_cpu(header->h_hash);
  1359. struct mb_cache_entry *ce;
  1360. if (!header->h_hash)
  1361. return NULL; /* never share */
  1362. ea_idebug(inode, "looking for cached blocks [%x]", (int)hash);
  1363. again:
  1364. ce = mb_cache_entry_find_first(ext4_xattr_cache, inode->i_sb->s_bdev,
  1365. hash);
  1366. while (ce) {
  1367. struct buffer_head *bh;
  1368. if (IS_ERR(ce)) {
  1369. if (PTR_ERR(ce) == -EAGAIN)
  1370. goto again;
  1371. break;
  1372. }
  1373. bh = sb_bread(inode->i_sb, ce->e_block);
  1374. if (!bh) {
  1375. EXT4_ERROR_INODE(inode, "block %lu read error",
  1376. (unsigned long) ce->e_block);
  1377. } else if (le32_to_cpu(BHDR(bh)->h_refcount) >=
  1378. EXT4_XATTR_REFCOUNT_MAX) {
  1379. ea_idebug(inode, "block %lu refcount %d>=%d",
  1380. (unsigned long) ce->e_block,
  1381. le32_to_cpu(BHDR(bh)->h_refcount),
  1382. EXT4_XATTR_REFCOUNT_MAX);
  1383. } else if (ext4_xattr_cmp(header, BHDR(bh)) == 0) {
  1384. *pce = ce;
  1385. return bh;
  1386. }
  1387. brelse(bh);
  1388. ce = mb_cache_entry_find_next(ce, inode->i_sb->s_bdev, hash);
  1389. }
  1390. return NULL;
  1391. }
  1392. #define NAME_HASH_SHIFT 5
  1393. #define VALUE_HASH_SHIFT 16
  1394. /*
  1395. * ext4_xattr_hash_entry()
  1396. *
  1397. * Compute the hash of an extended attribute.
  1398. */
  1399. static inline void ext4_xattr_hash_entry(struct ext4_xattr_header *header,
  1400. struct ext4_xattr_entry *entry)
  1401. {
  1402. __u32 hash = 0;
  1403. char *name = entry->e_name;
  1404. int n;
  1405. for (n = 0; n < entry->e_name_len; n++) {
  1406. hash = (hash << NAME_HASH_SHIFT) ^
  1407. (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^
  1408. *name++;
  1409. }
  1410. if (entry->e_value_block == 0 && entry->e_value_size != 0) {
  1411. __le32 *value = (__le32 *)((char *)header +
  1412. le16_to_cpu(entry->e_value_offs));
  1413. for (n = (le32_to_cpu(entry->e_value_size) +
  1414. EXT4_XATTR_ROUND) >> EXT4_XATTR_PAD_BITS; n; n--) {
  1415. hash = (hash << VALUE_HASH_SHIFT) ^
  1416. (hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^
  1417. le32_to_cpu(*value++);
  1418. }
  1419. }
  1420. entry->e_hash = cpu_to_le32(hash);
  1421. }
  1422. #undef NAME_HASH_SHIFT
  1423. #undef VALUE_HASH_SHIFT
  1424. #define BLOCK_HASH_SHIFT 16
  1425. /*
  1426. * ext4_xattr_rehash()
  1427. *
  1428. * Re-compute the extended attribute hash value after an entry has changed.
  1429. */
  1430. static void ext4_xattr_rehash(struct ext4_xattr_header *header,
  1431. struct ext4_xattr_entry *entry)
  1432. {
  1433. struct ext4_xattr_entry *here;
  1434. __u32 hash = 0;
  1435. ext4_xattr_hash_entry(header, entry);
  1436. here = ENTRY(header+1);
  1437. while (!IS_LAST_ENTRY(here)) {
  1438. if (!here->e_hash) {
  1439. /* Block is not shared if an entry's hash value == 0 */
  1440. hash = 0;
  1441. break;
  1442. }
  1443. hash = (hash << BLOCK_HASH_SHIFT) ^
  1444. (hash >> (8*sizeof(hash) - BLOCK_HASH_SHIFT)) ^
  1445. le32_to_cpu(here->e_hash);
  1446. here = EXT4_XATTR_NEXT(here);
  1447. }
  1448. header->h_hash = cpu_to_le32(hash);
  1449. }
  1450. #undef BLOCK_HASH_SHIFT
  1451. int __init
  1452. ext4_init_xattr(void)
  1453. {
  1454. ext4_xattr_cache = mb_cache_create("ext4_xattr", 6);
  1455. if (!ext4_xattr_cache)
  1456. return -ENOMEM;
  1457. return 0;
  1458. }
  1459. void
  1460. ext4_exit_xattr(void)
  1461. {
  1462. if (ext4_xattr_cache)
  1463. mb_cache_destroy(ext4_xattr_cache);
  1464. ext4_xattr_cache = NULL;
  1465. }