xattr.c 85 KB

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