xattr.c 34 KB

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