inode.c 204 KB

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