inode.c 197 KB

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