xattr.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325
  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. BHDR(bh)->h_refcount = cpu_to_le32(
  449. le32_to_cpu(BHDR(bh)->h_refcount) - 1);
  450. error = ext3_journal_dirty_metadata(handle, bh);
  451. if (IS_SYNC(inode))
  452. handle->h_sync = 1;
  453. DQUOT_FREE_BLOCK(inode, 1);
  454. ea_bdebug(bh, "refcount now=%d; releasing",
  455. le32_to_cpu(BHDR(bh)->h_refcount));
  456. if (ce)
  457. mb_cache_entry_release(ce);
  458. }
  459. unlock_buffer(bh);
  460. out:
  461. ext3_std_error(inode->i_sb, error);
  462. return;
  463. }
  464. struct ext3_xattr_info {
  465. int name_index;
  466. const char *name;
  467. const void *value;
  468. size_t value_len;
  469. };
  470. struct ext3_xattr_search {
  471. struct ext3_xattr_entry *first;
  472. void *base;
  473. void *end;
  474. struct ext3_xattr_entry *here;
  475. int not_found;
  476. };
  477. static int
  478. ext3_xattr_set_entry(struct ext3_xattr_info *i, struct ext3_xattr_search *s)
  479. {
  480. struct ext3_xattr_entry *last;
  481. size_t free, min_offs = s->end - s->base, name_len = strlen(i->name);
  482. /* Compute min_offs and last. */
  483. last = s->first;
  484. for (; !IS_LAST_ENTRY(last); last = EXT3_XATTR_NEXT(last)) {
  485. if (!last->e_value_block && last->e_value_size) {
  486. size_t offs = le16_to_cpu(last->e_value_offs);
  487. if (offs < min_offs)
  488. min_offs = offs;
  489. }
  490. }
  491. free = min_offs - ((void *)last - s->base) - sizeof(__u32);
  492. if (!s->not_found) {
  493. if (!s->here->e_value_block && s->here->e_value_size) {
  494. size_t size = le32_to_cpu(s->here->e_value_size);
  495. free += EXT3_XATTR_SIZE(size);
  496. }
  497. free += EXT3_XATTR_LEN(name_len);
  498. }
  499. if (i->value) {
  500. if (free < EXT3_XATTR_SIZE(i->value_len) ||
  501. free < EXT3_XATTR_LEN(name_len) +
  502. EXT3_XATTR_SIZE(i->value_len))
  503. return -ENOSPC;
  504. }
  505. if (i->value && s->not_found) {
  506. /* Insert the new name. */
  507. size_t size = EXT3_XATTR_LEN(name_len);
  508. size_t rest = (void *)last - (void *)s->here + sizeof(__u32);
  509. memmove((void *)s->here + size, s->here, rest);
  510. memset(s->here, 0, size);
  511. s->here->e_name_index = i->name_index;
  512. s->here->e_name_len = name_len;
  513. memcpy(s->here->e_name, i->name, name_len);
  514. } else {
  515. if (!s->here->e_value_block && s->here->e_value_size) {
  516. void *first_val = s->base + min_offs;
  517. size_t offs = le16_to_cpu(s->here->e_value_offs);
  518. void *val = s->base + offs;
  519. size_t size = EXT3_XATTR_SIZE(
  520. le32_to_cpu(s->here->e_value_size));
  521. if (i->value && size == EXT3_XATTR_SIZE(i->value_len)) {
  522. /* The old and the new value have the same
  523. size. Just replace. */
  524. s->here->e_value_size =
  525. cpu_to_le32(i->value_len);
  526. memset(val + size - EXT3_XATTR_PAD, 0,
  527. EXT3_XATTR_PAD); /* Clear pad bytes. */
  528. memcpy(val, i->value, i->value_len);
  529. return 0;
  530. }
  531. /* Remove the old value. */
  532. memmove(first_val + size, first_val, val - first_val);
  533. memset(first_val, 0, size);
  534. s->here->e_value_size = 0;
  535. s->here->e_value_offs = 0;
  536. min_offs += size;
  537. /* Adjust all value offsets. */
  538. last = s->first;
  539. while (!IS_LAST_ENTRY(last)) {
  540. size_t o = le16_to_cpu(last->e_value_offs);
  541. if (!last->e_value_block &&
  542. last->e_value_size && o < offs)
  543. last->e_value_offs =
  544. cpu_to_le16(o + size);
  545. last = EXT3_XATTR_NEXT(last);
  546. }
  547. }
  548. if (!i->value) {
  549. /* Remove the old name. */
  550. size_t size = EXT3_XATTR_LEN(name_len);
  551. last = ENTRY((void *)last - size);
  552. memmove(s->here, (void *)s->here + size,
  553. (void *)last - (void *)s->here + sizeof(__u32));
  554. memset(last, 0, size);
  555. }
  556. }
  557. if (i->value) {
  558. /* Insert the new value. */
  559. s->here->e_value_size = cpu_to_le32(i->value_len);
  560. if (i->value_len) {
  561. size_t size = EXT3_XATTR_SIZE(i->value_len);
  562. void *val = s->base + min_offs - size;
  563. s->here->e_value_offs = cpu_to_le16(min_offs - size);
  564. memset(val + size - EXT3_XATTR_PAD, 0,
  565. EXT3_XATTR_PAD); /* Clear the pad bytes. */
  566. memcpy(val, i->value, i->value_len);
  567. }
  568. }
  569. return 0;
  570. }
  571. struct ext3_xattr_block_find {
  572. struct ext3_xattr_search s;
  573. struct buffer_head *bh;
  574. };
  575. static int
  576. ext3_xattr_block_find(struct inode *inode, struct ext3_xattr_info *i,
  577. struct ext3_xattr_block_find *bs)
  578. {
  579. struct super_block *sb = inode->i_sb;
  580. int error;
  581. ea_idebug(inode, "name=%d.%s, value=%p, value_len=%ld",
  582. i->name_index, i->name, i->value, (long)i->value_len);
  583. if (EXT3_I(inode)->i_file_acl) {
  584. /* The inode already has an extended attribute block. */
  585. bs->bh = sb_bread(sb, EXT3_I(inode)->i_file_acl);
  586. error = -EIO;
  587. if (!bs->bh)
  588. goto cleanup;
  589. ea_bdebug(bs->bh, "b_count=%d, refcount=%d",
  590. atomic_read(&(bs->bh->b_count)),
  591. le32_to_cpu(BHDR(bs->bh)->h_refcount));
  592. if (ext3_xattr_check_block(bs->bh)) {
  593. ext3_error(sb, __FUNCTION__,
  594. "inode %lu: bad block "E3FSBLK, inode->i_ino,
  595. EXT3_I(inode)->i_file_acl);
  596. error = -EIO;
  597. goto cleanup;
  598. }
  599. /* Find the named attribute. */
  600. bs->s.base = BHDR(bs->bh);
  601. bs->s.first = BFIRST(bs->bh);
  602. bs->s.end = bs->bh->b_data + bs->bh->b_size;
  603. bs->s.here = bs->s.first;
  604. error = ext3_xattr_find_entry(&bs->s.here, i->name_index,
  605. i->name, bs->bh->b_size, 1);
  606. if (error && error != -ENODATA)
  607. goto cleanup;
  608. bs->s.not_found = error;
  609. }
  610. error = 0;
  611. cleanup:
  612. return error;
  613. }
  614. static int
  615. ext3_xattr_block_set(handle_t *handle, struct inode *inode,
  616. struct ext3_xattr_info *i,
  617. struct ext3_xattr_block_find *bs)
  618. {
  619. struct super_block *sb = inode->i_sb;
  620. struct buffer_head *new_bh = NULL;
  621. struct ext3_xattr_search *s = &bs->s;
  622. struct mb_cache_entry *ce = NULL;
  623. int error = 0;
  624. #define header(x) ((struct ext3_xattr_header *)(x))
  625. if (i->value && i->value_len > sb->s_blocksize)
  626. return -ENOSPC;
  627. if (s->base) {
  628. ce = mb_cache_entry_get(ext3_xattr_cache, bs->bh->b_bdev,
  629. bs->bh->b_blocknr);
  630. error = ext3_journal_get_write_access(handle, bs->bh);
  631. if (error)
  632. goto cleanup;
  633. lock_buffer(bs->bh);
  634. if (header(s->base)->h_refcount == cpu_to_le32(1)) {
  635. if (ce) {
  636. mb_cache_entry_free(ce);
  637. ce = NULL;
  638. }
  639. ea_bdebug(bs->bh, "modifying in-place");
  640. error = ext3_xattr_set_entry(i, s);
  641. if (!error) {
  642. if (!IS_LAST_ENTRY(s->first))
  643. ext3_xattr_rehash(header(s->base),
  644. s->here);
  645. ext3_xattr_cache_insert(bs->bh);
  646. }
  647. unlock_buffer(bs->bh);
  648. if (error == -EIO)
  649. goto bad_block;
  650. if (!error)
  651. error = ext3_journal_dirty_metadata(handle,
  652. bs->bh);
  653. if (error)
  654. goto cleanup;
  655. goto inserted;
  656. } else {
  657. int offset = (char *)s->here - bs->bh->b_data;
  658. unlock_buffer(bs->bh);
  659. journal_release_buffer(handle, bs->bh);
  660. if (ce) {
  661. mb_cache_entry_release(ce);
  662. ce = NULL;
  663. }
  664. ea_bdebug(bs->bh, "cloning");
  665. s->base = kmalloc(bs->bh->b_size, GFP_KERNEL);
  666. error = -ENOMEM;
  667. if (s->base == NULL)
  668. goto cleanup;
  669. memcpy(s->base, BHDR(bs->bh), bs->bh->b_size);
  670. s->first = ENTRY(header(s->base)+1);
  671. header(s->base)->h_refcount = cpu_to_le32(1);
  672. s->here = ENTRY(s->base + offset);
  673. s->end = s->base + bs->bh->b_size;
  674. }
  675. } else {
  676. /* Allocate a buffer where we construct the new block. */
  677. s->base = kmalloc(sb->s_blocksize, GFP_KERNEL);
  678. /* assert(header == s->base) */
  679. error = -ENOMEM;
  680. if (s->base == NULL)
  681. goto cleanup;
  682. memset(s->base, 0, sb->s_blocksize);
  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. BHDR(new_bh)->h_refcount = cpu_to_le32(1 +
  716. le32_to_cpu(BHDR(new_bh)->h_refcount));
  717. ea_bdebug(new_bh, "reusing; refcount now=%d",
  718. le32_to_cpu(BHDR(new_bh)->h_refcount));
  719. unlock_buffer(new_bh);
  720. error = ext3_journal_dirty_metadata(handle,
  721. new_bh);
  722. if (error)
  723. goto cleanup_dquot;
  724. }
  725. mb_cache_entry_release(ce);
  726. ce = NULL;
  727. } else if (bs->bh && s->base == bs->bh->b_data) {
  728. /* We were modifying this block in-place. */
  729. ea_bdebug(bs->bh, "keeping this block");
  730. new_bh = bs->bh;
  731. get_bh(new_bh);
  732. } else {
  733. /* We need to allocate a new block */
  734. ext3_fsblk_t goal = le32_to_cpu(
  735. EXT3_SB(sb)->s_es->s_first_data_block) +
  736. (ext3_fsblk_t)EXT3_I(inode)->i_block_group *
  737. EXT3_BLOCKS_PER_GROUP(sb);
  738. ext3_fsblk_t block = ext3_new_block(handle, inode,
  739. goal, &error);
  740. if (error)
  741. goto cleanup;
  742. ea_idebug(inode, "creating block %d", block);
  743. new_bh = sb_getblk(sb, block);
  744. if (!new_bh) {
  745. getblk_failed:
  746. ext3_free_blocks(handle, inode, block, 1);
  747. error = -EIO;
  748. goto cleanup;
  749. }
  750. lock_buffer(new_bh);
  751. error = ext3_journal_get_create_access(handle, new_bh);
  752. if (error) {
  753. unlock_buffer(new_bh);
  754. goto getblk_failed;
  755. }
  756. memcpy(new_bh->b_data, s->base, new_bh->b_size);
  757. set_buffer_uptodate(new_bh);
  758. unlock_buffer(new_bh);
  759. ext3_xattr_cache_insert(new_bh);
  760. error = ext3_journal_dirty_metadata(handle, new_bh);
  761. if (error)
  762. goto cleanup;
  763. }
  764. }
  765. /* Update the inode. */
  766. EXT3_I(inode)->i_file_acl = new_bh ? new_bh->b_blocknr : 0;
  767. /* Drop the previous xattr block. */
  768. if (bs->bh && bs->bh != new_bh)
  769. ext3_xattr_release_block(handle, inode, bs->bh);
  770. error = 0;
  771. cleanup:
  772. if (ce)
  773. mb_cache_entry_release(ce);
  774. brelse(new_bh);
  775. if (!(bs->bh && s->base == bs->bh->b_data))
  776. kfree(s->base);
  777. return error;
  778. cleanup_dquot:
  779. DQUOT_FREE_BLOCK(inode, 1);
  780. goto cleanup;
  781. bad_block:
  782. ext3_error(inode->i_sb, __FUNCTION__,
  783. "inode %lu: bad block "E3FSBLK, inode->i_ino,
  784. EXT3_I(inode)->i_file_acl);
  785. goto cleanup;
  786. #undef header
  787. }
  788. struct ext3_xattr_ibody_find {
  789. struct ext3_xattr_search s;
  790. struct ext3_iloc iloc;
  791. };
  792. static int
  793. ext3_xattr_ibody_find(struct inode *inode, struct ext3_xattr_info *i,
  794. struct ext3_xattr_ibody_find *is)
  795. {
  796. struct ext3_xattr_ibody_header *header;
  797. struct ext3_inode *raw_inode;
  798. int error;
  799. if (EXT3_I(inode)->i_extra_isize == 0)
  800. return 0;
  801. raw_inode = ext3_raw_inode(&is->iloc);
  802. header = IHDR(inode, raw_inode);
  803. is->s.base = is->s.first = IFIRST(header);
  804. is->s.here = is->s.first;
  805. is->s.end = (void *)raw_inode + EXT3_SB(inode->i_sb)->s_inode_size;
  806. if (EXT3_I(inode)->i_state & EXT3_STATE_XATTR) {
  807. error = ext3_xattr_check_names(IFIRST(header), is->s.end);
  808. if (error)
  809. return error;
  810. /* Find the named attribute. */
  811. error = ext3_xattr_find_entry(&is->s.here, i->name_index,
  812. i->name, is->s.end -
  813. (void *)is->s.base, 0);
  814. if (error && error != -ENODATA)
  815. return error;
  816. is->s.not_found = error;
  817. }
  818. return 0;
  819. }
  820. static int
  821. ext3_xattr_ibody_set(handle_t *handle, struct inode *inode,
  822. struct ext3_xattr_info *i,
  823. struct ext3_xattr_ibody_find *is)
  824. {
  825. struct ext3_xattr_ibody_header *header;
  826. struct ext3_xattr_search *s = &is->s;
  827. int error;
  828. if (EXT3_I(inode)->i_extra_isize == 0)
  829. return -ENOSPC;
  830. error = ext3_xattr_set_entry(i, s);
  831. if (error)
  832. return error;
  833. header = IHDR(inode, ext3_raw_inode(&is->iloc));
  834. if (!IS_LAST_ENTRY(s->first)) {
  835. header->h_magic = cpu_to_le32(EXT3_XATTR_MAGIC);
  836. EXT3_I(inode)->i_state |= EXT3_STATE_XATTR;
  837. } else {
  838. header->h_magic = cpu_to_le32(0);
  839. EXT3_I(inode)->i_state &= ~EXT3_STATE_XATTR;
  840. }
  841. return 0;
  842. }
  843. /*
  844. * ext3_xattr_set_handle()
  845. *
  846. * Create, replace or remove an extended attribute for this inode. Buffer
  847. * is NULL to remove an existing extended attribute, and non-NULL to
  848. * either replace an existing extended attribute, or create a new extended
  849. * attribute. The flags XATTR_REPLACE and XATTR_CREATE
  850. * specify that an extended attribute must exist and must not exist
  851. * previous to the call, respectively.
  852. *
  853. * Returns 0, or a negative error number on failure.
  854. */
  855. int
  856. ext3_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index,
  857. const char *name, const void *value, size_t value_len,
  858. int flags)
  859. {
  860. struct ext3_xattr_info i = {
  861. .name_index = name_index,
  862. .name = name,
  863. .value = value,
  864. .value_len = value_len,
  865. };
  866. struct ext3_xattr_ibody_find is = {
  867. .s = { .not_found = -ENODATA, },
  868. };
  869. struct ext3_xattr_block_find bs = {
  870. .s = { .not_found = -ENODATA, },
  871. };
  872. int error;
  873. if (!name)
  874. return -EINVAL;
  875. if (strlen(name) > 255)
  876. return -ERANGE;
  877. down_write(&EXT3_I(inode)->xattr_sem);
  878. error = ext3_get_inode_loc(inode, &is.iloc);
  879. if (error)
  880. goto cleanup;
  881. if (EXT3_I(inode)->i_state & EXT3_STATE_NEW) {
  882. struct ext3_inode *raw_inode = ext3_raw_inode(&is.iloc);
  883. memset(raw_inode, 0, EXT3_SB(inode->i_sb)->s_inode_size);
  884. EXT3_I(inode)->i_state &= ~EXT3_STATE_NEW;
  885. }
  886. error = ext3_xattr_ibody_find(inode, &i, &is);
  887. if (error)
  888. goto cleanup;
  889. if (is.s.not_found)
  890. error = ext3_xattr_block_find(inode, &i, &bs);
  891. if (error)
  892. goto cleanup;
  893. if (is.s.not_found && bs.s.not_found) {
  894. error = -ENODATA;
  895. if (flags & XATTR_REPLACE)
  896. goto cleanup;
  897. error = 0;
  898. if (!value)
  899. goto cleanup;
  900. } else {
  901. error = -EEXIST;
  902. if (flags & XATTR_CREATE)
  903. goto cleanup;
  904. }
  905. error = ext3_journal_get_write_access(handle, is.iloc.bh);
  906. if (error)
  907. goto cleanup;
  908. if (!value) {
  909. if (!is.s.not_found)
  910. error = ext3_xattr_ibody_set(handle, inode, &i, &is);
  911. else if (!bs.s.not_found)
  912. error = ext3_xattr_block_set(handle, inode, &i, &bs);
  913. } else {
  914. error = ext3_xattr_ibody_set(handle, inode, &i, &is);
  915. if (!error && !bs.s.not_found) {
  916. i.value = NULL;
  917. error = ext3_xattr_block_set(handle, inode, &i, &bs);
  918. } else if (error == -ENOSPC) {
  919. error = ext3_xattr_block_set(handle, inode, &i, &bs);
  920. if (error)
  921. goto cleanup;
  922. if (!is.s.not_found) {
  923. i.value = NULL;
  924. error = ext3_xattr_ibody_set(handle, inode, &i,
  925. &is);
  926. }
  927. }
  928. }
  929. if (!error) {
  930. ext3_xattr_update_super_block(handle, inode->i_sb);
  931. inode->i_ctime = CURRENT_TIME_SEC;
  932. error = ext3_mark_iloc_dirty(handle, inode, &is.iloc);
  933. /*
  934. * The bh is consumed by ext3_mark_iloc_dirty, even with
  935. * error != 0.
  936. */
  937. is.iloc.bh = NULL;
  938. if (IS_SYNC(inode))
  939. handle->h_sync = 1;
  940. }
  941. cleanup:
  942. brelse(is.iloc.bh);
  943. brelse(bs.bh);
  944. up_write(&EXT3_I(inode)->xattr_sem);
  945. return error;
  946. }
  947. /*
  948. * ext3_xattr_set()
  949. *
  950. * Like ext3_xattr_set_handle, but start from an inode. This extended
  951. * attribute modification is a filesystem transaction by itself.
  952. *
  953. * Returns 0, or a negative error number on failure.
  954. */
  955. int
  956. ext3_xattr_set(struct inode *inode, int name_index, const char *name,
  957. const void *value, size_t value_len, int flags)
  958. {
  959. handle_t *handle;
  960. int error, retries = 0;
  961. retry:
  962. handle = ext3_journal_start(inode, EXT3_DATA_TRANS_BLOCKS(inode->i_sb));
  963. if (IS_ERR(handle)) {
  964. error = PTR_ERR(handle);
  965. } else {
  966. int error2;
  967. error = ext3_xattr_set_handle(handle, inode, name_index, name,
  968. value, value_len, flags);
  969. error2 = ext3_journal_stop(handle);
  970. if (error == -ENOSPC &&
  971. ext3_should_retry_alloc(inode->i_sb, &retries))
  972. goto retry;
  973. if (error == 0)
  974. error = error2;
  975. }
  976. return error;
  977. }
  978. /*
  979. * ext3_xattr_delete_inode()
  980. *
  981. * Free extended attribute resources associated with this inode. This
  982. * is called immediately before an inode is freed. We have exclusive
  983. * access to the inode.
  984. */
  985. void
  986. ext3_xattr_delete_inode(handle_t *handle, struct inode *inode)
  987. {
  988. struct buffer_head *bh = NULL;
  989. if (!EXT3_I(inode)->i_file_acl)
  990. goto cleanup;
  991. bh = sb_bread(inode->i_sb, EXT3_I(inode)->i_file_acl);
  992. if (!bh) {
  993. ext3_error(inode->i_sb, __FUNCTION__,
  994. "inode %lu: block "E3FSBLK" read error", inode->i_ino,
  995. EXT3_I(inode)->i_file_acl);
  996. goto cleanup;
  997. }
  998. if (BHDR(bh)->h_magic != cpu_to_le32(EXT3_XATTR_MAGIC) ||
  999. BHDR(bh)->h_blocks != cpu_to_le32(1)) {
  1000. ext3_error(inode->i_sb, __FUNCTION__,
  1001. "inode %lu: bad block "E3FSBLK, inode->i_ino,
  1002. EXT3_I(inode)->i_file_acl);
  1003. goto cleanup;
  1004. }
  1005. ext3_xattr_release_block(handle, inode, bh);
  1006. EXT3_I(inode)->i_file_acl = 0;
  1007. cleanup:
  1008. brelse(bh);
  1009. }
  1010. /*
  1011. * ext3_xattr_put_super()
  1012. *
  1013. * This is called when a file system is unmounted.
  1014. */
  1015. void
  1016. ext3_xattr_put_super(struct super_block *sb)
  1017. {
  1018. mb_cache_shrink(sb->s_bdev);
  1019. }
  1020. /*
  1021. * ext3_xattr_cache_insert()
  1022. *
  1023. * Create a new entry in the extended attribute cache, and insert
  1024. * it unless such an entry is already in the cache.
  1025. *
  1026. * Returns 0, or a negative error number on failure.
  1027. */
  1028. static void
  1029. ext3_xattr_cache_insert(struct buffer_head *bh)
  1030. {
  1031. __u32 hash = le32_to_cpu(BHDR(bh)->h_hash);
  1032. struct mb_cache_entry *ce;
  1033. int error;
  1034. ce = mb_cache_entry_alloc(ext3_xattr_cache);
  1035. if (!ce) {
  1036. ea_bdebug(bh, "out of memory");
  1037. return;
  1038. }
  1039. error = mb_cache_entry_insert(ce, bh->b_bdev, bh->b_blocknr, &hash);
  1040. if (error) {
  1041. mb_cache_entry_free(ce);
  1042. if (error == -EBUSY) {
  1043. ea_bdebug(bh, "already in cache");
  1044. error = 0;
  1045. }
  1046. } else {
  1047. ea_bdebug(bh, "inserting [%x]", (int)hash);
  1048. mb_cache_entry_release(ce);
  1049. }
  1050. }
  1051. /*
  1052. * ext3_xattr_cmp()
  1053. *
  1054. * Compare two extended attribute blocks for equality.
  1055. *
  1056. * Returns 0 if the blocks are equal, 1 if they differ, and
  1057. * a negative error number on errors.
  1058. */
  1059. static int
  1060. ext3_xattr_cmp(struct ext3_xattr_header *header1,
  1061. struct ext3_xattr_header *header2)
  1062. {
  1063. struct ext3_xattr_entry *entry1, *entry2;
  1064. entry1 = ENTRY(header1+1);
  1065. entry2 = ENTRY(header2+1);
  1066. while (!IS_LAST_ENTRY(entry1)) {
  1067. if (IS_LAST_ENTRY(entry2))
  1068. return 1;
  1069. if (entry1->e_hash != entry2->e_hash ||
  1070. entry1->e_name_index != entry2->e_name_index ||
  1071. entry1->e_name_len != entry2->e_name_len ||
  1072. entry1->e_value_size != entry2->e_value_size ||
  1073. memcmp(entry1->e_name, entry2->e_name, entry1->e_name_len))
  1074. return 1;
  1075. if (entry1->e_value_block != 0 || entry2->e_value_block != 0)
  1076. return -EIO;
  1077. if (memcmp((char *)header1 + le16_to_cpu(entry1->e_value_offs),
  1078. (char *)header2 + le16_to_cpu(entry2->e_value_offs),
  1079. le32_to_cpu(entry1->e_value_size)))
  1080. return 1;
  1081. entry1 = EXT3_XATTR_NEXT(entry1);
  1082. entry2 = EXT3_XATTR_NEXT(entry2);
  1083. }
  1084. if (!IS_LAST_ENTRY(entry2))
  1085. return 1;
  1086. return 0;
  1087. }
  1088. /*
  1089. * ext3_xattr_cache_find()
  1090. *
  1091. * Find an identical extended attribute block.
  1092. *
  1093. * Returns a pointer to the block found, or NULL if such a block was
  1094. * not found or an error occurred.
  1095. */
  1096. static struct buffer_head *
  1097. ext3_xattr_cache_find(struct inode *inode, struct ext3_xattr_header *header,
  1098. struct mb_cache_entry **pce)
  1099. {
  1100. __u32 hash = le32_to_cpu(header->h_hash);
  1101. struct mb_cache_entry *ce;
  1102. if (!header->h_hash)
  1103. return NULL; /* never share */
  1104. ea_idebug(inode, "looking for cached blocks [%x]", (int)hash);
  1105. again:
  1106. ce = mb_cache_entry_find_first(ext3_xattr_cache, 0,
  1107. inode->i_sb->s_bdev, hash);
  1108. while (ce) {
  1109. struct buffer_head *bh;
  1110. if (IS_ERR(ce)) {
  1111. if (PTR_ERR(ce) == -EAGAIN)
  1112. goto again;
  1113. break;
  1114. }
  1115. bh = sb_bread(inode->i_sb, ce->e_block);
  1116. if (!bh) {
  1117. ext3_error(inode->i_sb, __FUNCTION__,
  1118. "inode %lu: block %lu read error",
  1119. inode->i_ino, (unsigned long) ce->e_block);
  1120. } else if (le32_to_cpu(BHDR(bh)->h_refcount) >=
  1121. EXT3_XATTR_REFCOUNT_MAX) {
  1122. ea_idebug(inode, "block %lu refcount %d>=%d",
  1123. (unsigned long) ce->e_block,
  1124. le32_to_cpu(BHDR(bh)->h_refcount),
  1125. EXT3_XATTR_REFCOUNT_MAX);
  1126. } else if (ext3_xattr_cmp(header, BHDR(bh)) == 0) {
  1127. *pce = ce;
  1128. return bh;
  1129. }
  1130. brelse(bh);
  1131. ce = mb_cache_entry_find_next(ce, 0, inode->i_sb->s_bdev, hash);
  1132. }
  1133. return NULL;
  1134. }
  1135. #define NAME_HASH_SHIFT 5
  1136. #define VALUE_HASH_SHIFT 16
  1137. /*
  1138. * ext3_xattr_hash_entry()
  1139. *
  1140. * Compute the hash of an extended attribute.
  1141. */
  1142. static inline void ext3_xattr_hash_entry(struct ext3_xattr_header *header,
  1143. struct ext3_xattr_entry *entry)
  1144. {
  1145. __u32 hash = 0;
  1146. char *name = entry->e_name;
  1147. int n;
  1148. for (n=0; n < entry->e_name_len; n++) {
  1149. hash = (hash << NAME_HASH_SHIFT) ^
  1150. (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^
  1151. *name++;
  1152. }
  1153. if (entry->e_value_block == 0 && entry->e_value_size != 0) {
  1154. __le32 *value = (__le32 *)((char *)header +
  1155. le16_to_cpu(entry->e_value_offs));
  1156. for (n = (le32_to_cpu(entry->e_value_size) +
  1157. EXT3_XATTR_ROUND) >> EXT3_XATTR_PAD_BITS; n; n--) {
  1158. hash = (hash << VALUE_HASH_SHIFT) ^
  1159. (hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^
  1160. le32_to_cpu(*value++);
  1161. }
  1162. }
  1163. entry->e_hash = cpu_to_le32(hash);
  1164. }
  1165. #undef NAME_HASH_SHIFT
  1166. #undef VALUE_HASH_SHIFT
  1167. #define BLOCK_HASH_SHIFT 16
  1168. /*
  1169. * ext3_xattr_rehash()
  1170. *
  1171. * Re-compute the extended attribute hash value after an entry has changed.
  1172. */
  1173. static void ext3_xattr_rehash(struct ext3_xattr_header *header,
  1174. struct ext3_xattr_entry *entry)
  1175. {
  1176. struct ext3_xattr_entry *here;
  1177. __u32 hash = 0;
  1178. ext3_xattr_hash_entry(header, entry);
  1179. here = ENTRY(header+1);
  1180. while (!IS_LAST_ENTRY(here)) {
  1181. if (!here->e_hash) {
  1182. /* Block is not shared if an entry's hash value == 0 */
  1183. hash = 0;
  1184. break;
  1185. }
  1186. hash = (hash << BLOCK_HASH_SHIFT) ^
  1187. (hash >> (8*sizeof(hash) - BLOCK_HASH_SHIFT)) ^
  1188. le32_to_cpu(here->e_hash);
  1189. here = EXT3_XATTR_NEXT(here);
  1190. }
  1191. header->h_hash = cpu_to_le32(hash);
  1192. }
  1193. #undef BLOCK_HASH_SHIFT
  1194. int __init
  1195. init_ext3_xattr(void)
  1196. {
  1197. ext3_xattr_cache = mb_cache_create("ext3_xattr", NULL,
  1198. sizeof(struct mb_cache_entry) +
  1199. sizeof(((struct mb_cache_entry *) 0)->e_indexes[0]), 1, 6);
  1200. if (!ext3_xattr_cache)
  1201. return -ENOMEM;
  1202. return 0;
  1203. }
  1204. void
  1205. exit_ext3_xattr(void)
  1206. {
  1207. if (ext3_xattr_cache)
  1208. mb_cache_destroy(ext3_xattr_cache);
  1209. ext3_xattr_cache = NULL;
  1210. }