inode.c 193 KB

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