xattr.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314
  1. /*
  2. * linux/fs/ext4/xattr.c
  3. *
  4. * Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de>
  5. *
  6. * Fix by Harrison Xing <harrison@mountainviewdata.com>.
  7. * Ext4 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. * EXT4_I(inode)->i_file_acl is protected by EXT4_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/ext4_jbd2.h>
  55. #include <linux/ext4_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 ext4_xattr_header *)((bh)->b_data))
  62. #define ENTRY(ptr) ((struct ext4_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 ext4_xattr_ibody_header *) \
  67. ((void *)raw_inode + \
  68. EXT4_GOOD_OLD_INODE_SIZE + \
  69. EXT4_I(inode)->i_extra_isize))
  70. #define IFIRST(hdr) ((struct ext4_xattr_entry *)((hdr)+1))
  71. #ifdef EXT4_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 ext4_xattr_cache_insert(struct buffer_head *);
  91. static struct buffer_head *ext4_xattr_cache_find(struct inode *,
  92. struct ext4_xattr_header *,
  93. struct mb_cache_entry **);
  94. static void ext4_xattr_rehash(struct ext4_xattr_header *,
  95. struct ext4_xattr_entry *);
  96. static struct mb_cache *ext4_xattr_cache;
  97. static struct xattr_handler *ext4_xattr_handler_map[] = {
  98. [EXT4_XATTR_INDEX_USER] = &ext4_xattr_user_handler,
  99. #ifdef CONFIG_EXT4DEV_FS_POSIX_ACL
  100. [EXT4_XATTR_INDEX_POSIX_ACL_ACCESS] = &ext4_xattr_acl_access_handler,
  101. [EXT4_XATTR_INDEX_POSIX_ACL_DEFAULT] = &ext4_xattr_acl_default_handler,
  102. #endif
  103. [EXT4_XATTR_INDEX_TRUSTED] = &ext4_xattr_trusted_handler,
  104. #ifdef CONFIG_EXT4DEV_FS_SECURITY
  105. [EXT4_XATTR_INDEX_SECURITY] = &ext4_xattr_security_handler,
  106. #endif
  107. };
  108. struct xattr_handler *ext4_xattr_handlers[] = {
  109. &ext4_xattr_user_handler,
  110. &ext4_xattr_trusted_handler,
  111. #ifdef CONFIG_EXT4DEV_FS_POSIX_ACL
  112. &ext4_xattr_acl_access_handler,
  113. &ext4_xattr_acl_default_handler,
  114. #endif
  115. #ifdef CONFIG_EXT4DEV_FS_SECURITY
  116. &ext4_xattr_security_handler,
  117. #endif
  118. NULL
  119. };
  120. static inline struct xattr_handler *
  121. ext4_xattr_handler(int name_index)
  122. {
  123. struct xattr_handler *handler = NULL;
  124. if (name_index > 0 && name_index < ARRAY_SIZE(ext4_xattr_handler_map))
  125. handler = ext4_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. ext4_listxattr(struct dentry *dentry, char *buffer, size_t size)
  135. {
  136. return ext4_xattr_list(dentry->d_inode, buffer, size);
  137. }
  138. static int
  139. ext4_xattr_check_names(struct ext4_xattr_entry *entry, void *end)
  140. {
  141. while (!IS_LAST_ENTRY(entry)) {
  142. struct ext4_xattr_entry *next = EXT4_XATTR_NEXT(entry);
  143. if ((void *)next >= end)
  144. return -EIO;
  145. entry = next;
  146. }
  147. return 0;
  148. }
  149. static inline int
  150. ext4_xattr_check_block(struct buffer_head *bh)
  151. {
  152. int error;
  153. if (BHDR(bh)->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC) ||
  154. BHDR(bh)->h_blocks != cpu_to_le32(1))
  155. return -EIO;
  156. error = ext4_xattr_check_names(BFIRST(bh), bh->b_data + bh->b_size);
  157. return error;
  158. }
  159. static inline int
  160. ext4_xattr_check_entry(struct ext4_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. ext4_xattr_find_entry(struct ext4_xattr_entry **pentry, int name_index,
  170. const char *name, size_t size, int sorted)
  171. {
  172. struct ext4_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 = EXT4_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 && ext4_xattr_check_entry(entry, size))
  190. return -EIO;
  191. return cmp ? -ENODATA : 0;
  192. }
  193. static int
  194. ext4_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 ext4_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 (!EXT4_I(inode)->i_file_acl)
  205. goto cleanup;
  206. ea_idebug(inode, "reading block %u", EXT4_I(inode)->i_file_acl);
  207. bh = sb_bread(inode->i_sb, EXT4_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 (ext4_xattr_check_block(bh)) {
  213. bad_block: ext4_error(inode->i_sb, __FUNCTION__,
  214. "inode %lu: bad block %llu", inode->i_ino,
  215. EXT4_I(inode)->i_file_acl);
  216. error = -EIO;
  217. goto cleanup;
  218. }
  219. ext4_xattr_cache_insert(bh);
  220. entry = BFIRST(bh);
  221. error = ext4_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. ext4_xattr_ibody_get(struct inode *inode, int name_index, const char *name,
  241. void *buffer, size_t buffer_size)
  242. {
  243. struct ext4_xattr_ibody_header *header;
  244. struct ext4_xattr_entry *entry;
  245. struct ext4_inode *raw_inode;
  246. struct ext4_iloc iloc;
  247. size_t size;
  248. void *end;
  249. int error;
  250. if (!(EXT4_I(inode)->i_state & EXT4_STATE_XATTR))
  251. return -ENODATA;
  252. error = ext4_get_inode_loc(inode, &iloc);
  253. if (error)
  254. return error;
  255. raw_inode = ext4_raw_inode(&iloc);
  256. header = IHDR(inode, raw_inode);
  257. entry = IFIRST(header);
  258. end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
  259. error = ext4_xattr_check_names(entry, end);
  260. if (error)
  261. goto cleanup;
  262. error = ext4_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. * ext4_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. ext4_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(&EXT4_I(inode)->xattr_sem);
  295. error = ext4_xattr_ibody_get(inode, name_index, name, buffer,
  296. buffer_size);
  297. if (error == -ENODATA)
  298. error = ext4_xattr_block_get(inode, name_index, name, buffer,
  299. buffer_size);
  300. up_read(&EXT4_I(inode)->xattr_sem);
  301. return error;
  302. }
  303. static int
  304. ext4_xattr_list_entries(struct inode *inode, struct ext4_xattr_entry *entry,
  305. char *buffer, size_t buffer_size)
  306. {
  307. size_t rest = buffer_size;
  308. for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
  309. struct xattr_handler *handler =
  310. ext4_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. ext4_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 (!EXT4_I(inode)->i_file_acl)
  334. goto cleanup;
  335. ea_idebug(inode, "reading block %u", EXT4_I(inode)->i_file_acl);
  336. bh = sb_bread(inode->i_sb, EXT4_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 (ext4_xattr_check_block(bh)) {
  343. ext4_error(inode->i_sb, __FUNCTION__,
  344. "inode %lu: bad block %llu", inode->i_ino,
  345. EXT4_I(inode)->i_file_acl);
  346. error = -EIO;
  347. goto cleanup;
  348. }
  349. ext4_xattr_cache_insert(bh);
  350. error = ext4_xattr_list_entries(inode, BFIRST(bh), buffer, buffer_size);
  351. cleanup:
  352. brelse(bh);
  353. return error;
  354. }
  355. static int
  356. ext4_xattr_ibody_list(struct inode *inode, char *buffer, size_t buffer_size)
  357. {
  358. struct ext4_xattr_ibody_header *header;
  359. struct ext4_inode *raw_inode;
  360. struct ext4_iloc iloc;
  361. void *end;
  362. int error;
  363. if (!(EXT4_I(inode)->i_state & EXT4_STATE_XATTR))
  364. return 0;
  365. error = ext4_get_inode_loc(inode, &iloc);
  366. if (error)
  367. return error;
  368. raw_inode = ext4_raw_inode(&iloc);
  369. header = IHDR(inode, raw_inode);
  370. end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
  371. error = ext4_xattr_check_names(IFIRST(header), end);
  372. if (error)
  373. goto cleanup;
  374. error = ext4_xattr_list_entries(inode, IFIRST(header),
  375. buffer, buffer_size);
  376. cleanup:
  377. brelse(iloc.bh);
  378. return error;
  379. }
  380. /*
  381. * ext4_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. ext4_xattr_list(struct inode *inode, char *buffer, size_t buffer_size)
  392. {
  393. int i_error, b_error;
  394. down_read(&EXT4_I(inode)->xattr_sem);
  395. i_error = ext4_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 = ext4_xattr_block_list(inode, buffer, buffer_size);
  404. if (b_error < 0)
  405. i_error = 0;
  406. }
  407. up_read(&EXT4_I(inode)->xattr_sem);
  408. return i_error + b_error;
  409. }
  410. /*
  411. * If the EXT4_FEATURE_COMPAT_EXT_ATTR feature of this file system is
  412. * not set, set it.
  413. */
  414. static void ext4_xattr_update_super_block(handle_t *handle,
  415. struct super_block *sb)
  416. {
  417. if (EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_EXT_ATTR))
  418. return;
  419. if (ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh) == 0) {
  420. EXT4_SET_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_EXT_ATTR);
  421. sb->s_dirt = 1;
  422. ext4_journal_dirty_metadata(handle, EXT4_SB(sb)->s_sbh);
  423. }
  424. }
  425. /*
  426. * Release the xattr block BH: If the reference count is > 1, decrement
  427. * it; otherwise free the block.
  428. */
  429. static void
  430. ext4_xattr_release_block(handle_t *handle, struct inode *inode,
  431. struct buffer_head *bh)
  432. {
  433. struct mb_cache_entry *ce = NULL;
  434. ce = mb_cache_entry_get(ext4_xattr_cache, bh->b_bdev, bh->b_blocknr);
  435. if (BHDR(bh)->h_refcount == cpu_to_le32(1)) {
  436. ea_bdebug(bh, "refcount now=0; freeing");
  437. if (ce)
  438. mb_cache_entry_free(ce);
  439. ext4_free_blocks(handle, inode, bh->b_blocknr, 1);
  440. get_bh(bh);
  441. ext4_forget(handle, 1, inode, bh, bh->b_blocknr);
  442. } else {
  443. if (ext4_journal_get_write_access(handle, bh) == 0) {
  444. lock_buffer(bh);
  445. BHDR(bh)->h_refcount = cpu_to_le32(
  446. le32_to_cpu(BHDR(bh)->h_refcount) - 1);
  447. ext4_journal_dirty_metadata(handle, bh);
  448. if (IS_SYNC(inode))
  449. handle->h_sync = 1;
  450. DQUOT_FREE_BLOCK(inode, 1);
  451. unlock_buffer(bh);
  452. ea_bdebug(bh, "refcount now=%d; releasing",
  453. le32_to_cpu(BHDR(bh)->h_refcount));
  454. }
  455. if (ce)
  456. mb_cache_entry_release(ce);
  457. }
  458. }
  459. struct ext4_xattr_info {
  460. int name_index;
  461. const char *name;
  462. const void *value;
  463. size_t value_len;
  464. };
  465. struct ext4_xattr_search {
  466. struct ext4_xattr_entry *first;
  467. void *base;
  468. void *end;
  469. struct ext4_xattr_entry *here;
  470. int not_found;
  471. };
  472. static int
  473. ext4_xattr_set_entry(struct ext4_xattr_info *i, struct ext4_xattr_search *s)
  474. {
  475. struct ext4_xattr_entry *last;
  476. size_t free, min_offs = s->end - s->base, name_len = strlen(i->name);
  477. /* Compute min_offs and last. */
  478. last = s->first;
  479. for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
  480. if (!last->e_value_block && last->e_value_size) {
  481. size_t offs = le16_to_cpu(last->e_value_offs);
  482. if (offs < min_offs)
  483. min_offs = offs;
  484. }
  485. }
  486. free = min_offs - ((void *)last - s->base) - sizeof(__u32);
  487. if (!s->not_found) {
  488. if (!s->here->e_value_block && s->here->e_value_size) {
  489. size_t size = le32_to_cpu(s->here->e_value_size);
  490. free += EXT4_XATTR_SIZE(size);
  491. }
  492. free += EXT4_XATTR_LEN(name_len);
  493. }
  494. if (i->value) {
  495. if (free < EXT4_XATTR_SIZE(i->value_len) ||
  496. free < EXT4_XATTR_LEN(name_len) +
  497. EXT4_XATTR_SIZE(i->value_len))
  498. return -ENOSPC;
  499. }
  500. if (i->value && s->not_found) {
  501. /* Insert the new name. */
  502. size_t size = EXT4_XATTR_LEN(name_len);
  503. size_t rest = (void *)last - (void *)s->here + sizeof(__u32);
  504. memmove((void *)s->here + size, s->here, rest);
  505. memset(s->here, 0, size);
  506. s->here->e_name_index = i->name_index;
  507. s->here->e_name_len = name_len;
  508. memcpy(s->here->e_name, i->name, name_len);
  509. } else {
  510. if (!s->here->e_value_block && s->here->e_value_size) {
  511. void *first_val = s->base + min_offs;
  512. size_t offs = le16_to_cpu(s->here->e_value_offs);
  513. void *val = s->base + offs;
  514. size_t size = EXT4_XATTR_SIZE(
  515. le32_to_cpu(s->here->e_value_size));
  516. if (i->value && size == EXT4_XATTR_SIZE(i->value_len)) {
  517. /* The old and the new value have the same
  518. size. Just replace. */
  519. s->here->e_value_size =
  520. cpu_to_le32(i->value_len);
  521. memset(val + size - EXT4_XATTR_PAD, 0,
  522. EXT4_XATTR_PAD); /* Clear pad bytes. */
  523. memcpy(val, i->value, i->value_len);
  524. return 0;
  525. }
  526. /* Remove the old value. */
  527. memmove(first_val + size, first_val, val - first_val);
  528. memset(first_val, 0, size);
  529. s->here->e_value_size = 0;
  530. s->here->e_value_offs = 0;
  531. min_offs += size;
  532. /* Adjust all value offsets. */
  533. last = s->first;
  534. while (!IS_LAST_ENTRY(last)) {
  535. size_t o = le16_to_cpu(last->e_value_offs);
  536. if (!last->e_value_block &&
  537. last->e_value_size && o < offs)
  538. last->e_value_offs =
  539. cpu_to_le16(o + size);
  540. last = EXT4_XATTR_NEXT(last);
  541. }
  542. }
  543. if (!i->value) {
  544. /* Remove the old name. */
  545. size_t size = EXT4_XATTR_LEN(name_len);
  546. last = ENTRY((void *)last - size);
  547. memmove(s->here, (void *)s->here + size,
  548. (void *)last - (void *)s->here + sizeof(__u32));
  549. memset(last, 0, size);
  550. }
  551. }
  552. if (i->value) {
  553. /* Insert the new value. */
  554. s->here->e_value_size = cpu_to_le32(i->value_len);
  555. if (i->value_len) {
  556. size_t size = EXT4_XATTR_SIZE(i->value_len);
  557. void *val = s->base + min_offs - size;
  558. s->here->e_value_offs = cpu_to_le16(min_offs - size);
  559. memset(val + size - EXT4_XATTR_PAD, 0,
  560. EXT4_XATTR_PAD); /* Clear the pad bytes. */
  561. memcpy(val, i->value, i->value_len);
  562. }
  563. }
  564. return 0;
  565. }
  566. struct ext4_xattr_block_find {
  567. struct ext4_xattr_search s;
  568. struct buffer_head *bh;
  569. };
  570. static int
  571. ext4_xattr_block_find(struct inode *inode, struct ext4_xattr_info *i,
  572. struct ext4_xattr_block_find *bs)
  573. {
  574. struct super_block *sb = inode->i_sb;
  575. int error;
  576. ea_idebug(inode, "name=%d.%s, value=%p, value_len=%ld",
  577. i->name_index, i->name, i->value, (long)i->value_len);
  578. if (EXT4_I(inode)->i_file_acl) {
  579. /* The inode already has an extended attribute block. */
  580. bs->bh = sb_bread(sb, EXT4_I(inode)->i_file_acl);
  581. error = -EIO;
  582. if (!bs->bh)
  583. goto cleanup;
  584. ea_bdebug(bs->bh, "b_count=%d, refcount=%d",
  585. atomic_read(&(bs->bh->b_count)),
  586. le32_to_cpu(BHDR(bs->bh)->h_refcount));
  587. if (ext4_xattr_check_block(bs->bh)) {
  588. ext4_error(sb, __FUNCTION__,
  589. "inode %lu: bad block %llu", inode->i_ino,
  590. EXT4_I(inode)->i_file_acl);
  591. error = -EIO;
  592. goto cleanup;
  593. }
  594. /* Find the named attribute. */
  595. bs->s.base = BHDR(bs->bh);
  596. bs->s.first = BFIRST(bs->bh);
  597. bs->s.end = bs->bh->b_data + bs->bh->b_size;
  598. bs->s.here = bs->s.first;
  599. error = ext4_xattr_find_entry(&bs->s.here, i->name_index,
  600. i->name, bs->bh->b_size, 1);
  601. if (error && error != -ENODATA)
  602. goto cleanup;
  603. bs->s.not_found = error;
  604. }
  605. error = 0;
  606. cleanup:
  607. return error;
  608. }
  609. static int
  610. ext4_xattr_block_set(handle_t *handle, struct inode *inode,
  611. struct ext4_xattr_info *i,
  612. struct ext4_xattr_block_find *bs)
  613. {
  614. struct super_block *sb = inode->i_sb;
  615. struct buffer_head *new_bh = NULL;
  616. struct ext4_xattr_search *s = &bs->s;
  617. struct mb_cache_entry *ce = NULL;
  618. int error;
  619. #define header(x) ((struct ext4_xattr_header *)(x))
  620. if (i->value && i->value_len > sb->s_blocksize)
  621. return -ENOSPC;
  622. if (s->base) {
  623. ce = mb_cache_entry_get(ext4_xattr_cache, bs->bh->b_bdev,
  624. bs->bh->b_blocknr);
  625. if (header(s->base)->h_refcount == cpu_to_le32(1)) {
  626. if (ce) {
  627. mb_cache_entry_free(ce);
  628. ce = NULL;
  629. }
  630. ea_bdebug(bs->bh, "modifying in-place");
  631. error = ext4_journal_get_write_access(handle, bs->bh);
  632. if (error)
  633. goto cleanup;
  634. lock_buffer(bs->bh);
  635. error = ext4_xattr_set_entry(i, s);
  636. if (!error) {
  637. if (!IS_LAST_ENTRY(s->first))
  638. ext4_xattr_rehash(header(s->base),
  639. s->here);
  640. ext4_xattr_cache_insert(bs->bh);
  641. }
  642. unlock_buffer(bs->bh);
  643. if (error == -EIO)
  644. goto bad_block;
  645. if (!error)
  646. error = ext4_journal_dirty_metadata(handle,
  647. bs->bh);
  648. if (error)
  649. goto cleanup;
  650. goto inserted;
  651. } else {
  652. int offset = (char *)s->here - bs->bh->b_data;
  653. if (ce) {
  654. mb_cache_entry_release(ce);
  655. ce = NULL;
  656. }
  657. ea_bdebug(bs->bh, "cloning");
  658. s->base = kmalloc(bs->bh->b_size, GFP_KERNEL);
  659. error = -ENOMEM;
  660. if (s->base == NULL)
  661. goto cleanup;
  662. memcpy(s->base, BHDR(bs->bh), bs->bh->b_size);
  663. s->first = ENTRY(header(s->base)+1);
  664. header(s->base)->h_refcount = cpu_to_le32(1);
  665. s->here = ENTRY(s->base + offset);
  666. s->end = s->base + bs->bh->b_size;
  667. }
  668. } else {
  669. /* Allocate a buffer where we construct the new block. */
  670. s->base = kmalloc(sb->s_blocksize, GFP_KERNEL);
  671. /* assert(header == s->base) */
  672. error = -ENOMEM;
  673. if (s->base == NULL)
  674. goto cleanup;
  675. memset(s->base, 0, sb->s_blocksize);
  676. header(s->base)->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
  677. header(s->base)->h_blocks = cpu_to_le32(1);
  678. header(s->base)->h_refcount = cpu_to_le32(1);
  679. s->first = ENTRY(header(s->base)+1);
  680. s->here = ENTRY(header(s->base)+1);
  681. s->end = s->base + sb->s_blocksize;
  682. }
  683. error = ext4_xattr_set_entry(i, s);
  684. if (error == -EIO)
  685. goto bad_block;
  686. if (error)
  687. goto cleanup;
  688. if (!IS_LAST_ENTRY(s->first))
  689. ext4_xattr_rehash(header(s->base), s->here);
  690. inserted:
  691. if (!IS_LAST_ENTRY(s->first)) {
  692. new_bh = ext4_xattr_cache_find(inode, header(s->base), &ce);
  693. if (new_bh) {
  694. /* We found an identical block in the cache. */
  695. if (new_bh == bs->bh)
  696. ea_bdebug(new_bh, "keeping");
  697. else {
  698. /* The old block is released after updating
  699. the inode. */
  700. error = -EDQUOT;
  701. if (DQUOT_ALLOC_BLOCK(inode, 1))
  702. goto cleanup;
  703. error = ext4_journal_get_write_access(handle,
  704. new_bh);
  705. if (error)
  706. goto cleanup_dquot;
  707. lock_buffer(new_bh);
  708. BHDR(new_bh)->h_refcount = cpu_to_le32(1 +
  709. le32_to_cpu(BHDR(new_bh)->h_refcount));
  710. ea_bdebug(new_bh, "reusing; refcount now=%d",
  711. le32_to_cpu(BHDR(new_bh)->h_refcount));
  712. unlock_buffer(new_bh);
  713. error = ext4_journal_dirty_metadata(handle,
  714. new_bh);
  715. if (error)
  716. goto cleanup_dquot;
  717. }
  718. mb_cache_entry_release(ce);
  719. ce = NULL;
  720. } else if (bs->bh && s->base == bs->bh->b_data) {
  721. /* We were modifying this block in-place. */
  722. ea_bdebug(bs->bh, "keeping this block");
  723. new_bh = bs->bh;
  724. get_bh(new_bh);
  725. } else {
  726. /* We need to allocate a new block */
  727. ext4_fsblk_t goal = le32_to_cpu(
  728. EXT4_SB(sb)->s_es->s_first_data_block) +
  729. (ext4_fsblk_t)EXT4_I(inode)->i_block_group *
  730. EXT4_BLOCKS_PER_GROUP(sb);
  731. ext4_fsblk_t block = ext4_new_block(handle, inode,
  732. goal, &error);
  733. if (error)
  734. goto cleanup;
  735. ea_idebug(inode, "creating block %d", block);
  736. new_bh = sb_getblk(sb, block);
  737. if (!new_bh) {
  738. getblk_failed:
  739. ext4_free_blocks(handle, inode, block, 1);
  740. error = -EIO;
  741. goto cleanup;
  742. }
  743. lock_buffer(new_bh);
  744. error = ext4_journal_get_create_access(handle, new_bh);
  745. if (error) {
  746. unlock_buffer(new_bh);
  747. goto getblk_failed;
  748. }
  749. memcpy(new_bh->b_data, s->base, new_bh->b_size);
  750. set_buffer_uptodate(new_bh);
  751. unlock_buffer(new_bh);
  752. ext4_xattr_cache_insert(new_bh);
  753. error = ext4_journal_dirty_metadata(handle, new_bh);
  754. if (error)
  755. goto cleanup;
  756. }
  757. }
  758. /* Update the inode. */
  759. EXT4_I(inode)->i_file_acl = new_bh ? new_bh->b_blocknr : 0;
  760. /* Drop the previous xattr block. */
  761. if (bs->bh && bs->bh != new_bh)
  762. ext4_xattr_release_block(handle, inode, bs->bh);
  763. error = 0;
  764. cleanup:
  765. if (ce)
  766. mb_cache_entry_release(ce);
  767. brelse(new_bh);
  768. if (!(bs->bh && s->base == bs->bh->b_data))
  769. kfree(s->base);
  770. return error;
  771. cleanup_dquot:
  772. DQUOT_FREE_BLOCK(inode, 1);
  773. goto cleanup;
  774. bad_block:
  775. ext4_error(inode->i_sb, __FUNCTION__,
  776. "inode %lu: bad block %llu", inode->i_ino,
  777. EXT4_I(inode)->i_file_acl);
  778. goto cleanup;
  779. #undef header
  780. }
  781. struct ext4_xattr_ibody_find {
  782. struct ext4_xattr_search s;
  783. struct ext4_iloc iloc;
  784. };
  785. static int
  786. ext4_xattr_ibody_find(struct inode *inode, struct ext4_xattr_info *i,
  787. struct ext4_xattr_ibody_find *is)
  788. {
  789. struct ext4_xattr_ibody_header *header;
  790. struct ext4_inode *raw_inode;
  791. int error;
  792. if (EXT4_I(inode)->i_extra_isize == 0)
  793. return 0;
  794. raw_inode = ext4_raw_inode(&is->iloc);
  795. header = IHDR(inode, raw_inode);
  796. is->s.base = is->s.first = IFIRST(header);
  797. is->s.here = is->s.first;
  798. is->s.end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
  799. if (EXT4_I(inode)->i_state & EXT4_STATE_XATTR) {
  800. error = ext4_xattr_check_names(IFIRST(header), is->s.end);
  801. if (error)
  802. return error;
  803. /* Find the named attribute. */
  804. error = ext4_xattr_find_entry(&is->s.here, i->name_index,
  805. i->name, is->s.end -
  806. (void *)is->s.base, 0);
  807. if (error && error != -ENODATA)
  808. return error;
  809. is->s.not_found = error;
  810. }
  811. return 0;
  812. }
  813. static int
  814. ext4_xattr_ibody_set(handle_t *handle, struct inode *inode,
  815. struct ext4_xattr_info *i,
  816. struct ext4_xattr_ibody_find *is)
  817. {
  818. struct ext4_xattr_ibody_header *header;
  819. struct ext4_xattr_search *s = &is->s;
  820. int error;
  821. if (EXT4_I(inode)->i_extra_isize == 0)
  822. return -ENOSPC;
  823. error = ext4_xattr_set_entry(i, s);
  824. if (error)
  825. return error;
  826. header = IHDR(inode, ext4_raw_inode(&is->iloc));
  827. if (!IS_LAST_ENTRY(s->first)) {
  828. header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
  829. EXT4_I(inode)->i_state |= EXT4_STATE_XATTR;
  830. } else {
  831. header->h_magic = cpu_to_le32(0);
  832. EXT4_I(inode)->i_state &= ~EXT4_STATE_XATTR;
  833. }
  834. return 0;
  835. }
  836. /*
  837. * ext4_xattr_set_handle()
  838. *
  839. * Create, replace or remove an extended attribute for this inode. Buffer
  840. * is NULL to remove an existing extended attribute, and non-NULL to
  841. * either replace an existing extended attribute, or create a new extended
  842. * attribute. The flags XATTR_REPLACE and XATTR_CREATE
  843. * specify that an extended attribute must exist and must not exist
  844. * previous to the call, respectively.
  845. *
  846. * Returns 0, or a negative error number on failure.
  847. */
  848. int
  849. ext4_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index,
  850. const char *name, const void *value, size_t value_len,
  851. int flags)
  852. {
  853. struct ext4_xattr_info i = {
  854. .name_index = name_index,
  855. .name = name,
  856. .value = value,
  857. .value_len = value_len,
  858. };
  859. struct ext4_xattr_ibody_find is = {
  860. .s = { .not_found = -ENODATA, },
  861. };
  862. struct ext4_xattr_block_find bs = {
  863. .s = { .not_found = -ENODATA, },
  864. };
  865. int error;
  866. if (!name)
  867. return -EINVAL;
  868. if (strlen(name) > 255)
  869. return -ERANGE;
  870. down_write(&EXT4_I(inode)->xattr_sem);
  871. error = ext4_get_inode_loc(inode, &is.iloc);
  872. if (error)
  873. goto cleanup;
  874. if (EXT4_I(inode)->i_state & EXT4_STATE_NEW) {
  875. struct ext4_inode *raw_inode = ext4_raw_inode(&is.iloc);
  876. memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size);
  877. EXT4_I(inode)->i_state &= ~EXT4_STATE_NEW;
  878. }
  879. error = ext4_xattr_ibody_find(inode, &i, &is);
  880. if (error)
  881. goto cleanup;
  882. if (is.s.not_found)
  883. error = ext4_xattr_block_find(inode, &i, &bs);
  884. if (error)
  885. goto cleanup;
  886. if (is.s.not_found && bs.s.not_found) {
  887. error = -ENODATA;
  888. if (flags & XATTR_REPLACE)
  889. goto cleanup;
  890. error = 0;
  891. if (!value)
  892. goto cleanup;
  893. } else {
  894. error = -EEXIST;
  895. if (flags & XATTR_CREATE)
  896. goto cleanup;
  897. }
  898. error = ext4_journal_get_write_access(handle, is.iloc.bh);
  899. if (error)
  900. goto cleanup;
  901. if (!value) {
  902. if (!is.s.not_found)
  903. error = ext4_xattr_ibody_set(handle, inode, &i, &is);
  904. else if (!bs.s.not_found)
  905. error = ext4_xattr_block_set(handle, inode, &i, &bs);
  906. } else {
  907. error = ext4_xattr_ibody_set(handle, inode, &i, &is);
  908. if (!error && !bs.s.not_found) {
  909. i.value = NULL;
  910. error = ext4_xattr_block_set(handle, inode, &i, &bs);
  911. } else if (error == -ENOSPC) {
  912. error = ext4_xattr_block_set(handle, inode, &i, &bs);
  913. if (error)
  914. goto cleanup;
  915. if (!is.s.not_found) {
  916. i.value = NULL;
  917. error = ext4_xattr_ibody_set(handle, inode, &i,
  918. &is);
  919. }
  920. }
  921. }
  922. if (!error) {
  923. ext4_xattr_update_super_block(handle, inode->i_sb);
  924. inode->i_ctime = CURRENT_TIME_SEC;
  925. error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
  926. /*
  927. * The bh is consumed by ext4_mark_iloc_dirty, even with
  928. * error != 0.
  929. */
  930. is.iloc.bh = NULL;
  931. if (IS_SYNC(inode))
  932. handle->h_sync = 1;
  933. }
  934. cleanup:
  935. brelse(is.iloc.bh);
  936. brelse(bs.bh);
  937. up_write(&EXT4_I(inode)->xattr_sem);
  938. return error;
  939. }
  940. /*
  941. * ext4_xattr_set()
  942. *
  943. * Like ext4_xattr_set_handle, but start from an inode. This extended
  944. * attribute modification is a filesystem transaction by itself.
  945. *
  946. * Returns 0, or a negative error number on failure.
  947. */
  948. int
  949. ext4_xattr_set(struct inode *inode, int name_index, const char *name,
  950. const void *value, size_t value_len, int flags)
  951. {
  952. handle_t *handle;
  953. int error, retries = 0;
  954. retry:
  955. handle = ext4_journal_start(inode, EXT4_DATA_TRANS_BLOCKS(inode->i_sb));
  956. if (IS_ERR(handle)) {
  957. error = PTR_ERR(handle);
  958. } else {
  959. int error2;
  960. error = ext4_xattr_set_handle(handle, inode, name_index, name,
  961. value, value_len, flags);
  962. error2 = ext4_journal_stop(handle);
  963. if (error == -ENOSPC &&
  964. ext4_should_retry_alloc(inode->i_sb, &retries))
  965. goto retry;
  966. if (error == 0)
  967. error = error2;
  968. }
  969. return error;
  970. }
  971. /*
  972. * ext4_xattr_delete_inode()
  973. *
  974. * Free extended attribute resources associated with this inode. This
  975. * is called immediately before an inode is freed. We have exclusive
  976. * access to the inode.
  977. */
  978. void
  979. ext4_xattr_delete_inode(handle_t *handle, struct inode *inode)
  980. {
  981. struct buffer_head *bh = NULL;
  982. if (!EXT4_I(inode)->i_file_acl)
  983. goto cleanup;
  984. bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
  985. if (!bh) {
  986. ext4_error(inode->i_sb, __FUNCTION__,
  987. "inode %lu: block %llu read error", inode->i_ino,
  988. EXT4_I(inode)->i_file_acl);
  989. goto cleanup;
  990. }
  991. if (BHDR(bh)->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC) ||
  992. BHDR(bh)->h_blocks != cpu_to_le32(1)) {
  993. ext4_error(inode->i_sb, __FUNCTION__,
  994. "inode %lu: bad block %llu", inode->i_ino,
  995. EXT4_I(inode)->i_file_acl);
  996. goto cleanup;
  997. }
  998. ext4_xattr_release_block(handle, inode, bh);
  999. EXT4_I(inode)->i_file_acl = 0;
  1000. cleanup:
  1001. brelse(bh);
  1002. }
  1003. /*
  1004. * ext4_xattr_put_super()
  1005. *
  1006. * This is called when a file system is unmounted.
  1007. */
  1008. void
  1009. ext4_xattr_put_super(struct super_block *sb)
  1010. {
  1011. mb_cache_shrink(sb->s_bdev);
  1012. }
  1013. /*
  1014. * ext4_xattr_cache_insert()
  1015. *
  1016. * Create a new entry in the extended attribute cache, and insert
  1017. * it unless such an entry is already in the cache.
  1018. *
  1019. * Returns 0, or a negative error number on failure.
  1020. */
  1021. static void
  1022. ext4_xattr_cache_insert(struct buffer_head *bh)
  1023. {
  1024. __u32 hash = le32_to_cpu(BHDR(bh)->h_hash);
  1025. struct mb_cache_entry *ce;
  1026. int error;
  1027. ce = mb_cache_entry_alloc(ext4_xattr_cache);
  1028. if (!ce) {
  1029. ea_bdebug(bh, "out of memory");
  1030. return;
  1031. }
  1032. error = mb_cache_entry_insert(ce, bh->b_bdev, bh->b_blocknr, &hash);
  1033. if (error) {
  1034. mb_cache_entry_free(ce);
  1035. if (error == -EBUSY) {
  1036. ea_bdebug(bh, "already in cache");
  1037. error = 0;
  1038. }
  1039. } else {
  1040. ea_bdebug(bh, "inserting [%x]", (int)hash);
  1041. mb_cache_entry_release(ce);
  1042. }
  1043. }
  1044. /*
  1045. * ext4_xattr_cmp()
  1046. *
  1047. * Compare two extended attribute blocks for equality.
  1048. *
  1049. * Returns 0 if the blocks are equal, 1 if they differ, and
  1050. * a negative error number on errors.
  1051. */
  1052. static int
  1053. ext4_xattr_cmp(struct ext4_xattr_header *header1,
  1054. struct ext4_xattr_header *header2)
  1055. {
  1056. struct ext4_xattr_entry *entry1, *entry2;
  1057. entry1 = ENTRY(header1+1);
  1058. entry2 = ENTRY(header2+1);
  1059. while (!IS_LAST_ENTRY(entry1)) {
  1060. if (IS_LAST_ENTRY(entry2))
  1061. return 1;
  1062. if (entry1->e_hash != entry2->e_hash ||
  1063. entry1->e_name_index != entry2->e_name_index ||
  1064. entry1->e_name_len != entry2->e_name_len ||
  1065. entry1->e_value_size != entry2->e_value_size ||
  1066. memcmp(entry1->e_name, entry2->e_name, entry1->e_name_len))
  1067. return 1;
  1068. if (entry1->e_value_block != 0 || entry2->e_value_block != 0)
  1069. return -EIO;
  1070. if (memcmp((char *)header1 + le16_to_cpu(entry1->e_value_offs),
  1071. (char *)header2 + le16_to_cpu(entry2->e_value_offs),
  1072. le32_to_cpu(entry1->e_value_size)))
  1073. return 1;
  1074. entry1 = EXT4_XATTR_NEXT(entry1);
  1075. entry2 = EXT4_XATTR_NEXT(entry2);
  1076. }
  1077. if (!IS_LAST_ENTRY(entry2))
  1078. return 1;
  1079. return 0;
  1080. }
  1081. /*
  1082. * ext4_xattr_cache_find()
  1083. *
  1084. * Find an identical extended attribute block.
  1085. *
  1086. * Returns a pointer to the block found, or NULL if such a block was
  1087. * not found or an error occurred.
  1088. */
  1089. static struct buffer_head *
  1090. ext4_xattr_cache_find(struct inode *inode, struct ext4_xattr_header *header,
  1091. struct mb_cache_entry **pce)
  1092. {
  1093. __u32 hash = le32_to_cpu(header->h_hash);
  1094. struct mb_cache_entry *ce;
  1095. if (!header->h_hash)
  1096. return NULL; /* never share */
  1097. ea_idebug(inode, "looking for cached blocks [%x]", (int)hash);
  1098. again:
  1099. ce = mb_cache_entry_find_first(ext4_xattr_cache, 0,
  1100. inode->i_sb->s_bdev, hash);
  1101. while (ce) {
  1102. struct buffer_head *bh;
  1103. if (IS_ERR(ce)) {
  1104. if (PTR_ERR(ce) == -EAGAIN)
  1105. goto again;
  1106. break;
  1107. }
  1108. bh = sb_bread(inode->i_sb, ce->e_block);
  1109. if (!bh) {
  1110. ext4_error(inode->i_sb, __FUNCTION__,
  1111. "inode %lu: block %lu read error",
  1112. inode->i_ino, (unsigned long) ce->e_block);
  1113. } else if (le32_to_cpu(BHDR(bh)->h_refcount) >=
  1114. EXT4_XATTR_REFCOUNT_MAX) {
  1115. ea_idebug(inode, "block %lu refcount %d>=%d",
  1116. (unsigned long) ce->e_block,
  1117. le32_to_cpu(BHDR(bh)->h_refcount),
  1118. EXT4_XATTR_REFCOUNT_MAX);
  1119. } else if (ext4_xattr_cmp(header, BHDR(bh)) == 0) {
  1120. *pce = ce;
  1121. return bh;
  1122. }
  1123. brelse(bh);
  1124. ce = mb_cache_entry_find_next(ce, 0, inode->i_sb->s_bdev, hash);
  1125. }
  1126. return NULL;
  1127. }
  1128. #define NAME_HASH_SHIFT 5
  1129. #define VALUE_HASH_SHIFT 16
  1130. /*
  1131. * ext4_xattr_hash_entry()
  1132. *
  1133. * Compute the hash of an extended attribute.
  1134. */
  1135. static inline void ext4_xattr_hash_entry(struct ext4_xattr_header *header,
  1136. struct ext4_xattr_entry *entry)
  1137. {
  1138. __u32 hash = 0;
  1139. char *name = entry->e_name;
  1140. int n;
  1141. for (n=0; n < entry->e_name_len; n++) {
  1142. hash = (hash << NAME_HASH_SHIFT) ^
  1143. (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^
  1144. *name++;
  1145. }
  1146. if (entry->e_value_block == 0 && entry->e_value_size != 0) {
  1147. __le32 *value = (__le32 *)((char *)header +
  1148. le16_to_cpu(entry->e_value_offs));
  1149. for (n = (le32_to_cpu(entry->e_value_size) +
  1150. EXT4_XATTR_ROUND) >> EXT4_XATTR_PAD_BITS; n; n--) {
  1151. hash = (hash << VALUE_HASH_SHIFT) ^
  1152. (hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^
  1153. le32_to_cpu(*value++);
  1154. }
  1155. }
  1156. entry->e_hash = cpu_to_le32(hash);
  1157. }
  1158. #undef NAME_HASH_SHIFT
  1159. #undef VALUE_HASH_SHIFT
  1160. #define BLOCK_HASH_SHIFT 16
  1161. /*
  1162. * ext4_xattr_rehash()
  1163. *
  1164. * Re-compute the extended attribute hash value after an entry has changed.
  1165. */
  1166. static void ext4_xattr_rehash(struct ext4_xattr_header *header,
  1167. struct ext4_xattr_entry *entry)
  1168. {
  1169. struct ext4_xattr_entry *here;
  1170. __u32 hash = 0;
  1171. ext4_xattr_hash_entry(header, entry);
  1172. here = ENTRY(header+1);
  1173. while (!IS_LAST_ENTRY(here)) {
  1174. if (!here->e_hash) {
  1175. /* Block is not shared if an entry's hash value == 0 */
  1176. hash = 0;
  1177. break;
  1178. }
  1179. hash = (hash << BLOCK_HASH_SHIFT) ^
  1180. (hash >> (8*sizeof(hash) - BLOCK_HASH_SHIFT)) ^
  1181. le32_to_cpu(here->e_hash);
  1182. here = EXT4_XATTR_NEXT(here);
  1183. }
  1184. header->h_hash = cpu_to_le32(hash);
  1185. }
  1186. #undef BLOCK_HASH_SHIFT
  1187. int __init
  1188. init_ext4_xattr(void)
  1189. {
  1190. ext4_xattr_cache = mb_cache_create("ext4_xattr", NULL,
  1191. sizeof(struct mb_cache_entry) +
  1192. sizeof(((struct mb_cache_entry *) 0)->e_indexes[0]), 1, 6);
  1193. if (!ext4_xattr_cache)
  1194. return -ENOMEM;
  1195. return 0;
  1196. }
  1197. void
  1198. exit_ext4_xattr(void)
  1199. {
  1200. if (ext4_xattr_cache)
  1201. mb_cache_destroy(ext4_xattr_cache);
  1202. ext4_xattr_cache = NULL;
  1203. }