PageRenderTime 52ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/fs/ext3/xattr.c

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