inode.c 161 KB

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