xattr.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317
  1. /*
  2. * linux/fs/ext3/xattr.c
  3. *
  4. * Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de>
  5. *
  6. * Fix by Harrison Xing <harrison@mountainviewdata.com>.
  7. * Ext3 code with a lot of help from Eric Jarman <ejarman@acm.org>.
  8. * Extended attributes for symlinks and special files added per
  9. * suggestion of Luka Renko <luka.renko@hermes.si>.
  10. * xattr consolidation Copyright (c) 2004 James Morris <jmorris@redhat.com>,
  11. * Red Hat Inc.
  12. * ea-in-inode support by Alex Tomas <alex@clusterfs.com> aka bzzz
  13. * and Andreas Gruenbacher <agruen@suse.de>.
  14. */
  15. /*
  16. * Extended attributes are stored directly in inodes (on file systems with
  17. * inodes bigger than 128 bytes) and on additional disk blocks. The i_file_acl
  18. * field contains the block number if an inode uses an additional block. All
  19. * attributes must fit in the inode and one additional block. Blocks that
  20. * contain the identical set of attributes may be shared among several inodes.
  21. * Identical blocks are detected by keeping a cache of blocks that have
  22. * recently been accessed.
  23. *
  24. * The attributes in inodes and on blocks have a different header; the entries
  25. * are stored in the same format:
  26. *
  27. * +------------------+
  28. * | header |
  29. * | entry 1 | |
  30. * | entry 2 | | growing downwards
  31. * | entry 3 | v
  32. * | four null bytes |
  33. * | . . . |
  34. * | value 1 | ^
  35. * | value 3 | | growing upwards
  36. * | value 2 | |
  37. * +------------------+
  38. *
  39. * The header is followed by multiple entry descriptors. In disk blocks, the
  40. * entry descriptors are kept sorted. In inodes, they are unsorted. The
  41. * attribute values are aligned to the end of the block in no specific order.
  42. *
  43. * Locking strategy
  44. * ----------------
  45. * EXT3_I(inode)->i_file_acl is protected by EXT3_I(inode)->xattr_sem.
  46. * EA blocks are only changed if they are exclusive to an inode, so
  47. * holding xattr_sem also means that nothing but the EA block's reference
  48. * count can change. Multiple writers to the same block are synchronized
  49. * by the buffer lock.
  50. */
  51. #include <linux/init.h>
  52. #include <linux/fs.h>
  53. #include <linux/slab.h>
  54. #include <linux/ext3_jbd.h>
  55. #include <linux/ext3_fs.h>
  56. #include <linux/mbcache.h>
  57. #include <linux/quotaops.h>
  58. #include <linux/rwsem.h>
  59. #include "xattr.h"
  60. #include "acl.h"
  61. #define BHDR(bh) ((struct ext3_xattr_header *)((bh)->b_data))
  62. #define ENTRY(ptr) ((struct ext3_xattr_entry *)(ptr))
  63. #define BFIRST(bh) ENTRY(BHDR(bh)+1)
  64. #define IS_LAST_ENTRY(entry) (*(__u32 *)(entry) == 0)
  65. #define IHDR(inode, raw_inode) \
  66. ((struct ext3_xattr_ibody_header *) \
  67. ((void *)raw_inode + \
  68. EXT3_GOOD_OLD_INODE_SIZE + \
  69. EXT3_I(inode)->i_extra_isize))
  70. #define IFIRST(hdr) ((struct ext3_xattr_entry *)((hdr)+1))
  71. #ifdef EXT3_XATTR_DEBUG
  72. # define ea_idebug(inode, f...) do { \
  73. printk(KERN_DEBUG "inode %s:%lu: ", \
  74. inode->i_sb->s_id, inode->i_ino); \
  75. printk(f); \
  76. printk("\n"); \
  77. } while (0)
  78. # define ea_bdebug(bh, f...) do { \
  79. char b[BDEVNAME_SIZE]; \
  80. printk(KERN_DEBUG "block %s:%lu: ", \
  81. bdevname(bh->b_bdev, b), \
  82. (unsigned long) bh->b_blocknr); \
  83. printk(f); \
  84. printk("\n"); \
  85. } while (0)
  86. #else
  87. # define ea_idebug(f...)
  88. # define ea_bdebug(f...)
  89. #endif
  90. static void ext3_xattr_cache_insert(struct buffer_head *);
  91. static struct buffer_head *ext3_xattr_cache_find(struct inode *,
  92. struct ext3_xattr_header *,
  93. struct mb_cache_entry **);
  94. static void ext3_xattr_rehash(struct ext3_xattr_header *,
  95. struct ext3_xattr_entry *);
  96. static struct mb_cache *ext3_xattr_cache;
  97. static struct xattr_handler *ext3_xattr_handler_map[] = {
  98. [EXT3_XATTR_INDEX_USER] = &ext3_xattr_user_handler,
  99. #ifdef CONFIG_EXT3_FS_POSIX_ACL
  100. [EXT3_XATTR_INDEX_POSIX_ACL_ACCESS] = &ext3_xattr_acl_access_handler,
  101. [EXT3_XATTR_INDEX_POSIX_ACL_DEFAULT] = &ext3_xattr_acl_default_handler,
  102. #endif
  103. [EXT3_XATTR_INDEX_TRUSTED] = &ext3_xattr_trusted_handler,
  104. #ifdef CONFIG_EXT3_FS_SECURITY
  105. [EXT3_XATTR_INDEX_SECURITY] = &ext3_xattr_security_handler,
  106. #endif
  107. };
  108. struct xattr_handler *ext3_xattr_handlers[] = {
  109. &ext3_xattr_user_handler,
  110. &ext3_xattr_trusted_handler,
  111. #ifdef CONFIG_EXT3_FS_POSIX_ACL
  112. &ext3_xattr_acl_access_handler,
  113. &ext3_xattr_acl_default_handler,
  114. #endif
  115. #ifdef CONFIG_EXT3_FS_SECURITY
  116. &ext3_xattr_security_handler,
  117. #endif
  118. NULL
  119. };
  120. static inline struct xattr_handler *
  121. ext3_xattr_handler(int name_index)
  122. {
  123. struct xattr_handler *handler = NULL;
  124. if (name_index > 0 && name_index < ARRAY_SIZE(ext3_xattr_handler_map))
  125. handler = ext3_xattr_handler_map[name_index];
  126. return handler;
  127. }
  128. /*
  129. * Inode operation listxattr()
  130. *
  131. * dentry->d_inode->i_mutex: don't care
  132. */
  133. ssize_t
  134. ext3_listxattr(struct dentry *dentry, char *buffer, size_t size)
  135. {
  136. return ext3_xattr_list(dentry->d_inode, buffer, size);
  137. }
  138. static int
  139. ext3_xattr_check_names(struct ext3_xattr_entry *entry, void *end)
  140. {
  141. while (!IS_LAST_ENTRY(entry)) {
  142. struct ext3_xattr_entry *next = EXT3_XATTR_NEXT(entry);
  143. if ((void *)next >= end)
  144. return -EIO;
  145. entry = next;
  146. }
  147. return 0;
  148. }
  149. static inline int
  150. ext3_xattr_check_block(struct buffer_head *bh)
  151. {
  152. int error;
  153. if (BHDR(bh)->h_magic != cpu_to_le32(EXT3_XATTR_MAGIC) ||
  154. BHDR(bh)->h_blocks != cpu_to_le32(1))
  155. return -EIO;
  156. error = ext3_xattr_check_names(BFIRST(bh), bh->b_data + bh->b_size);
  157. return error;
  158. }
  159. static inline int
  160. ext3_xattr_check_entry(struct ext3_xattr_entry *entry, size_t size)
  161. {
  162. size_t value_size = le32_to_cpu(entry->e_value_size);
  163. if (entry->e_value_block != 0 || value_size > size ||
  164. le16_to_cpu(entry->e_value_offs) + value_size > size)
  165. return -EIO;
  166. return 0;
  167. }
  168. static int
  169. ext3_xattr_find_entry(struct ext3_xattr_entry **pentry, int name_index,
  170. const char *name, size_t size, int sorted)
  171. {
  172. struct ext3_xattr_entry *entry;
  173. size_t name_len;
  174. int cmp = 1;
  175. if (name == NULL)
  176. return -EINVAL;
  177. name_len = strlen(name);
  178. entry = *pentry;
  179. for (; !IS_LAST_ENTRY(entry); entry = EXT3_XATTR_NEXT(entry)) {
  180. cmp = name_index - entry->e_name_index;
  181. if (!cmp)
  182. cmp = name_len - entry->e_name_len;
  183. if (!cmp)
  184. cmp = memcmp(name, entry->e_name, name_len);
  185. if (cmp <= 0 && (sorted || cmp == 0))
  186. break;
  187. }
  188. *pentry = entry;
  189. if (!cmp && ext3_xattr_check_entry(entry, size))
  190. return -EIO;
  191. return cmp ? -ENODATA : 0;
  192. }
  193. static int
  194. ext3_xattr_block_get(struct inode *inode, int name_index, const char *name,
  195. void *buffer, size_t buffer_size)
  196. {
  197. struct buffer_head *bh = NULL;
  198. struct ext3_xattr_entry *entry;
  199. size_t size;
  200. int error;
  201. ea_idebug(inode, "name=%d.%s, buffer=%p, buffer_size=%ld",
  202. name_index, name, buffer, (long)buffer_size);
  203. error = -ENODATA;
  204. if (!EXT3_I(inode)->i_file_acl)
  205. goto cleanup;
  206. ea_idebug(inode, "reading block %u", EXT3_I(inode)->i_file_acl);
  207. bh = sb_bread(inode->i_sb, EXT3_I(inode)->i_file_acl);
  208. if (!bh)
  209. goto cleanup;
  210. ea_bdebug(bh, "b_count=%d, refcount=%d",
  211. atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
  212. if (ext3_xattr_check_block(bh)) {
  213. bad_block: ext3_error(inode->i_sb, __FUNCTION__,
  214. "inode %lu: bad block "E3FSBLK, inode->i_ino,
  215. EXT3_I(inode)->i_file_acl);
  216. error = -EIO;
  217. goto cleanup;
  218. }
  219. ext3_xattr_cache_insert(bh);
  220. entry = BFIRST(bh);
  221. error = ext3_xattr_find_entry(&entry, name_index, name, bh->b_size, 1);
  222. if (error == -EIO)
  223. goto bad_block;
  224. if (error)
  225. goto cleanup;
  226. size = le32_to_cpu(entry->e_value_size);
  227. if (buffer) {
  228. error = -ERANGE;
  229. if (size > buffer_size)
  230. goto cleanup;
  231. memcpy(buffer, bh->b_data + le16_to_cpu(entry->e_value_offs),
  232. size);
  233. }
  234. error = size;
  235. cleanup:
  236. brelse(bh);
  237. return error;
  238. }
  239. static int
  240. ext3_xattr_ibody_get(struct inode *inode, int name_index, const char *name,
  241. void *buffer, size_t buffer_size)
  242. {
  243. struct ext3_xattr_ibody_header *header;
  244. struct ext3_xattr_entry *entry;
  245. struct ext3_inode *raw_inode;
  246. struct ext3_iloc iloc;
  247. size_t size;
  248. void *end;
  249. int error;
  250. if (!(EXT3_I(inode)->i_state & EXT3_STATE_XATTR))
  251. return -ENODATA;
  252. error = ext3_get_inode_loc(inode, &iloc);
  253. if (error)
  254. return error;
  255. raw_inode = ext3_raw_inode(&iloc);
  256. header = IHDR(inode, raw_inode);
  257. entry = IFIRST(header);
  258. end = (void *)raw_inode + EXT3_SB(inode->i_sb)->s_inode_size;
  259. error = ext3_xattr_check_names(entry, end);
  260. if (error)
  261. goto cleanup;
  262. error = ext3_xattr_find_entry(&entry, name_index, name,
  263. end - (void *)entry, 0);
  264. if (error)
  265. goto cleanup;
  266. size = le32_to_cpu(entry->e_value_size);
  267. if (buffer) {
  268. error = -ERANGE;
  269. if (size > buffer_size)
  270. goto cleanup;
  271. memcpy(buffer, (void *)IFIRST(header) +
  272. le16_to_cpu(entry->e_value_offs), size);
  273. }
  274. error = size;
  275. cleanup:
  276. brelse(iloc.bh);
  277. return error;
  278. }
  279. /*
  280. * ext3_xattr_get()
  281. *
  282. * Copy an extended attribute into the buffer
  283. * provided, or compute the buffer size required.
  284. * Buffer is NULL to compute the size of the buffer required.
  285. *
  286. * Returns a negative error number on failure, or the number of bytes
  287. * used / required on success.
  288. */
  289. int
  290. ext3_xattr_get(struct inode *inode, int name_index, const char *name,
  291. void *buffer, size_t buffer_size)
  292. {
  293. int error;
  294. down_read(&EXT3_I(inode)->xattr_sem);
  295. error = ext3_xattr_ibody_get(inode, name_index, name, buffer,
  296. buffer_size);
  297. if (error == -ENODATA)
  298. error = ext3_xattr_block_get(inode, name_index, name, buffer,
  299. buffer_size);
  300. up_read(&EXT3_I(inode)->xattr_sem);
  301. return error;
  302. }
  303. static int
  304. ext3_xattr_list_entries(struct inode *inode, struct ext3_xattr_entry *entry,
  305. char *buffer, size_t buffer_size)
  306. {
  307. size_t rest = buffer_size;
  308. for (; !IS_LAST_ENTRY(entry); entry = EXT3_XATTR_NEXT(entry)) {
  309. struct xattr_handler *handler =
  310. ext3_xattr_handler(entry->e_name_index);
  311. if (handler) {
  312. size_t size = handler->list(inode, buffer, rest,
  313. entry->e_name,
  314. entry->e_name_len);
  315. if (buffer) {
  316. if (size > rest)
  317. return -ERANGE;
  318. buffer += size;
  319. }
  320. rest -= size;
  321. }
  322. }
  323. return buffer_size - rest;
  324. }
  325. static int
  326. ext3_xattr_block_list(struct inode *inode, char *buffer, size_t buffer_size)
  327. {
  328. struct buffer_head *bh = NULL;
  329. int error;
  330. ea_idebug(inode, "buffer=%p, buffer_size=%ld",
  331. buffer, (long)buffer_size);
  332. error = 0;
  333. if (!EXT3_I(inode)->i_file_acl)
  334. goto cleanup;
  335. ea_idebug(inode, "reading block %u", EXT3_I(inode)->i_file_acl);
  336. bh = sb_bread(inode->i_sb, EXT3_I(inode)->i_file_acl);
  337. error = -EIO;
  338. if (!bh)
  339. goto cleanup;
  340. ea_bdebug(bh, "b_count=%d, refcount=%d",
  341. atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
  342. if (ext3_xattr_check_block(bh)) {
  343. ext3_error(inode->i_sb, __FUNCTION__,
  344. "inode %lu: bad block "E3FSBLK, inode->i_ino,
  345. EXT3_I(inode)->i_file_acl);
  346. error = -EIO;
  347. goto cleanup;
  348. }
  349. ext3_xattr_cache_insert(bh);
  350. error = ext3_xattr_list_entries(inode, BFIRST(bh), buffer, buffer_size);
  351. cleanup:
  352. brelse(bh);
  353. return error;
  354. }
  355. static int
  356. ext3_xattr_ibody_list(struct inode *inode, char *buffer, size_t buffer_size)
  357. {
  358. struct ext3_xattr_ibody_header *header;
  359. struct ext3_inode *raw_inode;
  360. struct ext3_iloc iloc;
  361. void *end;
  362. int error;
  363. if (!(EXT3_I(inode)->i_state & EXT3_STATE_XATTR))
  364. return 0;
  365. error = ext3_get_inode_loc(inode, &iloc);
  366. if (error)
  367. return error;
  368. raw_inode = ext3_raw_inode(&iloc);
  369. header = IHDR(inode, raw_inode);
  370. end = (void *)raw_inode + EXT3_SB(inode->i_sb)->s_inode_size;
  371. error = ext3_xattr_check_names(IFIRST(header), end);
  372. if (error)
  373. goto cleanup;
  374. error = ext3_xattr_list_entries(inode, IFIRST(header),
  375. buffer, buffer_size);
  376. cleanup:
  377. brelse(iloc.bh);
  378. return error;
  379. }
  380. /*
  381. * ext3_xattr_list()
  382. *
  383. * Copy a list of attribute names into the buffer
  384. * provided, or compute the buffer size required.
  385. * Buffer is NULL to compute the size of the buffer required.
  386. *
  387. * Returns a negative error number on failure, or the number of bytes
  388. * used / required on success.
  389. */
  390. int
  391. ext3_xattr_list(struct inode *inode, char *buffer, size_t buffer_size)
  392. {
  393. int i_error, b_error;
  394. down_read(&EXT3_I(inode)->xattr_sem);
  395. i_error = ext3_xattr_ibody_list(inode, buffer, buffer_size);
  396. if (i_error < 0) {
  397. b_error = 0;
  398. } else {
  399. if (buffer) {
  400. buffer += i_error;
  401. buffer_size -= i_error;
  402. }
  403. b_error = ext3_xattr_block_list(inode, buffer, buffer_size);
  404. if (b_error < 0)
  405. i_error = 0;
  406. }
  407. up_read(&EXT3_I(inode)->xattr_sem);
  408. return i_error + b_error;
  409. }
  410. /*
  411. * If the EXT3_FEATURE_COMPAT_EXT_ATTR feature of this file system is
  412. * not set, set it.
  413. */
  414. static void ext3_xattr_update_super_block(handle_t *handle,
  415. struct super_block *sb)
  416. {
  417. if (EXT3_HAS_COMPAT_FEATURE(sb, EXT3_FEATURE_COMPAT_EXT_ATTR))
  418. return;
  419. lock_super(sb);
  420. if (ext3_journal_get_write_access(handle, EXT3_SB(sb)->s_sbh) == 0) {
  421. EXT3_SB(sb)->s_es->s_feature_compat |=
  422. cpu_to_le32(EXT3_FEATURE_COMPAT_EXT_ATTR);
  423. sb->s_dirt = 1;
  424. ext3_journal_dirty_metadata(handle, EXT3_SB(sb)->s_sbh);
  425. }
  426. unlock_super(sb);
  427. }
  428. /*
  429. * Release the xattr block BH: If the reference count is > 1, decrement
  430. * it; otherwise free the block.
  431. */
  432. static void
  433. ext3_xattr_release_block(handle_t *handle, struct inode *inode,
  434. struct buffer_head *bh)
  435. {
  436. struct mb_cache_entry *ce = NULL;
  437. ce = mb_cache_entry_get(ext3_xattr_cache, bh->b_bdev, bh->b_blocknr);
  438. if (BHDR(bh)->h_refcount == cpu_to_le32(1)) {
  439. ea_bdebug(bh, "refcount now=0; freeing");
  440. if (ce)
  441. mb_cache_entry_free(ce);
  442. ext3_free_blocks(handle, inode, bh->b_blocknr, 1);
  443. get_bh(bh);
  444. ext3_forget(handle, 1, inode, bh, bh->b_blocknr);
  445. } else {
  446. if (ext3_journal_get_write_access(handle, bh) == 0) {
  447. lock_buffer(bh);
  448. BHDR(bh)->h_refcount = cpu_to_le32(
  449. le32_to_cpu(BHDR(bh)->h_refcount) - 1);
  450. ext3_journal_dirty_metadata(handle, bh);
  451. if (IS_SYNC(inode))
  452. handle->h_sync = 1;
  453. DQUOT_FREE_BLOCK(inode, 1);
  454. unlock_buffer(bh);
  455. ea_bdebug(bh, "refcount now=%d; releasing",
  456. le32_to_cpu(BHDR(bh)->h_refcount));
  457. }
  458. if (ce)
  459. mb_cache_entry_release(ce);
  460. }
  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, __FUNCTION__,
  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;
  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. if (header(s->base)->h_refcount == cpu_to_le32(1)) {
  629. if (ce) {
  630. mb_cache_entry_free(ce);
  631. ce = NULL;
  632. }
  633. ea_bdebug(bs->bh, "modifying in-place");
  634. error = ext3_journal_get_write_access(handle, bs->bh);
  635. if (error)
  636. goto cleanup;
  637. lock_buffer(bs->bh);
  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. if (ce) {
  657. mb_cache_entry_release(ce);
  658. ce = NULL;
  659. }
  660. ea_bdebug(bs->bh, "cloning");
  661. s->base = kmalloc(bs->bh->b_size, GFP_KERNEL);
  662. error = -ENOMEM;
  663. if (s->base == NULL)
  664. goto cleanup;
  665. memcpy(s->base, BHDR(bs->bh), bs->bh->b_size);
  666. s->first = ENTRY(header(s->base)+1);
  667. header(s->base)->h_refcount = cpu_to_le32(1);
  668. s->here = ENTRY(s->base + offset);
  669. s->end = s->base + bs->bh->b_size;
  670. }
  671. } else {
  672. /* Allocate a buffer where we construct the new block. */
  673. s->base = kmalloc(sb->s_blocksize, GFP_KERNEL);
  674. /* assert(header == s->base) */
  675. error = -ENOMEM;
  676. if (s->base == NULL)
  677. goto cleanup;
  678. memset(s->base, 0, sb->s_blocksize);
  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 = -EDQUOT;
  704. if (DQUOT_ALLOC_BLOCK(inode, 1))
  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. BHDR(new_bh)->h_refcount = cpu_to_le32(1 +
  712. le32_to_cpu(BHDR(new_bh)->h_refcount));
  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 = le32_to_cpu(
  731. EXT3_SB(sb)->s_es->s_first_data_block) +
  732. (ext3_fsblk_t)EXT3_I(inode)->i_block_group *
  733. EXT3_BLOCKS_PER_GROUP(sb);
  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. DQUOT_FREE_BLOCK(inode, 1);
  776. goto cleanup;
  777. bad_block:
  778. ext3_error(inode->i_sb, __FUNCTION__,
  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. error = ext3_xattr_block_set(handle, inode, &i, &bs);
  916. if (error)
  917. goto cleanup;
  918. if (!is.s.not_found) {
  919. i.value = NULL;
  920. error = ext3_xattr_ibody_set(handle, inode, &i,
  921. &is);
  922. }
  923. }
  924. }
  925. if (!error) {
  926. ext3_xattr_update_super_block(handle, inode->i_sb);
  927. inode->i_ctime = CURRENT_TIME_SEC;
  928. error = ext3_mark_iloc_dirty(handle, inode, &is.iloc);
  929. /*
  930. * The bh is consumed by ext3_mark_iloc_dirty, even with
  931. * error != 0.
  932. */
  933. is.iloc.bh = NULL;
  934. if (IS_SYNC(inode))
  935. handle->h_sync = 1;
  936. }
  937. cleanup:
  938. brelse(is.iloc.bh);
  939. brelse(bs.bh);
  940. up_write(&EXT3_I(inode)->xattr_sem);
  941. return error;
  942. }
  943. /*
  944. * ext3_xattr_set()
  945. *
  946. * Like ext3_xattr_set_handle, but start from an inode. This extended
  947. * attribute modification is a filesystem transaction by itself.
  948. *
  949. * Returns 0, or a negative error number on failure.
  950. */
  951. int
  952. ext3_xattr_set(struct inode *inode, int name_index, const char *name,
  953. const void *value, size_t value_len, int flags)
  954. {
  955. handle_t *handle;
  956. int error, retries = 0;
  957. retry:
  958. handle = ext3_journal_start(inode, EXT3_DATA_TRANS_BLOCKS(inode->i_sb));
  959. if (IS_ERR(handle)) {
  960. error = PTR_ERR(handle);
  961. } else {
  962. int error2;
  963. error = ext3_xattr_set_handle(handle, inode, name_index, name,
  964. value, value_len, flags);
  965. error2 = ext3_journal_stop(handle);
  966. if (error == -ENOSPC &&
  967. ext3_should_retry_alloc(inode->i_sb, &retries))
  968. goto retry;
  969. if (error == 0)
  970. error = error2;
  971. }
  972. return error;
  973. }
  974. /*
  975. * ext3_xattr_delete_inode()
  976. *
  977. * Free extended attribute resources associated with this inode. This
  978. * is called immediately before an inode is freed. We have exclusive
  979. * access to the inode.
  980. */
  981. void
  982. ext3_xattr_delete_inode(handle_t *handle, struct inode *inode)
  983. {
  984. struct buffer_head *bh = NULL;
  985. if (!EXT3_I(inode)->i_file_acl)
  986. goto cleanup;
  987. bh = sb_bread(inode->i_sb, EXT3_I(inode)->i_file_acl);
  988. if (!bh) {
  989. ext3_error(inode->i_sb, __FUNCTION__,
  990. "inode %lu: block "E3FSBLK" read error", inode->i_ino,
  991. EXT3_I(inode)->i_file_acl);
  992. goto cleanup;
  993. }
  994. if (BHDR(bh)->h_magic != cpu_to_le32(EXT3_XATTR_MAGIC) ||
  995. BHDR(bh)->h_blocks != cpu_to_le32(1)) {
  996. ext3_error(inode->i_sb, __FUNCTION__,
  997. "inode %lu: bad block "E3FSBLK, inode->i_ino,
  998. EXT3_I(inode)->i_file_acl);
  999. goto cleanup;
  1000. }
  1001. ext3_xattr_release_block(handle, inode, bh);
  1002. EXT3_I(inode)->i_file_acl = 0;
  1003. cleanup:
  1004. brelse(bh);
  1005. }
  1006. /*
  1007. * ext3_xattr_put_super()
  1008. *
  1009. * This is called when a file system is unmounted.
  1010. */
  1011. void
  1012. ext3_xattr_put_super(struct super_block *sb)
  1013. {
  1014. mb_cache_shrink(sb->s_bdev);
  1015. }
  1016. /*
  1017. * ext3_xattr_cache_insert()
  1018. *
  1019. * Create a new entry in the extended attribute cache, and insert
  1020. * it unless such an entry is already in the cache.
  1021. *
  1022. * Returns 0, or a negative error number on failure.
  1023. */
  1024. static void
  1025. ext3_xattr_cache_insert(struct buffer_head *bh)
  1026. {
  1027. __u32 hash = le32_to_cpu(BHDR(bh)->h_hash);
  1028. struct mb_cache_entry *ce;
  1029. int error;
  1030. ce = mb_cache_entry_alloc(ext3_xattr_cache);
  1031. if (!ce) {
  1032. ea_bdebug(bh, "out of memory");
  1033. return;
  1034. }
  1035. error = mb_cache_entry_insert(ce, bh->b_bdev, bh->b_blocknr, &hash);
  1036. if (error) {
  1037. mb_cache_entry_free(ce);
  1038. if (error == -EBUSY) {
  1039. ea_bdebug(bh, "already in cache");
  1040. error = 0;
  1041. }
  1042. } else {
  1043. ea_bdebug(bh, "inserting [%x]", (int)hash);
  1044. mb_cache_entry_release(ce);
  1045. }
  1046. }
  1047. /*
  1048. * ext3_xattr_cmp()
  1049. *
  1050. * Compare two extended attribute blocks for equality.
  1051. *
  1052. * Returns 0 if the blocks are equal, 1 if they differ, and
  1053. * a negative error number on errors.
  1054. */
  1055. static int
  1056. ext3_xattr_cmp(struct ext3_xattr_header *header1,
  1057. struct ext3_xattr_header *header2)
  1058. {
  1059. struct ext3_xattr_entry *entry1, *entry2;
  1060. entry1 = ENTRY(header1+1);
  1061. entry2 = ENTRY(header2+1);
  1062. while (!IS_LAST_ENTRY(entry1)) {
  1063. if (IS_LAST_ENTRY(entry2))
  1064. return 1;
  1065. if (entry1->e_hash != entry2->e_hash ||
  1066. entry1->e_name_index != entry2->e_name_index ||
  1067. entry1->e_name_len != entry2->e_name_len ||
  1068. entry1->e_value_size != entry2->e_value_size ||
  1069. memcmp(entry1->e_name, entry2->e_name, entry1->e_name_len))
  1070. return 1;
  1071. if (entry1->e_value_block != 0 || entry2->e_value_block != 0)
  1072. return -EIO;
  1073. if (memcmp((char *)header1 + le16_to_cpu(entry1->e_value_offs),
  1074. (char *)header2 + le16_to_cpu(entry2->e_value_offs),
  1075. le32_to_cpu(entry1->e_value_size)))
  1076. return 1;
  1077. entry1 = EXT3_XATTR_NEXT(entry1);
  1078. entry2 = EXT3_XATTR_NEXT(entry2);
  1079. }
  1080. if (!IS_LAST_ENTRY(entry2))
  1081. return 1;
  1082. return 0;
  1083. }
  1084. /*
  1085. * ext3_xattr_cache_find()
  1086. *
  1087. * Find an identical extended attribute block.
  1088. *
  1089. * Returns a pointer to the block found, or NULL if such a block was
  1090. * not found or an error occurred.
  1091. */
  1092. static struct buffer_head *
  1093. ext3_xattr_cache_find(struct inode *inode, struct ext3_xattr_header *header,
  1094. struct mb_cache_entry **pce)
  1095. {
  1096. __u32 hash = le32_to_cpu(header->h_hash);
  1097. struct mb_cache_entry *ce;
  1098. if (!header->h_hash)
  1099. return NULL; /* never share */
  1100. ea_idebug(inode, "looking for cached blocks [%x]", (int)hash);
  1101. again:
  1102. ce = mb_cache_entry_find_first(ext3_xattr_cache, 0,
  1103. inode->i_sb->s_bdev, hash);
  1104. while (ce) {
  1105. struct buffer_head *bh;
  1106. if (IS_ERR(ce)) {
  1107. if (PTR_ERR(ce) == -EAGAIN)
  1108. goto again;
  1109. break;
  1110. }
  1111. bh = sb_bread(inode->i_sb, ce->e_block);
  1112. if (!bh) {
  1113. ext3_error(inode->i_sb, __FUNCTION__,
  1114. "inode %lu: block %lu read error",
  1115. inode->i_ino, (unsigned long) ce->e_block);
  1116. } else if (le32_to_cpu(BHDR(bh)->h_refcount) >=
  1117. EXT3_XATTR_REFCOUNT_MAX) {
  1118. ea_idebug(inode, "block %lu refcount %d>=%d",
  1119. (unsigned long) ce->e_block,
  1120. le32_to_cpu(BHDR(bh)->h_refcount),
  1121. EXT3_XATTR_REFCOUNT_MAX);
  1122. } else if (ext3_xattr_cmp(header, BHDR(bh)) == 0) {
  1123. *pce = ce;
  1124. return bh;
  1125. }
  1126. brelse(bh);
  1127. ce = mb_cache_entry_find_next(ce, 0, inode->i_sb->s_bdev, hash);
  1128. }
  1129. return NULL;
  1130. }
  1131. #define NAME_HASH_SHIFT 5
  1132. #define VALUE_HASH_SHIFT 16
  1133. /*
  1134. * ext3_xattr_hash_entry()
  1135. *
  1136. * Compute the hash of an extended attribute.
  1137. */
  1138. static inline void ext3_xattr_hash_entry(struct ext3_xattr_header *header,
  1139. struct ext3_xattr_entry *entry)
  1140. {
  1141. __u32 hash = 0;
  1142. char *name = entry->e_name;
  1143. int n;
  1144. for (n=0; n < entry->e_name_len; n++) {
  1145. hash = (hash << NAME_HASH_SHIFT) ^
  1146. (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^
  1147. *name++;
  1148. }
  1149. if (entry->e_value_block == 0 && entry->e_value_size != 0) {
  1150. __le32 *value = (__le32 *)((char *)header +
  1151. le16_to_cpu(entry->e_value_offs));
  1152. for (n = (le32_to_cpu(entry->e_value_size) +
  1153. EXT3_XATTR_ROUND) >> EXT3_XATTR_PAD_BITS; n; n--) {
  1154. hash = (hash << VALUE_HASH_SHIFT) ^
  1155. (hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^
  1156. le32_to_cpu(*value++);
  1157. }
  1158. }
  1159. entry->e_hash = cpu_to_le32(hash);
  1160. }
  1161. #undef NAME_HASH_SHIFT
  1162. #undef VALUE_HASH_SHIFT
  1163. #define BLOCK_HASH_SHIFT 16
  1164. /*
  1165. * ext3_xattr_rehash()
  1166. *
  1167. * Re-compute the extended attribute hash value after an entry has changed.
  1168. */
  1169. static void ext3_xattr_rehash(struct ext3_xattr_header *header,
  1170. struct ext3_xattr_entry *entry)
  1171. {
  1172. struct ext3_xattr_entry *here;
  1173. __u32 hash = 0;
  1174. ext3_xattr_hash_entry(header, entry);
  1175. here = ENTRY(header+1);
  1176. while (!IS_LAST_ENTRY(here)) {
  1177. if (!here->e_hash) {
  1178. /* Block is not shared if an entry's hash value == 0 */
  1179. hash = 0;
  1180. break;
  1181. }
  1182. hash = (hash << BLOCK_HASH_SHIFT) ^
  1183. (hash >> (8*sizeof(hash) - BLOCK_HASH_SHIFT)) ^
  1184. le32_to_cpu(here->e_hash);
  1185. here = EXT3_XATTR_NEXT(here);
  1186. }
  1187. header->h_hash = cpu_to_le32(hash);
  1188. }
  1189. #undef BLOCK_HASH_SHIFT
  1190. int __init
  1191. init_ext3_xattr(void)
  1192. {
  1193. ext3_xattr_cache = mb_cache_create("ext3_xattr", NULL,
  1194. sizeof(struct mb_cache_entry) +
  1195. sizeof(((struct mb_cache_entry *) 0)->e_indexes[0]), 1, 6);
  1196. if (!ext3_xattr_cache)
  1197. return -ENOMEM;
  1198. return 0;
  1199. }
  1200. void
  1201. exit_ext3_xattr(void)
  1202. {
  1203. if (ext3_xattr_cache)
  1204. mb_cache_destroy(ext3_xattr_cache);
  1205. ext3_xattr_cache = NULL;
  1206. }