PageRenderTime 86ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 1ms

/linux-2.6.21.x/fs/ext4/xattr.c

https://bitbucket.org/altlc/wive-rtnl-ralink-rt305x-routers-firmware-amod
C | 1585 lines | 1250 code | 142 blank | 193 comment | 270 complexity | ff26491df6e996afe589815e1b637aeb MD5 | raw file
Possible License(s): CC-BY-SA-3.0, BSD-3-Clause, MPL-2.0-no-copyleft-exception, GPL-2.0, GPL-3.0, LGPL-3.0, 0BSD, AGPL-1.0, LGPL-2.1, LGPL-2.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/ext4_jbd2.h>
  55. #include <linux/ext4_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 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. #define IHDR(inode, raw_inode) \
  66. ((struct ext4_xattr_ibody_header *) \
  67. ((void *)raw_inode + \
  68. EXT4_GOOD_OLD_INODE_SIZE + \
  69. EXT4_I(inode)->i_extra_isize))
  70. #define IFIRST(hdr) ((struct ext4_xattr_entry *)((hdr)+1))
  71. #ifdef EXT4_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 ext4_xattr_cache_insert(struct buffer_head *);
  91. static struct buffer_head *ext4_xattr_cache_find(struct inode *,
  92. struct ext4_xattr_header *,
  93. struct mb_cache_entry **);
  94. static void ext4_xattr_rehash(struct ext4_xattr_header *,
  95. struct ext4_xattr_entry *);
  96. static struct mb_cache *ext4_xattr_cache;
  97. static struct xattr_handler *ext4_xattr_handler_map[] = {
  98. [EXT4_XATTR_INDEX_USER] = &ext4_xattr_user_handler,
  99. #ifdef CONFIG_EXT4DEV_FS_POSIX_ACL
  100. [EXT4_XATTR_INDEX_POSIX_ACL_ACCESS] = &ext4_xattr_acl_access_handler,
  101. [EXT4_XATTR_INDEX_POSIX_ACL_DEFAULT] = &ext4_xattr_acl_default_handler,
  102. #endif
  103. [EXT4_XATTR_INDEX_TRUSTED] = &ext4_xattr_trusted_handler,
  104. #ifdef CONFIG_EXT4DEV_FS_SECURITY
  105. [EXT4_XATTR_INDEX_SECURITY] = &ext4_xattr_security_handler,
  106. #endif
  107. };
  108. struct xattr_handler *ext4_xattr_handlers[] = {
  109. &ext4_xattr_user_handler,
  110. &ext4_xattr_trusted_handler,
  111. #ifdef CONFIG_EXT4DEV_FS_POSIX_ACL
  112. &ext4_xattr_acl_access_handler,
  113. &ext4_xattr_acl_default_handler,
  114. #endif
  115. #ifdef CONFIG_EXT4DEV_FS_SECURITY
  116. &ext4_xattr_security_handler,
  117. #endif
  118. NULL
  119. };
  120. static inline struct xattr_handler *
  121. ext4_xattr_handler(int name_index)
  122. {
  123. struct xattr_handler *handler = NULL;
  124. if (name_index > 0 && name_index < ARRAY_SIZE(ext4_xattr_handler_map))
  125. handler = ext4_xattr_handler_map[name_index];
  126. return handler;
  127. }
  128. /*
  129. * Inode operation listxattr()
  130. *
  131. * dentry->d_inode->i_mutex: don't care
  132. */
  133. ssize_t
  134. ext4_listxattr(struct dentry *dentry, char *buffer, size_t size)
  135. {
  136. return ext4_xattr_list(dentry->d_inode, buffer, size);
  137. }
  138. static int
  139. ext4_xattr_check_names(struct ext4_xattr_entry *entry, void *end)
  140. {
  141. while (!IS_LAST_ENTRY(entry)) {
  142. struct ext4_xattr_entry *next = EXT4_XATTR_NEXT(entry);
  143. if ((void *)next >= end)
  144. return -EIO;
  145. entry = next;
  146. }
  147. return 0;
  148. }
  149. static inline int
  150. ext4_xattr_check_block(struct buffer_head *bh)
  151. {
  152. int error;
  153. if (BHDR(bh)->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC) ||
  154. BHDR(bh)->h_blocks != cpu_to_le32(1))
  155. return -EIO;
  156. error = ext4_xattr_check_names(BFIRST(bh), bh->b_data + bh->b_size);
  157. return error;
  158. }
  159. static inline int
  160. ext4_xattr_check_entry(struct ext4_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. ext4_xattr_find_entry(struct ext4_xattr_entry **pentry, int name_index,
  170. const char *name, size_t size, int sorted)
  171. {
  172. struct ext4_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 = EXT4_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 && ext4_xattr_check_entry(entry, size))
  190. return -EIO;
  191. return cmp ? -ENODATA : 0;
  192. }
  193. static int
  194. ext4_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 ext4_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 (!EXT4_I(inode)->i_file_acl)
  205. goto cleanup;
  206. ea_idebug(inode, "reading block %u", EXT4_I(inode)->i_file_acl);
  207. bh = sb_bread(inode->i_sb, EXT4_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 (ext4_xattr_check_block(bh)) {
  213. bad_block: ext4_error(inode->i_sb, __FUNCTION__,
  214. "inode %lu: bad block %llu", inode->i_ino,
  215. EXT4_I(inode)->i_file_acl);
  216. error = -EIO;
  217. goto cleanup;
  218. }
  219. ext4_xattr_cache_insert(bh);
  220. entry = BFIRST(bh);
  221. error = ext4_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. ext4_xattr_ibody_get(struct inode *inode, int name_index, const char *name,
  241. void *buffer, size_t buffer_size)
  242. {
  243. struct ext4_xattr_ibody_header *header;
  244. struct ext4_xattr_entry *entry;
  245. struct ext4_inode *raw_inode;
  246. struct ext4_iloc iloc;
  247. size_t size;
  248. void *end;
  249. int error;
  250. if (!(EXT4_I(inode)->i_state & EXT4_STATE_XATTR))
  251. return -ENODATA;
  252. error = ext4_get_inode_loc(inode, &iloc);
  253. if (error)
  254. return error;
  255. raw_inode = ext4_raw_inode(&iloc);
  256. header = IHDR(inode, raw_inode);
  257. entry = IFIRST(header);
  258. end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
  259. error = ext4_xattr_check_names(entry, end);
  260. if (error)
  261. goto cleanup;
  262. error = ext4_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. * ext4_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. ext4_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(&EXT4_I(inode)->xattr_sem);
  295. error = ext4_xattr_ibody_get(inode, name_index, name, buffer,
  296. buffer_size);
  297. if (error == -ENODATA)
  298. error = ext4_xattr_block_get(inode, name_index, name, buffer,
  299. buffer_size);
  300. up_read(&EXT4_I(inode)->xattr_sem);
  301. return error;
  302. }
  303. static int
  304. ext4_xattr_list_entries(struct inode *inode, struct ext4_xattr_entry *entry,
  305. char *buffer, size_t buffer_size)
  306. {
  307. size_t rest = buffer_size;
  308. for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
  309. struct xattr_handler *handler =
  310. ext4_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. static int
  326. ext4_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 (!EXT4_I(inode)->i_file_acl)
  334. goto cleanup;
  335. ea_idebug(inode, "reading block %u", EXT4_I(inode)->i_file_acl);
  336. bh = sb_bread(inode->i_sb, EXT4_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 (ext4_xattr_check_block(bh)) {
  343. ext4_error(inode->i_sb, __FUNCTION__,
  344. "inode %lu: bad block %llu", inode->i_ino,
  345. EXT4_I(inode)->i_file_acl);
  346. error = -EIO;
  347. goto cleanup;
  348. }
  349. ext4_xattr_cache_insert(bh);
  350. error = ext4_xattr_list_entries(inode, BFIRST(bh), buffer, buffer_size);
  351. cleanup:
  352. brelse(bh);
  353. return error;
  354. }
  355. static int
  356. ext4_xattr_ibody_list(struct inode *inode, char *buffer, size_t buffer_size)
  357. {
  358. struct ext4_xattr_ibody_header *header;
  359. struct ext4_inode *raw_inode;
  360. struct ext4_iloc iloc;
  361. void *end;
  362. int error;
  363. if (!(EXT4_I(inode)->i_state & EXT4_STATE_XATTR))
  364. return 0;
  365. error = ext4_get_inode_loc(inode, &iloc);
  366. if (error)
  367. return error;
  368. raw_inode = ext4_raw_inode(&iloc);
  369. header = IHDR(inode, raw_inode);
  370. end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
  371. error = ext4_xattr_check_names(IFIRST(header), end);
  372. if (error)
  373. goto cleanup;
  374. error = ext4_xattr_list_entries(inode, IFIRST(header),
  375. buffer, buffer_size);
  376. cleanup:
  377. brelse(iloc.bh);
  378. return error;
  379. }
  380. /*
  381. * ext4_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. ext4_xattr_list(struct inode *inode, char *buffer, size_t buffer_size)
  392. {
  393. int i_error, b_error;
  394. down_read(&EXT4_I(inode)->xattr_sem);
  395. i_error = ext4_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 = ext4_xattr_block_list(inode, buffer, buffer_size);
  404. if (b_error < 0)
  405. i_error = 0;
  406. }
  407. up_read(&EXT4_I(inode)->xattr_sem);
  408. return i_error + b_error;
  409. }
  410. /*
  411. * If the EXT4_FEATURE_COMPAT_EXT_ATTR feature of this file system is
  412. * not set, set it.
  413. */
  414. static void ext4_xattr_update_super_block(handle_t *handle,
  415. struct super_block *sb)
  416. {
  417. if (EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_EXT_ATTR))
  418. return;
  419. if (ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh) == 0) {
  420. EXT4_SET_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_EXT_ATTR);
  421. sb->s_dirt = 1;
  422. ext4_journal_dirty_metadata(handle, EXT4_SB(sb)->s_sbh);
  423. }
  424. }
  425. /*
  426. * Release the xattr block BH: If the reference count is > 1, decrement
  427. * it; otherwise free the block.
  428. */
  429. static void
  430. ext4_xattr_release_block(handle_t *handle, struct inode *inode,
  431. struct buffer_head *bh)
  432. {
  433. struct mb_cache_entry *ce = NULL;
  434. int error = 0;
  435. ce = mb_cache_entry_get(ext4_xattr_cache, bh->b_bdev, bh->b_blocknr);
  436. error = ext4_journal_get_write_access(handle, bh);
  437. if (error)
  438. goto out;
  439. lock_buffer(bh);
  440. if (BHDR(bh)->h_refcount == cpu_to_le32(1)) {
  441. ea_bdebug(bh, "refcount now=0; freeing");
  442. if (ce)
  443. mb_cache_entry_free(ce);
  444. ext4_free_blocks(handle, inode, bh->b_blocknr, 1);
  445. get_bh(bh);
  446. ext4_forget(handle, 1, inode, bh, bh->b_blocknr);
  447. } else {
  448. BHDR(bh)->h_refcount = cpu_to_le32(
  449. le32_to_cpu(BHDR(bh)->h_refcount) - 1);
  450. error = ext4_journal_dirty_metadata(handle, bh);
  451. if (IS_SYNC(inode))
  452. handle->h_sync = 1;
  453. DQUOT_FREE_BLOCK(inode, 1);
  454. ea_bdebug(bh, "refcount now=%d; releasing",
  455. le32_to_cpu(BHDR(bh)->h_refcount));
  456. if (ce)
  457. mb_cache_entry_release(ce);
  458. }
  459. unlock_buffer(bh);
  460. out:
  461. ext4_std_error(inode->i_sb, error);
  462. return;
  463. }
  464. static inline size_t ext3_xattr_free_space(struct ext4_xattr_entry *last,
  465. size_t *min_offs, void *base, int *total)
  466. {
  467. for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
  468. *total += EXT4_XATTR_LEN(last->e_name_len);
  469. if (!last->e_value_block && last->e_value_size) {
  470. size_t offs = le16_to_cpu(last->e_value_offs);
  471. if (offs < *min_offs)
  472. *min_offs = offs;
  473. }
  474. }
  475. return (*min_offs - ((void *)last - base) - sizeof(__u32));
  476. }
  477. struct ext4_xattr_info {
  478. int name_index;
  479. const char *name;
  480. const void *value;
  481. size_t value_len;
  482. };
  483. struct ext4_xattr_search {
  484. struct ext4_xattr_entry *first;
  485. void *base;
  486. void *end;
  487. struct ext4_xattr_entry *here;
  488. int not_found;
  489. };
  490. static int
  491. ext4_xattr_set_entry(struct ext4_xattr_info *i, struct ext4_xattr_search *s)
  492. {
  493. struct ext4_xattr_entry *last;
  494. size_t free, min_offs = s->end - s->base, name_len = strlen(i->name);
  495. /* Compute min_offs and last. */
  496. last = s->first;
  497. for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
  498. if (!last->e_value_block && last->e_value_size) {
  499. size_t offs = le16_to_cpu(last->e_value_offs);
  500. if (offs < min_offs)
  501. min_offs = offs;
  502. }
  503. }
  504. free = min_offs - ((void *)last - s->base) - sizeof(__u32);
  505. if (!s->not_found) {
  506. if (!s->here->e_value_block && s->here->e_value_size) {
  507. size_t size = le32_to_cpu(s->here->e_value_size);
  508. free += EXT4_XATTR_SIZE(size);
  509. }
  510. free += EXT4_XATTR_LEN(name_len);
  511. }
  512. if (i->value) {
  513. if (free < EXT4_XATTR_SIZE(i->value_len) ||
  514. free < EXT4_XATTR_LEN(name_len) +
  515. EXT4_XATTR_SIZE(i->value_len))
  516. return -ENOSPC;
  517. }
  518. if (i->value && s->not_found) {
  519. /* Insert the new name. */
  520. size_t size = EXT4_XATTR_LEN(name_len);
  521. size_t rest = (void *)last - (void *)s->here + sizeof(__u32);
  522. memmove((void *)s->here + size, s->here, rest);
  523. memset(s->here, 0, size);
  524. s->here->e_name_index = i->name_index;
  525. s->here->e_name_len = name_len;
  526. memcpy(s->here->e_name, i->name, name_len);
  527. } else {
  528. if (!s->here->e_value_block && s->here->e_value_size) {
  529. void *first_val = s->base + min_offs;
  530. size_t offs = le16_to_cpu(s->here->e_value_offs);
  531. void *val = s->base + offs;
  532. size_t size = EXT4_XATTR_SIZE(
  533. le32_to_cpu(s->here->e_value_size));
  534. if (i->value && size == EXT4_XATTR_SIZE(i->value_len)) {
  535. /* The old and the new value have the same
  536. size. Just replace. */
  537. s->here->e_value_size =
  538. cpu_to_le32(i->value_len);
  539. memset(val + size - EXT4_XATTR_PAD, 0,
  540. EXT4_XATTR_PAD); /* Clear pad bytes. */
  541. memcpy(val, i->value, i->value_len);
  542. return 0;
  543. }
  544. /* Remove the old value. */
  545. memmove(first_val + size, first_val, val - first_val);
  546. memset(first_val, 0, size);
  547. s->here->e_value_size = 0;
  548. s->here->e_value_offs = 0;
  549. min_offs += size;
  550. /* Adjust all value offsets. */
  551. last = s->first;
  552. while (!IS_LAST_ENTRY(last)) {
  553. size_t o = le16_to_cpu(last->e_value_offs);
  554. if (!last->e_value_block &&
  555. last->e_value_size && o < offs)
  556. last->e_value_offs =
  557. cpu_to_le16(o + size);
  558. last = EXT4_XATTR_NEXT(last);
  559. }
  560. }
  561. if (!i->value) {
  562. /* Remove the old name. */
  563. size_t size = EXT4_XATTR_LEN(name_len);
  564. last = ENTRY((void *)last - size);
  565. memmove(s->here, (void *)s->here + size,
  566. (void *)last - (void *)s->here + sizeof(__u32));
  567. memset(last, 0, size);
  568. }
  569. }
  570. if (i->value) {
  571. /* Insert the new value. */
  572. s->here->e_value_size = cpu_to_le32(i->value_len);
  573. if (i->value_len) {
  574. size_t size = EXT4_XATTR_SIZE(i->value_len);
  575. void *val = s->base + min_offs - size;
  576. s->here->e_value_offs = cpu_to_le16(min_offs - size);
  577. memset(val + size - EXT4_XATTR_PAD, 0,
  578. EXT4_XATTR_PAD); /* Clear the pad bytes. */
  579. memcpy(val, i->value, i->value_len);
  580. }
  581. }
  582. return 0;
  583. }
  584. struct ext4_xattr_block_find {
  585. struct ext4_xattr_search s;
  586. struct buffer_head *bh;
  587. };
  588. static int
  589. ext4_xattr_block_find(struct inode *inode, struct ext4_xattr_info *i,
  590. struct ext4_xattr_block_find *bs)
  591. {
  592. struct super_block *sb = inode->i_sb;
  593. int error;
  594. ea_idebug(inode, "name=%d.%s, value=%p, value_len=%ld",
  595. i->name_index, i->name, i->value, (long)i->value_len);
  596. if (EXT4_I(inode)->i_file_acl) {
  597. /* The inode already has an extended attribute block. */
  598. bs->bh = sb_bread(sb, EXT4_I(inode)->i_file_acl);
  599. error = -EIO;
  600. if (!bs->bh)
  601. goto cleanup;
  602. ea_bdebug(bs->bh, "b_count=%d, refcount=%d",
  603. atomic_read(&(bs->bh->b_count)),
  604. le32_to_cpu(BHDR(bs->bh)->h_refcount));
  605. if (ext4_xattr_check_block(bs->bh)) {
  606. ext4_error(sb, __FUNCTION__,
  607. "inode %lu: bad block %llu", inode->i_ino,
  608. EXT4_I(inode)->i_file_acl);
  609. error = -EIO;
  610. goto cleanup;
  611. }
  612. /* Find the named attribute. */
  613. bs->s.base = BHDR(bs->bh);
  614. bs->s.first = BFIRST(bs->bh);
  615. bs->s.end = bs->bh->b_data + bs->bh->b_size;
  616. bs->s.here = bs->s.first;
  617. error = ext4_xattr_find_entry(&bs->s.here, i->name_index,
  618. i->name, bs->bh->b_size, 1);
  619. if (error && error != -ENODATA)
  620. goto cleanup;
  621. bs->s.not_found = error;
  622. }
  623. error = 0;
  624. cleanup:
  625. return error;
  626. }
  627. static int
  628. ext4_xattr_block_set(handle_t *handle, struct inode *inode,
  629. struct ext4_xattr_info *i,
  630. struct ext4_xattr_block_find *bs)
  631. {
  632. struct super_block *sb = inode->i_sb;
  633. struct buffer_head *new_bh = NULL;
  634. struct ext4_xattr_search *s = &bs->s;
  635. struct mb_cache_entry *ce = NULL;
  636. int error = 0;
  637. #define header(x) ((struct ext4_xattr_header *)(x))
  638. if (i->value && i->value_len > sb->s_blocksize)
  639. return -ENOSPC;
  640. if (s->base) {
  641. ce = mb_cache_entry_get(ext4_xattr_cache, bs->bh->b_bdev,
  642. bs->bh->b_blocknr);
  643. error = ext4_journal_get_write_access(handle, bs->bh);
  644. if (error)
  645. goto cleanup;
  646. lock_buffer(bs->bh);
  647. if (header(s->base)->h_refcount == cpu_to_le32(1)) {
  648. if (ce) {
  649. mb_cache_entry_free(ce);
  650. ce = NULL;
  651. }
  652. ea_bdebug(bs->bh, "modifying in-place");
  653. error = ext4_xattr_set_entry(i, s);
  654. if (!error) {
  655. if (!IS_LAST_ENTRY(s->first))
  656. ext4_xattr_rehash(header(s->base),
  657. s->here);
  658. ext4_xattr_cache_insert(bs->bh);
  659. }
  660. unlock_buffer(bs->bh);
  661. if (error == -EIO)
  662. goto bad_block;
  663. if (!error)
  664. error = ext4_journal_dirty_metadata(handle,
  665. bs->bh);
  666. if (error)
  667. goto cleanup;
  668. goto inserted;
  669. } else {
  670. int offset = (char *)s->here - bs->bh->b_data;
  671. unlock_buffer(bs->bh);
  672. jbd2_journal_release_buffer(handle, bs->bh);
  673. if (ce) {
  674. mb_cache_entry_release(ce);
  675. ce = NULL;
  676. }
  677. ea_bdebug(bs->bh, "cloning");
  678. s->base = kmalloc(bs->bh->b_size, GFP_KERNEL);
  679. error = -ENOMEM;
  680. if (s->base == NULL)
  681. goto cleanup;
  682. memcpy(s->base, BHDR(bs->bh), bs->bh->b_size);
  683. s->first = ENTRY(header(s->base)+1);
  684. header(s->base)->h_refcount = cpu_to_le32(1);
  685. s->here = ENTRY(s->base + offset);
  686. s->end = s->base + bs->bh->b_size;
  687. }
  688. } else {
  689. /* Allocate a buffer where we construct the new block. */
  690. s->base = kmalloc(sb->s_blocksize, GFP_KERNEL);
  691. /* assert(header == s->base) */
  692. error = -ENOMEM;
  693. if (s->base == NULL)
  694. goto cleanup;
  695. memset(s->base, 0, sb->s_blocksize);
  696. header(s->base)->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
  697. header(s->base)->h_blocks = cpu_to_le32(1);
  698. header(s->base)->h_refcount = cpu_to_le32(1);
  699. s->first = ENTRY(header(s->base)+1);
  700. s->here = ENTRY(header(s->base)+1);
  701. s->end = s->base + sb->s_blocksize;
  702. }
  703. error = ext4_xattr_set_entry(i, s);
  704. if (error == -EIO)
  705. goto bad_block;
  706. if (error)
  707. goto cleanup;
  708. if (!IS_LAST_ENTRY(s->first))
  709. ext4_xattr_rehash(header(s->base), s->here);
  710. inserted:
  711. if (!IS_LAST_ENTRY(s->first)) {
  712. new_bh = ext4_xattr_cache_find(inode, header(s->base), &ce);
  713. if (new_bh) {
  714. /* We found an identical block in the cache. */
  715. if (new_bh == bs->bh)
  716. ea_bdebug(new_bh, "keeping");
  717. else {
  718. /* The old block is released after updating
  719. the inode. */
  720. error = -EDQUOT;
  721. if (DQUOT_ALLOC_BLOCK(inode, 1))
  722. goto cleanup;
  723. error = ext4_journal_get_write_access(handle,
  724. new_bh);
  725. if (error)
  726. goto cleanup_dquot;
  727. lock_buffer(new_bh);
  728. BHDR(new_bh)->h_refcount = cpu_to_le32(1 +
  729. le32_to_cpu(BHDR(new_bh)->h_refcount));
  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_journal_dirty_metadata(handle,
  734. new_bh);
  735. if (error)
  736. goto cleanup_dquot;
  737. }
  738. mb_cache_entry_release(ce);
  739. ce = NULL;
  740. } else if (bs->bh && s->base == bs->bh->b_data) {
  741. /* We were modifying this block in-place. */
  742. ea_bdebug(bs->bh, "keeping this block");
  743. new_bh = bs->bh;
  744. get_bh(new_bh);
  745. } else {
  746. /* We need to allocate a new block */
  747. ext4_fsblk_t goal = le32_to_cpu(
  748. EXT4_SB(sb)->s_es->s_first_data_block) +
  749. (ext4_fsblk_t)EXT4_I(inode)->i_block_group *
  750. EXT4_BLOCKS_PER_GROUP(sb);
  751. ext4_fsblk_t block = ext4_new_block(handle, inode,
  752. goal, &error);
  753. if (error)
  754. goto cleanup;
  755. ea_idebug(inode, "creating block %d", block);
  756. new_bh = sb_getblk(sb, block);
  757. if (!new_bh) {
  758. getblk_failed:
  759. ext4_free_blocks(handle, inode, block, 1);
  760. error = -EIO;
  761. goto cleanup;
  762. }
  763. lock_buffer(new_bh);
  764. error = ext4_journal_get_create_access(handle, new_bh);
  765. if (error) {
  766. unlock_buffer(new_bh);
  767. goto getblk_failed;
  768. }
  769. memcpy(new_bh->b_data, s->base, new_bh->b_size);
  770. set_buffer_uptodate(new_bh);
  771. unlock_buffer(new_bh);
  772. ext4_xattr_cache_insert(new_bh);
  773. error = ext4_journal_dirty_metadata(handle, new_bh);
  774. if (error)
  775. goto cleanup;
  776. }
  777. }
  778. /* Update the inode. */
  779. EXT4_I(inode)->i_file_acl = new_bh ? new_bh->b_blocknr : 0;
  780. /* Drop the previous xattr block. */
  781. if (bs->bh && bs->bh != new_bh)
  782. ext4_xattr_release_block(handle, inode, bs->bh);
  783. error = 0;
  784. cleanup:
  785. if (ce)
  786. mb_cache_entry_release(ce);
  787. brelse(new_bh);
  788. if (!(bs->bh && s->base == bs->bh->b_data))
  789. kfree(s->base);
  790. return error;
  791. cleanup_dquot:
  792. DQUOT_FREE_BLOCK(inode, 1);
  793. goto cleanup;
  794. bad_block:
  795. ext4_error(inode->i_sb, __FUNCTION__,
  796. "inode %lu: bad block %llu", inode->i_ino,
  797. EXT4_I(inode)->i_file_acl);
  798. goto cleanup;
  799. #undef header
  800. }
  801. struct ext4_xattr_ibody_find {
  802. struct ext4_xattr_search s;
  803. struct ext4_iloc iloc;
  804. };
  805. static int
  806. ext4_xattr_ibody_find(struct inode *inode, struct ext4_xattr_info *i,
  807. struct ext4_xattr_ibody_find *is)
  808. {
  809. struct ext4_xattr_ibody_header *header;
  810. struct ext4_inode *raw_inode;
  811. int error;
  812. if (EXT4_I(inode)->i_extra_isize == 0)
  813. return 0;
  814. raw_inode = ext4_raw_inode(&is->iloc);
  815. header = IHDR(inode, raw_inode);
  816. is->s.base = is->s.first = IFIRST(header);
  817. is->s.here = is->s.first;
  818. is->s.end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
  819. if (EXT4_I(inode)->i_state & EXT4_STATE_XATTR) {
  820. error = ext4_xattr_check_names(IFIRST(header), is->s.end);
  821. if (error)
  822. return error;
  823. /* Find the named attribute. */
  824. error = ext4_xattr_find_entry(&is->s.here, i->name_index,
  825. i->name, is->s.end -
  826. (void *)is->s.base, 0);
  827. if (error && error != -ENODATA)
  828. return error;
  829. is->s.not_found = error;
  830. }
  831. return 0;
  832. }
  833. static int
  834. ext4_xattr_ibody_set(handle_t *handle, struct inode *inode,
  835. struct ext4_xattr_info *i,
  836. struct ext4_xattr_ibody_find *is)
  837. {
  838. struct ext4_xattr_ibody_header *header;
  839. struct ext4_xattr_search *s = &is->s;
  840. int error;
  841. if (EXT4_I(inode)->i_extra_isize == 0)
  842. return -ENOSPC;
  843. error = ext4_xattr_set_entry(i, s);
  844. if (error)
  845. return error;
  846. header = IHDR(inode, ext4_raw_inode(&is->iloc));
  847. if (!IS_LAST_ENTRY(s->first)) {
  848. header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
  849. EXT4_I(inode)->i_state |= EXT4_STATE_XATTR;
  850. } else {
  851. header->h_magic = cpu_to_le32(0);
  852. EXT4_I(inode)->i_state &= ~EXT4_STATE_XATTR;
  853. }
  854. return 0;
  855. }
  856. /*
  857. * ext4_xattr_set_handle()
  858. *
  859. * Create, replace or remove an extended attribute for this inode. Buffer
  860. * is NULL to remove an existing extended attribute, and non-NULL to
  861. * either replace an existing extended attribute, or create a new extended
  862. * attribute. The flags XATTR_REPLACE and XATTR_CREATE
  863. * specify that an extended attribute must exist and must not exist
  864. * previous to the call, respectively.
  865. *
  866. * Returns 0, or a negative error number on failure.
  867. */
  868. int
  869. ext4_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index,
  870. const char *name, const void *value, size_t value_len,
  871. int flags)
  872. {
  873. struct ext4_xattr_info i = {
  874. .name_index = name_index,
  875. .name = name,
  876. .value = value,
  877. .value_len = value_len,
  878. };
  879. struct ext4_xattr_ibody_find is = {
  880. .s = { .not_found = -ENODATA, },
  881. };
  882. struct ext4_xattr_block_find bs = {
  883. .s = { .not_found = -ENODATA, },
  884. };
  885. int error;
  886. if (!name)
  887. return -EINVAL;
  888. if (strlen(name) > 255)
  889. return -ERANGE;
  890. down_write(&EXT4_I(inode)->xattr_sem);
  891. error = ext4_get_inode_loc(inode, &is.iloc);
  892. if (error)
  893. goto cleanup;
  894. if (EXT4_I(inode)->i_state & EXT4_STATE_NEW) {
  895. struct ext4_inode *raw_inode = ext4_raw_inode(&is.iloc);
  896. memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size);
  897. EXT4_I(inode)->i_state &= ~EXT4_STATE_NEW;
  898. }
  899. error = ext4_xattr_ibody_find(inode, &i, &is);
  900. if (error)
  901. goto cleanup;
  902. if (is.s.not_found)
  903. error = ext4_xattr_block_find(inode, &i, &bs);
  904. if (error)
  905. goto cleanup;
  906. if (is.s.not_found && bs.s.not_found) {
  907. error = -ENODATA;
  908. if (flags & XATTR_REPLACE)
  909. goto cleanup;
  910. error = 0;
  911. if (!value)
  912. goto cleanup;
  913. } else {
  914. error = -EEXIST;
  915. if (flags & XATTR_CREATE)
  916. goto cleanup;
  917. }
  918. error = ext4_journal_get_write_access(handle, is.iloc.bh);
  919. if (error)
  920. goto cleanup;
  921. if (!value) {
  922. if (!is.s.not_found)
  923. error = ext4_xattr_ibody_set(handle, inode, &i, &is);
  924. else if (!bs.s.not_found)
  925. error = ext4_xattr_block_set(handle, inode, &i, &bs);
  926. } else {
  927. error = ext4_xattr_ibody_set(handle, inode, &i, &is);
  928. if (!error && !bs.s.not_found) {
  929. i.value = NULL;
  930. error = ext4_xattr_block_set(handle, inode, &i, &bs);
  931. } else if (error == -ENOSPC) {
  932. error = ext4_xattr_block_set(handle, inode, &i, &bs);
  933. if (error)
  934. goto cleanup;
  935. if (!is.s.not_found) {
  936. i.value = NULL;
  937. error = ext4_xattr_ibody_set(handle, inode, &i,
  938. &is);
  939. }
  940. }
  941. }
  942. if (!error) {
  943. ext4_xattr_update_super_block(handle, inode->i_sb);
  944. inode->i_ctime = ext4_current_time(inode);
  945. if(!value)
  946. EXT4_I(inode)->i_state &= ~EXT4_STATE_NO_EXPAND;
  947. error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
  948. /*
  949. * The bh is consumed by ext4_mark_iloc_dirty, even with
  950. * error != 0.
  951. */
  952. is.iloc.bh = NULL;
  953. if (IS_SYNC(inode))
  954. handle->h_sync = 1;
  955. }
  956. cleanup:
  957. brelse(is.iloc.bh);
  958. brelse(bs.bh);
  959. up_write(&EXT4_I(inode)->xattr_sem);
  960. return error;
  961. }
  962. /*
  963. * ext4_xattr_set()
  964. *
  965. * Like ext4_xattr_set_handle, but start from an inode. This extended
  966. * attribute modification is a filesystem transaction by itself.
  967. *
  968. * Returns 0, or a negative error number on failure.
  969. */
  970. int
  971. ext4_xattr_set(struct inode *inode, int name_index, const char *name,
  972. const void *value, size_t value_len, int flags)
  973. {
  974. handle_t *handle;
  975. int error, retries = 0;
  976. retry:
  977. handle = ext4_journal_start(inode, EXT4_DATA_TRANS_BLOCKS(inode->i_sb));
  978. if (IS_ERR(handle)) {
  979. error = PTR_ERR(handle);
  980. } else {
  981. int error2;
  982. error = ext4_xattr_set_handle(handle, inode, name_index, name,
  983. value, value_len, flags);
  984. error2 = ext4_journal_stop(handle);
  985. if (error == -ENOSPC &&
  986. ext4_should_retry_alloc(inode->i_sb, &retries))
  987. goto retry;
  988. if (error == 0)
  989. error = error2;
  990. }
  991. return error;
  992. }
  993. static void ext3_xattr_shift_entries(struct ext4_xattr_entry *entry,
  994. int value_offs_shift, void *to,
  995. void *from, size_t n, int blocksize)
  996. {
  997. struct ext4_xattr_entry *last = entry;
  998. int new_offs;
  999. /* Adjust the value offsets of the entries */
  1000. for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
  1001. if (!last->e_value_block && last->e_value_size) {
  1002. new_offs = le16_to_cpu(last->e_value_offs) +
  1003. value_offs_shift;
  1004. BUG_ON(new_offs + le32_to_cpu(last->e_value_size) > blocksize);
  1005. last->e_value_offs = cpu_to_le16(new_offs);
  1006. }
  1007. }
  1008. /* Shift the entries by n bytes */
  1009. memmove(to, from, n);
  1010. }
  1011. /* Expand an inode by new_extra_isize bytes.
  1012. * Returns 0 on success or negative error number on failure.
  1013. */
  1014. int ext4_expand_extra_isize(struct inode *inode, int new_extra_isize,
  1015. struct ext4_iloc iloc, handle_t *handle)
  1016. {
  1017. struct ext4_inode *raw_inode;
  1018. struct ext4_xattr_ibody_header *header;
  1019. struct ext4_xattr_entry *entry, *last, *first;
  1020. struct buffer_head *bh = NULL;
  1021. struct ext4_xattr_ibody_find *is = NULL;
  1022. struct ext4_xattr_block_find *bs = NULL;
  1023. char *buffer = NULL, *b_entry_name = NULL;
  1024. size_t min_offs, free;
  1025. int total_ino, total_blk;
  1026. void *base, *start, *end;
  1027. int extra_isize = 0, error = 0, tried_min_extra_isize = 0;
  1028. int s_min_extra_isize = EXT4_SB(inode->i_sb)->s_es->s_min_extra_isize;
  1029. down_write(&EXT4_I(inode)->xattr_sem);
  1030. retry:
  1031. if (EXT4_I(inode)->i_extra_isize >= new_extra_isize) {
  1032. up_write(&EXT4_I(inode)->xattr_sem);
  1033. return 0;
  1034. }
  1035. raw_inode = ext4_raw_inode(&iloc);
  1036. header = IHDR(inode, raw_inode);
  1037. entry = IFIRST(header);
  1038. /* No extended attributes present */
  1039. if (!(EXT4_I(inode)->i_state & EXT4_STATE_XATTR) ||
  1040. header->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC)) {
  1041. memset((void *)raw_inode + EXT4_GOOD_OLD_INODE_SIZE, 0,
  1042. new_extra_isize);
  1043. EXT4_I(inode)->i_extra_isize = new_extra_isize;
  1044. goto cleanup;
  1045. }
  1046. /*
  1047. * Check if enough free space is available in the inode to shift the
  1048. * entries ahead by new_extra_isize.
  1049. */
  1050. base = start = entry;
  1051. end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
  1052. min_offs = end - base;
  1053. last = entry;
  1054. total_ino = sizeof(struct ext4_xattr_ibody_header);
  1055. free = ext3_xattr_free_space(last, &min_offs, base, &total_ino);
  1056. if (free >= new_extra_isize) {
  1057. entry = IFIRST(header);
  1058. ext3_xattr_shift_entries(entry, EXT4_I(inode)->i_extra_isize
  1059. - new_extra_isize, (void *)raw_inode +
  1060. EXT4_GOOD_OLD_INODE_SIZE + new_extra_isize,
  1061. (void *)header, total_ino,
  1062. inode->i_sb->s_blocksize);
  1063. EXT4_I(inode)->i_extra_isize = new_extra_isize;
  1064. error = 0;
  1065. goto cleanup;
  1066. }
  1067. /*
  1068. * Enough free space isn't available in the inode, check if
  1069. * EA block can hold new_extra_isize bytes.
  1070. */
  1071. if (EXT4_I(inode)->i_file_acl) {
  1072. bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
  1073. error = -EIO;
  1074. if (!bh)
  1075. goto cleanup;
  1076. if (ext4_xattr_check_block(bh)) {
  1077. ext4_error(inode->i_sb, __FUNCTION__,
  1078. "inode %lu: bad block %llu", inode->i_ino,
  1079. EXT4_I(inode)->i_file_acl);
  1080. error = -EIO;
  1081. goto cleanup;
  1082. }
  1083. base = BHDR(bh);
  1084. first = BFIRST(bh);
  1085. end = bh->b_data + bh->b_size;
  1086. min_offs = end - base;
  1087. free = ext3_xattr_free_space(first, &min_offs, base,
  1088. &total_blk);
  1089. if (free < new_extra_isize) {
  1090. if (!tried_min_extra_isize && s_min_extra_isize) {
  1091. tried_min_extra_isize++;
  1092. new_extra_isize = s_min_extra_isize;
  1093. goto retry;
  1094. }
  1095. error = -1;
  1096. goto cleanup;
  1097. }
  1098. }
  1099. else {
  1100. free = inode->i_sb->s_blocksize;
  1101. }
  1102. while (new_extra_isize > 0) {
  1103. size_t offs, size, entry_size;
  1104. struct ext4_xattr_entry *small_entry = NULL;
  1105. struct ext4_xattr_info i = {
  1106. .value = NULL,
  1107. .value_len = 0,
  1108. };
  1109. unsigned int total_size, shift_bytes, temp = ~0U;
  1110. is = (struct ext4_xattr_ibody_find *) kmalloc(sizeof(struct
  1111. ext4_xattr_ibody_find), GFP_KERNEL);
  1112. bs = (struct ext4_xattr_block_find *) kmalloc(sizeof(struct
  1113. ext4_xattr_block_find), GFP_KERNEL);
  1114. memset((void *)is, 0, sizeof(struct ext4_xattr_ibody_find));
  1115. memset((void *)bs, 0, sizeof(struct ext4_xattr_block_find));
  1116. is->s.not_found = bs->s.not_found = -ENODATA;
  1117. is->iloc.bh = NULL;
  1118. bs->bh = NULL;
  1119. last = IFIRST(header);
  1120. /* Find the entry best suited to be pushed into EA block */
  1121. entry = NULL;
  1122. for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
  1123. total_size = EXT4_XATTR_SIZE(le32_to_cpu(last->e_value_size)) +
  1124. EXT4_XATTR_LEN(last->e_name_len);
  1125. if (total_size <= free && total_size < temp) {
  1126. if (total_size < new_extra_isize) {
  1127. small_entry = last;
  1128. }
  1129. else {
  1130. entry = last;
  1131. temp = total_size;
  1132. }
  1133. }
  1134. }
  1135. if (entry == NULL) {
  1136. if (small_entry) {
  1137. entry = small_entry;
  1138. }
  1139. else {
  1140. if (!tried_min_extra_isize &&
  1141. s_min_extra_isize) {
  1142. tried_min_extra_isize++;
  1143. new_extra_isize = s_min_extra_isize;
  1144. goto retry;
  1145. }
  1146. error = -1;
  1147. goto cleanup;
  1148. }
  1149. }
  1150. offs = le16_to_cpu(entry->e_value_offs);
  1151. size = le32_to_cpu(entry->e_value_size);
  1152. entry_size = EXT4_XATTR_LEN(entry->e_name_len);
  1153. i.name_index = entry->e_name_index,
  1154. buffer = kmalloc(EXT4_XATTR_SIZE(size), GFP_KERNEL);
  1155. b_entry_name = kmalloc(entry->e_name_len + 1, GFP_KERNEL);
  1156. /* Save the entry name and the entry value */
  1157. memcpy((void *)buffer, (void *)IFIRST(header) + offs,
  1158. EXT4_XATTR_SIZE(size));
  1159. memcpy((void *)b_entry_name, (void *)entry->e_name,
  1160. entry->e_name_len);
  1161. b_entry_name[entry->e_name_len] = '\0';
  1162. i.name = b_entry_name;
  1163. error = ext4_get_inode_loc(inode, &is->iloc);
  1164. if (error)
  1165. goto cleanup;
  1166. error = ext4_xattr_ibody_find(inode, &i, is);
  1167. if (error)
  1168. goto cleanup;
  1169. /* Remove the chosen entry from the inode */
  1170. error = ext4_xattr_ibody_set(handle, inode, &i, is);
  1171. entry = IFIRST(header);
  1172. if (entry_size + EXT4_XATTR_SIZE(size) >= new_extra_isize)
  1173. shift_bytes = new_extra_isize;
  1174. else
  1175. shift_bytes = entry_size + size;
  1176. /* Adjust the offsets and shift the remaining entries ahead */
  1177. ext3_xattr_shift_entries(entry, EXT4_I(inode)->i_extra_isize -
  1178. shift_bytes, (void *)raw_inode +
  1179. EXT4_GOOD_OLD_INODE_SIZE + extra_isize + shift_bytes,
  1180. (void *)header, total_ino - entry_size,
  1181. inode->i_sb->s_blocksize);
  1182. extra_isize += shift_bytes;
  1183. new_extra_isize -= shift_bytes;
  1184. EXT4_I(inode)->i_extra_isize = extra_isize;
  1185. i.name = b_entry_name;
  1186. i.value = buffer;
  1187. i.value_len = cpu_to_le32(size);
  1188. error = ext4_xattr_block_find(inode, &i, bs);
  1189. if (error)
  1190. goto cleanup;
  1191. /* Add entry which was removed from the inode into the block */
  1192. error = ext4_xattr_block_set(handle, inode, &i, bs);
  1193. if (error)
  1194. goto cleanup;
  1195. }
  1196. cleanup:
  1197. if (b_entry_name)
  1198. kfree(b_entry_name);
  1199. if (buffer)
  1200. kfree(buffer);
  1201. if (is) {
  1202. brelse(is->iloc.bh);
  1203. kfree(is);
  1204. }
  1205. if (bs)
  1206. kfree(bs);
  1207. brelse(bh);
  1208. up_write(&EXT4_I(inode)->xattr_sem);
  1209. return error;
  1210. }
  1211. /*
  1212. * ext4_xattr_delete_inode()
  1213. *
  1214. * Free extended attribute resources associated with this inode. This
  1215. * is called immediately before an inode is freed. We have exclusive
  1216. * access to the inode.
  1217. */
  1218. void
  1219. ext4_xattr_delete_inode(handle_t *handle, struct inode *inode)
  1220. {
  1221. struct buffer_head *bh = NULL;
  1222. if (!EXT4_I(inode)->i_file_acl)
  1223. goto cleanup;
  1224. bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
  1225. if (!bh) {
  1226. ext4_error(inode->i_sb, __FUNCTION__,
  1227. "inode %lu: block %llu read error", inode->i_ino,
  1228. EXT4_I(inode)->i_file_acl);
  1229. goto cleanup;
  1230. }
  1231. if (BHDR(bh)->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC) ||
  1232. BHDR(bh)->h_blocks != cpu_to_le32(1)) {
  1233. ext4_error(inode->i_sb, __FUNCTION__,
  1234. "inode %lu: bad block %llu", inode->i_ino,
  1235. EXT4_I(inode)->i_file_acl);
  1236. goto cleanup;
  1237. }
  1238. ext4_xattr_release_block(handle, inode, bh);
  1239. EXT4_I(inode)->i_file_acl = 0;
  1240. cleanup:
  1241. brelse(bh);
  1242. }
  1243. /*
  1244. * ext4_xattr_put_super()
  1245. *
  1246. * This is called when a file system is unmounted.
  1247. */
  1248. void
  1249. ext4_xattr_put_super(struct super_block *sb)
  1250. {
  1251. mb_cache_shrink(sb->s_bdev);
  1252. }
  1253. /*
  1254. * ext4_xattr_cache_insert()
  1255. *
  1256. * Create a new entry in the extended attribute cache, and insert
  1257. * it unless such an entry is already in the cache.
  1258. *
  1259. * Returns 0, or a negative error number on failure.
  1260. */
  1261. static void
  1262. ext4_xattr_cache_insert(struct buffer_head *bh)
  1263. {
  1264. __u32 hash = le32_to_cpu(BHDR(bh)->h_hash);
  1265. struct mb_cache_entry *ce;
  1266. int error;
  1267. ce = mb_cache_entry_alloc(ext4_xattr_cache);
  1268. if (!ce) {
  1269. ea_bdebug(bh, "out of memory");
  1270. return;
  1271. }
  1272. error = mb_cache_entry_insert(ce, bh->b_bdev, bh->b_blocknr, &hash);
  1273. if (error) {
  1274. mb_cache_entry_free(ce);
  1275. if (error == -EBUSY) {
  1276. ea_bdebug(bh, "already in cache");
  1277. error = 0;
  1278. }
  1279. } else {
  1280. ea_bdebug(bh, "inserting [%x]", (int)hash);
  1281. mb_cache_entry_release(ce);
  1282. }
  1283. }
  1284. /*
  1285. * ext4_xattr_cmp()
  1286. *
  1287. * Compare two extended attribute blocks for equality.
  1288. *
  1289. * Returns 0 if the blocks are equal, 1 if they differ, and
  1290. * a negative error number on errors.
  1291. */
  1292. static int
  1293. ext4_xattr_cmp(struct ext4_xattr_header *header1,
  1294. struct ext4_xattr_header *header2)
  1295. {
  1296. struct ext4_xattr_entry *entry1, *entry2;
  1297. entry1 = ENTRY(header1+1);
  1298. entry2 = ENTRY(header2+1);
  1299. while (!IS_LAST_ENTRY(entry1)) {
  1300. if (IS_LAST_ENTRY(entry2))
  1301. return 1;
  1302. if (entry1->e_hash != entry2->e_hash ||
  1303. entry1->e_name_index != entry2->e_name_index ||
  1304. entry1->e_name_len != entry2->e_name_len ||
  1305. entry1->e_value_size != entry2->e_value_size ||
  1306. memcmp(entry1->e_name, entry2->e_name, entry1->e_name_len))
  1307. return 1;
  1308. if (entry1->e_value_block != 0 || entry2->e_value_block != 0)
  1309. return -EIO;
  1310. if (memcmp((char *)header1 + le16_to_cpu(entry1->e_value_offs),
  1311. (char *)header2 + le16_to_cpu(entry2->e_value_offs),
  1312. le32_to_cpu(entry1->e_value_size)))
  1313. return 1;
  1314. entry1 = EXT4_XATTR_NEXT(entry1);
  1315. entry2 = EXT4_XATTR_NEXT(entry2);
  1316. }
  1317. if (!IS_LAST_ENTRY(entry2))
  1318. return 1;
  1319. return 0;
  1320. }
  1321. /*
  1322. * ext4_xattr_cache_find()
  1323. *
  1324. * Find an identical extended attribute block.
  1325. *
  1326. * Returns a pointer to the block found, or NULL if such a block was
  1327. * not found or an error occurred.
  1328. */
  1329. static struct buffer_head *
  1330. ext4_xattr_cache_find(struct inode *inode, struct ext4_xattr_header *header,
  1331. struct mb_cache_entry **pce)
  1332. {
  1333. __u32 hash = le32_to_cpu(header->h_hash);
  1334. struct mb_cache_entry *ce;
  1335. if (!header->h_hash)
  1336. return NULL; /* never share */
  1337. ea_idebug(inode, "looking for cached blocks [%x]", (int)hash);
  1338. again:
  1339. ce = mb_cache_entry_find_first(ext4_xattr_cache, 0,
  1340. inode->i_sb->s_bdev, hash);
  1341. while (ce) {
  1342. struct buffer_head *bh;
  1343. if (IS_ERR(ce)) {
  1344. if (PTR_ERR(ce) == -EAGAIN)
  1345. goto again;
  1346. break;
  1347. }
  1348. bh = sb_bread(inode->i_sb, ce->e_block);
  1349. if (!bh) {
  1350. ext4_error(inode->i_sb, __FUNCTION__,
  1351. "inode %lu: block %lu read error",
  1352. inode->i_ino, (unsigned long) ce->e_block);
  1353. } else if (le32_to_cpu(BHDR(bh)->h_refcount) >=
  1354. EXT4_XATTR_REFCOUNT_MAX) {
  1355. ea_idebug(inode, "block %lu refcount %d>=%d",
  1356. (unsigned long) ce->e_block,
  1357. le32_to_cpu(BHDR(bh)->h_refcount),
  1358. EXT4_XATTR_REFCOUNT_MAX);
  1359. } else if (ext4_xattr_cmp(header, BHDR(bh)) == 0) {
  1360. *pce = ce;
  1361. return bh;
  1362. }
  1363. brelse(bh);
  1364. ce = mb_cache_entry_find_next(ce, 0, inode->i_sb->s_bdev, hash);
  1365. }
  1366. return NULL;
  1367. }
  1368. #define NAME_HASH_SHIFT 5
  1369. #define VALUE_HASH_SHIFT 16
  1370. /*
  1371. * ext4_xattr_hash_entry()
  1372. *
  1373. * Compute the hash of an extended attribute.
  1374. */
  1375. static inline void ext4_xattr_hash_entry(struct ext4_xattr_header *header,
  1376. struct ext4_xattr_entry *entry)
  1377. {
  1378. __u32 hash = 0;
  1379. char *name = entry->e_name;
  1380. int n;
  1381. for (n=0; n < entry->e_name_len; n++) {
  1382. hash = (hash << NAME_HASH_SHIFT) ^
  1383. (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^
  1384. *name++;
  1385. }
  1386. if (entry->e_value_block == 0 && entry->e_value_size != 0) {
  1387. __le32 *value = (__le32 *)((char *)header +
  1388. le16_to_cpu(entry->e_value_offs));
  1389. for (n = (le32_to_cpu(entry->e_value_size) +
  1390. EXT4_XATTR_ROUND) >> EXT4_XATTR_PAD_BITS; n; n--) {
  1391. hash = (hash << VALUE_HASH_SHIFT) ^
  1392. (hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^
  1393. le32_to_cpu(*value++);
  1394. }
  1395. }
  1396. entry->e_hash = cpu_to_le32(hash);
  1397. }
  1398. #undef NAME_HASH_SHIFT
  1399. #undef VALUE_HASH_SHIFT
  1400. #define BLOCK_HASH_SHIFT 16
  1401. /*
  1402. * ext4_xattr_rehash()
  1403. *
  1404. * Re-compute the extended attribute hash value after an entry has changed.
  1405. */
  1406. static void ext4_xattr_rehash(struct ext4_xattr_header *header,
  1407. struct ext4_xattr_entry *entry)
  1408. {
  1409. struct ext4_xattr_entry *here;
  1410. __u32 hash = 0;
  1411. ext4_xattr_hash_entry(header, entry);
  1412. here = ENTRY(header+1);
  1413. while (!IS_LAST_ENTRY(here)) {
  1414. if (!here->e_hash) {
  1415. /* Block is not shared if an entry's hash value == 0 */
  1416. hash = 0;
  1417. break;
  1418. }
  1419. hash = (hash << BLOCK_HASH_SHIFT) ^
  1420. (hash >> (8*sizeof(hash) - BLOCK_HASH_SHIFT)) ^
  1421. le32_to_cpu(here->e_hash);
  1422. here = EXT4_XATTR_NEXT(here);
  1423. }
  1424. header->h_hash = cpu_to_le32(hash);
  1425. }
  1426. #undef BLOCK_HASH_SHIFT
  1427. int __init
  1428. init_ext4_xattr(void)
  1429. {
  1430. ext4_xattr_cache = mb_cache_create("ext4_xattr", NULL,
  1431. sizeof(struct mb_cache_entry) +
  1432. sizeof(((struct mb_cache_entry *) 0)->e_indexes[0]), 1, 6);
  1433. if (!ext4_xattr_cache)
  1434. return -ENOMEM;
  1435. return 0;
  1436. }
  1437. void
  1438. exit_ext4_xattr(void)
  1439. {
  1440. if (ext4_xattr_cache)
  1441. mb_cache_destroy(ext4_xattr_cache);
  1442. ext4_xattr_cache = NULL;
  1443. }