inode.c 161 KB

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