xattr.c 29 KB

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