dir.c 114 KB

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