xattr.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322
  1. /*
  2. * linux/fs/ext3/xattr.c
  3. *
  4. * Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de>
  5. *
  6. * Fix by Harrison Xing <harrison@mountainviewdata.com>.
  7. * Ext3 code with a lot of help from Eric Jarman <ejarman@acm.org>.
  8. * Extended attributes for symlinks and special files added per
  9. * suggestion of Luka Renko <luka.renko@hermes.si>.
  10. * xattr consolidation Copyright (c) 2004 James Morris <jmorris@redhat.com>,
  11. * Red Hat Inc.
  12. * ea-in-inode support by Alex Tomas <alex@clusterfs.com> aka bzzz
  13. * and Andreas Gruenbacher <agruen@suse.de>.
  14. */
  15. /*
  16. * Extended attributes are stored directly in inodes (on file systems with
  17. * inodes bigger than 128 bytes) and on additional disk blocks. The i_file_acl
  18. * field contains the block number if an inode uses an additional block. All
  19. * attributes must fit in the inode and one additional block. Blocks that
  20. * contain the identical set of attributes may be shared among several inodes.
  21. * Identical blocks are detected by keeping a cache of blocks that have
  22. * recently been accessed.
  23. *
  24. * The attributes in inodes and on blocks have a different header; the entries
  25. * are stored in the same format:
  26. *
  27. * +------------------+
  28. * | header |
  29. * | entry 1 | |
  30. * | entry 2 | | growing downwards
  31. * | entry 3 | v
  32. * | four null bytes |
  33. * | . . . |
  34. * | value 1 | ^
  35. * | value 3 | | growing upwards
  36. * | value 2 | |
  37. * +------------------+
  38. *
  39. * The header is followed by multiple entry descriptors. In disk blocks, the
  40. * entry descriptors are kept sorted. In inodes, they are unsorted. The
  41. * attribute values are aligned to the end of the block in no specific order.
  42. *
  43. * Locking strategy
  44. * ----------------
  45. * EXT3_I(inode)->i_file_acl is protected by EXT3_I(inode)->xattr_sem.
  46. * EA blocks are only changed if they are exclusive to an inode, so
  47. * holding xattr_sem also means that nothing but the EA block's reference
  48. * count can change. Multiple writers to the same block are synchronized
  49. * by the buffer lock.
  50. */
  51. #include <linux/init.h>
  52. #include <linux/fs.h>
  53. #include <linux/slab.h>
  54. #include <linux/ext3_jbd.h>
  55. #include <linux/ext3_fs.h>
  56. #include <linux/mbcache.h>
  57. #include <linux/quotaops.h>
  58. #include <linux/rwsem.h>
  59. #include "xattr.h"
  60. #include "acl.h"
  61. #define BHDR(bh) ((struct ext3_xattr_header *)((bh)->b_data))
  62. #define ENTRY(ptr) ((struct ext3_xattr_entry *)(ptr))
  63. #define BFIRST(bh) ENTRY(BHDR(bh)+1)
  64. #define IS_LAST_ENTRY(entry) (*(__u32 *)(entry) == 0)
  65. #define IHDR(inode, raw_inode) \
  66. ((struct ext3_xattr_ibody_header *) \
  67. ((void *)raw_inode + \
  68. EXT3_GOOD_OLD_INODE_SIZE + \
  69. EXT3_I(inode)->i_extra_isize))
  70. #define IFIRST(hdr) ((struct ext3_xattr_entry *)((hdr)+1))
  71. #ifdef EXT3_XATTR_DEBUG
  72. # define ea_idebug(inode, f...) do { \
  73. printk(KERN_DEBUG "inode %s:%lu: ", \
  74. inode->i_sb->s_id, inode->i_ino); \
  75. printk(f); \
  76. printk("\n"); \
  77. } while (0)
  78. # define ea_bdebug(bh, f...) do { \
  79. char b[BDEVNAME_SIZE]; \
  80. printk(KERN_DEBUG "block %s:%lu: ", \
  81. bdevname(bh->b_bdev, b), \
  82. (unsigned long) bh->b_blocknr); \
  83. printk(f); \
  84. printk("\n"); \
  85. } while (0)
  86. #else
  87. # define ea_idebug(f...)
  88. # define ea_bdebug(f...)
  89. #endif
  90. static void ext3_xattr_cache_insert(struct buffer_head *);
  91. static struct buffer_head *ext3_xattr_cache_find(struct inode *,
  92. struct ext3_xattr_header *,
  93. struct mb_cache_entry **);
  94. static void ext3_xattr_rehash(struct ext3_xattr_header *,
  95. struct ext3_xattr_entry *);
  96. static int ext3_xattr_list(struct inode *inode, char *buffer,
  97. size_t buffer_size);
  98. static struct mb_cache *ext3_xattr_cache;
  99. static struct xattr_handler *ext3_xattr_handler_map[] = {
  100. [EXT3_XATTR_INDEX_USER] = &ext3_xattr_user_handler,
  101. #ifdef CONFIG_EXT3_FS_POSIX_ACL
  102. [EXT3_XATTR_INDEX_POSIX_ACL_ACCESS] = &ext3_xattr_acl_access_handler,
  103. [EXT3_XATTR_INDEX_POSIX_ACL_DEFAULT] = &ext3_xattr_acl_default_handler,
  104. #endif
  105. [EXT3_XATTR_INDEX_TRUSTED] = &ext3_xattr_trusted_handler,
  106. #ifdef CONFIG_EXT3_FS_SECURITY
  107. [EXT3_XATTR_INDEX_SECURITY] = &ext3_xattr_security_handler,
  108. #endif
  109. };
  110. struct xattr_handler *ext3_xattr_handlers[] = {
  111. &ext3_xattr_user_handler,
  112. &ext3_xattr_trusted_handler,
  113. #ifdef CONFIG_EXT3_FS_POSIX_ACL
  114. &ext3_xattr_acl_access_handler,
  115. &ext3_xattr_acl_default_handler,
  116. #endif
  117. #ifdef CONFIG_EXT3_FS_SECURITY
  118. &ext3_xattr_security_handler,
  119. #endif
  120. NULL
  121. };
  122. static inline struct xattr_handler *
  123. ext3_xattr_handler(int name_index)
  124. {
  125. struct xattr_handler *handler = NULL;
  126. if (name_index > 0 && name_index < ARRAY_SIZE(ext3_xattr_handler_map))
  127. handler = ext3_xattr_handler_map[name_index];
  128. return handler;
  129. }
  130. /*
  131. * Inode operation listxattr()
  132. *
  133. * dentry->d_inode->i_mutex: don't care
  134. */
  135. ssize_t
  136. ext3_listxattr(struct dentry *dentry, char *buffer, size_t size)
  137. {
  138. return ext3_xattr_list(dentry->d_inode, buffer, size);
  139. }
  140. static int
  141. ext3_xattr_check_names(struct ext3_xattr_entry *entry, void *end)
  142. {
  143. while (!IS_LAST_ENTRY(entry)) {
  144. struct ext3_xattr_entry *next = EXT3_XATTR_NEXT(entry);
  145. if ((void *)next >= end)
  146. return -EIO;
  147. entry = next;
  148. }
  149. return 0;
  150. }
  151. static inline int
  152. ext3_xattr_check_block(struct buffer_head *bh)
  153. {
  154. int error;
  155. if (BHDR(bh)->h_magic != cpu_to_le32(EXT3_XATTR_MAGIC) ||
  156. BHDR(bh)->h_blocks != cpu_to_le32(1))
  157. return -EIO;
  158. error = ext3_xattr_check_names(BFIRST(bh), bh->b_data + bh->b_size);
  159. return error;
  160. }
  161. static inline int
  162. ext3_xattr_check_entry(struct ext3_xattr_entry *entry, size_t size)
  163. {
  164. size_t value_size = le32_to_cpu(entry->e_value_size);
  165. if (entry->e_value_block != 0 || value_size > size ||
  166. le16_to_cpu(entry->e_value_offs) + value_size > size)
  167. return -EIO;
  168. return 0;
  169. }
  170. static int
  171. ext3_xattr_find_entry(struct ext3_xattr_entry **pentry, int name_index,
  172. const char *name, size_t size, int sorted)
  173. {
  174. struct ext3_xattr_entry *entry;
  175. size_t name_len;
  176. int cmp = 1;
  177. if (name == NULL)
  178. return -EINVAL;
  179. name_len = strlen(name);
  180. entry = *pentry;
  181. for (; !IS_LAST_ENTRY(entry); entry = EXT3_XATTR_NEXT(entry)) {
  182. cmp = name_index - entry->e_name_index;
  183. if (!cmp)
  184. cmp = name_len - entry->e_name_len;
  185. if (!cmp)
  186. cmp = memcmp(name, entry->e_name, name_len);
  187. if (cmp <= 0 && (sorted || cmp == 0))
  188. break;
  189. }
  190. *pentry = entry;
  191. if (!cmp && ext3_xattr_check_entry(entry, size))
  192. return -EIO;
  193. return cmp ? -ENODATA : 0;
  194. }
  195. static int
  196. ext3_xattr_block_get(struct inode *inode, int name_index, const char *name,
  197. void *buffer, size_t buffer_size)
  198. {
  199. struct buffer_head *bh = NULL;
  200. struct ext3_xattr_entry *entry;
  201. size_t size;
  202. int error;
  203. ea_idebug(inode, "name=%d.%s, buffer=%p, buffer_size=%ld",
  204. name_index, name, buffer, (long)buffer_size);
  205. error = -ENODATA;
  206. if (!EXT3_I(inode)->i_file_acl)
  207. goto cleanup;
  208. ea_idebug(inode, "reading block %u", EXT3_I(inode)->i_file_acl);
  209. bh = sb_bread(inode->i_sb, EXT3_I(inode)->i_file_acl);
  210. if (!bh)
  211. goto cleanup;
  212. ea_bdebug(bh, "b_count=%d, refcount=%d",
  213. atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
  214. if (ext3_xattr_check_block(bh)) {
  215. bad_block: ext3_error(inode->i_sb, __func__,
  216. "inode %lu: bad block "E3FSBLK, inode->i_ino,
  217. EXT3_I(inode)->i_file_acl);
  218. error = -EIO;
  219. goto cleanup;
  220. }
  221. ext3_xattr_cache_insert(bh);
  222. entry = BFIRST(bh);
  223. error = ext3_xattr_find_entry(&entry, name_index, name, bh->b_size, 1);
  224. if (error == -EIO)
  225. goto bad_block;
  226. if (error)
  227. goto cleanup;
  228. size = le32_to_cpu(entry->e_value_size);
  229. if (buffer) {
  230. error = -ERANGE;
  231. if (size > buffer_size)
  232. goto cleanup;
  233. memcpy(buffer, bh->b_data + le16_to_cpu(entry->e_value_offs),
  234. size);
  235. }
  236. error = size;
  237. cleanup:
  238. brelse(bh);
  239. return error;
  240. }
  241. static int
  242. ext3_xattr_ibody_get(struct inode *inode, int name_index, const char *name,
  243. void *buffer, size_t buffer_size)
  244. {
  245. struct ext3_xattr_ibody_header *header;
  246. struct ext3_xattr_entry *entry;
  247. struct ext3_inode *raw_inode;
  248. struct ext3_iloc iloc;
  249. size_t size;
  250. void *end;
  251. int error;
  252. if (!(EXT3_I(inode)->i_state & EXT3_STATE_XATTR))
  253. return -ENODATA;
  254. error = ext3_get_inode_loc(inode, &iloc);
  255. if (error)
  256. return error;
  257. raw_inode = ext3_raw_inode(&iloc);
  258. header = IHDR(inode, raw_inode);
  259. entry = IFIRST(header);
  260. end = (void *)raw_inode + EXT3_SB(inode->i_sb)->s_inode_size;
  261. error = ext3_xattr_check_names(entry, end);
  262. if (error)
  263. goto cleanup;
  264. error = ext3_xattr_find_entry(&entry, name_index, name,
  265. end - (void *)entry, 0);
  266. if (error)
  267. goto cleanup;
  268. size = le32_to_cpu(entry->e_value_size);
  269. if (buffer) {
  270. error = -ERANGE;
  271. if (size > buffer_size)
  272. goto cleanup;
  273. memcpy(buffer, (void *)IFIRST(header) +
  274. le16_to_cpu(entry->e_value_offs), size);
  275. }
  276. error = size;
  277. cleanup:
  278. brelse(iloc.bh);
  279. return error;
  280. }
  281. /*
  282. * ext3_xattr_get()
  283. *
  284. * Copy an extended attribute into the buffer
  285. * provided, or compute the buffer size required.
  286. * Buffer is NULL to compute the size of the buffer required.
  287. *
  288. * Returns a negative error number on failure, or the number of bytes
  289. * used / required on success.
  290. */
  291. int
  292. ext3_xattr_get(struct inode *inode, int name_index, const char *name,
  293. void *buffer, size_t buffer_size)
  294. {
  295. int error;
  296. down_read(&EXT3_I(inode)->xattr_sem);
  297. error = ext3_xattr_ibody_get(inode, name_index, name, buffer,
  298. buffer_size);
  299. if (error == -ENODATA)
  300. error = ext3_xattr_block_get(inode, name_index, name, buffer,
  301. buffer_size);
  302. up_read(&EXT3_I(inode)->xattr_sem);
  303. return error;
  304. }
  305. static int
  306. ext3_xattr_list_entries(struct inode *inode, struct ext3_xattr_entry *entry,
  307. char *buffer, size_t buffer_size)
  308. {
  309. size_t rest = buffer_size;
  310. for (; !IS_LAST_ENTRY(entry); entry = EXT3_XATTR_NEXT(entry)) {
  311. struct xattr_handler *handler =
  312. ext3_xattr_handler(entry->e_name_index);
  313. if (handler) {
  314. size_t size = handler->list(inode, buffer, rest,
  315. entry->e_name,
  316. entry->e_name_len);
  317. if (buffer) {
  318. if (size > rest)
  319. return -ERANGE;
  320. buffer += size;
  321. }
  322. rest -= size;
  323. }
  324. }
  325. return buffer_size - rest;
  326. }
  327. static int
  328. ext3_xattr_block_list(struct inode *inode, char *buffer, size_t buffer_size)
  329. {
  330. struct buffer_head *bh = NULL;
  331. int error;
  332. ea_idebug(inode, "buffer=%p, buffer_size=%ld",
  333. buffer, (long)buffer_size);
  334. error = 0;
  335. if (!EXT3_I(inode)->i_file_acl)
  336. goto cleanup;
  337. ea_idebug(inode, "reading block %u", EXT3_I(inode)->i_file_acl);
  338. bh = sb_bread(inode->i_sb, EXT3_I(inode)->i_file_acl);
  339. error = -EIO;
  340. if (!bh)
  341. goto cleanup;
  342. ea_bdebug(bh, "b_count=%d, refcount=%d",
  343. atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
  344. if (ext3_xattr_check_block(bh)) {
  345. ext3_error(inode->i_sb, __func__,
  346. "inode %lu: bad block "E3FSBLK, inode->i_ino,
  347. EXT3_I(inode)->i_file_acl);
  348. error = -EIO;
  349. goto cleanup;
  350. }
  351. ext3_xattr_cache_insert(bh);
  352. error = ext3_xattr_list_entries(inode, BFIRST(bh), buffer, buffer_size);
  353. cleanup:
  354. brelse(bh);
  355. return error;
  356. }
  357. static int
  358. ext3_xattr_ibody_list(struct inode *inode, char *buffer, size_t buffer_size)
  359. {
  360. struct ext3_xattr_ibody_header *header;
  361. struct ext3_inode *raw_inode;
  362. struct ext3_iloc iloc;
  363. void *end;
  364. int error;
  365. if (!(EXT3_I(inode)->i_state & EXT3_STATE_XATTR))
  366. return 0;
  367. error = ext3_get_inode_loc(inode, &iloc);
  368. if (error)
  369. return error;
  370. raw_inode = ext3_raw_inode(&iloc);
  371. header = IHDR(inode, raw_inode);
  372. end = (void *)raw_inode + EXT3_SB(inode->i_sb)->s_inode_size;
  373. error = ext3_xattr_check_names(IFIRST(header), end);
  374. if (error)
  375. goto cleanup;
  376. error = ext3_xattr_list_entries(inode, IFIRST(header),
  377. buffer, buffer_size);
  378. cleanup:
  379. brelse(iloc.bh);
  380. return error;
  381. }
  382. /*
  383. * ext3_xattr_list()
  384. *
  385. * Copy a list of attribute names into the buffer
  386. * provided, or compute the buffer size required.
  387. * Buffer is NULL to compute the size of the buffer required.
  388. *
  389. * Returns a negative error number on failure, or the number of bytes
  390. * used / required on success.
  391. */
  392. static int
  393. ext3_xattr_list(struct inode *inode, char *buffer, size_t buffer_size)
  394. {
  395. int i_error, b_error;
  396. down_read(&EXT3_I(inode)->xattr_sem);
  397. i_error = ext3_xattr_ibody_list(inode, buffer, buffer_size);
  398. if (i_error < 0) {
  399. b_error = 0;
  400. } else {
  401. if (buffer) {
  402. buffer += i_error;
  403. buffer_size -= i_error;
  404. }
  405. b_error = ext3_xattr_block_list(inode, buffer, buffer_size);
  406. if (b_error < 0)
  407. i_error = 0;
  408. }
  409. up_read(&EXT3_I(inode)->xattr_sem);
  410. return i_error + b_error;
  411. }
  412. /*
  413. * If the EXT3_FEATURE_COMPAT_EXT_ATTR feature of this file system is
  414. * not set, set it.
  415. */
  416. static void ext3_xattr_update_super_block(handle_t *handle,
  417. struct super_block *sb)
  418. {
  419. if (EXT3_HAS_COMPAT_FEATURE(sb, EXT3_FEATURE_COMPAT_EXT_ATTR))
  420. return;
  421. if (ext3_journal_get_write_access(handle, EXT3_SB(sb)->s_sbh) == 0) {
  422. EXT3_SET_COMPAT_FEATURE(sb, EXT3_FEATURE_COMPAT_EXT_ATTR);
  423. sb->s_dirt = 1;
  424. ext3_journal_dirty_metadata(handle, EXT3_SB(sb)->s_sbh);
  425. }
  426. }
  427. /*
  428. * Release the xattr block BH: If the reference count is > 1, decrement
  429. * it; otherwise free the block.
  430. */
  431. static void
  432. ext3_xattr_release_block(handle_t *handle, struct inode *inode,
  433. struct buffer_head *bh)
  434. {
  435. struct mb_cache_entry *ce = NULL;
  436. int error = 0;
  437. ce = mb_cache_entry_get(ext3_xattr_cache, bh->b_bdev, bh->b_blocknr);
  438. error = ext3_journal_get_write_access(handle, bh);
  439. if (error)
  440. goto out;
  441. lock_buffer(bh);
  442. if (BHDR(bh)->h_refcount == cpu_to_le32(1)) {
  443. ea_bdebug(bh, "refcount now=0; freeing");
  444. if (ce)
  445. mb_cache_entry_free(ce);
  446. ext3_free_blocks(handle, inode, bh->b_blocknr, 1);
  447. get_bh(bh);
  448. ext3_forget(handle, 1, inode, bh, bh->b_blocknr);
  449. } else {
  450. le32_add_cpu(&BHDR(bh)->h_refcount, -1);
  451. error = ext3_journal_dirty_metadata(handle, bh);
  452. if (IS_SYNC(inode))
  453. handle->h_sync = 1;
  454. DQUOT_FREE_BLOCK(inode, 1);
  455. ea_bdebug(bh, "refcount now=%d; releasing",
  456. le32_to_cpu(BHDR(bh)->h_refcount));
  457. if (ce)
  458. mb_cache_entry_release(ce);
  459. }
  460. unlock_buffer(bh);
  461. out:
  462. ext3_std_error(inode->i_sb, error);
  463. return;
  464. }
  465. struct ext3_xattr_info {
  466. int name_index;
  467. const char *name;
  468. const void *value;
  469. size_t value_len;
  470. };
  471. struct ext3_xattr_search {
  472. struct ext3_xattr_entry *first;
  473. void *base;
  474. void *end;
  475. struct ext3_xattr_entry *here;
  476. int not_found;
  477. };
  478. static int
  479. ext3_xattr_set_entry(struct ext3_xattr_info *i, struct ext3_xattr_search *s)
  480. {
  481. struct ext3_xattr_entry *last;
  482. size_t free, min_offs = s->end - s->base, name_len = strlen(i->name);
  483. /* Compute min_offs and last. */
  484. last = s->first;
  485. for (; !IS_LAST_ENTRY(last); last = EXT3_XATTR_NEXT(last)) {
  486. if (!last->e_value_block && last->e_value_size) {
  487. size_t offs = le16_to_cpu(last->e_value_offs);
  488. if (offs < min_offs)
  489. min_offs = offs;
  490. }
  491. }
  492. free = min_offs - ((void *)last - s->base) - sizeof(__u32);
  493. if (!s->not_found) {
  494. if (!s->here->e_value_block && s->here->e_value_size) {
  495. size_t size = le32_to_cpu(s->here->e_value_size);
  496. free += EXT3_XATTR_SIZE(size);
  497. }
  498. free += EXT3_XATTR_LEN(name_len);
  499. }
  500. if (i->value) {
  501. if (free < EXT3_XATTR_SIZE(i->value_len) ||
  502. free < EXT3_XATTR_LEN(name_len) +
  503. EXT3_XATTR_SIZE(i->value_len))
  504. return -ENOSPC;
  505. }
  506. if (i->value && s->not_found) {
  507. /* Insert the new name. */
  508. size_t size = EXT3_XATTR_LEN(name_len);
  509. size_t rest = (void *)last - (void *)s->here + sizeof(__u32);
  510. memmove((void *)s->here + size, s->here, rest);
  511. memset(s->here, 0, size);
  512. s->here->e_name_index = i->name_index;
  513. s->here->e_name_len = name_len;
  514. memcpy(s->here->e_name, i->name, name_len);
  515. } else {
  516. if (!s->here->e_value_block && s->here->e_value_size) {
  517. void *first_val = s->base + min_offs;
  518. size_t offs = le16_to_cpu(s->here->e_value_offs);
  519. void *val = s->base + offs;
  520. size_t size = EXT3_XATTR_SIZE(
  521. le32_to_cpu(s->here->e_value_size));
  522. if (i->value && size == EXT3_XATTR_SIZE(i->value_len)) {
  523. /* The old and the new value have the same
  524. size. Just replace. */
  525. s->here->e_value_size =
  526. cpu_to_le32(i->value_len);
  527. memset(val + size - EXT3_XATTR_PAD, 0,
  528. EXT3_XATTR_PAD); /* Clear pad bytes. */
  529. memcpy(val, i->value, i->value_len);
  530. return 0;
  531. }
  532. /* Remove the old value. */
  533. memmove(first_val + size, first_val, val - first_val);
  534. memset(first_val, 0, size);
  535. s->here->e_value_size = 0;
  536. s->here->e_value_offs = 0;
  537. min_offs += size;
  538. /* Adjust all value offsets. */
  539. last = s->first;
  540. while (!IS_LAST_ENTRY(last)) {
  541. size_t o = le16_to_cpu(last->e_value_offs);
  542. if (!last->e_value_block &&
  543. last->e_value_size && o < offs)
  544. last->e_value_offs =
  545. cpu_to_le16(o + size);
  546. last = EXT3_XATTR_NEXT(last);
  547. }
  548. }
  549. if (!i->value) {
  550. /* Remove the old name. */
  551. size_t size = EXT3_XATTR_LEN(name_len);
  552. last = ENTRY((void *)last - size);
  553. memmove(s->here, (void *)s->here + size,
  554. (void *)last - (void *)s->here + sizeof(__u32));
  555. memset(last, 0, size);
  556. }
  557. }
  558. if (i->value) {
  559. /* Insert the new value. */
  560. s->here->e_value_size = cpu_to_le32(i->value_len);
  561. if (i->value_len) {
  562. size_t size = EXT3_XATTR_SIZE(i->value_len);
  563. void *val = s->base + min_offs - size;
  564. s->here->e_value_offs = cpu_to_le16(min_offs - size);
  565. memset(val + size - EXT3_XATTR_PAD, 0,
  566. EXT3_XATTR_PAD); /* Clear the pad bytes. */
  567. memcpy(val, i->value, i->value_len);
  568. }
  569. }
  570. return 0;
  571. }
  572. struct ext3_xattr_block_find {
  573. struct ext3_xattr_search s;
  574. struct buffer_head *bh;
  575. };
  576. static int
  577. ext3_xattr_block_find(struct inode *inode, struct ext3_xattr_info *i,
  578. struct ext3_xattr_block_find *bs)
  579. {
  580. struct super_block *sb = inode->i_sb;
  581. int error;
  582. ea_idebug(inode, "name=%d.%s, value=%p, value_len=%ld",
  583. i->name_index, i->name, i->value, (long)i->value_len);
  584. if (EXT3_I(inode)->i_file_acl) {
  585. /* The inode already has an extended attribute block. */
  586. bs->bh = sb_bread(sb, EXT3_I(inode)->i_file_acl);
  587. error = -EIO;
  588. if (!bs->bh)
  589. goto cleanup;
  590. ea_bdebug(bs->bh, "b_count=%d, refcount=%d",
  591. atomic_read(&(bs->bh->b_count)),
  592. le32_to_cpu(BHDR(bs->bh)->h_refcount));
  593. if (ext3_xattr_check_block(bs->bh)) {
  594. ext3_error(sb, __func__,
  595. "inode %lu: bad block "E3FSBLK, inode->i_ino,
  596. EXT3_I(inode)->i_file_acl);
  597. error = -EIO;
  598. goto cleanup;
  599. }
  600. /* Find the named attribute. */
  601. bs->s.base = BHDR(bs->bh);
  602. bs->s.first = BFIRST(bs->bh);
  603. bs->s.end = bs->bh->b_data + bs->bh->b_size;
  604. bs->s.here = bs->s.first;
  605. error = ext3_xattr_find_entry(&bs->s.here, i->name_index,
  606. i->name, bs->bh->b_size, 1);
  607. if (error && error != -ENODATA)
  608. goto cleanup;
  609. bs->s.not_found = error;
  610. }
  611. error = 0;
  612. cleanup:
  613. return error;
  614. }
  615. static int
  616. ext3_xattr_block_set(handle_t *handle, struct inode *inode,
  617. struct ext3_xattr_info *i,
  618. struct ext3_xattr_block_find *bs)
  619. {
  620. struct super_block *sb = inode->i_sb;
  621. struct buffer_head *new_bh = NULL;
  622. struct ext3_xattr_search *s = &bs->s;
  623. struct mb_cache_entry *ce = NULL;
  624. int error = 0;
  625. #define header(x) ((struct ext3_xattr_header *)(x))
  626. if (i->value && i->value_len > sb->s_blocksize)
  627. return -ENOSPC;
  628. if (s->base) {
  629. ce = mb_cache_entry_get(ext3_xattr_cache, bs->bh->b_bdev,
  630. bs->bh->b_blocknr);
  631. error = ext3_journal_get_write_access(handle, bs->bh);
  632. if (error)
  633. goto cleanup;
  634. lock_buffer(bs->bh);
  635. if (header(s->base)->h_refcount == cpu_to_le32(1)) {
  636. if (ce) {
  637. mb_cache_entry_free(ce);
  638. ce = NULL;
  639. }
  640. ea_bdebug(bs->bh, "modifying in-place");
  641. error = ext3_xattr_set_entry(i, s);
  642. if (!error) {
  643. if (!IS_LAST_ENTRY(s->first))
  644. ext3_xattr_rehash(header(s->base),
  645. s->here);
  646. ext3_xattr_cache_insert(bs->bh);
  647. }
  648. unlock_buffer(bs->bh);
  649. if (error == -EIO)
  650. goto bad_block;
  651. if (!error)
  652. error = ext3_journal_dirty_metadata(handle,
  653. bs->bh);
  654. if (error)
  655. goto cleanup;
  656. goto inserted;
  657. } else {
  658. int offset = (char *)s->here - bs->bh->b_data;
  659. unlock_buffer(bs->bh);
  660. journal_release_buffer(handle, bs->bh);
  661. if (ce) {
  662. mb_cache_entry_release(ce);
  663. ce = NULL;
  664. }
  665. ea_bdebug(bs->bh, "cloning");
  666. s->base = kmalloc(bs->bh->b_size, GFP_NOFS);
  667. error = -ENOMEM;
  668. if (s->base == NULL)
  669. goto cleanup;
  670. memcpy(s->base, BHDR(bs->bh), bs->bh->b_size);
  671. s->first = ENTRY(header(s->base)+1);
  672. header(s->base)->h_refcount = cpu_to_le32(1);
  673. s->here = ENTRY(s->base + offset);
  674. s->end = s->base + bs->bh->b_size;
  675. }
  676. } else {
  677. /* Allocate a buffer where we construct the new block. */
  678. s->base = kzalloc(sb->s_blocksize, GFP_NOFS);
  679. /* assert(header == s->base) */
  680. error = -ENOMEM;
  681. if (s->base == NULL)
  682. goto cleanup;
  683. header(s->base)->h_magic = cpu_to_le32(EXT3_XATTR_MAGIC);
  684. header(s->base)->h_blocks = cpu_to_le32(1);
  685. header(s->base)->h_refcount = cpu_to_le32(1);
  686. s->first = ENTRY(header(s->base)+1);
  687. s->here = ENTRY(header(s->base)+1);
  688. s->end = s->base + sb->s_blocksize;
  689. }
  690. error = ext3_xattr_set_entry(i, s);
  691. if (error == -EIO)
  692. goto bad_block;
  693. if (error)
  694. goto cleanup;
  695. if (!IS_LAST_ENTRY(s->first))
  696. ext3_xattr_rehash(header(s->base), s->here);
  697. inserted:
  698. if (!IS_LAST_ENTRY(s->first)) {
  699. new_bh = ext3_xattr_cache_find(inode, header(s->base), &ce);
  700. if (new_bh) {
  701. /* We found an identical block in the cache. */
  702. if (new_bh == bs->bh)
  703. ea_bdebug(new_bh, "keeping");
  704. else {
  705. /* The old block is released after updating
  706. the inode. */
  707. error = -EDQUOT;
  708. if (DQUOT_ALLOC_BLOCK(inode, 1))
  709. goto cleanup;
  710. error = ext3_journal_get_write_access(handle,
  711. new_bh);
  712. if (error)
  713. goto cleanup_dquot;
  714. lock_buffer(new_bh);
  715. le32_add_cpu(&BHDR(new_bh)->h_refcount, 1);
  716. ea_bdebug(new_bh, "reusing; refcount now=%d",
  717. le32_to_cpu(BHDR(new_bh)->h_refcount));
  718. unlock_buffer(new_bh);
  719. error = ext3_journal_dirty_metadata(handle,
  720. new_bh);
  721. if (error)
  722. goto cleanup_dquot;
  723. }
  724. mb_cache_entry_release(ce);
  725. ce = NULL;
  726. } else if (bs->bh && s->base == bs->bh->b_data) {
  727. /* We were modifying this block in-place. */
  728. ea_bdebug(bs->bh, "keeping this block");
  729. new_bh = bs->bh;
  730. get_bh(new_bh);
  731. } else {
  732. /* We need to allocate a new block */
  733. ext3_fsblk_t goal = ext3_group_first_block_no(sb,
  734. EXT3_I(inode)->i_block_group);
  735. ext3_fsblk_t block = ext3_new_block(handle, inode,
  736. goal, &error);
  737. if (error)
  738. goto cleanup;
  739. ea_idebug(inode, "creating block %d", block);
  740. new_bh = sb_getblk(sb, block);
  741. if (!new_bh) {
  742. getblk_failed:
  743. ext3_free_blocks(handle, inode, block, 1);
  744. error = -EIO;
  745. goto cleanup;
  746. }
  747. lock_buffer(new_bh);
  748. error = ext3_journal_get_create_access(handle, new_bh);
  749. if (error) {
  750. unlock_buffer(new_bh);
  751. goto getblk_failed;
  752. }
  753. memcpy(new_bh->b_data, s->base, new_bh->b_size);
  754. set_buffer_uptodate(new_bh);
  755. unlock_buffer(new_bh);
  756. ext3_xattr_cache_insert(new_bh);
  757. error = ext3_journal_dirty_metadata(handle, new_bh);
  758. if (error)
  759. goto cleanup;
  760. }
  761. }
  762. /* Update the inode. */
  763. EXT3_I(inode)->i_file_acl = new_bh ? new_bh->b_blocknr : 0;
  764. /* Drop the previous xattr block. */
  765. if (bs->bh && bs->bh != new_bh)
  766. ext3_xattr_release_block(handle, inode, bs->bh);
  767. error = 0;
  768. cleanup:
  769. if (ce)
  770. mb_cache_entry_release(ce);
  771. brelse(new_bh);
  772. if (!(bs->bh && s->base == bs->bh->b_data))
  773. kfree(s->base);
  774. return error;
  775. cleanup_dquot:
  776. DQUOT_FREE_BLOCK(inode, 1);
  777. goto cleanup;
  778. bad_block:
  779. ext3_error(inode->i_sb, __func__,
  780. "inode %lu: bad block "E3FSBLK, inode->i_ino,
  781. EXT3_I(inode)->i_file_acl);
  782. goto cleanup;
  783. #undef header
  784. }
  785. struct ext3_xattr_ibody_find {
  786. struct ext3_xattr_search s;
  787. struct ext3_iloc iloc;
  788. };
  789. static int
  790. ext3_xattr_ibody_find(struct inode *inode, struct ext3_xattr_info *i,
  791. struct ext3_xattr_ibody_find *is)
  792. {
  793. struct ext3_xattr_ibody_header *header;
  794. struct ext3_inode *raw_inode;
  795. int error;
  796. if (EXT3_I(inode)->i_extra_isize == 0)
  797. return 0;
  798. raw_inode = ext3_raw_inode(&is->iloc);
  799. header = IHDR(inode, raw_inode);
  800. is->s.base = is->s.first = IFIRST(header);
  801. is->s.here = is->s.first;
  802. is->s.end = (void *)raw_inode + EXT3_SB(inode->i_sb)->s_inode_size;
  803. if (EXT3_I(inode)->i_state & EXT3_STATE_XATTR) {
  804. error = ext3_xattr_check_names(IFIRST(header), is->s.end);
  805. if (error)
  806. return error;
  807. /* Find the named attribute. */
  808. error = ext3_xattr_find_entry(&is->s.here, i->name_index,
  809. i->name, is->s.end -
  810. (void *)is->s.base, 0);
  811. if (error && error != -ENODATA)
  812. return error;
  813. is->s.not_found = error;
  814. }
  815. return 0;
  816. }
  817. static int
  818. ext3_xattr_ibody_set(handle_t *handle, struct inode *inode,
  819. struct ext3_xattr_info *i,
  820. struct ext3_xattr_ibody_find *is)
  821. {
  822. struct ext3_xattr_ibody_header *header;
  823. struct ext3_xattr_search *s = &is->s;
  824. int error;
  825. if (EXT3_I(inode)->i_extra_isize == 0)
  826. return -ENOSPC;
  827. error = ext3_xattr_set_entry(i, s);
  828. if (error)
  829. return error;
  830. header = IHDR(inode, ext3_raw_inode(&is->iloc));
  831. if (!IS_LAST_ENTRY(s->first)) {
  832. header->h_magic = cpu_to_le32(EXT3_XATTR_MAGIC);
  833. EXT3_I(inode)->i_state |= EXT3_STATE_XATTR;
  834. } else {
  835. header->h_magic = cpu_to_le32(0);
  836. EXT3_I(inode)->i_state &= ~EXT3_STATE_XATTR;
  837. }
  838. return 0;
  839. }
  840. /*
  841. * ext3_xattr_set_handle()
  842. *
  843. * Create, replace or remove an extended attribute for this inode. Buffer
  844. * is NULL to remove an existing extended attribute, and non-NULL to
  845. * either replace an existing extended attribute, or create a new extended
  846. * attribute. The flags XATTR_REPLACE and XATTR_CREATE
  847. * specify that an extended attribute must exist and must not exist
  848. * previous to the call, respectively.
  849. *
  850. * Returns 0, or a negative error number on failure.
  851. */
  852. int
  853. ext3_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index,
  854. const char *name, const void *value, size_t value_len,
  855. int flags)
  856. {
  857. struct ext3_xattr_info i = {
  858. .name_index = name_index,
  859. .name = name,
  860. .value = value,
  861. .value_len = value_len,
  862. };
  863. struct ext3_xattr_ibody_find is = {
  864. .s = { .not_found = -ENODATA, },
  865. };
  866. struct ext3_xattr_block_find bs = {
  867. .s = { .not_found = -ENODATA, },
  868. };
  869. int error;
  870. if (!name)
  871. return -EINVAL;
  872. if (strlen(name) > 255)
  873. return -ERANGE;
  874. down_write(&EXT3_I(inode)->xattr_sem);
  875. error = ext3_get_inode_loc(inode, &is.iloc);
  876. if (error)
  877. goto cleanup;
  878. if (EXT3_I(inode)->i_state & EXT3_STATE_NEW) {
  879. struct ext3_inode *raw_inode = ext3_raw_inode(&is.iloc);
  880. memset(raw_inode, 0, EXT3_SB(inode->i_sb)->s_inode_size);
  881. EXT3_I(inode)->i_state &= ~EXT3_STATE_NEW;
  882. }
  883. error = ext3_xattr_ibody_find(inode, &i, &is);
  884. if (error)
  885. goto cleanup;
  886. if (is.s.not_found)
  887. error = ext3_xattr_block_find(inode, &i, &bs);
  888. if (error)
  889. goto cleanup;
  890. if (is.s.not_found && bs.s.not_found) {
  891. error = -ENODATA;
  892. if (flags & XATTR_REPLACE)
  893. goto cleanup;
  894. error = 0;
  895. if (!value)
  896. goto cleanup;
  897. } else {
  898. error = -EEXIST;
  899. if (flags & XATTR_CREATE)
  900. goto cleanup;
  901. }
  902. error = ext3_journal_get_write_access(handle, is.iloc.bh);
  903. if (error)
  904. goto cleanup;
  905. if (!value) {
  906. if (!is.s.not_found)
  907. error = ext3_xattr_ibody_set(handle, inode, &i, &is);
  908. else if (!bs.s.not_found)
  909. error = ext3_xattr_block_set(handle, inode, &i, &bs);
  910. } else {
  911. error = ext3_xattr_ibody_set(handle, inode, &i, &is);
  912. if (!error && !bs.s.not_found) {
  913. i.value = NULL;
  914. error = ext3_xattr_block_set(handle, inode, &i, &bs);
  915. } else if (error == -ENOSPC) {
  916. error = ext3_xattr_block_set(handle, inode, &i, &bs);
  917. if (error)
  918. goto cleanup;
  919. if (!is.s.not_found) {
  920. i.value = NULL;
  921. error = ext3_xattr_ibody_set(handle, inode, &i,
  922. &is);
  923. }
  924. }
  925. }
  926. if (!error) {
  927. ext3_xattr_update_super_block(handle, inode->i_sb);
  928. inode->i_ctime = CURRENT_TIME_SEC;
  929. error = ext3_mark_iloc_dirty(handle, inode, &is.iloc);
  930. /*
  931. * The bh is consumed by ext3_mark_iloc_dirty, even with
  932. * error != 0.
  933. */
  934. is.iloc.bh = NULL;
  935. if (IS_SYNC(inode))
  936. handle->h_sync = 1;
  937. }
  938. cleanup:
  939. brelse(is.iloc.bh);
  940. brelse(bs.bh);
  941. up_write(&EXT3_I(inode)->xattr_sem);
  942. return error;
  943. }
  944. /*
  945. * ext3_xattr_set()
  946. *
  947. * Like ext3_xattr_set_handle, but start from an inode. This extended
  948. * attribute modification is a filesystem transaction by itself.
  949. *
  950. * Returns 0, or a negative error number on failure.
  951. */
  952. int
  953. ext3_xattr_set(struct inode *inode, int name_index, const char *name,
  954. const void *value, size_t value_len, int flags)
  955. {
  956. handle_t *handle;
  957. int error, retries = 0;
  958. retry:
  959. handle = ext3_journal_start(inode, EXT3_DATA_TRANS_BLOCKS(inode->i_sb));
  960. if (IS_ERR(handle)) {
  961. error = PTR_ERR(handle);
  962. } else {
  963. int error2;
  964. error = ext3_xattr_set_handle(handle, inode, name_index, name,
  965. value, value_len, flags);
  966. error2 = ext3_journal_stop(handle);
  967. if (error == -ENOSPC &&
  968. ext3_should_retry_alloc(inode->i_sb, &retries))
  969. goto retry;
  970. if (error == 0)
  971. error = error2;
  972. }
  973. return error;
  974. }
  975. /*
  976. * ext3_xattr_delete_inode()
  977. *
  978. * Free extended attribute resources associated with this inode. This
  979. * is called immediately before an inode is freed. We have exclusive
  980. * access to the inode.
  981. */
  982. void
  983. ext3_xattr_delete_inode(handle_t *handle, struct inode *inode)
  984. {
  985. struct buffer_head *bh = NULL;
  986. if (!EXT3_I(inode)->i_file_acl)
  987. goto cleanup;
  988. bh = sb_bread(inode->i_sb, EXT3_I(inode)->i_file_acl);
  989. if (!bh) {
  990. ext3_error(inode->i_sb, __func__,
  991. "inode %lu: block "E3FSBLK" read error", inode->i_ino,
  992. EXT3_I(inode)->i_file_acl);
  993. goto cleanup;
  994. }
  995. if (BHDR(bh)->h_magic != cpu_to_le32(EXT3_XATTR_MAGIC) ||
  996. BHDR(bh)->h_blocks != cpu_to_le32(1)) {
  997. ext3_error(inode->i_sb, __func__,
  998. "inode %lu: bad block "E3FSBLK, inode->i_ino,
  999. EXT3_I(inode)->i_file_acl);
  1000. goto cleanup;
  1001. }
  1002. ext3_xattr_release_block(handle, inode, bh);
  1003. EXT3_I(inode)->i_file_acl = 0;
  1004. cleanup:
  1005. brelse(bh);
  1006. }
  1007. /*
  1008. * ext3_xattr_put_super()
  1009. *
  1010. * This is called when a file system is unmounted.
  1011. */
  1012. void
  1013. ext3_xattr_put_super(struct super_block *sb)
  1014. {
  1015. mb_cache_shrink(sb->s_bdev);
  1016. }
  1017. /*
  1018. * ext3_xattr_cache_insert()
  1019. *
  1020. * Create a new entry in the extended attribute cache, and insert
  1021. * it unless such an entry is already in the cache.
  1022. *
  1023. * Returns 0, or a negative error number on failure.
  1024. */
  1025. static void
  1026. ext3_xattr_cache_insert(struct buffer_head *bh)
  1027. {
  1028. __u32 hash = le32_to_cpu(BHDR(bh)->h_hash);
  1029. struct mb_cache_entry *ce;
  1030. int error;
  1031. ce = mb_cache_entry_alloc(ext3_xattr_cache, GFP_NOFS);
  1032. if (!ce) {
  1033. ea_bdebug(bh, "out of memory");
  1034. return;
  1035. }
  1036. error = mb_cache_entry_insert(ce, bh->b_bdev, bh->b_blocknr, &hash);
  1037. if (error) {
  1038. mb_cache_entry_free(ce);
  1039. if (error == -EBUSY) {
  1040. ea_bdebug(bh, "already in cache");
  1041. error = 0;
  1042. }
  1043. } else {
  1044. ea_bdebug(bh, "inserting [%x]", (int)hash);
  1045. mb_cache_entry_release(ce);
  1046. }
  1047. }
  1048. /*
  1049. * ext3_xattr_cmp()
  1050. *
  1051. * Compare two extended attribute blocks for equality.
  1052. *
  1053. * Returns 0 if the blocks are equal, 1 if they differ, and
  1054. * a negative error number on errors.
  1055. */
  1056. static int
  1057. ext3_xattr_cmp(struct ext3_xattr_header *header1,
  1058. struct ext3_xattr_header *header2)
  1059. {
  1060. struct ext3_xattr_entry *entry1, *entry2;
  1061. entry1 = ENTRY(header1+1);
  1062. entry2 = ENTRY(header2+1);
  1063. while (!IS_LAST_ENTRY(entry1)) {
  1064. if (IS_LAST_ENTRY(entry2))
  1065. return 1;
  1066. if (entry1->e_hash != entry2->e_hash ||
  1067. entry1->e_name_index != entry2->e_name_index ||
  1068. entry1->e_name_len != entry2->e_name_len ||
  1069. entry1->e_value_size != entry2->e_value_size ||
  1070. memcmp(entry1->e_name, entry2->e_name, entry1->e_name_len))
  1071. return 1;
  1072. if (entry1->e_value_block != 0 || entry2->e_value_block != 0)
  1073. return -EIO;
  1074. if (memcmp((char *)header1 + le16_to_cpu(entry1->e_value_offs),
  1075. (char *)header2 + le16_to_cpu(entry2->e_value_offs),
  1076. le32_to_cpu(entry1->e_value_size)))
  1077. return 1;
  1078. entry1 = EXT3_XATTR_NEXT(entry1);
  1079. entry2 = EXT3_XATTR_NEXT(entry2);
  1080. }
  1081. if (!IS_LAST_ENTRY(entry2))
  1082. return 1;
  1083. return 0;
  1084. }
  1085. /*
  1086. * ext3_xattr_cache_find()
  1087. *
  1088. * Find an identical extended attribute block.
  1089. *
  1090. * Returns a pointer to the block found, or NULL if such a block was
  1091. * not found or an error occurred.
  1092. */
  1093. static struct buffer_head *
  1094. ext3_xattr_cache_find(struct inode *inode, struct ext3_xattr_header *header,
  1095. struct mb_cache_entry **pce)
  1096. {
  1097. __u32 hash = le32_to_cpu(header->h_hash);
  1098. struct mb_cache_entry *ce;
  1099. if (!header->h_hash)
  1100. return NULL; /* never share */
  1101. ea_idebug(inode, "looking for cached blocks [%x]", (int)hash);
  1102. again:
  1103. ce = mb_cache_entry_find_first(ext3_xattr_cache, 0,
  1104. inode->i_sb->s_bdev, hash);
  1105. while (ce) {
  1106. struct buffer_head *bh;
  1107. if (IS_ERR(ce)) {
  1108. if (PTR_ERR(ce) == -EAGAIN)
  1109. goto again;
  1110. break;
  1111. }
  1112. bh = sb_bread(inode->i_sb, ce->e_block);
  1113. if (!bh) {
  1114. ext3_error(inode->i_sb, __func__,
  1115. "inode %lu: block %lu read error",
  1116. inode->i_ino, (unsigned long) ce->e_block);
  1117. } else if (le32_to_cpu(BHDR(bh)->h_refcount) >=
  1118. EXT3_XATTR_REFCOUNT_MAX) {
  1119. ea_idebug(inode, "block %lu refcount %d>=%d",
  1120. (unsigned long) ce->e_block,
  1121. le32_to_cpu(BHDR(bh)->h_refcount),
  1122. EXT3_XATTR_REFCOUNT_MAX);
  1123. } else if (ext3_xattr_cmp(header, BHDR(bh)) == 0) {
  1124. *pce = ce;
  1125. return bh;
  1126. }
  1127. brelse(bh);
  1128. ce = mb_cache_entry_find_next(ce, 0, inode->i_sb->s_bdev, hash);
  1129. }
  1130. return NULL;
  1131. }
  1132. #define NAME_HASH_SHIFT 5
  1133. #define VALUE_HASH_SHIFT 16
  1134. /*
  1135. * ext3_xattr_hash_entry()
  1136. *
  1137. * Compute the hash of an extended attribute.
  1138. */
  1139. static inline void ext3_xattr_hash_entry(struct ext3_xattr_header *header,
  1140. struct ext3_xattr_entry *entry)
  1141. {
  1142. __u32 hash = 0;
  1143. char *name = entry->e_name;
  1144. int n;
  1145. for (n=0; n < entry->e_name_len; n++) {
  1146. hash = (hash << NAME_HASH_SHIFT) ^
  1147. (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^
  1148. *name++;
  1149. }
  1150. if (entry->e_value_block == 0 && entry->e_value_size != 0) {
  1151. __le32 *value = (__le32 *)((char *)header +
  1152. le16_to_cpu(entry->e_value_offs));
  1153. for (n = (le32_to_cpu(entry->e_value_size) +
  1154. EXT3_XATTR_ROUND) >> EXT3_XATTR_PAD_BITS; n; n--) {
  1155. hash = (hash << VALUE_HASH_SHIFT) ^
  1156. (hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^
  1157. le32_to_cpu(*value++);
  1158. }
  1159. }
  1160. entry->e_hash = cpu_to_le32(hash);
  1161. }
  1162. #undef NAME_HASH_SHIFT
  1163. #undef VALUE_HASH_SHIFT
  1164. #define BLOCK_HASH_SHIFT 16
  1165. /*
  1166. * ext3_xattr_rehash()
  1167. *
  1168. * Re-compute the extended attribute hash value after an entry has changed.
  1169. */
  1170. static void ext3_xattr_rehash(struct ext3_xattr_header *header,
  1171. struct ext3_xattr_entry *entry)
  1172. {
  1173. struct ext3_xattr_entry *here;
  1174. __u32 hash = 0;
  1175. ext3_xattr_hash_entry(header, entry);
  1176. here = ENTRY(header+1);
  1177. while (!IS_LAST_ENTRY(here)) {
  1178. if (!here->e_hash) {
  1179. /* Block is not shared if an entry's hash value == 0 */
  1180. hash = 0;
  1181. break;
  1182. }
  1183. hash = (hash << BLOCK_HASH_SHIFT) ^
  1184. (hash >> (8*sizeof(hash) - BLOCK_HASH_SHIFT)) ^
  1185. le32_to_cpu(here->e_hash);
  1186. here = EXT3_XATTR_NEXT(here);
  1187. }
  1188. header->h_hash = cpu_to_le32(hash);
  1189. }
  1190. #undef BLOCK_HASH_SHIFT
  1191. int __init
  1192. init_ext3_xattr(void)
  1193. {
  1194. ext3_xattr_cache = mb_cache_create("ext3_xattr", NULL,
  1195. sizeof(struct mb_cache_entry) +
  1196. sizeof(((struct mb_cache_entry *) 0)->e_indexes[0]), 1, 6);
  1197. if (!ext3_xattr_cache)
  1198. return -ENOMEM;
  1199. return 0;
  1200. }
  1201. void
  1202. exit_ext3_xattr(void)
  1203. {
  1204. if (ext3_xattr_cache)
  1205. mb_cache_destroy(ext3_xattr_cache);
  1206. ext3_xattr_cache = NULL;
  1207. }