xattr.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756
  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/mbcache.h>
  55. #include <linux/quotaops.h>
  56. #include "ext4_jbd2.h"
  57. #include "ext4.h"
  58. #include "xattr.h"
  59. #include "acl.h"
  60. #ifdef EXT4_XATTR_DEBUG
  61. # define ea_idebug(inode, f...) do { \
  62. printk(KERN_DEBUG "inode %s:%lu: ", \
  63. inode->i_sb->s_id, inode->i_ino); \
  64. printk(f); \
  65. printk("\n"); \
  66. } while (0)
  67. # define ea_bdebug(bh, f...) do { \
  68. char b[BDEVNAME_SIZE]; \
  69. printk(KERN_DEBUG "block %s:%lu: ", \
  70. bdevname(bh->b_bdev, b), \
  71. (unsigned long) bh->b_blocknr); \
  72. printk(f); \
  73. printk("\n"); \
  74. } while (0)
  75. #else
  76. # define ea_idebug(inode, fmt, ...) no_printk(fmt, ##__VA_ARGS__)
  77. # define ea_bdebug(bh, fmt, ...) no_printk(fmt, ##__VA_ARGS__)
  78. #endif
  79. static void ext4_xattr_cache_insert(struct mb_cache *, struct buffer_head *);
  80. static struct buffer_head *ext4_xattr_cache_find(struct inode *,
  81. struct ext4_xattr_header *,
  82. struct mb_cache_entry **);
  83. static void ext4_xattr_rehash(struct ext4_xattr_header *,
  84. struct ext4_xattr_entry *);
  85. static int ext4_xattr_list(struct dentry *dentry, char *buffer,
  86. size_t buffer_size);
  87. static const struct xattr_handler *ext4_xattr_handler_map[] = {
  88. [EXT4_XATTR_INDEX_USER] = &ext4_xattr_user_handler,
  89. #ifdef CONFIG_EXT4_FS_POSIX_ACL
  90. [EXT4_XATTR_INDEX_POSIX_ACL_ACCESS] = &posix_acl_access_xattr_handler,
  91. [EXT4_XATTR_INDEX_POSIX_ACL_DEFAULT] = &posix_acl_default_xattr_handler,
  92. #endif
  93. [EXT4_XATTR_INDEX_TRUSTED] = &ext4_xattr_trusted_handler,
  94. #ifdef CONFIG_EXT4_FS_SECURITY
  95. [EXT4_XATTR_INDEX_SECURITY] = &ext4_xattr_security_handler,
  96. #endif
  97. };
  98. const struct xattr_handler *ext4_xattr_handlers[] = {
  99. &ext4_xattr_user_handler,
  100. &ext4_xattr_trusted_handler,
  101. #ifdef CONFIG_EXT4_FS_POSIX_ACL
  102. &posix_acl_access_xattr_handler,
  103. &posix_acl_default_xattr_handler,
  104. #endif
  105. #ifdef CONFIG_EXT4_FS_SECURITY
  106. &ext4_xattr_security_handler,
  107. #endif
  108. NULL
  109. };
  110. #define EXT4_GET_MB_CACHE(inode) (((struct ext4_sb_info *) \
  111. inode->i_sb->s_fs_info)->s_mb_cache)
  112. static __le32 ext4_xattr_block_csum(struct inode *inode,
  113. sector_t block_nr,
  114. struct ext4_xattr_header *hdr)
  115. {
  116. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  117. __u32 csum;
  118. __le64 dsk_block_nr = cpu_to_le64(block_nr);
  119. __u32 dummy_csum = 0;
  120. int offset = offsetof(struct ext4_xattr_header, h_checksum);
  121. csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&dsk_block_nr,
  122. sizeof(dsk_block_nr));
  123. csum = ext4_chksum(sbi, csum, (__u8 *)hdr, offset);
  124. csum = ext4_chksum(sbi, csum, (__u8 *)&dummy_csum, sizeof(dummy_csum));
  125. offset += sizeof(dummy_csum);
  126. csum = ext4_chksum(sbi, csum, (__u8 *)hdr + offset,
  127. EXT4_BLOCK_SIZE(inode->i_sb) - offset);
  128. return cpu_to_le32(csum);
  129. }
  130. static int ext4_xattr_block_csum_verify(struct inode *inode,
  131. sector_t block_nr,
  132. struct ext4_xattr_header *hdr)
  133. {
  134. if (ext4_has_metadata_csum(inode->i_sb) &&
  135. (hdr->h_checksum != ext4_xattr_block_csum(inode, block_nr, hdr)))
  136. return 0;
  137. return 1;
  138. }
  139. static void ext4_xattr_block_csum_set(struct inode *inode,
  140. sector_t block_nr,
  141. struct ext4_xattr_header *hdr)
  142. {
  143. if (!ext4_has_metadata_csum(inode->i_sb))
  144. return;
  145. hdr->h_checksum = ext4_xattr_block_csum(inode, block_nr, hdr);
  146. }
  147. static inline int ext4_handle_dirty_xattr_block(handle_t *handle,
  148. struct inode *inode,
  149. struct buffer_head *bh)
  150. {
  151. ext4_xattr_block_csum_set(inode, bh->b_blocknr, BHDR(bh));
  152. return ext4_handle_dirty_metadata(handle, inode, bh);
  153. }
  154. static inline const struct xattr_handler *
  155. ext4_xattr_handler(int name_index)
  156. {
  157. const struct xattr_handler *handler = NULL;
  158. if (name_index > 0 && name_index < ARRAY_SIZE(ext4_xattr_handler_map))
  159. handler = ext4_xattr_handler_map[name_index];
  160. return handler;
  161. }
  162. /*
  163. * Inode operation listxattr()
  164. *
  165. * d_inode(dentry)->i_mutex: don't care
  166. */
  167. ssize_t
  168. ext4_listxattr(struct dentry *dentry, char *buffer, size_t size)
  169. {
  170. return ext4_xattr_list(dentry, buffer, size);
  171. }
  172. static int
  173. ext4_xattr_check_names(struct ext4_xattr_entry *entry, void *end,
  174. void *value_start)
  175. {
  176. struct ext4_xattr_entry *e = entry;
  177. while (!IS_LAST_ENTRY(e)) {
  178. struct ext4_xattr_entry *next = EXT4_XATTR_NEXT(e);
  179. if ((void *)next >= end)
  180. return -EFSCORRUPTED;
  181. if (strnlen(e->e_name, e->e_name_len) != e->e_name_len)
  182. return -EFSCORRUPTED;
  183. e = next;
  184. }
  185. while (!IS_LAST_ENTRY(entry)) {
  186. if (entry->e_value_size != 0 &&
  187. (value_start + le16_to_cpu(entry->e_value_offs) <
  188. (void *)e + sizeof(__u32) ||
  189. value_start + le16_to_cpu(entry->e_value_offs) +
  190. le32_to_cpu(entry->e_value_size) > end))
  191. return -EFSCORRUPTED;
  192. entry = EXT4_XATTR_NEXT(entry);
  193. }
  194. return 0;
  195. }
  196. static inline int
  197. ext4_xattr_check_block(struct inode *inode, struct buffer_head *bh)
  198. {
  199. int error;
  200. if (BHDR(bh)->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC) ||
  201. BHDR(bh)->h_blocks != cpu_to_le32(1))
  202. return -EFSCORRUPTED;
  203. if (buffer_verified(bh))
  204. return 0;
  205. if (!ext4_xattr_block_csum_verify(inode, bh->b_blocknr, BHDR(bh)))
  206. return -EFSBADCRC;
  207. error = ext4_xattr_check_names(BFIRST(bh), bh->b_data + bh->b_size,
  208. bh->b_data);
  209. if (!error)
  210. set_buffer_verified(bh);
  211. return error;
  212. }
  213. static int
  214. __xattr_check_inode(struct inode *inode, struct ext4_xattr_ibody_header *header,
  215. void *end, const char *function, unsigned int line)
  216. {
  217. struct ext4_xattr_entry *entry = IFIRST(header);
  218. int error = -EFSCORRUPTED;
  219. if (((void *) header >= end) ||
  220. (header->h_magic != le32_to_cpu(EXT4_XATTR_MAGIC)))
  221. goto errout;
  222. error = ext4_xattr_check_names(entry, end, entry);
  223. errout:
  224. if (error)
  225. __ext4_error_inode(inode, function, line, 0,
  226. "corrupted in-inode xattr");
  227. return error;
  228. }
  229. #define xattr_check_inode(inode, header, end) \
  230. __xattr_check_inode((inode), (header), (end), __func__, __LINE__)
  231. static inline int
  232. ext4_xattr_check_entry(struct ext4_xattr_entry *entry, size_t size)
  233. {
  234. size_t value_size = le32_to_cpu(entry->e_value_size);
  235. if (entry->e_value_block != 0 || value_size > size ||
  236. le16_to_cpu(entry->e_value_offs) + value_size > size)
  237. return -EFSCORRUPTED;
  238. return 0;
  239. }
  240. static int
  241. ext4_xattr_find_entry(struct ext4_xattr_entry **pentry, int name_index,
  242. const char *name, size_t size, int sorted)
  243. {
  244. struct ext4_xattr_entry *entry;
  245. size_t name_len;
  246. int cmp = 1;
  247. if (name == NULL)
  248. return -EINVAL;
  249. name_len = strlen(name);
  250. entry = *pentry;
  251. for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
  252. cmp = name_index - entry->e_name_index;
  253. if (!cmp)
  254. cmp = name_len - entry->e_name_len;
  255. if (!cmp)
  256. cmp = memcmp(name, entry->e_name, name_len);
  257. if (cmp <= 0 && (sorted || cmp == 0))
  258. break;
  259. }
  260. *pentry = entry;
  261. if (!cmp && ext4_xattr_check_entry(entry, size))
  262. return -EFSCORRUPTED;
  263. return cmp ? -ENODATA : 0;
  264. }
  265. static int
  266. ext4_xattr_block_get(struct inode *inode, int name_index, const char *name,
  267. void *buffer, size_t buffer_size)
  268. {
  269. struct buffer_head *bh = NULL;
  270. struct ext4_xattr_entry *entry;
  271. size_t size;
  272. int error;
  273. struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
  274. ea_idebug(inode, "name=%d.%s, buffer=%p, buffer_size=%ld",
  275. name_index, name, buffer, (long)buffer_size);
  276. error = -ENODATA;
  277. if (!EXT4_I(inode)->i_file_acl)
  278. goto cleanup;
  279. ea_idebug(inode, "reading block %llu",
  280. (unsigned long long)EXT4_I(inode)->i_file_acl);
  281. bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
  282. if (!bh)
  283. goto cleanup;
  284. ea_bdebug(bh, "b_count=%d, refcount=%d",
  285. atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
  286. if (ext4_xattr_check_block(inode, bh)) {
  287. bad_block:
  288. EXT4_ERROR_INODE(inode, "bad block %llu",
  289. EXT4_I(inode)->i_file_acl);
  290. error = -EFSCORRUPTED;
  291. goto cleanup;
  292. }
  293. ext4_xattr_cache_insert(ext4_mb_cache, bh);
  294. entry = BFIRST(bh);
  295. error = ext4_xattr_find_entry(&entry, name_index, name, bh->b_size, 1);
  296. if (error == -EFSCORRUPTED)
  297. goto bad_block;
  298. if (error)
  299. goto cleanup;
  300. size = le32_to_cpu(entry->e_value_size);
  301. if (buffer) {
  302. error = -ERANGE;
  303. if (size > buffer_size)
  304. goto cleanup;
  305. memcpy(buffer, bh->b_data + le16_to_cpu(entry->e_value_offs),
  306. size);
  307. }
  308. error = size;
  309. cleanup:
  310. brelse(bh);
  311. return error;
  312. }
  313. int
  314. ext4_xattr_ibody_get(struct inode *inode, int name_index, const char *name,
  315. void *buffer, size_t buffer_size)
  316. {
  317. struct ext4_xattr_ibody_header *header;
  318. struct ext4_xattr_entry *entry;
  319. struct ext4_inode *raw_inode;
  320. struct ext4_iloc iloc;
  321. size_t size;
  322. void *end;
  323. int error;
  324. if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
  325. return -ENODATA;
  326. error = ext4_get_inode_loc(inode, &iloc);
  327. if (error)
  328. return error;
  329. raw_inode = ext4_raw_inode(&iloc);
  330. header = IHDR(inode, raw_inode);
  331. entry = IFIRST(header);
  332. end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
  333. error = xattr_check_inode(inode, header, end);
  334. if (error)
  335. goto cleanup;
  336. error = ext4_xattr_find_entry(&entry, name_index, name,
  337. end - (void *)entry, 0);
  338. if (error)
  339. goto cleanup;
  340. size = le32_to_cpu(entry->e_value_size);
  341. if (buffer) {
  342. error = -ERANGE;
  343. if (size > buffer_size)
  344. goto cleanup;
  345. memcpy(buffer, (void *)IFIRST(header) +
  346. le16_to_cpu(entry->e_value_offs), size);
  347. }
  348. error = size;
  349. cleanup:
  350. brelse(iloc.bh);
  351. return error;
  352. }
  353. /*
  354. * ext4_xattr_get()
  355. *
  356. * Copy an extended attribute into the buffer
  357. * provided, or compute the buffer size required.
  358. * Buffer is NULL to compute the size of the buffer required.
  359. *
  360. * Returns a negative error number on failure, or the number of bytes
  361. * used / required on success.
  362. */
  363. int
  364. ext4_xattr_get(struct inode *inode, int name_index, const char *name,
  365. void *buffer, size_t buffer_size)
  366. {
  367. int error;
  368. if (strlen(name) > 255)
  369. return -ERANGE;
  370. down_read(&EXT4_I(inode)->xattr_sem);
  371. error = ext4_xattr_ibody_get(inode, name_index, name, buffer,
  372. buffer_size);
  373. if (error == -ENODATA)
  374. error = ext4_xattr_block_get(inode, name_index, name, buffer,
  375. buffer_size);
  376. up_read(&EXT4_I(inode)->xattr_sem);
  377. return error;
  378. }
  379. static int
  380. ext4_xattr_list_entries(struct dentry *dentry, struct ext4_xattr_entry *entry,
  381. char *buffer, size_t buffer_size)
  382. {
  383. size_t rest = buffer_size;
  384. for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
  385. const struct xattr_handler *handler =
  386. ext4_xattr_handler(entry->e_name_index);
  387. if (handler) {
  388. size_t size = handler->list(handler, dentry, buffer,
  389. rest, entry->e_name,
  390. entry->e_name_len);
  391. if (buffer) {
  392. if (size > rest)
  393. return -ERANGE;
  394. buffer += size;
  395. }
  396. rest -= size;
  397. }
  398. }
  399. return buffer_size - rest;
  400. }
  401. static int
  402. ext4_xattr_block_list(struct dentry *dentry, char *buffer, size_t buffer_size)
  403. {
  404. struct inode *inode = d_inode(dentry);
  405. struct buffer_head *bh = NULL;
  406. int error;
  407. struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
  408. ea_idebug(inode, "buffer=%p, buffer_size=%ld",
  409. buffer, (long)buffer_size);
  410. error = 0;
  411. if (!EXT4_I(inode)->i_file_acl)
  412. goto cleanup;
  413. ea_idebug(inode, "reading block %llu",
  414. (unsigned long long)EXT4_I(inode)->i_file_acl);
  415. bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
  416. error = -EIO;
  417. if (!bh)
  418. goto cleanup;
  419. ea_bdebug(bh, "b_count=%d, refcount=%d",
  420. atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
  421. if (ext4_xattr_check_block(inode, bh)) {
  422. EXT4_ERROR_INODE(inode, "bad block %llu",
  423. EXT4_I(inode)->i_file_acl);
  424. error = -EFSCORRUPTED;
  425. goto cleanup;
  426. }
  427. ext4_xattr_cache_insert(ext4_mb_cache, bh);
  428. error = ext4_xattr_list_entries(dentry, BFIRST(bh), buffer, buffer_size);
  429. cleanup:
  430. brelse(bh);
  431. return error;
  432. }
  433. static int
  434. ext4_xattr_ibody_list(struct dentry *dentry, char *buffer, size_t buffer_size)
  435. {
  436. struct inode *inode = d_inode(dentry);
  437. struct ext4_xattr_ibody_header *header;
  438. struct ext4_inode *raw_inode;
  439. struct ext4_iloc iloc;
  440. void *end;
  441. int error;
  442. if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
  443. return 0;
  444. error = ext4_get_inode_loc(inode, &iloc);
  445. if (error)
  446. return error;
  447. raw_inode = ext4_raw_inode(&iloc);
  448. header = IHDR(inode, raw_inode);
  449. end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
  450. error = xattr_check_inode(inode, header, end);
  451. if (error)
  452. goto cleanup;
  453. error = ext4_xattr_list_entries(dentry, IFIRST(header),
  454. buffer, buffer_size);
  455. cleanup:
  456. brelse(iloc.bh);
  457. return error;
  458. }
  459. /*
  460. * ext4_xattr_list()
  461. *
  462. * Copy a list of attribute names into the buffer
  463. * provided, or compute the buffer size required.
  464. * Buffer is NULL to compute the size of the buffer required.
  465. *
  466. * Returns a negative error number on failure, or the number of bytes
  467. * used / required on success.
  468. */
  469. static int
  470. ext4_xattr_list(struct dentry *dentry, char *buffer, size_t buffer_size)
  471. {
  472. int ret, ret2;
  473. down_read(&EXT4_I(d_inode(dentry))->xattr_sem);
  474. ret = ret2 = ext4_xattr_ibody_list(dentry, buffer, buffer_size);
  475. if (ret < 0)
  476. goto errout;
  477. if (buffer) {
  478. buffer += ret;
  479. buffer_size -= ret;
  480. }
  481. ret = ext4_xattr_block_list(dentry, buffer, buffer_size);
  482. if (ret < 0)
  483. goto errout;
  484. ret += ret2;
  485. errout:
  486. up_read(&EXT4_I(d_inode(dentry))->xattr_sem);
  487. return ret;
  488. }
  489. /*
  490. * If the EXT4_FEATURE_COMPAT_EXT_ATTR feature of this file system is
  491. * not set, set it.
  492. */
  493. static void ext4_xattr_update_super_block(handle_t *handle,
  494. struct super_block *sb)
  495. {
  496. if (ext4_has_feature_xattr(sb))
  497. return;
  498. BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get_write_access");
  499. if (ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh) == 0) {
  500. ext4_set_feature_xattr(sb);
  501. ext4_handle_dirty_super(handle, sb);
  502. }
  503. }
  504. /*
  505. * Release the xattr block BH: If the reference count is > 1, decrement it;
  506. * otherwise free the block.
  507. */
  508. static void
  509. ext4_xattr_release_block(handle_t *handle, struct inode *inode,
  510. struct buffer_head *bh)
  511. {
  512. struct mb_cache_entry *ce = NULL;
  513. int error = 0;
  514. struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
  515. ce = mb_cache_entry_get(ext4_mb_cache, bh->b_bdev, bh->b_blocknr);
  516. BUFFER_TRACE(bh, "get_write_access");
  517. error = ext4_journal_get_write_access(handle, bh);
  518. if (error)
  519. goto out;
  520. lock_buffer(bh);
  521. if (BHDR(bh)->h_refcount == cpu_to_le32(1)) {
  522. ea_bdebug(bh, "refcount now=0; freeing");
  523. if (ce)
  524. mb_cache_entry_free(ce);
  525. get_bh(bh);
  526. unlock_buffer(bh);
  527. ext4_free_blocks(handle, inode, bh, 0, 1,
  528. EXT4_FREE_BLOCKS_METADATA |
  529. EXT4_FREE_BLOCKS_FORGET);
  530. } else {
  531. le32_add_cpu(&BHDR(bh)->h_refcount, -1);
  532. if (ce)
  533. mb_cache_entry_release(ce);
  534. /*
  535. * Beware of this ugliness: Releasing of xattr block references
  536. * from different inodes can race and so we have to protect
  537. * from a race where someone else frees the block (and releases
  538. * its journal_head) before we are done dirtying the buffer. In
  539. * nojournal mode this race is harmless and we actually cannot
  540. * call ext4_handle_dirty_xattr_block() with locked buffer as
  541. * that function can call sync_dirty_buffer() so for that case
  542. * we handle the dirtying after unlocking the buffer.
  543. */
  544. if (ext4_handle_valid(handle))
  545. error = ext4_handle_dirty_xattr_block(handle, inode,
  546. bh);
  547. unlock_buffer(bh);
  548. if (!ext4_handle_valid(handle))
  549. error = ext4_handle_dirty_xattr_block(handle, inode,
  550. bh);
  551. if (IS_SYNC(inode))
  552. ext4_handle_sync(handle);
  553. dquot_free_block(inode, EXT4_C2B(EXT4_SB(inode->i_sb), 1));
  554. ea_bdebug(bh, "refcount now=%d; releasing",
  555. le32_to_cpu(BHDR(bh)->h_refcount));
  556. }
  557. out:
  558. ext4_std_error(inode->i_sb, error);
  559. return;
  560. }
  561. /*
  562. * Find the available free space for EAs. This also returns the total number of
  563. * bytes used by EA entries.
  564. */
  565. static size_t ext4_xattr_free_space(struct ext4_xattr_entry *last,
  566. size_t *min_offs, void *base, int *total)
  567. {
  568. for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
  569. if (!last->e_value_block && last->e_value_size) {
  570. size_t offs = le16_to_cpu(last->e_value_offs);
  571. if (offs < *min_offs)
  572. *min_offs = offs;
  573. }
  574. if (total)
  575. *total += EXT4_XATTR_LEN(last->e_name_len);
  576. }
  577. return (*min_offs - ((void *)last - base) - sizeof(__u32));
  578. }
  579. static int
  580. ext4_xattr_set_entry(struct ext4_xattr_info *i, struct ext4_xattr_search *s,
  581. struct inode *inode)
  582. {
  583. struct ext4_xattr_entry *last, *next;
  584. size_t free, min_offs = s->end - s->base, name_len = strlen(i->name);
  585. /* Compute min_offs and last. */
  586. last = s->first;
  587. for (; !IS_LAST_ENTRY(last); last = next) {
  588. next = EXT4_XATTR_NEXT(last);
  589. if ((void *)next >= s->end) {
  590. EXT4_ERROR_INODE(inode, "corrupted xattr entries");
  591. return -EFSCORRUPTED;
  592. }
  593. if (!last->e_value_block && last->e_value_size) {
  594. size_t offs = le16_to_cpu(last->e_value_offs);
  595. if (offs < min_offs)
  596. min_offs = offs;
  597. }
  598. }
  599. free = min_offs - ((void *)last - s->base) - sizeof(__u32);
  600. if (!s->not_found) {
  601. if (!s->here->e_value_block && s->here->e_value_size) {
  602. size_t size = le32_to_cpu(s->here->e_value_size);
  603. free += EXT4_XATTR_SIZE(size);
  604. }
  605. free += EXT4_XATTR_LEN(name_len);
  606. }
  607. if (i->value) {
  608. if (free < EXT4_XATTR_LEN(name_len) +
  609. EXT4_XATTR_SIZE(i->value_len))
  610. return -ENOSPC;
  611. }
  612. if (i->value && s->not_found) {
  613. /* Insert the new name. */
  614. size_t size = EXT4_XATTR_LEN(name_len);
  615. size_t rest = (void *)last - (void *)s->here + sizeof(__u32);
  616. memmove((void *)s->here + size, s->here, rest);
  617. memset(s->here, 0, size);
  618. s->here->e_name_index = i->name_index;
  619. s->here->e_name_len = name_len;
  620. memcpy(s->here->e_name, i->name, name_len);
  621. } else {
  622. if (!s->here->e_value_block && s->here->e_value_size) {
  623. void *first_val = s->base + min_offs;
  624. size_t offs = le16_to_cpu(s->here->e_value_offs);
  625. void *val = s->base + offs;
  626. size_t size = EXT4_XATTR_SIZE(
  627. le32_to_cpu(s->here->e_value_size));
  628. if (i->value && size == EXT4_XATTR_SIZE(i->value_len)) {
  629. /* The old and the new value have the same
  630. size. Just replace. */
  631. s->here->e_value_size =
  632. cpu_to_le32(i->value_len);
  633. if (i->value == EXT4_ZERO_XATTR_VALUE) {
  634. memset(val, 0, size);
  635. } else {
  636. /* Clear pad bytes first. */
  637. memset(val + size - EXT4_XATTR_PAD, 0,
  638. EXT4_XATTR_PAD);
  639. memcpy(val, i->value, i->value_len);
  640. }
  641. return 0;
  642. }
  643. /* Remove the old value. */
  644. memmove(first_val + size, first_val, val - first_val);
  645. memset(first_val, 0, size);
  646. s->here->e_value_size = 0;
  647. s->here->e_value_offs = 0;
  648. min_offs += size;
  649. /* Adjust all value offsets. */
  650. last = s->first;
  651. while (!IS_LAST_ENTRY(last)) {
  652. size_t o = le16_to_cpu(last->e_value_offs);
  653. if (!last->e_value_block &&
  654. last->e_value_size && o < offs)
  655. last->e_value_offs =
  656. cpu_to_le16(o + size);
  657. last = EXT4_XATTR_NEXT(last);
  658. }
  659. }
  660. if (!i->value) {
  661. /* Remove the old name. */
  662. size_t size = EXT4_XATTR_LEN(name_len);
  663. last = ENTRY((void *)last - size);
  664. memmove(s->here, (void *)s->here + size,
  665. (void *)last - (void *)s->here + sizeof(__u32));
  666. memset(last, 0, size);
  667. }
  668. }
  669. if (i->value) {
  670. /* Insert the new value. */
  671. s->here->e_value_size = cpu_to_le32(i->value_len);
  672. if (i->value_len) {
  673. size_t size = EXT4_XATTR_SIZE(i->value_len);
  674. void *val = s->base + min_offs - size;
  675. s->here->e_value_offs = cpu_to_le16(min_offs - size);
  676. if (i->value == EXT4_ZERO_XATTR_VALUE) {
  677. memset(val, 0, size);
  678. } else {
  679. /* Clear the pad bytes first. */
  680. memset(val + size - EXT4_XATTR_PAD, 0,
  681. EXT4_XATTR_PAD);
  682. memcpy(val, i->value, i->value_len);
  683. }
  684. }
  685. }
  686. return 0;
  687. }
  688. struct ext4_xattr_block_find {
  689. struct ext4_xattr_search s;
  690. struct buffer_head *bh;
  691. };
  692. static int
  693. ext4_xattr_block_find(struct inode *inode, struct ext4_xattr_info *i,
  694. struct ext4_xattr_block_find *bs)
  695. {
  696. struct super_block *sb = inode->i_sb;
  697. int error;
  698. ea_idebug(inode, "name=%d.%s, value=%p, value_len=%ld",
  699. i->name_index, i->name, i->value, (long)i->value_len);
  700. if (EXT4_I(inode)->i_file_acl) {
  701. /* The inode already has an extended attribute block. */
  702. bs->bh = sb_bread(sb, EXT4_I(inode)->i_file_acl);
  703. error = -EIO;
  704. if (!bs->bh)
  705. goto cleanup;
  706. ea_bdebug(bs->bh, "b_count=%d, refcount=%d",
  707. atomic_read(&(bs->bh->b_count)),
  708. le32_to_cpu(BHDR(bs->bh)->h_refcount));
  709. if (ext4_xattr_check_block(inode, bs->bh)) {
  710. EXT4_ERROR_INODE(inode, "bad block %llu",
  711. EXT4_I(inode)->i_file_acl);
  712. error = -EFSCORRUPTED;
  713. goto cleanup;
  714. }
  715. /* Find the named attribute. */
  716. bs->s.base = BHDR(bs->bh);
  717. bs->s.first = BFIRST(bs->bh);
  718. bs->s.end = bs->bh->b_data + bs->bh->b_size;
  719. bs->s.here = bs->s.first;
  720. error = ext4_xattr_find_entry(&bs->s.here, i->name_index,
  721. i->name, bs->bh->b_size, 1);
  722. if (error && error != -ENODATA)
  723. goto cleanup;
  724. bs->s.not_found = error;
  725. }
  726. error = 0;
  727. cleanup:
  728. return error;
  729. }
  730. static int
  731. ext4_xattr_block_set(handle_t *handle, struct inode *inode,
  732. struct ext4_xattr_info *i,
  733. struct ext4_xattr_block_find *bs)
  734. {
  735. struct super_block *sb = inode->i_sb;
  736. struct buffer_head *new_bh = NULL;
  737. struct ext4_xattr_search *s = &bs->s;
  738. struct mb_cache_entry *ce = NULL;
  739. int error = 0;
  740. struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
  741. #define header(x) ((struct ext4_xattr_header *)(x))
  742. if (i->value && i->value_len > sb->s_blocksize)
  743. return -ENOSPC;
  744. if (s->base) {
  745. ce = mb_cache_entry_get(ext4_mb_cache, bs->bh->b_bdev,
  746. bs->bh->b_blocknr);
  747. BUFFER_TRACE(bs->bh, "get_write_access");
  748. error = ext4_journal_get_write_access(handle, bs->bh);
  749. if (error)
  750. goto cleanup;
  751. lock_buffer(bs->bh);
  752. if (header(s->base)->h_refcount == cpu_to_le32(1)) {
  753. if (ce) {
  754. mb_cache_entry_free(ce);
  755. ce = NULL;
  756. }
  757. ea_bdebug(bs->bh, "modifying in-place");
  758. error = ext4_xattr_set_entry(i, s, inode);
  759. if (!error) {
  760. if (!IS_LAST_ENTRY(s->first))
  761. ext4_xattr_rehash(header(s->base),
  762. s->here);
  763. }
  764. unlock_buffer(bs->bh);
  765. if (error == -EFSCORRUPTED)
  766. goto bad_block;
  767. if (!error)
  768. error = ext4_handle_dirty_xattr_block(handle,
  769. inode,
  770. bs->bh);
  771. if (error)
  772. goto cleanup;
  773. goto inserted;
  774. } else {
  775. int offset = (char *)s->here - bs->bh->b_data;
  776. unlock_buffer(bs->bh);
  777. if (ce) {
  778. mb_cache_entry_release(ce);
  779. ce = NULL;
  780. }
  781. ea_bdebug(bs->bh, "cloning");
  782. s->base = kmalloc(bs->bh->b_size, GFP_NOFS);
  783. error = -ENOMEM;
  784. if (s->base == NULL)
  785. goto cleanup;
  786. memcpy(s->base, BHDR(bs->bh), bs->bh->b_size);
  787. s->first = ENTRY(header(s->base)+1);
  788. header(s->base)->h_refcount = cpu_to_le32(1);
  789. s->here = ENTRY(s->base + offset);
  790. s->end = s->base + bs->bh->b_size;
  791. }
  792. } else {
  793. /* Allocate a buffer where we construct the new block. */
  794. s->base = kzalloc(sb->s_blocksize, GFP_NOFS);
  795. /* assert(header == s->base) */
  796. error = -ENOMEM;
  797. if (s->base == NULL)
  798. goto cleanup;
  799. header(s->base)->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
  800. header(s->base)->h_blocks = cpu_to_le32(1);
  801. header(s->base)->h_refcount = cpu_to_le32(1);
  802. s->first = ENTRY(header(s->base)+1);
  803. s->here = ENTRY(header(s->base)+1);
  804. s->end = s->base + sb->s_blocksize;
  805. }
  806. error = ext4_xattr_set_entry(i, s, inode);
  807. if (error == -EFSCORRUPTED)
  808. goto bad_block;
  809. if (error)
  810. goto cleanup;
  811. if (!IS_LAST_ENTRY(s->first))
  812. ext4_xattr_rehash(header(s->base), s->here);
  813. inserted:
  814. if (!IS_LAST_ENTRY(s->first)) {
  815. new_bh = ext4_xattr_cache_find(inode, header(s->base), &ce);
  816. if (new_bh) {
  817. /* We found an identical block in the cache. */
  818. if (new_bh == bs->bh)
  819. ea_bdebug(new_bh, "keeping");
  820. else {
  821. /* The old block is released after updating
  822. the inode. */
  823. error = dquot_alloc_block(inode,
  824. EXT4_C2B(EXT4_SB(sb), 1));
  825. if (error)
  826. goto cleanup;
  827. BUFFER_TRACE(new_bh, "get_write_access");
  828. error = ext4_journal_get_write_access(handle,
  829. new_bh);
  830. if (error)
  831. goto cleanup_dquot;
  832. lock_buffer(new_bh);
  833. le32_add_cpu(&BHDR(new_bh)->h_refcount, 1);
  834. ea_bdebug(new_bh, "reusing; refcount now=%d",
  835. le32_to_cpu(BHDR(new_bh)->h_refcount));
  836. unlock_buffer(new_bh);
  837. error = ext4_handle_dirty_xattr_block(handle,
  838. inode,
  839. new_bh);
  840. if (error)
  841. goto cleanup_dquot;
  842. }
  843. mb_cache_entry_release(ce);
  844. ce = NULL;
  845. } else if (bs->bh && s->base == bs->bh->b_data) {
  846. /* We were modifying this block in-place. */
  847. ea_bdebug(bs->bh, "keeping this block");
  848. ext4_xattr_cache_insert(ext4_mb_cache, bs->bh);
  849. new_bh = bs->bh;
  850. get_bh(new_bh);
  851. } else {
  852. /* We need to allocate a new block */
  853. ext4_fsblk_t goal, block;
  854. goal = ext4_group_first_block_no(sb,
  855. EXT4_I(inode)->i_block_group);
  856. /* non-extent files can't have physical blocks past 2^32 */
  857. if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
  858. goal = goal & EXT4_MAX_BLOCK_FILE_PHYS;
  859. block = ext4_new_meta_blocks(handle, inode, goal, 0,
  860. NULL, &error);
  861. if (error)
  862. goto cleanup;
  863. if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
  864. BUG_ON(block > EXT4_MAX_BLOCK_FILE_PHYS);
  865. ea_idebug(inode, "creating block %llu",
  866. (unsigned long long)block);
  867. new_bh = sb_getblk(sb, block);
  868. if (unlikely(!new_bh)) {
  869. error = -ENOMEM;
  870. getblk_failed:
  871. ext4_free_blocks(handle, inode, NULL, block, 1,
  872. EXT4_FREE_BLOCKS_METADATA);
  873. goto cleanup;
  874. }
  875. lock_buffer(new_bh);
  876. error = ext4_journal_get_create_access(handle, new_bh);
  877. if (error) {
  878. unlock_buffer(new_bh);
  879. error = -EIO;
  880. goto getblk_failed;
  881. }
  882. memcpy(new_bh->b_data, s->base, new_bh->b_size);
  883. set_buffer_uptodate(new_bh);
  884. unlock_buffer(new_bh);
  885. ext4_xattr_cache_insert(ext4_mb_cache, new_bh);
  886. error = ext4_handle_dirty_xattr_block(handle,
  887. inode, new_bh);
  888. if (error)
  889. goto cleanup;
  890. }
  891. }
  892. /* Update the inode. */
  893. EXT4_I(inode)->i_file_acl = new_bh ? new_bh->b_blocknr : 0;
  894. /* Drop the previous xattr block. */
  895. if (bs->bh && bs->bh != new_bh)
  896. ext4_xattr_release_block(handle, inode, bs->bh);
  897. error = 0;
  898. cleanup:
  899. if (ce)
  900. mb_cache_entry_release(ce);
  901. brelse(new_bh);
  902. if (!(bs->bh && s->base == bs->bh->b_data))
  903. kfree(s->base);
  904. return error;
  905. cleanup_dquot:
  906. dquot_free_block(inode, EXT4_C2B(EXT4_SB(sb), 1));
  907. goto cleanup;
  908. bad_block:
  909. EXT4_ERROR_INODE(inode, "bad block %llu",
  910. EXT4_I(inode)->i_file_acl);
  911. goto cleanup;
  912. #undef header
  913. }
  914. int ext4_xattr_ibody_find(struct inode *inode, struct ext4_xattr_info *i,
  915. struct ext4_xattr_ibody_find *is)
  916. {
  917. struct ext4_xattr_ibody_header *header;
  918. struct ext4_inode *raw_inode;
  919. int error;
  920. if (EXT4_I(inode)->i_extra_isize == 0)
  921. return 0;
  922. raw_inode = ext4_raw_inode(&is->iloc);
  923. header = IHDR(inode, raw_inode);
  924. is->s.base = is->s.first = IFIRST(header);
  925. is->s.here = is->s.first;
  926. is->s.end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
  927. if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
  928. error = xattr_check_inode(inode, header, is->s.end);
  929. if (error)
  930. return error;
  931. /* Find the named attribute. */
  932. error = ext4_xattr_find_entry(&is->s.here, i->name_index,
  933. i->name, is->s.end -
  934. (void *)is->s.base, 0);
  935. if (error && error != -ENODATA)
  936. return error;
  937. is->s.not_found = error;
  938. }
  939. return 0;
  940. }
  941. int ext4_xattr_ibody_inline_set(handle_t *handle, struct inode *inode,
  942. struct ext4_xattr_info *i,
  943. struct ext4_xattr_ibody_find *is)
  944. {
  945. struct ext4_xattr_ibody_header *header;
  946. struct ext4_xattr_search *s = &is->s;
  947. int error;
  948. if (EXT4_I(inode)->i_extra_isize == 0)
  949. return -ENOSPC;
  950. error = ext4_xattr_set_entry(i, s, inode);
  951. if (error)
  952. return error;
  953. header = IHDR(inode, ext4_raw_inode(&is->iloc));
  954. if (!IS_LAST_ENTRY(s->first)) {
  955. header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
  956. ext4_set_inode_state(inode, EXT4_STATE_XATTR);
  957. } else {
  958. header->h_magic = cpu_to_le32(0);
  959. ext4_clear_inode_state(inode, EXT4_STATE_XATTR);
  960. }
  961. return 0;
  962. }
  963. static int ext4_xattr_ibody_set(handle_t *handle, struct inode *inode,
  964. struct ext4_xattr_info *i,
  965. struct ext4_xattr_ibody_find *is)
  966. {
  967. struct ext4_xattr_ibody_header *header;
  968. struct ext4_xattr_search *s = &is->s;
  969. int error;
  970. if (EXT4_I(inode)->i_extra_isize == 0)
  971. return -ENOSPC;
  972. error = ext4_xattr_set_entry(i, s, inode);
  973. if (error)
  974. return error;
  975. header = IHDR(inode, ext4_raw_inode(&is->iloc));
  976. if (!IS_LAST_ENTRY(s->first)) {
  977. header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
  978. ext4_set_inode_state(inode, EXT4_STATE_XATTR);
  979. } else {
  980. header->h_magic = cpu_to_le32(0);
  981. ext4_clear_inode_state(inode, EXT4_STATE_XATTR);
  982. }
  983. return 0;
  984. }
  985. /*
  986. * ext4_xattr_set_handle()
  987. *
  988. * Create, replace or remove an extended attribute for this inode. Value
  989. * is NULL to remove an existing extended attribute, and non-NULL to
  990. * either replace an existing extended attribute, or create a new extended
  991. * attribute. The flags XATTR_REPLACE and XATTR_CREATE
  992. * specify that an extended attribute must exist and must not exist
  993. * previous to the call, respectively.
  994. *
  995. * Returns 0, or a negative error number on failure.
  996. */
  997. int
  998. ext4_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index,
  999. const char *name, const void *value, size_t value_len,
  1000. int flags)
  1001. {
  1002. struct ext4_xattr_info i = {
  1003. .name_index = name_index,
  1004. .name = name,
  1005. .value = value,
  1006. .value_len = value_len,
  1007. };
  1008. struct ext4_xattr_ibody_find is = {
  1009. .s = { .not_found = -ENODATA, },
  1010. };
  1011. struct ext4_xattr_block_find bs = {
  1012. .s = { .not_found = -ENODATA, },
  1013. };
  1014. int no_expand;
  1015. int error;
  1016. if (!name)
  1017. return -EINVAL;
  1018. if (strlen(name) > 255)
  1019. return -ERANGE;
  1020. ext4_write_lock_xattr(inode, &no_expand);
  1021. error = ext4_reserve_inode_write(handle, inode, &is.iloc);
  1022. if (error)
  1023. goto cleanup;
  1024. if (ext4_test_inode_state(inode, EXT4_STATE_NEW)) {
  1025. struct ext4_inode *raw_inode = ext4_raw_inode(&is.iloc);
  1026. memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size);
  1027. ext4_clear_inode_state(inode, EXT4_STATE_NEW);
  1028. }
  1029. error = ext4_xattr_ibody_find(inode, &i, &is);
  1030. if (error)
  1031. goto cleanup;
  1032. if (is.s.not_found)
  1033. error = ext4_xattr_block_find(inode, &i, &bs);
  1034. if (error)
  1035. goto cleanup;
  1036. if (is.s.not_found && bs.s.not_found) {
  1037. error = -ENODATA;
  1038. if (flags & XATTR_REPLACE)
  1039. goto cleanup;
  1040. error = 0;
  1041. if (!value)
  1042. goto cleanup;
  1043. } else {
  1044. error = -EEXIST;
  1045. if (flags & XATTR_CREATE)
  1046. goto cleanup;
  1047. }
  1048. if (!value) {
  1049. if (!is.s.not_found)
  1050. error = ext4_xattr_ibody_set(handle, inode, &i, &is);
  1051. else if (!bs.s.not_found)
  1052. error = ext4_xattr_block_set(handle, inode, &i, &bs);
  1053. } else {
  1054. error = ext4_xattr_ibody_set(handle, inode, &i, &is);
  1055. if (!error && !bs.s.not_found) {
  1056. i.value = NULL;
  1057. error = ext4_xattr_block_set(handle, inode, &i, &bs);
  1058. } else if (error == -ENOSPC) {
  1059. if (EXT4_I(inode)->i_file_acl && !bs.s.base) {
  1060. brelse(bs.bh);
  1061. bs.bh = NULL;
  1062. error = ext4_xattr_block_find(inode, &i, &bs);
  1063. if (error)
  1064. goto cleanup;
  1065. }
  1066. error = ext4_xattr_block_set(handle, inode, &i, &bs);
  1067. if (error)
  1068. goto cleanup;
  1069. if (!is.s.not_found) {
  1070. i.value = NULL;
  1071. error = ext4_xattr_ibody_set(handle, inode, &i,
  1072. &is);
  1073. }
  1074. }
  1075. }
  1076. if (!error) {
  1077. ext4_xattr_update_super_block(handle, inode->i_sb);
  1078. inode->i_ctime = ext4_current_time(inode);
  1079. if (!value)
  1080. no_expand = 0;
  1081. error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
  1082. /*
  1083. * The bh is consumed by ext4_mark_iloc_dirty, even with
  1084. * error != 0.
  1085. */
  1086. is.iloc.bh = NULL;
  1087. if (IS_SYNC(inode))
  1088. ext4_handle_sync(handle);
  1089. }
  1090. cleanup:
  1091. brelse(is.iloc.bh);
  1092. brelse(bs.bh);
  1093. ext4_write_unlock_xattr(inode, &no_expand);
  1094. return error;
  1095. }
  1096. /*
  1097. * ext4_xattr_set()
  1098. *
  1099. * Like ext4_xattr_set_handle, but start from an inode. This extended
  1100. * attribute modification is a filesystem transaction by itself.
  1101. *
  1102. * Returns 0, or a negative error number on failure.
  1103. */
  1104. int
  1105. ext4_xattr_set(struct inode *inode, int name_index, const char *name,
  1106. const void *value, size_t value_len, int flags)
  1107. {
  1108. handle_t *handle;
  1109. int error, retries = 0;
  1110. int credits = ext4_jbd2_credits_xattr(inode);
  1111. retry:
  1112. handle = ext4_journal_start(inode, EXT4_HT_XATTR, credits);
  1113. if (IS_ERR(handle)) {
  1114. error = PTR_ERR(handle);
  1115. } else {
  1116. int error2;
  1117. error = ext4_xattr_set_handle(handle, inode, name_index, name,
  1118. value, value_len, flags);
  1119. error2 = ext4_journal_stop(handle);
  1120. if (error == -ENOSPC &&
  1121. ext4_should_retry_alloc(inode->i_sb, &retries))
  1122. goto retry;
  1123. if (error == 0)
  1124. error = error2;
  1125. }
  1126. return error;
  1127. }
  1128. /*
  1129. * Shift the EA entries in the inode to create space for the increased
  1130. * i_extra_isize.
  1131. */
  1132. static void ext4_xattr_shift_entries(struct ext4_xattr_entry *entry,
  1133. int value_offs_shift, void *to,
  1134. void *from, size_t n, int blocksize)
  1135. {
  1136. struct ext4_xattr_entry *last = entry;
  1137. int new_offs;
  1138. /* Adjust the value offsets of the entries */
  1139. for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
  1140. if (!last->e_value_block && last->e_value_size) {
  1141. new_offs = le16_to_cpu(last->e_value_offs) +
  1142. value_offs_shift;
  1143. BUG_ON(new_offs + le32_to_cpu(last->e_value_size)
  1144. > blocksize);
  1145. last->e_value_offs = cpu_to_le16(new_offs);
  1146. }
  1147. }
  1148. /* Shift the entries by n bytes */
  1149. memmove(to, from, n);
  1150. }
  1151. /*
  1152. * Expand an inode by new_extra_isize bytes when EAs are present.
  1153. * Returns 0 on success or negative error number on failure.
  1154. */
  1155. int ext4_expand_extra_isize_ea(struct inode *inode, int new_extra_isize,
  1156. struct ext4_inode *raw_inode, handle_t *handle)
  1157. {
  1158. struct ext4_xattr_ibody_header *header;
  1159. struct ext4_xattr_entry *entry, *last, *first;
  1160. struct buffer_head *bh = NULL;
  1161. struct ext4_xattr_ibody_find *is = NULL;
  1162. struct ext4_xattr_block_find *bs = NULL;
  1163. char *buffer = NULL, *b_entry_name = NULL;
  1164. size_t min_offs, free;
  1165. int total_ino;
  1166. void *base, *start, *end;
  1167. int error = 0, tried_min_extra_isize = 0;
  1168. int s_min_extra_isize = le16_to_cpu(EXT4_SB(inode->i_sb)->s_es->s_min_extra_isize);
  1169. int isize_diff; /* How much do we need to grow i_extra_isize */
  1170. int no_expand;
  1171. if (ext4_write_trylock_xattr(inode, &no_expand) == 0)
  1172. return 0;
  1173. retry:
  1174. isize_diff = new_extra_isize - EXT4_I(inode)->i_extra_isize;
  1175. if (EXT4_I(inode)->i_extra_isize >= new_extra_isize)
  1176. goto out;
  1177. header = IHDR(inode, raw_inode);
  1178. entry = IFIRST(header);
  1179. /*
  1180. * Check if enough free space is available in the inode to shift the
  1181. * entries ahead by new_extra_isize.
  1182. */
  1183. base = start = entry;
  1184. end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
  1185. min_offs = end - base;
  1186. last = entry;
  1187. total_ino = sizeof(struct ext4_xattr_ibody_header);
  1188. error = xattr_check_inode(inode, header, end);
  1189. if (error)
  1190. goto cleanup;
  1191. free = ext4_xattr_free_space(last, &min_offs, base, &total_ino);
  1192. if (free >= isize_diff) {
  1193. entry = IFIRST(header);
  1194. ext4_xattr_shift_entries(entry, EXT4_I(inode)->i_extra_isize
  1195. - new_extra_isize, (void *)raw_inode +
  1196. EXT4_GOOD_OLD_INODE_SIZE + new_extra_isize,
  1197. (void *)header, total_ino,
  1198. inode->i_sb->s_blocksize);
  1199. EXT4_I(inode)->i_extra_isize = new_extra_isize;
  1200. goto out;
  1201. }
  1202. /*
  1203. * Enough free space isn't available in the inode, check if
  1204. * EA block can hold new_extra_isize bytes.
  1205. */
  1206. if (EXT4_I(inode)->i_file_acl) {
  1207. bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
  1208. error = -EIO;
  1209. if (!bh)
  1210. goto cleanup;
  1211. if (ext4_xattr_check_block(inode, bh)) {
  1212. EXT4_ERROR_INODE(inode, "bad block %llu",
  1213. EXT4_I(inode)->i_file_acl);
  1214. error = -EFSCORRUPTED;
  1215. goto cleanup;
  1216. }
  1217. base = BHDR(bh);
  1218. first = BFIRST(bh);
  1219. end = bh->b_data + bh->b_size;
  1220. min_offs = end - base;
  1221. free = ext4_xattr_free_space(first, &min_offs, base, NULL);
  1222. if (free < isize_diff) {
  1223. if (!tried_min_extra_isize && s_min_extra_isize) {
  1224. tried_min_extra_isize++;
  1225. new_extra_isize = s_min_extra_isize;
  1226. brelse(bh);
  1227. goto retry;
  1228. }
  1229. error = -1;
  1230. goto cleanup;
  1231. }
  1232. } else {
  1233. free = inode->i_sb->s_blocksize;
  1234. }
  1235. while (isize_diff > 0) {
  1236. size_t offs, size, entry_size;
  1237. struct ext4_xattr_entry *small_entry = NULL;
  1238. struct ext4_xattr_info i = {
  1239. .value = NULL,
  1240. .value_len = 0,
  1241. };
  1242. unsigned int total_size; /* EA entry size + value size */
  1243. unsigned int shift_bytes; /* No. of bytes to shift EAs by? */
  1244. unsigned int min_total_size = ~0U;
  1245. is = kzalloc(sizeof(struct ext4_xattr_ibody_find), GFP_NOFS);
  1246. bs = kzalloc(sizeof(struct ext4_xattr_block_find), GFP_NOFS);
  1247. if (!is || !bs) {
  1248. error = -ENOMEM;
  1249. goto cleanup;
  1250. }
  1251. is->s.not_found = -ENODATA;
  1252. bs->s.not_found = -ENODATA;
  1253. is->iloc.bh = NULL;
  1254. bs->bh = NULL;
  1255. last = IFIRST(header);
  1256. /* Find the entry best suited to be pushed into EA block */
  1257. entry = NULL;
  1258. for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
  1259. /* never move system.data out of the inode */
  1260. if ((last->e_name_len == 4) &&
  1261. (last->e_name_index == EXT4_XATTR_INDEX_SYSTEM) &&
  1262. !memcmp(last->e_name, "data", 4))
  1263. continue;
  1264. total_size =
  1265. EXT4_XATTR_SIZE(le32_to_cpu(last->e_value_size)) +
  1266. EXT4_XATTR_LEN(last->e_name_len);
  1267. if (total_size <= free && total_size < min_total_size) {
  1268. if (total_size < isize_diff) {
  1269. small_entry = last;
  1270. } else {
  1271. entry = last;
  1272. min_total_size = total_size;
  1273. }
  1274. }
  1275. }
  1276. if (entry == NULL) {
  1277. if (small_entry) {
  1278. entry = small_entry;
  1279. } else {
  1280. if (!tried_min_extra_isize &&
  1281. s_min_extra_isize) {
  1282. tried_min_extra_isize++;
  1283. new_extra_isize = s_min_extra_isize;
  1284. kfree(is); is = NULL;
  1285. kfree(bs); bs = NULL;
  1286. brelse(bh);
  1287. goto retry;
  1288. }
  1289. error = -1;
  1290. goto cleanup;
  1291. }
  1292. }
  1293. offs = le16_to_cpu(entry->e_value_offs);
  1294. size = le32_to_cpu(entry->e_value_size);
  1295. entry_size = EXT4_XATTR_LEN(entry->e_name_len);
  1296. i.name_index = entry->e_name_index,
  1297. buffer = kmalloc(EXT4_XATTR_SIZE(size), GFP_NOFS);
  1298. b_entry_name = kmalloc(entry->e_name_len + 1, GFP_NOFS);
  1299. if (!buffer || !b_entry_name) {
  1300. error = -ENOMEM;
  1301. goto cleanup;
  1302. }
  1303. /* Save the entry name and the entry value */
  1304. memcpy(buffer, (void *)IFIRST(header) + offs,
  1305. EXT4_XATTR_SIZE(size));
  1306. memcpy(b_entry_name, entry->e_name, entry->e_name_len);
  1307. b_entry_name[entry->e_name_len] = '\0';
  1308. i.name = b_entry_name;
  1309. error = ext4_get_inode_loc(inode, &is->iloc);
  1310. if (error)
  1311. goto cleanup;
  1312. error = ext4_xattr_ibody_find(inode, &i, is);
  1313. if (error)
  1314. goto cleanup;
  1315. /* Remove the chosen entry from the inode */
  1316. error = ext4_xattr_ibody_set(handle, inode, &i, is);
  1317. if (error)
  1318. goto cleanup;
  1319. total_ino -= entry_size;
  1320. entry = IFIRST(header);
  1321. if (entry_size + EXT4_XATTR_SIZE(size) >= isize_diff)
  1322. shift_bytes = isize_diff;
  1323. else
  1324. shift_bytes = entry_size + EXT4_XATTR_SIZE(size);
  1325. /* Adjust the offsets and shift the remaining entries ahead */
  1326. ext4_xattr_shift_entries(entry, -shift_bytes,
  1327. (void *)raw_inode + EXT4_GOOD_OLD_INODE_SIZE +
  1328. EXT4_I(inode)->i_extra_isize + shift_bytes,
  1329. (void *)header, total_ino, inode->i_sb->s_blocksize);
  1330. isize_diff -= shift_bytes;
  1331. EXT4_I(inode)->i_extra_isize += shift_bytes;
  1332. header = IHDR(inode, raw_inode);
  1333. i.name = b_entry_name;
  1334. i.value = buffer;
  1335. i.value_len = size;
  1336. error = ext4_xattr_block_find(inode, &i, bs);
  1337. if (error)
  1338. goto cleanup;
  1339. /* Add entry which was removed from the inode into the block */
  1340. error = ext4_xattr_block_set(handle, inode, &i, bs);
  1341. if (error)
  1342. goto cleanup;
  1343. kfree(b_entry_name);
  1344. kfree(buffer);
  1345. b_entry_name = NULL;
  1346. buffer = NULL;
  1347. brelse(is->iloc.bh);
  1348. kfree(is);
  1349. kfree(bs);
  1350. }
  1351. brelse(bh);
  1352. out:
  1353. ext4_write_unlock_xattr(inode, &no_expand);
  1354. return 0;
  1355. cleanup:
  1356. kfree(b_entry_name);
  1357. kfree(buffer);
  1358. if (is)
  1359. brelse(is->iloc.bh);
  1360. if (bs)
  1361. brelse(bs->bh);
  1362. kfree(is);
  1363. kfree(bs);
  1364. brelse(bh);
  1365. /*
  1366. * Inode size expansion failed; don't try again
  1367. */
  1368. no_expand = 1;
  1369. ext4_write_unlock_xattr(inode, &no_expand);
  1370. return error;
  1371. }
  1372. /*
  1373. * ext4_xattr_delete_inode()
  1374. *
  1375. * Free extended attribute resources associated with this inode. This
  1376. * is called immediately before an inode is freed. We have exclusive
  1377. * access to the inode.
  1378. */
  1379. void
  1380. ext4_xattr_delete_inode(handle_t *handle, struct inode *inode)
  1381. {
  1382. struct buffer_head *bh = NULL;
  1383. if (!EXT4_I(inode)->i_file_acl)
  1384. goto cleanup;
  1385. bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
  1386. if (!bh) {
  1387. EXT4_ERROR_INODE(inode, "block %llu read error",
  1388. EXT4_I(inode)->i_file_acl);
  1389. goto cleanup;
  1390. }
  1391. if (BHDR(bh)->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC) ||
  1392. BHDR(bh)->h_blocks != cpu_to_le32(1)) {
  1393. EXT4_ERROR_INODE(inode, "bad block %llu",
  1394. EXT4_I(inode)->i_file_acl);
  1395. goto cleanup;
  1396. }
  1397. ext4_xattr_release_block(handle, inode, bh);
  1398. EXT4_I(inode)->i_file_acl = 0;
  1399. cleanup:
  1400. brelse(bh);
  1401. }
  1402. /*
  1403. * ext4_xattr_put_super()
  1404. *
  1405. * This is called when a file system is unmounted.
  1406. */
  1407. void
  1408. ext4_xattr_put_super(struct super_block *sb)
  1409. {
  1410. mb_cache_shrink(sb->s_bdev);
  1411. }
  1412. /*
  1413. * ext4_xattr_cache_insert()
  1414. *
  1415. * Create a new entry in the extended attribute cache, and insert
  1416. * it unless such an entry is already in the cache.
  1417. *
  1418. * Returns 0, or a negative error number on failure.
  1419. */
  1420. static void
  1421. ext4_xattr_cache_insert(struct mb_cache *ext4_mb_cache, struct buffer_head *bh)
  1422. {
  1423. __u32 hash = le32_to_cpu(BHDR(bh)->h_hash);
  1424. struct mb_cache_entry *ce;
  1425. int error;
  1426. ce = mb_cache_entry_alloc(ext4_mb_cache, GFP_NOFS);
  1427. if (!ce) {
  1428. ea_bdebug(bh, "out of memory");
  1429. return;
  1430. }
  1431. error = mb_cache_entry_insert(ce, bh->b_bdev, bh->b_blocknr, hash);
  1432. if (error) {
  1433. mb_cache_entry_free(ce);
  1434. if (error == -EBUSY) {
  1435. ea_bdebug(bh, "already in cache");
  1436. error = 0;
  1437. }
  1438. } else {
  1439. ea_bdebug(bh, "inserting [%x]", (int)hash);
  1440. mb_cache_entry_release(ce);
  1441. }
  1442. }
  1443. /*
  1444. * ext4_xattr_cmp()
  1445. *
  1446. * Compare two extended attribute blocks for equality.
  1447. *
  1448. * Returns 0 if the blocks are equal, 1 if they differ, and
  1449. * a negative error number on errors.
  1450. */
  1451. static int
  1452. ext4_xattr_cmp(struct ext4_xattr_header *header1,
  1453. struct ext4_xattr_header *header2)
  1454. {
  1455. struct ext4_xattr_entry *entry1, *entry2;
  1456. entry1 = ENTRY(header1+1);
  1457. entry2 = ENTRY(header2+1);
  1458. while (!IS_LAST_ENTRY(entry1)) {
  1459. if (IS_LAST_ENTRY(entry2))
  1460. return 1;
  1461. if (entry1->e_hash != entry2->e_hash ||
  1462. entry1->e_name_index != entry2->e_name_index ||
  1463. entry1->e_name_len != entry2->e_name_len ||
  1464. entry1->e_value_size != entry2->e_value_size ||
  1465. memcmp(entry1->e_name, entry2->e_name, entry1->e_name_len))
  1466. return 1;
  1467. if (entry1->e_value_block != 0 || entry2->e_value_block != 0)
  1468. return -EFSCORRUPTED;
  1469. if (memcmp((char *)header1 + le16_to_cpu(entry1->e_value_offs),
  1470. (char *)header2 + le16_to_cpu(entry2->e_value_offs),
  1471. le32_to_cpu(entry1->e_value_size)))
  1472. return 1;
  1473. entry1 = EXT4_XATTR_NEXT(entry1);
  1474. entry2 = EXT4_XATTR_NEXT(entry2);
  1475. }
  1476. if (!IS_LAST_ENTRY(entry2))
  1477. return 1;
  1478. return 0;
  1479. }
  1480. /*
  1481. * ext4_xattr_cache_find()
  1482. *
  1483. * Find an identical extended attribute block.
  1484. *
  1485. * Returns a pointer to the block found, or NULL if such a block was
  1486. * not found or an error occurred.
  1487. */
  1488. static struct buffer_head *
  1489. ext4_xattr_cache_find(struct inode *inode, struct ext4_xattr_header *header,
  1490. struct mb_cache_entry **pce)
  1491. {
  1492. __u32 hash = le32_to_cpu(header->h_hash);
  1493. struct mb_cache_entry *ce;
  1494. struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
  1495. if (!header->h_hash)
  1496. return NULL; /* never share */
  1497. ea_idebug(inode, "looking for cached blocks [%x]", (int)hash);
  1498. again:
  1499. ce = mb_cache_entry_find_first(ext4_mb_cache, inode->i_sb->s_bdev,
  1500. hash);
  1501. while (ce) {
  1502. struct buffer_head *bh;
  1503. if (IS_ERR(ce)) {
  1504. if (PTR_ERR(ce) == -EAGAIN)
  1505. goto again;
  1506. break;
  1507. }
  1508. bh = sb_bread(inode->i_sb, ce->e_block);
  1509. if (!bh) {
  1510. EXT4_ERROR_INODE(inode, "block %lu read error",
  1511. (unsigned long) ce->e_block);
  1512. } else if (le32_to_cpu(BHDR(bh)->h_refcount) >=
  1513. EXT4_XATTR_REFCOUNT_MAX) {
  1514. ea_idebug(inode, "block %lu refcount %d>=%d",
  1515. (unsigned long) ce->e_block,
  1516. le32_to_cpu(BHDR(bh)->h_refcount),
  1517. EXT4_XATTR_REFCOUNT_MAX);
  1518. } else if (ext4_xattr_cmp(header, BHDR(bh)) == 0) {
  1519. *pce = ce;
  1520. return bh;
  1521. }
  1522. brelse(bh);
  1523. ce = mb_cache_entry_find_next(ce, inode->i_sb->s_bdev, hash);
  1524. }
  1525. return NULL;
  1526. }
  1527. #define NAME_HASH_SHIFT 5
  1528. #define VALUE_HASH_SHIFT 16
  1529. /*
  1530. * ext4_xattr_hash_entry()
  1531. *
  1532. * Compute the hash of an extended attribute.
  1533. */
  1534. static inline void ext4_xattr_hash_entry(struct ext4_xattr_header *header,
  1535. struct ext4_xattr_entry *entry)
  1536. {
  1537. __u32 hash = 0;
  1538. char *name = entry->e_name;
  1539. int n;
  1540. for (n = 0; n < entry->e_name_len; n++) {
  1541. hash = (hash << NAME_HASH_SHIFT) ^
  1542. (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^
  1543. *name++;
  1544. }
  1545. if (entry->e_value_block == 0 && entry->e_value_size != 0) {
  1546. __le32 *value = (__le32 *)((char *)header +
  1547. le16_to_cpu(entry->e_value_offs));
  1548. for (n = (le32_to_cpu(entry->e_value_size) +
  1549. EXT4_XATTR_ROUND) >> EXT4_XATTR_PAD_BITS; n; n--) {
  1550. hash = (hash << VALUE_HASH_SHIFT) ^
  1551. (hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^
  1552. le32_to_cpu(*value++);
  1553. }
  1554. }
  1555. entry->e_hash = cpu_to_le32(hash);
  1556. }
  1557. #undef NAME_HASH_SHIFT
  1558. #undef VALUE_HASH_SHIFT
  1559. #define BLOCK_HASH_SHIFT 16
  1560. /*
  1561. * ext4_xattr_rehash()
  1562. *
  1563. * Re-compute the extended attribute hash value after an entry has changed.
  1564. */
  1565. static void ext4_xattr_rehash(struct ext4_xattr_header *header,
  1566. struct ext4_xattr_entry *entry)
  1567. {
  1568. struct ext4_xattr_entry *here;
  1569. __u32 hash = 0;
  1570. ext4_xattr_hash_entry(header, entry);
  1571. here = ENTRY(header+1);
  1572. while (!IS_LAST_ENTRY(here)) {
  1573. if (!here->e_hash) {
  1574. /* Block is not shared if an entry's hash value == 0 */
  1575. hash = 0;
  1576. break;
  1577. }
  1578. hash = (hash << BLOCK_HASH_SHIFT) ^
  1579. (hash >> (8*sizeof(hash) - BLOCK_HASH_SHIFT)) ^
  1580. le32_to_cpu(here->e_hash);
  1581. here = EXT4_XATTR_NEXT(here);
  1582. }
  1583. header->h_hash = cpu_to_le32(hash);
  1584. }
  1585. #undef BLOCK_HASH_SHIFT
  1586. #define HASH_BUCKET_BITS 10
  1587. struct mb_cache *
  1588. ext4_xattr_create_cache(char *name)
  1589. {
  1590. return mb_cache_create(name, HASH_BUCKET_BITS);
  1591. }
  1592. void ext4_xattr_destroy_cache(struct mb_cache *cache)
  1593. {
  1594. if (cache)
  1595. mb_cache_destroy(cache);
  1596. }