PageRenderTime 54ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/fs/ext3/xattr.c

https://github.com/mstsirkin/linux
C | 1336 lines | 1040 code | 117 blank | 179 comment | 234 complexity | 2d0658123f0e0d97dd8afc3154ab14ad MD5 | raw file
  1. /*
  2. * linux/fs/ext3/xattr.c
  3. *
  4. * Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de>
  5. *
  6. * Fix by Harrison Xing <harrison@mountainviewdata.com>.
  7. * Ext3 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. * EXT3_I(inode)->i_file_acl is protected by EXT3_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/ext3_jbd.h>
  55. #include <linux/ext3_fs.h>
  56. #include <linux/mbcache.h>
  57. #include <linux/quotaops.h>
  58. #include <linux/rwsem.h>
  59. #include "xattr.h"
  60. #include "acl.h"
  61. #define BHDR(bh) ((struct ext3_xattr_header *)((bh)->b_data))
  62. #define ENTRY(ptr) ((struct ext3_xattr_entry *)(ptr))
  63. #define BFIRST(bh) ENTRY(BHDR(bh)+1)
  64. #define IS_LAST_ENTRY(entry) (*(__u32 *)(entry) == 0)
  65. #define IHDR(inode, raw_inode) \
  66. ((struct ext3_xattr_ibody_header *) \
  67. ((void *)raw_inode + \
  68. EXT3_GOOD_OLD_INODE_SIZE + \
  69. EXT3_I(inode)->i_extra_isize))
  70. #define IFIRST(hdr) ((struct ext3_xattr_entry *)((hdr)+1))
  71. #ifdef EXT3_XATTR_DEBUG
  72. # define ea_idebug(inode, f...) do { \
  73. printk(KERN_DEBUG "inode %s:%lu: ", \
  74. inode->i_sb->s_id, inode->i_ino); \
  75. printk(f); \
  76. printk("\n"); \
  77. } while (0)
  78. # define ea_bdebug(bh, f...) do { \
  79. char b[BDEVNAME_SIZE]; \
  80. printk(KERN_DEBUG "block %s:%lu: ", \
  81. bdevname(bh->b_bdev, b), \
  82. (unsigned long) bh->b_blocknr); \
  83. printk(f); \
  84. printk("\n"); \
  85. } while (0)
  86. #else
  87. # define ea_idebug(f...)
  88. # define ea_bdebug(f...)
  89. #endif
  90. static void ext3_xattr_cache_insert(struct buffer_head *);
  91. static struct buffer_head *ext3_xattr_cache_find(struct inode *,
  92. struct ext3_xattr_header *,
  93. struct mb_cache_entry **);
  94. static void ext3_xattr_rehash(struct ext3_xattr_header *,
  95. struct ext3_xattr_entry *);
  96. static int ext3_xattr_list(struct dentry *dentry, char *buffer,
  97. size_t buffer_size);
  98. static struct mb_cache *ext3_xattr_cache;
  99. static const struct xattr_handler *ext3_xattr_handler_map[] = {
  100. [EXT3_XATTR_INDEX_USER] = &ext3_xattr_user_handler,
  101. #ifdef CONFIG_EXT3_FS_POSIX_ACL
  102. [EXT3_XATTR_INDEX_POSIX_ACL_ACCESS] = &ext3_xattr_acl_access_handler,
  103. [EXT3_XATTR_INDEX_POSIX_ACL_DEFAULT] = &ext3_xattr_acl_default_handler,
  104. #endif
  105. [EXT3_XATTR_INDEX_TRUSTED] = &ext3_xattr_trusted_handler,
  106. #ifdef CONFIG_EXT3_FS_SECURITY
  107. [EXT3_XATTR_INDEX_SECURITY] = &ext3_xattr_security_handler,
  108. #endif
  109. };
  110. const struct xattr_handler *ext3_xattr_handlers[] = {
  111. &ext3_xattr_user_handler,
  112. &ext3_xattr_trusted_handler,
  113. #ifdef CONFIG_EXT3_FS_POSIX_ACL
  114. &ext3_xattr_acl_access_handler,
  115. &ext3_xattr_acl_default_handler,
  116. #endif
  117. #ifdef CONFIG_EXT3_FS_SECURITY
  118. &ext3_xattr_security_handler,
  119. #endif
  120. NULL
  121. };
  122. static inline const struct xattr_handler *
  123. ext3_xattr_handler(int name_index)
  124. {
  125. const struct xattr_handler *handler = NULL;
  126. if (name_index > 0 && name_index < ARRAY_SIZE(ext3_xattr_handler_map))
  127. handler = ext3_xattr_handler_map[name_index];
  128. return handler;
  129. }
  130. /*
  131. * Inode operation listxattr()
  132. *
  133. * dentry->d_inode->i_mutex: don't care
  134. */
  135. ssize_t
  136. ext3_listxattr(struct dentry *dentry, char *buffer, size_t size)
  137. {
  138. return ext3_xattr_list(dentry, buffer, size);
  139. }
  140. static int
  141. ext3_xattr_check_names(struct ext3_xattr_entry *entry, void *end)
  142. {
  143. while (!IS_LAST_ENTRY(entry)) {
  144. struct ext3_xattr_entry *next = EXT3_XATTR_NEXT(entry);
  145. if ((void *)next >= end)
  146. return -EIO;
  147. entry = next;
  148. }
  149. return 0;
  150. }
  151. static inline int
  152. ext3_xattr_check_block(struct buffer_head *bh)
  153. {
  154. int error;
  155. if (BHDR(bh)->h_magic != cpu_to_le32(EXT3_XATTR_MAGIC) ||
  156. BHDR(bh)->h_blocks != cpu_to_le32(1))
  157. return -EIO;
  158. error = ext3_xattr_check_names(BFIRST(bh), bh->b_data + bh->b_size);
  159. return error;
  160. }
  161. static inline int
  162. ext3_xattr_check_entry(struct ext3_xattr_entry *entry, size_t size)
  163. {
  164. size_t value_size = le32_to_cpu(entry->e_value_size);
  165. if (entry->e_value_block != 0 || value_size > size ||
  166. le16_to_cpu(entry->e_value_offs) + value_size > size)
  167. return -EIO;
  168. return 0;
  169. }
  170. static int
  171. ext3_xattr_find_entry(struct ext3_xattr_entry **pentry, int name_index,
  172. const char *name, size_t size, int sorted)
  173. {
  174. struct ext3_xattr_entry *entry;
  175. size_t name_len;
  176. int cmp = 1;
  177. if (name == NULL)
  178. return -EINVAL;
  179. name_len = strlen(name);
  180. entry = *pentry;
  181. for (; !IS_LAST_ENTRY(entry); entry = EXT3_XATTR_NEXT(entry)) {
  182. cmp = name_index - entry->e_name_index;
  183. if (!cmp)
  184. cmp = name_len - entry->e_name_len;
  185. if (!cmp)
  186. cmp = memcmp(name, entry->e_name, name_len);
  187. if (cmp <= 0 && (sorted || cmp == 0))
  188. break;
  189. }
  190. *pentry = entry;
  191. if (!cmp && ext3_xattr_check_entry(entry, size))
  192. return -EIO;
  193. return cmp ? -ENODATA : 0;
  194. }
  195. static int
  196. ext3_xattr_block_get(struct inode *inode, int name_index, const char *name,
  197. void *buffer, size_t buffer_size)
  198. {
  199. struct buffer_head *bh = NULL;
  200. struct ext3_xattr_entry *entry;
  201. size_t size;
  202. int error;
  203. ea_idebug(inode, "name=%d.%s, buffer=%p, buffer_size=%ld",
  204. name_index, name, buffer, (long)buffer_size);
  205. error = -ENODATA;
  206. if (!EXT3_I(inode)->i_file_acl)
  207. goto cleanup;
  208. ea_idebug(inode, "reading block %u", EXT3_I(inode)->i_file_acl);
  209. bh = sb_bread(inode->i_sb, EXT3_I(inode)->i_file_acl);
  210. if (!bh)
  211. goto cleanup;
  212. ea_bdebug(bh, "b_count=%d, refcount=%d",
  213. atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
  214. if (ext3_xattr_check_block(bh)) {
  215. bad_block: ext3_error(inode->i_sb, __func__,
  216. "inode %lu: bad block "E3FSBLK, inode->i_ino,
  217. EXT3_I(inode)->i_file_acl);
  218. error = -EIO;
  219. goto cleanup;
  220. }
  221. ext3_xattr_cache_insert(bh);
  222. entry = BFIRST(bh);
  223. error = ext3_xattr_find_entry(&entry, name_index, name, bh->b_size, 1);
  224. if (error == -EIO)
  225. goto bad_block;
  226. if (error)
  227. goto cleanup;
  228. size = le32_to_cpu(entry->e_value_size);
  229. if (buffer) {
  230. error = -ERANGE;
  231. if (size > buffer_size)
  232. goto cleanup;
  233. memcpy(buffer, bh->b_data + le16_to_cpu(entry->e_value_offs),
  234. size);
  235. }
  236. error = size;
  237. cleanup:
  238. brelse(bh);
  239. return error;
  240. }
  241. static int
  242. ext3_xattr_ibody_get(struct inode *inode, int name_index, const char *name,
  243. void *buffer, size_t buffer_size)
  244. {
  245. struct ext3_xattr_ibody_header *header;
  246. struct ext3_xattr_entry *entry;
  247. struct ext3_inode *raw_inode;
  248. struct ext3_iloc iloc;
  249. size_t size;
  250. void *end;
  251. int error;
  252. if (!ext3_test_inode_state(inode, EXT3_STATE_XATTR))
  253. return -ENODATA;
  254. error = ext3_get_inode_loc(inode, &iloc);
  255. if (error)
  256. return error;
  257. raw_inode = ext3_raw_inode(&iloc);
  258. header = IHDR(inode, raw_inode);
  259. entry = IFIRST(header);
  260. end = (void *)raw_inode + EXT3_SB(inode->i_sb)->s_inode_size;
  261. error = ext3_xattr_check_names(entry, end);
  262. if (error)
  263. goto cleanup;
  264. error = ext3_xattr_find_entry(&entry, name_index, name,
  265. end - (void *)entry, 0);
  266. if (error)
  267. goto cleanup;
  268. size = le32_to_cpu(entry->e_value_size);
  269. if (buffer) {
  270. error = -ERANGE;
  271. if (size > buffer_size)
  272. goto cleanup;
  273. memcpy(buffer, (void *)IFIRST(header) +
  274. le16_to_cpu(entry->e_value_offs), size);
  275. }
  276. error = size;
  277. cleanup:
  278. brelse(iloc.bh);
  279. return error;
  280. }
  281. /*
  282. * ext3_xattr_get()
  283. *
  284. * Copy an extended attribute into the buffer
  285. * provided, or compute the buffer size required.
  286. * Buffer is NULL to compute the size of the buffer required.
  287. *
  288. * Returns a negative error number on failure, or the number of bytes
  289. * used / required on success.
  290. */
  291. int
  292. ext3_xattr_get(struct inode *inode, int name_index, const char *name,
  293. void *buffer, size_t buffer_size)
  294. {
  295. int error;
  296. down_read(&EXT3_I(inode)->xattr_sem);
  297. error = ext3_xattr_ibody_get(inode, name_index, name, buffer,
  298. buffer_size);
  299. if (error == -ENODATA)
  300. error = ext3_xattr_block_get(inode, name_index, name, buffer,
  301. buffer_size);
  302. up_read(&EXT3_I(inode)->xattr_sem);
  303. return error;
  304. }
  305. static int
  306. ext3_xattr_list_entries(struct dentry *dentry, struct ext3_xattr_entry *entry,
  307. char *buffer, size_t buffer_size)
  308. {
  309. size_t rest = buffer_size;
  310. for (; !IS_LAST_ENTRY(entry); entry = EXT3_XATTR_NEXT(entry)) {
  311. const struct xattr_handler *handler =
  312. ext3_xattr_handler(entry->e_name_index);
  313. if (handler) {
  314. size_t size = handler->list(dentry, buffer, rest,
  315. entry->e_name,
  316. entry->e_name_len,
  317. handler->flags);
  318. if (buffer) {
  319. if (size > rest)
  320. return -ERANGE;
  321. buffer += size;
  322. }
  323. rest -= size;
  324. }
  325. }
  326. return buffer_size - rest;
  327. }
  328. static int
  329. ext3_xattr_block_list(struct dentry *dentry, char *buffer, size_t buffer_size)
  330. {
  331. struct inode *inode = dentry->d_inode;
  332. struct buffer_head *bh = NULL;
  333. int error;
  334. ea_idebug(inode, "buffer=%p, buffer_size=%ld",
  335. buffer, (long)buffer_size);
  336. error = 0;
  337. if (!EXT3_I(inode)->i_file_acl)
  338. goto cleanup;
  339. ea_idebug(inode, "reading block %u", EXT3_I(inode)->i_file_acl);
  340. bh = sb_bread(inode->i_sb, EXT3_I(inode)->i_file_acl);
  341. error = -EIO;
  342. if (!bh)
  343. goto cleanup;
  344. ea_bdebug(bh, "b_count=%d, refcount=%d",
  345. atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
  346. if (ext3_xattr_check_block(bh)) {
  347. ext3_error(inode->i_sb, __func__,
  348. "inode %lu: bad block "E3FSBLK, inode->i_ino,
  349. EXT3_I(inode)->i_file_acl);
  350. error = -EIO;
  351. goto cleanup;
  352. }
  353. ext3_xattr_cache_insert(bh);
  354. error = ext3_xattr_list_entries(dentry, BFIRST(bh), buffer, buffer_size);
  355. cleanup:
  356. brelse(bh);
  357. return error;
  358. }
  359. static int
  360. ext3_xattr_ibody_list(struct dentry *dentry, char *buffer, size_t buffer_size)
  361. {
  362. struct inode *inode = dentry->d_inode;
  363. struct ext3_xattr_ibody_header *header;
  364. struct ext3_inode *raw_inode;
  365. struct ext3_iloc iloc;
  366. void *end;
  367. int error;
  368. if (!ext3_test_inode_state(inode, EXT3_STATE_XATTR))
  369. return 0;
  370. error = ext3_get_inode_loc(inode, &iloc);
  371. if (error)
  372. return error;
  373. raw_inode = ext3_raw_inode(&iloc);
  374. header = IHDR(inode, raw_inode);
  375. end = (void *)raw_inode + EXT3_SB(inode->i_sb)->s_inode_size;
  376. error = ext3_xattr_check_names(IFIRST(header), end);
  377. if (error)
  378. goto cleanup;
  379. error = ext3_xattr_list_entries(dentry, IFIRST(header),
  380. buffer, buffer_size);
  381. cleanup:
  382. brelse(iloc.bh);
  383. return error;
  384. }
  385. /*
  386. * ext3_xattr_list()
  387. *
  388. * Copy a list of attribute names into the buffer
  389. * provided, or compute the buffer size required.
  390. * Buffer is NULL to compute the size of the buffer required.
  391. *
  392. * Returns a negative error number on failure, or the number of bytes
  393. * used / required on success.
  394. */
  395. static int
  396. ext3_xattr_list(struct dentry *dentry, char *buffer, size_t buffer_size)
  397. {
  398. int i_error, b_error;
  399. down_read(&EXT3_I(dentry->d_inode)->xattr_sem);
  400. i_error = ext3_xattr_ibody_list(dentry, buffer, buffer_size);
  401. if (i_error < 0) {
  402. b_error = 0;
  403. } else {
  404. if (buffer) {
  405. buffer += i_error;
  406. buffer_size -= i_error;
  407. }
  408. b_error = ext3_xattr_block_list(dentry, buffer, buffer_size);
  409. if (b_error < 0)
  410. i_error = 0;
  411. }
  412. up_read(&EXT3_I(dentry->d_inode)->xattr_sem);
  413. return i_error + b_error;
  414. }
  415. /*
  416. * If the EXT3_FEATURE_COMPAT_EXT_ATTR feature of this file system is
  417. * not set, set it.
  418. */
  419. static void ext3_xattr_update_super_block(handle_t *handle,
  420. struct super_block *sb)
  421. {
  422. if (EXT3_HAS_COMPAT_FEATURE(sb, EXT3_FEATURE_COMPAT_EXT_ATTR))
  423. return;
  424. if (ext3_journal_get_write_access(handle, EXT3_SB(sb)->s_sbh) == 0) {
  425. EXT3_SET_COMPAT_FEATURE(sb, EXT3_FEATURE_COMPAT_EXT_ATTR);
  426. ext3_journal_dirty_metadata(handle, EXT3_SB(sb)->s_sbh);
  427. }
  428. }
  429. /*
  430. * Release the xattr block BH: If the reference count is > 1, decrement
  431. * it; otherwise free the block.
  432. */
  433. static void
  434. ext3_xattr_release_block(handle_t *handle, struct inode *inode,
  435. struct buffer_head *bh)
  436. {
  437. struct mb_cache_entry *ce = NULL;
  438. int error = 0;
  439. ce = mb_cache_entry_get(ext3_xattr_cache, bh->b_bdev, bh->b_blocknr);
  440. error = ext3_journal_get_write_access(handle, bh);
  441. if (error)
  442. goto out;
  443. lock_buffer(bh);
  444. if (BHDR(bh)->h_refcount == cpu_to_le32(1)) {
  445. ea_bdebug(bh, "refcount now=0; freeing");
  446. if (ce)
  447. mb_cache_entry_free(ce);
  448. ext3_free_blocks(handle, inode, bh->b_blocknr, 1);
  449. get_bh(bh);
  450. ext3_forget(handle, 1, inode, bh, bh->b_blocknr);
  451. } else {
  452. le32_add_cpu(&BHDR(bh)->h_refcount, -1);
  453. error = ext3_journal_dirty_metadata(handle, bh);
  454. if (IS_SYNC(inode))
  455. handle->h_sync = 1;
  456. dquot_free_block(inode, 1);
  457. ea_bdebug(bh, "refcount now=%d; releasing",
  458. le32_to_cpu(BHDR(bh)->h_refcount));
  459. if (ce)
  460. mb_cache_entry_release(ce);
  461. }
  462. unlock_buffer(bh);
  463. out:
  464. ext3_std_error(inode->i_sb, error);
  465. return;
  466. }
  467. struct ext3_xattr_info {
  468. int name_index;
  469. const char *name;
  470. const void *value;
  471. size_t value_len;
  472. };
  473. struct ext3_xattr_search {
  474. struct ext3_xattr_entry *first;
  475. void *base;
  476. void *end;
  477. struct ext3_xattr_entry *here;
  478. int not_found;
  479. };
  480. static int
  481. ext3_xattr_set_entry(struct ext3_xattr_info *i, struct ext3_xattr_search *s)
  482. {
  483. struct ext3_xattr_entry *last;
  484. size_t free, min_offs = s->end - s->base, name_len = strlen(i->name);
  485. /* Compute min_offs and last. */
  486. last = s->first;
  487. for (; !IS_LAST_ENTRY(last); last = EXT3_XATTR_NEXT(last)) {
  488. if (!last->e_value_block && last->e_value_size) {
  489. size_t offs = le16_to_cpu(last->e_value_offs);
  490. if (offs < min_offs)
  491. min_offs = offs;
  492. }
  493. }
  494. free = min_offs - ((void *)last - s->base) - sizeof(__u32);
  495. if (!s->not_found) {
  496. if (!s->here->e_value_block && s->here->e_value_size) {
  497. size_t size = le32_to_cpu(s->here->e_value_size);
  498. free += EXT3_XATTR_SIZE(size);
  499. }
  500. free += EXT3_XATTR_LEN(name_len);
  501. }
  502. if (i->value) {
  503. if (free < EXT3_XATTR_SIZE(i->value_len) ||
  504. free < EXT3_XATTR_LEN(name_len) +
  505. EXT3_XATTR_SIZE(i->value_len))
  506. return -ENOSPC;
  507. }
  508. if (i->value && s->not_found) {
  509. /* Insert the new name. */
  510. size_t size = EXT3_XATTR_LEN(name_len);
  511. size_t rest = (void *)last - (void *)s->here + sizeof(__u32);
  512. memmove((void *)s->here + size, s->here, rest);
  513. memset(s->here, 0, size);
  514. s->here->e_name_index = i->name_index;
  515. s->here->e_name_len = name_len;
  516. memcpy(s->here->e_name, i->name, name_len);
  517. } else {
  518. if (!s->here->e_value_block && s->here->e_value_size) {
  519. void *first_val = s->base + min_offs;
  520. size_t offs = le16_to_cpu(s->here->e_value_offs);
  521. void *val = s->base + offs;
  522. size_t size = EXT3_XATTR_SIZE(
  523. le32_to_cpu(s->here->e_value_size));
  524. if (i->value && size == EXT3_XATTR_SIZE(i->value_len)) {
  525. /* The old and the new value have the same
  526. size. Just replace. */
  527. s->here->e_value_size =
  528. cpu_to_le32(i->value_len);
  529. memset(val + size - EXT3_XATTR_PAD, 0,
  530. EXT3_XATTR_PAD); /* Clear pad bytes. */
  531. memcpy(val, i->value, i->value_len);
  532. return 0;
  533. }
  534. /* Remove the old value. */
  535. memmove(first_val + size, first_val, val - first_val);
  536. memset(first_val, 0, size);
  537. s->here->e_value_size = 0;
  538. s->here->e_value_offs = 0;
  539. min_offs += size;
  540. /* Adjust all value offsets. */
  541. last = s->first;
  542. while (!IS_LAST_ENTRY(last)) {
  543. size_t o = le16_to_cpu(last->e_value_offs);
  544. if (!last->e_value_block &&
  545. last->e_value_size && o < offs)
  546. last->e_value_offs =
  547. cpu_to_le16(o + size);
  548. last = EXT3_XATTR_NEXT(last);
  549. }
  550. }
  551. if (!i->value) {
  552. /* Remove the old name. */
  553. size_t size = EXT3_XATTR_LEN(name_len);
  554. last = ENTRY((void *)last - size);
  555. memmove(s->here, (void *)s->here + size,
  556. (void *)last - (void *)s->here + sizeof(__u32));
  557. memset(last, 0, size);
  558. }
  559. }
  560. if (i->value) {
  561. /* Insert the new value. */
  562. s->here->e_value_size = cpu_to_le32(i->value_len);
  563. if (i->value_len) {
  564. size_t size = EXT3_XATTR_SIZE(i->value_len);
  565. void *val = s->base + min_offs - size;
  566. s->here->e_value_offs = cpu_to_le16(min_offs - size);
  567. memset(val + size - EXT3_XATTR_PAD, 0,
  568. EXT3_XATTR_PAD); /* Clear the pad bytes. */
  569. memcpy(val, i->value, i->value_len);
  570. }
  571. }
  572. return 0;
  573. }
  574. struct ext3_xattr_block_find {
  575. struct ext3_xattr_search s;
  576. struct buffer_head *bh;
  577. };
  578. static int
  579. ext3_xattr_block_find(struct inode *inode, struct ext3_xattr_info *i,
  580. struct ext3_xattr_block_find *bs)
  581. {
  582. struct super_block *sb = inode->i_sb;
  583. int error;
  584. ea_idebug(inode, "name=%d.%s, value=%p, value_len=%ld",
  585. i->name_index, i->name, i->value, (long)i->value_len);
  586. if (EXT3_I(inode)->i_file_acl) {
  587. /* The inode already has an extended attribute block. */
  588. bs->bh = sb_bread(sb, EXT3_I(inode)->i_file_acl);
  589. error = -EIO;
  590. if (!bs->bh)
  591. goto cleanup;
  592. ea_bdebug(bs->bh, "b_count=%d, refcount=%d",
  593. atomic_read(&(bs->bh->b_count)),
  594. le32_to_cpu(BHDR(bs->bh)->h_refcount));
  595. if (ext3_xattr_check_block(bs->bh)) {
  596. ext3_error(sb, __func__,
  597. "inode %lu: bad block "E3FSBLK, inode->i_ino,
  598. EXT3_I(inode)->i_file_acl);
  599. error = -EIO;
  600. goto cleanup;
  601. }
  602. /* Find the named attribute. */
  603. bs->s.base = BHDR(bs->bh);
  604. bs->s.first = BFIRST(bs->bh);
  605. bs->s.end = bs->bh->b_data + bs->bh->b_size;
  606. bs->s.here = bs->s.first;
  607. error = ext3_xattr_find_entry(&bs->s.here, i->name_index,
  608. i->name, bs->bh->b_size, 1);
  609. if (error && error != -ENODATA)
  610. goto cleanup;
  611. bs->s.not_found = error;
  612. }
  613. error = 0;
  614. cleanup:
  615. return error;
  616. }
  617. static int
  618. ext3_xattr_block_set(handle_t *handle, struct inode *inode,
  619. struct ext3_xattr_info *i,
  620. struct ext3_xattr_block_find *bs)
  621. {
  622. struct super_block *sb = inode->i_sb;
  623. struct buffer_head *new_bh = NULL;
  624. struct ext3_xattr_search *s = &bs->s;
  625. struct mb_cache_entry *ce = NULL;
  626. int error = 0;
  627. #define header(x) ((struct ext3_xattr_header *)(x))
  628. if (i->value && i->value_len > sb->s_blocksize)
  629. return -ENOSPC;
  630. if (s->base) {
  631. ce = mb_cache_entry_get(ext3_xattr_cache, bs->bh->b_bdev,
  632. bs->bh->b_blocknr);
  633. error = ext3_journal_get_write_access(handle, bs->bh);
  634. if (error)
  635. goto cleanup;
  636. lock_buffer(bs->bh);
  637. if (header(s->base)->h_refcount == cpu_to_le32(1)) {
  638. if (ce) {
  639. mb_cache_entry_free(ce);
  640. ce = NULL;
  641. }
  642. ea_bdebug(bs->bh, "modifying in-place");
  643. error = ext3_xattr_set_entry(i, s);
  644. if (!error) {
  645. if (!IS_LAST_ENTRY(s->first))
  646. ext3_xattr_rehash(header(s->base),
  647. s->here);
  648. ext3_xattr_cache_insert(bs->bh);
  649. }
  650. unlock_buffer(bs->bh);
  651. if (error == -EIO)
  652. goto bad_block;
  653. if (!error)
  654. error = ext3_journal_dirty_metadata(handle,
  655. bs->bh);
  656. if (error)
  657. goto cleanup;
  658. goto inserted;
  659. } else {
  660. int offset = (char *)s->here - bs->bh->b_data;
  661. unlock_buffer(bs->bh);
  662. journal_release_buffer(handle, bs->bh);
  663. if (ce) {
  664. mb_cache_entry_release(ce);
  665. ce = NULL;
  666. }
  667. ea_bdebug(bs->bh, "cloning");
  668. s->base = kmalloc(bs->bh->b_size, GFP_NOFS);
  669. error = -ENOMEM;
  670. if (s->base == NULL)
  671. goto cleanup;
  672. memcpy(s->base, BHDR(bs->bh), bs->bh->b_size);
  673. s->first = ENTRY(header(s->base)+1);
  674. header(s->base)->h_refcount = cpu_to_le32(1);
  675. s->here = ENTRY(s->base + offset);
  676. s->end = s->base + bs->bh->b_size;
  677. }
  678. } else {
  679. /* Allocate a buffer where we construct the new block. */
  680. s->base = kzalloc(sb->s_blocksize, GFP_NOFS);
  681. /* assert(header == s->base) */
  682. error = -ENOMEM;
  683. if (s->base == NULL)
  684. goto cleanup;
  685. header(s->base)->h_magic = cpu_to_le32(EXT3_XATTR_MAGIC);
  686. header(s->base)->h_blocks = cpu_to_le32(1);
  687. header(s->base)->h_refcount = cpu_to_le32(1);
  688. s->first = ENTRY(header(s->base)+1);
  689. s->here = ENTRY(header(s->base)+1);
  690. s->end = s->base + sb->s_blocksize;
  691. }
  692. error = ext3_xattr_set_entry(i, s);
  693. if (error == -EIO)
  694. goto bad_block;
  695. if (error)
  696. goto cleanup;
  697. if (!IS_LAST_ENTRY(s->first))
  698. ext3_xattr_rehash(header(s->base), s->here);
  699. inserted:
  700. if (!IS_LAST_ENTRY(s->first)) {
  701. new_bh = ext3_xattr_cache_find(inode, header(s->base), &ce);
  702. if (new_bh) {
  703. /* We found an identical block in the cache. */
  704. if (new_bh == bs->bh)
  705. ea_bdebug(new_bh, "keeping");
  706. else {
  707. /* The old block is released after updating
  708. the inode. */
  709. error = dquot_alloc_block(inode, 1);
  710. if (error)
  711. goto cleanup;
  712. error = ext3_journal_get_write_access(handle,
  713. new_bh);
  714. if (error)
  715. goto cleanup_dquot;
  716. lock_buffer(new_bh);
  717. le32_add_cpu(&BHDR(new_bh)->h_refcount, 1);
  718. ea_bdebug(new_bh, "reusing; refcount now=%d",
  719. le32_to_cpu(BHDR(new_bh)->h_refcount));
  720. unlock_buffer(new_bh);
  721. error = ext3_journal_dirty_metadata(handle,
  722. new_bh);
  723. if (error)
  724. goto cleanup_dquot;
  725. }
  726. mb_cache_entry_release(ce);
  727. ce = NULL;
  728. } else if (bs->bh && s->base == bs->bh->b_data) {
  729. /* We were modifying this block in-place. */
  730. ea_bdebug(bs->bh, "keeping this block");
  731. new_bh = bs->bh;
  732. get_bh(new_bh);
  733. } else {
  734. /* We need to allocate a new block */
  735. ext3_fsblk_t goal = ext3_group_first_block_no(sb,
  736. EXT3_I(inode)->i_block_group);
  737. ext3_fsblk_t block;
  738. /*
  739. * Protect us agaist concurrent allocations to the
  740. * same inode from ext3_..._writepage(). Reservation
  741. * code does not expect racing allocations.
  742. */
  743. mutex_lock(&EXT3_I(inode)->truncate_mutex);
  744. block = ext3_new_block(handle, inode, goal, &error);
  745. mutex_unlock(&EXT3_I(inode)->truncate_mutex);
  746. if (error)
  747. goto cleanup;
  748. ea_idebug(inode, "creating block %d", block);
  749. new_bh = sb_getblk(sb, block);
  750. if (!new_bh) {
  751. getblk_failed:
  752. ext3_free_blocks(handle, inode, block, 1);
  753. error = -EIO;
  754. goto cleanup;
  755. }
  756. lock_buffer(new_bh);
  757. error = ext3_journal_get_create_access(handle, new_bh);
  758. if (error) {
  759. unlock_buffer(new_bh);
  760. goto getblk_failed;
  761. }
  762. memcpy(new_bh->b_data, s->base, new_bh->b_size);
  763. set_buffer_uptodate(new_bh);
  764. unlock_buffer(new_bh);
  765. ext3_xattr_cache_insert(new_bh);
  766. error = ext3_journal_dirty_metadata(handle, new_bh);
  767. if (error)
  768. goto cleanup;
  769. }
  770. }
  771. /* Update the inode. */
  772. EXT3_I(inode)->i_file_acl = new_bh ? new_bh->b_blocknr : 0;
  773. /* Drop the previous xattr block. */
  774. if (bs->bh && bs->bh != new_bh)
  775. ext3_xattr_release_block(handle, inode, bs->bh);
  776. error = 0;
  777. cleanup:
  778. if (ce)
  779. mb_cache_entry_release(ce);
  780. brelse(new_bh);
  781. if (!(bs->bh && s->base == bs->bh->b_data))
  782. kfree(s->base);
  783. return error;
  784. cleanup_dquot:
  785. dquot_free_block(inode, 1);
  786. goto cleanup;
  787. bad_block:
  788. ext3_error(inode->i_sb, __func__,
  789. "inode %lu: bad block "E3FSBLK, inode->i_ino,
  790. EXT3_I(inode)->i_file_acl);
  791. goto cleanup;
  792. #undef header
  793. }
  794. struct ext3_xattr_ibody_find {
  795. struct ext3_xattr_search s;
  796. struct ext3_iloc iloc;
  797. };
  798. static int
  799. ext3_xattr_ibody_find(struct inode *inode, struct ext3_xattr_info *i,
  800. struct ext3_xattr_ibody_find *is)
  801. {
  802. struct ext3_xattr_ibody_header *header;
  803. struct ext3_inode *raw_inode;
  804. int error;
  805. if (EXT3_I(inode)->i_extra_isize == 0)
  806. return 0;
  807. raw_inode = ext3_raw_inode(&is->iloc);
  808. header = IHDR(inode, raw_inode);
  809. is->s.base = is->s.first = IFIRST(header);
  810. is->s.here = is->s.first;
  811. is->s.end = (void *)raw_inode + EXT3_SB(inode->i_sb)->s_inode_size;
  812. if (ext3_test_inode_state(inode, EXT3_STATE_XATTR)) {
  813. error = ext3_xattr_check_names(IFIRST(header), is->s.end);
  814. if (error)
  815. return error;
  816. /* Find the named attribute. */
  817. error = ext3_xattr_find_entry(&is->s.here, i->name_index,
  818. i->name, is->s.end -
  819. (void *)is->s.base, 0);
  820. if (error && error != -ENODATA)
  821. return error;
  822. is->s.not_found = error;
  823. }
  824. return 0;
  825. }
  826. static int
  827. ext3_xattr_ibody_set(handle_t *handle, struct inode *inode,
  828. struct ext3_xattr_info *i,
  829. struct ext3_xattr_ibody_find *is)
  830. {
  831. struct ext3_xattr_ibody_header *header;
  832. struct ext3_xattr_search *s = &is->s;
  833. int error;
  834. if (EXT3_I(inode)->i_extra_isize == 0)
  835. return -ENOSPC;
  836. error = ext3_xattr_set_entry(i, s);
  837. if (error)
  838. return error;
  839. header = IHDR(inode, ext3_raw_inode(&is->iloc));
  840. if (!IS_LAST_ENTRY(s->first)) {
  841. header->h_magic = cpu_to_le32(EXT3_XATTR_MAGIC);
  842. ext3_set_inode_state(inode, EXT3_STATE_XATTR);
  843. } else {
  844. header->h_magic = cpu_to_le32(0);
  845. ext3_clear_inode_state(inode, EXT3_STATE_XATTR);
  846. }
  847. return 0;
  848. }
  849. /*
  850. * ext3_xattr_set_handle()
  851. *
  852. * Create, replace or remove an extended attribute for this inode. Value
  853. * is NULL to remove an existing extended attribute, and non-NULL to
  854. * either replace an existing extended attribute, or create a new extended
  855. * attribute. The flags XATTR_REPLACE and XATTR_CREATE
  856. * specify that an extended attribute must exist and must not exist
  857. * previous to the call, respectively.
  858. *
  859. * Returns 0, or a negative error number on failure.
  860. */
  861. int
  862. ext3_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index,
  863. const char *name, const void *value, size_t value_len,
  864. int flags)
  865. {
  866. struct ext3_xattr_info i = {
  867. .name_index = name_index,
  868. .name = name,
  869. .value = value,
  870. .value_len = value_len,
  871. };
  872. struct ext3_xattr_ibody_find is = {
  873. .s = { .not_found = -ENODATA, },
  874. };
  875. struct ext3_xattr_block_find bs = {
  876. .s = { .not_found = -ENODATA, },
  877. };
  878. int error;
  879. if (!name)
  880. return -EINVAL;
  881. if (strlen(name) > 255)
  882. return -ERANGE;
  883. down_write(&EXT3_I(inode)->xattr_sem);
  884. error = ext3_get_inode_loc(inode, &is.iloc);
  885. if (error)
  886. goto cleanup;
  887. error = ext3_journal_get_write_access(handle, is.iloc.bh);
  888. if (error)
  889. goto cleanup;
  890. if (ext3_test_inode_state(inode, EXT3_STATE_NEW)) {
  891. struct ext3_inode *raw_inode = ext3_raw_inode(&is.iloc);
  892. memset(raw_inode, 0, EXT3_SB(inode->i_sb)->s_inode_size);
  893. ext3_clear_inode_state(inode, EXT3_STATE_NEW);
  894. }
  895. error = ext3_xattr_ibody_find(inode, &i, &is);
  896. if (error)
  897. goto cleanup;
  898. if (is.s.not_found)
  899. error = ext3_xattr_block_find(inode, &i, &bs);
  900. if (error)
  901. goto cleanup;
  902. if (is.s.not_found && bs.s.not_found) {
  903. error = -ENODATA;
  904. if (flags & XATTR_REPLACE)
  905. goto cleanup;
  906. error = 0;
  907. if (!value)
  908. goto cleanup;
  909. } else {
  910. error = -EEXIST;
  911. if (flags & XATTR_CREATE)
  912. goto cleanup;
  913. }
  914. if (!value) {
  915. if (!is.s.not_found)
  916. error = ext3_xattr_ibody_set(handle, inode, &i, &is);
  917. else if (!bs.s.not_found)
  918. error = ext3_xattr_block_set(handle, inode, &i, &bs);
  919. } else {
  920. error = ext3_xattr_ibody_set(handle, inode, &i, &is);
  921. if (!error && !bs.s.not_found) {
  922. i.value = NULL;
  923. error = ext3_xattr_block_set(handle, inode, &i, &bs);
  924. } else if (error == -ENOSPC) {
  925. if (EXT3_I(inode)->i_file_acl && !bs.s.base) {
  926. error = ext3_xattr_block_find(inode, &i, &bs);
  927. if (error)
  928. goto cleanup;
  929. }
  930. error = ext3_xattr_block_set(handle, inode, &i, &bs);
  931. if (error)
  932. goto cleanup;
  933. if (!is.s.not_found) {
  934. i.value = NULL;
  935. error = ext3_xattr_ibody_set(handle, inode, &i,
  936. &is);
  937. }
  938. }
  939. }
  940. if (!error) {
  941. ext3_xattr_update_super_block(handle, inode->i_sb);
  942. inode->i_ctime = CURRENT_TIME_SEC;
  943. error = ext3_mark_iloc_dirty(handle, inode, &is.iloc);
  944. /*
  945. * The bh is consumed by ext3_mark_iloc_dirty, even with
  946. * error != 0.
  947. */
  948. is.iloc.bh = NULL;
  949. if (IS_SYNC(inode))
  950. handle->h_sync = 1;
  951. }
  952. cleanup:
  953. brelse(is.iloc.bh);
  954. brelse(bs.bh);
  955. up_write(&EXT3_I(inode)->xattr_sem);
  956. return error;
  957. }
  958. /*
  959. * ext3_xattr_set()
  960. *
  961. * Like ext3_xattr_set_handle, but start from an inode. This extended
  962. * attribute modification is a filesystem transaction by itself.
  963. *
  964. * Returns 0, or a negative error number on failure.
  965. */
  966. int
  967. ext3_xattr_set(struct inode *inode, int name_index, const char *name,
  968. const void *value, size_t value_len, int flags)
  969. {
  970. handle_t *handle;
  971. int error, retries = 0;
  972. retry:
  973. handle = ext3_journal_start(inode, EXT3_DATA_TRANS_BLOCKS(inode->i_sb));
  974. if (IS_ERR(handle)) {
  975. error = PTR_ERR(handle);
  976. } else {
  977. int error2;
  978. error = ext3_xattr_set_handle(handle, inode, name_index, name,
  979. value, value_len, flags);
  980. error2 = ext3_journal_stop(handle);
  981. if (error == -ENOSPC &&
  982. ext3_should_retry_alloc(inode->i_sb, &retries))
  983. goto retry;
  984. if (error == 0)
  985. error = error2;
  986. }
  987. return error;
  988. }
  989. /*
  990. * ext3_xattr_delete_inode()
  991. *
  992. * Free extended attribute resources associated with this inode. This
  993. * is called immediately before an inode is freed. We have exclusive
  994. * access to the inode.
  995. */
  996. void
  997. ext3_xattr_delete_inode(handle_t *handle, struct inode *inode)
  998. {
  999. struct buffer_head *bh = NULL;
  1000. if (!EXT3_I(inode)->i_file_acl)
  1001. goto cleanup;
  1002. bh = sb_bread(inode->i_sb, EXT3_I(inode)->i_file_acl);
  1003. if (!bh) {
  1004. ext3_error(inode->i_sb, __func__,
  1005. "inode %lu: block "E3FSBLK" read error", inode->i_ino,
  1006. EXT3_I(inode)->i_file_acl);
  1007. goto cleanup;
  1008. }
  1009. if (BHDR(bh)->h_magic != cpu_to_le32(EXT3_XATTR_MAGIC) ||
  1010. BHDR(bh)->h_blocks != cpu_to_le32(1)) {
  1011. ext3_error(inode->i_sb, __func__,
  1012. "inode %lu: bad block "E3FSBLK, inode->i_ino,
  1013. EXT3_I(inode)->i_file_acl);
  1014. goto cleanup;
  1015. }
  1016. ext3_xattr_release_block(handle, inode, bh);
  1017. EXT3_I(inode)->i_file_acl = 0;
  1018. cleanup:
  1019. brelse(bh);
  1020. }
  1021. /*
  1022. * ext3_xattr_put_super()
  1023. *
  1024. * This is called when a file system is unmounted.
  1025. */
  1026. void
  1027. ext3_xattr_put_super(struct super_block *sb)
  1028. {
  1029. mb_cache_shrink(sb->s_bdev);
  1030. }
  1031. /*
  1032. * ext3_xattr_cache_insert()
  1033. *
  1034. * Create a new entry in the extended attribute cache, and insert
  1035. * it unless such an entry is already in the cache.
  1036. *
  1037. * Returns 0, or a negative error number on failure.
  1038. */
  1039. static void
  1040. ext3_xattr_cache_insert(struct buffer_head *bh)
  1041. {
  1042. __u32 hash = le32_to_cpu(BHDR(bh)->h_hash);
  1043. struct mb_cache_entry *ce;
  1044. int error;
  1045. ce = mb_cache_entry_alloc(ext3_xattr_cache, GFP_NOFS);
  1046. if (!ce) {
  1047. ea_bdebug(bh, "out of memory");
  1048. return;
  1049. }
  1050. error = mb_cache_entry_insert(ce, bh->b_bdev, bh->b_blocknr, hash);
  1051. if (error) {
  1052. mb_cache_entry_free(ce);
  1053. if (error == -EBUSY) {
  1054. ea_bdebug(bh, "already in cache");
  1055. error = 0;
  1056. }
  1057. } else {
  1058. ea_bdebug(bh, "inserting [%x]", (int)hash);
  1059. mb_cache_entry_release(ce);
  1060. }
  1061. }
  1062. /*
  1063. * ext3_xattr_cmp()
  1064. *
  1065. * Compare two extended attribute blocks for equality.
  1066. *
  1067. * Returns 0 if the blocks are equal, 1 if they differ, and
  1068. * a negative error number on errors.
  1069. */
  1070. static int
  1071. ext3_xattr_cmp(struct ext3_xattr_header *header1,
  1072. struct ext3_xattr_header *header2)
  1073. {
  1074. struct ext3_xattr_entry *entry1, *entry2;
  1075. entry1 = ENTRY(header1+1);
  1076. entry2 = ENTRY(header2+1);
  1077. while (!IS_LAST_ENTRY(entry1)) {
  1078. if (IS_LAST_ENTRY(entry2))
  1079. return 1;
  1080. if (entry1->e_hash != entry2->e_hash ||
  1081. entry1->e_name_index != entry2->e_name_index ||
  1082. entry1->e_name_len != entry2->e_name_len ||
  1083. entry1->e_value_size != entry2->e_value_size ||
  1084. memcmp(entry1->e_name, entry2->e_name, entry1->e_name_len))
  1085. return 1;
  1086. if (entry1->e_value_block != 0 || entry2->e_value_block != 0)
  1087. return -EIO;
  1088. if (memcmp((char *)header1 + le16_to_cpu(entry1->e_value_offs),
  1089. (char *)header2 + le16_to_cpu(entry2->e_value_offs),
  1090. le32_to_cpu(entry1->e_value_size)))
  1091. return 1;
  1092. entry1 = EXT3_XATTR_NEXT(entry1);
  1093. entry2 = EXT3_XATTR_NEXT(entry2);
  1094. }
  1095. if (!IS_LAST_ENTRY(entry2))
  1096. return 1;
  1097. return 0;
  1098. }
  1099. /*
  1100. * ext3_xattr_cache_find()
  1101. *
  1102. * Find an identical extended attribute block.
  1103. *
  1104. * Returns a pointer to the block found, or NULL if such a block was
  1105. * not found or an error occurred.
  1106. */
  1107. static struct buffer_head *
  1108. ext3_xattr_cache_find(struct inode *inode, struct ext3_xattr_header *header,
  1109. struct mb_cache_entry **pce)
  1110. {
  1111. __u32 hash = le32_to_cpu(header->h_hash);
  1112. struct mb_cache_entry *ce;
  1113. if (!header->h_hash)
  1114. return NULL; /* never share */
  1115. ea_idebug(inode, "looking for cached blocks [%x]", (int)hash);
  1116. again:
  1117. ce = mb_cache_entry_find_first(ext3_xattr_cache, inode->i_sb->s_bdev,
  1118. hash);
  1119. while (ce) {
  1120. struct buffer_head *bh;
  1121. if (IS_ERR(ce)) {
  1122. if (PTR_ERR(ce) == -EAGAIN)
  1123. goto again;
  1124. break;
  1125. }
  1126. bh = sb_bread(inode->i_sb, ce->e_block);
  1127. if (!bh) {
  1128. ext3_error(inode->i_sb, __func__,
  1129. "inode %lu: block %lu read error",
  1130. inode->i_ino, (unsigned long) ce->e_block);
  1131. } else if (le32_to_cpu(BHDR(bh)->h_refcount) >=
  1132. EXT3_XATTR_REFCOUNT_MAX) {
  1133. ea_idebug(inode, "block %lu refcount %d>=%d",
  1134. (unsigned long) ce->e_block,
  1135. le32_to_cpu(BHDR(bh)->h_refcount),
  1136. EXT3_XATTR_REFCOUNT_MAX);
  1137. } else if (ext3_xattr_cmp(header, BHDR(bh)) == 0) {
  1138. *pce = ce;
  1139. return bh;
  1140. }
  1141. brelse(bh);
  1142. ce = mb_cache_entry_find_next(ce, inode->i_sb->s_bdev, hash);
  1143. }
  1144. return NULL;
  1145. }
  1146. #define NAME_HASH_SHIFT 5
  1147. #define VALUE_HASH_SHIFT 16
  1148. /*
  1149. * ext3_xattr_hash_entry()
  1150. *
  1151. * Compute the hash of an extended attribute.
  1152. */
  1153. static inline void ext3_xattr_hash_entry(struct ext3_xattr_header *header,
  1154. struct ext3_xattr_entry *entry)
  1155. {
  1156. __u32 hash = 0;
  1157. char *name = entry->e_name;
  1158. int n;
  1159. for (n=0; n < entry->e_name_len; n++) {
  1160. hash = (hash << NAME_HASH_SHIFT) ^
  1161. (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^
  1162. *name++;
  1163. }
  1164. if (entry->e_value_block == 0 && entry->e_value_size != 0) {
  1165. __le32 *value = (__le32 *)((char *)header +
  1166. le16_to_cpu(entry->e_value_offs));
  1167. for (n = (le32_to_cpu(entry->e_value_size) +
  1168. EXT3_XATTR_ROUND) >> EXT3_XATTR_PAD_BITS; n; n--) {
  1169. hash = (hash << VALUE_HASH_SHIFT) ^
  1170. (hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^
  1171. le32_to_cpu(*value++);
  1172. }
  1173. }
  1174. entry->e_hash = cpu_to_le32(hash);
  1175. }
  1176. #undef NAME_HASH_SHIFT
  1177. #undef VALUE_HASH_SHIFT
  1178. #define BLOCK_HASH_SHIFT 16
  1179. /*
  1180. * ext3_xattr_rehash()
  1181. *
  1182. * Re-compute the extended attribute hash value after an entry has changed.
  1183. */
  1184. static void ext3_xattr_rehash(struct ext3_xattr_header *header,
  1185. struct ext3_xattr_entry *entry)
  1186. {
  1187. struct ext3_xattr_entry *here;
  1188. __u32 hash = 0;
  1189. ext3_xattr_hash_entry(header, entry);
  1190. here = ENTRY(header+1);
  1191. while (!IS_LAST_ENTRY(here)) {
  1192. if (!here->e_hash) {
  1193. /* Block is not shared if an entry's hash value == 0 */
  1194. hash = 0;
  1195. break;
  1196. }
  1197. hash = (hash << BLOCK_HASH_SHIFT) ^
  1198. (hash >> (8*sizeof(hash) - BLOCK_HASH_SHIFT)) ^
  1199. le32_to_cpu(here->e_hash);
  1200. here = EXT3_XATTR_NEXT(here);
  1201. }
  1202. header->h_hash = cpu_to_le32(hash);
  1203. }
  1204. #undef BLOCK_HASH_SHIFT
  1205. int __init
  1206. init_ext3_xattr(void)
  1207. {
  1208. ext3_xattr_cache = mb_cache_create("ext3_xattr", 6);
  1209. if (!ext3_xattr_cache)
  1210. return -ENOMEM;
  1211. return 0;
  1212. }
  1213. void
  1214. exit_ext3_xattr(void)
  1215. {
  1216. if (ext3_xattr_cache)
  1217. mb_cache_destroy(ext3_xattr_cache);
  1218. ext3_xattr_cache = NULL;
  1219. }