dir.c 116 KB

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