xattr.c 48 KB

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