inode.c 203 KB

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