inode.c 211 KB

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