inode.c 164 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158
  1. /*
  2. * Copyright (C) 2007 Oracle. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public
  6. * License v2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public
  14. * License along with this program; if not, write to the
  15. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  16. * Boston, MA 021110-1307, USA.
  17. */
  18. #include <linux/kernel.h>
  19. #include <linux/bio.h>
  20. #include <linux/buffer_head.h>
  21. #include <linux/file.h>
  22. #include <linux/fs.h>
  23. #include <linux/pagemap.h>
  24. #include <linux/highmem.h>
  25. #include <linux/time.h>
  26. #include <linux/init.h>
  27. #include <linux/string.h>
  28. #include <linux/backing-dev.h>
  29. #include <linux/mpage.h>
  30. #include <linux/swap.h>
  31. #include <linux/writeback.h>
  32. #include <linux/statfs.h>
  33. #include <linux/compat.h>
  34. #include <linux/bit_spinlock.h>
  35. #include <linux/xattr.h>
  36. #include <linux/posix_acl.h>
  37. #include <linux/falloc.h>
  38. #include <linux/slab.h>
  39. #include "compat.h"
  40. #include "ctree.h"
  41. #include "disk-io.h"
  42. #include "transaction.h"
  43. #include "btrfs_inode.h"
  44. #include "ioctl.h"
  45. #include "print-tree.h"
  46. #include "volumes.h"
  47. #include "ordered-data.h"
  48. #include "xattr.h"
  49. #include "tree-log.h"
  50. #include "compression.h"
  51. #include "locking.h"
  52. struct btrfs_iget_args {
  53. u64 ino;
  54. struct btrfs_root *root;
  55. };
  56. static const struct inode_operations btrfs_dir_inode_operations;
  57. static const struct inode_operations btrfs_symlink_inode_operations;
  58. static const struct inode_operations btrfs_dir_ro_inode_operations;
  59. static const struct inode_operations btrfs_special_inode_operations;
  60. static const struct inode_operations btrfs_file_inode_operations;
  61. static const struct address_space_operations btrfs_aops;
  62. static const struct address_space_operations btrfs_symlink_aops;
  63. static const struct file_operations btrfs_dir_file_operations;
  64. static struct extent_io_ops btrfs_extent_io_ops;
  65. static struct kmem_cache *btrfs_inode_cachep;
  66. struct kmem_cache *btrfs_trans_handle_cachep;
  67. struct kmem_cache *btrfs_transaction_cachep;
  68. struct kmem_cache *btrfs_path_cachep;
  69. #define S_SHIFT 12
  70. static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
  71. [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
  72. [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
  73. [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
  74. [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
  75. [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
  76. [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
  77. [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
  78. };
  79. static void btrfs_truncate(struct inode *inode);
  80. static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end);
  81. static noinline int cow_file_range(struct inode *inode,
  82. struct page *locked_page,
  83. u64 start, u64 end, int *page_started,
  84. unsigned long *nr_written, int unlock);
  85. static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
  86. struct inode *inode, struct inode *dir)
  87. {
  88. int err;
  89. err = btrfs_init_acl(trans, inode, dir);
  90. if (!err)
  91. err = btrfs_xattr_security_init(trans, inode, dir);
  92. return err;
  93. }
  94. /*
  95. * this does all the hard work for inserting an inline extent into
  96. * the btree. The caller should have done a btrfs_drop_extents so that
  97. * no overlapping inline items exist in the btree
  98. */
  99. static noinline int insert_inline_extent(struct btrfs_trans_handle *trans,
  100. struct btrfs_root *root, struct inode *inode,
  101. u64 start, size_t size, size_t compressed_size,
  102. struct page **compressed_pages)
  103. {
  104. struct btrfs_key key;
  105. struct btrfs_path *path;
  106. struct extent_buffer *leaf;
  107. struct page *page = NULL;
  108. char *kaddr;
  109. unsigned long ptr;
  110. struct btrfs_file_extent_item *ei;
  111. int err = 0;
  112. int ret;
  113. size_t cur_size = size;
  114. size_t datasize;
  115. unsigned long offset;
  116. int use_compress = 0;
  117. if (compressed_size && compressed_pages) {
  118. use_compress = 1;
  119. cur_size = compressed_size;
  120. }
  121. path = btrfs_alloc_path();
  122. if (!path)
  123. return -ENOMEM;
  124. path->leave_spinning = 1;
  125. btrfs_set_trans_block_group(trans, inode);
  126. key.objectid = inode->i_ino;
  127. key.offset = start;
  128. btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
  129. datasize = btrfs_file_extent_calc_inline_size(cur_size);
  130. inode_add_bytes(inode, size);
  131. ret = btrfs_insert_empty_item(trans, root, path, &key,
  132. datasize);
  133. BUG_ON(ret);
  134. if (ret) {
  135. err = ret;
  136. goto fail;
  137. }
  138. leaf = path->nodes[0];
  139. ei = btrfs_item_ptr(leaf, path->slots[0],
  140. struct btrfs_file_extent_item);
  141. btrfs_set_file_extent_generation(leaf, ei, trans->transid);
  142. btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
  143. btrfs_set_file_extent_encryption(leaf, ei, 0);
  144. btrfs_set_file_extent_other_encoding(leaf, ei, 0);
  145. btrfs_set_file_extent_ram_bytes(leaf, ei, size);
  146. ptr = btrfs_file_extent_inline_start(ei);
  147. if (use_compress) {
  148. struct page *cpage;
  149. int i = 0;
  150. while (compressed_size > 0) {
  151. cpage = compressed_pages[i];
  152. cur_size = min_t(unsigned long, compressed_size,
  153. PAGE_CACHE_SIZE);
  154. kaddr = kmap_atomic(cpage, KM_USER0);
  155. write_extent_buffer(leaf, kaddr, ptr, cur_size);
  156. kunmap_atomic(kaddr, KM_USER0);
  157. i++;
  158. ptr += cur_size;
  159. compressed_size -= cur_size;
  160. }
  161. btrfs_set_file_extent_compression(leaf, ei,
  162. BTRFS_COMPRESS_ZLIB);
  163. } else {
  164. page = find_get_page(inode->i_mapping,
  165. start >> PAGE_CACHE_SHIFT);
  166. btrfs_set_file_extent_compression(leaf, ei, 0);
  167. kaddr = kmap_atomic(page, KM_USER0);
  168. offset = start & (PAGE_CACHE_SIZE - 1);
  169. write_extent_buffer(leaf, kaddr + offset, ptr, size);
  170. kunmap_atomic(kaddr, KM_USER0);
  171. page_cache_release(page);
  172. }
  173. btrfs_mark_buffer_dirty(leaf);
  174. btrfs_free_path(path);
  175. /*
  176. * we're an inline extent, so nobody can
  177. * extend the file past i_size without locking
  178. * a page we already have locked.
  179. *
  180. * We must do any isize and inode updates
  181. * before we unlock the pages. Otherwise we
  182. * could end up racing with unlink.
  183. */
  184. BTRFS_I(inode)->disk_i_size = inode->i_size;
  185. btrfs_update_inode(trans, root, inode);
  186. return 0;
  187. fail:
  188. btrfs_free_path(path);
  189. return err;
  190. }
  191. /*
  192. * conditionally insert an inline extent into the file. This
  193. * does the checks required to make sure the data is small enough
  194. * to fit as an inline extent.
  195. */
  196. static noinline int cow_file_range_inline(struct btrfs_trans_handle *trans,
  197. struct btrfs_root *root,
  198. struct inode *inode, u64 start, u64 end,
  199. size_t compressed_size,
  200. struct page **compressed_pages)
  201. {
  202. u64 isize = i_size_read(inode);
  203. u64 actual_end = min(end + 1, isize);
  204. u64 inline_len = actual_end - start;
  205. u64 aligned_end = (end + root->sectorsize - 1) &
  206. ~((u64)root->sectorsize - 1);
  207. u64 hint_byte;
  208. u64 data_len = inline_len;
  209. int ret;
  210. if (compressed_size)
  211. data_len = compressed_size;
  212. if (start > 0 ||
  213. actual_end >= PAGE_CACHE_SIZE ||
  214. data_len >= BTRFS_MAX_INLINE_DATA_SIZE(root) ||
  215. (!compressed_size &&
  216. (actual_end & (root->sectorsize - 1)) == 0) ||
  217. end + 1 < isize ||
  218. data_len > root->fs_info->max_inline) {
  219. return 1;
  220. }
  221. ret = btrfs_drop_extents(trans, inode, start, aligned_end,
  222. &hint_byte, 1);
  223. BUG_ON(ret);
  224. if (isize > actual_end)
  225. inline_len = min_t(u64, isize, actual_end);
  226. ret = insert_inline_extent(trans, root, inode, start,
  227. inline_len, compressed_size,
  228. compressed_pages);
  229. BUG_ON(ret);
  230. btrfs_delalloc_release_metadata(inode, end + 1 - start);
  231. btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0);
  232. return 0;
  233. }
  234. struct async_extent {
  235. u64 start;
  236. u64 ram_size;
  237. u64 compressed_size;
  238. struct page **pages;
  239. unsigned long nr_pages;
  240. struct list_head list;
  241. };
  242. struct async_cow {
  243. struct inode *inode;
  244. struct btrfs_root *root;
  245. struct page *locked_page;
  246. u64 start;
  247. u64 end;
  248. struct list_head extents;
  249. struct btrfs_work work;
  250. };
  251. static noinline int add_async_extent(struct async_cow *cow,
  252. u64 start, u64 ram_size,
  253. u64 compressed_size,
  254. struct page **pages,
  255. unsigned long nr_pages)
  256. {
  257. struct async_extent *async_extent;
  258. async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
  259. async_extent->start = start;
  260. async_extent->ram_size = ram_size;
  261. async_extent->compressed_size = compressed_size;
  262. async_extent->pages = pages;
  263. async_extent->nr_pages = nr_pages;
  264. list_add_tail(&async_extent->list, &cow->extents);
  265. return 0;
  266. }
  267. /*
  268. * we create compressed extents in two phases. The first
  269. * phase compresses a range of pages that have already been
  270. * locked (both pages and state bits are locked).
  271. *
  272. * This is done inside an ordered work queue, and the compression
  273. * is spread across many cpus. The actual IO submission is step
  274. * two, and the ordered work queue takes care of making sure that
  275. * happens in the same order things were put onto the queue by
  276. * writepages and friends.
  277. *
  278. * If this code finds it can't get good compression, it puts an
  279. * entry onto the work queue to write the uncompressed bytes. This
  280. * makes sure that both compressed inodes and uncompressed inodes
  281. * are written in the same order that pdflush sent them down.
  282. */
  283. static noinline int compress_file_range(struct inode *inode,
  284. struct page *locked_page,
  285. u64 start, u64 end,
  286. struct async_cow *async_cow,
  287. int *num_added)
  288. {
  289. struct btrfs_root *root = BTRFS_I(inode)->root;
  290. struct btrfs_trans_handle *trans;
  291. u64 num_bytes;
  292. u64 orig_start;
  293. u64 disk_num_bytes;
  294. u64 blocksize = root->sectorsize;
  295. u64 actual_end;
  296. u64 isize = i_size_read(inode);
  297. int ret = 0;
  298. struct page **pages = NULL;
  299. unsigned long nr_pages;
  300. unsigned long nr_pages_ret = 0;
  301. unsigned long total_compressed = 0;
  302. unsigned long total_in = 0;
  303. unsigned long max_compressed = 128 * 1024;
  304. unsigned long max_uncompressed = 128 * 1024;
  305. int i;
  306. int will_compress;
  307. orig_start = start;
  308. actual_end = min_t(u64, isize, end + 1);
  309. again:
  310. will_compress = 0;
  311. nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1;
  312. nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE);
  313. /*
  314. * we don't want to send crud past the end of i_size through
  315. * compression, that's just a waste of CPU time. So, if the
  316. * end of the file is before the start of our current
  317. * requested range of bytes, we bail out to the uncompressed
  318. * cleanup code that can deal with all of this.
  319. *
  320. * It isn't really the fastest way to fix things, but this is a
  321. * very uncommon corner.
  322. */
  323. if (actual_end <= start)
  324. goto cleanup_and_bail_uncompressed;
  325. total_compressed = actual_end - start;
  326. /* we want to make sure that amount of ram required to uncompress
  327. * an extent is reasonable, so we limit the total size in ram
  328. * of a compressed extent to 128k. This is a crucial number
  329. * because it also controls how easily we can spread reads across
  330. * cpus for decompression.
  331. *
  332. * We also want to make sure the amount of IO required to do
  333. * a random read is reasonably small, so we limit the size of
  334. * a compressed extent to 128k.
  335. */
  336. total_compressed = min(total_compressed, max_uncompressed);
  337. num_bytes = (end - start + blocksize) & ~(blocksize - 1);
  338. num_bytes = max(blocksize, num_bytes);
  339. disk_num_bytes = num_bytes;
  340. total_in = 0;
  341. ret = 0;
  342. /*
  343. * we do compression for mount -o compress and when the
  344. * inode has not been flagged as nocompress. This flag can
  345. * change at any time if we discover bad compression ratios.
  346. */
  347. if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) &&
  348. (btrfs_test_opt(root, COMPRESS) ||
  349. (BTRFS_I(inode)->force_compress))) {
  350. WARN_ON(pages);
  351. pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS);
  352. ret = btrfs_zlib_compress_pages(inode->i_mapping, start,
  353. total_compressed, pages,
  354. nr_pages, &nr_pages_ret,
  355. &total_in,
  356. &total_compressed,
  357. max_compressed);
  358. if (!ret) {
  359. unsigned long offset = total_compressed &
  360. (PAGE_CACHE_SIZE - 1);
  361. struct page *page = pages[nr_pages_ret - 1];
  362. char *kaddr;
  363. /* zero the tail end of the last page, we might be
  364. * sending it down to disk
  365. */
  366. if (offset) {
  367. kaddr = kmap_atomic(page, KM_USER0);
  368. memset(kaddr + offset, 0,
  369. PAGE_CACHE_SIZE - offset);
  370. kunmap_atomic(kaddr, KM_USER0);
  371. }
  372. will_compress = 1;
  373. }
  374. }
  375. if (start == 0) {
  376. trans = btrfs_join_transaction(root, 1);
  377. BUG_ON(!trans);
  378. btrfs_set_trans_block_group(trans, inode);
  379. trans->block_rsv = &root->fs_info->delalloc_block_rsv;
  380. /* lets try to make an inline extent */
  381. if (ret || total_in < (actual_end - start)) {
  382. /* we didn't compress the entire range, try
  383. * to make an uncompressed inline extent.
  384. */
  385. ret = cow_file_range_inline(trans, root, inode,
  386. start, end, 0, NULL);
  387. } else {
  388. /* try making a compressed inline extent */
  389. ret = cow_file_range_inline(trans, root, inode,
  390. start, end,
  391. total_compressed, pages);
  392. }
  393. if (ret == 0) {
  394. /*
  395. * inline extent creation worked, we don't need
  396. * to create any more async work items. Unlock
  397. * and free up our temp pages.
  398. */
  399. extent_clear_unlock_delalloc(inode,
  400. &BTRFS_I(inode)->io_tree,
  401. start, end, NULL,
  402. EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY |
  403. EXTENT_CLEAR_DELALLOC |
  404. EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK);
  405. btrfs_end_transaction(trans, root);
  406. goto free_pages_out;
  407. }
  408. btrfs_end_transaction(trans, root);
  409. }
  410. if (will_compress) {
  411. /*
  412. * we aren't doing an inline extent round the compressed size
  413. * up to a block size boundary so the allocator does sane
  414. * things
  415. */
  416. total_compressed = (total_compressed + blocksize - 1) &
  417. ~(blocksize - 1);
  418. /*
  419. * one last check to make sure the compression is really a
  420. * win, compare the page count read with the blocks on disk
  421. */
  422. total_in = (total_in + PAGE_CACHE_SIZE - 1) &
  423. ~(PAGE_CACHE_SIZE - 1);
  424. if (total_compressed >= total_in) {
  425. will_compress = 0;
  426. } else {
  427. disk_num_bytes = total_compressed;
  428. num_bytes = total_in;
  429. }
  430. }
  431. if (!will_compress && pages) {
  432. /*
  433. * the compression code ran but failed to make things smaller,
  434. * free any pages it allocated and our page pointer array
  435. */
  436. for (i = 0; i < nr_pages_ret; i++) {
  437. WARN_ON(pages[i]->mapping);
  438. page_cache_release(pages[i]);
  439. }
  440. kfree(pages);
  441. pages = NULL;
  442. total_compressed = 0;
  443. nr_pages_ret = 0;
  444. /* flag the file so we don't compress in the future */
  445. if (!btrfs_test_opt(root, FORCE_COMPRESS) &&
  446. !(BTRFS_I(inode)->force_compress)) {
  447. BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
  448. }
  449. }
  450. if (will_compress) {
  451. *num_added += 1;
  452. /* the async work queues will take care of doing actual
  453. * allocation on disk for these compressed pages,
  454. * and will submit them to the elevator.
  455. */
  456. add_async_extent(async_cow, start, num_bytes,
  457. total_compressed, pages, nr_pages_ret);
  458. if (start + num_bytes < end && start + num_bytes < actual_end) {
  459. start += num_bytes;
  460. pages = NULL;
  461. cond_resched();
  462. goto again;
  463. }
  464. } else {
  465. cleanup_and_bail_uncompressed:
  466. /*
  467. * No compression, but we still need to write the pages in
  468. * the file we've been given so far. redirty the locked
  469. * page if it corresponds to our extent and set things up
  470. * for the async work queue to run cow_file_range to do
  471. * the normal delalloc dance
  472. */
  473. if (page_offset(locked_page) >= start &&
  474. page_offset(locked_page) <= end) {
  475. __set_page_dirty_nobuffers(locked_page);
  476. /* unlocked later on in the async handlers */
  477. }
  478. add_async_extent(async_cow, start, end - start + 1, 0, NULL, 0);
  479. *num_added += 1;
  480. }
  481. out:
  482. return 0;
  483. free_pages_out:
  484. for (i = 0; i < nr_pages_ret; i++) {
  485. WARN_ON(pages[i]->mapping);
  486. page_cache_release(pages[i]);
  487. }
  488. kfree(pages);
  489. goto out;
  490. }
  491. /*
  492. * phase two of compressed writeback. This is the ordered portion
  493. * of the code, which only gets called in the order the work was
  494. * queued. We walk all the async extents created by compress_file_range
  495. * and send them down to the disk.
  496. */
  497. static noinline int submit_compressed_extents(struct inode *inode,
  498. struct async_cow *async_cow)
  499. {
  500. struct async_extent *async_extent;
  501. u64 alloc_hint = 0;
  502. struct btrfs_trans_handle *trans;
  503. struct btrfs_key ins;
  504. struct extent_map *em;
  505. struct btrfs_root *root = BTRFS_I(inode)->root;
  506. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  507. struct extent_io_tree *io_tree;
  508. int ret = 0;
  509. if (list_empty(&async_cow->extents))
  510. return 0;
  511. while (!list_empty(&async_cow->extents)) {
  512. async_extent = list_entry(async_cow->extents.next,
  513. struct async_extent, list);
  514. list_del(&async_extent->list);
  515. io_tree = &BTRFS_I(inode)->io_tree;
  516. retry:
  517. /* did the compression code fall back to uncompressed IO? */
  518. if (!async_extent->pages) {
  519. int page_started = 0;
  520. unsigned long nr_written = 0;
  521. lock_extent(io_tree, async_extent->start,
  522. async_extent->start +
  523. async_extent->ram_size - 1, GFP_NOFS);
  524. /* allocate blocks */
  525. ret = cow_file_range(inode, async_cow->locked_page,
  526. async_extent->start,
  527. async_extent->start +
  528. async_extent->ram_size - 1,
  529. &page_started, &nr_written, 0);
  530. /*
  531. * if page_started, cow_file_range inserted an
  532. * inline extent and took care of all the unlocking
  533. * and IO for us. Otherwise, we need to submit
  534. * all those pages down to the drive.
  535. */
  536. if (!page_started && !ret)
  537. extent_write_locked_range(io_tree,
  538. inode, async_extent->start,
  539. async_extent->start +
  540. async_extent->ram_size - 1,
  541. btrfs_get_extent,
  542. WB_SYNC_ALL);
  543. kfree(async_extent);
  544. cond_resched();
  545. continue;
  546. }
  547. lock_extent(io_tree, async_extent->start,
  548. async_extent->start + async_extent->ram_size - 1,
  549. GFP_NOFS);
  550. trans = btrfs_join_transaction(root, 1);
  551. ret = btrfs_reserve_extent(trans, root,
  552. async_extent->compressed_size,
  553. async_extent->compressed_size,
  554. 0, alloc_hint,
  555. (u64)-1, &ins, 1);
  556. btrfs_end_transaction(trans, root);
  557. if (ret) {
  558. int i;
  559. for (i = 0; i < async_extent->nr_pages; i++) {
  560. WARN_ON(async_extent->pages[i]->mapping);
  561. page_cache_release(async_extent->pages[i]);
  562. }
  563. kfree(async_extent->pages);
  564. async_extent->nr_pages = 0;
  565. async_extent->pages = NULL;
  566. unlock_extent(io_tree, async_extent->start,
  567. async_extent->start +
  568. async_extent->ram_size - 1, GFP_NOFS);
  569. goto retry;
  570. }
  571. /*
  572. * here we're doing allocation and writeback of the
  573. * compressed pages
  574. */
  575. btrfs_drop_extent_cache(inode, async_extent->start,
  576. async_extent->start +
  577. async_extent->ram_size - 1, 0);
  578. em = alloc_extent_map(GFP_NOFS);
  579. em->start = async_extent->start;
  580. em->len = async_extent->ram_size;
  581. em->orig_start = em->start;
  582. em->block_start = ins.objectid;
  583. em->block_len = ins.offset;
  584. em->bdev = root->fs_info->fs_devices->latest_bdev;
  585. set_bit(EXTENT_FLAG_PINNED, &em->flags);
  586. set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
  587. while (1) {
  588. write_lock(&em_tree->lock);
  589. ret = add_extent_mapping(em_tree, em);
  590. write_unlock(&em_tree->lock);
  591. if (ret != -EEXIST) {
  592. free_extent_map(em);
  593. break;
  594. }
  595. btrfs_drop_extent_cache(inode, async_extent->start,
  596. async_extent->start +
  597. async_extent->ram_size - 1, 0);
  598. }
  599. ret = btrfs_add_ordered_extent(inode, async_extent->start,
  600. ins.objectid,
  601. async_extent->ram_size,
  602. ins.offset,
  603. BTRFS_ORDERED_COMPRESSED);
  604. BUG_ON(ret);
  605. /*
  606. * clear dirty, set writeback and unlock the pages.
  607. */
  608. extent_clear_unlock_delalloc(inode,
  609. &BTRFS_I(inode)->io_tree,
  610. async_extent->start,
  611. async_extent->start +
  612. async_extent->ram_size - 1,
  613. NULL, EXTENT_CLEAR_UNLOCK_PAGE |
  614. EXTENT_CLEAR_UNLOCK |
  615. EXTENT_CLEAR_DELALLOC |
  616. EXTENT_CLEAR_DIRTY | EXTENT_SET_WRITEBACK);
  617. ret = btrfs_submit_compressed_write(inode,
  618. async_extent->start,
  619. async_extent->ram_size,
  620. ins.objectid,
  621. ins.offset, async_extent->pages,
  622. async_extent->nr_pages);
  623. BUG_ON(ret);
  624. alloc_hint = ins.objectid + ins.offset;
  625. kfree(async_extent);
  626. cond_resched();
  627. }
  628. return 0;
  629. }
  630. /*
  631. * when extent_io.c finds a delayed allocation range in the file,
  632. * the call backs end up in this code. The basic idea is to
  633. * allocate extents on disk for the range, and create ordered data structs
  634. * in ram to track those extents.
  635. *
  636. * locked_page is the page that writepage had locked already. We use
  637. * it to make sure we don't do extra locks or unlocks.
  638. *
  639. * *page_started is set to one if we unlock locked_page and do everything
  640. * required to start IO on it. It may be clean and already done with
  641. * IO when we return.
  642. */
  643. static noinline int cow_file_range(struct inode *inode,
  644. struct page *locked_page,
  645. u64 start, u64 end, int *page_started,
  646. unsigned long *nr_written,
  647. int unlock)
  648. {
  649. struct btrfs_root *root = BTRFS_I(inode)->root;
  650. struct btrfs_trans_handle *trans;
  651. u64 alloc_hint = 0;
  652. u64 num_bytes;
  653. unsigned long ram_size;
  654. u64 disk_num_bytes;
  655. u64 cur_alloc_size;
  656. u64 blocksize = root->sectorsize;
  657. u64 actual_end;
  658. u64 isize = i_size_read(inode);
  659. struct btrfs_key ins;
  660. struct extent_map *em;
  661. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  662. int ret = 0;
  663. trans = btrfs_join_transaction(root, 1);
  664. BUG_ON(!trans);
  665. btrfs_set_trans_block_group(trans, inode);
  666. trans->block_rsv = &root->fs_info->delalloc_block_rsv;
  667. actual_end = min_t(u64, isize, end + 1);
  668. num_bytes = (end - start + blocksize) & ~(blocksize - 1);
  669. num_bytes = max(blocksize, num_bytes);
  670. disk_num_bytes = num_bytes;
  671. ret = 0;
  672. if (start == 0) {
  673. /* lets try to make an inline extent */
  674. ret = cow_file_range_inline(trans, root, inode,
  675. start, end, 0, NULL);
  676. if (ret == 0) {
  677. extent_clear_unlock_delalloc(inode,
  678. &BTRFS_I(inode)->io_tree,
  679. start, end, NULL,
  680. EXTENT_CLEAR_UNLOCK_PAGE |
  681. EXTENT_CLEAR_UNLOCK |
  682. EXTENT_CLEAR_DELALLOC |
  683. EXTENT_CLEAR_DIRTY |
  684. EXTENT_SET_WRITEBACK |
  685. EXTENT_END_WRITEBACK);
  686. *nr_written = *nr_written +
  687. (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE;
  688. *page_started = 1;
  689. ret = 0;
  690. goto out;
  691. }
  692. }
  693. BUG_ON(disk_num_bytes >
  694. btrfs_super_total_bytes(&root->fs_info->super_copy));
  695. read_lock(&BTRFS_I(inode)->extent_tree.lock);
  696. em = search_extent_mapping(&BTRFS_I(inode)->extent_tree,
  697. start, num_bytes);
  698. if (em) {
  699. /*
  700. * if block start isn't an actual block number then find the
  701. * first block in this inode and use that as a hint. If that
  702. * block is also bogus then just don't worry about it.
  703. */
  704. if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
  705. free_extent_map(em);
  706. em = search_extent_mapping(em_tree, 0, 0);
  707. if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
  708. alloc_hint = em->block_start;
  709. if (em)
  710. free_extent_map(em);
  711. } else {
  712. alloc_hint = em->block_start;
  713. free_extent_map(em);
  714. }
  715. }
  716. read_unlock(&BTRFS_I(inode)->extent_tree.lock);
  717. btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
  718. while (disk_num_bytes > 0) {
  719. unsigned long op;
  720. cur_alloc_size = disk_num_bytes;
  721. ret = btrfs_reserve_extent(trans, root, cur_alloc_size,
  722. root->sectorsize, 0, alloc_hint,
  723. (u64)-1, &ins, 1);
  724. BUG_ON(ret);
  725. em = alloc_extent_map(GFP_NOFS);
  726. em->start = start;
  727. em->orig_start = em->start;
  728. ram_size = ins.offset;
  729. em->len = ins.offset;
  730. em->block_start = ins.objectid;
  731. em->block_len = ins.offset;
  732. em->bdev = root->fs_info->fs_devices->latest_bdev;
  733. set_bit(EXTENT_FLAG_PINNED, &em->flags);
  734. while (1) {
  735. write_lock(&em_tree->lock);
  736. ret = add_extent_mapping(em_tree, em);
  737. write_unlock(&em_tree->lock);
  738. if (ret != -EEXIST) {
  739. free_extent_map(em);
  740. break;
  741. }
  742. btrfs_drop_extent_cache(inode, start,
  743. start + ram_size - 1, 0);
  744. }
  745. cur_alloc_size = ins.offset;
  746. ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
  747. ram_size, cur_alloc_size, 0);
  748. BUG_ON(ret);
  749. if (root->root_key.objectid ==
  750. BTRFS_DATA_RELOC_TREE_OBJECTID) {
  751. ret = btrfs_reloc_clone_csums(inode, start,
  752. cur_alloc_size);
  753. BUG_ON(ret);
  754. }
  755. if (disk_num_bytes < cur_alloc_size)
  756. break;
  757. /* we're not doing compressed IO, don't unlock the first
  758. * page (which the caller expects to stay locked), don't
  759. * clear any dirty bits and don't set any writeback bits
  760. *
  761. * Do set the Private2 bit so we know this page was properly
  762. * setup for writepage
  763. */
  764. op = unlock ? EXTENT_CLEAR_UNLOCK_PAGE : 0;
  765. op |= EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
  766. EXTENT_SET_PRIVATE2;
  767. extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
  768. start, start + ram_size - 1,
  769. locked_page, op);
  770. disk_num_bytes -= cur_alloc_size;
  771. num_bytes -= cur_alloc_size;
  772. alloc_hint = ins.objectid + ins.offset;
  773. start += cur_alloc_size;
  774. }
  775. out:
  776. ret = 0;
  777. btrfs_end_transaction(trans, root);
  778. return ret;
  779. }
  780. /*
  781. * work queue call back to started compression on a file and pages
  782. */
  783. static noinline void async_cow_start(struct btrfs_work *work)
  784. {
  785. struct async_cow *async_cow;
  786. int num_added = 0;
  787. async_cow = container_of(work, struct async_cow, work);
  788. compress_file_range(async_cow->inode, async_cow->locked_page,
  789. async_cow->start, async_cow->end, async_cow,
  790. &num_added);
  791. if (num_added == 0)
  792. async_cow->inode = NULL;
  793. }
  794. /*
  795. * work queue call back to submit previously compressed pages
  796. */
  797. static noinline void async_cow_submit(struct btrfs_work *work)
  798. {
  799. struct async_cow *async_cow;
  800. struct btrfs_root *root;
  801. unsigned long nr_pages;
  802. async_cow = container_of(work, struct async_cow, work);
  803. root = async_cow->root;
  804. nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >>
  805. PAGE_CACHE_SHIFT;
  806. atomic_sub(nr_pages, &root->fs_info->async_delalloc_pages);
  807. if (atomic_read(&root->fs_info->async_delalloc_pages) <
  808. 5 * 1042 * 1024 &&
  809. waitqueue_active(&root->fs_info->async_submit_wait))
  810. wake_up(&root->fs_info->async_submit_wait);
  811. if (async_cow->inode)
  812. submit_compressed_extents(async_cow->inode, async_cow);
  813. }
  814. static noinline void async_cow_free(struct btrfs_work *work)
  815. {
  816. struct async_cow *async_cow;
  817. async_cow = container_of(work, struct async_cow, work);
  818. kfree(async_cow);
  819. }
  820. static int cow_file_range_async(struct inode *inode, struct page *locked_page,
  821. u64 start, u64 end, int *page_started,
  822. unsigned long *nr_written)
  823. {
  824. struct async_cow *async_cow;
  825. struct btrfs_root *root = BTRFS_I(inode)->root;
  826. unsigned long nr_pages;
  827. u64 cur_end;
  828. int limit = 10 * 1024 * 1042;
  829. clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
  830. 1, 0, NULL, GFP_NOFS);
  831. while (start < end) {
  832. async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
  833. async_cow->inode = inode;
  834. async_cow->root = root;
  835. async_cow->locked_page = locked_page;
  836. async_cow->start = start;
  837. if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
  838. cur_end = end;
  839. else
  840. cur_end = min(end, start + 512 * 1024 - 1);
  841. async_cow->end = cur_end;
  842. INIT_LIST_HEAD(&async_cow->extents);
  843. async_cow->work.func = async_cow_start;
  844. async_cow->work.ordered_func = async_cow_submit;
  845. async_cow->work.ordered_free = async_cow_free;
  846. async_cow->work.flags = 0;
  847. nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >>
  848. PAGE_CACHE_SHIFT;
  849. atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
  850. btrfs_queue_worker(&root->fs_info->delalloc_workers,
  851. &async_cow->work);
  852. if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) {
  853. wait_event(root->fs_info->async_submit_wait,
  854. (atomic_read(&root->fs_info->async_delalloc_pages) <
  855. limit));
  856. }
  857. while (atomic_read(&root->fs_info->async_submit_draining) &&
  858. atomic_read(&root->fs_info->async_delalloc_pages)) {
  859. wait_event(root->fs_info->async_submit_wait,
  860. (atomic_read(&root->fs_info->async_delalloc_pages) ==
  861. 0));
  862. }
  863. *nr_written += nr_pages;
  864. start = cur_end + 1;
  865. }
  866. *page_started = 1;
  867. return 0;
  868. }
  869. static noinline int csum_exist_in_range(struct btrfs_root *root,
  870. u64 bytenr, u64 num_bytes)
  871. {
  872. int ret;
  873. struct btrfs_ordered_sum *sums;
  874. LIST_HEAD(list);
  875. ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
  876. bytenr + num_bytes - 1, &list);
  877. if (ret == 0 && list_empty(&list))
  878. return 0;
  879. while (!list_empty(&list)) {
  880. sums = list_entry(list.next, struct btrfs_ordered_sum, list);
  881. list_del(&sums->list);
  882. kfree(sums);
  883. }
  884. return 1;
  885. }
  886. /*
  887. * when nowcow writeback call back. This checks for snapshots or COW copies
  888. * of the extents that exist in the file, and COWs the file as required.
  889. *
  890. * If no cow copies or snapshots exist, we write directly to the existing
  891. * blocks on disk
  892. */
  893. static noinline int run_delalloc_nocow(struct inode *inode,
  894. struct page *locked_page,
  895. u64 start, u64 end, int *page_started, int force,
  896. unsigned long *nr_written)
  897. {
  898. struct btrfs_root *root = BTRFS_I(inode)->root;
  899. struct btrfs_trans_handle *trans;
  900. struct extent_buffer *leaf;
  901. struct btrfs_path *path;
  902. struct btrfs_file_extent_item *fi;
  903. struct btrfs_key found_key;
  904. u64 cow_start;
  905. u64 cur_offset;
  906. u64 extent_end;
  907. u64 extent_offset;
  908. u64 disk_bytenr;
  909. u64 num_bytes;
  910. int extent_type;
  911. int ret;
  912. int type;
  913. int nocow;
  914. int check_prev = 1;
  915. path = btrfs_alloc_path();
  916. BUG_ON(!path);
  917. trans = btrfs_join_transaction(root, 1);
  918. BUG_ON(!trans);
  919. cow_start = (u64)-1;
  920. cur_offset = start;
  921. while (1) {
  922. ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino,
  923. cur_offset, 0);
  924. BUG_ON(ret < 0);
  925. if (ret > 0 && path->slots[0] > 0 && check_prev) {
  926. leaf = path->nodes[0];
  927. btrfs_item_key_to_cpu(leaf, &found_key,
  928. path->slots[0] - 1);
  929. if (found_key.objectid == inode->i_ino &&
  930. found_key.type == BTRFS_EXTENT_DATA_KEY)
  931. path->slots[0]--;
  932. }
  933. check_prev = 0;
  934. next_slot:
  935. leaf = path->nodes[0];
  936. if (path->slots[0] >= btrfs_header_nritems(leaf)) {
  937. ret = btrfs_next_leaf(root, path);
  938. if (ret < 0)
  939. BUG_ON(1);
  940. if (ret > 0)
  941. break;
  942. leaf = path->nodes[0];
  943. }
  944. nocow = 0;
  945. disk_bytenr = 0;
  946. num_bytes = 0;
  947. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  948. if (found_key.objectid > inode->i_ino ||
  949. found_key.type > BTRFS_EXTENT_DATA_KEY ||
  950. found_key.offset > end)
  951. break;
  952. if (found_key.offset > cur_offset) {
  953. extent_end = found_key.offset;
  954. extent_type = 0;
  955. goto out_check;
  956. }
  957. fi = btrfs_item_ptr(leaf, path->slots[0],
  958. struct btrfs_file_extent_item);
  959. extent_type = btrfs_file_extent_type(leaf, fi);
  960. if (extent_type == BTRFS_FILE_EXTENT_REG ||
  961. extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
  962. disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
  963. extent_offset = btrfs_file_extent_offset(leaf, fi);
  964. extent_end = found_key.offset +
  965. btrfs_file_extent_num_bytes(leaf, fi);
  966. if (extent_end <= start) {
  967. path->slots[0]++;
  968. goto next_slot;
  969. }
  970. if (disk_bytenr == 0)
  971. goto out_check;
  972. if (btrfs_file_extent_compression(leaf, fi) ||
  973. btrfs_file_extent_encryption(leaf, fi) ||
  974. btrfs_file_extent_other_encoding(leaf, fi))
  975. goto out_check;
  976. if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
  977. goto out_check;
  978. if (btrfs_extent_readonly(root, disk_bytenr))
  979. goto out_check;
  980. if (btrfs_cross_ref_exist(trans, root, inode->i_ino,
  981. found_key.offset -
  982. extent_offset, disk_bytenr))
  983. goto out_check;
  984. disk_bytenr += extent_offset;
  985. disk_bytenr += cur_offset - found_key.offset;
  986. num_bytes = min(end + 1, extent_end) - cur_offset;
  987. /*
  988. * force cow if csum exists in the range.
  989. * this ensure that csum for a given extent are
  990. * either valid or do not exist.
  991. */
  992. if (csum_exist_in_range(root, disk_bytenr, num_bytes))
  993. goto out_check;
  994. nocow = 1;
  995. } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
  996. extent_end = found_key.offset +
  997. btrfs_file_extent_inline_len(leaf, fi);
  998. extent_end = ALIGN(extent_end, root->sectorsize);
  999. } else {
  1000. BUG_ON(1);
  1001. }
  1002. out_check:
  1003. if (extent_end <= start) {
  1004. path->slots[0]++;
  1005. goto next_slot;
  1006. }
  1007. if (!nocow) {
  1008. if (cow_start == (u64)-1)
  1009. cow_start = cur_offset;
  1010. cur_offset = extent_end;
  1011. if (cur_offset > end)
  1012. break;
  1013. path->slots[0]++;
  1014. goto next_slot;
  1015. }
  1016. btrfs_release_path(root, path);
  1017. if (cow_start != (u64)-1) {
  1018. ret = cow_file_range(inode, locked_page, cow_start,
  1019. found_key.offset - 1, page_started,
  1020. nr_written, 1);
  1021. BUG_ON(ret);
  1022. cow_start = (u64)-1;
  1023. }
  1024. if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
  1025. struct extent_map *em;
  1026. struct extent_map_tree *em_tree;
  1027. em_tree = &BTRFS_I(inode)->extent_tree;
  1028. em = alloc_extent_map(GFP_NOFS);
  1029. em->start = cur_offset;
  1030. em->orig_start = em->start;
  1031. em->len = num_bytes;
  1032. em->block_len = num_bytes;
  1033. em->block_start = disk_bytenr;
  1034. em->bdev = root->fs_info->fs_devices->latest_bdev;
  1035. set_bit(EXTENT_FLAG_PINNED, &em->flags);
  1036. while (1) {
  1037. write_lock(&em_tree->lock);
  1038. ret = add_extent_mapping(em_tree, em);
  1039. write_unlock(&em_tree->lock);
  1040. if (ret != -EEXIST) {
  1041. free_extent_map(em);
  1042. break;
  1043. }
  1044. btrfs_drop_extent_cache(inode, em->start,
  1045. em->start + em->len - 1, 0);
  1046. }
  1047. type = BTRFS_ORDERED_PREALLOC;
  1048. } else {
  1049. type = BTRFS_ORDERED_NOCOW;
  1050. }
  1051. ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
  1052. num_bytes, num_bytes, type);
  1053. BUG_ON(ret);
  1054. extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
  1055. cur_offset, cur_offset + num_bytes - 1,
  1056. locked_page, EXTENT_CLEAR_UNLOCK_PAGE |
  1057. EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
  1058. EXTENT_SET_PRIVATE2);
  1059. cur_offset = extent_end;
  1060. if (cur_offset > end)
  1061. break;
  1062. }
  1063. btrfs_release_path(root, path);
  1064. if (cur_offset <= end && cow_start == (u64)-1)
  1065. cow_start = cur_offset;
  1066. if (cow_start != (u64)-1) {
  1067. ret = cow_file_range(inode, locked_page, cow_start, end,
  1068. page_started, nr_written, 1);
  1069. BUG_ON(ret);
  1070. }
  1071. ret = btrfs_end_transaction(trans, root);
  1072. BUG_ON(ret);
  1073. btrfs_free_path(path);
  1074. return 0;
  1075. }
  1076. /*
  1077. * extent_io.c call back to do delayed allocation processing
  1078. */
  1079. static int run_delalloc_range(struct inode *inode, struct page *locked_page,
  1080. u64 start, u64 end, int *page_started,
  1081. unsigned long *nr_written)
  1082. {
  1083. int ret;
  1084. struct btrfs_root *root = BTRFS_I(inode)->root;
  1085. if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW)
  1086. ret = run_delalloc_nocow(inode, locked_page, start, end,
  1087. page_started, 1, nr_written);
  1088. else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC)
  1089. ret = run_delalloc_nocow(inode, locked_page, start, end,
  1090. page_started, 0, nr_written);
  1091. else if (!btrfs_test_opt(root, COMPRESS) &&
  1092. !(BTRFS_I(inode)->force_compress))
  1093. ret = cow_file_range(inode, locked_page, start, end,
  1094. page_started, nr_written, 1);
  1095. else
  1096. ret = cow_file_range_async(inode, locked_page, start, end,
  1097. page_started, nr_written);
  1098. return ret;
  1099. }
  1100. static int btrfs_split_extent_hook(struct inode *inode,
  1101. struct extent_state *orig, u64 split)
  1102. {
  1103. /* not delalloc, ignore it */
  1104. if (!(orig->state & EXTENT_DELALLOC))
  1105. return 0;
  1106. atomic_inc(&BTRFS_I(inode)->outstanding_extents);
  1107. return 0;
  1108. }
  1109. /*
  1110. * extent_io.c merge_extent_hook, used to track merged delayed allocation
  1111. * extents so we can keep track of new extents that are just merged onto old
  1112. * extents, such as when we are doing sequential writes, so we can properly
  1113. * account for the metadata space we'll need.
  1114. */
  1115. static int btrfs_merge_extent_hook(struct inode *inode,
  1116. struct extent_state *new,
  1117. struct extent_state *other)
  1118. {
  1119. /* not delalloc, ignore it */
  1120. if (!(other->state & EXTENT_DELALLOC))
  1121. return 0;
  1122. atomic_dec(&BTRFS_I(inode)->outstanding_extents);
  1123. return 0;
  1124. }
  1125. /*
  1126. * extent_io.c set_bit_hook, used to track delayed allocation
  1127. * bytes in this file, and to maintain the list of inodes that
  1128. * have pending delalloc work to be done.
  1129. */
  1130. static int btrfs_set_bit_hook(struct inode *inode,
  1131. struct extent_state *state, int *bits)
  1132. {
  1133. /*
  1134. * set_bit and clear bit hooks normally require _irqsave/restore
  1135. * but in this case, we are only testeing for the DELALLOC
  1136. * bit, which is only set or cleared with irqs on
  1137. */
  1138. if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
  1139. struct btrfs_root *root = BTRFS_I(inode)->root;
  1140. u64 len = state->end + 1 - state->start;
  1141. if (*bits & EXTENT_FIRST_DELALLOC)
  1142. *bits &= ~EXTENT_FIRST_DELALLOC;
  1143. else
  1144. atomic_inc(&BTRFS_I(inode)->outstanding_extents);
  1145. spin_lock(&root->fs_info->delalloc_lock);
  1146. BTRFS_I(inode)->delalloc_bytes += len;
  1147. root->fs_info->delalloc_bytes += len;
  1148. if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
  1149. list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
  1150. &root->fs_info->delalloc_inodes);
  1151. }
  1152. spin_unlock(&root->fs_info->delalloc_lock);
  1153. }
  1154. return 0;
  1155. }
  1156. /*
  1157. * extent_io.c clear_bit_hook, see set_bit_hook for why
  1158. */
  1159. static int btrfs_clear_bit_hook(struct inode *inode,
  1160. struct extent_state *state, int *bits)
  1161. {
  1162. /*
  1163. * set_bit and clear bit hooks normally require _irqsave/restore
  1164. * but in this case, we are only testeing for the DELALLOC
  1165. * bit, which is only set or cleared with irqs on
  1166. */
  1167. if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
  1168. struct btrfs_root *root = BTRFS_I(inode)->root;
  1169. u64 len = state->end + 1 - state->start;
  1170. if (*bits & EXTENT_FIRST_DELALLOC)
  1171. *bits &= ~EXTENT_FIRST_DELALLOC;
  1172. else if (!(*bits & EXTENT_DO_ACCOUNTING))
  1173. atomic_dec(&BTRFS_I(inode)->outstanding_extents);
  1174. if (*bits & EXTENT_DO_ACCOUNTING)
  1175. btrfs_delalloc_release_metadata(inode, len);
  1176. if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID)
  1177. btrfs_free_reserved_data_space(inode, len);
  1178. spin_lock(&root->fs_info->delalloc_lock);
  1179. root->fs_info->delalloc_bytes -= len;
  1180. BTRFS_I(inode)->delalloc_bytes -= len;
  1181. if (BTRFS_I(inode)->delalloc_bytes == 0 &&
  1182. !list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
  1183. list_del_init(&BTRFS_I(inode)->delalloc_inodes);
  1184. }
  1185. spin_unlock(&root->fs_info->delalloc_lock);
  1186. }
  1187. return 0;
  1188. }
  1189. /*
  1190. * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
  1191. * we don't create bios that span stripes or chunks
  1192. */
  1193. int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
  1194. size_t size, struct bio *bio,
  1195. unsigned long bio_flags)
  1196. {
  1197. struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
  1198. struct btrfs_mapping_tree *map_tree;
  1199. u64 logical = (u64)bio->bi_sector << 9;
  1200. u64 length = 0;
  1201. u64 map_length;
  1202. int ret;
  1203. if (bio_flags & EXTENT_BIO_COMPRESSED)
  1204. return 0;
  1205. length = bio->bi_size;
  1206. map_tree = &root->fs_info->mapping_tree;
  1207. map_length = length;
  1208. ret = btrfs_map_block(map_tree, READ, logical,
  1209. &map_length, NULL, 0);
  1210. if (map_length < length + size)
  1211. return 1;
  1212. return 0;
  1213. }
  1214. /*
  1215. * in order to insert checksums into the metadata in large chunks,
  1216. * we wait until bio submission time. All the pages in the bio are
  1217. * checksummed and sums are attached onto the ordered extent record.
  1218. *
  1219. * At IO completion time the cums attached on the ordered extent record
  1220. * are inserted into the btree
  1221. */
  1222. static int __btrfs_submit_bio_start(struct inode *inode, int rw,
  1223. struct bio *bio, int mirror_num,
  1224. unsigned long bio_flags)
  1225. {
  1226. struct btrfs_root *root = BTRFS_I(inode)->root;
  1227. int ret = 0;
  1228. ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
  1229. BUG_ON(ret);
  1230. return 0;
  1231. }
  1232. /*
  1233. * in order to insert checksums into the metadata in large chunks,
  1234. * we wait until bio submission time. All the pages in the bio are
  1235. * checksummed and sums are attached onto the ordered extent record.
  1236. *
  1237. * At IO completion time the cums attached on the ordered extent record
  1238. * are inserted into the btree
  1239. */
  1240. static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
  1241. int mirror_num, unsigned long bio_flags)
  1242. {
  1243. struct btrfs_root *root = BTRFS_I(inode)->root;
  1244. return btrfs_map_bio(root, rw, bio, mirror_num, 1);
  1245. }
  1246. /*
  1247. * extent_io.c submission hook. This does the right thing for csum calculation
  1248. * on write, or reading the csums from the tree before a read
  1249. */
  1250. static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
  1251. int mirror_num, unsigned long bio_flags)
  1252. {
  1253. struct btrfs_root *root = BTRFS_I(inode)->root;
  1254. int ret = 0;
  1255. int skip_sum;
  1256. skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
  1257. ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
  1258. BUG_ON(ret);
  1259. if (!(rw & (1 << BIO_RW))) {
  1260. if (bio_flags & EXTENT_BIO_COMPRESSED) {
  1261. return btrfs_submit_compressed_read(inode, bio,
  1262. mirror_num, bio_flags);
  1263. } else if (!skip_sum)
  1264. btrfs_lookup_bio_sums(root, inode, bio, NULL);
  1265. goto mapit;
  1266. } else if (!skip_sum) {
  1267. /* csum items have already been cloned */
  1268. if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
  1269. goto mapit;
  1270. /* we're doing a write, do the async checksumming */
  1271. return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
  1272. inode, rw, bio, mirror_num,
  1273. bio_flags, __btrfs_submit_bio_start,
  1274. __btrfs_submit_bio_done);
  1275. }
  1276. mapit:
  1277. return btrfs_map_bio(root, rw, bio, mirror_num, 0);
  1278. }
  1279. /*
  1280. * given a list of ordered sums record them in the inode. This happens
  1281. * at IO completion time based on sums calculated at bio submission time.
  1282. */
  1283. static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
  1284. struct inode *inode, u64 file_offset,
  1285. struct list_head *list)
  1286. {
  1287. struct btrfs_ordered_sum *sum;
  1288. btrfs_set_trans_block_group(trans, inode);
  1289. list_for_each_entry(sum, list, list) {
  1290. btrfs_csum_file_blocks(trans,
  1291. BTRFS_I(inode)->root->fs_info->csum_root, sum);
  1292. }
  1293. return 0;
  1294. }
  1295. int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
  1296. struct extent_state **cached_state)
  1297. {
  1298. if ((end & (PAGE_CACHE_SIZE - 1)) == 0)
  1299. WARN_ON(1);
  1300. return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
  1301. cached_state, GFP_NOFS);
  1302. }
  1303. /* see btrfs_writepage_start_hook for details on why this is required */
  1304. struct btrfs_writepage_fixup {
  1305. struct page *page;
  1306. struct btrfs_work work;
  1307. };
  1308. static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
  1309. {
  1310. struct btrfs_writepage_fixup *fixup;
  1311. struct btrfs_ordered_extent *ordered;
  1312. struct extent_state *cached_state = NULL;
  1313. struct page *page;
  1314. struct inode *inode;
  1315. u64 page_start;
  1316. u64 page_end;
  1317. fixup = container_of(work, struct btrfs_writepage_fixup, work);
  1318. page = fixup->page;
  1319. again:
  1320. lock_page(page);
  1321. if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
  1322. ClearPageChecked(page);
  1323. goto out_page;
  1324. }
  1325. inode = page->mapping->host;
  1326. page_start = page_offset(page);
  1327. page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
  1328. lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, 0,
  1329. &cached_state, GFP_NOFS);
  1330. /* already ordered? We're done */
  1331. if (PagePrivate2(page))
  1332. goto out;
  1333. ordered = btrfs_lookup_ordered_extent(inode, page_start);
  1334. if (ordered) {
  1335. unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
  1336. page_end, &cached_state, GFP_NOFS);
  1337. unlock_page(page);
  1338. btrfs_start_ordered_extent(inode, ordered, 1);
  1339. goto again;
  1340. }
  1341. BUG();
  1342. btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state);
  1343. ClearPageChecked(page);
  1344. out:
  1345. unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
  1346. &cached_state, GFP_NOFS);
  1347. out_page:
  1348. unlock_page(page);
  1349. page_cache_release(page);
  1350. }
  1351. /*
  1352. * There are a few paths in the higher layers of the kernel that directly
  1353. * set the page dirty bit without asking the filesystem if it is a
  1354. * good idea. This causes problems because we want to make sure COW
  1355. * properly happens and the data=ordered rules are followed.
  1356. *
  1357. * In our case any range that doesn't have the ORDERED bit set
  1358. * hasn't been properly setup for IO. We kick off an async process
  1359. * to fix it up. The async helper will wait for ordered extents, set
  1360. * the delalloc bit and make it safe to write the page.
  1361. */
  1362. static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
  1363. {
  1364. struct inode *inode = page->mapping->host;
  1365. struct btrfs_writepage_fixup *fixup;
  1366. struct btrfs_root *root = BTRFS_I(inode)->root;
  1367. /* this page is properly in the ordered list */
  1368. if (TestClearPagePrivate2(page))
  1369. return 0;
  1370. if (PageChecked(page))
  1371. return -EAGAIN;
  1372. fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
  1373. if (!fixup)
  1374. return -EAGAIN;
  1375. SetPageChecked(page);
  1376. page_cache_get(page);
  1377. fixup->work.func = btrfs_writepage_fixup_worker;
  1378. fixup->page = page;
  1379. btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
  1380. return -EAGAIN;
  1381. }
  1382. static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
  1383. struct inode *inode, u64 file_pos,
  1384. u64 disk_bytenr, u64 disk_num_bytes,
  1385. u64 num_bytes, u64 ram_bytes,
  1386. u8 compression, u8 encryption,
  1387. u16 other_encoding, int extent_type)
  1388. {
  1389. struct btrfs_root *root = BTRFS_I(inode)->root;
  1390. struct btrfs_file_extent_item *fi;
  1391. struct btrfs_path *path;
  1392. struct extent_buffer *leaf;
  1393. struct btrfs_key ins;
  1394. u64 hint;
  1395. int ret;
  1396. path = btrfs_alloc_path();
  1397. BUG_ON(!path);
  1398. path->leave_spinning = 1;
  1399. /*
  1400. * we may be replacing one extent in the tree with another.
  1401. * The new extent is pinned in the extent map, and we don't want
  1402. * to drop it from the cache until it is completely in the btree.
  1403. *
  1404. * So, tell btrfs_drop_extents to leave this extent in the cache.
  1405. * the caller is expected to unpin it and allow it to be merged
  1406. * with the others.
  1407. */
  1408. ret = btrfs_drop_extents(trans, inode, file_pos, file_pos + num_bytes,
  1409. &hint, 0);
  1410. BUG_ON(ret);
  1411. ins.objectid = inode->i_ino;
  1412. ins.offset = file_pos;
  1413. ins.type = BTRFS_EXTENT_DATA_KEY;
  1414. ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi));
  1415. BUG_ON(ret);
  1416. leaf = path->nodes[0];
  1417. fi = btrfs_item_ptr(leaf, path->slots[0],
  1418. struct btrfs_file_extent_item);
  1419. btrfs_set_file_extent_generation(leaf, fi, trans->transid);
  1420. btrfs_set_file_extent_type(leaf, fi, extent_type);
  1421. btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
  1422. btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
  1423. btrfs_set_file_extent_offset(leaf, fi, 0);
  1424. btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
  1425. btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
  1426. btrfs_set_file_extent_compression(leaf, fi, compression);
  1427. btrfs_set_file_extent_encryption(leaf, fi, encryption);
  1428. btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
  1429. btrfs_unlock_up_safe(path, 1);
  1430. btrfs_set_lock_blocking(leaf);
  1431. btrfs_mark_buffer_dirty(leaf);
  1432. inode_add_bytes(inode, num_bytes);
  1433. ins.objectid = disk_bytenr;
  1434. ins.offset = disk_num_bytes;
  1435. ins.type = BTRFS_EXTENT_ITEM_KEY;
  1436. ret = btrfs_alloc_reserved_file_extent(trans, root,
  1437. root->root_key.objectid,
  1438. inode->i_ino, file_pos, &ins);
  1439. BUG_ON(ret);
  1440. btrfs_free_path(path);
  1441. return 0;
  1442. }
  1443. /*
  1444. * helper function for btrfs_finish_ordered_io, this
  1445. * just reads in some of the csum leaves to prime them into ram
  1446. * before we start the transaction. It limits the amount of btree
  1447. * reads required while inside the transaction.
  1448. */
  1449. /* as ordered data IO finishes, this gets called so we can finish
  1450. * an ordered extent if the range of bytes in the file it covers are
  1451. * fully written.
  1452. */
  1453. static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end)
  1454. {
  1455. struct btrfs_root *root = BTRFS_I(inode)->root;
  1456. struct btrfs_trans_handle *trans = NULL;
  1457. struct btrfs_ordered_extent *ordered_extent = NULL;
  1458. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  1459. struct extent_state *cached_state = NULL;
  1460. int compressed = 0;
  1461. int ret;
  1462. ret = btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
  1463. end - start + 1);
  1464. if (!ret)
  1465. return 0;
  1466. BUG_ON(!ordered_extent);
  1467. if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
  1468. BUG_ON(!list_empty(&ordered_extent->list));
  1469. ret = btrfs_ordered_update_i_size(inode, 0, ordered_extent);
  1470. if (!ret) {
  1471. trans = btrfs_join_transaction(root, 1);
  1472. btrfs_set_trans_block_group(trans, inode);
  1473. trans->block_rsv = &root->fs_info->delalloc_block_rsv;
  1474. ret = btrfs_update_inode(trans, root, inode);
  1475. BUG_ON(ret);
  1476. }
  1477. goto out;
  1478. }
  1479. lock_extent_bits(io_tree, ordered_extent->file_offset,
  1480. ordered_extent->file_offset + ordered_extent->len - 1,
  1481. 0, &cached_state, GFP_NOFS);
  1482. trans = btrfs_join_transaction(root, 1);
  1483. btrfs_set_trans_block_group(trans, inode);
  1484. trans->block_rsv = &root->fs_info->delalloc_block_rsv;
  1485. if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
  1486. compressed = 1;
  1487. if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
  1488. BUG_ON(compressed);
  1489. ret = btrfs_mark_extent_written(trans, inode,
  1490. ordered_extent->file_offset,
  1491. ordered_extent->file_offset +
  1492. ordered_extent->len);
  1493. BUG_ON(ret);
  1494. } else {
  1495. ret = insert_reserved_file_extent(trans, inode,
  1496. ordered_extent->file_offset,
  1497. ordered_extent->start,
  1498. ordered_extent->disk_len,
  1499. ordered_extent->len,
  1500. ordered_extent->len,
  1501. compressed, 0, 0,
  1502. BTRFS_FILE_EXTENT_REG);
  1503. unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
  1504. ordered_extent->file_offset,
  1505. ordered_extent->len);
  1506. BUG_ON(ret);
  1507. }
  1508. unlock_extent_cached(io_tree, ordered_extent->file_offset,
  1509. ordered_extent->file_offset +
  1510. ordered_extent->len - 1, &cached_state, GFP_NOFS);
  1511. add_pending_csums(trans, inode, ordered_extent->file_offset,
  1512. &ordered_extent->list);
  1513. btrfs_ordered_update_i_size(inode, 0, ordered_extent);
  1514. ret = btrfs_update_inode(trans, root, inode);
  1515. BUG_ON(ret);
  1516. out:
  1517. btrfs_delalloc_release_metadata(inode, ordered_extent->len);
  1518. if (trans)
  1519. btrfs_end_transaction(trans, root);
  1520. /* once for us */
  1521. btrfs_put_ordered_extent(ordered_extent);
  1522. /* once for the tree */
  1523. btrfs_put_ordered_extent(ordered_extent);
  1524. return 0;
  1525. }
  1526. static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
  1527. struct extent_state *state, int uptodate)
  1528. {
  1529. ClearPagePrivate2(page);
  1530. return btrfs_finish_ordered_io(page->mapping->host, start, end);
  1531. }
  1532. /*
  1533. * When IO fails, either with EIO or csum verification fails, we
  1534. * try other mirrors that might have a good copy of the data. This
  1535. * io_failure_record is used to record state as we go through all the
  1536. * mirrors. If another mirror has good data, the page is set up to date
  1537. * and things continue. If a good mirror can't be found, the original
  1538. * bio end_io callback is called to indicate things have failed.
  1539. */
  1540. struct io_failure_record {
  1541. struct page *page;
  1542. u64 start;
  1543. u64 len;
  1544. u64 logical;
  1545. unsigned long bio_flags;
  1546. int last_mirror;
  1547. };
  1548. static int btrfs_io_failed_hook(struct bio *failed_bio,
  1549. struct page *page, u64 start, u64 end,
  1550. struct extent_state *state)
  1551. {
  1552. struct io_failure_record *failrec = NULL;
  1553. u64 private;
  1554. struct extent_map *em;
  1555. struct inode *inode = page->mapping->host;
  1556. struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
  1557. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  1558. struct bio *bio;
  1559. int num_copies;
  1560. int ret;
  1561. int rw;
  1562. u64 logical;
  1563. ret = get_state_private(failure_tree, start, &private);
  1564. if (ret) {
  1565. failrec = kmalloc(sizeof(*failrec), GFP_NOFS);
  1566. if (!failrec)
  1567. return -ENOMEM;
  1568. failrec->start = start;
  1569. failrec->len = end - start + 1;
  1570. failrec->last_mirror = 0;
  1571. failrec->bio_flags = 0;
  1572. read_lock(&em_tree->lock);
  1573. em = lookup_extent_mapping(em_tree, start, failrec->len);
  1574. if (em->start > start || em->start + em->len < start) {
  1575. free_extent_map(em);
  1576. em = NULL;
  1577. }
  1578. read_unlock(&em_tree->lock);
  1579. if (!em || IS_ERR(em)) {
  1580. kfree(failrec);
  1581. return -EIO;
  1582. }
  1583. logical = start - em->start;
  1584. logical = em->block_start + logical;
  1585. if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
  1586. logical = em->block_start;
  1587. failrec->bio_flags = EXTENT_BIO_COMPRESSED;
  1588. }
  1589. failrec->logical = logical;
  1590. free_extent_map(em);
  1591. set_extent_bits(failure_tree, start, end, EXTENT_LOCKED |
  1592. EXTENT_DIRTY, GFP_NOFS);
  1593. set_state_private(failure_tree, start,
  1594. (u64)(unsigned long)failrec);
  1595. } else {
  1596. failrec = (struct io_failure_record *)(unsigned long)private;
  1597. }
  1598. num_copies = btrfs_num_copies(
  1599. &BTRFS_I(inode)->root->fs_info->mapping_tree,
  1600. failrec->logical, failrec->len);
  1601. failrec->last_mirror++;
  1602. if (!state) {
  1603. spin_lock(&BTRFS_I(inode)->io_tree.lock);
  1604. state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
  1605. failrec->start,
  1606. EXTENT_LOCKED);
  1607. if (state && state->start != failrec->start)
  1608. state = NULL;
  1609. spin_unlock(&BTRFS_I(inode)->io_tree.lock);
  1610. }
  1611. if (!state || failrec->last_mirror > num_copies) {
  1612. set_state_private(failure_tree, failrec->start, 0);
  1613. clear_extent_bits(failure_tree, failrec->start,
  1614. failrec->start + failrec->len - 1,
  1615. EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
  1616. kfree(failrec);
  1617. return -EIO;
  1618. }
  1619. bio = bio_alloc(GFP_NOFS, 1);
  1620. bio->bi_private = state;
  1621. bio->bi_end_io = failed_bio->bi_end_io;
  1622. bio->bi_sector = failrec->logical >> 9;
  1623. bio->bi_bdev = failed_bio->bi_bdev;
  1624. bio->bi_size = 0;
  1625. bio_add_page(bio, page, failrec->len, start - page_offset(page));
  1626. if (failed_bio->bi_rw & (1 << BIO_RW))
  1627. rw = WRITE;
  1628. else
  1629. rw = READ;
  1630. BTRFS_I(inode)->io_tree.ops->submit_bio_hook(inode, rw, bio,
  1631. failrec->last_mirror,
  1632. failrec->bio_flags);
  1633. return 0;
  1634. }
  1635. /*
  1636. * each time an IO finishes, we do a fast check in the IO failure tree
  1637. * to see if we need to process or clean up an io_failure_record
  1638. */
  1639. static int btrfs_clean_io_failures(struct inode *inode, u64 start)
  1640. {
  1641. u64 private;
  1642. u64 private_failure;
  1643. struct io_failure_record *failure;
  1644. int ret;
  1645. private = 0;
  1646. if (count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
  1647. (u64)-1, 1, EXTENT_DIRTY)) {
  1648. ret = get_state_private(&BTRFS_I(inode)->io_failure_tree,
  1649. start, &private_failure);
  1650. if (ret == 0) {
  1651. failure = (struct io_failure_record *)(unsigned long)
  1652. private_failure;
  1653. set_state_private(&BTRFS_I(inode)->io_failure_tree,
  1654. failure->start, 0);
  1655. clear_extent_bits(&BTRFS_I(inode)->io_failure_tree,
  1656. failure->start,
  1657. failure->start + failure->len - 1,
  1658. EXTENT_DIRTY | EXTENT_LOCKED,
  1659. GFP_NOFS);
  1660. kfree(failure);
  1661. }
  1662. }
  1663. return 0;
  1664. }
  1665. /*
  1666. * when reads are done, we need to check csums to verify the data is correct
  1667. * if there's a match, we allow the bio to finish. If not, we go through
  1668. * the io_failure_record routines to find good copies
  1669. */
  1670. static int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
  1671. struct extent_state *state)
  1672. {
  1673. size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
  1674. struct inode *inode = page->mapping->host;
  1675. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  1676. char *kaddr;
  1677. u64 private = ~(u32)0;
  1678. int ret;
  1679. struct btrfs_root *root = BTRFS_I(inode)->root;
  1680. u32 csum = ~(u32)0;
  1681. if (PageChecked(page)) {
  1682. ClearPageChecked(page);
  1683. goto good;
  1684. }
  1685. if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
  1686. return 0;
  1687. if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
  1688. test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
  1689. clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
  1690. GFP_NOFS);
  1691. return 0;
  1692. }
  1693. if (state && state->start == start) {
  1694. private = state->private;
  1695. ret = 0;
  1696. } else {
  1697. ret = get_state_private(io_tree, start, &private);
  1698. }
  1699. kaddr = kmap_atomic(page, KM_USER0);
  1700. if (ret)
  1701. goto zeroit;
  1702. csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
  1703. btrfs_csum_final(csum, (char *)&csum);
  1704. if (csum != private)
  1705. goto zeroit;
  1706. kunmap_atomic(kaddr, KM_USER0);
  1707. good:
  1708. /* if the io failure tree for this inode is non-empty,
  1709. * check to see if we've recovered from a failed IO
  1710. */
  1711. btrfs_clean_io_failures(inode, start);
  1712. return 0;
  1713. zeroit:
  1714. if (printk_ratelimit()) {
  1715. printk(KERN_INFO "btrfs csum failed ino %lu off %llu csum %u "
  1716. "private %llu\n", page->mapping->host->i_ino,
  1717. (unsigned long long)start, csum,
  1718. (unsigned long long)private);
  1719. }
  1720. memset(kaddr + offset, 1, end - start + 1);
  1721. flush_dcache_page(page);
  1722. kunmap_atomic(kaddr, KM_USER0);
  1723. if (private == 0)
  1724. return 0;
  1725. return -EIO;
  1726. }
  1727. struct delayed_iput {
  1728. struct list_head list;
  1729. struct inode *inode;
  1730. };
  1731. void btrfs_add_delayed_iput(struct inode *inode)
  1732. {
  1733. struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
  1734. struct delayed_iput *delayed;
  1735. if (atomic_add_unless(&inode->i_count, -1, 1))
  1736. return;
  1737. delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL);
  1738. delayed->inode = inode;
  1739. spin_lock(&fs_info->delayed_iput_lock);
  1740. list_add_tail(&delayed->list, &fs_info->delayed_iputs);
  1741. spin_unlock(&fs_info->delayed_iput_lock);
  1742. }
  1743. void btrfs_run_delayed_iputs(struct btrfs_root *root)
  1744. {
  1745. LIST_HEAD(list);
  1746. struct btrfs_fs_info *fs_info = root->fs_info;
  1747. struct delayed_iput *delayed;
  1748. int empty;
  1749. spin_lock(&fs_info->delayed_iput_lock);
  1750. empty = list_empty(&fs_info->delayed_iputs);
  1751. spin_unlock(&fs_info->delayed_iput_lock);
  1752. if (empty)
  1753. return;
  1754. down_read(&root->fs_info->cleanup_work_sem);
  1755. spin_lock(&fs_info->delayed_iput_lock);
  1756. list_splice_init(&fs_info->delayed_iputs, &list);
  1757. spin_unlock(&fs_info->delayed_iput_lock);
  1758. while (!list_empty(&list)) {
  1759. delayed = list_entry(list.next, struct delayed_iput, list);
  1760. list_del(&delayed->list);
  1761. iput(delayed->inode);
  1762. kfree(delayed);
  1763. }
  1764. up_read(&root->fs_info->cleanup_work_sem);
  1765. }
  1766. /*
  1767. * This creates an orphan entry for the given inode in case something goes
  1768. * wrong in the middle of an unlink/truncate.
  1769. */
  1770. int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
  1771. {
  1772. struct btrfs_root *root = BTRFS_I(inode)->root;
  1773. int ret = 0;
  1774. spin_lock(&root->list_lock);
  1775. /* already on the orphan list, we're good */
  1776. if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
  1777. spin_unlock(&root->list_lock);
  1778. return 0;
  1779. }
  1780. list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
  1781. spin_unlock(&root->list_lock);
  1782. /*
  1783. * insert an orphan item to track this unlinked/truncated file
  1784. */
  1785. ret = btrfs_insert_orphan_item(trans, root, inode->i_ino);
  1786. return ret;
  1787. }
  1788. /*
  1789. * We have done the truncate/delete so we can go ahead and remove the orphan
  1790. * item for this particular inode.
  1791. */
  1792. int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode)
  1793. {
  1794. struct btrfs_root *root = BTRFS_I(inode)->root;
  1795. int ret = 0;
  1796. spin_lock(&root->list_lock);
  1797. if (list_empty(&BTRFS_I(inode)->i_orphan)) {
  1798. spin_unlock(&root->list_lock);
  1799. return 0;
  1800. }
  1801. list_del_init(&BTRFS_I(inode)->i_orphan);
  1802. if (!trans) {
  1803. spin_unlock(&root->list_lock);
  1804. return 0;
  1805. }
  1806. spin_unlock(&root->list_lock);
  1807. ret = btrfs_del_orphan_item(trans, root, inode->i_ino);
  1808. return ret;
  1809. }
  1810. /*
  1811. * this cleans up any orphans that may be left on the list from the last use
  1812. * of this root.
  1813. */
  1814. void btrfs_orphan_cleanup(struct btrfs_root *root)
  1815. {
  1816. struct btrfs_path *path;
  1817. struct extent_buffer *leaf;
  1818. struct btrfs_item *item;
  1819. struct btrfs_key key, found_key;
  1820. struct btrfs_trans_handle *trans;
  1821. struct inode *inode;
  1822. int ret = 0, nr_unlink = 0, nr_truncate = 0;
  1823. if (!xchg(&root->clean_orphans, 0))
  1824. return;
  1825. path = btrfs_alloc_path();
  1826. BUG_ON(!path);
  1827. path->reada = -1;
  1828. key.objectid = BTRFS_ORPHAN_OBJECTID;
  1829. btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
  1830. key.offset = (u64)-1;
  1831. while (1) {
  1832. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  1833. if (ret < 0) {
  1834. printk(KERN_ERR "Error searching slot for orphan: %d"
  1835. "\n", ret);
  1836. break;
  1837. }
  1838. /*
  1839. * if ret == 0 means we found what we were searching for, which
  1840. * is weird, but possible, so only screw with path if we didnt
  1841. * find the key and see if we have stuff that matches
  1842. */
  1843. if (ret > 0) {
  1844. if (path->slots[0] == 0)
  1845. break;
  1846. path->slots[0]--;
  1847. }
  1848. /* pull out the item */
  1849. leaf = path->nodes[0];
  1850. item = btrfs_item_nr(leaf, path->slots[0]);
  1851. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  1852. /* make sure the item matches what we want */
  1853. if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
  1854. break;
  1855. if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
  1856. break;
  1857. /* release the path since we're done with it */
  1858. btrfs_release_path(root, path);
  1859. /*
  1860. * this is where we are basically btrfs_lookup, without the
  1861. * crossing root thing. we store the inode number in the
  1862. * offset of the orphan item.
  1863. */
  1864. found_key.objectid = found_key.offset;
  1865. found_key.type = BTRFS_INODE_ITEM_KEY;
  1866. found_key.offset = 0;
  1867. inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL);
  1868. if (IS_ERR(inode))
  1869. break;
  1870. /*
  1871. * add this inode to the orphan list so btrfs_orphan_del does
  1872. * the proper thing when we hit it
  1873. */
  1874. spin_lock(&root->list_lock);
  1875. list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
  1876. spin_unlock(&root->list_lock);
  1877. /*
  1878. * if this is a bad inode, means we actually succeeded in
  1879. * removing the inode, but not the orphan record, which means
  1880. * we need to manually delete the orphan since iput will just
  1881. * do a destroy_inode
  1882. */
  1883. if (is_bad_inode(inode)) {
  1884. trans = btrfs_start_transaction(root, 0);
  1885. btrfs_orphan_del(trans, inode);
  1886. btrfs_end_transaction(trans, root);
  1887. iput(inode);
  1888. continue;
  1889. }
  1890. /* if we have links, this was a truncate, lets do that */
  1891. if (inode->i_nlink) {
  1892. nr_truncate++;
  1893. btrfs_truncate(inode);
  1894. } else {
  1895. nr_unlink++;
  1896. }
  1897. /* this will do delete_inode and everything for us */
  1898. iput(inode);
  1899. }
  1900. if (nr_unlink)
  1901. printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink);
  1902. if (nr_truncate)
  1903. printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate);
  1904. btrfs_free_path(path);
  1905. }
  1906. /*
  1907. * very simple check to peek ahead in the leaf looking for xattrs. If we
  1908. * don't find any xattrs, we know there can't be any acls.
  1909. *
  1910. * slot is the slot the inode is in, objectid is the objectid of the inode
  1911. */
  1912. static noinline int acls_after_inode_item(struct extent_buffer *leaf,
  1913. int slot, u64 objectid)
  1914. {
  1915. u32 nritems = btrfs_header_nritems(leaf);
  1916. struct btrfs_key found_key;
  1917. int scanned = 0;
  1918. slot++;
  1919. while (slot < nritems) {
  1920. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  1921. /* we found a different objectid, there must not be acls */
  1922. if (found_key.objectid != objectid)
  1923. return 0;
  1924. /* we found an xattr, assume we've got an acl */
  1925. if (found_key.type == BTRFS_XATTR_ITEM_KEY)
  1926. return 1;
  1927. /*
  1928. * we found a key greater than an xattr key, there can't
  1929. * be any acls later on
  1930. */
  1931. if (found_key.type > BTRFS_XATTR_ITEM_KEY)
  1932. return 0;
  1933. slot++;
  1934. scanned++;
  1935. /*
  1936. * it goes inode, inode backrefs, xattrs, extents,
  1937. * so if there are a ton of hard links to an inode there can
  1938. * be a lot of backrefs. Don't waste time searching too hard,
  1939. * this is just an optimization
  1940. */
  1941. if (scanned >= 8)
  1942. break;
  1943. }
  1944. /* we hit the end of the leaf before we found an xattr or
  1945. * something larger than an xattr. We have to assume the inode
  1946. * has acls
  1947. */
  1948. return 1;
  1949. }
  1950. /*
  1951. * read an inode from the btree into the in-memory inode
  1952. */
  1953. static void btrfs_read_locked_inode(struct inode *inode)
  1954. {
  1955. struct btrfs_path *path;
  1956. struct extent_buffer *leaf;
  1957. struct btrfs_inode_item *inode_item;
  1958. struct btrfs_timespec *tspec;
  1959. struct btrfs_root *root = BTRFS_I(inode)->root;
  1960. struct btrfs_key location;
  1961. int maybe_acls;
  1962. u64 alloc_group_block;
  1963. u32 rdev;
  1964. int ret;
  1965. path = btrfs_alloc_path();
  1966. BUG_ON(!path);
  1967. memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
  1968. ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
  1969. if (ret)
  1970. goto make_bad;
  1971. leaf = path->nodes[0];
  1972. inode_item = btrfs_item_ptr(leaf, path->slots[0],
  1973. struct btrfs_inode_item);
  1974. inode->i_mode = btrfs_inode_mode(leaf, inode_item);
  1975. inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
  1976. inode->i_uid = btrfs_inode_uid(leaf, inode_item);
  1977. inode->i_gid = btrfs_inode_gid(leaf, inode_item);
  1978. btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
  1979. tspec = btrfs_inode_atime(inode_item);
  1980. inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
  1981. inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
  1982. tspec = btrfs_inode_mtime(inode_item);
  1983. inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
  1984. inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
  1985. tspec = btrfs_inode_ctime(inode_item);
  1986. inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
  1987. inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
  1988. inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
  1989. BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
  1990. BTRFS_I(inode)->sequence = btrfs_inode_sequence(leaf, inode_item);
  1991. inode->i_generation = BTRFS_I(inode)->generation;
  1992. inode->i_rdev = 0;
  1993. rdev = btrfs_inode_rdev(leaf, inode_item);
  1994. BTRFS_I(inode)->index_cnt = (u64)-1;
  1995. BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
  1996. alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
  1997. /*
  1998. * try to precache a NULL acl entry for files that don't have
  1999. * any xattrs or acls
  2000. */
  2001. maybe_acls = acls_after_inode_item(leaf, path->slots[0], inode->i_ino);
  2002. if (!maybe_acls)
  2003. cache_no_acl(inode);
  2004. BTRFS_I(inode)->block_group = btrfs_find_block_group(root, 0,
  2005. alloc_group_block, 0);
  2006. btrfs_free_path(path);
  2007. inode_item = NULL;
  2008. switch (inode->i_mode & S_IFMT) {
  2009. case S_IFREG:
  2010. inode->i_mapping->a_ops = &btrfs_aops;
  2011. inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
  2012. BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
  2013. inode->i_fop = &btrfs_file_operations;
  2014. inode->i_op = &btrfs_file_inode_operations;
  2015. break;
  2016. case S_IFDIR:
  2017. inode->i_fop = &btrfs_dir_file_operations;
  2018. if (root == root->fs_info->tree_root)
  2019. inode->i_op = &btrfs_dir_ro_inode_operations;
  2020. else
  2021. inode->i_op = &btrfs_dir_inode_operations;
  2022. break;
  2023. case S_IFLNK:
  2024. inode->i_op = &btrfs_symlink_inode_operations;
  2025. inode->i_mapping->a_ops = &btrfs_symlink_aops;
  2026. inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
  2027. break;
  2028. default:
  2029. inode->i_op = &btrfs_special_inode_operations;
  2030. init_special_inode(inode, inode->i_mode, rdev);
  2031. break;
  2032. }
  2033. btrfs_update_iflags(inode);
  2034. return;
  2035. make_bad:
  2036. btrfs_free_path(path);
  2037. make_bad_inode(inode);
  2038. }
  2039. /*
  2040. * given a leaf and an inode, copy the inode fields into the leaf
  2041. */
  2042. static void fill_inode_item(struct btrfs_trans_handle *trans,
  2043. struct extent_buffer *leaf,
  2044. struct btrfs_inode_item *item,
  2045. struct inode *inode)
  2046. {
  2047. btrfs_set_inode_uid(leaf, item, inode->i_uid);
  2048. btrfs_set_inode_gid(leaf, item, inode->i_gid);
  2049. btrfs_set_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size);
  2050. btrfs_set_inode_mode(leaf, item, inode->i_mode);
  2051. btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
  2052. btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
  2053. inode->i_atime.tv_sec);
  2054. btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
  2055. inode->i_atime.tv_nsec);
  2056. btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
  2057. inode->i_mtime.tv_sec);
  2058. btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
  2059. inode->i_mtime.tv_nsec);
  2060. btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
  2061. inode->i_ctime.tv_sec);
  2062. btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
  2063. inode->i_ctime.tv_nsec);
  2064. btrfs_set_inode_nbytes(leaf, item, inode_get_bytes(inode));
  2065. btrfs_set_inode_generation(leaf, item, BTRFS_I(inode)->generation);
  2066. btrfs_set_inode_sequence(leaf, item, BTRFS_I(inode)->sequence);
  2067. btrfs_set_inode_transid(leaf, item, trans->transid);
  2068. btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
  2069. btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
  2070. btrfs_set_inode_block_group(leaf, item, BTRFS_I(inode)->block_group);
  2071. }
  2072. /*
  2073. * copy everything in the in-memory inode into the btree.
  2074. */
  2075. noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
  2076. struct btrfs_root *root, struct inode *inode)
  2077. {
  2078. struct btrfs_inode_item *inode_item;
  2079. struct btrfs_path *path;
  2080. struct extent_buffer *leaf;
  2081. int ret;
  2082. path = btrfs_alloc_path();
  2083. BUG_ON(!path);
  2084. path->leave_spinning = 1;
  2085. ret = btrfs_lookup_inode(trans, root, path,
  2086. &BTRFS_I(inode)->location, 1);
  2087. if (ret) {
  2088. if (ret > 0)
  2089. ret = -ENOENT;
  2090. goto failed;
  2091. }
  2092. btrfs_unlock_up_safe(path, 1);
  2093. leaf = path->nodes[0];
  2094. inode_item = btrfs_item_ptr(leaf, path->slots[0],
  2095. struct btrfs_inode_item);
  2096. fill_inode_item(trans, leaf, inode_item, inode);
  2097. btrfs_mark_buffer_dirty(leaf);
  2098. btrfs_set_inode_last_trans(trans, inode);
  2099. ret = 0;
  2100. failed:
  2101. btrfs_free_path(path);
  2102. return ret;
  2103. }
  2104. /*
  2105. * unlink helper that gets used here in inode.c and in the tree logging
  2106. * recovery code. It remove a link in a directory with a given name, and
  2107. * also drops the back refs in the inode to the directory
  2108. */
  2109. int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
  2110. struct btrfs_root *root,
  2111. struct inode *dir, struct inode *inode,
  2112. const char *name, int name_len)
  2113. {
  2114. struct btrfs_path *path;
  2115. int ret = 0;
  2116. struct extent_buffer *leaf;
  2117. struct btrfs_dir_item *di;
  2118. struct btrfs_key key;
  2119. u64 index;
  2120. path = btrfs_alloc_path();
  2121. if (!path) {
  2122. ret = -ENOMEM;
  2123. goto err;
  2124. }
  2125. path->leave_spinning = 1;
  2126. di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
  2127. name, name_len, -1);
  2128. if (IS_ERR(di)) {
  2129. ret = PTR_ERR(di);
  2130. goto err;
  2131. }
  2132. if (!di) {
  2133. ret = -ENOENT;
  2134. goto err;
  2135. }
  2136. leaf = path->nodes[0];
  2137. btrfs_dir_item_key_to_cpu(leaf, di, &key);
  2138. ret = btrfs_delete_one_dir_name(trans, root, path, di);
  2139. if (ret)
  2140. goto err;
  2141. btrfs_release_path(root, path);
  2142. ret = btrfs_del_inode_ref(trans, root, name, name_len,
  2143. inode->i_ino,
  2144. dir->i_ino, &index);
  2145. if (ret) {
  2146. printk(KERN_INFO "btrfs failed to delete reference to %.*s, "
  2147. "inode %lu parent %lu\n", name_len, name,
  2148. inode->i_ino, dir->i_ino);
  2149. goto err;
  2150. }
  2151. di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
  2152. index, name, name_len, -1);
  2153. if (IS_ERR(di)) {
  2154. ret = PTR_ERR(di);
  2155. goto err;
  2156. }
  2157. if (!di) {
  2158. ret = -ENOENT;
  2159. goto err;
  2160. }
  2161. ret = btrfs_delete_one_dir_name(trans, root, path, di);
  2162. btrfs_release_path(root, path);
  2163. ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
  2164. inode, dir->i_ino);
  2165. BUG_ON(ret != 0 && ret != -ENOENT);
  2166. ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
  2167. dir, index);
  2168. BUG_ON(ret);
  2169. err:
  2170. btrfs_free_path(path);
  2171. if (ret)
  2172. goto out;
  2173. btrfs_i_size_write(dir, dir->i_size - name_len * 2);
  2174. inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
  2175. btrfs_update_inode(trans, root, dir);
  2176. btrfs_drop_nlink(inode);
  2177. ret = btrfs_update_inode(trans, root, inode);
  2178. out:
  2179. return ret;
  2180. }
  2181. /* helper to check if there is any shared block in the path */
  2182. static int check_path_shared(struct btrfs_root *root,
  2183. struct btrfs_path *path)
  2184. {
  2185. struct extent_buffer *eb;
  2186. int level;
  2187. int ret;
  2188. u64 refs;
  2189. for (level = 0; level < BTRFS_MAX_LEVEL; level++) {
  2190. if (!path->nodes[level])
  2191. break;
  2192. eb = path->nodes[level];
  2193. if (!btrfs_block_can_be_shared(root, eb))
  2194. continue;
  2195. ret = btrfs_lookup_extent_info(NULL, root, eb->start, eb->len,
  2196. &refs, NULL);
  2197. if (refs > 1)
  2198. return 1;
  2199. }
  2200. return 0;
  2201. }
  2202. /*
  2203. * helper to start transaction for unlink and rmdir.
  2204. *
  2205. * unlink and rmdir are special in btrfs, they do not always free space.
  2206. * so in enospc case, we should make sure they will free space before
  2207. * allowing them to use the global metadata reservation.
  2208. */
  2209. static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir,
  2210. struct dentry *dentry)
  2211. {
  2212. struct btrfs_trans_handle *trans;
  2213. struct btrfs_root *root = BTRFS_I(dir)->root;
  2214. struct btrfs_path *path;
  2215. struct btrfs_inode_ref *ref;
  2216. struct btrfs_dir_item *di;
  2217. struct inode *inode = dentry->d_inode;
  2218. u64 index;
  2219. int check_link = 1;
  2220. int err = -ENOSPC;
  2221. int ret;
  2222. trans = btrfs_start_transaction(root, 10);
  2223. if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
  2224. return trans;
  2225. if (inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
  2226. return ERR_PTR(-ENOSPC);
  2227. /* check if there is someone else holds reference */
  2228. if (S_ISDIR(inode->i_mode) && atomic_read(&inode->i_count) > 1)
  2229. return ERR_PTR(-ENOSPC);
  2230. if (atomic_read(&inode->i_count) > 2)
  2231. return ERR_PTR(-ENOSPC);
  2232. if (xchg(&root->fs_info->enospc_unlink, 1))
  2233. return ERR_PTR(-ENOSPC);
  2234. path = btrfs_alloc_path();
  2235. if (!path) {
  2236. root->fs_info->enospc_unlink = 0;
  2237. return ERR_PTR(-ENOMEM);
  2238. }
  2239. trans = btrfs_start_transaction(root, 0);
  2240. if (IS_ERR(trans)) {
  2241. btrfs_free_path(path);
  2242. root->fs_info->enospc_unlink = 0;
  2243. return trans;
  2244. }
  2245. path->skip_locking = 1;
  2246. path->search_commit_root = 1;
  2247. ret = btrfs_lookup_inode(trans, root, path,
  2248. &BTRFS_I(dir)->location, 0);
  2249. if (ret < 0) {
  2250. err = ret;
  2251. goto out;
  2252. }
  2253. if (ret == 0) {
  2254. if (check_path_shared(root, path))
  2255. goto out;
  2256. } else {
  2257. check_link = 0;
  2258. }
  2259. btrfs_release_path(root, path);
  2260. ret = btrfs_lookup_inode(trans, root, path,
  2261. &BTRFS_I(inode)->location, 0);
  2262. if (ret < 0) {
  2263. err = ret;
  2264. goto out;
  2265. }
  2266. if (ret == 0) {
  2267. if (check_path_shared(root, path))
  2268. goto out;
  2269. } else {
  2270. check_link = 0;
  2271. }
  2272. btrfs_release_path(root, path);
  2273. if (ret == 0 && S_ISREG(inode->i_mode)) {
  2274. ret = btrfs_lookup_file_extent(trans, root, path,
  2275. inode->i_ino, (u64)-1, 0);
  2276. if (ret < 0) {
  2277. err = ret;
  2278. goto out;
  2279. }
  2280. BUG_ON(ret == 0);
  2281. if (check_path_shared(root, path))
  2282. goto out;
  2283. btrfs_release_path(root, path);
  2284. }
  2285. if (!check_link) {
  2286. err = 0;
  2287. goto out;
  2288. }
  2289. di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
  2290. dentry->d_name.name, dentry->d_name.len, 0);
  2291. if (IS_ERR(di)) {
  2292. err = PTR_ERR(di);
  2293. goto out;
  2294. }
  2295. if (di) {
  2296. if (check_path_shared(root, path))
  2297. goto out;
  2298. } else {
  2299. err = 0;
  2300. goto out;
  2301. }
  2302. btrfs_release_path(root, path);
  2303. ref = btrfs_lookup_inode_ref(trans, root, path,
  2304. dentry->d_name.name, dentry->d_name.len,
  2305. inode->i_ino, dir->i_ino, 0);
  2306. if (IS_ERR(ref)) {
  2307. err = PTR_ERR(ref);
  2308. goto out;
  2309. }
  2310. BUG_ON(!ref);
  2311. if (check_path_shared(root, path))
  2312. goto out;
  2313. index = btrfs_inode_ref_index(path->nodes[0], ref);
  2314. btrfs_release_path(root, path);
  2315. di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino, index,
  2316. dentry->d_name.name, dentry->d_name.len, 0);
  2317. if (IS_ERR(di)) {
  2318. err = PTR_ERR(di);
  2319. goto out;
  2320. }
  2321. BUG_ON(ret == -ENOENT);
  2322. if (check_path_shared(root, path))
  2323. goto out;
  2324. err = 0;
  2325. out:
  2326. btrfs_free_path(path);
  2327. if (err) {
  2328. btrfs_end_transaction(trans, root);
  2329. root->fs_info->enospc_unlink = 0;
  2330. return ERR_PTR(err);
  2331. }
  2332. trans->block_rsv = &root->fs_info->global_block_rsv;
  2333. return trans;
  2334. }
  2335. static void __unlink_end_trans(struct btrfs_trans_handle *trans,
  2336. struct btrfs_root *root)
  2337. {
  2338. if (trans->block_rsv == &root->fs_info->global_block_rsv) {
  2339. BUG_ON(!root->fs_info->enospc_unlink);
  2340. root->fs_info->enospc_unlink = 0;
  2341. }
  2342. btrfs_end_transaction_throttle(trans, root);
  2343. }
  2344. static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
  2345. {
  2346. struct btrfs_root *root = BTRFS_I(dir)->root;
  2347. struct btrfs_trans_handle *trans;
  2348. struct inode *inode = dentry->d_inode;
  2349. int ret;
  2350. unsigned long nr = 0;
  2351. trans = __unlink_start_trans(dir, dentry);
  2352. if (IS_ERR(trans))
  2353. return PTR_ERR(trans);
  2354. btrfs_set_trans_block_group(trans, dir);
  2355. btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0);
  2356. ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
  2357. dentry->d_name.name, dentry->d_name.len);
  2358. BUG_ON(ret);
  2359. if (inode->i_nlink == 0) {
  2360. ret = btrfs_orphan_add(trans, inode);
  2361. BUG_ON(ret);
  2362. }
  2363. nr = trans->blocks_used;
  2364. __unlink_end_trans(trans, root);
  2365. btrfs_btree_balance_dirty(root, nr);
  2366. return ret;
  2367. }
  2368. int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
  2369. struct btrfs_root *root,
  2370. struct inode *dir, u64 objectid,
  2371. const char *name, int name_len)
  2372. {
  2373. struct btrfs_path *path;
  2374. struct extent_buffer *leaf;
  2375. struct btrfs_dir_item *di;
  2376. struct btrfs_key key;
  2377. u64 index;
  2378. int ret;
  2379. path = btrfs_alloc_path();
  2380. if (!path)
  2381. return -ENOMEM;
  2382. di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
  2383. name, name_len, -1);
  2384. BUG_ON(!di || IS_ERR(di));
  2385. leaf = path->nodes[0];
  2386. btrfs_dir_item_key_to_cpu(leaf, di, &key);
  2387. WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
  2388. ret = btrfs_delete_one_dir_name(trans, root, path, di);
  2389. BUG_ON(ret);
  2390. btrfs_release_path(root, path);
  2391. ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
  2392. objectid, root->root_key.objectid,
  2393. dir->i_ino, &index, name, name_len);
  2394. if (ret < 0) {
  2395. BUG_ON(ret != -ENOENT);
  2396. di = btrfs_search_dir_index_item(root, path, dir->i_ino,
  2397. name, name_len);
  2398. BUG_ON(!di || IS_ERR(di));
  2399. leaf = path->nodes[0];
  2400. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  2401. btrfs_release_path(root, path);
  2402. index = key.offset;
  2403. }
  2404. di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
  2405. index, name, name_len, -1);
  2406. BUG_ON(!di || IS_ERR(di));
  2407. leaf = path->nodes[0];
  2408. btrfs_dir_item_key_to_cpu(leaf, di, &key);
  2409. WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
  2410. ret = btrfs_delete_one_dir_name(trans, root, path, di);
  2411. BUG_ON(ret);
  2412. btrfs_release_path(root, path);
  2413. btrfs_i_size_write(dir, dir->i_size - name_len * 2);
  2414. dir->i_mtime = dir->i_ctime = CURRENT_TIME;
  2415. ret = btrfs_update_inode(trans, root, dir);
  2416. BUG_ON(ret);
  2417. dir->i_sb->s_dirt = 1;
  2418. btrfs_free_path(path);
  2419. return 0;
  2420. }
  2421. static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
  2422. {
  2423. struct inode *inode = dentry->d_inode;
  2424. int err = 0;
  2425. struct btrfs_root *root = BTRFS_I(dir)->root;
  2426. struct btrfs_trans_handle *trans;
  2427. unsigned long nr = 0;
  2428. if (inode->i_size > BTRFS_EMPTY_DIR_SIZE ||
  2429. inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
  2430. return -ENOTEMPTY;
  2431. trans = __unlink_start_trans(dir, dentry);
  2432. if (IS_ERR(trans))
  2433. return PTR_ERR(trans);
  2434. btrfs_set_trans_block_group(trans, dir);
  2435. if (unlikely(inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
  2436. err = btrfs_unlink_subvol(trans, root, dir,
  2437. BTRFS_I(inode)->location.objectid,
  2438. dentry->d_name.name,
  2439. dentry->d_name.len);
  2440. goto out;
  2441. }
  2442. err = btrfs_orphan_add(trans, inode);
  2443. if (err)
  2444. goto out;
  2445. /* now the directory is empty */
  2446. err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
  2447. dentry->d_name.name, dentry->d_name.len);
  2448. if (!err)
  2449. btrfs_i_size_write(inode, 0);
  2450. out:
  2451. nr = trans->blocks_used;
  2452. __unlink_end_trans(trans, root);
  2453. btrfs_btree_balance_dirty(root, nr);
  2454. return err;
  2455. }
  2456. #if 0
  2457. /*
  2458. * when truncating bytes in a file, it is possible to avoid reading
  2459. * the leaves that contain only checksum items. This can be the
  2460. * majority of the IO required to delete a large file, but it must
  2461. * be done carefully.
  2462. *
  2463. * The keys in the level just above the leaves are checked to make sure
  2464. * the lowest key in a given leaf is a csum key, and starts at an offset
  2465. * after the new size.
  2466. *
  2467. * Then the key for the next leaf is checked to make sure it also has
  2468. * a checksum item for the same file. If it does, we know our target leaf
  2469. * contains only checksum items, and it can be safely freed without reading
  2470. * it.
  2471. *
  2472. * This is just an optimization targeted at large files. It may do
  2473. * nothing. It will return 0 unless things went badly.
  2474. */
  2475. static noinline int drop_csum_leaves(struct btrfs_trans_handle *trans,
  2476. struct btrfs_root *root,
  2477. struct btrfs_path *path,
  2478. struct inode *inode, u64 new_size)
  2479. {
  2480. struct btrfs_key key;
  2481. int ret;
  2482. int nritems;
  2483. struct btrfs_key found_key;
  2484. struct btrfs_key other_key;
  2485. struct btrfs_leaf_ref *ref;
  2486. u64 leaf_gen;
  2487. u64 leaf_start;
  2488. path->lowest_level = 1;
  2489. key.objectid = inode->i_ino;
  2490. key.type = BTRFS_CSUM_ITEM_KEY;
  2491. key.offset = new_size;
  2492. again:
  2493. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  2494. if (ret < 0)
  2495. goto out;
  2496. if (path->nodes[1] == NULL) {
  2497. ret = 0;
  2498. goto out;
  2499. }
  2500. ret = 0;
  2501. btrfs_node_key_to_cpu(path->nodes[1], &found_key, path->slots[1]);
  2502. nritems = btrfs_header_nritems(path->nodes[1]);
  2503. if (!nritems)
  2504. goto out;
  2505. if (path->slots[1] >= nritems)
  2506. goto next_node;
  2507. /* did we find a key greater than anything we want to delete? */
  2508. if (found_key.objectid > inode->i_ino ||
  2509. (found_key.objectid == inode->i_ino && found_key.type > key.type))
  2510. goto out;
  2511. /* we check the next key in the node to make sure the leave contains
  2512. * only checksum items. This comparison doesn't work if our
  2513. * leaf is the last one in the node
  2514. */
  2515. if (path->slots[1] + 1 >= nritems) {
  2516. next_node:
  2517. /* search forward from the last key in the node, this
  2518. * will bring us into the next node in the tree
  2519. */
  2520. btrfs_node_key_to_cpu(path->nodes[1], &found_key, nritems - 1);
  2521. /* unlikely, but we inc below, so check to be safe */
  2522. if (found_key.offset == (u64)-1)
  2523. goto out;
  2524. /* search_forward needs a path with locks held, do the
  2525. * search again for the original key. It is possible
  2526. * this will race with a balance and return a path that
  2527. * we could modify, but this drop is just an optimization
  2528. * and is allowed to miss some leaves.
  2529. */
  2530. btrfs_release_path(root, path);
  2531. found_key.offset++;
  2532. /* setup a max key for search_forward */
  2533. other_key.offset = (u64)-1;
  2534. other_key.type = key.type;
  2535. other_key.objectid = key.objectid;
  2536. path->keep_locks = 1;
  2537. ret = btrfs_search_forward(root, &found_key, &other_key,
  2538. path, 0, 0);
  2539. path->keep_locks = 0;
  2540. if (ret || found_key.objectid != key.objectid ||
  2541. found_key.type != key.type) {
  2542. ret = 0;
  2543. goto out;
  2544. }
  2545. key.offset = found_key.offset;
  2546. btrfs_release_path(root, path);
  2547. cond_resched();
  2548. goto again;
  2549. }
  2550. /* we know there's one more slot after us in the tree,
  2551. * read that key so we can verify it is also a checksum item
  2552. */
  2553. btrfs_node_key_to_cpu(path->nodes[1], &other_key, path->slots[1] + 1);
  2554. if (found_key.objectid < inode->i_ino)
  2555. goto next_key;
  2556. if (found_key.type != key.type || found_key.offset < new_size)
  2557. goto next_key;
  2558. /*
  2559. * if the key for the next leaf isn't a csum key from this objectid,
  2560. * we can't be sure there aren't good items inside this leaf.
  2561. * Bail out
  2562. */
  2563. if (other_key.objectid != inode->i_ino || other_key.type != key.type)
  2564. goto out;
  2565. leaf_start = btrfs_node_blockptr(path->nodes[1], path->slots[1]);
  2566. leaf_gen = btrfs_node_ptr_generation(path->nodes[1], path->slots[1]);
  2567. /*
  2568. * it is safe to delete this leaf, it contains only
  2569. * csum items from this inode at an offset >= new_size
  2570. */
  2571. ret = btrfs_del_leaf(trans, root, path, leaf_start);
  2572. BUG_ON(ret);
  2573. if (root->ref_cows && leaf_gen < trans->transid) {
  2574. ref = btrfs_alloc_leaf_ref(root, 0);
  2575. if (ref) {
  2576. ref->root_gen = root->root_key.offset;
  2577. ref->bytenr = leaf_start;
  2578. ref->owner = 0;
  2579. ref->generation = leaf_gen;
  2580. ref->nritems = 0;
  2581. btrfs_sort_leaf_ref(ref);
  2582. ret = btrfs_add_leaf_ref(root, ref, 0);
  2583. WARN_ON(ret);
  2584. btrfs_free_leaf_ref(root, ref);
  2585. } else {
  2586. WARN_ON(1);
  2587. }
  2588. }
  2589. next_key:
  2590. btrfs_release_path(root, path);
  2591. if (other_key.objectid == inode->i_ino &&
  2592. other_key.type == key.type && other_key.offset > key.offset) {
  2593. key.offset = other_key.offset;
  2594. cond_resched();
  2595. goto again;
  2596. }
  2597. ret = 0;
  2598. out:
  2599. /* fixup any changes we've made to the path */
  2600. path->lowest_level = 0;
  2601. path->keep_locks = 0;
  2602. btrfs_release_path(root, path);
  2603. return ret;
  2604. }
  2605. #endif
  2606. /*
  2607. * this can truncate away extent items, csum items and directory items.
  2608. * It starts at a high offset and removes keys until it can't find
  2609. * any higher than new_size
  2610. *
  2611. * csum items that cross the new i_size are truncated to the new size
  2612. * as well.
  2613. *
  2614. * min_type is the minimum key type to truncate down to. If set to 0, this
  2615. * will kill all the items on this inode, including the INODE_ITEM_KEY.
  2616. */
  2617. int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
  2618. struct btrfs_root *root,
  2619. struct inode *inode,
  2620. u64 new_size, u32 min_type)
  2621. {
  2622. struct btrfs_path *path;
  2623. struct extent_buffer *leaf;
  2624. struct btrfs_file_extent_item *fi;
  2625. struct btrfs_key key;
  2626. struct btrfs_key found_key;
  2627. u64 extent_start = 0;
  2628. u64 extent_num_bytes = 0;
  2629. u64 extent_offset = 0;
  2630. u64 item_end = 0;
  2631. u64 mask = root->sectorsize - 1;
  2632. u32 found_type = (u8)-1;
  2633. int found_extent;
  2634. int del_item;
  2635. int pending_del_nr = 0;
  2636. int pending_del_slot = 0;
  2637. int extent_type = -1;
  2638. int encoding;
  2639. int ret;
  2640. int err = 0;
  2641. BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
  2642. if (root->ref_cows)
  2643. btrfs_drop_extent_cache(inode, new_size & (~mask), (u64)-1, 0);
  2644. path = btrfs_alloc_path();
  2645. BUG_ON(!path);
  2646. path->reada = -1;
  2647. key.objectid = inode->i_ino;
  2648. key.offset = (u64)-1;
  2649. key.type = (u8)-1;
  2650. search_again:
  2651. path->leave_spinning = 1;
  2652. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  2653. if (ret < 0) {
  2654. err = ret;
  2655. goto out;
  2656. }
  2657. if (ret > 0) {
  2658. /* there are no items in the tree for us to truncate, we're
  2659. * done
  2660. */
  2661. if (path->slots[0] == 0)
  2662. goto out;
  2663. path->slots[0]--;
  2664. }
  2665. while (1) {
  2666. fi = NULL;
  2667. leaf = path->nodes[0];
  2668. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  2669. found_type = btrfs_key_type(&found_key);
  2670. encoding = 0;
  2671. if (found_key.objectid != inode->i_ino)
  2672. break;
  2673. if (found_type < min_type)
  2674. break;
  2675. item_end = found_key.offset;
  2676. if (found_type == BTRFS_EXTENT_DATA_KEY) {
  2677. fi = btrfs_item_ptr(leaf, path->slots[0],
  2678. struct btrfs_file_extent_item);
  2679. extent_type = btrfs_file_extent_type(leaf, fi);
  2680. encoding = btrfs_file_extent_compression(leaf, fi);
  2681. encoding |= btrfs_file_extent_encryption(leaf, fi);
  2682. encoding |= btrfs_file_extent_other_encoding(leaf, fi);
  2683. if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
  2684. item_end +=
  2685. btrfs_file_extent_num_bytes(leaf, fi);
  2686. } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
  2687. item_end += btrfs_file_extent_inline_len(leaf,
  2688. fi);
  2689. }
  2690. item_end--;
  2691. }
  2692. if (found_type > min_type) {
  2693. del_item = 1;
  2694. } else {
  2695. if (item_end < new_size)
  2696. break;
  2697. if (found_key.offset >= new_size)
  2698. del_item = 1;
  2699. else
  2700. del_item = 0;
  2701. }
  2702. found_extent = 0;
  2703. /* FIXME, shrink the extent if the ref count is only 1 */
  2704. if (found_type != BTRFS_EXTENT_DATA_KEY)
  2705. goto delete;
  2706. if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
  2707. u64 num_dec;
  2708. extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
  2709. if (!del_item && !encoding) {
  2710. u64 orig_num_bytes =
  2711. btrfs_file_extent_num_bytes(leaf, fi);
  2712. extent_num_bytes = new_size -
  2713. found_key.offset + root->sectorsize - 1;
  2714. extent_num_bytes = extent_num_bytes &
  2715. ~((u64)root->sectorsize - 1);
  2716. btrfs_set_file_extent_num_bytes(leaf, fi,
  2717. extent_num_bytes);
  2718. num_dec = (orig_num_bytes -
  2719. extent_num_bytes);
  2720. if (root->ref_cows && extent_start != 0)
  2721. inode_sub_bytes(inode, num_dec);
  2722. btrfs_mark_buffer_dirty(leaf);
  2723. } else {
  2724. extent_num_bytes =
  2725. btrfs_file_extent_disk_num_bytes(leaf,
  2726. fi);
  2727. extent_offset = found_key.offset -
  2728. btrfs_file_extent_offset(leaf, fi);
  2729. /* FIXME blocksize != 4096 */
  2730. num_dec = btrfs_file_extent_num_bytes(leaf, fi);
  2731. if (extent_start != 0) {
  2732. found_extent = 1;
  2733. if (root->ref_cows)
  2734. inode_sub_bytes(inode, num_dec);
  2735. }
  2736. }
  2737. } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
  2738. /*
  2739. * we can't truncate inline items that have had
  2740. * special encodings
  2741. */
  2742. if (!del_item &&
  2743. btrfs_file_extent_compression(leaf, fi) == 0 &&
  2744. btrfs_file_extent_encryption(leaf, fi) == 0 &&
  2745. btrfs_file_extent_other_encoding(leaf, fi) == 0) {
  2746. u32 size = new_size - found_key.offset;
  2747. if (root->ref_cows) {
  2748. inode_sub_bytes(inode, item_end + 1 -
  2749. new_size);
  2750. }
  2751. size =
  2752. btrfs_file_extent_calc_inline_size(size);
  2753. ret = btrfs_truncate_item(trans, root, path,
  2754. size, 1);
  2755. BUG_ON(ret);
  2756. } else if (root->ref_cows) {
  2757. inode_sub_bytes(inode, item_end + 1 -
  2758. found_key.offset);
  2759. }
  2760. }
  2761. delete:
  2762. if (del_item) {
  2763. if (!pending_del_nr) {
  2764. /* no pending yet, add ourselves */
  2765. pending_del_slot = path->slots[0];
  2766. pending_del_nr = 1;
  2767. } else if (pending_del_nr &&
  2768. path->slots[0] + 1 == pending_del_slot) {
  2769. /* hop on the pending chunk */
  2770. pending_del_nr++;
  2771. pending_del_slot = path->slots[0];
  2772. } else {
  2773. BUG();
  2774. }
  2775. } else {
  2776. break;
  2777. }
  2778. if (found_extent && root->ref_cows) {
  2779. btrfs_set_path_blocking(path);
  2780. ret = btrfs_free_extent(trans, root, extent_start,
  2781. extent_num_bytes, 0,
  2782. btrfs_header_owner(leaf),
  2783. inode->i_ino, extent_offset);
  2784. BUG_ON(ret);
  2785. }
  2786. if (found_type == BTRFS_INODE_ITEM_KEY)
  2787. break;
  2788. if (path->slots[0] == 0 ||
  2789. path->slots[0] != pending_del_slot) {
  2790. if (root->ref_cows) {
  2791. err = -EAGAIN;
  2792. goto out;
  2793. }
  2794. if (pending_del_nr) {
  2795. ret = btrfs_del_items(trans, root, path,
  2796. pending_del_slot,
  2797. pending_del_nr);
  2798. BUG_ON(ret);
  2799. pending_del_nr = 0;
  2800. }
  2801. btrfs_release_path(root, path);
  2802. goto search_again;
  2803. } else {
  2804. path->slots[0]--;
  2805. }
  2806. }
  2807. out:
  2808. if (pending_del_nr) {
  2809. ret = btrfs_del_items(trans, root, path, pending_del_slot,
  2810. pending_del_nr);
  2811. }
  2812. btrfs_free_path(path);
  2813. return err;
  2814. }
  2815. /*
  2816. * taken from block_truncate_page, but does cow as it zeros out
  2817. * any bytes left in the last page in the file.
  2818. */
  2819. static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
  2820. {
  2821. struct inode *inode = mapping->host;
  2822. struct btrfs_root *root = BTRFS_I(inode)->root;
  2823. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  2824. struct btrfs_ordered_extent *ordered;
  2825. struct extent_state *cached_state = NULL;
  2826. char *kaddr;
  2827. u32 blocksize = root->sectorsize;
  2828. pgoff_t index = from >> PAGE_CACHE_SHIFT;
  2829. unsigned offset = from & (PAGE_CACHE_SIZE-1);
  2830. struct page *page;
  2831. int ret = 0;
  2832. u64 page_start;
  2833. u64 page_end;
  2834. if ((offset & (blocksize - 1)) == 0)
  2835. goto out;
  2836. ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
  2837. if (ret)
  2838. goto out;
  2839. ret = -ENOMEM;
  2840. again:
  2841. page = grab_cache_page(mapping, index);
  2842. if (!page) {
  2843. btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
  2844. goto out;
  2845. }
  2846. page_start = page_offset(page);
  2847. page_end = page_start + PAGE_CACHE_SIZE - 1;
  2848. if (!PageUptodate(page)) {
  2849. ret = btrfs_readpage(NULL, page);
  2850. lock_page(page);
  2851. if (page->mapping != mapping) {
  2852. unlock_page(page);
  2853. page_cache_release(page);
  2854. goto again;
  2855. }
  2856. if (!PageUptodate(page)) {
  2857. ret = -EIO;
  2858. goto out_unlock;
  2859. }
  2860. }
  2861. wait_on_page_writeback(page);
  2862. lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state,
  2863. GFP_NOFS);
  2864. set_page_extent_mapped(page);
  2865. ordered = btrfs_lookup_ordered_extent(inode, page_start);
  2866. if (ordered) {
  2867. unlock_extent_cached(io_tree, page_start, page_end,
  2868. &cached_state, GFP_NOFS);
  2869. unlock_page(page);
  2870. page_cache_release(page);
  2871. btrfs_start_ordered_extent(inode, ordered, 1);
  2872. btrfs_put_ordered_extent(ordered);
  2873. goto again;
  2874. }
  2875. clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
  2876. EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING,
  2877. 0, 0, &cached_state, GFP_NOFS);
  2878. ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
  2879. &cached_state);
  2880. if (ret) {
  2881. unlock_extent_cached(io_tree, page_start, page_end,
  2882. &cached_state, GFP_NOFS);
  2883. goto out_unlock;
  2884. }
  2885. ret = 0;
  2886. if (offset != PAGE_CACHE_SIZE) {
  2887. kaddr = kmap(page);
  2888. memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
  2889. flush_dcache_page(page);
  2890. kunmap(page);
  2891. }
  2892. ClearPageChecked(page);
  2893. set_page_dirty(page);
  2894. unlock_extent_cached(io_tree, page_start, page_end, &cached_state,
  2895. GFP_NOFS);
  2896. out_unlock:
  2897. if (ret)
  2898. btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
  2899. unlock_page(page);
  2900. page_cache_release(page);
  2901. out:
  2902. return ret;
  2903. }
  2904. int btrfs_cont_expand(struct inode *inode, loff_t size)
  2905. {
  2906. struct btrfs_trans_handle *trans;
  2907. struct btrfs_root *root = BTRFS_I(inode)->root;
  2908. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  2909. struct extent_map *em = NULL;
  2910. struct extent_state *cached_state = NULL;
  2911. u64 mask = root->sectorsize - 1;
  2912. u64 hole_start = (inode->i_size + mask) & ~mask;
  2913. u64 block_end = (size + mask) & ~mask;
  2914. u64 last_byte;
  2915. u64 cur_offset;
  2916. u64 hole_size;
  2917. int err = 0;
  2918. if (size <= hole_start)
  2919. return 0;
  2920. while (1) {
  2921. struct btrfs_ordered_extent *ordered;
  2922. btrfs_wait_ordered_range(inode, hole_start,
  2923. block_end - hole_start);
  2924. lock_extent_bits(io_tree, hole_start, block_end - 1, 0,
  2925. &cached_state, GFP_NOFS);
  2926. ordered = btrfs_lookup_ordered_extent(inode, hole_start);
  2927. if (!ordered)
  2928. break;
  2929. unlock_extent_cached(io_tree, hole_start, block_end - 1,
  2930. &cached_state, GFP_NOFS);
  2931. btrfs_put_ordered_extent(ordered);
  2932. }
  2933. cur_offset = hole_start;
  2934. while (1) {
  2935. em = btrfs_get_extent(inode, NULL, 0, cur_offset,
  2936. block_end - cur_offset, 0);
  2937. BUG_ON(IS_ERR(em) || !em);
  2938. last_byte = min(extent_map_end(em), block_end);
  2939. last_byte = (last_byte + mask) & ~mask;
  2940. if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
  2941. u64 hint_byte = 0;
  2942. hole_size = last_byte - cur_offset;
  2943. trans = btrfs_start_transaction(root, 2);
  2944. if (IS_ERR(trans)) {
  2945. err = PTR_ERR(trans);
  2946. break;
  2947. }
  2948. btrfs_set_trans_block_group(trans, inode);
  2949. err = btrfs_drop_extents(trans, inode, cur_offset,
  2950. cur_offset + hole_size,
  2951. &hint_byte, 1);
  2952. BUG_ON(err);
  2953. err = btrfs_insert_file_extent(trans, root,
  2954. inode->i_ino, cur_offset, 0,
  2955. 0, hole_size, 0, hole_size,
  2956. 0, 0, 0);
  2957. BUG_ON(err);
  2958. btrfs_drop_extent_cache(inode, hole_start,
  2959. last_byte - 1, 0);
  2960. btrfs_end_transaction(trans, root);
  2961. }
  2962. free_extent_map(em);
  2963. em = NULL;
  2964. cur_offset = last_byte;
  2965. if (cur_offset >= block_end)
  2966. break;
  2967. }
  2968. free_extent_map(em);
  2969. unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
  2970. GFP_NOFS);
  2971. return err;
  2972. }
  2973. static int btrfs_setattr_size(struct inode *inode, struct iattr *attr)
  2974. {
  2975. struct btrfs_root *root = BTRFS_I(inode)->root;
  2976. struct btrfs_trans_handle *trans;
  2977. unsigned long nr;
  2978. int ret;
  2979. if (attr->ia_size == inode->i_size)
  2980. return 0;
  2981. if (attr->ia_size > inode->i_size) {
  2982. unsigned long limit;
  2983. limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur;
  2984. if (attr->ia_size > inode->i_sb->s_maxbytes)
  2985. return -EFBIG;
  2986. if (limit != RLIM_INFINITY && attr->ia_size > limit) {
  2987. send_sig(SIGXFSZ, current, 0);
  2988. return -EFBIG;
  2989. }
  2990. }
  2991. trans = btrfs_start_transaction(root, 1);
  2992. btrfs_set_trans_block_group(trans, inode);
  2993. ret = btrfs_orphan_add(trans, inode);
  2994. BUG_ON(ret);
  2995. nr = trans->blocks_used;
  2996. btrfs_end_transaction(trans, root);
  2997. btrfs_btree_balance_dirty(root, nr);
  2998. if (attr->ia_size > inode->i_size) {
  2999. ret = btrfs_cont_expand(inode, attr->ia_size);
  3000. if (ret) {
  3001. btrfs_truncate(inode);
  3002. return ret;
  3003. }
  3004. i_size_write(inode, attr->ia_size);
  3005. btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
  3006. trans = btrfs_start_transaction(root, 1);
  3007. btrfs_set_trans_block_group(trans, inode);
  3008. ret = btrfs_update_inode(trans, root, inode);
  3009. BUG_ON(ret);
  3010. if (inode->i_nlink > 0) {
  3011. ret = btrfs_orphan_del(trans, inode);
  3012. BUG_ON(ret);
  3013. }
  3014. nr = trans->blocks_used;
  3015. btrfs_end_transaction(trans, root);
  3016. btrfs_btree_balance_dirty(root, nr);
  3017. return 0;
  3018. }
  3019. /*
  3020. * We're truncating a file that used to have good data down to
  3021. * zero. Make sure it gets into the ordered flush list so that
  3022. * any new writes get down to disk quickly.
  3023. */
  3024. if (attr->ia_size == 0)
  3025. BTRFS_I(inode)->ordered_data_close = 1;
  3026. /* we don't support swapfiles, so vmtruncate shouldn't fail */
  3027. ret = vmtruncate(inode, attr->ia_size);
  3028. BUG_ON(ret);
  3029. return 0;
  3030. }
  3031. static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
  3032. {
  3033. struct inode *inode = dentry->d_inode;
  3034. int err;
  3035. err = inode_change_ok(inode, attr);
  3036. if (err)
  3037. return err;
  3038. if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
  3039. err = btrfs_setattr_size(inode, attr);
  3040. if (err)
  3041. return err;
  3042. }
  3043. attr->ia_valid &= ~ATTR_SIZE;
  3044. if (attr->ia_valid)
  3045. err = inode_setattr(inode, attr);
  3046. if (!err && ((attr->ia_valid & ATTR_MODE)))
  3047. err = btrfs_acl_chmod(inode);
  3048. return err;
  3049. }
  3050. void btrfs_delete_inode(struct inode *inode)
  3051. {
  3052. struct btrfs_trans_handle *trans;
  3053. struct btrfs_root *root = BTRFS_I(inode)->root;
  3054. unsigned long nr;
  3055. int ret;
  3056. truncate_inode_pages(&inode->i_data, 0);
  3057. if (is_bad_inode(inode)) {
  3058. btrfs_orphan_del(NULL, inode);
  3059. goto no_delete;
  3060. }
  3061. btrfs_wait_ordered_range(inode, 0, (u64)-1);
  3062. if (root->fs_info->log_root_recovering) {
  3063. BUG_ON(!list_empty(&BTRFS_I(inode)->i_orphan));
  3064. goto no_delete;
  3065. }
  3066. if (inode->i_nlink > 0) {
  3067. BUG_ON(btrfs_root_refs(&root->root_item) != 0);
  3068. goto no_delete;
  3069. }
  3070. btrfs_i_size_write(inode, 0);
  3071. while (1) {
  3072. trans = btrfs_start_transaction(root, 1);
  3073. btrfs_set_trans_block_group(trans, inode);
  3074. ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
  3075. if (ret != -EAGAIN)
  3076. break;
  3077. nr = trans->blocks_used;
  3078. btrfs_end_transaction(trans, root);
  3079. trans = NULL;
  3080. btrfs_btree_balance_dirty(root, nr);
  3081. }
  3082. if (ret == 0) {
  3083. ret = btrfs_orphan_del(trans, inode);
  3084. BUG_ON(ret);
  3085. }
  3086. nr = trans->blocks_used;
  3087. btrfs_end_transaction(trans, root);
  3088. btrfs_btree_balance_dirty(root, nr);
  3089. no_delete:
  3090. clear_inode(inode);
  3091. return;
  3092. }
  3093. /*
  3094. * this returns the key found in the dir entry in the location pointer.
  3095. * If no dir entries were found, location->objectid is 0.
  3096. */
  3097. static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
  3098. struct btrfs_key *location)
  3099. {
  3100. const char *name = dentry->d_name.name;
  3101. int namelen = dentry->d_name.len;
  3102. struct btrfs_dir_item *di;
  3103. struct btrfs_path *path;
  3104. struct btrfs_root *root = BTRFS_I(dir)->root;
  3105. int ret = 0;
  3106. path = btrfs_alloc_path();
  3107. BUG_ON(!path);
  3108. di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
  3109. namelen, 0);
  3110. if (IS_ERR(di))
  3111. ret = PTR_ERR(di);
  3112. if (!di || IS_ERR(di))
  3113. goto out_err;
  3114. btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
  3115. out:
  3116. btrfs_free_path(path);
  3117. return ret;
  3118. out_err:
  3119. location->objectid = 0;
  3120. goto out;
  3121. }
  3122. /*
  3123. * when we hit a tree root in a directory, the btrfs part of the inode
  3124. * needs to be changed to reflect the root directory of the tree root. This
  3125. * is kind of like crossing a mount point.
  3126. */
  3127. static int fixup_tree_root_location(struct btrfs_root *root,
  3128. struct inode *dir,
  3129. struct dentry *dentry,
  3130. struct btrfs_key *location,
  3131. struct btrfs_root **sub_root)
  3132. {
  3133. struct btrfs_path *path;
  3134. struct btrfs_root *new_root;
  3135. struct btrfs_root_ref *ref;
  3136. struct extent_buffer *leaf;
  3137. int ret;
  3138. int err = 0;
  3139. path = btrfs_alloc_path();
  3140. if (!path) {
  3141. err = -ENOMEM;
  3142. goto out;
  3143. }
  3144. err = -ENOENT;
  3145. ret = btrfs_find_root_ref(root->fs_info->tree_root, path,
  3146. BTRFS_I(dir)->root->root_key.objectid,
  3147. location->objectid);
  3148. if (ret) {
  3149. if (ret < 0)
  3150. err = ret;
  3151. goto out;
  3152. }
  3153. leaf = path->nodes[0];
  3154. ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
  3155. if (btrfs_root_ref_dirid(leaf, ref) != dir->i_ino ||
  3156. btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
  3157. goto out;
  3158. ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
  3159. (unsigned long)(ref + 1),
  3160. dentry->d_name.len);
  3161. if (ret)
  3162. goto out;
  3163. btrfs_release_path(root->fs_info->tree_root, path);
  3164. new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
  3165. if (IS_ERR(new_root)) {
  3166. err = PTR_ERR(new_root);
  3167. goto out;
  3168. }
  3169. if (btrfs_root_refs(&new_root->root_item) == 0) {
  3170. err = -ENOENT;
  3171. goto out;
  3172. }
  3173. *sub_root = new_root;
  3174. location->objectid = btrfs_root_dirid(&new_root->root_item);
  3175. location->type = BTRFS_INODE_ITEM_KEY;
  3176. location->offset = 0;
  3177. err = 0;
  3178. out:
  3179. btrfs_free_path(path);
  3180. return err;
  3181. }
  3182. static void inode_tree_add(struct inode *inode)
  3183. {
  3184. struct btrfs_root *root = BTRFS_I(inode)->root;
  3185. struct btrfs_inode *entry;
  3186. struct rb_node **p;
  3187. struct rb_node *parent;
  3188. again:
  3189. p = &root->inode_tree.rb_node;
  3190. parent = NULL;
  3191. if (hlist_unhashed(&inode->i_hash))
  3192. return;
  3193. spin_lock(&root->inode_lock);
  3194. while (*p) {
  3195. parent = *p;
  3196. entry = rb_entry(parent, struct btrfs_inode, rb_node);
  3197. if (inode->i_ino < entry->vfs_inode.i_ino)
  3198. p = &parent->rb_left;
  3199. else if (inode->i_ino > entry->vfs_inode.i_ino)
  3200. p = &parent->rb_right;
  3201. else {
  3202. WARN_ON(!(entry->vfs_inode.i_state &
  3203. (I_WILL_FREE | I_FREEING | I_CLEAR)));
  3204. rb_erase(parent, &root->inode_tree);
  3205. RB_CLEAR_NODE(parent);
  3206. spin_unlock(&root->inode_lock);
  3207. goto again;
  3208. }
  3209. }
  3210. rb_link_node(&BTRFS_I(inode)->rb_node, parent, p);
  3211. rb_insert_color(&BTRFS_I(inode)->rb_node, &root->inode_tree);
  3212. spin_unlock(&root->inode_lock);
  3213. }
  3214. static void inode_tree_del(struct inode *inode)
  3215. {
  3216. struct btrfs_root *root = BTRFS_I(inode)->root;
  3217. int empty = 0;
  3218. spin_lock(&root->inode_lock);
  3219. if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
  3220. rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
  3221. RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
  3222. empty = RB_EMPTY_ROOT(&root->inode_tree);
  3223. }
  3224. spin_unlock(&root->inode_lock);
  3225. if (empty && btrfs_root_refs(&root->root_item) == 0) {
  3226. synchronize_srcu(&root->fs_info->subvol_srcu);
  3227. spin_lock(&root->inode_lock);
  3228. empty = RB_EMPTY_ROOT(&root->inode_tree);
  3229. spin_unlock(&root->inode_lock);
  3230. if (empty)
  3231. btrfs_add_dead_root(root);
  3232. }
  3233. }
  3234. int btrfs_invalidate_inodes(struct btrfs_root *root)
  3235. {
  3236. struct rb_node *node;
  3237. struct rb_node *prev;
  3238. struct btrfs_inode *entry;
  3239. struct inode *inode;
  3240. u64 objectid = 0;
  3241. WARN_ON(btrfs_root_refs(&root->root_item) != 0);
  3242. spin_lock(&root->inode_lock);
  3243. again:
  3244. node = root->inode_tree.rb_node;
  3245. prev = NULL;
  3246. while (node) {
  3247. prev = node;
  3248. entry = rb_entry(node, struct btrfs_inode, rb_node);
  3249. if (objectid < entry->vfs_inode.i_ino)
  3250. node = node->rb_left;
  3251. else if (objectid > entry->vfs_inode.i_ino)
  3252. node = node->rb_right;
  3253. else
  3254. break;
  3255. }
  3256. if (!node) {
  3257. while (prev) {
  3258. entry = rb_entry(prev, struct btrfs_inode, rb_node);
  3259. if (objectid <= entry->vfs_inode.i_ino) {
  3260. node = prev;
  3261. break;
  3262. }
  3263. prev = rb_next(prev);
  3264. }
  3265. }
  3266. while (node) {
  3267. entry = rb_entry(node, struct btrfs_inode, rb_node);
  3268. objectid = entry->vfs_inode.i_ino + 1;
  3269. inode = igrab(&entry->vfs_inode);
  3270. if (inode) {
  3271. spin_unlock(&root->inode_lock);
  3272. if (atomic_read(&inode->i_count) > 1)
  3273. d_prune_aliases(inode);
  3274. /*
  3275. * btrfs_drop_inode will remove it from
  3276. * the inode cache when its usage count
  3277. * hits zero.
  3278. */
  3279. iput(inode);
  3280. cond_resched();
  3281. spin_lock(&root->inode_lock);
  3282. goto again;
  3283. }
  3284. if (cond_resched_lock(&root->inode_lock))
  3285. goto again;
  3286. node = rb_next(node);
  3287. }
  3288. spin_unlock(&root->inode_lock);
  3289. return 0;
  3290. }
  3291. static int btrfs_init_locked_inode(struct inode *inode, void *p)
  3292. {
  3293. struct btrfs_iget_args *args = p;
  3294. inode->i_ino = args->ino;
  3295. BTRFS_I(inode)->root = args->root;
  3296. btrfs_set_inode_space_info(args->root, inode);
  3297. return 0;
  3298. }
  3299. static int btrfs_find_actor(struct inode *inode, void *opaque)
  3300. {
  3301. struct btrfs_iget_args *args = opaque;
  3302. return args->ino == inode->i_ino &&
  3303. args->root == BTRFS_I(inode)->root;
  3304. }
  3305. static struct inode *btrfs_iget_locked(struct super_block *s,
  3306. u64 objectid,
  3307. struct btrfs_root *root)
  3308. {
  3309. struct inode *inode;
  3310. struct btrfs_iget_args args;
  3311. args.ino = objectid;
  3312. args.root = root;
  3313. inode = iget5_locked(s, objectid, btrfs_find_actor,
  3314. btrfs_init_locked_inode,
  3315. (void *)&args);
  3316. return inode;
  3317. }
  3318. /* Get an inode object given its location and corresponding root.
  3319. * Returns in *is_new if the inode was read from disk
  3320. */
  3321. struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
  3322. struct btrfs_root *root, int *new)
  3323. {
  3324. struct inode *inode;
  3325. inode = btrfs_iget_locked(s, location->objectid, root);
  3326. if (!inode)
  3327. return ERR_PTR(-ENOMEM);
  3328. if (inode->i_state & I_NEW) {
  3329. BTRFS_I(inode)->root = root;
  3330. memcpy(&BTRFS_I(inode)->location, location, sizeof(*location));
  3331. btrfs_read_locked_inode(inode);
  3332. inode_tree_add(inode);
  3333. unlock_new_inode(inode);
  3334. if (new)
  3335. *new = 1;
  3336. }
  3337. return inode;
  3338. }
  3339. static struct inode *new_simple_dir(struct super_block *s,
  3340. struct btrfs_key *key,
  3341. struct btrfs_root *root)
  3342. {
  3343. struct inode *inode = new_inode(s);
  3344. if (!inode)
  3345. return ERR_PTR(-ENOMEM);
  3346. BTRFS_I(inode)->root = root;
  3347. memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
  3348. BTRFS_I(inode)->dummy_inode = 1;
  3349. inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
  3350. inode->i_op = &simple_dir_inode_operations;
  3351. inode->i_fop = &simple_dir_operations;
  3352. inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
  3353. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
  3354. return inode;
  3355. }
  3356. struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
  3357. {
  3358. struct inode *inode;
  3359. struct btrfs_root *root = BTRFS_I(dir)->root;
  3360. struct btrfs_root *sub_root = root;
  3361. struct btrfs_key location;
  3362. int index;
  3363. int ret;
  3364. dentry->d_op = &btrfs_dentry_operations;
  3365. if (dentry->d_name.len > BTRFS_NAME_LEN)
  3366. return ERR_PTR(-ENAMETOOLONG);
  3367. ret = btrfs_inode_by_name(dir, dentry, &location);
  3368. if (ret < 0)
  3369. return ERR_PTR(ret);
  3370. if (location.objectid == 0)
  3371. return NULL;
  3372. if (location.type == BTRFS_INODE_ITEM_KEY) {
  3373. inode = btrfs_iget(dir->i_sb, &location, root, NULL);
  3374. return inode;
  3375. }
  3376. BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
  3377. index = srcu_read_lock(&root->fs_info->subvol_srcu);
  3378. ret = fixup_tree_root_location(root, dir, dentry,
  3379. &location, &sub_root);
  3380. if (ret < 0) {
  3381. if (ret != -ENOENT)
  3382. inode = ERR_PTR(ret);
  3383. else
  3384. inode = new_simple_dir(dir->i_sb, &location, sub_root);
  3385. } else {
  3386. inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
  3387. }
  3388. srcu_read_unlock(&root->fs_info->subvol_srcu, index);
  3389. if (root != sub_root) {
  3390. down_read(&root->fs_info->cleanup_work_sem);
  3391. if (!(inode->i_sb->s_flags & MS_RDONLY))
  3392. btrfs_orphan_cleanup(sub_root);
  3393. up_read(&root->fs_info->cleanup_work_sem);
  3394. }
  3395. return inode;
  3396. }
  3397. static int btrfs_dentry_delete(struct dentry *dentry)
  3398. {
  3399. struct btrfs_root *root;
  3400. if (!dentry->d_inode && !IS_ROOT(dentry))
  3401. dentry = dentry->d_parent;
  3402. if (dentry->d_inode) {
  3403. root = BTRFS_I(dentry->d_inode)->root;
  3404. if (btrfs_root_refs(&root->root_item) == 0)
  3405. return 1;
  3406. }
  3407. return 0;
  3408. }
  3409. static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
  3410. struct nameidata *nd)
  3411. {
  3412. struct inode *inode;
  3413. inode = btrfs_lookup_dentry(dir, dentry);
  3414. if (IS_ERR(inode))
  3415. return ERR_CAST(inode);
  3416. return d_splice_alias(inode, dentry);
  3417. }
  3418. static unsigned char btrfs_filetype_table[] = {
  3419. DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
  3420. };
  3421. static int btrfs_real_readdir(struct file *filp, void *dirent,
  3422. filldir_t filldir)
  3423. {
  3424. struct inode *inode = filp->f_dentry->d_inode;
  3425. struct btrfs_root *root = BTRFS_I(inode)->root;
  3426. struct btrfs_item *item;
  3427. struct btrfs_dir_item *di;
  3428. struct btrfs_key key;
  3429. struct btrfs_key found_key;
  3430. struct btrfs_path *path;
  3431. int ret;
  3432. u32 nritems;
  3433. struct extent_buffer *leaf;
  3434. int slot;
  3435. int advance;
  3436. unsigned char d_type;
  3437. int over = 0;
  3438. u32 di_cur;
  3439. u32 di_total;
  3440. u32 di_len;
  3441. int key_type = BTRFS_DIR_INDEX_KEY;
  3442. char tmp_name[32];
  3443. char *name_ptr;
  3444. int name_len;
  3445. /* FIXME, use a real flag for deciding about the key type */
  3446. if (root->fs_info->tree_root == root)
  3447. key_type = BTRFS_DIR_ITEM_KEY;
  3448. /* special case for "." */
  3449. if (filp->f_pos == 0) {
  3450. over = filldir(dirent, ".", 1,
  3451. 1, inode->i_ino,
  3452. DT_DIR);
  3453. if (over)
  3454. return 0;
  3455. filp->f_pos = 1;
  3456. }
  3457. /* special case for .., just use the back ref */
  3458. if (filp->f_pos == 1) {
  3459. u64 pino = parent_ino(filp->f_path.dentry);
  3460. over = filldir(dirent, "..", 2,
  3461. 2, pino, DT_DIR);
  3462. if (over)
  3463. return 0;
  3464. filp->f_pos = 2;
  3465. }
  3466. path = btrfs_alloc_path();
  3467. path->reada = 2;
  3468. btrfs_set_key_type(&key, key_type);
  3469. key.offset = filp->f_pos;
  3470. key.objectid = inode->i_ino;
  3471. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  3472. if (ret < 0)
  3473. goto err;
  3474. advance = 0;
  3475. while (1) {
  3476. leaf = path->nodes[0];
  3477. nritems = btrfs_header_nritems(leaf);
  3478. slot = path->slots[0];
  3479. if (advance || slot >= nritems) {
  3480. if (slot >= nritems - 1) {
  3481. ret = btrfs_next_leaf(root, path);
  3482. if (ret)
  3483. break;
  3484. leaf = path->nodes[0];
  3485. nritems = btrfs_header_nritems(leaf);
  3486. slot = path->slots[0];
  3487. } else {
  3488. slot++;
  3489. path->slots[0]++;
  3490. }
  3491. }
  3492. advance = 1;
  3493. item = btrfs_item_nr(leaf, slot);
  3494. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  3495. if (found_key.objectid != key.objectid)
  3496. break;
  3497. if (btrfs_key_type(&found_key) != key_type)
  3498. break;
  3499. if (found_key.offset < filp->f_pos)
  3500. continue;
  3501. filp->f_pos = found_key.offset;
  3502. di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
  3503. di_cur = 0;
  3504. di_total = btrfs_item_size(leaf, item);
  3505. while (di_cur < di_total) {
  3506. struct btrfs_key location;
  3507. name_len = btrfs_dir_name_len(leaf, di);
  3508. if (name_len <= sizeof(tmp_name)) {
  3509. name_ptr = tmp_name;
  3510. } else {
  3511. name_ptr = kmalloc(name_len, GFP_NOFS);
  3512. if (!name_ptr) {
  3513. ret = -ENOMEM;
  3514. goto err;
  3515. }
  3516. }
  3517. read_extent_buffer(leaf, name_ptr,
  3518. (unsigned long)(di + 1), name_len);
  3519. d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
  3520. btrfs_dir_item_key_to_cpu(leaf, di, &location);
  3521. /* is this a reference to our own snapshot? If so
  3522. * skip it
  3523. */
  3524. if (location.type == BTRFS_ROOT_ITEM_KEY &&
  3525. location.objectid == root->root_key.objectid) {
  3526. over = 0;
  3527. goto skip;
  3528. }
  3529. over = filldir(dirent, name_ptr, name_len,
  3530. found_key.offset, location.objectid,
  3531. d_type);
  3532. skip:
  3533. if (name_ptr != tmp_name)
  3534. kfree(name_ptr);
  3535. if (over)
  3536. goto nopos;
  3537. di_len = btrfs_dir_name_len(leaf, di) +
  3538. btrfs_dir_data_len(leaf, di) + sizeof(*di);
  3539. di_cur += di_len;
  3540. di = (struct btrfs_dir_item *)((char *)di + di_len);
  3541. }
  3542. }
  3543. /* Reached end of directory/root. Bump pos past the last item. */
  3544. if (key_type == BTRFS_DIR_INDEX_KEY)
  3545. /*
  3546. * 32-bit glibc will use getdents64, but then strtol -
  3547. * so the last number we can serve is this.
  3548. */
  3549. filp->f_pos = 0x7fffffff;
  3550. else
  3551. filp->f_pos++;
  3552. nopos:
  3553. ret = 0;
  3554. err:
  3555. btrfs_free_path(path);
  3556. return ret;
  3557. }
  3558. int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
  3559. {
  3560. struct btrfs_root *root = BTRFS_I(inode)->root;
  3561. struct btrfs_trans_handle *trans;
  3562. int ret = 0;
  3563. if (BTRFS_I(inode)->dummy_inode)
  3564. return 0;
  3565. if (wbc->sync_mode == WB_SYNC_ALL) {
  3566. trans = btrfs_join_transaction(root, 1);
  3567. btrfs_set_trans_block_group(trans, inode);
  3568. ret = btrfs_commit_transaction(trans, root);
  3569. }
  3570. return ret;
  3571. }
  3572. /*
  3573. * This is somewhat expensive, updating the tree every time the
  3574. * inode changes. But, it is most likely to find the inode in cache.
  3575. * FIXME, needs more benchmarking...there are no reasons other than performance
  3576. * to keep or drop this code.
  3577. */
  3578. void btrfs_dirty_inode(struct inode *inode)
  3579. {
  3580. struct btrfs_root *root = BTRFS_I(inode)->root;
  3581. struct btrfs_trans_handle *trans;
  3582. int ret;
  3583. if (BTRFS_I(inode)->dummy_inode)
  3584. return;
  3585. trans = btrfs_join_transaction(root, 1);
  3586. btrfs_set_trans_block_group(trans, inode);
  3587. ret = btrfs_update_inode(trans, root, inode);
  3588. if (ret)
  3589. printk(KERN_ERR"btrfs: fail to dirty inode %lu error %d\n",
  3590. inode->i_ino, ret);
  3591. btrfs_end_transaction(trans, root);
  3592. }
  3593. /*
  3594. * find the highest existing sequence number in a directory
  3595. * and then set the in-memory index_cnt variable to reflect
  3596. * free sequence numbers
  3597. */
  3598. static int btrfs_set_inode_index_count(struct inode *inode)
  3599. {
  3600. struct btrfs_root *root = BTRFS_I(inode)->root;
  3601. struct btrfs_key key, found_key;
  3602. struct btrfs_path *path;
  3603. struct extent_buffer *leaf;
  3604. int ret;
  3605. key.objectid = inode->i_ino;
  3606. btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
  3607. key.offset = (u64)-1;
  3608. path = btrfs_alloc_path();
  3609. if (!path)
  3610. return -ENOMEM;
  3611. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  3612. if (ret < 0)
  3613. goto out;
  3614. /* FIXME: we should be able to handle this */
  3615. if (ret == 0)
  3616. goto out;
  3617. ret = 0;
  3618. /*
  3619. * MAGIC NUMBER EXPLANATION:
  3620. * since we search a directory based on f_pos we have to start at 2
  3621. * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
  3622. * else has to start at 2
  3623. */
  3624. if (path->slots[0] == 0) {
  3625. BTRFS_I(inode)->index_cnt = 2;
  3626. goto out;
  3627. }
  3628. path->slots[0]--;
  3629. leaf = path->nodes[0];
  3630. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  3631. if (found_key.objectid != inode->i_ino ||
  3632. btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
  3633. BTRFS_I(inode)->index_cnt = 2;
  3634. goto out;
  3635. }
  3636. BTRFS_I(inode)->index_cnt = found_key.offset + 1;
  3637. out:
  3638. btrfs_free_path(path);
  3639. return ret;
  3640. }
  3641. /*
  3642. * helper to find a free sequence number in a given directory. This current
  3643. * code is very simple, later versions will do smarter things in the btree
  3644. */
  3645. int btrfs_set_inode_index(struct inode *dir, u64 *index)
  3646. {
  3647. int ret = 0;
  3648. if (BTRFS_I(dir)->index_cnt == (u64)-1) {
  3649. ret = btrfs_set_inode_index_count(dir);
  3650. if (ret)
  3651. return ret;
  3652. }
  3653. *index = BTRFS_I(dir)->index_cnt;
  3654. BTRFS_I(dir)->index_cnt++;
  3655. return ret;
  3656. }
  3657. static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
  3658. struct btrfs_root *root,
  3659. struct inode *dir,
  3660. const char *name, int name_len,
  3661. u64 ref_objectid, u64 objectid,
  3662. u64 alloc_hint, int mode, u64 *index)
  3663. {
  3664. struct inode *inode;
  3665. struct btrfs_inode_item *inode_item;
  3666. struct btrfs_key *location;
  3667. struct btrfs_path *path;
  3668. struct btrfs_inode_ref *ref;
  3669. struct btrfs_key key[2];
  3670. u32 sizes[2];
  3671. unsigned long ptr;
  3672. int ret;
  3673. int owner;
  3674. path = btrfs_alloc_path();
  3675. BUG_ON(!path);
  3676. inode = new_inode(root->fs_info->sb);
  3677. if (!inode)
  3678. return ERR_PTR(-ENOMEM);
  3679. if (dir) {
  3680. ret = btrfs_set_inode_index(dir, index);
  3681. if (ret) {
  3682. iput(inode);
  3683. return ERR_PTR(ret);
  3684. }
  3685. }
  3686. /*
  3687. * index_cnt is ignored for everything but a dir,
  3688. * btrfs_get_inode_index_count has an explanation for the magic
  3689. * number
  3690. */
  3691. BTRFS_I(inode)->index_cnt = 2;
  3692. BTRFS_I(inode)->root = root;
  3693. BTRFS_I(inode)->generation = trans->transid;
  3694. btrfs_set_inode_space_info(root, inode);
  3695. if (mode & S_IFDIR)
  3696. owner = 0;
  3697. else
  3698. owner = 1;
  3699. BTRFS_I(inode)->block_group =
  3700. btrfs_find_block_group(root, 0, alloc_hint, owner);
  3701. key[0].objectid = objectid;
  3702. btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
  3703. key[0].offset = 0;
  3704. key[1].objectid = objectid;
  3705. btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
  3706. key[1].offset = ref_objectid;
  3707. sizes[0] = sizeof(struct btrfs_inode_item);
  3708. sizes[1] = name_len + sizeof(*ref);
  3709. path->leave_spinning = 1;
  3710. ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
  3711. if (ret != 0)
  3712. goto fail;
  3713. inode->i_uid = current_fsuid();
  3714. if (dir && (dir->i_mode & S_ISGID)) {
  3715. inode->i_gid = dir->i_gid;
  3716. if (S_ISDIR(mode))
  3717. mode |= S_ISGID;
  3718. } else
  3719. inode->i_gid = current_fsgid();
  3720. inode->i_mode = mode;
  3721. inode->i_ino = objectid;
  3722. inode_set_bytes(inode, 0);
  3723. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
  3724. inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  3725. struct btrfs_inode_item);
  3726. fill_inode_item(trans, path->nodes[0], inode_item, inode);
  3727. ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
  3728. struct btrfs_inode_ref);
  3729. btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
  3730. btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
  3731. ptr = (unsigned long)(ref + 1);
  3732. write_extent_buffer(path->nodes[0], name, ptr, name_len);
  3733. btrfs_mark_buffer_dirty(path->nodes[0]);
  3734. btrfs_free_path(path);
  3735. location = &BTRFS_I(inode)->location;
  3736. location->objectid = objectid;
  3737. location->offset = 0;
  3738. btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
  3739. btrfs_inherit_iflags(inode, dir);
  3740. if ((mode & S_IFREG)) {
  3741. if (btrfs_test_opt(root, NODATASUM))
  3742. BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
  3743. if (btrfs_test_opt(root, NODATACOW))
  3744. BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
  3745. }
  3746. insert_inode_hash(inode);
  3747. inode_tree_add(inode);
  3748. return inode;
  3749. fail:
  3750. if (dir)
  3751. BTRFS_I(dir)->index_cnt--;
  3752. btrfs_free_path(path);
  3753. iput(inode);
  3754. return ERR_PTR(ret);
  3755. }
  3756. static inline u8 btrfs_inode_type(struct inode *inode)
  3757. {
  3758. return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
  3759. }
  3760. /*
  3761. * utility function to add 'inode' into 'parent_inode' with
  3762. * a give name and a given sequence number.
  3763. * if 'add_backref' is true, also insert a backref from the
  3764. * inode to the parent directory.
  3765. */
  3766. int btrfs_add_link(struct btrfs_trans_handle *trans,
  3767. struct inode *parent_inode, struct inode *inode,
  3768. const char *name, int name_len, int add_backref, u64 index)
  3769. {
  3770. int ret = 0;
  3771. struct btrfs_key key;
  3772. struct btrfs_root *root = BTRFS_I(parent_inode)->root;
  3773. if (unlikely(inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
  3774. memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
  3775. } else {
  3776. key.objectid = inode->i_ino;
  3777. btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
  3778. key.offset = 0;
  3779. }
  3780. if (unlikely(inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
  3781. ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
  3782. key.objectid, root->root_key.objectid,
  3783. parent_inode->i_ino,
  3784. index, name, name_len);
  3785. } else if (add_backref) {
  3786. ret = btrfs_insert_inode_ref(trans, root,
  3787. name, name_len, inode->i_ino,
  3788. parent_inode->i_ino, index);
  3789. }
  3790. if (ret == 0) {
  3791. ret = btrfs_insert_dir_item(trans, root, name, name_len,
  3792. parent_inode->i_ino, &key,
  3793. btrfs_inode_type(inode), index);
  3794. BUG_ON(ret);
  3795. btrfs_i_size_write(parent_inode, parent_inode->i_size +
  3796. name_len * 2);
  3797. parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
  3798. ret = btrfs_update_inode(trans, root, parent_inode);
  3799. }
  3800. return ret;
  3801. }
  3802. static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
  3803. struct dentry *dentry, struct inode *inode,
  3804. int backref, u64 index)
  3805. {
  3806. int err = btrfs_add_link(trans, dentry->d_parent->d_inode,
  3807. inode, dentry->d_name.name,
  3808. dentry->d_name.len, backref, index);
  3809. if (!err) {
  3810. d_instantiate(dentry, inode);
  3811. return 0;
  3812. }
  3813. if (err > 0)
  3814. err = -EEXIST;
  3815. return err;
  3816. }
  3817. static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
  3818. int mode, dev_t rdev)
  3819. {
  3820. struct btrfs_trans_handle *trans;
  3821. struct btrfs_root *root = BTRFS_I(dir)->root;
  3822. struct inode *inode = NULL;
  3823. int err;
  3824. int drop_inode = 0;
  3825. u64 objectid;
  3826. unsigned long nr = 0;
  3827. u64 index = 0;
  3828. if (!new_valid_dev(rdev))
  3829. return -EINVAL;
  3830. err = btrfs_find_free_objectid(NULL, root, dir->i_ino, &objectid);
  3831. if (err)
  3832. return err;
  3833. /*
  3834. * 2 for inode item and ref
  3835. * 2 for dir items
  3836. * 1 for xattr if selinux is on
  3837. */
  3838. trans = btrfs_start_transaction(root, 5);
  3839. if (IS_ERR(trans))
  3840. return PTR_ERR(trans);
  3841. btrfs_set_trans_block_group(trans, dir);
  3842. inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
  3843. dentry->d_name.len,
  3844. dentry->d_parent->d_inode->i_ino, objectid,
  3845. BTRFS_I(dir)->block_group, mode, &index);
  3846. err = PTR_ERR(inode);
  3847. if (IS_ERR(inode))
  3848. goto out_unlock;
  3849. err = btrfs_init_inode_security(trans, inode, dir);
  3850. if (err) {
  3851. drop_inode = 1;
  3852. goto out_unlock;
  3853. }
  3854. btrfs_set_trans_block_group(trans, inode);
  3855. err = btrfs_add_nondir(trans, dentry, inode, 0, index);
  3856. if (err)
  3857. drop_inode = 1;
  3858. else {
  3859. inode->i_op = &btrfs_special_inode_operations;
  3860. init_special_inode(inode, inode->i_mode, rdev);
  3861. btrfs_update_inode(trans, root, inode);
  3862. }
  3863. btrfs_update_inode_block_group(trans, inode);
  3864. btrfs_update_inode_block_group(trans, dir);
  3865. out_unlock:
  3866. nr = trans->blocks_used;
  3867. btrfs_end_transaction_throttle(trans, root);
  3868. btrfs_btree_balance_dirty(root, nr);
  3869. if (drop_inode) {
  3870. inode_dec_link_count(inode);
  3871. iput(inode);
  3872. }
  3873. return err;
  3874. }
  3875. static int btrfs_create(struct inode *dir, struct dentry *dentry,
  3876. int mode, struct nameidata *nd)
  3877. {
  3878. struct btrfs_trans_handle *trans;
  3879. struct btrfs_root *root = BTRFS_I(dir)->root;
  3880. struct inode *inode = NULL;
  3881. int drop_inode = 0;
  3882. int err;
  3883. unsigned long nr = 0;
  3884. u64 objectid;
  3885. u64 index = 0;
  3886. err = btrfs_find_free_objectid(NULL, root, dir->i_ino, &objectid);
  3887. if (err)
  3888. return err;
  3889. /*
  3890. * 2 for inode item and ref
  3891. * 2 for dir items
  3892. * 1 for xattr if selinux is on
  3893. */
  3894. trans = btrfs_start_transaction(root, 5);
  3895. if (IS_ERR(trans))
  3896. return PTR_ERR(trans);
  3897. btrfs_set_trans_block_group(trans, dir);
  3898. inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
  3899. dentry->d_name.len,
  3900. dentry->d_parent->d_inode->i_ino,
  3901. objectid, BTRFS_I(dir)->block_group, mode,
  3902. &index);
  3903. err = PTR_ERR(inode);
  3904. if (IS_ERR(inode))
  3905. goto out_unlock;
  3906. err = btrfs_init_inode_security(trans, inode, dir);
  3907. if (err) {
  3908. drop_inode = 1;
  3909. goto out_unlock;
  3910. }
  3911. btrfs_set_trans_block_group(trans, inode);
  3912. err = btrfs_add_nondir(trans, dentry, inode, 0, index);
  3913. if (err)
  3914. drop_inode = 1;
  3915. else {
  3916. inode->i_mapping->a_ops = &btrfs_aops;
  3917. inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
  3918. inode->i_fop = &btrfs_file_operations;
  3919. inode->i_op = &btrfs_file_inode_operations;
  3920. BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
  3921. }
  3922. btrfs_update_inode_block_group(trans, inode);
  3923. btrfs_update_inode_block_group(trans, dir);
  3924. out_unlock:
  3925. nr = trans->blocks_used;
  3926. btrfs_end_transaction_throttle(trans, root);
  3927. if (drop_inode) {
  3928. inode_dec_link_count(inode);
  3929. iput(inode);
  3930. }
  3931. btrfs_btree_balance_dirty(root, nr);
  3932. return err;
  3933. }
  3934. static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
  3935. struct dentry *dentry)
  3936. {
  3937. struct btrfs_trans_handle *trans;
  3938. struct btrfs_root *root = BTRFS_I(dir)->root;
  3939. struct inode *inode = old_dentry->d_inode;
  3940. u64 index;
  3941. unsigned long nr = 0;
  3942. int err;
  3943. int drop_inode = 0;
  3944. if (inode->i_nlink == 0)
  3945. return -ENOENT;
  3946. /* do not allow sys_link's with other subvols of the same device */
  3947. if (root->objectid != BTRFS_I(inode)->root->objectid)
  3948. return -EPERM;
  3949. btrfs_inc_nlink(inode);
  3950. err = btrfs_set_inode_index(dir, &index);
  3951. if (err)
  3952. goto fail;
  3953. /*
  3954. * 1 item for inode ref
  3955. * 2 items for dir items
  3956. */
  3957. trans = btrfs_start_transaction(root, 3);
  3958. if (IS_ERR(trans)) {
  3959. err = PTR_ERR(trans);
  3960. goto fail;
  3961. }
  3962. btrfs_set_trans_block_group(trans, dir);
  3963. atomic_inc(&inode->i_count);
  3964. err = btrfs_add_nondir(trans, dentry, inode, 1, index);
  3965. if (err) {
  3966. drop_inode = 1;
  3967. } else {
  3968. btrfs_update_inode_block_group(trans, dir);
  3969. err = btrfs_update_inode(trans, root, inode);
  3970. BUG_ON(err);
  3971. btrfs_log_new_name(trans, inode, NULL, dentry->d_parent);
  3972. }
  3973. nr = trans->blocks_used;
  3974. btrfs_end_transaction_throttle(trans, root);
  3975. fail:
  3976. if (drop_inode) {
  3977. inode_dec_link_count(inode);
  3978. iput(inode);
  3979. }
  3980. btrfs_btree_balance_dirty(root, nr);
  3981. return err;
  3982. }
  3983. static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  3984. {
  3985. struct inode *inode = NULL;
  3986. struct btrfs_trans_handle *trans;
  3987. struct btrfs_root *root = BTRFS_I(dir)->root;
  3988. int err = 0;
  3989. int drop_on_err = 0;
  3990. u64 objectid = 0;
  3991. u64 index = 0;
  3992. unsigned long nr = 1;
  3993. err = btrfs_find_free_objectid(NULL, root, dir->i_ino, &objectid);
  3994. if (err)
  3995. return err;
  3996. /*
  3997. * 2 items for inode and ref
  3998. * 2 items for dir items
  3999. * 1 for xattr if selinux is on
  4000. */
  4001. trans = btrfs_start_transaction(root, 5);
  4002. if (IS_ERR(trans))
  4003. return PTR_ERR(trans);
  4004. btrfs_set_trans_block_group(trans, dir);
  4005. inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
  4006. dentry->d_name.len,
  4007. dentry->d_parent->d_inode->i_ino, objectid,
  4008. BTRFS_I(dir)->block_group, S_IFDIR | mode,
  4009. &index);
  4010. if (IS_ERR(inode)) {
  4011. err = PTR_ERR(inode);
  4012. goto out_fail;
  4013. }
  4014. drop_on_err = 1;
  4015. err = btrfs_init_inode_security(trans, inode, dir);
  4016. if (err)
  4017. goto out_fail;
  4018. inode->i_op = &btrfs_dir_inode_operations;
  4019. inode->i_fop = &btrfs_dir_file_operations;
  4020. btrfs_set_trans_block_group(trans, inode);
  4021. btrfs_i_size_write(inode, 0);
  4022. err = btrfs_update_inode(trans, root, inode);
  4023. if (err)
  4024. goto out_fail;
  4025. err = btrfs_add_link(trans, dentry->d_parent->d_inode,
  4026. inode, dentry->d_name.name,
  4027. dentry->d_name.len, 0, index);
  4028. if (err)
  4029. goto out_fail;
  4030. d_instantiate(dentry, inode);
  4031. drop_on_err = 0;
  4032. btrfs_update_inode_block_group(trans, inode);
  4033. btrfs_update_inode_block_group(trans, dir);
  4034. out_fail:
  4035. nr = trans->blocks_used;
  4036. btrfs_end_transaction_throttle(trans, root);
  4037. if (drop_on_err)
  4038. iput(inode);
  4039. btrfs_btree_balance_dirty(root, nr);
  4040. return err;
  4041. }
  4042. /* helper for btfs_get_extent. Given an existing extent in the tree,
  4043. * and an extent that you want to insert, deal with overlap and insert
  4044. * the new extent into the tree.
  4045. */
  4046. static int merge_extent_mapping(struct extent_map_tree *em_tree,
  4047. struct extent_map *existing,
  4048. struct extent_map *em,
  4049. u64 map_start, u64 map_len)
  4050. {
  4051. u64 start_diff;
  4052. BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
  4053. start_diff = map_start - em->start;
  4054. em->start = map_start;
  4055. em->len = map_len;
  4056. if (em->block_start < EXTENT_MAP_LAST_BYTE &&
  4057. !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
  4058. em->block_start += start_diff;
  4059. em->block_len -= start_diff;
  4060. }
  4061. return add_extent_mapping(em_tree, em);
  4062. }
  4063. static noinline int uncompress_inline(struct btrfs_path *path,
  4064. struct inode *inode, struct page *page,
  4065. size_t pg_offset, u64 extent_offset,
  4066. struct btrfs_file_extent_item *item)
  4067. {
  4068. int ret;
  4069. struct extent_buffer *leaf = path->nodes[0];
  4070. char *tmp;
  4071. size_t max_size;
  4072. unsigned long inline_size;
  4073. unsigned long ptr;
  4074. WARN_ON(pg_offset != 0);
  4075. max_size = btrfs_file_extent_ram_bytes(leaf, item);
  4076. inline_size = btrfs_file_extent_inline_item_len(leaf,
  4077. btrfs_item_nr(leaf, path->slots[0]));
  4078. tmp = kmalloc(inline_size, GFP_NOFS);
  4079. ptr = btrfs_file_extent_inline_start(item);
  4080. read_extent_buffer(leaf, tmp, ptr, inline_size);
  4081. max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
  4082. ret = btrfs_zlib_decompress(tmp, page, extent_offset,
  4083. inline_size, max_size);
  4084. if (ret) {
  4085. char *kaddr = kmap_atomic(page, KM_USER0);
  4086. unsigned long copy_size = min_t(u64,
  4087. PAGE_CACHE_SIZE - pg_offset,
  4088. max_size - extent_offset);
  4089. memset(kaddr + pg_offset, 0, copy_size);
  4090. kunmap_atomic(kaddr, KM_USER0);
  4091. }
  4092. kfree(tmp);
  4093. return 0;
  4094. }
  4095. /*
  4096. * a bit scary, this does extent mapping from logical file offset to the disk.
  4097. * the ugly parts come from merging extents from the disk with the in-ram
  4098. * representation. This gets more complex because of the data=ordered code,
  4099. * where the in-ram extents might be locked pending data=ordered completion.
  4100. *
  4101. * This also copies inline extents directly into the page.
  4102. */
  4103. struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
  4104. size_t pg_offset, u64 start, u64 len,
  4105. int create)
  4106. {
  4107. int ret;
  4108. int err = 0;
  4109. u64 bytenr;
  4110. u64 extent_start = 0;
  4111. u64 extent_end = 0;
  4112. u64 objectid = inode->i_ino;
  4113. u32 found_type;
  4114. struct btrfs_path *path = NULL;
  4115. struct btrfs_root *root = BTRFS_I(inode)->root;
  4116. struct btrfs_file_extent_item *item;
  4117. struct extent_buffer *leaf;
  4118. struct btrfs_key found_key;
  4119. struct extent_map *em = NULL;
  4120. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  4121. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  4122. struct btrfs_trans_handle *trans = NULL;
  4123. int compressed;
  4124. again:
  4125. read_lock(&em_tree->lock);
  4126. em = lookup_extent_mapping(em_tree, start, len);
  4127. if (em)
  4128. em->bdev = root->fs_info->fs_devices->latest_bdev;
  4129. read_unlock(&em_tree->lock);
  4130. if (em) {
  4131. if (em->start > start || em->start + em->len <= start)
  4132. free_extent_map(em);
  4133. else if (em->block_start == EXTENT_MAP_INLINE && page)
  4134. free_extent_map(em);
  4135. else
  4136. goto out;
  4137. }
  4138. em = alloc_extent_map(GFP_NOFS);
  4139. if (!em) {
  4140. err = -ENOMEM;
  4141. goto out;
  4142. }
  4143. em->bdev = root->fs_info->fs_devices->latest_bdev;
  4144. em->start = EXTENT_MAP_HOLE;
  4145. em->orig_start = EXTENT_MAP_HOLE;
  4146. em->len = (u64)-1;
  4147. em->block_len = (u64)-1;
  4148. if (!path) {
  4149. path = btrfs_alloc_path();
  4150. BUG_ON(!path);
  4151. }
  4152. ret = btrfs_lookup_file_extent(trans, root, path,
  4153. objectid, start, trans != NULL);
  4154. if (ret < 0) {
  4155. err = ret;
  4156. goto out;
  4157. }
  4158. if (ret != 0) {
  4159. if (path->slots[0] == 0)
  4160. goto not_found;
  4161. path->slots[0]--;
  4162. }
  4163. leaf = path->nodes[0];
  4164. item = btrfs_item_ptr(leaf, path->slots[0],
  4165. struct btrfs_file_extent_item);
  4166. /* are we inside the extent that was found? */
  4167. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  4168. found_type = btrfs_key_type(&found_key);
  4169. if (found_key.objectid != objectid ||
  4170. found_type != BTRFS_EXTENT_DATA_KEY) {
  4171. goto not_found;
  4172. }
  4173. found_type = btrfs_file_extent_type(leaf, item);
  4174. extent_start = found_key.offset;
  4175. compressed = btrfs_file_extent_compression(leaf, item);
  4176. if (found_type == BTRFS_FILE_EXTENT_REG ||
  4177. found_type == BTRFS_FILE_EXTENT_PREALLOC) {
  4178. extent_end = extent_start +
  4179. btrfs_file_extent_num_bytes(leaf, item);
  4180. } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
  4181. size_t size;
  4182. size = btrfs_file_extent_inline_len(leaf, item);
  4183. extent_end = (extent_start + size + root->sectorsize - 1) &
  4184. ~((u64)root->sectorsize - 1);
  4185. }
  4186. if (start >= extent_end) {
  4187. path->slots[0]++;
  4188. if (path->slots[0] >= btrfs_header_nritems(leaf)) {
  4189. ret = btrfs_next_leaf(root, path);
  4190. if (ret < 0) {
  4191. err = ret;
  4192. goto out;
  4193. }
  4194. if (ret > 0)
  4195. goto not_found;
  4196. leaf = path->nodes[0];
  4197. }
  4198. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  4199. if (found_key.objectid != objectid ||
  4200. found_key.type != BTRFS_EXTENT_DATA_KEY)
  4201. goto not_found;
  4202. if (start + len <= found_key.offset)
  4203. goto not_found;
  4204. em->start = start;
  4205. em->len = found_key.offset - start;
  4206. goto not_found_em;
  4207. }
  4208. if (found_type == BTRFS_FILE_EXTENT_REG ||
  4209. found_type == BTRFS_FILE_EXTENT_PREALLOC) {
  4210. em->start = extent_start;
  4211. em->len = extent_end - extent_start;
  4212. em->orig_start = extent_start -
  4213. btrfs_file_extent_offset(leaf, item);
  4214. bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
  4215. if (bytenr == 0) {
  4216. em->block_start = EXTENT_MAP_HOLE;
  4217. goto insert;
  4218. }
  4219. if (compressed) {
  4220. set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
  4221. em->block_start = bytenr;
  4222. em->block_len = btrfs_file_extent_disk_num_bytes(leaf,
  4223. item);
  4224. } else {
  4225. bytenr += btrfs_file_extent_offset(leaf, item);
  4226. em->block_start = bytenr;
  4227. em->block_len = em->len;
  4228. if (found_type == BTRFS_FILE_EXTENT_PREALLOC)
  4229. set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
  4230. }
  4231. goto insert;
  4232. } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
  4233. unsigned long ptr;
  4234. char *map;
  4235. size_t size;
  4236. size_t extent_offset;
  4237. size_t copy_size;
  4238. em->block_start = EXTENT_MAP_INLINE;
  4239. if (!page || create) {
  4240. em->start = extent_start;
  4241. em->len = extent_end - extent_start;
  4242. goto out;
  4243. }
  4244. size = btrfs_file_extent_inline_len(leaf, item);
  4245. extent_offset = page_offset(page) + pg_offset - extent_start;
  4246. copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
  4247. size - extent_offset);
  4248. em->start = extent_start + extent_offset;
  4249. em->len = (copy_size + root->sectorsize - 1) &
  4250. ~((u64)root->sectorsize - 1);
  4251. em->orig_start = EXTENT_MAP_INLINE;
  4252. if (compressed)
  4253. set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
  4254. ptr = btrfs_file_extent_inline_start(item) + extent_offset;
  4255. if (create == 0 && !PageUptodate(page)) {
  4256. if (btrfs_file_extent_compression(leaf, item) ==
  4257. BTRFS_COMPRESS_ZLIB) {
  4258. ret = uncompress_inline(path, inode, page,
  4259. pg_offset,
  4260. extent_offset, item);
  4261. BUG_ON(ret);
  4262. } else {
  4263. map = kmap(page);
  4264. read_extent_buffer(leaf, map + pg_offset, ptr,
  4265. copy_size);
  4266. if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
  4267. memset(map + pg_offset + copy_size, 0,
  4268. PAGE_CACHE_SIZE - pg_offset -
  4269. copy_size);
  4270. }
  4271. kunmap(page);
  4272. }
  4273. flush_dcache_page(page);
  4274. } else if (create && PageUptodate(page)) {
  4275. WARN_ON(1);
  4276. if (!trans) {
  4277. kunmap(page);
  4278. free_extent_map(em);
  4279. em = NULL;
  4280. btrfs_release_path(root, path);
  4281. trans = btrfs_join_transaction(root, 1);
  4282. goto again;
  4283. }
  4284. map = kmap(page);
  4285. write_extent_buffer(leaf, map + pg_offset, ptr,
  4286. copy_size);
  4287. kunmap(page);
  4288. btrfs_mark_buffer_dirty(leaf);
  4289. }
  4290. set_extent_uptodate(io_tree, em->start,
  4291. extent_map_end(em) - 1, GFP_NOFS);
  4292. goto insert;
  4293. } else {
  4294. printk(KERN_ERR "btrfs unknown found_type %d\n", found_type);
  4295. WARN_ON(1);
  4296. }
  4297. not_found:
  4298. em->start = start;
  4299. em->len = len;
  4300. not_found_em:
  4301. em->block_start = EXTENT_MAP_HOLE;
  4302. set_bit(EXTENT_FLAG_VACANCY, &em->flags);
  4303. insert:
  4304. btrfs_release_path(root, path);
  4305. if (em->start > start || extent_map_end(em) <= start) {
  4306. printk(KERN_ERR "Btrfs: bad extent! em: [%llu %llu] passed "
  4307. "[%llu %llu]\n", (unsigned long long)em->start,
  4308. (unsigned long long)em->len,
  4309. (unsigned long long)start,
  4310. (unsigned long long)len);
  4311. err = -EIO;
  4312. goto out;
  4313. }
  4314. err = 0;
  4315. write_lock(&em_tree->lock);
  4316. ret = add_extent_mapping(em_tree, em);
  4317. /* it is possible that someone inserted the extent into the tree
  4318. * while we had the lock dropped. It is also possible that
  4319. * an overlapping map exists in the tree
  4320. */
  4321. if (ret == -EEXIST) {
  4322. struct extent_map *existing;
  4323. ret = 0;
  4324. existing = lookup_extent_mapping(em_tree, start, len);
  4325. if (existing && (existing->start > start ||
  4326. existing->start + existing->len <= start)) {
  4327. free_extent_map(existing);
  4328. existing = NULL;
  4329. }
  4330. if (!existing) {
  4331. existing = lookup_extent_mapping(em_tree, em->start,
  4332. em->len);
  4333. if (existing) {
  4334. err = merge_extent_mapping(em_tree, existing,
  4335. em, start,
  4336. root->sectorsize);
  4337. free_extent_map(existing);
  4338. if (err) {
  4339. free_extent_map(em);
  4340. em = NULL;
  4341. }
  4342. } else {
  4343. err = -EIO;
  4344. free_extent_map(em);
  4345. em = NULL;
  4346. }
  4347. } else {
  4348. free_extent_map(em);
  4349. em = existing;
  4350. err = 0;
  4351. }
  4352. }
  4353. write_unlock(&em_tree->lock);
  4354. out:
  4355. if (path)
  4356. btrfs_free_path(path);
  4357. if (trans) {
  4358. ret = btrfs_end_transaction(trans, root);
  4359. if (!err)
  4360. err = ret;
  4361. }
  4362. if (err) {
  4363. free_extent_map(em);
  4364. return ERR_PTR(err);
  4365. }
  4366. return em;
  4367. }
  4368. static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
  4369. const struct iovec *iov, loff_t offset,
  4370. unsigned long nr_segs)
  4371. {
  4372. return -EINVAL;
  4373. }
  4374. static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
  4375. __u64 start, __u64 len)
  4376. {
  4377. return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent);
  4378. }
  4379. int btrfs_readpage(struct file *file, struct page *page)
  4380. {
  4381. struct extent_io_tree *tree;
  4382. tree = &BTRFS_I(page->mapping->host)->io_tree;
  4383. return extent_read_full_page(tree, page, btrfs_get_extent);
  4384. }
  4385. static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
  4386. {
  4387. struct extent_io_tree *tree;
  4388. if (current->flags & PF_MEMALLOC) {
  4389. redirty_page_for_writepage(wbc, page);
  4390. unlock_page(page);
  4391. return 0;
  4392. }
  4393. tree = &BTRFS_I(page->mapping->host)->io_tree;
  4394. return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
  4395. }
  4396. int btrfs_writepages(struct address_space *mapping,
  4397. struct writeback_control *wbc)
  4398. {
  4399. struct extent_io_tree *tree;
  4400. tree = &BTRFS_I(mapping->host)->io_tree;
  4401. return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
  4402. }
  4403. static int
  4404. btrfs_readpages(struct file *file, struct address_space *mapping,
  4405. struct list_head *pages, unsigned nr_pages)
  4406. {
  4407. struct extent_io_tree *tree;
  4408. tree = &BTRFS_I(mapping->host)->io_tree;
  4409. return extent_readpages(tree, mapping, pages, nr_pages,
  4410. btrfs_get_extent);
  4411. }
  4412. static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
  4413. {
  4414. struct extent_io_tree *tree;
  4415. struct extent_map_tree *map;
  4416. int ret;
  4417. tree = &BTRFS_I(page->mapping->host)->io_tree;
  4418. map = &BTRFS_I(page->mapping->host)->extent_tree;
  4419. ret = try_release_extent_mapping(map, tree, page, gfp_flags);
  4420. if (ret == 1) {
  4421. ClearPagePrivate(page);
  4422. set_page_private(page, 0);
  4423. page_cache_release(page);
  4424. }
  4425. return ret;
  4426. }
  4427. static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
  4428. {
  4429. if (PageWriteback(page) || PageDirty(page))
  4430. return 0;
  4431. return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
  4432. }
  4433. static void btrfs_invalidatepage(struct page *page, unsigned long offset)
  4434. {
  4435. struct extent_io_tree *tree;
  4436. struct btrfs_ordered_extent *ordered;
  4437. struct extent_state *cached_state = NULL;
  4438. u64 page_start = page_offset(page);
  4439. u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
  4440. /*
  4441. * we have the page locked, so new writeback can't start,
  4442. * and the dirty bit won't be cleared while we are here.
  4443. *
  4444. * Wait for IO on this page so that we can safely clear
  4445. * the PagePrivate2 bit and do ordered accounting
  4446. */
  4447. wait_on_page_writeback(page);
  4448. tree = &BTRFS_I(page->mapping->host)->io_tree;
  4449. if (offset) {
  4450. btrfs_releasepage(page, GFP_NOFS);
  4451. return;
  4452. }
  4453. lock_extent_bits(tree, page_start, page_end, 0, &cached_state,
  4454. GFP_NOFS);
  4455. ordered = btrfs_lookup_ordered_extent(page->mapping->host,
  4456. page_offset(page));
  4457. if (ordered) {
  4458. /*
  4459. * IO on this page will never be started, so we need
  4460. * to account for any ordered extents now
  4461. */
  4462. clear_extent_bit(tree, page_start, page_end,
  4463. EXTENT_DIRTY | EXTENT_DELALLOC |
  4464. EXTENT_LOCKED | EXTENT_DO_ACCOUNTING, 1, 0,
  4465. &cached_state, GFP_NOFS);
  4466. /*
  4467. * whoever cleared the private bit is responsible
  4468. * for the finish_ordered_io
  4469. */
  4470. if (TestClearPagePrivate2(page)) {
  4471. btrfs_finish_ordered_io(page->mapping->host,
  4472. page_start, page_end);
  4473. }
  4474. btrfs_put_ordered_extent(ordered);
  4475. cached_state = NULL;
  4476. lock_extent_bits(tree, page_start, page_end, 0, &cached_state,
  4477. GFP_NOFS);
  4478. }
  4479. clear_extent_bit(tree, page_start, page_end,
  4480. EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
  4481. EXTENT_DO_ACCOUNTING, 1, 1, &cached_state, GFP_NOFS);
  4482. __btrfs_releasepage(page, GFP_NOFS);
  4483. ClearPageChecked(page);
  4484. if (PagePrivate(page)) {
  4485. ClearPagePrivate(page);
  4486. set_page_private(page, 0);
  4487. page_cache_release(page);
  4488. }
  4489. }
  4490. /*
  4491. * btrfs_page_mkwrite() is not allowed to change the file size as it gets
  4492. * called from a page fault handler when a page is first dirtied. Hence we must
  4493. * be careful to check for EOF conditions here. We set the page up correctly
  4494. * for a written page which means we get ENOSPC checking when writing into
  4495. * holes and correct delalloc and unwritten extent mapping on filesystems that
  4496. * support these features.
  4497. *
  4498. * We are not allowed to take the i_mutex here so we have to play games to
  4499. * protect against truncate races as the page could now be beyond EOF. Because
  4500. * vmtruncate() writes the inode size before removing pages, once we have the
  4501. * page lock we can determine safely if the page is beyond EOF. If it is not
  4502. * beyond EOF, then the page is guaranteed safe against truncation until we
  4503. * unlock the page.
  4504. */
  4505. int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
  4506. {
  4507. struct page *page = vmf->page;
  4508. struct inode *inode = fdentry(vma->vm_file)->d_inode;
  4509. struct btrfs_root *root = BTRFS_I(inode)->root;
  4510. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  4511. struct btrfs_ordered_extent *ordered;
  4512. struct extent_state *cached_state = NULL;
  4513. char *kaddr;
  4514. unsigned long zero_start;
  4515. loff_t size;
  4516. int ret;
  4517. u64 page_start;
  4518. u64 page_end;
  4519. ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
  4520. if (ret) {
  4521. if (ret == -ENOMEM)
  4522. ret = VM_FAULT_OOM;
  4523. else /* -ENOSPC, -EIO, etc */
  4524. ret = VM_FAULT_SIGBUS;
  4525. goto out;
  4526. }
  4527. ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
  4528. again:
  4529. lock_page(page);
  4530. size = i_size_read(inode);
  4531. page_start = page_offset(page);
  4532. page_end = page_start + PAGE_CACHE_SIZE - 1;
  4533. if ((page->mapping != inode->i_mapping) ||
  4534. (page_start >= size)) {
  4535. /* page got truncated out from underneath us */
  4536. goto out_unlock;
  4537. }
  4538. wait_on_page_writeback(page);
  4539. lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state,
  4540. GFP_NOFS);
  4541. set_page_extent_mapped(page);
  4542. /*
  4543. * we can't set the delalloc bits if there are pending ordered
  4544. * extents. Drop our locks and wait for them to finish
  4545. */
  4546. ordered = btrfs_lookup_ordered_extent(inode, page_start);
  4547. if (ordered) {
  4548. unlock_extent_cached(io_tree, page_start, page_end,
  4549. &cached_state, GFP_NOFS);
  4550. unlock_page(page);
  4551. btrfs_start_ordered_extent(inode, ordered, 1);
  4552. btrfs_put_ordered_extent(ordered);
  4553. goto again;
  4554. }
  4555. /*
  4556. * XXX - page_mkwrite gets called every time the page is dirtied, even
  4557. * if it was already dirty, so for space accounting reasons we need to
  4558. * clear any delalloc bits for the range we are fixing to save. There
  4559. * is probably a better way to do this, but for now keep consistent with
  4560. * prepare_pages in the normal write path.
  4561. */
  4562. clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
  4563. EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING,
  4564. 0, 0, &cached_state, GFP_NOFS);
  4565. ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
  4566. &cached_state);
  4567. if (ret) {
  4568. unlock_extent_cached(io_tree, page_start, page_end,
  4569. &cached_state, GFP_NOFS);
  4570. ret = VM_FAULT_SIGBUS;
  4571. goto out_unlock;
  4572. }
  4573. ret = 0;
  4574. /* page is wholly or partially inside EOF */
  4575. if (page_start + PAGE_CACHE_SIZE > size)
  4576. zero_start = size & ~PAGE_CACHE_MASK;
  4577. else
  4578. zero_start = PAGE_CACHE_SIZE;
  4579. if (zero_start != PAGE_CACHE_SIZE) {
  4580. kaddr = kmap(page);
  4581. memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
  4582. flush_dcache_page(page);
  4583. kunmap(page);
  4584. }
  4585. ClearPageChecked(page);
  4586. set_page_dirty(page);
  4587. SetPageUptodate(page);
  4588. BTRFS_I(inode)->last_trans = root->fs_info->generation;
  4589. BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
  4590. unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
  4591. out_unlock:
  4592. if (!ret)
  4593. return VM_FAULT_LOCKED;
  4594. unlock_page(page);
  4595. btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
  4596. out:
  4597. return ret;
  4598. }
  4599. static void btrfs_truncate(struct inode *inode)
  4600. {
  4601. struct btrfs_root *root = BTRFS_I(inode)->root;
  4602. int ret;
  4603. struct btrfs_trans_handle *trans;
  4604. unsigned long nr;
  4605. u64 mask = root->sectorsize - 1;
  4606. if (!S_ISREG(inode->i_mode)) {
  4607. WARN_ON(1);
  4608. return;
  4609. }
  4610. ret = btrfs_truncate_page(inode->i_mapping, inode->i_size);
  4611. if (ret)
  4612. return;
  4613. btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
  4614. btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
  4615. trans = btrfs_start_transaction(root, 1);
  4616. btrfs_set_trans_block_group(trans, inode);
  4617. /*
  4618. * setattr is responsible for setting the ordered_data_close flag,
  4619. * but that is only tested during the last file release. That
  4620. * could happen well after the next commit, leaving a great big
  4621. * window where new writes may get lost if someone chooses to write
  4622. * to this file after truncating to zero
  4623. *
  4624. * The inode doesn't have any dirty data here, and so if we commit
  4625. * this is a noop. If someone immediately starts writing to the inode
  4626. * it is very likely we'll catch some of their writes in this
  4627. * transaction, and the commit will find this file on the ordered
  4628. * data list with good things to send down.
  4629. *
  4630. * This is a best effort solution, there is still a window where
  4631. * using truncate to replace the contents of the file will
  4632. * end up with a zero length file after a crash.
  4633. */
  4634. if (inode->i_size == 0 && BTRFS_I(inode)->ordered_data_close)
  4635. btrfs_add_ordered_operation(trans, root, inode);
  4636. while (1) {
  4637. ret = btrfs_truncate_inode_items(trans, root, inode,
  4638. inode->i_size,
  4639. BTRFS_EXTENT_DATA_KEY);
  4640. if (ret != -EAGAIN)
  4641. break;
  4642. ret = btrfs_update_inode(trans, root, inode);
  4643. BUG_ON(ret);
  4644. nr = trans->blocks_used;
  4645. btrfs_end_transaction(trans, root);
  4646. btrfs_btree_balance_dirty(root, nr);
  4647. trans = btrfs_start_transaction(root, 1);
  4648. btrfs_set_trans_block_group(trans, inode);
  4649. }
  4650. if (ret == 0 && inode->i_nlink > 0) {
  4651. ret = btrfs_orphan_del(trans, inode);
  4652. BUG_ON(ret);
  4653. }
  4654. ret = btrfs_update_inode(trans, root, inode);
  4655. BUG_ON(ret);
  4656. nr = trans->blocks_used;
  4657. ret = btrfs_end_transaction_throttle(trans, root);
  4658. BUG_ON(ret);
  4659. btrfs_btree_balance_dirty(root, nr);
  4660. }
  4661. /*
  4662. * create a new subvolume directory/inode (helper for the ioctl).
  4663. */
  4664. int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
  4665. struct btrfs_root *new_root,
  4666. u64 new_dirid, u64 alloc_hint)
  4667. {
  4668. struct inode *inode;
  4669. int err;
  4670. u64 index = 0;
  4671. inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, new_dirid,
  4672. new_dirid, alloc_hint, S_IFDIR | 0700, &index);
  4673. if (IS_ERR(inode))
  4674. return PTR_ERR(inode);
  4675. inode->i_op = &btrfs_dir_inode_operations;
  4676. inode->i_fop = &btrfs_dir_file_operations;
  4677. inode->i_nlink = 1;
  4678. btrfs_i_size_write(inode, 0);
  4679. err = btrfs_update_inode(trans, new_root, inode);
  4680. BUG_ON(err);
  4681. iput(inode);
  4682. return 0;
  4683. }
  4684. /* helper function for file defrag and space balancing. This
  4685. * forces readahead on a given range of bytes in an inode
  4686. */
  4687. unsigned long btrfs_force_ra(struct address_space *mapping,
  4688. struct file_ra_state *ra, struct file *file,
  4689. pgoff_t offset, pgoff_t last_index)
  4690. {
  4691. pgoff_t req_size = last_index - offset + 1;
  4692. page_cache_sync_readahead(mapping, ra, file, offset, req_size);
  4693. return offset + req_size;
  4694. }
  4695. struct inode *btrfs_alloc_inode(struct super_block *sb)
  4696. {
  4697. struct btrfs_inode *ei;
  4698. struct inode *inode;
  4699. ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
  4700. if (!ei)
  4701. return NULL;
  4702. ei->root = NULL;
  4703. ei->space_info = NULL;
  4704. ei->generation = 0;
  4705. ei->sequence = 0;
  4706. ei->last_trans = 0;
  4707. ei->last_sub_trans = 0;
  4708. ei->logged_trans = 0;
  4709. ei->delalloc_bytes = 0;
  4710. ei->reserved_bytes = 0;
  4711. ei->disk_i_size = 0;
  4712. ei->flags = 0;
  4713. ei->index_cnt = (u64)-1;
  4714. ei->last_unlink_trans = 0;
  4715. spin_lock_init(&ei->accounting_lock);
  4716. atomic_set(&ei->outstanding_extents, 0);
  4717. ei->reserved_extents = 0;
  4718. ei->ordered_data_close = 0;
  4719. ei->dummy_inode = 0;
  4720. ei->force_compress = 0;
  4721. inode = &ei->vfs_inode;
  4722. extent_map_tree_init(&ei->extent_tree, GFP_NOFS);
  4723. extent_io_tree_init(&ei->io_tree, &inode->i_data, GFP_NOFS);
  4724. extent_io_tree_init(&ei->io_failure_tree, &inode->i_data, GFP_NOFS);
  4725. mutex_init(&ei->log_mutex);
  4726. btrfs_ordered_inode_tree_init(&ei->ordered_tree);
  4727. INIT_LIST_HEAD(&ei->i_orphan);
  4728. INIT_LIST_HEAD(&ei->delalloc_inodes);
  4729. INIT_LIST_HEAD(&ei->ordered_operations);
  4730. RB_CLEAR_NODE(&ei->rb_node);
  4731. return inode;
  4732. }
  4733. void btrfs_destroy_inode(struct inode *inode)
  4734. {
  4735. struct btrfs_ordered_extent *ordered;
  4736. struct btrfs_root *root = BTRFS_I(inode)->root;
  4737. WARN_ON(!list_empty(&inode->i_dentry));
  4738. WARN_ON(inode->i_data.nrpages);
  4739. WARN_ON(atomic_read(&BTRFS_I(inode)->outstanding_extents));
  4740. WARN_ON(BTRFS_I(inode)->reserved_extents);
  4741. /*
  4742. * This can happen where we create an inode, but somebody else also
  4743. * created the same inode and we need to destroy the one we already
  4744. * created.
  4745. */
  4746. if (!root)
  4747. goto free;
  4748. /*
  4749. * Make sure we're properly removed from the ordered operation
  4750. * lists.
  4751. */
  4752. smp_mb();
  4753. if (!list_empty(&BTRFS_I(inode)->ordered_operations)) {
  4754. spin_lock(&root->fs_info->ordered_extent_lock);
  4755. list_del_init(&BTRFS_I(inode)->ordered_operations);
  4756. spin_unlock(&root->fs_info->ordered_extent_lock);
  4757. }
  4758. spin_lock(&root->list_lock);
  4759. if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
  4760. printk(KERN_INFO "BTRFS: inode %lu still on the orphan list\n",
  4761. inode->i_ino);
  4762. list_del_init(&BTRFS_I(inode)->i_orphan);
  4763. }
  4764. spin_unlock(&root->list_lock);
  4765. while (1) {
  4766. ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
  4767. if (!ordered)
  4768. break;
  4769. else {
  4770. printk(KERN_ERR "btrfs found ordered "
  4771. "extent %llu %llu on inode cleanup\n",
  4772. (unsigned long long)ordered->file_offset,
  4773. (unsigned long long)ordered->len);
  4774. btrfs_remove_ordered_extent(inode, ordered);
  4775. btrfs_put_ordered_extent(ordered);
  4776. btrfs_put_ordered_extent(ordered);
  4777. }
  4778. }
  4779. inode_tree_del(inode);
  4780. btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
  4781. free:
  4782. kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
  4783. }
  4784. void btrfs_drop_inode(struct inode *inode)
  4785. {
  4786. struct btrfs_root *root = BTRFS_I(inode)->root;
  4787. if (inode->i_nlink > 0 && btrfs_root_refs(&root->root_item) == 0)
  4788. generic_delete_inode(inode);
  4789. else
  4790. generic_drop_inode(inode);
  4791. }
  4792. static void init_once(void *foo)
  4793. {
  4794. struct btrfs_inode *ei = (struct btrfs_inode *) foo;
  4795. inode_init_once(&ei->vfs_inode);
  4796. }
  4797. void btrfs_destroy_cachep(void)
  4798. {
  4799. if (btrfs_inode_cachep)
  4800. kmem_cache_destroy(btrfs_inode_cachep);
  4801. if (btrfs_trans_handle_cachep)
  4802. kmem_cache_destroy(btrfs_trans_handle_cachep);
  4803. if (btrfs_transaction_cachep)
  4804. kmem_cache_destroy(btrfs_transaction_cachep);
  4805. if (btrfs_path_cachep)
  4806. kmem_cache_destroy(btrfs_path_cachep);
  4807. }
  4808. int btrfs_init_cachep(void)
  4809. {
  4810. btrfs_inode_cachep = kmem_cache_create("btrfs_inode_cache",
  4811. sizeof(struct btrfs_inode), 0,
  4812. SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
  4813. if (!btrfs_inode_cachep)
  4814. goto fail;
  4815. btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle_cache",
  4816. sizeof(struct btrfs_trans_handle), 0,
  4817. SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
  4818. if (!btrfs_trans_handle_cachep)
  4819. goto fail;
  4820. btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction_cache",
  4821. sizeof(struct btrfs_transaction), 0,
  4822. SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
  4823. if (!btrfs_transaction_cachep)
  4824. goto fail;
  4825. btrfs_path_cachep = kmem_cache_create("btrfs_path_cache",
  4826. sizeof(struct btrfs_path), 0,
  4827. SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
  4828. if (!btrfs_path_cachep)
  4829. goto fail;
  4830. return 0;
  4831. fail:
  4832. btrfs_destroy_cachep();
  4833. return -ENOMEM;
  4834. }
  4835. static int btrfs_getattr(struct vfsmount *mnt,
  4836. struct dentry *dentry, struct kstat *stat)
  4837. {
  4838. struct inode *inode = dentry->d_inode;
  4839. generic_fillattr(inode, stat);
  4840. stat->dev = BTRFS_I(inode)->root->anon_super.s_dev;
  4841. stat->blksize = PAGE_CACHE_SIZE;
  4842. stat->blocks = (inode_get_bytes(inode) +
  4843. BTRFS_I(inode)->delalloc_bytes) >> 9;
  4844. return 0;
  4845. }
  4846. static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
  4847. struct inode *new_dir, struct dentry *new_dentry)
  4848. {
  4849. struct btrfs_trans_handle *trans;
  4850. struct btrfs_root *root = BTRFS_I(old_dir)->root;
  4851. struct btrfs_root *dest = BTRFS_I(new_dir)->root;
  4852. struct inode *new_inode = new_dentry->d_inode;
  4853. struct inode *old_inode = old_dentry->d_inode;
  4854. struct timespec ctime = CURRENT_TIME;
  4855. u64 index = 0;
  4856. u64 root_objectid;
  4857. int ret;
  4858. if (new_dir->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
  4859. return -EPERM;
  4860. /* we only allow rename subvolume link between subvolumes */
  4861. if (old_inode->i_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
  4862. return -EXDEV;
  4863. if (old_inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
  4864. (new_inode && new_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID))
  4865. return -ENOTEMPTY;
  4866. if (S_ISDIR(old_inode->i_mode) && new_inode &&
  4867. new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
  4868. return -ENOTEMPTY;
  4869. /*
  4870. * we're using rename to replace one file with another.
  4871. * and the replacement file is large. Start IO on it now so
  4872. * we don't add too much work to the end of the transaction
  4873. */
  4874. if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size &&
  4875. old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
  4876. filemap_flush(old_inode->i_mapping);
  4877. /* close the racy window with snapshot create/destroy ioctl */
  4878. if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
  4879. down_read(&root->fs_info->subvol_sem);
  4880. /*
  4881. * We want to reserve the absolute worst case amount of items. So if
  4882. * both inodes are subvols and we need to unlink them then that would
  4883. * require 4 item modifications, but if they are both normal inodes it
  4884. * would require 5 item modifications, so we'll assume their normal
  4885. * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
  4886. * should cover the worst case number of items we'll modify.
  4887. */
  4888. trans = btrfs_start_transaction(root, 20);
  4889. if (IS_ERR(trans))
  4890. return PTR_ERR(trans);
  4891. btrfs_set_trans_block_group(trans, new_dir);
  4892. if (dest != root)
  4893. btrfs_record_root_in_trans(trans, dest);
  4894. ret = btrfs_set_inode_index(new_dir, &index);
  4895. if (ret)
  4896. goto out_fail;
  4897. if (unlikely(old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
  4898. /* force full log commit if subvolume involved. */
  4899. root->fs_info->last_trans_log_full_commit = trans->transid;
  4900. } else {
  4901. ret = btrfs_insert_inode_ref(trans, dest,
  4902. new_dentry->d_name.name,
  4903. new_dentry->d_name.len,
  4904. old_inode->i_ino,
  4905. new_dir->i_ino, index);
  4906. if (ret)
  4907. goto out_fail;
  4908. /*
  4909. * this is an ugly little race, but the rename is required
  4910. * to make sure that if we crash, the inode is either at the
  4911. * old name or the new one. pinning the log transaction lets
  4912. * us make sure we don't allow a log commit to come in after
  4913. * we unlink the name but before we add the new name back in.
  4914. */
  4915. btrfs_pin_log_trans(root);
  4916. }
  4917. /*
  4918. * make sure the inode gets flushed if it is replacing
  4919. * something.
  4920. */
  4921. if (new_inode && new_inode->i_size &&
  4922. old_inode && S_ISREG(old_inode->i_mode)) {
  4923. btrfs_add_ordered_operation(trans, root, old_inode);
  4924. }
  4925. old_dir->i_ctime = old_dir->i_mtime = ctime;
  4926. new_dir->i_ctime = new_dir->i_mtime = ctime;
  4927. old_inode->i_ctime = ctime;
  4928. if (old_dentry->d_parent != new_dentry->d_parent)
  4929. btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
  4930. if (unlikely(old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
  4931. root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
  4932. ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
  4933. old_dentry->d_name.name,
  4934. old_dentry->d_name.len);
  4935. } else {
  4936. btrfs_inc_nlink(old_dentry->d_inode);
  4937. ret = btrfs_unlink_inode(trans, root, old_dir,
  4938. old_dentry->d_inode,
  4939. old_dentry->d_name.name,
  4940. old_dentry->d_name.len);
  4941. }
  4942. BUG_ON(ret);
  4943. if (new_inode) {
  4944. new_inode->i_ctime = CURRENT_TIME;
  4945. if (unlikely(new_inode->i_ino ==
  4946. BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
  4947. root_objectid = BTRFS_I(new_inode)->location.objectid;
  4948. ret = btrfs_unlink_subvol(trans, dest, new_dir,
  4949. root_objectid,
  4950. new_dentry->d_name.name,
  4951. new_dentry->d_name.len);
  4952. BUG_ON(new_inode->i_nlink == 0);
  4953. } else {
  4954. ret = btrfs_unlink_inode(trans, dest, new_dir,
  4955. new_dentry->d_inode,
  4956. new_dentry->d_name.name,
  4957. new_dentry->d_name.len);
  4958. }
  4959. BUG_ON(ret);
  4960. if (new_inode->i_nlink == 0) {
  4961. ret = btrfs_orphan_add(trans, new_dentry->d_inode);
  4962. BUG_ON(ret);
  4963. }
  4964. }
  4965. ret = btrfs_add_link(trans, new_dir, old_inode,
  4966. new_dentry->d_name.name,
  4967. new_dentry->d_name.len, 0, index);
  4968. BUG_ON(ret);
  4969. if (old_inode->i_ino != BTRFS_FIRST_FREE_OBJECTID) {
  4970. btrfs_log_new_name(trans, old_inode, old_dir,
  4971. new_dentry->d_parent);
  4972. btrfs_end_log_trans(root);
  4973. }
  4974. out_fail:
  4975. btrfs_end_transaction_throttle(trans, root);
  4976. if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
  4977. up_read(&root->fs_info->subvol_sem);
  4978. return ret;
  4979. }
  4980. /*
  4981. * some fairly slow code that needs optimization. This walks the list
  4982. * of all the inodes with pending delalloc and forces them to disk.
  4983. */
  4984. int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
  4985. {
  4986. struct list_head *head = &root->fs_info->delalloc_inodes;
  4987. struct btrfs_inode *binode;
  4988. struct inode *inode;
  4989. if (root->fs_info->sb->s_flags & MS_RDONLY)
  4990. return -EROFS;
  4991. spin_lock(&root->fs_info->delalloc_lock);
  4992. while (!list_empty(head)) {
  4993. binode = list_entry(head->next, struct btrfs_inode,
  4994. delalloc_inodes);
  4995. inode = igrab(&binode->vfs_inode);
  4996. if (!inode)
  4997. list_del_init(&binode->delalloc_inodes);
  4998. spin_unlock(&root->fs_info->delalloc_lock);
  4999. if (inode) {
  5000. filemap_flush(inode->i_mapping);
  5001. if (delay_iput)
  5002. btrfs_add_delayed_iput(inode);
  5003. else
  5004. iput(inode);
  5005. }
  5006. cond_resched();
  5007. spin_lock(&root->fs_info->delalloc_lock);
  5008. }
  5009. spin_unlock(&root->fs_info->delalloc_lock);
  5010. /* the filemap_flush will queue IO into the worker threads, but
  5011. * we have to make sure the IO is actually started and that
  5012. * ordered extents get created before we return
  5013. */
  5014. atomic_inc(&root->fs_info->async_submit_draining);
  5015. while (atomic_read(&root->fs_info->nr_async_submits) ||
  5016. atomic_read(&root->fs_info->async_delalloc_pages)) {
  5017. wait_event(root->fs_info->async_submit_wait,
  5018. (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
  5019. atomic_read(&root->fs_info->async_delalloc_pages) == 0));
  5020. }
  5021. atomic_dec(&root->fs_info->async_submit_draining);
  5022. return 0;
  5023. }
  5024. int btrfs_start_one_delalloc_inode(struct btrfs_root *root, int delay_iput)
  5025. {
  5026. struct btrfs_inode *binode;
  5027. struct inode *inode = NULL;
  5028. spin_lock(&root->fs_info->delalloc_lock);
  5029. while (!list_empty(&root->fs_info->delalloc_inodes)) {
  5030. binode = list_entry(root->fs_info->delalloc_inodes.next,
  5031. struct btrfs_inode, delalloc_inodes);
  5032. inode = igrab(&binode->vfs_inode);
  5033. if (inode) {
  5034. list_move_tail(&binode->delalloc_inodes,
  5035. &root->fs_info->delalloc_inodes);
  5036. break;
  5037. }
  5038. list_del_init(&binode->delalloc_inodes);
  5039. cond_resched_lock(&root->fs_info->delalloc_lock);
  5040. }
  5041. spin_unlock(&root->fs_info->delalloc_lock);
  5042. if (inode) {
  5043. write_inode_now(inode, 0);
  5044. if (delay_iput)
  5045. btrfs_add_delayed_iput(inode);
  5046. else
  5047. iput(inode);
  5048. return 1;
  5049. }
  5050. return 0;
  5051. }
  5052. static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
  5053. const char *symname)
  5054. {
  5055. struct btrfs_trans_handle *trans;
  5056. struct btrfs_root *root = BTRFS_I(dir)->root;
  5057. struct btrfs_path *path;
  5058. struct btrfs_key key;
  5059. struct inode *inode = NULL;
  5060. int err;
  5061. int drop_inode = 0;
  5062. u64 objectid;
  5063. u64 index = 0 ;
  5064. int name_len;
  5065. int datasize;
  5066. unsigned long ptr;
  5067. struct btrfs_file_extent_item *ei;
  5068. struct extent_buffer *leaf;
  5069. unsigned long nr = 0;
  5070. name_len = strlen(symname) + 1;
  5071. if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
  5072. return -ENAMETOOLONG;
  5073. err = btrfs_find_free_objectid(NULL, root, dir->i_ino, &objectid);
  5074. if (err)
  5075. return err;
  5076. /*
  5077. * 2 items for inode item and ref
  5078. * 2 items for dir items
  5079. * 1 item for xattr if selinux is on
  5080. */
  5081. trans = btrfs_start_transaction(root, 5);
  5082. if (IS_ERR(trans))
  5083. return PTR_ERR(trans);
  5084. btrfs_set_trans_block_group(trans, dir);
  5085. inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
  5086. dentry->d_name.len,
  5087. dentry->d_parent->d_inode->i_ino, objectid,
  5088. BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO,
  5089. &index);
  5090. err = PTR_ERR(inode);
  5091. if (IS_ERR(inode))
  5092. goto out_unlock;
  5093. err = btrfs_init_inode_security(trans, inode, dir);
  5094. if (err) {
  5095. drop_inode = 1;
  5096. goto out_unlock;
  5097. }
  5098. btrfs_set_trans_block_group(trans, inode);
  5099. err = btrfs_add_nondir(trans, dentry, inode, 0, index);
  5100. if (err)
  5101. drop_inode = 1;
  5102. else {
  5103. inode->i_mapping->a_ops = &btrfs_aops;
  5104. inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
  5105. inode->i_fop = &btrfs_file_operations;
  5106. inode->i_op = &btrfs_file_inode_operations;
  5107. BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
  5108. }
  5109. btrfs_update_inode_block_group(trans, inode);
  5110. btrfs_update_inode_block_group(trans, dir);
  5111. if (drop_inode)
  5112. goto out_unlock;
  5113. path = btrfs_alloc_path();
  5114. BUG_ON(!path);
  5115. key.objectid = inode->i_ino;
  5116. key.offset = 0;
  5117. btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
  5118. datasize = btrfs_file_extent_calc_inline_size(name_len);
  5119. err = btrfs_insert_empty_item(trans, root, path, &key,
  5120. datasize);
  5121. if (err) {
  5122. drop_inode = 1;
  5123. goto out_unlock;
  5124. }
  5125. leaf = path->nodes[0];
  5126. ei = btrfs_item_ptr(leaf, path->slots[0],
  5127. struct btrfs_file_extent_item);
  5128. btrfs_set_file_extent_generation(leaf, ei, trans->transid);
  5129. btrfs_set_file_extent_type(leaf, ei,
  5130. BTRFS_FILE_EXTENT_INLINE);
  5131. btrfs_set_file_extent_encryption(leaf, ei, 0);
  5132. btrfs_set_file_extent_compression(leaf, ei, 0);
  5133. btrfs_set_file_extent_other_encoding(leaf, ei, 0);
  5134. btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
  5135. ptr = btrfs_file_extent_inline_start(ei);
  5136. write_extent_buffer(leaf, symname, ptr, name_len);
  5137. btrfs_mark_buffer_dirty(leaf);
  5138. btrfs_free_path(path);
  5139. inode->i_op = &btrfs_symlink_inode_operations;
  5140. inode->i_mapping->a_ops = &btrfs_symlink_aops;
  5141. inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
  5142. inode_set_bytes(inode, name_len);
  5143. btrfs_i_size_write(inode, name_len - 1);
  5144. err = btrfs_update_inode(trans, root, inode);
  5145. if (err)
  5146. drop_inode = 1;
  5147. out_unlock:
  5148. nr = trans->blocks_used;
  5149. btrfs_end_transaction_throttle(trans, root);
  5150. if (drop_inode) {
  5151. inode_dec_link_count(inode);
  5152. iput(inode);
  5153. }
  5154. btrfs_btree_balance_dirty(root, nr);
  5155. return err;
  5156. }
  5157. static int prealloc_file_range(struct inode *inode, u64 start, u64 end,
  5158. u64 alloc_hint, int mode, loff_t actual_len)
  5159. {
  5160. struct btrfs_trans_handle *trans;
  5161. struct btrfs_root *root = BTRFS_I(inode)->root;
  5162. struct btrfs_key ins;
  5163. u64 cur_offset = start;
  5164. u64 num_bytes = end - start;
  5165. int ret = 0;
  5166. u64 i_size;
  5167. while (num_bytes > 0) {
  5168. trans = btrfs_start_transaction(root, 3);
  5169. if (IS_ERR(trans)) {
  5170. ret = PTR_ERR(trans);
  5171. break;
  5172. }
  5173. ret = btrfs_reserve_extent(trans, root, num_bytes,
  5174. root->sectorsize, 0, alloc_hint,
  5175. (u64)-1, &ins, 1);
  5176. if (ret) {
  5177. btrfs_end_transaction(trans, root);
  5178. break;
  5179. }
  5180. ret = insert_reserved_file_extent(trans, inode,
  5181. cur_offset, ins.objectid,
  5182. ins.offset, ins.offset,
  5183. ins.offset, 0, 0, 0,
  5184. BTRFS_FILE_EXTENT_PREALLOC);
  5185. BUG_ON(ret);
  5186. btrfs_drop_extent_cache(inode, cur_offset,
  5187. cur_offset + ins.offset -1, 0);
  5188. num_bytes -= ins.offset;
  5189. cur_offset += ins.offset;
  5190. alloc_hint = ins.objectid + ins.offset;
  5191. inode->i_ctime = CURRENT_TIME;
  5192. BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
  5193. if (!(mode & FALLOC_FL_KEEP_SIZE) &&
  5194. (actual_len > inode->i_size) &&
  5195. (cur_offset > inode->i_size)) {
  5196. if (cur_offset > actual_len)
  5197. i_size = actual_len;
  5198. else
  5199. i_size = cur_offset;
  5200. i_size_write(inode, i_size);
  5201. btrfs_ordered_update_i_size(inode, i_size, NULL);
  5202. }
  5203. ret = btrfs_update_inode(trans, root, inode);
  5204. BUG_ON(ret);
  5205. btrfs_end_transaction(trans, root);
  5206. }
  5207. return ret;
  5208. }
  5209. static long btrfs_fallocate(struct inode *inode, int mode,
  5210. loff_t offset, loff_t len)
  5211. {
  5212. struct extent_state *cached_state = NULL;
  5213. u64 cur_offset;
  5214. u64 last_byte;
  5215. u64 alloc_start;
  5216. u64 alloc_end;
  5217. u64 alloc_hint = 0;
  5218. u64 locked_end;
  5219. u64 mask = BTRFS_I(inode)->root->sectorsize - 1;
  5220. struct extent_map *em;
  5221. int ret;
  5222. alloc_start = offset & ~mask;
  5223. alloc_end = (offset + len + mask) & ~mask;
  5224. /*
  5225. * wait for ordered IO before we have any locks. We'll loop again
  5226. * below with the locks held.
  5227. */
  5228. btrfs_wait_ordered_range(inode, alloc_start, alloc_end - alloc_start);
  5229. mutex_lock(&inode->i_mutex);
  5230. if (alloc_start > inode->i_size) {
  5231. ret = btrfs_cont_expand(inode, alloc_start);
  5232. if (ret)
  5233. goto out;
  5234. }
  5235. ret = btrfs_check_data_free_space(inode, alloc_end - alloc_start);
  5236. if (ret)
  5237. goto out;
  5238. locked_end = alloc_end - 1;
  5239. while (1) {
  5240. struct btrfs_ordered_extent *ordered;
  5241. /* the extent lock is ordered inside the running
  5242. * transaction
  5243. */
  5244. lock_extent_bits(&BTRFS_I(inode)->io_tree, alloc_start,
  5245. locked_end, 0, &cached_state, GFP_NOFS);
  5246. ordered = btrfs_lookup_first_ordered_extent(inode,
  5247. alloc_end - 1);
  5248. if (ordered &&
  5249. ordered->file_offset + ordered->len > alloc_start &&
  5250. ordered->file_offset < alloc_end) {
  5251. btrfs_put_ordered_extent(ordered);
  5252. unlock_extent_cached(&BTRFS_I(inode)->io_tree,
  5253. alloc_start, locked_end,
  5254. &cached_state, GFP_NOFS);
  5255. /*
  5256. * we can't wait on the range with the transaction
  5257. * running or with the extent lock held
  5258. */
  5259. btrfs_wait_ordered_range(inode, alloc_start,
  5260. alloc_end - alloc_start);
  5261. } else {
  5262. if (ordered)
  5263. btrfs_put_ordered_extent(ordered);
  5264. break;
  5265. }
  5266. }
  5267. cur_offset = alloc_start;
  5268. while (1) {
  5269. em = btrfs_get_extent(inode, NULL, 0, cur_offset,
  5270. alloc_end - cur_offset, 0);
  5271. BUG_ON(IS_ERR(em) || !em);
  5272. last_byte = min(extent_map_end(em), alloc_end);
  5273. last_byte = (last_byte + mask) & ~mask;
  5274. if (em->block_start == EXTENT_MAP_HOLE ||
  5275. (cur_offset >= inode->i_size &&
  5276. !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
  5277. ret = prealloc_file_range(inode,
  5278. cur_offset, last_byte,
  5279. alloc_hint, mode, offset+len);
  5280. if (ret < 0) {
  5281. free_extent_map(em);
  5282. break;
  5283. }
  5284. }
  5285. if (em->block_start <= EXTENT_MAP_LAST_BYTE)
  5286. alloc_hint = em->block_start;
  5287. free_extent_map(em);
  5288. cur_offset = last_byte;
  5289. if (cur_offset >= alloc_end) {
  5290. ret = 0;
  5291. break;
  5292. }
  5293. }
  5294. unlock_extent_cached(&BTRFS_I(inode)->io_tree, alloc_start, locked_end,
  5295. &cached_state, GFP_NOFS);
  5296. btrfs_free_reserved_data_space(inode, alloc_end - alloc_start);
  5297. out:
  5298. mutex_unlock(&inode->i_mutex);
  5299. return ret;
  5300. }
  5301. static int btrfs_set_page_dirty(struct page *page)
  5302. {
  5303. return __set_page_dirty_nobuffers(page);
  5304. }
  5305. static int btrfs_permission(struct inode *inode, int mask)
  5306. {
  5307. if ((BTRFS_I(inode)->flags & BTRFS_INODE_READONLY) && (mask & MAY_WRITE))
  5308. return -EACCES;
  5309. return generic_permission(inode, mask, btrfs_check_acl);
  5310. }
  5311. static const struct inode_operations btrfs_dir_inode_operations = {
  5312. .getattr = btrfs_getattr,
  5313. .lookup = btrfs_lookup,
  5314. .create = btrfs_create,
  5315. .unlink = btrfs_unlink,
  5316. .link = btrfs_link,
  5317. .mkdir = btrfs_mkdir,
  5318. .rmdir = btrfs_rmdir,
  5319. .rename = btrfs_rename,
  5320. .symlink = btrfs_symlink,
  5321. .setattr = btrfs_setattr,
  5322. .mknod = btrfs_mknod,
  5323. .setxattr = btrfs_setxattr,
  5324. .getxattr = btrfs_getxattr,
  5325. .listxattr = btrfs_listxattr,
  5326. .removexattr = btrfs_removexattr,
  5327. .permission = btrfs_permission,
  5328. };
  5329. static const struct inode_operations btrfs_dir_ro_inode_operations = {
  5330. .lookup = btrfs_lookup,
  5331. .permission = btrfs_permission,
  5332. };
  5333. static const struct file_operations btrfs_dir_file_operations = {
  5334. .llseek = generic_file_llseek,
  5335. .read = generic_read_dir,
  5336. .readdir = btrfs_real_readdir,
  5337. .unlocked_ioctl = btrfs_ioctl,
  5338. #ifdef CONFIG_COMPAT
  5339. .compat_ioctl = btrfs_ioctl,
  5340. #endif
  5341. .release = btrfs_release_file,
  5342. .fsync = btrfs_sync_file,
  5343. };
  5344. static struct extent_io_ops btrfs_extent_io_ops = {
  5345. .fill_delalloc = run_delalloc_range,
  5346. .submit_bio_hook = btrfs_submit_bio_hook,
  5347. .merge_bio_hook = btrfs_merge_bio_hook,
  5348. .readpage_end_io_hook = btrfs_readpage_end_io_hook,
  5349. .writepage_end_io_hook = btrfs_writepage_end_io_hook,
  5350. .writepage_start_hook = btrfs_writepage_start_hook,
  5351. .readpage_io_failed_hook = btrfs_io_failed_hook,
  5352. .set_bit_hook = btrfs_set_bit_hook,
  5353. .clear_bit_hook = btrfs_clear_bit_hook,
  5354. .merge_extent_hook = btrfs_merge_extent_hook,
  5355. .split_extent_hook = btrfs_split_extent_hook,
  5356. };
  5357. /*
  5358. * btrfs doesn't support the bmap operation because swapfiles
  5359. * use bmap to make a mapping of extents in the file. They assume
  5360. * these extents won't change over the life of the file and they
  5361. * use the bmap result to do IO directly to the drive.
  5362. *
  5363. * the btrfs bmap call would return logical addresses that aren't
  5364. * suitable for IO and they also will change frequently as COW
  5365. * operations happen. So, swapfile + btrfs == corruption.
  5366. *
  5367. * For now we're avoiding this by dropping bmap.
  5368. */
  5369. static const struct address_space_operations btrfs_aops = {
  5370. .readpage = btrfs_readpage,
  5371. .writepage = btrfs_writepage,
  5372. .writepages = btrfs_writepages,
  5373. .readpages = btrfs_readpages,
  5374. .sync_page = block_sync_page,
  5375. .direct_IO = btrfs_direct_IO,
  5376. .invalidatepage = btrfs_invalidatepage,
  5377. .releasepage = btrfs_releasepage,
  5378. .set_page_dirty = btrfs_set_page_dirty,
  5379. .error_remove_page = generic_error_remove_page,
  5380. };
  5381. static const struct address_space_operations btrfs_symlink_aops = {
  5382. .readpage = btrfs_readpage,
  5383. .writepage = btrfs_writepage,
  5384. .invalidatepage = btrfs_invalidatepage,
  5385. .releasepage = btrfs_releasepage,
  5386. };
  5387. static const struct inode_operations btrfs_file_inode_operations = {
  5388. .truncate = btrfs_truncate,
  5389. .getattr = btrfs_getattr,
  5390. .setattr = btrfs_setattr,
  5391. .setxattr = btrfs_setxattr,
  5392. .getxattr = btrfs_getxattr,
  5393. .listxattr = btrfs_listxattr,
  5394. .removexattr = btrfs_removexattr,
  5395. .permission = btrfs_permission,
  5396. .fallocate = btrfs_fallocate,
  5397. .fiemap = btrfs_fiemap,
  5398. };
  5399. static const struct inode_operations btrfs_special_inode_operations = {
  5400. .getattr = btrfs_getattr,
  5401. .setattr = btrfs_setattr,
  5402. .permission = btrfs_permission,
  5403. .setxattr = btrfs_setxattr,
  5404. .getxattr = btrfs_getxattr,
  5405. .listxattr = btrfs_listxattr,
  5406. .removexattr = btrfs_removexattr,
  5407. };
  5408. static const struct inode_operations btrfs_symlink_inode_operations = {
  5409. .readlink = generic_readlink,
  5410. .follow_link = page_follow_link_light,
  5411. .put_link = page_put_link,
  5412. .permission = btrfs_permission,
  5413. .setxattr = btrfs_setxattr,
  5414. .getxattr = btrfs_getxattr,
  5415. .listxattr = btrfs_listxattr,
  5416. .removexattr = btrfs_removexattr,
  5417. };
  5418. const struct dentry_operations btrfs_dentry_operations = {
  5419. .d_delete = btrfs_dentry_delete,
  5420. };