inode.c 212 KB

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