namei.c 112 KB

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