xattr.c 48 KB

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