dir.c 116 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560
  1. /* -*- mode: c; c-basic-offset: 8; -*-
  2. * vim: noexpandtab sw=8 ts=8 sts=0:
  3. *
  4. * dir.c
  5. *
  6. * Creates, reads, walks and deletes directory-nodes
  7. *
  8. * Copyright (C) 2002, 2004 Oracle. All rights reserved.
  9. *
  10. * Portions of this code from linux/fs/ext3/dir.c
  11. *
  12. * Copyright (C) 1992, 1993, 1994, 1995
  13. * Remy Card (card@masi.ibp.fr)
  14. * Laboratoire MASI - Institut Blaise pascal
  15. * Universite Pierre et Marie Curie (Paris VI)
  16. *
  17. * from
  18. *
  19. * linux/fs/minix/dir.c
  20. *
  21. * Copyright (C) 1991, 1992 Linux Torvalds
  22. *
  23. * This program is free software; you can redistribute it and/or
  24. * modify it under the terms of the GNU General Public
  25. * License as published by the Free Software Foundation; either
  26. * version 2 of the License, or (at your option) any later version.
  27. *
  28. * This program is distributed in the hope that it will be useful,
  29. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  30. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  31. * General Public License for more details.
  32. *
  33. * You should have received a copy of the GNU General Public
  34. * License along with this program; if not, write to the
  35. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  36. * Boston, MA 021110-1307, USA.
  37. */
  38. #include <linux/fs.h>
  39. #include <linux/types.h>
  40. #include <linux/slab.h>
  41. #include <linux/highmem.h>
  42. #include <linux/quotaops.h>
  43. #include <linux/sort.h>
  44. #define MLOG_MASK_PREFIX ML_NAMEI
  45. #include <cluster/masklog.h>
  46. #include "ocfs2.h"
  47. #include "alloc.h"
  48. #include "blockcheck.h"
  49. #include "dir.h"
  50. #include "dlmglue.h"
  51. #include "extent_map.h"
  52. #include "file.h"
  53. #include "inode.h"
  54. #include "journal.h"
  55. #include "namei.h"
  56. #include "suballoc.h"
  57. #include "super.h"
  58. #include "sysfile.h"
  59. #include "uptodate.h"
  60. #include "buffer_head_io.h"
  61. #define NAMEI_RA_CHUNKS 2
  62. #define NAMEI_RA_BLOCKS 4
  63. #define NAMEI_RA_SIZE (NAMEI_RA_CHUNKS * NAMEI_RA_BLOCKS)
  64. #define NAMEI_RA_INDEX(c,b) (((c) * NAMEI_RA_BLOCKS) + (b))
  65. static unsigned char ocfs2_filetype_table[] = {
  66. DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
  67. };
  68. static int ocfs2_do_extend_dir(struct super_block *sb,
  69. handle_t *handle,
  70. struct inode *dir,
  71. struct buffer_head *parent_fe_bh,
  72. struct ocfs2_alloc_context *data_ac,
  73. struct ocfs2_alloc_context *meta_ac,
  74. struct buffer_head **new_bh);
  75. static int ocfs2_dir_indexed(struct inode *inode);
  76. /*
  77. * These are distinct checks because future versions of the file system will
  78. * want to have a trailing dirent structure independent of indexing.
  79. */
  80. static int ocfs2_supports_dir_trailer(struct inode *dir)
  81. {
  82. struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
  83. if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
  84. return 0;
  85. return ocfs2_meta_ecc(osb) || ocfs2_dir_indexed(dir);
  86. }
  87. /*
  88. * "new' here refers to the point at which we're creating a new
  89. * directory via "mkdir()", but also when we're expanding an inline
  90. * directory. In either case, we don't yet have the indexing bit set
  91. * on the directory, so the standard checks will fail in when metaecc
  92. * is turned off. Only directory-initialization type functions should
  93. * use this then. Everything else wants ocfs2_supports_dir_trailer()
  94. */
  95. static int ocfs2_new_dir_wants_trailer(struct inode *dir)
  96. {
  97. struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
  98. return ocfs2_meta_ecc(osb) ||
  99. ocfs2_supports_indexed_dirs(osb);
  100. }
  101. static inline unsigned int ocfs2_dir_trailer_blk_off(struct super_block *sb)
  102. {
  103. return sb->s_blocksize - sizeof(struct ocfs2_dir_block_trailer);
  104. }
  105. #define ocfs2_trailer_from_bh(_bh, _sb) ((struct ocfs2_dir_block_trailer *) ((_bh)->b_data + ocfs2_dir_trailer_blk_off((_sb))))
  106. /* XXX ocfs2_block_dqtrailer() is similar but not quite - can we make
  107. * them more consistent? */
  108. struct ocfs2_dir_block_trailer *ocfs2_dir_trailer_from_size(int blocksize,
  109. void *data)
  110. {
  111. char *p = data;
  112. p += blocksize - sizeof(struct ocfs2_dir_block_trailer);
  113. return (struct ocfs2_dir_block_trailer *)p;
  114. }
  115. /*
  116. * XXX: This is executed once on every dirent. We should consider optimizing
  117. * it.
  118. */
  119. static int ocfs2_skip_dir_trailer(struct inode *dir,
  120. struct ocfs2_dir_entry *de,
  121. unsigned long offset,
  122. unsigned long blklen)
  123. {
  124. unsigned long toff = blklen - sizeof(struct ocfs2_dir_block_trailer);
  125. if (!ocfs2_supports_dir_trailer(dir))
  126. return 0;
  127. if (offset != toff)
  128. return 0;
  129. return 1;
  130. }
  131. static void ocfs2_init_dir_trailer(struct inode *inode,
  132. struct buffer_head *bh, u16 rec_len)
  133. {
  134. struct ocfs2_dir_block_trailer *trailer;
  135. trailer = ocfs2_trailer_from_bh(bh, inode->i_sb);
  136. strcpy(trailer->db_signature, OCFS2_DIR_TRAILER_SIGNATURE);
  137. trailer->db_compat_rec_len =
  138. cpu_to_le16(sizeof(struct ocfs2_dir_block_trailer));
  139. trailer->db_parent_dinode = cpu_to_le64(OCFS2_I(inode)->ip_blkno);
  140. trailer->db_blkno = cpu_to_le64(bh->b_blocknr);
  141. trailer->db_free_rec_len = cpu_to_le16(rec_len);
  142. }
  143. /*
  144. * Link an unindexed block with a dir trailer structure into the index free
  145. * list. This function will modify dirdata_bh, but assumes you've already
  146. * passed it to the journal.
  147. */
  148. static int ocfs2_dx_dir_link_trailer(struct inode *dir, handle_t *handle,
  149. struct buffer_head *dx_root_bh,
  150. struct buffer_head *dirdata_bh)
  151. {
  152. int ret;
  153. struct ocfs2_dx_root_block *dx_root;
  154. struct ocfs2_dir_block_trailer *trailer;
  155. ret = ocfs2_journal_access_dr(handle, INODE_CACHE(dir), dx_root_bh,
  156. OCFS2_JOURNAL_ACCESS_WRITE);
  157. if (ret) {
  158. mlog_errno(ret);
  159. goto out;
  160. }
  161. trailer = ocfs2_trailer_from_bh(dirdata_bh, dir->i_sb);
  162. dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
  163. trailer->db_free_next = dx_root->dr_free_blk;
  164. dx_root->dr_free_blk = cpu_to_le64(dirdata_bh->b_blocknr);
  165. ocfs2_journal_dirty(handle, dx_root_bh);
  166. out:
  167. return ret;
  168. }
  169. static int ocfs2_free_list_at_root(struct ocfs2_dir_lookup_result *res)
  170. {
  171. return res->dl_prev_leaf_bh == NULL;
  172. }
  173. void ocfs2_free_dir_lookup_result(struct ocfs2_dir_lookup_result *res)
  174. {
  175. brelse(res->dl_dx_root_bh);
  176. brelse(res->dl_leaf_bh);
  177. brelse(res->dl_dx_leaf_bh);
  178. brelse(res->dl_prev_leaf_bh);
  179. }
  180. static int ocfs2_dir_indexed(struct inode *inode)
  181. {
  182. if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INDEXED_DIR_FL)
  183. return 1;
  184. return 0;
  185. }
  186. static inline int ocfs2_dx_root_inline(struct ocfs2_dx_root_block *dx_root)
  187. {
  188. return dx_root->dr_flags & OCFS2_DX_FLAG_INLINE;
  189. }
  190. /*
  191. * Hashing code adapted from ext3
  192. */
  193. #define DELTA 0x9E3779B9
  194. static void TEA_transform(__u32 buf[4], __u32 const in[])
  195. {
  196. __u32 sum = 0;
  197. __u32 b0 = buf[0], b1 = buf[1];
  198. __u32 a = in[0], b = in[1], c = in[2], d = in[3];
  199. int n = 16;
  200. do {
  201. sum += DELTA;
  202. b0 += ((b1 << 4)+a) ^ (b1+sum) ^ ((b1 >> 5)+b);
  203. b1 += ((b0 << 4)+c) ^ (b0+sum) ^ ((b0 >> 5)+d);
  204. } while (--n);
  205. buf[0] += b0;
  206. buf[1] += b1;
  207. }
  208. static void str2hashbuf(const char *msg, int len, __u32 *buf, int num)
  209. {
  210. __u32 pad, val;
  211. int i;
  212. pad = (__u32)len | ((__u32)len << 8);
  213. pad |= pad << 16;
  214. val = pad;
  215. if (len > num*4)
  216. len = num * 4;
  217. for (i = 0; i < len; i++) {
  218. if ((i % 4) == 0)
  219. val = pad;
  220. val = msg[i] + (val << 8);
  221. if ((i % 4) == 3) {
  222. *buf++ = val;
  223. val = pad;
  224. num--;
  225. }
  226. }
  227. if (--num >= 0)
  228. *buf++ = val;
  229. while (--num >= 0)
  230. *buf++ = pad;
  231. }
  232. static void ocfs2_dx_dir_name_hash(struct inode *dir, const char *name, int len,
  233. struct ocfs2_dx_hinfo *hinfo)
  234. {
  235. struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
  236. const char *p;
  237. __u32 in[8], buf[4];
  238. /*
  239. * XXX: Is this really necessary, if the index is never looked
  240. * at by readdir? Is a hash value of '0' a bad idea?
  241. */
  242. if ((len == 1 && !strncmp(".", name, 1)) ||
  243. (len == 2 && !strncmp("..", name, 2))) {
  244. buf[0] = buf[1] = 0;
  245. goto out;
  246. }
  247. #ifdef OCFS2_DEBUG_DX_DIRS
  248. /*
  249. * This makes it very easy to debug indexing problems. We
  250. * should never allow this to be selected without hand editing
  251. * this file though.
  252. */
  253. buf[0] = buf[1] = len;
  254. goto out;
  255. #endif
  256. memcpy(buf, osb->osb_dx_seed, sizeof(buf));
  257. p = name;
  258. while (len > 0) {
  259. str2hashbuf(p, len, in, 4);
  260. TEA_transform(buf, in);
  261. len -= 16;
  262. p += 16;
  263. }
  264. out:
  265. hinfo->major_hash = buf[0];
  266. hinfo->minor_hash = buf[1];
  267. }
  268. /*
  269. * bh passed here can be an inode block or a dir data block, depending
  270. * on the inode inline data flag.
  271. */
  272. static int ocfs2_check_dir_entry(struct inode * dir,
  273. struct ocfs2_dir_entry * de,
  274. struct buffer_head * bh,
  275. unsigned long offset)
  276. {
  277. const char *error_msg = NULL;
  278. const int rlen = le16_to_cpu(de->rec_len);
  279. if (rlen < OCFS2_DIR_REC_LEN(1))
  280. error_msg = "rec_len is smaller than minimal";
  281. else if (rlen % 4 != 0)
  282. error_msg = "rec_len % 4 != 0";
  283. else if (rlen < OCFS2_DIR_REC_LEN(de->name_len))
  284. error_msg = "rec_len is too small for name_len";
  285. else if (((char *) de - bh->b_data) + rlen > dir->i_sb->s_blocksize)
  286. error_msg = "directory entry across blocks";
  287. if (error_msg != NULL)
  288. mlog(ML_ERROR, "bad entry in directory #%llu: %s - "
  289. "offset=%lu, inode=%llu, rec_len=%d, name_len=%d\n",
  290. (unsigned long long)OCFS2_I(dir)->ip_blkno, error_msg,
  291. offset, (unsigned long long)le64_to_cpu(de->inode), rlen,
  292. de->name_len);
  293. return error_msg == NULL ? 1 : 0;
  294. }
  295. static inline int ocfs2_match(int len,
  296. const char * const name,
  297. struct ocfs2_dir_entry *de)
  298. {
  299. if (len != de->name_len)
  300. return 0;
  301. if (!de->inode)
  302. return 0;
  303. return !memcmp(name, de->name, len);
  304. }
  305. /*
  306. * Returns 0 if not found, -1 on failure, and 1 on success
  307. */
  308. static int inline ocfs2_search_dirblock(struct buffer_head *bh,
  309. struct inode *dir,
  310. const char *name, int namelen,
  311. unsigned long offset,
  312. char *first_de,
  313. unsigned int bytes,
  314. struct ocfs2_dir_entry **res_dir)
  315. {
  316. struct ocfs2_dir_entry *de;
  317. char *dlimit, *de_buf;
  318. int de_len;
  319. int ret = 0;
  320. mlog_entry_void();
  321. de_buf = first_de;
  322. dlimit = de_buf + bytes;
  323. while (de_buf < dlimit) {
  324. /* this code is executed quadratically often */
  325. /* do minimal checking `by hand' */
  326. de = (struct ocfs2_dir_entry *) de_buf;
  327. if (de_buf + namelen <= dlimit &&
  328. ocfs2_match(namelen, name, de)) {
  329. /* found a match - just to be sure, do a full check */
  330. if (!ocfs2_check_dir_entry(dir, de, bh, offset)) {
  331. ret = -1;
  332. goto bail;
  333. }
  334. *res_dir = de;
  335. ret = 1;
  336. goto bail;
  337. }
  338. /* prevent looping on a bad block */
  339. de_len = le16_to_cpu(de->rec_len);
  340. if (de_len <= 0) {
  341. ret = -1;
  342. goto bail;
  343. }
  344. de_buf += de_len;
  345. offset += de_len;
  346. }
  347. bail:
  348. mlog_exit(ret);
  349. return ret;
  350. }
  351. static struct buffer_head *ocfs2_find_entry_id(const char *name,
  352. int namelen,
  353. struct inode *dir,
  354. struct ocfs2_dir_entry **res_dir)
  355. {
  356. int ret, found;
  357. struct buffer_head *di_bh = NULL;
  358. struct ocfs2_dinode *di;
  359. struct ocfs2_inline_data *data;
  360. ret = ocfs2_read_inode_block(dir, &di_bh);
  361. if (ret) {
  362. mlog_errno(ret);
  363. goto out;
  364. }
  365. di = (struct ocfs2_dinode *)di_bh->b_data;
  366. data = &di->id2.i_data;
  367. found = ocfs2_search_dirblock(di_bh, dir, name, namelen, 0,
  368. data->id_data, i_size_read(dir), res_dir);
  369. if (found == 1)
  370. return di_bh;
  371. brelse(di_bh);
  372. out:
  373. return NULL;
  374. }
  375. static int ocfs2_validate_dir_block(struct super_block *sb,
  376. struct buffer_head *bh)
  377. {
  378. int rc;
  379. struct ocfs2_dir_block_trailer *trailer =
  380. ocfs2_trailer_from_bh(bh, sb);
  381. /*
  382. * We don't validate dirents here, that's handled
  383. * in-place when the code walks them.
  384. */
  385. mlog(0, "Validating dirblock %llu\n",
  386. (unsigned long long)bh->b_blocknr);
  387. BUG_ON(!buffer_uptodate(bh));
  388. /*
  389. * If the ecc fails, we return the error but otherwise
  390. * leave the filesystem running. We know any error is
  391. * local to this block.
  392. *
  393. * Note that we are safe to call this even if the directory
  394. * doesn't have a trailer. Filesystems without metaecc will do
  395. * nothing, and filesystems with it will have one.
  396. */
  397. rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &trailer->db_check);
  398. if (rc)
  399. mlog(ML_ERROR, "Checksum failed for dinode %llu\n",
  400. (unsigned long long)bh->b_blocknr);
  401. return rc;
  402. }
  403. /*
  404. * Validate a directory trailer.
  405. *
  406. * We check the trailer here rather than in ocfs2_validate_dir_block()
  407. * because that function doesn't have the inode to test.
  408. */
  409. static int ocfs2_check_dir_trailer(struct inode *dir, struct buffer_head *bh)
  410. {
  411. int rc = 0;
  412. struct ocfs2_dir_block_trailer *trailer;
  413. trailer = ocfs2_trailer_from_bh(bh, dir->i_sb);
  414. if (!OCFS2_IS_VALID_DIR_TRAILER(trailer)) {
  415. rc = -EINVAL;
  416. ocfs2_error(dir->i_sb,
  417. "Invalid dirblock #%llu: "
  418. "signature = %.*s\n",
  419. (unsigned long long)bh->b_blocknr, 7,
  420. trailer->db_signature);
  421. goto out;
  422. }
  423. if (le64_to_cpu(trailer->db_blkno) != bh->b_blocknr) {
  424. rc = -EINVAL;
  425. ocfs2_error(dir->i_sb,
  426. "Directory block #%llu has an invalid "
  427. "db_blkno of %llu",
  428. (unsigned long long)bh->b_blocknr,
  429. (unsigned long long)le64_to_cpu(trailer->db_blkno));
  430. goto out;
  431. }
  432. if (le64_to_cpu(trailer->db_parent_dinode) !=
  433. OCFS2_I(dir)->ip_blkno) {
  434. rc = -EINVAL;
  435. ocfs2_error(dir->i_sb,
  436. "Directory block #%llu on dinode "
  437. "#%llu has an invalid parent_dinode "
  438. "of %llu",
  439. (unsigned long long)bh->b_blocknr,
  440. (unsigned long long)OCFS2_I(dir)->ip_blkno,
  441. (unsigned long long)le64_to_cpu(trailer->db_blkno));
  442. goto out;
  443. }
  444. out:
  445. return rc;
  446. }
  447. /*
  448. * This function forces all errors to -EIO for consistency with its
  449. * predecessor, ocfs2_bread(). We haven't audited what returning the
  450. * real error codes would do to callers. We log the real codes with
  451. * mlog_errno() before we squash them.
  452. */
  453. static int ocfs2_read_dir_block(struct inode *inode, u64 v_block,
  454. struct buffer_head **bh, int flags)
  455. {
  456. int rc = 0;
  457. struct buffer_head *tmp = *bh;
  458. rc = ocfs2_read_virt_blocks(inode, v_block, 1, &tmp, flags,
  459. ocfs2_validate_dir_block);
  460. if (rc) {
  461. mlog_errno(rc);
  462. goto out;
  463. }
  464. if (!(flags & OCFS2_BH_READAHEAD) &&
  465. ocfs2_supports_dir_trailer(inode)) {
  466. rc = ocfs2_check_dir_trailer(inode, tmp);
  467. if (rc) {
  468. if (!*bh)
  469. brelse(tmp);
  470. mlog_errno(rc);
  471. goto out;
  472. }
  473. }
  474. /* If ocfs2_read_virt_blocks() got us a new bh, pass it up. */
  475. if (!*bh)
  476. *bh = tmp;
  477. out:
  478. return rc ? -EIO : 0;
  479. }
  480. /*
  481. * Read the block at 'phys' which belongs to this directory
  482. * inode. This function does no virtual->physical block translation -
  483. * what's passed in is assumed to be a valid directory block.
  484. */
  485. static int ocfs2_read_dir_block_direct(struct inode *dir, u64 phys,
  486. struct buffer_head **bh)
  487. {
  488. int ret;
  489. struct buffer_head *tmp = *bh;
  490. ret = ocfs2_read_block(INODE_CACHE(dir), phys, &tmp,
  491. ocfs2_validate_dir_block);
  492. if (ret) {
  493. mlog_errno(ret);
  494. goto out;
  495. }
  496. if (ocfs2_supports_dir_trailer(dir)) {
  497. ret = ocfs2_check_dir_trailer(dir, tmp);
  498. if (ret) {
  499. if (!*bh)
  500. brelse(tmp);
  501. mlog_errno(ret);
  502. goto out;
  503. }
  504. }
  505. if (!ret && !*bh)
  506. *bh = tmp;
  507. out:
  508. return ret;
  509. }
  510. static int ocfs2_validate_dx_root(struct super_block *sb,
  511. struct buffer_head *bh)
  512. {
  513. int ret;
  514. struct ocfs2_dx_root_block *dx_root;
  515. BUG_ON(!buffer_uptodate(bh));
  516. dx_root = (struct ocfs2_dx_root_block *) bh->b_data;
  517. ret = ocfs2_validate_meta_ecc(sb, bh->b_data, &dx_root->dr_check);
  518. if (ret) {
  519. mlog(ML_ERROR,
  520. "Checksum failed for dir index root block %llu\n",
  521. (unsigned long long)bh->b_blocknr);
  522. return ret;
  523. }
  524. if (!OCFS2_IS_VALID_DX_ROOT(dx_root)) {
  525. ocfs2_error(sb,
  526. "Dir Index Root # %llu has bad signature %.*s",
  527. (unsigned long long)le64_to_cpu(dx_root->dr_blkno),
  528. 7, dx_root->dr_signature);
  529. return -EINVAL;
  530. }
  531. return 0;
  532. }
  533. static int ocfs2_read_dx_root(struct inode *dir, struct ocfs2_dinode *di,
  534. struct buffer_head **dx_root_bh)
  535. {
  536. int ret;
  537. u64 blkno = le64_to_cpu(di->i_dx_root);
  538. struct buffer_head *tmp = *dx_root_bh;
  539. ret = ocfs2_read_block(INODE_CACHE(dir), blkno, &tmp,
  540. ocfs2_validate_dx_root);
  541. /* If ocfs2_read_block() got us a new bh, pass it up. */
  542. if (!ret && !*dx_root_bh)
  543. *dx_root_bh = tmp;
  544. return ret;
  545. }
  546. static int ocfs2_validate_dx_leaf(struct super_block *sb,
  547. struct buffer_head *bh)
  548. {
  549. int ret;
  550. struct ocfs2_dx_leaf *dx_leaf = (struct ocfs2_dx_leaf *)bh->b_data;
  551. BUG_ON(!buffer_uptodate(bh));
  552. ret = ocfs2_validate_meta_ecc(sb, bh->b_data, &dx_leaf->dl_check);
  553. if (ret) {
  554. mlog(ML_ERROR,
  555. "Checksum failed for dir index leaf block %llu\n",
  556. (unsigned long long)bh->b_blocknr);
  557. return ret;
  558. }
  559. if (!OCFS2_IS_VALID_DX_LEAF(dx_leaf)) {
  560. ocfs2_error(sb, "Dir Index Leaf has bad signature %.*s",
  561. 7, dx_leaf->dl_signature);
  562. return -EROFS;
  563. }
  564. return 0;
  565. }
  566. static int ocfs2_read_dx_leaf(struct inode *dir, u64 blkno,
  567. struct buffer_head **dx_leaf_bh)
  568. {
  569. int ret;
  570. struct buffer_head *tmp = *dx_leaf_bh;
  571. ret = ocfs2_read_block(INODE_CACHE(dir), blkno, &tmp,
  572. ocfs2_validate_dx_leaf);
  573. /* If ocfs2_read_block() got us a new bh, pass it up. */
  574. if (!ret && !*dx_leaf_bh)
  575. *dx_leaf_bh = tmp;
  576. return ret;
  577. }
  578. /*
  579. * Read a series of dx_leaf blocks. This expects all buffer_head
  580. * pointers to be NULL on function entry.
  581. */
  582. static int ocfs2_read_dx_leaves(struct inode *dir, u64 start, int num,
  583. struct buffer_head **dx_leaf_bhs)
  584. {
  585. int ret;
  586. ret = ocfs2_read_blocks(INODE_CACHE(dir), start, num, dx_leaf_bhs, 0,
  587. ocfs2_validate_dx_leaf);
  588. if (ret)
  589. mlog_errno(ret);
  590. return ret;
  591. }
  592. static struct buffer_head *ocfs2_find_entry_el(const char *name, int namelen,
  593. struct inode *dir,
  594. struct ocfs2_dir_entry **res_dir)
  595. {
  596. struct super_block *sb;
  597. struct buffer_head *bh_use[NAMEI_RA_SIZE];
  598. struct buffer_head *bh, *ret = NULL;
  599. unsigned long start, block, b;
  600. int ra_max = 0; /* Number of bh's in the readahead
  601. buffer, bh_use[] */
  602. int ra_ptr = 0; /* Current index into readahead
  603. buffer */
  604. int num = 0;
  605. int nblocks, i, err;
  606. mlog_entry_void();
  607. sb = dir->i_sb;
  608. nblocks = i_size_read(dir) >> sb->s_blocksize_bits;
  609. start = OCFS2_I(dir)->ip_dir_start_lookup;
  610. if (start >= nblocks)
  611. start = 0;
  612. block = start;
  613. restart:
  614. do {
  615. /*
  616. * We deal with the read-ahead logic here.
  617. */
  618. if (ra_ptr >= ra_max) {
  619. /* Refill the readahead buffer */
  620. ra_ptr = 0;
  621. b = block;
  622. for (ra_max = 0; ra_max < NAMEI_RA_SIZE; ra_max++) {
  623. /*
  624. * Terminate if we reach the end of the
  625. * directory and must wrap, or if our
  626. * search has finished at this block.
  627. */
  628. if (b >= nblocks || (num && block == start)) {
  629. bh_use[ra_max] = NULL;
  630. break;
  631. }
  632. num++;
  633. bh = NULL;
  634. err = ocfs2_read_dir_block(dir, b++, &bh,
  635. OCFS2_BH_READAHEAD);
  636. bh_use[ra_max] = bh;
  637. }
  638. }
  639. if ((bh = bh_use[ra_ptr++]) == NULL)
  640. goto next;
  641. if (ocfs2_read_dir_block(dir, block, &bh, 0)) {
  642. /* read error, skip block & hope for the best.
  643. * ocfs2_read_dir_block() has released the bh. */
  644. ocfs2_error(dir->i_sb, "reading directory %llu, "
  645. "offset %lu\n",
  646. (unsigned long long)OCFS2_I(dir)->ip_blkno,
  647. block);
  648. goto next;
  649. }
  650. i = ocfs2_search_dirblock(bh, dir, name, namelen,
  651. block << sb->s_blocksize_bits,
  652. bh->b_data, sb->s_blocksize,
  653. res_dir);
  654. if (i == 1) {
  655. OCFS2_I(dir)->ip_dir_start_lookup = block;
  656. ret = bh;
  657. goto cleanup_and_exit;
  658. } else {
  659. brelse(bh);
  660. if (i < 0)
  661. goto cleanup_and_exit;
  662. }
  663. next:
  664. if (++block >= nblocks)
  665. block = 0;
  666. } while (block != start);
  667. /*
  668. * If the directory has grown while we were searching, then
  669. * search the last part of the directory before giving up.
  670. */
  671. block = nblocks;
  672. nblocks = i_size_read(dir) >> sb->s_blocksize_bits;
  673. if (block < nblocks) {
  674. start = 0;
  675. goto restart;
  676. }
  677. cleanup_and_exit:
  678. /* Clean up the read-ahead blocks */
  679. for (; ra_ptr < ra_max; ra_ptr++)
  680. brelse(bh_use[ra_ptr]);
  681. mlog_exit_ptr(ret);
  682. return ret;
  683. }
  684. static int ocfs2_dx_dir_lookup_rec(struct inode *inode,
  685. struct ocfs2_extent_list *el,
  686. u32 major_hash,
  687. u32 *ret_cpos,
  688. u64 *ret_phys_blkno,
  689. unsigned int *ret_clen)
  690. {
  691. int ret = 0, i, found;
  692. struct buffer_head *eb_bh = NULL;
  693. struct ocfs2_extent_block *eb;
  694. struct ocfs2_extent_rec *rec = NULL;
  695. if (el->l_tree_depth) {
  696. ret = ocfs2_find_leaf(INODE_CACHE(inode), el, major_hash,
  697. &eb_bh);
  698. if (ret) {
  699. mlog_errno(ret);
  700. goto out;
  701. }
  702. eb = (struct ocfs2_extent_block *) eb_bh->b_data;
  703. el = &eb->h_list;
  704. if (el->l_tree_depth) {
  705. ocfs2_error(inode->i_sb,
  706. "Inode %lu has non zero tree depth in "
  707. "btree tree block %llu\n", inode->i_ino,
  708. (unsigned long long)eb_bh->b_blocknr);
  709. ret = -EROFS;
  710. goto out;
  711. }
  712. }
  713. found = 0;
  714. for (i = le16_to_cpu(el->l_next_free_rec) - 1; i >= 0; i--) {
  715. rec = &el->l_recs[i];
  716. if (le32_to_cpu(rec->e_cpos) <= major_hash) {
  717. found = 1;
  718. break;
  719. }
  720. }
  721. if (!found) {
  722. ocfs2_error(inode->i_sb, "Inode %lu has bad extent "
  723. "record (%u, %u, 0) in btree", inode->i_ino,
  724. le32_to_cpu(rec->e_cpos),
  725. ocfs2_rec_clusters(el, rec));
  726. ret = -EROFS;
  727. goto out;
  728. }
  729. if (ret_phys_blkno)
  730. *ret_phys_blkno = le64_to_cpu(rec->e_blkno);
  731. if (ret_cpos)
  732. *ret_cpos = le32_to_cpu(rec->e_cpos);
  733. if (ret_clen)
  734. *ret_clen = le16_to_cpu(rec->e_leaf_clusters);
  735. out:
  736. brelse(eb_bh);
  737. return ret;
  738. }
  739. /*
  740. * Returns the block index, from the start of the cluster which this
  741. * hash belongs too.
  742. */
  743. static inline unsigned int __ocfs2_dx_dir_hash_idx(struct ocfs2_super *osb,
  744. u32 minor_hash)
  745. {
  746. return minor_hash & osb->osb_dx_mask;
  747. }
  748. static inline unsigned int ocfs2_dx_dir_hash_idx(struct ocfs2_super *osb,
  749. struct ocfs2_dx_hinfo *hinfo)
  750. {
  751. return __ocfs2_dx_dir_hash_idx(osb, hinfo->minor_hash);
  752. }
  753. static int ocfs2_dx_dir_lookup(struct inode *inode,
  754. struct ocfs2_extent_list *el,
  755. struct ocfs2_dx_hinfo *hinfo,
  756. u32 *ret_cpos,
  757. u64 *ret_phys_blkno)
  758. {
  759. int ret = 0;
  760. unsigned int cend, uninitialized_var(clen);
  761. u32 uninitialized_var(cpos);
  762. u64 uninitialized_var(blkno);
  763. u32 name_hash = hinfo->major_hash;
  764. ret = ocfs2_dx_dir_lookup_rec(inode, el, name_hash, &cpos, &blkno,
  765. &clen);
  766. if (ret) {
  767. mlog_errno(ret);
  768. goto out;
  769. }
  770. cend = cpos + clen;
  771. if (name_hash >= cend) {
  772. /* We want the last cluster */
  773. blkno += ocfs2_clusters_to_blocks(inode->i_sb, clen - 1);
  774. cpos += clen - 1;
  775. } else {
  776. blkno += ocfs2_clusters_to_blocks(inode->i_sb,
  777. name_hash - cpos);
  778. cpos = name_hash;
  779. }
  780. /*
  781. * We now have the cluster which should hold our entry. To
  782. * find the exact block from the start of the cluster to
  783. * search, we take the lower bits of the hash.
  784. */
  785. blkno += ocfs2_dx_dir_hash_idx(OCFS2_SB(inode->i_sb), hinfo);
  786. if (ret_phys_blkno)
  787. *ret_phys_blkno = blkno;
  788. if (ret_cpos)
  789. *ret_cpos = cpos;
  790. out:
  791. return ret;
  792. }
  793. static int ocfs2_dx_dir_search(const char *name, int namelen,
  794. struct inode *dir,
  795. struct ocfs2_dx_root_block *dx_root,
  796. struct ocfs2_dir_lookup_result *res)
  797. {
  798. int ret, i, found;
  799. u64 uninitialized_var(phys);
  800. struct buffer_head *dx_leaf_bh = NULL;
  801. struct ocfs2_dx_leaf *dx_leaf;
  802. struct ocfs2_dx_entry *dx_entry = NULL;
  803. struct buffer_head *dir_ent_bh = NULL;
  804. struct ocfs2_dir_entry *dir_ent = NULL;
  805. struct ocfs2_dx_hinfo *hinfo = &res->dl_hinfo;
  806. struct ocfs2_extent_list *dr_el;
  807. struct ocfs2_dx_entry_list *entry_list;
  808. ocfs2_dx_dir_name_hash(dir, name, namelen, &res->dl_hinfo);
  809. if (ocfs2_dx_root_inline(dx_root)) {
  810. entry_list = &dx_root->dr_entries;
  811. goto search;
  812. }
  813. dr_el = &dx_root->dr_list;
  814. ret = ocfs2_dx_dir_lookup(dir, dr_el, hinfo, NULL, &phys);
  815. if (ret) {
  816. mlog_errno(ret);
  817. goto out;
  818. }
  819. mlog(0, "Dir %llu: name: \"%.*s\", lookup of hash: %u.0x%x "
  820. "returns: %llu\n",
  821. (unsigned long long)OCFS2_I(dir)->ip_blkno,
  822. namelen, name, hinfo->major_hash, hinfo->minor_hash,
  823. (unsigned long long)phys);
  824. ret = ocfs2_read_dx_leaf(dir, phys, &dx_leaf_bh);
  825. if (ret) {
  826. mlog_errno(ret);
  827. goto out;
  828. }
  829. dx_leaf = (struct ocfs2_dx_leaf *) dx_leaf_bh->b_data;
  830. mlog(0, "leaf info: num_used: %d, count: %d\n",
  831. le16_to_cpu(dx_leaf->dl_list.de_num_used),
  832. le16_to_cpu(dx_leaf->dl_list.de_count));
  833. entry_list = &dx_leaf->dl_list;
  834. search:
  835. /*
  836. * Empty leaf is legal, so no need to check for that.
  837. */
  838. found = 0;
  839. for (i = 0; i < le16_to_cpu(entry_list->de_num_used); i++) {
  840. dx_entry = &entry_list->de_entries[i];
  841. if (hinfo->major_hash != le32_to_cpu(dx_entry->dx_major_hash)
  842. || hinfo->minor_hash != le32_to_cpu(dx_entry->dx_minor_hash))
  843. continue;
  844. /*
  845. * Search unindexed leaf block now. We're not
  846. * guaranteed to find anything.
  847. */
  848. ret = ocfs2_read_dir_block_direct(dir,
  849. le64_to_cpu(dx_entry->dx_dirent_blk),
  850. &dir_ent_bh);
  851. if (ret) {
  852. mlog_errno(ret);
  853. goto out;
  854. }
  855. /*
  856. * XXX: We should check the unindexed block here,
  857. * before using it.
  858. */
  859. found = ocfs2_search_dirblock(dir_ent_bh, dir, name, namelen,
  860. 0, dir_ent_bh->b_data,
  861. dir->i_sb->s_blocksize, &dir_ent);
  862. if (found == 1)
  863. break;
  864. if (found == -1) {
  865. /* This means we found a bad directory entry. */
  866. ret = -EIO;
  867. mlog_errno(ret);
  868. goto out;
  869. }
  870. brelse(dir_ent_bh);
  871. dir_ent_bh = NULL;
  872. }
  873. if (found <= 0) {
  874. ret = -ENOENT;
  875. goto out;
  876. }
  877. res->dl_leaf_bh = dir_ent_bh;
  878. res->dl_entry = dir_ent;
  879. res->dl_dx_leaf_bh = dx_leaf_bh;
  880. res->dl_dx_entry = dx_entry;
  881. ret = 0;
  882. out:
  883. if (ret) {
  884. brelse(dx_leaf_bh);
  885. brelse(dir_ent_bh);
  886. }
  887. return ret;
  888. }
  889. static int ocfs2_find_entry_dx(const char *name, int namelen,
  890. struct inode *dir,
  891. struct ocfs2_dir_lookup_result *lookup)
  892. {
  893. int ret;
  894. struct buffer_head *di_bh = NULL;
  895. struct ocfs2_dinode *di;
  896. struct buffer_head *dx_root_bh = NULL;
  897. struct ocfs2_dx_root_block *dx_root;
  898. ret = ocfs2_read_inode_block(dir, &di_bh);
  899. if (ret) {
  900. mlog_errno(ret);
  901. goto out;
  902. }
  903. di = (struct ocfs2_dinode *)di_bh->b_data;
  904. ret = ocfs2_read_dx_root(dir, di, &dx_root_bh);
  905. if (ret) {
  906. mlog_errno(ret);
  907. goto out;
  908. }
  909. dx_root = (struct ocfs2_dx_root_block *) dx_root_bh->b_data;
  910. ret = ocfs2_dx_dir_search(name, namelen, dir, dx_root, lookup);
  911. if (ret) {
  912. if (ret != -ENOENT)
  913. mlog_errno(ret);
  914. goto out;
  915. }
  916. lookup->dl_dx_root_bh = dx_root_bh;
  917. dx_root_bh = NULL;
  918. out:
  919. brelse(di_bh);
  920. brelse(dx_root_bh);
  921. return ret;
  922. }
  923. /*
  924. * Try to find an entry of the provided name within 'dir'.
  925. *
  926. * If nothing was found, -ENOENT is returned. Otherwise, zero is
  927. * returned and the struct 'res' will contain information useful to
  928. * other directory manipulation functions.
  929. *
  930. * Caller can NOT assume anything about the contents of the
  931. * buffer_heads - they are passed back only so that it can be passed
  932. * into any one of the manipulation functions (add entry, delete
  933. * entry, etc). As an example, bh in the extent directory case is a
  934. * data block, in the inline-data case it actually points to an inode,
  935. * in the indexed directory case, multiple buffers are involved.
  936. */
  937. int ocfs2_find_entry(const char *name, int namelen,
  938. struct inode *dir, struct ocfs2_dir_lookup_result *lookup)
  939. {
  940. struct buffer_head *bh;
  941. struct ocfs2_dir_entry *res_dir = NULL;
  942. if (ocfs2_dir_indexed(dir))
  943. return ocfs2_find_entry_dx(name, namelen, dir, lookup);
  944. /*
  945. * The unindexed dir code only uses part of the lookup
  946. * structure, so there's no reason to push it down further
  947. * than this.
  948. */
  949. if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
  950. bh = ocfs2_find_entry_id(name, namelen, dir, &res_dir);
  951. else
  952. bh = ocfs2_find_entry_el(name, namelen, dir, &res_dir);
  953. if (bh == NULL)
  954. return -ENOENT;
  955. lookup->dl_leaf_bh = bh;
  956. lookup->dl_entry = res_dir;
  957. return 0;
  958. }
  959. /*
  960. * Update inode number and type of a previously found directory entry.
  961. */
  962. int ocfs2_update_entry(struct inode *dir, handle_t *handle,
  963. struct ocfs2_dir_lookup_result *res,
  964. struct inode *new_entry_inode)
  965. {
  966. int ret;
  967. ocfs2_journal_access_func access = ocfs2_journal_access_db;
  968. struct ocfs2_dir_entry *de = res->dl_entry;
  969. struct buffer_head *de_bh = res->dl_leaf_bh;
  970. /*
  971. * The same code works fine for both inline-data and extent
  972. * based directories, so no need to split this up. The only
  973. * difference is the journal_access function.
  974. */
  975. if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
  976. access = ocfs2_journal_access_di;
  977. ret = access(handle, INODE_CACHE(dir), de_bh,
  978. OCFS2_JOURNAL_ACCESS_WRITE);
  979. if (ret) {
  980. mlog_errno(ret);
  981. goto out;
  982. }
  983. de->inode = cpu_to_le64(OCFS2_I(new_entry_inode)->ip_blkno);
  984. ocfs2_set_de_type(de, new_entry_inode->i_mode);
  985. ocfs2_journal_dirty(handle, de_bh);
  986. out:
  987. return ret;
  988. }
  989. /*
  990. * __ocfs2_delete_entry deletes a directory entry by merging it with the
  991. * previous entry
  992. */
  993. static int __ocfs2_delete_entry(handle_t *handle, struct inode *dir,
  994. struct ocfs2_dir_entry *de_del,
  995. struct buffer_head *bh, char *first_de,
  996. unsigned int bytes)
  997. {
  998. struct ocfs2_dir_entry *de, *pde;
  999. int i, status = -ENOENT;
  1000. ocfs2_journal_access_func access = ocfs2_journal_access_db;
  1001. mlog_entry("(0x%p, 0x%p, 0x%p, 0x%p)\n", handle, dir, de_del, bh);
  1002. if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
  1003. access = ocfs2_journal_access_di;
  1004. i = 0;
  1005. pde = NULL;
  1006. de = (struct ocfs2_dir_entry *) first_de;
  1007. while (i < bytes) {
  1008. if (!ocfs2_check_dir_entry(dir, de, bh, i)) {
  1009. status = -EIO;
  1010. mlog_errno(status);
  1011. goto bail;
  1012. }
  1013. if (de == de_del) {
  1014. status = access(handle, INODE_CACHE(dir), bh,
  1015. OCFS2_JOURNAL_ACCESS_WRITE);
  1016. if (status < 0) {
  1017. status = -EIO;
  1018. mlog_errno(status);
  1019. goto bail;
  1020. }
  1021. if (pde)
  1022. le16_add_cpu(&pde->rec_len,
  1023. le16_to_cpu(de->rec_len));
  1024. else
  1025. de->inode = 0;
  1026. dir->i_version++;
  1027. ocfs2_journal_dirty(handle, bh);
  1028. goto bail;
  1029. }
  1030. i += le16_to_cpu(de->rec_len);
  1031. pde = de;
  1032. de = (struct ocfs2_dir_entry *)((char *)de + le16_to_cpu(de->rec_len));
  1033. }
  1034. bail:
  1035. mlog_exit(status);
  1036. return status;
  1037. }
  1038. static unsigned int ocfs2_figure_dirent_hole(struct ocfs2_dir_entry *de)
  1039. {
  1040. unsigned int hole;
  1041. if (le64_to_cpu(de->inode) == 0)
  1042. hole = le16_to_cpu(de->rec_len);
  1043. else
  1044. hole = le16_to_cpu(de->rec_len) -
  1045. OCFS2_DIR_REC_LEN(de->name_len);
  1046. return hole;
  1047. }
  1048. static int ocfs2_find_max_rec_len(struct super_block *sb,
  1049. struct buffer_head *dirblock_bh)
  1050. {
  1051. int size, this_hole, largest_hole = 0;
  1052. char *trailer, *de_buf, *limit, *start = dirblock_bh->b_data;
  1053. struct ocfs2_dir_entry *de;
  1054. trailer = (char *)ocfs2_trailer_from_bh(dirblock_bh, sb);
  1055. size = ocfs2_dir_trailer_blk_off(sb);
  1056. limit = start + size;
  1057. de_buf = start;
  1058. de = (struct ocfs2_dir_entry *)de_buf;
  1059. do {
  1060. if (de_buf != trailer) {
  1061. this_hole = ocfs2_figure_dirent_hole(de);
  1062. if (this_hole > largest_hole)
  1063. largest_hole = this_hole;
  1064. }
  1065. de_buf += le16_to_cpu(de->rec_len);
  1066. de = (struct ocfs2_dir_entry *)de_buf;
  1067. } while (de_buf < limit);
  1068. if (largest_hole >= OCFS2_DIR_MIN_REC_LEN)
  1069. return largest_hole;
  1070. return 0;
  1071. }
  1072. static void ocfs2_dx_list_remove_entry(struct ocfs2_dx_entry_list *entry_list,
  1073. int index)
  1074. {
  1075. int num_used = le16_to_cpu(entry_list->de_num_used);
  1076. if (num_used == 1 || index == (num_used - 1))
  1077. goto clear;
  1078. memmove(&entry_list->de_entries[index],
  1079. &entry_list->de_entries[index + 1],
  1080. (num_used - index - 1)*sizeof(struct ocfs2_dx_entry));
  1081. clear:
  1082. num_used--;
  1083. memset(&entry_list->de_entries[num_used], 0,
  1084. sizeof(struct ocfs2_dx_entry));
  1085. entry_list->de_num_used = cpu_to_le16(num_used);
  1086. }
  1087. static int ocfs2_delete_entry_dx(handle_t *handle, struct inode *dir,
  1088. struct ocfs2_dir_lookup_result *lookup)
  1089. {
  1090. int ret, index, max_rec_len, add_to_free_list = 0;
  1091. struct buffer_head *dx_root_bh = lookup->dl_dx_root_bh;
  1092. struct buffer_head *leaf_bh = lookup->dl_leaf_bh;
  1093. struct ocfs2_dx_leaf *dx_leaf;
  1094. struct ocfs2_dx_entry *dx_entry = lookup->dl_dx_entry;
  1095. struct ocfs2_dir_block_trailer *trailer;
  1096. struct ocfs2_dx_root_block *dx_root;
  1097. struct ocfs2_dx_entry_list *entry_list;
  1098. /*
  1099. * This function gets a bit messy because we might have to
  1100. * modify the root block, regardless of whether the indexed
  1101. * entries are stored inline.
  1102. */
  1103. /*
  1104. * *Only* set 'entry_list' here, based on where we're looking
  1105. * for the indexed entries. Later, we might still want to
  1106. * journal both blocks, based on free list state.
  1107. */
  1108. dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
  1109. if (ocfs2_dx_root_inline(dx_root)) {
  1110. entry_list = &dx_root->dr_entries;
  1111. } else {
  1112. dx_leaf = (struct ocfs2_dx_leaf *) lookup->dl_dx_leaf_bh->b_data;
  1113. entry_list = &dx_leaf->dl_list;
  1114. }
  1115. /* Neither of these are a disk corruption - that should have
  1116. * been caught by lookup, before we got here. */
  1117. BUG_ON(le16_to_cpu(entry_list->de_count) <= 0);
  1118. BUG_ON(le16_to_cpu(entry_list->de_num_used) <= 0);
  1119. index = (char *)dx_entry - (char *)entry_list->de_entries;
  1120. index /= sizeof(*dx_entry);
  1121. if (index >= le16_to_cpu(entry_list->de_num_used)) {
  1122. mlog(ML_ERROR, "Dir %llu: Bad dx_entry ptr idx %d, (%p, %p)\n",
  1123. (unsigned long long)OCFS2_I(dir)->ip_blkno, index,
  1124. entry_list, dx_entry);
  1125. return -EIO;
  1126. }
  1127. /*
  1128. * We know that removal of this dirent will leave enough room
  1129. * for a new one, so add this block to the free list if it
  1130. * isn't already there.
  1131. */
  1132. trailer = ocfs2_trailer_from_bh(leaf_bh, dir->i_sb);
  1133. if (trailer->db_free_rec_len == 0)
  1134. add_to_free_list = 1;
  1135. /*
  1136. * Add the block holding our index into the journal before
  1137. * removing the unindexed entry. If we get an error return
  1138. * from __ocfs2_delete_entry(), then it hasn't removed the
  1139. * entry yet. Likewise, successful return means we *must*
  1140. * remove the indexed entry.
  1141. *
  1142. * We're also careful to journal the root tree block here as
  1143. * the entry count needs to be updated. Also, we might be
  1144. * adding to the start of the free list.
  1145. */
  1146. ret = ocfs2_journal_access_dr(handle, INODE_CACHE(dir), dx_root_bh,
  1147. OCFS2_JOURNAL_ACCESS_WRITE);
  1148. if (ret) {
  1149. mlog_errno(ret);
  1150. goto out;
  1151. }
  1152. if (!ocfs2_dx_root_inline(dx_root)) {
  1153. ret = ocfs2_journal_access_dl(handle, INODE_CACHE(dir),
  1154. lookup->dl_dx_leaf_bh,
  1155. OCFS2_JOURNAL_ACCESS_WRITE);
  1156. if (ret) {
  1157. mlog_errno(ret);
  1158. goto out;
  1159. }
  1160. }
  1161. mlog(0, "Dir %llu: delete entry at index: %d\n",
  1162. (unsigned long long)OCFS2_I(dir)->ip_blkno, index);
  1163. ret = __ocfs2_delete_entry(handle, dir, lookup->dl_entry,
  1164. leaf_bh, leaf_bh->b_data, leaf_bh->b_size);
  1165. if (ret) {
  1166. mlog_errno(ret);
  1167. goto out;
  1168. }
  1169. max_rec_len = ocfs2_find_max_rec_len(dir->i_sb, leaf_bh);
  1170. trailer->db_free_rec_len = cpu_to_le16(max_rec_len);
  1171. if (add_to_free_list) {
  1172. trailer->db_free_next = dx_root->dr_free_blk;
  1173. dx_root->dr_free_blk = cpu_to_le64(leaf_bh->b_blocknr);
  1174. ocfs2_journal_dirty(handle, dx_root_bh);
  1175. }
  1176. /* leaf_bh was journal_accessed for us in __ocfs2_delete_entry */
  1177. ocfs2_journal_dirty(handle, leaf_bh);
  1178. le32_add_cpu(&dx_root->dr_num_entries, -1);
  1179. ocfs2_journal_dirty(handle, dx_root_bh);
  1180. ocfs2_dx_list_remove_entry(entry_list, index);
  1181. if (!ocfs2_dx_root_inline(dx_root))
  1182. ocfs2_journal_dirty(handle, lookup->dl_dx_leaf_bh);
  1183. out:
  1184. return ret;
  1185. }
  1186. static inline int ocfs2_delete_entry_id(handle_t *handle,
  1187. struct inode *dir,
  1188. struct ocfs2_dir_entry *de_del,
  1189. struct buffer_head *bh)
  1190. {
  1191. int ret;
  1192. struct buffer_head *di_bh = NULL;
  1193. struct ocfs2_dinode *di;
  1194. struct ocfs2_inline_data *data;
  1195. ret = ocfs2_read_inode_block(dir, &di_bh);
  1196. if (ret) {
  1197. mlog_errno(ret);
  1198. goto out;
  1199. }
  1200. di = (struct ocfs2_dinode *)di_bh->b_data;
  1201. data = &di->id2.i_data;
  1202. ret = __ocfs2_delete_entry(handle, dir, de_del, bh, data->id_data,
  1203. i_size_read(dir));
  1204. brelse(di_bh);
  1205. out:
  1206. return ret;
  1207. }
  1208. static inline int ocfs2_delete_entry_el(handle_t *handle,
  1209. struct inode *dir,
  1210. struct ocfs2_dir_entry *de_del,
  1211. struct buffer_head *bh)
  1212. {
  1213. return __ocfs2_delete_entry(handle, dir, de_del, bh, bh->b_data,
  1214. bh->b_size);
  1215. }
  1216. /*
  1217. * Delete a directory entry. Hide the details of directory
  1218. * implementation from the caller.
  1219. */
  1220. int ocfs2_delete_entry(handle_t *handle,
  1221. struct inode *dir,
  1222. struct ocfs2_dir_lookup_result *res)
  1223. {
  1224. if (ocfs2_dir_indexed(dir))
  1225. return ocfs2_delete_entry_dx(handle, dir, res);
  1226. if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
  1227. return ocfs2_delete_entry_id(handle, dir, res->dl_entry,
  1228. res->dl_leaf_bh);
  1229. return ocfs2_delete_entry_el(handle, dir, res->dl_entry,
  1230. res->dl_leaf_bh);
  1231. }
  1232. /*
  1233. * Check whether 'de' has enough room to hold an entry of
  1234. * 'new_rec_len' bytes.
  1235. */
  1236. static inline int ocfs2_dirent_would_fit(struct ocfs2_dir_entry *de,
  1237. unsigned int new_rec_len)
  1238. {
  1239. unsigned int de_really_used;
  1240. /* Check whether this is an empty record with enough space */
  1241. if (le64_to_cpu(de->inode) == 0 &&
  1242. le16_to_cpu(de->rec_len) >= new_rec_len)
  1243. return 1;
  1244. /*
  1245. * Record might have free space at the end which we can
  1246. * use.
  1247. */
  1248. de_really_used = OCFS2_DIR_REC_LEN(de->name_len);
  1249. if (le16_to_cpu(de->rec_len) >= (de_really_used + new_rec_len))
  1250. return 1;
  1251. return 0;
  1252. }
  1253. static void ocfs2_dx_dir_leaf_insert_tail(struct ocfs2_dx_leaf *dx_leaf,
  1254. struct ocfs2_dx_entry *dx_new_entry)
  1255. {
  1256. int i;
  1257. i = le16_to_cpu(dx_leaf->dl_list.de_num_used);
  1258. dx_leaf->dl_list.de_entries[i] = *dx_new_entry;
  1259. le16_add_cpu(&dx_leaf->dl_list.de_num_used, 1);
  1260. }
  1261. static void ocfs2_dx_entry_list_insert(struct ocfs2_dx_entry_list *entry_list,
  1262. struct ocfs2_dx_hinfo *hinfo,
  1263. u64 dirent_blk)
  1264. {
  1265. int i;
  1266. struct ocfs2_dx_entry *dx_entry;
  1267. i = le16_to_cpu(entry_list->de_num_used);
  1268. dx_entry = &entry_list->de_entries[i];
  1269. memset(dx_entry, 0, sizeof(*dx_entry));
  1270. dx_entry->dx_major_hash = cpu_to_le32(hinfo->major_hash);
  1271. dx_entry->dx_minor_hash = cpu_to_le32(hinfo->minor_hash);
  1272. dx_entry->dx_dirent_blk = cpu_to_le64(dirent_blk);
  1273. le16_add_cpu(&entry_list->de_num_used, 1);
  1274. }
  1275. static int __ocfs2_dx_dir_leaf_insert(struct inode *dir, handle_t *handle,
  1276. struct ocfs2_dx_hinfo *hinfo,
  1277. u64 dirent_blk,
  1278. struct buffer_head *dx_leaf_bh)
  1279. {
  1280. int ret;
  1281. struct ocfs2_dx_leaf *dx_leaf;
  1282. ret = ocfs2_journal_access_dl(handle, INODE_CACHE(dir), dx_leaf_bh,
  1283. OCFS2_JOURNAL_ACCESS_WRITE);
  1284. if (ret) {
  1285. mlog_errno(ret);
  1286. goto out;
  1287. }
  1288. dx_leaf = (struct ocfs2_dx_leaf *)dx_leaf_bh->b_data;
  1289. ocfs2_dx_entry_list_insert(&dx_leaf->dl_list, hinfo, dirent_blk);
  1290. ocfs2_journal_dirty(handle, dx_leaf_bh);
  1291. out:
  1292. return ret;
  1293. }
  1294. static void ocfs2_dx_inline_root_insert(struct inode *dir, handle_t *handle,
  1295. struct ocfs2_dx_hinfo *hinfo,
  1296. u64 dirent_blk,
  1297. struct ocfs2_dx_root_block *dx_root)
  1298. {
  1299. ocfs2_dx_entry_list_insert(&dx_root->dr_entries, hinfo, dirent_blk);
  1300. }
  1301. static int ocfs2_dx_dir_insert(struct inode *dir, handle_t *handle,
  1302. struct ocfs2_dir_lookup_result *lookup)
  1303. {
  1304. int ret = 0;
  1305. struct ocfs2_dx_root_block *dx_root;
  1306. struct buffer_head *dx_root_bh = lookup->dl_dx_root_bh;
  1307. ret = ocfs2_journal_access_dr(handle, INODE_CACHE(dir), dx_root_bh,
  1308. OCFS2_JOURNAL_ACCESS_WRITE);
  1309. if (ret) {
  1310. mlog_errno(ret);
  1311. goto out;
  1312. }
  1313. dx_root = (struct ocfs2_dx_root_block *)lookup->dl_dx_root_bh->b_data;
  1314. if (ocfs2_dx_root_inline(dx_root)) {
  1315. ocfs2_dx_inline_root_insert(dir, handle,
  1316. &lookup->dl_hinfo,
  1317. lookup->dl_leaf_bh->b_blocknr,
  1318. dx_root);
  1319. } else {
  1320. ret = __ocfs2_dx_dir_leaf_insert(dir, handle, &lookup->dl_hinfo,
  1321. lookup->dl_leaf_bh->b_blocknr,
  1322. lookup->dl_dx_leaf_bh);
  1323. if (ret)
  1324. goto out;
  1325. }
  1326. le32_add_cpu(&dx_root->dr_num_entries, 1);
  1327. ocfs2_journal_dirty(handle, dx_root_bh);
  1328. out:
  1329. return ret;
  1330. }
  1331. static void ocfs2_remove_block_from_free_list(struct inode *dir,
  1332. handle_t *handle,
  1333. struct ocfs2_dir_lookup_result *lookup)
  1334. {
  1335. struct ocfs2_dir_block_trailer *trailer, *prev;
  1336. struct ocfs2_dx_root_block *dx_root;
  1337. struct buffer_head *bh;
  1338. trailer = ocfs2_trailer_from_bh(lookup->dl_leaf_bh, dir->i_sb);
  1339. if (ocfs2_free_list_at_root(lookup)) {
  1340. bh = lookup->dl_dx_root_bh;
  1341. dx_root = (struct ocfs2_dx_root_block *)bh->b_data;
  1342. dx_root->dr_free_blk = trailer->db_free_next;
  1343. } else {
  1344. bh = lookup->dl_prev_leaf_bh;
  1345. prev = ocfs2_trailer_from_bh(bh, dir->i_sb);
  1346. prev->db_free_next = trailer->db_free_next;
  1347. }
  1348. trailer->db_free_rec_len = cpu_to_le16(0);
  1349. trailer->db_free_next = cpu_to_le64(0);
  1350. ocfs2_journal_dirty(handle, bh);
  1351. ocfs2_journal_dirty(handle, lookup->dl_leaf_bh);
  1352. }
  1353. /*
  1354. * This expects that a journal write has been reserved on
  1355. * lookup->dl_prev_leaf_bh or lookup->dl_dx_root_bh
  1356. */
  1357. static void ocfs2_recalc_free_list(struct inode *dir, handle_t *handle,
  1358. struct ocfs2_dir_lookup_result *lookup)
  1359. {
  1360. int max_rec_len;
  1361. struct ocfs2_dir_block_trailer *trailer;
  1362. /* Walk dl_leaf_bh to figure out what the new free rec_len is. */
  1363. max_rec_len = ocfs2_find_max_rec_len(dir->i_sb, lookup->dl_leaf_bh);
  1364. if (max_rec_len) {
  1365. /*
  1366. * There's still room in this block, so no need to remove it
  1367. * from the free list. In this case, we just want to update
  1368. * the rec len accounting.
  1369. */
  1370. trailer = ocfs2_trailer_from_bh(lookup->dl_leaf_bh, dir->i_sb);
  1371. trailer->db_free_rec_len = cpu_to_le16(max_rec_len);
  1372. ocfs2_journal_dirty(handle, lookup->dl_leaf_bh);
  1373. } else {
  1374. ocfs2_remove_block_from_free_list(dir, handle, lookup);
  1375. }
  1376. }
  1377. /* we don't always have a dentry for what we want to add, so people
  1378. * like orphan dir can call this instead.
  1379. *
  1380. * The lookup context must have been filled from
  1381. * ocfs2_prepare_dir_for_insert.
  1382. */
  1383. int __ocfs2_add_entry(handle_t *handle,
  1384. struct inode *dir,
  1385. const char *name, int namelen,
  1386. struct inode *inode, u64 blkno,
  1387. struct buffer_head *parent_fe_bh,
  1388. struct ocfs2_dir_lookup_result *lookup)
  1389. {
  1390. unsigned long offset;
  1391. unsigned short rec_len;
  1392. struct ocfs2_dir_entry *de, *de1;
  1393. struct ocfs2_dinode *di = (struct ocfs2_dinode *)parent_fe_bh->b_data;
  1394. struct super_block *sb = dir->i_sb;
  1395. int retval, status;
  1396. unsigned int size = sb->s_blocksize;
  1397. struct buffer_head *insert_bh = lookup->dl_leaf_bh;
  1398. char *data_start = insert_bh->b_data;
  1399. mlog_entry_void();
  1400. if (!namelen)
  1401. return -EINVAL;
  1402. if (ocfs2_dir_indexed(dir)) {
  1403. struct buffer_head *bh;
  1404. /*
  1405. * An indexed dir may require that we update the free space
  1406. * list. Reserve a write to the previous node in the list so
  1407. * that we don't fail later.
  1408. *
  1409. * XXX: This can be either a dx_root_block, or an unindexed
  1410. * directory tree leaf block.
  1411. */
  1412. if (ocfs2_free_list_at_root(lookup)) {
  1413. bh = lookup->dl_dx_root_bh;
  1414. retval = ocfs2_journal_access_dr(handle,
  1415. INODE_CACHE(dir), bh,
  1416. OCFS2_JOURNAL_ACCESS_WRITE);
  1417. } else {
  1418. bh = lookup->dl_prev_leaf_bh;
  1419. retval = ocfs2_journal_access_db(handle,
  1420. INODE_CACHE(dir), bh,
  1421. OCFS2_JOURNAL_ACCESS_WRITE);
  1422. }
  1423. if (retval) {
  1424. mlog_errno(retval);
  1425. return retval;
  1426. }
  1427. } else if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
  1428. data_start = di->id2.i_data.id_data;
  1429. size = i_size_read(dir);
  1430. BUG_ON(insert_bh != parent_fe_bh);
  1431. }
  1432. rec_len = OCFS2_DIR_REC_LEN(namelen);
  1433. offset = 0;
  1434. de = (struct ocfs2_dir_entry *) data_start;
  1435. while (1) {
  1436. BUG_ON((char *)de >= (size + data_start));
  1437. /* These checks should've already been passed by the
  1438. * prepare function, but I guess we can leave them
  1439. * here anyway. */
  1440. if (!ocfs2_check_dir_entry(dir, de, insert_bh, offset)) {
  1441. retval = -ENOENT;
  1442. goto bail;
  1443. }
  1444. if (ocfs2_match(namelen, name, de)) {
  1445. retval = -EEXIST;
  1446. goto bail;
  1447. }
  1448. /* We're guaranteed that we should have space, so we
  1449. * can't possibly have hit the trailer...right? */
  1450. mlog_bug_on_msg(ocfs2_skip_dir_trailer(dir, de, offset, size),
  1451. "Hit dir trailer trying to insert %.*s "
  1452. "(namelen %d) into directory %llu. "
  1453. "offset is %lu, trailer offset is %d\n",
  1454. namelen, name, namelen,
  1455. (unsigned long long)parent_fe_bh->b_blocknr,
  1456. offset, ocfs2_dir_trailer_blk_off(dir->i_sb));
  1457. if (ocfs2_dirent_would_fit(de, rec_len)) {
  1458. dir->i_mtime = dir->i_ctime = CURRENT_TIME;
  1459. retval = ocfs2_mark_inode_dirty(handle, dir, parent_fe_bh);
  1460. if (retval < 0) {
  1461. mlog_errno(retval);
  1462. goto bail;
  1463. }
  1464. if (insert_bh == parent_fe_bh)
  1465. status = ocfs2_journal_access_di(handle,
  1466. INODE_CACHE(dir),
  1467. insert_bh,
  1468. OCFS2_JOURNAL_ACCESS_WRITE);
  1469. else {
  1470. status = ocfs2_journal_access_db(handle,
  1471. INODE_CACHE(dir),
  1472. insert_bh,
  1473. OCFS2_JOURNAL_ACCESS_WRITE);
  1474. if (ocfs2_dir_indexed(dir)) {
  1475. status = ocfs2_dx_dir_insert(dir,
  1476. handle,
  1477. lookup);
  1478. if (status) {
  1479. mlog_errno(status);
  1480. goto bail;
  1481. }
  1482. }
  1483. }
  1484. /* By now the buffer is marked for journaling */
  1485. offset += le16_to_cpu(de->rec_len);
  1486. if (le64_to_cpu(de->inode)) {
  1487. de1 = (struct ocfs2_dir_entry *)((char *) de +
  1488. OCFS2_DIR_REC_LEN(de->name_len));
  1489. de1->rec_len =
  1490. cpu_to_le16(le16_to_cpu(de->rec_len) -
  1491. OCFS2_DIR_REC_LEN(de->name_len));
  1492. de->rec_len = cpu_to_le16(OCFS2_DIR_REC_LEN(de->name_len));
  1493. de = de1;
  1494. }
  1495. de->file_type = OCFS2_FT_UNKNOWN;
  1496. if (blkno) {
  1497. de->inode = cpu_to_le64(blkno);
  1498. ocfs2_set_de_type(de, inode->i_mode);
  1499. } else
  1500. de->inode = 0;
  1501. de->name_len = namelen;
  1502. memcpy(de->name, name, namelen);
  1503. if (ocfs2_dir_indexed(dir))
  1504. ocfs2_recalc_free_list(dir, handle, lookup);
  1505. dir->i_version++;
  1506. ocfs2_journal_dirty(handle, insert_bh);
  1507. retval = 0;
  1508. goto bail;
  1509. }
  1510. offset += le16_to_cpu(de->rec_len);
  1511. de = (struct ocfs2_dir_entry *) ((char *) de + le16_to_cpu(de->rec_len));
  1512. }
  1513. /* when you think about it, the assert above should prevent us
  1514. * from ever getting here. */
  1515. retval = -ENOSPC;
  1516. bail:
  1517. mlog_exit(retval);
  1518. return retval;
  1519. }
  1520. static int ocfs2_dir_foreach_blk_id(struct inode *inode,
  1521. u64 *f_version,
  1522. loff_t *f_pos, void *priv,
  1523. filldir_t filldir, int *filldir_err)
  1524. {
  1525. int ret, i, filldir_ret;
  1526. unsigned long offset = *f_pos;
  1527. struct buffer_head *di_bh = NULL;
  1528. struct ocfs2_dinode *di;
  1529. struct ocfs2_inline_data *data;
  1530. struct ocfs2_dir_entry *de;
  1531. ret = ocfs2_read_inode_block(inode, &di_bh);
  1532. if (ret) {
  1533. mlog(ML_ERROR, "Unable to read inode block for dir %llu\n",
  1534. (unsigned long long)OCFS2_I(inode)->ip_blkno);
  1535. goto out;
  1536. }
  1537. di = (struct ocfs2_dinode *)di_bh->b_data;
  1538. data = &di->id2.i_data;
  1539. while (*f_pos < i_size_read(inode)) {
  1540. revalidate:
  1541. /* If the dir block has changed since the last call to
  1542. * readdir(2), then we might be pointing to an invalid
  1543. * dirent right now. Scan from the start of the block
  1544. * to make sure. */
  1545. if (*f_version != inode->i_version) {
  1546. for (i = 0; i < i_size_read(inode) && i < offset; ) {
  1547. de = (struct ocfs2_dir_entry *)
  1548. (data->id_data + i);
  1549. /* It's too expensive to do a full
  1550. * dirent test each time round this
  1551. * loop, but we do have to test at
  1552. * least that it is non-zero. A
  1553. * failure will be detected in the
  1554. * dirent test below. */
  1555. if (le16_to_cpu(de->rec_len) <
  1556. OCFS2_DIR_REC_LEN(1))
  1557. break;
  1558. i += le16_to_cpu(de->rec_len);
  1559. }
  1560. *f_pos = offset = i;
  1561. *f_version = inode->i_version;
  1562. }
  1563. de = (struct ocfs2_dir_entry *) (data->id_data + *f_pos);
  1564. if (!ocfs2_check_dir_entry(inode, de, di_bh, *f_pos)) {
  1565. /* On error, skip the f_pos to the end. */
  1566. *f_pos = i_size_read(inode);
  1567. goto out;
  1568. }
  1569. offset += le16_to_cpu(de->rec_len);
  1570. if (le64_to_cpu(de->inode)) {
  1571. /* We might block in the next section
  1572. * if the data destination is
  1573. * currently swapped out. So, use a
  1574. * version stamp to detect whether or
  1575. * not the directory has been modified
  1576. * during the copy operation.
  1577. */
  1578. u64 version = *f_version;
  1579. unsigned char d_type = DT_UNKNOWN;
  1580. if (de->file_type < OCFS2_FT_MAX)
  1581. d_type = ocfs2_filetype_table[de->file_type];
  1582. filldir_ret = filldir(priv, de->name,
  1583. de->name_len,
  1584. *f_pos,
  1585. le64_to_cpu(de->inode),
  1586. d_type);
  1587. if (filldir_ret) {
  1588. if (filldir_err)
  1589. *filldir_err = filldir_ret;
  1590. break;
  1591. }
  1592. if (version != *f_version)
  1593. goto revalidate;
  1594. }
  1595. *f_pos += le16_to_cpu(de->rec_len);
  1596. }
  1597. out:
  1598. brelse(di_bh);
  1599. return 0;
  1600. }
  1601. /*
  1602. * NOTE: This function can be called against unindexed directories,
  1603. * and indexed ones.
  1604. */
  1605. static int ocfs2_dir_foreach_blk_el(struct inode *inode,
  1606. u64 *f_version,
  1607. loff_t *f_pos, void *priv,
  1608. filldir_t filldir, int *filldir_err)
  1609. {
  1610. int error = 0;
  1611. unsigned long offset, blk, last_ra_blk = 0;
  1612. int i, stored;
  1613. struct buffer_head * bh, * tmp;
  1614. struct ocfs2_dir_entry * de;
  1615. struct super_block * sb = inode->i_sb;
  1616. unsigned int ra_sectors = 16;
  1617. stored = 0;
  1618. bh = NULL;
  1619. offset = (*f_pos) & (sb->s_blocksize - 1);
  1620. while (!error && !stored && *f_pos < i_size_read(inode)) {
  1621. blk = (*f_pos) >> sb->s_blocksize_bits;
  1622. if (ocfs2_read_dir_block(inode, blk, &bh, 0)) {
  1623. /* Skip the corrupt dirblock and keep trying */
  1624. *f_pos += sb->s_blocksize - offset;
  1625. continue;
  1626. }
  1627. /* The idea here is to begin with 8k read-ahead and to stay
  1628. * 4k ahead of our current position.
  1629. *
  1630. * TODO: Use the pagecache for this. We just need to
  1631. * make sure it's cluster-safe... */
  1632. if (!last_ra_blk
  1633. || (((last_ra_blk - blk) << 9) <= (ra_sectors / 2))) {
  1634. for (i = ra_sectors >> (sb->s_blocksize_bits - 9);
  1635. i > 0; i--) {
  1636. tmp = NULL;
  1637. if (!ocfs2_read_dir_block(inode, ++blk, &tmp,
  1638. OCFS2_BH_READAHEAD))
  1639. brelse(tmp);
  1640. }
  1641. last_ra_blk = blk;
  1642. ra_sectors = 8;
  1643. }
  1644. revalidate:
  1645. /* If the dir block has changed since the last call to
  1646. * readdir(2), then we might be pointing to an invalid
  1647. * dirent right now. Scan from the start of the block
  1648. * to make sure. */
  1649. if (*f_version != inode->i_version) {
  1650. for (i = 0; i < sb->s_blocksize && i < offset; ) {
  1651. de = (struct ocfs2_dir_entry *) (bh->b_data + i);
  1652. /* It's too expensive to do a full
  1653. * dirent test each time round this
  1654. * loop, but we do have to test at
  1655. * least that it is non-zero. A
  1656. * failure will be detected in the
  1657. * dirent test below. */
  1658. if (le16_to_cpu(de->rec_len) <
  1659. OCFS2_DIR_REC_LEN(1))
  1660. break;
  1661. i += le16_to_cpu(de->rec_len);
  1662. }
  1663. offset = i;
  1664. *f_pos = ((*f_pos) & ~(sb->s_blocksize - 1))
  1665. | offset;
  1666. *f_version = inode->i_version;
  1667. }
  1668. while (!error && *f_pos < i_size_read(inode)
  1669. && offset < sb->s_blocksize) {
  1670. de = (struct ocfs2_dir_entry *) (bh->b_data + offset);
  1671. if (!ocfs2_check_dir_entry(inode, de, bh, offset)) {
  1672. /* On error, skip the f_pos to the
  1673. next block. */
  1674. *f_pos = ((*f_pos) | (sb->s_blocksize - 1)) + 1;
  1675. brelse(bh);
  1676. goto out;
  1677. }
  1678. offset += le16_to_cpu(de->rec_len);
  1679. if (le64_to_cpu(de->inode)) {
  1680. /* We might block in the next section
  1681. * if the data destination is
  1682. * currently swapped out. So, use a
  1683. * version stamp to detect whether or
  1684. * not the directory has been modified
  1685. * during the copy operation.
  1686. */
  1687. unsigned long version = *f_version;
  1688. unsigned char d_type = DT_UNKNOWN;
  1689. if (de->file_type < OCFS2_FT_MAX)
  1690. d_type = ocfs2_filetype_table[de->file_type];
  1691. error = filldir(priv, de->name,
  1692. de->name_len,
  1693. *f_pos,
  1694. le64_to_cpu(de->inode),
  1695. d_type);
  1696. if (error) {
  1697. if (filldir_err)
  1698. *filldir_err = error;
  1699. break;
  1700. }
  1701. if (version != *f_version)
  1702. goto revalidate;
  1703. stored ++;
  1704. }
  1705. *f_pos += le16_to_cpu(de->rec_len);
  1706. }
  1707. offset = 0;
  1708. brelse(bh);
  1709. bh = NULL;
  1710. }
  1711. stored = 0;
  1712. out:
  1713. return stored;
  1714. }
  1715. static int ocfs2_dir_foreach_blk(struct inode *inode, u64 *f_version,
  1716. loff_t *f_pos, void *priv, filldir_t filldir,
  1717. int *filldir_err)
  1718. {
  1719. if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
  1720. return ocfs2_dir_foreach_blk_id(inode, f_version, f_pos, priv,
  1721. filldir, filldir_err);
  1722. return ocfs2_dir_foreach_blk_el(inode, f_version, f_pos, priv, filldir,
  1723. filldir_err);
  1724. }
  1725. /*
  1726. * This is intended to be called from inside other kernel functions,
  1727. * so we fake some arguments.
  1728. */
  1729. int ocfs2_dir_foreach(struct inode *inode, loff_t *f_pos, void *priv,
  1730. filldir_t filldir)
  1731. {
  1732. int ret = 0, filldir_err = 0;
  1733. u64 version = inode->i_version;
  1734. while (*f_pos < i_size_read(inode)) {
  1735. ret = ocfs2_dir_foreach_blk(inode, &version, f_pos, priv,
  1736. filldir, &filldir_err);
  1737. if (ret || filldir_err)
  1738. break;
  1739. }
  1740. if (ret > 0)
  1741. ret = -EIO;
  1742. return 0;
  1743. }
  1744. /*
  1745. * ocfs2_readdir()
  1746. *
  1747. */
  1748. int ocfs2_readdir(struct file * filp, void * dirent, filldir_t filldir)
  1749. {
  1750. int error = 0;
  1751. struct inode *inode = filp->f_path.dentry->d_inode;
  1752. int lock_level = 0;
  1753. mlog_entry("dirino=%llu\n",
  1754. (unsigned long long)OCFS2_I(inode)->ip_blkno);
  1755. error = ocfs2_inode_lock_atime(inode, filp->f_vfsmnt, &lock_level);
  1756. if (lock_level && error >= 0) {
  1757. /* We release EX lock which used to update atime
  1758. * and get PR lock again to reduce contention
  1759. * on commonly accessed directories. */
  1760. ocfs2_inode_unlock(inode, 1);
  1761. lock_level = 0;
  1762. error = ocfs2_inode_lock(inode, NULL, 0);
  1763. }
  1764. if (error < 0) {
  1765. if (error != -ENOENT)
  1766. mlog_errno(error);
  1767. /* we haven't got any yet, so propagate the error. */
  1768. goto bail_nolock;
  1769. }
  1770. error = ocfs2_dir_foreach_blk(inode, &filp->f_version, &filp->f_pos,
  1771. dirent, filldir, NULL);
  1772. ocfs2_inode_unlock(inode, lock_level);
  1773. bail_nolock:
  1774. mlog_exit(error);
  1775. return error;
  1776. }
  1777. /*
  1778. * NOTE: this should always be called with parent dir i_mutex taken.
  1779. */
  1780. int ocfs2_find_files_on_disk(const char *name,
  1781. int namelen,
  1782. u64 *blkno,
  1783. struct inode *inode,
  1784. struct ocfs2_dir_lookup_result *lookup)
  1785. {
  1786. int status = -ENOENT;
  1787. mlog(0, "name=%.*s, blkno=%p, inode=%llu\n", namelen, name, blkno,
  1788. (unsigned long long)OCFS2_I(inode)->ip_blkno);
  1789. status = ocfs2_find_entry(name, namelen, inode, lookup);
  1790. if (status)
  1791. goto leave;
  1792. *blkno = le64_to_cpu(lookup->dl_entry->inode);
  1793. status = 0;
  1794. leave:
  1795. return status;
  1796. }
  1797. /*
  1798. * Convenience function for callers which just want the block number
  1799. * mapped to a name and don't require the full dirent info, etc.
  1800. */
  1801. int ocfs2_lookup_ino_from_name(struct inode *dir, const char *name,
  1802. int namelen, u64 *blkno)
  1803. {
  1804. int ret;
  1805. struct ocfs2_dir_lookup_result lookup = { NULL, };
  1806. ret = ocfs2_find_files_on_disk(name, namelen, blkno, dir, &lookup);
  1807. ocfs2_free_dir_lookup_result(&lookup);
  1808. return ret;
  1809. }
  1810. /* Check for a name within a directory.
  1811. *
  1812. * Return 0 if the name does not exist
  1813. * Return -EEXIST if the directory contains the name
  1814. *
  1815. * Callers should have i_mutex + a cluster lock on dir
  1816. */
  1817. int ocfs2_check_dir_for_entry(struct inode *dir,
  1818. const char *name,
  1819. int namelen)
  1820. {
  1821. int ret;
  1822. struct ocfs2_dir_lookup_result lookup = { NULL, };
  1823. mlog_entry("dir %llu, name '%.*s'\n",
  1824. (unsigned long long)OCFS2_I(dir)->ip_blkno, namelen, name);
  1825. ret = -EEXIST;
  1826. if (ocfs2_find_entry(name, namelen, dir, &lookup) == 0)
  1827. goto bail;
  1828. ret = 0;
  1829. bail:
  1830. ocfs2_free_dir_lookup_result(&lookup);
  1831. mlog_exit(ret);
  1832. return ret;
  1833. }
  1834. struct ocfs2_empty_dir_priv {
  1835. unsigned seen_dot;
  1836. unsigned seen_dot_dot;
  1837. unsigned seen_other;
  1838. unsigned dx_dir;
  1839. };
  1840. static int ocfs2_empty_dir_filldir(void *priv, const char *name, int name_len,
  1841. loff_t pos, u64 ino, unsigned type)
  1842. {
  1843. struct ocfs2_empty_dir_priv *p = priv;
  1844. /*
  1845. * Check the positions of "." and ".." records to be sure
  1846. * they're in the correct place.
  1847. *
  1848. * Indexed directories don't need to proceed past the first
  1849. * two entries, so we end the scan after seeing '..'. Despite
  1850. * that, we allow the scan to proceed In the event that we
  1851. * have a corrupted indexed directory (no dot or dot dot
  1852. * entries). This allows us to double check for existing
  1853. * entries which might not have been found in the index.
  1854. */
  1855. if (name_len == 1 && !strncmp(".", name, 1) && pos == 0) {
  1856. p->seen_dot = 1;
  1857. return 0;
  1858. }
  1859. if (name_len == 2 && !strncmp("..", name, 2) &&
  1860. pos == OCFS2_DIR_REC_LEN(1)) {
  1861. p->seen_dot_dot = 1;
  1862. if (p->dx_dir && p->seen_dot)
  1863. return 1;
  1864. return 0;
  1865. }
  1866. p->seen_other = 1;
  1867. return 1;
  1868. }
  1869. static int ocfs2_empty_dir_dx(struct inode *inode,
  1870. struct ocfs2_empty_dir_priv *priv)
  1871. {
  1872. int ret;
  1873. struct buffer_head *di_bh = NULL;
  1874. struct buffer_head *dx_root_bh = NULL;
  1875. struct ocfs2_dinode *di;
  1876. struct ocfs2_dx_root_block *dx_root;
  1877. priv->dx_dir = 1;
  1878. ret = ocfs2_read_inode_block(inode, &di_bh);
  1879. if (ret) {
  1880. mlog_errno(ret);
  1881. goto out;
  1882. }
  1883. di = (struct ocfs2_dinode *)di_bh->b_data;
  1884. ret = ocfs2_read_dx_root(inode, di, &dx_root_bh);
  1885. if (ret) {
  1886. mlog_errno(ret);
  1887. goto out;
  1888. }
  1889. dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
  1890. if (le32_to_cpu(dx_root->dr_num_entries) != 2)
  1891. priv->seen_other = 1;
  1892. out:
  1893. brelse(di_bh);
  1894. brelse(dx_root_bh);
  1895. return ret;
  1896. }
  1897. /*
  1898. * routine to check that the specified directory is empty (for rmdir)
  1899. *
  1900. * Returns 1 if dir is empty, zero otherwise.
  1901. *
  1902. * XXX: This is a performance problem for unindexed directories.
  1903. */
  1904. int ocfs2_empty_dir(struct inode *inode)
  1905. {
  1906. int ret;
  1907. loff_t start = 0;
  1908. struct ocfs2_empty_dir_priv priv;
  1909. memset(&priv, 0, sizeof(priv));
  1910. if (ocfs2_dir_indexed(inode)) {
  1911. ret = ocfs2_empty_dir_dx(inode, &priv);
  1912. if (ret)
  1913. mlog_errno(ret);
  1914. /*
  1915. * We still run ocfs2_dir_foreach to get the checks
  1916. * for "." and "..".
  1917. */
  1918. }
  1919. ret = ocfs2_dir_foreach(inode, &start, &priv, ocfs2_empty_dir_filldir);
  1920. if (ret)
  1921. mlog_errno(ret);
  1922. if (!priv.seen_dot || !priv.seen_dot_dot) {
  1923. mlog(ML_ERROR, "bad directory (dir #%llu) - no `.' or `..'\n",
  1924. (unsigned long long)OCFS2_I(inode)->ip_blkno);
  1925. /*
  1926. * XXX: Is it really safe to allow an unlink to continue?
  1927. */
  1928. return 1;
  1929. }
  1930. return !priv.seen_other;
  1931. }
  1932. /*
  1933. * Fills "." and ".." dirents in a new directory block. Returns dirent for
  1934. * "..", which might be used during creation of a directory with a trailing
  1935. * header. It is otherwise safe to ignore the return code.
  1936. */
  1937. static struct ocfs2_dir_entry *ocfs2_fill_initial_dirents(struct inode *inode,
  1938. struct inode *parent,
  1939. char *start,
  1940. unsigned int size)
  1941. {
  1942. struct ocfs2_dir_entry *de = (struct ocfs2_dir_entry *)start;
  1943. de->inode = cpu_to_le64(OCFS2_I(inode)->ip_blkno);
  1944. de->name_len = 1;
  1945. de->rec_len =
  1946. cpu_to_le16(OCFS2_DIR_REC_LEN(de->name_len));
  1947. strcpy(de->name, ".");
  1948. ocfs2_set_de_type(de, S_IFDIR);
  1949. de = (struct ocfs2_dir_entry *) ((char *)de + le16_to_cpu(de->rec_len));
  1950. de->inode = cpu_to_le64(OCFS2_I(parent)->ip_blkno);
  1951. de->rec_len = cpu_to_le16(size - OCFS2_DIR_REC_LEN(1));
  1952. de->name_len = 2;
  1953. strcpy(de->name, "..");
  1954. ocfs2_set_de_type(de, S_IFDIR);
  1955. return de;
  1956. }
  1957. /*
  1958. * This works together with code in ocfs2_mknod_locked() which sets
  1959. * the inline-data flag and initializes the inline-data section.
  1960. */
  1961. static int ocfs2_fill_new_dir_id(struct ocfs2_super *osb,
  1962. handle_t *handle,
  1963. struct inode *parent,
  1964. struct inode *inode,
  1965. struct buffer_head *di_bh)
  1966. {
  1967. int ret;
  1968. struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
  1969. struct ocfs2_inline_data *data = &di->id2.i_data;
  1970. unsigned int size = le16_to_cpu(data->id_count);
  1971. ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
  1972. OCFS2_JOURNAL_ACCESS_WRITE);
  1973. if (ret) {
  1974. mlog_errno(ret);
  1975. goto out;
  1976. }
  1977. ocfs2_fill_initial_dirents(inode, parent, data->id_data, size);
  1978. ocfs2_journal_dirty(handle, di_bh);
  1979. i_size_write(inode, size);
  1980. inode->i_nlink = 2;
  1981. inode->i_blocks = ocfs2_inode_sector_count(inode);
  1982. ret = ocfs2_mark_inode_dirty(handle, inode, di_bh);
  1983. if (ret < 0)
  1984. mlog_errno(ret);
  1985. out:
  1986. return ret;
  1987. }
  1988. static int ocfs2_fill_new_dir_el(struct ocfs2_super *osb,
  1989. handle_t *handle,
  1990. struct inode *parent,
  1991. struct inode *inode,
  1992. struct buffer_head *fe_bh,
  1993. struct ocfs2_alloc_context *data_ac,
  1994. struct buffer_head **ret_new_bh)
  1995. {
  1996. int status;
  1997. unsigned int size = osb->sb->s_blocksize;
  1998. struct buffer_head *new_bh = NULL;
  1999. struct ocfs2_dir_entry *de;
  2000. mlog_entry_void();
  2001. if (ocfs2_new_dir_wants_trailer(inode))
  2002. size = ocfs2_dir_trailer_blk_off(parent->i_sb);
  2003. status = ocfs2_do_extend_dir(osb->sb, handle, inode, fe_bh,
  2004. data_ac, NULL, &new_bh);
  2005. if (status < 0) {
  2006. mlog_errno(status);
  2007. goto bail;
  2008. }
  2009. ocfs2_set_new_buffer_uptodate(INODE_CACHE(inode), new_bh);
  2010. status = ocfs2_journal_access_db(handle, INODE_CACHE(inode), new_bh,
  2011. OCFS2_JOURNAL_ACCESS_CREATE);
  2012. if (status < 0) {
  2013. mlog_errno(status);
  2014. goto bail;
  2015. }
  2016. memset(new_bh->b_data, 0, osb->sb->s_blocksize);
  2017. de = ocfs2_fill_initial_dirents(inode, parent, new_bh->b_data, size);
  2018. if (ocfs2_new_dir_wants_trailer(inode)) {
  2019. int size = le16_to_cpu(de->rec_len);
  2020. /*
  2021. * Figure out the size of the hole left over after
  2022. * insertion of '.' and '..'. The trailer wants this
  2023. * information.
  2024. */
  2025. size -= OCFS2_DIR_REC_LEN(2);
  2026. size -= sizeof(struct ocfs2_dir_block_trailer);
  2027. ocfs2_init_dir_trailer(inode, new_bh, size);
  2028. }
  2029. ocfs2_journal_dirty(handle, new_bh);
  2030. i_size_write(inode, inode->i_sb->s_blocksize);
  2031. inode->i_nlink = 2;
  2032. inode->i_blocks = ocfs2_inode_sector_count(inode);
  2033. status = ocfs2_mark_inode_dirty(handle, inode, fe_bh);
  2034. if (status < 0) {
  2035. mlog_errno(status);
  2036. goto bail;
  2037. }
  2038. status = 0;
  2039. if (ret_new_bh) {
  2040. *ret_new_bh = new_bh;
  2041. new_bh = NULL;
  2042. }
  2043. bail:
  2044. brelse(new_bh);
  2045. mlog_exit(status);
  2046. return status;
  2047. }
  2048. static int ocfs2_dx_dir_attach_index(struct ocfs2_super *osb,
  2049. handle_t *handle, struct inode *dir,
  2050. struct buffer_head *di_bh,
  2051. struct buffer_head *dirdata_bh,
  2052. struct ocfs2_alloc_context *meta_ac,
  2053. int dx_inline, u32 num_entries,
  2054. struct buffer_head **ret_dx_root_bh)
  2055. {
  2056. int ret;
  2057. struct ocfs2_dinode *di = (struct ocfs2_dinode *) di_bh->b_data;
  2058. u16 dr_suballoc_bit;
  2059. u64 suballoc_loc, dr_blkno;
  2060. unsigned int num_bits;
  2061. struct buffer_head *dx_root_bh = NULL;
  2062. struct ocfs2_dx_root_block *dx_root;
  2063. struct ocfs2_dir_block_trailer *trailer =
  2064. ocfs2_trailer_from_bh(dirdata_bh, dir->i_sb);
  2065. ret = ocfs2_claim_metadata(handle, meta_ac, 1, &suballoc_loc,
  2066. &dr_suballoc_bit, &num_bits, &dr_blkno);
  2067. if (ret) {
  2068. mlog_errno(ret);
  2069. goto out;
  2070. }
  2071. mlog(0, "Dir %llu, attach new index block: %llu\n",
  2072. (unsigned long long)OCFS2_I(dir)->ip_blkno,
  2073. (unsigned long long)dr_blkno);
  2074. dx_root_bh = sb_getblk(osb->sb, dr_blkno);
  2075. if (dx_root_bh == NULL) {
  2076. ret = -EIO;
  2077. goto out;
  2078. }
  2079. ocfs2_set_new_buffer_uptodate(INODE_CACHE(dir), dx_root_bh);
  2080. ret = ocfs2_journal_access_dr(handle, INODE_CACHE(dir), dx_root_bh,
  2081. OCFS2_JOURNAL_ACCESS_CREATE);
  2082. if (ret < 0) {
  2083. mlog_errno(ret);
  2084. goto out;
  2085. }
  2086. dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
  2087. memset(dx_root, 0, osb->sb->s_blocksize);
  2088. strcpy(dx_root->dr_signature, OCFS2_DX_ROOT_SIGNATURE);
  2089. dx_root->dr_suballoc_slot = cpu_to_le16(meta_ac->ac_alloc_slot);
  2090. dx_root->dr_suballoc_loc = cpu_to_le64(suballoc_loc);
  2091. dx_root->dr_suballoc_bit = cpu_to_le16(dr_suballoc_bit);
  2092. dx_root->dr_fs_generation = cpu_to_le32(osb->fs_generation);
  2093. dx_root->dr_blkno = cpu_to_le64(dr_blkno);
  2094. dx_root->dr_dir_blkno = cpu_to_le64(OCFS2_I(dir)->ip_blkno);
  2095. dx_root->dr_num_entries = cpu_to_le32(num_entries);
  2096. if (le16_to_cpu(trailer->db_free_rec_len))
  2097. dx_root->dr_free_blk = cpu_to_le64(dirdata_bh->b_blocknr);
  2098. else
  2099. dx_root->dr_free_blk = cpu_to_le64(0);
  2100. if (dx_inline) {
  2101. dx_root->dr_flags |= OCFS2_DX_FLAG_INLINE;
  2102. dx_root->dr_entries.de_count =
  2103. cpu_to_le16(ocfs2_dx_entries_per_root(osb->sb));
  2104. } else {
  2105. dx_root->dr_list.l_count =
  2106. cpu_to_le16(ocfs2_extent_recs_per_dx_root(osb->sb));
  2107. }
  2108. ocfs2_journal_dirty(handle, dx_root_bh);
  2109. ret = ocfs2_journal_access_di(handle, INODE_CACHE(dir), di_bh,
  2110. OCFS2_JOURNAL_ACCESS_CREATE);
  2111. if (ret) {
  2112. mlog_errno(ret);
  2113. goto out;
  2114. }
  2115. di->i_dx_root = cpu_to_le64(dr_blkno);
  2116. OCFS2_I(dir)->ip_dyn_features |= OCFS2_INDEXED_DIR_FL;
  2117. di->i_dyn_features = cpu_to_le16(OCFS2_I(dir)->ip_dyn_features);
  2118. ocfs2_journal_dirty(handle, di_bh);
  2119. *ret_dx_root_bh = dx_root_bh;
  2120. dx_root_bh = NULL;
  2121. out:
  2122. brelse(dx_root_bh);
  2123. return ret;
  2124. }
  2125. static int ocfs2_dx_dir_format_cluster(struct ocfs2_super *osb,
  2126. handle_t *handle, struct inode *dir,
  2127. struct buffer_head **dx_leaves,
  2128. int num_dx_leaves, u64 start_blk)
  2129. {
  2130. int ret, i;
  2131. struct ocfs2_dx_leaf *dx_leaf;
  2132. struct buffer_head *bh;
  2133. for (i = 0; i < num_dx_leaves; i++) {
  2134. bh = sb_getblk(osb->sb, start_blk + i);
  2135. if (bh == NULL) {
  2136. ret = -EIO;
  2137. goto out;
  2138. }
  2139. dx_leaves[i] = bh;
  2140. ocfs2_set_new_buffer_uptodate(INODE_CACHE(dir), bh);
  2141. ret = ocfs2_journal_access_dl(handle, INODE_CACHE(dir), bh,
  2142. OCFS2_JOURNAL_ACCESS_CREATE);
  2143. if (ret < 0) {
  2144. mlog_errno(ret);
  2145. goto out;
  2146. }
  2147. dx_leaf = (struct ocfs2_dx_leaf *) bh->b_data;
  2148. memset(dx_leaf, 0, osb->sb->s_blocksize);
  2149. strcpy(dx_leaf->dl_signature, OCFS2_DX_LEAF_SIGNATURE);
  2150. dx_leaf->dl_fs_generation = cpu_to_le32(osb->fs_generation);
  2151. dx_leaf->dl_blkno = cpu_to_le64(bh->b_blocknr);
  2152. dx_leaf->dl_list.de_count =
  2153. cpu_to_le16(ocfs2_dx_entries_per_leaf(osb->sb));
  2154. mlog(0,
  2155. "Dir %llu, format dx_leaf: %llu, entry count: %u\n",
  2156. (unsigned long long)OCFS2_I(dir)->ip_blkno,
  2157. (unsigned long long)bh->b_blocknr,
  2158. le16_to_cpu(dx_leaf->dl_list.de_count));
  2159. ocfs2_journal_dirty(handle, bh);
  2160. }
  2161. ret = 0;
  2162. out:
  2163. return ret;
  2164. }
  2165. /*
  2166. * Allocates and formats a new cluster for use in an indexed dir
  2167. * leaf. This version will not do the extent insert, so that it can be
  2168. * used by operations which need careful ordering.
  2169. */
  2170. static int __ocfs2_dx_dir_new_cluster(struct inode *dir,
  2171. u32 cpos, handle_t *handle,
  2172. struct ocfs2_alloc_context *data_ac,
  2173. struct buffer_head **dx_leaves,
  2174. int num_dx_leaves, u64 *ret_phys_blkno)
  2175. {
  2176. int ret;
  2177. u32 phys, num;
  2178. u64 phys_blkno;
  2179. struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
  2180. /*
  2181. * XXX: For create, this should claim cluster for the index
  2182. * *before* the unindexed insert so that we have a better
  2183. * chance of contiguousness as the directory grows in number
  2184. * of entries.
  2185. */
  2186. ret = __ocfs2_claim_clusters(handle, data_ac, 1, 1, &phys, &num);
  2187. if (ret) {
  2188. mlog_errno(ret);
  2189. goto out;
  2190. }
  2191. /*
  2192. * Format the new cluster first. That way, we're inserting
  2193. * valid data.
  2194. */
  2195. phys_blkno = ocfs2_clusters_to_blocks(osb->sb, phys);
  2196. ret = ocfs2_dx_dir_format_cluster(osb, handle, dir, dx_leaves,
  2197. num_dx_leaves, phys_blkno);
  2198. if (ret) {
  2199. mlog_errno(ret);
  2200. goto out;
  2201. }
  2202. *ret_phys_blkno = phys_blkno;
  2203. out:
  2204. return ret;
  2205. }
  2206. static int ocfs2_dx_dir_new_cluster(struct inode *dir,
  2207. struct ocfs2_extent_tree *et,
  2208. u32 cpos, handle_t *handle,
  2209. struct ocfs2_alloc_context *data_ac,
  2210. struct ocfs2_alloc_context *meta_ac,
  2211. struct buffer_head **dx_leaves,
  2212. int num_dx_leaves)
  2213. {
  2214. int ret;
  2215. u64 phys_blkno;
  2216. ret = __ocfs2_dx_dir_new_cluster(dir, cpos, handle, data_ac, dx_leaves,
  2217. num_dx_leaves, &phys_blkno);
  2218. if (ret) {
  2219. mlog_errno(ret);
  2220. goto out;
  2221. }
  2222. ret = ocfs2_insert_extent(handle, et, cpos, phys_blkno, 1, 0,
  2223. meta_ac);
  2224. if (ret)
  2225. mlog_errno(ret);
  2226. out:
  2227. return ret;
  2228. }
  2229. static struct buffer_head **ocfs2_dx_dir_kmalloc_leaves(struct super_block *sb,
  2230. int *ret_num_leaves)
  2231. {
  2232. int num_dx_leaves = ocfs2_clusters_to_blocks(sb, 1);
  2233. struct buffer_head **dx_leaves;
  2234. dx_leaves = kcalloc(num_dx_leaves, sizeof(struct buffer_head *),
  2235. GFP_NOFS);
  2236. if (dx_leaves && ret_num_leaves)
  2237. *ret_num_leaves = num_dx_leaves;
  2238. return dx_leaves;
  2239. }
  2240. static int ocfs2_fill_new_dir_dx(struct ocfs2_super *osb,
  2241. handle_t *handle,
  2242. struct inode *parent,
  2243. struct inode *inode,
  2244. struct buffer_head *di_bh,
  2245. struct ocfs2_alloc_context *data_ac,
  2246. struct ocfs2_alloc_context *meta_ac)
  2247. {
  2248. int ret;
  2249. struct buffer_head *leaf_bh = NULL;
  2250. struct buffer_head *dx_root_bh = NULL;
  2251. struct ocfs2_dx_hinfo hinfo;
  2252. struct ocfs2_dx_root_block *dx_root;
  2253. struct ocfs2_dx_entry_list *entry_list;
  2254. /*
  2255. * Our strategy is to create the directory as though it were
  2256. * unindexed, then add the index block. This works with very
  2257. * little complication since the state of a new directory is a
  2258. * very well known quantity.
  2259. *
  2260. * Essentially, we have two dirents ("." and ".."), in the 1st
  2261. * block which need indexing. These are easily inserted into
  2262. * the index block.
  2263. */
  2264. ret = ocfs2_fill_new_dir_el(osb, handle, parent, inode, di_bh,
  2265. data_ac, &leaf_bh);
  2266. if (ret) {
  2267. mlog_errno(ret);
  2268. goto out;
  2269. }
  2270. ret = ocfs2_dx_dir_attach_index(osb, handle, inode, di_bh, leaf_bh,
  2271. meta_ac, 1, 2, &dx_root_bh);
  2272. if (ret) {
  2273. mlog_errno(ret);
  2274. goto out;
  2275. }
  2276. dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
  2277. entry_list = &dx_root->dr_entries;
  2278. /* Buffer has been journaled for us by ocfs2_dx_dir_attach_index */
  2279. ocfs2_dx_dir_name_hash(inode, ".", 1, &hinfo);
  2280. ocfs2_dx_entry_list_insert(entry_list, &hinfo, leaf_bh->b_blocknr);
  2281. ocfs2_dx_dir_name_hash(inode, "..", 2, &hinfo);
  2282. ocfs2_dx_entry_list_insert(entry_list, &hinfo, leaf_bh->b_blocknr);
  2283. out:
  2284. brelse(dx_root_bh);
  2285. brelse(leaf_bh);
  2286. return ret;
  2287. }
  2288. int ocfs2_fill_new_dir(struct ocfs2_super *osb,
  2289. handle_t *handle,
  2290. struct inode *parent,
  2291. struct inode *inode,
  2292. struct buffer_head *fe_bh,
  2293. struct ocfs2_alloc_context *data_ac,
  2294. struct ocfs2_alloc_context *meta_ac)
  2295. {
  2296. BUG_ON(!ocfs2_supports_inline_data(osb) && data_ac == NULL);
  2297. if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
  2298. return ocfs2_fill_new_dir_id(osb, handle, parent, inode, fe_bh);
  2299. if (ocfs2_supports_indexed_dirs(osb))
  2300. return ocfs2_fill_new_dir_dx(osb, handle, parent, inode, fe_bh,
  2301. data_ac, meta_ac);
  2302. return ocfs2_fill_new_dir_el(osb, handle, parent, inode, fe_bh,
  2303. data_ac, NULL);
  2304. }
  2305. static int ocfs2_dx_dir_index_block(struct inode *dir,
  2306. handle_t *handle,
  2307. struct buffer_head **dx_leaves,
  2308. int num_dx_leaves,
  2309. u32 *num_dx_entries,
  2310. struct buffer_head *dirent_bh)
  2311. {
  2312. int ret = 0, namelen, i;
  2313. char *de_buf, *limit;
  2314. struct ocfs2_dir_entry *de;
  2315. struct buffer_head *dx_leaf_bh;
  2316. struct ocfs2_dx_hinfo hinfo;
  2317. u64 dirent_blk = dirent_bh->b_blocknr;
  2318. de_buf = dirent_bh->b_data;
  2319. limit = de_buf + dir->i_sb->s_blocksize;
  2320. while (de_buf < limit) {
  2321. de = (struct ocfs2_dir_entry *)de_buf;
  2322. namelen = de->name_len;
  2323. if (!namelen || !de->inode)
  2324. goto inc;
  2325. ocfs2_dx_dir_name_hash(dir, de->name, namelen, &hinfo);
  2326. i = ocfs2_dx_dir_hash_idx(OCFS2_SB(dir->i_sb), &hinfo);
  2327. dx_leaf_bh = dx_leaves[i];
  2328. ret = __ocfs2_dx_dir_leaf_insert(dir, handle, &hinfo,
  2329. dirent_blk, dx_leaf_bh);
  2330. if (ret) {
  2331. mlog_errno(ret);
  2332. goto out;
  2333. }
  2334. *num_dx_entries = *num_dx_entries + 1;
  2335. inc:
  2336. de_buf += le16_to_cpu(de->rec_len);
  2337. }
  2338. out:
  2339. return ret;
  2340. }
  2341. /*
  2342. * XXX: This expects dx_root_bh to already be part of the transaction.
  2343. */
  2344. static void ocfs2_dx_dir_index_root_block(struct inode *dir,
  2345. struct buffer_head *dx_root_bh,
  2346. struct buffer_head *dirent_bh)
  2347. {
  2348. char *de_buf, *limit;
  2349. struct ocfs2_dx_root_block *dx_root;
  2350. struct ocfs2_dir_entry *de;
  2351. struct ocfs2_dx_hinfo hinfo;
  2352. u64 dirent_blk = dirent_bh->b_blocknr;
  2353. dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
  2354. de_buf = dirent_bh->b_data;
  2355. limit = de_buf + dir->i_sb->s_blocksize;
  2356. while (de_buf < limit) {
  2357. de = (struct ocfs2_dir_entry *)de_buf;
  2358. if (!de->name_len || !de->inode)
  2359. goto inc;
  2360. ocfs2_dx_dir_name_hash(dir, de->name, de->name_len, &hinfo);
  2361. mlog(0,
  2362. "dir: %llu, major: 0x%x minor: 0x%x, index: %u, name: %.*s\n",
  2363. (unsigned long long)dir->i_ino, hinfo.major_hash,
  2364. hinfo.minor_hash,
  2365. le16_to_cpu(dx_root->dr_entries.de_num_used),
  2366. de->name_len, de->name);
  2367. ocfs2_dx_entry_list_insert(&dx_root->dr_entries, &hinfo,
  2368. dirent_blk);
  2369. le32_add_cpu(&dx_root->dr_num_entries, 1);
  2370. inc:
  2371. de_buf += le16_to_cpu(de->rec_len);
  2372. }
  2373. }
  2374. /*
  2375. * Count the number of inline directory entries in di_bh and compare
  2376. * them against the number of entries we can hold in an inline dx root
  2377. * block.
  2378. */
  2379. static int ocfs2_new_dx_should_be_inline(struct inode *dir,
  2380. struct buffer_head *di_bh)
  2381. {
  2382. int dirent_count = 0;
  2383. char *de_buf, *limit;
  2384. struct ocfs2_dir_entry *de;
  2385. struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
  2386. de_buf = di->id2.i_data.id_data;
  2387. limit = de_buf + i_size_read(dir);
  2388. while (de_buf < limit) {
  2389. de = (struct ocfs2_dir_entry *)de_buf;
  2390. if (de->name_len && de->inode)
  2391. dirent_count++;
  2392. de_buf += le16_to_cpu(de->rec_len);
  2393. }
  2394. /* We are careful to leave room for one extra record. */
  2395. return dirent_count < ocfs2_dx_entries_per_root(dir->i_sb);
  2396. }
  2397. /*
  2398. * Expand rec_len of the rightmost dirent in a directory block so that it
  2399. * contains the end of our valid space for dirents. We do this during
  2400. * expansion from an inline directory to one with extents. The first dir block
  2401. * in that case is taken from the inline data portion of the inode block.
  2402. *
  2403. * This will also return the largest amount of contiguous space for a dirent
  2404. * in the block. That value is *not* necessarily the last dirent, even after
  2405. * expansion. The directory indexing code wants this value for free space
  2406. * accounting. We do this here since we're already walking the entire dir
  2407. * block.
  2408. *
  2409. * We add the dir trailer if this filesystem wants it.
  2410. */
  2411. static unsigned int ocfs2_expand_last_dirent(char *start, unsigned int old_size,
  2412. struct inode *dir)
  2413. {
  2414. struct super_block *sb = dir->i_sb;
  2415. struct ocfs2_dir_entry *de;
  2416. struct ocfs2_dir_entry *prev_de;
  2417. char *de_buf, *limit;
  2418. unsigned int new_size = sb->s_blocksize;
  2419. unsigned int bytes, this_hole;
  2420. unsigned int largest_hole = 0;
  2421. if (ocfs2_new_dir_wants_trailer(dir))
  2422. new_size = ocfs2_dir_trailer_blk_off(sb);
  2423. bytes = new_size - old_size;
  2424. limit = start + old_size;
  2425. de_buf = start;
  2426. de = (struct ocfs2_dir_entry *)de_buf;
  2427. do {
  2428. this_hole = ocfs2_figure_dirent_hole(de);
  2429. if (this_hole > largest_hole)
  2430. largest_hole = this_hole;
  2431. prev_de = de;
  2432. de_buf += le16_to_cpu(de->rec_len);
  2433. de = (struct ocfs2_dir_entry *)de_buf;
  2434. } while (de_buf < limit);
  2435. le16_add_cpu(&prev_de->rec_len, bytes);
  2436. /* We need to double check this after modification of the final
  2437. * dirent. */
  2438. this_hole = ocfs2_figure_dirent_hole(prev_de);
  2439. if (this_hole > largest_hole)
  2440. largest_hole = this_hole;
  2441. if (largest_hole >= OCFS2_DIR_MIN_REC_LEN)
  2442. return largest_hole;
  2443. return 0;
  2444. }
  2445. /*
  2446. * We allocate enough clusters to fulfill "blocks_wanted", but set
  2447. * i_size to exactly one block. Ocfs2_extend_dir() will handle the
  2448. * rest automatically for us.
  2449. *
  2450. * *first_block_bh is a pointer to the 1st data block allocated to the
  2451. * directory.
  2452. */
  2453. static int ocfs2_expand_inline_dir(struct inode *dir, struct buffer_head *di_bh,
  2454. unsigned int blocks_wanted,
  2455. struct ocfs2_dir_lookup_result *lookup,
  2456. struct buffer_head **first_block_bh)
  2457. {
  2458. u32 alloc, dx_alloc, bit_off, len, num_dx_entries = 0;
  2459. struct super_block *sb = dir->i_sb;
  2460. int ret, i, num_dx_leaves = 0, dx_inline = 0,
  2461. credits = ocfs2_inline_to_extents_credits(sb);
  2462. u64 dx_insert_blkno, blkno,
  2463. bytes = blocks_wanted << sb->s_blocksize_bits;
  2464. struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
  2465. struct ocfs2_inode_info *oi = OCFS2_I(dir);
  2466. struct ocfs2_alloc_context *data_ac;
  2467. struct ocfs2_alloc_context *meta_ac = NULL;
  2468. struct buffer_head *dirdata_bh = NULL;
  2469. struct buffer_head *dx_root_bh = NULL;
  2470. struct buffer_head **dx_leaves = NULL;
  2471. struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
  2472. handle_t *handle;
  2473. struct ocfs2_extent_tree et;
  2474. struct ocfs2_extent_tree dx_et;
  2475. int did_quota = 0, bytes_allocated = 0;
  2476. ocfs2_init_dinode_extent_tree(&et, INODE_CACHE(dir), di_bh);
  2477. alloc = ocfs2_clusters_for_bytes(sb, bytes);
  2478. dx_alloc = 0;
  2479. down_write(&oi->ip_alloc_sem);
  2480. if (ocfs2_supports_indexed_dirs(osb)) {
  2481. credits += ocfs2_add_dir_index_credits(sb);
  2482. dx_inline = ocfs2_new_dx_should_be_inline(dir, di_bh);
  2483. if (!dx_inline) {
  2484. /* Add one more cluster for an index leaf */
  2485. dx_alloc++;
  2486. dx_leaves = ocfs2_dx_dir_kmalloc_leaves(sb,
  2487. &num_dx_leaves);
  2488. if (!dx_leaves) {
  2489. ret = -ENOMEM;
  2490. mlog_errno(ret);
  2491. goto out;
  2492. }
  2493. }
  2494. /* This gets us the dx_root */
  2495. ret = ocfs2_reserve_new_metadata_blocks(osb, 1, &meta_ac);
  2496. if (ret) {
  2497. mlog_errno(ret);
  2498. goto out;
  2499. }
  2500. }
  2501. /*
  2502. * We should never need more than 2 clusters for the unindexed
  2503. * tree - maximum dirent size is far less than one block. In
  2504. * fact, the only time we'd need more than one cluster is if
  2505. * blocksize == clustersize and the dirent won't fit in the
  2506. * extra space that the expansion to a single block gives. As
  2507. * of today, that only happens on 4k/4k file systems.
  2508. */
  2509. BUG_ON(alloc > 2);
  2510. ret = ocfs2_reserve_clusters(osb, alloc + dx_alloc, &data_ac);
  2511. if (ret) {
  2512. mlog_errno(ret);
  2513. goto out;
  2514. }
  2515. /*
  2516. * Prepare for worst case allocation scenario of two separate
  2517. * extents in the unindexed tree.
  2518. */
  2519. if (alloc == 2)
  2520. credits += OCFS2_SUBALLOC_ALLOC;
  2521. handle = ocfs2_start_trans(osb, credits);
  2522. if (IS_ERR(handle)) {
  2523. ret = PTR_ERR(handle);
  2524. mlog_errno(ret);
  2525. goto out;
  2526. }
  2527. ret = dquot_alloc_space_nodirty(dir,
  2528. ocfs2_clusters_to_bytes(osb->sb, alloc + dx_alloc));
  2529. if (ret)
  2530. goto out_commit;
  2531. did_quota = 1;
  2532. if (ocfs2_supports_indexed_dirs(osb) && !dx_inline) {
  2533. /*
  2534. * Allocate our index cluster first, to maximize the
  2535. * possibility that unindexed leaves grow
  2536. * contiguously.
  2537. */
  2538. ret = __ocfs2_dx_dir_new_cluster(dir, 0, handle, data_ac,
  2539. dx_leaves, num_dx_leaves,
  2540. &dx_insert_blkno);
  2541. if (ret) {
  2542. mlog_errno(ret);
  2543. goto out_commit;
  2544. }
  2545. bytes_allocated += ocfs2_clusters_to_bytes(dir->i_sb, 1);
  2546. }
  2547. /*
  2548. * Try to claim as many clusters as the bitmap can give though
  2549. * if we only get one now, that's enough to continue. The rest
  2550. * will be claimed after the conversion to extents.
  2551. */
  2552. if (ocfs2_dir_resv_allowed(osb))
  2553. data_ac->ac_resv = &oi->ip_la_data_resv;
  2554. ret = ocfs2_claim_clusters(handle, data_ac, 1, &bit_off, &len);
  2555. if (ret) {
  2556. mlog_errno(ret);
  2557. goto out_commit;
  2558. }
  2559. bytes_allocated += ocfs2_clusters_to_bytes(dir->i_sb, 1);
  2560. /*
  2561. * Operations are carefully ordered so that we set up the new
  2562. * data block first. The conversion from inline data to
  2563. * extents follows.
  2564. */
  2565. blkno = ocfs2_clusters_to_blocks(dir->i_sb, bit_off);
  2566. dirdata_bh = sb_getblk(sb, blkno);
  2567. if (!dirdata_bh) {
  2568. ret = -EIO;
  2569. mlog_errno(ret);
  2570. goto out_commit;
  2571. }
  2572. ocfs2_set_new_buffer_uptodate(INODE_CACHE(dir), dirdata_bh);
  2573. ret = ocfs2_journal_access_db(handle, INODE_CACHE(dir), dirdata_bh,
  2574. OCFS2_JOURNAL_ACCESS_CREATE);
  2575. if (ret) {
  2576. mlog_errno(ret);
  2577. goto out_commit;
  2578. }
  2579. memcpy(dirdata_bh->b_data, di->id2.i_data.id_data, i_size_read(dir));
  2580. memset(dirdata_bh->b_data + i_size_read(dir), 0,
  2581. sb->s_blocksize - i_size_read(dir));
  2582. i = ocfs2_expand_last_dirent(dirdata_bh->b_data, i_size_read(dir), dir);
  2583. if (ocfs2_new_dir_wants_trailer(dir)) {
  2584. /*
  2585. * Prepare the dir trailer up front. It will otherwise look
  2586. * like a valid dirent. Even if inserting the index fails
  2587. * (unlikely), then all we'll have done is given first dir
  2588. * block a small amount of fragmentation.
  2589. */
  2590. ocfs2_init_dir_trailer(dir, dirdata_bh, i);
  2591. }
  2592. ocfs2_journal_dirty(handle, dirdata_bh);
  2593. if (ocfs2_supports_indexed_dirs(osb) && !dx_inline) {
  2594. /*
  2595. * Dx dirs with an external cluster need to do this up
  2596. * front. Inline dx root's get handled later, after
  2597. * we've allocated our root block. We get passed back
  2598. * a total number of items so that dr_num_entries can
  2599. * be correctly set once the dx_root has been
  2600. * allocated.
  2601. */
  2602. ret = ocfs2_dx_dir_index_block(dir, handle, dx_leaves,
  2603. num_dx_leaves, &num_dx_entries,
  2604. dirdata_bh);
  2605. if (ret) {
  2606. mlog_errno(ret);
  2607. goto out_commit;
  2608. }
  2609. }
  2610. /*
  2611. * Set extent, i_size, etc on the directory. After this, the
  2612. * inode should contain the same exact dirents as before and
  2613. * be fully accessible from system calls.
  2614. *
  2615. * We let the later dirent insert modify c/mtime - to the user
  2616. * the data hasn't changed.
  2617. */
  2618. ret = ocfs2_journal_access_di(handle, INODE_CACHE(dir), di_bh,
  2619. OCFS2_JOURNAL_ACCESS_CREATE);
  2620. if (ret) {
  2621. mlog_errno(ret);
  2622. goto out_commit;
  2623. }
  2624. spin_lock(&oi->ip_lock);
  2625. oi->ip_dyn_features &= ~OCFS2_INLINE_DATA_FL;
  2626. di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
  2627. spin_unlock(&oi->ip_lock);
  2628. ocfs2_dinode_new_extent_list(dir, di);
  2629. i_size_write(dir, sb->s_blocksize);
  2630. dir->i_mtime = dir->i_ctime = CURRENT_TIME;
  2631. di->i_size = cpu_to_le64(sb->s_blocksize);
  2632. di->i_ctime = di->i_mtime = cpu_to_le64(dir->i_ctime.tv_sec);
  2633. di->i_ctime_nsec = di->i_mtime_nsec = cpu_to_le32(dir->i_ctime.tv_nsec);
  2634. /*
  2635. * This should never fail as our extent list is empty and all
  2636. * related blocks have been journaled already.
  2637. */
  2638. ret = ocfs2_insert_extent(handle, &et, 0, blkno, len,
  2639. 0, NULL);
  2640. if (ret) {
  2641. mlog_errno(ret);
  2642. goto out_commit;
  2643. }
  2644. /*
  2645. * Set i_blocks after the extent insert for the most up to
  2646. * date ip_clusters value.
  2647. */
  2648. dir->i_blocks = ocfs2_inode_sector_count(dir);
  2649. ocfs2_journal_dirty(handle, di_bh);
  2650. if (ocfs2_supports_indexed_dirs(osb)) {
  2651. ret = ocfs2_dx_dir_attach_index(osb, handle, dir, di_bh,
  2652. dirdata_bh, meta_ac, dx_inline,
  2653. num_dx_entries, &dx_root_bh);
  2654. if (ret) {
  2655. mlog_errno(ret);
  2656. goto out_commit;
  2657. }
  2658. if (dx_inline) {
  2659. ocfs2_dx_dir_index_root_block(dir, dx_root_bh,
  2660. dirdata_bh);
  2661. } else {
  2662. ocfs2_init_dx_root_extent_tree(&dx_et,
  2663. INODE_CACHE(dir),
  2664. dx_root_bh);
  2665. ret = ocfs2_insert_extent(handle, &dx_et, 0,
  2666. dx_insert_blkno, 1, 0, NULL);
  2667. if (ret)
  2668. mlog_errno(ret);
  2669. }
  2670. }
  2671. /*
  2672. * We asked for two clusters, but only got one in the 1st
  2673. * pass. Claim the 2nd cluster as a separate extent.
  2674. */
  2675. if (alloc > len) {
  2676. ret = ocfs2_claim_clusters(handle, data_ac, 1, &bit_off,
  2677. &len);
  2678. if (ret) {
  2679. mlog_errno(ret);
  2680. goto out_commit;
  2681. }
  2682. blkno = ocfs2_clusters_to_blocks(dir->i_sb, bit_off);
  2683. ret = ocfs2_insert_extent(handle, &et, 1,
  2684. blkno, len, 0, NULL);
  2685. if (ret) {
  2686. mlog_errno(ret);
  2687. goto out_commit;
  2688. }
  2689. bytes_allocated += ocfs2_clusters_to_bytes(dir->i_sb, 1);
  2690. }
  2691. *first_block_bh = dirdata_bh;
  2692. dirdata_bh = NULL;
  2693. if (ocfs2_supports_indexed_dirs(osb)) {
  2694. unsigned int off;
  2695. if (!dx_inline) {
  2696. /*
  2697. * We need to return the correct block within the
  2698. * cluster which should hold our entry.
  2699. */
  2700. off = ocfs2_dx_dir_hash_idx(OCFS2_SB(dir->i_sb),
  2701. &lookup->dl_hinfo);
  2702. get_bh(dx_leaves[off]);
  2703. lookup->dl_dx_leaf_bh = dx_leaves[off];
  2704. }
  2705. lookup->dl_dx_root_bh = dx_root_bh;
  2706. dx_root_bh = NULL;
  2707. }
  2708. out_commit:
  2709. if (ret < 0 && did_quota)
  2710. dquot_free_space_nodirty(dir, bytes_allocated);
  2711. ocfs2_commit_trans(osb, handle);
  2712. out:
  2713. up_write(&oi->ip_alloc_sem);
  2714. if (data_ac)
  2715. ocfs2_free_alloc_context(data_ac);
  2716. if (meta_ac)
  2717. ocfs2_free_alloc_context(meta_ac);
  2718. if (dx_leaves) {
  2719. for (i = 0; i < num_dx_leaves; i++)
  2720. brelse(dx_leaves[i]);
  2721. kfree(dx_leaves);
  2722. }
  2723. brelse(dirdata_bh);
  2724. brelse(dx_root_bh);
  2725. return ret;
  2726. }
  2727. /* returns a bh of the 1st new block in the allocation. */
  2728. static int ocfs2_do_extend_dir(struct super_block *sb,
  2729. handle_t *handle,
  2730. struct inode *dir,
  2731. struct buffer_head *parent_fe_bh,
  2732. struct ocfs2_alloc_context *data_ac,
  2733. struct ocfs2_alloc_context *meta_ac,
  2734. struct buffer_head **new_bh)
  2735. {
  2736. int status;
  2737. int extend, did_quota = 0;
  2738. u64 p_blkno, v_blkno;
  2739. spin_lock(&OCFS2_I(dir)->ip_lock);
  2740. extend = (i_size_read(dir) == ocfs2_clusters_to_bytes(sb, OCFS2_I(dir)->ip_clusters));
  2741. spin_unlock(&OCFS2_I(dir)->ip_lock);
  2742. if (extend) {
  2743. u32 offset = OCFS2_I(dir)->ip_clusters;
  2744. status = dquot_alloc_space_nodirty(dir,
  2745. ocfs2_clusters_to_bytes(sb, 1));
  2746. if (status)
  2747. goto bail;
  2748. did_quota = 1;
  2749. status = ocfs2_add_inode_data(OCFS2_SB(sb), dir, &offset,
  2750. 1, 0, parent_fe_bh, handle,
  2751. data_ac, meta_ac, NULL);
  2752. BUG_ON(status == -EAGAIN);
  2753. if (status < 0) {
  2754. mlog_errno(status);
  2755. goto bail;
  2756. }
  2757. }
  2758. v_blkno = ocfs2_blocks_for_bytes(sb, i_size_read(dir));
  2759. status = ocfs2_extent_map_get_blocks(dir, v_blkno, &p_blkno, NULL, NULL);
  2760. if (status < 0) {
  2761. mlog_errno(status);
  2762. goto bail;
  2763. }
  2764. *new_bh = sb_getblk(sb, p_blkno);
  2765. if (!*new_bh) {
  2766. status = -EIO;
  2767. mlog_errno(status);
  2768. goto bail;
  2769. }
  2770. status = 0;
  2771. bail:
  2772. if (did_quota && status < 0)
  2773. dquot_free_space_nodirty(dir, ocfs2_clusters_to_bytes(sb, 1));
  2774. mlog_exit(status);
  2775. return status;
  2776. }
  2777. /*
  2778. * Assumes you already have a cluster lock on the directory.
  2779. *
  2780. * 'blocks_wanted' is only used if we have an inline directory which
  2781. * is to be turned into an extent based one. The size of the dirent to
  2782. * insert might be larger than the space gained by growing to just one
  2783. * block, so we may have to grow the inode by two blocks in that case.
  2784. *
  2785. * If the directory is already indexed, dx_root_bh must be provided.
  2786. */
  2787. static int ocfs2_extend_dir(struct ocfs2_super *osb,
  2788. struct inode *dir,
  2789. struct buffer_head *parent_fe_bh,
  2790. unsigned int blocks_wanted,
  2791. struct ocfs2_dir_lookup_result *lookup,
  2792. struct buffer_head **new_de_bh)
  2793. {
  2794. int status = 0;
  2795. int credits, num_free_extents, drop_alloc_sem = 0;
  2796. loff_t dir_i_size;
  2797. struct ocfs2_dinode *fe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
  2798. struct ocfs2_extent_list *el = &fe->id2.i_list;
  2799. struct ocfs2_alloc_context *data_ac = NULL;
  2800. struct ocfs2_alloc_context *meta_ac = NULL;
  2801. handle_t *handle = NULL;
  2802. struct buffer_head *new_bh = NULL;
  2803. struct ocfs2_dir_entry * de;
  2804. struct super_block *sb = osb->sb;
  2805. struct ocfs2_extent_tree et;
  2806. struct buffer_head *dx_root_bh = lookup->dl_dx_root_bh;
  2807. mlog_entry_void();
  2808. if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
  2809. /*
  2810. * This would be a code error as an inline directory should
  2811. * never have an index root.
  2812. */
  2813. BUG_ON(dx_root_bh);
  2814. status = ocfs2_expand_inline_dir(dir, parent_fe_bh,
  2815. blocks_wanted, lookup,
  2816. &new_bh);
  2817. if (status) {
  2818. mlog_errno(status);
  2819. goto bail;
  2820. }
  2821. /* Expansion from inline to an indexed directory will
  2822. * have given us this. */
  2823. dx_root_bh = lookup->dl_dx_root_bh;
  2824. if (blocks_wanted == 1) {
  2825. /*
  2826. * If the new dirent will fit inside the space
  2827. * created by pushing out to one block, then
  2828. * we can complete the operation
  2829. * here. Otherwise we have to expand i_size
  2830. * and format the 2nd block below.
  2831. */
  2832. BUG_ON(new_bh == NULL);
  2833. goto bail_bh;
  2834. }
  2835. /*
  2836. * Get rid of 'new_bh' - we want to format the 2nd
  2837. * data block and return that instead.
  2838. */
  2839. brelse(new_bh);
  2840. new_bh = NULL;
  2841. down_write(&OCFS2_I(dir)->ip_alloc_sem);
  2842. drop_alloc_sem = 1;
  2843. dir_i_size = i_size_read(dir);
  2844. credits = OCFS2_SIMPLE_DIR_EXTEND_CREDITS;
  2845. goto do_extend;
  2846. }
  2847. down_write(&OCFS2_I(dir)->ip_alloc_sem);
  2848. drop_alloc_sem = 1;
  2849. dir_i_size = i_size_read(dir);
  2850. mlog(0, "extending dir %llu (i_size = %lld)\n",
  2851. (unsigned long long)OCFS2_I(dir)->ip_blkno, dir_i_size);
  2852. /* dir->i_size is always block aligned. */
  2853. spin_lock(&OCFS2_I(dir)->ip_lock);
  2854. if (dir_i_size == ocfs2_clusters_to_bytes(sb, OCFS2_I(dir)->ip_clusters)) {
  2855. spin_unlock(&OCFS2_I(dir)->ip_lock);
  2856. ocfs2_init_dinode_extent_tree(&et, INODE_CACHE(dir),
  2857. parent_fe_bh);
  2858. num_free_extents = ocfs2_num_free_extents(osb, &et);
  2859. if (num_free_extents < 0) {
  2860. status = num_free_extents;
  2861. mlog_errno(status);
  2862. goto bail;
  2863. }
  2864. if (!num_free_extents) {
  2865. status = ocfs2_reserve_new_metadata(osb, el, &meta_ac);
  2866. if (status < 0) {
  2867. if (status != -ENOSPC)
  2868. mlog_errno(status);
  2869. goto bail;
  2870. }
  2871. }
  2872. status = ocfs2_reserve_clusters(osb, 1, &data_ac);
  2873. if (status < 0) {
  2874. if (status != -ENOSPC)
  2875. mlog_errno(status);
  2876. goto bail;
  2877. }
  2878. if (ocfs2_dir_resv_allowed(osb))
  2879. data_ac->ac_resv = &OCFS2_I(dir)->ip_la_data_resv;
  2880. credits = ocfs2_calc_extend_credits(sb, el, 1);
  2881. } else {
  2882. spin_unlock(&OCFS2_I(dir)->ip_lock);
  2883. credits = OCFS2_SIMPLE_DIR_EXTEND_CREDITS;
  2884. }
  2885. do_extend:
  2886. if (ocfs2_dir_indexed(dir))
  2887. credits++; /* For attaching the new dirent block to the
  2888. * dx_root */
  2889. handle = ocfs2_start_trans(osb, credits);
  2890. if (IS_ERR(handle)) {
  2891. status = PTR_ERR(handle);
  2892. handle = NULL;
  2893. mlog_errno(status);
  2894. goto bail;
  2895. }
  2896. status = ocfs2_do_extend_dir(osb->sb, handle, dir, parent_fe_bh,
  2897. data_ac, meta_ac, &new_bh);
  2898. if (status < 0) {
  2899. mlog_errno(status);
  2900. goto bail;
  2901. }
  2902. ocfs2_set_new_buffer_uptodate(INODE_CACHE(dir), new_bh);
  2903. status = ocfs2_journal_access_db(handle, INODE_CACHE(dir), new_bh,
  2904. OCFS2_JOURNAL_ACCESS_CREATE);
  2905. if (status < 0) {
  2906. mlog_errno(status);
  2907. goto bail;
  2908. }
  2909. memset(new_bh->b_data, 0, sb->s_blocksize);
  2910. de = (struct ocfs2_dir_entry *) new_bh->b_data;
  2911. de->inode = 0;
  2912. if (ocfs2_supports_dir_trailer(dir)) {
  2913. de->rec_len = cpu_to_le16(ocfs2_dir_trailer_blk_off(sb));
  2914. ocfs2_init_dir_trailer(dir, new_bh, le16_to_cpu(de->rec_len));
  2915. if (ocfs2_dir_indexed(dir)) {
  2916. status = ocfs2_dx_dir_link_trailer(dir, handle,
  2917. dx_root_bh, new_bh);
  2918. if (status) {
  2919. mlog_errno(status);
  2920. goto bail;
  2921. }
  2922. }
  2923. } else {
  2924. de->rec_len = cpu_to_le16(sb->s_blocksize);
  2925. }
  2926. ocfs2_journal_dirty(handle, new_bh);
  2927. dir_i_size += dir->i_sb->s_blocksize;
  2928. i_size_write(dir, dir_i_size);
  2929. dir->i_blocks = ocfs2_inode_sector_count(dir);
  2930. status = ocfs2_mark_inode_dirty(handle, dir, parent_fe_bh);
  2931. if (status < 0) {
  2932. mlog_errno(status);
  2933. goto bail;
  2934. }
  2935. bail_bh:
  2936. *new_de_bh = new_bh;
  2937. get_bh(*new_de_bh);
  2938. bail:
  2939. if (handle)
  2940. ocfs2_commit_trans(osb, handle);
  2941. if (drop_alloc_sem)
  2942. up_write(&OCFS2_I(dir)->ip_alloc_sem);
  2943. if (data_ac)
  2944. ocfs2_free_alloc_context(data_ac);
  2945. if (meta_ac)
  2946. ocfs2_free_alloc_context(meta_ac);
  2947. brelse(new_bh);
  2948. mlog_exit(status);
  2949. return status;
  2950. }
  2951. static int ocfs2_find_dir_space_id(struct inode *dir, struct buffer_head *di_bh,
  2952. const char *name, int namelen,
  2953. struct buffer_head **ret_de_bh,
  2954. unsigned int *blocks_wanted)
  2955. {
  2956. int ret;
  2957. struct super_block *sb = dir->i_sb;
  2958. struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
  2959. struct ocfs2_dir_entry *de, *last_de = NULL;
  2960. char *de_buf, *limit;
  2961. unsigned long offset = 0;
  2962. unsigned int rec_len, new_rec_len, free_space = dir->i_sb->s_blocksize;
  2963. /*
  2964. * This calculates how many free bytes we'd have in block zero, should
  2965. * this function force expansion to an extent tree.
  2966. */
  2967. if (ocfs2_new_dir_wants_trailer(dir))
  2968. free_space = ocfs2_dir_trailer_blk_off(sb) - i_size_read(dir);
  2969. else
  2970. free_space = dir->i_sb->s_blocksize - i_size_read(dir);
  2971. de_buf = di->id2.i_data.id_data;
  2972. limit = de_buf + i_size_read(dir);
  2973. rec_len = OCFS2_DIR_REC_LEN(namelen);
  2974. while (de_buf < limit) {
  2975. de = (struct ocfs2_dir_entry *)de_buf;
  2976. if (!ocfs2_check_dir_entry(dir, de, di_bh, offset)) {
  2977. ret = -ENOENT;
  2978. goto out;
  2979. }
  2980. if (ocfs2_match(namelen, name, de)) {
  2981. ret = -EEXIST;
  2982. goto out;
  2983. }
  2984. /*
  2985. * No need to check for a trailing dirent record here as
  2986. * they're not used for inline dirs.
  2987. */
  2988. if (ocfs2_dirent_would_fit(de, rec_len)) {
  2989. /* Ok, we found a spot. Return this bh and let
  2990. * the caller actually fill it in. */
  2991. *ret_de_bh = di_bh;
  2992. get_bh(*ret_de_bh);
  2993. ret = 0;
  2994. goto out;
  2995. }
  2996. last_de = de;
  2997. de_buf += le16_to_cpu(de->rec_len);
  2998. offset += le16_to_cpu(de->rec_len);
  2999. }
  3000. /*
  3001. * We're going to require expansion of the directory - figure
  3002. * out how many blocks we'll need so that a place for the
  3003. * dirent can be found.
  3004. */
  3005. *blocks_wanted = 1;
  3006. new_rec_len = le16_to_cpu(last_de->rec_len) + free_space;
  3007. if (new_rec_len < (rec_len + OCFS2_DIR_REC_LEN(last_de->name_len)))
  3008. *blocks_wanted = 2;
  3009. ret = -ENOSPC;
  3010. out:
  3011. return ret;
  3012. }
  3013. static int ocfs2_find_dir_space_el(struct inode *dir, const char *name,
  3014. int namelen, struct buffer_head **ret_de_bh)
  3015. {
  3016. unsigned long offset;
  3017. struct buffer_head *bh = NULL;
  3018. unsigned short rec_len;
  3019. struct ocfs2_dir_entry *de;
  3020. struct super_block *sb = dir->i_sb;
  3021. int status;
  3022. int blocksize = dir->i_sb->s_blocksize;
  3023. status = ocfs2_read_dir_block(dir, 0, &bh, 0);
  3024. if (status) {
  3025. mlog_errno(status);
  3026. goto bail;
  3027. }
  3028. rec_len = OCFS2_DIR_REC_LEN(namelen);
  3029. offset = 0;
  3030. de = (struct ocfs2_dir_entry *) bh->b_data;
  3031. while (1) {
  3032. if ((char *)de >= sb->s_blocksize + bh->b_data) {
  3033. brelse(bh);
  3034. bh = NULL;
  3035. if (i_size_read(dir) <= offset) {
  3036. /*
  3037. * Caller will have to expand this
  3038. * directory.
  3039. */
  3040. status = -ENOSPC;
  3041. goto bail;
  3042. }
  3043. status = ocfs2_read_dir_block(dir,
  3044. offset >> sb->s_blocksize_bits,
  3045. &bh, 0);
  3046. if (status) {
  3047. mlog_errno(status);
  3048. goto bail;
  3049. }
  3050. /* move to next block */
  3051. de = (struct ocfs2_dir_entry *) bh->b_data;
  3052. }
  3053. if (!ocfs2_check_dir_entry(dir, de, bh, offset)) {
  3054. status = -ENOENT;
  3055. goto bail;
  3056. }
  3057. if (ocfs2_match(namelen, name, de)) {
  3058. status = -EEXIST;
  3059. goto bail;
  3060. }
  3061. if (ocfs2_skip_dir_trailer(dir, de, offset % blocksize,
  3062. blocksize))
  3063. goto next;
  3064. if (ocfs2_dirent_would_fit(de, rec_len)) {
  3065. /* Ok, we found a spot. Return this bh and let
  3066. * the caller actually fill it in. */
  3067. *ret_de_bh = bh;
  3068. get_bh(*ret_de_bh);
  3069. status = 0;
  3070. goto bail;
  3071. }
  3072. next:
  3073. offset += le16_to_cpu(de->rec_len);
  3074. de = (struct ocfs2_dir_entry *)((char *) de + le16_to_cpu(de->rec_len));
  3075. }
  3076. status = 0;
  3077. bail:
  3078. brelse(bh);
  3079. mlog_exit(status);
  3080. return status;
  3081. }
  3082. static int dx_leaf_sort_cmp(const void *a, const void *b)
  3083. {
  3084. const struct ocfs2_dx_entry *entry1 = a;
  3085. const struct ocfs2_dx_entry *entry2 = b;
  3086. u32 major_hash1 = le32_to_cpu(entry1->dx_major_hash);
  3087. u32 major_hash2 = le32_to_cpu(entry2->dx_major_hash);
  3088. u32 minor_hash1 = le32_to_cpu(entry1->dx_minor_hash);
  3089. u32 minor_hash2 = le32_to_cpu(entry2->dx_minor_hash);
  3090. if (major_hash1 > major_hash2)
  3091. return 1;
  3092. if (major_hash1 < major_hash2)
  3093. return -1;
  3094. /*
  3095. * It is not strictly necessary to sort by minor
  3096. */
  3097. if (minor_hash1 > minor_hash2)
  3098. return 1;
  3099. if (minor_hash1 < minor_hash2)
  3100. return -1;
  3101. return 0;
  3102. }
  3103. static void dx_leaf_sort_swap(void *a, void *b, int size)
  3104. {
  3105. struct ocfs2_dx_entry *entry1 = a;
  3106. struct ocfs2_dx_entry *entry2 = b;
  3107. struct ocfs2_dx_entry tmp;
  3108. BUG_ON(size != sizeof(*entry1));
  3109. tmp = *entry1;
  3110. *entry1 = *entry2;
  3111. *entry2 = tmp;
  3112. }
  3113. static int ocfs2_dx_leaf_same_major(struct ocfs2_dx_leaf *dx_leaf)
  3114. {
  3115. struct ocfs2_dx_entry_list *dl_list = &dx_leaf->dl_list;
  3116. int i, num = le16_to_cpu(dl_list->de_num_used);
  3117. for (i = 0; i < (num - 1); i++) {
  3118. if (le32_to_cpu(dl_list->de_entries[i].dx_major_hash) !=
  3119. le32_to_cpu(dl_list->de_entries[i + 1].dx_major_hash))
  3120. return 0;
  3121. }
  3122. return 1;
  3123. }
  3124. /*
  3125. * Find the optimal value to split this leaf on. This expects the leaf
  3126. * entries to be in sorted order.
  3127. *
  3128. * leaf_cpos is the cpos of the leaf we're splitting. insert_hash is
  3129. * the hash we want to insert.
  3130. *
  3131. * This function is only concerned with the major hash - that which
  3132. * determines which cluster an item belongs to.
  3133. */
  3134. static int ocfs2_dx_dir_find_leaf_split(struct ocfs2_dx_leaf *dx_leaf,
  3135. u32 leaf_cpos, u32 insert_hash,
  3136. u32 *split_hash)
  3137. {
  3138. struct ocfs2_dx_entry_list *dl_list = &dx_leaf->dl_list;
  3139. int i, num_used = le16_to_cpu(dl_list->de_num_used);
  3140. int allsame;
  3141. /*
  3142. * There's a couple rare, but nasty corner cases we have to
  3143. * check for here. All of them involve a leaf where all value
  3144. * have the same hash, which is what we look for first.
  3145. *
  3146. * Most of the time, all of the above is false, and we simply
  3147. * pick the median value for a split.
  3148. */
  3149. allsame = ocfs2_dx_leaf_same_major(dx_leaf);
  3150. if (allsame) {
  3151. u32 val = le32_to_cpu(dl_list->de_entries[0].dx_major_hash);
  3152. if (val == insert_hash) {
  3153. /*
  3154. * No matter where we would choose to split,
  3155. * the new entry would want to occupy the same
  3156. * block as these. Since there's no space left
  3157. * in their existing block, we know there
  3158. * won't be space after the split.
  3159. */
  3160. return -ENOSPC;
  3161. }
  3162. if (val == leaf_cpos) {
  3163. /*
  3164. * Because val is the same as leaf_cpos (which
  3165. * is the smallest value this leaf can have),
  3166. * yet is not equal to insert_hash, then we
  3167. * know that insert_hash *must* be larger than
  3168. * val (and leaf_cpos). At least cpos+1 in value.
  3169. *
  3170. * We also know then, that there cannot be an
  3171. * adjacent extent (otherwise we'd be looking
  3172. * at it). Choosing this value gives us a
  3173. * chance to get some contiguousness.
  3174. */
  3175. *split_hash = leaf_cpos + 1;
  3176. return 0;
  3177. }
  3178. if (val > insert_hash) {
  3179. /*
  3180. * val can not be the same as insert hash, and
  3181. * also must be larger than leaf_cpos. Also,
  3182. * we know that there can't be a leaf between
  3183. * cpos and val, otherwise the entries with
  3184. * hash 'val' would be there.
  3185. */
  3186. *split_hash = val;
  3187. return 0;
  3188. }
  3189. *split_hash = insert_hash;
  3190. return 0;
  3191. }
  3192. /*
  3193. * Since the records are sorted and the checks above
  3194. * guaranteed that not all records in this block are the same,
  3195. * we simple travel forward, from the median, and pick the 1st
  3196. * record whose value is larger than leaf_cpos.
  3197. */
  3198. for (i = (num_used / 2); i < num_used; i++)
  3199. if (le32_to_cpu(dl_list->de_entries[i].dx_major_hash) >
  3200. leaf_cpos)
  3201. break;
  3202. BUG_ON(i == num_used); /* Should be impossible */
  3203. *split_hash = le32_to_cpu(dl_list->de_entries[i].dx_major_hash);
  3204. return 0;
  3205. }
  3206. /*
  3207. * Transfer all entries in orig_dx_leaves whose major hash is equal to or
  3208. * larger than split_hash into new_dx_leaves. We use a temporary
  3209. * buffer (tmp_dx_leaf) to make the changes to the original leaf blocks.
  3210. *
  3211. * Since the block offset inside a leaf (cluster) is a constant mask
  3212. * of minor_hash, we can optimize - an item at block offset X within
  3213. * the original cluster, will be at offset X within the new cluster.
  3214. */
  3215. static void ocfs2_dx_dir_transfer_leaf(struct inode *dir, u32 split_hash,
  3216. handle_t *handle,
  3217. struct ocfs2_dx_leaf *tmp_dx_leaf,
  3218. struct buffer_head **orig_dx_leaves,
  3219. struct buffer_head **new_dx_leaves,
  3220. int num_dx_leaves)
  3221. {
  3222. int i, j, num_used;
  3223. u32 major_hash;
  3224. struct ocfs2_dx_leaf *orig_dx_leaf, *new_dx_leaf;
  3225. struct ocfs2_dx_entry_list *orig_list, *new_list, *tmp_list;
  3226. struct ocfs2_dx_entry *dx_entry;
  3227. tmp_list = &tmp_dx_leaf->dl_list;
  3228. for (i = 0; i < num_dx_leaves; i++) {
  3229. orig_dx_leaf = (struct ocfs2_dx_leaf *) orig_dx_leaves[i]->b_data;
  3230. orig_list = &orig_dx_leaf->dl_list;
  3231. new_dx_leaf = (struct ocfs2_dx_leaf *) new_dx_leaves[i]->b_data;
  3232. new_list = &new_dx_leaf->dl_list;
  3233. num_used = le16_to_cpu(orig_list->de_num_used);
  3234. memcpy(tmp_dx_leaf, orig_dx_leaf, dir->i_sb->s_blocksize);
  3235. tmp_list->de_num_used = cpu_to_le16(0);
  3236. memset(&tmp_list->de_entries, 0, sizeof(*dx_entry)*num_used);
  3237. for (j = 0; j < num_used; j++) {
  3238. dx_entry = &orig_list->de_entries[j];
  3239. major_hash = le32_to_cpu(dx_entry->dx_major_hash);
  3240. if (major_hash >= split_hash)
  3241. ocfs2_dx_dir_leaf_insert_tail(new_dx_leaf,
  3242. dx_entry);
  3243. else
  3244. ocfs2_dx_dir_leaf_insert_tail(tmp_dx_leaf,
  3245. dx_entry);
  3246. }
  3247. memcpy(orig_dx_leaf, tmp_dx_leaf, dir->i_sb->s_blocksize);
  3248. ocfs2_journal_dirty(handle, orig_dx_leaves[i]);
  3249. ocfs2_journal_dirty(handle, new_dx_leaves[i]);
  3250. }
  3251. }
  3252. static int ocfs2_dx_dir_rebalance_credits(struct ocfs2_super *osb,
  3253. struct ocfs2_dx_root_block *dx_root)
  3254. {
  3255. int credits = ocfs2_clusters_to_blocks(osb->sb, 2);
  3256. credits += ocfs2_calc_extend_credits(osb->sb, &dx_root->dr_list, 1);
  3257. credits += ocfs2_quota_trans_credits(osb->sb);
  3258. return credits;
  3259. }
  3260. /*
  3261. * Find the median value in dx_leaf_bh and allocate a new leaf to move
  3262. * half our entries into.
  3263. */
  3264. static int ocfs2_dx_dir_rebalance(struct ocfs2_super *osb, struct inode *dir,
  3265. struct buffer_head *dx_root_bh,
  3266. struct buffer_head *dx_leaf_bh,
  3267. struct ocfs2_dx_hinfo *hinfo, u32 leaf_cpos,
  3268. u64 leaf_blkno)
  3269. {
  3270. struct ocfs2_dx_leaf *dx_leaf = (struct ocfs2_dx_leaf *)dx_leaf_bh->b_data;
  3271. int credits, ret, i, num_used, did_quota = 0;
  3272. u32 cpos, split_hash, insert_hash = hinfo->major_hash;
  3273. u64 orig_leaves_start;
  3274. int num_dx_leaves;
  3275. struct buffer_head **orig_dx_leaves = NULL;
  3276. struct buffer_head **new_dx_leaves = NULL;
  3277. struct ocfs2_alloc_context *data_ac = NULL, *meta_ac = NULL;
  3278. struct ocfs2_extent_tree et;
  3279. handle_t *handle = NULL;
  3280. struct ocfs2_dx_root_block *dx_root;
  3281. struct ocfs2_dx_leaf *tmp_dx_leaf = NULL;
  3282. mlog(0, "DX Dir: %llu, rebalance leaf leaf_blkno: %llu insert: %u\n",
  3283. (unsigned long long)OCFS2_I(dir)->ip_blkno,
  3284. (unsigned long long)leaf_blkno, insert_hash);
  3285. ocfs2_init_dx_root_extent_tree(&et, INODE_CACHE(dir), dx_root_bh);
  3286. dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
  3287. /*
  3288. * XXX: This is a rather large limit. We should use a more
  3289. * realistic value.
  3290. */
  3291. if (le32_to_cpu(dx_root->dr_clusters) == UINT_MAX)
  3292. return -ENOSPC;
  3293. num_used = le16_to_cpu(dx_leaf->dl_list.de_num_used);
  3294. if (num_used < le16_to_cpu(dx_leaf->dl_list.de_count)) {
  3295. mlog(ML_ERROR, "DX Dir: %llu, Asked to rebalance empty leaf: "
  3296. "%llu, %d\n", (unsigned long long)OCFS2_I(dir)->ip_blkno,
  3297. (unsigned long long)leaf_blkno, num_used);
  3298. ret = -EIO;
  3299. goto out;
  3300. }
  3301. orig_dx_leaves = ocfs2_dx_dir_kmalloc_leaves(osb->sb, &num_dx_leaves);
  3302. if (!orig_dx_leaves) {
  3303. ret = -ENOMEM;
  3304. mlog_errno(ret);
  3305. goto out;
  3306. }
  3307. new_dx_leaves = ocfs2_dx_dir_kmalloc_leaves(osb->sb, NULL);
  3308. if (!new_dx_leaves) {
  3309. ret = -ENOMEM;
  3310. mlog_errno(ret);
  3311. goto out;
  3312. }
  3313. ret = ocfs2_lock_allocators(dir, &et, 1, 0, &data_ac, &meta_ac);
  3314. if (ret) {
  3315. if (ret != -ENOSPC)
  3316. mlog_errno(ret);
  3317. goto out;
  3318. }
  3319. credits = ocfs2_dx_dir_rebalance_credits(osb, dx_root);
  3320. handle = ocfs2_start_trans(osb, credits);
  3321. if (IS_ERR(handle)) {
  3322. ret = PTR_ERR(handle);
  3323. handle = NULL;
  3324. mlog_errno(ret);
  3325. goto out;
  3326. }
  3327. ret = dquot_alloc_space_nodirty(dir,
  3328. ocfs2_clusters_to_bytes(dir->i_sb, 1));
  3329. if (ret)
  3330. goto out_commit;
  3331. did_quota = 1;
  3332. ret = ocfs2_journal_access_dl(handle, INODE_CACHE(dir), dx_leaf_bh,
  3333. OCFS2_JOURNAL_ACCESS_WRITE);
  3334. if (ret) {
  3335. mlog_errno(ret);
  3336. goto out_commit;
  3337. }
  3338. /*
  3339. * This block is changing anyway, so we can sort it in place.
  3340. */
  3341. sort(dx_leaf->dl_list.de_entries, num_used,
  3342. sizeof(struct ocfs2_dx_entry), dx_leaf_sort_cmp,
  3343. dx_leaf_sort_swap);
  3344. ocfs2_journal_dirty(handle, dx_leaf_bh);
  3345. ret = ocfs2_dx_dir_find_leaf_split(dx_leaf, leaf_cpos, insert_hash,
  3346. &split_hash);
  3347. if (ret) {
  3348. mlog_errno(ret);
  3349. goto out_commit;
  3350. }
  3351. mlog(0, "Split leaf (%u) at %u, insert major hash is %u\n",
  3352. leaf_cpos, split_hash, insert_hash);
  3353. /*
  3354. * We have to carefully order operations here. There are items
  3355. * which want to be in the new cluster before insert, but in
  3356. * order to put those items in the new cluster, we alter the
  3357. * old cluster. A failure to insert gets nasty.
  3358. *
  3359. * So, start by reserving writes to the old
  3360. * cluster. ocfs2_dx_dir_new_cluster will reserve writes on
  3361. * the new cluster for us, before inserting it. The insert
  3362. * won't happen if there's an error before that. Once the
  3363. * insert is done then, we can transfer from one leaf into the
  3364. * other without fear of hitting any error.
  3365. */
  3366. /*
  3367. * The leaf transfer wants some scratch space so that we don't
  3368. * wind up doing a bunch of expensive memmove().
  3369. */
  3370. tmp_dx_leaf = kmalloc(osb->sb->s_blocksize, GFP_NOFS);
  3371. if (!tmp_dx_leaf) {
  3372. ret = -ENOMEM;
  3373. mlog_errno(ret);
  3374. goto out_commit;
  3375. }
  3376. orig_leaves_start = ocfs2_block_to_cluster_start(dir->i_sb, leaf_blkno);
  3377. ret = ocfs2_read_dx_leaves(dir, orig_leaves_start, num_dx_leaves,
  3378. orig_dx_leaves);
  3379. if (ret) {
  3380. mlog_errno(ret);
  3381. goto out_commit;
  3382. }
  3383. for (i = 0; i < num_dx_leaves; i++) {
  3384. ret = ocfs2_journal_access_dl(handle, INODE_CACHE(dir),
  3385. orig_dx_leaves[i],
  3386. OCFS2_JOURNAL_ACCESS_WRITE);
  3387. if (ret) {
  3388. mlog_errno(ret);
  3389. goto out_commit;
  3390. }
  3391. }
  3392. cpos = split_hash;
  3393. ret = ocfs2_dx_dir_new_cluster(dir, &et, cpos, handle,
  3394. data_ac, meta_ac, new_dx_leaves,
  3395. num_dx_leaves);
  3396. if (ret) {
  3397. mlog_errno(ret);
  3398. goto out_commit;
  3399. }
  3400. ocfs2_dx_dir_transfer_leaf(dir, split_hash, handle, tmp_dx_leaf,
  3401. orig_dx_leaves, new_dx_leaves, num_dx_leaves);
  3402. out_commit:
  3403. if (ret < 0 && did_quota)
  3404. dquot_free_space_nodirty(dir,
  3405. ocfs2_clusters_to_bytes(dir->i_sb, 1));
  3406. ocfs2_commit_trans(osb, handle);
  3407. out:
  3408. if (orig_dx_leaves || new_dx_leaves) {
  3409. for (i = 0; i < num_dx_leaves; i++) {
  3410. if (orig_dx_leaves)
  3411. brelse(orig_dx_leaves[i]);
  3412. if (new_dx_leaves)
  3413. brelse(new_dx_leaves[i]);
  3414. }
  3415. kfree(orig_dx_leaves);
  3416. kfree(new_dx_leaves);
  3417. }
  3418. if (meta_ac)
  3419. ocfs2_free_alloc_context(meta_ac);
  3420. if (data_ac)
  3421. ocfs2_free_alloc_context(data_ac);
  3422. kfree(tmp_dx_leaf);
  3423. return ret;
  3424. }
  3425. static int ocfs2_find_dir_space_dx(struct ocfs2_super *osb, struct inode *dir,
  3426. struct buffer_head *di_bh,
  3427. struct buffer_head *dx_root_bh,
  3428. const char *name, int namelen,
  3429. struct ocfs2_dir_lookup_result *lookup)
  3430. {
  3431. int ret, rebalanced = 0;
  3432. struct ocfs2_dx_root_block *dx_root;
  3433. struct buffer_head *dx_leaf_bh = NULL;
  3434. struct ocfs2_dx_leaf *dx_leaf;
  3435. u64 blkno;
  3436. u32 leaf_cpos;
  3437. dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
  3438. restart_search:
  3439. ret = ocfs2_dx_dir_lookup(dir, &dx_root->dr_list, &lookup->dl_hinfo,
  3440. &leaf_cpos, &blkno);
  3441. if (ret) {
  3442. mlog_errno(ret);
  3443. goto out;
  3444. }
  3445. ret = ocfs2_read_dx_leaf(dir, blkno, &dx_leaf_bh);
  3446. if (ret) {
  3447. mlog_errno(ret);
  3448. goto out;
  3449. }
  3450. dx_leaf = (struct ocfs2_dx_leaf *)dx_leaf_bh->b_data;
  3451. if (le16_to_cpu(dx_leaf->dl_list.de_num_used) >=
  3452. le16_to_cpu(dx_leaf->dl_list.de_count)) {
  3453. if (rebalanced) {
  3454. /*
  3455. * Rebalancing should have provided us with
  3456. * space in an appropriate leaf.
  3457. *
  3458. * XXX: Is this an abnormal condition then?
  3459. * Should we print a message here?
  3460. */
  3461. ret = -ENOSPC;
  3462. goto out;
  3463. }
  3464. ret = ocfs2_dx_dir_rebalance(osb, dir, dx_root_bh, dx_leaf_bh,
  3465. &lookup->dl_hinfo, leaf_cpos,
  3466. blkno);
  3467. if (ret) {
  3468. if (ret != -ENOSPC)
  3469. mlog_errno(ret);
  3470. goto out;
  3471. }
  3472. /*
  3473. * Restart the lookup. The rebalance might have
  3474. * changed which block our item fits into. Mark our
  3475. * progress, so we only execute this once.
  3476. */
  3477. brelse(dx_leaf_bh);
  3478. dx_leaf_bh = NULL;
  3479. rebalanced = 1;
  3480. goto restart_search;
  3481. }
  3482. lookup->dl_dx_leaf_bh = dx_leaf_bh;
  3483. dx_leaf_bh = NULL;
  3484. out:
  3485. brelse(dx_leaf_bh);
  3486. return ret;
  3487. }
  3488. static int ocfs2_search_dx_free_list(struct inode *dir,
  3489. struct buffer_head *dx_root_bh,
  3490. int namelen,
  3491. struct ocfs2_dir_lookup_result *lookup)
  3492. {
  3493. int ret = -ENOSPC;
  3494. struct buffer_head *leaf_bh = NULL, *prev_leaf_bh = NULL;
  3495. struct ocfs2_dir_block_trailer *db;
  3496. u64 next_block;
  3497. int rec_len = OCFS2_DIR_REC_LEN(namelen);
  3498. struct ocfs2_dx_root_block *dx_root;
  3499. dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
  3500. next_block = le64_to_cpu(dx_root->dr_free_blk);
  3501. while (next_block) {
  3502. brelse(prev_leaf_bh);
  3503. prev_leaf_bh = leaf_bh;
  3504. leaf_bh = NULL;
  3505. ret = ocfs2_read_dir_block_direct(dir, next_block, &leaf_bh);
  3506. if (ret) {
  3507. mlog_errno(ret);
  3508. goto out;
  3509. }
  3510. db = ocfs2_trailer_from_bh(leaf_bh, dir->i_sb);
  3511. if (rec_len <= le16_to_cpu(db->db_free_rec_len)) {
  3512. lookup->dl_leaf_bh = leaf_bh;
  3513. lookup->dl_prev_leaf_bh = prev_leaf_bh;
  3514. leaf_bh = NULL;
  3515. prev_leaf_bh = NULL;
  3516. break;
  3517. }
  3518. next_block = le64_to_cpu(db->db_free_next);
  3519. }
  3520. if (!next_block)
  3521. ret = -ENOSPC;
  3522. out:
  3523. brelse(leaf_bh);
  3524. brelse(prev_leaf_bh);
  3525. return ret;
  3526. }
  3527. static int ocfs2_expand_inline_dx_root(struct inode *dir,
  3528. struct buffer_head *dx_root_bh)
  3529. {
  3530. int ret, num_dx_leaves, i, j, did_quota = 0;
  3531. struct buffer_head **dx_leaves = NULL;
  3532. struct ocfs2_extent_tree et;
  3533. u64 insert_blkno;
  3534. struct ocfs2_alloc_context *data_ac = NULL;
  3535. struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
  3536. handle_t *handle = NULL;
  3537. struct ocfs2_dx_root_block *dx_root;
  3538. struct ocfs2_dx_entry_list *entry_list;
  3539. struct ocfs2_dx_entry *dx_entry;
  3540. struct ocfs2_dx_leaf *target_leaf;
  3541. ret = ocfs2_reserve_clusters(osb, 1, &data_ac);
  3542. if (ret) {
  3543. mlog_errno(ret);
  3544. goto out;
  3545. }
  3546. dx_leaves = ocfs2_dx_dir_kmalloc_leaves(osb->sb, &num_dx_leaves);
  3547. if (!dx_leaves) {
  3548. ret = -ENOMEM;
  3549. mlog_errno(ret);
  3550. goto out;
  3551. }
  3552. handle = ocfs2_start_trans(osb, ocfs2_calc_dxi_expand_credits(osb->sb));
  3553. if (IS_ERR(handle)) {
  3554. ret = PTR_ERR(handle);
  3555. mlog_errno(ret);
  3556. goto out;
  3557. }
  3558. ret = dquot_alloc_space_nodirty(dir,
  3559. ocfs2_clusters_to_bytes(osb->sb, 1));
  3560. if (ret)
  3561. goto out_commit;
  3562. did_quota = 1;
  3563. /*
  3564. * We do this up front, before the allocation, so that a
  3565. * failure to add the dx_root_bh to the journal won't result
  3566. * us losing clusters.
  3567. */
  3568. ret = ocfs2_journal_access_dr(handle, INODE_CACHE(dir), dx_root_bh,
  3569. OCFS2_JOURNAL_ACCESS_WRITE);
  3570. if (ret) {
  3571. mlog_errno(ret);
  3572. goto out_commit;
  3573. }
  3574. ret = __ocfs2_dx_dir_new_cluster(dir, 0, handle, data_ac, dx_leaves,
  3575. num_dx_leaves, &insert_blkno);
  3576. if (ret) {
  3577. mlog_errno(ret);
  3578. goto out_commit;
  3579. }
  3580. /*
  3581. * Transfer the entries from our dx_root into the appropriate
  3582. * block
  3583. */
  3584. dx_root = (struct ocfs2_dx_root_block *) dx_root_bh->b_data;
  3585. entry_list = &dx_root->dr_entries;
  3586. for (i = 0; i < le16_to_cpu(entry_list->de_num_used); i++) {
  3587. dx_entry = &entry_list->de_entries[i];
  3588. j = __ocfs2_dx_dir_hash_idx(osb,
  3589. le32_to_cpu(dx_entry->dx_minor_hash));
  3590. target_leaf = (struct ocfs2_dx_leaf *)dx_leaves[j]->b_data;
  3591. ocfs2_dx_dir_leaf_insert_tail(target_leaf, dx_entry);
  3592. /* Each leaf has been passed to the journal already
  3593. * via __ocfs2_dx_dir_new_cluster() */
  3594. }
  3595. dx_root->dr_flags &= ~OCFS2_DX_FLAG_INLINE;
  3596. memset(&dx_root->dr_list, 0, osb->sb->s_blocksize -
  3597. offsetof(struct ocfs2_dx_root_block, dr_list));
  3598. dx_root->dr_list.l_count =
  3599. cpu_to_le16(ocfs2_extent_recs_per_dx_root(osb->sb));
  3600. /* This should never fail considering we start with an empty
  3601. * dx_root. */
  3602. ocfs2_init_dx_root_extent_tree(&et, INODE_CACHE(dir), dx_root_bh);
  3603. ret = ocfs2_insert_extent(handle, &et, 0, insert_blkno, 1, 0, NULL);
  3604. if (ret)
  3605. mlog_errno(ret);
  3606. did_quota = 0;
  3607. ocfs2_journal_dirty(handle, dx_root_bh);
  3608. out_commit:
  3609. if (ret < 0 && did_quota)
  3610. dquot_free_space_nodirty(dir,
  3611. ocfs2_clusters_to_bytes(dir->i_sb, 1));
  3612. ocfs2_commit_trans(osb, handle);
  3613. out:
  3614. if (data_ac)
  3615. ocfs2_free_alloc_context(data_ac);
  3616. if (dx_leaves) {
  3617. for (i = 0; i < num_dx_leaves; i++)
  3618. brelse(dx_leaves[i]);
  3619. kfree(dx_leaves);
  3620. }
  3621. return ret;
  3622. }
  3623. static int ocfs2_inline_dx_has_space(struct buffer_head *dx_root_bh)
  3624. {
  3625. struct ocfs2_dx_root_block *dx_root;
  3626. struct ocfs2_dx_entry_list *entry_list;
  3627. dx_root = (struct ocfs2_dx_root_block *) dx_root_bh->b_data;
  3628. entry_list = &dx_root->dr_entries;
  3629. if (le16_to_cpu(entry_list->de_num_used) >=
  3630. le16_to_cpu(entry_list->de_count))
  3631. return -ENOSPC;
  3632. return 0;
  3633. }
  3634. static int ocfs2_prepare_dx_dir_for_insert(struct inode *dir,
  3635. struct buffer_head *di_bh,
  3636. const char *name,
  3637. int namelen,
  3638. struct ocfs2_dir_lookup_result *lookup)
  3639. {
  3640. int ret, free_dx_root = 1;
  3641. struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
  3642. struct buffer_head *dx_root_bh = NULL;
  3643. struct buffer_head *leaf_bh = NULL;
  3644. struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
  3645. struct ocfs2_dx_root_block *dx_root;
  3646. ret = ocfs2_read_dx_root(dir, di, &dx_root_bh);
  3647. if (ret) {
  3648. mlog_errno(ret);
  3649. goto out;
  3650. }
  3651. dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
  3652. if (le32_to_cpu(dx_root->dr_num_entries) == OCFS2_DX_ENTRIES_MAX) {
  3653. ret = -ENOSPC;
  3654. mlog_errno(ret);
  3655. goto out;
  3656. }
  3657. if (ocfs2_dx_root_inline(dx_root)) {
  3658. ret = ocfs2_inline_dx_has_space(dx_root_bh);
  3659. if (ret == 0)
  3660. goto search_el;
  3661. /*
  3662. * We ran out of room in the root block. Expand it to
  3663. * an extent, then allow ocfs2_find_dir_space_dx to do
  3664. * the rest.
  3665. */
  3666. ret = ocfs2_expand_inline_dx_root(dir, dx_root_bh);
  3667. if (ret) {
  3668. mlog_errno(ret);
  3669. goto out;
  3670. }
  3671. }
  3672. /*
  3673. * Insert preparation for an indexed directory is split into two
  3674. * steps. The call to find_dir_space_dx reserves room in the index for
  3675. * an additional item. If we run out of space there, it's a real error
  3676. * we can't continue on.
  3677. */
  3678. ret = ocfs2_find_dir_space_dx(osb, dir, di_bh, dx_root_bh, name,
  3679. namelen, lookup);
  3680. if (ret) {
  3681. mlog_errno(ret);
  3682. goto out;
  3683. }
  3684. search_el:
  3685. /*
  3686. * Next, we need to find space in the unindexed tree. This call
  3687. * searches using the free space linked list. If the unindexed tree
  3688. * lacks sufficient space, we'll expand it below. The expansion code
  3689. * is smart enough to add any new blocks to the free space list.
  3690. */
  3691. ret = ocfs2_search_dx_free_list(dir, dx_root_bh, namelen, lookup);
  3692. if (ret && ret != -ENOSPC) {
  3693. mlog_errno(ret);
  3694. goto out;
  3695. }
  3696. /* Do this up here - ocfs2_extend_dir might need the dx_root */
  3697. lookup->dl_dx_root_bh = dx_root_bh;
  3698. free_dx_root = 0;
  3699. if (ret == -ENOSPC) {
  3700. ret = ocfs2_extend_dir(osb, dir, di_bh, 1, lookup, &leaf_bh);
  3701. if (ret) {
  3702. mlog_errno(ret);
  3703. goto out;
  3704. }
  3705. /*
  3706. * We make the assumption here that new leaf blocks are added
  3707. * to the front of our free list.
  3708. */
  3709. lookup->dl_prev_leaf_bh = NULL;
  3710. lookup->dl_leaf_bh = leaf_bh;
  3711. }
  3712. out:
  3713. if (free_dx_root)
  3714. brelse(dx_root_bh);
  3715. return ret;
  3716. }
  3717. /*
  3718. * Get a directory ready for insert. Any directory allocation required
  3719. * happens here. Success returns zero, and enough context in the dir
  3720. * lookup result that ocfs2_add_entry() will be able complete the task
  3721. * with minimal performance impact.
  3722. */
  3723. int ocfs2_prepare_dir_for_insert(struct ocfs2_super *osb,
  3724. struct inode *dir,
  3725. struct buffer_head *parent_fe_bh,
  3726. const char *name,
  3727. int namelen,
  3728. struct ocfs2_dir_lookup_result *lookup)
  3729. {
  3730. int ret;
  3731. unsigned int blocks_wanted = 1;
  3732. struct buffer_head *bh = NULL;
  3733. mlog(0, "getting ready to insert namelen %d into dir %llu\n",
  3734. namelen, (unsigned long long)OCFS2_I(dir)->ip_blkno);
  3735. if (!namelen) {
  3736. ret = -EINVAL;
  3737. mlog_errno(ret);
  3738. goto out;
  3739. }
  3740. /*
  3741. * Do this up front to reduce confusion.
  3742. *
  3743. * The directory might start inline, then be turned into an
  3744. * indexed one, in which case we'd need to hash deep inside
  3745. * ocfs2_find_dir_space_id(). Since
  3746. * ocfs2_prepare_dx_dir_for_insert() also needs this hash
  3747. * done, there seems no point in spreading out the calls. We
  3748. * can optimize away the case where the file system doesn't
  3749. * support indexing.
  3750. */
  3751. if (ocfs2_supports_indexed_dirs(osb))
  3752. ocfs2_dx_dir_name_hash(dir, name, namelen, &lookup->dl_hinfo);
  3753. if (ocfs2_dir_indexed(dir)) {
  3754. ret = ocfs2_prepare_dx_dir_for_insert(dir, parent_fe_bh,
  3755. name, namelen, lookup);
  3756. if (ret)
  3757. mlog_errno(ret);
  3758. goto out;
  3759. }
  3760. if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
  3761. ret = ocfs2_find_dir_space_id(dir, parent_fe_bh, name,
  3762. namelen, &bh, &blocks_wanted);
  3763. } else
  3764. ret = ocfs2_find_dir_space_el(dir, name, namelen, &bh);
  3765. if (ret && ret != -ENOSPC) {
  3766. mlog_errno(ret);
  3767. goto out;
  3768. }
  3769. if (ret == -ENOSPC) {
  3770. /*
  3771. * We have to expand the directory to add this name.
  3772. */
  3773. BUG_ON(bh);
  3774. ret = ocfs2_extend_dir(osb, dir, parent_fe_bh, blocks_wanted,
  3775. lookup, &bh);
  3776. if (ret) {
  3777. if (ret != -ENOSPC)
  3778. mlog_errno(ret);
  3779. goto out;
  3780. }
  3781. BUG_ON(!bh);
  3782. }
  3783. lookup->dl_leaf_bh = bh;
  3784. bh = NULL;
  3785. out:
  3786. brelse(bh);
  3787. return ret;
  3788. }
  3789. static int ocfs2_dx_dir_remove_index(struct inode *dir,
  3790. struct buffer_head *di_bh,
  3791. struct buffer_head *dx_root_bh)
  3792. {
  3793. int ret;
  3794. struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
  3795. struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
  3796. struct ocfs2_dx_root_block *dx_root;
  3797. struct inode *dx_alloc_inode = NULL;
  3798. struct buffer_head *dx_alloc_bh = NULL;
  3799. handle_t *handle;
  3800. u64 blk;
  3801. u16 bit;
  3802. u64 bg_blkno;
  3803. dx_root = (struct ocfs2_dx_root_block *) dx_root_bh->b_data;
  3804. dx_alloc_inode = ocfs2_get_system_file_inode(osb,
  3805. EXTENT_ALLOC_SYSTEM_INODE,
  3806. le16_to_cpu(dx_root->dr_suballoc_slot));
  3807. if (!dx_alloc_inode) {
  3808. ret = -ENOMEM;
  3809. mlog_errno(ret);
  3810. goto out;
  3811. }
  3812. mutex_lock(&dx_alloc_inode->i_mutex);
  3813. ret = ocfs2_inode_lock(dx_alloc_inode, &dx_alloc_bh, 1);
  3814. if (ret) {
  3815. mlog_errno(ret);
  3816. goto out_mutex;
  3817. }
  3818. handle = ocfs2_start_trans(osb, OCFS2_DX_ROOT_REMOVE_CREDITS);
  3819. if (IS_ERR(handle)) {
  3820. ret = PTR_ERR(handle);
  3821. mlog_errno(ret);
  3822. goto out_unlock;
  3823. }
  3824. ret = ocfs2_journal_access_di(handle, INODE_CACHE(dir), di_bh,
  3825. OCFS2_JOURNAL_ACCESS_WRITE);
  3826. if (ret) {
  3827. mlog_errno(ret);
  3828. goto out_commit;
  3829. }
  3830. OCFS2_I(dir)->ip_dyn_features &= ~OCFS2_INDEXED_DIR_FL;
  3831. di->i_dyn_features = cpu_to_le16(OCFS2_I(dir)->ip_dyn_features);
  3832. di->i_dx_root = cpu_to_le64(0ULL);
  3833. ocfs2_journal_dirty(handle, di_bh);
  3834. blk = le64_to_cpu(dx_root->dr_blkno);
  3835. bit = le16_to_cpu(dx_root->dr_suballoc_bit);
  3836. if (dx_root->dr_suballoc_loc)
  3837. bg_blkno = le64_to_cpu(dx_root->dr_suballoc_loc);
  3838. else
  3839. bg_blkno = ocfs2_which_suballoc_group(blk, bit);
  3840. ret = ocfs2_free_suballoc_bits(handle, dx_alloc_inode, dx_alloc_bh,
  3841. bit, bg_blkno, 1);
  3842. if (ret)
  3843. mlog_errno(ret);
  3844. out_commit:
  3845. ocfs2_commit_trans(osb, handle);
  3846. out_unlock:
  3847. ocfs2_inode_unlock(dx_alloc_inode, 1);
  3848. out_mutex:
  3849. mutex_unlock(&dx_alloc_inode->i_mutex);
  3850. brelse(dx_alloc_bh);
  3851. out:
  3852. iput(dx_alloc_inode);
  3853. return ret;
  3854. }
  3855. int ocfs2_dx_dir_truncate(struct inode *dir, struct buffer_head *di_bh)
  3856. {
  3857. int ret;
  3858. unsigned int uninitialized_var(clen);
  3859. u32 major_hash = UINT_MAX, p_cpos, uninitialized_var(cpos);
  3860. u64 uninitialized_var(blkno);
  3861. struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
  3862. struct buffer_head *dx_root_bh = NULL;
  3863. struct ocfs2_dx_root_block *dx_root;
  3864. struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
  3865. struct ocfs2_cached_dealloc_ctxt dealloc;
  3866. struct ocfs2_extent_tree et;
  3867. ocfs2_init_dealloc_ctxt(&dealloc);
  3868. if (!ocfs2_dir_indexed(dir))
  3869. return 0;
  3870. ret = ocfs2_read_dx_root(dir, di, &dx_root_bh);
  3871. if (ret) {
  3872. mlog_errno(ret);
  3873. goto out;
  3874. }
  3875. dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
  3876. if (ocfs2_dx_root_inline(dx_root))
  3877. goto remove_index;
  3878. ocfs2_init_dx_root_extent_tree(&et, INODE_CACHE(dir), dx_root_bh);
  3879. /* XXX: What if dr_clusters is too large? */
  3880. while (le32_to_cpu(dx_root->dr_clusters)) {
  3881. ret = ocfs2_dx_dir_lookup_rec(dir, &dx_root->dr_list,
  3882. major_hash, &cpos, &blkno, &clen);
  3883. if (ret) {
  3884. mlog_errno(ret);
  3885. goto out;
  3886. }
  3887. p_cpos = ocfs2_blocks_to_clusters(dir->i_sb, blkno);
  3888. ret = ocfs2_remove_btree_range(dir, &et, cpos, p_cpos, clen, 0,
  3889. &dealloc, 0);
  3890. if (ret) {
  3891. mlog_errno(ret);
  3892. goto out;
  3893. }
  3894. if (cpos == 0)
  3895. break;
  3896. major_hash = cpos - 1;
  3897. }
  3898. remove_index:
  3899. ret = ocfs2_dx_dir_remove_index(dir, di_bh, dx_root_bh);
  3900. if (ret) {
  3901. mlog_errno(ret);
  3902. goto out;
  3903. }
  3904. ocfs2_remove_from_cache(INODE_CACHE(dir), dx_root_bh);
  3905. out:
  3906. ocfs2_schedule_truncate_log_flush(osb, 1);
  3907. ocfs2_run_deallocs(osb, &dealloc);
  3908. brelse(dx_root_bh);
  3909. return ret;
  3910. }