xattr.c 47 KB

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