inode.c 230 KB

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