inode.c 158 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885
  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 "compat.h"
  39. #include "ctree.h"
  40. #include "disk-io.h"
  41. #include "transaction.h"
  42. #include "btrfs_inode.h"
  43. #include "ioctl.h"
  44. #include "print-tree.h"
  45. #include "volumes.h"
  46. #include "ordered-data.h"
  47. #include "xattr.h"
  48. #include "tree-log.h"
  49. #include "compression.h"
  50. #include "locking.h"
  51. struct btrfs_iget_args {
  52. u64 ino;
  53. struct btrfs_root *root;
  54. };
  55. static struct inode_operations btrfs_dir_inode_operations;
  56. static struct inode_operations btrfs_symlink_inode_operations;
  57. static struct inode_operations btrfs_dir_ro_inode_operations;
  58. static struct inode_operations btrfs_special_inode_operations;
  59. static struct inode_operations btrfs_file_inode_operations;
  60. static struct address_space_operations btrfs_aops;
  61. static struct address_space_operations btrfs_symlink_aops;
  62. static struct file_operations btrfs_dir_file_operations;
  63. static struct extent_io_ops btrfs_extent_io_ops;
  64. static struct kmem_cache *btrfs_inode_cachep;
  65. struct kmem_cache *btrfs_trans_handle_cachep;
  66. struct kmem_cache *btrfs_transaction_cachep;
  67. struct kmem_cache *btrfs_path_cachep;
  68. #define S_SHIFT 12
  69. static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
  70. [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
  71. [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
  72. [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
  73. [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
  74. [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
  75. [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
  76. [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
  77. };
  78. static void btrfs_truncate(struct inode *inode);
  79. static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end);
  80. static noinline int cow_file_range(struct inode *inode,
  81. struct page *locked_page,
  82. u64 start, u64 end, int *page_started,
  83. unsigned long *nr_written, int unlock);
  84. static int btrfs_init_inode_security(struct inode *inode, struct inode *dir)
  85. {
  86. int err;
  87. err = btrfs_init_acl(inode, dir);
  88. if (!err)
  89. err = btrfs_xattr_security_init(inode, dir);
  90. return err;
  91. }
  92. /*
  93. * this does all the hard work for inserting an inline extent into
  94. * the btree. The caller should have done a btrfs_drop_extents so that
  95. * no overlapping inline items exist in the btree
  96. */
  97. static noinline int insert_inline_extent(struct btrfs_trans_handle *trans,
  98. struct btrfs_root *root, struct inode *inode,
  99. u64 start, size_t size, size_t compressed_size,
  100. struct page **compressed_pages)
  101. {
  102. struct btrfs_key key;
  103. struct btrfs_path *path;
  104. struct extent_buffer *leaf;
  105. struct page *page = NULL;
  106. char *kaddr;
  107. unsigned long ptr;
  108. struct btrfs_file_extent_item *ei;
  109. int err = 0;
  110. int ret;
  111. size_t cur_size = size;
  112. size_t datasize;
  113. unsigned long offset;
  114. int use_compress = 0;
  115. if (compressed_size && compressed_pages) {
  116. use_compress = 1;
  117. cur_size = compressed_size;
  118. }
  119. path = btrfs_alloc_path();
  120. if (!path)
  121. return -ENOMEM;
  122. path->leave_spinning = 1;
  123. btrfs_set_trans_block_group(trans, inode);
  124. key.objectid = inode->i_ino;
  125. key.offset = start;
  126. btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
  127. datasize = btrfs_file_extent_calc_inline_size(cur_size);
  128. inode_add_bytes(inode, size);
  129. ret = btrfs_insert_empty_item(trans, root, path, &key,
  130. datasize);
  131. BUG_ON(ret);
  132. if (ret) {
  133. err = ret;
  134. goto fail;
  135. }
  136. leaf = path->nodes[0];
  137. ei = btrfs_item_ptr(leaf, path->slots[0],
  138. struct btrfs_file_extent_item);
  139. btrfs_set_file_extent_generation(leaf, ei, trans->transid);
  140. btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
  141. btrfs_set_file_extent_encryption(leaf, ei, 0);
  142. btrfs_set_file_extent_other_encoding(leaf, ei, 0);
  143. btrfs_set_file_extent_ram_bytes(leaf, ei, size);
  144. ptr = btrfs_file_extent_inline_start(ei);
  145. if (use_compress) {
  146. struct page *cpage;
  147. int i = 0;
  148. while (compressed_size > 0) {
  149. cpage = compressed_pages[i];
  150. cur_size = min_t(unsigned long, compressed_size,
  151. PAGE_CACHE_SIZE);
  152. kaddr = kmap_atomic(cpage, KM_USER0);
  153. write_extent_buffer(leaf, kaddr, ptr, cur_size);
  154. kunmap_atomic(kaddr, KM_USER0);
  155. i++;
  156. ptr += cur_size;
  157. compressed_size -= cur_size;
  158. }
  159. btrfs_set_file_extent_compression(leaf, ei,
  160. BTRFS_COMPRESS_ZLIB);
  161. } else {
  162. page = find_get_page(inode->i_mapping,
  163. start >> PAGE_CACHE_SHIFT);
  164. btrfs_set_file_extent_compression(leaf, ei, 0);
  165. kaddr = kmap_atomic(page, KM_USER0);
  166. offset = start & (PAGE_CACHE_SIZE - 1);
  167. write_extent_buffer(leaf, kaddr + offset, ptr, size);
  168. kunmap_atomic(kaddr, KM_USER0);
  169. page_cache_release(page);
  170. }
  171. btrfs_mark_buffer_dirty(leaf);
  172. btrfs_free_path(path);
  173. BTRFS_I(inode)->disk_i_size = inode->i_size;
  174. btrfs_update_inode(trans, root, inode);
  175. return 0;
  176. fail:
  177. btrfs_free_path(path);
  178. return err;
  179. }
  180. /*
  181. * conditionally insert an inline extent into the file. This
  182. * does the checks required to make sure the data is small enough
  183. * to fit as an inline extent.
  184. */
  185. static noinline int cow_file_range_inline(struct btrfs_trans_handle *trans,
  186. struct btrfs_root *root,
  187. struct inode *inode, u64 start, u64 end,
  188. size_t compressed_size,
  189. struct page **compressed_pages)
  190. {
  191. u64 isize = i_size_read(inode);
  192. u64 actual_end = min(end + 1, isize);
  193. u64 inline_len = actual_end - start;
  194. u64 aligned_end = (end + root->sectorsize - 1) &
  195. ~((u64)root->sectorsize - 1);
  196. u64 hint_byte;
  197. u64 data_len = inline_len;
  198. int ret;
  199. if (compressed_size)
  200. data_len = compressed_size;
  201. if (start > 0 ||
  202. actual_end >= PAGE_CACHE_SIZE ||
  203. data_len >= BTRFS_MAX_INLINE_DATA_SIZE(root) ||
  204. (!compressed_size &&
  205. (actual_end & (root->sectorsize - 1)) == 0) ||
  206. end + 1 < isize ||
  207. data_len > root->fs_info->max_inline) {
  208. return 1;
  209. }
  210. ret = btrfs_drop_extents(trans, root, inode, start,
  211. aligned_end, aligned_end, start,
  212. &hint_byte, 1);
  213. BUG_ON(ret);
  214. if (isize > actual_end)
  215. inline_len = min_t(u64, isize, actual_end);
  216. ret = insert_inline_extent(trans, root, inode, start,
  217. inline_len, compressed_size,
  218. compressed_pages);
  219. BUG_ON(ret);
  220. btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0);
  221. return 0;
  222. }
  223. struct async_extent {
  224. u64 start;
  225. u64 ram_size;
  226. u64 compressed_size;
  227. struct page **pages;
  228. unsigned long nr_pages;
  229. struct list_head list;
  230. };
  231. struct async_cow {
  232. struct inode *inode;
  233. struct btrfs_root *root;
  234. struct page *locked_page;
  235. u64 start;
  236. u64 end;
  237. struct list_head extents;
  238. struct btrfs_work work;
  239. };
  240. static noinline int add_async_extent(struct async_cow *cow,
  241. u64 start, u64 ram_size,
  242. u64 compressed_size,
  243. struct page **pages,
  244. unsigned long nr_pages)
  245. {
  246. struct async_extent *async_extent;
  247. async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
  248. async_extent->start = start;
  249. async_extent->ram_size = ram_size;
  250. async_extent->compressed_size = compressed_size;
  251. async_extent->pages = pages;
  252. async_extent->nr_pages = nr_pages;
  253. list_add_tail(&async_extent->list, &cow->extents);
  254. return 0;
  255. }
  256. /*
  257. * we create compressed extents in two phases. The first
  258. * phase compresses a range of pages that have already been
  259. * locked (both pages and state bits are locked).
  260. *
  261. * This is done inside an ordered work queue, and the compression
  262. * is spread across many cpus. The actual IO submission is step
  263. * two, and the ordered work queue takes care of making sure that
  264. * happens in the same order things were put onto the queue by
  265. * writepages and friends.
  266. *
  267. * If this code finds it can't get good compression, it puts an
  268. * entry onto the work queue to write the uncompressed bytes. This
  269. * makes sure that both compressed inodes and uncompressed inodes
  270. * are written in the same order that pdflush sent them down.
  271. */
  272. static noinline int compress_file_range(struct inode *inode,
  273. struct page *locked_page,
  274. u64 start, u64 end,
  275. struct async_cow *async_cow,
  276. int *num_added)
  277. {
  278. struct btrfs_root *root = BTRFS_I(inode)->root;
  279. struct btrfs_trans_handle *trans;
  280. u64 num_bytes;
  281. u64 orig_start;
  282. u64 disk_num_bytes;
  283. u64 blocksize = root->sectorsize;
  284. u64 actual_end;
  285. u64 isize = i_size_read(inode);
  286. int ret = 0;
  287. struct page **pages = NULL;
  288. unsigned long nr_pages;
  289. unsigned long nr_pages_ret = 0;
  290. unsigned long total_compressed = 0;
  291. unsigned long total_in = 0;
  292. unsigned long max_compressed = 128 * 1024;
  293. unsigned long max_uncompressed = 128 * 1024;
  294. int i;
  295. int will_compress;
  296. orig_start = start;
  297. actual_end = min_t(u64, isize, end + 1);
  298. again:
  299. will_compress = 0;
  300. nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1;
  301. nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE);
  302. /*
  303. * we don't want to send crud past the end of i_size through
  304. * compression, that's just a waste of CPU time. So, if the
  305. * end of the file is before the start of our current
  306. * requested range of bytes, we bail out to the uncompressed
  307. * cleanup code that can deal with all of this.
  308. *
  309. * It isn't really the fastest way to fix things, but this is a
  310. * very uncommon corner.
  311. */
  312. if (actual_end <= start)
  313. goto cleanup_and_bail_uncompressed;
  314. total_compressed = actual_end - start;
  315. /* we want to make sure that amount of ram required to uncompress
  316. * an extent is reasonable, so we limit the total size in ram
  317. * of a compressed extent to 128k. This is a crucial number
  318. * because it also controls how easily we can spread reads across
  319. * cpus for decompression.
  320. *
  321. * We also want to make sure the amount of IO required to do
  322. * a random read is reasonably small, so we limit the size of
  323. * a compressed extent to 128k.
  324. */
  325. total_compressed = min(total_compressed, max_uncompressed);
  326. num_bytes = (end - start + blocksize) & ~(blocksize - 1);
  327. num_bytes = max(blocksize, num_bytes);
  328. disk_num_bytes = num_bytes;
  329. total_in = 0;
  330. ret = 0;
  331. /*
  332. * we do compression for mount -o compress and when the
  333. * inode has not been flagged as nocompress. This flag can
  334. * change at any time if we discover bad compression ratios.
  335. */
  336. if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) &&
  337. btrfs_test_opt(root, COMPRESS)) {
  338. WARN_ON(pages);
  339. pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS);
  340. ret = btrfs_zlib_compress_pages(inode->i_mapping, start,
  341. total_compressed, pages,
  342. nr_pages, &nr_pages_ret,
  343. &total_in,
  344. &total_compressed,
  345. max_compressed);
  346. if (!ret) {
  347. unsigned long offset = total_compressed &
  348. (PAGE_CACHE_SIZE - 1);
  349. struct page *page = pages[nr_pages_ret - 1];
  350. char *kaddr;
  351. /* zero the tail end of the last page, we might be
  352. * sending it down to disk
  353. */
  354. if (offset) {
  355. kaddr = kmap_atomic(page, KM_USER0);
  356. memset(kaddr + offset, 0,
  357. PAGE_CACHE_SIZE - offset);
  358. kunmap_atomic(kaddr, KM_USER0);
  359. }
  360. will_compress = 1;
  361. }
  362. }
  363. if (start == 0) {
  364. trans = btrfs_join_transaction(root, 1);
  365. BUG_ON(!trans);
  366. btrfs_set_trans_block_group(trans, inode);
  367. /* lets try to make an inline extent */
  368. if (ret || total_in < (actual_end - start)) {
  369. /* we didn't compress the entire range, try
  370. * to make an uncompressed inline extent.
  371. */
  372. ret = cow_file_range_inline(trans, root, inode,
  373. start, end, 0, NULL);
  374. } else {
  375. /* try making a compressed inline extent */
  376. ret = cow_file_range_inline(trans, root, inode,
  377. start, end,
  378. total_compressed, pages);
  379. }
  380. btrfs_end_transaction(trans, root);
  381. if (ret == 0) {
  382. /*
  383. * inline extent creation worked, we don't need
  384. * to create any more async work items. Unlock
  385. * and free up our temp pages.
  386. */
  387. extent_clear_unlock_delalloc(inode,
  388. &BTRFS_I(inode)->io_tree,
  389. start, end, NULL,
  390. EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY |
  391. EXTENT_CLEAR_DELALLOC |
  392. EXTENT_CLEAR_ACCOUNTING |
  393. EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK);
  394. ret = 0;
  395. goto free_pages_out;
  396. }
  397. }
  398. if (will_compress) {
  399. /*
  400. * we aren't doing an inline extent round the compressed size
  401. * up to a block size boundary so the allocator does sane
  402. * things
  403. */
  404. total_compressed = (total_compressed + blocksize - 1) &
  405. ~(blocksize - 1);
  406. /*
  407. * one last check to make sure the compression is really a
  408. * win, compare the page count read with the blocks on disk
  409. */
  410. total_in = (total_in + PAGE_CACHE_SIZE - 1) &
  411. ~(PAGE_CACHE_SIZE - 1);
  412. if (total_compressed >= total_in) {
  413. will_compress = 0;
  414. } else {
  415. disk_num_bytes = total_compressed;
  416. num_bytes = total_in;
  417. }
  418. }
  419. if (!will_compress && pages) {
  420. /*
  421. * the compression code ran but failed to make things smaller,
  422. * free any pages it allocated and our page pointer array
  423. */
  424. for (i = 0; i < nr_pages_ret; i++) {
  425. WARN_ON(pages[i]->mapping);
  426. page_cache_release(pages[i]);
  427. }
  428. kfree(pages);
  429. pages = NULL;
  430. total_compressed = 0;
  431. nr_pages_ret = 0;
  432. /* flag the file so we don't compress in the future */
  433. BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
  434. }
  435. if (will_compress) {
  436. *num_added += 1;
  437. /* the async work queues will take care of doing actual
  438. * allocation on disk for these compressed pages,
  439. * and will submit them to the elevator.
  440. */
  441. add_async_extent(async_cow, start, num_bytes,
  442. total_compressed, pages, nr_pages_ret);
  443. if (start + num_bytes < end && start + num_bytes < actual_end) {
  444. start += num_bytes;
  445. pages = NULL;
  446. cond_resched();
  447. goto again;
  448. }
  449. } else {
  450. cleanup_and_bail_uncompressed:
  451. /*
  452. * No compression, but we still need to write the pages in
  453. * the file we've been given so far. redirty the locked
  454. * page if it corresponds to our extent and set things up
  455. * for the async work queue to run cow_file_range to do
  456. * the normal delalloc dance
  457. */
  458. if (page_offset(locked_page) >= start &&
  459. page_offset(locked_page) <= end) {
  460. __set_page_dirty_nobuffers(locked_page);
  461. /* unlocked later on in the async handlers */
  462. }
  463. add_async_extent(async_cow, start, end - start + 1, 0, NULL, 0);
  464. *num_added += 1;
  465. }
  466. out:
  467. return 0;
  468. free_pages_out:
  469. for (i = 0; i < nr_pages_ret; i++) {
  470. WARN_ON(pages[i]->mapping);
  471. page_cache_release(pages[i]);
  472. }
  473. kfree(pages);
  474. goto out;
  475. }
  476. /*
  477. * phase two of compressed writeback. This is the ordered portion
  478. * of the code, which only gets called in the order the work was
  479. * queued. We walk all the async extents created by compress_file_range
  480. * and send them down to the disk.
  481. */
  482. static noinline int submit_compressed_extents(struct inode *inode,
  483. struct async_cow *async_cow)
  484. {
  485. struct async_extent *async_extent;
  486. u64 alloc_hint = 0;
  487. struct btrfs_trans_handle *trans;
  488. struct btrfs_key ins;
  489. struct extent_map *em;
  490. struct btrfs_root *root = BTRFS_I(inode)->root;
  491. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  492. struct extent_io_tree *io_tree;
  493. int ret;
  494. if (list_empty(&async_cow->extents))
  495. return 0;
  496. trans = btrfs_join_transaction(root, 1);
  497. while (!list_empty(&async_cow->extents)) {
  498. async_extent = list_entry(async_cow->extents.next,
  499. struct async_extent, list);
  500. list_del(&async_extent->list);
  501. io_tree = &BTRFS_I(inode)->io_tree;
  502. /* did the compression code fall back to uncompressed IO? */
  503. if (!async_extent->pages) {
  504. int page_started = 0;
  505. unsigned long nr_written = 0;
  506. lock_extent(io_tree, async_extent->start,
  507. async_extent->start +
  508. async_extent->ram_size - 1, GFP_NOFS);
  509. /* allocate blocks */
  510. cow_file_range(inode, async_cow->locked_page,
  511. async_extent->start,
  512. async_extent->start +
  513. async_extent->ram_size - 1,
  514. &page_started, &nr_written, 0);
  515. /*
  516. * if page_started, cow_file_range inserted an
  517. * inline extent and took care of all the unlocking
  518. * and IO for us. Otherwise, we need to submit
  519. * all those pages down to the drive.
  520. */
  521. if (!page_started)
  522. extent_write_locked_range(io_tree,
  523. inode, async_extent->start,
  524. async_extent->start +
  525. async_extent->ram_size - 1,
  526. btrfs_get_extent,
  527. WB_SYNC_ALL);
  528. kfree(async_extent);
  529. cond_resched();
  530. continue;
  531. }
  532. lock_extent(io_tree, async_extent->start,
  533. async_extent->start + async_extent->ram_size - 1,
  534. GFP_NOFS);
  535. /*
  536. * here we're doing allocation and writeback of the
  537. * compressed pages
  538. */
  539. btrfs_drop_extent_cache(inode, async_extent->start,
  540. async_extent->start +
  541. async_extent->ram_size - 1, 0);
  542. ret = btrfs_reserve_extent(trans, root,
  543. async_extent->compressed_size,
  544. async_extent->compressed_size,
  545. 0, alloc_hint,
  546. (u64)-1, &ins, 1);
  547. BUG_ON(ret);
  548. em = alloc_extent_map(GFP_NOFS);
  549. em->start = async_extent->start;
  550. em->len = async_extent->ram_size;
  551. em->orig_start = em->start;
  552. em->block_start = ins.objectid;
  553. em->block_len = ins.offset;
  554. em->bdev = root->fs_info->fs_devices->latest_bdev;
  555. set_bit(EXTENT_FLAG_PINNED, &em->flags);
  556. set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
  557. while (1) {
  558. write_lock(&em_tree->lock);
  559. ret = add_extent_mapping(em_tree, em);
  560. write_unlock(&em_tree->lock);
  561. if (ret != -EEXIST) {
  562. free_extent_map(em);
  563. break;
  564. }
  565. btrfs_drop_extent_cache(inode, async_extent->start,
  566. async_extent->start +
  567. async_extent->ram_size - 1, 0);
  568. }
  569. ret = btrfs_add_ordered_extent(inode, async_extent->start,
  570. ins.objectid,
  571. async_extent->ram_size,
  572. ins.offset,
  573. BTRFS_ORDERED_COMPRESSED);
  574. BUG_ON(ret);
  575. btrfs_end_transaction(trans, root);
  576. /*
  577. * clear dirty, set writeback and unlock the pages.
  578. */
  579. extent_clear_unlock_delalloc(inode,
  580. &BTRFS_I(inode)->io_tree,
  581. async_extent->start,
  582. async_extent->start +
  583. async_extent->ram_size - 1,
  584. NULL, EXTENT_CLEAR_UNLOCK_PAGE |
  585. EXTENT_CLEAR_UNLOCK |
  586. EXTENT_CLEAR_DELALLOC |
  587. EXTENT_CLEAR_DIRTY | EXTENT_SET_WRITEBACK);
  588. ret = btrfs_submit_compressed_write(inode,
  589. async_extent->start,
  590. async_extent->ram_size,
  591. ins.objectid,
  592. ins.offset, async_extent->pages,
  593. async_extent->nr_pages);
  594. BUG_ON(ret);
  595. trans = btrfs_join_transaction(root, 1);
  596. alloc_hint = ins.objectid + ins.offset;
  597. kfree(async_extent);
  598. cond_resched();
  599. }
  600. btrfs_end_transaction(trans, root);
  601. return 0;
  602. }
  603. /*
  604. * when extent_io.c finds a delayed allocation range in the file,
  605. * the call backs end up in this code. The basic idea is to
  606. * allocate extents on disk for the range, and create ordered data structs
  607. * in ram to track those extents.
  608. *
  609. * locked_page is the page that writepage had locked already. We use
  610. * it to make sure we don't do extra locks or unlocks.
  611. *
  612. * *page_started is set to one if we unlock locked_page and do everything
  613. * required to start IO on it. It may be clean and already done with
  614. * IO when we return.
  615. */
  616. static noinline int cow_file_range(struct inode *inode,
  617. struct page *locked_page,
  618. u64 start, u64 end, int *page_started,
  619. unsigned long *nr_written,
  620. int unlock)
  621. {
  622. struct btrfs_root *root = BTRFS_I(inode)->root;
  623. struct btrfs_trans_handle *trans;
  624. u64 alloc_hint = 0;
  625. u64 num_bytes;
  626. unsigned long ram_size;
  627. u64 disk_num_bytes;
  628. u64 cur_alloc_size;
  629. u64 blocksize = root->sectorsize;
  630. u64 actual_end;
  631. u64 isize = i_size_read(inode);
  632. struct btrfs_key ins;
  633. struct extent_map *em;
  634. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  635. int ret = 0;
  636. trans = btrfs_join_transaction(root, 1);
  637. BUG_ON(!trans);
  638. btrfs_set_trans_block_group(trans, inode);
  639. actual_end = min_t(u64, isize, end + 1);
  640. num_bytes = (end - start + blocksize) & ~(blocksize - 1);
  641. num_bytes = max(blocksize, num_bytes);
  642. disk_num_bytes = num_bytes;
  643. ret = 0;
  644. if (start == 0) {
  645. /* lets try to make an inline extent */
  646. ret = cow_file_range_inline(trans, root, inode,
  647. start, end, 0, NULL);
  648. if (ret == 0) {
  649. extent_clear_unlock_delalloc(inode,
  650. &BTRFS_I(inode)->io_tree,
  651. start, end, NULL,
  652. EXTENT_CLEAR_UNLOCK_PAGE |
  653. EXTENT_CLEAR_UNLOCK |
  654. EXTENT_CLEAR_DELALLOC |
  655. EXTENT_CLEAR_ACCOUNTING |
  656. EXTENT_CLEAR_DIRTY |
  657. EXTENT_SET_WRITEBACK |
  658. EXTENT_END_WRITEBACK);
  659. *nr_written = *nr_written +
  660. (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE;
  661. *page_started = 1;
  662. ret = 0;
  663. goto out;
  664. }
  665. }
  666. BUG_ON(disk_num_bytes >
  667. btrfs_super_total_bytes(&root->fs_info->super_copy));
  668. read_lock(&BTRFS_I(inode)->extent_tree.lock);
  669. em = search_extent_mapping(&BTRFS_I(inode)->extent_tree,
  670. start, num_bytes);
  671. if (em) {
  672. /*
  673. * if block start isn't an actual block number then find the
  674. * first block in this inode and use that as a hint. If that
  675. * block is also bogus then just don't worry about it.
  676. */
  677. if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
  678. free_extent_map(em);
  679. em = search_extent_mapping(em_tree, 0, 0);
  680. if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
  681. alloc_hint = em->block_start;
  682. if (em)
  683. free_extent_map(em);
  684. } else {
  685. alloc_hint = em->block_start;
  686. free_extent_map(em);
  687. }
  688. }
  689. read_unlock(&BTRFS_I(inode)->extent_tree.lock);
  690. btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
  691. while (disk_num_bytes > 0) {
  692. unsigned long op;
  693. cur_alloc_size = min(disk_num_bytes, root->fs_info->max_extent);
  694. ret = btrfs_reserve_extent(trans, root, cur_alloc_size,
  695. root->sectorsize, 0, alloc_hint,
  696. (u64)-1, &ins, 1);
  697. BUG_ON(ret);
  698. em = alloc_extent_map(GFP_NOFS);
  699. em->start = start;
  700. em->orig_start = em->start;
  701. ram_size = ins.offset;
  702. em->len = ins.offset;
  703. em->block_start = ins.objectid;
  704. em->block_len = ins.offset;
  705. em->bdev = root->fs_info->fs_devices->latest_bdev;
  706. set_bit(EXTENT_FLAG_PINNED, &em->flags);
  707. while (1) {
  708. write_lock(&em_tree->lock);
  709. ret = add_extent_mapping(em_tree, em);
  710. write_unlock(&em_tree->lock);
  711. if (ret != -EEXIST) {
  712. free_extent_map(em);
  713. break;
  714. }
  715. btrfs_drop_extent_cache(inode, start,
  716. start + ram_size - 1, 0);
  717. }
  718. cur_alloc_size = ins.offset;
  719. ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
  720. ram_size, cur_alloc_size, 0);
  721. BUG_ON(ret);
  722. if (root->root_key.objectid ==
  723. BTRFS_DATA_RELOC_TREE_OBJECTID) {
  724. ret = btrfs_reloc_clone_csums(inode, start,
  725. cur_alloc_size);
  726. BUG_ON(ret);
  727. }
  728. if (disk_num_bytes < cur_alloc_size)
  729. break;
  730. /* we're not doing compressed IO, don't unlock the first
  731. * page (which the caller expects to stay locked), don't
  732. * clear any dirty bits and don't set any writeback bits
  733. *
  734. * Do set the Private2 bit so we know this page was properly
  735. * setup for writepage
  736. */
  737. op = unlock ? EXTENT_CLEAR_UNLOCK_PAGE : 0;
  738. op |= EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
  739. EXTENT_SET_PRIVATE2;
  740. extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
  741. start, start + ram_size - 1,
  742. locked_page, op);
  743. disk_num_bytes -= cur_alloc_size;
  744. num_bytes -= cur_alloc_size;
  745. alloc_hint = ins.objectid + ins.offset;
  746. start += cur_alloc_size;
  747. }
  748. out:
  749. ret = 0;
  750. btrfs_end_transaction(trans, root);
  751. return ret;
  752. }
  753. /*
  754. * work queue call back to started compression on a file and pages
  755. */
  756. static noinline void async_cow_start(struct btrfs_work *work)
  757. {
  758. struct async_cow *async_cow;
  759. int num_added = 0;
  760. async_cow = container_of(work, struct async_cow, work);
  761. compress_file_range(async_cow->inode, async_cow->locked_page,
  762. async_cow->start, async_cow->end, async_cow,
  763. &num_added);
  764. if (num_added == 0)
  765. async_cow->inode = NULL;
  766. }
  767. /*
  768. * work queue call back to submit previously compressed pages
  769. */
  770. static noinline void async_cow_submit(struct btrfs_work *work)
  771. {
  772. struct async_cow *async_cow;
  773. struct btrfs_root *root;
  774. unsigned long nr_pages;
  775. async_cow = container_of(work, struct async_cow, work);
  776. root = async_cow->root;
  777. nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >>
  778. PAGE_CACHE_SHIFT;
  779. atomic_sub(nr_pages, &root->fs_info->async_delalloc_pages);
  780. if (atomic_read(&root->fs_info->async_delalloc_pages) <
  781. 5 * 1042 * 1024 &&
  782. waitqueue_active(&root->fs_info->async_submit_wait))
  783. wake_up(&root->fs_info->async_submit_wait);
  784. if (async_cow->inode)
  785. submit_compressed_extents(async_cow->inode, async_cow);
  786. }
  787. static noinline void async_cow_free(struct btrfs_work *work)
  788. {
  789. struct async_cow *async_cow;
  790. async_cow = container_of(work, struct async_cow, work);
  791. kfree(async_cow);
  792. }
  793. static int cow_file_range_async(struct inode *inode, struct page *locked_page,
  794. u64 start, u64 end, int *page_started,
  795. unsigned long *nr_written)
  796. {
  797. struct async_cow *async_cow;
  798. struct btrfs_root *root = BTRFS_I(inode)->root;
  799. unsigned long nr_pages;
  800. u64 cur_end;
  801. int limit = 10 * 1024 * 1042;
  802. clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
  803. 1, 0, NULL, GFP_NOFS);
  804. while (start < end) {
  805. async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
  806. async_cow->inode = inode;
  807. async_cow->root = root;
  808. async_cow->locked_page = locked_page;
  809. async_cow->start = start;
  810. if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
  811. cur_end = end;
  812. else
  813. cur_end = min(end, start + 512 * 1024 - 1);
  814. async_cow->end = cur_end;
  815. INIT_LIST_HEAD(&async_cow->extents);
  816. async_cow->work.func = async_cow_start;
  817. async_cow->work.ordered_func = async_cow_submit;
  818. async_cow->work.ordered_free = async_cow_free;
  819. async_cow->work.flags = 0;
  820. nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >>
  821. PAGE_CACHE_SHIFT;
  822. atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
  823. btrfs_queue_worker(&root->fs_info->delalloc_workers,
  824. &async_cow->work);
  825. if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) {
  826. wait_event(root->fs_info->async_submit_wait,
  827. (atomic_read(&root->fs_info->async_delalloc_pages) <
  828. limit));
  829. }
  830. while (atomic_read(&root->fs_info->async_submit_draining) &&
  831. atomic_read(&root->fs_info->async_delalloc_pages)) {
  832. wait_event(root->fs_info->async_submit_wait,
  833. (atomic_read(&root->fs_info->async_delalloc_pages) ==
  834. 0));
  835. }
  836. *nr_written += nr_pages;
  837. start = cur_end + 1;
  838. }
  839. *page_started = 1;
  840. return 0;
  841. }
  842. static noinline int csum_exist_in_range(struct btrfs_root *root,
  843. u64 bytenr, u64 num_bytes)
  844. {
  845. int ret;
  846. struct btrfs_ordered_sum *sums;
  847. LIST_HEAD(list);
  848. ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
  849. bytenr + num_bytes - 1, &list);
  850. if (ret == 0 && list_empty(&list))
  851. return 0;
  852. while (!list_empty(&list)) {
  853. sums = list_entry(list.next, struct btrfs_ordered_sum, list);
  854. list_del(&sums->list);
  855. kfree(sums);
  856. }
  857. return 1;
  858. }
  859. /*
  860. * when nowcow writeback call back. This checks for snapshots or COW copies
  861. * of the extents that exist in the file, and COWs the file as required.
  862. *
  863. * If no cow copies or snapshots exist, we write directly to the existing
  864. * blocks on disk
  865. */
  866. static noinline int run_delalloc_nocow(struct inode *inode,
  867. struct page *locked_page,
  868. u64 start, u64 end, int *page_started, int force,
  869. unsigned long *nr_written)
  870. {
  871. struct btrfs_root *root = BTRFS_I(inode)->root;
  872. struct btrfs_trans_handle *trans;
  873. struct extent_buffer *leaf;
  874. struct btrfs_path *path;
  875. struct btrfs_file_extent_item *fi;
  876. struct btrfs_key found_key;
  877. u64 cow_start;
  878. u64 cur_offset;
  879. u64 extent_end;
  880. u64 extent_offset;
  881. u64 disk_bytenr;
  882. u64 num_bytes;
  883. int extent_type;
  884. int ret;
  885. int type;
  886. int nocow;
  887. int check_prev = 1;
  888. path = btrfs_alloc_path();
  889. BUG_ON(!path);
  890. trans = btrfs_join_transaction(root, 1);
  891. BUG_ON(!trans);
  892. cow_start = (u64)-1;
  893. cur_offset = start;
  894. while (1) {
  895. ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino,
  896. cur_offset, 0);
  897. BUG_ON(ret < 0);
  898. if (ret > 0 && path->slots[0] > 0 && check_prev) {
  899. leaf = path->nodes[0];
  900. btrfs_item_key_to_cpu(leaf, &found_key,
  901. path->slots[0] - 1);
  902. if (found_key.objectid == inode->i_ino &&
  903. found_key.type == BTRFS_EXTENT_DATA_KEY)
  904. path->slots[0]--;
  905. }
  906. check_prev = 0;
  907. next_slot:
  908. leaf = path->nodes[0];
  909. if (path->slots[0] >= btrfs_header_nritems(leaf)) {
  910. ret = btrfs_next_leaf(root, path);
  911. if (ret < 0)
  912. BUG_ON(1);
  913. if (ret > 0)
  914. break;
  915. leaf = path->nodes[0];
  916. }
  917. nocow = 0;
  918. disk_bytenr = 0;
  919. num_bytes = 0;
  920. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  921. if (found_key.objectid > inode->i_ino ||
  922. found_key.type > BTRFS_EXTENT_DATA_KEY ||
  923. found_key.offset > end)
  924. break;
  925. if (found_key.offset > cur_offset) {
  926. extent_end = found_key.offset;
  927. extent_type = 0;
  928. goto out_check;
  929. }
  930. fi = btrfs_item_ptr(leaf, path->slots[0],
  931. struct btrfs_file_extent_item);
  932. extent_type = btrfs_file_extent_type(leaf, fi);
  933. if (extent_type == BTRFS_FILE_EXTENT_REG ||
  934. extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
  935. disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
  936. extent_offset = btrfs_file_extent_offset(leaf, fi);
  937. extent_end = found_key.offset +
  938. btrfs_file_extent_num_bytes(leaf, fi);
  939. if (extent_end <= start) {
  940. path->slots[0]++;
  941. goto next_slot;
  942. }
  943. if (disk_bytenr == 0)
  944. goto out_check;
  945. if (btrfs_file_extent_compression(leaf, fi) ||
  946. btrfs_file_extent_encryption(leaf, fi) ||
  947. btrfs_file_extent_other_encoding(leaf, fi))
  948. goto out_check;
  949. if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
  950. goto out_check;
  951. if (btrfs_extent_readonly(root, disk_bytenr))
  952. goto out_check;
  953. if (btrfs_cross_ref_exist(trans, root, inode->i_ino,
  954. found_key.offset -
  955. extent_offset, disk_bytenr))
  956. goto out_check;
  957. disk_bytenr += extent_offset;
  958. disk_bytenr += cur_offset - found_key.offset;
  959. num_bytes = min(end + 1, extent_end) - cur_offset;
  960. /*
  961. * force cow if csum exists in the range.
  962. * this ensure that csum for a given extent are
  963. * either valid or do not exist.
  964. */
  965. if (csum_exist_in_range(root, disk_bytenr, num_bytes))
  966. goto out_check;
  967. nocow = 1;
  968. } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
  969. extent_end = found_key.offset +
  970. btrfs_file_extent_inline_len(leaf, fi);
  971. extent_end = ALIGN(extent_end, root->sectorsize);
  972. } else {
  973. BUG_ON(1);
  974. }
  975. out_check:
  976. if (extent_end <= start) {
  977. path->slots[0]++;
  978. goto next_slot;
  979. }
  980. if (!nocow) {
  981. if (cow_start == (u64)-1)
  982. cow_start = cur_offset;
  983. cur_offset = extent_end;
  984. if (cur_offset > end)
  985. break;
  986. path->slots[0]++;
  987. goto next_slot;
  988. }
  989. btrfs_release_path(root, path);
  990. if (cow_start != (u64)-1) {
  991. ret = cow_file_range(inode, locked_page, cow_start,
  992. found_key.offset - 1, page_started,
  993. nr_written, 1);
  994. BUG_ON(ret);
  995. cow_start = (u64)-1;
  996. }
  997. if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
  998. struct extent_map *em;
  999. struct extent_map_tree *em_tree;
  1000. em_tree = &BTRFS_I(inode)->extent_tree;
  1001. em = alloc_extent_map(GFP_NOFS);
  1002. em->start = cur_offset;
  1003. em->orig_start = em->start;
  1004. em->len = num_bytes;
  1005. em->block_len = num_bytes;
  1006. em->block_start = disk_bytenr;
  1007. em->bdev = root->fs_info->fs_devices->latest_bdev;
  1008. set_bit(EXTENT_FLAG_PINNED, &em->flags);
  1009. while (1) {
  1010. write_lock(&em_tree->lock);
  1011. ret = add_extent_mapping(em_tree, em);
  1012. write_unlock(&em_tree->lock);
  1013. if (ret != -EEXIST) {
  1014. free_extent_map(em);
  1015. break;
  1016. }
  1017. btrfs_drop_extent_cache(inode, em->start,
  1018. em->start + em->len - 1, 0);
  1019. }
  1020. type = BTRFS_ORDERED_PREALLOC;
  1021. } else {
  1022. type = BTRFS_ORDERED_NOCOW;
  1023. }
  1024. ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
  1025. num_bytes, num_bytes, type);
  1026. BUG_ON(ret);
  1027. extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
  1028. cur_offset, cur_offset + num_bytes - 1,
  1029. locked_page, EXTENT_CLEAR_UNLOCK_PAGE |
  1030. EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
  1031. EXTENT_SET_PRIVATE2);
  1032. cur_offset = extent_end;
  1033. if (cur_offset > end)
  1034. break;
  1035. }
  1036. btrfs_release_path(root, path);
  1037. if (cur_offset <= end && cow_start == (u64)-1)
  1038. cow_start = cur_offset;
  1039. if (cow_start != (u64)-1) {
  1040. ret = cow_file_range(inode, locked_page, cow_start, end,
  1041. page_started, nr_written, 1);
  1042. BUG_ON(ret);
  1043. }
  1044. ret = btrfs_end_transaction(trans, root);
  1045. BUG_ON(ret);
  1046. btrfs_free_path(path);
  1047. return 0;
  1048. }
  1049. /*
  1050. * extent_io.c call back to do delayed allocation processing
  1051. */
  1052. static int run_delalloc_range(struct inode *inode, struct page *locked_page,
  1053. u64 start, u64 end, int *page_started,
  1054. unsigned long *nr_written)
  1055. {
  1056. int ret;
  1057. struct btrfs_root *root = BTRFS_I(inode)->root;
  1058. if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW)
  1059. ret = run_delalloc_nocow(inode, locked_page, start, end,
  1060. page_started, 1, nr_written);
  1061. else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC)
  1062. ret = run_delalloc_nocow(inode, locked_page, start, end,
  1063. page_started, 0, nr_written);
  1064. else if (!btrfs_test_opt(root, COMPRESS))
  1065. ret = cow_file_range(inode, locked_page, start, end,
  1066. page_started, nr_written, 1);
  1067. else
  1068. ret = cow_file_range_async(inode, locked_page, start, end,
  1069. page_started, nr_written);
  1070. return ret;
  1071. }
  1072. static int btrfs_split_extent_hook(struct inode *inode,
  1073. struct extent_state *orig, u64 split)
  1074. {
  1075. struct btrfs_root *root = BTRFS_I(inode)->root;
  1076. u64 size;
  1077. if (!(orig->state & EXTENT_DELALLOC))
  1078. return 0;
  1079. size = orig->end - orig->start + 1;
  1080. if (size > root->fs_info->max_extent) {
  1081. u64 num_extents;
  1082. u64 new_size;
  1083. new_size = orig->end - split + 1;
  1084. num_extents = div64_u64(size + root->fs_info->max_extent - 1,
  1085. root->fs_info->max_extent);
  1086. /*
  1087. * if we break a large extent up then leave oustanding_extents
  1088. * be, since we've already accounted for the large extent.
  1089. */
  1090. if (div64_u64(new_size + root->fs_info->max_extent - 1,
  1091. root->fs_info->max_extent) < num_extents)
  1092. return 0;
  1093. }
  1094. spin_lock(&BTRFS_I(inode)->accounting_lock);
  1095. BTRFS_I(inode)->outstanding_extents++;
  1096. spin_unlock(&BTRFS_I(inode)->accounting_lock);
  1097. return 0;
  1098. }
  1099. /*
  1100. * extent_io.c merge_extent_hook, used to track merged delayed allocation
  1101. * extents so we can keep track of new extents that are just merged onto old
  1102. * extents, such as when we are doing sequential writes, so we can properly
  1103. * account for the metadata space we'll need.
  1104. */
  1105. static int btrfs_merge_extent_hook(struct inode *inode,
  1106. struct extent_state *new,
  1107. struct extent_state *other)
  1108. {
  1109. struct btrfs_root *root = BTRFS_I(inode)->root;
  1110. u64 new_size, old_size;
  1111. u64 num_extents;
  1112. /* not delalloc, ignore it */
  1113. if (!(other->state & EXTENT_DELALLOC))
  1114. return 0;
  1115. old_size = other->end - other->start + 1;
  1116. if (new->start < other->start)
  1117. new_size = other->end - new->start + 1;
  1118. else
  1119. new_size = new->end - other->start + 1;
  1120. /* we're not bigger than the max, unreserve the space and go */
  1121. if (new_size <= root->fs_info->max_extent) {
  1122. spin_lock(&BTRFS_I(inode)->accounting_lock);
  1123. BTRFS_I(inode)->outstanding_extents--;
  1124. spin_unlock(&BTRFS_I(inode)->accounting_lock);
  1125. return 0;
  1126. }
  1127. /*
  1128. * If we grew by another max_extent, just return, we want to keep that
  1129. * reserved amount.
  1130. */
  1131. num_extents = div64_u64(old_size + root->fs_info->max_extent - 1,
  1132. root->fs_info->max_extent);
  1133. if (div64_u64(new_size + root->fs_info->max_extent - 1,
  1134. root->fs_info->max_extent) > num_extents)
  1135. return 0;
  1136. spin_lock(&BTRFS_I(inode)->accounting_lock);
  1137. BTRFS_I(inode)->outstanding_extents--;
  1138. spin_unlock(&BTRFS_I(inode)->accounting_lock);
  1139. return 0;
  1140. }
  1141. /*
  1142. * extent_io.c set_bit_hook, used to track delayed allocation
  1143. * bytes in this file, and to maintain the list of inodes that
  1144. * have pending delalloc work to be done.
  1145. */
  1146. static int btrfs_set_bit_hook(struct inode *inode, u64 start, u64 end,
  1147. unsigned long old, unsigned long bits)
  1148. {
  1149. /*
  1150. * set_bit and clear bit hooks normally require _irqsave/restore
  1151. * but in this case, we are only testeing for the DELALLOC
  1152. * bit, which is only set or cleared with irqs on
  1153. */
  1154. if (!(old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
  1155. struct btrfs_root *root = BTRFS_I(inode)->root;
  1156. spin_lock(&BTRFS_I(inode)->accounting_lock);
  1157. BTRFS_I(inode)->outstanding_extents++;
  1158. spin_unlock(&BTRFS_I(inode)->accounting_lock);
  1159. btrfs_delalloc_reserve_space(root, inode, end - start + 1);
  1160. spin_lock(&root->fs_info->delalloc_lock);
  1161. BTRFS_I(inode)->delalloc_bytes += end - start + 1;
  1162. root->fs_info->delalloc_bytes += end - start + 1;
  1163. if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
  1164. list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
  1165. &root->fs_info->delalloc_inodes);
  1166. }
  1167. spin_unlock(&root->fs_info->delalloc_lock);
  1168. }
  1169. return 0;
  1170. }
  1171. /*
  1172. * extent_io.c clear_bit_hook, see set_bit_hook for why
  1173. */
  1174. static int btrfs_clear_bit_hook(struct inode *inode,
  1175. struct extent_state *state, unsigned long bits)
  1176. {
  1177. /*
  1178. * set_bit and clear bit hooks normally require _irqsave/restore
  1179. * but in this case, we are only testeing for the DELALLOC
  1180. * bit, which is only set or cleared with irqs on
  1181. */
  1182. if ((state->state & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
  1183. struct btrfs_root *root = BTRFS_I(inode)->root;
  1184. if (bits & EXTENT_DO_ACCOUNTING) {
  1185. spin_lock(&BTRFS_I(inode)->accounting_lock);
  1186. BTRFS_I(inode)->outstanding_extents--;
  1187. spin_unlock(&BTRFS_I(inode)->accounting_lock);
  1188. btrfs_unreserve_metadata_for_delalloc(root, inode, 1);
  1189. }
  1190. spin_lock(&root->fs_info->delalloc_lock);
  1191. if (state->end - state->start + 1 >
  1192. root->fs_info->delalloc_bytes) {
  1193. printk(KERN_INFO "btrfs warning: delalloc account "
  1194. "%llu %llu\n",
  1195. (unsigned long long)
  1196. state->end - state->start + 1,
  1197. (unsigned long long)
  1198. root->fs_info->delalloc_bytes);
  1199. btrfs_delalloc_free_space(root, inode, (u64)-1);
  1200. root->fs_info->delalloc_bytes = 0;
  1201. BTRFS_I(inode)->delalloc_bytes = 0;
  1202. } else {
  1203. btrfs_delalloc_free_space(root, inode,
  1204. state->end -
  1205. state->start + 1);
  1206. root->fs_info->delalloc_bytes -= state->end -
  1207. state->start + 1;
  1208. BTRFS_I(inode)->delalloc_bytes -= state->end -
  1209. state->start + 1;
  1210. }
  1211. if (BTRFS_I(inode)->delalloc_bytes == 0 &&
  1212. !list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
  1213. list_del_init(&BTRFS_I(inode)->delalloc_inodes);
  1214. }
  1215. spin_unlock(&root->fs_info->delalloc_lock);
  1216. }
  1217. return 0;
  1218. }
  1219. /*
  1220. * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
  1221. * we don't create bios that span stripes or chunks
  1222. */
  1223. int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
  1224. size_t size, struct bio *bio,
  1225. unsigned long bio_flags)
  1226. {
  1227. struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
  1228. struct btrfs_mapping_tree *map_tree;
  1229. u64 logical = (u64)bio->bi_sector << 9;
  1230. u64 length = 0;
  1231. u64 map_length;
  1232. int ret;
  1233. if (bio_flags & EXTENT_BIO_COMPRESSED)
  1234. return 0;
  1235. length = bio->bi_size;
  1236. map_tree = &root->fs_info->mapping_tree;
  1237. map_length = length;
  1238. ret = btrfs_map_block(map_tree, READ, logical,
  1239. &map_length, NULL, 0);
  1240. if (map_length < length + size)
  1241. return 1;
  1242. return 0;
  1243. }
  1244. /*
  1245. * in order to insert checksums into the metadata in large chunks,
  1246. * we wait until bio submission time. All the pages in the bio are
  1247. * checksummed and sums are attached onto the ordered extent record.
  1248. *
  1249. * At IO completion time the cums attached on the ordered extent record
  1250. * are inserted into the btree
  1251. */
  1252. static int __btrfs_submit_bio_start(struct inode *inode, int rw,
  1253. struct bio *bio, int mirror_num,
  1254. unsigned long bio_flags)
  1255. {
  1256. struct btrfs_root *root = BTRFS_I(inode)->root;
  1257. int ret = 0;
  1258. ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
  1259. BUG_ON(ret);
  1260. return 0;
  1261. }
  1262. /*
  1263. * in order to insert checksums into the metadata in large chunks,
  1264. * we wait until bio submission time. All the pages in the bio are
  1265. * checksummed and sums are attached onto the ordered extent record.
  1266. *
  1267. * At IO completion time the cums attached on the ordered extent record
  1268. * are inserted into the btree
  1269. */
  1270. static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
  1271. int mirror_num, unsigned long bio_flags)
  1272. {
  1273. struct btrfs_root *root = BTRFS_I(inode)->root;
  1274. return btrfs_map_bio(root, rw, bio, mirror_num, 1);
  1275. }
  1276. /*
  1277. * extent_io.c submission hook. This does the right thing for csum calculation
  1278. * on write, or reading the csums from the tree before a read
  1279. */
  1280. static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
  1281. int mirror_num, unsigned long bio_flags)
  1282. {
  1283. struct btrfs_root *root = BTRFS_I(inode)->root;
  1284. int ret = 0;
  1285. int skip_sum;
  1286. skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
  1287. ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
  1288. BUG_ON(ret);
  1289. if (!(rw & (1 << BIO_RW))) {
  1290. if (bio_flags & EXTENT_BIO_COMPRESSED) {
  1291. return btrfs_submit_compressed_read(inode, bio,
  1292. mirror_num, bio_flags);
  1293. } else if (!skip_sum)
  1294. btrfs_lookup_bio_sums(root, inode, bio, NULL);
  1295. goto mapit;
  1296. } else if (!skip_sum) {
  1297. /* csum items have already been cloned */
  1298. if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
  1299. goto mapit;
  1300. /* we're doing a write, do the async checksumming */
  1301. return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
  1302. inode, rw, bio, mirror_num,
  1303. bio_flags, __btrfs_submit_bio_start,
  1304. __btrfs_submit_bio_done);
  1305. }
  1306. mapit:
  1307. return btrfs_map_bio(root, rw, bio, mirror_num, 0);
  1308. }
  1309. /*
  1310. * given a list of ordered sums record them in the inode. This happens
  1311. * at IO completion time based on sums calculated at bio submission time.
  1312. */
  1313. static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
  1314. struct inode *inode, u64 file_offset,
  1315. struct list_head *list)
  1316. {
  1317. struct btrfs_ordered_sum *sum;
  1318. btrfs_set_trans_block_group(trans, inode);
  1319. list_for_each_entry(sum, list, list) {
  1320. btrfs_csum_file_blocks(trans,
  1321. BTRFS_I(inode)->root->fs_info->csum_root, sum);
  1322. }
  1323. return 0;
  1324. }
  1325. int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end)
  1326. {
  1327. if ((end & (PAGE_CACHE_SIZE - 1)) == 0)
  1328. WARN_ON(1);
  1329. return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
  1330. GFP_NOFS);
  1331. }
  1332. /* see btrfs_writepage_start_hook for details on why this is required */
  1333. struct btrfs_writepage_fixup {
  1334. struct page *page;
  1335. struct btrfs_work work;
  1336. };
  1337. static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
  1338. {
  1339. struct btrfs_writepage_fixup *fixup;
  1340. struct btrfs_ordered_extent *ordered;
  1341. struct page *page;
  1342. struct inode *inode;
  1343. u64 page_start;
  1344. u64 page_end;
  1345. fixup = container_of(work, struct btrfs_writepage_fixup, work);
  1346. page = fixup->page;
  1347. again:
  1348. lock_page(page);
  1349. if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
  1350. ClearPageChecked(page);
  1351. goto out_page;
  1352. }
  1353. inode = page->mapping->host;
  1354. page_start = page_offset(page);
  1355. page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
  1356. lock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS);
  1357. /* already ordered? We're done */
  1358. if (PagePrivate2(page))
  1359. goto out;
  1360. ordered = btrfs_lookup_ordered_extent(inode, page_start);
  1361. if (ordered) {
  1362. unlock_extent(&BTRFS_I(inode)->io_tree, page_start,
  1363. page_end, GFP_NOFS);
  1364. unlock_page(page);
  1365. btrfs_start_ordered_extent(inode, ordered, 1);
  1366. goto again;
  1367. }
  1368. btrfs_set_extent_delalloc(inode, page_start, page_end);
  1369. ClearPageChecked(page);
  1370. out:
  1371. unlock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS);
  1372. out_page:
  1373. unlock_page(page);
  1374. page_cache_release(page);
  1375. }
  1376. /*
  1377. * There are a few paths in the higher layers of the kernel that directly
  1378. * set the page dirty bit without asking the filesystem if it is a
  1379. * good idea. This causes problems because we want to make sure COW
  1380. * properly happens and the data=ordered rules are followed.
  1381. *
  1382. * In our case any range that doesn't have the ORDERED bit set
  1383. * hasn't been properly setup for IO. We kick off an async process
  1384. * to fix it up. The async helper will wait for ordered extents, set
  1385. * the delalloc bit and make it safe to write the page.
  1386. */
  1387. static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
  1388. {
  1389. struct inode *inode = page->mapping->host;
  1390. struct btrfs_writepage_fixup *fixup;
  1391. struct btrfs_root *root = BTRFS_I(inode)->root;
  1392. /* this page is properly in the ordered list */
  1393. if (TestClearPagePrivate2(page))
  1394. return 0;
  1395. if (PageChecked(page))
  1396. return -EAGAIN;
  1397. fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
  1398. if (!fixup)
  1399. return -EAGAIN;
  1400. SetPageChecked(page);
  1401. page_cache_get(page);
  1402. fixup->work.func = btrfs_writepage_fixup_worker;
  1403. fixup->page = page;
  1404. btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
  1405. return -EAGAIN;
  1406. }
  1407. static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
  1408. struct inode *inode, u64 file_pos,
  1409. u64 disk_bytenr, u64 disk_num_bytes,
  1410. u64 num_bytes, u64 ram_bytes,
  1411. u64 locked_end,
  1412. u8 compression, u8 encryption,
  1413. u16 other_encoding, int extent_type)
  1414. {
  1415. struct btrfs_root *root = BTRFS_I(inode)->root;
  1416. struct btrfs_file_extent_item *fi;
  1417. struct btrfs_path *path;
  1418. struct extent_buffer *leaf;
  1419. struct btrfs_key ins;
  1420. u64 hint;
  1421. int ret;
  1422. path = btrfs_alloc_path();
  1423. BUG_ON(!path);
  1424. path->leave_spinning = 1;
  1425. /*
  1426. * we may be replacing one extent in the tree with another.
  1427. * The new extent is pinned in the extent map, and we don't want
  1428. * to drop it from the cache until it is completely in the btree.
  1429. *
  1430. * So, tell btrfs_drop_extents to leave this extent in the cache.
  1431. * the caller is expected to unpin it and allow it to be merged
  1432. * with the others.
  1433. */
  1434. ret = btrfs_drop_extents(trans, root, inode, file_pos,
  1435. file_pos + num_bytes, locked_end,
  1436. file_pos, &hint, 0);
  1437. BUG_ON(ret);
  1438. ins.objectid = inode->i_ino;
  1439. ins.offset = file_pos;
  1440. ins.type = BTRFS_EXTENT_DATA_KEY;
  1441. ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi));
  1442. BUG_ON(ret);
  1443. leaf = path->nodes[0];
  1444. fi = btrfs_item_ptr(leaf, path->slots[0],
  1445. struct btrfs_file_extent_item);
  1446. btrfs_set_file_extent_generation(leaf, fi, trans->transid);
  1447. btrfs_set_file_extent_type(leaf, fi, extent_type);
  1448. btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
  1449. btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
  1450. btrfs_set_file_extent_offset(leaf, fi, 0);
  1451. btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
  1452. btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
  1453. btrfs_set_file_extent_compression(leaf, fi, compression);
  1454. btrfs_set_file_extent_encryption(leaf, fi, encryption);
  1455. btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
  1456. btrfs_unlock_up_safe(path, 1);
  1457. btrfs_set_lock_blocking(leaf);
  1458. btrfs_mark_buffer_dirty(leaf);
  1459. inode_add_bytes(inode, num_bytes);
  1460. ins.objectid = disk_bytenr;
  1461. ins.offset = disk_num_bytes;
  1462. ins.type = BTRFS_EXTENT_ITEM_KEY;
  1463. ret = btrfs_alloc_reserved_file_extent(trans, root,
  1464. root->root_key.objectid,
  1465. inode->i_ino, file_pos, &ins);
  1466. BUG_ON(ret);
  1467. btrfs_free_path(path);
  1468. return 0;
  1469. }
  1470. /*
  1471. * helper function for btrfs_finish_ordered_io, this
  1472. * just reads in some of the csum leaves to prime them into ram
  1473. * before we start the transaction. It limits the amount of btree
  1474. * reads required while inside the transaction.
  1475. */
  1476. static noinline void reada_csum(struct btrfs_root *root,
  1477. struct btrfs_path *path,
  1478. struct btrfs_ordered_extent *ordered_extent)
  1479. {
  1480. struct btrfs_ordered_sum *sum;
  1481. u64 bytenr;
  1482. sum = list_entry(ordered_extent->list.next, struct btrfs_ordered_sum,
  1483. list);
  1484. bytenr = sum->sums[0].bytenr;
  1485. /*
  1486. * we don't care about the results, the point of this search is
  1487. * just to get the btree leaves into ram
  1488. */
  1489. btrfs_lookup_csum(NULL, root->fs_info->csum_root, path, bytenr, 0);
  1490. }
  1491. /* as ordered data IO finishes, this gets called so we can finish
  1492. * an ordered extent if the range of bytes in the file it covers are
  1493. * fully written.
  1494. */
  1495. static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end)
  1496. {
  1497. struct btrfs_root *root = BTRFS_I(inode)->root;
  1498. struct btrfs_trans_handle *trans;
  1499. struct btrfs_ordered_extent *ordered_extent = NULL;
  1500. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  1501. struct btrfs_path *path;
  1502. int compressed = 0;
  1503. int ret;
  1504. ret = btrfs_dec_test_ordered_pending(inode, start, end - start + 1);
  1505. if (!ret)
  1506. return 0;
  1507. /*
  1508. * before we join the transaction, try to do some of our IO.
  1509. * This will limit the amount of IO that we have to do with
  1510. * the transaction running. We're unlikely to need to do any
  1511. * IO if the file extents are new, the disk_i_size checks
  1512. * covers the most common case.
  1513. */
  1514. if (start < BTRFS_I(inode)->disk_i_size) {
  1515. path = btrfs_alloc_path();
  1516. if (path) {
  1517. ret = btrfs_lookup_file_extent(NULL, root, path,
  1518. inode->i_ino,
  1519. start, 0);
  1520. ordered_extent = btrfs_lookup_ordered_extent(inode,
  1521. start);
  1522. if (!list_empty(&ordered_extent->list)) {
  1523. btrfs_release_path(root, path);
  1524. reada_csum(root, path, ordered_extent);
  1525. }
  1526. btrfs_free_path(path);
  1527. }
  1528. }
  1529. trans = btrfs_join_transaction(root, 1);
  1530. if (!ordered_extent)
  1531. ordered_extent = btrfs_lookup_ordered_extent(inode, start);
  1532. BUG_ON(!ordered_extent);
  1533. if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags))
  1534. goto nocow;
  1535. lock_extent(io_tree, ordered_extent->file_offset,
  1536. ordered_extent->file_offset + ordered_extent->len - 1,
  1537. GFP_NOFS);
  1538. if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
  1539. compressed = 1;
  1540. if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
  1541. BUG_ON(compressed);
  1542. ret = btrfs_mark_extent_written(trans, root, inode,
  1543. ordered_extent->file_offset,
  1544. ordered_extent->file_offset +
  1545. ordered_extent->len);
  1546. BUG_ON(ret);
  1547. } else {
  1548. ret = insert_reserved_file_extent(trans, inode,
  1549. ordered_extent->file_offset,
  1550. ordered_extent->start,
  1551. ordered_extent->disk_len,
  1552. ordered_extent->len,
  1553. ordered_extent->len,
  1554. ordered_extent->file_offset +
  1555. ordered_extent->len,
  1556. compressed, 0, 0,
  1557. BTRFS_FILE_EXTENT_REG);
  1558. unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
  1559. ordered_extent->file_offset,
  1560. ordered_extent->len);
  1561. BUG_ON(ret);
  1562. }
  1563. unlock_extent(io_tree, ordered_extent->file_offset,
  1564. ordered_extent->file_offset + ordered_extent->len - 1,
  1565. GFP_NOFS);
  1566. nocow:
  1567. add_pending_csums(trans, inode, ordered_extent->file_offset,
  1568. &ordered_extent->list);
  1569. mutex_lock(&BTRFS_I(inode)->extent_mutex);
  1570. btrfs_ordered_update_i_size(inode, ordered_extent);
  1571. btrfs_update_inode(trans, root, inode);
  1572. btrfs_remove_ordered_extent(inode, ordered_extent);
  1573. mutex_unlock(&BTRFS_I(inode)->extent_mutex);
  1574. /* once for us */
  1575. btrfs_put_ordered_extent(ordered_extent);
  1576. /* once for the tree */
  1577. btrfs_put_ordered_extent(ordered_extent);
  1578. btrfs_end_transaction(trans, root);
  1579. return 0;
  1580. }
  1581. static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
  1582. struct extent_state *state, int uptodate)
  1583. {
  1584. ClearPagePrivate2(page);
  1585. return btrfs_finish_ordered_io(page->mapping->host, start, end);
  1586. }
  1587. /*
  1588. * When IO fails, either with EIO or csum verification fails, we
  1589. * try other mirrors that might have a good copy of the data. This
  1590. * io_failure_record is used to record state as we go through all the
  1591. * mirrors. If another mirror has good data, the page is set up to date
  1592. * and things continue. If a good mirror can't be found, the original
  1593. * bio end_io callback is called to indicate things have failed.
  1594. */
  1595. struct io_failure_record {
  1596. struct page *page;
  1597. u64 start;
  1598. u64 len;
  1599. u64 logical;
  1600. unsigned long bio_flags;
  1601. int last_mirror;
  1602. };
  1603. static int btrfs_io_failed_hook(struct bio *failed_bio,
  1604. struct page *page, u64 start, u64 end,
  1605. struct extent_state *state)
  1606. {
  1607. struct io_failure_record *failrec = NULL;
  1608. u64 private;
  1609. struct extent_map *em;
  1610. struct inode *inode = page->mapping->host;
  1611. struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
  1612. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  1613. struct bio *bio;
  1614. int num_copies;
  1615. int ret;
  1616. int rw;
  1617. u64 logical;
  1618. ret = get_state_private(failure_tree, start, &private);
  1619. if (ret) {
  1620. failrec = kmalloc(sizeof(*failrec), GFP_NOFS);
  1621. if (!failrec)
  1622. return -ENOMEM;
  1623. failrec->start = start;
  1624. failrec->len = end - start + 1;
  1625. failrec->last_mirror = 0;
  1626. failrec->bio_flags = 0;
  1627. read_lock(&em_tree->lock);
  1628. em = lookup_extent_mapping(em_tree, start, failrec->len);
  1629. if (em->start > start || em->start + em->len < start) {
  1630. free_extent_map(em);
  1631. em = NULL;
  1632. }
  1633. read_unlock(&em_tree->lock);
  1634. if (!em || IS_ERR(em)) {
  1635. kfree(failrec);
  1636. return -EIO;
  1637. }
  1638. logical = start - em->start;
  1639. logical = em->block_start + logical;
  1640. if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
  1641. logical = em->block_start;
  1642. failrec->bio_flags = EXTENT_BIO_COMPRESSED;
  1643. }
  1644. failrec->logical = logical;
  1645. free_extent_map(em);
  1646. set_extent_bits(failure_tree, start, end, EXTENT_LOCKED |
  1647. EXTENT_DIRTY, GFP_NOFS);
  1648. set_state_private(failure_tree, start,
  1649. (u64)(unsigned long)failrec);
  1650. } else {
  1651. failrec = (struct io_failure_record *)(unsigned long)private;
  1652. }
  1653. num_copies = btrfs_num_copies(
  1654. &BTRFS_I(inode)->root->fs_info->mapping_tree,
  1655. failrec->logical, failrec->len);
  1656. failrec->last_mirror++;
  1657. if (!state) {
  1658. spin_lock(&BTRFS_I(inode)->io_tree.lock);
  1659. state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
  1660. failrec->start,
  1661. EXTENT_LOCKED);
  1662. if (state && state->start != failrec->start)
  1663. state = NULL;
  1664. spin_unlock(&BTRFS_I(inode)->io_tree.lock);
  1665. }
  1666. if (!state || failrec->last_mirror > num_copies) {
  1667. set_state_private(failure_tree, failrec->start, 0);
  1668. clear_extent_bits(failure_tree, failrec->start,
  1669. failrec->start + failrec->len - 1,
  1670. EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
  1671. kfree(failrec);
  1672. return -EIO;
  1673. }
  1674. bio = bio_alloc(GFP_NOFS, 1);
  1675. bio->bi_private = state;
  1676. bio->bi_end_io = failed_bio->bi_end_io;
  1677. bio->bi_sector = failrec->logical >> 9;
  1678. bio->bi_bdev = failed_bio->bi_bdev;
  1679. bio->bi_size = 0;
  1680. bio_add_page(bio, page, failrec->len, start - page_offset(page));
  1681. if (failed_bio->bi_rw & (1 << BIO_RW))
  1682. rw = WRITE;
  1683. else
  1684. rw = READ;
  1685. BTRFS_I(inode)->io_tree.ops->submit_bio_hook(inode, rw, bio,
  1686. failrec->last_mirror,
  1687. failrec->bio_flags);
  1688. return 0;
  1689. }
  1690. /*
  1691. * each time an IO finishes, we do a fast check in the IO failure tree
  1692. * to see if we need to process or clean up an io_failure_record
  1693. */
  1694. static int btrfs_clean_io_failures(struct inode *inode, u64 start)
  1695. {
  1696. u64 private;
  1697. u64 private_failure;
  1698. struct io_failure_record *failure;
  1699. int ret;
  1700. private = 0;
  1701. if (count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
  1702. (u64)-1, 1, EXTENT_DIRTY)) {
  1703. ret = get_state_private(&BTRFS_I(inode)->io_failure_tree,
  1704. start, &private_failure);
  1705. if (ret == 0) {
  1706. failure = (struct io_failure_record *)(unsigned long)
  1707. private_failure;
  1708. set_state_private(&BTRFS_I(inode)->io_failure_tree,
  1709. failure->start, 0);
  1710. clear_extent_bits(&BTRFS_I(inode)->io_failure_tree,
  1711. failure->start,
  1712. failure->start + failure->len - 1,
  1713. EXTENT_DIRTY | EXTENT_LOCKED,
  1714. GFP_NOFS);
  1715. kfree(failure);
  1716. }
  1717. }
  1718. return 0;
  1719. }
  1720. /*
  1721. * when reads are done, we need to check csums to verify the data is correct
  1722. * if there's a match, we allow the bio to finish. If not, we go through
  1723. * the io_failure_record routines to find good copies
  1724. */
  1725. static int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
  1726. struct extent_state *state)
  1727. {
  1728. size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
  1729. struct inode *inode = page->mapping->host;
  1730. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  1731. char *kaddr;
  1732. u64 private = ~(u32)0;
  1733. int ret;
  1734. struct btrfs_root *root = BTRFS_I(inode)->root;
  1735. u32 csum = ~(u32)0;
  1736. if (PageChecked(page)) {
  1737. ClearPageChecked(page);
  1738. goto good;
  1739. }
  1740. if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
  1741. return 0;
  1742. if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
  1743. test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
  1744. clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
  1745. GFP_NOFS);
  1746. return 0;
  1747. }
  1748. if (state && state->start == start) {
  1749. private = state->private;
  1750. ret = 0;
  1751. } else {
  1752. ret = get_state_private(io_tree, start, &private);
  1753. }
  1754. kaddr = kmap_atomic(page, KM_USER0);
  1755. if (ret)
  1756. goto zeroit;
  1757. csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
  1758. btrfs_csum_final(csum, (char *)&csum);
  1759. if (csum != private)
  1760. goto zeroit;
  1761. kunmap_atomic(kaddr, KM_USER0);
  1762. good:
  1763. /* if the io failure tree for this inode is non-empty,
  1764. * check to see if we've recovered from a failed IO
  1765. */
  1766. btrfs_clean_io_failures(inode, start);
  1767. return 0;
  1768. zeroit:
  1769. if (printk_ratelimit()) {
  1770. printk(KERN_INFO "btrfs csum failed ino %lu off %llu csum %u "
  1771. "private %llu\n", page->mapping->host->i_ino,
  1772. (unsigned long long)start, csum,
  1773. (unsigned long long)private);
  1774. }
  1775. memset(kaddr + offset, 1, end - start + 1);
  1776. flush_dcache_page(page);
  1777. kunmap_atomic(kaddr, KM_USER0);
  1778. if (private == 0)
  1779. return 0;
  1780. return -EIO;
  1781. }
  1782. /*
  1783. * This creates an orphan entry for the given inode in case something goes
  1784. * wrong in the middle of an unlink/truncate.
  1785. */
  1786. int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
  1787. {
  1788. struct btrfs_root *root = BTRFS_I(inode)->root;
  1789. int ret = 0;
  1790. spin_lock(&root->list_lock);
  1791. /* already on the orphan list, we're good */
  1792. if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
  1793. spin_unlock(&root->list_lock);
  1794. return 0;
  1795. }
  1796. list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
  1797. spin_unlock(&root->list_lock);
  1798. /*
  1799. * insert an orphan item to track this unlinked/truncated file
  1800. */
  1801. ret = btrfs_insert_orphan_item(trans, root, inode->i_ino);
  1802. return ret;
  1803. }
  1804. /*
  1805. * We have done the truncate/delete so we can go ahead and remove the orphan
  1806. * item for this particular inode.
  1807. */
  1808. int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode)
  1809. {
  1810. struct btrfs_root *root = BTRFS_I(inode)->root;
  1811. int ret = 0;
  1812. spin_lock(&root->list_lock);
  1813. if (list_empty(&BTRFS_I(inode)->i_orphan)) {
  1814. spin_unlock(&root->list_lock);
  1815. return 0;
  1816. }
  1817. list_del_init(&BTRFS_I(inode)->i_orphan);
  1818. if (!trans) {
  1819. spin_unlock(&root->list_lock);
  1820. return 0;
  1821. }
  1822. spin_unlock(&root->list_lock);
  1823. ret = btrfs_del_orphan_item(trans, root, inode->i_ino);
  1824. return ret;
  1825. }
  1826. /*
  1827. * this cleans up any orphans that may be left on the list from the last use
  1828. * of this root.
  1829. */
  1830. void btrfs_orphan_cleanup(struct btrfs_root *root)
  1831. {
  1832. struct btrfs_path *path;
  1833. struct extent_buffer *leaf;
  1834. struct btrfs_item *item;
  1835. struct btrfs_key key, found_key;
  1836. struct btrfs_trans_handle *trans;
  1837. struct inode *inode;
  1838. int ret = 0, nr_unlink = 0, nr_truncate = 0;
  1839. path = btrfs_alloc_path();
  1840. if (!path)
  1841. return;
  1842. path->reada = -1;
  1843. key.objectid = BTRFS_ORPHAN_OBJECTID;
  1844. btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
  1845. key.offset = (u64)-1;
  1846. while (1) {
  1847. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  1848. if (ret < 0) {
  1849. printk(KERN_ERR "Error searching slot for orphan: %d"
  1850. "\n", ret);
  1851. break;
  1852. }
  1853. /*
  1854. * if ret == 0 means we found what we were searching for, which
  1855. * is weird, but possible, so only screw with path if we didnt
  1856. * find the key and see if we have stuff that matches
  1857. */
  1858. if (ret > 0) {
  1859. if (path->slots[0] == 0)
  1860. break;
  1861. path->slots[0]--;
  1862. }
  1863. /* pull out the item */
  1864. leaf = path->nodes[0];
  1865. item = btrfs_item_nr(leaf, path->slots[0]);
  1866. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  1867. /* make sure the item matches what we want */
  1868. if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
  1869. break;
  1870. if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
  1871. break;
  1872. /* release the path since we're done with it */
  1873. btrfs_release_path(root, path);
  1874. /*
  1875. * this is where we are basically btrfs_lookup, without the
  1876. * crossing root thing. we store the inode number in the
  1877. * offset of the orphan item.
  1878. */
  1879. found_key.objectid = found_key.offset;
  1880. found_key.type = BTRFS_INODE_ITEM_KEY;
  1881. found_key.offset = 0;
  1882. inode = btrfs_iget(root->fs_info->sb, &found_key, root);
  1883. if (IS_ERR(inode))
  1884. break;
  1885. /*
  1886. * add this inode to the orphan list so btrfs_orphan_del does
  1887. * the proper thing when we hit it
  1888. */
  1889. spin_lock(&root->list_lock);
  1890. list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
  1891. spin_unlock(&root->list_lock);
  1892. /*
  1893. * if this is a bad inode, means we actually succeeded in
  1894. * removing the inode, but not the orphan record, which means
  1895. * we need to manually delete the orphan since iput will just
  1896. * do a destroy_inode
  1897. */
  1898. if (is_bad_inode(inode)) {
  1899. trans = btrfs_start_transaction(root, 1);
  1900. btrfs_orphan_del(trans, inode);
  1901. btrfs_end_transaction(trans, root);
  1902. iput(inode);
  1903. continue;
  1904. }
  1905. /* if we have links, this was a truncate, lets do that */
  1906. if (inode->i_nlink) {
  1907. nr_truncate++;
  1908. btrfs_truncate(inode);
  1909. } else {
  1910. nr_unlink++;
  1911. }
  1912. /* this will do delete_inode and everything for us */
  1913. iput(inode);
  1914. }
  1915. if (nr_unlink)
  1916. printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink);
  1917. if (nr_truncate)
  1918. printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate);
  1919. btrfs_free_path(path);
  1920. }
  1921. /*
  1922. * very simple check to peek ahead in the leaf looking for xattrs. If we
  1923. * don't find any xattrs, we know there can't be any acls.
  1924. *
  1925. * slot is the slot the inode is in, objectid is the objectid of the inode
  1926. */
  1927. static noinline int acls_after_inode_item(struct extent_buffer *leaf,
  1928. int slot, u64 objectid)
  1929. {
  1930. u32 nritems = btrfs_header_nritems(leaf);
  1931. struct btrfs_key found_key;
  1932. int scanned = 0;
  1933. slot++;
  1934. while (slot < nritems) {
  1935. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  1936. /* we found a different objectid, there must not be acls */
  1937. if (found_key.objectid != objectid)
  1938. return 0;
  1939. /* we found an xattr, assume we've got an acl */
  1940. if (found_key.type == BTRFS_XATTR_ITEM_KEY)
  1941. return 1;
  1942. /*
  1943. * we found a key greater than an xattr key, there can't
  1944. * be any acls later on
  1945. */
  1946. if (found_key.type > BTRFS_XATTR_ITEM_KEY)
  1947. return 0;
  1948. slot++;
  1949. scanned++;
  1950. /*
  1951. * it goes inode, inode backrefs, xattrs, extents,
  1952. * so if there are a ton of hard links to an inode there can
  1953. * be a lot of backrefs. Don't waste time searching too hard,
  1954. * this is just an optimization
  1955. */
  1956. if (scanned >= 8)
  1957. break;
  1958. }
  1959. /* we hit the end of the leaf before we found an xattr or
  1960. * something larger than an xattr. We have to assume the inode
  1961. * has acls
  1962. */
  1963. return 1;
  1964. }
  1965. /*
  1966. * read an inode from the btree into the in-memory inode
  1967. */
  1968. static void btrfs_read_locked_inode(struct inode *inode)
  1969. {
  1970. struct btrfs_path *path;
  1971. struct extent_buffer *leaf;
  1972. struct btrfs_inode_item *inode_item;
  1973. struct btrfs_timespec *tspec;
  1974. struct btrfs_root *root = BTRFS_I(inode)->root;
  1975. struct btrfs_key location;
  1976. int maybe_acls;
  1977. u64 alloc_group_block;
  1978. u32 rdev;
  1979. int ret;
  1980. path = btrfs_alloc_path();
  1981. BUG_ON(!path);
  1982. memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
  1983. ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
  1984. if (ret)
  1985. goto make_bad;
  1986. leaf = path->nodes[0];
  1987. inode_item = btrfs_item_ptr(leaf, path->slots[0],
  1988. struct btrfs_inode_item);
  1989. inode->i_mode = btrfs_inode_mode(leaf, inode_item);
  1990. inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
  1991. inode->i_uid = btrfs_inode_uid(leaf, inode_item);
  1992. inode->i_gid = btrfs_inode_gid(leaf, inode_item);
  1993. btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
  1994. tspec = btrfs_inode_atime(inode_item);
  1995. inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
  1996. inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
  1997. tspec = btrfs_inode_mtime(inode_item);
  1998. inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
  1999. inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
  2000. tspec = btrfs_inode_ctime(inode_item);
  2001. inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
  2002. inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
  2003. inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
  2004. BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
  2005. BTRFS_I(inode)->sequence = btrfs_inode_sequence(leaf, inode_item);
  2006. inode->i_generation = BTRFS_I(inode)->generation;
  2007. inode->i_rdev = 0;
  2008. rdev = btrfs_inode_rdev(leaf, inode_item);
  2009. BTRFS_I(inode)->index_cnt = (u64)-1;
  2010. BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
  2011. alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
  2012. /*
  2013. * try to precache a NULL acl entry for files that don't have
  2014. * any xattrs or acls
  2015. */
  2016. maybe_acls = acls_after_inode_item(leaf, path->slots[0], inode->i_ino);
  2017. if (!maybe_acls)
  2018. cache_no_acl(inode);
  2019. BTRFS_I(inode)->block_group = btrfs_find_block_group(root, 0,
  2020. alloc_group_block, 0);
  2021. btrfs_free_path(path);
  2022. inode_item = NULL;
  2023. switch (inode->i_mode & S_IFMT) {
  2024. case S_IFREG:
  2025. inode->i_mapping->a_ops = &btrfs_aops;
  2026. inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
  2027. BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
  2028. inode->i_fop = &btrfs_file_operations;
  2029. inode->i_op = &btrfs_file_inode_operations;
  2030. break;
  2031. case S_IFDIR:
  2032. inode->i_fop = &btrfs_dir_file_operations;
  2033. if (root == root->fs_info->tree_root)
  2034. inode->i_op = &btrfs_dir_ro_inode_operations;
  2035. else
  2036. inode->i_op = &btrfs_dir_inode_operations;
  2037. break;
  2038. case S_IFLNK:
  2039. inode->i_op = &btrfs_symlink_inode_operations;
  2040. inode->i_mapping->a_ops = &btrfs_symlink_aops;
  2041. inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
  2042. break;
  2043. default:
  2044. inode->i_op = &btrfs_special_inode_operations;
  2045. init_special_inode(inode, inode->i_mode, rdev);
  2046. break;
  2047. }
  2048. btrfs_update_iflags(inode);
  2049. return;
  2050. make_bad:
  2051. btrfs_free_path(path);
  2052. make_bad_inode(inode);
  2053. }
  2054. /*
  2055. * given a leaf and an inode, copy the inode fields into the leaf
  2056. */
  2057. static void fill_inode_item(struct btrfs_trans_handle *trans,
  2058. struct extent_buffer *leaf,
  2059. struct btrfs_inode_item *item,
  2060. struct inode *inode)
  2061. {
  2062. btrfs_set_inode_uid(leaf, item, inode->i_uid);
  2063. btrfs_set_inode_gid(leaf, item, inode->i_gid);
  2064. btrfs_set_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size);
  2065. btrfs_set_inode_mode(leaf, item, inode->i_mode);
  2066. btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
  2067. btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
  2068. inode->i_atime.tv_sec);
  2069. btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
  2070. inode->i_atime.tv_nsec);
  2071. btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
  2072. inode->i_mtime.tv_sec);
  2073. btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
  2074. inode->i_mtime.tv_nsec);
  2075. btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
  2076. inode->i_ctime.tv_sec);
  2077. btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
  2078. inode->i_ctime.tv_nsec);
  2079. btrfs_set_inode_nbytes(leaf, item, inode_get_bytes(inode));
  2080. btrfs_set_inode_generation(leaf, item, BTRFS_I(inode)->generation);
  2081. btrfs_set_inode_sequence(leaf, item, BTRFS_I(inode)->sequence);
  2082. btrfs_set_inode_transid(leaf, item, trans->transid);
  2083. btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
  2084. btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
  2085. btrfs_set_inode_block_group(leaf, item, BTRFS_I(inode)->block_group);
  2086. }
  2087. /*
  2088. * copy everything in the in-memory inode into the btree.
  2089. */
  2090. noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
  2091. struct btrfs_root *root, struct inode *inode)
  2092. {
  2093. struct btrfs_inode_item *inode_item;
  2094. struct btrfs_path *path;
  2095. struct extent_buffer *leaf;
  2096. int ret;
  2097. path = btrfs_alloc_path();
  2098. BUG_ON(!path);
  2099. path->leave_spinning = 1;
  2100. ret = btrfs_lookup_inode(trans, root, path,
  2101. &BTRFS_I(inode)->location, 1);
  2102. if (ret) {
  2103. if (ret > 0)
  2104. ret = -ENOENT;
  2105. goto failed;
  2106. }
  2107. btrfs_unlock_up_safe(path, 1);
  2108. leaf = path->nodes[0];
  2109. inode_item = btrfs_item_ptr(leaf, path->slots[0],
  2110. struct btrfs_inode_item);
  2111. fill_inode_item(trans, leaf, inode_item, inode);
  2112. btrfs_mark_buffer_dirty(leaf);
  2113. btrfs_set_inode_last_trans(trans, inode);
  2114. ret = 0;
  2115. failed:
  2116. btrfs_free_path(path);
  2117. return ret;
  2118. }
  2119. /*
  2120. * unlink helper that gets used here in inode.c and in the tree logging
  2121. * recovery code. It remove a link in a directory with a given name, and
  2122. * also drops the back refs in the inode to the directory
  2123. */
  2124. int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
  2125. struct btrfs_root *root,
  2126. struct inode *dir, struct inode *inode,
  2127. const char *name, int name_len)
  2128. {
  2129. struct btrfs_path *path;
  2130. int ret = 0;
  2131. struct extent_buffer *leaf;
  2132. struct btrfs_dir_item *di;
  2133. struct btrfs_key key;
  2134. u64 index;
  2135. path = btrfs_alloc_path();
  2136. if (!path) {
  2137. ret = -ENOMEM;
  2138. goto err;
  2139. }
  2140. path->leave_spinning = 1;
  2141. di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
  2142. name, name_len, -1);
  2143. if (IS_ERR(di)) {
  2144. ret = PTR_ERR(di);
  2145. goto err;
  2146. }
  2147. if (!di) {
  2148. ret = -ENOENT;
  2149. goto err;
  2150. }
  2151. leaf = path->nodes[0];
  2152. btrfs_dir_item_key_to_cpu(leaf, di, &key);
  2153. ret = btrfs_delete_one_dir_name(trans, root, path, di);
  2154. if (ret)
  2155. goto err;
  2156. btrfs_release_path(root, path);
  2157. ret = btrfs_del_inode_ref(trans, root, name, name_len,
  2158. inode->i_ino,
  2159. dir->i_ino, &index);
  2160. if (ret) {
  2161. printk(KERN_INFO "btrfs failed to delete reference to %.*s, "
  2162. "inode %lu parent %lu\n", name_len, name,
  2163. inode->i_ino, dir->i_ino);
  2164. goto err;
  2165. }
  2166. di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
  2167. index, name, name_len, -1);
  2168. if (IS_ERR(di)) {
  2169. ret = PTR_ERR(di);
  2170. goto err;
  2171. }
  2172. if (!di) {
  2173. ret = -ENOENT;
  2174. goto err;
  2175. }
  2176. ret = btrfs_delete_one_dir_name(trans, root, path, di);
  2177. btrfs_release_path(root, path);
  2178. ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
  2179. inode, dir->i_ino);
  2180. BUG_ON(ret != 0 && ret != -ENOENT);
  2181. ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
  2182. dir, index);
  2183. BUG_ON(ret);
  2184. err:
  2185. btrfs_free_path(path);
  2186. if (ret)
  2187. goto out;
  2188. btrfs_i_size_write(dir, dir->i_size - name_len * 2);
  2189. inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
  2190. btrfs_update_inode(trans, root, dir);
  2191. btrfs_drop_nlink(inode);
  2192. ret = btrfs_update_inode(trans, root, inode);
  2193. out:
  2194. return ret;
  2195. }
  2196. static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
  2197. {
  2198. struct btrfs_root *root;
  2199. struct btrfs_trans_handle *trans;
  2200. struct inode *inode = dentry->d_inode;
  2201. int ret;
  2202. unsigned long nr = 0;
  2203. root = BTRFS_I(dir)->root;
  2204. trans = btrfs_start_transaction(root, 1);
  2205. btrfs_set_trans_block_group(trans, dir);
  2206. btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0);
  2207. ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
  2208. dentry->d_name.name, dentry->d_name.len);
  2209. if (inode->i_nlink == 0)
  2210. ret = btrfs_orphan_add(trans, inode);
  2211. nr = trans->blocks_used;
  2212. btrfs_end_transaction_throttle(trans, root);
  2213. btrfs_btree_balance_dirty(root, nr);
  2214. return ret;
  2215. }
  2216. int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
  2217. struct btrfs_root *root,
  2218. struct inode *dir, u64 objectid,
  2219. const char *name, int name_len)
  2220. {
  2221. struct btrfs_path *path;
  2222. struct extent_buffer *leaf;
  2223. struct btrfs_dir_item *di;
  2224. struct btrfs_key key;
  2225. u64 index;
  2226. int ret;
  2227. path = btrfs_alloc_path();
  2228. if (!path)
  2229. return -ENOMEM;
  2230. di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
  2231. name, name_len, -1);
  2232. BUG_ON(!di || IS_ERR(di));
  2233. leaf = path->nodes[0];
  2234. btrfs_dir_item_key_to_cpu(leaf, di, &key);
  2235. WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
  2236. ret = btrfs_delete_one_dir_name(trans, root, path, di);
  2237. BUG_ON(ret);
  2238. btrfs_release_path(root, path);
  2239. ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
  2240. objectid, root->root_key.objectid,
  2241. dir->i_ino, &index, name, name_len);
  2242. if (ret < 0) {
  2243. BUG_ON(ret != -ENOENT);
  2244. di = btrfs_search_dir_index_item(root, path, dir->i_ino,
  2245. name, name_len);
  2246. BUG_ON(!di || IS_ERR(di));
  2247. leaf = path->nodes[0];
  2248. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  2249. btrfs_release_path(root, path);
  2250. index = key.offset;
  2251. }
  2252. di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
  2253. index, name, name_len, -1);
  2254. BUG_ON(!di || IS_ERR(di));
  2255. leaf = path->nodes[0];
  2256. btrfs_dir_item_key_to_cpu(leaf, di, &key);
  2257. WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
  2258. ret = btrfs_delete_one_dir_name(trans, root, path, di);
  2259. BUG_ON(ret);
  2260. btrfs_release_path(root, path);
  2261. btrfs_i_size_write(dir, dir->i_size - name_len * 2);
  2262. dir->i_mtime = dir->i_ctime = CURRENT_TIME;
  2263. ret = btrfs_update_inode(trans, root, dir);
  2264. BUG_ON(ret);
  2265. dir->i_sb->s_dirt = 1;
  2266. btrfs_free_path(path);
  2267. return 0;
  2268. }
  2269. static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
  2270. {
  2271. struct inode *inode = dentry->d_inode;
  2272. int err = 0;
  2273. int ret;
  2274. struct btrfs_root *root = BTRFS_I(dir)->root;
  2275. struct btrfs_trans_handle *trans;
  2276. unsigned long nr = 0;
  2277. if (inode->i_size > BTRFS_EMPTY_DIR_SIZE ||
  2278. inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
  2279. return -ENOTEMPTY;
  2280. trans = btrfs_start_transaction(root, 1);
  2281. btrfs_set_trans_block_group(trans, dir);
  2282. if (unlikely(inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
  2283. err = btrfs_unlink_subvol(trans, root, dir,
  2284. BTRFS_I(inode)->location.objectid,
  2285. dentry->d_name.name,
  2286. dentry->d_name.len);
  2287. goto out;
  2288. }
  2289. err = btrfs_orphan_add(trans, inode);
  2290. if (err)
  2291. goto out;
  2292. /* now the directory is empty */
  2293. err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
  2294. dentry->d_name.name, dentry->d_name.len);
  2295. if (!err)
  2296. btrfs_i_size_write(inode, 0);
  2297. out:
  2298. nr = trans->blocks_used;
  2299. ret = btrfs_end_transaction_throttle(trans, root);
  2300. btrfs_btree_balance_dirty(root, nr);
  2301. if (ret && !err)
  2302. err = ret;
  2303. return err;
  2304. }
  2305. #if 0
  2306. /*
  2307. * when truncating bytes in a file, it is possible to avoid reading
  2308. * the leaves that contain only checksum items. This can be the
  2309. * majority of the IO required to delete a large file, but it must
  2310. * be done carefully.
  2311. *
  2312. * The keys in the level just above the leaves are checked to make sure
  2313. * the lowest key in a given leaf is a csum key, and starts at an offset
  2314. * after the new size.
  2315. *
  2316. * Then the key for the next leaf is checked to make sure it also has
  2317. * a checksum item for the same file. If it does, we know our target leaf
  2318. * contains only checksum items, and it can be safely freed without reading
  2319. * it.
  2320. *
  2321. * This is just an optimization targeted at large files. It may do
  2322. * nothing. It will return 0 unless things went badly.
  2323. */
  2324. static noinline int drop_csum_leaves(struct btrfs_trans_handle *trans,
  2325. struct btrfs_root *root,
  2326. struct btrfs_path *path,
  2327. struct inode *inode, u64 new_size)
  2328. {
  2329. struct btrfs_key key;
  2330. int ret;
  2331. int nritems;
  2332. struct btrfs_key found_key;
  2333. struct btrfs_key other_key;
  2334. struct btrfs_leaf_ref *ref;
  2335. u64 leaf_gen;
  2336. u64 leaf_start;
  2337. path->lowest_level = 1;
  2338. key.objectid = inode->i_ino;
  2339. key.type = BTRFS_CSUM_ITEM_KEY;
  2340. key.offset = new_size;
  2341. again:
  2342. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  2343. if (ret < 0)
  2344. goto out;
  2345. if (path->nodes[1] == NULL) {
  2346. ret = 0;
  2347. goto out;
  2348. }
  2349. ret = 0;
  2350. btrfs_node_key_to_cpu(path->nodes[1], &found_key, path->slots[1]);
  2351. nritems = btrfs_header_nritems(path->nodes[1]);
  2352. if (!nritems)
  2353. goto out;
  2354. if (path->slots[1] >= nritems)
  2355. goto next_node;
  2356. /* did we find a key greater than anything we want to delete? */
  2357. if (found_key.objectid > inode->i_ino ||
  2358. (found_key.objectid == inode->i_ino && found_key.type > key.type))
  2359. goto out;
  2360. /* we check the next key in the node to make sure the leave contains
  2361. * only checksum items. This comparison doesn't work if our
  2362. * leaf is the last one in the node
  2363. */
  2364. if (path->slots[1] + 1 >= nritems) {
  2365. next_node:
  2366. /* search forward from the last key in the node, this
  2367. * will bring us into the next node in the tree
  2368. */
  2369. btrfs_node_key_to_cpu(path->nodes[1], &found_key, nritems - 1);
  2370. /* unlikely, but we inc below, so check to be safe */
  2371. if (found_key.offset == (u64)-1)
  2372. goto out;
  2373. /* search_forward needs a path with locks held, do the
  2374. * search again for the original key. It is possible
  2375. * this will race with a balance and return a path that
  2376. * we could modify, but this drop is just an optimization
  2377. * and is allowed to miss some leaves.
  2378. */
  2379. btrfs_release_path(root, path);
  2380. found_key.offset++;
  2381. /* setup a max key for search_forward */
  2382. other_key.offset = (u64)-1;
  2383. other_key.type = key.type;
  2384. other_key.objectid = key.objectid;
  2385. path->keep_locks = 1;
  2386. ret = btrfs_search_forward(root, &found_key, &other_key,
  2387. path, 0, 0);
  2388. path->keep_locks = 0;
  2389. if (ret || found_key.objectid != key.objectid ||
  2390. found_key.type != key.type) {
  2391. ret = 0;
  2392. goto out;
  2393. }
  2394. key.offset = found_key.offset;
  2395. btrfs_release_path(root, path);
  2396. cond_resched();
  2397. goto again;
  2398. }
  2399. /* we know there's one more slot after us in the tree,
  2400. * read that key so we can verify it is also a checksum item
  2401. */
  2402. btrfs_node_key_to_cpu(path->nodes[1], &other_key, path->slots[1] + 1);
  2403. if (found_key.objectid < inode->i_ino)
  2404. goto next_key;
  2405. if (found_key.type != key.type || found_key.offset < new_size)
  2406. goto next_key;
  2407. /*
  2408. * if the key for the next leaf isn't a csum key from this objectid,
  2409. * we can't be sure there aren't good items inside this leaf.
  2410. * Bail out
  2411. */
  2412. if (other_key.objectid != inode->i_ino || other_key.type != key.type)
  2413. goto out;
  2414. leaf_start = btrfs_node_blockptr(path->nodes[1], path->slots[1]);
  2415. leaf_gen = btrfs_node_ptr_generation(path->nodes[1], path->slots[1]);
  2416. /*
  2417. * it is safe to delete this leaf, it contains only
  2418. * csum items from this inode at an offset >= new_size
  2419. */
  2420. ret = btrfs_del_leaf(trans, root, path, leaf_start);
  2421. BUG_ON(ret);
  2422. if (root->ref_cows && leaf_gen < trans->transid) {
  2423. ref = btrfs_alloc_leaf_ref(root, 0);
  2424. if (ref) {
  2425. ref->root_gen = root->root_key.offset;
  2426. ref->bytenr = leaf_start;
  2427. ref->owner = 0;
  2428. ref->generation = leaf_gen;
  2429. ref->nritems = 0;
  2430. btrfs_sort_leaf_ref(ref);
  2431. ret = btrfs_add_leaf_ref(root, ref, 0);
  2432. WARN_ON(ret);
  2433. btrfs_free_leaf_ref(root, ref);
  2434. } else {
  2435. WARN_ON(1);
  2436. }
  2437. }
  2438. next_key:
  2439. btrfs_release_path(root, path);
  2440. if (other_key.objectid == inode->i_ino &&
  2441. other_key.type == key.type && other_key.offset > key.offset) {
  2442. key.offset = other_key.offset;
  2443. cond_resched();
  2444. goto again;
  2445. }
  2446. ret = 0;
  2447. out:
  2448. /* fixup any changes we've made to the path */
  2449. path->lowest_level = 0;
  2450. path->keep_locks = 0;
  2451. btrfs_release_path(root, path);
  2452. return ret;
  2453. }
  2454. #endif
  2455. /*
  2456. * this can truncate away extent items, csum items and directory items.
  2457. * It starts at a high offset and removes keys until it can't find
  2458. * any higher than new_size
  2459. *
  2460. * csum items that cross the new i_size are truncated to the new size
  2461. * as well.
  2462. *
  2463. * min_type is the minimum key type to truncate down to. If set to 0, this
  2464. * will kill all the items on this inode, including the INODE_ITEM_KEY.
  2465. */
  2466. noinline int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
  2467. struct btrfs_root *root,
  2468. struct inode *inode,
  2469. u64 new_size, u32 min_type)
  2470. {
  2471. int ret;
  2472. struct btrfs_path *path;
  2473. struct btrfs_key key;
  2474. struct btrfs_key found_key;
  2475. u32 found_type = (u8)-1;
  2476. struct extent_buffer *leaf;
  2477. struct btrfs_file_extent_item *fi;
  2478. u64 extent_start = 0;
  2479. u64 extent_num_bytes = 0;
  2480. u64 extent_offset = 0;
  2481. u64 item_end = 0;
  2482. int found_extent;
  2483. int del_item;
  2484. int pending_del_nr = 0;
  2485. int pending_del_slot = 0;
  2486. int extent_type = -1;
  2487. int encoding;
  2488. u64 mask = root->sectorsize - 1;
  2489. if (root->ref_cows)
  2490. btrfs_drop_extent_cache(inode, new_size & (~mask), (u64)-1, 0);
  2491. path = btrfs_alloc_path();
  2492. BUG_ON(!path);
  2493. path->reada = -1;
  2494. /* FIXME, add redo link to tree so we don't leak on crash */
  2495. key.objectid = inode->i_ino;
  2496. key.offset = (u64)-1;
  2497. key.type = (u8)-1;
  2498. search_again:
  2499. path->leave_spinning = 1;
  2500. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  2501. if (ret < 0)
  2502. goto error;
  2503. if (ret > 0) {
  2504. /* there are no items in the tree for us to truncate, we're
  2505. * done
  2506. */
  2507. if (path->slots[0] == 0) {
  2508. ret = 0;
  2509. goto error;
  2510. }
  2511. path->slots[0]--;
  2512. }
  2513. while (1) {
  2514. fi = NULL;
  2515. leaf = path->nodes[0];
  2516. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  2517. found_type = btrfs_key_type(&found_key);
  2518. encoding = 0;
  2519. if (found_key.objectid != inode->i_ino)
  2520. break;
  2521. if (found_type < min_type)
  2522. break;
  2523. item_end = found_key.offset;
  2524. if (found_type == BTRFS_EXTENT_DATA_KEY) {
  2525. fi = btrfs_item_ptr(leaf, path->slots[0],
  2526. struct btrfs_file_extent_item);
  2527. extent_type = btrfs_file_extent_type(leaf, fi);
  2528. encoding = btrfs_file_extent_compression(leaf, fi);
  2529. encoding |= btrfs_file_extent_encryption(leaf, fi);
  2530. encoding |= btrfs_file_extent_other_encoding(leaf, fi);
  2531. if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
  2532. item_end +=
  2533. btrfs_file_extent_num_bytes(leaf, fi);
  2534. } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
  2535. item_end += btrfs_file_extent_inline_len(leaf,
  2536. fi);
  2537. }
  2538. item_end--;
  2539. }
  2540. if (item_end < new_size) {
  2541. if (found_type == BTRFS_DIR_ITEM_KEY)
  2542. found_type = BTRFS_INODE_ITEM_KEY;
  2543. else if (found_type == BTRFS_EXTENT_ITEM_KEY)
  2544. found_type = BTRFS_EXTENT_DATA_KEY;
  2545. else if (found_type == BTRFS_EXTENT_DATA_KEY)
  2546. found_type = BTRFS_XATTR_ITEM_KEY;
  2547. else if (found_type == BTRFS_XATTR_ITEM_KEY)
  2548. found_type = BTRFS_INODE_REF_KEY;
  2549. else if (found_type)
  2550. found_type--;
  2551. else
  2552. break;
  2553. btrfs_set_key_type(&key, found_type);
  2554. goto next;
  2555. }
  2556. if (found_key.offset >= new_size)
  2557. del_item = 1;
  2558. else
  2559. del_item = 0;
  2560. found_extent = 0;
  2561. /* FIXME, shrink the extent if the ref count is only 1 */
  2562. if (found_type != BTRFS_EXTENT_DATA_KEY)
  2563. goto delete;
  2564. if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
  2565. u64 num_dec;
  2566. extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
  2567. if (!del_item && !encoding) {
  2568. u64 orig_num_bytes =
  2569. btrfs_file_extent_num_bytes(leaf, fi);
  2570. extent_num_bytes = new_size -
  2571. found_key.offset + root->sectorsize - 1;
  2572. extent_num_bytes = extent_num_bytes &
  2573. ~((u64)root->sectorsize - 1);
  2574. btrfs_set_file_extent_num_bytes(leaf, fi,
  2575. extent_num_bytes);
  2576. num_dec = (orig_num_bytes -
  2577. extent_num_bytes);
  2578. if (root->ref_cows && extent_start != 0)
  2579. inode_sub_bytes(inode, num_dec);
  2580. btrfs_mark_buffer_dirty(leaf);
  2581. } else {
  2582. extent_num_bytes =
  2583. btrfs_file_extent_disk_num_bytes(leaf,
  2584. fi);
  2585. extent_offset = found_key.offset -
  2586. btrfs_file_extent_offset(leaf, fi);
  2587. /* FIXME blocksize != 4096 */
  2588. num_dec = btrfs_file_extent_num_bytes(leaf, fi);
  2589. if (extent_start != 0) {
  2590. found_extent = 1;
  2591. if (root->ref_cows)
  2592. inode_sub_bytes(inode, num_dec);
  2593. }
  2594. }
  2595. } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
  2596. /*
  2597. * we can't truncate inline items that have had
  2598. * special encodings
  2599. */
  2600. if (!del_item &&
  2601. btrfs_file_extent_compression(leaf, fi) == 0 &&
  2602. btrfs_file_extent_encryption(leaf, fi) == 0 &&
  2603. btrfs_file_extent_other_encoding(leaf, fi) == 0) {
  2604. u32 size = new_size - found_key.offset;
  2605. if (root->ref_cows) {
  2606. inode_sub_bytes(inode, item_end + 1 -
  2607. new_size);
  2608. }
  2609. size =
  2610. btrfs_file_extent_calc_inline_size(size);
  2611. ret = btrfs_truncate_item(trans, root, path,
  2612. size, 1);
  2613. BUG_ON(ret);
  2614. } else if (root->ref_cows) {
  2615. inode_sub_bytes(inode, item_end + 1 -
  2616. found_key.offset);
  2617. }
  2618. }
  2619. delete:
  2620. if (del_item) {
  2621. if (!pending_del_nr) {
  2622. /* no pending yet, add ourselves */
  2623. pending_del_slot = path->slots[0];
  2624. pending_del_nr = 1;
  2625. } else if (pending_del_nr &&
  2626. path->slots[0] + 1 == pending_del_slot) {
  2627. /* hop on the pending chunk */
  2628. pending_del_nr++;
  2629. pending_del_slot = path->slots[0];
  2630. } else {
  2631. BUG();
  2632. }
  2633. } else {
  2634. break;
  2635. }
  2636. if (found_extent && root->ref_cows) {
  2637. btrfs_set_path_blocking(path);
  2638. ret = btrfs_free_extent(trans, root, extent_start,
  2639. extent_num_bytes, 0,
  2640. btrfs_header_owner(leaf),
  2641. inode->i_ino, extent_offset);
  2642. BUG_ON(ret);
  2643. }
  2644. next:
  2645. if (path->slots[0] == 0) {
  2646. if (pending_del_nr)
  2647. goto del_pending;
  2648. btrfs_release_path(root, path);
  2649. if (found_type == BTRFS_INODE_ITEM_KEY)
  2650. break;
  2651. goto search_again;
  2652. }
  2653. path->slots[0]--;
  2654. if (pending_del_nr &&
  2655. path->slots[0] + 1 != pending_del_slot) {
  2656. struct btrfs_key debug;
  2657. del_pending:
  2658. btrfs_item_key_to_cpu(path->nodes[0], &debug,
  2659. pending_del_slot);
  2660. ret = btrfs_del_items(trans, root, path,
  2661. pending_del_slot,
  2662. pending_del_nr);
  2663. BUG_ON(ret);
  2664. pending_del_nr = 0;
  2665. btrfs_release_path(root, path);
  2666. if (found_type == BTRFS_INODE_ITEM_KEY)
  2667. break;
  2668. goto search_again;
  2669. }
  2670. }
  2671. ret = 0;
  2672. error:
  2673. if (pending_del_nr) {
  2674. ret = btrfs_del_items(trans, root, path, pending_del_slot,
  2675. pending_del_nr);
  2676. }
  2677. btrfs_free_path(path);
  2678. return ret;
  2679. }
  2680. /*
  2681. * taken from block_truncate_page, but does cow as it zeros out
  2682. * any bytes left in the last page in the file.
  2683. */
  2684. static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
  2685. {
  2686. struct inode *inode = mapping->host;
  2687. struct btrfs_root *root = BTRFS_I(inode)->root;
  2688. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  2689. struct btrfs_ordered_extent *ordered;
  2690. char *kaddr;
  2691. u32 blocksize = root->sectorsize;
  2692. pgoff_t index = from >> PAGE_CACHE_SHIFT;
  2693. unsigned offset = from & (PAGE_CACHE_SIZE-1);
  2694. struct page *page;
  2695. int ret = 0;
  2696. u64 page_start;
  2697. u64 page_end;
  2698. if ((offset & (blocksize - 1)) == 0)
  2699. goto out;
  2700. ret = btrfs_check_data_free_space(root, inode, PAGE_CACHE_SIZE);
  2701. if (ret)
  2702. goto out;
  2703. ret = btrfs_reserve_metadata_for_delalloc(root, inode, 1);
  2704. if (ret)
  2705. goto out;
  2706. ret = -ENOMEM;
  2707. again:
  2708. page = grab_cache_page(mapping, index);
  2709. if (!page) {
  2710. btrfs_free_reserved_data_space(root, inode, PAGE_CACHE_SIZE);
  2711. btrfs_unreserve_metadata_for_delalloc(root, inode, 1);
  2712. goto out;
  2713. }
  2714. page_start = page_offset(page);
  2715. page_end = page_start + PAGE_CACHE_SIZE - 1;
  2716. if (!PageUptodate(page)) {
  2717. ret = btrfs_readpage(NULL, page);
  2718. lock_page(page);
  2719. if (page->mapping != mapping) {
  2720. unlock_page(page);
  2721. page_cache_release(page);
  2722. goto again;
  2723. }
  2724. if (!PageUptodate(page)) {
  2725. ret = -EIO;
  2726. goto out_unlock;
  2727. }
  2728. }
  2729. wait_on_page_writeback(page);
  2730. lock_extent(io_tree, page_start, page_end, GFP_NOFS);
  2731. set_page_extent_mapped(page);
  2732. ordered = btrfs_lookup_ordered_extent(inode, page_start);
  2733. if (ordered) {
  2734. unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
  2735. unlock_page(page);
  2736. page_cache_release(page);
  2737. btrfs_start_ordered_extent(inode, ordered, 1);
  2738. btrfs_put_ordered_extent(ordered);
  2739. goto again;
  2740. }
  2741. clear_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end,
  2742. EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING,
  2743. GFP_NOFS);
  2744. ret = btrfs_set_extent_delalloc(inode, page_start, page_end);
  2745. if (ret) {
  2746. unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
  2747. goto out_unlock;
  2748. }
  2749. ret = 0;
  2750. if (offset != PAGE_CACHE_SIZE) {
  2751. kaddr = kmap(page);
  2752. memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
  2753. flush_dcache_page(page);
  2754. kunmap(page);
  2755. }
  2756. ClearPageChecked(page);
  2757. set_page_dirty(page);
  2758. unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
  2759. out_unlock:
  2760. if (ret)
  2761. btrfs_free_reserved_data_space(root, inode, PAGE_CACHE_SIZE);
  2762. btrfs_unreserve_metadata_for_delalloc(root, inode, 1);
  2763. unlock_page(page);
  2764. page_cache_release(page);
  2765. out:
  2766. return ret;
  2767. }
  2768. int btrfs_cont_expand(struct inode *inode, loff_t size)
  2769. {
  2770. struct btrfs_trans_handle *trans;
  2771. struct btrfs_root *root = BTRFS_I(inode)->root;
  2772. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  2773. struct extent_map *em;
  2774. u64 mask = root->sectorsize - 1;
  2775. u64 hole_start = (inode->i_size + mask) & ~mask;
  2776. u64 block_end = (size + mask) & ~mask;
  2777. u64 last_byte;
  2778. u64 cur_offset;
  2779. u64 hole_size;
  2780. int err = 0;
  2781. if (size <= hole_start)
  2782. return 0;
  2783. err = btrfs_truncate_page(inode->i_mapping, inode->i_size);
  2784. if (err)
  2785. return err;
  2786. while (1) {
  2787. struct btrfs_ordered_extent *ordered;
  2788. btrfs_wait_ordered_range(inode, hole_start,
  2789. block_end - hole_start);
  2790. lock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
  2791. ordered = btrfs_lookup_ordered_extent(inode, hole_start);
  2792. if (!ordered)
  2793. break;
  2794. unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
  2795. btrfs_put_ordered_extent(ordered);
  2796. }
  2797. trans = btrfs_start_transaction(root, 1);
  2798. btrfs_set_trans_block_group(trans, inode);
  2799. cur_offset = hole_start;
  2800. while (1) {
  2801. em = btrfs_get_extent(inode, NULL, 0, cur_offset,
  2802. block_end - cur_offset, 0);
  2803. BUG_ON(IS_ERR(em) || !em);
  2804. last_byte = min(extent_map_end(em), block_end);
  2805. last_byte = (last_byte + mask) & ~mask;
  2806. if (test_bit(EXTENT_FLAG_VACANCY, &em->flags)) {
  2807. u64 hint_byte = 0;
  2808. hole_size = last_byte - cur_offset;
  2809. err = btrfs_drop_extents(trans, root, inode,
  2810. cur_offset,
  2811. cur_offset + hole_size,
  2812. block_end,
  2813. cur_offset, &hint_byte, 1);
  2814. if (err)
  2815. break;
  2816. err = btrfs_reserve_metadata_space(root, 1);
  2817. if (err)
  2818. break;
  2819. err = btrfs_insert_file_extent(trans, root,
  2820. inode->i_ino, cur_offset, 0,
  2821. 0, hole_size, 0, hole_size,
  2822. 0, 0, 0);
  2823. btrfs_drop_extent_cache(inode, hole_start,
  2824. last_byte - 1, 0);
  2825. btrfs_unreserve_metadata_space(root, 1);
  2826. }
  2827. free_extent_map(em);
  2828. cur_offset = last_byte;
  2829. if (err || cur_offset >= block_end)
  2830. break;
  2831. }
  2832. btrfs_end_transaction(trans, root);
  2833. unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
  2834. return err;
  2835. }
  2836. static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
  2837. {
  2838. struct inode *inode = dentry->d_inode;
  2839. int err;
  2840. err = inode_change_ok(inode, attr);
  2841. if (err)
  2842. return err;
  2843. if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
  2844. if (attr->ia_size > inode->i_size) {
  2845. err = btrfs_cont_expand(inode, attr->ia_size);
  2846. if (err)
  2847. return err;
  2848. } else if (inode->i_size > 0 &&
  2849. attr->ia_size == 0) {
  2850. /* we're truncating a file that used to have good
  2851. * data down to zero. Make sure it gets into
  2852. * the ordered flush list so that any new writes
  2853. * get down to disk quickly.
  2854. */
  2855. BTRFS_I(inode)->ordered_data_close = 1;
  2856. }
  2857. }
  2858. err = inode_setattr(inode, attr);
  2859. if (!err && ((attr->ia_valid & ATTR_MODE)))
  2860. err = btrfs_acl_chmod(inode);
  2861. return err;
  2862. }
  2863. void btrfs_delete_inode(struct inode *inode)
  2864. {
  2865. struct btrfs_trans_handle *trans;
  2866. struct btrfs_root *root = BTRFS_I(inode)->root;
  2867. unsigned long nr;
  2868. int ret;
  2869. truncate_inode_pages(&inode->i_data, 0);
  2870. if (is_bad_inode(inode)) {
  2871. btrfs_orphan_del(NULL, inode);
  2872. goto no_delete;
  2873. }
  2874. btrfs_wait_ordered_range(inode, 0, (u64)-1);
  2875. if (inode->i_nlink > 0) {
  2876. BUG_ON(btrfs_root_refs(&root->root_item) != 0);
  2877. goto no_delete;
  2878. }
  2879. btrfs_i_size_write(inode, 0);
  2880. trans = btrfs_join_transaction(root, 1);
  2881. btrfs_set_trans_block_group(trans, inode);
  2882. ret = btrfs_truncate_inode_items(trans, root, inode, inode->i_size, 0);
  2883. if (ret) {
  2884. btrfs_orphan_del(NULL, inode);
  2885. goto no_delete_lock;
  2886. }
  2887. btrfs_orphan_del(trans, inode);
  2888. nr = trans->blocks_used;
  2889. clear_inode(inode);
  2890. btrfs_end_transaction(trans, root);
  2891. btrfs_btree_balance_dirty(root, nr);
  2892. return;
  2893. no_delete_lock:
  2894. nr = trans->blocks_used;
  2895. btrfs_end_transaction(trans, root);
  2896. btrfs_btree_balance_dirty(root, nr);
  2897. no_delete:
  2898. clear_inode(inode);
  2899. }
  2900. /*
  2901. * this returns the key found in the dir entry in the location pointer.
  2902. * If no dir entries were found, location->objectid is 0.
  2903. */
  2904. static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
  2905. struct btrfs_key *location)
  2906. {
  2907. const char *name = dentry->d_name.name;
  2908. int namelen = dentry->d_name.len;
  2909. struct btrfs_dir_item *di;
  2910. struct btrfs_path *path;
  2911. struct btrfs_root *root = BTRFS_I(dir)->root;
  2912. int ret = 0;
  2913. path = btrfs_alloc_path();
  2914. BUG_ON(!path);
  2915. di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
  2916. namelen, 0);
  2917. if (IS_ERR(di))
  2918. ret = PTR_ERR(di);
  2919. if (!di || IS_ERR(di))
  2920. goto out_err;
  2921. btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
  2922. out:
  2923. btrfs_free_path(path);
  2924. return ret;
  2925. out_err:
  2926. location->objectid = 0;
  2927. goto out;
  2928. }
  2929. /*
  2930. * when we hit a tree root in a directory, the btrfs part of the inode
  2931. * needs to be changed to reflect the root directory of the tree root. This
  2932. * is kind of like crossing a mount point.
  2933. */
  2934. static int fixup_tree_root_location(struct btrfs_root *root,
  2935. struct inode *dir,
  2936. struct dentry *dentry,
  2937. struct btrfs_key *location,
  2938. struct btrfs_root **sub_root)
  2939. {
  2940. struct btrfs_path *path;
  2941. struct btrfs_root *new_root;
  2942. struct btrfs_root_ref *ref;
  2943. struct extent_buffer *leaf;
  2944. int ret;
  2945. int err = 0;
  2946. path = btrfs_alloc_path();
  2947. if (!path) {
  2948. err = -ENOMEM;
  2949. goto out;
  2950. }
  2951. err = -ENOENT;
  2952. ret = btrfs_find_root_ref(root->fs_info->tree_root, path,
  2953. BTRFS_I(dir)->root->root_key.objectid,
  2954. location->objectid);
  2955. if (ret) {
  2956. if (ret < 0)
  2957. err = ret;
  2958. goto out;
  2959. }
  2960. leaf = path->nodes[0];
  2961. ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
  2962. if (btrfs_root_ref_dirid(leaf, ref) != dir->i_ino ||
  2963. btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
  2964. goto out;
  2965. ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
  2966. (unsigned long)(ref + 1),
  2967. dentry->d_name.len);
  2968. if (ret)
  2969. goto out;
  2970. btrfs_release_path(root->fs_info->tree_root, path);
  2971. new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
  2972. if (IS_ERR(new_root)) {
  2973. err = PTR_ERR(new_root);
  2974. goto out;
  2975. }
  2976. if (btrfs_root_refs(&new_root->root_item) == 0) {
  2977. err = -ENOENT;
  2978. goto out;
  2979. }
  2980. *sub_root = new_root;
  2981. location->objectid = btrfs_root_dirid(&new_root->root_item);
  2982. location->type = BTRFS_INODE_ITEM_KEY;
  2983. location->offset = 0;
  2984. err = 0;
  2985. out:
  2986. btrfs_free_path(path);
  2987. return err;
  2988. }
  2989. static void inode_tree_add(struct inode *inode)
  2990. {
  2991. struct btrfs_root *root = BTRFS_I(inode)->root;
  2992. struct btrfs_inode *entry;
  2993. struct rb_node **p;
  2994. struct rb_node *parent;
  2995. again:
  2996. p = &root->inode_tree.rb_node;
  2997. parent = NULL;
  2998. if (hlist_unhashed(&inode->i_hash))
  2999. return;
  3000. spin_lock(&root->inode_lock);
  3001. while (*p) {
  3002. parent = *p;
  3003. entry = rb_entry(parent, struct btrfs_inode, rb_node);
  3004. if (inode->i_ino < entry->vfs_inode.i_ino)
  3005. p = &parent->rb_left;
  3006. else if (inode->i_ino > entry->vfs_inode.i_ino)
  3007. p = &parent->rb_right;
  3008. else {
  3009. WARN_ON(!(entry->vfs_inode.i_state &
  3010. (I_WILL_FREE | I_FREEING | I_CLEAR)));
  3011. rb_erase(parent, &root->inode_tree);
  3012. RB_CLEAR_NODE(parent);
  3013. spin_unlock(&root->inode_lock);
  3014. goto again;
  3015. }
  3016. }
  3017. rb_link_node(&BTRFS_I(inode)->rb_node, parent, p);
  3018. rb_insert_color(&BTRFS_I(inode)->rb_node, &root->inode_tree);
  3019. spin_unlock(&root->inode_lock);
  3020. }
  3021. static void inode_tree_del(struct inode *inode)
  3022. {
  3023. struct btrfs_root *root = BTRFS_I(inode)->root;
  3024. int empty = 0;
  3025. spin_lock(&root->inode_lock);
  3026. if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
  3027. rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
  3028. RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
  3029. empty = RB_EMPTY_ROOT(&root->inode_tree);
  3030. }
  3031. spin_unlock(&root->inode_lock);
  3032. if (empty && btrfs_root_refs(&root->root_item) == 0) {
  3033. synchronize_srcu(&root->fs_info->subvol_srcu);
  3034. spin_lock(&root->inode_lock);
  3035. empty = RB_EMPTY_ROOT(&root->inode_tree);
  3036. spin_unlock(&root->inode_lock);
  3037. if (empty)
  3038. btrfs_add_dead_root(root);
  3039. }
  3040. }
  3041. int btrfs_invalidate_inodes(struct btrfs_root *root)
  3042. {
  3043. struct rb_node *node;
  3044. struct rb_node *prev;
  3045. struct btrfs_inode *entry;
  3046. struct inode *inode;
  3047. u64 objectid = 0;
  3048. WARN_ON(btrfs_root_refs(&root->root_item) != 0);
  3049. spin_lock(&root->inode_lock);
  3050. again:
  3051. node = root->inode_tree.rb_node;
  3052. prev = NULL;
  3053. while (node) {
  3054. prev = node;
  3055. entry = rb_entry(node, struct btrfs_inode, rb_node);
  3056. if (objectid < entry->vfs_inode.i_ino)
  3057. node = node->rb_left;
  3058. else if (objectid > entry->vfs_inode.i_ino)
  3059. node = node->rb_right;
  3060. else
  3061. break;
  3062. }
  3063. if (!node) {
  3064. while (prev) {
  3065. entry = rb_entry(prev, struct btrfs_inode, rb_node);
  3066. if (objectid <= entry->vfs_inode.i_ino) {
  3067. node = prev;
  3068. break;
  3069. }
  3070. prev = rb_next(prev);
  3071. }
  3072. }
  3073. while (node) {
  3074. entry = rb_entry(node, struct btrfs_inode, rb_node);
  3075. objectid = entry->vfs_inode.i_ino + 1;
  3076. inode = igrab(&entry->vfs_inode);
  3077. if (inode) {
  3078. spin_unlock(&root->inode_lock);
  3079. if (atomic_read(&inode->i_count) > 1)
  3080. d_prune_aliases(inode);
  3081. /*
  3082. * btrfs_drop_inode will remove it from
  3083. * the inode cache when its usage count
  3084. * hits zero.
  3085. */
  3086. iput(inode);
  3087. cond_resched();
  3088. spin_lock(&root->inode_lock);
  3089. goto again;
  3090. }
  3091. if (cond_resched_lock(&root->inode_lock))
  3092. goto again;
  3093. node = rb_next(node);
  3094. }
  3095. spin_unlock(&root->inode_lock);
  3096. return 0;
  3097. }
  3098. static noinline void init_btrfs_i(struct inode *inode)
  3099. {
  3100. struct btrfs_inode *bi = BTRFS_I(inode);
  3101. bi->generation = 0;
  3102. bi->sequence = 0;
  3103. bi->last_trans = 0;
  3104. bi->last_sub_trans = 0;
  3105. bi->logged_trans = 0;
  3106. bi->delalloc_bytes = 0;
  3107. bi->reserved_bytes = 0;
  3108. bi->disk_i_size = 0;
  3109. bi->flags = 0;
  3110. bi->index_cnt = (u64)-1;
  3111. bi->last_unlink_trans = 0;
  3112. bi->ordered_data_close = 0;
  3113. extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
  3114. extent_io_tree_init(&BTRFS_I(inode)->io_tree,
  3115. inode->i_mapping, GFP_NOFS);
  3116. extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
  3117. inode->i_mapping, GFP_NOFS);
  3118. INIT_LIST_HEAD(&BTRFS_I(inode)->delalloc_inodes);
  3119. INIT_LIST_HEAD(&BTRFS_I(inode)->ordered_operations);
  3120. RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
  3121. btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
  3122. mutex_init(&BTRFS_I(inode)->extent_mutex);
  3123. mutex_init(&BTRFS_I(inode)->log_mutex);
  3124. }
  3125. static int btrfs_init_locked_inode(struct inode *inode, void *p)
  3126. {
  3127. struct btrfs_iget_args *args = p;
  3128. inode->i_ino = args->ino;
  3129. init_btrfs_i(inode);
  3130. BTRFS_I(inode)->root = args->root;
  3131. btrfs_set_inode_space_info(args->root, inode);
  3132. return 0;
  3133. }
  3134. static int btrfs_find_actor(struct inode *inode, void *opaque)
  3135. {
  3136. struct btrfs_iget_args *args = opaque;
  3137. return args->ino == inode->i_ino &&
  3138. args->root == BTRFS_I(inode)->root;
  3139. }
  3140. static struct inode *btrfs_iget_locked(struct super_block *s,
  3141. u64 objectid,
  3142. struct btrfs_root *root)
  3143. {
  3144. struct inode *inode;
  3145. struct btrfs_iget_args args;
  3146. args.ino = objectid;
  3147. args.root = root;
  3148. inode = iget5_locked(s, objectid, btrfs_find_actor,
  3149. btrfs_init_locked_inode,
  3150. (void *)&args);
  3151. return inode;
  3152. }
  3153. /* Get an inode object given its location and corresponding root.
  3154. * Returns in *is_new if the inode was read from disk
  3155. */
  3156. struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
  3157. struct btrfs_root *root)
  3158. {
  3159. struct inode *inode;
  3160. inode = btrfs_iget_locked(s, location->objectid, root);
  3161. if (!inode)
  3162. return ERR_PTR(-ENOMEM);
  3163. if (inode->i_state & I_NEW) {
  3164. BTRFS_I(inode)->root = root;
  3165. memcpy(&BTRFS_I(inode)->location, location, sizeof(*location));
  3166. btrfs_read_locked_inode(inode);
  3167. inode_tree_add(inode);
  3168. unlock_new_inode(inode);
  3169. }
  3170. return inode;
  3171. }
  3172. static struct inode *new_simple_dir(struct super_block *s,
  3173. struct btrfs_key *key,
  3174. struct btrfs_root *root)
  3175. {
  3176. struct inode *inode = new_inode(s);
  3177. if (!inode)
  3178. return ERR_PTR(-ENOMEM);
  3179. init_btrfs_i(inode);
  3180. BTRFS_I(inode)->root = root;
  3181. memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
  3182. BTRFS_I(inode)->dummy_inode = 1;
  3183. inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
  3184. inode->i_op = &simple_dir_inode_operations;
  3185. inode->i_fop = &simple_dir_operations;
  3186. inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
  3187. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
  3188. return inode;
  3189. }
  3190. struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
  3191. {
  3192. struct inode *inode;
  3193. struct btrfs_root *root = BTRFS_I(dir)->root;
  3194. struct btrfs_root *sub_root = root;
  3195. struct btrfs_key location;
  3196. int index;
  3197. int ret;
  3198. dentry->d_op = &btrfs_dentry_operations;
  3199. if (dentry->d_name.len > BTRFS_NAME_LEN)
  3200. return ERR_PTR(-ENAMETOOLONG);
  3201. ret = btrfs_inode_by_name(dir, dentry, &location);
  3202. if (ret < 0)
  3203. return ERR_PTR(ret);
  3204. if (location.objectid == 0)
  3205. return NULL;
  3206. if (location.type == BTRFS_INODE_ITEM_KEY) {
  3207. inode = btrfs_iget(dir->i_sb, &location, root);
  3208. return inode;
  3209. }
  3210. BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
  3211. index = srcu_read_lock(&root->fs_info->subvol_srcu);
  3212. ret = fixup_tree_root_location(root, dir, dentry,
  3213. &location, &sub_root);
  3214. if (ret < 0) {
  3215. if (ret != -ENOENT)
  3216. inode = ERR_PTR(ret);
  3217. else
  3218. inode = new_simple_dir(dir->i_sb, &location, sub_root);
  3219. } else {
  3220. inode = btrfs_iget(dir->i_sb, &location, sub_root);
  3221. }
  3222. srcu_read_unlock(&root->fs_info->subvol_srcu, index);
  3223. return inode;
  3224. }
  3225. static int btrfs_dentry_delete(struct dentry *dentry)
  3226. {
  3227. struct btrfs_root *root;
  3228. if (!dentry->d_inode && !IS_ROOT(dentry))
  3229. dentry = dentry->d_parent;
  3230. if (dentry->d_inode) {
  3231. root = BTRFS_I(dentry->d_inode)->root;
  3232. if (btrfs_root_refs(&root->root_item) == 0)
  3233. return 1;
  3234. }
  3235. return 0;
  3236. }
  3237. static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
  3238. struct nameidata *nd)
  3239. {
  3240. struct inode *inode;
  3241. inode = btrfs_lookup_dentry(dir, dentry);
  3242. if (IS_ERR(inode))
  3243. return ERR_CAST(inode);
  3244. return d_splice_alias(inode, dentry);
  3245. }
  3246. static unsigned char btrfs_filetype_table[] = {
  3247. DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
  3248. };
  3249. static int btrfs_real_readdir(struct file *filp, void *dirent,
  3250. filldir_t filldir)
  3251. {
  3252. struct inode *inode = filp->f_dentry->d_inode;
  3253. struct btrfs_root *root = BTRFS_I(inode)->root;
  3254. struct btrfs_item *item;
  3255. struct btrfs_dir_item *di;
  3256. struct btrfs_key key;
  3257. struct btrfs_key found_key;
  3258. struct btrfs_path *path;
  3259. int ret;
  3260. u32 nritems;
  3261. struct extent_buffer *leaf;
  3262. int slot;
  3263. int advance;
  3264. unsigned char d_type;
  3265. int over = 0;
  3266. u32 di_cur;
  3267. u32 di_total;
  3268. u32 di_len;
  3269. int key_type = BTRFS_DIR_INDEX_KEY;
  3270. char tmp_name[32];
  3271. char *name_ptr;
  3272. int name_len;
  3273. /* FIXME, use a real flag for deciding about the key type */
  3274. if (root->fs_info->tree_root == root)
  3275. key_type = BTRFS_DIR_ITEM_KEY;
  3276. /* special case for "." */
  3277. if (filp->f_pos == 0) {
  3278. over = filldir(dirent, ".", 1,
  3279. 1, inode->i_ino,
  3280. DT_DIR);
  3281. if (over)
  3282. return 0;
  3283. filp->f_pos = 1;
  3284. }
  3285. /* special case for .., just use the back ref */
  3286. if (filp->f_pos == 1) {
  3287. u64 pino = parent_ino(filp->f_path.dentry);
  3288. over = filldir(dirent, "..", 2,
  3289. 2, pino, DT_DIR);
  3290. if (over)
  3291. return 0;
  3292. filp->f_pos = 2;
  3293. }
  3294. path = btrfs_alloc_path();
  3295. path->reada = 2;
  3296. btrfs_set_key_type(&key, key_type);
  3297. key.offset = filp->f_pos;
  3298. key.objectid = inode->i_ino;
  3299. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  3300. if (ret < 0)
  3301. goto err;
  3302. advance = 0;
  3303. while (1) {
  3304. leaf = path->nodes[0];
  3305. nritems = btrfs_header_nritems(leaf);
  3306. slot = path->slots[0];
  3307. if (advance || slot >= nritems) {
  3308. if (slot >= nritems - 1) {
  3309. ret = btrfs_next_leaf(root, path);
  3310. if (ret)
  3311. break;
  3312. leaf = path->nodes[0];
  3313. nritems = btrfs_header_nritems(leaf);
  3314. slot = path->slots[0];
  3315. } else {
  3316. slot++;
  3317. path->slots[0]++;
  3318. }
  3319. }
  3320. advance = 1;
  3321. item = btrfs_item_nr(leaf, slot);
  3322. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  3323. if (found_key.objectid != key.objectid)
  3324. break;
  3325. if (btrfs_key_type(&found_key) != key_type)
  3326. break;
  3327. if (found_key.offset < filp->f_pos)
  3328. continue;
  3329. filp->f_pos = found_key.offset;
  3330. di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
  3331. di_cur = 0;
  3332. di_total = btrfs_item_size(leaf, item);
  3333. while (di_cur < di_total) {
  3334. struct btrfs_key location;
  3335. name_len = btrfs_dir_name_len(leaf, di);
  3336. if (name_len <= sizeof(tmp_name)) {
  3337. name_ptr = tmp_name;
  3338. } else {
  3339. name_ptr = kmalloc(name_len, GFP_NOFS);
  3340. if (!name_ptr) {
  3341. ret = -ENOMEM;
  3342. goto err;
  3343. }
  3344. }
  3345. read_extent_buffer(leaf, name_ptr,
  3346. (unsigned long)(di + 1), name_len);
  3347. d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
  3348. btrfs_dir_item_key_to_cpu(leaf, di, &location);
  3349. /* is this a reference to our own snapshot? If so
  3350. * skip it
  3351. */
  3352. if (location.type == BTRFS_ROOT_ITEM_KEY &&
  3353. location.objectid == root->root_key.objectid) {
  3354. over = 0;
  3355. goto skip;
  3356. }
  3357. over = filldir(dirent, name_ptr, name_len,
  3358. found_key.offset, location.objectid,
  3359. d_type);
  3360. skip:
  3361. if (name_ptr != tmp_name)
  3362. kfree(name_ptr);
  3363. if (over)
  3364. goto nopos;
  3365. di_len = btrfs_dir_name_len(leaf, di) +
  3366. btrfs_dir_data_len(leaf, di) + sizeof(*di);
  3367. di_cur += di_len;
  3368. di = (struct btrfs_dir_item *)((char *)di + di_len);
  3369. }
  3370. }
  3371. /* Reached end of directory/root. Bump pos past the last item. */
  3372. if (key_type == BTRFS_DIR_INDEX_KEY)
  3373. filp->f_pos = INT_LIMIT(off_t);
  3374. else
  3375. filp->f_pos++;
  3376. nopos:
  3377. ret = 0;
  3378. err:
  3379. btrfs_free_path(path);
  3380. return ret;
  3381. }
  3382. int btrfs_write_inode(struct inode *inode, int wait)
  3383. {
  3384. struct btrfs_root *root = BTRFS_I(inode)->root;
  3385. struct btrfs_trans_handle *trans;
  3386. int ret = 0;
  3387. if (root->fs_info->btree_inode == inode)
  3388. return 0;
  3389. if (wait) {
  3390. trans = btrfs_join_transaction(root, 1);
  3391. btrfs_set_trans_block_group(trans, inode);
  3392. ret = btrfs_commit_transaction(trans, root);
  3393. }
  3394. return ret;
  3395. }
  3396. /*
  3397. * This is somewhat expensive, updating the tree every time the
  3398. * inode changes. But, it is most likely to find the inode in cache.
  3399. * FIXME, needs more benchmarking...there are no reasons other than performance
  3400. * to keep or drop this code.
  3401. */
  3402. void btrfs_dirty_inode(struct inode *inode)
  3403. {
  3404. struct btrfs_root *root = BTRFS_I(inode)->root;
  3405. struct btrfs_trans_handle *trans;
  3406. trans = btrfs_join_transaction(root, 1);
  3407. btrfs_set_trans_block_group(trans, inode);
  3408. btrfs_update_inode(trans, root, inode);
  3409. btrfs_end_transaction(trans, root);
  3410. }
  3411. /*
  3412. * find the highest existing sequence number in a directory
  3413. * and then set the in-memory index_cnt variable to reflect
  3414. * free sequence numbers
  3415. */
  3416. static int btrfs_set_inode_index_count(struct inode *inode)
  3417. {
  3418. struct btrfs_root *root = BTRFS_I(inode)->root;
  3419. struct btrfs_key key, found_key;
  3420. struct btrfs_path *path;
  3421. struct extent_buffer *leaf;
  3422. int ret;
  3423. key.objectid = inode->i_ino;
  3424. btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
  3425. key.offset = (u64)-1;
  3426. path = btrfs_alloc_path();
  3427. if (!path)
  3428. return -ENOMEM;
  3429. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  3430. if (ret < 0)
  3431. goto out;
  3432. /* FIXME: we should be able to handle this */
  3433. if (ret == 0)
  3434. goto out;
  3435. ret = 0;
  3436. /*
  3437. * MAGIC NUMBER EXPLANATION:
  3438. * since we search a directory based on f_pos we have to start at 2
  3439. * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
  3440. * else has to start at 2
  3441. */
  3442. if (path->slots[0] == 0) {
  3443. BTRFS_I(inode)->index_cnt = 2;
  3444. goto out;
  3445. }
  3446. path->slots[0]--;
  3447. leaf = path->nodes[0];
  3448. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  3449. if (found_key.objectid != inode->i_ino ||
  3450. btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
  3451. BTRFS_I(inode)->index_cnt = 2;
  3452. goto out;
  3453. }
  3454. BTRFS_I(inode)->index_cnt = found_key.offset + 1;
  3455. out:
  3456. btrfs_free_path(path);
  3457. return ret;
  3458. }
  3459. /*
  3460. * helper to find a free sequence number in a given directory. This current
  3461. * code is very simple, later versions will do smarter things in the btree
  3462. */
  3463. int btrfs_set_inode_index(struct inode *dir, u64 *index)
  3464. {
  3465. int ret = 0;
  3466. if (BTRFS_I(dir)->index_cnt == (u64)-1) {
  3467. ret = btrfs_set_inode_index_count(dir);
  3468. if (ret)
  3469. return ret;
  3470. }
  3471. *index = BTRFS_I(dir)->index_cnt;
  3472. BTRFS_I(dir)->index_cnt++;
  3473. return ret;
  3474. }
  3475. static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
  3476. struct btrfs_root *root,
  3477. struct inode *dir,
  3478. const char *name, int name_len,
  3479. u64 ref_objectid, u64 objectid,
  3480. u64 alloc_hint, int mode, u64 *index)
  3481. {
  3482. struct inode *inode;
  3483. struct btrfs_inode_item *inode_item;
  3484. struct btrfs_key *location;
  3485. struct btrfs_path *path;
  3486. struct btrfs_inode_ref *ref;
  3487. struct btrfs_key key[2];
  3488. u32 sizes[2];
  3489. unsigned long ptr;
  3490. int ret;
  3491. int owner;
  3492. path = btrfs_alloc_path();
  3493. BUG_ON(!path);
  3494. inode = new_inode(root->fs_info->sb);
  3495. if (!inode)
  3496. return ERR_PTR(-ENOMEM);
  3497. if (dir) {
  3498. ret = btrfs_set_inode_index(dir, index);
  3499. if (ret) {
  3500. iput(inode);
  3501. return ERR_PTR(ret);
  3502. }
  3503. }
  3504. /*
  3505. * index_cnt is ignored for everything but a dir,
  3506. * btrfs_get_inode_index_count has an explanation for the magic
  3507. * number
  3508. */
  3509. init_btrfs_i(inode);
  3510. BTRFS_I(inode)->index_cnt = 2;
  3511. BTRFS_I(inode)->root = root;
  3512. BTRFS_I(inode)->generation = trans->transid;
  3513. btrfs_set_inode_space_info(root, inode);
  3514. if (mode & S_IFDIR)
  3515. owner = 0;
  3516. else
  3517. owner = 1;
  3518. BTRFS_I(inode)->block_group =
  3519. btrfs_find_block_group(root, 0, alloc_hint, owner);
  3520. key[0].objectid = objectid;
  3521. btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
  3522. key[0].offset = 0;
  3523. key[1].objectid = objectid;
  3524. btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
  3525. key[1].offset = ref_objectid;
  3526. sizes[0] = sizeof(struct btrfs_inode_item);
  3527. sizes[1] = name_len + sizeof(*ref);
  3528. path->leave_spinning = 1;
  3529. ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
  3530. if (ret != 0)
  3531. goto fail;
  3532. inode->i_uid = current_fsuid();
  3533. if (dir && (dir->i_mode & S_ISGID)) {
  3534. inode->i_gid = dir->i_gid;
  3535. if (S_ISDIR(mode))
  3536. mode |= S_ISGID;
  3537. } else
  3538. inode->i_gid = current_fsgid();
  3539. inode->i_mode = mode;
  3540. inode->i_ino = objectid;
  3541. inode_set_bytes(inode, 0);
  3542. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
  3543. inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  3544. struct btrfs_inode_item);
  3545. fill_inode_item(trans, path->nodes[0], inode_item, inode);
  3546. ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
  3547. struct btrfs_inode_ref);
  3548. btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
  3549. btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
  3550. ptr = (unsigned long)(ref + 1);
  3551. write_extent_buffer(path->nodes[0], name, ptr, name_len);
  3552. btrfs_mark_buffer_dirty(path->nodes[0]);
  3553. btrfs_free_path(path);
  3554. location = &BTRFS_I(inode)->location;
  3555. location->objectid = objectid;
  3556. location->offset = 0;
  3557. btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
  3558. btrfs_inherit_iflags(inode, dir);
  3559. if ((mode & S_IFREG)) {
  3560. if (btrfs_test_opt(root, NODATASUM))
  3561. BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
  3562. if (btrfs_test_opt(root, NODATACOW))
  3563. BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
  3564. }
  3565. insert_inode_hash(inode);
  3566. inode_tree_add(inode);
  3567. return inode;
  3568. fail:
  3569. if (dir)
  3570. BTRFS_I(dir)->index_cnt--;
  3571. btrfs_free_path(path);
  3572. iput(inode);
  3573. return ERR_PTR(ret);
  3574. }
  3575. static inline u8 btrfs_inode_type(struct inode *inode)
  3576. {
  3577. return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
  3578. }
  3579. /*
  3580. * utility function to add 'inode' into 'parent_inode' with
  3581. * a give name and a given sequence number.
  3582. * if 'add_backref' is true, also insert a backref from the
  3583. * inode to the parent directory.
  3584. */
  3585. int btrfs_add_link(struct btrfs_trans_handle *trans,
  3586. struct inode *parent_inode, struct inode *inode,
  3587. const char *name, int name_len, int add_backref, u64 index)
  3588. {
  3589. int ret = 0;
  3590. struct btrfs_key key;
  3591. struct btrfs_root *root = BTRFS_I(parent_inode)->root;
  3592. if (unlikely(inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
  3593. memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
  3594. } else {
  3595. key.objectid = inode->i_ino;
  3596. btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
  3597. key.offset = 0;
  3598. }
  3599. if (unlikely(inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
  3600. ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
  3601. key.objectid, root->root_key.objectid,
  3602. parent_inode->i_ino,
  3603. index, name, name_len);
  3604. } else if (add_backref) {
  3605. ret = btrfs_insert_inode_ref(trans, root,
  3606. name, name_len, inode->i_ino,
  3607. parent_inode->i_ino, index);
  3608. }
  3609. if (ret == 0) {
  3610. ret = btrfs_insert_dir_item(trans, root, name, name_len,
  3611. parent_inode->i_ino, &key,
  3612. btrfs_inode_type(inode), index);
  3613. BUG_ON(ret);
  3614. btrfs_i_size_write(parent_inode, parent_inode->i_size +
  3615. name_len * 2);
  3616. parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
  3617. ret = btrfs_update_inode(trans, root, parent_inode);
  3618. }
  3619. return ret;
  3620. }
  3621. static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
  3622. struct dentry *dentry, struct inode *inode,
  3623. int backref, u64 index)
  3624. {
  3625. int err = btrfs_add_link(trans, dentry->d_parent->d_inode,
  3626. inode, dentry->d_name.name,
  3627. dentry->d_name.len, backref, index);
  3628. if (!err) {
  3629. d_instantiate(dentry, inode);
  3630. return 0;
  3631. }
  3632. if (err > 0)
  3633. err = -EEXIST;
  3634. return err;
  3635. }
  3636. static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
  3637. int mode, dev_t rdev)
  3638. {
  3639. struct btrfs_trans_handle *trans;
  3640. struct btrfs_root *root = BTRFS_I(dir)->root;
  3641. struct inode *inode = NULL;
  3642. int err;
  3643. int drop_inode = 0;
  3644. u64 objectid;
  3645. unsigned long nr = 0;
  3646. u64 index = 0;
  3647. if (!new_valid_dev(rdev))
  3648. return -EINVAL;
  3649. /*
  3650. * 2 for inode item and ref
  3651. * 2 for dir items
  3652. * 1 for xattr if selinux is on
  3653. */
  3654. err = btrfs_reserve_metadata_space(root, 5);
  3655. if (err)
  3656. return err;
  3657. trans = btrfs_start_transaction(root, 1);
  3658. if (!trans)
  3659. goto fail;
  3660. btrfs_set_trans_block_group(trans, dir);
  3661. err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
  3662. if (err) {
  3663. err = -ENOSPC;
  3664. goto out_unlock;
  3665. }
  3666. inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
  3667. dentry->d_name.len,
  3668. dentry->d_parent->d_inode->i_ino, objectid,
  3669. BTRFS_I(dir)->block_group, mode, &index);
  3670. err = PTR_ERR(inode);
  3671. if (IS_ERR(inode))
  3672. goto out_unlock;
  3673. err = btrfs_init_inode_security(inode, dir);
  3674. if (err) {
  3675. drop_inode = 1;
  3676. goto out_unlock;
  3677. }
  3678. btrfs_set_trans_block_group(trans, inode);
  3679. err = btrfs_add_nondir(trans, dentry, inode, 0, index);
  3680. if (err)
  3681. drop_inode = 1;
  3682. else {
  3683. inode->i_op = &btrfs_special_inode_operations;
  3684. init_special_inode(inode, inode->i_mode, rdev);
  3685. btrfs_update_inode(trans, root, inode);
  3686. }
  3687. btrfs_update_inode_block_group(trans, inode);
  3688. btrfs_update_inode_block_group(trans, dir);
  3689. out_unlock:
  3690. nr = trans->blocks_used;
  3691. btrfs_end_transaction_throttle(trans, root);
  3692. fail:
  3693. btrfs_unreserve_metadata_space(root, 5);
  3694. if (drop_inode) {
  3695. inode_dec_link_count(inode);
  3696. iput(inode);
  3697. }
  3698. btrfs_btree_balance_dirty(root, nr);
  3699. return err;
  3700. }
  3701. static int btrfs_create(struct inode *dir, struct dentry *dentry,
  3702. int mode, struct nameidata *nd)
  3703. {
  3704. struct btrfs_trans_handle *trans;
  3705. struct btrfs_root *root = BTRFS_I(dir)->root;
  3706. struct inode *inode = NULL;
  3707. int err;
  3708. int drop_inode = 0;
  3709. unsigned long nr = 0;
  3710. u64 objectid;
  3711. u64 index = 0;
  3712. /*
  3713. * 2 for inode item and ref
  3714. * 2 for dir items
  3715. * 1 for xattr if selinux is on
  3716. */
  3717. err = btrfs_reserve_metadata_space(root, 5);
  3718. if (err)
  3719. return err;
  3720. trans = btrfs_start_transaction(root, 1);
  3721. if (!trans)
  3722. goto fail;
  3723. btrfs_set_trans_block_group(trans, dir);
  3724. err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
  3725. if (err) {
  3726. err = -ENOSPC;
  3727. goto out_unlock;
  3728. }
  3729. inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
  3730. dentry->d_name.len,
  3731. dentry->d_parent->d_inode->i_ino,
  3732. objectid, BTRFS_I(dir)->block_group, mode,
  3733. &index);
  3734. err = PTR_ERR(inode);
  3735. if (IS_ERR(inode))
  3736. goto out_unlock;
  3737. err = btrfs_init_inode_security(inode, dir);
  3738. if (err) {
  3739. drop_inode = 1;
  3740. goto out_unlock;
  3741. }
  3742. btrfs_set_trans_block_group(trans, inode);
  3743. err = btrfs_add_nondir(trans, dentry, inode, 0, index);
  3744. if (err)
  3745. drop_inode = 1;
  3746. else {
  3747. inode->i_mapping->a_ops = &btrfs_aops;
  3748. inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
  3749. inode->i_fop = &btrfs_file_operations;
  3750. inode->i_op = &btrfs_file_inode_operations;
  3751. BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
  3752. }
  3753. btrfs_update_inode_block_group(trans, inode);
  3754. btrfs_update_inode_block_group(trans, dir);
  3755. out_unlock:
  3756. nr = trans->blocks_used;
  3757. btrfs_end_transaction_throttle(trans, root);
  3758. fail:
  3759. btrfs_unreserve_metadata_space(root, 5);
  3760. if (drop_inode) {
  3761. inode_dec_link_count(inode);
  3762. iput(inode);
  3763. }
  3764. btrfs_btree_balance_dirty(root, nr);
  3765. return err;
  3766. }
  3767. static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
  3768. struct dentry *dentry)
  3769. {
  3770. struct btrfs_trans_handle *trans;
  3771. struct btrfs_root *root = BTRFS_I(dir)->root;
  3772. struct inode *inode = old_dentry->d_inode;
  3773. u64 index;
  3774. unsigned long nr = 0;
  3775. int err;
  3776. int drop_inode = 0;
  3777. if (inode->i_nlink == 0)
  3778. return -ENOENT;
  3779. /*
  3780. * 1 item for inode ref
  3781. * 2 items for dir items
  3782. */
  3783. err = btrfs_reserve_metadata_space(root, 3);
  3784. if (err)
  3785. return err;
  3786. btrfs_inc_nlink(inode);
  3787. err = btrfs_set_inode_index(dir, &index);
  3788. if (err)
  3789. goto fail;
  3790. trans = btrfs_start_transaction(root, 1);
  3791. btrfs_set_trans_block_group(trans, dir);
  3792. atomic_inc(&inode->i_count);
  3793. err = btrfs_add_nondir(trans, dentry, inode, 1, index);
  3794. if (err) {
  3795. drop_inode = 1;
  3796. } else {
  3797. btrfs_update_inode_block_group(trans, dir);
  3798. err = btrfs_update_inode(trans, root, inode);
  3799. BUG_ON(err);
  3800. btrfs_log_new_name(trans, inode, NULL, dentry->d_parent);
  3801. }
  3802. nr = trans->blocks_used;
  3803. btrfs_end_transaction_throttle(trans, root);
  3804. fail:
  3805. btrfs_unreserve_metadata_space(root, 3);
  3806. if (drop_inode) {
  3807. inode_dec_link_count(inode);
  3808. iput(inode);
  3809. }
  3810. btrfs_btree_balance_dirty(root, nr);
  3811. return err;
  3812. }
  3813. static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  3814. {
  3815. struct inode *inode = NULL;
  3816. struct btrfs_trans_handle *trans;
  3817. struct btrfs_root *root = BTRFS_I(dir)->root;
  3818. int err = 0;
  3819. int drop_on_err = 0;
  3820. u64 objectid = 0;
  3821. u64 index = 0;
  3822. unsigned long nr = 1;
  3823. /*
  3824. * 2 items for inode and ref
  3825. * 2 items for dir items
  3826. * 1 for xattr if selinux is on
  3827. */
  3828. err = btrfs_reserve_metadata_space(root, 5);
  3829. if (err)
  3830. return err;
  3831. trans = btrfs_start_transaction(root, 1);
  3832. if (!trans) {
  3833. err = -ENOMEM;
  3834. goto out_unlock;
  3835. }
  3836. btrfs_set_trans_block_group(trans, dir);
  3837. err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
  3838. if (err) {
  3839. err = -ENOSPC;
  3840. goto out_unlock;
  3841. }
  3842. inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
  3843. dentry->d_name.len,
  3844. dentry->d_parent->d_inode->i_ino, objectid,
  3845. BTRFS_I(dir)->block_group, S_IFDIR | mode,
  3846. &index);
  3847. if (IS_ERR(inode)) {
  3848. err = PTR_ERR(inode);
  3849. goto out_fail;
  3850. }
  3851. drop_on_err = 1;
  3852. err = btrfs_init_inode_security(inode, dir);
  3853. if (err)
  3854. goto out_fail;
  3855. inode->i_op = &btrfs_dir_inode_operations;
  3856. inode->i_fop = &btrfs_dir_file_operations;
  3857. btrfs_set_trans_block_group(trans, inode);
  3858. btrfs_i_size_write(inode, 0);
  3859. err = btrfs_update_inode(trans, root, inode);
  3860. if (err)
  3861. goto out_fail;
  3862. err = btrfs_add_link(trans, dentry->d_parent->d_inode,
  3863. inode, dentry->d_name.name,
  3864. dentry->d_name.len, 0, index);
  3865. if (err)
  3866. goto out_fail;
  3867. d_instantiate(dentry, inode);
  3868. drop_on_err = 0;
  3869. btrfs_update_inode_block_group(trans, inode);
  3870. btrfs_update_inode_block_group(trans, dir);
  3871. out_fail:
  3872. nr = trans->blocks_used;
  3873. btrfs_end_transaction_throttle(trans, root);
  3874. out_unlock:
  3875. btrfs_unreserve_metadata_space(root, 5);
  3876. if (drop_on_err)
  3877. iput(inode);
  3878. btrfs_btree_balance_dirty(root, nr);
  3879. return err;
  3880. }
  3881. /* helper for btfs_get_extent. Given an existing extent in the tree,
  3882. * and an extent that you want to insert, deal with overlap and insert
  3883. * the new extent into the tree.
  3884. */
  3885. static int merge_extent_mapping(struct extent_map_tree *em_tree,
  3886. struct extent_map *existing,
  3887. struct extent_map *em,
  3888. u64 map_start, u64 map_len)
  3889. {
  3890. u64 start_diff;
  3891. BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
  3892. start_diff = map_start - em->start;
  3893. em->start = map_start;
  3894. em->len = map_len;
  3895. if (em->block_start < EXTENT_MAP_LAST_BYTE &&
  3896. !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
  3897. em->block_start += start_diff;
  3898. em->block_len -= start_diff;
  3899. }
  3900. return add_extent_mapping(em_tree, em);
  3901. }
  3902. static noinline int uncompress_inline(struct btrfs_path *path,
  3903. struct inode *inode, struct page *page,
  3904. size_t pg_offset, u64 extent_offset,
  3905. struct btrfs_file_extent_item *item)
  3906. {
  3907. int ret;
  3908. struct extent_buffer *leaf = path->nodes[0];
  3909. char *tmp;
  3910. size_t max_size;
  3911. unsigned long inline_size;
  3912. unsigned long ptr;
  3913. WARN_ON(pg_offset != 0);
  3914. max_size = btrfs_file_extent_ram_bytes(leaf, item);
  3915. inline_size = btrfs_file_extent_inline_item_len(leaf,
  3916. btrfs_item_nr(leaf, path->slots[0]));
  3917. tmp = kmalloc(inline_size, GFP_NOFS);
  3918. ptr = btrfs_file_extent_inline_start(item);
  3919. read_extent_buffer(leaf, tmp, ptr, inline_size);
  3920. max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
  3921. ret = btrfs_zlib_decompress(tmp, page, extent_offset,
  3922. inline_size, max_size);
  3923. if (ret) {
  3924. char *kaddr = kmap_atomic(page, KM_USER0);
  3925. unsigned long copy_size = min_t(u64,
  3926. PAGE_CACHE_SIZE - pg_offset,
  3927. max_size - extent_offset);
  3928. memset(kaddr + pg_offset, 0, copy_size);
  3929. kunmap_atomic(kaddr, KM_USER0);
  3930. }
  3931. kfree(tmp);
  3932. return 0;
  3933. }
  3934. /*
  3935. * a bit scary, this does extent mapping from logical file offset to the disk.
  3936. * the ugly parts come from merging extents from the disk with the in-ram
  3937. * representation. This gets more complex because of the data=ordered code,
  3938. * where the in-ram extents might be locked pending data=ordered completion.
  3939. *
  3940. * This also copies inline extents directly into the page.
  3941. */
  3942. struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
  3943. size_t pg_offset, u64 start, u64 len,
  3944. int create)
  3945. {
  3946. int ret;
  3947. int err = 0;
  3948. u64 bytenr;
  3949. u64 extent_start = 0;
  3950. u64 extent_end = 0;
  3951. u64 objectid = inode->i_ino;
  3952. u32 found_type;
  3953. struct btrfs_path *path = NULL;
  3954. struct btrfs_root *root = BTRFS_I(inode)->root;
  3955. struct btrfs_file_extent_item *item;
  3956. struct extent_buffer *leaf;
  3957. struct btrfs_key found_key;
  3958. struct extent_map *em = NULL;
  3959. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  3960. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  3961. struct btrfs_trans_handle *trans = NULL;
  3962. int compressed;
  3963. again:
  3964. read_lock(&em_tree->lock);
  3965. em = lookup_extent_mapping(em_tree, start, len);
  3966. if (em)
  3967. em->bdev = root->fs_info->fs_devices->latest_bdev;
  3968. read_unlock(&em_tree->lock);
  3969. if (em) {
  3970. if (em->start > start || em->start + em->len <= start)
  3971. free_extent_map(em);
  3972. else if (em->block_start == EXTENT_MAP_INLINE && page)
  3973. free_extent_map(em);
  3974. else
  3975. goto out;
  3976. }
  3977. em = alloc_extent_map(GFP_NOFS);
  3978. if (!em) {
  3979. err = -ENOMEM;
  3980. goto out;
  3981. }
  3982. em->bdev = root->fs_info->fs_devices->latest_bdev;
  3983. em->start = EXTENT_MAP_HOLE;
  3984. em->orig_start = EXTENT_MAP_HOLE;
  3985. em->len = (u64)-1;
  3986. em->block_len = (u64)-1;
  3987. if (!path) {
  3988. path = btrfs_alloc_path();
  3989. BUG_ON(!path);
  3990. }
  3991. ret = btrfs_lookup_file_extent(trans, root, path,
  3992. objectid, start, trans != NULL);
  3993. if (ret < 0) {
  3994. err = ret;
  3995. goto out;
  3996. }
  3997. if (ret != 0) {
  3998. if (path->slots[0] == 0)
  3999. goto not_found;
  4000. path->slots[0]--;
  4001. }
  4002. leaf = path->nodes[0];
  4003. item = btrfs_item_ptr(leaf, path->slots[0],
  4004. struct btrfs_file_extent_item);
  4005. /* are we inside the extent that was found? */
  4006. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  4007. found_type = btrfs_key_type(&found_key);
  4008. if (found_key.objectid != objectid ||
  4009. found_type != BTRFS_EXTENT_DATA_KEY) {
  4010. goto not_found;
  4011. }
  4012. found_type = btrfs_file_extent_type(leaf, item);
  4013. extent_start = found_key.offset;
  4014. compressed = btrfs_file_extent_compression(leaf, item);
  4015. if (found_type == BTRFS_FILE_EXTENT_REG ||
  4016. found_type == BTRFS_FILE_EXTENT_PREALLOC) {
  4017. extent_end = extent_start +
  4018. btrfs_file_extent_num_bytes(leaf, item);
  4019. } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
  4020. size_t size;
  4021. size = btrfs_file_extent_inline_len(leaf, item);
  4022. extent_end = (extent_start + size + root->sectorsize - 1) &
  4023. ~((u64)root->sectorsize - 1);
  4024. }
  4025. if (start >= extent_end) {
  4026. path->slots[0]++;
  4027. if (path->slots[0] >= btrfs_header_nritems(leaf)) {
  4028. ret = btrfs_next_leaf(root, path);
  4029. if (ret < 0) {
  4030. err = ret;
  4031. goto out;
  4032. }
  4033. if (ret > 0)
  4034. goto not_found;
  4035. leaf = path->nodes[0];
  4036. }
  4037. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  4038. if (found_key.objectid != objectid ||
  4039. found_key.type != BTRFS_EXTENT_DATA_KEY)
  4040. goto not_found;
  4041. if (start + len <= found_key.offset)
  4042. goto not_found;
  4043. em->start = start;
  4044. em->len = found_key.offset - start;
  4045. goto not_found_em;
  4046. }
  4047. if (found_type == BTRFS_FILE_EXTENT_REG ||
  4048. found_type == BTRFS_FILE_EXTENT_PREALLOC) {
  4049. em->start = extent_start;
  4050. em->len = extent_end - extent_start;
  4051. em->orig_start = extent_start -
  4052. btrfs_file_extent_offset(leaf, item);
  4053. bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
  4054. if (bytenr == 0) {
  4055. em->block_start = EXTENT_MAP_HOLE;
  4056. goto insert;
  4057. }
  4058. if (compressed) {
  4059. set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
  4060. em->block_start = bytenr;
  4061. em->block_len = btrfs_file_extent_disk_num_bytes(leaf,
  4062. item);
  4063. } else {
  4064. bytenr += btrfs_file_extent_offset(leaf, item);
  4065. em->block_start = bytenr;
  4066. em->block_len = em->len;
  4067. if (found_type == BTRFS_FILE_EXTENT_PREALLOC)
  4068. set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
  4069. }
  4070. goto insert;
  4071. } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
  4072. unsigned long ptr;
  4073. char *map;
  4074. size_t size;
  4075. size_t extent_offset;
  4076. size_t copy_size;
  4077. em->block_start = EXTENT_MAP_INLINE;
  4078. if (!page || create) {
  4079. em->start = extent_start;
  4080. em->len = extent_end - extent_start;
  4081. goto out;
  4082. }
  4083. size = btrfs_file_extent_inline_len(leaf, item);
  4084. extent_offset = page_offset(page) + pg_offset - extent_start;
  4085. copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
  4086. size - extent_offset);
  4087. em->start = extent_start + extent_offset;
  4088. em->len = (copy_size + root->sectorsize - 1) &
  4089. ~((u64)root->sectorsize - 1);
  4090. em->orig_start = EXTENT_MAP_INLINE;
  4091. if (compressed)
  4092. set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
  4093. ptr = btrfs_file_extent_inline_start(item) + extent_offset;
  4094. if (create == 0 && !PageUptodate(page)) {
  4095. if (btrfs_file_extent_compression(leaf, item) ==
  4096. BTRFS_COMPRESS_ZLIB) {
  4097. ret = uncompress_inline(path, inode, page,
  4098. pg_offset,
  4099. extent_offset, item);
  4100. BUG_ON(ret);
  4101. } else {
  4102. map = kmap(page);
  4103. read_extent_buffer(leaf, map + pg_offset, ptr,
  4104. copy_size);
  4105. if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
  4106. memset(map + pg_offset + copy_size, 0,
  4107. PAGE_CACHE_SIZE - pg_offset -
  4108. copy_size);
  4109. }
  4110. kunmap(page);
  4111. }
  4112. flush_dcache_page(page);
  4113. } else if (create && PageUptodate(page)) {
  4114. if (!trans) {
  4115. kunmap(page);
  4116. free_extent_map(em);
  4117. em = NULL;
  4118. btrfs_release_path(root, path);
  4119. trans = btrfs_join_transaction(root, 1);
  4120. goto again;
  4121. }
  4122. map = kmap(page);
  4123. write_extent_buffer(leaf, map + pg_offset, ptr,
  4124. copy_size);
  4125. kunmap(page);
  4126. btrfs_mark_buffer_dirty(leaf);
  4127. }
  4128. set_extent_uptodate(io_tree, em->start,
  4129. extent_map_end(em) - 1, GFP_NOFS);
  4130. goto insert;
  4131. } else {
  4132. printk(KERN_ERR "btrfs unknown found_type %d\n", found_type);
  4133. WARN_ON(1);
  4134. }
  4135. not_found:
  4136. em->start = start;
  4137. em->len = len;
  4138. not_found_em:
  4139. em->block_start = EXTENT_MAP_HOLE;
  4140. set_bit(EXTENT_FLAG_VACANCY, &em->flags);
  4141. insert:
  4142. btrfs_release_path(root, path);
  4143. if (em->start > start || extent_map_end(em) <= start) {
  4144. printk(KERN_ERR "Btrfs: bad extent! em: [%llu %llu] passed "
  4145. "[%llu %llu]\n", (unsigned long long)em->start,
  4146. (unsigned long long)em->len,
  4147. (unsigned long long)start,
  4148. (unsigned long long)len);
  4149. err = -EIO;
  4150. goto out;
  4151. }
  4152. err = 0;
  4153. write_lock(&em_tree->lock);
  4154. ret = add_extent_mapping(em_tree, em);
  4155. /* it is possible that someone inserted the extent into the tree
  4156. * while we had the lock dropped. It is also possible that
  4157. * an overlapping map exists in the tree
  4158. */
  4159. if (ret == -EEXIST) {
  4160. struct extent_map *existing;
  4161. ret = 0;
  4162. existing = lookup_extent_mapping(em_tree, start, len);
  4163. if (existing && (existing->start > start ||
  4164. existing->start + existing->len <= start)) {
  4165. free_extent_map(existing);
  4166. existing = NULL;
  4167. }
  4168. if (!existing) {
  4169. existing = lookup_extent_mapping(em_tree, em->start,
  4170. em->len);
  4171. if (existing) {
  4172. err = merge_extent_mapping(em_tree, existing,
  4173. em, start,
  4174. root->sectorsize);
  4175. free_extent_map(existing);
  4176. if (err) {
  4177. free_extent_map(em);
  4178. em = NULL;
  4179. }
  4180. } else {
  4181. err = -EIO;
  4182. free_extent_map(em);
  4183. em = NULL;
  4184. }
  4185. } else {
  4186. free_extent_map(em);
  4187. em = existing;
  4188. err = 0;
  4189. }
  4190. }
  4191. write_unlock(&em_tree->lock);
  4192. out:
  4193. if (path)
  4194. btrfs_free_path(path);
  4195. if (trans) {
  4196. ret = btrfs_end_transaction(trans, root);
  4197. if (!err)
  4198. err = ret;
  4199. }
  4200. if (err) {
  4201. free_extent_map(em);
  4202. return ERR_PTR(err);
  4203. }
  4204. return em;
  4205. }
  4206. static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
  4207. const struct iovec *iov, loff_t offset,
  4208. unsigned long nr_segs)
  4209. {
  4210. return -EINVAL;
  4211. }
  4212. static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
  4213. __u64 start, __u64 len)
  4214. {
  4215. return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent);
  4216. }
  4217. int btrfs_readpage(struct file *file, struct page *page)
  4218. {
  4219. struct extent_io_tree *tree;
  4220. tree = &BTRFS_I(page->mapping->host)->io_tree;
  4221. return extent_read_full_page(tree, page, btrfs_get_extent);
  4222. }
  4223. static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
  4224. {
  4225. struct extent_io_tree *tree;
  4226. if (current->flags & PF_MEMALLOC) {
  4227. redirty_page_for_writepage(wbc, page);
  4228. unlock_page(page);
  4229. return 0;
  4230. }
  4231. tree = &BTRFS_I(page->mapping->host)->io_tree;
  4232. return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
  4233. }
  4234. int btrfs_writepages(struct address_space *mapping,
  4235. struct writeback_control *wbc)
  4236. {
  4237. struct extent_io_tree *tree;
  4238. tree = &BTRFS_I(mapping->host)->io_tree;
  4239. return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
  4240. }
  4241. static int
  4242. btrfs_readpages(struct file *file, struct address_space *mapping,
  4243. struct list_head *pages, unsigned nr_pages)
  4244. {
  4245. struct extent_io_tree *tree;
  4246. tree = &BTRFS_I(mapping->host)->io_tree;
  4247. return extent_readpages(tree, mapping, pages, nr_pages,
  4248. btrfs_get_extent);
  4249. }
  4250. static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
  4251. {
  4252. struct extent_io_tree *tree;
  4253. struct extent_map_tree *map;
  4254. int ret;
  4255. tree = &BTRFS_I(page->mapping->host)->io_tree;
  4256. map = &BTRFS_I(page->mapping->host)->extent_tree;
  4257. ret = try_release_extent_mapping(map, tree, page, gfp_flags);
  4258. if (ret == 1) {
  4259. ClearPagePrivate(page);
  4260. set_page_private(page, 0);
  4261. page_cache_release(page);
  4262. }
  4263. return ret;
  4264. }
  4265. static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
  4266. {
  4267. if (PageWriteback(page) || PageDirty(page))
  4268. return 0;
  4269. return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
  4270. }
  4271. static void btrfs_invalidatepage(struct page *page, unsigned long offset)
  4272. {
  4273. struct extent_io_tree *tree;
  4274. struct btrfs_ordered_extent *ordered;
  4275. u64 page_start = page_offset(page);
  4276. u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
  4277. /*
  4278. * we have the page locked, so new writeback can't start,
  4279. * and the dirty bit won't be cleared while we are here.
  4280. *
  4281. * Wait for IO on this page so that we can safely clear
  4282. * the PagePrivate2 bit and do ordered accounting
  4283. */
  4284. wait_on_page_writeback(page);
  4285. tree = &BTRFS_I(page->mapping->host)->io_tree;
  4286. if (offset) {
  4287. btrfs_releasepage(page, GFP_NOFS);
  4288. return;
  4289. }
  4290. lock_extent(tree, page_start, page_end, GFP_NOFS);
  4291. ordered = btrfs_lookup_ordered_extent(page->mapping->host,
  4292. page_offset(page));
  4293. if (ordered) {
  4294. /*
  4295. * IO on this page will never be started, so we need
  4296. * to account for any ordered extents now
  4297. */
  4298. clear_extent_bit(tree, page_start, page_end,
  4299. EXTENT_DIRTY | EXTENT_DELALLOC |
  4300. EXTENT_LOCKED | EXTENT_DO_ACCOUNTING, 1, 0,
  4301. NULL, GFP_NOFS);
  4302. /*
  4303. * whoever cleared the private bit is responsible
  4304. * for the finish_ordered_io
  4305. */
  4306. if (TestClearPagePrivate2(page)) {
  4307. btrfs_finish_ordered_io(page->mapping->host,
  4308. page_start, page_end);
  4309. }
  4310. btrfs_put_ordered_extent(ordered);
  4311. lock_extent(tree, page_start, page_end, GFP_NOFS);
  4312. }
  4313. clear_extent_bit(tree, page_start, page_end,
  4314. EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
  4315. EXTENT_DO_ACCOUNTING, 1, 1, NULL, GFP_NOFS);
  4316. __btrfs_releasepage(page, GFP_NOFS);
  4317. ClearPageChecked(page);
  4318. if (PagePrivate(page)) {
  4319. ClearPagePrivate(page);
  4320. set_page_private(page, 0);
  4321. page_cache_release(page);
  4322. }
  4323. }
  4324. /*
  4325. * btrfs_page_mkwrite() is not allowed to change the file size as it gets
  4326. * called from a page fault handler when a page is first dirtied. Hence we must
  4327. * be careful to check for EOF conditions here. We set the page up correctly
  4328. * for a written page which means we get ENOSPC checking when writing into
  4329. * holes and correct delalloc and unwritten extent mapping on filesystems that
  4330. * support these features.
  4331. *
  4332. * We are not allowed to take the i_mutex here so we have to play games to
  4333. * protect against truncate races as the page could now be beyond EOF. Because
  4334. * vmtruncate() writes the inode size before removing pages, once we have the
  4335. * page lock we can determine safely if the page is beyond EOF. If it is not
  4336. * beyond EOF, then the page is guaranteed safe against truncation until we
  4337. * unlock the page.
  4338. */
  4339. int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
  4340. {
  4341. struct page *page = vmf->page;
  4342. struct inode *inode = fdentry(vma->vm_file)->d_inode;
  4343. struct btrfs_root *root = BTRFS_I(inode)->root;
  4344. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  4345. struct btrfs_ordered_extent *ordered;
  4346. char *kaddr;
  4347. unsigned long zero_start;
  4348. loff_t size;
  4349. int ret;
  4350. u64 page_start;
  4351. u64 page_end;
  4352. ret = btrfs_check_data_free_space(root, inode, PAGE_CACHE_SIZE);
  4353. if (ret) {
  4354. if (ret == -ENOMEM)
  4355. ret = VM_FAULT_OOM;
  4356. else /* -ENOSPC, -EIO, etc */
  4357. ret = VM_FAULT_SIGBUS;
  4358. goto out;
  4359. }
  4360. ret = btrfs_reserve_metadata_for_delalloc(root, inode, 1);
  4361. if (ret) {
  4362. btrfs_free_reserved_data_space(root, inode, PAGE_CACHE_SIZE);
  4363. ret = VM_FAULT_SIGBUS;
  4364. goto out;
  4365. }
  4366. ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
  4367. again:
  4368. lock_page(page);
  4369. size = i_size_read(inode);
  4370. page_start = page_offset(page);
  4371. page_end = page_start + PAGE_CACHE_SIZE - 1;
  4372. if ((page->mapping != inode->i_mapping) ||
  4373. (page_start >= size)) {
  4374. btrfs_free_reserved_data_space(root, inode, PAGE_CACHE_SIZE);
  4375. /* page got truncated out from underneath us */
  4376. goto out_unlock;
  4377. }
  4378. wait_on_page_writeback(page);
  4379. lock_extent(io_tree, page_start, page_end, GFP_NOFS);
  4380. set_page_extent_mapped(page);
  4381. /*
  4382. * we can't set the delalloc bits if there are pending ordered
  4383. * extents. Drop our locks and wait for them to finish
  4384. */
  4385. ordered = btrfs_lookup_ordered_extent(inode, page_start);
  4386. if (ordered) {
  4387. unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
  4388. unlock_page(page);
  4389. btrfs_start_ordered_extent(inode, ordered, 1);
  4390. btrfs_put_ordered_extent(ordered);
  4391. goto again;
  4392. }
  4393. /*
  4394. * XXX - page_mkwrite gets called every time the page is dirtied, even
  4395. * if it was already dirty, so for space accounting reasons we need to
  4396. * clear any delalloc bits for the range we are fixing to save. There
  4397. * is probably a better way to do this, but for now keep consistent with
  4398. * prepare_pages in the normal write path.
  4399. */
  4400. clear_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end,
  4401. EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING,
  4402. GFP_NOFS);
  4403. ret = btrfs_set_extent_delalloc(inode, page_start, page_end);
  4404. if (ret) {
  4405. unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
  4406. ret = VM_FAULT_SIGBUS;
  4407. btrfs_free_reserved_data_space(root, inode, PAGE_CACHE_SIZE);
  4408. goto out_unlock;
  4409. }
  4410. ret = 0;
  4411. /* page is wholly or partially inside EOF */
  4412. if (page_start + PAGE_CACHE_SIZE > size)
  4413. zero_start = size & ~PAGE_CACHE_MASK;
  4414. else
  4415. zero_start = PAGE_CACHE_SIZE;
  4416. if (zero_start != PAGE_CACHE_SIZE) {
  4417. kaddr = kmap(page);
  4418. memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
  4419. flush_dcache_page(page);
  4420. kunmap(page);
  4421. }
  4422. ClearPageChecked(page);
  4423. set_page_dirty(page);
  4424. SetPageUptodate(page);
  4425. BTRFS_I(inode)->last_trans = root->fs_info->generation;
  4426. BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
  4427. unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
  4428. out_unlock:
  4429. btrfs_unreserve_metadata_for_delalloc(root, inode, 1);
  4430. if (!ret)
  4431. return VM_FAULT_LOCKED;
  4432. unlock_page(page);
  4433. out:
  4434. return ret;
  4435. }
  4436. static void btrfs_truncate(struct inode *inode)
  4437. {
  4438. struct btrfs_root *root = BTRFS_I(inode)->root;
  4439. int ret;
  4440. struct btrfs_trans_handle *trans;
  4441. unsigned long nr;
  4442. u64 mask = root->sectorsize - 1;
  4443. if (!S_ISREG(inode->i_mode))
  4444. return;
  4445. if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
  4446. return;
  4447. ret = btrfs_truncate_page(inode->i_mapping, inode->i_size);
  4448. if (ret)
  4449. return;
  4450. btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
  4451. trans = btrfs_start_transaction(root, 1);
  4452. /*
  4453. * setattr is responsible for setting the ordered_data_close flag,
  4454. * but that is only tested during the last file release. That
  4455. * could happen well after the next commit, leaving a great big
  4456. * window where new writes may get lost if someone chooses to write
  4457. * to this file after truncating to zero
  4458. *
  4459. * The inode doesn't have any dirty data here, and so if we commit
  4460. * this is a noop. If someone immediately starts writing to the inode
  4461. * it is very likely we'll catch some of their writes in this
  4462. * transaction, and the commit will find this file on the ordered
  4463. * data list with good things to send down.
  4464. *
  4465. * This is a best effort solution, there is still a window where
  4466. * using truncate to replace the contents of the file will
  4467. * end up with a zero length file after a crash.
  4468. */
  4469. if (inode->i_size == 0 && BTRFS_I(inode)->ordered_data_close)
  4470. btrfs_add_ordered_operation(trans, root, inode);
  4471. btrfs_set_trans_block_group(trans, inode);
  4472. btrfs_i_size_write(inode, inode->i_size);
  4473. ret = btrfs_orphan_add(trans, inode);
  4474. if (ret)
  4475. goto out;
  4476. /* FIXME, add redo link to tree so we don't leak on crash */
  4477. ret = btrfs_truncate_inode_items(trans, root, inode, inode->i_size,
  4478. BTRFS_EXTENT_DATA_KEY);
  4479. btrfs_update_inode(trans, root, inode);
  4480. ret = btrfs_orphan_del(trans, inode);
  4481. BUG_ON(ret);
  4482. out:
  4483. nr = trans->blocks_used;
  4484. ret = btrfs_end_transaction_throttle(trans, root);
  4485. BUG_ON(ret);
  4486. btrfs_btree_balance_dirty(root, nr);
  4487. }
  4488. /*
  4489. * create a new subvolume directory/inode (helper for the ioctl).
  4490. */
  4491. int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
  4492. struct btrfs_root *new_root,
  4493. u64 new_dirid, u64 alloc_hint)
  4494. {
  4495. struct inode *inode;
  4496. int err;
  4497. u64 index = 0;
  4498. inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, new_dirid,
  4499. new_dirid, alloc_hint, S_IFDIR | 0700, &index);
  4500. if (IS_ERR(inode))
  4501. return PTR_ERR(inode);
  4502. inode->i_op = &btrfs_dir_inode_operations;
  4503. inode->i_fop = &btrfs_dir_file_operations;
  4504. inode->i_nlink = 1;
  4505. btrfs_i_size_write(inode, 0);
  4506. err = btrfs_update_inode(trans, new_root, inode);
  4507. BUG_ON(err);
  4508. iput(inode);
  4509. return 0;
  4510. }
  4511. /* helper function for file defrag and space balancing. This
  4512. * forces readahead on a given range of bytes in an inode
  4513. */
  4514. unsigned long btrfs_force_ra(struct address_space *mapping,
  4515. struct file_ra_state *ra, struct file *file,
  4516. pgoff_t offset, pgoff_t last_index)
  4517. {
  4518. pgoff_t req_size = last_index - offset + 1;
  4519. page_cache_sync_readahead(mapping, ra, file, offset, req_size);
  4520. return offset + req_size;
  4521. }
  4522. struct inode *btrfs_alloc_inode(struct super_block *sb)
  4523. {
  4524. struct btrfs_inode *ei;
  4525. ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
  4526. if (!ei)
  4527. return NULL;
  4528. ei->last_trans = 0;
  4529. ei->last_sub_trans = 0;
  4530. ei->logged_trans = 0;
  4531. ei->outstanding_extents = 0;
  4532. ei->reserved_extents = 0;
  4533. spin_lock_init(&ei->accounting_lock);
  4534. btrfs_ordered_inode_tree_init(&ei->ordered_tree);
  4535. INIT_LIST_HEAD(&ei->i_orphan);
  4536. INIT_LIST_HEAD(&ei->ordered_operations);
  4537. return &ei->vfs_inode;
  4538. }
  4539. void btrfs_destroy_inode(struct inode *inode)
  4540. {
  4541. struct btrfs_ordered_extent *ordered;
  4542. struct btrfs_root *root = BTRFS_I(inode)->root;
  4543. WARN_ON(!list_empty(&inode->i_dentry));
  4544. WARN_ON(inode->i_data.nrpages);
  4545. /*
  4546. * Make sure we're properly removed from the ordered operation
  4547. * lists.
  4548. */
  4549. smp_mb();
  4550. if (!list_empty(&BTRFS_I(inode)->ordered_operations)) {
  4551. spin_lock(&root->fs_info->ordered_extent_lock);
  4552. list_del_init(&BTRFS_I(inode)->ordered_operations);
  4553. spin_unlock(&root->fs_info->ordered_extent_lock);
  4554. }
  4555. spin_lock(&root->list_lock);
  4556. if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
  4557. printk(KERN_ERR "BTRFS: inode %lu: inode still on the orphan"
  4558. " list\n", inode->i_ino);
  4559. dump_stack();
  4560. }
  4561. spin_unlock(&root->list_lock);
  4562. while (1) {
  4563. ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
  4564. if (!ordered)
  4565. break;
  4566. else {
  4567. printk(KERN_ERR "btrfs found ordered "
  4568. "extent %llu %llu on inode cleanup\n",
  4569. (unsigned long long)ordered->file_offset,
  4570. (unsigned long long)ordered->len);
  4571. btrfs_remove_ordered_extent(inode, ordered);
  4572. btrfs_put_ordered_extent(ordered);
  4573. btrfs_put_ordered_extent(ordered);
  4574. }
  4575. }
  4576. inode_tree_del(inode);
  4577. btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
  4578. kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
  4579. }
  4580. void btrfs_drop_inode(struct inode *inode)
  4581. {
  4582. struct btrfs_root *root = BTRFS_I(inode)->root;
  4583. if (inode->i_nlink > 0 && btrfs_root_refs(&root->root_item) == 0)
  4584. generic_delete_inode(inode);
  4585. else
  4586. generic_drop_inode(inode);
  4587. }
  4588. static void init_once(void *foo)
  4589. {
  4590. struct btrfs_inode *ei = (struct btrfs_inode *) foo;
  4591. inode_init_once(&ei->vfs_inode);
  4592. }
  4593. void btrfs_destroy_cachep(void)
  4594. {
  4595. if (btrfs_inode_cachep)
  4596. kmem_cache_destroy(btrfs_inode_cachep);
  4597. if (btrfs_trans_handle_cachep)
  4598. kmem_cache_destroy(btrfs_trans_handle_cachep);
  4599. if (btrfs_transaction_cachep)
  4600. kmem_cache_destroy(btrfs_transaction_cachep);
  4601. if (btrfs_path_cachep)
  4602. kmem_cache_destroy(btrfs_path_cachep);
  4603. }
  4604. int btrfs_init_cachep(void)
  4605. {
  4606. btrfs_inode_cachep = kmem_cache_create("btrfs_inode_cache",
  4607. sizeof(struct btrfs_inode), 0,
  4608. SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
  4609. if (!btrfs_inode_cachep)
  4610. goto fail;
  4611. btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle_cache",
  4612. sizeof(struct btrfs_trans_handle), 0,
  4613. SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
  4614. if (!btrfs_trans_handle_cachep)
  4615. goto fail;
  4616. btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction_cache",
  4617. sizeof(struct btrfs_transaction), 0,
  4618. SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
  4619. if (!btrfs_transaction_cachep)
  4620. goto fail;
  4621. btrfs_path_cachep = kmem_cache_create("btrfs_path_cache",
  4622. sizeof(struct btrfs_path), 0,
  4623. SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
  4624. if (!btrfs_path_cachep)
  4625. goto fail;
  4626. return 0;
  4627. fail:
  4628. btrfs_destroy_cachep();
  4629. return -ENOMEM;
  4630. }
  4631. static int btrfs_getattr(struct vfsmount *mnt,
  4632. struct dentry *dentry, struct kstat *stat)
  4633. {
  4634. struct inode *inode = dentry->d_inode;
  4635. generic_fillattr(inode, stat);
  4636. stat->dev = BTRFS_I(inode)->root->anon_super.s_dev;
  4637. stat->blksize = PAGE_CACHE_SIZE;
  4638. stat->blocks = (inode_get_bytes(inode) +
  4639. BTRFS_I(inode)->delalloc_bytes) >> 9;
  4640. return 0;
  4641. }
  4642. static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
  4643. struct inode *new_dir, struct dentry *new_dentry)
  4644. {
  4645. struct btrfs_trans_handle *trans;
  4646. struct btrfs_root *root = BTRFS_I(old_dir)->root;
  4647. struct btrfs_root *dest = BTRFS_I(new_dir)->root;
  4648. struct inode *new_inode = new_dentry->d_inode;
  4649. struct inode *old_inode = old_dentry->d_inode;
  4650. struct timespec ctime = CURRENT_TIME;
  4651. u64 index = 0;
  4652. u64 root_objectid;
  4653. int ret;
  4654. if (new_dir->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
  4655. return -EPERM;
  4656. /* we only allow rename subvolume link between subvolumes */
  4657. if (old_inode->i_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
  4658. return -EXDEV;
  4659. if (old_inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
  4660. (new_inode && new_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID))
  4661. return -ENOTEMPTY;
  4662. if (S_ISDIR(old_inode->i_mode) && new_inode &&
  4663. new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
  4664. return -ENOTEMPTY;
  4665. /*
  4666. * 2 items for dir items
  4667. * 1 item for orphan entry
  4668. * 1 item for ref
  4669. */
  4670. ret = btrfs_reserve_metadata_space(root, 4);
  4671. if (ret)
  4672. return ret;
  4673. /*
  4674. * we're using rename to replace one file with another.
  4675. * and the replacement file is large. Start IO on it now so
  4676. * we don't add too much work to the end of the transaction
  4677. */
  4678. if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size &&
  4679. old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
  4680. filemap_flush(old_inode->i_mapping);
  4681. /* close the racy window with snapshot create/destroy ioctl */
  4682. if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
  4683. down_read(&root->fs_info->subvol_sem);
  4684. trans = btrfs_start_transaction(root, 1);
  4685. btrfs_set_trans_block_group(trans, new_dir);
  4686. if (dest != root)
  4687. btrfs_record_root_in_trans(trans, dest);
  4688. ret = btrfs_set_inode_index(new_dir, &index);
  4689. if (ret)
  4690. goto out_fail;
  4691. if (unlikely(old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
  4692. /* force full log commit if subvolume involved. */
  4693. root->fs_info->last_trans_log_full_commit = trans->transid;
  4694. } else {
  4695. ret = btrfs_insert_inode_ref(trans, dest,
  4696. new_dentry->d_name.name,
  4697. new_dentry->d_name.len,
  4698. old_inode->i_ino,
  4699. new_dir->i_ino, index);
  4700. if (ret)
  4701. goto out_fail;
  4702. /*
  4703. * this is an ugly little race, but the rename is required
  4704. * to make sure that if we crash, the inode is either at the
  4705. * old name or the new one. pinning the log transaction lets
  4706. * us make sure we don't allow a log commit to come in after
  4707. * we unlink the name but before we add the new name back in.
  4708. */
  4709. btrfs_pin_log_trans(root);
  4710. }
  4711. /*
  4712. * make sure the inode gets flushed if it is replacing
  4713. * something.
  4714. */
  4715. if (new_inode && new_inode->i_size &&
  4716. old_inode && S_ISREG(old_inode->i_mode)) {
  4717. btrfs_add_ordered_operation(trans, root, old_inode);
  4718. }
  4719. old_dir->i_ctime = old_dir->i_mtime = ctime;
  4720. new_dir->i_ctime = new_dir->i_mtime = ctime;
  4721. old_inode->i_ctime = ctime;
  4722. if (old_dentry->d_parent != new_dentry->d_parent)
  4723. btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
  4724. if (unlikely(old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
  4725. root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
  4726. ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
  4727. old_dentry->d_name.name,
  4728. old_dentry->d_name.len);
  4729. } else {
  4730. btrfs_inc_nlink(old_dentry->d_inode);
  4731. ret = btrfs_unlink_inode(trans, root, old_dir,
  4732. old_dentry->d_inode,
  4733. old_dentry->d_name.name,
  4734. old_dentry->d_name.len);
  4735. }
  4736. BUG_ON(ret);
  4737. if (new_inode) {
  4738. new_inode->i_ctime = CURRENT_TIME;
  4739. if (unlikely(new_inode->i_ino ==
  4740. BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
  4741. root_objectid = BTRFS_I(new_inode)->location.objectid;
  4742. ret = btrfs_unlink_subvol(trans, dest, new_dir,
  4743. root_objectid,
  4744. new_dentry->d_name.name,
  4745. new_dentry->d_name.len);
  4746. BUG_ON(new_inode->i_nlink == 0);
  4747. } else {
  4748. ret = btrfs_unlink_inode(trans, dest, new_dir,
  4749. new_dentry->d_inode,
  4750. new_dentry->d_name.name,
  4751. new_dentry->d_name.len);
  4752. }
  4753. BUG_ON(ret);
  4754. if (new_inode->i_nlink == 0) {
  4755. ret = btrfs_orphan_add(trans, new_dentry->d_inode);
  4756. BUG_ON(ret);
  4757. }
  4758. }
  4759. ret = btrfs_add_link(trans, new_dir, old_inode,
  4760. new_dentry->d_name.name,
  4761. new_dentry->d_name.len, 0, index);
  4762. BUG_ON(ret);
  4763. if (old_inode->i_ino != BTRFS_FIRST_FREE_OBJECTID) {
  4764. btrfs_log_new_name(trans, old_inode, old_dir,
  4765. new_dentry->d_parent);
  4766. btrfs_end_log_trans(root);
  4767. }
  4768. out_fail:
  4769. btrfs_end_transaction_throttle(trans, root);
  4770. if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
  4771. up_read(&root->fs_info->subvol_sem);
  4772. btrfs_unreserve_metadata_space(root, 4);
  4773. return ret;
  4774. }
  4775. /*
  4776. * some fairly slow code that needs optimization. This walks the list
  4777. * of all the inodes with pending delalloc and forces them to disk.
  4778. */
  4779. int btrfs_start_delalloc_inodes(struct btrfs_root *root)
  4780. {
  4781. struct list_head *head = &root->fs_info->delalloc_inodes;
  4782. struct btrfs_inode *binode;
  4783. struct inode *inode;
  4784. if (root->fs_info->sb->s_flags & MS_RDONLY)
  4785. return -EROFS;
  4786. spin_lock(&root->fs_info->delalloc_lock);
  4787. while (!list_empty(head)) {
  4788. binode = list_entry(head->next, struct btrfs_inode,
  4789. delalloc_inodes);
  4790. inode = igrab(&binode->vfs_inode);
  4791. if (!inode)
  4792. list_del_init(&binode->delalloc_inodes);
  4793. spin_unlock(&root->fs_info->delalloc_lock);
  4794. if (inode) {
  4795. filemap_flush(inode->i_mapping);
  4796. iput(inode);
  4797. }
  4798. cond_resched();
  4799. spin_lock(&root->fs_info->delalloc_lock);
  4800. }
  4801. spin_unlock(&root->fs_info->delalloc_lock);
  4802. /* the filemap_flush will queue IO into the worker threads, but
  4803. * we have to make sure the IO is actually started and that
  4804. * ordered extents get created before we return
  4805. */
  4806. atomic_inc(&root->fs_info->async_submit_draining);
  4807. while (atomic_read(&root->fs_info->nr_async_submits) ||
  4808. atomic_read(&root->fs_info->async_delalloc_pages)) {
  4809. wait_event(root->fs_info->async_submit_wait,
  4810. (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
  4811. atomic_read(&root->fs_info->async_delalloc_pages) == 0));
  4812. }
  4813. atomic_dec(&root->fs_info->async_submit_draining);
  4814. return 0;
  4815. }
  4816. static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
  4817. const char *symname)
  4818. {
  4819. struct btrfs_trans_handle *trans;
  4820. struct btrfs_root *root = BTRFS_I(dir)->root;
  4821. struct btrfs_path *path;
  4822. struct btrfs_key key;
  4823. struct inode *inode = NULL;
  4824. int err;
  4825. int drop_inode = 0;
  4826. u64 objectid;
  4827. u64 index = 0 ;
  4828. int name_len;
  4829. int datasize;
  4830. unsigned long ptr;
  4831. struct btrfs_file_extent_item *ei;
  4832. struct extent_buffer *leaf;
  4833. unsigned long nr = 0;
  4834. name_len = strlen(symname) + 1;
  4835. if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
  4836. return -ENAMETOOLONG;
  4837. /*
  4838. * 2 items for inode item and ref
  4839. * 2 items for dir items
  4840. * 1 item for xattr if selinux is on
  4841. */
  4842. err = btrfs_reserve_metadata_space(root, 5);
  4843. if (err)
  4844. return err;
  4845. trans = btrfs_start_transaction(root, 1);
  4846. if (!trans)
  4847. goto out_fail;
  4848. btrfs_set_trans_block_group(trans, dir);
  4849. err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
  4850. if (err) {
  4851. err = -ENOSPC;
  4852. goto out_unlock;
  4853. }
  4854. inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
  4855. dentry->d_name.len,
  4856. dentry->d_parent->d_inode->i_ino, objectid,
  4857. BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO,
  4858. &index);
  4859. err = PTR_ERR(inode);
  4860. if (IS_ERR(inode))
  4861. goto out_unlock;
  4862. err = btrfs_init_inode_security(inode, dir);
  4863. if (err) {
  4864. drop_inode = 1;
  4865. goto out_unlock;
  4866. }
  4867. btrfs_set_trans_block_group(trans, inode);
  4868. err = btrfs_add_nondir(trans, dentry, inode, 0, index);
  4869. if (err)
  4870. drop_inode = 1;
  4871. else {
  4872. inode->i_mapping->a_ops = &btrfs_aops;
  4873. inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
  4874. inode->i_fop = &btrfs_file_operations;
  4875. inode->i_op = &btrfs_file_inode_operations;
  4876. BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
  4877. }
  4878. btrfs_update_inode_block_group(trans, inode);
  4879. btrfs_update_inode_block_group(trans, dir);
  4880. if (drop_inode)
  4881. goto out_unlock;
  4882. path = btrfs_alloc_path();
  4883. BUG_ON(!path);
  4884. key.objectid = inode->i_ino;
  4885. key.offset = 0;
  4886. btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
  4887. datasize = btrfs_file_extent_calc_inline_size(name_len);
  4888. err = btrfs_insert_empty_item(trans, root, path, &key,
  4889. datasize);
  4890. if (err) {
  4891. drop_inode = 1;
  4892. goto out_unlock;
  4893. }
  4894. leaf = path->nodes[0];
  4895. ei = btrfs_item_ptr(leaf, path->slots[0],
  4896. struct btrfs_file_extent_item);
  4897. btrfs_set_file_extent_generation(leaf, ei, trans->transid);
  4898. btrfs_set_file_extent_type(leaf, ei,
  4899. BTRFS_FILE_EXTENT_INLINE);
  4900. btrfs_set_file_extent_encryption(leaf, ei, 0);
  4901. btrfs_set_file_extent_compression(leaf, ei, 0);
  4902. btrfs_set_file_extent_other_encoding(leaf, ei, 0);
  4903. btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
  4904. ptr = btrfs_file_extent_inline_start(ei);
  4905. write_extent_buffer(leaf, symname, ptr, name_len);
  4906. btrfs_mark_buffer_dirty(leaf);
  4907. btrfs_free_path(path);
  4908. inode->i_op = &btrfs_symlink_inode_operations;
  4909. inode->i_mapping->a_ops = &btrfs_symlink_aops;
  4910. inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
  4911. inode_set_bytes(inode, name_len);
  4912. btrfs_i_size_write(inode, name_len - 1);
  4913. err = btrfs_update_inode(trans, root, inode);
  4914. if (err)
  4915. drop_inode = 1;
  4916. out_unlock:
  4917. nr = trans->blocks_used;
  4918. btrfs_end_transaction_throttle(trans, root);
  4919. out_fail:
  4920. btrfs_unreserve_metadata_space(root, 5);
  4921. if (drop_inode) {
  4922. inode_dec_link_count(inode);
  4923. iput(inode);
  4924. }
  4925. btrfs_btree_balance_dirty(root, nr);
  4926. return err;
  4927. }
  4928. static int prealloc_file_range(struct btrfs_trans_handle *trans,
  4929. struct inode *inode, u64 start, u64 end,
  4930. u64 locked_end, u64 alloc_hint, int mode)
  4931. {
  4932. struct btrfs_root *root = BTRFS_I(inode)->root;
  4933. struct btrfs_key ins;
  4934. u64 alloc_size;
  4935. u64 cur_offset = start;
  4936. u64 num_bytes = end - start;
  4937. int ret = 0;
  4938. while (num_bytes > 0) {
  4939. alloc_size = min(num_bytes, root->fs_info->max_extent);
  4940. ret = btrfs_reserve_metadata_space(root, 1);
  4941. if (ret)
  4942. goto out;
  4943. ret = btrfs_reserve_extent(trans, root, alloc_size,
  4944. root->sectorsize, 0, alloc_hint,
  4945. (u64)-1, &ins, 1);
  4946. if (ret) {
  4947. WARN_ON(1);
  4948. goto out;
  4949. }
  4950. ret = insert_reserved_file_extent(trans, inode,
  4951. cur_offset, ins.objectid,
  4952. ins.offset, ins.offset,
  4953. ins.offset, locked_end,
  4954. 0, 0, 0,
  4955. BTRFS_FILE_EXTENT_PREALLOC);
  4956. BUG_ON(ret);
  4957. btrfs_drop_extent_cache(inode, cur_offset,
  4958. cur_offset + ins.offset -1, 0);
  4959. num_bytes -= ins.offset;
  4960. cur_offset += ins.offset;
  4961. alloc_hint = ins.objectid + ins.offset;
  4962. btrfs_unreserve_metadata_space(root, 1);
  4963. }
  4964. out:
  4965. if (cur_offset > start) {
  4966. inode->i_ctime = CURRENT_TIME;
  4967. BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
  4968. if (!(mode & FALLOC_FL_KEEP_SIZE) &&
  4969. cur_offset > i_size_read(inode))
  4970. btrfs_i_size_write(inode, cur_offset);
  4971. ret = btrfs_update_inode(trans, root, inode);
  4972. BUG_ON(ret);
  4973. }
  4974. return ret;
  4975. }
  4976. static long btrfs_fallocate(struct inode *inode, int mode,
  4977. loff_t offset, loff_t len)
  4978. {
  4979. u64 cur_offset;
  4980. u64 last_byte;
  4981. u64 alloc_start;
  4982. u64 alloc_end;
  4983. u64 alloc_hint = 0;
  4984. u64 locked_end;
  4985. u64 mask = BTRFS_I(inode)->root->sectorsize - 1;
  4986. struct extent_map *em;
  4987. struct btrfs_trans_handle *trans;
  4988. struct btrfs_root *root;
  4989. int ret;
  4990. alloc_start = offset & ~mask;
  4991. alloc_end = (offset + len + mask) & ~mask;
  4992. /*
  4993. * wait for ordered IO before we have any locks. We'll loop again
  4994. * below with the locks held.
  4995. */
  4996. btrfs_wait_ordered_range(inode, alloc_start, alloc_end - alloc_start);
  4997. mutex_lock(&inode->i_mutex);
  4998. if (alloc_start > inode->i_size) {
  4999. ret = btrfs_cont_expand(inode, alloc_start);
  5000. if (ret)
  5001. goto out;
  5002. }
  5003. root = BTRFS_I(inode)->root;
  5004. ret = btrfs_check_data_free_space(root, inode,
  5005. alloc_end - alloc_start);
  5006. if (ret)
  5007. goto out;
  5008. locked_end = alloc_end - 1;
  5009. while (1) {
  5010. struct btrfs_ordered_extent *ordered;
  5011. trans = btrfs_start_transaction(BTRFS_I(inode)->root, 1);
  5012. if (!trans) {
  5013. ret = -EIO;
  5014. goto out_free;
  5015. }
  5016. /* the extent lock is ordered inside the running
  5017. * transaction
  5018. */
  5019. lock_extent(&BTRFS_I(inode)->io_tree, alloc_start, locked_end,
  5020. GFP_NOFS);
  5021. ordered = btrfs_lookup_first_ordered_extent(inode,
  5022. alloc_end - 1);
  5023. if (ordered &&
  5024. ordered->file_offset + ordered->len > alloc_start &&
  5025. ordered->file_offset < alloc_end) {
  5026. btrfs_put_ordered_extent(ordered);
  5027. unlock_extent(&BTRFS_I(inode)->io_tree,
  5028. alloc_start, locked_end, GFP_NOFS);
  5029. btrfs_end_transaction(trans, BTRFS_I(inode)->root);
  5030. /*
  5031. * we can't wait on the range with the transaction
  5032. * running or with the extent lock held
  5033. */
  5034. btrfs_wait_ordered_range(inode, alloc_start,
  5035. alloc_end - alloc_start);
  5036. } else {
  5037. if (ordered)
  5038. btrfs_put_ordered_extent(ordered);
  5039. break;
  5040. }
  5041. }
  5042. cur_offset = alloc_start;
  5043. while (1) {
  5044. em = btrfs_get_extent(inode, NULL, 0, cur_offset,
  5045. alloc_end - cur_offset, 0);
  5046. BUG_ON(IS_ERR(em) || !em);
  5047. last_byte = min(extent_map_end(em), alloc_end);
  5048. last_byte = (last_byte + mask) & ~mask;
  5049. if (em->block_start == EXTENT_MAP_HOLE) {
  5050. ret = prealloc_file_range(trans, inode, cur_offset,
  5051. last_byte, locked_end + 1,
  5052. alloc_hint, mode);
  5053. if (ret < 0) {
  5054. free_extent_map(em);
  5055. break;
  5056. }
  5057. }
  5058. if (em->block_start <= EXTENT_MAP_LAST_BYTE)
  5059. alloc_hint = em->block_start;
  5060. free_extent_map(em);
  5061. cur_offset = last_byte;
  5062. if (cur_offset >= alloc_end) {
  5063. ret = 0;
  5064. break;
  5065. }
  5066. }
  5067. unlock_extent(&BTRFS_I(inode)->io_tree, alloc_start, locked_end,
  5068. GFP_NOFS);
  5069. btrfs_end_transaction(trans, BTRFS_I(inode)->root);
  5070. out_free:
  5071. btrfs_free_reserved_data_space(root, inode, alloc_end - alloc_start);
  5072. out:
  5073. mutex_unlock(&inode->i_mutex);
  5074. return ret;
  5075. }
  5076. static int btrfs_set_page_dirty(struct page *page)
  5077. {
  5078. return __set_page_dirty_nobuffers(page);
  5079. }
  5080. static int btrfs_permission(struct inode *inode, int mask)
  5081. {
  5082. if ((BTRFS_I(inode)->flags & BTRFS_INODE_READONLY) && (mask & MAY_WRITE))
  5083. return -EACCES;
  5084. return generic_permission(inode, mask, btrfs_check_acl);
  5085. }
  5086. static struct inode_operations btrfs_dir_inode_operations = {
  5087. .getattr = btrfs_getattr,
  5088. .lookup = btrfs_lookup,
  5089. .create = btrfs_create,
  5090. .unlink = btrfs_unlink,
  5091. .link = btrfs_link,
  5092. .mkdir = btrfs_mkdir,
  5093. .rmdir = btrfs_rmdir,
  5094. .rename = btrfs_rename,
  5095. .symlink = btrfs_symlink,
  5096. .setattr = btrfs_setattr,
  5097. .mknod = btrfs_mknod,
  5098. .setxattr = btrfs_setxattr,
  5099. .getxattr = btrfs_getxattr,
  5100. .listxattr = btrfs_listxattr,
  5101. .removexattr = btrfs_removexattr,
  5102. .permission = btrfs_permission,
  5103. };
  5104. static struct inode_operations btrfs_dir_ro_inode_operations = {
  5105. .lookup = btrfs_lookup,
  5106. .permission = btrfs_permission,
  5107. };
  5108. static struct file_operations btrfs_dir_file_operations = {
  5109. .llseek = generic_file_llseek,
  5110. .read = generic_read_dir,
  5111. .readdir = btrfs_real_readdir,
  5112. .unlocked_ioctl = btrfs_ioctl,
  5113. #ifdef CONFIG_COMPAT
  5114. .compat_ioctl = btrfs_ioctl,
  5115. #endif
  5116. .release = btrfs_release_file,
  5117. .fsync = btrfs_sync_file,
  5118. };
  5119. static struct extent_io_ops btrfs_extent_io_ops = {
  5120. .fill_delalloc = run_delalloc_range,
  5121. .submit_bio_hook = btrfs_submit_bio_hook,
  5122. .merge_bio_hook = btrfs_merge_bio_hook,
  5123. .readpage_end_io_hook = btrfs_readpage_end_io_hook,
  5124. .writepage_end_io_hook = btrfs_writepage_end_io_hook,
  5125. .writepage_start_hook = btrfs_writepage_start_hook,
  5126. .readpage_io_failed_hook = btrfs_io_failed_hook,
  5127. .set_bit_hook = btrfs_set_bit_hook,
  5128. .clear_bit_hook = btrfs_clear_bit_hook,
  5129. .merge_extent_hook = btrfs_merge_extent_hook,
  5130. .split_extent_hook = btrfs_split_extent_hook,
  5131. };
  5132. /*
  5133. * btrfs doesn't support the bmap operation because swapfiles
  5134. * use bmap to make a mapping of extents in the file. They assume
  5135. * these extents won't change over the life of the file and they
  5136. * use the bmap result to do IO directly to the drive.
  5137. *
  5138. * the btrfs bmap call would return logical addresses that aren't
  5139. * suitable for IO and they also will change frequently as COW
  5140. * operations happen. So, swapfile + btrfs == corruption.
  5141. *
  5142. * For now we're avoiding this by dropping bmap.
  5143. */
  5144. static struct address_space_operations btrfs_aops = {
  5145. .readpage = btrfs_readpage,
  5146. .writepage = btrfs_writepage,
  5147. .writepages = btrfs_writepages,
  5148. .readpages = btrfs_readpages,
  5149. .sync_page = block_sync_page,
  5150. .direct_IO = btrfs_direct_IO,
  5151. .invalidatepage = btrfs_invalidatepage,
  5152. .releasepage = btrfs_releasepage,
  5153. .set_page_dirty = btrfs_set_page_dirty,
  5154. };
  5155. static struct address_space_operations btrfs_symlink_aops = {
  5156. .readpage = btrfs_readpage,
  5157. .writepage = btrfs_writepage,
  5158. .invalidatepage = btrfs_invalidatepage,
  5159. .releasepage = btrfs_releasepage,
  5160. };
  5161. static struct inode_operations btrfs_file_inode_operations = {
  5162. .truncate = btrfs_truncate,
  5163. .getattr = btrfs_getattr,
  5164. .setattr = btrfs_setattr,
  5165. .setxattr = btrfs_setxattr,
  5166. .getxattr = btrfs_getxattr,
  5167. .listxattr = btrfs_listxattr,
  5168. .removexattr = btrfs_removexattr,
  5169. .permission = btrfs_permission,
  5170. .fallocate = btrfs_fallocate,
  5171. .fiemap = btrfs_fiemap,
  5172. };
  5173. static struct inode_operations btrfs_special_inode_operations = {
  5174. .getattr = btrfs_getattr,
  5175. .setattr = btrfs_setattr,
  5176. .permission = btrfs_permission,
  5177. .setxattr = btrfs_setxattr,
  5178. .getxattr = btrfs_getxattr,
  5179. .listxattr = btrfs_listxattr,
  5180. .removexattr = btrfs_removexattr,
  5181. };
  5182. static struct inode_operations btrfs_symlink_inode_operations = {
  5183. .readlink = generic_readlink,
  5184. .follow_link = page_follow_link_light,
  5185. .put_link = page_put_link,
  5186. .permission = btrfs_permission,
  5187. .setxattr = btrfs_setxattr,
  5188. .getxattr = btrfs_getxattr,
  5189. .listxattr = btrfs_listxattr,
  5190. .removexattr = btrfs_removexattr,
  5191. };
  5192. const struct dentry_operations btrfs_dentry_operations = {
  5193. .d_delete = btrfs_dentry_delete,
  5194. };