inode.c 208 KB

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