dir.c 116 KB

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