inode.c 202 KB

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