xattr.c 35 KB

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