inode.c 203 KB

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