inode.c 203 KB

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