inode.c 190 KB

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