inode.c 254 KB

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