inode.c 199 KB

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