PageRenderTime 78ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/fs/ext4/xattr.c

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