namei.c 111 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * linux/fs/ext4/namei.c
  4. *
  5. * Copyright (C) 1992, 1993, 1994, 1995
  6. * Remy Card (card@masi.ibp.fr)
  7. * Laboratoire MASI - Institut Blaise Pascal
  8. * Universite Pierre et Marie Curie (Paris VI)
  9. *
  10. * from
  11. *
  12. * linux/fs/minix/namei.c
  13. *
  14. * Copyright (C) 1991, 1992 Linus Torvalds
  15. *
  16. * Big-endian to little-endian byte-swapping/bitmaps by
  17. * David S. Miller (davem@caip.rutgers.edu), 1995
  18. * Directory entry file type support and forward compatibility hooks
  19. * for B-tree directories by Theodore Ts'o (tytso@mit.edu), 1998
  20. * Hash Tree Directory indexing (c)
  21. * Daniel Phillips, 2001
  22. * Hash Tree Directory indexing porting
  23. * Christopher Li, 2002
  24. * Hash Tree Directory indexing cleanup
  25. * Theodore Ts'o, 2002
  26. */
  27. #include <linux/fs.h>
  28. #include <linux/pagemap.h>
  29. #include <linux/time.h>
  30. #include <linux/fcntl.h>
  31. #include <linux/stat.h>
  32. #include <linux/string.h>
  33. #include <linux/quotaops.h>
  34. #include <linux/buffer_head.h>
  35. #include <linux/bio.h>
  36. #include <linux/iversion.h>
  37. #include <linux/unicode.h>
  38. #include "ext4.h"
  39. #include "ext4_jbd2.h"
  40. #include "xattr.h"
  41. #include "acl.h"
  42. #include <trace/events/ext4.h>
  43. /*
  44. * define how far ahead to read directories while searching them.
  45. */
  46. #define NAMEI_RA_CHUNKS 2
  47. #define NAMEI_RA_BLOCKS 4
  48. #define NAMEI_RA_SIZE (NAMEI_RA_CHUNKS * NAMEI_RA_BLOCKS)
  49. static struct buffer_head *ext4_append(handle_t *handle,
  50. struct inode *inode,
  51. ext4_lblk_t *block)
  52. {
  53. struct ext4_map_blocks map;
  54. struct buffer_head *bh;
  55. int err;
  56. if (unlikely(EXT4_SB(inode->i_sb)->s_max_dir_size_kb &&
  57. ((inode->i_size >> 10) >=
  58. EXT4_SB(inode->i_sb)->s_max_dir_size_kb)))
  59. return ERR_PTR(-ENOSPC);
  60. *block = inode->i_size >> inode->i_sb->s_blocksize_bits;
  61. map.m_lblk = *block;
  62. map.m_len = 1;
  63. /*
  64. * We're appending new directory block. Make sure the block is not
  65. * allocated yet, otherwise we will end up corrupting the
  66. * directory.
  67. */
  68. err = ext4_map_blocks(NULL, inode, &map, 0);
  69. if (err < 0)
  70. return ERR_PTR(err);
  71. if (err) {
  72. EXT4_ERROR_INODE(inode, "Logical block already allocated");
  73. return ERR_PTR(-EFSCORRUPTED);
  74. }
  75. bh = ext4_bread(handle, inode, *block, EXT4_GET_BLOCKS_CREATE);
  76. if (IS_ERR(bh))
  77. return bh;
  78. inode->i_size += inode->i_sb->s_blocksize;
  79. EXT4_I(inode)->i_disksize = inode->i_size;
  80. err = ext4_mark_inode_dirty(handle, inode);
  81. if (err)
  82. goto out;
  83. BUFFER_TRACE(bh, "get_write_access");
  84. err = ext4_journal_get_write_access(handle, inode->i_sb, bh,
  85. EXT4_JTR_NONE);
  86. if (err)
  87. goto out;
  88. return bh;
  89. out:
  90. brelse(bh);
  91. ext4_std_error(inode->i_sb, err);
  92. return ERR_PTR(err);
  93. }
  94. static int ext4_dx_csum_verify(struct inode *inode,
  95. struct ext4_dir_entry *dirent);
  96. /*
  97. * Hints to ext4_read_dirblock regarding whether we expect a directory
  98. * block being read to be an index block, or a block containing
  99. * directory entries (and if the latter, whether it was found via a
  100. * logical block in an htree index block). This is used to control
  101. * what sort of sanity checkinig ext4_read_dirblock() will do on the
  102. * directory block read from the storage device. EITHER will means
  103. * the caller doesn't know what kind of directory block will be read,
  104. * so no specific verification will be done.
  105. */
  106. typedef enum {
  107. EITHER, INDEX, DIRENT, DIRENT_HTREE
  108. } dirblock_type_t;
  109. #define ext4_read_dirblock(inode, block, type) \
  110. __ext4_read_dirblock((inode), (block), (type), __func__, __LINE__)
  111. static struct buffer_head *__ext4_read_dirblock(struct inode *inode,
  112. ext4_lblk_t block,
  113. dirblock_type_t type,
  114. const char *func,
  115. unsigned int line)
  116. {
  117. struct buffer_head *bh;
  118. struct ext4_dir_entry *dirent;
  119. int is_dx_block = 0;
  120. if (block >= inode->i_size >> inode->i_blkbits) {
  121. ext4_error_inode(inode, func, line, block,
  122. "Attempting to read directory block (%u) that is past i_size (%llu)",
  123. block, inode->i_size);
  124. return ERR_PTR(-EFSCORRUPTED);
  125. }
  126. if (ext4_simulate_fail(inode->i_sb, EXT4_SIM_DIRBLOCK_EIO))
  127. bh = ERR_PTR(-EIO);
  128. else
  129. bh = ext4_bread(NULL, inode, block, 0);
  130. if (IS_ERR(bh)) {
  131. __ext4_warning(inode->i_sb, func, line,
  132. "inode #%lu: lblock %lu: comm %s: "
  133. "error %ld reading directory block",
  134. inode->i_ino, (unsigned long)block,
  135. current->comm, PTR_ERR(bh));
  136. return bh;
  137. }
  138. /* The first directory block must not be a hole. */
  139. if (!bh && (type == INDEX || type == DIRENT_HTREE || block == 0)) {
  140. ext4_error_inode(inode, func, line, block,
  141. "Directory hole found for htree %s block %u",
  142. (type == INDEX) ? "index" : "leaf", block);
  143. return ERR_PTR(-EFSCORRUPTED);
  144. }
  145. if (!bh)
  146. return NULL;
  147. dirent = (struct ext4_dir_entry *) bh->b_data;
  148. /* Determine whether or not we have an index block */
  149. if (is_dx(inode)) {
  150. if (block == 0)
  151. is_dx_block = 1;
  152. else if (ext4_rec_len_from_disk(dirent->rec_len,
  153. inode->i_sb->s_blocksize) ==
  154. inode->i_sb->s_blocksize)
  155. is_dx_block = 1;
  156. }
  157. if (!is_dx_block && type == INDEX) {
  158. ext4_error_inode(inode, func, line, block,
  159. "directory leaf block found instead of index block");
  160. brelse(bh);
  161. return ERR_PTR(-EFSCORRUPTED);
  162. }
  163. if (!ext4_has_feature_metadata_csum(inode->i_sb) ||
  164. buffer_verified(bh))
  165. return bh;
  166. /*
  167. * An empty leaf block can get mistaken for a index block; for
  168. * this reason, we can only check the index checksum when the
  169. * caller is sure it should be an index block.
  170. */
  171. if (is_dx_block && type == INDEX) {
  172. if (ext4_dx_csum_verify(inode, dirent) &&
  173. !ext4_simulate_fail(inode->i_sb, EXT4_SIM_DIRBLOCK_CRC))
  174. set_buffer_verified(bh);
  175. else {
  176. ext4_error_inode_err(inode, func, line, block,
  177. EFSBADCRC,
  178. "Directory index failed checksum");
  179. brelse(bh);
  180. return ERR_PTR(-EFSBADCRC);
  181. }
  182. }
  183. if (!is_dx_block) {
  184. if (ext4_dirblock_csum_verify(inode, bh) &&
  185. !ext4_simulate_fail(inode->i_sb, EXT4_SIM_DIRBLOCK_CRC))
  186. set_buffer_verified(bh);
  187. else {
  188. ext4_error_inode_err(inode, func, line, block,
  189. EFSBADCRC,
  190. "Directory block failed checksum");
  191. brelse(bh);
  192. return ERR_PTR(-EFSBADCRC);
  193. }
  194. }
  195. return bh;
  196. }
  197. #ifdef DX_DEBUG
  198. #define dxtrace(command) command
  199. #else
  200. #define dxtrace(command)
  201. #endif
  202. struct fake_dirent
  203. {
  204. __le32 inode;
  205. __le16 rec_len;
  206. u8 name_len;
  207. u8 file_type;
  208. };
  209. struct dx_countlimit
  210. {
  211. __le16 limit;
  212. __le16 count;
  213. };
  214. struct dx_entry
  215. {
  216. __le32 hash;
  217. __le32 block;
  218. };
  219. /*
  220. * dx_root_info is laid out so that if it should somehow get overlaid by a
  221. * dirent the two low bits of the hash version will be zero. Therefore, the
  222. * hash version mod 4 should never be 0. Sincerely, the paranoia department.
  223. */
  224. struct dx_root
  225. {
  226. struct fake_dirent dot;
  227. char dot_name[4];
  228. struct fake_dirent dotdot;
  229. char dotdot_name[4];
  230. struct dx_root_info
  231. {
  232. __le32 reserved_zero;
  233. u8 hash_version;
  234. u8 info_length; /* 8 */
  235. u8 indirect_levels;
  236. u8 unused_flags;
  237. }
  238. info;
  239. struct dx_entry entries[];
  240. };
  241. struct dx_node
  242. {
  243. struct fake_dirent fake;
  244. struct dx_entry entries[];
  245. };
  246. struct dx_frame
  247. {
  248. struct buffer_head *bh;
  249. struct dx_entry *entries;
  250. struct dx_entry *at;
  251. };
  252. struct dx_map_entry
  253. {
  254. u32 hash;
  255. u16 offs;
  256. u16 size;
  257. };
  258. /*
  259. * This goes at the end of each htree block.
  260. */
  261. struct dx_tail {
  262. u32 dt_reserved;
  263. __le32 dt_checksum; /* crc32c(uuid+inum+dirblock) */
  264. };
  265. static struct buffer_head * ext4_dx_find_entry(struct inode *dir,
  266. struct ext4_filename *fname,
  267. struct ext4_dir_entry_2 **res_dir);
  268. static int ext4_dx_add_entry(handle_t *handle, struct ext4_filename *fname,
  269. struct inode *dir, struct inode *inode);
  270. /* checksumming functions */
  271. void ext4_initialize_dirent_tail(struct buffer_head *bh,
  272. unsigned int blocksize)
  273. {
  274. struct ext4_dir_entry_tail *t = EXT4_DIRENT_TAIL(bh->b_data, blocksize);
  275. memset(t, 0, sizeof(struct ext4_dir_entry_tail));
  276. t->det_rec_len = ext4_rec_len_to_disk(
  277. sizeof(struct ext4_dir_entry_tail), blocksize);
  278. t->det_reserved_ft = EXT4_FT_DIR_CSUM;
  279. }
  280. /* Walk through a dirent block to find a checksum "dirent" at the tail */
  281. static struct ext4_dir_entry_tail *get_dirent_tail(struct inode *inode,
  282. struct buffer_head *bh)
  283. {
  284. struct ext4_dir_entry_tail *t;
  285. int blocksize = EXT4_BLOCK_SIZE(inode->i_sb);
  286. #ifdef PARANOID
  287. struct ext4_dir_entry *d, *top;
  288. d = (struct ext4_dir_entry *)bh->b_data;
  289. top = (struct ext4_dir_entry *)(bh->b_data +
  290. (blocksize - sizeof(struct ext4_dir_entry_tail)));
  291. while (d < top && ext4_rec_len_from_disk(d->rec_len, blocksize))
  292. d = (struct ext4_dir_entry *)(((void *)d) +
  293. ext4_rec_len_from_disk(d->rec_len, blocksize));
  294. if (d != top)
  295. return NULL;
  296. t = (struct ext4_dir_entry_tail *)d;
  297. #else
  298. t = EXT4_DIRENT_TAIL(bh->b_data, EXT4_BLOCK_SIZE(inode->i_sb));
  299. #endif
  300. if (t->det_reserved_zero1 ||
  301. (ext4_rec_len_from_disk(t->det_rec_len, blocksize) !=
  302. sizeof(struct ext4_dir_entry_tail)) ||
  303. t->det_reserved_zero2 ||
  304. t->det_reserved_ft != EXT4_FT_DIR_CSUM)
  305. return NULL;
  306. return t;
  307. }
  308. static __le32 ext4_dirblock_csum(struct inode *inode, void *dirent, int size)
  309. {
  310. struct ext4_inode_info *ei = EXT4_I(inode);
  311. __u32 csum;
  312. csum = ext4_chksum(ei->i_csum_seed, (__u8 *)dirent, size);
  313. return cpu_to_le32(csum);
  314. }
  315. #define warn_no_space_for_csum(inode) \
  316. __warn_no_space_for_csum((inode), __func__, __LINE__)
  317. static void __warn_no_space_for_csum(struct inode *inode, const char *func,
  318. unsigned int line)
  319. {
  320. __ext4_warning_inode(inode, func, line,
  321. "No space for directory leaf checksum. Please run e2fsck -D.");
  322. }
  323. int ext4_dirblock_csum_verify(struct inode *inode, struct buffer_head *bh)
  324. {
  325. struct ext4_dir_entry_tail *t;
  326. if (!ext4_has_feature_metadata_csum(inode->i_sb))
  327. return 1;
  328. t = get_dirent_tail(inode, bh);
  329. if (!t) {
  330. warn_no_space_for_csum(inode);
  331. return 0;
  332. }
  333. if (t->det_checksum != ext4_dirblock_csum(inode, bh->b_data,
  334. (char *)t - bh->b_data))
  335. return 0;
  336. return 1;
  337. }
  338. static void ext4_dirblock_csum_set(struct inode *inode,
  339. struct buffer_head *bh)
  340. {
  341. struct ext4_dir_entry_tail *t;
  342. if (!ext4_has_feature_metadata_csum(inode->i_sb))
  343. return;
  344. t = get_dirent_tail(inode, bh);
  345. if (!t) {
  346. warn_no_space_for_csum(inode);
  347. return;
  348. }
  349. t->det_checksum = ext4_dirblock_csum(inode, bh->b_data,
  350. (char *)t - bh->b_data);
  351. }
  352. int ext4_handle_dirty_dirblock(handle_t *handle,
  353. struct inode *inode,
  354. struct buffer_head *bh)
  355. {
  356. ext4_dirblock_csum_set(inode, bh);
  357. return ext4_handle_dirty_metadata(handle, inode, bh);
  358. }
  359. static struct dx_countlimit *get_dx_countlimit(struct inode *inode,
  360. struct ext4_dir_entry *dirent,
  361. int *offset)
  362. {
  363. struct ext4_dir_entry *dp;
  364. struct dx_root_info *root;
  365. int count_offset;
  366. int blocksize = EXT4_BLOCK_SIZE(inode->i_sb);
  367. unsigned int rlen = ext4_rec_len_from_disk(dirent->rec_len, blocksize);
  368. if (rlen == blocksize)
  369. count_offset = 8;
  370. else if (rlen == 12) {
  371. dp = (struct ext4_dir_entry *)(((void *)dirent) + 12);
  372. if (ext4_rec_len_from_disk(dp->rec_len, blocksize) != blocksize - 12)
  373. return NULL;
  374. root = (struct dx_root_info *)(((void *)dp + 12));
  375. if (root->reserved_zero ||
  376. root->info_length != sizeof(struct dx_root_info))
  377. return NULL;
  378. count_offset = 32;
  379. } else
  380. return NULL;
  381. if (offset)
  382. *offset = count_offset;
  383. return (struct dx_countlimit *)(((void *)dirent) + count_offset);
  384. }
  385. static __le32 ext4_dx_csum(struct inode *inode, struct ext4_dir_entry *dirent,
  386. int count_offset, int count, struct dx_tail *t)
  387. {
  388. struct ext4_inode_info *ei = EXT4_I(inode);
  389. __u32 csum;
  390. int size;
  391. __u32 dummy_csum = 0;
  392. int offset = offsetof(struct dx_tail, dt_checksum);
  393. size = count_offset + (count * sizeof(struct dx_entry));
  394. csum = ext4_chksum(ei->i_csum_seed, (__u8 *)dirent, size);
  395. csum = ext4_chksum(csum, (__u8 *)t, offset);
  396. csum = ext4_chksum(csum, (__u8 *)&dummy_csum, sizeof(dummy_csum));
  397. return cpu_to_le32(csum);
  398. }
  399. static int ext4_dx_csum_verify(struct inode *inode,
  400. struct ext4_dir_entry *dirent)
  401. {
  402. struct dx_countlimit *c;
  403. struct dx_tail *t;
  404. int count_offset, limit, count;
  405. if (!ext4_has_feature_metadata_csum(inode->i_sb))
  406. return 1;
  407. c = get_dx_countlimit(inode, dirent, &count_offset);
  408. if (!c) {
  409. EXT4_ERROR_INODE(inode, "dir seems corrupt? Run e2fsck -D.");
  410. return 0;
  411. }
  412. limit = le16_to_cpu(c->limit);
  413. count = le16_to_cpu(c->count);
  414. if (count_offset + (limit * sizeof(struct dx_entry)) >
  415. EXT4_BLOCK_SIZE(inode->i_sb) - sizeof(struct dx_tail)) {
  416. warn_no_space_for_csum(inode);
  417. return 0;
  418. }
  419. t = (struct dx_tail *)(((struct dx_entry *)c) + limit);
  420. if (t->dt_checksum != ext4_dx_csum(inode, dirent, count_offset,
  421. count, t))
  422. return 0;
  423. return 1;
  424. }
  425. static void ext4_dx_csum_set(struct inode *inode, struct ext4_dir_entry *dirent)
  426. {
  427. struct dx_countlimit *c;
  428. struct dx_tail *t;
  429. int count_offset, limit, count;
  430. if (!ext4_has_feature_metadata_csum(inode->i_sb))
  431. return;
  432. c = get_dx_countlimit(inode, dirent, &count_offset);
  433. if (!c) {
  434. EXT4_ERROR_INODE(inode, "dir seems corrupt? Run e2fsck -D.");
  435. return;
  436. }
  437. limit = le16_to_cpu(c->limit);
  438. count = le16_to_cpu(c->count);
  439. if (count_offset + (limit * sizeof(struct dx_entry)) >
  440. EXT4_BLOCK_SIZE(inode->i_sb) - sizeof(struct dx_tail)) {
  441. warn_no_space_for_csum(inode);
  442. return;
  443. }
  444. t = (struct dx_tail *)(((struct dx_entry *)c) + limit);
  445. t->dt_checksum = ext4_dx_csum(inode, dirent, count_offset, count, t);
  446. }
  447. static inline int ext4_handle_dirty_dx_node(handle_t *handle,
  448. struct inode *inode,
  449. struct buffer_head *bh)
  450. {
  451. ext4_dx_csum_set(inode, (struct ext4_dir_entry *)bh->b_data);
  452. return ext4_handle_dirty_metadata(handle, inode, bh);
  453. }
  454. /*
  455. * p is at least 6 bytes before the end of page
  456. */
  457. static inline struct ext4_dir_entry_2 *
  458. ext4_next_entry(struct ext4_dir_entry_2 *p, unsigned long blocksize)
  459. {
  460. return (struct ext4_dir_entry_2 *)((char *)p +
  461. ext4_rec_len_from_disk(p->rec_len, blocksize));
  462. }
  463. /*
  464. * Future: use high four bits of block for coalesce-on-delete flags
  465. * Mask them off for now.
  466. */
  467. static inline ext4_lblk_t dx_get_block(struct dx_entry *entry)
  468. {
  469. return le32_to_cpu(entry->block) & 0x0fffffff;
  470. }
  471. static inline void dx_set_block(struct dx_entry *entry, ext4_lblk_t value)
  472. {
  473. entry->block = cpu_to_le32(value);
  474. }
  475. static inline unsigned dx_get_hash(struct dx_entry *entry)
  476. {
  477. return le32_to_cpu(entry->hash);
  478. }
  479. static inline void dx_set_hash(struct dx_entry *entry, unsigned value)
  480. {
  481. entry->hash = cpu_to_le32(value);
  482. }
  483. static inline unsigned dx_get_count(struct dx_entry *entries)
  484. {
  485. return le16_to_cpu(((struct dx_countlimit *) entries)->count);
  486. }
  487. static inline unsigned dx_get_limit(struct dx_entry *entries)
  488. {
  489. return le16_to_cpu(((struct dx_countlimit *) entries)->limit);
  490. }
  491. static inline void dx_set_count(struct dx_entry *entries, unsigned value)
  492. {
  493. ((struct dx_countlimit *) entries)->count = cpu_to_le16(value);
  494. }
  495. static inline void dx_set_limit(struct dx_entry *entries, unsigned value)
  496. {
  497. ((struct dx_countlimit *) entries)->limit = cpu_to_le16(value);
  498. }
  499. static inline unsigned dx_root_limit(struct inode *dir, unsigned infosize)
  500. {
  501. unsigned int entry_space = dir->i_sb->s_blocksize -
  502. ext4_dir_rec_len(1, NULL) -
  503. ext4_dir_rec_len(2, NULL) - infosize;
  504. if (ext4_has_feature_metadata_csum(dir->i_sb))
  505. entry_space -= sizeof(struct dx_tail);
  506. return entry_space / sizeof(struct dx_entry);
  507. }
  508. static inline unsigned dx_node_limit(struct inode *dir)
  509. {
  510. unsigned int entry_space = dir->i_sb->s_blocksize -
  511. ext4_dir_rec_len(0, dir);
  512. if (ext4_has_feature_metadata_csum(dir->i_sb))
  513. entry_space -= sizeof(struct dx_tail);
  514. return entry_space / sizeof(struct dx_entry);
  515. }
  516. /*
  517. * Debug
  518. */
  519. #ifdef DX_DEBUG
  520. static void dx_show_index(char * label, struct dx_entry *entries)
  521. {
  522. int i, n = dx_get_count (entries);
  523. printk(KERN_DEBUG "%s index", label);
  524. for (i = 0; i < n; i++) {
  525. printk(KERN_CONT " %x->%lu",
  526. i ? dx_get_hash(entries + i) : 0,
  527. (unsigned long)dx_get_block(entries + i));
  528. }
  529. printk(KERN_CONT "\n");
  530. }
  531. struct stats
  532. {
  533. unsigned names;
  534. unsigned space;
  535. unsigned bcount;
  536. };
  537. static struct stats dx_show_leaf(struct inode *dir,
  538. struct dx_hash_info *hinfo,
  539. struct ext4_dir_entry_2 *de,
  540. int size, int show_names)
  541. {
  542. unsigned names = 0, space = 0;
  543. char *base = (char *) de;
  544. struct dx_hash_info h = *hinfo;
  545. printk("names: ");
  546. while ((char *) de < base + size)
  547. {
  548. if (de->inode)
  549. {
  550. if (show_names)
  551. {
  552. #ifdef CONFIG_FS_ENCRYPTION
  553. int len;
  554. char *name;
  555. struct fscrypt_str fname_crypto_str =
  556. FSTR_INIT(NULL, 0);
  557. int res = 0;
  558. name = de->name;
  559. len = de->name_len;
  560. if (!IS_ENCRYPTED(dir)) {
  561. /* Directory is not encrypted */
  562. (void) ext4fs_dirhash(dir, de->name,
  563. de->name_len, &h);
  564. printk("%*.s:(U)%x.%u ", len,
  565. name, h.hash,
  566. (unsigned) ((char *) de
  567. - base));
  568. } else {
  569. struct fscrypt_str de_name =
  570. FSTR_INIT(name, len);
  571. /* Directory is encrypted */
  572. res = fscrypt_fname_alloc_buffer(
  573. len, &fname_crypto_str);
  574. if (res)
  575. printk(KERN_WARNING "Error "
  576. "allocating crypto "
  577. "buffer--skipping "
  578. "crypto\n");
  579. res = fscrypt_fname_disk_to_usr(dir,
  580. 0, 0, &de_name,
  581. &fname_crypto_str);
  582. if (res) {
  583. printk(KERN_WARNING "Error "
  584. "converting filename "
  585. "from disk to usr"
  586. "\n");
  587. name = "??";
  588. len = 2;
  589. } else {
  590. name = fname_crypto_str.name;
  591. len = fname_crypto_str.len;
  592. }
  593. if (IS_CASEFOLDED(dir))
  594. h.hash = EXT4_DIRENT_HASH(de);
  595. else
  596. (void) ext4fs_dirhash(dir,
  597. de->name,
  598. de->name_len, &h);
  599. printk("%*.s:(E)%x.%u ", len, name,
  600. h.hash, (unsigned) ((char *) de
  601. - base));
  602. fscrypt_fname_free_buffer(
  603. &fname_crypto_str);
  604. }
  605. #else
  606. int len = de->name_len;
  607. char *name = de->name;
  608. (void) ext4fs_dirhash(dir, de->name,
  609. de->name_len, &h);
  610. printk("%*.s:%x.%u ", len, name, h.hash,
  611. (unsigned) ((char *) de - base));
  612. #endif
  613. }
  614. space += ext4_dir_rec_len(de->name_len, dir);
  615. names++;
  616. }
  617. de = ext4_next_entry(de, size);
  618. }
  619. printk(KERN_CONT "(%i)\n", names);
  620. return (struct stats) { names, space, 1 };
  621. }
  622. struct stats dx_show_entries(struct dx_hash_info *hinfo, struct inode *dir,
  623. struct dx_entry *entries, int levels)
  624. {
  625. unsigned blocksize = dir->i_sb->s_blocksize;
  626. unsigned count = dx_get_count(entries), names = 0, space = 0, i;
  627. unsigned bcount = 0;
  628. struct buffer_head *bh;
  629. printk("%i indexed blocks...\n", count);
  630. for (i = 0; i < count; i++, entries++)
  631. {
  632. ext4_lblk_t block = dx_get_block(entries);
  633. ext4_lblk_t hash = i ? dx_get_hash(entries): 0;
  634. u32 range = i < count - 1? (dx_get_hash(entries + 1) - hash): ~hash;
  635. struct stats stats;
  636. printk("%s%3u:%03u hash %8x/%8x ",levels?"":" ", i, block, hash, range);
  637. bh = ext4_bread(NULL,dir, block, 0);
  638. if (!bh || IS_ERR(bh))
  639. continue;
  640. stats = levels?
  641. dx_show_entries(hinfo, dir, ((struct dx_node *) bh->b_data)->entries, levels - 1):
  642. dx_show_leaf(dir, hinfo, (struct ext4_dir_entry_2 *)
  643. bh->b_data, blocksize, 0);
  644. names += stats.names;
  645. space += stats.space;
  646. bcount += stats.bcount;
  647. brelse(bh);
  648. }
  649. if (bcount)
  650. printk(KERN_DEBUG "%snames %u, fullness %u (%u%%)\n",
  651. levels ? "" : " ", names, space/bcount,
  652. (space/bcount)*100/blocksize);
  653. return (struct stats) { names, space, bcount};
  654. }
  655. /*
  656. * Linear search cross check
  657. */
  658. static inline void htree_rep_invariant_check(struct dx_entry *at,
  659. struct dx_entry *target,
  660. u32 hash, unsigned int n)
  661. {
  662. while (n--) {
  663. dxtrace(printk(KERN_CONT ","));
  664. if (dx_get_hash(++at) > hash) {
  665. at--;
  666. break;
  667. }
  668. }
  669. ASSERT(at == target - 1);
  670. }
  671. #else /* DX_DEBUG */
  672. static inline void htree_rep_invariant_check(struct dx_entry *at,
  673. struct dx_entry *target,
  674. u32 hash, unsigned int n)
  675. {
  676. }
  677. #endif /* DX_DEBUG */
  678. /*
  679. * Probe for a directory leaf block to search.
  680. *
  681. * dx_probe can return ERR_BAD_DX_DIR, which means there was a format
  682. * error in the directory index, and the caller should fall back to
  683. * searching the directory normally. The callers of dx_probe **MUST**
  684. * check for this error code, and make sure it never gets reflected
  685. * back to userspace.
  686. */
  687. static struct dx_frame *
  688. dx_probe(struct ext4_filename *fname, struct inode *dir,
  689. struct dx_hash_info *hinfo, struct dx_frame *frame_in)
  690. {
  691. unsigned count, indirect, level, i;
  692. struct dx_entry *at, *entries, *p, *q, *m;
  693. struct dx_root *root;
  694. struct dx_frame *frame = frame_in;
  695. struct dx_frame *ret_err = ERR_PTR(ERR_BAD_DX_DIR);
  696. u32 hash;
  697. ext4_lblk_t block;
  698. ext4_lblk_t blocks[EXT4_HTREE_LEVEL];
  699. memset(frame_in, 0, EXT4_HTREE_LEVEL * sizeof(frame_in[0]));
  700. frame->bh = ext4_read_dirblock(dir, 0, INDEX);
  701. if (IS_ERR(frame->bh))
  702. return (struct dx_frame *) frame->bh;
  703. root = (struct dx_root *) frame->bh->b_data;
  704. if (root->info.hash_version != DX_HASH_TEA &&
  705. root->info.hash_version != DX_HASH_HALF_MD4 &&
  706. root->info.hash_version != DX_HASH_LEGACY &&
  707. root->info.hash_version != DX_HASH_SIPHASH) {
  708. ext4_warning_inode(dir, "Unrecognised inode hash code %u",
  709. root->info.hash_version);
  710. goto fail;
  711. }
  712. if (ext4_hash_in_dirent(dir)) {
  713. if (root->info.hash_version != DX_HASH_SIPHASH) {
  714. ext4_warning_inode(dir,
  715. "Hash in dirent, but hash is not SIPHASH");
  716. goto fail;
  717. }
  718. } else {
  719. if (root->info.hash_version == DX_HASH_SIPHASH) {
  720. ext4_warning_inode(dir,
  721. "Hash code is SIPHASH, but hash not in dirent");
  722. goto fail;
  723. }
  724. }
  725. if (fname)
  726. hinfo = &fname->hinfo;
  727. hinfo->hash_version = root->info.hash_version;
  728. if (hinfo->hash_version <= DX_HASH_TEA)
  729. hinfo->hash_version += EXT4_SB(dir->i_sb)->s_hash_unsigned;
  730. hinfo->seed = EXT4_SB(dir->i_sb)->s_hash_seed;
  731. /* hash is already computed for encrypted casefolded directory */
  732. if (fname && fname_name(fname) &&
  733. !(IS_ENCRYPTED(dir) && IS_CASEFOLDED(dir))) {
  734. int ret = ext4fs_dirhash(dir, fname_name(fname),
  735. fname_len(fname), hinfo);
  736. if (ret < 0) {
  737. ret_err = ERR_PTR(ret);
  738. goto fail;
  739. }
  740. }
  741. hash = hinfo->hash;
  742. if (root->info.unused_flags & 1) {
  743. ext4_warning_inode(dir, "Unimplemented hash flags: %#06x",
  744. root->info.unused_flags);
  745. goto fail;
  746. }
  747. indirect = root->info.indirect_levels;
  748. if (indirect >= ext4_dir_htree_level(dir->i_sb)) {
  749. ext4_warning(dir->i_sb,
  750. "Directory (ino: %lu) htree depth %#06x exceed"
  751. "supported value", dir->i_ino,
  752. ext4_dir_htree_level(dir->i_sb));
  753. if (ext4_dir_htree_level(dir->i_sb) < EXT4_HTREE_LEVEL) {
  754. ext4_warning(dir->i_sb, "Enable large directory "
  755. "feature to access it");
  756. }
  757. goto fail;
  758. }
  759. entries = (struct dx_entry *)(((char *)&root->info) +
  760. root->info.info_length);
  761. if (dx_get_limit(entries) != dx_root_limit(dir,
  762. root->info.info_length)) {
  763. ext4_warning_inode(dir, "dx entry: limit %u != root limit %u",
  764. dx_get_limit(entries),
  765. dx_root_limit(dir, root->info.info_length));
  766. goto fail;
  767. }
  768. dxtrace(printk("Look up %x", hash));
  769. level = 0;
  770. blocks[0] = 0;
  771. while (1) {
  772. count = dx_get_count(entries);
  773. if (!count || count > dx_get_limit(entries)) {
  774. ext4_warning_inode(dir,
  775. "dx entry: count %u beyond limit %u",
  776. count, dx_get_limit(entries));
  777. goto fail;
  778. }
  779. p = entries + 1;
  780. q = entries + count - 1;
  781. while (p <= q) {
  782. m = p + (q - p) / 2;
  783. dxtrace(printk(KERN_CONT "."));
  784. if (dx_get_hash(m) > hash)
  785. q = m - 1;
  786. else
  787. p = m + 1;
  788. }
  789. htree_rep_invariant_check(entries, p, hash, count - 1);
  790. at = p - 1;
  791. dxtrace(printk(KERN_CONT " %x->%u\n",
  792. at == entries ? 0 : dx_get_hash(at),
  793. dx_get_block(at)));
  794. frame->entries = entries;
  795. frame->at = at;
  796. block = dx_get_block(at);
  797. for (i = 0; i <= level; i++) {
  798. if (blocks[i] == block) {
  799. ext4_warning_inode(dir,
  800. "dx entry: tree cycle block %u points back to block %u",
  801. blocks[level], block);
  802. goto fail;
  803. }
  804. }
  805. if (++level > indirect)
  806. return frame;
  807. blocks[level] = block;
  808. frame++;
  809. frame->bh = ext4_read_dirblock(dir, block, INDEX);
  810. if (IS_ERR(frame->bh)) {
  811. ret_err = (struct dx_frame *) frame->bh;
  812. frame->bh = NULL;
  813. goto fail;
  814. }
  815. entries = ((struct dx_node *) frame->bh->b_data)->entries;
  816. if (dx_get_limit(entries) != dx_node_limit(dir)) {
  817. ext4_warning_inode(dir,
  818. "dx entry: limit %u != node limit %u",
  819. dx_get_limit(entries), dx_node_limit(dir));
  820. goto fail;
  821. }
  822. }
  823. fail:
  824. while (frame >= frame_in) {
  825. brelse(frame->bh);
  826. frame--;
  827. }
  828. if (ret_err == ERR_PTR(ERR_BAD_DX_DIR))
  829. ext4_warning_inode(dir,
  830. "Corrupt directory, running e2fsck is recommended");
  831. return ret_err;
  832. }
  833. static void dx_release(struct dx_frame *frames)
  834. {
  835. struct dx_root_info *info;
  836. int i;
  837. unsigned int indirect_levels;
  838. if (frames[0].bh == NULL)
  839. return;
  840. info = &((struct dx_root *)frames[0].bh->b_data)->info;
  841. /* save local copy, "info" may be freed after brelse() */
  842. indirect_levels = info->indirect_levels;
  843. for (i = 0; i <= indirect_levels; i++) {
  844. if (frames[i].bh == NULL)
  845. break;
  846. brelse(frames[i].bh);
  847. frames[i].bh = NULL;
  848. }
  849. }
  850. /*
  851. * This function increments the frame pointer to search the next leaf
  852. * block, and reads in the necessary intervening nodes if the search
  853. * should be necessary. Whether or not the search is necessary is
  854. * controlled by the hash parameter. If the hash value is even, then
  855. * the search is only continued if the next block starts with that
  856. * hash value. This is used if we are searching for a specific file.
  857. *
  858. * If the hash value is HASH_NB_ALWAYS, then always go to the next block.
  859. *
  860. * This function returns 1 if the caller should continue to search,
  861. * or 0 if it should not. If there is an error reading one of the
  862. * index blocks, it will a negative error code.
  863. *
  864. * If start_hash is non-null, it will be filled in with the starting
  865. * hash of the next page.
  866. */
  867. static int ext4_htree_next_block(struct inode *dir, __u32 hash,
  868. struct dx_frame *frame,
  869. struct dx_frame *frames,
  870. __u32 *start_hash)
  871. {
  872. struct dx_frame *p;
  873. struct buffer_head *bh;
  874. int num_frames = 0;
  875. __u32 bhash;
  876. p = frame;
  877. /*
  878. * Find the next leaf page by incrementing the frame pointer.
  879. * If we run out of entries in the interior node, loop around and
  880. * increment pointer in the parent node. When we break out of
  881. * this loop, num_frames indicates the number of interior
  882. * nodes need to be read.
  883. */
  884. while (1) {
  885. if (++(p->at) < p->entries + dx_get_count(p->entries))
  886. break;
  887. if (p == frames)
  888. return 0;
  889. num_frames++;
  890. p--;
  891. }
  892. /*
  893. * If the hash is 1, then continue only if the next page has a
  894. * continuation hash of any value. This is used for readdir
  895. * handling. Otherwise, check to see if the hash matches the
  896. * desired continuation hash. If it doesn't, return since
  897. * there's no point to read in the successive index pages.
  898. */
  899. bhash = dx_get_hash(p->at);
  900. if (start_hash)
  901. *start_hash = bhash;
  902. if ((hash & 1) == 0) {
  903. if ((bhash & ~1) != hash)
  904. return 0;
  905. }
  906. /*
  907. * If the hash is HASH_NB_ALWAYS, we always go to the next
  908. * block so no check is necessary
  909. */
  910. while (num_frames--) {
  911. bh = ext4_read_dirblock(dir, dx_get_block(p->at), INDEX);
  912. if (IS_ERR(bh))
  913. return PTR_ERR(bh);
  914. p++;
  915. brelse(p->bh);
  916. p->bh = bh;
  917. p->at = p->entries = ((struct dx_node *) bh->b_data)->entries;
  918. }
  919. return 1;
  920. }
  921. /*
  922. * This function fills a red-black tree with information from a
  923. * directory block. It returns the number directory entries loaded
  924. * into the tree. If there is an error it is returned in err.
  925. */
  926. static int htree_dirblock_to_tree(struct file *dir_file,
  927. struct inode *dir, ext4_lblk_t block,
  928. struct dx_hash_info *hinfo,
  929. __u32 start_hash, __u32 start_minor_hash)
  930. {
  931. struct buffer_head *bh;
  932. struct ext4_dir_entry_2 *de, *top;
  933. int err = 0, count = 0;
  934. struct fscrypt_str fname_crypto_str = FSTR_INIT(NULL, 0), tmp_str;
  935. int csum = ext4_has_feature_metadata_csum(dir->i_sb);
  936. dxtrace(printk(KERN_INFO "In htree dirblock_to_tree: block %lu\n",
  937. (unsigned long)block));
  938. bh = ext4_read_dirblock(dir, block, DIRENT_HTREE);
  939. if (IS_ERR(bh))
  940. return PTR_ERR(bh);
  941. de = (struct ext4_dir_entry_2 *) bh->b_data;
  942. /* csum entries are not larger in the casefolded encrypted case */
  943. top = (struct ext4_dir_entry_2 *) ((char *) de +
  944. dir->i_sb->s_blocksize -
  945. ext4_dir_rec_len(0,
  946. csum ? NULL : dir));
  947. /* Check if the directory is encrypted */
  948. if (IS_ENCRYPTED(dir)) {
  949. err = fscrypt_prepare_readdir(dir);
  950. if (err < 0) {
  951. brelse(bh);
  952. return err;
  953. }
  954. err = fscrypt_fname_alloc_buffer(EXT4_NAME_LEN,
  955. &fname_crypto_str);
  956. if (err < 0) {
  957. brelse(bh);
  958. return err;
  959. }
  960. }
  961. for (; de < top; de = ext4_next_entry(de, dir->i_sb->s_blocksize)) {
  962. if (ext4_check_dir_entry(dir, NULL, de, bh,
  963. bh->b_data, bh->b_size,
  964. EXT4_LBLK_TO_B(dir, block)
  965. + ((char *)de - bh->b_data))) {
  966. /* silently ignore the rest of the block */
  967. break;
  968. }
  969. if (ext4_hash_in_dirent(dir)) {
  970. if (de->name_len && de->inode) {
  971. hinfo->hash = EXT4_DIRENT_HASH(de);
  972. hinfo->minor_hash = EXT4_DIRENT_MINOR_HASH(de);
  973. } else {
  974. hinfo->hash = 0;
  975. hinfo->minor_hash = 0;
  976. }
  977. } else {
  978. err = ext4fs_dirhash(dir, de->name,
  979. de->name_len, hinfo);
  980. if (err < 0) {
  981. count = err;
  982. goto errout;
  983. }
  984. }
  985. if ((hinfo->hash < start_hash) ||
  986. ((hinfo->hash == start_hash) &&
  987. (hinfo->minor_hash < start_minor_hash)))
  988. continue;
  989. if (de->inode == 0)
  990. continue;
  991. if (!IS_ENCRYPTED(dir)) {
  992. tmp_str.name = de->name;
  993. tmp_str.len = de->name_len;
  994. err = ext4_htree_store_dirent(dir_file,
  995. hinfo->hash, hinfo->minor_hash, de,
  996. &tmp_str);
  997. } else {
  998. int save_len = fname_crypto_str.len;
  999. struct fscrypt_str de_name = FSTR_INIT(de->name,
  1000. de->name_len);
  1001. /* Directory is encrypted */
  1002. err = fscrypt_fname_disk_to_usr(dir, hinfo->hash,
  1003. hinfo->minor_hash, &de_name,
  1004. &fname_crypto_str);
  1005. if (err) {
  1006. count = err;
  1007. goto errout;
  1008. }
  1009. err = ext4_htree_store_dirent(dir_file,
  1010. hinfo->hash, hinfo->minor_hash, de,
  1011. &fname_crypto_str);
  1012. fname_crypto_str.len = save_len;
  1013. }
  1014. if (err != 0) {
  1015. count = err;
  1016. goto errout;
  1017. }
  1018. count++;
  1019. }
  1020. errout:
  1021. brelse(bh);
  1022. fscrypt_fname_free_buffer(&fname_crypto_str);
  1023. return count;
  1024. }
  1025. /*
  1026. * This function fills a red-black tree with information from a
  1027. * directory. We start scanning the directory in hash order, starting
  1028. * at start_hash and start_minor_hash.
  1029. *
  1030. * This function returns the number of entries inserted into the tree,
  1031. * or a negative error code.
  1032. */
  1033. int ext4_htree_fill_tree(struct file *dir_file, __u32 start_hash,
  1034. __u32 start_minor_hash, __u32 *next_hash)
  1035. {
  1036. struct dx_hash_info hinfo;
  1037. struct ext4_dir_entry_2 *de;
  1038. struct dx_frame frames[EXT4_HTREE_LEVEL], *frame;
  1039. struct inode *dir;
  1040. ext4_lblk_t block;
  1041. int count = 0;
  1042. int ret, err;
  1043. __u32 hashval;
  1044. struct fscrypt_str tmp_str;
  1045. dxtrace(printk(KERN_DEBUG "In htree_fill_tree, start hash: %x:%x\n",
  1046. start_hash, start_minor_hash));
  1047. dir = file_inode(dir_file);
  1048. if (!(ext4_test_inode_flag(dir, EXT4_INODE_INDEX))) {
  1049. if (ext4_hash_in_dirent(dir))
  1050. hinfo.hash_version = DX_HASH_SIPHASH;
  1051. else
  1052. hinfo.hash_version =
  1053. EXT4_SB(dir->i_sb)->s_def_hash_version;
  1054. if (hinfo.hash_version <= DX_HASH_TEA)
  1055. hinfo.hash_version +=
  1056. EXT4_SB(dir->i_sb)->s_hash_unsigned;
  1057. hinfo.seed = EXT4_SB(dir->i_sb)->s_hash_seed;
  1058. if (ext4_has_inline_data(dir)) {
  1059. int has_inline_data = 1;
  1060. count = ext4_inlinedir_to_tree(dir_file, dir, 0,
  1061. &hinfo, start_hash,
  1062. start_minor_hash,
  1063. &has_inline_data);
  1064. if (has_inline_data) {
  1065. *next_hash = ~0;
  1066. return count;
  1067. }
  1068. }
  1069. count = htree_dirblock_to_tree(dir_file, dir, 0, &hinfo,
  1070. start_hash, start_minor_hash);
  1071. *next_hash = ~0;
  1072. return count;
  1073. }
  1074. hinfo.hash = start_hash;
  1075. hinfo.minor_hash = 0;
  1076. frame = dx_probe(NULL, dir, &hinfo, frames);
  1077. if (IS_ERR(frame))
  1078. return PTR_ERR(frame);
  1079. /* Add '.' and '..' from the htree header */
  1080. if (!start_hash && !start_minor_hash) {
  1081. de = (struct ext4_dir_entry_2 *) frames[0].bh->b_data;
  1082. tmp_str.name = de->name;
  1083. tmp_str.len = de->name_len;
  1084. err = ext4_htree_store_dirent(dir_file, 0, 0,
  1085. de, &tmp_str);
  1086. if (err != 0)
  1087. goto errout;
  1088. count++;
  1089. }
  1090. if (start_hash < 2 || (start_hash ==2 && start_minor_hash==0)) {
  1091. de = (struct ext4_dir_entry_2 *) frames[0].bh->b_data;
  1092. de = ext4_next_entry(de, dir->i_sb->s_blocksize);
  1093. tmp_str.name = de->name;
  1094. tmp_str.len = de->name_len;
  1095. err = ext4_htree_store_dirent(dir_file, 2, 0,
  1096. de, &tmp_str);
  1097. if (err != 0)
  1098. goto errout;
  1099. count++;
  1100. }
  1101. while (1) {
  1102. if (fatal_signal_pending(current)) {
  1103. err = -ERESTARTSYS;
  1104. goto errout;
  1105. }
  1106. cond_resched();
  1107. block = dx_get_block(frame->at);
  1108. ret = htree_dirblock_to_tree(dir_file, dir, block, &hinfo,
  1109. start_hash, start_minor_hash);
  1110. if (ret < 0) {
  1111. err = ret;
  1112. goto errout;
  1113. }
  1114. count += ret;
  1115. hashval = ~0;
  1116. ret = ext4_htree_next_block(dir, HASH_NB_ALWAYS,
  1117. frame, frames, &hashval);
  1118. *next_hash = hashval;
  1119. if (ret < 0) {
  1120. err = ret;
  1121. goto errout;
  1122. }
  1123. /*
  1124. * Stop if: (a) there are no more entries, or
  1125. * (b) we have inserted at least one entry and the
  1126. * next hash value is not a continuation
  1127. */
  1128. if ((ret == 0) ||
  1129. (count && ((hashval & 1) == 0)))
  1130. break;
  1131. }
  1132. dx_release(frames);
  1133. dxtrace(printk(KERN_DEBUG "Fill tree: returned %d entries, "
  1134. "next hash: %x\n", count, *next_hash));
  1135. return count;
  1136. errout:
  1137. dx_release(frames);
  1138. return (err);
  1139. }
  1140. static inline int search_dirblock(struct buffer_head *bh,
  1141. struct inode *dir,
  1142. struct ext4_filename *fname,
  1143. unsigned int offset,
  1144. struct ext4_dir_entry_2 **res_dir)
  1145. {
  1146. return ext4_search_dir(bh, bh->b_data, dir->i_sb->s_blocksize, dir,
  1147. fname, offset, res_dir);
  1148. }
  1149. /*
  1150. * Directory block splitting, compacting
  1151. */
  1152. /*
  1153. * Create map of hash values, offsets, and sizes, stored at end of block.
  1154. * Returns number of entries mapped.
  1155. */
  1156. static int dx_make_map(struct inode *dir, struct buffer_head *bh,
  1157. struct dx_hash_info *hinfo,
  1158. struct dx_map_entry *map_tail)
  1159. {
  1160. int count = 0;
  1161. struct ext4_dir_entry_2 *de = (struct ext4_dir_entry_2 *)bh->b_data;
  1162. unsigned int buflen = bh->b_size;
  1163. char *base = bh->b_data;
  1164. struct dx_hash_info h = *hinfo;
  1165. int blocksize = EXT4_BLOCK_SIZE(dir->i_sb);
  1166. if (ext4_has_feature_metadata_csum(dir->i_sb))
  1167. buflen -= sizeof(struct ext4_dir_entry_tail);
  1168. while ((char *) de < base + buflen) {
  1169. if (ext4_check_dir_entry(dir, NULL, de, bh, base, buflen,
  1170. ((char *)de) - base))
  1171. return -EFSCORRUPTED;
  1172. if (de->name_len && de->inode) {
  1173. if (ext4_hash_in_dirent(dir))
  1174. h.hash = EXT4_DIRENT_HASH(de);
  1175. else {
  1176. int err = ext4fs_dirhash(dir, de->name,
  1177. de->name_len, &h);
  1178. if (err < 0)
  1179. return err;
  1180. }
  1181. map_tail--;
  1182. map_tail->hash = h.hash;
  1183. map_tail->offs = ((char *) de - base)>>2;
  1184. map_tail->size = ext4_rec_len_from_disk(de->rec_len,
  1185. blocksize);
  1186. count++;
  1187. cond_resched();
  1188. }
  1189. de = ext4_next_entry(de, blocksize);
  1190. }
  1191. return count;
  1192. }
  1193. /* Sort map by hash value */
  1194. static void dx_sort_map (struct dx_map_entry *map, unsigned count)
  1195. {
  1196. struct dx_map_entry *p, *q, *top = map + count - 1;
  1197. int more;
  1198. /* Combsort until bubble sort doesn't suck */
  1199. while (count > 2) {
  1200. count = count*10/13;
  1201. if (count - 9 < 2) /* 9, 10 -> 11 */
  1202. count = 11;
  1203. for (p = top, q = p - count; q >= map; p--, q--)
  1204. if (p->hash < q->hash)
  1205. swap(*p, *q);
  1206. }
  1207. /* Garden variety bubble sort */
  1208. do {
  1209. more = 0;
  1210. q = top;
  1211. while (q-- > map) {
  1212. if (q[1].hash >= q[0].hash)
  1213. continue;
  1214. swap(*(q+1), *q);
  1215. more = 1;
  1216. }
  1217. } while(more);
  1218. }
  1219. static void dx_insert_block(struct dx_frame *frame, u32 hash, ext4_lblk_t block)
  1220. {
  1221. struct dx_entry *entries = frame->entries;
  1222. struct dx_entry *old = frame->at, *new = old + 1;
  1223. int count = dx_get_count(entries);
  1224. ASSERT(count < dx_get_limit(entries));
  1225. ASSERT(old < entries + count);
  1226. memmove(new + 1, new, (char *)(entries + count) - (char *)(new));
  1227. dx_set_hash(new, hash);
  1228. dx_set_block(new, block);
  1229. dx_set_count(entries, count + 1);
  1230. }
  1231. #if IS_ENABLED(CONFIG_UNICODE)
  1232. int ext4_fname_setup_ci_filename(struct inode *dir, const struct qstr *iname,
  1233. struct ext4_filename *name)
  1234. {
  1235. struct qstr *cf_name = &name->cf_name;
  1236. unsigned char *buf;
  1237. struct dx_hash_info *hinfo = &name->hinfo;
  1238. int len;
  1239. if (!IS_CASEFOLDED(dir) ||
  1240. (IS_ENCRYPTED(dir) && !fscrypt_has_encryption_key(dir))) {
  1241. cf_name->name = NULL;
  1242. return 0;
  1243. }
  1244. buf = kmalloc(EXT4_NAME_LEN, GFP_NOFS);
  1245. if (!buf)
  1246. return -ENOMEM;
  1247. len = utf8_casefold(dir->i_sb->s_encoding, iname, buf, EXT4_NAME_LEN);
  1248. if (len <= 0) {
  1249. kfree(buf);
  1250. buf = NULL;
  1251. }
  1252. cf_name->name = buf;
  1253. cf_name->len = (unsigned) len;
  1254. if (!IS_ENCRYPTED(dir))
  1255. return 0;
  1256. hinfo->hash_version = DX_HASH_SIPHASH;
  1257. hinfo->seed = NULL;
  1258. if (cf_name->name)
  1259. return ext4fs_dirhash(dir, cf_name->name, cf_name->len, hinfo);
  1260. else
  1261. return ext4fs_dirhash(dir, iname->name, iname->len, hinfo);
  1262. }
  1263. #endif
  1264. /*
  1265. * Test whether a directory entry matches the filename being searched for.
  1266. *
  1267. * Return: %true if the directory entry matches, otherwise %false.
  1268. */
  1269. static bool ext4_match(struct inode *parent,
  1270. const struct ext4_filename *fname,
  1271. struct ext4_dir_entry_2 *de)
  1272. {
  1273. struct fscrypt_name f;
  1274. if (!de->inode)
  1275. return false;
  1276. f.usr_fname = fname->usr_fname;
  1277. f.disk_name = fname->disk_name;
  1278. #ifdef CONFIG_FS_ENCRYPTION
  1279. f.crypto_buf = fname->crypto_buf;
  1280. #endif
  1281. #if IS_ENABLED(CONFIG_UNICODE)
  1282. if (IS_CASEFOLDED(parent) &&
  1283. (!IS_ENCRYPTED(parent) || fscrypt_has_encryption_key(parent))) {
  1284. /*
  1285. * Just checking IS_ENCRYPTED(parent) below is not
  1286. * sufficient to decide whether one can use the hash for
  1287. * skipping the string comparison, because the key might
  1288. * have been added right after
  1289. * ext4_fname_setup_ci_filename(). In this case, a hash
  1290. * mismatch will be a false negative. Therefore, make
  1291. * sure cf_name was properly initialized before
  1292. * considering the calculated hash.
  1293. */
  1294. if (sb_no_casefold_compat_fallback(parent->i_sb) &&
  1295. IS_ENCRYPTED(parent) && fname->cf_name.name &&
  1296. (fname->hinfo.hash != EXT4_DIRENT_HASH(de) ||
  1297. fname->hinfo.minor_hash != EXT4_DIRENT_MINOR_HASH(de)))
  1298. return false;
  1299. /*
  1300. * Treat comparison errors as not a match. The
  1301. * only case where it happens is on a disk
  1302. * corruption or ENOMEM.
  1303. */
  1304. return generic_ci_match(parent, fname->usr_fname,
  1305. &fname->cf_name, de->name,
  1306. de->name_len) > 0;
  1307. }
  1308. #endif
  1309. return fscrypt_match_name(&f, de->name, de->name_len);
  1310. }
  1311. /*
  1312. * Returns 0 if not found, -EFSCORRUPTED on failure, and 1 on success
  1313. */
  1314. int ext4_search_dir(struct buffer_head *bh, char *search_buf, int buf_size,
  1315. struct inode *dir, struct ext4_filename *fname,
  1316. unsigned int offset, struct ext4_dir_entry_2 **res_dir)
  1317. {
  1318. struct ext4_dir_entry_2 * de;
  1319. char * dlimit;
  1320. int de_len;
  1321. de = (struct ext4_dir_entry_2 *)search_buf;
  1322. dlimit = search_buf + buf_size;
  1323. while ((char *) de < dlimit - EXT4_BASE_DIR_LEN) {
  1324. /* this code is executed quadratically often */
  1325. /* do minimal checking `by hand' */
  1326. if (de->name + de->name_len <= dlimit &&
  1327. ext4_match(dir, fname, de)) {
  1328. /* found a match - just to be sure, do
  1329. * a full check */
  1330. if (ext4_check_dir_entry(dir, NULL, de, bh, search_buf,
  1331. buf_size, offset))
  1332. return -EFSCORRUPTED;
  1333. *res_dir = de;
  1334. return 1;
  1335. }
  1336. /* prevent looping on a bad block */
  1337. de_len = ext4_rec_len_from_disk(de->rec_len,
  1338. dir->i_sb->s_blocksize);
  1339. if (de_len <= 0)
  1340. return -EFSCORRUPTED;
  1341. offset += de_len;
  1342. de = (struct ext4_dir_entry_2 *) ((char *) de + de_len);
  1343. }
  1344. return 0;
  1345. }
  1346. static int is_dx_internal_node(struct inode *dir, ext4_lblk_t block,
  1347. struct ext4_dir_entry *de)
  1348. {
  1349. struct super_block *sb = dir->i_sb;
  1350. if (!is_dx(dir))
  1351. return 0;
  1352. if (block == 0)
  1353. return 1;
  1354. if (de->inode == 0 &&
  1355. ext4_rec_len_from_disk(de->rec_len, sb->s_blocksize) ==
  1356. sb->s_blocksize)
  1357. return 1;
  1358. return 0;
  1359. }
  1360. /*
  1361. * __ext4_find_entry()
  1362. *
  1363. * finds an entry in the specified directory with the wanted name. It
  1364. * returns the cache buffer in which the entry was found, and the entry
  1365. * itself (as a parameter - res_dir). It does NOT read the inode of the
  1366. * entry - you'll have to do that yourself if you want to.
  1367. *
  1368. * The returned buffer_head has ->b_count elevated. The caller is expected
  1369. * to brelse() it when appropriate.
  1370. */
  1371. static struct buffer_head *__ext4_find_entry(struct inode *dir,
  1372. struct ext4_filename *fname,
  1373. struct ext4_dir_entry_2 **res_dir,
  1374. int *inlined)
  1375. {
  1376. struct super_block *sb;
  1377. struct buffer_head *bh_use[NAMEI_RA_SIZE];
  1378. struct buffer_head *bh, *ret = NULL;
  1379. ext4_lblk_t start, block;
  1380. const u8 *name = fname->usr_fname->name;
  1381. size_t ra_max = 0; /* Number of bh's in the readahead
  1382. buffer, bh_use[] */
  1383. size_t ra_ptr = 0; /* Current index into readahead
  1384. buffer */
  1385. ext4_lblk_t nblocks;
  1386. int i, namelen, retval;
  1387. *res_dir = NULL;
  1388. sb = dir->i_sb;
  1389. namelen = fname->usr_fname->len;
  1390. if (namelen > EXT4_NAME_LEN)
  1391. return NULL;
  1392. if (ext4_has_inline_data(dir)) {
  1393. int has_inline_data = 1;
  1394. ret = ext4_find_inline_entry(dir, fname, res_dir,
  1395. &has_inline_data);
  1396. if (inlined)
  1397. *inlined = has_inline_data;
  1398. if (has_inline_data || IS_ERR(ret))
  1399. goto cleanup_and_exit;
  1400. }
  1401. if ((namelen <= 2) && (name[0] == '.') &&
  1402. (name[1] == '.' || name[1] == '\0')) {
  1403. /*
  1404. * "." or ".." will only be in the first block
  1405. * NFS may look up ".."; "." should be handled by the VFS
  1406. */
  1407. block = start = 0;
  1408. nblocks = 1;
  1409. goto restart;
  1410. }
  1411. if (is_dx(dir)) {
  1412. ret = ext4_dx_find_entry(dir, fname, res_dir);
  1413. /*
  1414. * On success, or if the error was file not found,
  1415. * return. Otherwise, fall back to doing a search the
  1416. * old fashioned way.
  1417. */
  1418. if (IS_ERR(ret) && PTR_ERR(ret) == ERR_BAD_DX_DIR)
  1419. dxtrace(printk(KERN_DEBUG "ext4_find_entry: dx failed, "
  1420. "falling back\n"));
  1421. else if (!sb_no_casefold_compat_fallback(dir->i_sb) &&
  1422. *res_dir == NULL && IS_CASEFOLDED(dir))
  1423. dxtrace(printk(KERN_DEBUG "ext4_find_entry: casefold "
  1424. "failed, falling back\n"));
  1425. else
  1426. goto cleanup_and_exit;
  1427. ret = NULL;
  1428. }
  1429. nblocks = dir->i_size >> EXT4_BLOCK_SIZE_BITS(sb);
  1430. if (!nblocks) {
  1431. ret = NULL;
  1432. goto cleanup_and_exit;
  1433. }
  1434. start = EXT4_I(dir)->i_dir_start_lookup;
  1435. if (start >= nblocks)
  1436. start = 0;
  1437. block = start;
  1438. restart:
  1439. do {
  1440. /*
  1441. * We deal with the read-ahead logic here.
  1442. */
  1443. cond_resched();
  1444. if (ra_ptr >= ra_max) {
  1445. /* Refill the readahead buffer */
  1446. ra_ptr = 0;
  1447. if (block < start)
  1448. ra_max = start - block;
  1449. else
  1450. ra_max = nblocks - block;
  1451. ra_max = min(ra_max, ARRAY_SIZE(bh_use));
  1452. retval = ext4_bread_batch(dir, block, ra_max,
  1453. false /* wait */, bh_use);
  1454. if (retval) {
  1455. ret = ERR_PTR(retval);
  1456. ra_max = 0;
  1457. goto cleanup_and_exit;
  1458. }
  1459. }
  1460. if ((bh = bh_use[ra_ptr++]) == NULL)
  1461. goto next;
  1462. wait_on_buffer(bh);
  1463. if (!buffer_uptodate(bh)) {
  1464. EXT4_ERROR_INODE_ERR(dir, EIO,
  1465. "reading directory lblock %lu",
  1466. (unsigned long) block);
  1467. brelse(bh);
  1468. ret = ERR_PTR(-EIO);
  1469. goto cleanup_and_exit;
  1470. }
  1471. if (!buffer_verified(bh) &&
  1472. !is_dx_internal_node(dir, block,
  1473. (struct ext4_dir_entry *)bh->b_data) &&
  1474. !ext4_dirblock_csum_verify(dir, bh)) {
  1475. EXT4_ERROR_INODE_ERR(dir, EFSBADCRC,
  1476. "checksumming directory "
  1477. "block %lu", (unsigned long)block);
  1478. brelse(bh);
  1479. ret = ERR_PTR(-EFSBADCRC);
  1480. goto cleanup_and_exit;
  1481. }
  1482. set_buffer_verified(bh);
  1483. i = search_dirblock(bh, dir, fname,
  1484. EXT4_LBLK_TO_B(dir, block), res_dir);
  1485. if (i == 1) {
  1486. EXT4_I(dir)->i_dir_start_lookup = block;
  1487. ret = bh;
  1488. goto cleanup_and_exit;
  1489. } else {
  1490. brelse(bh);
  1491. if (i < 0) {
  1492. ret = ERR_PTR(i);
  1493. goto cleanup_and_exit;
  1494. }
  1495. }
  1496. next:
  1497. if (++block >= nblocks)
  1498. block = 0;
  1499. } while (block != start);
  1500. /*
  1501. * If the directory has grown while we were searching, then
  1502. * search the last part of the directory before giving up.
  1503. */
  1504. block = nblocks;
  1505. nblocks = dir->i_size >> EXT4_BLOCK_SIZE_BITS(sb);
  1506. if (block < nblocks) {
  1507. start = 0;
  1508. goto restart;
  1509. }
  1510. cleanup_and_exit:
  1511. /* Clean up the read-ahead blocks */
  1512. for (; ra_ptr < ra_max; ra_ptr++)
  1513. brelse(bh_use[ra_ptr]);
  1514. return ret;
  1515. }
  1516. static struct buffer_head *ext4_find_entry(struct inode *dir,
  1517. const struct qstr *d_name,
  1518. struct ext4_dir_entry_2 **res_dir,
  1519. int *inlined)
  1520. {
  1521. int err;
  1522. struct ext4_filename fname;
  1523. struct buffer_head *bh;
  1524. err = ext4_fname_setup_filename(dir, d_name, 1, &fname);
  1525. if (err == -ENOENT)
  1526. return NULL;
  1527. if (err)
  1528. return ERR_PTR(err);
  1529. bh = __ext4_find_entry(dir, &fname, res_dir, inlined);
  1530. ext4_fname_free_filename(&fname);
  1531. return bh;
  1532. }
  1533. static struct buffer_head *ext4_lookup_entry(struct inode *dir,
  1534. struct dentry *dentry,
  1535. struct ext4_dir_entry_2 **res_dir)
  1536. {
  1537. int err;
  1538. struct ext4_filename fname;
  1539. struct buffer_head *bh;
  1540. err = ext4_fname_prepare_lookup(dir, dentry, &fname);
  1541. if (err == -ENOENT)
  1542. return NULL;
  1543. if (err)
  1544. return ERR_PTR(err);
  1545. bh = __ext4_find_entry(dir, &fname, res_dir, NULL);
  1546. ext4_fname_free_filename(&fname);
  1547. return bh;
  1548. }
  1549. static struct buffer_head * ext4_dx_find_entry(struct inode *dir,
  1550. struct ext4_filename *fname,
  1551. struct ext4_dir_entry_2 **res_dir)
  1552. {
  1553. struct dx_frame frames[EXT4_HTREE_LEVEL], *frame;
  1554. struct buffer_head *bh;
  1555. ext4_lblk_t block;
  1556. int retval;
  1557. #ifdef CONFIG_FS_ENCRYPTION
  1558. *res_dir = NULL;
  1559. #endif
  1560. frame = dx_probe(fname, dir, NULL, frames);
  1561. if (IS_ERR(frame))
  1562. return ERR_CAST(frame);
  1563. do {
  1564. block = dx_get_block(frame->at);
  1565. bh = ext4_read_dirblock(dir, block, DIRENT_HTREE);
  1566. if (IS_ERR(bh))
  1567. goto errout;
  1568. retval = search_dirblock(bh, dir, fname,
  1569. EXT4_LBLK_TO_B(dir, block), res_dir);
  1570. if (retval == 1)
  1571. goto success;
  1572. brelse(bh);
  1573. if (retval < 0) {
  1574. bh = ERR_PTR(ERR_BAD_DX_DIR);
  1575. goto errout;
  1576. }
  1577. /* Check to see if we should continue to search */
  1578. retval = ext4_htree_next_block(dir, fname->hinfo.hash, frame,
  1579. frames, NULL);
  1580. if (retval < 0) {
  1581. ext4_warning_inode(dir,
  1582. "error %d reading directory index block",
  1583. retval);
  1584. bh = ERR_PTR(retval);
  1585. goto errout;
  1586. }
  1587. } while (retval == 1);
  1588. bh = NULL;
  1589. errout:
  1590. dxtrace(printk(KERN_DEBUG "%s not found\n", fname->usr_fname->name));
  1591. success:
  1592. dx_release(frames);
  1593. return bh;
  1594. }
  1595. static struct dentry *ext4_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
  1596. {
  1597. struct inode *inode;
  1598. struct ext4_dir_entry_2 *de = NULL;
  1599. struct buffer_head *bh;
  1600. if (dentry->d_name.len > EXT4_NAME_LEN)
  1601. return ERR_PTR(-ENAMETOOLONG);
  1602. bh = ext4_lookup_entry(dir, dentry, &de);
  1603. if (IS_ERR(bh))
  1604. return ERR_CAST(bh);
  1605. inode = NULL;
  1606. if (bh) {
  1607. __u32 ino = le32_to_cpu(de->inode);
  1608. brelse(bh);
  1609. if (!ext4_valid_inum(dir->i_sb, ino)) {
  1610. EXT4_ERROR_INODE(dir, "bad inode number: %u", ino);
  1611. return ERR_PTR(-EFSCORRUPTED);
  1612. }
  1613. if (unlikely(ino == dir->i_ino)) {
  1614. EXT4_ERROR_INODE(dir, "'%pd' linked to parent dir",
  1615. dentry);
  1616. return ERR_PTR(-EFSCORRUPTED);
  1617. }
  1618. inode = ext4_iget(dir->i_sb, ino, EXT4_IGET_NORMAL);
  1619. if (inode == ERR_PTR(-ESTALE)) {
  1620. EXT4_ERROR_INODE(dir,
  1621. "deleted inode referenced: %u",
  1622. ino);
  1623. return ERR_PTR(-EFSCORRUPTED);
  1624. }
  1625. if (!IS_ERR(inode) && IS_ENCRYPTED(dir) &&
  1626. (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)) &&
  1627. !fscrypt_has_permitted_context(dir, inode)) {
  1628. ext4_warning(inode->i_sb,
  1629. "Inconsistent encryption contexts: %lu/%lu",
  1630. dir->i_ino, inode->i_ino);
  1631. iput(inode);
  1632. return ERR_PTR(-EPERM);
  1633. }
  1634. }
  1635. if (IS_ENABLED(CONFIG_UNICODE) && !inode && IS_CASEFOLDED(dir)) {
  1636. /* Eventually we want to call d_add_ci(dentry, NULL)
  1637. * for negative dentries in the encoding case as
  1638. * well. For now, prevent the negative dentry
  1639. * from being cached.
  1640. */
  1641. return NULL;
  1642. }
  1643. return d_splice_alias(inode, dentry);
  1644. }
  1645. struct dentry *ext4_get_parent(struct dentry *child)
  1646. {
  1647. __u32 ino;
  1648. struct ext4_dir_entry_2 * de = NULL;
  1649. struct buffer_head *bh;
  1650. bh = ext4_find_entry(d_inode(child), &dotdot_name, &de, NULL);
  1651. if (IS_ERR(bh))
  1652. return ERR_CAST(bh);
  1653. if (!bh)
  1654. return ERR_PTR(-ENOENT);
  1655. ino = le32_to_cpu(de->inode);
  1656. brelse(bh);
  1657. if (!ext4_valid_inum(child->d_sb, ino)) {
  1658. EXT4_ERROR_INODE(d_inode(child),
  1659. "bad parent inode number: %u", ino);
  1660. return ERR_PTR(-EFSCORRUPTED);
  1661. }
  1662. return d_obtain_alias(ext4_iget(child->d_sb, ino, EXT4_IGET_NORMAL));
  1663. }
  1664. /*
  1665. * Move count entries from end of map between two memory locations.
  1666. * Returns pointer to last entry moved.
  1667. */
  1668. static struct ext4_dir_entry_2 *
  1669. dx_move_dirents(struct inode *dir, char *from, char *to,
  1670. struct dx_map_entry *map, int count,
  1671. unsigned blocksize)
  1672. {
  1673. unsigned rec_len = 0;
  1674. while (count--) {
  1675. struct ext4_dir_entry_2 *de = (struct ext4_dir_entry_2 *)
  1676. (from + (map->offs<<2));
  1677. rec_len = ext4_dir_rec_len(de->name_len, dir);
  1678. memcpy (to, de, rec_len);
  1679. ((struct ext4_dir_entry_2 *) to)->rec_len =
  1680. ext4_rec_len_to_disk(rec_len, blocksize);
  1681. /* wipe dir_entry excluding the rec_len field */
  1682. de->inode = 0;
  1683. memset(&de->name_len, 0, ext4_rec_len_from_disk(de->rec_len,
  1684. blocksize) -
  1685. offsetof(struct ext4_dir_entry_2,
  1686. name_len));
  1687. map++;
  1688. to += rec_len;
  1689. }
  1690. return (struct ext4_dir_entry_2 *) (to - rec_len);
  1691. }
  1692. /*
  1693. * Compact each dir entry in the range to the minimal rec_len.
  1694. * Returns pointer to last entry in range.
  1695. */
  1696. static struct ext4_dir_entry_2 *dx_pack_dirents(struct inode *dir, char *base,
  1697. unsigned int blocksize)
  1698. {
  1699. struct ext4_dir_entry_2 *next, *to, *prev, *de = (struct ext4_dir_entry_2 *) base;
  1700. unsigned rec_len = 0;
  1701. prev = to = de;
  1702. while ((char*)de < base + blocksize) {
  1703. next = ext4_next_entry(de, blocksize);
  1704. if (de->inode && de->name_len) {
  1705. rec_len = ext4_dir_rec_len(de->name_len, dir);
  1706. if (de > to)
  1707. memmove(to, de, rec_len);
  1708. to->rec_len = ext4_rec_len_to_disk(rec_len, blocksize);
  1709. prev = to;
  1710. to = (struct ext4_dir_entry_2 *) (((char *) to) + rec_len);
  1711. }
  1712. de = next;
  1713. }
  1714. return prev;
  1715. }
  1716. /*
  1717. * Split a full leaf block to make room for a new dir entry.
  1718. * Allocate a new block, and move entries so that they are approx. equally full.
  1719. * Returns pointer to de in block into which the new entry will be inserted.
  1720. */
  1721. static struct ext4_dir_entry_2 *do_split(handle_t *handle, struct inode *dir,
  1722. struct buffer_head **bh,struct dx_frame *frame,
  1723. struct dx_hash_info *hinfo)
  1724. {
  1725. unsigned blocksize = dir->i_sb->s_blocksize;
  1726. unsigned continued;
  1727. int count;
  1728. struct buffer_head *bh2;
  1729. ext4_lblk_t newblock;
  1730. u32 hash2;
  1731. struct dx_map_entry *map;
  1732. char *data1 = (*bh)->b_data, *data2;
  1733. unsigned split, move, size;
  1734. struct ext4_dir_entry_2 *de = NULL, *de2;
  1735. int csum_size = 0;
  1736. int err = 0, i;
  1737. if (ext4_has_feature_metadata_csum(dir->i_sb))
  1738. csum_size = sizeof(struct ext4_dir_entry_tail);
  1739. bh2 = ext4_append(handle, dir, &newblock);
  1740. if (IS_ERR(bh2)) {
  1741. brelse(*bh);
  1742. *bh = NULL;
  1743. return ERR_CAST(bh2);
  1744. }
  1745. BUFFER_TRACE(*bh, "get_write_access");
  1746. err = ext4_journal_get_write_access(handle, dir->i_sb, *bh,
  1747. EXT4_JTR_NONE);
  1748. if (err)
  1749. goto journal_error;
  1750. BUFFER_TRACE(frame->bh, "get_write_access");
  1751. err = ext4_journal_get_write_access(handle, dir->i_sb, frame->bh,
  1752. EXT4_JTR_NONE);
  1753. if (err)
  1754. goto journal_error;
  1755. data2 = bh2->b_data;
  1756. /* create map in the end of data2 block */
  1757. map = (struct dx_map_entry *) (data2 + blocksize);
  1758. count = dx_make_map(dir, *bh, hinfo, map);
  1759. if (count < 0) {
  1760. err = count;
  1761. goto journal_error;
  1762. }
  1763. map -= count;
  1764. dx_sort_map(map, count);
  1765. /* Ensure that neither split block is over half full */
  1766. size = 0;
  1767. move = 0;
  1768. for (i = count-1; i >= 0; i--) {
  1769. /* is more than half of this entry in 2nd half of the block? */
  1770. if (size + map[i].size/2 > blocksize/2)
  1771. break;
  1772. size += map[i].size;
  1773. move++;
  1774. }
  1775. /*
  1776. * map index at which we will split
  1777. *
  1778. * If the sum of active entries didn't exceed half the block size, just
  1779. * split it in half by count; each resulting block will have at least
  1780. * half the space free.
  1781. */
  1782. if (i >= 0)
  1783. split = count - move;
  1784. else
  1785. split = count/2;
  1786. if (WARN_ON_ONCE(split == 0)) {
  1787. /* Should never happen, but avoid out-of-bounds access below */
  1788. ext4_error_inode_block(dir, (*bh)->b_blocknr, 0,
  1789. "bad indexed directory? hash=%08x:%08x count=%d move=%u",
  1790. hinfo->hash, hinfo->minor_hash, count, move);
  1791. err = -EFSCORRUPTED;
  1792. goto out;
  1793. }
  1794. hash2 = map[split].hash;
  1795. continued = hash2 == map[split - 1].hash;
  1796. dxtrace(printk(KERN_INFO "Split block %lu at %x, %i/%i\n",
  1797. (unsigned long)dx_get_block(frame->at),
  1798. hash2, split, count-split));
  1799. /* Fancy dance to stay within two buffers */
  1800. de2 = dx_move_dirents(dir, data1, data2, map + split, count - split,
  1801. blocksize);
  1802. de = dx_pack_dirents(dir, data1, blocksize);
  1803. de->rec_len = ext4_rec_len_to_disk(data1 + (blocksize - csum_size) -
  1804. (char *) de,
  1805. blocksize);
  1806. de2->rec_len = ext4_rec_len_to_disk(data2 + (blocksize - csum_size) -
  1807. (char *) de2,
  1808. blocksize);
  1809. if (csum_size) {
  1810. ext4_initialize_dirent_tail(*bh, blocksize);
  1811. ext4_initialize_dirent_tail(bh2, blocksize);
  1812. }
  1813. dxtrace(dx_show_leaf(dir, hinfo, (struct ext4_dir_entry_2 *) data1,
  1814. blocksize, 1));
  1815. dxtrace(dx_show_leaf(dir, hinfo, (struct ext4_dir_entry_2 *) data2,
  1816. blocksize, 1));
  1817. /* Which block gets the new entry? */
  1818. if (hinfo->hash >= hash2) {
  1819. swap(*bh, bh2);
  1820. de = de2;
  1821. }
  1822. dx_insert_block(frame, hash2 + continued, newblock);
  1823. err = ext4_handle_dirty_dirblock(handle, dir, bh2);
  1824. if (err)
  1825. goto journal_error;
  1826. err = ext4_handle_dirty_dx_node(handle, dir, frame->bh);
  1827. if (err)
  1828. goto journal_error;
  1829. brelse(bh2);
  1830. dxtrace(dx_show_index("frame", frame->entries));
  1831. return de;
  1832. journal_error:
  1833. ext4_std_error(dir->i_sb, err);
  1834. out:
  1835. brelse(*bh);
  1836. brelse(bh2);
  1837. *bh = NULL;
  1838. return ERR_PTR(err);
  1839. }
  1840. int ext4_find_dest_de(struct inode *dir, struct buffer_head *bh,
  1841. void *buf, int buf_size,
  1842. struct ext4_filename *fname,
  1843. struct ext4_dir_entry_2 **dest_de)
  1844. {
  1845. struct ext4_dir_entry_2 *de;
  1846. unsigned short reclen = ext4_dir_rec_len(fname_len(fname), dir);
  1847. int nlen, rlen;
  1848. unsigned int offset = 0;
  1849. char *top;
  1850. de = buf;
  1851. top = buf + buf_size - reclen;
  1852. while ((char *) de <= top) {
  1853. if (ext4_check_dir_entry(dir, NULL, de, bh,
  1854. buf, buf_size, offset))
  1855. return -EFSCORRUPTED;
  1856. if (ext4_match(dir, fname, de))
  1857. return -EEXIST;
  1858. nlen = ext4_dir_rec_len(de->name_len, dir);
  1859. rlen = ext4_rec_len_from_disk(de->rec_len, buf_size);
  1860. if ((de->inode ? rlen - nlen : rlen) >= reclen)
  1861. break;
  1862. de = (struct ext4_dir_entry_2 *)((char *)de + rlen);
  1863. offset += rlen;
  1864. }
  1865. if ((char *) de > top)
  1866. return -ENOSPC;
  1867. *dest_de = de;
  1868. return 0;
  1869. }
  1870. void ext4_insert_dentry(struct inode *dir,
  1871. struct inode *inode,
  1872. struct ext4_dir_entry_2 *de,
  1873. int buf_size,
  1874. struct ext4_filename *fname)
  1875. {
  1876. int nlen, rlen;
  1877. nlen = ext4_dir_rec_len(de->name_len, dir);
  1878. rlen = ext4_rec_len_from_disk(de->rec_len, buf_size);
  1879. if (de->inode) {
  1880. struct ext4_dir_entry_2 *de1 =
  1881. (struct ext4_dir_entry_2 *)((char *)de + nlen);
  1882. de1->rec_len = ext4_rec_len_to_disk(rlen - nlen, buf_size);
  1883. de->rec_len = ext4_rec_len_to_disk(nlen, buf_size);
  1884. de = de1;
  1885. }
  1886. de->file_type = EXT4_FT_UNKNOWN;
  1887. de->inode = cpu_to_le32(inode->i_ino);
  1888. ext4_set_de_type(inode->i_sb, de, inode->i_mode);
  1889. de->name_len = fname_len(fname);
  1890. memcpy(de->name, fname_name(fname), fname_len(fname));
  1891. if (ext4_hash_in_dirent(dir)) {
  1892. struct dx_hash_info *hinfo = &fname->hinfo;
  1893. EXT4_DIRENT_HASHES(de)->hash = cpu_to_le32(hinfo->hash);
  1894. EXT4_DIRENT_HASHES(de)->minor_hash =
  1895. cpu_to_le32(hinfo->minor_hash);
  1896. }
  1897. }
  1898. /*
  1899. * Add a new entry into a directory (leaf) block. If de is non-NULL,
  1900. * it points to a directory entry which is guaranteed to be large
  1901. * enough for new directory entry. If de is NULL, then
  1902. * add_dirent_to_buf will attempt search the directory block for
  1903. * space. It will return -ENOSPC if no space is available, and -EIO
  1904. * and -EEXIST if directory entry already exists.
  1905. */
  1906. static int add_dirent_to_buf(handle_t *handle, struct ext4_filename *fname,
  1907. struct inode *dir,
  1908. struct inode *inode, struct ext4_dir_entry_2 *de,
  1909. struct buffer_head *bh)
  1910. {
  1911. unsigned int blocksize = dir->i_sb->s_blocksize;
  1912. int csum_size = 0;
  1913. int err, err2;
  1914. if (ext4_has_feature_metadata_csum(inode->i_sb))
  1915. csum_size = sizeof(struct ext4_dir_entry_tail);
  1916. if (!de) {
  1917. err = ext4_find_dest_de(dir, bh, bh->b_data,
  1918. blocksize - csum_size, fname, &de);
  1919. if (err)
  1920. return err;
  1921. }
  1922. BUFFER_TRACE(bh, "get_write_access");
  1923. err = ext4_journal_get_write_access(handle, dir->i_sb, bh,
  1924. EXT4_JTR_NONE);
  1925. if (err) {
  1926. ext4_std_error(dir->i_sb, err);
  1927. return err;
  1928. }
  1929. /* By now the buffer is marked for journaling */
  1930. ext4_insert_dentry(dir, inode, de, blocksize, fname);
  1931. /*
  1932. * XXX shouldn't update any times until successful
  1933. * completion of syscall, but too many callers depend
  1934. * on this.
  1935. *
  1936. * XXX similarly, too many callers depend on
  1937. * ext4_new_inode() setting the times, but error
  1938. * recovery deletes the inode, so the worst that can
  1939. * happen is that the times are slightly out of date
  1940. * and/or different from the directory change time.
  1941. */
  1942. inode_set_mtime_to_ts(dir, inode_set_ctime_current(dir));
  1943. ext4_update_dx_flag(dir);
  1944. inode_inc_iversion(dir);
  1945. err2 = ext4_mark_inode_dirty(handle, dir);
  1946. BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
  1947. err = ext4_handle_dirty_dirblock(handle, dir, bh);
  1948. if (err)
  1949. ext4_std_error(dir->i_sb, err);
  1950. return err ? err : err2;
  1951. }
  1952. static bool ext4_check_dx_root(struct inode *dir, struct dx_root *root)
  1953. {
  1954. struct fake_dirent *fde;
  1955. const char *error_msg;
  1956. unsigned int rlen;
  1957. unsigned int blocksize = dir->i_sb->s_blocksize;
  1958. char *blockend = (char *)root + dir->i_sb->s_blocksize;
  1959. fde = &root->dot;
  1960. if (unlikely(fde->name_len != 1)) {
  1961. error_msg = "invalid name_len for '.'";
  1962. goto corrupted;
  1963. }
  1964. if (unlikely(strncmp(root->dot_name, ".", fde->name_len))) {
  1965. error_msg = "invalid name for '.'";
  1966. goto corrupted;
  1967. }
  1968. rlen = ext4_rec_len_from_disk(fde->rec_len, blocksize);
  1969. if (unlikely((char *)fde + rlen >= blockend)) {
  1970. error_msg = "invalid rec_len for '.'";
  1971. goto corrupted;
  1972. }
  1973. fde = &root->dotdot;
  1974. if (unlikely(fde->name_len != 2)) {
  1975. error_msg = "invalid name_len for '..'";
  1976. goto corrupted;
  1977. }
  1978. if (unlikely(strncmp(root->dotdot_name, "..", fde->name_len))) {
  1979. error_msg = "invalid name for '..'";
  1980. goto corrupted;
  1981. }
  1982. rlen = ext4_rec_len_from_disk(fde->rec_len, blocksize);
  1983. if (unlikely((char *)fde + rlen >= blockend)) {
  1984. error_msg = "invalid rec_len for '..'";
  1985. goto corrupted;
  1986. }
  1987. return true;
  1988. corrupted:
  1989. EXT4_ERROR_INODE(dir, "Corrupt dir, %s, running e2fsck is recommended",
  1990. error_msg);
  1991. return false;
  1992. }
  1993. /*
  1994. * This converts a one block unindexed directory to a 3 block indexed
  1995. * directory, and adds the dentry to the indexed directory.
  1996. */
  1997. static int make_indexed_dir(handle_t *handle, struct ext4_filename *fname,
  1998. struct inode *dir,
  1999. struct inode *inode, struct buffer_head *bh)
  2000. {
  2001. struct buffer_head *bh2;
  2002. struct dx_root *root;
  2003. struct dx_frame frames[EXT4_HTREE_LEVEL], *frame;
  2004. struct dx_entry *entries;
  2005. struct ext4_dir_entry_2 *de, *de2;
  2006. char *data2, *top;
  2007. unsigned len;
  2008. int retval;
  2009. unsigned blocksize;
  2010. ext4_lblk_t block;
  2011. struct fake_dirent *fde;
  2012. int csum_size = 0;
  2013. if (ext4_has_feature_metadata_csum(inode->i_sb))
  2014. csum_size = sizeof(struct ext4_dir_entry_tail);
  2015. blocksize = dir->i_sb->s_blocksize;
  2016. dxtrace(printk(KERN_DEBUG "Creating index: inode %lu\n", dir->i_ino));
  2017. BUFFER_TRACE(bh, "get_write_access");
  2018. retval = ext4_journal_get_write_access(handle, dir->i_sb, bh,
  2019. EXT4_JTR_NONE);
  2020. if (retval) {
  2021. ext4_std_error(dir->i_sb, retval);
  2022. brelse(bh);
  2023. return retval;
  2024. }
  2025. root = (struct dx_root *) bh->b_data;
  2026. if (!ext4_check_dx_root(dir, root)) {
  2027. brelse(bh);
  2028. return -EFSCORRUPTED;
  2029. }
  2030. /* The 0th block becomes the root, move the dirents out */
  2031. fde = &root->dotdot;
  2032. de = (struct ext4_dir_entry_2 *)((char *)fde +
  2033. ext4_rec_len_from_disk(fde->rec_len, blocksize));
  2034. len = ((char *) root) + (blocksize - csum_size) - (char *) de;
  2035. /* Allocate new block for the 0th block's dirents */
  2036. bh2 = ext4_append(handle, dir, &block);
  2037. if (IS_ERR(bh2)) {
  2038. brelse(bh);
  2039. return PTR_ERR(bh2);
  2040. }
  2041. ext4_set_inode_flag(dir, EXT4_INODE_INDEX);
  2042. data2 = bh2->b_data;
  2043. memcpy(data2, de, len);
  2044. memset(de, 0, len); /* wipe old data */
  2045. de = (struct ext4_dir_entry_2 *) data2;
  2046. top = data2 + len;
  2047. while ((char *)(de2 = ext4_next_entry(de, blocksize)) < top) {
  2048. if (ext4_check_dir_entry(dir, NULL, de, bh2, data2, len,
  2049. (char *)de - data2)) {
  2050. brelse(bh2);
  2051. brelse(bh);
  2052. return -EFSCORRUPTED;
  2053. }
  2054. de = de2;
  2055. }
  2056. de->rec_len = ext4_rec_len_to_disk(data2 + (blocksize - csum_size) -
  2057. (char *) de, blocksize);
  2058. if (csum_size)
  2059. ext4_initialize_dirent_tail(bh2, blocksize);
  2060. /* Initialize the root; the dot dirents already exist */
  2061. de = (struct ext4_dir_entry_2 *) (&root->dotdot);
  2062. de->rec_len = ext4_rec_len_to_disk(
  2063. blocksize - ext4_dir_rec_len(2, NULL), blocksize);
  2064. memset (&root->info, 0, sizeof(root->info));
  2065. root->info.info_length = sizeof(root->info);
  2066. if (ext4_hash_in_dirent(dir))
  2067. root->info.hash_version = DX_HASH_SIPHASH;
  2068. else
  2069. root->info.hash_version =
  2070. EXT4_SB(dir->i_sb)->s_def_hash_version;
  2071. entries = root->entries;
  2072. dx_set_block(entries, 1);
  2073. dx_set_count(entries, 1);
  2074. dx_set_limit(entries, dx_root_limit(dir, sizeof(root->info)));
  2075. /* Initialize as for dx_probe */
  2076. fname->hinfo.hash_version = root->info.hash_version;
  2077. if (fname->hinfo.hash_version <= DX_HASH_TEA)
  2078. fname->hinfo.hash_version += EXT4_SB(dir->i_sb)->s_hash_unsigned;
  2079. fname->hinfo.seed = EXT4_SB(dir->i_sb)->s_hash_seed;
  2080. /* casefolded encrypted hashes are computed on fname setup */
  2081. if (!ext4_hash_in_dirent(dir)) {
  2082. int err = ext4fs_dirhash(dir, fname_name(fname),
  2083. fname_len(fname), &fname->hinfo);
  2084. if (err < 0) {
  2085. brelse(bh2);
  2086. brelse(bh);
  2087. return err;
  2088. }
  2089. }
  2090. memset(frames, 0, sizeof(frames));
  2091. frame = frames;
  2092. frame->entries = entries;
  2093. frame->at = entries;
  2094. frame->bh = bh;
  2095. retval = ext4_handle_dirty_dx_node(handle, dir, frame->bh);
  2096. if (retval)
  2097. goto out_frames;
  2098. retval = ext4_handle_dirty_dirblock(handle, dir, bh2);
  2099. if (retval)
  2100. goto out_frames;
  2101. de = do_split(handle,dir, &bh2, frame, &fname->hinfo);
  2102. if (IS_ERR(de)) {
  2103. retval = PTR_ERR(de);
  2104. goto out_frames;
  2105. }
  2106. retval = add_dirent_to_buf(handle, fname, dir, inode, de, bh2);
  2107. out_frames:
  2108. /*
  2109. * Even if the block split failed, we have to properly write
  2110. * out all the changes we did so far. Otherwise we can end up
  2111. * with corrupted filesystem.
  2112. */
  2113. if (retval)
  2114. ext4_mark_inode_dirty(handle, dir);
  2115. dx_release(frames);
  2116. brelse(bh2);
  2117. return retval;
  2118. }
  2119. /*
  2120. * ext4_add_entry()
  2121. *
  2122. * adds a file entry to the specified directory, using the same
  2123. * semantics as ext4_find_entry(). It returns NULL if it failed.
  2124. *
  2125. * NOTE!! The inode part of 'de' is left at 0 - which means you
  2126. * may not sleep between calling this and putting something into
  2127. * the entry, as someone else might have used it while you slept.
  2128. */
  2129. static int ext4_add_entry(handle_t *handle, struct dentry *dentry,
  2130. struct inode *inode)
  2131. {
  2132. struct inode *dir = d_inode(dentry->d_parent);
  2133. struct buffer_head *bh = NULL;
  2134. struct ext4_dir_entry_2 *de;
  2135. struct super_block *sb;
  2136. struct ext4_filename fname;
  2137. int retval;
  2138. int dx_fallback=0;
  2139. unsigned blocksize;
  2140. ext4_lblk_t block, blocks;
  2141. int csum_size = 0;
  2142. if (ext4_has_feature_metadata_csum(inode->i_sb))
  2143. csum_size = sizeof(struct ext4_dir_entry_tail);
  2144. sb = dir->i_sb;
  2145. blocksize = sb->s_blocksize;
  2146. if (fscrypt_is_nokey_name(dentry))
  2147. return -ENOKEY;
  2148. if (!generic_ci_validate_strict_name(dir, &dentry->d_name))
  2149. return -EINVAL;
  2150. retval = ext4_fname_setup_filename(dir, &dentry->d_name, 0, &fname);
  2151. if (retval)
  2152. return retval;
  2153. if (ext4_has_inline_data(dir)) {
  2154. retval = ext4_try_add_inline_entry(handle, &fname, dir, inode);
  2155. if (retval < 0)
  2156. goto out;
  2157. if (retval == 1) {
  2158. retval = 0;
  2159. goto out;
  2160. }
  2161. }
  2162. if (is_dx(dir)) {
  2163. retval = ext4_dx_add_entry(handle, &fname, dir, inode);
  2164. if (!retval || (retval != ERR_BAD_DX_DIR))
  2165. goto out;
  2166. /* Can we just ignore htree data? */
  2167. if (ext4_has_feature_metadata_csum(sb)) {
  2168. EXT4_ERROR_INODE(dir,
  2169. "Directory has corrupted htree index.");
  2170. retval = -EFSCORRUPTED;
  2171. goto out;
  2172. }
  2173. ext4_clear_inode_flag(dir, EXT4_INODE_INDEX);
  2174. dx_fallback++;
  2175. retval = ext4_mark_inode_dirty(handle, dir);
  2176. if (unlikely(retval))
  2177. goto out;
  2178. }
  2179. blocks = dir->i_size >> sb->s_blocksize_bits;
  2180. for (block = 0; block < blocks; block++) {
  2181. bh = ext4_read_dirblock(dir, block, DIRENT);
  2182. if (bh == NULL) {
  2183. bh = ext4_bread(handle, dir, block,
  2184. EXT4_GET_BLOCKS_CREATE);
  2185. goto add_to_new_block;
  2186. }
  2187. if (IS_ERR(bh)) {
  2188. retval = PTR_ERR(bh);
  2189. bh = NULL;
  2190. goto out;
  2191. }
  2192. retval = add_dirent_to_buf(handle, &fname, dir, inode,
  2193. NULL, bh);
  2194. if (retval != -ENOSPC)
  2195. goto out;
  2196. if (blocks == 1 && !dx_fallback &&
  2197. ext4_has_feature_dir_index(sb)) {
  2198. retval = make_indexed_dir(handle, &fname, dir,
  2199. inode, bh);
  2200. bh = NULL; /* make_indexed_dir releases bh */
  2201. goto out;
  2202. }
  2203. brelse(bh);
  2204. }
  2205. bh = ext4_append(handle, dir, &block);
  2206. add_to_new_block:
  2207. if (IS_ERR(bh)) {
  2208. retval = PTR_ERR(bh);
  2209. bh = NULL;
  2210. goto out;
  2211. }
  2212. de = (struct ext4_dir_entry_2 *) bh->b_data;
  2213. de->inode = 0;
  2214. de->rec_len = ext4_rec_len_to_disk(blocksize - csum_size, blocksize);
  2215. if (csum_size)
  2216. ext4_initialize_dirent_tail(bh, blocksize);
  2217. retval = add_dirent_to_buf(handle, &fname, dir, inode, de, bh);
  2218. out:
  2219. ext4_fname_free_filename(&fname);
  2220. brelse(bh);
  2221. if (retval == 0)
  2222. ext4_set_inode_state(inode, EXT4_STATE_NEWENTRY);
  2223. return retval;
  2224. }
  2225. /*
  2226. * Returns 0 for success, or a negative error value
  2227. */
  2228. static int ext4_dx_add_entry(handle_t *handle, struct ext4_filename *fname,
  2229. struct inode *dir, struct inode *inode)
  2230. {
  2231. struct dx_frame frames[EXT4_HTREE_LEVEL], *frame;
  2232. struct dx_entry *entries, *at;
  2233. struct buffer_head *bh;
  2234. struct super_block *sb = dir->i_sb;
  2235. struct ext4_dir_entry_2 *de;
  2236. int restart;
  2237. int err;
  2238. again:
  2239. restart = 0;
  2240. frame = dx_probe(fname, dir, NULL, frames);
  2241. if (IS_ERR(frame))
  2242. return PTR_ERR(frame);
  2243. entries = frame->entries;
  2244. at = frame->at;
  2245. bh = ext4_read_dirblock(dir, dx_get_block(frame->at), DIRENT_HTREE);
  2246. if (IS_ERR(bh)) {
  2247. err = PTR_ERR(bh);
  2248. bh = NULL;
  2249. goto cleanup;
  2250. }
  2251. BUFFER_TRACE(bh, "get_write_access");
  2252. err = ext4_journal_get_write_access(handle, sb, bh, EXT4_JTR_NONE);
  2253. if (err)
  2254. goto journal_error;
  2255. err = add_dirent_to_buf(handle, fname, dir, inode, NULL, bh);
  2256. if (err != -ENOSPC)
  2257. goto cleanup;
  2258. err = 0;
  2259. /* Block full, should compress but for now just split */
  2260. dxtrace(printk(KERN_DEBUG "using %u of %u node entries\n",
  2261. dx_get_count(entries), dx_get_limit(entries)));
  2262. /* Need to split index? */
  2263. if (dx_get_count(entries) == dx_get_limit(entries)) {
  2264. ext4_lblk_t newblock;
  2265. int levels = frame - frames + 1;
  2266. unsigned int icount;
  2267. int add_level = 1;
  2268. struct dx_entry *entries2;
  2269. struct dx_node *node2;
  2270. struct buffer_head *bh2;
  2271. while (frame > frames) {
  2272. if (dx_get_count((frame - 1)->entries) <
  2273. dx_get_limit((frame - 1)->entries)) {
  2274. add_level = 0;
  2275. break;
  2276. }
  2277. frame--; /* split higher index block */
  2278. at = frame->at;
  2279. entries = frame->entries;
  2280. restart = 1;
  2281. }
  2282. if (add_level && levels == ext4_dir_htree_level(sb)) {
  2283. ext4_warning(sb, "Directory (ino: %lu) index full, "
  2284. "reach max htree level :%d",
  2285. dir->i_ino, levels);
  2286. if (ext4_dir_htree_level(sb) < EXT4_HTREE_LEVEL) {
  2287. ext4_warning(sb, "Large directory feature is "
  2288. "not enabled on this "
  2289. "filesystem");
  2290. }
  2291. err = -ENOSPC;
  2292. goto cleanup;
  2293. }
  2294. icount = dx_get_count(entries);
  2295. bh2 = ext4_append(handle, dir, &newblock);
  2296. if (IS_ERR(bh2)) {
  2297. err = PTR_ERR(bh2);
  2298. goto cleanup;
  2299. }
  2300. node2 = (struct dx_node *)(bh2->b_data);
  2301. entries2 = node2->entries;
  2302. memset(&node2->fake, 0, sizeof(struct fake_dirent));
  2303. node2->fake.rec_len = ext4_rec_len_to_disk(sb->s_blocksize,
  2304. sb->s_blocksize);
  2305. BUFFER_TRACE(frame->bh, "get_write_access");
  2306. err = ext4_journal_get_write_access(handle, sb, frame->bh,
  2307. EXT4_JTR_NONE);
  2308. if (err) {
  2309. brelse(bh2);
  2310. goto journal_error;
  2311. }
  2312. if (!add_level) {
  2313. unsigned icount1 = icount/2, icount2 = icount - icount1;
  2314. unsigned hash2 = dx_get_hash(entries + icount1);
  2315. dxtrace(printk(KERN_DEBUG "Split index %i/%i\n",
  2316. icount1, icount2));
  2317. BUFFER_TRACE(frame->bh, "get_write_access"); /* index root */
  2318. err = ext4_journal_get_write_access(handle, sb,
  2319. (frame - 1)->bh,
  2320. EXT4_JTR_NONE);
  2321. if (err) {
  2322. brelse(bh2);
  2323. goto journal_error;
  2324. }
  2325. memcpy((char *) entries2, (char *) (entries + icount1),
  2326. icount2 * sizeof(struct dx_entry));
  2327. dx_set_count(entries, icount1);
  2328. dx_set_count(entries2, icount2);
  2329. dx_set_limit(entries2, dx_node_limit(dir));
  2330. /* Which index block gets the new entry? */
  2331. if (at - entries >= icount1) {
  2332. frame->at = at - entries - icount1 + entries2;
  2333. frame->entries = entries = entries2;
  2334. swap(frame->bh, bh2);
  2335. }
  2336. dx_insert_block((frame - 1), hash2, newblock);
  2337. dxtrace(dx_show_index("node", frame->entries));
  2338. dxtrace(dx_show_index("node",
  2339. ((struct dx_node *) bh2->b_data)->entries));
  2340. err = ext4_handle_dirty_dx_node(handle, dir, bh2);
  2341. if (err) {
  2342. brelse(bh2);
  2343. goto journal_error;
  2344. }
  2345. brelse (bh2);
  2346. err = ext4_handle_dirty_dx_node(handle, dir,
  2347. (frame - 1)->bh);
  2348. if (err)
  2349. goto journal_error;
  2350. err = ext4_handle_dirty_dx_node(handle, dir,
  2351. frame->bh);
  2352. if (restart || err)
  2353. goto journal_error;
  2354. } else {
  2355. struct dx_root *dxroot;
  2356. memcpy((char *) entries2, (char *) entries,
  2357. icount * sizeof(struct dx_entry));
  2358. dx_set_limit(entries2, dx_node_limit(dir));
  2359. /* Set up root */
  2360. dx_set_count(entries, 1);
  2361. dx_set_block(entries + 0, newblock);
  2362. dxroot = (struct dx_root *)frames[0].bh->b_data;
  2363. dxroot->info.indirect_levels += 1;
  2364. dxtrace(printk(KERN_DEBUG
  2365. "Creating %d level index...\n",
  2366. dxroot->info.indirect_levels));
  2367. err = ext4_handle_dirty_dx_node(handle, dir, frame->bh);
  2368. if (err) {
  2369. brelse(bh2);
  2370. goto journal_error;
  2371. }
  2372. err = ext4_handle_dirty_dx_node(handle, dir, bh2);
  2373. brelse(bh2);
  2374. restart = 1;
  2375. goto journal_error;
  2376. }
  2377. }
  2378. de = do_split(handle, dir, &bh, frame, &fname->hinfo);
  2379. if (IS_ERR(de)) {
  2380. err = PTR_ERR(de);
  2381. goto cleanup;
  2382. }
  2383. err = add_dirent_to_buf(handle, fname, dir, inode, de, bh);
  2384. goto cleanup;
  2385. journal_error:
  2386. ext4_std_error(dir->i_sb, err); /* this is a no-op if err == 0 */
  2387. cleanup:
  2388. brelse(bh);
  2389. dx_release(frames);
  2390. /* @restart is true means htree-path has been changed, we need to
  2391. * repeat dx_probe() to find out valid htree-path
  2392. */
  2393. if (restart && err == 0)
  2394. goto again;
  2395. return err;
  2396. }
  2397. /*
  2398. * ext4_generic_delete_entry deletes a directory entry by merging it
  2399. * with the previous entry
  2400. */
  2401. int ext4_generic_delete_entry(struct inode *dir,
  2402. struct ext4_dir_entry_2 *de_del,
  2403. struct buffer_head *bh,
  2404. void *entry_buf,
  2405. int buf_size,
  2406. int csum_size)
  2407. {
  2408. struct ext4_dir_entry_2 *de, *pde;
  2409. unsigned int blocksize = dir->i_sb->s_blocksize;
  2410. int i;
  2411. i = 0;
  2412. pde = NULL;
  2413. de = entry_buf;
  2414. while (i < buf_size - csum_size) {
  2415. if (ext4_check_dir_entry(dir, NULL, de, bh,
  2416. entry_buf, buf_size, i))
  2417. return -EFSCORRUPTED;
  2418. if (de == de_del) {
  2419. if (pde) {
  2420. pde->rec_len = ext4_rec_len_to_disk(
  2421. ext4_rec_len_from_disk(pde->rec_len,
  2422. blocksize) +
  2423. ext4_rec_len_from_disk(de->rec_len,
  2424. blocksize),
  2425. blocksize);
  2426. /* wipe entire dir_entry */
  2427. memset(de, 0, ext4_rec_len_from_disk(de->rec_len,
  2428. blocksize));
  2429. } else {
  2430. /* wipe dir_entry excluding the rec_len field */
  2431. de->inode = 0;
  2432. memset(&de->name_len, 0,
  2433. ext4_rec_len_from_disk(de->rec_len,
  2434. blocksize) -
  2435. offsetof(struct ext4_dir_entry_2,
  2436. name_len));
  2437. }
  2438. inode_inc_iversion(dir);
  2439. return 0;
  2440. }
  2441. i += ext4_rec_len_from_disk(de->rec_len, blocksize);
  2442. pde = de;
  2443. de = ext4_next_entry(de, blocksize);
  2444. }
  2445. return -ENOENT;
  2446. }
  2447. static int ext4_delete_entry(handle_t *handle,
  2448. struct inode *dir,
  2449. struct ext4_dir_entry_2 *de_del,
  2450. struct buffer_head *bh)
  2451. {
  2452. int err, csum_size = 0;
  2453. if (ext4_has_inline_data(dir)) {
  2454. int has_inline_data = 1;
  2455. err = ext4_delete_inline_entry(handle, dir, de_del, bh,
  2456. &has_inline_data);
  2457. if (has_inline_data)
  2458. return err;
  2459. }
  2460. if (ext4_has_feature_metadata_csum(dir->i_sb))
  2461. csum_size = sizeof(struct ext4_dir_entry_tail);
  2462. BUFFER_TRACE(bh, "get_write_access");
  2463. err = ext4_journal_get_write_access(handle, dir->i_sb, bh,
  2464. EXT4_JTR_NONE);
  2465. if (unlikely(err))
  2466. goto out;
  2467. err = ext4_generic_delete_entry(dir, de_del, bh, bh->b_data,
  2468. dir->i_sb->s_blocksize, csum_size);
  2469. if (err)
  2470. goto out;
  2471. BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
  2472. err = ext4_handle_dirty_dirblock(handle, dir, bh);
  2473. if (unlikely(err))
  2474. goto out;
  2475. return 0;
  2476. out:
  2477. if (err != -ENOENT)
  2478. ext4_std_error(dir->i_sb, err);
  2479. return err;
  2480. }
  2481. /*
  2482. * Set directory link count to 1 if nlinks > EXT4_LINK_MAX, or if nlinks == 2
  2483. * since this indicates that nlinks count was previously 1 to avoid overflowing
  2484. * the 16-bit i_links_count field on disk. Directories with i_nlink == 1 mean
  2485. * that subdirectory link counts are not being maintained accurately.
  2486. *
  2487. * The caller has already checked for i_nlink overflow in case the DIR_LINK
  2488. * feature is not enabled and returned -EMLINK. The is_dx() check is a proxy
  2489. * for checking S_ISDIR(inode) (since the INODE_INDEX feature will not be set
  2490. * on regular files) and to avoid creating huge/slow non-HTREE directories.
  2491. */
  2492. static void ext4_inc_count(struct inode *inode)
  2493. {
  2494. inc_nlink(inode);
  2495. if (is_dx(inode) &&
  2496. (inode->i_nlink > EXT4_LINK_MAX || inode->i_nlink == 2))
  2497. set_nlink(inode, 1);
  2498. }
  2499. /*
  2500. * If a directory had nlink == 1, then we should let it be 1. This indicates
  2501. * directory has >EXT4_LINK_MAX subdirs.
  2502. */
  2503. static void ext4_dec_count(struct inode *inode)
  2504. {
  2505. if (!S_ISDIR(inode->i_mode) || inode->i_nlink > 2)
  2506. drop_nlink(inode);
  2507. }
  2508. /*
  2509. * Add non-directory inode to a directory. On success, the inode reference is
  2510. * consumed by dentry is instantiation. This is also indicated by clearing of
  2511. * *inodep pointer. On failure, the caller is responsible for dropping the
  2512. * inode reference in the safe context.
  2513. */
  2514. static int ext4_add_nondir(handle_t *handle,
  2515. struct dentry *dentry, struct inode **inodep)
  2516. {
  2517. struct inode *dir = d_inode(dentry->d_parent);
  2518. struct inode *inode = *inodep;
  2519. int err = ext4_add_entry(handle, dentry, inode);
  2520. if (!err) {
  2521. err = ext4_mark_inode_dirty(handle, inode);
  2522. if (IS_DIRSYNC(dir))
  2523. ext4_handle_sync(handle);
  2524. d_instantiate_new(dentry, inode);
  2525. *inodep = NULL;
  2526. return err;
  2527. }
  2528. drop_nlink(inode);
  2529. ext4_mark_inode_dirty(handle, inode);
  2530. ext4_orphan_add(handle, inode);
  2531. unlock_new_inode(inode);
  2532. return err;
  2533. }
  2534. /*
  2535. * By the time this is called, we already have created
  2536. * the directory cache entry for the new file, but it
  2537. * is so far negative - it has no inode.
  2538. *
  2539. * If the create succeeds, we fill in the inode information
  2540. * with d_instantiate().
  2541. */
  2542. static int ext4_create(struct mnt_idmap *idmap, struct inode *dir,
  2543. struct dentry *dentry, umode_t mode, bool excl)
  2544. {
  2545. handle_t *handle;
  2546. struct inode *inode;
  2547. int err, credits, retries = 0;
  2548. err = dquot_initialize(dir);
  2549. if (err)
  2550. return err;
  2551. credits = (EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
  2552. EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3);
  2553. retry:
  2554. inode = ext4_new_inode_start_handle(idmap, dir, mode, &dentry->d_name,
  2555. 0, NULL, EXT4_HT_DIR, credits);
  2556. handle = ext4_journal_current_handle();
  2557. err = PTR_ERR(inode);
  2558. if (!IS_ERR(inode)) {
  2559. inode->i_op = &ext4_file_inode_operations;
  2560. inode->i_fop = &ext4_file_operations;
  2561. ext4_set_aops(inode);
  2562. err = ext4_add_nondir(handle, dentry, &inode);
  2563. if (!err)
  2564. ext4_fc_track_create(handle, dentry);
  2565. }
  2566. if (handle)
  2567. ext4_journal_stop(handle);
  2568. if (!IS_ERR_OR_NULL(inode))
  2569. iput(inode);
  2570. if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
  2571. goto retry;
  2572. return err;
  2573. }
  2574. static int ext4_mknod(struct mnt_idmap *idmap, struct inode *dir,
  2575. struct dentry *dentry, umode_t mode, dev_t rdev)
  2576. {
  2577. handle_t *handle;
  2578. struct inode *inode;
  2579. int err, credits, retries = 0;
  2580. err = dquot_initialize(dir);
  2581. if (err)
  2582. return err;
  2583. credits = (EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
  2584. EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3);
  2585. retry:
  2586. inode = ext4_new_inode_start_handle(idmap, dir, mode, &dentry->d_name,
  2587. 0, NULL, EXT4_HT_DIR, credits);
  2588. handle = ext4_journal_current_handle();
  2589. err = PTR_ERR(inode);
  2590. if (!IS_ERR(inode)) {
  2591. init_special_inode(inode, inode->i_mode, rdev);
  2592. inode->i_op = &ext4_special_inode_operations;
  2593. err = ext4_add_nondir(handle, dentry, &inode);
  2594. if (!err)
  2595. ext4_fc_track_create(handle, dentry);
  2596. }
  2597. if (handle)
  2598. ext4_journal_stop(handle);
  2599. if (!IS_ERR_OR_NULL(inode))
  2600. iput(inode);
  2601. if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
  2602. goto retry;
  2603. return err;
  2604. }
  2605. static int ext4_tmpfile(struct mnt_idmap *idmap, struct inode *dir,
  2606. struct file *file, umode_t mode)
  2607. {
  2608. handle_t *handle;
  2609. struct inode *inode;
  2610. int err, retries = 0;
  2611. err = dquot_initialize(dir);
  2612. if (err)
  2613. return err;
  2614. retry:
  2615. inode = ext4_new_inode_start_handle(idmap, dir, mode,
  2616. NULL, 0, NULL,
  2617. EXT4_HT_DIR,
  2618. EXT4_MAXQUOTAS_TRANS_BLOCKS(dir->i_sb) +
  2619. 4 + EXT4_XATTR_TRANS_BLOCKS);
  2620. handle = ext4_journal_current_handle();
  2621. err = PTR_ERR(inode);
  2622. if (!IS_ERR(inode)) {
  2623. inode->i_op = &ext4_file_inode_operations;
  2624. inode->i_fop = &ext4_file_operations;
  2625. ext4_set_aops(inode);
  2626. d_tmpfile(file, inode);
  2627. err = ext4_orphan_add(handle, inode);
  2628. if (err)
  2629. goto err_unlock_inode;
  2630. mark_inode_dirty(inode);
  2631. unlock_new_inode(inode);
  2632. }
  2633. if (handle)
  2634. ext4_journal_stop(handle);
  2635. if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
  2636. goto retry;
  2637. return finish_open_simple(file, err);
  2638. err_unlock_inode:
  2639. ext4_journal_stop(handle);
  2640. unlock_new_inode(inode);
  2641. return err;
  2642. }
  2643. int ext4_init_dirblock(handle_t *handle, struct inode *inode,
  2644. struct buffer_head *bh, unsigned int parent_ino,
  2645. void *inline_buf, int inline_size)
  2646. {
  2647. struct ext4_dir_entry_2 *de = (struct ext4_dir_entry_2 *) bh->b_data;
  2648. size_t blocksize = bh->b_size;
  2649. int csum_size = 0, header_size;
  2650. if (ext4_has_feature_metadata_csum(inode->i_sb))
  2651. csum_size = sizeof(struct ext4_dir_entry_tail);
  2652. de->inode = cpu_to_le32(inode->i_ino);
  2653. de->name_len = 1;
  2654. de->rec_len = ext4_rec_len_to_disk(ext4_dir_rec_len(de->name_len, NULL),
  2655. blocksize);
  2656. memcpy(de->name, ".", 2);
  2657. ext4_set_de_type(inode->i_sb, de, S_IFDIR);
  2658. de = ext4_next_entry(de, blocksize);
  2659. de->inode = cpu_to_le32(parent_ino);
  2660. de->name_len = 2;
  2661. memcpy(de->name, "..", 3);
  2662. ext4_set_de_type(inode->i_sb, de, S_IFDIR);
  2663. if (inline_buf) {
  2664. de->rec_len = ext4_rec_len_to_disk(
  2665. ext4_dir_rec_len(de->name_len, NULL),
  2666. blocksize);
  2667. de = ext4_next_entry(de, blocksize);
  2668. header_size = (char *)de - bh->b_data;
  2669. memcpy((void *)de, inline_buf, inline_size);
  2670. ext4_update_final_de(bh->b_data, inline_size + header_size,
  2671. blocksize - csum_size);
  2672. } else {
  2673. de->rec_len = ext4_rec_len_to_disk(blocksize -
  2674. (csum_size + ext4_dir_rec_len(1, NULL)),
  2675. blocksize);
  2676. }
  2677. if (csum_size)
  2678. ext4_initialize_dirent_tail(bh, blocksize);
  2679. BUFFER_TRACE(dir_block, "call ext4_handle_dirty_metadata");
  2680. set_buffer_uptodate(bh);
  2681. set_buffer_verified(bh);
  2682. return ext4_handle_dirty_dirblock(handle, inode, bh);
  2683. }
  2684. int ext4_init_new_dir(handle_t *handle, struct inode *dir,
  2685. struct inode *inode)
  2686. {
  2687. struct buffer_head *dir_block = NULL;
  2688. ext4_lblk_t block = 0;
  2689. int err;
  2690. if (ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) {
  2691. err = ext4_try_create_inline_dir(handle, dir, inode);
  2692. if (err < 0 && err != -ENOSPC)
  2693. goto out;
  2694. if (!err)
  2695. goto out;
  2696. }
  2697. set_nlink(inode, 2);
  2698. inode->i_size = 0;
  2699. dir_block = ext4_append(handle, inode, &block);
  2700. if (IS_ERR(dir_block))
  2701. return PTR_ERR(dir_block);
  2702. err = ext4_init_dirblock(handle, inode, dir_block, dir->i_ino, NULL, 0);
  2703. out:
  2704. brelse(dir_block);
  2705. return err;
  2706. }
  2707. static struct dentry *ext4_mkdir(struct mnt_idmap *idmap, struct inode *dir,
  2708. struct dentry *dentry, umode_t mode)
  2709. {
  2710. handle_t *handle;
  2711. struct inode *inode;
  2712. int err, err2 = 0, credits, retries = 0;
  2713. if (EXT4_DIR_LINK_MAX(dir))
  2714. return ERR_PTR(-EMLINK);
  2715. err = dquot_initialize(dir);
  2716. if (err)
  2717. return ERR_PTR(err);
  2718. credits = (EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
  2719. EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3);
  2720. retry:
  2721. inode = ext4_new_inode_start_handle(idmap, dir, S_IFDIR | mode,
  2722. &dentry->d_name,
  2723. 0, NULL, EXT4_HT_DIR, credits);
  2724. handle = ext4_journal_current_handle();
  2725. err = PTR_ERR(inode);
  2726. if (IS_ERR(inode))
  2727. goto out_stop;
  2728. inode->i_op = &ext4_dir_inode_operations;
  2729. inode->i_fop = &ext4_dir_operations;
  2730. err = ext4_init_new_dir(handle, dir, inode);
  2731. if (err)
  2732. goto out_clear_inode;
  2733. err = ext4_mark_inode_dirty(handle, inode);
  2734. if (!err)
  2735. err = ext4_add_entry(handle, dentry, inode);
  2736. if (err) {
  2737. out_clear_inode:
  2738. clear_nlink(inode);
  2739. ext4_orphan_add(handle, inode);
  2740. unlock_new_inode(inode);
  2741. err2 = ext4_mark_inode_dirty(handle, inode);
  2742. if (unlikely(err2))
  2743. err = err2;
  2744. ext4_journal_stop(handle);
  2745. iput(inode);
  2746. goto out_retry;
  2747. }
  2748. ext4_inc_count(dir);
  2749. ext4_update_dx_flag(dir);
  2750. err = ext4_mark_inode_dirty(handle, dir);
  2751. if (err)
  2752. goto out_clear_inode;
  2753. d_instantiate_new(dentry, inode);
  2754. ext4_fc_track_create(handle, dentry);
  2755. if (IS_DIRSYNC(dir))
  2756. ext4_handle_sync(handle);
  2757. out_stop:
  2758. if (handle)
  2759. ext4_journal_stop(handle);
  2760. out_retry:
  2761. if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
  2762. goto retry;
  2763. return ERR_PTR(err);
  2764. }
  2765. /*
  2766. * routine to check that the specified directory is empty (for rmdir)
  2767. */
  2768. bool ext4_empty_dir(struct inode *inode)
  2769. {
  2770. unsigned int offset;
  2771. struct buffer_head *bh;
  2772. struct ext4_dir_entry_2 *de;
  2773. struct super_block *sb;
  2774. if (ext4_has_inline_data(inode)) {
  2775. int has_inline_data = 1;
  2776. int ret;
  2777. ret = empty_inline_dir(inode, &has_inline_data);
  2778. if (has_inline_data)
  2779. return ret;
  2780. }
  2781. sb = inode->i_sb;
  2782. if (inode->i_size < ext4_dir_rec_len(1, NULL) +
  2783. ext4_dir_rec_len(2, NULL)) {
  2784. EXT4_ERROR_INODE(inode, "invalid size");
  2785. return false;
  2786. }
  2787. bh = ext4_read_dirblock(inode, 0, EITHER);
  2788. if (IS_ERR(bh))
  2789. return false;
  2790. de = (struct ext4_dir_entry_2 *) bh->b_data;
  2791. if (ext4_check_dir_entry(inode, NULL, de, bh, bh->b_data, bh->b_size,
  2792. 0) ||
  2793. le32_to_cpu(de->inode) != inode->i_ino || de->name_len != 1 ||
  2794. de->name[0] != '.') {
  2795. ext4_warning_inode(inode, "directory missing '.'");
  2796. brelse(bh);
  2797. return false;
  2798. }
  2799. offset = ext4_rec_len_from_disk(de->rec_len, sb->s_blocksize);
  2800. de = ext4_next_entry(de, sb->s_blocksize);
  2801. if (ext4_check_dir_entry(inode, NULL, de, bh, bh->b_data, bh->b_size,
  2802. offset) ||
  2803. le32_to_cpu(de->inode) == 0 || de->name_len != 2 ||
  2804. de->name[0] != '.' || de->name[1] != '.') {
  2805. ext4_warning_inode(inode, "directory missing '..'");
  2806. brelse(bh);
  2807. return false;
  2808. }
  2809. offset += ext4_rec_len_from_disk(de->rec_len, sb->s_blocksize);
  2810. while (offset < inode->i_size) {
  2811. if (!(offset & (sb->s_blocksize - 1))) {
  2812. unsigned int lblock;
  2813. brelse(bh);
  2814. lblock = offset >> EXT4_BLOCK_SIZE_BITS(sb);
  2815. bh = ext4_read_dirblock(inode, lblock, EITHER);
  2816. if (bh == NULL) {
  2817. offset += sb->s_blocksize;
  2818. continue;
  2819. }
  2820. if (IS_ERR(bh))
  2821. return false;
  2822. }
  2823. de = (struct ext4_dir_entry_2 *) (bh->b_data +
  2824. (offset & (sb->s_blocksize - 1)));
  2825. if (ext4_check_dir_entry(inode, NULL, de, bh,
  2826. bh->b_data, bh->b_size, offset) ||
  2827. le32_to_cpu(de->inode)) {
  2828. brelse(bh);
  2829. return false;
  2830. }
  2831. offset += ext4_rec_len_from_disk(de->rec_len, sb->s_blocksize);
  2832. }
  2833. brelse(bh);
  2834. return true;
  2835. }
  2836. static int ext4_rmdir(struct inode *dir, struct dentry *dentry)
  2837. {
  2838. int retval;
  2839. struct inode *inode;
  2840. struct buffer_head *bh;
  2841. struct ext4_dir_entry_2 *de = NULL;
  2842. handle_t *handle = NULL;
  2843. retval = ext4_emergency_state(dir->i_sb);
  2844. if (unlikely(retval))
  2845. return retval;
  2846. /* Initialize quotas before so that eventual writes go in
  2847. * separate transaction */
  2848. retval = dquot_initialize(dir);
  2849. if (retval)
  2850. return retval;
  2851. retval = dquot_initialize(d_inode(dentry));
  2852. if (retval)
  2853. return retval;
  2854. retval = -ENOENT;
  2855. bh = ext4_find_entry(dir, &dentry->d_name, &de, NULL);
  2856. if (IS_ERR(bh))
  2857. return PTR_ERR(bh);
  2858. if (!bh)
  2859. goto end_rmdir;
  2860. inode = d_inode(dentry);
  2861. retval = -EFSCORRUPTED;
  2862. if (le32_to_cpu(de->inode) != inode->i_ino)
  2863. goto end_rmdir;
  2864. retval = -ENOTEMPTY;
  2865. if (!ext4_empty_dir(inode))
  2866. goto end_rmdir;
  2867. handle = ext4_journal_start(dir, EXT4_HT_DIR,
  2868. EXT4_DATA_TRANS_BLOCKS(dir->i_sb));
  2869. if (IS_ERR(handle)) {
  2870. retval = PTR_ERR(handle);
  2871. handle = NULL;
  2872. goto end_rmdir;
  2873. }
  2874. if (IS_DIRSYNC(dir))
  2875. ext4_handle_sync(handle);
  2876. retval = ext4_delete_entry(handle, dir, de, bh);
  2877. if (retval)
  2878. goto end_rmdir;
  2879. if (!EXT4_DIR_LINK_EMPTY(inode))
  2880. ext4_warning_inode(inode,
  2881. "empty directory '%.*s' has too many links (%u)",
  2882. dentry->d_name.len, dentry->d_name.name,
  2883. inode->i_nlink);
  2884. inode_inc_iversion(inode);
  2885. clear_nlink(inode);
  2886. /* There's no need to set i_disksize: the fact that i_nlink is
  2887. * zero will ensure that the right thing happens during any
  2888. * recovery. */
  2889. inode->i_size = 0;
  2890. ext4_orphan_add(handle, inode);
  2891. inode_set_mtime_to_ts(dir, inode_set_ctime_current(dir));
  2892. inode_set_ctime_current(inode);
  2893. retval = ext4_mark_inode_dirty(handle, inode);
  2894. if (retval)
  2895. goto end_rmdir;
  2896. ext4_dec_count(dir);
  2897. ext4_update_dx_flag(dir);
  2898. ext4_fc_track_unlink(handle, dentry);
  2899. retval = ext4_mark_inode_dirty(handle, dir);
  2900. /* VFS negative dentries are incompatible with Encoding and
  2901. * Case-insensitiveness. Eventually we'll want avoid
  2902. * invalidating the dentries here, alongside with returning the
  2903. * negative dentries at ext4_lookup(), when it is better
  2904. * supported by the VFS for the CI case.
  2905. */
  2906. if (IS_ENABLED(CONFIG_UNICODE) && IS_CASEFOLDED(dir))
  2907. d_invalidate(dentry);
  2908. end_rmdir:
  2909. brelse(bh);
  2910. if (handle)
  2911. ext4_journal_stop(handle);
  2912. return retval;
  2913. }
  2914. int __ext4_unlink(struct inode *dir, const struct qstr *d_name,
  2915. struct inode *inode,
  2916. struct dentry *dentry /* NULL during fast_commit recovery */)
  2917. {
  2918. int retval = -ENOENT;
  2919. struct buffer_head *bh;
  2920. struct ext4_dir_entry_2 *de = NULL;
  2921. handle_t *handle;
  2922. int skip_remove_dentry = 0;
  2923. /*
  2924. * Keep this outside the transaction; it may have to set up the
  2925. * directory's encryption key, which isn't GFP_NOFS-safe.
  2926. */
  2927. bh = ext4_find_entry(dir, d_name, &de, NULL);
  2928. if (IS_ERR(bh))
  2929. return PTR_ERR(bh);
  2930. if (!bh)
  2931. return -ENOENT;
  2932. if (le32_to_cpu(de->inode) != inode->i_ino) {
  2933. /*
  2934. * It's okay if we find dont find dentry which matches
  2935. * the inode. That's because it might have gotten
  2936. * renamed to a different inode number
  2937. */
  2938. if (EXT4_SB(inode->i_sb)->s_mount_state & EXT4_FC_REPLAY)
  2939. skip_remove_dentry = 1;
  2940. else
  2941. goto out_bh;
  2942. }
  2943. handle = ext4_journal_start(dir, EXT4_HT_DIR,
  2944. EXT4_DATA_TRANS_BLOCKS(dir->i_sb));
  2945. if (IS_ERR(handle)) {
  2946. retval = PTR_ERR(handle);
  2947. goto out_bh;
  2948. }
  2949. if (IS_DIRSYNC(dir))
  2950. ext4_handle_sync(handle);
  2951. if (!skip_remove_dentry) {
  2952. retval = ext4_delete_entry(handle, dir, de, bh);
  2953. if (retval)
  2954. goto out_handle;
  2955. inode_set_mtime_to_ts(dir, inode_set_ctime_current(dir));
  2956. ext4_update_dx_flag(dir);
  2957. retval = ext4_mark_inode_dirty(handle, dir);
  2958. if (retval)
  2959. goto out_handle;
  2960. } else {
  2961. retval = 0;
  2962. }
  2963. if (inode->i_nlink == 0)
  2964. ext4_warning_inode(inode, "Deleting file '%.*s' with no links",
  2965. d_name->len, d_name->name);
  2966. else
  2967. drop_nlink(inode);
  2968. if (!inode->i_nlink)
  2969. ext4_orphan_add(handle, inode);
  2970. inode_set_ctime_current(inode);
  2971. retval = ext4_mark_inode_dirty(handle, inode);
  2972. if (dentry && !retval)
  2973. ext4_fc_track_unlink(handle, dentry);
  2974. out_handle:
  2975. ext4_journal_stop(handle);
  2976. out_bh:
  2977. brelse(bh);
  2978. return retval;
  2979. }
  2980. static int ext4_unlink(struct inode *dir, struct dentry *dentry)
  2981. {
  2982. int retval;
  2983. retval = ext4_emergency_state(dir->i_sb);
  2984. if (unlikely(retval))
  2985. return retval;
  2986. trace_ext4_unlink_enter(dir, dentry);
  2987. /*
  2988. * Initialize quotas before so that eventual writes go
  2989. * in separate transaction
  2990. */
  2991. retval = dquot_initialize(dir);
  2992. if (retval)
  2993. goto out_trace;
  2994. retval = dquot_initialize(d_inode(dentry));
  2995. if (retval)
  2996. goto out_trace;
  2997. retval = __ext4_unlink(dir, &dentry->d_name, d_inode(dentry), dentry);
  2998. /* VFS negative dentries are incompatible with Encoding and
  2999. * Case-insensitiveness. Eventually we'll want avoid
  3000. * invalidating the dentries here, alongside with returning the
  3001. * negative dentries at ext4_lookup(), when it is better
  3002. * supported by the VFS for the CI case.
  3003. */
  3004. if (IS_ENABLED(CONFIG_UNICODE) && IS_CASEFOLDED(dir))
  3005. d_invalidate(dentry);
  3006. out_trace:
  3007. trace_ext4_unlink_exit(dentry, retval);
  3008. return retval;
  3009. }
  3010. static int ext4_init_symlink_block(handle_t *handle, struct inode *inode,
  3011. struct fscrypt_str *disk_link)
  3012. {
  3013. struct buffer_head *bh;
  3014. char *kaddr;
  3015. int err = 0;
  3016. bh = ext4_bread(handle, inode, 0, EXT4_GET_BLOCKS_CREATE);
  3017. if (IS_ERR(bh))
  3018. return PTR_ERR(bh);
  3019. BUFFER_TRACE(bh, "get_write_access");
  3020. err = ext4_journal_get_write_access(handle, inode->i_sb, bh, EXT4_JTR_NONE);
  3021. if (err)
  3022. goto out;
  3023. kaddr = (char *)bh->b_data;
  3024. memcpy(kaddr, disk_link->name, disk_link->len);
  3025. inode->i_size = disk_link->len - 1;
  3026. EXT4_I(inode)->i_disksize = inode->i_size;
  3027. err = ext4_handle_dirty_metadata(handle, inode, bh);
  3028. out:
  3029. brelse(bh);
  3030. return err;
  3031. }
  3032. static int ext4_symlink(struct mnt_idmap *idmap, struct inode *dir,
  3033. struct dentry *dentry, const char *symname)
  3034. {
  3035. handle_t *handle;
  3036. struct inode *inode;
  3037. int err, len = strlen(symname);
  3038. int credits;
  3039. struct fscrypt_str disk_link;
  3040. int retries = 0;
  3041. err = ext4_emergency_state(dir->i_sb);
  3042. if (unlikely(err))
  3043. return err;
  3044. err = fscrypt_prepare_symlink(dir, symname, len, dir->i_sb->s_blocksize,
  3045. &disk_link);
  3046. if (err)
  3047. return err;
  3048. err = dquot_initialize(dir);
  3049. if (err)
  3050. return err;
  3051. /*
  3052. * EXT4_INDEX_EXTRA_TRANS_BLOCKS for addition of entry into the
  3053. * directory. +3 for inode, inode bitmap, group descriptor allocation.
  3054. * EXT4_DATA_TRANS_BLOCKS for the data block allocation and
  3055. * modification.
  3056. */
  3057. credits = EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
  3058. EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3;
  3059. retry:
  3060. inode = ext4_new_inode_start_handle(idmap, dir, S_IFLNK|S_IRWXUGO,
  3061. &dentry->d_name, 0, NULL,
  3062. EXT4_HT_DIR, credits);
  3063. handle = ext4_journal_current_handle();
  3064. if (IS_ERR(inode)) {
  3065. if (handle)
  3066. ext4_journal_stop(handle);
  3067. err = PTR_ERR(inode);
  3068. goto out_retry;
  3069. }
  3070. if (IS_ENCRYPTED(inode)) {
  3071. err = fscrypt_encrypt_symlink(inode, symname, len, &disk_link);
  3072. if (err)
  3073. goto err_drop_inode;
  3074. inode->i_op = &ext4_encrypted_symlink_inode_operations;
  3075. } else {
  3076. if ((disk_link.len > EXT4_N_BLOCKS * 4)) {
  3077. inode->i_op = &ext4_symlink_inode_operations;
  3078. } else {
  3079. inode->i_op = &ext4_fast_symlink_inode_operations;
  3080. }
  3081. }
  3082. if ((disk_link.len > EXT4_N_BLOCKS * 4)) {
  3083. /* alloc symlink block and fill it */
  3084. err = ext4_init_symlink_block(handle, inode, &disk_link);
  3085. if (err)
  3086. goto err_drop_inode;
  3087. } else {
  3088. /* clear the extent format for fast symlink */
  3089. ext4_clear_inode_flag(inode, EXT4_INODE_EXTENTS);
  3090. memcpy((char *)&EXT4_I(inode)->i_data, disk_link.name,
  3091. disk_link.len);
  3092. inode->i_size = disk_link.len - 1;
  3093. EXT4_I(inode)->i_disksize = inode->i_size;
  3094. if (!IS_ENCRYPTED(inode))
  3095. inode_set_cached_link(inode, (char *)&EXT4_I(inode)->i_data,
  3096. inode->i_size);
  3097. }
  3098. err = ext4_add_nondir(handle, dentry, &inode);
  3099. if (handle)
  3100. ext4_journal_stop(handle);
  3101. iput(inode);
  3102. goto out_retry;
  3103. err_drop_inode:
  3104. clear_nlink(inode);
  3105. ext4_mark_inode_dirty(handle, inode);
  3106. ext4_orphan_add(handle, inode);
  3107. unlock_new_inode(inode);
  3108. if (handle)
  3109. ext4_journal_stop(handle);
  3110. iput(inode);
  3111. out_retry:
  3112. if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
  3113. goto retry;
  3114. if (disk_link.name != (unsigned char *)symname)
  3115. kfree(disk_link.name);
  3116. return err;
  3117. }
  3118. int __ext4_link(struct inode *dir, struct inode *inode, struct dentry *dentry)
  3119. {
  3120. handle_t *handle;
  3121. int err, retries = 0;
  3122. retry:
  3123. handle = ext4_journal_start(dir, EXT4_HT_DIR,
  3124. (EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
  3125. EXT4_INDEX_EXTRA_TRANS_BLOCKS) + 1);
  3126. if (IS_ERR(handle))
  3127. return PTR_ERR(handle);
  3128. if (IS_DIRSYNC(dir))
  3129. ext4_handle_sync(handle);
  3130. inode_set_ctime_current(inode);
  3131. ext4_inc_count(inode);
  3132. ihold(inode);
  3133. err = ext4_add_entry(handle, dentry, inode);
  3134. if (!err) {
  3135. err = ext4_mark_inode_dirty(handle, inode);
  3136. /* this can happen only for tmpfile being
  3137. * linked the first time
  3138. */
  3139. if (inode->i_nlink == 1)
  3140. ext4_orphan_del(handle, inode);
  3141. d_instantiate(dentry, inode);
  3142. ext4_fc_track_link(handle, dentry);
  3143. } else {
  3144. drop_nlink(inode);
  3145. iput(inode);
  3146. }
  3147. ext4_journal_stop(handle);
  3148. if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
  3149. goto retry;
  3150. return err;
  3151. }
  3152. static int ext4_link(struct dentry *old_dentry,
  3153. struct inode *dir, struct dentry *dentry)
  3154. {
  3155. struct inode *inode = d_inode(old_dentry);
  3156. int err;
  3157. if (inode->i_nlink >= EXT4_LINK_MAX)
  3158. return -EMLINK;
  3159. err = fscrypt_prepare_link(old_dentry, dir, dentry);
  3160. if (err)
  3161. return err;
  3162. if ((ext4_test_inode_flag(dir, EXT4_INODE_PROJINHERIT)) &&
  3163. (!projid_eq(EXT4_I(dir)->i_projid,
  3164. EXT4_I(old_dentry->d_inode)->i_projid)))
  3165. return -EXDEV;
  3166. err = dquot_initialize(dir);
  3167. if (err)
  3168. return err;
  3169. return __ext4_link(dir, inode, dentry);
  3170. }
  3171. /*
  3172. * Try to find buffer head where contains the parent block.
  3173. * It should be the inode block if it is inlined or the 1st block
  3174. * if it is a normal dir.
  3175. */
  3176. static struct buffer_head *ext4_get_first_dir_block(handle_t *handle,
  3177. struct inode *inode,
  3178. int *retval,
  3179. struct ext4_dir_entry_2 **parent_de,
  3180. int *inlined)
  3181. {
  3182. struct buffer_head *bh;
  3183. if (!ext4_has_inline_data(inode)) {
  3184. struct ext4_dir_entry_2 *de;
  3185. unsigned int offset;
  3186. bh = ext4_read_dirblock(inode, 0, EITHER);
  3187. if (IS_ERR(bh)) {
  3188. *retval = PTR_ERR(bh);
  3189. return NULL;
  3190. }
  3191. de = (struct ext4_dir_entry_2 *) bh->b_data;
  3192. if (ext4_check_dir_entry(inode, NULL, de, bh, bh->b_data,
  3193. bh->b_size, 0) ||
  3194. le32_to_cpu(de->inode) != inode->i_ino ||
  3195. de->name_len != 1 || de->name[0] != '.') {
  3196. EXT4_ERROR_INODE(inode, "directory missing '.'");
  3197. brelse(bh);
  3198. *retval = -EFSCORRUPTED;
  3199. return NULL;
  3200. }
  3201. offset = ext4_rec_len_from_disk(de->rec_len,
  3202. inode->i_sb->s_blocksize);
  3203. de = ext4_next_entry(de, inode->i_sb->s_blocksize);
  3204. if (ext4_check_dir_entry(inode, NULL, de, bh, bh->b_data,
  3205. bh->b_size, offset) ||
  3206. le32_to_cpu(de->inode) == 0 || de->name_len != 2 ||
  3207. de->name[0] != '.' || de->name[1] != '.') {
  3208. EXT4_ERROR_INODE(inode, "directory missing '..'");
  3209. brelse(bh);
  3210. *retval = -EFSCORRUPTED;
  3211. return NULL;
  3212. }
  3213. *parent_de = de;
  3214. return bh;
  3215. }
  3216. *inlined = 1;
  3217. return ext4_get_first_inline_block(inode, parent_de, retval);
  3218. }
  3219. struct ext4_renament {
  3220. struct inode *dir;
  3221. struct dentry *dentry;
  3222. struct inode *inode;
  3223. bool is_dir;
  3224. int dir_nlink_delta;
  3225. /* entry for "dentry" */
  3226. struct buffer_head *bh;
  3227. struct ext4_dir_entry_2 *de;
  3228. int inlined;
  3229. /* entry for ".." in inode if it's a directory */
  3230. struct buffer_head *dir_bh;
  3231. struct ext4_dir_entry_2 *parent_de;
  3232. int dir_inlined;
  3233. };
  3234. static int ext4_rename_dir_prepare(handle_t *handle, struct ext4_renament *ent, bool is_cross)
  3235. {
  3236. int retval;
  3237. ent->is_dir = true;
  3238. if (!is_cross)
  3239. return 0;
  3240. ent->dir_bh = ext4_get_first_dir_block(handle, ent->inode,
  3241. &retval, &ent->parent_de,
  3242. &ent->dir_inlined);
  3243. if (!ent->dir_bh)
  3244. return retval;
  3245. if (le32_to_cpu(ent->parent_de->inode) != ent->dir->i_ino)
  3246. return -EFSCORRUPTED;
  3247. BUFFER_TRACE(ent->dir_bh, "get_write_access");
  3248. return ext4_journal_get_write_access(handle, ent->dir->i_sb,
  3249. ent->dir_bh, EXT4_JTR_NONE);
  3250. }
  3251. static int ext4_rename_dir_finish(handle_t *handle, struct ext4_renament *ent,
  3252. unsigned dir_ino)
  3253. {
  3254. int retval;
  3255. if (!ent->dir_bh)
  3256. return 0;
  3257. ent->parent_de->inode = cpu_to_le32(dir_ino);
  3258. BUFFER_TRACE(ent->dir_bh, "call ext4_handle_dirty_metadata");
  3259. if (!ent->dir_inlined) {
  3260. if (is_dx(ent->inode)) {
  3261. retval = ext4_handle_dirty_dx_node(handle,
  3262. ent->inode,
  3263. ent->dir_bh);
  3264. } else {
  3265. retval = ext4_handle_dirty_dirblock(handle, ent->inode,
  3266. ent->dir_bh);
  3267. }
  3268. } else {
  3269. retval = ext4_mark_inode_dirty(handle, ent->inode);
  3270. }
  3271. if (retval) {
  3272. ext4_std_error(ent->dir->i_sb, retval);
  3273. return retval;
  3274. }
  3275. return 0;
  3276. }
  3277. static int ext4_setent(handle_t *handle, struct ext4_renament *ent,
  3278. unsigned ino, unsigned file_type)
  3279. {
  3280. int retval, retval2;
  3281. BUFFER_TRACE(ent->bh, "get write access");
  3282. retval = ext4_journal_get_write_access(handle, ent->dir->i_sb, ent->bh,
  3283. EXT4_JTR_NONE);
  3284. if (retval)
  3285. return retval;
  3286. ent->de->inode = cpu_to_le32(ino);
  3287. if (ext4_has_feature_filetype(ent->dir->i_sb))
  3288. ent->de->file_type = file_type;
  3289. inode_inc_iversion(ent->dir);
  3290. inode_set_mtime_to_ts(ent->dir, inode_set_ctime_current(ent->dir));
  3291. retval = ext4_mark_inode_dirty(handle, ent->dir);
  3292. BUFFER_TRACE(ent->bh, "call ext4_handle_dirty_metadata");
  3293. if (!ent->inlined) {
  3294. retval2 = ext4_handle_dirty_dirblock(handle, ent->dir, ent->bh);
  3295. if (unlikely(retval2)) {
  3296. ext4_std_error(ent->dir->i_sb, retval2);
  3297. return retval2;
  3298. }
  3299. }
  3300. return retval;
  3301. }
  3302. static void ext4_resetent(handle_t *handle, struct ext4_renament *ent,
  3303. unsigned ino, unsigned file_type)
  3304. {
  3305. struct ext4_renament old = *ent;
  3306. int retval = 0;
  3307. /*
  3308. * old->de could have moved from under us during make indexed dir,
  3309. * so the old->de may no longer valid and need to find it again
  3310. * before reset old inode info.
  3311. */
  3312. old.bh = ext4_find_entry(old.dir, &old.dentry->d_name, &old.de,
  3313. &old.inlined);
  3314. if (IS_ERR(old.bh))
  3315. retval = PTR_ERR(old.bh);
  3316. if (!old.bh)
  3317. retval = -ENOENT;
  3318. if (retval) {
  3319. ext4_std_error(old.dir->i_sb, retval);
  3320. return;
  3321. }
  3322. ext4_setent(handle, &old, ino, file_type);
  3323. brelse(old.bh);
  3324. }
  3325. static int ext4_find_delete_entry(handle_t *handle, struct inode *dir,
  3326. const struct qstr *d_name)
  3327. {
  3328. int retval = -ENOENT;
  3329. struct buffer_head *bh;
  3330. struct ext4_dir_entry_2 *de = NULL;
  3331. bh = ext4_find_entry(dir, d_name, &de, NULL);
  3332. if (IS_ERR(bh))
  3333. return PTR_ERR(bh);
  3334. if (bh) {
  3335. retval = ext4_delete_entry(handle, dir, de, bh);
  3336. brelse(bh);
  3337. }
  3338. return retval;
  3339. }
  3340. static void ext4_rename_delete(handle_t *handle, struct ext4_renament *ent,
  3341. int force_reread)
  3342. {
  3343. int retval;
  3344. /*
  3345. * ent->de could have moved from under us during htree split, so make
  3346. * sure that we are deleting the right entry. We might also be pointing
  3347. * to a stale entry in the unused part of ent->bh so just checking inum
  3348. * and the name isn't enough.
  3349. */
  3350. if (le32_to_cpu(ent->de->inode) != ent->inode->i_ino ||
  3351. ent->de->name_len != ent->dentry->d_name.len ||
  3352. strncmp(ent->de->name, ent->dentry->d_name.name,
  3353. ent->de->name_len) ||
  3354. force_reread) {
  3355. retval = ext4_find_delete_entry(handle, ent->dir,
  3356. &ent->dentry->d_name);
  3357. } else {
  3358. retval = ext4_delete_entry(handle, ent->dir, ent->de, ent->bh);
  3359. if (retval == -ENOENT) {
  3360. retval = ext4_find_delete_entry(handle, ent->dir,
  3361. &ent->dentry->d_name);
  3362. }
  3363. }
  3364. if (retval) {
  3365. ext4_warning_inode(ent->dir,
  3366. "Deleting old file: nlink %d, error=%d",
  3367. ent->dir->i_nlink, retval);
  3368. }
  3369. }
  3370. static void ext4_update_dir_count(handle_t *handle, struct ext4_renament *ent)
  3371. {
  3372. if (ent->dir_nlink_delta) {
  3373. if (ent->dir_nlink_delta == -1)
  3374. ext4_dec_count(ent->dir);
  3375. else
  3376. ext4_inc_count(ent->dir);
  3377. ext4_mark_inode_dirty(handle, ent->dir);
  3378. }
  3379. }
  3380. static struct inode *ext4_whiteout_for_rename(struct mnt_idmap *idmap,
  3381. struct ext4_renament *ent,
  3382. int credits, handle_t **h)
  3383. {
  3384. struct inode *wh;
  3385. handle_t *handle;
  3386. int retries = 0;
  3387. /*
  3388. * for inode block, sb block, group summaries,
  3389. * and inode bitmap
  3390. */
  3391. credits += (EXT4_MAXQUOTAS_TRANS_BLOCKS(ent->dir->i_sb) +
  3392. EXT4_XATTR_TRANS_BLOCKS + 4);
  3393. retry:
  3394. wh = ext4_new_inode_start_handle(idmap, ent->dir,
  3395. S_IFCHR | WHITEOUT_MODE,
  3396. &ent->dentry->d_name, 0, NULL,
  3397. EXT4_HT_DIR, credits);
  3398. handle = ext4_journal_current_handle();
  3399. if (IS_ERR(wh)) {
  3400. if (handle)
  3401. ext4_journal_stop(handle);
  3402. if (PTR_ERR(wh) == -ENOSPC &&
  3403. ext4_should_retry_alloc(ent->dir->i_sb, &retries))
  3404. goto retry;
  3405. } else {
  3406. *h = handle;
  3407. init_special_inode(wh, wh->i_mode, WHITEOUT_DEV);
  3408. wh->i_op = &ext4_special_inode_operations;
  3409. }
  3410. return wh;
  3411. }
  3412. /*
  3413. * Anybody can rename anything with this: the permission checks are left to the
  3414. * higher-level routines.
  3415. *
  3416. * n.b. old_{dentry,inode) refers to the source dentry/inode
  3417. * while new_{dentry,inode) refers to the destination dentry/inode
  3418. * This comes from rename(const char *oldpath, const char *newpath)
  3419. */
  3420. static int ext4_rename(struct mnt_idmap *idmap, struct inode *old_dir,
  3421. struct dentry *old_dentry, struct inode *new_dir,
  3422. struct dentry *new_dentry, unsigned int flags)
  3423. {
  3424. handle_t *handle = NULL;
  3425. struct ext4_renament old = {
  3426. .dir = old_dir,
  3427. .dentry = old_dentry,
  3428. .inode = d_inode(old_dentry),
  3429. };
  3430. struct ext4_renament new = {
  3431. .dir = new_dir,
  3432. .dentry = new_dentry,
  3433. .inode = d_inode(new_dentry),
  3434. };
  3435. int force_reread;
  3436. int retval;
  3437. struct inode *whiteout = NULL;
  3438. int credits;
  3439. u8 old_file_type;
  3440. if (new.inode && new.inode->i_nlink == 0) {
  3441. EXT4_ERROR_INODE(new.inode,
  3442. "target of rename is already freed");
  3443. return -EFSCORRUPTED;
  3444. }
  3445. if ((ext4_test_inode_flag(new_dir, EXT4_INODE_PROJINHERIT)) &&
  3446. (!projid_eq(EXT4_I(new_dir)->i_projid,
  3447. EXT4_I(old_dentry->d_inode)->i_projid)))
  3448. return -EXDEV;
  3449. retval = dquot_initialize(old.dir);
  3450. if (retval)
  3451. return retval;
  3452. retval = dquot_initialize(old.inode);
  3453. if (retval)
  3454. return retval;
  3455. retval = dquot_initialize(new.dir);
  3456. if (retval)
  3457. return retval;
  3458. /* Initialize quotas before so that eventual writes go
  3459. * in separate transaction */
  3460. if (new.inode) {
  3461. retval = dquot_initialize(new.inode);
  3462. if (retval)
  3463. return retval;
  3464. }
  3465. old.bh = ext4_find_entry(old.dir, &old.dentry->d_name, &old.de,
  3466. &old.inlined);
  3467. if (IS_ERR(old.bh))
  3468. return PTR_ERR(old.bh);
  3469. /*
  3470. * Check for inode number is _not_ due to possible IO errors.
  3471. * We might rmdir the source, keep it as pwd of some process
  3472. * and merrily kill the link to whatever was created under the
  3473. * same name. Goodbye sticky bit ;-<
  3474. */
  3475. retval = -ENOENT;
  3476. if (!old.bh || le32_to_cpu(old.de->inode) != old.inode->i_ino)
  3477. goto release_bh;
  3478. new.bh = ext4_find_entry(new.dir, &new.dentry->d_name,
  3479. &new.de, &new.inlined);
  3480. if (IS_ERR(new.bh)) {
  3481. retval = PTR_ERR(new.bh);
  3482. new.bh = NULL;
  3483. goto release_bh;
  3484. }
  3485. if (new.bh) {
  3486. if (!new.inode) {
  3487. brelse(new.bh);
  3488. new.bh = NULL;
  3489. }
  3490. }
  3491. if (new.inode && !test_opt(new.dir->i_sb, NO_AUTO_DA_ALLOC))
  3492. ext4_alloc_da_blocks(old.inode);
  3493. credits = (2 * EXT4_DATA_TRANS_BLOCKS(old.dir->i_sb) +
  3494. EXT4_INDEX_EXTRA_TRANS_BLOCKS + 2);
  3495. if (!(flags & RENAME_WHITEOUT)) {
  3496. handle = ext4_journal_start(old.dir, EXT4_HT_DIR, credits);
  3497. if (IS_ERR(handle)) {
  3498. retval = PTR_ERR(handle);
  3499. goto release_bh;
  3500. }
  3501. } else {
  3502. whiteout = ext4_whiteout_for_rename(idmap, &old, credits, &handle);
  3503. if (IS_ERR(whiteout)) {
  3504. retval = PTR_ERR(whiteout);
  3505. goto release_bh;
  3506. }
  3507. }
  3508. old_file_type = old.de->file_type;
  3509. if (IS_DIRSYNC(old.dir) || IS_DIRSYNC(new.dir))
  3510. ext4_handle_sync(handle);
  3511. if (S_ISDIR(old.inode->i_mode)) {
  3512. if (new.inode) {
  3513. retval = -ENOTEMPTY;
  3514. if (!ext4_empty_dir(new.inode))
  3515. goto end_rename;
  3516. } else {
  3517. retval = -EMLINK;
  3518. if (new.dir != old.dir && EXT4_DIR_LINK_MAX(new.dir))
  3519. goto end_rename;
  3520. }
  3521. retval = ext4_rename_dir_prepare(handle, &old, new.dir != old.dir);
  3522. if (retval)
  3523. goto end_rename;
  3524. }
  3525. /*
  3526. * If we're renaming a file within an inline_data dir and adding or
  3527. * setting the new dirent causes a conversion from inline_data to
  3528. * extents/blockmap, we need to force the dirent delete code to
  3529. * re-read the directory, or else we end up trying to delete a dirent
  3530. * from what is now the extent tree root (or a block map).
  3531. */
  3532. force_reread = (new.dir->i_ino == old.dir->i_ino &&
  3533. ext4_test_inode_flag(new.dir, EXT4_INODE_INLINE_DATA));
  3534. if (whiteout) {
  3535. /*
  3536. * Do this before adding a new entry, so the old entry is sure
  3537. * to be still pointing to the valid old entry.
  3538. */
  3539. retval = ext4_setent(handle, &old, whiteout->i_ino,
  3540. EXT4_FT_CHRDEV);
  3541. if (retval)
  3542. goto end_rename;
  3543. retval = ext4_mark_inode_dirty(handle, whiteout);
  3544. if (unlikely(retval))
  3545. goto end_rename;
  3546. }
  3547. if (!new.bh) {
  3548. retval = ext4_add_entry(handle, new.dentry, old.inode);
  3549. if (retval)
  3550. goto end_rename;
  3551. } else {
  3552. retval = ext4_setent(handle, &new,
  3553. old.inode->i_ino, old_file_type);
  3554. if (retval)
  3555. goto end_rename;
  3556. }
  3557. if (force_reread)
  3558. force_reread = !ext4_test_inode_flag(new.dir,
  3559. EXT4_INODE_INLINE_DATA);
  3560. /*
  3561. * Like most other Unix systems, set the ctime for inodes on a
  3562. * rename.
  3563. */
  3564. inode_set_ctime_current(old.inode);
  3565. retval = ext4_mark_inode_dirty(handle, old.inode);
  3566. if (unlikely(retval))
  3567. goto end_rename;
  3568. if (!whiteout) {
  3569. /*
  3570. * ok, that's it
  3571. */
  3572. ext4_rename_delete(handle, &old, force_reread);
  3573. }
  3574. if (new.inode) {
  3575. ext4_dec_count(new.inode);
  3576. inode_set_ctime_current(new.inode);
  3577. }
  3578. inode_set_mtime_to_ts(old.dir, inode_set_ctime_current(old.dir));
  3579. ext4_update_dx_flag(old.dir);
  3580. if (old.is_dir) {
  3581. retval = ext4_rename_dir_finish(handle, &old, new.dir->i_ino);
  3582. if (retval)
  3583. goto end_rename;
  3584. ext4_dec_count(old.dir);
  3585. if (new.inode) {
  3586. /* checked ext4_empty_dir above, can't have another
  3587. * parent, ext4_dec_count() won't work for many-linked
  3588. * dirs */
  3589. clear_nlink(new.inode);
  3590. } else {
  3591. ext4_inc_count(new.dir);
  3592. ext4_update_dx_flag(new.dir);
  3593. retval = ext4_mark_inode_dirty(handle, new.dir);
  3594. if (unlikely(retval))
  3595. goto end_rename;
  3596. }
  3597. }
  3598. retval = ext4_mark_inode_dirty(handle, old.dir);
  3599. if (unlikely(retval))
  3600. goto end_rename;
  3601. if (old.is_dir) {
  3602. /*
  3603. * We disable fast commits here that's because the
  3604. * replay code is not yet capable of changing dot dot
  3605. * dirents in directories.
  3606. */
  3607. ext4_fc_mark_ineligible(old.inode->i_sb,
  3608. EXT4_FC_REASON_RENAME_DIR, handle);
  3609. } else {
  3610. struct super_block *sb = old.inode->i_sb;
  3611. if (new.inode)
  3612. ext4_fc_track_unlink(handle, new.dentry);
  3613. if (test_opt2(sb, JOURNAL_FAST_COMMIT) &&
  3614. !(EXT4_SB(sb)->s_mount_state & EXT4_FC_REPLAY) &&
  3615. !(ext4_test_mount_flag(sb, EXT4_MF_FC_INELIGIBLE))) {
  3616. __ext4_fc_track_link(handle, old.inode, new.dentry);
  3617. __ext4_fc_track_unlink(handle, old.inode, old.dentry);
  3618. if (whiteout)
  3619. __ext4_fc_track_create(handle, whiteout,
  3620. old.dentry);
  3621. }
  3622. }
  3623. if (new.inode) {
  3624. retval = ext4_mark_inode_dirty(handle, new.inode);
  3625. if (unlikely(retval))
  3626. goto end_rename;
  3627. if (!new.inode->i_nlink)
  3628. ext4_orphan_add(handle, new.inode);
  3629. }
  3630. retval = 0;
  3631. end_rename:
  3632. if (whiteout) {
  3633. if (retval) {
  3634. ext4_resetent(handle, &old,
  3635. old.inode->i_ino, old_file_type);
  3636. drop_nlink(whiteout);
  3637. ext4_mark_inode_dirty(handle, whiteout);
  3638. ext4_orphan_add(handle, whiteout);
  3639. }
  3640. unlock_new_inode(whiteout);
  3641. ext4_journal_stop(handle);
  3642. iput(whiteout);
  3643. } else {
  3644. ext4_journal_stop(handle);
  3645. }
  3646. release_bh:
  3647. brelse(old.dir_bh);
  3648. brelse(old.bh);
  3649. brelse(new.bh);
  3650. return retval;
  3651. }
  3652. static int ext4_cross_rename(struct inode *old_dir, struct dentry *old_dentry,
  3653. struct inode *new_dir, struct dentry *new_dentry)
  3654. {
  3655. handle_t *handle = NULL;
  3656. struct ext4_renament old = {
  3657. .dir = old_dir,
  3658. .dentry = old_dentry,
  3659. .inode = d_inode(old_dentry),
  3660. };
  3661. struct ext4_renament new = {
  3662. .dir = new_dir,
  3663. .dentry = new_dentry,
  3664. .inode = d_inode(new_dentry),
  3665. };
  3666. u8 new_file_type;
  3667. int retval;
  3668. if ((ext4_test_inode_flag(new_dir, EXT4_INODE_PROJINHERIT) &&
  3669. !projid_eq(EXT4_I(new_dir)->i_projid,
  3670. EXT4_I(old_dentry->d_inode)->i_projid)) ||
  3671. (ext4_test_inode_flag(old_dir, EXT4_INODE_PROJINHERIT) &&
  3672. !projid_eq(EXT4_I(old_dir)->i_projid,
  3673. EXT4_I(new_dentry->d_inode)->i_projid)))
  3674. return -EXDEV;
  3675. retval = dquot_initialize(old.dir);
  3676. if (retval)
  3677. return retval;
  3678. retval = dquot_initialize(new.dir);
  3679. if (retval)
  3680. return retval;
  3681. old.bh = ext4_find_entry(old.dir, &old.dentry->d_name,
  3682. &old.de, &old.inlined);
  3683. if (IS_ERR(old.bh))
  3684. return PTR_ERR(old.bh);
  3685. /*
  3686. * Check for inode number is _not_ due to possible IO errors.
  3687. * We might rmdir the source, keep it as pwd of some process
  3688. * and merrily kill the link to whatever was created under the
  3689. * same name. Goodbye sticky bit ;-<
  3690. */
  3691. retval = -ENOENT;
  3692. if (!old.bh || le32_to_cpu(old.de->inode) != old.inode->i_ino)
  3693. goto end_rename;
  3694. new.bh = ext4_find_entry(new.dir, &new.dentry->d_name,
  3695. &new.de, &new.inlined);
  3696. if (IS_ERR(new.bh)) {
  3697. retval = PTR_ERR(new.bh);
  3698. new.bh = NULL;
  3699. goto end_rename;
  3700. }
  3701. /* RENAME_EXCHANGE case: old *and* new must both exist */
  3702. if (!new.bh || le32_to_cpu(new.de->inode) != new.inode->i_ino)
  3703. goto end_rename;
  3704. handle = ext4_journal_start(old.dir, EXT4_HT_DIR,
  3705. (2 * EXT4_DATA_TRANS_BLOCKS(old.dir->i_sb) +
  3706. 2 * EXT4_INDEX_EXTRA_TRANS_BLOCKS + 2));
  3707. if (IS_ERR(handle)) {
  3708. retval = PTR_ERR(handle);
  3709. handle = NULL;
  3710. goto end_rename;
  3711. }
  3712. if (IS_DIRSYNC(old.dir) || IS_DIRSYNC(new.dir))
  3713. ext4_handle_sync(handle);
  3714. if (S_ISDIR(old.inode->i_mode)) {
  3715. retval = ext4_rename_dir_prepare(handle, &old, new.dir != old.dir);
  3716. if (retval)
  3717. goto end_rename;
  3718. }
  3719. if (S_ISDIR(new.inode->i_mode)) {
  3720. retval = ext4_rename_dir_prepare(handle, &new, new.dir != old.dir);
  3721. if (retval)
  3722. goto end_rename;
  3723. }
  3724. /*
  3725. * Other than the special case of overwriting a directory, parents'
  3726. * nlink only needs to be modified if this is a cross directory rename.
  3727. */
  3728. if (old.dir != new.dir && old.is_dir != new.is_dir) {
  3729. old.dir_nlink_delta = old.is_dir ? -1 : 1;
  3730. new.dir_nlink_delta = -old.dir_nlink_delta;
  3731. retval = -EMLINK;
  3732. if ((old.dir_nlink_delta > 0 && EXT4_DIR_LINK_MAX(old.dir)) ||
  3733. (new.dir_nlink_delta > 0 && EXT4_DIR_LINK_MAX(new.dir)))
  3734. goto end_rename;
  3735. }
  3736. new_file_type = new.de->file_type;
  3737. retval = ext4_setent(handle, &new, old.inode->i_ino, old.de->file_type);
  3738. if (retval)
  3739. goto end_rename;
  3740. retval = ext4_setent(handle, &old, new.inode->i_ino, new_file_type);
  3741. if (retval)
  3742. goto end_rename;
  3743. /*
  3744. * Like most other Unix systems, set the ctime for inodes on a
  3745. * rename.
  3746. */
  3747. inode_set_ctime_current(old.inode);
  3748. inode_set_ctime_current(new.inode);
  3749. retval = ext4_mark_inode_dirty(handle, old.inode);
  3750. if (unlikely(retval))
  3751. goto end_rename;
  3752. retval = ext4_mark_inode_dirty(handle, new.inode);
  3753. if (unlikely(retval))
  3754. goto end_rename;
  3755. ext4_fc_mark_ineligible(new.inode->i_sb,
  3756. EXT4_FC_REASON_CROSS_RENAME, handle);
  3757. if (old.dir_bh) {
  3758. retval = ext4_rename_dir_finish(handle, &old, new.dir->i_ino);
  3759. if (retval)
  3760. goto end_rename;
  3761. }
  3762. if (new.dir_bh) {
  3763. retval = ext4_rename_dir_finish(handle, &new, old.dir->i_ino);
  3764. if (retval)
  3765. goto end_rename;
  3766. }
  3767. ext4_update_dir_count(handle, &old);
  3768. ext4_update_dir_count(handle, &new);
  3769. retval = 0;
  3770. end_rename:
  3771. brelse(old.dir_bh);
  3772. brelse(new.dir_bh);
  3773. brelse(old.bh);
  3774. brelse(new.bh);
  3775. if (handle)
  3776. ext4_journal_stop(handle);
  3777. return retval;
  3778. }
  3779. static int ext4_rename2(struct mnt_idmap *idmap,
  3780. struct inode *old_dir, struct dentry *old_dentry,
  3781. struct inode *new_dir, struct dentry *new_dentry,
  3782. unsigned int flags)
  3783. {
  3784. int err;
  3785. err = ext4_emergency_state(old_dir->i_sb);
  3786. if (unlikely(err))
  3787. return err;
  3788. if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
  3789. return -EINVAL;
  3790. err = fscrypt_prepare_rename(old_dir, old_dentry, new_dir, new_dentry,
  3791. flags);
  3792. if (err)
  3793. return err;
  3794. if (flags & RENAME_EXCHANGE) {
  3795. return ext4_cross_rename(old_dir, old_dentry,
  3796. new_dir, new_dentry);
  3797. }
  3798. return ext4_rename(idmap, old_dir, old_dentry, new_dir, new_dentry, flags);
  3799. }
  3800. /*
  3801. * directories can handle most operations...
  3802. */
  3803. const struct inode_operations ext4_dir_inode_operations = {
  3804. .create = ext4_create,
  3805. .lookup = ext4_lookup,
  3806. .link = ext4_link,
  3807. .unlink = ext4_unlink,
  3808. .symlink = ext4_symlink,
  3809. .mkdir = ext4_mkdir,
  3810. .rmdir = ext4_rmdir,
  3811. .mknod = ext4_mknod,
  3812. .tmpfile = ext4_tmpfile,
  3813. .rename = ext4_rename2,
  3814. .setattr = ext4_setattr,
  3815. .getattr = ext4_getattr,
  3816. .listxattr = ext4_listxattr,
  3817. .get_inode_acl = ext4_get_acl,
  3818. .set_acl = ext4_set_acl,
  3819. .fiemap = ext4_fiemap,
  3820. .fileattr_get = ext4_fileattr_get,
  3821. .fileattr_set = ext4_fileattr_set,
  3822. };
  3823. const struct inode_operations ext4_special_inode_operations = {
  3824. .setattr = ext4_setattr,
  3825. .getattr = ext4_getattr,
  3826. .listxattr = ext4_listxattr,
  3827. .get_inode_acl = ext4_get_acl,
  3828. .set_acl = ext4_set_acl,
  3829. };