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 struct mb_cache *ext3_xattr_cache;
  97. static struct xattr_handler *ext3_xattr_handler_map[] = {
  98. [EXT3_XATTR_INDEX_USER] = &ext3_xattr_user_handler,
  99. #ifdef CONFIG_EXT3_FS_POSIX_ACL
  100. [EXT3_XATTR_INDEX_POSIX_ACL_ACCESS] = &ext3_xattr_acl_access_handler,
  101. [EXT3_XATTR_INDEX_POSIX_ACL_DEFAULT] = &ext3_xattr_acl_default_handler,
  102. #endif
  103. [EXT3_XATTR_INDEX_TRUSTED] = &ext3_xattr_trusted_handler,
  104. #ifdef CONFIG_EXT3_FS_SECURITY
  105. [EXT3_XATTR_INDEX_SECURITY] = &ext3_xattr_security_handler,
  106. #endif
  107. };
  108. struct xattr_handler *ext3_xattr_handlers[] = {
  109. &ext3_xattr_user_handler,
  110. &ext3_xattr_trusted_handler,
  111. #ifdef CONFIG_EXT3_FS_POSIX_ACL
  112. &ext3_xattr_acl_access_handler,
  113. &ext3_xattr_acl_default_handler,
  114. #endif
  115. #ifdef CONFIG_EXT3_FS_SECURITY
  116. &ext3_xattr_security_handler,
  117. #endif
  118. NULL
  119. };
  120. static inline struct xattr_handler *
  121. ext3_xattr_handler(int name_index)
  122. {
  123. struct xattr_handler *handler = NULL;
  124. if (name_index > 0 && name_index < ARRAY_SIZE(ext3_xattr_handler_map))
  125. handler = ext3_xattr_handler_map[name_index];
  126. return handler;
  127. }
  128. /*
  129. * Inode operation listxattr()
  130. *
  131. * dentry->d_inode->i_mutex: don't care
  132. */
  133. ssize_t
  134. ext3_listxattr(struct dentry *dentry, char *buffer, size_t size)
  135. {
  136. return ext3_xattr_list(dentry->d_inode, buffer, size);
  137. }
  138. static int
  139. ext3_xattr_check_names(struct ext3_xattr_entry *entry, void *end)
  140. {
  141. while (!IS_LAST_ENTRY(entry)) {
  142. struct ext3_xattr_entry *next = EXT3_XATTR_NEXT(entry);
  143. if ((void *)next >= end)
  144. return -EIO;
  145. entry = next;
  146. }
  147. return 0;
  148. }
  149. static inline int
  150. ext3_xattr_check_block(struct buffer_head *bh)
  151. {
  152. int error;
  153. if (BHDR(bh)->h_magic != cpu_to_le32(EXT3_XATTR_MAGIC) ||
  154. BHDR(bh)->h_blocks != cpu_to_le32(1))
  155. return -EIO;
  156. error = ext3_xattr_check_names(BFIRST(bh), bh->b_data + bh->b_size);
  157. return error;
  158. }
  159. static inline int
  160. ext3_xattr_check_entry(struct ext3_xattr_entry *entry, size_t size)
  161. {
  162. size_t value_size = le32_to_cpu(entry->e_value_size);
  163. if (entry->e_value_block != 0 || value_size > size ||
  164. le16_to_cpu(entry->e_value_offs) + value_size > size)
  165. return -EIO;
  166. return 0;
  167. }
  168. static int
  169. ext3_xattr_find_entry(struct ext3_xattr_entry **pentry, int name_index,
  170. const char *name, size_t size, int sorted)
  171. {
  172. struct ext3_xattr_entry *entry;
  173. size_t name_len;
  174. int cmp = 1;
  175. if (name == NULL)
  176. return -EINVAL;
  177. name_len = strlen(name);
  178. entry = *pentry;
  179. for (; !IS_LAST_ENTRY(entry); entry = EXT3_XATTR_NEXT(entry)) {
  180. cmp = name_index - entry->e_name_index;
  181. if (!cmp)
  182. cmp = name_len - entry->e_name_len;
  183. if (!cmp)
  184. cmp = memcmp(name, entry->e_name, name_len);
  185. if (cmp <= 0 && (sorted || cmp == 0))
  186. break;
  187. }
  188. *pentry = entry;
  189. if (!cmp && ext3_xattr_check_entry(entry, size))
  190. return -EIO;
  191. return cmp ? -ENODATA : 0;
  192. }
  193. static int
  194. ext3_xattr_block_get(struct inode *inode, int name_index, const char *name,
  195. void *buffer, size_t buffer_size)
  196. {
  197. struct buffer_head *bh = NULL;
  198. struct ext3_xattr_entry *entry;
  199. size_t size;
  200. int error;
  201. ea_idebug(inode, "name=%d.%s, buffer=%p, buffer_size=%ld",
  202. name_index, name, buffer, (long)buffer_size);
  203. error = -ENODATA;
  204. if (!EXT3_I(inode)->i_file_acl)
  205. goto cleanup;
  206. ea_idebug(inode, "reading block %u", EXT3_I(inode)->i_file_acl);
  207. bh = sb_bread(inode->i_sb, EXT3_I(inode)->i_file_acl);
  208. if (!bh)
  209. goto cleanup;
  210. ea_bdebug(bh, "b_count=%d, refcount=%d",
  211. atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
  212. if (ext3_xattr_check_block(bh)) {
  213. bad_block: ext3_error(inode->i_sb, __FUNCTION__,
  214. "inode %lu: bad block "E3FSBLK, inode->i_ino,
  215. EXT3_I(inode)->i_file_acl);
  216. error = -EIO;
  217. goto cleanup;
  218. }
  219. ext3_xattr_cache_insert(bh);
  220. entry = BFIRST(bh);
  221. error = ext3_xattr_find_entry(&entry, name_index, name, bh->b_size, 1);
  222. if (error == -EIO)
  223. goto bad_block;
  224. if (error)
  225. goto cleanup;
  226. size = le32_to_cpu(entry->e_value_size);
  227. if (buffer) {
  228. error = -ERANGE;
  229. if (size > buffer_size)
  230. goto cleanup;
  231. memcpy(buffer, bh->b_data + le16_to_cpu(entry->e_value_offs),
  232. size);
  233. }
  234. error = size;
  235. cleanup:
  236. brelse(bh);
  237. return error;
  238. }
  239. static int
  240. ext3_xattr_ibody_get(struct inode *inode, int name_index, const char *name,
  241. void *buffer, size_t buffer_size)
  242. {
  243. struct ext3_xattr_ibody_header *header;
  244. struct ext3_xattr_entry *entry;
  245. struct ext3_inode *raw_inode;
  246. struct ext3_iloc iloc;
  247. size_t size;
  248. void *end;
  249. int error;
  250. if (!(EXT3_I(inode)->i_state & EXT3_STATE_XATTR))
  251. return -ENODATA;
  252. error = ext3_get_inode_loc(inode, &iloc);
  253. if (error)
  254. return error;
  255. raw_inode = ext3_raw_inode(&iloc);
  256. header = IHDR(inode, raw_inode);
  257. entry = IFIRST(header);
  258. end = (void *)raw_inode + EXT3_SB(inode->i_sb)->s_inode_size;
  259. error = ext3_xattr_check_names(entry, end);
  260. if (error)
  261. goto cleanup;
  262. error = ext3_xattr_find_entry(&entry, name_index, name,
  263. end - (void *)entry, 0);
  264. if (error)
  265. goto cleanup;
  266. size = le32_to_cpu(entry->e_value_size);
  267. if (buffer) {
  268. error = -ERANGE;
  269. if (size > buffer_size)
  270. goto cleanup;
  271. memcpy(buffer, (void *)IFIRST(header) +
  272. le16_to_cpu(entry->e_value_offs), size);
  273. }
  274. error = size;
  275. cleanup:
  276. brelse(iloc.bh);
  277. return error;
  278. }
  279. /*
  280. * ext3_xattr_get()
  281. *
  282. * Copy an extended attribute into the buffer
  283. * provided, or compute the buffer size required.
  284. * Buffer is NULL to compute the size of the buffer required.
  285. *
  286. * Returns a negative error number on failure, or the number of bytes
  287. * used / required on success.
  288. */
  289. int
  290. ext3_xattr_get(struct inode *inode, int name_index, const char *name,
  291. void *buffer, size_t buffer_size)
  292. {
  293. int error;
  294. down_read(&EXT3_I(inode)->xattr_sem);
  295. error = ext3_xattr_ibody_get(inode, name_index, name, buffer,
  296. buffer_size);
  297. if (error == -ENODATA)
  298. error = ext3_xattr_block_get(inode, name_index, name, buffer,
  299. buffer_size);
  300. up_read(&EXT3_I(inode)->xattr_sem);
  301. return error;
  302. }
  303. static int
  304. ext3_xattr_list_entries(struct inode *inode, struct ext3_xattr_entry *entry,
  305. char *buffer, size_t buffer_size)
  306. {
  307. size_t rest = buffer_size;
  308. for (; !IS_LAST_ENTRY(entry); entry = EXT3_XATTR_NEXT(entry)) {
  309. struct xattr_handler *handler =
  310. ext3_xattr_handler(entry->e_name_index);
  311. if (handler) {
  312. size_t size = handler->list(inode, buffer, rest,
  313. entry->e_name,
  314. entry->e_name_len);
  315. if (buffer) {
  316. if (size > rest)
  317. return -ERANGE;
  318. buffer += size;
  319. }
  320. rest -= size;
  321. }
  322. }
  323. return buffer_size - rest;
  324. }
  325. static int
  326. ext3_xattr_block_list(struct inode *inode, char *buffer, size_t buffer_size)
  327. {
  328. struct buffer_head *bh = NULL;
  329. int error;
  330. ea_idebug(inode, "buffer=%p, buffer_size=%ld",
  331. buffer, (long)buffer_size);
  332. error = 0;
  333. if (!EXT3_I(inode)->i_file_acl)
  334. goto cleanup;
  335. ea_idebug(inode, "reading block %u", EXT3_I(inode)->i_file_acl);
  336. bh = sb_bread(inode->i_sb, EXT3_I(inode)->i_file_acl);
  337. error = -EIO;
  338. if (!bh)
  339. goto cleanup;
  340. ea_bdebug(bh, "b_count=%d, refcount=%d",
  341. atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
  342. if (ext3_xattr_check_block(bh)) {
  343. ext3_error(inode->i_sb, __FUNCTION__,
  344. "inode %lu: bad block "E3FSBLK, inode->i_ino,
  345. EXT3_I(inode)->i_file_acl);
  346. error = -EIO;
  347. goto cleanup;
  348. }
  349. ext3_xattr_cache_insert(bh);
  350. error = ext3_xattr_list_entries(inode, BFIRST(bh), buffer, buffer_size);
  351. cleanup:
  352. brelse(bh);
  353. return error;
  354. }
  355. static int
  356. ext3_xattr_ibody_list(struct inode *inode, char *buffer, size_t buffer_size)
  357. {
  358. struct ext3_xattr_ibody_header *header;
  359. struct ext3_inode *raw_inode;
  360. struct ext3_iloc iloc;
  361. void *end;
  362. int error;
  363. if (!(EXT3_I(inode)->i_state & EXT3_STATE_XATTR))
  364. return 0;
  365. error = ext3_get_inode_loc(inode, &iloc);
  366. if (error)
  367. return error;
  368. raw_inode = ext3_raw_inode(&iloc);
  369. header = IHDR(inode, raw_inode);
  370. end = (void *)raw_inode + EXT3_SB(inode->i_sb)->s_inode_size;
  371. error = ext3_xattr_check_names(IFIRST(header), end);
  372. if (error)
  373. goto cleanup;
  374. error = ext3_xattr_list_entries(inode, IFIRST(header),
  375. buffer, buffer_size);
  376. cleanup:
  377. brelse(iloc.bh);
  378. return error;
  379. }
  380. /*
  381. * ext3_xattr_list()
  382. *
  383. * Copy a list of attribute names into the buffer
  384. * provided, or compute the buffer size required.
  385. * Buffer is NULL to compute the size of the buffer required.
  386. *
  387. * Returns a negative error number on failure, or the number of bytes
  388. * used / required on success.
  389. */
  390. int
  391. ext3_xattr_list(struct inode *inode, char *buffer, size_t buffer_size)
  392. {
  393. int i_error, b_error;
  394. down_read(&EXT3_I(inode)->xattr_sem);
  395. i_error = ext3_xattr_ibody_list(inode, buffer, buffer_size);
  396. if (i_error < 0) {
  397. b_error = 0;
  398. } else {
  399. if (buffer) {
  400. buffer += i_error;
  401. buffer_size -= i_error;
  402. }
  403. b_error = ext3_xattr_block_list(inode, buffer, buffer_size);
  404. if (b_error < 0)
  405. i_error = 0;
  406. }
  407. up_read(&EXT3_I(inode)->xattr_sem);
  408. return i_error + b_error;
  409. }
  410. /*
  411. * If the EXT3_FEATURE_COMPAT_EXT_ATTR feature of this file system is
  412. * not set, set it.
  413. */
  414. static void ext3_xattr_update_super_block(handle_t *handle,
  415. struct super_block *sb)
  416. {
  417. if (EXT3_HAS_COMPAT_FEATURE(sb, EXT3_FEATURE_COMPAT_EXT_ATTR))
  418. return;
  419. if (ext3_journal_get_write_access(handle, EXT3_SB(sb)->s_sbh) == 0) {
  420. EXT3_SET_COMPAT_FEATURE(sb, EXT3_FEATURE_COMPAT_EXT_ATTR);
  421. sb->s_dirt = 1;
  422. ext3_journal_dirty_metadata(handle, EXT3_SB(sb)->s_sbh);
  423. }
  424. }
  425. /*
  426. * Release the xattr block BH: If the reference count is > 1, decrement
  427. * it; otherwise free the block.
  428. */
  429. static void
  430. ext3_xattr_release_block(handle_t *handle, struct inode *inode,
  431. struct buffer_head *bh)
  432. {
  433. struct mb_cache_entry *ce = NULL;
  434. int error = 0;
  435. ce = mb_cache_entry_get(ext3_xattr_cache, bh->b_bdev, bh->b_blocknr);
  436. error = ext3_journal_get_write_access(handle, bh);
  437. if (error)
  438. goto out;
  439. lock_buffer(bh);
  440. if (BHDR(bh)->h_refcount == cpu_to_le32(1)) {
  441. ea_bdebug(bh, "refcount now=0; freeing");
  442. if (ce)
  443. mb_cache_entry_free(ce);
  444. ext3_free_blocks(handle, inode, bh->b_blocknr, 1);
  445. get_bh(bh);
  446. ext3_forget(handle, 1, inode, bh, bh->b_blocknr);
  447. } else {
  448. le32_add_cpu(&BHDR(bh)->h_refcount, -1);
  449. error = ext3_journal_dirty_metadata(handle, bh);
  450. if (IS_SYNC(inode))
  451. handle->h_sync = 1;
  452. DQUOT_FREE_BLOCK(inode, 1);
  453. ea_bdebug(bh, "refcount now=%d; releasing",
  454. le32_to_cpu(BHDR(bh)->h_refcount));
  455. if (ce)
  456. mb_cache_entry_release(ce);
  457. }
  458. unlock_buffer(bh);
  459. out:
  460. ext3_std_error(inode->i_sb, error);
  461. return;
  462. }
  463. struct ext3_xattr_info {
  464. int name_index;
  465. const char *name;
  466. const void *value;
  467. size_t value_len;
  468. };
  469. struct ext3_xattr_search {
  470. struct ext3_xattr_entry *first;
  471. void *base;
  472. void *end;
  473. struct ext3_xattr_entry *here;
  474. int not_found;
  475. };
  476. static int
  477. ext3_xattr_set_entry(struct ext3_xattr_info *i, struct ext3_xattr_search *s)
  478. {
  479. struct ext3_xattr_entry *last;
  480. size_t free, min_offs = s->end - s->base, name_len = strlen(i->name);
  481. /* Compute min_offs and last. */
  482. last = s->first;
  483. for (; !IS_LAST_ENTRY(last); last = EXT3_XATTR_NEXT(last)) {
  484. if (!last->e_value_block && last->e_value_size) {
  485. size_t offs = le16_to_cpu(last->e_value_offs);
  486. if (offs < min_offs)
  487. min_offs = offs;
  488. }
  489. }
  490. free = min_offs - ((void *)last - s->base) - sizeof(__u32);
  491. if (!s->not_found) {
  492. if (!s->here->e_value_block && s->here->e_value_size) {
  493. size_t size = le32_to_cpu(s->here->e_value_size);
  494. free += EXT3_XATTR_SIZE(size);
  495. }
  496. free += EXT3_XATTR_LEN(name_len);
  497. }
  498. if (i->value) {
  499. if (free < EXT3_XATTR_SIZE(i->value_len) ||
  500. free < EXT3_XATTR_LEN(name_len) +
  501. EXT3_XATTR_SIZE(i->value_len))
  502. return -ENOSPC;
  503. }
  504. if (i->value && s->not_found) {
  505. /* Insert the new name. */
  506. size_t size = EXT3_XATTR_LEN(name_len);
  507. size_t rest = (void *)last - (void *)s->here + sizeof(__u32);
  508. memmove((void *)s->here + size, s->here, rest);
  509. memset(s->here, 0, size);
  510. s->here->e_name_index = i->name_index;
  511. s->here->e_name_len = name_len;
  512. memcpy(s->here->e_name, i->name, name_len);
  513. } else {
  514. if (!s->here->e_value_block && s->here->e_value_size) {
  515. void *first_val = s->base + min_offs;
  516. size_t offs = le16_to_cpu(s->here->e_value_offs);
  517. void *val = s->base + offs;
  518. size_t size = EXT3_XATTR_SIZE(
  519. le32_to_cpu(s->here->e_value_size));
  520. if (i->value && size == EXT3_XATTR_SIZE(i->value_len)) {
  521. /* The old and the new value have the same
  522. size. Just replace. */
  523. s->here->e_value_size =
  524. cpu_to_le32(i->value_len);
  525. memset(val + size - EXT3_XATTR_PAD, 0,
  526. EXT3_XATTR_PAD); /* Clear pad bytes. */
  527. memcpy(val, i->value, i->value_len);
  528. return 0;
  529. }
  530. /* Remove the old value. */
  531. memmove(first_val + size, first_val, val - first_val);
  532. memset(first_val, 0, size);
  533. s->here->e_value_size = 0;
  534. s->here->e_value_offs = 0;
  535. min_offs += size;
  536. /* Adjust all value offsets. */
  537. last = s->first;
  538. while (!IS_LAST_ENTRY(last)) {
  539. size_t o = le16_to_cpu(last->e_value_offs);
  540. if (!last->e_value_block &&
  541. last->e_value_size && o < offs)
  542. last->e_value_offs =
  543. cpu_to_le16(o + size);
  544. last = EXT3_XATTR_NEXT(last);
  545. }
  546. }
  547. if (!i->value) {
  548. /* Remove the old name. */
  549. size_t size = EXT3_XATTR_LEN(name_len);
  550. last = ENTRY((void *)last - size);
  551. memmove(s->here, (void *)s->here + size,
  552. (void *)last - (void *)s->here + sizeof(__u32));
  553. memset(last, 0, size);
  554. }
  555. }
  556. if (i->value) {
  557. /* Insert the new value. */
  558. s->here->e_value_size = cpu_to_le32(i->value_len);
  559. if (i->value_len) {
  560. size_t size = EXT3_XATTR_SIZE(i->value_len);
  561. void *val = s->base + min_offs - size;
  562. s->here->e_value_offs = cpu_to_le16(min_offs - size);
  563. memset(val + size - EXT3_XATTR_PAD, 0,
  564. EXT3_XATTR_PAD); /* Clear the pad bytes. */
  565. memcpy(val, i->value, i->value_len);
  566. }
  567. }
  568. return 0;
  569. }
  570. struct ext3_xattr_block_find {
  571. struct ext3_xattr_search s;
  572. struct buffer_head *bh;
  573. };
  574. static int
  575. ext3_xattr_block_find(struct inode *inode, struct ext3_xattr_info *i,
  576. struct ext3_xattr_block_find *bs)
  577. {
  578. struct super_block *sb = inode->i_sb;
  579. int error;
  580. ea_idebug(inode, "name=%d.%s, value=%p, value_len=%ld",
  581. i->name_index, i->name, i->value, (long)i->value_len);
  582. if (EXT3_I(inode)->i_file_acl) {
  583. /* The inode already has an extended attribute block. */
  584. bs->bh = sb_bread(sb, EXT3_I(inode)->i_file_acl);
  585. error = -EIO;
  586. if (!bs->bh)
  587. goto cleanup;
  588. ea_bdebug(bs->bh, "b_count=%d, refcount=%d",
  589. atomic_read(&(bs->bh->b_count)),
  590. le32_to_cpu(BHDR(bs->bh)->h_refcount));
  591. if (ext3_xattr_check_block(bs->bh)) {
  592. ext3_error(sb, __FUNCTION__,
  593. "inode %lu: bad block "E3FSBLK, inode->i_ino,
  594. EXT3_I(inode)->i_file_acl);
  595. error = -EIO;
  596. goto cleanup;
  597. }
  598. /* Find the named attribute. */
  599. bs->s.base = BHDR(bs->bh);
  600. bs->s.first = BFIRST(bs->bh);
  601. bs->s.end = bs->bh->b_data + bs->bh->b_size;
  602. bs->s.here = bs->s.first;
  603. error = ext3_xattr_find_entry(&bs->s.here, i->name_index,
  604. i->name, bs->bh->b_size, 1);
  605. if (error && error != -ENODATA)
  606. goto cleanup;
  607. bs->s.not_found = error;
  608. }
  609. error = 0;
  610. cleanup:
  611. return error;
  612. }
  613. static int
  614. ext3_xattr_block_set(handle_t *handle, struct inode *inode,
  615. struct ext3_xattr_info *i,
  616. struct ext3_xattr_block_find *bs)
  617. {
  618. struct super_block *sb = inode->i_sb;
  619. struct buffer_head *new_bh = NULL;
  620. struct ext3_xattr_search *s = &bs->s;
  621. struct mb_cache_entry *ce = NULL;
  622. int error = 0;
  623. #define header(x) ((struct ext3_xattr_header *)(x))
  624. if (i->value && i->value_len > sb->s_blocksize)
  625. return -ENOSPC;
  626. if (s->base) {
  627. ce = mb_cache_entry_get(ext3_xattr_cache, bs->bh->b_bdev,
  628. bs->bh->b_blocknr);
  629. error = ext3_journal_get_write_access(handle, bs->bh);
  630. if (error)
  631. goto cleanup;
  632. lock_buffer(bs->bh);
  633. if (header(s->base)->h_refcount == cpu_to_le32(1)) {
  634. if (ce) {
  635. mb_cache_entry_free(ce);
  636. ce = NULL;
  637. }
  638. ea_bdebug(bs->bh, "modifying in-place");
  639. error = ext3_xattr_set_entry(i, s);
  640. if (!error) {
  641. if (!IS_LAST_ENTRY(s->first))
  642. ext3_xattr_rehash(header(s->base),
  643. s->here);
  644. ext3_xattr_cache_insert(bs->bh);
  645. }
  646. unlock_buffer(bs->bh);
  647. if (error == -EIO)
  648. goto bad_block;
  649. if (!error)
  650. error = ext3_journal_dirty_metadata(handle,
  651. bs->bh);
  652. if (error)
  653. goto cleanup;
  654. goto inserted;
  655. } else {
  656. int offset = (char *)s->here - bs->bh->b_data;
  657. unlock_buffer(bs->bh);
  658. journal_release_buffer(handle, bs->bh);
  659. if (ce) {
  660. mb_cache_entry_release(ce);
  661. ce = NULL;
  662. }
  663. ea_bdebug(bs->bh, "cloning");
  664. s->base = kmalloc(bs->bh->b_size, GFP_NOFS);
  665. error = -ENOMEM;
  666. if (s->base == NULL)
  667. goto cleanup;
  668. memcpy(s->base, BHDR(bs->bh), bs->bh->b_size);
  669. s->first = ENTRY(header(s->base)+1);
  670. header(s->base)->h_refcount = cpu_to_le32(1);
  671. s->here = ENTRY(s->base + offset);
  672. s->end = s->base + bs->bh->b_size;
  673. }
  674. } else {
  675. /* Allocate a buffer where we construct the new block. */
  676. s->base = kzalloc(sb->s_blocksize, GFP_NOFS);
  677. /* assert(header == s->base) */
  678. error = -ENOMEM;
  679. if (s->base == NULL)
  680. goto cleanup;
  681. header(s->base)->h_magic = cpu_to_le32(EXT3_XATTR_MAGIC);
  682. header(s->base)->h_blocks = cpu_to_le32(1);
  683. header(s->base)->h_refcount = cpu_to_le32(1);
  684. s->first = ENTRY(header(s->base)+1);
  685. s->here = ENTRY(header(s->base)+1);
  686. s->end = s->base + sb->s_blocksize;
  687. }
  688. error = ext3_xattr_set_entry(i, s);
  689. if (error == -EIO)
  690. goto bad_block;
  691. if (error)
  692. goto cleanup;
  693. if (!IS_LAST_ENTRY(s->first))
  694. ext3_xattr_rehash(header(s->base), s->here);
  695. inserted:
  696. if (!IS_LAST_ENTRY(s->first)) {
  697. new_bh = ext3_xattr_cache_find(inode, header(s->base), &ce);
  698. if (new_bh) {
  699. /* We found an identical block in the cache. */
  700. if (new_bh == bs->bh)
  701. ea_bdebug(new_bh, "keeping");
  702. else {
  703. /* The old block is released after updating
  704. the inode. */
  705. error = -EDQUOT;
  706. if (DQUOT_ALLOC_BLOCK(inode, 1))
  707. goto cleanup;
  708. error = ext3_journal_get_write_access(handle,
  709. new_bh);
  710. if (error)
  711. goto cleanup_dquot;
  712. lock_buffer(new_bh);
  713. le32_add_cpu(&BHDR(new_bh)->h_refcount, 1);
  714. ea_bdebug(new_bh, "reusing; refcount now=%d",
  715. le32_to_cpu(BHDR(new_bh)->h_refcount));
  716. unlock_buffer(new_bh);
  717. error = ext3_journal_dirty_metadata(handle,
  718. new_bh);
  719. if (error)
  720. goto cleanup_dquot;
  721. }
  722. mb_cache_entry_release(ce);
  723. ce = NULL;
  724. } else if (bs->bh && s->base == bs->bh->b_data) {
  725. /* We were modifying this block in-place. */
  726. ea_bdebug(bs->bh, "keeping this block");
  727. new_bh = bs->bh;
  728. get_bh(new_bh);
  729. } else {
  730. /* We need to allocate a new block */
  731. ext3_fsblk_t goal = le32_to_cpu(
  732. EXT3_SB(sb)->s_es->s_first_data_block) +
  733. (ext3_fsblk_t)EXT3_I(inode)->i_block_group *
  734. EXT3_BLOCKS_PER_GROUP(sb);
  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, __FUNCTION__,
  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, __FUNCTION__,
  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, __FUNCTION__,
  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, __FUNCTION__,
  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. }