xattr.c 29 KB

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