inode.c 163 KB

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