xattr.c 84 KB

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