dir.c 116 KB

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