inode.c 202 KB

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