inode.c 231 KB

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