xattr.c 29 KB

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