inode.c 230 KB

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