xattr.c 82 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * linux/fs/ext4/xattr.c
  4. *
  5. * Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de>
  6. *
  7. * Fix by Harrison Xing <harrison@mountainviewdata.com>.
  8. * Ext4 code with a lot of help from Eric Jarman <ejarman@acm.org>.
  9. * Extended attributes for symlinks and special files added per
  10. * suggestion of Luka Renko <luka.renko@hermes.si>.
  11. * xattr consolidation Copyright (c) 2004 James Morris <jmorris@redhat.com>,
  12. * Red Hat Inc.
  13. * ea-in-inode support by Alex Tomas <alex@clusterfs.com> aka bzzz
  14. * and Andreas Gruenbacher <agruen@suse.de>.
  15. */
  16. /*
  17. * Extended attributes are stored directly in inodes (on file systems with
  18. * inodes bigger than 128 bytes) and on additional disk blocks. The i_file_acl
  19. * field contains the block number if an inode uses an additional block. All
  20. * attributes must fit in the inode and one additional block. Blocks that
  21. * contain the identical set of attributes may be shared among several inodes.
  22. * Identical blocks are detected by keeping a cache of blocks that have
  23. * recently been accessed.
  24. *
  25. * The attributes in inodes and on blocks have a different header; the entries
  26. * are stored in the same format:
  27. *
  28. * +------------------+
  29. * | header |
  30. * | entry 1 | |
  31. * | entry 2 | | growing downwards
  32. * | entry 3 | v
  33. * | four null bytes |
  34. * | . . . |
  35. * | value 1 | ^
  36. * | value 3 | | growing upwards
  37. * | value 2 | |
  38. * +------------------+
  39. *
  40. * The header is followed by multiple entry descriptors. In disk blocks, the
  41. * entry descriptors are kept sorted. In inodes, they are unsorted. The
  42. * attribute values are aligned to the end of the block in no specific order.
  43. *
  44. * Locking strategy
  45. * ----------------
  46. * EXT4_I(inode)->i_file_acl is protected by EXT4_I(inode)->xattr_sem.
  47. * EA blocks are only changed if they are exclusive to an inode, so
  48. * holding xattr_sem also means that nothing but the EA block's reference
  49. * count can change. Multiple writers to the same block are synchronized
  50. * by the buffer lock.
  51. */
  52. #include <linux/init.h>
  53. #include <linux/fs.h>
  54. #include <linux/slab.h>
  55. #include <linux/mbcache.h>
  56. #include <linux/quotaops.h>
  57. #include <linux/iversion.h>
  58. #include "ext4_jbd2.h"
  59. #include "ext4.h"
  60. #include "xattr.h"
  61. #include "acl.h"
  62. #ifdef EXT4_XATTR_DEBUG
  63. # define ea_idebug(inode, fmt, ...) \
  64. printk(KERN_DEBUG "inode %s:%lu: " fmt "\n", \
  65. inode->i_sb->s_id, inode->i_ino, ##__VA_ARGS__)
  66. # define ea_bdebug(bh, fmt, ...) \
  67. printk(KERN_DEBUG "block %pg:%lu: " fmt "\n", \
  68. bh->b_bdev, (unsigned long)bh->b_blocknr, ##__VA_ARGS__)
  69. #else
  70. # define ea_idebug(inode, fmt, ...) no_printk(fmt, ##__VA_ARGS__)
  71. # define ea_bdebug(bh, fmt, ...) no_printk(fmt, ##__VA_ARGS__)
  72. #endif
  73. static void ext4_xattr_block_cache_insert(struct mb_cache *,
  74. struct buffer_head *);
  75. static struct buffer_head *
  76. ext4_xattr_block_cache_find(struct inode *, struct ext4_xattr_header *,
  77. struct mb_cache_entry **);
  78. static __le32 ext4_xattr_hash_entry(char *name, size_t name_len, __le32 *value,
  79. size_t value_count);
  80. static void ext4_xattr_rehash(struct ext4_xattr_header *);
  81. static const struct xattr_handler * const ext4_xattr_handler_map[] = {
  82. [EXT4_XATTR_INDEX_USER] = &ext4_xattr_user_handler,
  83. #ifdef CONFIG_EXT4_FS_POSIX_ACL
  84. [EXT4_XATTR_INDEX_POSIX_ACL_ACCESS] = &posix_acl_access_xattr_handler,
  85. [EXT4_XATTR_INDEX_POSIX_ACL_DEFAULT] = &posix_acl_default_xattr_handler,
  86. #endif
  87. [EXT4_XATTR_INDEX_TRUSTED] = &ext4_xattr_trusted_handler,
  88. #ifdef CONFIG_EXT4_FS_SECURITY
  89. [EXT4_XATTR_INDEX_SECURITY] = &ext4_xattr_security_handler,
  90. #endif
  91. };
  92. const struct xattr_handler *ext4_xattr_handlers[] = {
  93. &ext4_xattr_user_handler,
  94. &ext4_xattr_trusted_handler,
  95. #ifdef CONFIG_EXT4_FS_POSIX_ACL
  96. &posix_acl_access_xattr_handler,
  97. &posix_acl_default_xattr_handler,
  98. #endif
  99. #ifdef CONFIG_EXT4_FS_SECURITY
  100. &ext4_xattr_security_handler,
  101. #endif
  102. NULL
  103. };
  104. #define EA_BLOCK_CACHE(inode) (((struct ext4_sb_info *) \
  105. inode->i_sb->s_fs_info)->s_ea_block_cache)
  106. #define EA_INODE_CACHE(inode) (((struct ext4_sb_info *) \
  107. inode->i_sb->s_fs_info)->s_ea_inode_cache)
  108. static int
  109. ext4_expand_inode_array(struct ext4_xattr_inode_array **ea_inode_array,
  110. struct inode *inode);
  111. #ifdef CONFIG_LOCKDEP
  112. void ext4_xattr_inode_set_class(struct inode *ea_inode)
  113. {
  114. lockdep_set_subclass(&ea_inode->i_rwsem, 1);
  115. }
  116. #endif
  117. static __le32 ext4_xattr_block_csum(struct inode *inode,
  118. sector_t block_nr,
  119. struct ext4_xattr_header *hdr)
  120. {
  121. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  122. __u32 csum;
  123. __le64 dsk_block_nr = cpu_to_le64(block_nr);
  124. __u32 dummy_csum = 0;
  125. int offset = offsetof(struct ext4_xattr_header, h_checksum);
  126. csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&dsk_block_nr,
  127. sizeof(dsk_block_nr));
  128. csum = ext4_chksum(sbi, csum, (__u8 *)hdr, offset);
  129. csum = ext4_chksum(sbi, csum, (__u8 *)&dummy_csum, sizeof(dummy_csum));
  130. offset += sizeof(dummy_csum);
  131. csum = ext4_chksum(sbi, csum, (__u8 *)hdr + offset,
  132. EXT4_BLOCK_SIZE(inode->i_sb) - offset);
  133. return cpu_to_le32(csum);
  134. }
  135. static int ext4_xattr_block_csum_verify(struct inode *inode,
  136. struct buffer_head *bh)
  137. {
  138. struct ext4_xattr_header *hdr = BHDR(bh);
  139. int ret = 1;
  140. if (ext4_has_metadata_csum(inode->i_sb)) {
  141. lock_buffer(bh);
  142. ret = (hdr->h_checksum == ext4_xattr_block_csum(inode,
  143. bh->b_blocknr, hdr));
  144. unlock_buffer(bh);
  145. }
  146. return ret;
  147. }
  148. static void ext4_xattr_block_csum_set(struct inode *inode,
  149. struct buffer_head *bh)
  150. {
  151. if (ext4_has_metadata_csum(inode->i_sb))
  152. BHDR(bh)->h_checksum = ext4_xattr_block_csum(inode,
  153. bh->b_blocknr, BHDR(bh));
  154. }
  155. static inline const struct xattr_handler *
  156. ext4_xattr_handler(int name_index)
  157. {
  158. const struct xattr_handler *handler = NULL;
  159. if (name_index > 0 && name_index < ARRAY_SIZE(ext4_xattr_handler_map))
  160. handler = ext4_xattr_handler_map[name_index];
  161. return handler;
  162. }
  163. static int
  164. ext4_xattr_check_entries(struct ext4_xattr_entry *entry, void *end,
  165. void *value_start)
  166. {
  167. struct ext4_xattr_entry *e = entry;
  168. /* Find the end of the names list */
  169. while (!IS_LAST_ENTRY(e)) {
  170. struct ext4_xattr_entry *next = EXT4_XATTR_NEXT(e);
  171. if ((void *)next >= end)
  172. return -EFSCORRUPTED;
  173. if (strnlen(e->e_name, e->e_name_len) != e->e_name_len)
  174. return -EFSCORRUPTED;
  175. e = next;
  176. }
  177. /* Check the values */
  178. while (!IS_LAST_ENTRY(entry)) {
  179. u32 size = le32_to_cpu(entry->e_value_size);
  180. if (size > EXT4_XATTR_SIZE_MAX)
  181. return -EFSCORRUPTED;
  182. if (size != 0 && entry->e_value_inum == 0) {
  183. u16 offs = le16_to_cpu(entry->e_value_offs);
  184. void *value;
  185. /*
  186. * The value cannot overlap the names, and the value
  187. * with padding cannot extend beyond 'end'. Check both
  188. * the padded and unpadded sizes, since the size may
  189. * overflow to 0 when adding padding.
  190. */
  191. if (offs > end - value_start)
  192. return -EFSCORRUPTED;
  193. value = value_start + offs;
  194. if (value < (void *)e + sizeof(u32) ||
  195. size > end - value ||
  196. EXT4_XATTR_SIZE(size) > end - value)
  197. return -EFSCORRUPTED;
  198. }
  199. entry = EXT4_XATTR_NEXT(entry);
  200. }
  201. return 0;
  202. }
  203. static inline int
  204. __ext4_xattr_check_block(struct inode *inode, struct buffer_head *bh,
  205. const char *function, unsigned int line)
  206. {
  207. int error = -EFSCORRUPTED;
  208. if (BHDR(bh)->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC) ||
  209. BHDR(bh)->h_blocks != cpu_to_le32(1))
  210. goto errout;
  211. if (buffer_verified(bh))
  212. return 0;
  213. error = -EFSBADCRC;
  214. if (!ext4_xattr_block_csum_verify(inode, bh))
  215. goto errout;
  216. error = ext4_xattr_check_entries(BFIRST(bh), bh->b_data + bh->b_size,
  217. bh->b_data);
  218. errout:
  219. if (error)
  220. __ext4_error_inode(inode, function, line, 0,
  221. "corrupted xattr block %llu",
  222. (unsigned long long) bh->b_blocknr);
  223. else
  224. set_buffer_verified(bh);
  225. return error;
  226. }
  227. #define ext4_xattr_check_block(inode, bh) \
  228. __ext4_xattr_check_block((inode), (bh), __func__, __LINE__)
  229. static int
  230. __xattr_check_inode(struct inode *inode, struct ext4_xattr_ibody_header *header,
  231. void *end, const char *function, unsigned int line)
  232. {
  233. int error = -EFSCORRUPTED;
  234. if (end - (void *)header < sizeof(*header) + sizeof(u32) ||
  235. (header->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC)))
  236. goto errout;
  237. error = ext4_xattr_check_entries(IFIRST(header), end, IFIRST(header));
  238. errout:
  239. if (error)
  240. __ext4_error_inode(inode, function, line, 0,
  241. "corrupted in-inode xattr");
  242. return error;
  243. }
  244. #define xattr_check_inode(inode, header, end) \
  245. __xattr_check_inode((inode), (header), (end), __func__, __LINE__)
  246. static int
  247. xattr_find_entry(struct inode *inode, struct ext4_xattr_entry **pentry,
  248. void *end, int name_index, const char *name, int sorted)
  249. {
  250. struct ext4_xattr_entry *entry, *next;
  251. size_t name_len;
  252. int cmp = 1;
  253. if (name == NULL)
  254. return -EINVAL;
  255. name_len = strlen(name);
  256. for (entry = *pentry; !IS_LAST_ENTRY(entry); entry = next) {
  257. next = EXT4_XATTR_NEXT(entry);
  258. if ((void *) next >= end) {
  259. EXT4_ERROR_INODE(inode, "corrupted xattr entries");
  260. return -EFSCORRUPTED;
  261. }
  262. cmp = name_index - entry->e_name_index;
  263. if (!cmp)
  264. cmp = name_len - entry->e_name_len;
  265. if (!cmp)
  266. cmp = memcmp(name, entry->e_name, name_len);
  267. if (cmp <= 0 && (sorted || cmp == 0))
  268. break;
  269. }
  270. *pentry = entry;
  271. return cmp ? -ENODATA : 0;
  272. }
  273. static u32
  274. ext4_xattr_inode_hash(struct ext4_sb_info *sbi, const void *buffer, size_t size)
  275. {
  276. return ext4_chksum(sbi, sbi->s_csum_seed, buffer, size);
  277. }
  278. static u64 ext4_xattr_inode_get_ref(struct inode *ea_inode)
  279. {
  280. return ((u64)ea_inode->i_ctime.tv_sec << 32) |
  281. (u32) inode_peek_iversion_raw(ea_inode);
  282. }
  283. static void ext4_xattr_inode_set_ref(struct inode *ea_inode, u64 ref_count)
  284. {
  285. ea_inode->i_ctime.tv_sec = (u32)(ref_count >> 32);
  286. inode_set_iversion_raw(ea_inode, ref_count & 0xffffffff);
  287. }
  288. static u32 ext4_xattr_inode_get_hash(struct inode *ea_inode)
  289. {
  290. return (u32)ea_inode->i_atime.tv_sec;
  291. }
  292. static void ext4_xattr_inode_set_hash(struct inode *ea_inode, u32 hash)
  293. {
  294. ea_inode->i_atime.tv_sec = hash;
  295. }
  296. /*
  297. * Read the EA value from an inode.
  298. */
  299. static int ext4_xattr_inode_read(struct inode *ea_inode, void *buf, size_t size)
  300. {
  301. int blocksize = 1 << ea_inode->i_blkbits;
  302. int bh_count = (size + blocksize - 1) >> ea_inode->i_blkbits;
  303. int tail_size = (size % blocksize) ?: blocksize;
  304. struct buffer_head *bhs_inline[8];
  305. struct buffer_head **bhs = bhs_inline;
  306. int i, ret;
  307. if (bh_count > ARRAY_SIZE(bhs_inline)) {
  308. bhs = kmalloc_array(bh_count, sizeof(*bhs), GFP_NOFS);
  309. if (!bhs)
  310. return -ENOMEM;
  311. }
  312. ret = ext4_bread_batch(ea_inode, 0 /* block */, bh_count,
  313. true /* wait */, bhs);
  314. if (ret)
  315. goto free_bhs;
  316. for (i = 0; i < bh_count; i++) {
  317. /* There shouldn't be any holes in ea_inode. */
  318. if (!bhs[i]) {
  319. ret = -EFSCORRUPTED;
  320. goto put_bhs;
  321. }
  322. memcpy((char *)buf + blocksize * i, bhs[i]->b_data,
  323. i < bh_count - 1 ? blocksize : tail_size);
  324. }
  325. ret = 0;
  326. put_bhs:
  327. for (i = 0; i < bh_count; i++)
  328. brelse(bhs[i]);
  329. free_bhs:
  330. if (bhs != bhs_inline)
  331. kfree(bhs);
  332. return ret;
  333. }
  334. #define EXT4_XATTR_INODE_GET_PARENT(inode) ((__u32)(inode)->i_mtime.tv_sec)
  335. static int ext4_xattr_inode_iget(struct inode *parent, unsigned long ea_ino,
  336. u32 ea_inode_hash, struct inode **ea_inode)
  337. {
  338. struct inode *inode;
  339. int err;
  340. inode = ext4_iget(parent->i_sb, ea_ino);
  341. if (IS_ERR(inode)) {
  342. err = PTR_ERR(inode);
  343. ext4_error(parent->i_sb,
  344. "error while reading EA inode %lu err=%d", ea_ino,
  345. err);
  346. return err;
  347. }
  348. if (is_bad_inode(inode)) {
  349. ext4_error(parent->i_sb,
  350. "error while reading EA inode %lu is_bad_inode",
  351. ea_ino);
  352. err = -EIO;
  353. goto error;
  354. }
  355. if (!(EXT4_I(inode)->i_flags & EXT4_EA_INODE_FL)) {
  356. ext4_error(parent->i_sb,
  357. "EA inode %lu does not have EXT4_EA_INODE_FL flag",
  358. ea_ino);
  359. err = -EINVAL;
  360. goto error;
  361. }
  362. ext4_xattr_inode_set_class(inode);
  363. /*
  364. * Check whether this is an old Lustre-style xattr inode. Lustre
  365. * implementation does not have hash validation, rather it has a
  366. * backpointer from ea_inode to the parent inode.
  367. */
  368. if (ea_inode_hash != ext4_xattr_inode_get_hash(inode) &&
  369. EXT4_XATTR_INODE_GET_PARENT(inode) == parent->i_ino &&
  370. inode->i_generation == parent->i_generation) {
  371. ext4_set_inode_state(inode, EXT4_STATE_LUSTRE_EA_INODE);
  372. ext4_xattr_inode_set_ref(inode, 1);
  373. } else {
  374. inode_lock(inode);
  375. inode->i_flags |= S_NOQUOTA;
  376. inode_unlock(inode);
  377. }
  378. *ea_inode = inode;
  379. return 0;
  380. error:
  381. iput(inode);
  382. return err;
  383. }
  384. static int
  385. ext4_xattr_inode_verify_hashes(struct inode *ea_inode,
  386. struct ext4_xattr_entry *entry, void *buffer,
  387. size_t size)
  388. {
  389. u32 hash;
  390. /* Verify stored hash matches calculated hash. */
  391. hash = ext4_xattr_inode_hash(EXT4_SB(ea_inode->i_sb), buffer, size);
  392. if (hash != ext4_xattr_inode_get_hash(ea_inode))
  393. return -EFSCORRUPTED;
  394. if (entry) {
  395. __le32 e_hash, tmp_data;
  396. /* Verify entry hash. */
  397. tmp_data = cpu_to_le32(hash);
  398. e_hash = ext4_xattr_hash_entry(entry->e_name, entry->e_name_len,
  399. &tmp_data, 1);
  400. if (e_hash != entry->e_hash)
  401. return -EFSCORRUPTED;
  402. }
  403. return 0;
  404. }
  405. /*
  406. * Read xattr value from the EA inode.
  407. */
  408. static int
  409. ext4_xattr_inode_get(struct inode *inode, struct ext4_xattr_entry *entry,
  410. void *buffer, size_t size)
  411. {
  412. struct mb_cache *ea_inode_cache = EA_INODE_CACHE(inode);
  413. struct inode *ea_inode;
  414. int err;
  415. err = ext4_xattr_inode_iget(inode, le32_to_cpu(entry->e_value_inum),
  416. le32_to_cpu(entry->e_hash), &ea_inode);
  417. if (err) {
  418. ea_inode = NULL;
  419. goto out;
  420. }
  421. if (i_size_read(ea_inode) != size) {
  422. ext4_warning_inode(ea_inode,
  423. "ea_inode file size=%llu entry size=%zu",
  424. i_size_read(ea_inode), size);
  425. err = -EFSCORRUPTED;
  426. goto out;
  427. }
  428. err = ext4_xattr_inode_read(ea_inode, buffer, size);
  429. if (err)
  430. goto out;
  431. if (!ext4_test_inode_state(ea_inode, EXT4_STATE_LUSTRE_EA_INODE)) {
  432. err = ext4_xattr_inode_verify_hashes(ea_inode, entry, buffer,
  433. size);
  434. if (err) {
  435. ext4_warning_inode(ea_inode,
  436. "EA inode hash validation failed");
  437. goto out;
  438. }
  439. if (ea_inode_cache)
  440. mb_cache_entry_create(ea_inode_cache, GFP_NOFS,
  441. ext4_xattr_inode_get_hash(ea_inode),
  442. ea_inode->i_ino, true /* reusable */);
  443. }
  444. out:
  445. iput(ea_inode);
  446. return err;
  447. }
  448. static int
  449. ext4_xattr_block_get(struct inode *inode, int name_index, const char *name,
  450. void *buffer, size_t buffer_size)
  451. {
  452. struct buffer_head *bh = NULL;
  453. struct ext4_xattr_entry *entry;
  454. size_t size;
  455. void *end;
  456. int error;
  457. struct mb_cache *ea_block_cache = EA_BLOCK_CACHE(inode);
  458. ea_idebug(inode, "name=%d.%s, buffer=%p, buffer_size=%ld",
  459. name_index, name, buffer, (long)buffer_size);
  460. error = -ENODATA;
  461. if (!EXT4_I(inode)->i_file_acl)
  462. goto cleanup;
  463. ea_idebug(inode, "reading block %llu",
  464. (unsigned long long)EXT4_I(inode)->i_file_acl);
  465. bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
  466. if (!bh)
  467. goto cleanup;
  468. ea_bdebug(bh, "b_count=%d, refcount=%d",
  469. atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
  470. error = ext4_xattr_check_block(inode, bh);
  471. if (error)
  472. goto cleanup;
  473. ext4_xattr_block_cache_insert(ea_block_cache, bh);
  474. entry = BFIRST(bh);
  475. end = bh->b_data + bh->b_size;
  476. error = xattr_find_entry(inode, &entry, end, name_index, name, 1);
  477. if (error)
  478. goto cleanup;
  479. size = le32_to_cpu(entry->e_value_size);
  480. error = -ERANGE;
  481. if (unlikely(size > EXT4_XATTR_SIZE_MAX))
  482. goto cleanup;
  483. if (buffer) {
  484. if (size > buffer_size)
  485. goto cleanup;
  486. if (entry->e_value_inum) {
  487. error = ext4_xattr_inode_get(inode, entry, buffer,
  488. size);
  489. if (error)
  490. goto cleanup;
  491. } else {
  492. u16 offset = le16_to_cpu(entry->e_value_offs);
  493. void *p = bh->b_data + offset;
  494. if (unlikely(p + size > end))
  495. goto cleanup;
  496. memcpy(buffer, p, size);
  497. }
  498. }
  499. error = size;
  500. cleanup:
  501. brelse(bh);
  502. return error;
  503. }
  504. int
  505. ext4_xattr_ibody_get(struct inode *inode, int name_index, const char *name,
  506. void *buffer, size_t buffer_size)
  507. {
  508. struct ext4_xattr_ibody_header *header;
  509. struct ext4_xattr_entry *entry;
  510. struct ext4_inode *raw_inode;
  511. struct ext4_iloc iloc;
  512. size_t size;
  513. void *end;
  514. int error;
  515. if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
  516. return -ENODATA;
  517. error = ext4_get_inode_loc(inode, &iloc);
  518. if (error)
  519. return error;
  520. raw_inode = ext4_raw_inode(&iloc);
  521. header = IHDR(inode, raw_inode);
  522. end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
  523. error = xattr_check_inode(inode, header, end);
  524. if (error)
  525. goto cleanup;
  526. entry = IFIRST(header);
  527. error = xattr_find_entry(inode, &entry, end, name_index, name, 0);
  528. if (error)
  529. goto cleanup;
  530. size = le32_to_cpu(entry->e_value_size);
  531. error = -ERANGE;
  532. if (unlikely(size > EXT4_XATTR_SIZE_MAX))
  533. goto cleanup;
  534. if (buffer) {
  535. if (size > buffer_size)
  536. goto cleanup;
  537. if (entry->e_value_inum) {
  538. error = ext4_xattr_inode_get(inode, entry, buffer,
  539. size);
  540. if (error)
  541. goto cleanup;
  542. } else {
  543. u16 offset = le16_to_cpu(entry->e_value_offs);
  544. void *p = (void *)IFIRST(header) + offset;
  545. if (unlikely(p + size > end))
  546. goto cleanup;
  547. memcpy(buffer, p, size);
  548. }
  549. }
  550. error = size;
  551. cleanup:
  552. brelse(iloc.bh);
  553. return error;
  554. }
  555. /*
  556. * ext4_xattr_get()
  557. *
  558. * Copy an extended attribute into the buffer
  559. * provided, or compute the buffer size required.
  560. * Buffer is NULL to compute the size of the buffer required.
  561. *
  562. * Returns a negative error number on failure, or the number of bytes
  563. * used / required on success.
  564. */
  565. int
  566. ext4_xattr_get(struct inode *inode, int name_index, const char *name,
  567. void *buffer, size_t buffer_size)
  568. {
  569. int error;
  570. if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
  571. return -EIO;
  572. if (strlen(name) > 255)
  573. return -ERANGE;
  574. down_read(&EXT4_I(inode)->xattr_sem);
  575. error = ext4_xattr_ibody_get(inode, name_index, name, buffer,
  576. buffer_size);
  577. if (error == -ENODATA)
  578. error = ext4_xattr_block_get(inode, name_index, name, buffer,
  579. buffer_size);
  580. up_read(&EXT4_I(inode)->xattr_sem);
  581. return error;
  582. }
  583. static int
  584. ext4_xattr_list_entries(struct dentry *dentry, struct ext4_xattr_entry *entry,
  585. char *buffer, size_t buffer_size)
  586. {
  587. size_t rest = buffer_size;
  588. for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
  589. const struct xattr_handler *handler =
  590. ext4_xattr_handler(entry->e_name_index);
  591. if (handler && (!handler->list || handler->list(dentry))) {
  592. const char *prefix = handler->prefix ?: handler->name;
  593. size_t prefix_len = strlen(prefix);
  594. size_t size = prefix_len + entry->e_name_len + 1;
  595. if (buffer) {
  596. if (size > rest)
  597. return -ERANGE;
  598. memcpy(buffer, prefix, prefix_len);
  599. buffer += prefix_len;
  600. memcpy(buffer, entry->e_name, entry->e_name_len);
  601. buffer += entry->e_name_len;
  602. *buffer++ = 0;
  603. }
  604. rest -= size;
  605. }
  606. }
  607. return buffer_size - rest; /* total size */
  608. }
  609. static int
  610. ext4_xattr_block_list(struct dentry *dentry, char *buffer, size_t buffer_size)
  611. {
  612. struct inode *inode = d_inode(dentry);
  613. struct buffer_head *bh = NULL;
  614. int error;
  615. ea_idebug(inode, "buffer=%p, buffer_size=%ld",
  616. buffer, (long)buffer_size);
  617. error = 0;
  618. if (!EXT4_I(inode)->i_file_acl)
  619. goto cleanup;
  620. ea_idebug(inode, "reading block %llu",
  621. (unsigned long long)EXT4_I(inode)->i_file_acl);
  622. bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
  623. error = -EIO;
  624. if (!bh)
  625. goto cleanup;
  626. ea_bdebug(bh, "b_count=%d, refcount=%d",
  627. atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
  628. error = ext4_xattr_check_block(inode, bh);
  629. if (error)
  630. goto cleanup;
  631. ext4_xattr_block_cache_insert(EA_BLOCK_CACHE(inode), bh);
  632. error = ext4_xattr_list_entries(dentry, BFIRST(bh), buffer, buffer_size);
  633. cleanup:
  634. brelse(bh);
  635. return error;
  636. }
  637. static int
  638. ext4_xattr_ibody_list(struct dentry *dentry, char *buffer, size_t buffer_size)
  639. {
  640. struct inode *inode = d_inode(dentry);
  641. struct ext4_xattr_ibody_header *header;
  642. struct ext4_inode *raw_inode;
  643. struct ext4_iloc iloc;
  644. void *end;
  645. int error;
  646. if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
  647. return 0;
  648. error = ext4_get_inode_loc(inode, &iloc);
  649. if (error)
  650. return error;
  651. raw_inode = ext4_raw_inode(&iloc);
  652. header = IHDR(inode, raw_inode);
  653. end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
  654. error = xattr_check_inode(inode, header, end);
  655. if (error)
  656. goto cleanup;
  657. error = ext4_xattr_list_entries(dentry, IFIRST(header),
  658. buffer, buffer_size);
  659. cleanup:
  660. brelse(iloc.bh);
  661. return error;
  662. }
  663. /*
  664. * Inode operation listxattr()
  665. *
  666. * d_inode(dentry)->i_rwsem: don't care
  667. *
  668. * Copy a list of attribute names into the buffer
  669. * provided, or compute the buffer size required.
  670. * Buffer is NULL to compute the size of the buffer required.
  671. *
  672. * Returns a negative error number on failure, or the number of bytes
  673. * used / required on success.
  674. */
  675. ssize_t
  676. ext4_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
  677. {
  678. int ret, ret2;
  679. down_read(&EXT4_I(d_inode(dentry))->xattr_sem);
  680. ret = ret2 = ext4_xattr_ibody_list(dentry, buffer, buffer_size);
  681. if (ret < 0)
  682. goto errout;
  683. if (buffer) {
  684. buffer += ret;
  685. buffer_size -= ret;
  686. }
  687. ret = ext4_xattr_block_list(dentry, buffer, buffer_size);
  688. if (ret < 0)
  689. goto errout;
  690. ret += ret2;
  691. errout:
  692. up_read(&EXT4_I(d_inode(dentry))->xattr_sem);
  693. return ret;
  694. }
  695. /*
  696. * If the EXT4_FEATURE_COMPAT_EXT_ATTR feature of this file system is
  697. * not set, set it.
  698. */
  699. static void ext4_xattr_update_super_block(handle_t *handle,
  700. struct super_block *sb)
  701. {
  702. if (ext4_has_feature_xattr(sb))
  703. return;
  704. BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get_write_access");
  705. if (ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh) == 0) {
  706. ext4_set_feature_xattr(sb);
  707. ext4_handle_dirty_super(handle, sb);
  708. }
  709. }
  710. int ext4_get_inode_usage(struct inode *inode, qsize_t *usage)
  711. {
  712. struct ext4_iloc iloc = { .bh = NULL };
  713. struct buffer_head *bh = NULL;
  714. struct ext4_inode *raw_inode;
  715. struct ext4_xattr_ibody_header *header;
  716. struct ext4_xattr_entry *entry;
  717. qsize_t ea_inode_refs = 0;
  718. void *end;
  719. int ret;
  720. lockdep_assert_held_read(&EXT4_I(inode)->xattr_sem);
  721. if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
  722. ret = ext4_get_inode_loc(inode, &iloc);
  723. if (ret)
  724. goto out;
  725. raw_inode = ext4_raw_inode(&iloc);
  726. header = IHDR(inode, raw_inode);
  727. end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
  728. ret = xattr_check_inode(inode, header, end);
  729. if (ret)
  730. goto out;
  731. for (entry = IFIRST(header); !IS_LAST_ENTRY(entry);
  732. entry = EXT4_XATTR_NEXT(entry))
  733. if (entry->e_value_inum)
  734. ea_inode_refs++;
  735. }
  736. if (EXT4_I(inode)->i_file_acl) {
  737. bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
  738. if (!bh) {
  739. ret = -EIO;
  740. goto out;
  741. }
  742. ret = ext4_xattr_check_block(inode, bh);
  743. if (ret)
  744. goto out;
  745. for (entry = BFIRST(bh); !IS_LAST_ENTRY(entry);
  746. entry = EXT4_XATTR_NEXT(entry))
  747. if (entry->e_value_inum)
  748. ea_inode_refs++;
  749. }
  750. *usage = ea_inode_refs + 1;
  751. ret = 0;
  752. out:
  753. brelse(iloc.bh);
  754. brelse(bh);
  755. return ret;
  756. }
  757. static inline size_t round_up_cluster(struct inode *inode, size_t length)
  758. {
  759. struct super_block *sb = inode->i_sb;
  760. size_t cluster_size = 1 << (EXT4_SB(sb)->s_cluster_bits +
  761. inode->i_blkbits);
  762. size_t mask = ~(cluster_size - 1);
  763. return (length + cluster_size - 1) & mask;
  764. }
  765. static int ext4_xattr_inode_alloc_quota(struct inode *inode, size_t len)
  766. {
  767. int err;
  768. err = dquot_alloc_inode(inode);
  769. if (err)
  770. return err;
  771. err = dquot_alloc_space_nodirty(inode, round_up_cluster(inode, len));
  772. if (err)
  773. dquot_free_inode(inode);
  774. return err;
  775. }
  776. static void ext4_xattr_inode_free_quota(struct inode *parent,
  777. struct inode *ea_inode,
  778. size_t len)
  779. {
  780. if (ea_inode &&
  781. ext4_test_inode_state(ea_inode, EXT4_STATE_LUSTRE_EA_INODE))
  782. return;
  783. dquot_free_space_nodirty(parent, round_up_cluster(parent, len));
  784. dquot_free_inode(parent);
  785. }
  786. int __ext4_xattr_set_credits(struct super_block *sb, struct inode *inode,
  787. struct buffer_head *block_bh, size_t value_len,
  788. bool is_create)
  789. {
  790. int credits;
  791. int blocks;
  792. /*
  793. * 1) Owner inode update
  794. * 2) Ref count update on old xattr block
  795. * 3) new xattr block
  796. * 4) block bitmap update for new xattr block
  797. * 5) group descriptor for new xattr block
  798. * 6) block bitmap update for old xattr block
  799. * 7) group descriptor for old block
  800. *
  801. * 6 & 7 can happen if we have two racing threads T_a and T_b
  802. * which are each trying to set an xattr on inodes I_a and I_b
  803. * which were both initially sharing an xattr block.
  804. */
  805. credits = 7;
  806. /* Quota updates. */
  807. credits += EXT4_MAXQUOTAS_TRANS_BLOCKS(sb);
  808. /*
  809. * In case of inline data, we may push out the data to a block,
  810. * so we need to reserve credits for this eventuality
  811. */
  812. if (inode && ext4_has_inline_data(inode))
  813. credits += ext4_writepage_trans_blocks(inode) + 1;
  814. /* We are done if ea_inode feature is not enabled. */
  815. if (!ext4_has_feature_ea_inode(sb))
  816. return credits;
  817. /* New ea_inode, inode map, block bitmap, group descriptor. */
  818. credits += 4;
  819. /* Data blocks. */
  820. blocks = (value_len + sb->s_blocksize - 1) >> sb->s_blocksize_bits;
  821. /* Indirection block or one level of extent tree. */
  822. blocks += 1;
  823. /* Block bitmap and group descriptor updates for each block. */
  824. credits += blocks * 2;
  825. /* Blocks themselves. */
  826. credits += blocks;
  827. if (!is_create) {
  828. /* Dereference ea_inode holding old xattr value.
  829. * Old ea_inode, inode map, block bitmap, group descriptor.
  830. */
  831. credits += 4;
  832. /* Data blocks for old ea_inode. */
  833. blocks = XATTR_SIZE_MAX >> sb->s_blocksize_bits;
  834. /* Indirection block or one level of extent tree for old
  835. * ea_inode.
  836. */
  837. blocks += 1;
  838. /* Block bitmap and group descriptor updates for each block. */
  839. credits += blocks * 2;
  840. }
  841. /* We may need to clone the existing xattr block in which case we need
  842. * to increment ref counts for existing ea_inodes referenced by it.
  843. */
  844. if (block_bh) {
  845. struct ext4_xattr_entry *entry = BFIRST(block_bh);
  846. for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry))
  847. if (entry->e_value_inum)
  848. /* Ref count update on ea_inode. */
  849. credits += 1;
  850. }
  851. return credits;
  852. }
  853. static int ext4_xattr_ensure_credits(handle_t *handle, struct inode *inode,
  854. int credits, struct buffer_head *bh,
  855. bool dirty, bool block_csum)
  856. {
  857. int error;
  858. if (!ext4_handle_valid(handle))
  859. return 0;
  860. if (handle->h_buffer_credits >= credits)
  861. return 0;
  862. error = ext4_journal_extend(handle, credits - handle->h_buffer_credits);
  863. if (!error)
  864. return 0;
  865. if (error < 0) {
  866. ext4_warning(inode->i_sb, "Extend journal (error %d)", error);
  867. return error;
  868. }
  869. if (bh && dirty) {
  870. if (block_csum)
  871. ext4_xattr_block_csum_set(inode, bh);
  872. error = ext4_handle_dirty_metadata(handle, NULL, bh);
  873. if (error) {
  874. ext4_warning(inode->i_sb, "Handle metadata (error %d)",
  875. error);
  876. return error;
  877. }
  878. }
  879. error = ext4_journal_restart(handle, credits);
  880. if (error) {
  881. ext4_warning(inode->i_sb, "Restart journal (error %d)", error);
  882. return error;
  883. }
  884. if (bh) {
  885. error = ext4_journal_get_write_access(handle, bh);
  886. if (error) {
  887. ext4_warning(inode->i_sb,
  888. "Get write access failed (error %d)",
  889. error);
  890. return error;
  891. }
  892. }
  893. return 0;
  894. }
  895. static int ext4_xattr_inode_update_ref(handle_t *handle, struct inode *ea_inode,
  896. int ref_change)
  897. {
  898. struct mb_cache *ea_inode_cache = EA_INODE_CACHE(ea_inode);
  899. struct ext4_iloc iloc;
  900. s64 ref_count;
  901. u32 hash;
  902. int ret;
  903. inode_lock(ea_inode);
  904. ret = ext4_reserve_inode_write(handle, ea_inode, &iloc);
  905. if (ret)
  906. goto out;
  907. ref_count = ext4_xattr_inode_get_ref(ea_inode);
  908. ref_count += ref_change;
  909. ext4_xattr_inode_set_ref(ea_inode, ref_count);
  910. if (ref_change > 0) {
  911. WARN_ONCE(ref_count <= 0, "EA inode %lu ref_count=%lld",
  912. ea_inode->i_ino, ref_count);
  913. if (ref_count == 1) {
  914. WARN_ONCE(ea_inode->i_nlink, "EA inode %lu i_nlink=%u",
  915. ea_inode->i_ino, ea_inode->i_nlink);
  916. set_nlink(ea_inode, 1);
  917. ext4_orphan_del(handle, ea_inode);
  918. if (ea_inode_cache) {
  919. hash = ext4_xattr_inode_get_hash(ea_inode);
  920. mb_cache_entry_create(ea_inode_cache,
  921. GFP_NOFS, hash,
  922. ea_inode->i_ino,
  923. true /* reusable */);
  924. }
  925. }
  926. } else {
  927. WARN_ONCE(ref_count < 0, "EA inode %lu ref_count=%lld",
  928. ea_inode->i_ino, ref_count);
  929. if (ref_count == 0) {
  930. WARN_ONCE(ea_inode->i_nlink != 1,
  931. "EA inode %lu i_nlink=%u",
  932. ea_inode->i_ino, ea_inode->i_nlink);
  933. clear_nlink(ea_inode);
  934. ext4_orphan_add(handle, ea_inode);
  935. if (ea_inode_cache) {
  936. hash = ext4_xattr_inode_get_hash(ea_inode);
  937. mb_cache_entry_delete(ea_inode_cache, hash,
  938. ea_inode->i_ino);
  939. }
  940. }
  941. }
  942. ret = ext4_mark_iloc_dirty(handle, ea_inode, &iloc);
  943. if (ret)
  944. ext4_warning_inode(ea_inode,
  945. "ext4_mark_iloc_dirty() failed ret=%d", ret);
  946. out:
  947. inode_unlock(ea_inode);
  948. return ret;
  949. }
  950. static int ext4_xattr_inode_inc_ref(handle_t *handle, struct inode *ea_inode)
  951. {
  952. return ext4_xattr_inode_update_ref(handle, ea_inode, 1);
  953. }
  954. static int ext4_xattr_inode_dec_ref(handle_t *handle, struct inode *ea_inode)
  955. {
  956. return ext4_xattr_inode_update_ref(handle, ea_inode, -1);
  957. }
  958. static int ext4_xattr_inode_inc_ref_all(handle_t *handle, struct inode *parent,
  959. struct ext4_xattr_entry *first)
  960. {
  961. struct inode *ea_inode;
  962. struct ext4_xattr_entry *entry;
  963. struct ext4_xattr_entry *failed_entry;
  964. unsigned int ea_ino;
  965. int err, saved_err;
  966. for (entry = first; !IS_LAST_ENTRY(entry);
  967. entry = EXT4_XATTR_NEXT(entry)) {
  968. if (!entry->e_value_inum)
  969. continue;
  970. ea_ino = le32_to_cpu(entry->e_value_inum);
  971. err = ext4_xattr_inode_iget(parent, ea_ino,
  972. le32_to_cpu(entry->e_hash),
  973. &ea_inode);
  974. if (err)
  975. goto cleanup;
  976. err = ext4_xattr_inode_inc_ref(handle, ea_inode);
  977. if (err) {
  978. ext4_warning_inode(ea_inode, "inc ref error %d", err);
  979. iput(ea_inode);
  980. goto cleanup;
  981. }
  982. iput(ea_inode);
  983. }
  984. return 0;
  985. cleanup:
  986. saved_err = err;
  987. failed_entry = entry;
  988. for (entry = first; entry != failed_entry;
  989. entry = EXT4_XATTR_NEXT(entry)) {
  990. if (!entry->e_value_inum)
  991. continue;
  992. ea_ino = le32_to_cpu(entry->e_value_inum);
  993. err = ext4_xattr_inode_iget(parent, ea_ino,
  994. le32_to_cpu(entry->e_hash),
  995. &ea_inode);
  996. if (err) {
  997. ext4_warning(parent->i_sb,
  998. "cleanup ea_ino %u iget error %d", ea_ino,
  999. err);
  1000. continue;
  1001. }
  1002. err = ext4_xattr_inode_dec_ref(handle, ea_inode);
  1003. if (err)
  1004. ext4_warning_inode(ea_inode, "cleanup dec ref error %d",
  1005. err);
  1006. iput(ea_inode);
  1007. }
  1008. return saved_err;
  1009. }
  1010. static void
  1011. ext4_xattr_inode_dec_ref_all(handle_t *handle, struct inode *parent,
  1012. struct buffer_head *bh,
  1013. struct ext4_xattr_entry *first, bool block_csum,
  1014. struct ext4_xattr_inode_array **ea_inode_array,
  1015. int extra_credits, bool skip_quota)
  1016. {
  1017. struct inode *ea_inode;
  1018. struct ext4_xattr_entry *entry;
  1019. bool dirty = false;
  1020. unsigned int ea_ino;
  1021. int err;
  1022. int credits;
  1023. /* One credit for dec ref on ea_inode, one for orphan list addition, */
  1024. credits = 2 + extra_credits;
  1025. for (entry = first; !IS_LAST_ENTRY(entry);
  1026. entry = EXT4_XATTR_NEXT(entry)) {
  1027. if (!entry->e_value_inum)
  1028. continue;
  1029. ea_ino = le32_to_cpu(entry->e_value_inum);
  1030. err = ext4_xattr_inode_iget(parent, ea_ino,
  1031. le32_to_cpu(entry->e_hash),
  1032. &ea_inode);
  1033. if (err)
  1034. continue;
  1035. err = ext4_expand_inode_array(ea_inode_array, ea_inode);
  1036. if (err) {
  1037. ext4_warning_inode(ea_inode,
  1038. "Expand inode array err=%d", err);
  1039. iput(ea_inode);
  1040. continue;
  1041. }
  1042. err = ext4_xattr_ensure_credits(handle, parent, credits, bh,
  1043. dirty, block_csum);
  1044. if (err) {
  1045. ext4_warning_inode(ea_inode, "Ensure credits err=%d",
  1046. err);
  1047. continue;
  1048. }
  1049. err = ext4_xattr_inode_dec_ref(handle, ea_inode);
  1050. if (err) {
  1051. ext4_warning_inode(ea_inode, "ea_inode dec ref err=%d",
  1052. err);
  1053. continue;
  1054. }
  1055. if (!skip_quota)
  1056. ext4_xattr_inode_free_quota(parent, ea_inode,
  1057. le32_to_cpu(entry->e_value_size));
  1058. /*
  1059. * Forget about ea_inode within the same transaction that
  1060. * decrements the ref count. This avoids duplicate decrements in
  1061. * case the rest of the work spills over to subsequent
  1062. * transactions.
  1063. */
  1064. entry->e_value_inum = 0;
  1065. entry->e_value_size = 0;
  1066. dirty = true;
  1067. }
  1068. if (dirty) {
  1069. /*
  1070. * Note that we are deliberately skipping csum calculation for
  1071. * the final update because we do not expect any journal
  1072. * restarts until xattr block is freed.
  1073. */
  1074. err = ext4_handle_dirty_metadata(handle, NULL, bh);
  1075. if (err)
  1076. ext4_warning_inode(parent,
  1077. "handle dirty metadata err=%d", err);
  1078. }
  1079. }
  1080. /*
  1081. * Release the xattr block BH: If the reference count is > 1, decrement it;
  1082. * otherwise free the block.
  1083. */
  1084. static void
  1085. ext4_xattr_release_block(handle_t *handle, struct inode *inode,
  1086. struct buffer_head *bh,
  1087. struct ext4_xattr_inode_array **ea_inode_array,
  1088. int extra_credits)
  1089. {
  1090. struct mb_cache *ea_block_cache = EA_BLOCK_CACHE(inode);
  1091. u32 hash, ref;
  1092. int error = 0;
  1093. BUFFER_TRACE(bh, "get_write_access");
  1094. error = ext4_journal_get_write_access(handle, bh);
  1095. if (error)
  1096. goto out;
  1097. lock_buffer(bh);
  1098. hash = le32_to_cpu(BHDR(bh)->h_hash);
  1099. ref = le32_to_cpu(BHDR(bh)->h_refcount);
  1100. if (ref == 1) {
  1101. ea_bdebug(bh, "refcount now=0; freeing");
  1102. /*
  1103. * This must happen under buffer lock for
  1104. * ext4_xattr_block_set() to reliably detect freed block
  1105. */
  1106. if (ea_block_cache)
  1107. mb_cache_entry_delete(ea_block_cache, hash,
  1108. bh->b_blocknr);
  1109. get_bh(bh);
  1110. unlock_buffer(bh);
  1111. if (ext4_has_feature_ea_inode(inode->i_sb))
  1112. ext4_xattr_inode_dec_ref_all(handle, inode, bh,
  1113. BFIRST(bh),
  1114. true /* block_csum */,
  1115. ea_inode_array,
  1116. extra_credits,
  1117. true /* skip_quota */);
  1118. ext4_free_blocks(handle, inode, bh, 0, 1,
  1119. EXT4_FREE_BLOCKS_METADATA |
  1120. EXT4_FREE_BLOCKS_FORGET);
  1121. } else {
  1122. ref--;
  1123. BHDR(bh)->h_refcount = cpu_to_le32(ref);
  1124. if (ref == EXT4_XATTR_REFCOUNT_MAX - 1) {
  1125. struct mb_cache_entry *ce;
  1126. if (ea_block_cache) {
  1127. ce = mb_cache_entry_get(ea_block_cache, hash,
  1128. bh->b_blocknr);
  1129. if (ce) {
  1130. ce->e_reusable = 1;
  1131. mb_cache_entry_put(ea_block_cache, ce);
  1132. }
  1133. }
  1134. }
  1135. ext4_xattr_block_csum_set(inode, bh);
  1136. /*
  1137. * Beware of this ugliness: Releasing of xattr block references
  1138. * from different inodes can race and so we have to protect
  1139. * from a race where someone else frees the block (and releases
  1140. * its journal_head) before we are done dirtying the buffer. In
  1141. * nojournal mode this race is harmless and we actually cannot
  1142. * call ext4_handle_dirty_metadata() with locked buffer as
  1143. * that function can call sync_dirty_buffer() so for that case
  1144. * we handle the dirtying after unlocking the buffer.
  1145. */
  1146. if (ext4_handle_valid(handle))
  1147. error = ext4_handle_dirty_metadata(handle, inode, bh);
  1148. unlock_buffer(bh);
  1149. if (!ext4_handle_valid(handle))
  1150. error = ext4_handle_dirty_metadata(handle, inode, bh);
  1151. if (IS_SYNC(inode))
  1152. ext4_handle_sync(handle);
  1153. dquot_free_block(inode, EXT4_C2B(EXT4_SB(inode->i_sb), 1));
  1154. ea_bdebug(bh, "refcount now=%d; releasing",
  1155. le32_to_cpu(BHDR(bh)->h_refcount));
  1156. }
  1157. out:
  1158. ext4_std_error(inode->i_sb, error);
  1159. return;
  1160. }
  1161. /*
  1162. * Find the available free space for EAs. This also returns the total number of
  1163. * bytes used by EA entries.
  1164. */
  1165. static size_t ext4_xattr_free_space(struct ext4_xattr_entry *last,
  1166. size_t *min_offs, void *base, int *total)
  1167. {
  1168. for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
  1169. if (!last->e_value_inum && last->e_value_size) {
  1170. size_t offs = le16_to_cpu(last->e_value_offs);
  1171. if (offs < *min_offs)
  1172. *min_offs = offs;
  1173. }
  1174. if (total)
  1175. *total += EXT4_XATTR_LEN(last->e_name_len);
  1176. }
  1177. return (*min_offs - ((void *)last - base) - sizeof(__u32));
  1178. }
  1179. /*
  1180. * Write the value of the EA in an inode.
  1181. */
  1182. static int ext4_xattr_inode_write(handle_t *handle, struct inode *ea_inode,
  1183. const void *buf, int bufsize)
  1184. {
  1185. struct buffer_head *bh = NULL;
  1186. unsigned long block = 0;
  1187. int blocksize = ea_inode->i_sb->s_blocksize;
  1188. int max_blocks = (bufsize + blocksize - 1) >> ea_inode->i_blkbits;
  1189. int csize, wsize = 0;
  1190. int ret = 0;
  1191. int retries = 0;
  1192. retry:
  1193. while (ret >= 0 && ret < max_blocks) {
  1194. struct ext4_map_blocks map;
  1195. map.m_lblk = block += ret;
  1196. map.m_len = max_blocks -= ret;
  1197. ret = ext4_map_blocks(handle, ea_inode, &map,
  1198. EXT4_GET_BLOCKS_CREATE);
  1199. if (ret <= 0) {
  1200. ext4_mark_inode_dirty(handle, ea_inode);
  1201. if (ret == -ENOSPC &&
  1202. ext4_should_retry_alloc(ea_inode->i_sb, &retries)) {
  1203. ret = 0;
  1204. goto retry;
  1205. }
  1206. break;
  1207. }
  1208. }
  1209. if (ret < 0)
  1210. return ret;
  1211. block = 0;
  1212. while (wsize < bufsize) {
  1213. if (bh != NULL)
  1214. brelse(bh);
  1215. csize = (bufsize - wsize) > blocksize ? blocksize :
  1216. bufsize - wsize;
  1217. bh = ext4_getblk(handle, ea_inode, block, 0);
  1218. if (IS_ERR(bh))
  1219. return PTR_ERR(bh);
  1220. if (!bh) {
  1221. WARN_ON_ONCE(1);
  1222. EXT4_ERROR_INODE(ea_inode,
  1223. "ext4_getblk() return bh = NULL");
  1224. return -EFSCORRUPTED;
  1225. }
  1226. ret = ext4_journal_get_write_access(handle, bh);
  1227. if (ret)
  1228. goto out;
  1229. memcpy(bh->b_data, buf, csize);
  1230. set_buffer_uptodate(bh);
  1231. ext4_handle_dirty_metadata(handle, ea_inode, bh);
  1232. buf += csize;
  1233. wsize += csize;
  1234. block += 1;
  1235. }
  1236. inode_lock(ea_inode);
  1237. i_size_write(ea_inode, wsize);
  1238. ext4_update_i_disksize(ea_inode, wsize);
  1239. inode_unlock(ea_inode);
  1240. ext4_mark_inode_dirty(handle, ea_inode);
  1241. out:
  1242. brelse(bh);
  1243. return ret;
  1244. }
  1245. /*
  1246. * Create an inode to store the value of a large EA.
  1247. */
  1248. static struct inode *ext4_xattr_inode_create(handle_t *handle,
  1249. struct inode *inode, u32 hash)
  1250. {
  1251. struct inode *ea_inode = NULL;
  1252. uid_t owner[2] = { i_uid_read(inode), i_gid_read(inode) };
  1253. int err;
  1254. /*
  1255. * Let the next inode be the goal, so we try and allocate the EA inode
  1256. * in the same group, or nearby one.
  1257. */
  1258. ea_inode = ext4_new_inode(handle, inode->i_sb->s_root->d_inode,
  1259. S_IFREG | 0600, NULL, inode->i_ino + 1, owner,
  1260. EXT4_EA_INODE_FL);
  1261. if (!IS_ERR(ea_inode)) {
  1262. ea_inode->i_op = &ext4_file_inode_operations;
  1263. ea_inode->i_fop = &ext4_file_operations;
  1264. ext4_set_aops(ea_inode);
  1265. ext4_xattr_inode_set_class(ea_inode);
  1266. unlock_new_inode(ea_inode);
  1267. ext4_xattr_inode_set_ref(ea_inode, 1);
  1268. ext4_xattr_inode_set_hash(ea_inode, hash);
  1269. err = ext4_mark_inode_dirty(handle, ea_inode);
  1270. if (!err)
  1271. err = ext4_inode_attach_jinode(ea_inode);
  1272. if (err) {
  1273. iput(ea_inode);
  1274. return ERR_PTR(err);
  1275. }
  1276. /*
  1277. * Xattr inodes are shared therefore quota charging is performed
  1278. * at a higher level.
  1279. */
  1280. dquot_free_inode(ea_inode);
  1281. dquot_drop(ea_inode);
  1282. inode_lock(ea_inode);
  1283. ea_inode->i_flags |= S_NOQUOTA;
  1284. inode_unlock(ea_inode);
  1285. }
  1286. return ea_inode;
  1287. }
  1288. static struct inode *
  1289. ext4_xattr_inode_cache_find(struct inode *inode, const void *value,
  1290. size_t value_len, u32 hash)
  1291. {
  1292. struct inode *ea_inode;
  1293. struct mb_cache_entry *ce;
  1294. struct mb_cache *ea_inode_cache = EA_INODE_CACHE(inode);
  1295. void *ea_data;
  1296. if (!ea_inode_cache)
  1297. return NULL;
  1298. ce = mb_cache_entry_find_first(ea_inode_cache, hash);
  1299. if (!ce)
  1300. return NULL;
  1301. ea_data = ext4_kvmalloc(value_len, GFP_NOFS);
  1302. if (!ea_data) {
  1303. mb_cache_entry_put(ea_inode_cache, ce);
  1304. return NULL;
  1305. }
  1306. while (ce) {
  1307. ea_inode = ext4_iget(inode->i_sb, ce->e_value);
  1308. if (!IS_ERR(ea_inode) &&
  1309. !is_bad_inode(ea_inode) &&
  1310. (EXT4_I(ea_inode)->i_flags & EXT4_EA_INODE_FL) &&
  1311. i_size_read(ea_inode) == value_len &&
  1312. !ext4_xattr_inode_read(ea_inode, ea_data, value_len) &&
  1313. !ext4_xattr_inode_verify_hashes(ea_inode, NULL, ea_data,
  1314. value_len) &&
  1315. !memcmp(value, ea_data, value_len)) {
  1316. mb_cache_entry_touch(ea_inode_cache, ce);
  1317. mb_cache_entry_put(ea_inode_cache, ce);
  1318. kvfree(ea_data);
  1319. return ea_inode;
  1320. }
  1321. if (!IS_ERR(ea_inode))
  1322. iput(ea_inode);
  1323. ce = mb_cache_entry_find_next(ea_inode_cache, ce);
  1324. }
  1325. kvfree(ea_data);
  1326. return NULL;
  1327. }
  1328. /*
  1329. * Add value of the EA in an inode.
  1330. */
  1331. static int ext4_xattr_inode_lookup_create(handle_t *handle, struct inode *inode,
  1332. const void *value, size_t value_len,
  1333. struct inode **ret_inode)
  1334. {
  1335. struct inode *ea_inode;
  1336. u32 hash;
  1337. int err;
  1338. hash = ext4_xattr_inode_hash(EXT4_SB(inode->i_sb), value, value_len);
  1339. ea_inode = ext4_xattr_inode_cache_find(inode, value, value_len, hash);
  1340. if (ea_inode) {
  1341. err = ext4_xattr_inode_inc_ref(handle, ea_inode);
  1342. if (err) {
  1343. iput(ea_inode);
  1344. return err;
  1345. }
  1346. *ret_inode = ea_inode;
  1347. return 0;
  1348. }
  1349. /* Create an inode for the EA value */
  1350. ea_inode = ext4_xattr_inode_create(handle, inode, hash);
  1351. if (IS_ERR(ea_inode))
  1352. return PTR_ERR(ea_inode);
  1353. err = ext4_xattr_inode_write(handle, ea_inode, value, value_len);
  1354. if (err) {
  1355. ext4_xattr_inode_dec_ref(handle, ea_inode);
  1356. iput(ea_inode);
  1357. return err;
  1358. }
  1359. if (EA_INODE_CACHE(inode))
  1360. mb_cache_entry_create(EA_INODE_CACHE(inode), GFP_NOFS, hash,
  1361. ea_inode->i_ino, true /* reusable */);
  1362. *ret_inode = ea_inode;
  1363. return 0;
  1364. }
  1365. /*
  1366. * Reserve min(block_size/8, 1024) bytes for xattr entries/names if ea_inode
  1367. * feature is enabled.
  1368. */
  1369. #define EXT4_XATTR_BLOCK_RESERVE(inode) min(i_blocksize(inode)/8, 1024U)
  1370. static int ext4_xattr_set_entry(struct ext4_xattr_info *i,
  1371. struct ext4_xattr_search *s,
  1372. handle_t *handle, struct inode *inode,
  1373. bool is_block)
  1374. {
  1375. struct ext4_xattr_entry *last, *next;
  1376. struct ext4_xattr_entry *here = s->here;
  1377. size_t min_offs = s->end - s->base, name_len = strlen(i->name);
  1378. int in_inode = i->in_inode;
  1379. struct inode *old_ea_inode = NULL;
  1380. struct inode *new_ea_inode = NULL;
  1381. size_t old_size, new_size;
  1382. int ret;
  1383. /* Space used by old and new values. */
  1384. old_size = (!s->not_found && !here->e_value_inum) ?
  1385. EXT4_XATTR_SIZE(le32_to_cpu(here->e_value_size)) : 0;
  1386. new_size = (i->value && !in_inode) ? EXT4_XATTR_SIZE(i->value_len) : 0;
  1387. /*
  1388. * Optimization for the simple case when old and new values have the
  1389. * same padded sizes. Not applicable if external inodes are involved.
  1390. */
  1391. if (new_size && new_size == old_size) {
  1392. size_t offs = le16_to_cpu(here->e_value_offs);
  1393. void *val = s->base + offs;
  1394. here->e_value_size = cpu_to_le32(i->value_len);
  1395. if (i->value == EXT4_ZERO_XATTR_VALUE) {
  1396. memset(val, 0, new_size);
  1397. } else {
  1398. memcpy(val, i->value, i->value_len);
  1399. /* Clear padding bytes. */
  1400. memset(val + i->value_len, 0, new_size - i->value_len);
  1401. }
  1402. goto update_hash;
  1403. }
  1404. /* Compute min_offs and last. */
  1405. last = s->first;
  1406. for (; !IS_LAST_ENTRY(last); last = next) {
  1407. next = EXT4_XATTR_NEXT(last);
  1408. if ((void *)next >= s->end) {
  1409. EXT4_ERROR_INODE(inode, "corrupted xattr entries");
  1410. ret = -EFSCORRUPTED;
  1411. goto out;
  1412. }
  1413. if (!last->e_value_inum && last->e_value_size) {
  1414. size_t offs = le16_to_cpu(last->e_value_offs);
  1415. if (offs < min_offs)
  1416. min_offs = offs;
  1417. }
  1418. }
  1419. /* Check whether we have enough space. */
  1420. if (i->value) {
  1421. size_t free;
  1422. free = min_offs - ((void *)last - s->base) - sizeof(__u32);
  1423. if (!s->not_found)
  1424. free += EXT4_XATTR_LEN(name_len) + old_size;
  1425. if (free < EXT4_XATTR_LEN(name_len) + new_size) {
  1426. ret = -ENOSPC;
  1427. goto out;
  1428. }
  1429. /*
  1430. * If storing the value in an external inode is an option,
  1431. * reserve space for xattr entries/names in the external
  1432. * attribute block so that a long value does not occupy the
  1433. * whole space and prevent futher entries being added.
  1434. */
  1435. if (ext4_has_feature_ea_inode(inode->i_sb) &&
  1436. new_size && is_block &&
  1437. (min_offs + old_size - new_size) <
  1438. EXT4_XATTR_BLOCK_RESERVE(inode)) {
  1439. ret = -ENOSPC;
  1440. goto out;
  1441. }
  1442. }
  1443. /*
  1444. * Getting access to old and new ea inodes is subject to failures.
  1445. * Finish that work before doing any modifications to the xattr data.
  1446. */
  1447. if (!s->not_found && here->e_value_inum) {
  1448. ret = ext4_xattr_inode_iget(inode,
  1449. le32_to_cpu(here->e_value_inum),
  1450. le32_to_cpu(here->e_hash),
  1451. &old_ea_inode);
  1452. if (ret) {
  1453. old_ea_inode = NULL;
  1454. goto out;
  1455. }
  1456. }
  1457. if (i->value && in_inode) {
  1458. WARN_ON_ONCE(!i->value_len);
  1459. ret = ext4_xattr_inode_alloc_quota(inode, i->value_len);
  1460. if (ret)
  1461. goto out;
  1462. ret = ext4_xattr_inode_lookup_create(handle, inode, i->value,
  1463. i->value_len,
  1464. &new_ea_inode);
  1465. if (ret) {
  1466. new_ea_inode = NULL;
  1467. ext4_xattr_inode_free_quota(inode, NULL, i->value_len);
  1468. goto out;
  1469. }
  1470. }
  1471. if (old_ea_inode) {
  1472. /* We are ready to release ref count on the old_ea_inode. */
  1473. ret = ext4_xattr_inode_dec_ref(handle, old_ea_inode);
  1474. if (ret) {
  1475. /* Release newly required ref count on new_ea_inode. */
  1476. if (new_ea_inode) {
  1477. int err;
  1478. err = ext4_xattr_inode_dec_ref(handle,
  1479. new_ea_inode);
  1480. if (err)
  1481. ext4_warning_inode(new_ea_inode,
  1482. "dec ref new_ea_inode err=%d",
  1483. err);
  1484. ext4_xattr_inode_free_quota(inode, new_ea_inode,
  1485. i->value_len);
  1486. }
  1487. goto out;
  1488. }
  1489. ext4_xattr_inode_free_quota(inode, old_ea_inode,
  1490. le32_to_cpu(here->e_value_size));
  1491. }
  1492. /* No failures allowed past this point. */
  1493. if (!s->not_found && here->e_value_size && here->e_value_offs) {
  1494. /* Remove the old value. */
  1495. void *first_val = s->base + min_offs;
  1496. size_t offs = le16_to_cpu(here->e_value_offs);
  1497. void *val = s->base + offs;
  1498. memmove(first_val + old_size, first_val, val - first_val);
  1499. memset(first_val, 0, old_size);
  1500. min_offs += old_size;
  1501. /* Adjust all value offsets. */
  1502. last = s->first;
  1503. while (!IS_LAST_ENTRY(last)) {
  1504. size_t o = le16_to_cpu(last->e_value_offs);
  1505. if (!last->e_value_inum &&
  1506. last->e_value_size && o < offs)
  1507. last->e_value_offs = cpu_to_le16(o + old_size);
  1508. last = EXT4_XATTR_NEXT(last);
  1509. }
  1510. }
  1511. if (!i->value) {
  1512. /* Remove old name. */
  1513. size_t size = EXT4_XATTR_LEN(name_len);
  1514. last = ENTRY((void *)last - size);
  1515. memmove(here, (void *)here + size,
  1516. (void *)last - (void *)here + sizeof(__u32));
  1517. memset(last, 0, size);
  1518. } else if (s->not_found) {
  1519. /* Insert new name. */
  1520. size_t size = EXT4_XATTR_LEN(name_len);
  1521. size_t rest = (void *)last - (void *)here + sizeof(__u32);
  1522. memmove((void *)here + size, here, rest);
  1523. memset(here, 0, size);
  1524. here->e_name_index = i->name_index;
  1525. here->e_name_len = name_len;
  1526. memcpy(here->e_name, i->name, name_len);
  1527. } else {
  1528. /* This is an update, reset value info. */
  1529. here->e_value_inum = 0;
  1530. here->e_value_offs = 0;
  1531. here->e_value_size = 0;
  1532. }
  1533. if (i->value) {
  1534. /* Insert new value. */
  1535. if (in_inode) {
  1536. here->e_value_inum = cpu_to_le32(new_ea_inode->i_ino);
  1537. } else if (i->value_len) {
  1538. void *val = s->base + min_offs - new_size;
  1539. here->e_value_offs = cpu_to_le16(min_offs - new_size);
  1540. if (i->value == EXT4_ZERO_XATTR_VALUE) {
  1541. memset(val, 0, new_size);
  1542. } else {
  1543. memcpy(val, i->value, i->value_len);
  1544. /* Clear padding bytes. */
  1545. memset(val + i->value_len, 0,
  1546. new_size - i->value_len);
  1547. }
  1548. }
  1549. here->e_value_size = cpu_to_le32(i->value_len);
  1550. }
  1551. update_hash:
  1552. if (i->value) {
  1553. __le32 hash = 0;
  1554. /* Entry hash calculation. */
  1555. if (in_inode) {
  1556. __le32 crc32c_hash;
  1557. /*
  1558. * Feed crc32c hash instead of the raw value for entry
  1559. * hash calculation. This is to avoid walking
  1560. * potentially long value buffer again.
  1561. */
  1562. crc32c_hash = cpu_to_le32(
  1563. ext4_xattr_inode_get_hash(new_ea_inode));
  1564. hash = ext4_xattr_hash_entry(here->e_name,
  1565. here->e_name_len,
  1566. &crc32c_hash, 1);
  1567. } else if (is_block) {
  1568. __le32 *value = s->base + le16_to_cpu(
  1569. here->e_value_offs);
  1570. hash = ext4_xattr_hash_entry(here->e_name,
  1571. here->e_name_len, value,
  1572. new_size >> 2);
  1573. }
  1574. here->e_hash = hash;
  1575. }
  1576. if (is_block)
  1577. ext4_xattr_rehash((struct ext4_xattr_header *)s->base);
  1578. ret = 0;
  1579. out:
  1580. iput(old_ea_inode);
  1581. iput(new_ea_inode);
  1582. return ret;
  1583. }
  1584. struct ext4_xattr_block_find {
  1585. struct ext4_xattr_search s;
  1586. struct buffer_head *bh;
  1587. };
  1588. static int
  1589. ext4_xattr_block_find(struct inode *inode, struct ext4_xattr_info *i,
  1590. struct ext4_xattr_block_find *bs)
  1591. {
  1592. struct super_block *sb = inode->i_sb;
  1593. int error;
  1594. ea_idebug(inode, "name=%d.%s, value=%p, value_len=%ld",
  1595. i->name_index, i->name, i->value, (long)i->value_len);
  1596. if (EXT4_I(inode)->i_file_acl) {
  1597. /* The inode already has an extended attribute block. */
  1598. bs->bh = sb_bread(sb, EXT4_I(inode)->i_file_acl);
  1599. error = -EIO;
  1600. if (!bs->bh)
  1601. goto cleanup;
  1602. ea_bdebug(bs->bh, "b_count=%d, refcount=%d",
  1603. atomic_read(&(bs->bh->b_count)),
  1604. le32_to_cpu(BHDR(bs->bh)->h_refcount));
  1605. error = ext4_xattr_check_block(inode, bs->bh);
  1606. if (error)
  1607. goto cleanup;
  1608. /* Find the named attribute. */
  1609. bs->s.base = BHDR(bs->bh);
  1610. bs->s.first = BFIRST(bs->bh);
  1611. bs->s.end = bs->bh->b_data + bs->bh->b_size;
  1612. bs->s.here = bs->s.first;
  1613. error = xattr_find_entry(inode, &bs->s.here, bs->s.end,
  1614. i->name_index, i->name, 1);
  1615. if (error && error != -ENODATA)
  1616. goto cleanup;
  1617. bs->s.not_found = error;
  1618. }
  1619. error = 0;
  1620. cleanup:
  1621. return error;
  1622. }
  1623. static int
  1624. ext4_xattr_block_set(handle_t *handle, struct inode *inode,
  1625. struct ext4_xattr_info *i,
  1626. struct ext4_xattr_block_find *bs)
  1627. {
  1628. struct super_block *sb = inode->i_sb;
  1629. struct buffer_head *new_bh = NULL;
  1630. struct ext4_xattr_search s_copy = bs->s;
  1631. struct ext4_xattr_search *s = &s_copy;
  1632. struct mb_cache_entry *ce = NULL;
  1633. int error = 0;
  1634. struct mb_cache *ea_block_cache = EA_BLOCK_CACHE(inode);
  1635. struct inode *ea_inode = NULL, *tmp_inode;
  1636. size_t old_ea_inode_quota = 0;
  1637. unsigned int ea_ino;
  1638. #define header(x) ((struct ext4_xattr_header *)(x))
  1639. if (s->base) {
  1640. BUFFER_TRACE(bs->bh, "get_write_access");
  1641. error = ext4_journal_get_write_access(handle, bs->bh);
  1642. if (error)
  1643. goto cleanup;
  1644. lock_buffer(bs->bh);
  1645. if (header(s->base)->h_refcount == cpu_to_le32(1)) {
  1646. __u32 hash = le32_to_cpu(BHDR(bs->bh)->h_hash);
  1647. /*
  1648. * This must happen under buffer lock for
  1649. * ext4_xattr_block_set() to reliably detect modified
  1650. * block
  1651. */
  1652. if (ea_block_cache)
  1653. mb_cache_entry_delete(ea_block_cache, hash,
  1654. bs->bh->b_blocknr);
  1655. ea_bdebug(bs->bh, "modifying in-place");
  1656. error = ext4_xattr_set_entry(i, s, handle, inode,
  1657. true /* is_block */);
  1658. ext4_xattr_block_csum_set(inode, bs->bh);
  1659. unlock_buffer(bs->bh);
  1660. if (error == -EFSCORRUPTED)
  1661. goto bad_block;
  1662. if (!error)
  1663. error = ext4_handle_dirty_metadata(handle,
  1664. inode,
  1665. bs->bh);
  1666. if (error)
  1667. goto cleanup;
  1668. goto inserted;
  1669. } else {
  1670. int offset = (char *)s->here - bs->bh->b_data;
  1671. unlock_buffer(bs->bh);
  1672. ea_bdebug(bs->bh, "cloning");
  1673. s->base = kmalloc(bs->bh->b_size, GFP_NOFS);
  1674. error = -ENOMEM;
  1675. if (s->base == NULL)
  1676. goto cleanup;
  1677. memcpy(s->base, BHDR(bs->bh), bs->bh->b_size);
  1678. s->first = ENTRY(header(s->base)+1);
  1679. header(s->base)->h_refcount = cpu_to_le32(1);
  1680. s->here = ENTRY(s->base + offset);
  1681. s->end = s->base + bs->bh->b_size;
  1682. /*
  1683. * If existing entry points to an xattr inode, we need
  1684. * to prevent ext4_xattr_set_entry() from decrementing
  1685. * ref count on it because the reference belongs to the
  1686. * original block. In this case, make the entry look
  1687. * like it has an empty value.
  1688. */
  1689. if (!s->not_found && s->here->e_value_inum) {
  1690. ea_ino = le32_to_cpu(s->here->e_value_inum);
  1691. error = ext4_xattr_inode_iget(inode, ea_ino,
  1692. le32_to_cpu(s->here->e_hash),
  1693. &tmp_inode);
  1694. if (error)
  1695. goto cleanup;
  1696. if (!ext4_test_inode_state(tmp_inode,
  1697. EXT4_STATE_LUSTRE_EA_INODE)) {
  1698. /*
  1699. * Defer quota free call for previous
  1700. * inode until success is guaranteed.
  1701. */
  1702. old_ea_inode_quota = le32_to_cpu(
  1703. s->here->e_value_size);
  1704. }
  1705. iput(tmp_inode);
  1706. s->here->e_value_inum = 0;
  1707. s->here->e_value_size = 0;
  1708. }
  1709. }
  1710. } else {
  1711. /* Allocate a buffer where we construct the new block. */
  1712. s->base = kzalloc(sb->s_blocksize, GFP_NOFS);
  1713. /* assert(header == s->base) */
  1714. error = -ENOMEM;
  1715. if (s->base == NULL)
  1716. goto cleanup;
  1717. header(s->base)->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
  1718. header(s->base)->h_blocks = cpu_to_le32(1);
  1719. header(s->base)->h_refcount = cpu_to_le32(1);
  1720. s->first = ENTRY(header(s->base)+1);
  1721. s->here = ENTRY(header(s->base)+1);
  1722. s->end = s->base + sb->s_blocksize;
  1723. }
  1724. error = ext4_xattr_set_entry(i, s, handle, inode, true /* is_block */);
  1725. if (error == -EFSCORRUPTED)
  1726. goto bad_block;
  1727. if (error)
  1728. goto cleanup;
  1729. if (i->value && s->here->e_value_inum) {
  1730. /*
  1731. * A ref count on ea_inode has been taken as part of the call to
  1732. * ext4_xattr_set_entry() above. We would like to drop this
  1733. * extra ref but we have to wait until the xattr block is
  1734. * initialized and has its own ref count on the ea_inode.
  1735. */
  1736. ea_ino = le32_to_cpu(s->here->e_value_inum);
  1737. error = ext4_xattr_inode_iget(inode, ea_ino,
  1738. le32_to_cpu(s->here->e_hash),
  1739. &ea_inode);
  1740. if (error) {
  1741. ea_inode = NULL;
  1742. goto cleanup;
  1743. }
  1744. }
  1745. inserted:
  1746. if (!IS_LAST_ENTRY(s->first)) {
  1747. new_bh = ext4_xattr_block_cache_find(inode, header(s->base),
  1748. &ce);
  1749. if (new_bh) {
  1750. /* We found an identical block in the cache. */
  1751. if (new_bh == bs->bh)
  1752. ea_bdebug(new_bh, "keeping");
  1753. else {
  1754. u32 ref;
  1755. WARN_ON_ONCE(dquot_initialize_needed(inode));
  1756. /* The old block is released after updating
  1757. the inode. */
  1758. error = dquot_alloc_block(inode,
  1759. EXT4_C2B(EXT4_SB(sb), 1));
  1760. if (error)
  1761. goto cleanup;
  1762. BUFFER_TRACE(new_bh, "get_write_access");
  1763. error = ext4_journal_get_write_access(handle,
  1764. new_bh);
  1765. if (error)
  1766. goto cleanup_dquot;
  1767. lock_buffer(new_bh);
  1768. /*
  1769. * We have to be careful about races with
  1770. * freeing, rehashing or adding references to
  1771. * xattr block. Once we hold buffer lock xattr
  1772. * block's state is stable so we can check
  1773. * whether the block got freed / rehashed or
  1774. * not. Since we unhash mbcache entry under
  1775. * buffer lock when freeing / rehashing xattr
  1776. * block, checking whether entry is still
  1777. * hashed is reliable. Same rules hold for
  1778. * e_reusable handling.
  1779. */
  1780. if (hlist_bl_unhashed(&ce->e_hash_list) ||
  1781. !ce->e_reusable) {
  1782. /*
  1783. * Undo everything and check mbcache
  1784. * again.
  1785. */
  1786. unlock_buffer(new_bh);
  1787. dquot_free_block(inode,
  1788. EXT4_C2B(EXT4_SB(sb),
  1789. 1));
  1790. brelse(new_bh);
  1791. mb_cache_entry_put(ea_block_cache, ce);
  1792. ce = NULL;
  1793. new_bh = NULL;
  1794. goto inserted;
  1795. }
  1796. ref = le32_to_cpu(BHDR(new_bh)->h_refcount) + 1;
  1797. BHDR(new_bh)->h_refcount = cpu_to_le32(ref);
  1798. if (ref >= EXT4_XATTR_REFCOUNT_MAX)
  1799. ce->e_reusable = 0;
  1800. ea_bdebug(new_bh, "reusing; refcount now=%d",
  1801. ref);
  1802. ext4_xattr_block_csum_set(inode, new_bh);
  1803. unlock_buffer(new_bh);
  1804. error = ext4_handle_dirty_metadata(handle,
  1805. inode,
  1806. new_bh);
  1807. if (error)
  1808. goto cleanup_dquot;
  1809. }
  1810. mb_cache_entry_touch(ea_block_cache, ce);
  1811. mb_cache_entry_put(ea_block_cache, ce);
  1812. ce = NULL;
  1813. } else if (bs->bh && s->base == bs->bh->b_data) {
  1814. /* We were modifying this block in-place. */
  1815. ea_bdebug(bs->bh, "keeping this block");
  1816. ext4_xattr_block_cache_insert(ea_block_cache, bs->bh);
  1817. new_bh = bs->bh;
  1818. get_bh(new_bh);
  1819. } else {
  1820. /* We need to allocate a new block */
  1821. ext4_fsblk_t goal, block;
  1822. WARN_ON_ONCE(dquot_initialize_needed(inode));
  1823. goal = ext4_group_first_block_no(sb,
  1824. EXT4_I(inode)->i_block_group);
  1825. /* non-extent files can't have physical blocks past 2^32 */
  1826. if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
  1827. goal = goal & EXT4_MAX_BLOCK_FILE_PHYS;
  1828. block = ext4_new_meta_blocks(handle, inode, goal, 0,
  1829. NULL, &error);
  1830. if (error)
  1831. goto cleanup;
  1832. if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
  1833. BUG_ON(block > EXT4_MAX_BLOCK_FILE_PHYS);
  1834. ea_idebug(inode, "creating block %llu",
  1835. (unsigned long long)block);
  1836. new_bh = sb_getblk(sb, block);
  1837. if (unlikely(!new_bh)) {
  1838. error = -ENOMEM;
  1839. getblk_failed:
  1840. ext4_free_blocks(handle, inode, NULL, block, 1,
  1841. EXT4_FREE_BLOCKS_METADATA);
  1842. goto cleanup;
  1843. }
  1844. error = ext4_xattr_inode_inc_ref_all(handle, inode,
  1845. ENTRY(header(s->base)+1));
  1846. if (error)
  1847. goto getblk_failed;
  1848. if (ea_inode) {
  1849. /* Drop the extra ref on ea_inode. */
  1850. error = ext4_xattr_inode_dec_ref(handle,
  1851. ea_inode);
  1852. if (error)
  1853. ext4_warning_inode(ea_inode,
  1854. "dec ref error=%d",
  1855. error);
  1856. iput(ea_inode);
  1857. ea_inode = NULL;
  1858. }
  1859. lock_buffer(new_bh);
  1860. error = ext4_journal_get_create_access(handle, new_bh);
  1861. if (error) {
  1862. unlock_buffer(new_bh);
  1863. error = -EIO;
  1864. goto getblk_failed;
  1865. }
  1866. memcpy(new_bh->b_data, s->base, new_bh->b_size);
  1867. ext4_xattr_block_csum_set(inode, new_bh);
  1868. set_buffer_uptodate(new_bh);
  1869. unlock_buffer(new_bh);
  1870. ext4_xattr_block_cache_insert(ea_block_cache, new_bh);
  1871. error = ext4_handle_dirty_metadata(handle, inode,
  1872. new_bh);
  1873. if (error)
  1874. goto cleanup;
  1875. }
  1876. }
  1877. if (old_ea_inode_quota)
  1878. ext4_xattr_inode_free_quota(inode, NULL, old_ea_inode_quota);
  1879. /* Update the inode. */
  1880. EXT4_I(inode)->i_file_acl = new_bh ? new_bh->b_blocknr : 0;
  1881. /* Drop the previous xattr block. */
  1882. if (bs->bh && bs->bh != new_bh) {
  1883. struct ext4_xattr_inode_array *ea_inode_array = NULL;
  1884. ext4_xattr_release_block(handle, inode, bs->bh,
  1885. &ea_inode_array,
  1886. 0 /* extra_credits */);
  1887. ext4_xattr_inode_array_free(ea_inode_array);
  1888. }
  1889. error = 0;
  1890. cleanup:
  1891. if (ea_inode) {
  1892. int error2;
  1893. error2 = ext4_xattr_inode_dec_ref(handle, ea_inode);
  1894. if (error2)
  1895. ext4_warning_inode(ea_inode, "dec ref error=%d",
  1896. error2);
  1897. /* If there was an error, revert the quota charge. */
  1898. if (error)
  1899. ext4_xattr_inode_free_quota(inode, ea_inode,
  1900. i_size_read(ea_inode));
  1901. iput(ea_inode);
  1902. }
  1903. if (ce)
  1904. mb_cache_entry_put(ea_block_cache, ce);
  1905. brelse(new_bh);
  1906. if (!(bs->bh && s->base == bs->bh->b_data))
  1907. kfree(s->base);
  1908. return error;
  1909. cleanup_dquot:
  1910. dquot_free_block(inode, EXT4_C2B(EXT4_SB(sb), 1));
  1911. goto cleanup;
  1912. bad_block:
  1913. EXT4_ERROR_INODE(inode, "bad block %llu",
  1914. EXT4_I(inode)->i_file_acl);
  1915. goto cleanup;
  1916. #undef header
  1917. }
  1918. int ext4_xattr_ibody_find(struct inode *inode, struct ext4_xattr_info *i,
  1919. struct ext4_xattr_ibody_find *is)
  1920. {
  1921. struct ext4_xattr_ibody_header *header;
  1922. struct ext4_inode *raw_inode;
  1923. int error;
  1924. if (EXT4_I(inode)->i_extra_isize == 0)
  1925. return 0;
  1926. raw_inode = ext4_raw_inode(&is->iloc);
  1927. header = IHDR(inode, raw_inode);
  1928. is->s.base = is->s.first = IFIRST(header);
  1929. is->s.here = is->s.first;
  1930. is->s.end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
  1931. if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
  1932. error = xattr_check_inode(inode, header, is->s.end);
  1933. if (error)
  1934. return error;
  1935. /* Find the named attribute. */
  1936. error = xattr_find_entry(inode, &is->s.here, is->s.end,
  1937. i->name_index, i->name, 0);
  1938. if (error && error != -ENODATA)
  1939. return error;
  1940. is->s.not_found = error;
  1941. }
  1942. return 0;
  1943. }
  1944. int ext4_xattr_ibody_inline_set(handle_t *handle, struct inode *inode,
  1945. struct ext4_xattr_info *i,
  1946. struct ext4_xattr_ibody_find *is)
  1947. {
  1948. struct ext4_xattr_ibody_header *header;
  1949. struct ext4_xattr_search *s = &is->s;
  1950. int error;
  1951. if (EXT4_I(inode)->i_extra_isize == 0)
  1952. return -ENOSPC;
  1953. error = ext4_xattr_set_entry(i, s, handle, inode, false /* is_block */);
  1954. if (error)
  1955. return error;
  1956. header = IHDR(inode, ext4_raw_inode(&is->iloc));
  1957. if (!IS_LAST_ENTRY(s->first)) {
  1958. header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
  1959. ext4_set_inode_state(inode, EXT4_STATE_XATTR);
  1960. } else {
  1961. header->h_magic = cpu_to_le32(0);
  1962. ext4_clear_inode_state(inode, EXT4_STATE_XATTR);
  1963. }
  1964. return 0;
  1965. }
  1966. static int ext4_xattr_ibody_set(handle_t *handle, struct inode *inode,
  1967. struct ext4_xattr_info *i,
  1968. struct ext4_xattr_ibody_find *is)
  1969. {
  1970. struct ext4_xattr_ibody_header *header;
  1971. struct ext4_xattr_search *s = &is->s;
  1972. int error;
  1973. if (EXT4_I(inode)->i_extra_isize == 0)
  1974. return -ENOSPC;
  1975. error = ext4_xattr_set_entry(i, s, handle, inode, false /* is_block */);
  1976. if (error)
  1977. return error;
  1978. header = IHDR(inode, ext4_raw_inode(&is->iloc));
  1979. if (!IS_LAST_ENTRY(s->first)) {
  1980. header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
  1981. ext4_set_inode_state(inode, EXT4_STATE_XATTR);
  1982. } else {
  1983. header->h_magic = cpu_to_le32(0);
  1984. ext4_clear_inode_state(inode, EXT4_STATE_XATTR);
  1985. }
  1986. return 0;
  1987. }
  1988. static int ext4_xattr_value_same(struct ext4_xattr_search *s,
  1989. struct ext4_xattr_info *i)
  1990. {
  1991. void *value;
  1992. /* When e_value_inum is set the value is stored externally. */
  1993. if (s->here->e_value_inum)
  1994. return 0;
  1995. if (le32_to_cpu(s->here->e_value_size) != i->value_len)
  1996. return 0;
  1997. value = ((void *)s->base) + le16_to_cpu(s->here->e_value_offs);
  1998. return !memcmp(value, i->value, i->value_len);
  1999. }
  2000. static struct buffer_head *ext4_xattr_get_block(struct inode *inode)
  2001. {
  2002. struct buffer_head *bh;
  2003. int error;
  2004. if (!EXT4_I(inode)->i_file_acl)
  2005. return NULL;
  2006. bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
  2007. if (!bh)
  2008. return ERR_PTR(-EIO);
  2009. error = ext4_xattr_check_block(inode, bh);
  2010. if (error) {
  2011. brelse(bh);
  2012. return ERR_PTR(error);
  2013. }
  2014. return bh;
  2015. }
  2016. /*
  2017. * ext4_xattr_set_handle()
  2018. *
  2019. * Create, replace or remove an extended attribute for this inode. Value
  2020. * is NULL to remove an existing extended attribute, and non-NULL to
  2021. * either replace an existing extended attribute, or create a new extended
  2022. * attribute. The flags XATTR_REPLACE and XATTR_CREATE
  2023. * specify that an extended attribute must exist and must not exist
  2024. * previous to the call, respectively.
  2025. *
  2026. * Returns 0, or a negative error number on failure.
  2027. */
  2028. int
  2029. ext4_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index,
  2030. const char *name, const void *value, size_t value_len,
  2031. int flags)
  2032. {
  2033. struct ext4_xattr_info i = {
  2034. .name_index = name_index,
  2035. .name = name,
  2036. .value = value,
  2037. .value_len = value_len,
  2038. .in_inode = 0,
  2039. };
  2040. struct ext4_xattr_ibody_find is = {
  2041. .s = { .not_found = -ENODATA, },
  2042. };
  2043. struct ext4_xattr_block_find bs = {
  2044. .s = { .not_found = -ENODATA, },
  2045. };
  2046. int no_expand;
  2047. int error;
  2048. if (!name)
  2049. return -EINVAL;
  2050. if (strlen(name) > 255)
  2051. return -ERANGE;
  2052. ext4_write_lock_xattr(inode, &no_expand);
  2053. /* Check journal credits under write lock. */
  2054. if (ext4_handle_valid(handle)) {
  2055. struct buffer_head *bh;
  2056. int credits;
  2057. bh = ext4_xattr_get_block(inode);
  2058. if (IS_ERR(bh)) {
  2059. error = PTR_ERR(bh);
  2060. goto cleanup;
  2061. }
  2062. credits = __ext4_xattr_set_credits(inode->i_sb, inode, bh,
  2063. value_len,
  2064. flags & XATTR_CREATE);
  2065. brelse(bh);
  2066. if (!ext4_handle_has_enough_credits(handle, credits)) {
  2067. error = -ENOSPC;
  2068. goto cleanup;
  2069. }
  2070. }
  2071. error = ext4_reserve_inode_write(handle, inode, &is.iloc);
  2072. if (error)
  2073. goto cleanup;
  2074. if (ext4_test_inode_state(inode, EXT4_STATE_NEW)) {
  2075. struct ext4_inode *raw_inode = ext4_raw_inode(&is.iloc);
  2076. memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size);
  2077. ext4_clear_inode_state(inode, EXT4_STATE_NEW);
  2078. }
  2079. error = ext4_xattr_ibody_find(inode, &i, &is);
  2080. if (error)
  2081. goto cleanup;
  2082. if (is.s.not_found)
  2083. error = ext4_xattr_block_find(inode, &i, &bs);
  2084. if (error)
  2085. goto cleanup;
  2086. if (is.s.not_found && bs.s.not_found) {
  2087. error = -ENODATA;
  2088. if (flags & XATTR_REPLACE)
  2089. goto cleanup;
  2090. error = 0;
  2091. if (!value)
  2092. goto cleanup;
  2093. } else {
  2094. error = -EEXIST;
  2095. if (flags & XATTR_CREATE)
  2096. goto cleanup;
  2097. }
  2098. if (!value) {
  2099. if (!is.s.not_found)
  2100. error = ext4_xattr_ibody_set(handle, inode, &i, &is);
  2101. else if (!bs.s.not_found)
  2102. error = ext4_xattr_block_set(handle, inode, &i, &bs);
  2103. } else {
  2104. error = 0;
  2105. /* Xattr value did not change? Save us some work and bail out */
  2106. if (!is.s.not_found && ext4_xattr_value_same(&is.s, &i))
  2107. goto cleanup;
  2108. if (!bs.s.not_found && ext4_xattr_value_same(&bs.s, &i))
  2109. goto cleanup;
  2110. if (ext4_has_feature_ea_inode(inode->i_sb) &&
  2111. (EXT4_XATTR_SIZE(i.value_len) >
  2112. EXT4_XATTR_MIN_LARGE_EA_SIZE(inode->i_sb->s_blocksize)))
  2113. i.in_inode = 1;
  2114. retry_inode:
  2115. error = ext4_xattr_ibody_set(handle, inode, &i, &is);
  2116. if (!error && !bs.s.not_found) {
  2117. i.value = NULL;
  2118. error = ext4_xattr_block_set(handle, inode, &i, &bs);
  2119. } else if (error == -ENOSPC) {
  2120. if (EXT4_I(inode)->i_file_acl && !bs.s.base) {
  2121. brelse(bs.bh);
  2122. bs.bh = NULL;
  2123. error = ext4_xattr_block_find(inode, &i, &bs);
  2124. if (error)
  2125. goto cleanup;
  2126. }
  2127. error = ext4_xattr_block_set(handle, inode, &i, &bs);
  2128. if (!error && !is.s.not_found) {
  2129. i.value = NULL;
  2130. error = ext4_xattr_ibody_set(handle, inode, &i,
  2131. &is);
  2132. } else if (error == -ENOSPC) {
  2133. /*
  2134. * Xattr does not fit in the block, store at
  2135. * external inode if possible.
  2136. */
  2137. if (ext4_has_feature_ea_inode(inode->i_sb) &&
  2138. !i.in_inode) {
  2139. i.in_inode = 1;
  2140. goto retry_inode;
  2141. }
  2142. }
  2143. }
  2144. }
  2145. if (!error) {
  2146. ext4_xattr_update_super_block(handle, inode->i_sb);
  2147. inode->i_ctime = current_time(inode);
  2148. if (!value)
  2149. no_expand = 0;
  2150. error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
  2151. /*
  2152. * The bh is consumed by ext4_mark_iloc_dirty, even with
  2153. * error != 0.
  2154. */
  2155. is.iloc.bh = NULL;
  2156. if (IS_SYNC(inode))
  2157. ext4_handle_sync(handle);
  2158. }
  2159. cleanup:
  2160. brelse(is.iloc.bh);
  2161. brelse(bs.bh);
  2162. ext4_write_unlock_xattr(inode, &no_expand);
  2163. return error;
  2164. }
  2165. int ext4_xattr_set_credits(struct inode *inode, size_t value_len,
  2166. bool is_create, int *credits)
  2167. {
  2168. struct buffer_head *bh;
  2169. int err;
  2170. *credits = 0;
  2171. if (!EXT4_SB(inode->i_sb)->s_journal)
  2172. return 0;
  2173. down_read(&EXT4_I(inode)->xattr_sem);
  2174. bh = ext4_xattr_get_block(inode);
  2175. if (IS_ERR(bh)) {
  2176. err = PTR_ERR(bh);
  2177. } else {
  2178. *credits = __ext4_xattr_set_credits(inode->i_sb, inode, bh,
  2179. value_len, is_create);
  2180. brelse(bh);
  2181. err = 0;
  2182. }
  2183. up_read(&EXT4_I(inode)->xattr_sem);
  2184. return err;
  2185. }
  2186. /*
  2187. * ext4_xattr_set()
  2188. *
  2189. * Like ext4_xattr_set_handle, but start from an inode. This extended
  2190. * attribute modification is a filesystem transaction by itself.
  2191. *
  2192. * Returns 0, or a negative error number on failure.
  2193. */
  2194. int
  2195. ext4_xattr_set(struct inode *inode, int name_index, const char *name,
  2196. const void *value, size_t value_len, int flags)
  2197. {
  2198. handle_t *handle;
  2199. struct super_block *sb = inode->i_sb;
  2200. int error, retries = 0;
  2201. int credits;
  2202. error = dquot_initialize(inode);
  2203. if (error)
  2204. return error;
  2205. retry:
  2206. error = ext4_xattr_set_credits(inode, value_len, flags & XATTR_CREATE,
  2207. &credits);
  2208. if (error)
  2209. return error;
  2210. handle = ext4_journal_start(inode, EXT4_HT_XATTR, credits);
  2211. if (IS_ERR(handle)) {
  2212. error = PTR_ERR(handle);
  2213. } else {
  2214. int error2;
  2215. error = ext4_xattr_set_handle(handle, inode, name_index, name,
  2216. value, value_len, flags);
  2217. error2 = ext4_journal_stop(handle);
  2218. if (error == -ENOSPC &&
  2219. ext4_should_retry_alloc(sb, &retries))
  2220. goto retry;
  2221. if (error == 0)
  2222. error = error2;
  2223. }
  2224. return error;
  2225. }
  2226. /*
  2227. * Shift the EA entries in the inode to create space for the increased
  2228. * i_extra_isize.
  2229. */
  2230. static void ext4_xattr_shift_entries(struct ext4_xattr_entry *entry,
  2231. int value_offs_shift, void *to,
  2232. void *from, size_t n)
  2233. {
  2234. struct ext4_xattr_entry *last = entry;
  2235. int new_offs;
  2236. /* We always shift xattr headers further thus offsets get lower */
  2237. BUG_ON(value_offs_shift > 0);
  2238. /* Adjust the value offsets of the entries */
  2239. for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
  2240. if (!last->e_value_inum && last->e_value_size) {
  2241. new_offs = le16_to_cpu(last->e_value_offs) +
  2242. value_offs_shift;
  2243. last->e_value_offs = cpu_to_le16(new_offs);
  2244. }
  2245. }
  2246. /* Shift the entries by n bytes */
  2247. memmove(to, from, n);
  2248. }
  2249. /*
  2250. * Move xattr pointed to by 'entry' from inode into external xattr block
  2251. */
  2252. static int ext4_xattr_move_to_block(handle_t *handle, struct inode *inode,
  2253. struct ext4_inode *raw_inode,
  2254. struct ext4_xattr_entry *entry)
  2255. {
  2256. struct ext4_xattr_ibody_find *is = NULL;
  2257. struct ext4_xattr_block_find *bs = NULL;
  2258. char *buffer = NULL, *b_entry_name = NULL;
  2259. size_t value_size = le32_to_cpu(entry->e_value_size);
  2260. struct ext4_xattr_info i = {
  2261. .value = NULL,
  2262. .value_len = 0,
  2263. .name_index = entry->e_name_index,
  2264. .in_inode = !!entry->e_value_inum,
  2265. };
  2266. struct ext4_xattr_ibody_header *header = IHDR(inode, raw_inode);
  2267. int error;
  2268. is = kzalloc(sizeof(struct ext4_xattr_ibody_find), GFP_NOFS);
  2269. bs = kzalloc(sizeof(struct ext4_xattr_block_find), GFP_NOFS);
  2270. buffer = kmalloc(value_size, GFP_NOFS);
  2271. b_entry_name = kmalloc(entry->e_name_len + 1, GFP_NOFS);
  2272. if (!is || !bs || !buffer || !b_entry_name) {
  2273. error = -ENOMEM;
  2274. goto out;
  2275. }
  2276. is->s.not_found = -ENODATA;
  2277. bs->s.not_found = -ENODATA;
  2278. is->iloc.bh = NULL;
  2279. bs->bh = NULL;
  2280. /* Save the entry name and the entry value */
  2281. if (entry->e_value_inum) {
  2282. error = ext4_xattr_inode_get(inode, entry, buffer, value_size);
  2283. if (error)
  2284. goto out;
  2285. } else {
  2286. size_t value_offs = le16_to_cpu(entry->e_value_offs);
  2287. memcpy(buffer, (void *)IFIRST(header) + value_offs, value_size);
  2288. }
  2289. memcpy(b_entry_name, entry->e_name, entry->e_name_len);
  2290. b_entry_name[entry->e_name_len] = '\0';
  2291. i.name = b_entry_name;
  2292. error = ext4_get_inode_loc(inode, &is->iloc);
  2293. if (error)
  2294. goto out;
  2295. error = ext4_xattr_ibody_find(inode, &i, is);
  2296. if (error)
  2297. goto out;
  2298. /* Remove the chosen entry from the inode */
  2299. error = ext4_xattr_ibody_set(handle, inode, &i, is);
  2300. if (error)
  2301. goto out;
  2302. i.value = buffer;
  2303. i.value_len = value_size;
  2304. error = ext4_xattr_block_find(inode, &i, bs);
  2305. if (error)
  2306. goto out;
  2307. /* Add entry which was removed from the inode into the block */
  2308. error = ext4_xattr_block_set(handle, inode, &i, bs);
  2309. if (error)
  2310. goto out;
  2311. error = 0;
  2312. out:
  2313. kfree(b_entry_name);
  2314. kfree(buffer);
  2315. if (is)
  2316. brelse(is->iloc.bh);
  2317. if (bs)
  2318. brelse(bs->bh);
  2319. kfree(is);
  2320. kfree(bs);
  2321. return error;
  2322. }
  2323. static int ext4_xattr_make_inode_space(handle_t *handle, struct inode *inode,
  2324. struct ext4_inode *raw_inode,
  2325. int isize_diff, size_t ifree,
  2326. size_t bfree, int *total_ino)
  2327. {
  2328. struct ext4_xattr_ibody_header *header = IHDR(inode, raw_inode);
  2329. struct ext4_xattr_entry *small_entry;
  2330. struct ext4_xattr_entry *entry;
  2331. struct ext4_xattr_entry *last;
  2332. unsigned int entry_size; /* EA entry size */
  2333. unsigned int total_size; /* EA entry size + value size */
  2334. unsigned int min_total_size;
  2335. int error;
  2336. while (isize_diff > ifree) {
  2337. entry = NULL;
  2338. small_entry = NULL;
  2339. min_total_size = ~0U;
  2340. last = IFIRST(header);
  2341. /* Find the entry best suited to be pushed into EA block */
  2342. for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
  2343. /* never move system.data out of the inode */
  2344. if ((last->e_name_len == 4) &&
  2345. (last->e_name_index == EXT4_XATTR_INDEX_SYSTEM) &&
  2346. !memcmp(last->e_name, "data", 4))
  2347. continue;
  2348. total_size = EXT4_XATTR_LEN(last->e_name_len);
  2349. if (!last->e_value_inum)
  2350. total_size += EXT4_XATTR_SIZE(
  2351. le32_to_cpu(last->e_value_size));
  2352. if (total_size <= bfree &&
  2353. total_size < min_total_size) {
  2354. if (total_size + ifree < isize_diff) {
  2355. small_entry = last;
  2356. } else {
  2357. entry = last;
  2358. min_total_size = total_size;
  2359. }
  2360. }
  2361. }
  2362. if (entry == NULL) {
  2363. if (small_entry == NULL)
  2364. return -ENOSPC;
  2365. entry = small_entry;
  2366. }
  2367. entry_size = EXT4_XATTR_LEN(entry->e_name_len);
  2368. total_size = entry_size;
  2369. if (!entry->e_value_inum)
  2370. total_size += EXT4_XATTR_SIZE(
  2371. le32_to_cpu(entry->e_value_size));
  2372. error = ext4_xattr_move_to_block(handle, inode, raw_inode,
  2373. entry);
  2374. if (error)
  2375. return error;
  2376. *total_ino -= entry_size;
  2377. ifree += total_size;
  2378. bfree -= total_size;
  2379. }
  2380. return 0;
  2381. }
  2382. /*
  2383. * Expand an inode by new_extra_isize bytes when EAs are present.
  2384. * Returns 0 on success or negative error number on failure.
  2385. */
  2386. int ext4_expand_extra_isize_ea(struct inode *inode, int new_extra_isize,
  2387. struct ext4_inode *raw_inode, handle_t *handle)
  2388. {
  2389. struct ext4_xattr_ibody_header *header;
  2390. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  2391. static unsigned int mnt_count;
  2392. size_t min_offs;
  2393. size_t ifree, bfree;
  2394. int total_ino;
  2395. void *base, *end;
  2396. int error = 0, tried_min_extra_isize = 0;
  2397. int s_min_extra_isize = le16_to_cpu(sbi->s_es->s_min_extra_isize);
  2398. int isize_diff; /* How much do we need to grow i_extra_isize */
  2399. retry:
  2400. isize_diff = new_extra_isize - EXT4_I(inode)->i_extra_isize;
  2401. if (EXT4_I(inode)->i_extra_isize >= new_extra_isize)
  2402. return 0;
  2403. header = IHDR(inode, raw_inode);
  2404. /*
  2405. * Check if enough free space is available in the inode to shift the
  2406. * entries ahead by new_extra_isize.
  2407. */
  2408. base = IFIRST(header);
  2409. end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
  2410. min_offs = end - base;
  2411. total_ino = sizeof(struct ext4_xattr_ibody_header);
  2412. error = xattr_check_inode(inode, header, end);
  2413. if (error)
  2414. goto cleanup;
  2415. ifree = ext4_xattr_free_space(base, &min_offs, base, &total_ino);
  2416. if (ifree >= isize_diff)
  2417. goto shift;
  2418. /*
  2419. * Enough free space isn't available in the inode, check if
  2420. * EA block can hold new_extra_isize bytes.
  2421. */
  2422. if (EXT4_I(inode)->i_file_acl) {
  2423. struct buffer_head *bh;
  2424. bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
  2425. error = -EIO;
  2426. if (!bh)
  2427. goto cleanup;
  2428. error = ext4_xattr_check_block(inode, bh);
  2429. if (error) {
  2430. brelse(bh);
  2431. goto cleanup;
  2432. }
  2433. base = BHDR(bh);
  2434. end = bh->b_data + bh->b_size;
  2435. min_offs = end - base;
  2436. bfree = ext4_xattr_free_space(BFIRST(bh), &min_offs, base,
  2437. NULL);
  2438. brelse(bh);
  2439. if (bfree + ifree < isize_diff) {
  2440. if (!tried_min_extra_isize && s_min_extra_isize) {
  2441. tried_min_extra_isize++;
  2442. new_extra_isize = s_min_extra_isize;
  2443. goto retry;
  2444. }
  2445. error = -ENOSPC;
  2446. goto cleanup;
  2447. }
  2448. } else {
  2449. bfree = inode->i_sb->s_blocksize;
  2450. }
  2451. error = ext4_xattr_make_inode_space(handle, inode, raw_inode,
  2452. isize_diff, ifree, bfree,
  2453. &total_ino);
  2454. if (error) {
  2455. if (error == -ENOSPC && !tried_min_extra_isize &&
  2456. s_min_extra_isize) {
  2457. tried_min_extra_isize++;
  2458. new_extra_isize = s_min_extra_isize;
  2459. goto retry;
  2460. }
  2461. goto cleanup;
  2462. }
  2463. shift:
  2464. /* Adjust the offsets and shift the remaining entries ahead */
  2465. ext4_xattr_shift_entries(IFIRST(header), EXT4_I(inode)->i_extra_isize
  2466. - new_extra_isize, (void *)raw_inode +
  2467. EXT4_GOOD_OLD_INODE_SIZE + new_extra_isize,
  2468. (void *)header, total_ino);
  2469. EXT4_I(inode)->i_extra_isize = new_extra_isize;
  2470. cleanup:
  2471. if (error && (mnt_count != le16_to_cpu(sbi->s_es->s_mnt_count))) {
  2472. ext4_warning(inode->i_sb, "Unable to expand inode %lu. Delete some EAs or run e2fsck.",
  2473. inode->i_ino);
  2474. mnt_count = le16_to_cpu(sbi->s_es->s_mnt_count);
  2475. }
  2476. return error;
  2477. }
  2478. #define EIA_INCR 16 /* must be 2^n */
  2479. #define EIA_MASK (EIA_INCR - 1)
  2480. /* Add the large xattr @inode into @ea_inode_array for deferred iput().
  2481. * If @ea_inode_array is new or full it will be grown and the old
  2482. * contents copied over.
  2483. */
  2484. static int
  2485. ext4_expand_inode_array(struct ext4_xattr_inode_array **ea_inode_array,
  2486. struct inode *inode)
  2487. {
  2488. if (*ea_inode_array == NULL) {
  2489. /*
  2490. * Start with 15 inodes, so it fits into a power-of-two size.
  2491. * If *ea_inode_array is NULL, this is essentially offsetof()
  2492. */
  2493. (*ea_inode_array) =
  2494. kmalloc(offsetof(struct ext4_xattr_inode_array,
  2495. inodes[EIA_MASK]),
  2496. GFP_NOFS);
  2497. if (*ea_inode_array == NULL)
  2498. return -ENOMEM;
  2499. (*ea_inode_array)->count = 0;
  2500. } else if (((*ea_inode_array)->count & EIA_MASK) == EIA_MASK) {
  2501. /* expand the array once all 15 + n * 16 slots are full */
  2502. struct ext4_xattr_inode_array *new_array = NULL;
  2503. int count = (*ea_inode_array)->count;
  2504. /* if new_array is NULL, this is essentially offsetof() */
  2505. new_array = kmalloc(
  2506. offsetof(struct ext4_xattr_inode_array,
  2507. inodes[count + EIA_INCR]),
  2508. GFP_NOFS);
  2509. if (new_array == NULL)
  2510. return -ENOMEM;
  2511. memcpy(new_array, *ea_inode_array,
  2512. offsetof(struct ext4_xattr_inode_array, inodes[count]));
  2513. kfree(*ea_inode_array);
  2514. *ea_inode_array = new_array;
  2515. }
  2516. (*ea_inode_array)->inodes[(*ea_inode_array)->count++] = inode;
  2517. return 0;
  2518. }
  2519. /*
  2520. * ext4_xattr_delete_inode()
  2521. *
  2522. * Free extended attribute resources associated with this inode. Traverse
  2523. * all entries and decrement reference on any xattr inodes associated with this
  2524. * inode. This is called immediately before an inode is freed. We have exclusive
  2525. * access to the inode. If an orphan inode is deleted it will also release its
  2526. * references on xattr block and xattr inodes.
  2527. */
  2528. int ext4_xattr_delete_inode(handle_t *handle, struct inode *inode,
  2529. struct ext4_xattr_inode_array **ea_inode_array,
  2530. int extra_credits)
  2531. {
  2532. struct buffer_head *bh = NULL;
  2533. struct ext4_xattr_ibody_header *header;
  2534. struct ext4_iloc iloc = { .bh = NULL };
  2535. struct ext4_xattr_entry *entry;
  2536. struct inode *ea_inode;
  2537. int error;
  2538. error = ext4_xattr_ensure_credits(handle, inode, extra_credits,
  2539. NULL /* bh */,
  2540. false /* dirty */,
  2541. false /* block_csum */);
  2542. if (error) {
  2543. EXT4_ERROR_INODE(inode, "ensure credits (error %d)", error);
  2544. goto cleanup;
  2545. }
  2546. if (ext4_has_feature_ea_inode(inode->i_sb) &&
  2547. ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
  2548. error = ext4_get_inode_loc(inode, &iloc);
  2549. if (error) {
  2550. EXT4_ERROR_INODE(inode, "inode loc (error %d)", error);
  2551. goto cleanup;
  2552. }
  2553. error = ext4_journal_get_write_access(handle, iloc.bh);
  2554. if (error) {
  2555. EXT4_ERROR_INODE(inode, "write access (error %d)",
  2556. error);
  2557. goto cleanup;
  2558. }
  2559. header = IHDR(inode, ext4_raw_inode(&iloc));
  2560. if (header->h_magic == cpu_to_le32(EXT4_XATTR_MAGIC))
  2561. ext4_xattr_inode_dec_ref_all(handle, inode, iloc.bh,
  2562. IFIRST(header),
  2563. false /* block_csum */,
  2564. ea_inode_array,
  2565. extra_credits,
  2566. false /* skip_quota */);
  2567. }
  2568. if (EXT4_I(inode)->i_file_acl) {
  2569. bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
  2570. if (!bh) {
  2571. EXT4_ERROR_INODE(inode, "block %llu read error",
  2572. EXT4_I(inode)->i_file_acl);
  2573. error = -EIO;
  2574. goto cleanup;
  2575. }
  2576. error = ext4_xattr_check_block(inode, bh);
  2577. if (error)
  2578. goto cleanup;
  2579. if (ext4_has_feature_ea_inode(inode->i_sb)) {
  2580. for (entry = BFIRST(bh); !IS_LAST_ENTRY(entry);
  2581. entry = EXT4_XATTR_NEXT(entry)) {
  2582. if (!entry->e_value_inum)
  2583. continue;
  2584. error = ext4_xattr_inode_iget(inode,
  2585. le32_to_cpu(entry->e_value_inum),
  2586. le32_to_cpu(entry->e_hash),
  2587. &ea_inode);
  2588. if (error)
  2589. continue;
  2590. ext4_xattr_inode_free_quota(inode, ea_inode,
  2591. le32_to_cpu(entry->e_value_size));
  2592. iput(ea_inode);
  2593. }
  2594. }
  2595. ext4_xattr_release_block(handle, inode, bh, ea_inode_array,
  2596. extra_credits);
  2597. /*
  2598. * Update i_file_acl value in the same transaction that releases
  2599. * block.
  2600. */
  2601. EXT4_I(inode)->i_file_acl = 0;
  2602. error = ext4_mark_inode_dirty(handle, inode);
  2603. if (error) {
  2604. EXT4_ERROR_INODE(inode, "mark inode dirty (error %d)",
  2605. error);
  2606. goto cleanup;
  2607. }
  2608. }
  2609. error = 0;
  2610. cleanup:
  2611. brelse(iloc.bh);
  2612. brelse(bh);
  2613. return error;
  2614. }
  2615. void ext4_xattr_inode_array_free(struct ext4_xattr_inode_array *ea_inode_array)
  2616. {
  2617. int idx;
  2618. if (ea_inode_array == NULL)
  2619. return;
  2620. for (idx = 0; idx < ea_inode_array->count; ++idx)
  2621. iput(ea_inode_array->inodes[idx]);
  2622. kfree(ea_inode_array);
  2623. }
  2624. /*
  2625. * ext4_xattr_block_cache_insert()
  2626. *
  2627. * Create a new entry in the extended attribute block cache, and insert
  2628. * it unless such an entry is already in the cache.
  2629. *
  2630. * Returns 0, or a negative error number on failure.
  2631. */
  2632. static void
  2633. ext4_xattr_block_cache_insert(struct mb_cache *ea_block_cache,
  2634. struct buffer_head *bh)
  2635. {
  2636. struct ext4_xattr_header *header = BHDR(bh);
  2637. __u32 hash = le32_to_cpu(header->h_hash);
  2638. int reusable = le32_to_cpu(header->h_refcount) <
  2639. EXT4_XATTR_REFCOUNT_MAX;
  2640. int error;
  2641. if (!ea_block_cache)
  2642. return;
  2643. error = mb_cache_entry_create(ea_block_cache, GFP_NOFS, hash,
  2644. bh->b_blocknr, reusable);
  2645. if (error) {
  2646. if (error == -EBUSY)
  2647. ea_bdebug(bh, "already in cache");
  2648. } else
  2649. ea_bdebug(bh, "inserting [%x]", (int)hash);
  2650. }
  2651. /*
  2652. * ext4_xattr_cmp()
  2653. *
  2654. * Compare two extended attribute blocks for equality.
  2655. *
  2656. * Returns 0 if the blocks are equal, 1 if they differ, and
  2657. * a negative error number on errors.
  2658. */
  2659. static int
  2660. ext4_xattr_cmp(struct ext4_xattr_header *header1,
  2661. struct ext4_xattr_header *header2)
  2662. {
  2663. struct ext4_xattr_entry *entry1, *entry2;
  2664. entry1 = ENTRY(header1+1);
  2665. entry2 = ENTRY(header2+1);
  2666. while (!IS_LAST_ENTRY(entry1)) {
  2667. if (IS_LAST_ENTRY(entry2))
  2668. return 1;
  2669. if (entry1->e_hash != entry2->e_hash ||
  2670. entry1->e_name_index != entry2->e_name_index ||
  2671. entry1->e_name_len != entry2->e_name_len ||
  2672. entry1->e_value_size != entry2->e_value_size ||
  2673. entry1->e_value_inum != entry2->e_value_inum ||
  2674. memcmp(entry1->e_name, entry2->e_name, entry1->e_name_len))
  2675. return 1;
  2676. if (!entry1->e_value_inum &&
  2677. memcmp((char *)header1 + le16_to_cpu(entry1->e_value_offs),
  2678. (char *)header2 + le16_to_cpu(entry2->e_value_offs),
  2679. le32_to_cpu(entry1->e_value_size)))
  2680. return 1;
  2681. entry1 = EXT4_XATTR_NEXT(entry1);
  2682. entry2 = EXT4_XATTR_NEXT(entry2);
  2683. }
  2684. if (!IS_LAST_ENTRY(entry2))
  2685. return 1;
  2686. return 0;
  2687. }
  2688. /*
  2689. * ext4_xattr_block_cache_find()
  2690. *
  2691. * Find an identical extended attribute block.
  2692. *
  2693. * Returns a pointer to the block found, or NULL if such a block was
  2694. * not found or an error occurred.
  2695. */
  2696. static struct buffer_head *
  2697. ext4_xattr_block_cache_find(struct inode *inode,
  2698. struct ext4_xattr_header *header,
  2699. struct mb_cache_entry **pce)
  2700. {
  2701. __u32 hash = le32_to_cpu(header->h_hash);
  2702. struct mb_cache_entry *ce;
  2703. struct mb_cache *ea_block_cache = EA_BLOCK_CACHE(inode);
  2704. if (!ea_block_cache)
  2705. return NULL;
  2706. if (!header->h_hash)
  2707. return NULL; /* never share */
  2708. ea_idebug(inode, "looking for cached blocks [%x]", (int)hash);
  2709. ce = mb_cache_entry_find_first(ea_block_cache, hash);
  2710. while (ce) {
  2711. struct buffer_head *bh;
  2712. bh = sb_bread(inode->i_sb, ce->e_value);
  2713. if (!bh) {
  2714. EXT4_ERROR_INODE(inode, "block %lu read error",
  2715. (unsigned long)ce->e_value);
  2716. } else if (ext4_xattr_cmp(header, BHDR(bh)) == 0) {
  2717. *pce = ce;
  2718. return bh;
  2719. }
  2720. brelse(bh);
  2721. ce = mb_cache_entry_find_next(ea_block_cache, ce);
  2722. }
  2723. return NULL;
  2724. }
  2725. #define NAME_HASH_SHIFT 5
  2726. #define VALUE_HASH_SHIFT 16
  2727. /*
  2728. * ext4_xattr_hash_entry()
  2729. *
  2730. * Compute the hash of an extended attribute.
  2731. */
  2732. static __le32 ext4_xattr_hash_entry(char *name, size_t name_len, __le32 *value,
  2733. size_t value_count)
  2734. {
  2735. __u32 hash = 0;
  2736. while (name_len--) {
  2737. hash = (hash << NAME_HASH_SHIFT) ^
  2738. (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^
  2739. *name++;
  2740. }
  2741. while (value_count--) {
  2742. hash = (hash << VALUE_HASH_SHIFT) ^
  2743. (hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^
  2744. le32_to_cpu(*value++);
  2745. }
  2746. return cpu_to_le32(hash);
  2747. }
  2748. #undef NAME_HASH_SHIFT
  2749. #undef VALUE_HASH_SHIFT
  2750. #define BLOCK_HASH_SHIFT 16
  2751. /*
  2752. * ext4_xattr_rehash()
  2753. *
  2754. * Re-compute the extended attribute hash value after an entry has changed.
  2755. */
  2756. static void ext4_xattr_rehash(struct ext4_xattr_header *header)
  2757. {
  2758. struct ext4_xattr_entry *here;
  2759. __u32 hash = 0;
  2760. here = ENTRY(header+1);
  2761. while (!IS_LAST_ENTRY(here)) {
  2762. if (!here->e_hash) {
  2763. /* Block is not shared if an entry's hash value == 0 */
  2764. hash = 0;
  2765. break;
  2766. }
  2767. hash = (hash << BLOCK_HASH_SHIFT) ^
  2768. (hash >> (8*sizeof(hash) - BLOCK_HASH_SHIFT)) ^
  2769. le32_to_cpu(here->e_hash);
  2770. here = EXT4_XATTR_NEXT(here);
  2771. }
  2772. header->h_hash = cpu_to_le32(hash);
  2773. }
  2774. #undef BLOCK_HASH_SHIFT
  2775. #define HASH_BUCKET_BITS 10
  2776. struct mb_cache *
  2777. ext4_xattr_create_cache(void)
  2778. {
  2779. return mb_cache_create(HASH_BUCKET_BITS);
  2780. }
  2781. void ext4_xattr_destroy_cache(struct mb_cache *cache)
  2782. {
  2783. if (cache)
  2784. mb_cache_destroy(cache);
  2785. }