xattr.c 34 KB

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