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