inode.c 197 KB

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