inode.c 204 KB

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