inode.c 230 KB

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