inode.c 202 KB

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