xattr.c 49 KB

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