xattr.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331
  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 "ext3.h"
  52. #include <linux/mbcache.h>
  53. #include <linux/quotaops.h>
  54. #include "xattr.h"
  55. #include "acl.h"
  56. #define BHDR(bh) ((struct ext3_xattr_header *)((bh)->b_data))
  57. #define ENTRY(ptr) ((struct ext3_xattr_entry *)(ptr))
  58. #define BFIRST(bh) ENTRY(BHDR(bh)+1)
  59. #define IS_LAST_ENTRY(entry) (*(__u32 *)(entry) == 0)
  60. #define IHDR(inode, raw_inode) \
  61. ((struct ext3_xattr_ibody_header *) \
  62. ((void *)raw_inode + \
  63. EXT3_GOOD_OLD_INODE_SIZE + \
  64. EXT3_I(inode)->i_extra_isize))
  65. #define IFIRST(hdr) ((struct ext3_xattr_entry *)((hdr)+1))
  66. #ifdef EXT3_XATTR_DEBUG
  67. # define ea_idebug(inode, f...) do { \
  68. printk(KERN_DEBUG "inode %s:%lu: ", \
  69. inode->i_sb->s_id, inode->i_ino); \
  70. printk(f); \
  71. printk("\n"); \
  72. } while (0)
  73. # define ea_bdebug(bh, f...) do { \
  74. char b[BDEVNAME_SIZE]; \
  75. printk(KERN_DEBUG "block %s:%lu: ", \
  76. bdevname(bh->b_bdev, b), \
  77. (unsigned long) bh->b_blocknr); \
  78. printk(f); \
  79. printk("\n"); \
  80. } while (0)
  81. #else
  82. # define ea_idebug(f...)
  83. # define ea_bdebug(f...)
  84. #endif
  85. static void ext3_xattr_cache_insert(struct buffer_head *);
  86. static struct buffer_head *ext3_xattr_cache_find(struct inode *,
  87. struct ext3_xattr_header *,
  88. struct mb_cache_entry **);
  89. static void ext3_xattr_rehash(struct ext3_xattr_header *,
  90. struct ext3_xattr_entry *);
  91. static int ext3_xattr_list(struct dentry *dentry, char *buffer,
  92. size_t buffer_size);
  93. static struct mb_cache *ext3_xattr_cache;
  94. static const struct xattr_handler *ext3_xattr_handler_map[] = {
  95. [EXT3_XATTR_INDEX_USER] = &ext3_xattr_user_handler,
  96. #ifdef CONFIG_EXT3_FS_POSIX_ACL
  97. [EXT3_XATTR_INDEX_POSIX_ACL_ACCESS] = &posix_acl_access_xattr_handler,
  98. [EXT3_XATTR_INDEX_POSIX_ACL_DEFAULT] = &posix_acl_default_xattr_handler,
  99. #endif
  100. [EXT3_XATTR_INDEX_TRUSTED] = &ext3_xattr_trusted_handler,
  101. #ifdef CONFIG_EXT3_FS_SECURITY
  102. [EXT3_XATTR_INDEX_SECURITY] = &ext3_xattr_security_handler,
  103. #endif
  104. };
  105. const struct xattr_handler *ext3_xattr_handlers[] = {
  106. &ext3_xattr_user_handler,
  107. &ext3_xattr_trusted_handler,
  108. #ifdef CONFIG_EXT3_FS_POSIX_ACL
  109. &posix_acl_access_xattr_handler,
  110. &posix_acl_default_xattr_handler,
  111. #endif
  112. #ifdef CONFIG_EXT3_FS_SECURITY
  113. &ext3_xattr_security_handler,
  114. #endif
  115. NULL
  116. };
  117. static inline const struct xattr_handler *
  118. ext3_xattr_handler(int name_index)
  119. {
  120. const struct xattr_handler *handler = NULL;
  121. if (name_index > 0 && name_index < ARRAY_SIZE(ext3_xattr_handler_map))
  122. handler = ext3_xattr_handler_map[name_index];
  123. return handler;
  124. }
  125. /*
  126. * Inode operation listxattr()
  127. *
  128. * d_inode(dentry)->i_mutex: don't care
  129. */
  130. ssize_t
  131. ext3_listxattr(struct dentry *dentry, char *buffer, size_t size)
  132. {
  133. return ext3_xattr_list(dentry, buffer, size);
  134. }
  135. static int
  136. ext3_xattr_check_names(struct ext3_xattr_entry *entry, void *end)
  137. {
  138. while (!IS_LAST_ENTRY(entry)) {
  139. struct ext3_xattr_entry *next = EXT3_XATTR_NEXT(entry);
  140. if ((void *)next >= end)
  141. return -EIO;
  142. entry = next;
  143. }
  144. return 0;
  145. }
  146. static inline int
  147. ext3_xattr_check_block(struct buffer_head *bh)
  148. {
  149. int error;
  150. if (BHDR(bh)->h_magic != cpu_to_le32(EXT3_XATTR_MAGIC) ||
  151. BHDR(bh)->h_blocks != cpu_to_le32(1))
  152. return -EIO;
  153. error = ext3_xattr_check_names(BFIRST(bh), bh->b_data + bh->b_size);
  154. return error;
  155. }
  156. static inline int
  157. ext3_xattr_check_entry(struct ext3_xattr_entry *entry, size_t size)
  158. {
  159. size_t value_size = le32_to_cpu(entry->e_value_size);
  160. if (entry->e_value_block != 0 || value_size > size ||
  161. le16_to_cpu(entry->e_value_offs) + value_size > size)
  162. return -EIO;
  163. return 0;
  164. }
  165. static int
  166. ext3_xattr_find_entry(struct ext3_xattr_entry **pentry, int name_index,
  167. const char *name, size_t size, int sorted)
  168. {
  169. struct ext3_xattr_entry *entry;
  170. size_t name_len;
  171. int cmp = 1;
  172. if (name == NULL)
  173. return -EINVAL;
  174. name_len = strlen(name);
  175. entry = *pentry;
  176. for (; !IS_LAST_ENTRY(entry); entry = EXT3_XATTR_NEXT(entry)) {
  177. cmp = name_index - entry->e_name_index;
  178. if (!cmp)
  179. cmp = name_len - entry->e_name_len;
  180. if (!cmp)
  181. cmp = memcmp(name, entry->e_name, name_len);
  182. if (cmp <= 0 && (sorted || cmp == 0))
  183. break;
  184. }
  185. *pentry = entry;
  186. if (!cmp && ext3_xattr_check_entry(entry, size))
  187. return -EIO;
  188. return cmp ? -ENODATA : 0;
  189. }
  190. static int
  191. ext3_xattr_block_get(struct inode *inode, int name_index, const char *name,
  192. void *buffer, size_t buffer_size)
  193. {
  194. struct buffer_head *bh = NULL;
  195. struct ext3_xattr_entry *entry;
  196. size_t size;
  197. int error;
  198. ea_idebug(inode, "name=%d.%s, buffer=%p, buffer_size=%ld",
  199. name_index, name, buffer, (long)buffer_size);
  200. error = -ENODATA;
  201. if (!EXT3_I(inode)->i_file_acl)
  202. goto cleanup;
  203. ea_idebug(inode, "reading block %u", EXT3_I(inode)->i_file_acl);
  204. bh = sb_bread(inode->i_sb, EXT3_I(inode)->i_file_acl);
  205. if (!bh)
  206. goto cleanup;
  207. ea_bdebug(bh, "b_count=%d, refcount=%d",
  208. atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
  209. if (ext3_xattr_check_block(bh)) {
  210. bad_block: ext3_error(inode->i_sb, __func__,
  211. "inode %lu: bad block "E3FSBLK, inode->i_ino,
  212. EXT3_I(inode)->i_file_acl);
  213. error = -EIO;
  214. goto cleanup;
  215. }
  216. ext3_xattr_cache_insert(bh);
  217. entry = BFIRST(bh);
  218. error = ext3_xattr_find_entry(&entry, name_index, name, bh->b_size, 1);
  219. if (error == -EIO)
  220. goto bad_block;
  221. if (error)
  222. goto cleanup;
  223. size = le32_to_cpu(entry->e_value_size);
  224. if (buffer) {
  225. error = -ERANGE;
  226. if (size > buffer_size)
  227. goto cleanup;
  228. memcpy(buffer, bh->b_data + le16_to_cpu(entry->e_value_offs),
  229. size);
  230. }
  231. error = size;
  232. cleanup:
  233. brelse(bh);
  234. return error;
  235. }
  236. static int
  237. ext3_xattr_ibody_get(struct inode *inode, int name_index, const char *name,
  238. void *buffer, size_t buffer_size)
  239. {
  240. struct ext3_xattr_ibody_header *header;
  241. struct ext3_xattr_entry *entry;
  242. struct ext3_inode *raw_inode;
  243. struct ext3_iloc iloc;
  244. size_t size;
  245. void *end;
  246. int error;
  247. if (!ext3_test_inode_state(inode, EXT3_STATE_XATTR))
  248. return -ENODATA;
  249. error = ext3_get_inode_loc(inode, &iloc);
  250. if (error)
  251. return error;
  252. raw_inode = ext3_raw_inode(&iloc);
  253. header = IHDR(inode, raw_inode);
  254. entry = IFIRST(header);
  255. end = (void *)raw_inode + EXT3_SB(inode->i_sb)->s_inode_size;
  256. error = ext3_xattr_check_names(entry, end);
  257. if (error)
  258. goto cleanup;
  259. error = ext3_xattr_find_entry(&entry, name_index, name,
  260. end - (void *)entry, 0);
  261. if (error)
  262. goto cleanup;
  263. size = le32_to_cpu(entry->e_value_size);
  264. if (buffer) {
  265. error = -ERANGE;
  266. if (size > buffer_size)
  267. goto cleanup;
  268. memcpy(buffer, (void *)IFIRST(header) +
  269. le16_to_cpu(entry->e_value_offs), size);
  270. }
  271. error = size;
  272. cleanup:
  273. brelse(iloc.bh);
  274. return error;
  275. }
  276. /*
  277. * ext3_xattr_get()
  278. *
  279. * Copy an extended attribute into the buffer
  280. * provided, or compute the buffer size required.
  281. * Buffer is NULL to compute the size of the buffer required.
  282. *
  283. * Returns a negative error number on failure, or the number of bytes
  284. * used / required on success.
  285. */
  286. int
  287. ext3_xattr_get(struct inode *inode, int name_index, const char *name,
  288. void *buffer, size_t buffer_size)
  289. {
  290. int error;
  291. down_read(&EXT3_I(inode)->xattr_sem);
  292. error = ext3_xattr_ibody_get(inode, name_index, name, buffer,
  293. buffer_size);
  294. if (error == -ENODATA)
  295. error = ext3_xattr_block_get(inode, name_index, name, buffer,
  296. buffer_size);
  297. up_read(&EXT3_I(inode)->xattr_sem);
  298. return error;
  299. }
  300. static int
  301. ext3_xattr_list_entries(struct dentry *dentry, struct ext3_xattr_entry *entry,
  302. char *buffer, size_t buffer_size)
  303. {
  304. size_t rest = buffer_size;
  305. for (; !IS_LAST_ENTRY(entry); entry = EXT3_XATTR_NEXT(entry)) {
  306. const struct xattr_handler *handler =
  307. ext3_xattr_handler(entry->e_name_index);
  308. if (handler) {
  309. size_t size = handler->list(dentry, buffer, rest,
  310. entry->e_name,
  311. entry->e_name_len,
  312. handler->flags);
  313. if (buffer) {
  314. if (size > rest)
  315. return -ERANGE;
  316. buffer += size;
  317. }
  318. rest -= size;
  319. }
  320. }
  321. return buffer_size - rest;
  322. }
  323. static int
  324. ext3_xattr_block_list(struct dentry *dentry, char *buffer, size_t buffer_size)
  325. {
  326. struct inode *inode = d_inode(dentry);
  327. struct buffer_head *bh = NULL;
  328. int error;
  329. ea_idebug(inode, "buffer=%p, buffer_size=%ld",
  330. buffer, (long)buffer_size);
  331. error = 0;
  332. if (!EXT3_I(inode)->i_file_acl)
  333. goto cleanup;
  334. ea_idebug(inode, "reading block %u", EXT3_I(inode)->i_file_acl);
  335. bh = sb_bread(inode->i_sb, EXT3_I(inode)->i_file_acl);
  336. error = -EIO;
  337. if (!bh)
  338. goto cleanup;
  339. ea_bdebug(bh, "b_count=%d, refcount=%d",
  340. atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
  341. if (ext3_xattr_check_block(bh)) {
  342. ext3_error(inode->i_sb, __func__,
  343. "inode %lu: bad block "E3FSBLK, inode->i_ino,
  344. EXT3_I(inode)->i_file_acl);
  345. error = -EIO;
  346. goto cleanup;
  347. }
  348. ext3_xattr_cache_insert(bh);
  349. error = ext3_xattr_list_entries(dentry, BFIRST(bh), buffer, buffer_size);
  350. cleanup:
  351. brelse(bh);
  352. return error;
  353. }
  354. static int
  355. ext3_xattr_ibody_list(struct dentry *dentry, char *buffer, size_t buffer_size)
  356. {
  357. struct inode *inode = d_inode(dentry);
  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_test_inode_state(inode, 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(dentry, 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. static int
  391. ext3_xattr_list(struct dentry *dentry, char *buffer, size_t buffer_size)
  392. {
  393. int i_error, b_error;
  394. down_read(&EXT3_I(d_inode(dentry))->xattr_sem);
  395. i_error = ext3_xattr_ibody_list(dentry, 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(dentry, buffer, buffer_size);
  404. if (b_error < 0)
  405. i_error = 0;
  406. }
  407. up_read(&EXT3_I(d_inode(dentry))->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. ext3_journal_dirty_metadata(handle, EXT3_SB(sb)->s_sbh);
  422. }
  423. }
  424. /*
  425. * Release the xattr block BH: If the reference count is > 1, decrement
  426. * it; otherwise free the block.
  427. */
  428. static void
  429. ext3_xattr_release_block(handle_t *handle, struct inode *inode,
  430. struct buffer_head *bh)
  431. {
  432. struct mb_cache_entry *ce = NULL;
  433. int error = 0;
  434. ce = mb_cache_entry_get(ext3_xattr_cache, bh->b_bdev, bh->b_blocknr);
  435. error = ext3_journal_get_write_access(handle, bh);
  436. if (error)
  437. goto out;
  438. lock_buffer(bh);
  439. if (BHDR(bh)->h_refcount == cpu_to_le32(1)) {
  440. ea_bdebug(bh, "refcount now=0; freeing");
  441. if (ce)
  442. mb_cache_entry_free(ce);
  443. ext3_free_blocks(handle, inode, bh->b_blocknr, 1);
  444. get_bh(bh);
  445. ext3_forget(handle, 1, inode, bh, bh->b_blocknr);
  446. } else {
  447. le32_add_cpu(&BHDR(bh)->h_refcount, -1);
  448. error = ext3_journal_dirty_metadata(handle, bh);
  449. if (IS_SYNC(inode))
  450. handle->h_sync = 1;
  451. dquot_free_block(inode, 1);
  452. ea_bdebug(bh, "refcount now=%d; releasing",
  453. le32_to_cpu(BHDR(bh)->h_refcount));
  454. if (ce)
  455. mb_cache_entry_release(ce);
  456. }
  457. unlock_buffer(bh);
  458. out:
  459. ext3_std_error(inode->i_sb, error);
  460. return;
  461. }
  462. struct ext3_xattr_info {
  463. int name_index;
  464. const char *name;
  465. const void *value;
  466. size_t value_len;
  467. };
  468. struct ext3_xattr_search {
  469. struct ext3_xattr_entry *first;
  470. void *base;
  471. void *end;
  472. struct ext3_xattr_entry *here;
  473. int not_found;
  474. };
  475. static int
  476. ext3_xattr_set_entry(struct ext3_xattr_info *i, struct ext3_xattr_search *s)
  477. {
  478. struct ext3_xattr_entry *last;
  479. size_t free, min_offs = s->end - s->base, name_len = strlen(i->name);
  480. /* Compute min_offs and last. */
  481. last = s->first;
  482. for (; !IS_LAST_ENTRY(last); last = EXT3_XATTR_NEXT(last)) {
  483. if (!last->e_value_block && last->e_value_size) {
  484. size_t offs = le16_to_cpu(last->e_value_offs);
  485. if (offs < min_offs)
  486. min_offs = offs;
  487. }
  488. }
  489. free = min_offs - ((void *)last - s->base) - sizeof(__u32);
  490. if (!s->not_found) {
  491. if (!s->here->e_value_block && s->here->e_value_size) {
  492. size_t size = le32_to_cpu(s->here->e_value_size);
  493. free += EXT3_XATTR_SIZE(size);
  494. }
  495. free += EXT3_XATTR_LEN(name_len);
  496. }
  497. if (i->value) {
  498. if (free < EXT3_XATTR_LEN(name_len) +
  499. EXT3_XATTR_SIZE(i->value_len))
  500. return -ENOSPC;
  501. }
  502. if (i->value && s->not_found) {
  503. /* Insert the new name. */
  504. size_t size = EXT3_XATTR_LEN(name_len);
  505. size_t rest = (void *)last - (void *)s->here + sizeof(__u32);
  506. memmove((void *)s->here + size, s->here, rest);
  507. memset(s->here, 0, size);
  508. s->here->e_name_index = i->name_index;
  509. s->here->e_name_len = name_len;
  510. memcpy(s->here->e_name, i->name, name_len);
  511. } else {
  512. if (!s->here->e_value_block && s->here->e_value_size) {
  513. void *first_val = s->base + min_offs;
  514. size_t offs = le16_to_cpu(s->here->e_value_offs);
  515. void *val = s->base + offs;
  516. size_t size = EXT3_XATTR_SIZE(
  517. le32_to_cpu(s->here->e_value_size));
  518. if (i->value && size == EXT3_XATTR_SIZE(i->value_len)) {
  519. /* The old and the new value have the same
  520. size. Just replace. */
  521. s->here->e_value_size =
  522. cpu_to_le32(i->value_len);
  523. memset(val + size - EXT3_XATTR_PAD, 0,
  524. EXT3_XATTR_PAD); /* Clear pad bytes. */
  525. memcpy(val, i->value, i->value_len);
  526. return 0;
  527. }
  528. /* Remove the old value. */
  529. memmove(first_val + size, first_val, val - first_val);
  530. memset(first_val, 0, size);
  531. s->here->e_value_size = 0;
  532. s->here->e_value_offs = 0;
  533. min_offs += size;
  534. /* Adjust all value offsets. */
  535. last = s->first;
  536. while (!IS_LAST_ENTRY(last)) {
  537. size_t o = le16_to_cpu(last->e_value_offs);
  538. if (!last->e_value_block &&
  539. last->e_value_size && o < offs)
  540. last->e_value_offs =
  541. cpu_to_le16(o + size);
  542. last = EXT3_XATTR_NEXT(last);
  543. }
  544. }
  545. if (!i->value) {
  546. /* Remove the old name. */
  547. size_t size = EXT3_XATTR_LEN(name_len);
  548. last = ENTRY((void *)last - size);
  549. memmove(s->here, (void *)s->here + size,
  550. (void *)last - (void *)s->here + sizeof(__u32));
  551. memset(last, 0, size);
  552. }
  553. }
  554. if (i->value) {
  555. /* Insert the new value. */
  556. s->here->e_value_size = cpu_to_le32(i->value_len);
  557. if (i->value_len) {
  558. size_t size = EXT3_XATTR_SIZE(i->value_len);
  559. void *val = s->base + min_offs - size;
  560. s->here->e_value_offs = cpu_to_le16(min_offs - size);
  561. memset(val + size - EXT3_XATTR_PAD, 0,
  562. EXT3_XATTR_PAD); /* Clear the pad bytes. */
  563. memcpy(val, i->value, i->value_len);
  564. }
  565. }
  566. return 0;
  567. }
  568. struct ext3_xattr_block_find {
  569. struct ext3_xattr_search s;
  570. struct buffer_head *bh;
  571. };
  572. static int
  573. ext3_xattr_block_find(struct inode *inode, struct ext3_xattr_info *i,
  574. struct ext3_xattr_block_find *bs)
  575. {
  576. struct super_block *sb = inode->i_sb;
  577. int error;
  578. ea_idebug(inode, "name=%d.%s, value=%p, value_len=%ld",
  579. i->name_index, i->name, i->value, (long)i->value_len);
  580. if (EXT3_I(inode)->i_file_acl) {
  581. /* The inode already has an extended attribute block. */
  582. bs->bh = sb_bread(sb, EXT3_I(inode)->i_file_acl);
  583. error = -EIO;
  584. if (!bs->bh)
  585. goto cleanup;
  586. ea_bdebug(bs->bh, "b_count=%d, refcount=%d",
  587. atomic_read(&(bs->bh->b_count)),
  588. le32_to_cpu(BHDR(bs->bh)->h_refcount));
  589. if (ext3_xattr_check_block(bs->bh)) {
  590. ext3_error(sb, __func__,
  591. "inode %lu: bad block "E3FSBLK, inode->i_ino,
  592. EXT3_I(inode)->i_file_acl);
  593. error = -EIO;
  594. goto cleanup;
  595. }
  596. /* Find the named attribute. */
  597. bs->s.base = BHDR(bs->bh);
  598. bs->s.first = BFIRST(bs->bh);
  599. bs->s.end = bs->bh->b_data + bs->bh->b_size;
  600. bs->s.here = bs->s.first;
  601. error = ext3_xattr_find_entry(&bs->s.here, i->name_index,
  602. i->name, bs->bh->b_size, 1);
  603. if (error && error != -ENODATA)
  604. goto cleanup;
  605. bs->s.not_found = error;
  606. }
  607. error = 0;
  608. cleanup:
  609. return error;
  610. }
  611. static int
  612. ext3_xattr_block_set(handle_t *handle, struct inode *inode,
  613. struct ext3_xattr_info *i,
  614. struct ext3_xattr_block_find *bs)
  615. {
  616. struct super_block *sb = inode->i_sb;
  617. struct buffer_head *new_bh = NULL;
  618. struct ext3_xattr_search *s = &bs->s;
  619. struct mb_cache_entry *ce = NULL;
  620. int error = 0;
  621. #define header(x) ((struct ext3_xattr_header *)(x))
  622. if (i->value && i->value_len > sb->s_blocksize)
  623. return -ENOSPC;
  624. if (s->base) {
  625. ce = mb_cache_entry_get(ext3_xattr_cache, bs->bh->b_bdev,
  626. bs->bh->b_blocknr);
  627. error = ext3_journal_get_write_access(handle, bs->bh);
  628. if (error)
  629. goto cleanup;
  630. lock_buffer(bs->bh);
  631. if (header(s->base)->h_refcount == cpu_to_le32(1)) {
  632. if (ce) {
  633. mb_cache_entry_free(ce);
  634. ce = NULL;
  635. }
  636. ea_bdebug(bs->bh, "modifying in-place");
  637. error = ext3_xattr_set_entry(i, s);
  638. if (!error) {
  639. if (!IS_LAST_ENTRY(s->first))
  640. ext3_xattr_rehash(header(s->base),
  641. s->here);
  642. ext3_xattr_cache_insert(bs->bh);
  643. }
  644. unlock_buffer(bs->bh);
  645. if (error == -EIO)
  646. goto bad_block;
  647. if (!error)
  648. error = ext3_journal_dirty_metadata(handle,
  649. bs->bh);
  650. if (error)
  651. goto cleanup;
  652. goto inserted;
  653. } else {
  654. int offset = (char *)s->here - bs->bh->b_data;
  655. unlock_buffer(bs->bh);
  656. journal_release_buffer(handle, bs->bh);
  657. if (ce) {
  658. mb_cache_entry_release(ce);
  659. ce = NULL;
  660. }
  661. ea_bdebug(bs->bh, "cloning");
  662. s->base = kmalloc(bs->bh->b_size, GFP_NOFS);
  663. error = -ENOMEM;
  664. if (s->base == NULL)
  665. goto cleanup;
  666. memcpy(s->base, BHDR(bs->bh), bs->bh->b_size);
  667. s->first = ENTRY(header(s->base)+1);
  668. header(s->base)->h_refcount = cpu_to_le32(1);
  669. s->here = ENTRY(s->base + offset);
  670. s->end = s->base + bs->bh->b_size;
  671. }
  672. } else {
  673. /* Allocate a buffer where we construct the new block. */
  674. s->base = kzalloc(sb->s_blocksize, GFP_NOFS);
  675. /* assert(header == s->base) */
  676. error = -ENOMEM;
  677. if (s->base == NULL)
  678. goto cleanup;
  679. header(s->base)->h_magic = cpu_to_le32(EXT3_XATTR_MAGIC);
  680. header(s->base)->h_blocks = cpu_to_le32(1);
  681. header(s->base)->h_refcount = cpu_to_le32(1);
  682. s->first = ENTRY(header(s->base)+1);
  683. s->here = ENTRY(header(s->base)+1);
  684. s->end = s->base + sb->s_blocksize;
  685. }
  686. error = ext3_xattr_set_entry(i, s);
  687. if (error == -EIO)
  688. goto bad_block;
  689. if (error)
  690. goto cleanup;
  691. if (!IS_LAST_ENTRY(s->first))
  692. ext3_xattr_rehash(header(s->base), s->here);
  693. inserted:
  694. if (!IS_LAST_ENTRY(s->first)) {
  695. new_bh = ext3_xattr_cache_find(inode, header(s->base), &ce);
  696. if (new_bh) {
  697. /* We found an identical block in the cache. */
  698. if (new_bh == bs->bh)
  699. ea_bdebug(new_bh, "keeping");
  700. else {
  701. /* The old block is released after updating
  702. the inode. */
  703. error = dquot_alloc_block(inode, 1);
  704. if (error)
  705. goto cleanup;
  706. error = ext3_journal_get_write_access(handle,
  707. new_bh);
  708. if (error)
  709. goto cleanup_dquot;
  710. lock_buffer(new_bh);
  711. le32_add_cpu(&BHDR(new_bh)->h_refcount, 1);
  712. ea_bdebug(new_bh, "reusing; refcount now=%d",
  713. le32_to_cpu(BHDR(new_bh)->h_refcount));
  714. unlock_buffer(new_bh);
  715. error = ext3_journal_dirty_metadata(handle,
  716. new_bh);
  717. if (error)
  718. goto cleanup_dquot;
  719. }
  720. mb_cache_entry_release(ce);
  721. ce = NULL;
  722. } else if (bs->bh && s->base == bs->bh->b_data) {
  723. /* We were modifying this block in-place. */
  724. ea_bdebug(bs->bh, "keeping this block");
  725. new_bh = bs->bh;
  726. get_bh(new_bh);
  727. } else {
  728. /* We need to allocate a new block */
  729. ext3_fsblk_t goal = ext3_group_first_block_no(sb,
  730. EXT3_I(inode)->i_block_group);
  731. ext3_fsblk_t block;
  732. /*
  733. * Protect us agaist concurrent allocations to the
  734. * same inode from ext3_..._writepage(). Reservation
  735. * code does not expect racing allocations.
  736. */
  737. mutex_lock(&EXT3_I(inode)->truncate_mutex);
  738. block = ext3_new_block(handle, inode, goal, &error);
  739. mutex_unlock(&EXT3_I(inode)->truncate_mutex);
  740. if (error)
  741. goto cleanup;
  742. ea_idebug(inode, "creating block %d", block);
  743. new_bh = sb_getblk(sb, block);
  744. if (unlikely(!new_bh)) {
  745. getblk_failed:
  746. ext3_free_blocks(handle, inode, block, 1);
  747. error = -ENOMEM;
  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, __func__,
  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_test_inode_state(inode, 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_set_inode_state(inode, EXT3_STATE_XATTR);
  837. } else {
  838. header->h_magic = cpu_to_le32(0);
  839. ext3_clear_inode_state(inode, 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. Value
  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. error = ext3_journal_get_write_access(handle, is.iloc.bh);
  882. if (error)
  883. goto cleanup;
  884. if (ext3_test_inode_state(inode, EXT3_STATE_NEW)) {
  885. struct ext3_inode *raw_inode = ext3_raw_inode(&is.iloc);
  886. memset(raw_inode, 0, EXT3_SB(inode->i_sb)->s_inode_size);
  887. ext3_clear_inode_state(inode, EXT3_STATE_NEW);
  888. }
  889. error = ext3_xattr_ibody_find(inode, &i, &is);
  890. if (error)
  891. goto cleanup;
  892. if (is.s.not_found)
  893. error = ext3_xattr_block_find(inode, &i, &bs);
  894. if (error)
  895. goto cleanup;
  896. if (is.s.not_found && bs.s.not_found) {
  897. error = -ENODATA;
  898. if (flags & XATTR_REPLACE)
  899. goto cleanup;
  900. error = 0;
  901. if (!value)
  902. goto cleanup;
  903. } else {
  904. error = -EEXIST;
  905. if (flags & XATTR_CREATE)
  906. goto cleanup;
  907. }
  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. if (EXT3_I(inode)->i_file_acl && !bs.s.base) {
  920. error = ext3_xattr_block_find(inode, &i, &bs);
  921. if (error)
  922. goto cleanup;
  923. }
  924. error = ext3_xattr_block_set(handle, inode, &i, &bs);
  925. if (error)
  926. goto cleanup;
  927. if (!is.s.not_found) {
  928. i.value = NULL;
  929. error = ext3_xattr_ibody_set(handle, inode, &i,
  930. &is);
  931. }
  932. }
  933. }
  934. if (!error) {
  935. ext3_xattr_update_super_block(handle, inode->i_sb);
  936. inode->i_ctime = CURRENT_TIME_SEC;
  937. error = ext3_mark_iloc_dirty(handle, inode, &is.iloc);
  938. /*
  939. * The bh is consumed by ext3_mark_iloc_dirty, even with
  940. * error != 0.
  941. */
  942. is.iloc.bh = NULL;
  943. if (IS_SYNC(inode))
  944. handle->h_sync = 1;
  945. }
  946. cleanup:
  947. brelse(is.iloc.bh);
  948. brelse(bs.bh);
  949. up_write(&EXT3_I(inode)->xattr_sem);
  950. return error;
  951. }
  952. /*
  953. * ext3_xattr_set()
  954. *
  955. * Like ext3_xattr_set_handle, but start from an inode. This extended
  956. * attribute modification is a filesystem transaction by itself.
  957. *
  958. * Returns 0, or a negative error number on failure.
  959. */
  960. int
  961. ext3_xattr_set(struct inode *inode, int name_index, const char *name,
  962. const void *value, size_t value_len, int flags)
  963. {
  964. handle_t *handle;
  965. int error, retries = 0;
  966. retry:
  967. handle = ext3_journal_start(inode, EXT3_DATA_TRANS_BLOCKS(inode->i_sb));
  968. if (IS_ERR(handle)) {
  969. error = PTR_ERR(handle);
  970. } else {
  971. int error2;
  972. error = ext3_xattr_set_handle(handle, inode, name_index, name,
  973. value, value_len, flags);
  974. error2 = ext3_journal_stop(handle);
  975. if (error == -ENOSPC &&
  976. ext3_should_retry_alloc(inode->i_sb, &retries))
  977. goto retry;
  978. if (error == 0)
  979. error = error2;
  980. }
  981. return error;
  982. }
  983. /*
  984. * ext3_xattr_delete_inode()
  985. *
  986. * Free extended attribute resources associated with this inode. This
  987. * is called immediately before an inode is freed. We have exclusive
  988. * access to the inode.
  989. */
  990. void
  991. ext3_xattr_delete_inode(handle_t *handle, struct inode *inode)
  992. {
  993. struct buffer_head *bh = NULL;
  994. if (!EXT3_I(inode)->i_file_acl)
  995. goto cleanup;
  996. bh = sb_bread(inode->i_sb, EXT3_I(inode)->i_file_acl);
  997. if (!bh) {
  998. ext3_error(inode->i_sb, __func__,
  999. "inode %lu: block "E3FSBLK" read error", inode->i_ino,
  1000. EXT3_I(inode)->i_file_acl);
  1001. goto cleanup;
  1002. }
  1003. if (BHDR(bh)->h_magic != cpu_to_le32(EXT3_XATTR_MAGIC) ||
  1004. BHDR(bh)->h_blocks != cpu_to_le32(1)) {
  1005. ext3_error(inode->i_sb, __func__,
  1006. "inode %lu: bad block "E3FSBLK, inode->i_ino,
  1007. EXT3_I(inode)->i_file_acl);
  1008. goto cleanup;
  1009. }
  1010. ext3_xattr_release_block(handle, inode, bh);
  1011. EXT3_I(inode)->i_file_acl = 0;
  1012. cleanup:
  1013. brelse(bh);
  1014. }
  1015. /*
  1016. * ext3_xattr_put_super()
  1017. *
  1018. * This is called when a file system is unmounted.
  1019. */
  1020. void
  1021. ext3_xattr_put_super(struct super_block *sb)
  1022. {
  1023. mb_cache_shrink(sb->s_bdev);
  1024. }
  1025. /*
  1026. * ext3_xattr_cache_insert()
  1027. *
  1028. * Create a new entry in the extended attribute cache, and insert
  1029. * it unless such an entry is already in the cache.
  1030. *
  1031. * Returns 0, or a negative error number on failure.
  1032. */
  1033. static void
  1034. ext3_xattr_cache_insert(struct buffer_head *bh)
  1035. {
  1036. __u32 hash = le32_to_cpu(BHDR(bh)->h_hash);
  1037. struct mb_cache_entry *ce;
  1038. int error;
  1039. ce = mb_cache_entry_alloc(ext3_xattr_cache, GFP_NOFS);
  1040. if (!ce) {
  1041. ea_bdebug(bh, "out of memory");
  1042. return;
  1043. }
  1044. error = mb_cache_entry_insert(ce, bh->b_bdev, bh->b_blocknr, hash);
  1045. if (error) {
  1046. mb_cache_entry_free(ce);
  1047. if (error == -EBUSY) {
  1048. ea_bdebug(bh, "already in cache");
  1049. error = 0;
  1050. }
  1051. } else {
  1052. ea_bdebug(bh, "inserting [%x]", (int)hash);
  1053. mb_cache_entry_release(ce);
  1054. }
  1055. }
  1056. /*
  1057. * ext3_xattr_cmp()
  1058. *
  1059. * Compare two extended attribute blocks for equality.
  1060. *
  1061. * Returns 0 if the blocks are equal, 1 if they differ, and
  1062. * a negative error number on errors.
  1063. */
  1064. static int
  1065. ext3_xattr_cmp(struct ext3_xattr_header *header1,
  1066. struct ext3_xattr_header *header2)
  1067. {
  1068. struct ext3_xattr_entry *entry1, *entry2;
  1069. entry1 = ENTRY(header1+1);
  1070. entry2 = ENTRY(header2+1);
  1071. while (!IS_LAST_ENTRY(entry1)) {
  1072. if (IS_LAST_ENTRY(entry2))
  1073. return 1;
  1074. if (entry1->e_hash != entry2->e_hash ||
  1075. entry1->e_name_index != entry2->e_name_index ||
  1076. entry1->e_name_len != entry2->e_name_len ||
  1077. entry1->e_value_size != entry2->e_value_size ||
  1078. memcmp(entry1->e_name, entry2->e_name, entry1->e_name_len))
  1079. return 1;
  1080. if (entry1->e_value_block != 0 || entry2->e_value_block != 0)
  1081. return -EIO;
  1082. if (memcmp((char *)header1 + le16_to_cpu(entry1->e_value_offs),
  1083. (char *)header2 + le16_to_cpu(entry2->e_value_offs),
  1084. le32_to_cpu(entry1->e_value_size)))
  1085. return 1;
  1086. entry1 = EXT3_XATTR_NEXT(entry1);
  1087. entry2 = EXT3_XATTR_NEXT(entry2);
  1088. }
  1089. if (!IS_LAST_ENTRY(entry2))
  1090. return 1;
  1091. return 0;
  1092. }
  1093. /*
  1094. * ext3_xattr_cache_find()
  1095. *
  1096. * Find an identical extended attribute block.
  1097. *
  1098. * Returns a pointer to the block found, or NULL if such a block was
  1099. * not found or an error occurred.
  1100. */
  1101. static struct buffer_head *
  1102. ext3_xattr_cache_find(struct inode *inode, struct ext3_xattr_header *header,
  1103. struct mb_cache_entry **pce)
  1104. {
  1105. __u32 hash = le32_to_cpu(header->h_hash);
  1106. struct mb_cache_entry *ce;
  1107. if (!header->h_hash)
  1108. return NULL; /* never share */
  1109. ea_idebug(inode, "looking for cached blocks [%x]", (int)hash);
  1110. again:
  1111. ce = mb_cache_entry_find_first(ext3_xattr_cache, inode->i_sb->s_bdev,
  1112. hash);
  1113. while (ce) {
  1114. struct buffer_head *bh;
  1115. if (IS_ERR(ce)) {
  1116. if (PTR_ERR(ce) == -EAGAIN)
  1117. goto again;
  1118. break;
  1119. }
  1120. bh = sb_bread(inode->i_sb, ce->e_block);
  1121. if (!bh) {
  1122. ext3_error(inode->i_sb, __func__,
  1123. "inode %lu: block %lu read error",
  1124. inode->i_ino, (unsigned long) ce->e_block);
  1125. } else if (le32_to_cpu(BHDR(bh)->h_refcount) >=
  1126. EXT3_XATTR_REFCOUNT_MAX) {
  1127. ea_idebug(inode, "block %lu refcount %d>=%d",
  1128. (unsigned long) ce->e_block,
  1129. le32_to_cpu(BHDR(bh)->h_refcount),
  1130. EXT3_XATTR_REFCOUNT_MAX);
  1131. } else if (ext3_xattr_cmp(header, BHDR(bh)) == 0) {
  1132. *pce = ce;
  1133. return bh;
  1134. }
  1135. brelse(bh);
  1136. ce = mb_cache_entry_find_next(ce, inode->i_sb->s_bdev, hash);
  1137. }
  1138. return NULL;
  1139. }
  1140. #define NAME_HASH_SHIFT 5
  1141. #define VALUE_HASH_SHIFT 16
  1142. /*
  1143. * ext3_xattr_hash_entry()
  1144. *
  1145. * Compute the hash of an extended attribute.
  1146. */
  1147. static inline void ext3_xattr_hash_entry(struct ext3_xattr_header *header,
  1148. struct ext3_xattr_entry *entry)
  1149. {
  1150. __u32 hash = 0;
  1151. char *name = entry->e_name;
  1152. int n;
  1153. for (n=0; n < entry->e_name_len; n++) {
  1154. hash = (hash << NAME_HASH_SHIFT) ^
  1155. (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^
  1156. *name++;
  1157. }
  1158. if (entry->e_value_block == 0 && entry->e_value_size != 0) {
  1159. __le32 *value = (__le32 *)((char *)header +
  1160. le16_to_cpu(entry->e_value_offs));
  1161. for (n = (le32_to_cpu(entry->e_value_size) +
  1162. EXT3_XATTR_ROUND) >> EXT3_XATTR_PAD_BITS; n; n--) {
  1163. hash = (hash << VALUE_HASH_SHIFT) ^
  1164. (hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^
  1165. le32_to_cpu(*value++);
  1166. }
  1167. }
  1168. entry->e_hash = cpu_to_le32(hash);
  1169. }
  1170. #undef NAME_HASH_SHIFT
  1171. #undef VALUE_HASH_SHIFT
  1172. #define BLOCK_HASH_SHIFT 16
  1173. /*
  1174. * ext3_xattr_rehash()
  1175. *
  1176. * Re-compute the extended attribute hash value after an entry has changed.
  1177. */
  1178. static void ext3_xattr_rehash(struct ext3_xattr_header *header,
  1179. struct ext3_xattr_entry *entry)
  1180. {
  1181. struct ext3_xattr_entry *here;
  1182. __u32 hash = 0;
  1183. ext3_xattr_hash_entry(header, entry);
  1184. here = ENTRY(header+1);
  1185. while (!IS_LAST_ENTRY(here)) {
  1186. if (!here->e_hash) {
  1187. /* Block is not shared if an entry's hash value == 0 */
  1188. hash = 0;
  1189. break;
  1190. }
  1191. hash = (hash << BLOCK_HASH_SHIFT) ^
  1192. (hash >> (8*sizeof(hash) - BLOCK_HASH_SHIFT)) ^
  1193. le32_to_cpu(here->e_hash);
  1194. here = EXT3_XATTR_NEXT(here);
  1195. }
  1196. header->h_hash = cpu_to_le32(hash);
  1197. }
  1198. #undef BLOCK_HASH_SHIFT
  1199. int __init
  1200. init_ext3_xattr(void)
  1201. {
  1202. ext3_xattr_cache = mb_cache_create("ext3_xattr", 6);
  1203. if (!ext3_xattr_cache)
  1204. return -ENOMEM;
  1205. return 0;
  1206. }
  1207. void
  1208. exit_ext3_xattr(void)
  1209. {
  1210. if (ext3_xattr_cache)
  1211. mb_cache_destroy(ext3_xattr_cache);
  1212. ext3_xattr_cache = NULL;
  1213. }