PageRenderTime 56ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/fs/ext4/xattr.c

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