inode.c 204 KB

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