inode.c 231 KB

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