inode.c 231 KB

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