inode.c 211 KB

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