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. }
  762. ext4_xattr_block_csum_set(inode, bs->bh);
  763. unlock_buffer(bs->bh);
  764. if (error == -EFSCORRUPTED)
  765. goto bad_block;
  766. if (!error)
  767. error = ext4_handle_dirty_metadata(handle,
  768. inode,
  769. bs->bh);
  770. if (error)
  771. goto cleanup;
  772. goto inserted;
  773. } else {
  774. int offset = (char *)s->here - bs->bh->b_data;
  775. unlock_buffer(bs->bh);
  776. ea_bdebug(bs->bh, "cloning");
  777. s->base = kmalloc(bs->bh->b_size, GFP_NOFS);
  778. error = -ENOMEM;
  779. if (s->base == NULL)
  780. goto cleanup;
  781. memcpy(s->base, BHDR(bs->bh), bs->bh->b_size);
  782. s->first = ENTRY(header(s->base)+1);
  783. header(s->base)->h_refcount = cpu_to_le32(1);
  784. s->here = ENTRY(s->base + offset);
  785. s->end = s->base + bs->bh->b_size;
  786. }
  787. } else {
  788. /* Allocate a buffer where we construct the new block. */
  789. s->base = kzalloc(sb->s_blocksize, GFP_NOFS);
  790. /* assert(header == s->base) */
  791. error = -ENOMEM;
  792. if (s->base == NULL)
  793. goto cleanup;
  794. header(s->base)->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
  795. header(s->base)->h_blocks = cpu_to_le32(1);
  796. header(s->base)->h_refcount = cpu_to_le32(1);
  797. s->first = ENTRY(header(s->base)+1);
  798. s->here = ENTRY(header(s->base)+1);
  799. s->end = s->base + sb->s_blocksize;
  800. }
  801. error = ext4_xattr_set_entry(i, s);
  802. if (error == -EFSCORRUPTED)
  803. goto bad_block;
  804. if (error)
  805. goto cleanup;
  806. if (!IS_LAST_ENTRY(s->first))
  807. ext4_xattr_rehash(header(s->base), s->here);
  808. inserted:
  809. if (!IS_LAST_ENTRY(s->first)) {
  810. new_bh = ext4_xattr_cache_find(inode, header(s->base), &ce);
  811. if (new_bh) {
  812. /* We found an identical block in the cache. */
  813. if (new_bh == bs->bh)
  814. ea_bdebug(new_bh, "keeping");
  815. else {
  816. u32 ref;
  817. /* The old block is released after updating
  818. the inode. */
  819. error = dquot_alloc_block(inode,
  820. EXT4_C2B(EXT4_SB(sb), 1));
  821. if (error)
  822. goto cleanup;
  823. BUFFER_TRACE(new_bh, "get_write_access");
  824. error = ext4_journal_get_write_access(handle,
  825. new_bh);
  826. if (error)
  827. goto cleanup_dquot;
  828. lock_buffer(new_bh);
  829. /*
  830. * We have to be careful about races with
  831. * freeing, rehashing or adding references to
  832. * xattr block. Once we hold buffer lock xattr
  833. * block's state is stable so we can check
  834. * whether the block got freed / rehashed or
  835. * not. Since we unhash mbcache entry under
  836. * buffer lock when freeing / rehashing xattr
  837. * block, checking whether entry is still
  838. * hashed is reliable. Same rules hold for
  839. * e_reusable handling.
  840. */
  841. if (hlist_bl_unhashed(&ce->e_hash_list) ||
  842. !ce->e_reusable) {
  843. /*
  844. * Undo everything and check mbcache
  845. * again.
  846. */
  847. unlock_buffer(new_bh);
  848. dquot_free_block(inode,
  849. EXT4_C2B(EXT4_SB(sb),
  850. 1));
  851. brelse(new_bh);
  852. mb_cache_entry_put(ext4_mb_cache, ce);
  853. ce = NULL;
  854. new_bh = NULL;
  855. goto inserted;
  856. }
  857. ref = le32_to_cpu(BHDR(new_bh)->h_refcount) + 1;
  858. BHDR(new_bh)->h_refcount = cpu_to_le32(ref);
  859. if (ref >= EXT4_XATTR_REFCOUNT_MAX)
  860. ce->e_reusable = 0;
  861. ea_bdebug(new_bh, "reusing; refcount now=%d",
  862. ref);
  863. ext4_xattr_block_csum_set(inode, new_bh);
  864. unlock_buffer(new_bh);
  865. error = ext4_handle_dirty_metadata(handle,
  866. inode,
  867. new_bh);
  868. if (error)
  869. goto cleanup_dquot;
  870. }
  871. mb_cache_entry_touch(ext4_mb_cache, ce);
  872. mb_cache_entry_put(ext4_mb_cache, ce);
  873. ce = NULL;
  874. } else if (bs->bh && s->base == bs->bh->b_data) {
  875. /* We were modifying this block in-place. */
  876. ea_bdebug(bs->bh, "keeping this block");
  877. ext4_xattr_cache_insert(ext4_mb_cache, bs->bh);
  878. new_bh = bs->bh;
  879. get_bh(new_bh);
  880. } else {
  881. /* We need to allocate a new block */
  882. ext4_fsblk_t goal, block;
  883. goal = ext4_group_first_block_no(sb,
  884. EXT4_I(inode)->i_block_group);
  885. /* non-extent files can't have physical blocks past 2^32 */
  886. if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
  887. goal = goal & EXT4_MAX_BLOCK_FILE_PHYS;
  888. block = ext4_new_meta_blocks(handle, inode, goal, 0,
  889. NULL, &error);
  890. if (error)
  891. goto cleanup;
  892. if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
  893. BUG_ON(block > EXT4_MAX_BLOCK_FILE_PHYS);
  894. ea_idebug(inode, "creating block %llu",
  895. (unsigned long long)block);
  896. new_bh = sb_getblk(sb, block);
  897. if (unlikely(!new_bh)) {
  898. error = -ENOMEM;
  899. getblk_failed:
  900. ext4_free_blocks(handle, inode, NULL, block, 1,
  901. EXT4_FREE_BLOCKS_METADATA);
  902. goto cleanup;
  903. }
  904. lock_buffer(new_bh);
  905. error = ext4_journal_get_create_access(handle, new_bh);
  906. if (error) {
  907. unlock_buffer(new_bh);
  908. error = -EIO;
  909. goto getblk_failed;
  910. }
  911. memcpy(new_bh->b_data, s->base, new_bh->b_size);
  912. ext4_xattr_block_csum_set(inode, new_bh);
  913. set_buffer_uptodate(new_bh);
  914. unlock_buffer(new_bh);
  915. ext4_xattr_cache_insert(ext4_mb_cache, new_bh);
  916. error = ext4_handle_dirty_metadata(handle, inode,
  917. new_bh);
  918. if (error)
  919. goto cleanup;
  920. }
  921. }
  922. /* Update the inode. */
  923. EXT4_I(inode)->i_file_acl = new_bh ? new_bh->b_blocknr : 0;
  924. /* Drop the previous xattr block. */
  925. if (bs->bh && bs->bh != new_bh)
  926. ext4_xattr_release_block(handle, inode, bs->bh);
  927. error = 0;
  928. cleanup:
  929. if (ce)
  930. mb_cache_entry_put(ext4_mb_cache, ce);
  931. brelse(new_bh);
  932. if (!(bs->bh && s->base == bs->bh->b_data))
  933. kfree(s->base);
  934. return error;
  935. cleanup_dquot:
  936. dquot_free_block(inode, EXT4_C2B(EXT4_SB(sb), 1));
  937. goto cleanup;
  938. bad_block:
  939. EXT4_ERROR_INODE(inode, "bad block %llu",
  940. EXT4_I(inode)->i_file_acl);
  941. goto cleanup;
  942. #undef header
  943. }
  944. int ext4_xattr_ibody_find(struct inode *inode, struct ext4_xattr_info *i,
  945. struct ext4_xattr_ibody_find *is)
  946. {
  947. struct ext4_xattr_ibody_header *header;
  948. struct ext4_inode *raw_inode;
  949. int error;
  950. if (EXT4_I(inode)->i_extra_isize == 0)
  951. return 0;
  952. raw_inode = ext4_raw_inode(&is->iloc);
  953. header = IHDR(inode, raw_inode);
  954. is->s.base = is->s.first = IFIRST(header);
  955. is->s.here = is->s.first;
  956. is->s.end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
  957. if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
  958. error = xattr_check_inode(inode, header, is->s.end);
  959. if (error)
  960. return error;
  961. /* Find the named attribute. */
  962. error = ext4_xattr_find_entry(&is->s.here, i->name_index,
  963. i->name, is->s.end -
  964. (void *)is->s.base, 0);
  965. if (error && error != -ENODATA)
  966. return error;
  967. is->s.not_found = error;
  968. }
  969. return 0;
  970. }
  971. int ext4_xattr_ibody_inline_set(handle_t *handle, struct inode *inode,
  972. struct ext4_xattr_info *i,
  973. struct ext4_xattr_ibody_find *is)
  974. {
  975. struct ext4_xattr_ibody_header *header;
  976. struct ext4_xattr_search *s = &is->s;
  977. int error;
  978. if (EXT4_I(inode)->i_extra_isize == 0)
  979. return -ENOSPC;
  980. error = ext4_xattr_set_entry(i, s);
  981. if (error) {
  982. if (error == -ENOSPC &&
  983. ext4_has_inline_data(inode)) {
  984. error = ext4_try_to_evict_inline_data(handle, inode,
  985. EXT4_XATTR_LEN(strlen(i->name) +
  986. EXT4_XATTR_SIZE(i->value_len)));
  987. if (error)
  988. return error;
  989. error = ext4_xattr_ibody_find(inode, i, is);
  990. if (error)
  991. return error;
  992. error = ext4_xattr_set_entry(i, s);
  993. }
  994. if (error)
  995. return error;
  996. }
  997. header = IHDR(inode, ext4_raw_inode(&is->iloc));
  998. if (!IS_LAST_ENTRY(s->first)) {
  999. header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
  1000. ext4_set_inode_state(inode, EXT4_STATE_XATTR);
  1001. } else {
  1002. header->h_magic = cpu_to_le32(0);
  1003. ext4_clear_inode_state(inode, EXT4_STATE_XATTR);
  1004. }
  1005. return 0;
  1006. }
  1007. static int ext4_xattr_ibody_set(handle_t *handle, struct inode *inode,
  1008. struct ext4_xattr_info *i,
  1009. struct ext4_xattr_ibody_find *is)
  1010. {
  1011. struct ext4_xattr_ibody_header *header;
  1012. struct ext4_xattr_search *s = &is->s;
  1013. int error;
  1014. if (EXT4_I(inode)->i_extra_isize == 0)
  1015. return -ENOSPC;
  1016. error = ext4_xattr_set_entry(i, s);
  1017. if (error)
  1018. return error;
  1019. header = IHDR(inode, ext4_raw_inode(&is->iloc));
  1020. if (!IS_LAST_ENTRY(s->first)) {
  1021. header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
  1022. ext4_set_inode_state(inode, EXT4_STATE_XATTR);
  1023. } else {
  1024. header->h_magic = cpu_to_le32(0);
  1025. ext4_clear_inode_state(inode, EXT4_STATE_XATTR);
  1026. }
  1027. return 0;
  1028. }
  1029. static int ext4_xattr_value_same(struct ext4_xattr_search *s,
  1030. struct ext4_xattr_info *i)
  1031. {
  1032. void *value;
  1033. if (le32_to_cpu(s->here->e_value_size) != i->value_len)
  1034. return 0;
  1035. value = ((void *)s->base) + le16_to_cpu(s->here->e_value_offs);
  1036. return !memcmp(value, i->value, i->value_len);
  1037. }
  1038. /*
  1039. * ext4_xattr_set_handle()
  1040. *
  1041. * Create, replace or remove an extended attribute for this inode. Value
  1042. * is NULL to remove an existing extended attribute, and non-NULL to
  1043. * either replace an existing extended attribute, or create a new extended
  1044. * attribute. The flags XATTR_REPLACE and XATTR_CREATE
  1045. * specify that an extended attribute must exist and must not exist
  1046. * previous to the call, respectively.
  1047. *
  1048. * Returns 0, or a negative error number on failure.
  1049. */
  1050. int
  1051. ext4_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index,
  1052. const char *name, const void *value, size_t value_len,
  1053. int flags)
  1054. {
  1055. struct ext4_xattr_info i = {
  1056. .name_index = name_index,
  1057. .name = name,
  1058. .value = value,
  1059. .value_len = value_len,
  1060. };
  1061. struct ext4_xattr_ibody_find is = {
  1062. .s = { .not_found = -ENODATA, },
  1063. };
  1064. struct ext4_xattr_block_find bs = {
  1065. .s = { .not_found = -ENODATA, },
  1066. };
  1067. int no_expand;
  1068. int error;
  1069. if (!name)
  1070. return -EINVAL;
  1071. if (strlen(name) > 255)
  1072. return -ERANGE;
  1073. ext4_write_lock_xattr(inode, &no_expand);
  1074. error = ext4_reserve_inode_write(handle, inode, &is.iloc);
  1075. if (error)
  1076. goto cleanup;
  1077. if (ext4_test_inode_state(inode, EXT4_STATE_NEW)) {
  1078. struct ext4_inode *raw_inode = ext4_raw_inode(&is.iloc);
  1079. memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size);
  1080. ext4_clear_inode_state(inode, EXT4_STATE_NEW);
  1081. }
  1082. error = ext4_xattr_ibody_find(inode, &i, &is);
  1083. if (error)
  1084. goto cleanup;
  1085. if (is.s.not_found)
  1086. error = ext4_xattr_block_find(inode, &i, &bs);
  1087. if (error)
  1088. goto cleanup;
  1089. if (is.s.not_found && bs.s.not_found) {
  1090. error = -ENODATA;
  1091. if (flags & XATTR_REPLACE)
  1092. goto cleanup;
  1093. error = 0;
  1094. if (!value)
  1095. goto cleanup;
  1096. } else {
  1097. error = -EEXIST;
  1098. if (flags & XATTR_CREATE)
  1099. goto cleanup;
  1100. }
  1101. if (!value) {
  1102. if (!is.s.not_found)
  1103. error = ext4_xattr_ibody_set(handle, inode, &i, &is);
  1104. else if (!bs.s.not_found)
  1105. error = ext4_xattr_block_set(handle, inode, &i, &bs);
  1106. } else {
  1107. error = 0;
  1108. /* Xattr value did not change? Save us some work and bail out */
  1109. if (!is.s.not_found && ext4_xattr_value_same(&is.s, &i))
  1110. goto cleanup;
  1111. if (!bs.s.not_found && ext4_xattr_value_same(&bs.s, &i))
  1112. goto cleanup;
  1113. error = ext4_xattr_ibody_set(handle, inode, &i, &is);
  1114. if (!error && !bs.s.not_found) {
  1115. i.value = NULL;
  1116. error = ext4_xattr_block_set(handle, inode, &i, &bs);
  1117. } else if (error == -ENOSPC) {
  1118. if (EXT4_I(inode)->i_file_acl && !bs.s.base) {
  1119. error = ext4_xattr_block_find(inode, &i, &bs);
  1120. if (error)
  1121. goto cleanup;
  1122. }
  1123. error = ext4_xattr_block_set(handle, inode, &i, &bs);
  1124. if (error)
  1125. goto cleanup;
  1126. if (!is.s.not_found) {
  1127. i.value = NULL;
  1128. error = ext4_xattr_ibody_set(handle, inode, &i,
  1129. &is);
  1130. }
  1131. }
  1132. }
  1133. if (!error) {
  1134. ext4_xattr_update_super_block(handle, inode->i_sb);
  1135. inode->i_ctime = ext4_current_time(inode);
  1136. if (!value)
  1137. no_expand = 0;
  1138. error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
  1139. /*
  1140. * The bh is consumed by ext4_mark_iloc_dirty, even with
  1141. * error != 0.
  1142. */
  1143. is.iloc.bh = NULL;
  1144. if (IS_SYNC(inode))
  1145. ext4_handle_sync(handle);
  1146. }
  1147. cleanup:
  1148. brelse(is.iloc.bh);
  1149. brelse(bs.bh);
  1150. ext4_write_unlock_xattr(inode, &no_expand);
  1151. return error;
  1152. }
  1153. /*
  1154. * ext4_xattr_set()
  1155. *
  1156. * Like ext4_xattr_set_handle, but start from an inode. This extended
  1157. * attribute modification is a filesystem transaction by itself.
  1158. *
  1159. * Returns 0, or a negative error number on failure.
  1160. */
  1161. int
  1162. ext4_xattr_set(struct inode *inode, int name_index, const char *name,
  1163. const void *value, size_t value_len, int flags)
  1164. {
  1165. handle_t *handle;
  1166. int error, retries = 0;
  1167. int credits = ext4_jbd2_credits_xattr(inode);
  1168. retry:
  1169. handle = ext4_journal_start(inode, EXT4_HT_XATTR, credits);
  1170. if (IS_ERR(handle)) {
  1171. error = PTR_ERR(handle);
  1172. } else {
  1173. int error2;
  1174. error = ext4_xattr_set_handle(handle, inode, name_index, name,
  1175. value, value_len, flags);
  1176. error2 = ext4_journal_stop(handle);
  1177. if (error == -ENOSPC &&
  1178. ext4_should_retry_alloc(inode->i_sb, &retries))
  1179. goto retry;
  1180. if (error == 0)
  1181. error = error2;
  1182. }
  1183. return error;
  1184. }
  1185. /*
  1186. * Shift the EA entries in the inode to create space for the increased
  1187. * i_extra_isize.
  1188. */
  1189. static void ext4_xattr_shift_entries(struct ext4_xattr_entry *entry,
  1190. int value_offs_shift, void *to,
  1191. void *from, size_t n)
  1192. {
  1193. struct ext4_xattr_entry *last = entry;
  1194. int new_offs;
  1195. /* We always shift xattr headers further thus offsets get lower */
  1196. BUG_ON(value_offs_shift > 0);
  1197. /* Adjust the value offsets of the entries */
  1198. for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
  1199. if (last->e_value_size) {
  1200. new_offs = le16_to_cpu(last->e_value_offs) +
  1201. value_offs_shift;
  1202. last->e_value_offs = cpu_to_le16(new_offs);
  1203. }
  1204. }
  1205. /* Shift the entries by n bytes */
  1206. memmove(to, from, n);
  1207. }
  1208. /*
  1209. * Move xattr pointed to by 'entry' from inode into external xattr block
  1210. */
  1211. static int ext4_xattr_move_to_block(handle_t *handle, struct inode *inode,
  1212. struct ext4_inode *raw_inode,
  1213. struct ext4_xattr_entry *entry)
  1214. {
  1215. struct ext4_xattr_ibody_find *is = NULL;
  1216. struct ext4_xattr_block_find *bs = NULL;
  1217. char *buffer = NULL, *b_entry_name = NULL;
  1218. size_t value_offs, value_size;
  1219. struct ext4_xattr_info i = {
  1220. .value = NULL,
  1221. .value_len = 0,
  1222. .name_index = entry->e_name_index,
  1223. };
  1224. struct ext4_xattr_ibody_header *header = IHDR(inode, raw_inode);
  1225. int error;
  1226. value_offs = le16_to_cpu(entry->e_value_offs);
  1227. value_size = le32_to_cpu(entry->e_value_size);
  1228. is = kzalloc(sizeof(struct ext4_xattr_ibody_find), GFP_NOFS);
  1229. bs = kzalloc(sizeof(struct ext4_xattr_block_find), GFP_NOFS);
  1230. buffer = kmalloc(value_size, GFP_NOFS);
  1231. b_entry_name = kmalloc(entry->e_name_len + 1, GFP_NOFS);
  1232. if (!is || !bs || !buffer || !b_entry_name) {
  1233. error = -ENOMEM;
  1234. goto out;
  1235. }
  1236. is->s.not_found = -ENODATA;
  1237. bs->s.not_found = -ENODATA;
  1238. is->iloc.bh = NULL;
  1239. bs->bh = NULL;
  1240. /* Save the entry name and the entry value */
  1241. memcpy(buffer, (void *)IFIRST(header) + value_offs, value_size);
  1242. memcpy(b_entry_name, entry->e_name, entry->e_name_len);
  1243. b_entry_name[entry->e_name_len] = '\0';
  1244. i.name = b_entry_name;
  1245. error = ext4_get_inode_loc(inode, &is->iloc);
  1246. if (error)
  1247. goto out;
  1248. error = ext4_xattr_ibody_find(inode, &i, is);
  1249. if (error)
  1250. goto out;
  1251. /* Remove the chosen entry from the inode */
  1252. error = ext4_xattr_ibody_set(handle, inode, &i, is);
  1253. if (error)
  1254. goto out;
  1255. i.name = b_entry_name;
  1256. i.value = buffer;
  1257. i.value_len = value_size;
  1258. error = ext4_xattr_block_find(inode, &i, bs);
  1259. if (error)
  1260. goto out;
  1261. /* Add entry which was removed from the inode into the block */
  1262. error = ext4_xattr_block_set(handle, inode, &i, bs);
  1263. if (error)
  1264. goto out;
  1265. error = 0;
  1266. out:
  1267. kfree(b_entry_name);
  1268. kfree(buffer);
  1269. if (is)
  1270. brelse(is->iloc.bh);
  1271. kfree(is);
  1272. kfree(bs);
  1273. return error;
  1274. }
  1275. static int ext4_xattr_make_inode_space(handle_t *handle, struct inode *inode,
  1276. struct ext4_inode *raw_inode,
  1277. int isize_diff, size_t ifree,
  1278. size_t bfree, int *total_ino)
  1279. {
  1280. struct ext4_xattr_ibody_header *header = IHDR(inode, raw_inode);
  1281. struct ext4_xattr_entry *small_entry;
  1282. struct ext4_xattr_entry *entry;
  1283. struct ext4_xattr_entry *last;
  1284. unsigned int entry_size; /* EA entry size */
  1285. unsigned int total_size; /* EA entry size + value size */
  1286. unsigned int min_total_size;
  1287. int error;
  1288. while (isize_diff > ifree) {
  1289. entry = NULL;
  1290. small_entry = NULL;
  1291. min_total_size = ~0U;
  1292. last = IFIRST(header);
  1293. /* Find the entry best suited to be pushed into EA block */
  1294. for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
  1295. total_size =
  1296. EXT4_XATTR_SIZE(le32_to_cpu(last->e_value_size)) +
  1297. EXT4_XATTR_LEN(last->e_name_len);
  1298. if (total_size <= bfree &&
  1299. total_size < min_total_size) {
  1300. if (total_size + ifree < isize_diff) {
  1301. small_entry = last;
  1302. } else {
  1303. entry = last;
  1304. min_total_size = total_size;
  1305. }
  1306. }
  1307. }
  1308. if (entry == NULL) {
  1309. if (small_entry == NULL)
  1310. return -ENOSPC;
  1311. entry = small_entry;
  1312. }
  1313. entry_size = EXT4_XATTR_LEN(entry->e_name_len);
  1314. total_size = entry_size +
  1315. EXT4_XATTR_SIZE(le32_to_cpu(entry->e_value_size));
  1316. error = ext4_xattr_move_to_block(handle, inode, raw_inode,
  1317. entry);
  1318. if (error)
  1319. return error;
  1320. *total_ino -= entry_size;
  1321. ifree += total_size;
  1322. bfree -= total_size;
  1323. }
  1324. return 0;
  1325. }
  1326. /*
  1327. * Expand an inode by new_extra_isize bytes when EAs are present.
  1328. * Returns 0 on success or negative error number on failure.
  1329. */
  1330. int ext4_expand_extra_isize_ea(struct inode *inode, int new_extra_isize,
  1331. struct ext4_inode *raw_inode, handle_t *handle)
  1332. {
  1333. struct ext4_xattr_ibody_header *header;
  1334. struct buffer_head *bh = NULL;
  1335. size_t min_offs;
  1336. size_t ifree, bfree;
  1337. int total_ino;
  1338. void *base, *end;
  1339. int error = 0, tried_min_extra_isize = 0;
  1340. int s_min_extra_isize = le16_to_cpu(EXT4_SB(inode->i_sb)->s_es->s_min_extra_isize);
  1341. int isize_diff; /* How much do we need to grow i_extra_isize */
  1342. int no_expand;
  1343. if (ext4_write_trylock_xattr(inode, &no_expand) == 0)
  1344. return 0;
  1345. retry:
  1346. isize_diff = new_extra_isize - EXT4_I(inode)->i_extra_isize;
  1347. if (EXT4_I(inode)->i_extra_isize >= new_extra_isize)
  1348. goto out;
  1349. header = IHDR(inode, raw_inode);
  1350. /*
  1351. * Check if enough free space is available in the inode to shift the
  1352. * entries ahead by new_extra_isize.
  1353. */
  1354. base = IFIRST(header);
  1355. end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
  1356. min_offs = end - base;
  1357. total_ino = sizeof(struct ext4_xattr_ibody_header);
  1358. error = xattr_check_inode(inode, header, end);
  1359. if (error)
  1360. goto cleanup;
  1361. ifree = ext4_xattr_free_space(base, &min_offs, base, &total_ino);
  1362. if (ifree >= isize_diff)
  1363. goto shift;
  1364. /*
  1365. * Enough free space isn't available in the inode, check if
  1366. * EA block can hold new_extra_isize bytes.
  1367. */
  1368. if (EXT4_I(inode)->i_file_acl) {
  1369. bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
  1370. error = -EIO;
  1371. if (!bh)
  1372. goto cleanup;
  1373. if (ext4_xattr_check_block(inode, bh)) {
  1374. EXT4_ERROR_INODE(inode, "bad block %llu",
  1375. EXT4_I(inode)->i_file_acl);
  1376. error = -EFSCORRUPTED;
  1377. goto cleanup;
  1378. }
  1379. base = BHDR(bh);
  1380. end = bh->b_data + bh->b_size;
  1381. min_offs = end - base;
  1382. bfree = ext4_xattr_free_space(BFIRST(bh), &min_offs, base,
  1383. NULL);
  1384. if (bfree + ifree < isize_diff) {
  1385. if (!tried_min_extra_isize && s_min_extra_isize) {
  1386. tried_min_extra_isize++;
  1387. new_extra_isize = s_min_extra_isize;
  1388. brelse(bh);
  1389. goto retry;
  1390. }
  1391. error = -ENOSPC;
  1392. goto cleanup;
  1393. }
  1394. } else {
  1395. bfree = inode->i_sb->s_blocksize;
  1396. }
  1397. error = ext4_xattr_make_inode_space(handle, inode, raw_inode,
  1398. isize_diff, ifree, bfree,
  1399. &total_ino);
  1400. if (error) {
  1401. if (error == -ENOSPC && !tried_min_extra_isize &&
  1402. s_min_extra_isize) {
  1403. tried_min_extra_isize++;
  1404. new_extra_isize = s_min_extra_isize;
  1405. brelse(bh);
  1406. goto retry;
  1407. }
  1408. goto cleanup;
  1409. }
  1410. shift:
  1411. /* Adjust the offsets and shift the remaining entries ahead */
  1412. ext4_xattr_shift_entries(IFIRST(header), EXT4_I(inode)->i_extra_isize
  1413. - new_extra_isize, (void *)raw_inode +
  1414. EXT4_GOOD_OLD_INODE_SIZE + new_extra_isize,
  1415. (void *)header, total_ino);
  1416. EXT4_I(inode)->i_extra_isize = new_extra_isize;
  1417. brelse(bh);
  1418. out:
  1419. ext4_write_unlock_xattr(inode, &no_expand);
  1420. return 0;
  1421. cleanup:
  1422. brelse(bh);
  1423. /*
  1424. * Inode size expansion failed; don't try again
  1425. */
  1426. no_expand = 1;
  1427. ext4_write_unlock_xattr(inode, &no_expand);
  1428. return error;
  1429. }
  1430. /*
  1431. * ext4_xattr_delete_inode()
  1432. *
  1433. * Free extended attribute resources associated with this inode. This
  1434. * is called immediately before an inode is freed. We have exclusive
  1435. * access to the inode.
  1436. */
  1437. void
  1438. ext4_xattr_delete_inode(handle_t *handle, struct inode *inode)
  1439. {
  1440. struct buffer_head *bh = NULL;
  1441. if (!EXT4_I(inode)->i_file_acl)
  1442. goto cleanup;
  1443. bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
  1444. if (!bh) {
  1445. EXT4_ERROR_INODE(inode, "block %llu read error",
  1446. EXT4_I(inode)->i_file_acl);
  1447. goto cleanup;
  1448. }
  1449. if (BHDR(bh)->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC) ||
  1450. BHDR(bh)->h_blocks != cpu_to_le32(1)) {
  1451. EXT4_ERROR_INODE(inode, "bad block %llu",
  1452. EXT4_I(inode)->i_file_acl);
  1453. goto cleanup;
  1454. }
  1455. ext4_xattr_release_block(handle, inode, bh);
  1456. EXT4_I(inode)->i_file_acl = 0;
  1457. cleanup:
  1458. brelse(bh);
  1459. }
  1460. /*
  1461. * ext4_xattr_cache_insert()
  1462. *
  1463. * Create a new entry in the extended attribute cache, and insert
  1464. * it unless such an entry is already in the cache.
  1465. *
  1466. * Returns 0, or a negative error number on failure.
  1467. */
  1468. static void
  1469. ext4_xattr_cache_insert(struct mb_cache *ext4_mb_cache, struct buffer_head *bh)
  1470. {
  1471. struct ext4_xattr_header *header = BHDR(bh);
  1472. __u32 hash = le32_to_cpu(header->h_hash);
  1473. int reusable = le32_to_cpu(header->h_refcount) <
  1474. EXT4_XATTR_REFCOUNT_MAX;
  1475. int error;
  1476. error = mb_cache_entry_create(ext4_mb_cache, GFP_NOFS, hash,
  1477. bh->b_blocknr, reusable);
  1478. if (error) {
  1479. if (error == -EBUSY)
  1480. ea_bdebug(bh, "already in cache");
  1481. } else
  1482. ea_bdebug(bh, "inserting [%x]", (int)hash);
  1483. }
  1484. /*
  1485. * ext4_xattr_cmp()
  1486. *
  1487. * Compare two extended attribute blocks for equality.
  1488. *
  1489. * Returns 0 if the blocks are equal, 1 if they differ, and
  1490. * a negative error number on errors.
  1491. */
  1492. static int
  1493. ext4_xattr_cmp(struct ext4_xattr_header *header1,
  1494. struct ext4_xattr_header *header2)
  1495. {
  1496. struct ext4_xattr_entry *entry1, *entry2;
  1497. entry1 = ENTRY(header1+1);
  1498. entry2 = ENTRY(header2+1);
  1499. while (!IS_LAST_ENTRY(entry1)) {
  1500. if (IS_LAST_ENTRY(entry2))
  1501. return 1;
  1502. if (entry1->e_hash != entry2->e_hash ||
  1503. entry1->e_name_index != entry2->e_name_index ||
  1504. entry1->e_name_len != entry2->e_name_len ||
  1505. entry1->e_value_size != entry2->e_value_size ||
  1506. memcmp(entry1->e_name, entry2->e_name, entry1->e_name_len))
  1507. return 1;
  1508. if (entry1->e_value_block != 0 || entry2->e_value_block != 0)
  1509. return -EFSCORRUPTED;
  1510. if (memcmp((char *)header1 + le16_to_cpu(entry1->e_value_offs),
  1511. (char *)header2 + le16_to_cpu(entry2->e_value_offs),
  1512. le32_to_cpu(entry1->e_value_size)))
  1513. return 1;
  1514. entry1 = EXT4_XATTR_NEXT(entry1);
  1515. entry2 = EXT4_XATTR_NEXT(entry2);
  1516. }
  1517. if (!IS_LAST_ENTRY(entry2))
  1518. return 1;
  1519. return 0;
  1520. }
  1521. /*
  1522. * ext4_xattr_cache_find()
  1523. *
  1524. * Find an identical extended attribute block.
  1525. *
  1526. * Returns a pointer to the block found, or NULL if such a block was
  1527. * not found or an error occurred.
  1528. */
  1529. static struct buffer_head *
  1530. ext4_xattr_cache_find(struct inode *inode, struct ext4_xattr_header *header,
  1531. struct mb_cache_entry **pce)
  1532. {
  1533. __u32 hash = le32_to_cpu(header->h_hash);
  1534. struct mb_cache_entry *ce;
  1535. struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
  1536. if (!header->h_hash)
  1537. return NULL; /* never share */
  1538. ea_idebug(inode, "looking for cached blocks [%x]", (int)hash);
  1539. ce = mb_cache_entry_find_first(ext4_mb_cache, hash);
  1540. while (ce) {
  1541. struct buffer_head *bh;
  1542. bh = sb_bread(inode->i_sb, ce->e_block);
  1543. if (!bh) {
  1544. EXT4_ERROR_INODE(inode, "block %lu read error",
  1545. (unsigned long) ce->e_block);
  1546. } else if (ext4_xattr_cmp(header, BHDR(bh)) == 0) {
  1547. *pce = ce;
  1548. return bh;
  1549. }
  1550. brelse(bh);
  1551. ce = mb_cache_entry_find_next(ext4_mb_cache, ce);
  1552. }
  1553. return NULL;
  1554. }
  1555. #define NAME_HASH_SHIFT 5
  1556. #define VALUE_HASH_SHIFT 16
  1557. /*
  1558. * ext4_xattr_hash_entry()
  1559. *
  1560. * Compute the hash of an extended attribute.
  1561. */
  1562. static inline void ext4_xattr_hash_entry(struct ext4_xattr_header *header,
  1563. struct ext4_xattr_entry *entry)
  1564. {
  1565. __u32 hash = 0;
  1566. char *name = entry->e_name;
  1567. int n;
  1568. for (n = 0; n < entry->e_name_len; n++) {
  1569. hash = (hash << NAME_HASH_SHIFT) ^
  1570. (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^
  1571. *name++;
  1572. }
  1573. if (entry->e_value_size != 0) {
  1574. __le32 *value = (__le32 *)((char *)header +
  1575. le16_to_cpu(entry->e_value_offs));
  1576. for (n = (le32_to_cpu(entry->e_value_size) +
  1577. EXT4_XATTR_ROUND) >> EXT4_XATTR_PAD_BITS; n; n--) {
  1578. hash = (hash << VALUE_HASH_SHIFT) ^
  1579. (hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^
  1580. le32_to_cpu(*value++);
  1581. }
  1582. }
  1583. entry->e_hash = cpu_to_le32(hash);
  1584. }
  1585. #undef NAME_HASH_SHIFT
  1586. #undef VALUE_HASH_SHIFT
  1587. #define BLOCK_HASH_SHIFT 16
  1588. /*
  1589. * ext4_xattr_rehash()
  1590. *
  1591. * Re-compute the extended attribute hash value after an entry has changed.
  1592. */
  1593. static void ext4_xattr_rehash(struct ext4_xattr_header *header,
  1594. struct ext4_xattr_entry *entry)
  1595. {
  1596. struct ext4_xattr_entry *here;
  1597. __u32 hash = 0;
  1598. ext4_xattr_hash_entry(header, entry);
  1599. here = ENTRY(header+1);
  1600. while (!IS_LAST_ENTRY(here)) {
  1601. if (!here->e_hash) {
  1602. /* Block is not shared if an entry's hash value == 0 */
  1603. hash = 0;
  1604. break;
  1605. }
  1606. hash = (hash << BLOCK_HASH_SHIFT) ^
  1607. (hash >> (8*sizeof(hash) - BLOCK_HASH_SHIFT)) ^
  1608. le32_to_cpu(here->e_hash);
  1609. here = EXT4_XATTR_NEXT(here);
  1610. }
  1611. header->h_hash = cpu_to_le32(hash);
  1612. }
  1613. #undef BLOCK_HASH_SHIFT
  1614. #define HASH_BUCKET_BITS 10
  1615. struct mb_cache *
  1616. ext4_xattr_create_cache(void)
  1617. {
  1618. return mb_cache_create(HASH_BUCKET_BITS);
  1619. }
  1620. void ext4_xattr_destroy_cache(struct mb_cache *cache)
  1621. {
  1622. if (cache)
  1623. mb_cache_destroy(cache);
  1624. }