inode.c 187 KB

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