inode.c 198 KB

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