xattr.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * linux/fs/ext2/xattr.c
  4. *
  5. * Copyright (C) 2001-2003 Andreas Gruenbacher <agruen@suse.de>
  6. *
  7. * Fix by Harrison Xing <harrison@mountainviewdata.com>.
  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. *
  13. */
  14. /*
  15. * Extended attributes are stored on disk blocks allocated outside of
  16. * any inode. The i_file_acl field is then made to point to this allocated
  17. * block. If all extended attributes of an inode are identical, these
  18. * inodes may share the same extended attribute block. Such situations
  19. * are automatically detected by keeping a cache of recent attribute block
  20. * numbers and hashes over the block's contents in memory.
  21. *
  22. *
  23. * Extended attribute block layout:
  24. *
  25. * +------------------+
  26. * | header |
  27. * | entry 1 | |
  28. * | entry 2 | | growing downwards
  29. * | entry 3 | v
  30. * | four null bytes |
  31. * | . . . |
  32. * | value 1 | ^
  33. * | value 3 | | growing upwards
  34. * | value 2 | |
  35. * +------------------+
  36. *
  37. * The block header is followed by multiple entry descriptors. These entry
  38. * descriptors are variable in size, and aligned to EXT2_XATTR_PAD
  39. * byte boundaries. The entry descriptors are sorted by attribute name,
  40. * so that two extended attribute blocks can be compared efficiently.
  41. *
  42. * Attribute values are aligned to the end of the block, stored in
  43. * no specific order. They are also padded to EXT2_XATTR_PAD byte
  44. * boundaries. No additional gaps are left between them.
  45. *
  46. * Locking strategy
  47. * ----------------
  48. * EXT2_I(inode)->i_file_acl is protected by EXT2_I(inode)->xattr_sem.
  49. * EA blocks are only changed if they are exclusive to an inode, so
  50. * holding xattr_sem also means that nothing but the EA block's reference
  51. * count will change. Multiple writers to an EA block are synchronized
  52. * by the bh lock. No more than a single bh lock is held at any time
  53. * to avoid deadlocks.
  54. */
  55. #include <linux/buffer_head.h>
  56. #include <linux/init.h>
  57. #include <linux/printk.h>
  58. #include <linux/slab.h>
  59. #include <linux/mbcache.h>
  60. #include <linux/quotaops.h>
  61. #include <linux/rwsem.h>
  62. #include <linux/security.h>
  63. #include "ext2.h"
  64. #include "xattr.h"
  65. #include "acl.h"
  66. #define HDR(bh) ((struct ext2_xattr_header *)((bh)->b_data))
  67. #define ENTRY(ptr) ((struct ext2_xattr_entry *)(ptr))
  68. #define FIRST_ENTRY(bh) ENTRY(HDR(bh)+1)
  69. #define IS_LAST_ENTRY(entry) (*(__u32 *)(entry) == 0)
  70. #ifdef EXT2_XATTR_DEBUG
  71. # define ea_idebug(inode, f...) do { \
  72. printk(KERN_DEBUG "inode %s:%ld: ", \
  73. inode->i_sb->s_id, inode->i_ino); \
  74. printk(f); \
  75. printk("\n"); \
  76. } while (0)
  77. # define ea_bdebug(bh, f...) do { \
  78. printk(KERN_DEBUG "block %pg:%lu: ", \
  79. bh->b_bdev, (unsigned long) bh->b_blocknr); \
  80. printk(f); \
  81. printk("\n"); \
  82. } while (0)
  83. #else
  84. # define ea_idebug(inode, f...) no_printk(f)
  85. # define ea_bdebug(bh, f...) no_printk(f)
  86. #endif
  87. static int ext2_xattr_set2(struct inode *, struct buffer_head *,
  88. struct ext2_xattr_header *);
  89. static int ext2_xattr_cache_insert(struct mb_cache *, struct buffer_head *);
  90. static struct buffer_head *ext2_xattr_cache_find(struct inode *,
  91. struct ext2_xattr_header *);
  92. static void ext2_xattr_rehash(struct ext2_xattr_header *,
  93. struct ext2_xattr_entry *);
  94. static const struct xattr_handler * const ext2_xattr_handler_map[] = {
  95. [EXT2_XATTR_INDEX_USER] = &ext2_xattr_user_handler,
  96. #ifdef CONFIG_EXT2_FS_POSIX_ACL
  97. [EXT2_XATTR_INDEX_POSIX_ACL_ACCESS] = &nop_posix_acl_access,
  98. [EXT2_XATTR_INDEX_POSIX_ACL_DEFAULT] = &nop_posix_acl_default,
  99. #endif
  100. [EXT2_XATTR_INDEX_TRUSTED] = &ext2_xattr_trusted_handler,
  101. #ifdef CONFIG_EXT2_FS_SECURITY
  102. [EXT2_XATTR_INDEX_SECURITY] = &ext2_xattr_security_handler,
  103. #endif
  104. };
  105. const struct xattr_handler * const ext2_xattr_handlers[] = {
  106. &ext2_xattr_user_handler,
  107. &ext2_xattr_trusted_handler,
  108. #ifdef CONFIG_EXT2_FS_SECURITY
  109. &ext2_xattr_security_handler,
  110. #endif
  111. NULL
  112. };
  113. #define EA_BLOCK_CACHE(inode) (EXT2_SB(inode->i_sb)->s_ea_block_cache)
  114. static inline const char *ext2_xattr_prefix(int name_index,
  115. struct dentry *dentry)
  116. {
  117. const struct xattr_handler *handler = NULL;
  118. if (name_index > 0 && name_index < ARRAY_SIZE(ext2_xattr_handler_map))
  119. handler = ext2_xattr_handler_map[name_index];
  120. if (!xattr_handler_can_list(handler, dentry))
  121. return NULL;
  122. return xattr_prefix(handler);
  123. }
  124. static bool
  125. ext2_xattr_header_valid(struct ext2_xattr_header *header)
  126. {
  127. if (header->h_magic != cpu_to_le32(EXT2_XATTR_MAGIC) ||
  128. header->h_blocks != cpu_to_le32(1))
  129. return false;
  130. return true;
  131. }
  132. static bool
  133. ext2_xattr_entry_valid(struct ext2_xattr_entry *entry,
  134. char *end, size_t end_offs)
  135. {
  136. struct ext2_xattr_entry *next;
  137. size_t size;
  138. next = EXT2_XATTR_NEXT(entry);
  139. if ((char *)next >= end)
  140. return false;
  141. if (entry->e_value_block != 0)
  142. return false;
  143. size = le32_to_cpu(entry->e_value_size);
  144. if (size > end_offs ||
  145. le16_to_cpu(entry->e_value_offs) + size > end_offs)
  146. return false;
  147. return true;
  148. }
  149. static int
  150. ext2_xattr_cmp_entry(int name_index, size_t name_len, const char *name,
  151. struct ext2_xattr_entry *entry)
  152. {
  153. int cmp;
  154. cmp = name_index - entry->e_name_index;
  155. if (!cmp)
  156. cmp = name_len - entry->e_name_len;
  157. if (!cmp)
  158. cmp = memcmp(name, entry->e_name, name_len);
  159. return cmp;
  160. }
  161. /*
  162. * ext2_xattr_get()
  163. *
  164. * Copy an extended attribute into the buffer
  165. * provided, or compute the buffer size required.
  166. * Buffer is NULL to compute the size of the buffer required.
  167. *
  168. * Returns a negative error number on failure, or the number of bytes
  169. * used / required on success.
  170. */
  171. int
  172. ext2_xattr_get(struct inode *inode, int name_index, const char *name,
  173. void *buffer, size_t buffer_size)
  174. {
  175. struct buffer_head *bh = NULL;
  176. struct ext2_xattr_entry *entry;
  177. size_t name_len, size;
  178. char *end;
  179. int error, not_found;
  180. struct mb_cache *ea_block_cache = EA_BLOCK_CACHE(inode);
  181. ea_idebug(inode, "name=%d.%s, buffer=%p, buffer_size=%ld",
  182. name_index, name, buffer, (long)buffer_size);
  183. if (name == NULL)
  184. return -EINVAL;
  185. name_len = strlen(name);
  186. if (name_len > 255)
  187. return -ERANGE;
  188. down_read(&EXT2_I(inode)->xattr_sem);
  189. error = -ENODATA;
  190. if (!EXT2_I(inode)->i_file_acl)
  191. goto cleanup;
  192. ea_idebug(inode, "reading block %d", EXT2_I(inode)->i_file_acl);
  193. bh = sb_bread(inode->i_sb, EXT2_I(inode)->i_file_acl);
  194. error = -EIO;
  195. if (!bh)
  196. goto cleanup;
  197. ea_bdebug(bh, "b_count=%d, refcount=%d",
  198. atomic_read(&(bh->b_count)), le32_to_cpu(HDR(bh)->h_refcount));
  199. end = bh->b_data + bh->b_size;
  200. if (!ext2_xattr_header_valid(HDR(bh))) {
  201. bad_block:
  202. ext2_error(inode->i_sb, "ext2_xattr_get",
  203. "inode %ld: bad block %d", inode->i_ino,
  204. EXT2_I(inode)->i_file_acl);
  205. error = -EIO;
  206. goto cleanup;
  207. }
  208. /* find named attribute */
  209. entry = FIRST_ENTRY(bh);
  210. while (!IS_LAST_ENTRY(entry)) {
  211. if (!ext2_xattr_entry_valid(entry, end,
  212. inode->i_sb->s_blocksize))
  213. goto bad_block;
  214. not_found = ext2_xattr_cmp_entry(name_index, name_len, name,
  215. entry);
  216. if (!not_found)
  217. goto found;
  218. if (not_found < 0)
  219. break;
  220. entry = EXT2_XATTR_NEXT(entry);
  221. }
  222. if (ext2_xattr_cache_insert(ea_block_cache, bh))
  223. ea_idebug(inode, "cache insert failed");
  224. error = -ENODATA;
  225. goto cleanup;
  226. found:
  227. size = le32_to_cpu(entry->e_value_size);
  228. if (ext2_xattr_cache_insert(ea_block_cache, bh))
  229. ea_idebug(inode, "cache insert failed");
  230. if (buffer) {
  231. error = -ERANGE;
  232. if (size > buffer_size)
  233. goto cleanup;
  234. /* return value of attribute */
  235. memcpy(buffer, bh->b_data + le16_to_cpu(entry->e_value_offs),
  236. size);
  237. }
  238. error = size;
  239. cleanup:
  240. brelse(bh);
  241. up_read(&EXT2_I(inode)->xattr_sem);
  242. return error;
  243. }
  244. /*
  245. * ext2_xattr_list()
  246. *
  247. * Copy a list of attribute names into the buffer
  248. * provided, or compute the buffer size required.
  249. * Buffer is NULL to compute the size of the buffer required.
  250. *
  251. * Returns a negative error number on failure, or the number of bytes
  252. * used / required on success.
  253. */
  254. static int
  255. ext2_xattr_list(struct dentry *dentry, char *buffer, size_t buffer_size)
  256. {
  257. struct inode *inode = d_inode(dentry);
  258. struct buffer_head *bh = NULL;
  259. struct ext2_xattr_entry *entry;
  260. char *end;
  261. size_t rest = buffer_size;
  262. int error;
  263. struct mb_cache *ea_block_cache = EA_BLOCK_CACHE(inode);
  264. ea_idebug(inode, "buffer=%p, buffer_size=%ld",
  265. buffer, (long)buffer_size);
  266. down_read(&EXT2_I(inode)->xattr_sem);
  267. error = 0;
  268. if (!EXT2_I(inode)->i_file_acl)
  269. goto cleanup;
  270. ea_idebug(inode, "reading block %d", EXT2_I(inode)->i_file_acl);
  271. bh = sb_bread(inode->i_sb, EXT2_I(inode)->i_file_acl);
  272. error = -EIO;
  273. if (!bh)
  274. goto cleanup;
  275. ea_bdebug(bh, "b_count=%d, refcount=%d",
  276. atomic_read(&(bh->b_count)), le32_to_cpu(HDR(bh)->h_refcount));
  277. end = bh->b_data + bh->b_size;
  278. if (!ext2_xattr_header_valid(HDR(bh))) {
  279. bad_block:
  280. ext2_error(inode->i_sb, "ext2_xattr_list",
  281. "inode %ld: bad block %d", inode->i_ino,
  282. EXT2_I(inode)->i_file_acl);
  283. error = -EIO;
  284. goto cleanup;
  285. }
  286. /* check the on-disk data structure */
  287. entry = FIRST_ENTRY(bh);
  288. while (!IS_LAST_ENTRY(entry)) {
  289. if (!ext2_xattr_entry_valid(entry, end,
  290. inode->i_sb->s_blocksize))
  291. goto bad_block;
  292. entry = EXT2_XATTR_NEXT(entry);
  293. }
  294. if (ext2_xattr_cache_insert(ea_block_cache, bh))
  295. ea_idebug(inode, "cache insert failed");
  296. /* list the attribute names */
  297. for (entry = FIRST_ENTRY(bh); !IS_LAST_ENTRY(entry);
  298. entry = EXT2_XATTR_NEXT(entry)) {
  299. const char *prefix;
  300. prefix = ext2_xattr_prefix(entry->e_name_index, dentry);
  301. if (prefix) {
  302. size_t prefix_len = strlen(prefix);
  303. size_t size = prefix_len + entry->e_name_len + 1;
  304. if (buffer) {
  305. if (size > rest) {
  306. error = -ERANGE;
  307. goto cleanup;
  308. }
  309. memcpy(buffer, prefix, prefix_len);
  310. buffer += prefix_len;
  311. memcpy(buffer, entry->e_name, entry->e_name_len);
  312. buffer += entry->e_name_len;
  313. *buffer++ = 0;
  314. }
  315. rest -= size;
  316. }
  317. }
  318. error = buffer_size - rest; /* total size */
  319. cleanup:
  320. brelse(bh);
  321. up_read(&EXT2_I(inode)->xattr_sem);
  322. return error;
  323. }
  324. /*
  325. * Inode operation listxattr()
  326. *
  327. * d_inode(dentry)->i_mutex: don't care
  328. */
  329. ssize_t
  330. ext2_listxattr(struct dentry *dentry, char *buffer, size_t size)
  331. {
  332. return ext2_xattr_list(dentry, buffer, size);
  333. }
  334. /*
  335. * If the EXT2_FEATURE_COMPAT_EXT_ATTR feature of this file system is
  336. * not set, set it.
  337. */
  338. static void ext2_xattr_update_super_block(struct super_block *sb)
  339. {
  340. if (EXT2_HAS_COMPAT_FEATURE(sb, EXT2_FEATURE_COMPAT_EXT_ATTR))
  341. return;
  342. spin_lock(&EXT2_SB(sb)->s_lock);
  343. ext2_update_dynamic_rev(sb);
  344. EXT2_SET_COMPAT_FEATURE(sb, EXT2_FEATURE_COMPAT_EXT_ATTR);
  345. spin_unlock(&EXT2_SB(sb)->s_lock);
  346. mark_buffer_dirty(EXT2_SB(sb)->s_sbh);
  347. }
  348. /*
  349. * ext2_xattr_set()
  350. *
  351. * Create, replace or remove an extended attribute for this inode. Value
  352. * is NULL to remove an existing extended attribute, and non-NULL to
  353. * either replace an existing extended attribute, or create a new extended
  354. * attribute. The flags XATTR_REPLACE and XATTR_CREATE
  355. * specify that an extended attribute must exist and must not exist
  356. * previous to the call, respectively.
  357. *
  358. * Returns 0, or a negative error number on failure.
  359. */
  360. int
  361. ext2_xattr_set(struct inode *inode, int name_index, const char *name,
  362. const void *value, size_t value_len, int flags)
  363. {
  364. struct super_block *sb = inode->i_sb;
  365. struct buffer_head *bh = NULL;
  366. struct ext2_xattr_header *header = NULL;
  367. struct ext2_xattr_entry *here = NULL, *last = NULL;
  368. size_t name_len, free, min_offs = sb->s_blocksize;
  369. int not_found = 1, error;
  370. char *end;
  371. /*
  372. * header -- Points either into bh, or to a temporarily
  373. * allocated buffer.
  374. * here -- The named entry found, or the place for inserting, within
  375. * the block pointed to by header.
  376. * last -- Points right after the last named entry within the block
  377. * pointed to by header.
  378. * min_offs -- The offset of the first value (values are aligned
  379. * towards the end of the block).
  380. * end -- Points right after the block pointed to by header.
  381. */
  382. ea_idebug(inode, "name=%d.%s, value=%p, value_len=%ld",
  383. name_index, name, value, (long)value_len);
  384. if (value == NULL)
  385. value_len = 0;
  386. if (name == NULL)
  387. return -EINVAL;
  388. name_len = strlen(name);
  389. if (name_len > 255 || value_len > sb->s_blocksize)
  390. return -ERANGE;
  391. error = dquot_initialize(inode);
  392. if (error)
  393. return error;
  394. down_write(&EXT2_I(inode)->xattr_sem);
  395. if (EXT2_I(inode)->i_file_acl) {
  396. /* The inode already has an extended attribute block. */
  397. bh = sb_bread(sb, EXT2_I(inode)->i_file_acl);
  398. error = -EIO;
  399. if (!bh)
  400. goto cleanup;
  401. ea_bdebug(bh, "b_count=%d, refcount=%d",
  402. atomic_read(&(bh->b_count)),
  403. le32_to_cpu(HDR(bh)->h_refcount));
  404. header = HDR(bh);
  405. end = bh->b_data + bh->b_size;
  406. if (!ext2_xattr_header_valid(header)) {
  407. bad_block:
  408. ext2_error(sb, "ext2_xattr_set",
  409. "inode %ld: bad block %d", inode->i_ino,
  410. EXT2_I(inode)->i_file_acl);
  411. error = -EIO;
  412. goto cleanup;
  413. }
  414. /*
  415. * Find the named attribute. If not found, 'here' will point
  416. * to entry where the new attribute should be inserted to
  417. * maintain sorting.
  418. */
  419. last = FIRST_ENTRY(bh);
  420. while (!IS_LAST_ENTRY(last)) {
  421. if (!ext2_xattr_entry_valid(last, end, sb->s_blocksize))
  422. goto bad_block;
  423. if (last->e_value_size) {
  424. size_t offs = le16_to_cpu(last->e_value_offs);
  425. if (offs < min_offs)
  426. min_offs = offs;
  427. }
  428. if (not_found > 0) {
  429. not_found = ext2_xattr_cmp_entry(name_index,
  430. name_len,
  431. name, last);
  432. if (not_found <= 0)
  433. here = last;
  434. }
  435. last = EXT2_XATTR_NEXT(last);
  436. }
  437. if (not_found > 0)
  438. here = last;
  439. /* Check whether we have enough space left. */
  440. free = min_offs - ((char*)last - (char*)header) - sizeof(__u32);
  441. } else {
  442. /* We will use a new extended attribute block. */
  443. free = sb->s_blocksize -
  444. sizeof(struct ext2_xattr_header) - sizeof(__u32);
  445. }
  446. if (not_found) {
  447. /* Request to remove a nonexistent attribute? */
  448. error = -ENODATA;
  449. if (flags & XATTR_REPLACE)
  450. goto cleanup;
  451. error = 0;
  452. if (value == NULL)
  453. goto cleanup;
  454. } else {
  455. /* Request to create an existing attribute? */
  456. error = -EEXIST;
  457. if (flags & XATTR_CREATE)
  458. goto cleanup;
  459. free += EXT2_XATTR_SIZE(le32_to_cpu(here->e_value_size));
  460. free += EXT2_XATTR_LEN(name_len);
  461. }
  462. error = -ENOSPC;
  463. if (free < EXT2_XATTR_LEN(name_len) + EXT2_XATTR_SIZE(value_len))
  464. goto cleanup;
  465. /* Here we know that we can set the new attribute. */
  466. if (header) {
  467. int offset;
  468. lock_buffer(bh);
  469. if (header->h_refcount == cpu_to_le32(1)) {
  470. __u32 hash = le32_to_cpu(header->h_hash);
  471. struct mb_cache_entry *oe;
  472. oe = mb_cache_entry_delete_or_get(EA_BLOCK_CACHE(inode),
  473. hash, bh->b_blocknr);
  474. if (!oe) {
  475. ea_bdebug(bh, "modifying in-place");
  476. goto update_block;
  477. }
  478. /*
  479. * Someone is trying to reuse the block, leave it alone
  480. */
  481. mb_cache_entry_put(EA_BLOCK_CACHE(inode), oe);
  482. }
  483. unlock_buffer(bh);
  484. ea_bdebug(bh, "cloning");
  485. header = kmemdup(HDR(bh), bh->b_size, GFP_KERNEL);
  486. error = -ENOMEM;
  487. if (header == NULL)
  488. goto cleanup;
  489. header->h_refcount = cpu_to_le32(1);
  490. offset = (char *)here - bh->b_data;
  491. here = ENTRY((char *)header + offset);
  492. offset = (char *)last - bh->b_data;
  493. last = ENTRY((char *)header + offset);
  494. } else {
  495. /* Allocate a buffer where we construct the new block. */
  496. header = kzalloc(sb->s_blocksize, GFP_KERNEL);
  497. error = -ENOMEM;
  498. if (header == NULL)
  499. goto cleanup;
  500. header->h_magic = cpu_to_le32(EXT2_XATTR_MAGIC);
  501. header->h_blocks = header->h_refcount = cpu_to_le32(1);
  502. last = here = ENTRY(header+1);
  503. }
  504. update_block:
  505. /* Iff we are modifying the block in-place, bh is locked here. */
  506. if (not_found) {
  507. /* Insert the new name. */
  508. size_t size = EXT2_XATTR_LEN(name_len);
  509. size_t rest = (char *)last - (char *)here;
  510. memmove((char *)here + size, here, rest);
  511. memset(here, 0, size);
  512. here->e_name_index = name_index;
  513. here->e_name_len = name_len;
  514. memcpy(here->e_name, name, name_len);
  515. } else {
  516. if (here->e_value_size) {
  517. char *first_val = (char *)header + min_offs;
  518. size_t offs = le16_to_cpu(here->e_value_offs);
  519. char *val = (char *)header + offs;
  520. size_t size = EXT2_XATTR_SIZE(
  521. le32_to_cpu(here->e_value_size));
  522. if (size == EXT2_XATTR_SIZE(value_len)) {
  523. /* The old and the new value have the same
  524. size. Just replace. */
  525. here->e_value_size = cpu_to_le32(value_len);
  526. memset(val + size - EXT2_XATTR_PAD, 0,
  527. EXT2_XATTR_PAD); /* Clear pad bytes. */
  528. memcpy(val, value, value_len);
  529. goto skip_replace;
  530. }
  531. /* Remove the old value. */
  532. memmove(first_val + size, first_val, val - first_val);
  533. memset(first_val, 0, size);
  534. min_offs += size;
  535. /* Adjust all value offsets. */
  536. last = ENTRY(header+1);
  537. while (!IS_LAST_ENTRY(last)) {
  538. size_t o = le16_to_cpu(last->e_value_offs);
  539. if (o < offs)
  540. last->e_value_offs =
  541. cpu_to_le16(o + size);
  542. last = EXT2_XATTR_NEXT(last);
  543. }
  544. here->e_value_offs = 0;
  545. }
  546. if (value == NULL) {
  547. /* Remove the old name. */
  548. size_t size = EXT2_XATTR_LEN(name_len);
  549. last = ENTRY((char *)last - size);
  550. memmove(here, (char*)here + size,
  551. (char*)last - (char*)here);
  552. memset(last, 0, size);
  553. }
  554. }
  555. if (value != NULL) {
  556. /* Insert the new value. */
  557. here->e_value_size = cpu_to_le32(value_len);
  558. if (value_len) {
  559. size_t size = EXT2_XATTR_SIZE(value_len);
  560. char *val = (char *)header + min_offs - size;
  561. here->e_value_offs =
  562. cpu_to_le16((char *)val - (char *)header);
  563. memset(val + size - EXT2_XATTR_PAD, 0,
  564. EXT2_XATTR_PAD); /* Clear the pad bytes. */
  565. memcpy(val, value, value_len);
  566. }
  567. }
  568. skip_replace:
  569. if (IS_LAST_ENTRY(ENTRY(header+1))) {
  570. /* This block is now empty. */
  571. if (bh && header == HDR(bh))
  572. unlock_buffer(bh); /* we were modifying in-place. */
  573. error = ext2_xattr_set2(inode, bh, NULL);
  574. } else {
  575. ext2_xattr_rehash(header, here);
  576. if (bh && header == HDR(bh))
  577. unlock_buffer(bh); /* we were modifying in-place. */
  578. error = ext2_xattr_set2(inode, bh, header);
  579. }
  580. cleanup:
  581. if (!(bh && header == HDR(bh)))
  582. kfree(header);
  583. brelse(bh);
  584. up_write(&EXT2_I(inode)->xattr_sem);
  585. return error;
  586. }
  587. static void ext2_xattr_release_block(struct inode *inode,
  588. struct buffer_head *bh)
  589. {
  590. struct mb_cache *ea_block_cache = EA_BLOCK_CACHE(inode);
  591. retry_ref:
  592. lock_buffer(bh);
  593. if (HDR(bh)->h_refcount == cpu_to_le32(1)) {
  594. __u32 hash = le32_to_cpu(HDR(bh)->h_hash);
  595. struct mb_cache_entry *oe;
  596. /*
  597. * This must happen under buffer lock to properly
  598. * serialize with ext2_xattr_set() reusing the block.
  599. */
  600. oe = mb_cache_entry_delete_or_get(ea_block_cache, hash,
  601. bh->b_blocknr);
  602. if (oe) {
  603. /*
  604. * Someone is trying to reuse the block. Wait
  605. * and retry.
  606. */
  607. unlock_buffer(bh);
  608. mb_cache_entry_wait_unused(oe);
  609. mb_cache_entry_put(ea_block_cache, oe);
  610. goto retry_ref;
  611. }
  612. /* Free the old block. */
  613. ea_bdebug(bh, "freeing");
  614. ext2_free_blocks(inode, bh->b_blocknr, 1);
  615. /* We let our caller release bh, so we
  616. * need to duplicate the buffer before. */
  617. get_bh(bh);
  618. bforget(bh);
  619. unlock_buffer(bh);
  620. } else {
  621. /* Decrement the refcount only. */
  622. le32_add_cpu(&HDR(bh)->h_refcount, -1);
  623. dquot_free_block(inode, 1);
  624. mark_buffer_dirty(bh);
  625. unlock_buffer(bh);
  626. ea_bdebug(bh, "refcount now=%d",
  627. le32_to_cpu(HDR(bh)->h_refcount));
  628. if (IS_SYNC(inode))
  629. sync_dirty_buffer(bh);
  630. }
  631. }
  632. /*
  633. * Second half of ext2_xattr_set(): Update the file system.
  634. */
  635. static int
  636. ext2_xattr_set2(struct inode *inode, struct buffer_head *old_bh,
  637. struct ext2_xattr_header *header)
  638. {
  639. struct super_block *sb = inode->i_sb;
  640. struct buffer_head *new_bh = NULL;
  641. int error;
  642. struct mb_cache *ea_block_cache = EA_BLOCK_CACHE(inode);
  643. if (header) {
  644. new_bh = ext2_xattr_cache_find(inode, header);
  645. if (new_bh) {
  646. /* We found an identical block in the cache. */
  647. if (new_bh == old_bh) {
  648. ea_bdebug(new_bh, "keeping this block");
  649. } else {
  650. /* The old block is released after updating
  651. the inode. */
  652. ea_bdebug(new_bh, "reusing block");
  653. error = dquot_alloc_block(inode, 1);
  654. if (error) {
  655. unlock_buffer(new_bh);
  656. goto cleanup;
  657. }
  658. le32_add_cpu(&HDR(new_bh)->h_refcount, 1);
  659. ea_bdebug(new_bh, "refcount now=%d",
  660. le32_to_cpu(HDR(new_bh)->h_refcount));
  661. }
  662. unlock_buffer(new_bh);
  663. } else if (old_bh && header == HDR(old_bh)) {
  664. /* Keep this block. No need to lock the block as we
  665. don't need to change the reference count. */
  666. new_bh = old_bh;
  667. get_bh(new_bh);
  668. ext2_xattr_cache_insert(ea_block_cache, new_bh);
  669. } else {
  670. /* We need to allocate a new block */
  671. ext2_fsblk_t goal = ext2_group_first_block_no(sb,
  672. EXT2_I(inode)->i_block_group);
  673. unsigned long count = 1;
  674. ext2_fsblk_t block = ext2_new_blocks(inode, goal,
  675. &count, &error,
  676. EXT2_ALLOC_NORESERVE);
  677. if (error)
  678. goto cleanup;
  679. ea_idebug(inode, "creating block %lu", block);
  680. new_bh = sb_getblk(sb, block);
  681. if (unlikely(!new_bh)) {
  682. ext2_free_blocks(inode, block, 1);
  683. mark_inode_dirty(inode);
  684. error = -ENOMEM;
  685. goto cleanup;
  686. }
  687. lock_buffer(new_bh);
  688. memcpy(new_bh->b_data, header, new_bh->b_size);
  689. set_buffer_uptodate(new_bh);
  690. unlock_buffer(new_bh);
  691. ext2_xattr_cache_insert(ea_block_cache, new_bh);
  692. ext2_xattr_update_super_block(sb);
  693. }
  694. mark_buffer_dirty(new_bh);
  695. if (IS_SYNC(inode)) {
  696. sync_dirty_buffer(new_bh);
  697. error = -EIO;
  698. if (buffer_req(new_bh) && !buffer_uptodate(new_bh))
  699. goto cleanup;
  700. }
  701. }
  702. /* Update the inode. */
  703. EXT2_I(inode)->i_file_acl = new_bh ? new_bh->b_blocknr : 0;
  704. inode_set_ctime_current(inode);
  705. if (IS_SYNC(inode)) {
  706. error = sync_inode_metadata(inode, 1);
  707. /* In case sync failed due to ENOSPC the inode was actually
  708. * written (only some dirty data were not) so we just proceed
  709. * as if nothing happened and cleanup the unused block */
  710. if (error && error != -ENOSPC) {
  711. if (new_bh && new_bh != old_bh) {
  712. dquot_free_block_nodirty(inode, 1);
  713. mark_inode_dirty(inode);
  714. }
  715. goto cleanup;
  716. }
  717. } else
  718. mark_inode_dirty(inode);
  719. error = 0;
  720. if (old_bh && old_bh != new_bh) {
  721. /*
  722. * If there was an old block and we are no longer using it,
  723. * release the old block.
  724. */
  725. ext2_xattr_release_block(inode, old_bh);
  726. }
  727. cleanup:
  728. brelse(new_bh);
  729. return error;
  730. }
  731. /*
  732. * ext2_xattr_delete_inode()
  733. *
  734. * Free extended attribute resources associated with this inode. This
  735. * is called immediately before an inode is freed.
  736. */
  737. void
  738. ext2_xattr_delete_inode(struct inode *inode)
  739. {
  740. struct buffer_head *bh = NULL;
  741. struct ext2_sb_info *sbi = EXT2_SB(inode->i_sb);
  742. /*
  743. * We are the only ones holding inode reference. The xattr_sem should
  744. * better be unlocked! We could as well just not acquire xattr_sem at
  745. * all but this makes the code more futureproof. OTOH we need trylock
  746. * here to avoid false-positive warning from lockdep about reclaim
  747. * circular dependency.
  748. */
  749. if (WARN_ON_ONCE(!down_write_trylock(&EXT2_I(inode)->xattr_sem)))
  750. return;
  751. if (!EXT2_I(inode)->i_file_acl)
  752. goto cleanup;
  753. if (!ext2_data_block_valid(sbi, EXT2_I(inode)->i_file_acl, 1)) {
  754. ext2_error(inode->i_sb, "ext2_xattr_delete_inode",
  755. "inode %ld: xattr block %d is out of data blocks range",
  756. inode->i_ino, EXT2_I(inode)->i_file_acl);
  757. goto cleanup;
  758. }
  759. bh = sb_bread(inode->i_sb, EXT2_I(inode)->i_file_acl);
  760. if (!bh) {
  761. ext2_error(inode->i_sb, "ext2_xattr_delete_inode",
  762. "inode %ld: block %d read error", inode->i_ino,
  763. EXT2_I(inode)->i_file_acl);
  764. goto cleanup;
  765. }
  766. ea_bdebug(bh, "b_count=%d", atomic_read(&(bh->b_count)));
  767. if (!ext2_xattr_header_valid(HDR(bh))) {
  768. ext2_error(inode->i_sb, "ext2_xattr_delete_inode",
  769. "inode %ld: bad block %d", inode->i_ino,
  770. EXT2_I(inode)->i_file_acl);
  771. goto cleanup;
  772. }
  773. ext2_xattr_release_block(inode, bh);
  774. EXT2_I(inode)->i_file_acl = 0;
  775. cleanup:
  776. brelse(bh);
  777. up_write(&EXT2_I(inode)->xattr_sem);
  778. }
  779. /*
  780. * ext2_xattr_cache_insert()
  781. *
  782. * Create a new entry in the extended attribute cache, and insert
  783. * it unless such an entry is already in the cache.
  784. *
  785. * Returns 0, or a negative error number on failure.
  786. */
  787. static int
  788. ext2_xattr_cache_insert(struct mb_cache *cache, struct buffer_head *bh)
  789. {
  790. __u32 hash = le32_to_cpu(HDR(bh)->h_hash);
  791. int error;
  792. error = mb_cache_entry_create(cache, GFP_KERNEL, hash, bh->b_blocknr,
  793. true);
  794. if (error) {
  795. if (error == -EBUSY) {
  796. ea_bdebug(bh, "already in cache");
  797. error = 0;
  798. }
  799. } else
  800. ea_bdebug(bh, "inserting [%x]", (int)hash);
  801. return error;
  802. }
  803. /*
  804. * ext2_xattr_cmp()
  805. *
  806. * Compare two extended attribute blocks for equality.
  807. *
  808. * Returns 0 if the blocks are equal, 1 if they differ, and
  809. * a negative error number on errors.
  810. */
  811. static int
  812. ext2_xattr_cmp(struct ext2_xattr_header *header1,
  813. struct ext2_xattr_header *header2)
  814. {
  815. struct ext2_xattr_entry *entry1, *entry2;
  816. entry1 = ENTRY(header1+1);
  817. entry2 = ENTRY(header2+1);
  818. while (!IS_LAST_ENTRY(entry1)) {
  819. if (IS_LAST_ENTRY(entry2))
  820. return 1;
  821. if (entry1->e_hash != entry2->e_hash ||
  822. entry1->e_name_index != entry2->e_name_index ||
  823. entry1->e_name_len != entry2->e_name_len ||
  824. entry1->e_value_size != entry2->e_value_size ||
  825. memcmp(entry1->e_name, entry2->e_name, entry1->e_name_len))
  826. return 1;
  827. if (entry1->e_value_block != 0 || entry2->e_value_block != 0)
  828. return -EIO;
  829. if (memcmp((char *)header1 + le16_to_cpu(entry1->e_value_offs),
  830. (char *)header2 + le16_to_cpu(entry2->e_value_offs),
  831. le32_to_cpu(entry1->e_value_size)))
  832. return 1;
  833. entry1 = EXT2_XATTR_NEXT(entry1);
  834. entry2 = EXT2_XATTR_NEXT(entry2);
  835. }
  836. if (!IS_LAST_ENTRY(entry2))
  837. return 1;
  838. return 0;
  839. }
  840. /*
  841. * ext2_xattr_cache_find()
  842. *
  843. * Find an identical extended attribute block.
  844. *
  845. * Returns a locked buffer head to the block found, or NULL if such
  846. * a block was not found or an error occurred.
  847. */
  848. static struct buffer_head *
  849. ext2_xattr_cache_find(struct inode *inode, struct ext2_xattr_header *header)
  850. {
  851. __u32 hash = le32_to_cpu(header->h_hash);
  852. struct mb_cache_entry *ce;
  853. struct mb_cache *ea_block_cache = EA_BLOCK_CACHE(inode);
  854. if (!header->h_hash)
  855. return NULL; /* never share */
  856. ea_idebug(inode, "looking for cached blocks [%x]", (int)hash);
  857. ce = mb_cache_entry_find_first(ea_block_cache, hash);
  858. while (ce) {
  859. struct buffer_head *bh;
  860. bh = sb_bread(inode->i_sb, ce->e_value);
  861. if (!bh) {
  862. ext2_error(inode->i_sb, "ext2_xattr_cache_find",
  863. "inode %ld: block %ld read error",
  864. inode->i_ino, (unsigned long) ce->e_value);
  865. } else {
  866. lock_buffer(bh);
  867. if (le32_to_cpu(HDR(bh)->h_refcount) >
  868. EXT2_XATTR_REFCOUNT_MAX) {
  869. ea_idebug(inode, "block %ld refcount %d>%d",
  870. (unsigned long) ce->e_value,
  871. le32_to_cpu(HDR(bh)->h_refcount),
  872. EXT2_XATTR_REFCOUNT_MAX);
  873. } else if (!ext2_xattr_cmp(header, HDR(bh))) {
  874. ea_bdebug(bh, "b_count=%d",
  875. atomic_read(&(bh->b_count)));
  876. mb_cache_entry_touch(ea_block_cache, ce);
  877. mb_cache_entry_put(ea_block_cache, ce);
  878. return bh;
  879. }
  880. unlock_buffer(bh);
  881. brelse(bh);
  882. }
  883. ce = mb_cache_entry_find_next(ea_block_cache, ce);
  884. }
  885. return NULL;
  886. }
  887. #define NAME_HASH_SHIFT 5
  888. #define VALUE_HASH_SHIFT 16
  889. /*
  890. * ext2_xattr_hash_entry()
  891. *
  892. * Compute the hash of an extended attribute.
  893. */
  894. static inline void ext2_xattr_hash_entry(struct ext2_xattr_header *header,
  895. struct ext2_xattr_entry *entry)
  896. {
  897. __u32 hash = 0;
  898. char *name = entry->e_name;
  899. int n;
  900. for (n=0; n < entry->e_name_len; n++) {
  901. hash = (hash << NAME_HASH_SHIFT) ^
  902. (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^
  903. *name++;
  904. }
  905. if (entry->e_value_block == 0 && entry->e_value_size != 0) {
  906. __le32 *value = (__le32 *)((char *)header +
  907. le16_to_cpu(entry->e_value_offs));
  908. for (n = (le32_to_cpu(entry->e_value_size) +
  909. EXT2_XATTR_ROUND) >> EXT2_XATTR_PAD_BITS; n; n--) {
  910. hash = (hash << VALUE_HASH_SHIFT) ^
  911. (hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^
  912. le32_to_cpu(*value++);
  913. }
  914. }
  915. entry->e_hash = cpu_to_le32(hash);
  916. }
  917. #undef NAME_HASH_SHIFT
  918. #undef VALUE_HASH_SHIFT
  919. #define BLOCK_HASH_SHIFT 16
  920. /*
  921. * ext2_xattr_rehash()
  922. *
  923. * Re-compute the extended attribute hash value after an entry has changed.
  924. */
  925. static void ext2_xattr_rehash(struct ext2_xattr_header *header,
  926. struct ext2_xattr_entry *entry)
  927. {
  928. struct ext2_xattr_entry *here;
  929. __u32 hash = 0;
  930. ext2_xattr_hash_entry(header, entry);
  931. here = ENTRY(header+1);
  932. while (!IS_LAST_ENTRY(here)) {
  933. if (!here->e_hash) {
  934. /* Block is not shared if an entry's hash value == 0 */
  935. hash = 0;
  936. break;
  937. }
  938. hash = (hash << BLOCK_HASH_SHIFT) ^
  939. (hash >> (8*sizeof(hash) - BLOCK_HASH_SHIFT)) ^
  940. le32_to_cpu(here->e_hash);
  941. here = EXT2_XATTR_NEXT(here);
  942. }
  943. header->h_hash = cpu_to_le32(hash);
  944. }
  945. #undef BLOCK_HASH_SHIFT
  946. #define HASH_BUCKET_BITS 10
  947. struct mb_cache *ext2_xattr_create_cache(void)
  948. {
  949. return mb_cache_create(HASH_BUCKET_BITS);
  950. }
  951. void ext2_xattr_destroy_cache(struct mb_cache *cache)
  952. {
  953. if (cache)
  954. mb_cache_destroy(cache);
  955. }