inode.c 208 KB

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