inode.c 201 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 (!list_empty(&root->orphan_list) ||
  1875. root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
  1876. return;
  1877. spin_lock(&root->orphan_lock);
  1878. if (!list_empty(&root->orphan_list)) {
  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 (list_empty(&BTRFS_I(inode)->i_orphan)) {
  1928. list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
  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. }
  1942. if (!BTRFS_I(inode)->orphan_meta_reserved) {
  1943. BTRFS_I(inode)->orphan_meta_reserved = 1;
  1944. reserve = 1;
  1945. }
  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. btrfs_abort_transaction(trans, root, ret);
  1957. return ret;
  1958. }
  1959. ret = 0;
  1960. }
  1961. /* insert an orphan item to track subvolume contains orphan files */
  1962. if (insert >= 2) {
  1963. ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
  1964. root->root_key.objectid);
  1965. if (ret && ret != -EEXIST) {
  1966. btrfs_abort_transaction(trans, root, ret);
  1967. return ret;
  1968. }
  1969. }
  1970. return 0;
  1971. }
  1972. /*
  1973. * We have done the truncate/delete so we can go ahead and remove the orphan
  1974. * item for this particular inode.
  1975. */
  1976. int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode)
  1977. {
  1978. struct btrfs_root *root = BTRFS_I(inode)->root;
  1979. int delete_item = 0;
  1980. int release_rsv = 0;
  1981. int ret = 0;
  1982. spin_lock(&root->orphan_lock);
  1983. if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
  1984. list_del_init(&BTRFS_I(inode)->i_orphan);
  1985. delete_item = 1;
  1986. }
  1987. if (BTRFS_I(inode)->orphan_meta_reserved) {
  1988. BTRFS_I(inode)->orphan_meta_reserved = 0;
  1989. release_rsv = 1;
  1990. }
  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. return 0;
  1999. }
  2000. /*
  2001. * this cleans up any orphans that may be left on the list from the last use
  2002. * of this root.
  2003. */
  2004. int btrfs_orphan_cleanup(struct btrfs_root *root)
  2005. {
  2006. struct btrfs_path *path;
  2007. struct extent_buffer *leaf;
  2008. struct btrfs_key key, found_key;
  2009. struct btrfs_trans_handle *trans;
  2010. struct inode *inode;
  2011. u64 last_objectid = 0;
  2012. int ret = 0, nr_unlink = 0, nr_truncate = 0;
  2013. if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
  2014. return 0;
  2015. path = btrfs_alloc_path();
  2016. if (!path) {
  2017. ret = -ENOMEM;
  2018. goto out;
  2019. }
  2020. path->reada = -1;
  2021. key.objectid = BTRFS_ORPHAN_OBJECTID;
  2022. btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
  2023. key.offset = (u64)-1;
  2024. while (1) {
  2025. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  2026. if (ret < 0)
  2027. goto out;
  2028. /*
  2029. * if ret == 0 means we found what we were searching for, which
  2030. * is weird, but possible, so only screw with path if we didn't
  2031. * find the key and see if we have stuff that matches
  2032. */
  2033. if (ret > 0) {
  2034. ret = 0;
  2035. if (path->slots[0] == 0)
  2036. break;
  2037. path->slots[0]--;
  2038. }
  2039. /* pull out the item */
  2040. leaf = path->nodes[0];
  2041. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  2042. /* make sure the item matches what we want */
  2043. if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
  2044. break;
  2045. if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
  2046. break;
  2047. /* release the path since we're done with it */
  2048. btrfs_release_path(path);
  2049. /*
  2050. * this is where we are basically btrfs_lookup, without the
  2051. * crossing root thing. we store the inode number in the
  2052. * offset of the orphan item.
  2053. */
  2054. if (found_key.offset == last_objectid) {
  2055. printk(KERN_ERR "btrfs: Error removing orphan entry, "
  2056. "stopping orphan cleanup\n");
  2057. ret = -EINVAL;
  2058. goto out;
  2059. }
  2060. last_objectid = found_key.offset;
  2061. found_key.objectid = found_key.offset;
  2062. found_key.type = BTRFS_INODE_ITEM_KEY;
  2063. found_key.offset = 0;
  2064. inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL);
  2065. ret = PTR_RET(inode);
  2066. if (ret && ret != -ESTALE)
  2067. goto out;
  2068. if (ret == -ESTALE && root == root->fs_info->tree_root) {
  2069. struct btrfs_root *dead_root;
  2070. struct btrfs_fs_info *fs_info = root->fs_info;
  2071. int is_dead_root = 0;
  2072. /*
  2073. * this is an orphan in the tree root. Currently these
  2074. * could come from 2 sources:
  2075. * a) a snapshot deletion in progress
  2076. * b) a free space cache inode
  2077. * We need to distinguish those two, as the snapshot
  2078. * orphan must not get deleted.
  2079. * find_dead_roots already ran before us, so if this
  2080. * is a snapshot deletion, we should find the root
  2081. * in the dead_roots list
  2082. */
  2083. spin_lock(&fs_info->trans_lock);
  2084. list_for_each_entry(dead_root, &fs_info->dead_roots,
  2085. root_list) {
  2086. if (dead_root->root_key.objectid ==
  2087. found_key.objectid) {
  2088. is_dead_root = 1;
  2089. break;
  2090. }
  2091. }
  2092. spin_unlock(&fs_info->trans_lock);
  2093. if (is_dead_root) {
  2094. /* prevent this orphan from being found again */
  2095. key.offset = found_key.objectid - 1;
  2096. continue;
  2097. }
  2098. }
  2099. /*
  2100. * Inode is already gone but the orphan item is still there,
  2101. * kill the orphan item.
  2102. */
  2103. if (ret == -ESTALE) {
  2104. trans = btrfs_start_transaction(root, 1);
  2105. if (IS_ERR(trans)) {
  2106. ret = PTR_ERR(trans);
  2107. goto out;
  2108. }
  2109. ret = btrfs_del_orphan_item(trans, root,
  2110. found_key.objectid);
  2111. BUG_ON(ret); /* -ENOMEM or corruption (JDM: Recheck) */
  2112. btrfs_end_transaction(trans, root);
  2113. continue;
  2114. }
  2115. /*
  2116. * add this inode to the orphan list so btrfs_orphan_del does
  2117. * the proper thing when we hit it
  2118. */
  2119. spin_lock(&root->orphan_lock);
  2120. list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
  2121. spin_unlock(&root->orphan_lock);
  2122. /* if we have links, this was a truncate, lets do that */
  2123. if (inode->i_nlink) {
  2124. if (!S_ISREG(inode->i_mode)) {
  2125. WARN_ON(1);
  2126. iput(inode);
  2127. continue;
  2128. }
  2129. nr_truncate++;
  2130. ret = btrfs_truncate(inode);
  2131. } else {
  2132. nr_unlink++;
  2133. }
  2134. /* this will do delete_inode and everything for us */
  2135. iput(inode);
  2136. if (ret)
  2137. goto out;
  2138. }
  2139. /* release the path since we're done with it */
  2140. btrfs_release_path(path);
  2141. root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
  2142. if (root->orphan_block_rsv)
  2143. btrfs_block_rsv_release(root, root->orphan_block_rsv,
  2144. (u64)-1);
  2145. if (root->orphan_block_rsv || root->orphan_item_inserted) {
  2146. trans = btrfs_join_transaction(root);
  2147. if (!IS_ERR(trans))
  2148. btrfs_end_transaction(trans, root);
  2149. }
  2150. if (nr_unlink)
  2151. printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink);
  2152. if (nr_truncate)
  2153. printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate);
  2154. out:
  2155. if (ret)
  2156. printk(KERN_CRIT "btrfs: could not do orphan cleanup %d\n", ret);
  2157. btrfs_free_path(path);
  2158. return ret;
  2159. }
  2160. /*
  2161. * very simple check to peek ahead in the leaf looking for xattrs. If we
  2162. * don't find any xattrs, we know there can't be any acls.
  2163. *
  2164. * slot is the slot the inode is in, objectid is the objectid of the inode
  2165. */
  2166. static noinline int acls_after_inode_item(struct extent_buffer *leaf,
  2167. int slot, u64 objectid)
  2168. {
  2169. u32 nritems = btrfs_header_nritems(leaf);
  2170. struct btrfs_key found_key;
  2171. int scanned = 0;
  2172. slot++;
  2173. while (slot < nritems) {
  2174. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  2175. /* we found a different objectid, there must not be acls */
  2176. if (found_key.objectid != objectid)
  2177. return 0;
  2178. /* we found an xattr, assume we've got an acl */
  2179. if (found_key.type == BTRFS_XATTR_ITEM_KEY)
  2180. return 1;
  2181. /*
  2182. * we found a key greater than an xattr key, there can't
  2183. * be any acls later on
  2184. */
  2185. if (found_key.type > BTRFS_XATTR_ITEM_KEY)
  2186. return 0;
  2187. slot++;
  2188. scanned++;
  2189. /*
  2190. * it goes inode, inode backrefs, xattrs, extents,
  2191. * so if there are a ton of hard links to an inode there can
  2192. * be a lot of backrefs. Don't waste time searching too hard,
  2193. * this is just an optimization
  2194. */
  2195. if (scanned >= 8)
  2196. break;
  2197. }
  2198. /* we hit the end of the leaf before we found an xattr or
  2199. * something larger than an xattr. We have to assume the inode
  2200. * has acls
  2201. */
  2202. return 1;
  2203. }
  2204. /*
  2205. * read an inode from the btree into the in-memory inode
  2206. */
  2207. static void btrfs_read_locked_inode(struct inode *inode)
  2208. {
  2209. struct btrfs_path *path;
  2210. struct extent_buffer *leaf;
  2211. struct btrfs_inode_item *inode_item;
  2212. struct btrfs_timespec *tspec;
  2213. struct btrfs_root *root = BTRFS_I(inode)->root;
  2214. struct btrfs_key location;
  2215. int maybe_acls;
  2216. u32 rdev;
  2217. int ret;
  2218. bool filled = false;
  2219. ret = btrfs_fill_inode(inode, &rdev);
  2220. if (!ret)
  2221. filled = true;
  2222. path = btrfs_alloc_path();
  2223. if (!path)
  2224. goto make_bad;
  2225. path->leave_spinning = 1;
  2226. memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
  2227. ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
  2228. if (ret)
  2229. goto make_bad;
  2230. leaf = path->nodes[0];
  2231. if (filled)
  2232. goto cache_acl;
  2233. inode_item = btrfs_item_ptr(leaf, path->slots[0],
  2234. struct btrfs_inode_item);
  2235. inode->i_mode = btrfs_inode_mode(leaf, inode_item);
  2236. set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
  2237. inode->i_uid = btrfs_inode_uid(leaf, inode_item);
  2238. inode->i_gid = btrfs_inode_gid(leaf, inode_item);
  2239. btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
  2240. tspec = btrfs_inode_atime(inode_item);
  2241. inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
  2242. inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
  2243. tspec = btrfs_inode_mtime(inode_item);
  2244. inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
  2245. inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
  2246. tspec = btrfs_inode_ctime(inode_item);
  2247. inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
  2248. inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
  2249. inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
  2250. BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
  2251. inode->i_version = btrfs_inode_sequence(leaf, inode_item);
  2252. inode->i_generation = BTRFS_I(inode)->generation;
  2253. inode->i_rdev = 0;
  2254. rdev = btrfs_inode_rdev(leaf, inode_item);
  2255. BTRFS_I(inode)->index_cnt = (u64)-1;
  2256. BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
  2257. cache_acl:
  2258. /*
  2259. * try to precache a NULL acl entry for files that don't have
  2260. * any xattrs or acls
  2261. */
  2262. maybe_acls = acls_after_inode_item(leaf, path->slots[0],
  2263. btrfs_ino(inode));
  2264. if (!maybe_acls)
  2265. cache_no_acl(inode);
  2266. btrfs_free_path(path);
  2267. switch (inode->i_mode & S_IFMT) {
  2268. case S_IFREG:
  2269. inode->i_mapping->a_ops = &btrfs_aops;
  2270. inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
  2271. BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
  2272. inode->i_fop = &btrfs_file_operations;
  2273. inode->i_op = &btrfs_file_inode_operations;
  2274. break;
  2275. case S_IFDIR:
  2276. inode->i_fop = &btrfs_dir_file_operations;
  2277. if (root == root->fs_info->tree_root)
  2278. inode->i_op = &btrfs_dir_ro_inode_operations;
  2279. else
  2280. inode->i_op = &btrfs_dir_inode_operations;
  2281. break;
  2282. case S_IFLNK:
  2283. inode->i_op = &btrfs_symlink_inode_operations;
  2284. inode->i_mapping->a_ops = &btrfs_symlink_aops;
  2285. inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
  2286. break;
  2287. default:
  2288. inode->i_op = &btrfs_special_inode_operations;
  2289. init_special_inode(inode, inode->i_mode, rdev);
  2290. break;
  2291. }
  2292. btrfs_update_iflags(inode);
  2293. return;
  2294. make_bad:
  2295. btrfs_free_path(path);
  2296. make_bad_inode(inode);
  2297. }
  2298. /*
  2299. * given a leaf and an inode, copy the inode fields into the leaf
  2300. */
  2301. static void fill_inode_item(struct btrfs_trans_handle *trans,
  2302. struct extent_buffer *leaf,
  2303. struct btrfs_inode_item *item,
  2304. struct inode *inode)
  2305. {
  2306. btrfs_set_inode_uid(leaf, item, inode->i_uid);
  2307. btrfs_set_inode_gid(leaf, item, inode->i_gid);
  2308. btrfs_set_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size);
  2309. btrfs_set_inode_mode(leaf, item, inode->i_mode);
  2310. btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
  2311. btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
  2312. inode->i_atime.tv_sec);
  2313. btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
  2314. inode->i_atime.tv_nsec);
  2315. btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
  2316. inode->i_mtime.tv_sec);
  2317. btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
  2318. inode->i_mtime.tv_nsec);
  2319. btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
  2320. inode->i_ctime.tv_sec);
  2321. btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
  2322. inode->i_ctime.tv_nsec);
  2323. btrfs_set_inode_nbytes(leaf, item, inode_get_bytes(inode));
  2324. btrfs_set_inode_generation(leaf, item, BTRFS_I(inode)->generation);
  2325. btrfs_set_inode_sequence(leaf, item, inode->i_version);
  2326. btrfs_set_inode_transid(leaf, item, trans->transid);
  2327. btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
  2328. btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
  2329. btrfs_set_inode_block_group(leaf, item, 0);
  2330. }
  2331. /*
  2332. * copy everything in the in-memory inode into the btree.
  2333. */
  2334. static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
  2335. struct btrfs_root *root, struct inode *inode)
  2336. {
  2337. struct btrfs_inode_item *inode_item;
  2338. struct btrfs_path *path;
  2339. struct extent_buffer *leaf;
  2340. int ret;
  2341. path = btrfs_alloc_path();
  2342. if (!path)
  2343. return -ENOMEM;
  2344. path->leave_spinning = 1;
  2345. ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
  2346. 1);
  2347. if (ret) {
  2348. if (ret > 0)
  2349. ret = -ENOENT;
  2350. goto failed;
  2351. }
  2352. btrfs_unlock_up_safe(path, 1);
  2353. leaf = path->nodes[0];
  2354. inode_item = btrfs_item_ptr(leaf, path->slots[0],
  2355. struct btrfs_inode_item);
  2356. fill_inode_item(trans, leaf, inode_item, inode);
  2357. btrfs_mark_buffer_dirty(leaf);
  2358. btrfs_set_inode_last_trans(trans, inode);
  2359. ret = 0;
  2360. failed:
  2361. btrfs_free_path(path);
  2362. return ret;
  2363. }
  2364. /*
  2365. * copy everything in the in-memory inode into the btree.
  2366. */
  2367. noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
  2368. struct btrfs_root *root, struct inode *inode)
  2369. {
  2370. int ret;
  2371. /*
  2372. * If the inode is a free space inode, we can deadlock during commit
  2373. * if we put it into the delayed code.
  2374. *
  2375. * The data relocation inode should also be directly updated
  2376. * without delay
  2377. */
  2378. if (!btrfs_is_free_space_inode(root, inode)
  2379. && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID) {
  2380. ret = btrfs_delayed_update_inode(trans, root, inode);
  2381. if (!ret)
  2382. btrfs_set_inode_last_trans(trans, inode);
  2383. return ret;
  2384. }
  2385. return btrfs_update_inode_item(trans, root, inode);
  2386. }
  2387. static noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
  2388. struct btrfs_root *root, struct inode *inode)
  2389. {
  2390. int ret;
  2391. ret = btrfs_update_inode(trans, root, inode);
  2392. if (ret == -ENOSPC)
  2393. return btrfs_update_inode_item(trans, root, inode);
  2394. return ret;
  2395. }
  2396. /*
  2397. * unlink helper that gets used here in inode.c and in the tree logging
  2398. * recovery code. It remove a link in a directory with a given name, and
  2399. * also drops the back refs in the inode to the directory
  2400. */
  2401. static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
  2402. struct btrfs_root *root,
  2403. struct inode *dir, struct inode *inode,
  2404. const char *name, int name_len)
  2405. {
  2406. struct btrfs_path *path;
  2407. int ret = 0;
  2408. struct extent_buffer *leaf;
  2409. struct btrfs_dir_item *di;
  2410. struct btrfs_key key;
  2411. u64 index;
  2412. u64 ino = btrfs_ino(inode);
  2413. u64 dir_ino = btrfs_ino(dir);
  2414. path = btrfs_alloc_path();
  2415. if (!path) {
  2416. ret = -ENOMEM;
  2417. goto out;
  2418. }
  2419. path->leave_spinning = 1;
  2420. di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
  2421. name, name_len, -1);
  2422. if (IS_ERR(di)) {
  2423. ret = PTR_ERR(di);
  2424. goto err;
  2425. }
  2426. if (!di) {
  2427. ret = -ENOENT;
  2428. goto err;
  2429. }
  2430. leaf = path->nodes[0];
  2431. btrfs_dir_item_key_to_cpu(leaf, di, &key);
  2432. ret = btrfs_delete_one_dir_name(trans, root, path, di);
  2433. if (ret)
  2434. goto err;
  2435. btrfs_release_path(path);
  2436. ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
  2437. dir_ino, &index);
  2438. if (ret) {
  2439. printk(KERN_INFO "btrfs failed to delete reference to %.*s, "
  2440. "inode %llu parent %llu\n", name_len, name,
  2441. (unsigned long long)ino, (unsigned long long)dir_ino);
  2442. btrfs_abort_transaction(trans, root, ret);
  2443. goto err;
  2444. }
  2445. ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
  2446. if (ret) {
  2447. btrfs_abort_transaction(trans, root, ret);
  2448. goto err;
  2449. }
  2450. ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
  2451. inode, dir_ino);
  2452. if (ret != 0 && ret != -ENOENT) {
  2453. btrfs_abort_transaction(trans, root, ret);
  2454. goto err;
  2455. }
  2456. ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
  2457. dir, index);
  2458. if (ret == -ENOENT)
  2459. ret = 0;
  2460. err:
  2461. btrfs_free_path(path);
  2462. if (ret)
  2463. goto out;
  2464. btrfs_i_size_write(dir, dir->i_size - name_len * 2);
  2465. inode_inc_iversion(inode);
  2466. inode_inc_iversion(dir);
  2467. inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
  2468. btrfs_update_inode(trans, root, dir);
  2469. out:
  2470. return ret;
  2471. }
  2472. int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
  2473. struct btrfs_root *root,
  2474. struct inode *dir, struct inode *inode,
  2475. const char *name, int name_len)
  2476. {
  2477. int ret;
  2478. ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
  2479. if (!ret) {
  2480. btrfs_drop_nlink(inode);
  2481. ret = btrfs_update_inode(trans, root, inode);
  2482. }
  2483. return ret;
  2484. }
  2485. /* helper to check if there is any shared block in the path */
  2486. static int check_path_shared(struct btrfs_root *root,
  2487. struct btrfs_path *path)
  2488. {
  2489. struct extent_buffer *eb;
  2490. int level;
  2491. u64 refs = 1;
  2492. for (level = 0; level < BTRFS_MAX_LEVEL; level++) {
  2493. int ret;
  2494. if (!path->nodes[level])
  2495. break;
  2496. eb = path->nodes[level];
  2497. if (!btrfs_block_can_be_shared(root, eb))
  2498. continue;
  2499. ret = btrfs_lookup_extent_info(NULL, root, eb->start, eb->len,
  2500. &refs, NULL);
  2501. if (refs > 1)
  2502. return 1;
  2503. }
  2504. return 0;
  2505. }
  2506. /*
  2507. * helper to start transaction for unlink and rmdir.
  2508. *
  2509. * unlink and rmdir are special in btrfs, they do not always free space.
  2510. * so in enospc case, we should make sure they will free space before
  2511. * allowing them to use the global metadata reservation.
  2512. */
  2513. static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir,
  2514. struct dentry *dentry)
  2515. {
  2516. struct btrfs_trans_handle *trans;
  2517. struct btrfs_root *root = BTRFS_I(dir)->root;
  2518. struct btrfs_path *path;
  2519. struct btrfs_inode_ref *ref;
  2520. struct btrfs_dir_item *di;
  2521. struct inode *inode = dentry->d_inode;
  2522. u64 index;
  2523. int check_link = 1;
  2524. int err = -ENOSPC;
  2525. int ret;
  2526. u64 ino = btrfs_ino(inode);
  2527. u64 dir_ino = btrfs_ino(dir);
  2528. /*
  2529. * 1 for the possible orphan item
  2530. * 1 for the dir item
  2531. * 1 for the dir index
  2532. * 1 for the inode ref
  2533. * 1 for the inode ref in the tree log
  2534. * 2 for the dir entries in the log
  2535. * 1 for the inode
  2536. */
  2537. trans = btrfs_start_transaction(root, 8);
  2538. if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
  2539. return trans;
  2540. if (ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
  2541. return ERR_PTR(-ENOSPC);
  2542. /* check if there is someone else holds reference */
  2543. if (S_ISDIR(inode->i_mode) && atomic_read(&inode->i_count) > 1)
  2544. return ERR_PTR(-ENOSPC);
  2545. if (atomic_read(&inode->i_count) > 2)
  2546. return ERR_PTR(-ENOSPC);
  2547. if (xchg(&root->fs_info->enospc_unlink, 1))
  2548. return ERR_PTR(-ENOSPC);
  2549. path = btrfs_alloc_path();
  2550. if (!path) {
  2551. root->fs_info->enospc_unlink = 0;
  2552. return ERR_PTR(-ENOMEM);
  2553. }
  2554. /* 1 for the orphan item */
  2555. trans = btrfs_start_transaction(root, 1);
  2556. if (IS_ERR(trans)) {
  2557. btrfs_free_path(path);
  2558. root->fs_info->enospc_unlink = 0;
  2559. return trans;
  2560. }
  2561. path->skip_locking = 1;
  2562. path->search_commit_root = 1;
  2563. ret = btrfs_lookup_inode(trans, root, path,
  2564. &BTRFS_I(dir)->location, 0);
  2565. if (ret < 0) {
  2566. err = ret;
  2567. goto out;
  2568. }
  2569. if (ret == 0) {
  2570. if (check_path_shared(root, path))
  2571. goto out;
  2572. } else {
  2573. check_link = 0;
  2574. }
  2575. btrfs_release_path(path);
  2576. ret = btrfs_lookup_inode(trans, root, path,
  2577. &BTRFS_I(inode)->location, 0);
  2578. if (ret < 0) {
  2579. err = ret;
  2580. goto out;
  2581. }
  2582. if (ret == 0) {
  2583. if (check_path_shared(root, path))
  2584. goto out;
  2585. } else {
  2586. check_link = 0;
  2587. }
  2588. btrfs_release_path(path);
  2589. if (ret == 0 && S_ISREG(inode->i_mode)) {
  2590. ret = btrfs_lookup_file_extent(trans, root, path,
  2591. ino, (u64)-1, 0);
  2592. if (ret < 0) {
  2593. err = ret;
  2594. goto out;
  2595. }
  2596. BUG_ON(ret == 0); /* Corruption */
  2597. if (check_path_shared(root, path))
  2598. goto out;
  2599. btrfs_release_path(path);
  2600. }
  2601. if (!check_link) {
  2602. err = 0;
  2603. goto out;
  2604. }
  2605. di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
  2606. dentry->d_name.name, dentry->d_name.len, 0);
  2607. if (IS_ERR(di)) {
  2608. err = PTR_ERR(di);
  2609. goto out;
  2610. }
  2611. if (di) {
  2612. if (check_path_shared(root, path))
  2613. goto out;
  2614. } else {
  2615. err = 0;
  2616. goto out;
  2617. }
  2618. btrfs_release_path(path);
  2619. ref = btrfs_lookup_inode_ref(trans, root, path,
  2620. dentry->d_name.name, dentry->d_name.len,
  2621. ino, dir_ino, 0);
  2622. if (IS_ERR(ref)) {
  2623. err = PTR_ERR(ref);
  2624. goto out;
  2625. }
  2626. BUG_ON(!ref); /* Logic error */
  2627. if (check_path_shared(root, path))
  2628. goto out;
  2629. index = btrfs_inode_ref_index(path->nodes[0], ref);
  2630. btrfs_release_path(path);
  2631. /*
  2632. * This is a commit root search, if we can lookup inode item and other
  2633. * relative items in the commit root, it means the transaction of
  2634. * dir/file creation has been committed, and the dir index item that we
  2635. * delay to insert has also been inserted into the commit root. So
  2636. * we needn't worry about the delayed insertion of the dir index item
  2637. * here.
  2638. */
  2639. di = btrfs_lookup_dir_index_item(trans, root, path, dir_ino, index,
  2640. dentry->d_name.name, dentry->d_name.len, 0);
  2641. if (IS_ERR(di)) {
  2642. err = PTR_ERR(di);
  2643. goto out;
  2644. }
  2645. BUG_ON(ret == -ENOENT);
  2646. if (check_path_shared(root, path))
  2647. goto out;
  2648. err = 0;
  2649. out:
  2650. btrfs_free_path(path);
  2651. /* Migrate the orphan reservation over */
  2652. if (!err)
  2653. err = btrfs_block_rsv_migrate(trans->block_rsv,
  2654. &root->fs_info->global_block_rsv,
  2655. trans->bytes_reserved);
  2656. if (err) {
  2657. btrfs_end_transaction(trans, root);
  2658. root->fs_info->enospc_unlink = 0;
  2659. return ERR_PTR(err);
  2660. }
  2661. trans->block_rsv = &root->fs_info->global_block_rsv;
  2662. return trans;
  2663. }
  2664. static void __unlink_end_trans(struct btrfs_trans_handle *trans,
  2665. struct btrfs_root *root)
  2666. {
  2667. if (trans->block_rsv == &root->fs_info->global_block_rsv) {
  2668. btrfs_block_rsv_release(root, trans->block_rsv,
  2669. trans->bytes_reserved);
  2670. trans->block_rsv = &root->fs_info->trans_block_rsv;
  2671. BUG_ON(!root->fs_info->enospc_unlink);
  2672. root->fs_info->enospc_unlink = 0;
  2673. }
  2674. btrfs_end_transaction(trans, root);
  2675. }
  2676. static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
  2677. {
  2678. struct btrfs_root *root = BTRFS_I(dir)->root;
  2679. struct btrfs_trans_handle *trans;
  2680. struct inode *inode = dentry->d_inode;
  2681. int ret;
  2682. unsigned long nr = 0;
  2683. trans = __unlink_start_trans(dir, dentry);
  2684. if (IS_ERR(trans))
  2685. return PTR_ERR(trans);
  2686. btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0);
  2687. ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
  2688. dentry->d_name.name, dentry->d_name.len);
  2689. if (ret)
  2690. goto out;
  2691. if (inode->i_nlink == 0) {
  2692. ret = btrfs_orphan_add(trans, inode);
  2693. if (ret)
  2694. goto out;
  2695. }
  2696. out:
  2697. nr = trans->blocks_used;
  2698. __unlink_end_trans(trans, root);
  2699. btrfs_btree_balance_dirty(root, nr);
  2700. return ret;
  2701. }
  2702. int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
  2703. struct btrfs_root *root,
  2704. struct inode *dir, u64 objectid,
  2705. const char *name, int name_len)
  2706. {
  2707. struct btrfs_path *path;
  2708. struct extent_buffer *leaf;
  2709. struct btrfs_dir_item *di;
  2710. struct btrfs_key key;
  2711. u64 index;
  2712. int ret;
  2713. u64 dir_ino = btrfs_ino(dir);
  2714. path = btrfs_alloc_path();
  2715. if (!path)
  2716. return -ENOMEM;
  2717. di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
  2718. name, name_len, -1);
  2719. if (IS_ERR_OR_NULL(di)) {
  2720. if (!di)
  2721. ret = -ENOENT;
  2722. else
  2723. ret = PTR_ERR(di);
  2724. goto out;
  2725. }
  2726. leaf = path->nodes[0];
  2727. btrfs_dir_item_key_to_cpu(leaf, di, &key);
  2728. WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
  2729. ret = btrfs_delete_one_dir_name(trans, root, path, di);
  2730. if (ret) {
  2731. btrfs_abort_transaction(trans, root, ret);
  2732. goto out;
  2733. }
  2734. btrfs_release_path(path);
  2735. ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
  2736. objectid, root->root_key.objectid,
  2737. dir_ino, &index, name, name_len);
  2738. if (ret < 0) {
  2739. if (ret != -ENOENT) {
  2740. btrfs_abort_transaction(trans, root, ret);
  2741. goto out;
  2742. }
  2743. di = btrfs_search_dir_index_item(root, path, dir_ino,
  2744. name, name_len);
  2745. if (IS_ERR_OR_NULL(di)) {
  2746. if (!di)
  2747. ret = -ENOENT;
  2748. else
  2749. ret = PTR_ERR(di);
  2750. btrfs_abort_transaction(trans, root, ret);
  2751. goto out;
  2752. }
  2753. leaf = path->nodes[0];
  2754. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  2755. btrfs_release_path(path);
  2756. index = key.offset;
  2757. }
  2758. btrfs_release_path(path);
  2759. ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
  2760. if (ret) {
  2761. btrfs_abort_transaction(trans, root, ret);
  2762. goto out;
  2763. }
  2764. btrfs_i_size_write(dir, dir->i_size - name_len * 2);
  2765. inode_inc_iversion(dir);
  2766. dir->i_mtime = dir->i_ctime = CURRENT_TIME;
  2767. ret = btrfs_update_inode(trans, root, dir);
  2768. if (ret)
  2769. btrfs_abort_transaction(trans, root, ret);
  2770. out:
  2771. btrfs_free_path(path);
  2772. return ret;
  2773. }
  2774. static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
  2775. {
  2776. struct inode *inode = dentry->d_inode;
  2777. int err = 0;
  2778. struct btrfs_root *root = BTRFS_I(dir)->root;
  2779. struct btrfs_trans_handle *trans;
  2780. unsigned long nr = 0;
  2781. if (inode->i_size > BTRFS_EMPTY_DIR_SIZE ||
  2782. btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID)
  2783. return -ENOTEMPTY;
  2784. trans = __unlink_start_trans(dir, dentry);
  2785. if (IS_ERR(trans))
  2786. return PTR_ERR(trans);
  2787. if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
  2788. err = btrfs_unlink_subvol(trans, root, dir,
  2789. BTRFS_I(inode)->location.objectid,
  2790. dentry->d_name.name,
  2791. dentry->d_name.len);
  2792. goto out;
  2793. }
  2794. err = btrfs_orphan_add(trans, inode);
  2795. if (err)
  2796. goto out;
  2797. /* now the directory is empty */
  2798. err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
  2799. dentry->d_name.name, dentry->d_name.len);
  2800. if (!err)
  2801. btrfs_i_size_write(inode, 0);
  2802. out:
  2803. nr = trans->blocks_used;
  2804. __unlink_end_trans(trans, root);
  2805. btrfs_btree_balance_dirty(root, nr);
  2806. return err;
  2807. }
  2808. /*
  2809. * this can truncate away extent items, csum items and directory items.
  2810. * It starts at a high offset and removes keys until it can't find
  2811. * any higher than new_size
  2812. *
  2813. * csum items that cross the new i_size are truncated to the new size
  2814. * as well.
  2815. *
  2816. * min_type is the minimum key type to truncate down to. If set to 0, this
  2817. * will kill all the items on this inode, including the INODE_ITEM_KEY.
  2818. */
  2819. int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
  2820. struct btrfs_root *root,
  2821. struct inode *inode,
  2822. u64 new_size, u32 min_type)
  2823. {
  2824. struct btrfs_path *path;
  2825. struct extent_buffer *leaf;
  2826. struct btrfs_file_extent_item *fi;
  2827. struct btrfs_key key;
  2828. struct btrfs_key found_key;
  2829. u64 extent_start = 0;
  2830. u64 extent_num_bytes = 0;
  2831. u64 extent_offset = 0;
  2832. u64 item_end = 0;
  2833. u64 mask = root->sectorsize - 1;
  2834. u32 found_type = (u8)-1;
  2835. int found_extent;
  2836. int del_item;
  2837. int pending_del_nr = 0;
  2838. int pending_del_slot = 0;
  2839. int extent_type = -1;
  2840. int ret;
  2841. int err = 0;
  2842. u64 ino = btrfs_ino(inode);
  2843. BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
  2844. path = btrfs_alloc_path();
  2845. if (!path)
  2846. return -ENOMEM;
  2847. path->reada = -1;
  2848. if (root->ref_cows || root == root->fs_info->tree_root)
  2849. btrfs_drop_extent_cache(inode, new_size & (~mask), (u64)-1, 0);
  2850. /*
  2851. * This function is also used to drop the items in the log tree before
  2852. * we relog the inode, so if root != BTRFS_I(inode)->root, it means
  2853. * it is used to drop the loged items. So we shouldn't kill the delayed
  2854. * items.
  2855. */
  2856. if (min_type == 0 && root == BTRFS_I(inode)->root)
  2857. btrfs_kill_delayed_inode_items(inode);
  2858. key.objectid = ino;
  2859. key.offset = (u64)-1;
  2860. key.type = (u8)-1;
  2861. search_again:
  2862. path->leave_spinning = 1;
  2863. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  2864. if (ret < 0) {
  2865. err = ret;
  2866. goto out;
  2867. }
  2868. if (ret > 0) {
  2869. /* there are no items in the tree for us to truncate, we're
  2870. * done
  2871. */
  2872. if (path->slots[0] == 0)
  2873. goto out;
  2874. path->slots[0]--;
  2875. }
  2876. while (1) {
  2877. fi = NULL;
  2878. leaf = path->nodes[0];
  2879. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  2880. found_type = btrfs_key_type(&found_key);
  2881. if (found_key.objectid != ino)
  2882. break;
  2883. if (found_type < min_type)
  2884. break;
  2885. item_end = found_key.offset;
  2886. if (found_type == BTRFS_EXTENT_DATA_KEY) {
  2887. fi = btrfs_item_ptr(leaf, path->slots[0],
  2888. struct btrfs_file_extent_item);
  2889. extent_type = btrfs_file_extent_type(leaf, fi);
  2890. if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
  2891. item_end +=
  2892. btrfs_file_extent_num_bytes(leaf, fi);
  2893. } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
  2894. item_end += btrfs_file_extent_inline_len(leaf,
  2895. fi);
  2896. }
  2897. item_end--;
  2898. }
  2899. if (found_type > min_type) {
  2900. del_item = 1;
  2901. } else {
  2902. if (item_end < new_size)
  2903. break;
  2904. if (found_key.offset >= new_size)
  2905. del_item = 1;
  2906. else
  2907. del_item = 0;
  2908. }
  2909. found_extent = 0;
  2910. /* FIXME, shrink the extent if the ref count is only 1 */
  2911. if (found_type != BTRFS_EXTENT_DATA_KEY)
  2912. goto delete;
  2913. if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
  2914. u64 num_dec;
  2915. extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
  2916. if (!del_item) {
  2917. u64 orig_num_bytes =
  2918. btrfs_file_extent_num_bytes(leaf, fi);
  2919. extent_num_bytes = new_size -
  2920. found_key.offset + root->sectorsize - 1;
  2921. extent_num_bytes = extent_num_bytes &
  2922. ~((u64)root->sectorsize - 1);
  2923. btrfs_set_file_extent_num_bytes(leaf, fi,
  2924. extent_num_bytes);
  2925. num_dec = (orig_num_bytes -
  2926. extent_num_bytes);
  2927. if (root->ref_cows && extent_start != 0)
  2928. inode_sub_bytes(inode, num_dec);
  2929. btrfs_mark_buffer_dirty(leaf);
  2930. } else {
  2931. extent_num_bytes =
  2932. btrfs_file_extent_disk_num_bytes(leaf,
  2933. fi);
  2934. extent_offset = found_key.offset -
  2935. btrfs_file_extent_offset(leaf, fi);
  2936. /* FIXME blocksize != 4096 */
  2937. num_dec = btrfs_file_extent_num_bytes(leaf, fi);
  2938. if (extent_start != 0) {
  2939. found_extent = 1;
  2940. if (root->ref_cows)
  2941. inode_sub_bytes(inode, num_dec);
  2942. }
  2943. }
  2944. } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
  2945. /*
  2946. * we can't truncate inline items that have had
  2947. * special encodings
  2948. */
  2949. if (!del_item &&
  2950. btrfs_file_extent_compression(leaf, fi) == 0 &&
  2951. btrfs_file_extent_encryption(leaf, fi) == 0 &&
  2952. btrfs_file_extent_other_encoding(leaf, fi) == 0) {
  2953. u32 size = new_size - found_key.offset;
  2954. if (root->ref_cows) {
  2955. inode_sub_bytes(inode, item_end + 1 -
  2956. new_size);
  2957. }
  2958. size =
  2959. btrfs_file_extent_calc_inline_size(size);
  2960. btrfs_truncate_item(trans, root, path,
  2961. size, 1);
  2962. } else if (root->ref_cows) {
  2963. inode_sub_bytes(inode, item_end + 1 -
  2964. found_key.offset);
  2965. }
  2966. }
  2967. delete:
  2968. if (del_item) {
  2969. if (!pending_del_nr) {
  2970. /* no pending yet, add ourselves */
  2971. pending_del_slot = path->slots[0];
  2972. pending_del_nr = 1;
  2973. } else if (pending_del_nr &&
  2974. path->slots[0] + 1 == pending_del_slot) {
  2975. /* hop on the pending chunk */
  2976. pending_del_nr++;
  2977. pending_del_slot = path->slots[0];
  2978. } else {
  2979. BUG();
  2980. }
  2981. } else {
  2982. break;
  2983. }
  2984. if (found_extent && (root->ref_cows ||
  2985. root == root->fs_info->tree_root)) {
  2986. btrfs_set_path_blocking(path);
  2987. ret = btrfs_free_extent(trans, root, extent_start,
  2988. extent_num_bytes, 0,
  2989. btrfs_header_owner(leaf),
  2990. ino, extent_offset, 0);
  2991. BUG_ON(ret);
  2992. }
  2993. if (found_type == BTRFS_INODE_ITEM_KEY)
  2994. break;
  2995. if (path->slots[0] == 0 ||
  2996. path->slots[0] != pending_del_slot) {
  2997. if (root->ref_cows &&
  2998. BTRFS_I(inode)->location.objectid !=
  2999. BTRFS_FREE_INO_OBJECTID) {
  3000. err = -EAGAIN;
  3001. goto out;
  3002. }
  3003. if (pending_del_nr) {
  3004. ret = btrfs_del_items(trans, root, path,
  3005. pending_del_slot,
  3006. pending_del_nr);
  3007. if (ret) {
  3008. btrfs_abort_transaction(trans,
  3009. root, ret);
  3010. goto error;
  3011. }
  3012. pending_del_nr = 0;
  3013. }
  3014. btrfs_release_path(path);
  3015. goto search_again;
  3016. } else {
  3017. path->slots[0]--;
  3018. }
  3019. }
  3020. out:
  3021. if (pending_del_nr) {
  3022. ret = btrfs_del_items(trans, root, path, pending_del_slot,
  3023. pending_del_nr);
  3024. if (ret)
  3025. btrfs_abort_transaction(trans, root, ret);
  3026. }
  3027. error:
  3028. btrfs_free_path(path);
  3029. return err;
  3030. }
  3031. /*
  3032. * taken from block_truncate_page, but does cow as it zeros out
  3033. * any bytes left in the last page in the file.
  3034. */
  3035. static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
  3036. {
  3037. struct inode *inode = mapping->host;
  3038. struct btrfs_root *root = BTRFS_I(inode)->root;
  3039. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  3040. struct btrfs_ordered_extent *ordered;
  3041. struct extent_state *cached_state = NULL;
  3042. char *kaddr;
  3043. u32 blocksize = root->sectorsize;
  3044. pgoff_t index = from >> PAGE_CACHE_SHIFT;
  3045. unsigned offset = from & (PAGE_CACHE_SIZE-1);
  3046. struct page *page;
  3047. gfp_t mask = btrfs_alloc_write_mask(mapping);
  3048. int ret = 0;
  3049. u64 page_start;
  3050. u64 page_end;
  3051. if ((offset & (blocksize - 1)) == 0)
  3052. goto out;
  3053. ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
  3054. if (ret)
  3055. goto out;
  3056. ret = -ENOMEM;
  3057. again:
  3058. page = find_or_create_page(mapping, index, mask);
  3059. if (!page) {
  3060. btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
  3061. goto out;
  3062. }
  3063. page_start = page_offset(page);
  3064. page_end = page_start + PAGE_CACHE_SIZE - 1;
  3065. if (!PageUptodate(page)) {
  3066. ret = btrfs_readpage(NULL, page);
  3067. lock_page(page);
  3068. if (page->mapping != mapping) {
  3069. unlock_page(page);
  3070. page_cache_release(page);
  3071. goto again;
  3072. }
  3073. if (!PageUptodate(page)) {
  3074. ret = -EIO;
  3075. goto out_unlock;
  3076. }
  3077. }
  3078. wait_on_page_writeback(page);
  3079. lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
  3080. set_page_extent_mapped(page);
  3081. ordered = btrfs_lookup_ordered_extent(inode, page_start);
  3082. if (ordered) {
  3083. unlock_extent_cached(io_tree, page_start, page_end,
  3084. &cached_state, GFP_NOFS);
  3085. unlock_page(page);
  3086. page_cache_release(page);
  3087. btrfs_start_ordered_extent(inode, ordered, 1);
  3088. btrfs_put_ordered_extent(ordered);
  3089. goto again;
  3090. }
  3091. clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
  3092. EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING,
  3093. 0, 0, &cached_state, GFP_NOFS);
  3094. ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
  3095. &cached_state);
  3096. if (ret) {
  3097. unlock_extent_cached(io_tree, page_start, page_end,
  3098. &cached_state, GFP_NOFS);
  3099. goto out_unlock;
  3100. }
  3101. ret = 0;
  3102. if (offset != PAGE_CACHE_SIZE) {
  3103. kaddr = kmap(page);
  3104. memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
  3105. flush_dcache_page(page);
  3106. kunmap(page);
  3107. }
  3108. ClearPageChecked(page);
  3109. set_page_dirty(page);
  3110. unlock_extent_cached(io_tree, page_start, page_end, &cached_state,
  3111. GFP_NOFS);
  3112. out_unlock:
  3113. if (ret)
  3114. btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
  3115. unlock_page(page);
  3116. page_cache_release(page);
  3117. out:
  3118. return ret;
  3119. }
  3120. /*
  3121. * This function puts in dummy file extents for the area we're creating a hole
  3122. * for. So if we are truncating this file to a larger size we need to insert
  3123. * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
  3124. * the range between oldsize and size
  3125. */
  3126. int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
  3127. {
  3128. struct btrfs_trans_handle *trans;
  3129. struct btrfs_root *root = BTRFS_I(inode)->root;
  3130. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  3131. struct extent_map *em = NULL;
  3132. struct extent_state *cached_state = NULL;
  3133. u64 mask = root->sectorsize - 1;
  3134. u64 hole_start = (oldsize + mask) & ~mask;
  3135. u64 block_end = (size + mask) & ~mask;
  3136. u64 last_byte;
  3137. u64 cur_offset;
  3138. u64 hole_size;
  3139. int err = 0;
  3140. if (size <= hole_start)
  3141. return 0;
  3142. while (1) {
  3143. struct btrfs_ordered_extent *ordered;
  3144. btrfs_wait_ordered_range(inode, hole_start,
  3145. block_end - hole_start);
  3146. lock_extent_bits(io_tree, hole_start, block_end - 1, 0,
  3147. &cached_state);
  3148. ordered = btrfs_lookup_ordered_extent(inode, hole_start);
  3149. if (!ordered)
  3150. break;
  3151. unlock_extent_cached(io_tree, hole_start, block_end - 1,
  3152. &cached_state, GFP_NOFS);
  3153. btrfs_put_ordered_extent(ordered);
  3154. }
  3155. cur_offset = hole_start;
  3156. while (1) {
  3157. em = btrfs_get_extent(inode, NULL, 0, cur_offset,
  3158. block_end - cur_offset, 0);
  3159. if (IS_ERR(em)) {
  3160. err = PTR_ERR(em);
  3161. break;
  3162. }
  3163. last_byte = min(extent_map_end(em), block_end);
  3164. last_byte = (last_byte + mask) & ~mask;
  3165. if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
  3166. u64 hint_byte = 0;
  3167. hole_size = last_byte - cur_offset;
  3168. trans = btrfs_start_transaction(root, 3);
  3169. if (IS_ERR(trans)) {
  3170. err = PTR_ERR(trans);
  3171. break;
  3172. }
  3173. err = btrfs_drop_extents(trans, inode, cur_offset,
  3174. cur_offset + hole_size,
  3175. &hint_byte, 1);
  3176. if (err) {
  3177. btrfs_abort_transaction(trans, root, err);
  3178. btrfs_end_transaction(trans, root);
  3179. break;
  3180. }
  3181. err = btrfs_insert_file_extent(trans, root,
  3182. btrfs_ino(inode), cur_offset, 0,
  3183. 0, hole_size, 0, hole_size,
  3184. 0, 0, 0);
  3185. if (err) {
  3186. btrfs_abort_transaction(trans, root, err);
  3187. btrfs_end_transaction(trans, root);
  3188. break;
  3189. }
  3190. btrfs_drop_extent_cache(inode, hole_start,
  3191. last_byte - 1, 0);
  3192. btrfs_update_inode(trans, root, inode);
  3193. btrfs_end_transaction(trans, root);
  3194. }
  3195. free_extent_map(em);
  3196. em = NULL;
  3197. cur_offset = last_byte;
  3198. if (cur_offset >= block_end)
  3199. break;
  3200. }
  3201. free_extent_map(em);
  3202. unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
  3203. GFP_NOFS);
  3204. return err;
  3205. }
  3206. static int btrfs_setsize(struct inode *inode, loff_t newsize)
  3207. {
  3208. struct btrfs_root *root = BTRFS_I(inode)->root;
  3209. struct btrfs_trans_handle *trans;
  3210. loff_t oldsize = i_size_read(inode);
  3211. int ret;
  3212. if (newsize == oldsize)
  3213. return 0;
  3214. if (newsize > oldsize) {
  3215. truncate_pagecache(inode, oldsize, newsize);
  3216. ret = btrfs_cont_expand(inode, oldsize, newsize);
  3217. if (ret)
  3218. return ret;
  3219. trans = btrfs_start_transaction(root, 1);
  3220. if (IS_ERR(trans))
  3221. return PTR_ERR(trans);
  3222. i_size_write(inode, newsize);
  3223. btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
  3224. ret = btrfs_update_inode(trans, root, inode);
  3225. btrfs_end_transaction(trans, root);
  3226. } else {
  3227. /*
  3228. * We're truncating a file that used to have good data down to
  3229. * zero. Make sure it gets into the ordered flush list so that
  3230. * any new writes get down to disk quickly.
  3231. */
  3232. if (newsize == 0)
  3233. BTRFS_I(inode)->ordered_data_close = 1;
  3234. /* we don't support swapfiles, so vmtruncate shouldn't fail */
  3235. truncate_setsize(inode, newsize);
  3236. ret = btrfs_truncate(inode);
  3237. }
  3238. return ret;
  3239. }
  3240. static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
  3241. {
  3242. struct inode *inode = dentry->d_inode;
  3243. struct btrfs_root *root = BTRFS_I(inode)->root;
  3244. int err;
  3245. if (btrfs_root_readonly(root))
  3246. return -EROFS;
  3247. err = inode_change_ok(inode, attr);
  3248. if (err)
  3249. return err;
  3250. if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
  3251. err = btrfs_setsize(inode, attr->ia_size);
  3252. if (err)
  3253. return err;
  3254. }
  3255. if (attr->ia_valid) {
  3256. setattr_copy(inode, attr);
  3257. inode_inc_iversion(inode);
  3258. err = btrfs_dirty_inode(inode);
  3259. if (!err && attr->ia_valid & ATTR_MODE)
  3260. err = btrfs_acl_chmod(inode);
  3261. }
  3262. return err;
  3263. }
  3264. void btrfs_evict_inode(struct inode *inode)
  3265. {
  3266. struct btrfs_trans_handle *trans;
  3267. struct btrfs_root *root = BTRFS_I(inode)->root;
  3268. struct btrfs_block_rsv *rsv, *global_rsv;
  3269. u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
  3270. unsigned long nr;
  3271. int ret;
  3272. trace_btrfs_inode_evict(inode);
  3273. truncate_inode_pages(&inode->i_data, 0);
  3274. if (inode->i_nlink && (btrfs_root_refs(&root->root_item) != 0 ||
  3275. btrfs_is_free_space_inode(root, inode)))
  3276. goto no_delete;
  3277. if (is_bad_inode(inode)) {
  3278. btrfs_orphan_del(NULL, inode);
  3279. goto no_delete;
  3280. }
  3281. /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
  3282. btrfs_wait_ordered_range(inode, 0, (u64)-1);
  3283. if (root->fs_info->log_root_recovering) {
  3284. BUG_ON(!list_empty(&BTRFS_I(inode)->i_orphan));
  3285. goto no_delete;
  3286. }
  3287. if (inode->i_nlink > 0) {
  3288. BUG_ON(btrfs_root_refs(&root->root_item) != 0);
  3289. goto no_delete;
  3290. }
  3291. rsv = btrfs_alloc_block_rsv(root);
  3292. if (!rsv) {
  3293. btrfs_orphan_del(NULL, inode);
  3294. goto no_delete;
  3295. }
  3296. rsv->size = min_size;
  3297. global_rsv = &root->fs_info->global_block_rsv;
  3298. btrfs_i_size_write(inode, 0);
  3299. /*
  3300. * This is a bit simpler than btrfs_truncate since
  3301. *
  3302. * 1) We've already reserved our space for our orphan item in the
  3303. * unlink.
  3304. * 2) We're going to delete the inode item, so we don't need to update
  3305. * it at all.
  3306. *
  3307. * So we just need to reserve some slack space in case we add bytes when
  3308. * doing the truncate.
  3309. */
  3310. while (1) {
  3311. ret = btrfs_block_rsv_refill_noflush(root, rsv, min_size);
  3312. /*
  3313. * Try and steal from the global reserve since we will
  3314. * likely not use this space anyway, we want to try as
  3315. * hard as possible to get this to work.
  3316. */
  3317. if (ret)
  3318. ret = btrfs_block_rsv_migrate(global_rsv, rsv, min_size);
  3319. if (ret) {
  3320. printk(KERN_WARNING "Could not get space for a "
  3321. "delete, will truncate on mount %d\n", ret);
  3322. btrfs_orphan_del(NULL, inode);
  3323. btrfs_free_block_rsv(root, rsv);
  3324. goto no_delete;
  3325. }
  3326. trans = btrfs_start_transaction(root, 0);
  3327. if (IS_ERR(trans)) {
  3328. btrfs_orphan_del(NULL, inode);
  3329. btrfs_free_block_rsv(root, rsv);
  3330. goto no_delete;
  3331. }
  3332. trans->block_rsv = rsv;
  3333. ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
  3334. if (ret != -EAGAIN)
  3335. break;
  3336. nr = trans->blocks_used;
  3337. btrfs_end_transaction(trans, root);
  3338. trans = NULL;
  3339. btrfs_btree_balance_dirty(root, nr);
  3340. }
  3341. btrfs_free_block_rsv(root, rsv);
  3342. if (ret == 0) {
  3343. trans->block_rsv = root->orphan_block_rsv;
  3344. ret = btrfs_orphan_del(trans, inode);
  3345. BUG_ON(ret);
  3346. }
  3347. trans->block_rsv = &root->fs_info->trans_block_rsv;
  3348. if (!(root == root->fs_info->tree_root ||
  3349. root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
  3350. btrfs_return_ino(root, btrfs_ino(inode));
  3351. nr = trans->blocks_used;
  3352. btrfs_end_transaction(trans, root);
  3353. btrfs_btree_balance_dirty(root, nr);
  3354. no_delete:
  3355. end_writeback(inode);
  3356. return;
  3357. }
  3358. /*
  3359. * this returns the key found in the dir entry in the location pointer.
  3360. * If no dir entries were found, location->objectid is 0.
  3361. */
  3362. static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
  3363. struct btrfs_key *location)
  3364. {
  3365. const char *name = dentry->d_name.name;
  3366. int namelen = dentry->d_name.len;
  3367. struct btrfs_dir_item *di;
  3368. struct btrfs_path *path;
  3369. struct btrfs_root *root = BTRFS_I(dir)->root;
  3370. int ret = 0;
  3371. path = btrfs_alloc_path();
  3372. if (!path)
  3373. return -ENOMEM;
  3374. di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name,
  3375. namelen, 0);
  3376. if (IS_ERR(di))
  3377. ret = PTR_ERR(di);
  3378. if (IS_ERR_OR_NULL(di))
  3379. goto out_err;
  3380. btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
  3381. out:
  3382. btrfs_free_path(path);
  3383. return ret;
  3384. out_err:
  3385. location->objectid = 0;
  3386. goto out;
  3387. }
  3388. /*
  3389. * when we hit a tree root in a directory, the btrfs part of the inode
  3390. * needs to be changed to reflect the root directory of the tree root. This
  3391. * is kind of like crossing a mount point.
  3392. */
  3393. static int fixup_tree_root_location(struct btrfs_root *root,
  3394. struct inode *dir,
  3395. struct dentry *dentry,
  3396. struct btrfs_key *location,
  3397. struct btrfs_root **sub_root)
  3398. {
  3399. struct btrfs_path *path;
  3400. struct btrfs_root *new_root;
  3401. struct btrfs_root_ref *ref;
  3402. struct extent_buffer *leaf;
  3403. int ret;
  3404. int err = 0;
  3405. path = btrfs_alloc_path();
  3406. if (!path) {
  3407. err = -ENOMEM;
  3408. goto out;
  3409. }
  3410. err = -ENOENT;
  3411. ret = btrfs_find_root_ref(root->fs_info->tree_root, path,
  3412. BTRFS_I(dir)->root->root_key.objectid,
  3413. location->objectid);
  3414. if (ret) {
  3415. if (ret < 0)
  3416. err = ret;
  3417. goto out;
  3418. }
  3419. leaf = path->nodes[0];
  3420. ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
  3421. if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) ||
  3422. btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
  3423. goto out;
  3424. ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
  3425. (unsigned long)(ref + 1),
  3426. dentry->d_name.len);
  3427. if (ret)
  3428. goto out;
  3429. btrfs_release_path(path);
  3430. new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
  3431. if (IS_ERR(new_root)) {
  3432. err = PTR_ERR(new_root);
  3433. goto out;
  3434. }
  3435. if (btrfs_root_refs(&new_root->root_item) == 0) {
  3436. err = -ENOENT;
  3437. goto out;
  3438. }
  3439. *sub_root = new_root;
  3440. location->objectid = btrfs_root_dirid(&new_root->root_item);
  3441. location->type = BTRFS_INODE_ITEM_KEY;
  3442. location->offset = 0;
  3443. err = 0;
  3444. out:
  3445. btrfs_free_path(path);
  3446. return err;
  3447. }
  3448. static void inode_tree_add(struct inode *inode)
  3449. {
  3450. struct btrfs_root *root = BTRFS_I(inode)->root;
  3451. struct btrfs_inode *entry;
  3452. struct rb_node **p;
  3453. struct rb_node *parent;
  3454. u64 ino = btrfs_ino(inode);
  3455. again:
  3456. p = &root->inode_tree.rb_node;
  3457. parent = NULL;
  3458. if (inode_unhashed(inode))
  3459. return;
  3460. spin_lock(&root->inode_lock);
  3461. while (*p) {
  3462. parent = *p;
  3463. entry = rb_entry(parent, struct btrfs_inode, rb_node);
  3464. if (ino < btrfs_ino(&entry->vfs_inode))
  3465. p = &parent->rb_left;
  3466. else if (ino > btrfs_ino(&entry->vfs_inode))
  3467. p = &parent->rb_right;
  3468. else {
  3469. WARN_ON(!(entry->vfs_inode.i_state &
  3470. (I_WILL_FREE | I_FREEING)));
  3471. rb_erase(parent, &root->inode_tree);
  3472. RB_CLEAR_NODE(parent);
  3473. spin_unlock(&root->inode_lock);
  3474. goto again;
  3475. }
  3476. }
  3477. rb_link_node(&BTRFS_I(inode)->rb_node, parent, p);
  3478. rb_insert_color(&BTRFS_I(inode)->rb_node, &root->inode_tree);
  3479. spin_unlock(&root->inode_lock);
  3480. }
  3481. static void inode_tree_del(struct inode *inode)
  3482. {
  3483. struct btrfs_root *root = BTRFS_I(inode)->root;
  3484. int empty = 0;
  3485. spin_lock(&root->inode_lock);
  3486. if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
  3487. rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
  3488. RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
  3489. empty = RB_EMPTY_ROOT(&root->inode_tree);
  3490. }
  3491. spin_unlock(&root->inode_lock);
  3492. /*
  3493. * Free space cache has inodes in the tree root, but the tree root has a
  3494. * root_refs of 0, so this could end up dropping the tree root as a
  3495. * snapshot, so we need the extra !root->fs_info->tree_root check to
  3496. * make sure we don't drop it.
  3497. */
  3498. if (empty && btrfs_root_refs(&root->root_item) == 0 &&
  3499. root != root->fs_info->tree_root) {
  3500. synchronize_srcu(&root->fs_info->subvol_srcu);
  3501. spin_lock(&root->inode_lock);
  3502. empty = RB_EMPTY_ROOT(&root->inode_tree);
  3503. spin_unlock(&root->inode_lock);
  3504. if (empty)
  3505. btrfs_add_dead_root(root);
  3506. }
  3507. }
  3508. void btrfs_invalidate_inodes(struct btrfs_root *root)
  3509. {
  3510. struct rb_node *node;
  3511. struct rb_node *prev;
  3512. struct btrfs_inode *entry;
  3513. struct inode *inode;
  3514. u64 objectid = 0;
  3515. WARN_ON(btrfs_root_refs(&root->root_item) != 0);
  3516. spin_lock(&root->inode_lock);
  3517. again:
  3518. node = root->inode_tree.rb_node;
  3519. prev = NULL;
  3520. while (node) {
  3521. prev = node;
  3522. entry = rb_entry(node, struct btrfs_inode, rb_node);
  3523. if (objectid < btrfs_ino(&entry->vfs_inode))
  3524. node = node->rb_left;
  3525. else if (objectid > btrfs_ino(&entry->vfs_inode))
  3526. node = node->rb_right;
  3527. else
  3528. break;
  3529. }
  3530. if (!node) {
  3531. while (prev) {
  3532. entry = rb_entry(prev, struct btrfs_inode, rb_node);
  3533. if (objectid <= btrfs_ino(&entry->vfs_inode)) {
  3534. node = prev;
  3535. break;
  3536. }
  3537. prev = rb_next(prev);
  3538. }
  3539. }
  3540. while (node) {
  3541. entry = rb_entry(node, struct btrfs_inode, rb_node);
  3542. objectid = btrfs_ino(&entry->vfs_inode) + 1;
  3543. inode = igrab(&entry->vfs_inode);
  3544. if (inode) {
  3545. spin_unlock(&root->inode_lock);
  3546. if (atomic_read(&inode->i_count) > 1)
  3547. d_prune_aliases(inode);
  3548. /*
  3549. * btrfs_drop_inode will have it removed from
  3550. * the inode cache when its usage count
  3551. * hits zero.
  3552. */
  3553. iput(inode);
  3554. cond_resched();
  3555. spin_lock(&root->inode_lock);
  3556. goto again;
  3557. }
  3558. if (cond_resched_lock(&root->inode_lock))
  3559. goto again;
  3560. node = rb_next(node);
  3561. }
  3562. spin_unlock(&root->inode_lock);
  3563. }
  3564. static int btrfs_init_locked_inode(struct inode *inode, void *p)
  3565. {
  3566. struct btrfs_iget_args *args = p;
  3567. inode->i_ino = args->ino;
  3568. BTRFS_I(inode)->root = args->root;
  3569. btrfs_set_inode_space_info(args->root, inode);
  3570. return 0;
  3571. }
  3572. static int btrfs_find_actor(struct inode *inode, void *opaque)
  3573. {
  3574. struct btrfs_iget_args *args = opaque;
  3575. return args->ino == btrfs_ino(inode) &&
  3576. args->root == BTRFS_I(inode)->root;
  3577. }
  3578. static struct inode *btrfs_iget_locked(struct super_block *s,
  3579. u64 objectid,
  3580. struct btrfs_root *root)
  3581. {
  3582. struct inode *inode;
  3583. struct btrfs_iget_args args;
  3584. args.ino = objectid;
  3585. args.root = root;
  3586. inode = iget5_locked(s, objectid, btrfs_find_actor,
  3587. btrfs_init_locked_inode,
  3588. (void *)&args);
  3589. return inode;
  3590. }
  3591. /* Get an inode object given its location and corresponding root.
  3592. * Returns in *is_new if the inode was read from disk
  3593. */
  3594. struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
  3595. struct btrfs_root *root, int *new)
  3596. {
  3597. struct inode *inode;
  3598. inode = btrfs_iget_locked(s, location->objectid, root);
  3599. if (!inode)
  3600. return ERR_PTR(-ENOMEM);
  3601. if (inode->i_state & I_NEW) {
  3602. BTRFS_I(inode)->root = root;
  3603. memcpy(&BTRFS_I(inode)->location, location, sizeof(*location));
  3604. btrfs_read_locked_inode(inode);
  3605. if (!is_bad_inode(inode)) {
  3606. inode_tree_add(inode);
  3607. unlock_new_inode(inode);
  3608. if (new)
  3609. *new = 1;
  3610. } else {
  3611. unlock_new_inode(inode);
  3612. iput(inode);
  3613. inode = ERR_PTR(-ESTALE);
  3614. }
  3615. }
  3616. return inode;
  3617. }
  3618. static struct inode *new_simple_dir(struct super_block *s,
  3619. struct btrfs_key *key,
  3620. struct btrfs_root *root)
  3621. {
  3622. struct inode *inode = new_inode(s);
  3623. if (!inode)
  3624. return ERR_PTR(-ENOMEM);
  3625. BTRFS_I(inode)->root = root;
  3626. memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
  3627. BTRFS_I(inode)->dummy_inode = 1;
  3628. inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
  3629. inode->i_op = &btrfs_dir_ro_inode_operations;
  3630. inode->i_fop = &simple_dir_operations;
  3631. inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
  3632. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
  3633. return inode;
  3634. }
  3635. struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
  3636. {
  3637. struct inode *inode;
  3638. struct btrfs_root *root = BTRFS_I(dir)->root;
  3639. struct btrfs_root *sub_root = root;
  3640. struct btrfs_key location;
  3641. int index;
  3642. int ret = 0;
  3643. if (dentry->d_name.len > BTRFS_NAME_LEN)
  3644. return ERR_PTR(-ENAMETOOLONG);
  3645. if (unlikely(d_need_lookup(dentry))) {
  3646. memcpy(&location, dentry->d_fsdata, sizeof(struct btrfs_key));
  3647. kfree(dentry->d_fsdata);
  3648. dentry->d_fsdata = NULL;
  3649. /* This thing is hashed, drop it for now */
  3650. d_drop(dentry);
  3651. } else {
  3652. ret = btrfs_inode_by_name(dir, dentry, &location);
  3653. }
  3654. if (ret < 0)
  3655. return ERR_PTR(ret);
  3656. if (location.objectid == 0)
  3657. return NULL;
  3658. if (location.type == BTRFS_INODE_ITEM_KEY) {
  3659. inode = btrfs_iget(dir->i_sb, &location, root, NULL);
  3660. return inode;
  3661. }
  3662. BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
  3663. index = srcu_read_lock(&root->fs_info->subvol_srcu);
  3664. ret = fixup_tree_root_location(root, dir, dentry,
  3665. &location, &sub_root);
  3666. if (ret < 0) {
  3667. if (ret != -ENOENT)
  3668. inode = ERR_PTR(ret);
  3669. else
  3670. inode = new_simple_dir(dir->i_sb, &location, sub_root);
  3671. } else {
  3672. inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
  3673. }
  3674. srcu_read_unlock(&root->fs_info->subvol_srcu, index);
  3675. if (!IS_ERR(inode) && root != sub_root) {
  3676. down_read(&root->fs_info->cleanup_work_sem);
  3677. if (!(inode->i_sb->s_flags & MS_RDONLY))
  3678. ret = btrfs_orphan_cleanup(sub_root);
  3679. up_read(&root->fs_info->cleanup_work_sem);
  3680. if (ret)
  3681. inode = ERR_PTR(ret);
  3682. }
  3683. return inode;
  3684. }
  3685. static int btrfs_dentry_delete(const struct dentry *dentry)
  3686. {
  3687. struct btrfs_root *root;
  3688. struct inode *inode = dentry->d_inode;
  3689. if (!inode && !IS_ROOT(dentry))
  3690. inode = dentry->d_parent->d_inode;
  3691. if (inode) {
  3692. root = BTRFS_I(inode)->root;
  3693. if (btrfs_root_refs(&root->root_item) == 0)
  3694. return 1;
  3695. if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
  3696. return 1;
  3697. }
  3698. return 0;
  3699. }
  3700. static void btrfs_dentry_release(struct dentry *dentry)
  3701. {
  3702. if (dentry->d_fsdata)
  3703. kfree(dentry->d_fsdata);
  3704. }
  3705. static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
  3706. struct nameidata *nd)
  3707. {
  3708. struct dentry *ret;
  3709. ret = d_splice_alias(btrfs_lookup_dentry(dir, dentry), dentry);
  3710. if (unlikely(d_need_lookup(dentry))) {
  3711. spin_lock(&dentry->d_lock);
  3712. dentry->d_flags &= ~DCACHE_NEED_LOOKUP;
  3713. spin_unlock(&dentry->d_lock);
  3714. }
  3715. return ret;
  3716. }
  3717. unsigned char btrfs_filetype_table[] = {
  3718. DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
  3719. };
  3720. static int btrfs_real_readdir(struct file *filp, void *dirent,
  3721. filldir_t filldir)
  3722. {
  3723. struct inode *inode = filp->f_dentry->d_inode;
  3724. struct btrfs_root *root = BTRFS_I(inode)->root;
  3725. struct btrfs_item *item;
  3726. struct btrfs_dir_item *di;
  3727. struct btrfs_key key;
  3728. struct btrfs_key found_key;
  3729. struct btrfs_path *path;
  3730. struct list_head ins_list;
  3731. struct list_head del_list;
  3732. int ret;
  3733. struct extent_buffer *leaf;
  3734. int slot;
  3735. unsigned char d_type;
  3736. int over = 0;
  3737. u32 di_cur;
  3738. u32 di_total;
  3739. u32 di_len;
  3740. int key_type = BTRFS_DIR_INDEX_KEY;
  3741. char tmp_name[32];
  3742. char *name_ptr;
  3743. int name_len;
  3744. int is_curr = 0; /* filp->f_pos points to the current index? */
  3745. /* FIXME, use a real flag for deciding about the key type */
  3746. if (root->fs_info->tree_root == root)
  3747. key_type = BTRFS_DIR_ITEM_KEY;
  3748. /* special case for "." */
  3749. if (filp->f_pos == 0) {
  3750. over = filldir(dirent, ".", 1,
  3751. filp->f_pos, btrfs_ino(inode), DT_DIR);
  3752. if (over)
  3753. return 0;
  3754. filp->f_pos = 1;
  3755. }
  3756. /* special case for .., just use the back ref */
  3757. if (filp->f_pos == 1) {
  3758. u64 pino = parent_ino(filp->f_path.dentry);
  3759. over = filldir(dirent, "..", 2,
  3760. filp->f_pos, pino, DT_DIR);
  3761. if (over)
  3762. return 0;
  3763. filp->f_pos = 2;
  3764. }
  3765. path = btrfs_alloc_path();
  3766. if (!path)
  3767. return -ENOMEM;
  3768. path->reada = 1;
  3769. if (key_type == BTRFS_DIR_INDEX_KEY) {
  3770. INIT_LIST_HEAD(&ins_list);
  3771. INIT_LIST_HEAD(&del_list);
  3772. btrfs_get_delayed_items(inode, &ins_list, &del_list);
  3773. }
  3774. btrfs_set_key_type(&key, key_type);
  3775. key.offset = filp->f_pos;
  3776. key.objectid = btrfs_ino(inode);
  3777. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  3778. if (ret < 0)
  3779. goto err;
  3780. while (1) {
  3781. leaf = path->nodes[0];
  3782. slot = path->slots[0];
  3783. if (slot >= btrfs_header_nritems(leaf)) {
  3784. ret = btrfs_next_leaf(root, path);
  3785. if (ret < 0)
  3786. goto err;
  3787. else if (ret > 0)
  3788. break;
  3789. continue;
  3790. }
  3791. item = btrfs_item_nr(leaf, slot);
  3792. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  3793. if (found_key.objectid != key.objectid)
  3794. break;
  3795. if (btrfs_key_type(&found_key) != key_type)
  3796. break;
  3797. if (found_key.offset < filp->f_pos)
  3798. goto next;
  3799. if (key_type == BTRFS_DIR_INDEX_KEY &&
  3800. btrfs_should_delete_dir_index(&del_list,
  3801. found_key.offset))
  3802. goto next;
  3803. filp->f_pos = found_key.offset;
  3804. is_curr = 1;
  3805. di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
  3806. di_cur = 0;
  3807. di_total = btrfs_item_size(leaf, item);
  3808. while (di_cur < di_total) {
  3809. struct btrfs_key location;
  3810. if (verify_dir_item(root, leaf, di))
  3811. break;
  3812. name_len = btrfs_dir_name_len(leaf, di);
  3813. if (name_len <= sizeof(tmp_name)) {
  3814. name_ptr = tmp_name;
  3815. } else {
  3816. name_ptr = kmalloc(name_len, GFP_NOFS);
  3817. if (!name_ptr) {
  3818. ret = -ENOMEM;
  3819. goto err;
  3820. }
  3821. }
  3822. read_extent_buffer(leaf, name_ptr,
  3823. (unsigned long)(di + 1), name_len);
  3824. d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
  3825. btrfs_dir_item_key_to_cpu(leaf, di, &location);
  3826. /* is this a reference to our own snapshot? If so
  3827. * skip it.
  3828. *
  3829. * In contrast to old kernels, we insert the snapshot's
  3830. * dir item and dir index after it has been created, so
  3831. * we won't find a reference to our own snapshot. We
  3832. * still keep the following code for backward
  3833. * compatibility.
  3834. */
  3835. if (location.type == BTRFS_ROOT_ITEM_KEY &&
  3836. location.objectid == root->root_key.objectid) {
  3837. over = 0;
  3838. goto skip;
  3839. }
  3840. over = filldir(dirent, name_ptr, name_len,
  3841. found_key.offset, location.objectid,
  3842. d_type);
  3843. skip:
  3844. if (name_ptr != tmp_name)
  3845. kfree(name_ptr);
  3846. if (over)
  3847. goto nopos;
  3848. di_len = btrfs_dir_name_len(leaf, di) +
  3849. btrfs_dir_data_len(leaf, di) + sizeof(*di);
  3850. di_cur += di_len;
  3851. di = (struct btrfs_dir_item *)((char *)di + di_len);
  3852. }
  3853. next:
  3854. path->slots[0]++;
  3855. }
  3856. if (key_type == BTRFS_DIR_INDEX_KEY) {
  3857. if (is_curr)
  3858. filp->f_pos++;
  3859. ret = btrfs_readdir_delayed_dir_index(filp, dirent, filldir,
  3860. &ins_list);
  3861. if (ret)
  3862. goto nopos;
  3863. }
  3864. /* Reached end of directory/root. Bump pos past the last item. */
  3865. if (key_type == BTRFS_DIR_INDEX_KEY)
  3866. /*
  3867. * 32-bit glibc will use getdents64, but then strtol -
  3868. * so the last number we can serve is this.
  3869. */
  3870. filp->f_pos = 0x7fffffff;
  3871. else
  3872. filp->f_pos++;
  3873. nopos:
  3874. ret = 0;
  3875. err:
  3876. if (key_type == BTRFS_DIR_INDEX_KEY)
  3877. btrfs_put_delayed_items(&ins_list, &del_list);
  3878. btrfs_free_path(path);
  3879. return ret;
  3880. }
  3881. int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
  3882. {
  3883. struct btrfs_root *root = BTRFS_I(inode)->root;
  3884. struct btrfs_trans_handle *trans;
  3885. int ret = 0;
  3886. bool nolock = false;
  3887. if (BTRFS_I(inode)->dummy_inode)
  3888. return 0;
  3889. if (btrfs_fs_closing(root->fs_info) && btrfs_is_free_space_inode(root, inode))
  3890. nolock = true;
  3891. if (wbc->sync_mode == WB_SYNC_ALL) {
  3892. if (nolock)
  3893. trans = btrfs_join_transaction_nolock(root);
  3894. else
  3895. trans = btrfs_join_transaction(root);
  3896. if (IS_ERR(trans))
  3897. return PTR_ERR(trans);
  3898. if (nolock)
  3899. ret = btrfs_end_transaction_nolock(trans, root);
  3900. else
  3901. ret = btrfs_commit_transaction(trans, root);
  3902. }
  3903. return ret;
  3904. }
  3905. /*
  3906. * This is somewhat expensive, updating the tree every time the
  3907. * inode changes. But, it is most likely to find the inode in cache.
  3908. * FIXME, needs more benchmarking...there are no reasons other than performance
  3909. * to keep or drop this code.
  3910. */
  3911. int btrfs_dirty_inode(struct inode *inode)
  3912. {
  3913. struct btrfs_root *root = BTRFS_I(inode)->root;
  3914. struct btrfs_trans_handle *trans;
  3915. int ret;
  3916. if (BTRFS_I(inode)->dummy_inode)
  3917. return 0;
  3918. trans = btrfs_join_transaction(root);
  3919. if (IS_ERR(trans))
  3920. return PTR_ERR(trans);
  3921. ret = btrfs_update_inode(trans, root, inode);
  3922. if (ret && ret == -ENOSPC) {
  3923. /* whoops, lets try again with the full transaction */
  3924. btrfs_end_transaction(trans, root);
  3925. trans = btrfs_start_transaction(root, 1);
  3926. if (IS_ERR(trans))
  3927. return PTR_ERR(trans);
  3928. ret = btrfs_update_inode(trans, root, inode);
  3929. }
  3930. btrfs_end_transaction(trans, root);
  3931. if (BTRFS_I(inode)->delayed_node)
  3932. btrfs_balance_delayed_items(root);
  3933. return ret;
  3934. }
  3935. /*
  3936. * This is a copy of file_update_time. We need this so we can return error on
  3937. * ENOSPC for updating the inode in the case of file write and mmap writes.
  3938. */
  3939. int btrfs_update_time(struct file *file)
  3940. {
  3941. struct inode *inode = file->f_path.dentry->d_inode;
  3942. struct timespec now;
  3943. int ret;
  3944. enum { S_MTIME = 1, S_CTIME = 2, S_VERSION = 4 } sync_it = 0;
  3945. /* First try to exhaust all avenues to not sync */
  3946. if (IS_NOCMTIME(inode))
  3947. return 0;
  3948. now = current_fs_time(inode->i_sb);
  3949. if (!timespec_equal(&inode->i_mtime, &now))
  3950. sync_it = S_MTIME;
  3951. if (!timespec_equal(&inode->i_ctime, &now))
  3952. sync_it |= S_CTIME;
  3953. if (IS_I_VERSION(inode))
  3954. sync_it |= S_VERSION;
  3955. if (!sync_it)
  3956. return 0;
  3957. /* Finally allowed to write? Takes lock. */
  3958. if (mnt_want_write_file(file))
  3959. return 0;
  3960. /* Only change inode inside the lock region */
  3961. if (sync_it & S_VERSION)
  3962. inode_inc_iversion(inode);
  3963. if (sync_it & S_CTIME)
  3964. inode->i_ctime = now;
  3965. if (sync_it & S_MTIME)
  3966. inode->i_mtime = now;
  3967. ret = btrfs_dirty_inode(inode);
  3968. if (!ret)
  3969. mark_inode_dirty_sync(inode);
  3970. mnt_drop_write(file->f_path.mnt);
  3971. return ret;
  3972. }
  3973. /*
  3974. * find the highest existing sequence number in a directory
  3975. * and then set the in-memory index_cnt variable to reflect
  3976. * free sequence numbers
  3977. */
  3978. static int btrfs_set_inode_index_count(struct inode *inode)
  3979. {
  3980. struct btrfs_root *root = BTRFS_I(inode)->root;
  3981. struct btrfs_key key, found_key;
  3982. struct btrfs_path *path;
  3983. struct extent_buffer *leaf;
  3984. int ret;
  3985. key.objectid = btrfs_ino(inode);
  3986. btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
  3987. key.offset = (u64)-1;
  3988. path = btrfs_alloc_path();
  3989. if (!path)
  3990. return -ENOMEM;
  3991. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  3992. if (ret < 0)
  3993. goto out;
  3994. /* FIXME: we should be able to handle this */
  3995. if (ret == 0)
  3996. goto out;
  3997. ret = 0;
  3998. /*
  3999. * MAGIC NUMBER EXPLANATION:
  4000. * since we search a directory based on f_pos we have to start at 2
  4001. * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
  4002. * else has to start at 2
  4003. */
  4004. if (path->slots[0] == 0) {
  4005. BTRFS_I(inode)->index_cnt = 2;
  4006. goto out;
  4007. }
  4008. path->slots[0]--;
  4009. leaf = path->nodes[0];
  4010. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  4011. if (found_key.objectid != btrfs_ino(inode) ||
  4012. btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
  4013. BTRFS_I(inode)->index_cnt = 2;
  4014. goto out;
  4015. }
  4016. BTRFS_I(inode)->index_cnt = found_key.offset + 1;
  4017. out:
  4018. btrfs_free_path(path);
  4019. return ret;
  4020. }
  4021. /*
  4022. * helper to find a free sequence number in a given directory. This current
  4023. * code is very simple, later versions will do smarter things in the btree
  4024. */
  4025. int btrfs_set_inode_index(struct inode *dir, u64 *index)
  4026. {
  4027. int ret = 0;
  4028. if (BTRFS_I(dir)->index_cnt == (u64)-1) {
  4029. ret = btrfs_inode_delayed_dir_index_count(dir);
  4030. if (ret) {
  4031. ret = btrfs_set_inode_index_count(dir);
  4032. if (ret)
  4033. return ret;
  4034. }
  4035. }
  4036. *index = BTRFS_I(dir)->index_cnt;
  4037. BTRFS_I(dir)->index_cnt++;
  4038. return ret;
  4039. }
  4040. static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
  4041. struct btrfs_root *root,
  4042. struct inode *dir,
  4043. const char *name, int name_len,
  4044. u64 ref_objectid, u64 objectid,
  4045. umode_t mode, u64 *index)
  4046. {
  4047. struct inode *inode;
  4048. struct btrfs_inode_item *inode_item;
  4049. struct btrfs_key *location;
  4050. struct btrfs_path *path;
  4051. struct btrfs_inode_ref *ref;
  4052. struct btrfs_key key[2];
  4053. u32 sizes[2];
  4054. unsigned long ptr;
  4055. int ret;
  4056. int owner;
  4057. path = btrfs_alloc_path();
  4058. if (!path)
  4059. return ERR_PTR(-ENOMEM);
  4060. inode = new_inode(root->fs_info->sb);
  4061. if (!inode) {
  4062. btrfs_free_path(path);
  4063. return ERR_PTR(-ENOMEM);
  4064. }
  4065. /*
  4066. * we have to initialize this early, so we can reclaim the inode
  4067. * number if we fail afterwards in this function.
  4068. */
  4069. inode->i_ino = objectid;
  4070. if (dir) {
  4071. trace_btrfs_inode_request(dir);
  4072. ret = btrfs_set_inode_index(dir, index);
  4073. if (ret) {
  4074. btrfs_free_path(path);
  4075. iput(inode);
  4076. return ERR_PTR(ret);
  4077. }
  4078. }
  4079. /*
  4080. * index_cnt is ignored for everything but a dir,
  4081. * btrfs_get_inode_index_count has an explanation for the magic
  4082. * number
  4083. */
  4084. BTRFS_I(inode)->index_cnt = 2;
  4085. BTRFS_I(inode)->root = root;
  4086. BTRFS_I(inode)->generation = trans->transid;
  4087. inode->i_generation = BTRFS_I(inode)->generation;
  4088. btrfs_set_inode_space_info(root, inode);
  4089. if (S_ISDIR(mode))
  4090. owner = 0;
  4091. else
  4092. owner = 1;
  4093. key[0].objectid = objectid;
  4094. btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
  4095. key[0].offset = 0;
  4096. key[1].objectid = objectid;
  4097. btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
  4098. key[1].offset = ref_objectid;
  4099. sizes[0] = sizeof(struct btrfs_inode_item);
  4100. sizes[1] = name_len + sizeof(*ref);
  4101. path->leave_spinning = 1;
  4102. ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
  4103. if (ret != 0)
  4104. goto fail;
  4105. inode_init_owner(inode, dir, mode);
  4106. inode_set_bytes(inode, 0);
  4107. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
  4108. inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  4109. struct btrfs_inode_item);
  4110. fill_inode_item(trans, path->nodes[0], inode_item, inode);
  4111. ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
  4112. struct btrfs_inode_ref);
  4113. btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
  4114. btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
  4115. ptr = (unsigned long)(ref + 1);
  4116. write_extent_buffer(path->nodes[0], name, ptr, name_len);
  4117. btrfs_mark_buffer_dirty(path->nodes[0]);
  4118. btrfs_free_path(path);
  4119. location = &BTRFS_I(inode)->location;
  4120. location->objectid = objectid;
  4121. location->offset = 0;
  4122. btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
  4123. btrfs_inherit_iflags(inode, dir);
  4124. if (S_ISREG(mode)) {
  4125. if (btrfs_test_opt(root, NODATASUM))
  4126. BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
  4127. if (btrfs_test_opt(root, NODATACOW) ||
  4128. (BTRFS_I(dir)->flags & BTRFS_INODE_NODATACOW))
  4129. BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
  4130. }
  4131. insert_inode_hash(inode);
  4132. inode_tree_add(inode);
  4133. trace_btrfs_inode_new(inode);
  4134. btrfs_set_inode_last_trans(trans, inode);
  4135. return inode;
  4136. fail:
  4137. if (dir)
  4138. BTRFS_I(dir)->index_cnt--;
  4139. btrfs_free_path(path);
  4140. iput(inode);
  4141. return ERR_PTR(ret);
  4142. }
  4143. static inline u8 btrfs_inode_type(struct inode *inode)
  4144. {
  4145. return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
  4146. }
  4147. /*
  4148. * utility function to add 'inode' into 'parent_inode' with
  4149. * a give name and a given sequence number.
  4150. * if 'add_backref' is true, also insert a backref from the
  4151. * inode to the parent directory.
  4152. */
  4153. int btrfs_add_link(struct btrfs_trans_handle *trans,
  4154. struct inode *parent_inode, struct inode *inode,
  4155. const char *name, int name_len, int add_backref, u64 index)
  4156. {
  4157. int ret = 0;
  4158. struct btrfs_key key;
  4159. struct btrfs_root *root = BTRFS_I(parent_inode)->root;
  4160. u64 ino = btrfs_ino(inode);
  4161. u64 parent_ino = btrfs_ino(parent_inode);
  4162. if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
  4163. memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
  4164. } else {
  4165. key.objectid = ino;
  4166. btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
  4167. key.offset = 0;
  4168. }
  4169. if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
  4170. ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
  4171. key.objectid, root->root_key.objectid,
  4172. parent_ino, index, name, name_len);
  4173. } else if (add_backref) {
  4174. ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
  4175. parent_ino, index);
  4176. }
  4177. /* Nothing to clean up yet */
  4178. if (ret)
  4179. return ret;
  4180. ret = btrfs_insert_dir_item(trans, root, name, name_len,
  4181. parent_inode, &key,
  4182. btrfs_inode_type(inode), index);
  4183. if (ret == -EEXIST)
  4184. goto fail_dir_item;
  4185. else if (ret) {
  4186. btrfs_abort_transaction(trans, root, ret);
  4187. return ret;
  4188. }
  4189. btrfs_i_size_write(parent_inode, parent_inode->i_size +
  4190. name_len * 2);
  4191. inode_inc_iversion(parent_inode);
  4192. parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
  4193. ret = btrfs_update_inode(trans, root, parent_inode);
  4194. if (ret)
  4195. btrfs_abort_transaction(trans, root, ret);
  4196. return ret;
  4197. fail_dir_item:
  4198. if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
  4199. u64 local_index;
  4200. int err;
  4201. err = btrfs_del_root_ref(trans, root->fs_info->tree_root,
  4202. key.objectid, root->root_key.objectid,
  4203. parent_ino, &local_index, name, name_len);
  4204. } else if (add_backref) {
  4205. u64 local_index;
  4206. int err;
  4207. err = btrfs_del_inode_ref(trans, root, name, name_len,
  4208. ino, parent_ino, &local_index);
  4209. }
  4210. return ret;
  4211. }
  4212. static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
  4213. struct inode *dir, struct dentry *dentry,
  4214. struct inode *inode, int backref, u64 index)
  4215. {
  4216. int err = btrfs_add_link(trans, dir, inode,
  4217. dentry->d_name.name, dentry->d_name.len,
  4218. backref, index);
  4219. if (err > 0)
  4220. err = -EEXIST;
  4221. return err;
  4222. }
  4223. static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
  4224. umode_t mode, dev_t rdev)
  4225. {
  4226. struct btrfs_trans_handle *trans;
  4227. struct btrfs_root *root = BTRFS_I(dir)->root;
  4228. struct inode *inode = NULL;
  4229. int err;
  4230. int drop_inode = 0;
  4231. u64 objectid;
  4232. unsigned long nr = 0;
  4233. u64 index = 0;
  4234. if (!new_valid_dev(rdev))
  4235. return -EINVAL;
  4236. /*
  4237. * 2 for inode item and ref
  4238. * 2 for dir items
  4239. * 1 for xattr if selinux is on
  4240. */
  4241. trans = btrfs_start_transaction(root, 5);
  4242. if (IS_ERR(trans))
  4243. return PTR_ERR(trans);
  4244. err = btrfs_find_free_ino(root, &objectid);
  4245. if (err)
  4246. goto out_unlock;
  4247. inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
  4248. dentry->d_name.len, btrfs_ino(dir), objectid,
  4249. mode, &index);
  4250. if (IS_ERR(inode)) {
  4251. err = PTR_ERR(inode);
  4252. goto out_unlock;
  4253. }
  4254. err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
  4255. if (err) {
  4256. drop_inode = 1;
  4257. goto out_unlock;
  4258. }
  4259. /*
  4260. * If the active LSM wants to access the inode during
  4261. * d_instantiate it needs these. Smack checks to see
  4262. * if the filesystem supports xattrs by looking at the
  4263. * ops vector.
  4264. */
  4265. inode->i_op = &btrfs_special_inode_operations;
  4266. err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
  4267. if (err)
  4268. drop_inode = 1;
  4269. else {
  4270. init_special_inode(inode, inode->i_mode, rdev);
  4271. btrfs_update_inode(trans, root, inode);
  4272. d_instantiate(dentry, inode);
  4273. }
  4274. out_unlock:
  4275. nr = trans->blocks_used;
  4276. btrfs_end_transaction(trans, root);
  4277. btrfs_btree_balance_dirty(root, nr);
  4278. if (drop_inode) {
  4279. inode_dec_link_count(inode);
  4280. iput(inode);
  4281. }
  4282. return err;
  4283. }
  4284. static int btrfs_create(struct inode *dir, struct dentry *dentry,
  4285. umode_t mode, struct nameidata *nd)
  4286. {
  4287. struct btrfs_trans_handle *trans;
  4288. struct btrfs_root *root = BTRFS_I(dir)->root;
  4289. struct inode *inode = NULL;
  4290. int drop_inode = 0;
  4291. int err;
  4292. unsigned long nr = 0;
  4293. u64 objectid;
  4294. u64 index = 0;
  4295. /*
  4296. * 2 for inode item and ref
  4297. * 2 for dir items
  4298. * 1 for xattr if selinux is on
  4299. */
  4300. trans = btrfs_start_transaction(root, 5);
  4301. if (IS_ERR(trans))
  4302. return PTR_ERR(trans);
  4303. err = btrfs_find_free_ino(root, &objectid);
  4304. if (err)
  4305. goto out_unlock;
  4306. inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
  4307. dentry->d_name.len, btrfs_ino(dir), objectid,
  4308. mode, &index);
  4309. if (IS_ERR(inode)) {
  4310. err = PTR_ERR(inode);
  4311. goto out_unlock;
  4312. }
  4313. err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
  4314. if (err) {
  4315. drop_inode = 1;
  4316. goto out_unlock;
  4317. }
  4318. /*
  4319. * If the active LSM wants to access the inode during
  4320. * d_instantiate it needs these. Smack checks to see
  4321. * if the filesystem supports xattrs by looking at the
  4322. * ops vector.
  4323. */
  4324. inode->i_fop = &btrfs_file_operations;
  4325. inode->i_op = &btrfs_file_inode_operations;
  4326. err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
  4327. if (err)
  4328. drop_inode = 1;
  4329. else {
  4330. inode->i_mapping->a_ops = &btrfs_aops;
  4331. inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
  4332. BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
  4333. d_instantiate(dentry, inode);
  4334. }
  4335. out_unlock:
  4336. nr = trans->blocks_used;
  4337. btrfs_end_transaction(trans, root);
  4338. if (drop_inode) {
  4339. inode_dec_link_count(inode);
  4340. iput(inode);
  4341. }
  4342. btrfs_btree_balance_dirty(root, nr);
  4343. return err;
  4344. }
  4345. static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
  4346. struct dentry *dentry)
  4347. {
  4348. struct btrfs_trans_handle *trans;
  4349. struct btrfs_root *root = BTRFS_I(dir)->root;
  4350. struct inode *inode = old_dentry->d_inode;
  4351. u64 index;
  4352. unsigned long nr = 0;
  4353. int err;
  4354. int drop_inode = 0;
  4355. /* do not allow sys_link's with other subvols of the same device */
  4356. if (root->objectid != BTRFS_I(inode)->root->objectid)
  4357. return -EXDEV;
  4358. if (inode->i_nlink == ~0U)
  4359. return -EMLINK;
  4360. err = btrfs_set_inode_index(dir, &index);
  4361. if (err)
  4362. goto fail;
  4363. /*
  4364. * 2 items for inode and inode ref
  4365. * 2 items for dir items
  4366. * 1 item for parent inode
  4367. */
  4368. trans = btrfs_start_transaction(root, 5);
  4369. if (IS_ERR(trans)) {
  4370. err = PTR_ERR(trans);
  4371. goto fail;
  4372. }
  4373. btrfs_inc_nlink(inode);
  4374. inode_inc_iversion(inode);
  4375. inode->i_ctime = CURRENT_TIME;
  4376. ihold(inode);
  4377. err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
  4378. if (err) {
  4379. drop_inode = 1;
  4380. } else {
  4381. struct dentry *parent = dentry->d_parent;
  4382. err = btrfs_update_inode(trans, root, inode);
  4383. if (err)
  4384. goto fail;
  4385. d_instantiate(dentry, inode);
  4386. btrfs_log_new_name(trans, inode, NULL, parent);
  4387. }
  4388. nr = trans->blocks_used;
  4389. btrfs_end_transaction(trans, root);
  4390. fail:
  4391. if (drop_inode) {
  4392. inode_dec_link_count(inode);
  4393. iput(inode);
  4394. }
  4395. btrfs_btree_balance_dirty(root, nr);
  4396. return err;
  4397. }
  4398. static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
  4399. {
  4400. struct inode *inode = NULL;
  4401. struct btrfs_trans_handle *trans;
  4402. struct btrfs_root *root = BTRFS_I(dir)->root;
  4403. int err = 0;
  4404. int drop_on_err = 0;
  4405. u64 objectid = 0;
  4406. u64 index = 0;
  4407. unsigned long nr = 1;
  4408. /*
  4409. * 2 items for inode and ref
  4410. * 2 items for dir items
  4411. * 1 for xattr if selinux is on
  4412. */
  4413. trans = btrfs_start_transaction(root, 5);
  4414. if (IS_ERR(trans))
  4415. return PTR_ERR(trans);
  4416. err = btrfs_find_free_ino(root, &objectid);
  4417. if (err)
  4418. goto out_fail;
  4419. inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
  4420. dentry->d_name.len, btrfs_ino(dir), objectid,
  4421. S_IFDIR | mode, &index);
  4422. if (IS_ERR(inode)) {
  4423. err = PTR_ERR(inode);
  4424. goto out_fail;
  4425. }
  4426. drop_on_err = 1;
  4427. err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
  4428. if (err)
  4429. goto out_fail;
  4430. inode->i_op = &btrfs_dir_inode_operations;
  4431. inode->i_fop = &btrfs_dir_file_operations;
  4432. btrfs_i_size_write(inode, 0);
  4433. err = btrfs_update_inode(trans, root, inode);
  4434. if (err)
  4435. goto out_fail;
  4436. err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
  4437. dentry->d_name.len, 0, index);
  4438. if (err)
  4439. goto out_fail;
  4440. d_instantiate(dentry, inode);
  4441. drop_on_err = 0;
  4442. out_fail:
  4443. nr = trans->blocks_used;
  4444. btrfs_end_transaction(trans, root);
  4445. if (drop_on_err)
  4446. iput(inode);
  4447. btrfs_btree_balance_dirty(root, nr);
  4448. return err;
  4449. }
  4450. /* helper for btfs_get_extent. Given an existing extent in the tree,
  4451. * and an extent that you want to insert, deal with overlap and insert
  4452. * the new extent into the tree.
  4453. */
  4454. static int merge_extent_mapping(struct extent_map_tree *em_tree,
  4455. struct extent_map *existing,
  4456. struct extent_map *em,
  4457. u64 map_start, u64 map_len)
  4458. {
  4459. u64 start_diff;
  4460. BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
  4461. start_diff = map_start - em->start;
  4462. em->start = map_start;
  4463. em->len = map_len;
  4464. if (em->block_start < EXTENT_MAP_LAST_BYTE &&
  4465. !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
  4466. em->block_start += start_diff;
  4467. em->block_len -= start_diff;
  4468. }
  4469. return add_extent_mapping(em_tree, em);
  4470. }
  4471. static noinline int uncompress_inline(struct btrfs_path *path,
  4472. struct inode *inode, struct page *page,
  4473. size_t pg_offset, u64 extent_offset,
  4474. struct btrfs_file_extent_item *item)
  4475. {
  4476. int ret;
  4477. struct extent_buffer *leaf = path->nodes[0];
  4478. char *tmp;
  4479. size_t max_size;
  4480. unsigned long inline_size;
  4481. unsigned long ptr;
  4482. int compress_type;
  4483. WARN_ON(pg_offset != 0);
  4484. compress_type = btrfs_file_extent_compression(leaf, item);
  4485. max_size = btrfs_file_extent_ram_bytes(leaf, item);
  4486. inline_size = btrfs_file_extent_inline_item_len(leaf,
  4487. btrfs_item_nr(leaf, path->slots[0]));
  4488. tmp = kmalloc(inline_size, GFP_NOFS);
  4489. if (!tmp)
  4490. return -ENOMEM;
  4491. ptr = btrfs_file_extent_inline_start(item);
  4492. read_extent_buffer(leaf, tmp, ptr, inline_size);
  4493. max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
  4494. ret = btrfs_decompress(compress_type, tmp, page,
  4495. extent_offset, inline_size, max_size);
  4496. if (ret) {
  4497. char *kaddr = kmap_atomic(page);
  4498. unsigned long copy_size = min_t(u64,
  4499. PAGE_CACHE_SIZE - pg_offset,
  4500. max_size - extent_offset);
  4501. memset(kaddr + pg_offset, 0, copy_size);
  4502. kunmap_atomic(kaddr);
  4503. }
  4504. kfree(tmp);
  4505. return 0;
  4506. }
  4507. /*
  4508. * a bit scary, this does extent mapping from logical file offset to the disk.
  4509. * the ugly parts come from merging extents from the disk with the in-ram
  4510. * representation. This gets more complex because of the data=ordered code,
  4511. * where the in-ram extents might be locked pending data=ordered completion.
  4512. *
  4513. * This also copies inline extents directly into the page.
  4514. */
  4515. struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
  4516. size_t pg_offset, u64 start, u64 len,
  4517. int create)
  4518. {
  4519. int ret;
  4520. int err = 0;
  4521. u64 bytenr;
  4522. u64 extent_start = 0;
  4523. u64 extent_end = 0;
  4524. u64 objectid = btrfs_ino(inode);
  4525. u32 found_type;
  4526. struct btrfs_path *path = NULL;
  4527. struct btrfs_root *root = BTRFS_I(inode)->root;
  4528. struct btrfs_file_extent_item *item;
  4529. struct extent_buffer *leaf;
  4530. struct btrfs_key found_key;
  4531. struct extent_map *em = NULL;
  4532. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  4533. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  4534. struct btrfs_trans_handle *trans = NULL;
  4535. int compress_type;
  4536. again:
  4537. read_lock(&em_tree->lock);
  4538. em = lookup_extent_mapping(em_tree, start, len);
  4539. if (em)
  4540. em->bdev = root->fs_info->fs_devices->latest_bdev;
  4541. read_unlock(&em_tree->lock);
  4542. if (em) {
  4543. if (em->start > start || em->start + em->len <= start)
  4544. free_extent_map(em);
  4545. else if (em->block_start == EXTENT_MAP_INLINE && page)
  4546. free_extent_map(em);
  4547. else
  4548. goto out;
  4549. }
  4550. em = alloc_extent_map();
  4551. if (!em) {
  4552. err = -ENOMEM;
  4553. goto out;
  4554. }
  4555. em->bdev = root->fs_info->fs_devices->latest_bdev;
  4556. em->start = EXTENT_MAP_HOLE;
  4557. em->orig_start = EXTENT_MAP_HOLE;
  4558. em->len = (u64)-1;
  4559. em->block_len = (u64)-1;
  4560. if (!path) {
  4561. path = btrfs_alloc_path();
  4562. if (!path) {
  4563. err = -ENOMEM;
  4564. goto out;
  4565. }
  4566. /*
  4567. * Chances are we'll be called again, so go ahead and do
  4568. * readahead
  4569. */
  4570. path->reada = 1;
  4571. }
  4572. ret = btrfs_lookup_file_extent(trans, root, path,
  4573. objectid, start, trans != NULL);
  4574. if (ret < 0) {
  4575. err = ret;
  4576. goto out;
  4577. }
  4578. if (ret != 0) {
  4579. if (path->slots[0] == 0)
  4580. goto not_found;
  4581. path->slots[0]--;
  4582. }
  4583. leaf = path->nodes[0];
  4584. item = btrfs_item_ptr(leaf, path->slots[0],
  4585. struct btrfs_file_extent_item);
  4586. /* are we inside the extent that was found? */
  4587. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  4588. found_type = btrfs_key_type(&found_key);
  4589. if (found_key.objectid != objectid ||
  4590. found_type != BTRFS_EXTENT_DATA_KEY) {
  4591. goto not_found;
  4592. }
  4593. found_type = btrfs_file_extent_type(leaf, item);
  4594. extent_start = found_key.offset;
  4595. compress_type = btrfs_file_extent_compression(leaf, item);
  4596. if (found_type == BTRFS_FILE_EXTENT_REG ||
  4597. found_type == BTRFS_FILE_EXTENT_PREALLOC) {
  4598. extent_end = extent_start +
  4599. btrfs_file_extent_num_bytes(leaf, item);
  4600. } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
  4601. size_t size;
  4602. size = btrfs_file_extent_inline_len(leaf, item);
  4603. extent_end = (extent_start + size + root->sectorsize - 1) &
  4604. ~((u64)root->sectorsize - 1);
  4605. }
  4606. if (start >= extent_end) {
  4607. path->slots[0]++;
  4608. if (path->slots[0] >= btrfs_header_nritems(leaf)) {
  4609. ret = btrfs_next_leaf(root, path);
  4610. if (ret < 0) {
  4611. err = ret;
  4612. goto out;
  4613. }
  4614. if (ret > 0)
  4615. goto not_found;
  4616. leaf = path->nodes[0];
  4617. }
  4618. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  4619. if (found_key.objectid != objectid ||
  4620. found_key.type != BTRFS_EXTENT_DATA_KEY)
  4621. goto not_found;
  4622. if (start + len <= found_key.offset)
  4623. goto not_found;
  4624. em->start = start;
  4625. em->len = found_key.offset - start;
  4626. goto not_found_em;
  4627. }
  4628. if (found_type == BTRFS_FILE_EXTENT_REG ||
  4629. found_type == BTRFS_FILE_EXTENT_PREALLOC) {
  4630. em->start = extent_start;
  4631. em->len = extent_end - extent_start;
  4632. em->orig_start = extent_start -
  4633. btrfs_file_extent_offset(leaf, item);
  4634. bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
  4635. if (bytenr == 0) {
  4636. em->block_start = EXTENT_MAP_HOLE;
  4637. goto insert;
  4638. }
  4639. if (compress_type != BTRFS_COMPRESS_NONE) {
  4640. set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
  4641. em->compress_type = compress_type;
  4642. em->block_start = bytenr;
  4643. em->block_len = btrfs_file_extent_disk_num_bytes(leaf,
  4644. item);
  4645. } else {
  4646. bytenr += btrfs_file_extent_offset(leaf, item);
  4647. em->block_start = bytenr;
  4648. em->block_len = em->len;
  4649. if (found_type == BTRFS_FILE_EXTENT_PREALLOC)
  4650. set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
  4651. }
  4652. goto insert;
  4653. } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
  4654. unsigned long ptr;
  4655. char *map;
  4656. size_t size;
  4657. size_t extent_offset;
  4658. size_t copy_size;
  4659. em->block_start = EXTENT_MAP_INLINE;
  4660. if (!page || create) {
  4661. em->start = extent_start;
  4662. em->len = extent_end - extent_start;
  4663. goto out;
  4664. }
  4665. size = btrfs_file_extent_inline_len(leaf, item);
  4666. extent_offset = page_offset(page) + pg_offset - extent_start;
  4667. copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
  4668. size - extent_offset);
  4669. em->start = extent_start + extent_offset;
  4670. em->len = (copy_size + root->sectorsize - 1) &
  4671. ~((u64)root->sectorsize - 1);
  4672. em->orig_start = EXTENT_MAP_INLINE;
  4673. if (compress_type) {
  4674. set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
  4675. em->compress_type = compress_type;
  4676. }
  4677. ptr = btrfs_file_extent_inline_start(item) + extent_offset;
  4678. if (create == 0 && !PageUptodate(page)) {
  4679. if (btrfs_file_extent_compression(leaf, item) !=
  4680. BTRFS_COMPRESS_NONE) {
  4681. ret = uncompress_inline(path, inode, page,
  4682. pg_offset,
  4683. extent_offset, item);
  4684. BUG_ON(ret); /* -ENOMEM */
  4685. } else {
  4686. map = kmap(page);
  4687. read_extent_buffer(leaf, map + pg_offset, ptr,
  4688. copy_size);
  4689. if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
  4690. memset(map + pg_offset + copy_size, 0,
  4691. PAGE_CACHE_SIZE - pg_offset -
  4692. copy_size);
  4693. }
  4694. kunmap(page);
  4695. }
  4696. flush_dcache_page(page);
  4697. } else if (create && PageUptodate(page)) {
  4698. BUG();
  4699. if (!trans) {
  4700. kunmap(page);
  4701. free_extent_map(em);
  4702. em = NULL;
  4703. btrfs_release_path(path);
  4704. trans = btrfs_join_transaction(root);
  4705. if (IS_ERR(trans))
  4706. return ERR_CAST(trans);
  4707. goto again;
  4708. }
  4709. map = kmap(page);
  4710. write_extent_buffer(leaf, map + pg_offset, ptr,
  4711. copy_size);
  4712. kunmap(page);
  4713. btrfs_mark_buffer_dirty(leaf);
  4714. }
  4715. set_extent_uptodate(io_tree, em->start,
  4716. extent_map_end(em) - 1, NULL, GFP_NOFS);
  4717. goto insert;
  4718. } else {
  4719. printk(KERN_ERR "btrfs unknown found_type %d\n", found_type);
  4720. WARN_ON(1);
  4721. }
  4722. not_found:
  4723. em->start = start;
  4724. em->len = len;
  4725. not_found_em:
  4726. em->block_start = EXTENT_MAP_HOLE;
  4727. set_bit(EXTENT_FLAG_VACANCY, &em->flags);
  4728. insert:
  4729. btrfs_release_path(path);
  4730. if (em->start > start || extent_map_end(em) <= start) {
  4731. printk(KERN_ERR "Btrfs: bad extent! em: [%llu %llu] passed "
  4732. "[%llu %llu]\n", (unsigned long long)em->start,
  4733. (unsigned long long)em->len,
  4734. (unsigned long long)start,
  4735. (unsigned long long)len);
  4736. err = -EIO;
  4737. goto out;
  4738. }
  4739. err = 0;
  4740. write_lock(&em_tree->lock);
  4741. ret = add_extent_mapping(em_tree, em);
  4742. /* it is possible that someone inserted the extent into the tree
  4743. * while we had the lock dropped. It is also possible that
  4744. * an overlapping map exists in the tree
  4745. */
  4746. if (ret == -EEXIST) {
  4747. struct extent_map *existing;
  4748. ret = 0;
  4749. existing = lookup_extent_mapping(em_tree, start, len);
  4750. if (existing && (existing->start > start ||
  4751. existing->start + existing->len <= start)) {
  4752. free_extent_map(existing);
  4753. existing = NULL;
  4754. }
  4755. if (!existing) {
  4756. existing = lookup_extent_mapping(em_tree, em->start,
  4757. em->len);
  4758. if (existing) {
  4759. err = merge_extent_mapping(em_tree, existing,
  4760. em, start,
  4761. root->sectorsize);
  4762. free_extent_map(existing);
  4763. if (err) {
  4764. free_extent_map(em);
  4765. em = NULL;
  4766. }
  4767. } else {
  4768. err = -EIO;
  4769. free_extent_map(em);
  4770. em = NULL;
  4771. }
  4772. } else {
  4773. free_extent_map(em);
  4774. em = existing;
  4775. err = 0;
  4776. }
  4777. }
  4778. write_unlock(&em_tree->lock);
  4779. out:
  4780. trace_btrfs_get_extent(root, em);
  4781. if (path)
  4782. btrfs_free_path(path);
  4783. if (trans) {
  4784. ret = btrfs_end_transaction(trans, root);
  4785. if (!err)
  4786. err = ret;
  4787. }
  4788. if (err) {
  4789. free_extent_map(em);
  4790. return ERR_PTR(err);
  4791. }
  4792. BUG_ON(!em); /* Error is always set */
  4793. return em;
  4794. }
  4795. struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
  4796. size_t pg_offset, u64 start, u64 len,
  4797. int create)
  4798. {
  4799. struct extent_map *em;
  4800. struct extent_map *hole_em = NULL;
  4801. u64 range_start = start;
  4802. u64 end;
  4803. u64 found;
  4804. u64 found_end;
  4805. int err = 0;
  4806. em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
  4807. if (IS_ERR(em))
  4808. return em;
  4809. if (em) {
  4810. /*
  4811. * if our em maps to a hole, there might
  4812. * actually be delalloc bytes behind it
  4813. */
  4814. if (em->block_start != EXTENT_MAP_HOLE)
  4815. return em;
  4816. else
  4817. hole_em = em;
  4818. }
  4819. /* check to see if we've wrapped (len == -1 or similar) */
  4820. end = start + len;
  4821. if (end < start)
  4822. end = (u64)-1;
  4823. else
  4824. end -= 1;
  4825. em = NULL;
  4826. /* ok, we didn't find anything, lets look for delalloc */
  4827. found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
  4828. end, len, EXTENT_DELALLOC, 1);
  4829. found_end = range_start + found;
  4830. if (found_end < range_start)
  4831. found_end = (u64)-1;
  4832. /*
  4833. * we didn't find anything useful, return
  4834. * the original results from get_extent()
  4835. */
  4836. if (range_start > end || found_end <= start) {
  4837. em = hole_em;
  4838. hole_em = NULL;
  4839. goto out;
  4840. }
  4841. /* adjust the range_start to make sure it doesn't
  4842. * go backwards from the start they passed in
  4843. */
  4844. range_start = max(start,range_start);
  4845. found = found_end - range_start;
  4846. if (found > 0) {
  4847. u64 hole_start = start;
  4848. u64 hole_len = len;
  4849. em = alloc_extent_map();
  4850. if (!em) {
  4851. err = -ENOMEM;
  4852. goto out;
  4853. }
  4854. /*
  4855. * when btrfs_get_extent can't find anything it
  4856. * returns one huge hole
  4857. *
  4858. * make sure what it found really fits our range, and
  4859. * adjust to make sure it is based on the start from
  4860. * the caller
  4861. */
  4862. if (hole_em) {
  4863. u64 calc_end = extent_map_end(hole_em);
  4864. if (calc_end <= start || (hole_em->start > end)) {
  4865. free_extent_map(hole_em);
  4866. hole_em = NULL;
  4867. } else {
  4868. hole_start = max(hole_em->start, start);
  4869. hole_len = calc_end - hole_start;
  4870. }
  4871. }
  4872. em->bdev = NULL;
  4873. if (hole_em && range_start > hole_start) {
  4874. /* our hole starts before our delalloc, so we
  4875. * have to return just the parts of the hole
  4876. * that go until the delalloc starts
  4877. */
  4878. em->len = min(hole_len,
  4879. range_start - hole_start);
  4880. em->start = hole_start;
  4881. em->orig_start = hole_start;
  4882. /*
  4883. * don't adjust block start at all,
  4884. * it is fixed at EXTENT_MAP_HOLE
  4885. */
  4886. em->block_start = hole_em->block_start;
  4887. em->block_len = hole_len;
  4888. } else {
  4889. em->start = range_start;
  4890. em->len = found;
  4891. em->orig_start = range_start;
  4892. em->block_start = EXTENT_MAP_DELALLOC;
  4893. em->block_len = found;
  4894. }
  4895. } else if (hole_em) {
  4896. return hole_em;
  4897. }
  4898. out:
  4899. free_extent_map(hole_em);
  4900. if (err) {
  4901. free_extent_map(em);
  4902. return ERR_PTR(err);
  4903. }
  4904. return em;
  4905. }
  4906. static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
  4907. struct extent_map *em,
  4908. u64 start, u64 len)
  4909. {
  4910. struct btrfs_root *root = BTRFS_I(inode)->root;
  4911. struct btrfs_trans_handle *trans;
  4912. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  4913. struct btrfs_key ins;
  4914. u64 alloc_hint;
  4915. int ret;
  4916. bool insert = false;
  4917. /*
  4918. * Ok if the extent map we looked up is a hole and is for the exact
  4919. * range we want, there is no reason to allocate a new one, however if
  4920. * it is not right then we need to free this one and drop the cache for
  4921. * our range.
  4922. */
  4923. if (em->block_start != EXTENT_MAP_HOLE || em->start != start ||
  4924. em->len != len) {
  4925. free_extent_map(em);
  4926. em = NULL;
  4927. insert = true;
  4928. btrfs_drop_extent_cache(inode, start, start + len - 1, 0);
  4929. }
  4930. trans = btrfs_join_transaction(root);
  4931. if (IS_ERR(trans))
  4932. return ERR_CAST(trans);
  4933. if (start <= BTRFS_I(inode)->disk_i_size && len < 64 * 1024)
  4934. btrfs_add_inode_defrag(trans, inode);
  4935. trans->block_rsv = &root->fs_info->delalloc_block_rsv;
  4936. alloc_hint = get_extent_allocation_hint(inode, start, len);
  4937. ret = btrfs_reserve_extent(trans, root, len, root->sectorsize, 0,
  4938. alloc_hint, &ins, 1);
  4939. if (ret) {
  4940. em = ERR_PTR(ret);
  4941. goto out;
  4942. }
  4943. if (!em) {
  4944. em = alloc_extent_map();
  4945. if (!em) {
  4946. em = ERR_PTR(-ENOMEM);
  4947. goto out;
  4948. }
  4949. }
  4950. em->start = start;
  4951. em->orig_start = em->start;
  4952. em->len = ins.offset;
  4953. em->block_start = ins.objectid;
  4954. em->block_len = ins.offset;
  4955. em->bdev = root->fs_info->fs_devices->latest_bdev;
  4956. /*
  4957. * We need to do this because if we're using the original em we searched
  4958. * for, we could have EXTENT_FLAG_VACANCY set, and we don't want that.
  4959. */
  4960. em->flags = 0;
  4961. set_bit(EXTENT_FLAG_PINNED, &em->flags);
  4962. while (insert) {
  4963. write_lock(&em_tree->lock);
  4964. ret = add_extent_mapping(em_tree, em);
  4965. write_unlock(&em_tree->lock);
  4966. if (ret != -EEXIST)
  4967. break;
  4968. btrfs_drop_extent_cache(inode, start, start + em->len - 1, 0);
  4969. }
  4970. ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid,
  4971. ins.offset, ins.offset, 0);
  4972. if (ret) {
  4973. btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
  4974. em = ERR_PTR(ret);
  4975. }
  4976. out:
  4977. btrfs_end_transaction(trans, root);
  4978. return em;
  4979. }
  4980. /*
  4981. * returns 1 when the nocow is safe, < 1 on error, 0 if the
  4982. * block must be cow'd
  4983. */
  4984. static noinline int can_nocow_odirect(struct btrfs_trans_handle *trans,
  4985. struct inode *inode, u64 offset, u64 len)
  4986. {
  4987. struct btrfs_path *path;
  4988. int ret;
  4989. struct extent_buffer *leaf;
  4990. struct btrfs_root *root = BTRFS_I(inode)->root;
  4991. struct btrfs_file_extent_item *fi;
  4992. struct btrfs_key key;
  4993. u64 disk_bytenr;
  4994. u64 backref_offset;
  4995. u64 extent_end;
  4996. u64 num_bytes;
  4997. int slot;
  4998. int found_type;
  4999. path = btrfs_alloc_path();
  5000. if (!path)
  5001. return -ENOMEM;
  5002. ret = btrfs_lookup_file_extent(trans, root, path, btrfs_ino(inode),
  5003. offset, 0);
  5004. if (ret < 0)
  5005. goto out;
  5006. slot = path->slots[0];
  5007. if (ret == 1) {
  5008. if (slot == 0) {
  5009. /* can't find the item, must cow */
  5010. ret = 0;
  5011. goto out;
  5012. }
  5013. slot--;
  5014. }
  5015. ret = 0;
  5016. leaf = path->nodes[0];
  5017. btrfs_item_key_to_cpu(leaf, &key, slot);
  5018. if (key.objectid != btrfs_ino(inode) ||
  5019. key.type != BTRFS_EXTENT_DATA_KEY) {
  5020. /* not our file or wrong item type, must cow */
  5021. goto out;
  5022. }
  5023. if (key.offset > offset) {
  5024. /* Wrong offset, must cow */
  5025. goto out;
  5026. }
  5027. fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
  5028. found_type = btrfs_file_extent_type(leaf, fi);
  5029. if (found_type != BTRFS_FILE_EXTENT_REG &&
  5030. found_type != BTRFS_FILE_EXTENT_PREALLOC) {
  5031. /* not a regular extent, must cow */
  5032. goto out;
  5033. }
  5034. disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
  5035. backref_offset = btrfs_file_extent_offset(leaf, fi);
  5036. extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
  5037. if (extent_end < offset + len) {
  5038. /* extent doesn't include our full range, must cow */
  5039. goto out;
  5040. }
  5041. if (btrfs_extent_readonly(root, disk_bytenr))
  5042. goto out;
  5043. /*
  5044. * look for other files referencing this extent, if we
  5045. * find any we must cow
  5046. */
  5047. if (btrfs_cross_ref_exist(trans, root, btrfs_ino(inode),
  5048. key.offset - backref_offset, disk_bytenr))
  5049. goto out;
  5050. /*
  5051. * adjust disk_bytenr and num_bytes to cover just the bytes
  5052. * in this extent we are about to write. If there
  5053. * are any csums in that range we have to cow in order
  5054. * to keep the csums correct
  5055. */
  5056. disk_bytenr += backref_offset;
  5057. disk_bytenr += offset - key.offset;
  5058. num_bytes = min(offset + len, extent_end) - offset;
  5059. if (csum_exist_in_range(root, disk_bytenr, num_bytes))
  5060. goto out;
  5061. /*
  5062. * all of the above have passed, it is safe to overwrite this extent
  5063. * without cow
  5064. */
  5065. ret = 1;
  5066. out:
  5067. btrfs_free_path(path);
  5068. return ret;
  5069. }
  5070. static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
  5071. struct buffer_head *bh_result, int create)
  5072. {
  5073. struct extent_map *em;
  5074. struct btrfs_root *root = BTRFS_I(inode)->root;
  5075. u64 start = iblock << inode->i_blkbits;
  5076. u64 len = bh_result->b_size;
  5077. struct btrfs_trans_handle *trans;
  5078. em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
  5079. if (IS_ERR(em))
  5080. return PTR_ERR(em);
  5081. /*
  5082. * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
  5083. * io. INLINE is special, and we could probably kludge it in here, but
  5084. * it's still buffered so for safety lets just fall back to the generic
  5085. * buffered path.
  5086. *
  5087. * For COMPRESSED we _have_ to read the entire extent in so we can
  5088. * decompress it, so there will be buffering required no matter what we
  5089. * do, so go ahead and fallback to buffered.
  5090. *
  5091. * We return -ENOTBLK because thats what makes DIO go ahead and go back
  5092. * to buffered IO. Don't blame me, this is the price we pay for using
  5093. * the generic code.
  5094. */
  5095. if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
  5096. em->block_start == EXTENT_MAP_INLINE) {
  5097. free_extent_map(em);
  5098. return -ENOTBLK;
  5099. }
  5100. /* Just a good old fashioned hole, return */
  5101. if (!create && (em->block_start == EXTENT_MAP_HOLE ||
  5102. test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
  5103. free_extent_map(em);
  5104. /* DIO will do one hole at a time, so just unlock a sector */
  5105. unlock_extent(&BTRFS_I(inode)->io_tree, start,
  5106. start + root->sectorsize - 1);
  5107. return 0;
  5108. }
  5109. /*
  5110. * We don't allocate a new extent in the following cases
  5111. *
  5112. * 1) The inode is marked as NODATACOW. In this case we'll just use the
  5113. * existing extent.
  5114. * 2) The extent is marked as PREALLOC. We're good to go here and can
  5115. * just use the extent.
  5116. *
  5117. */
  5118. if (!create) {
  5119. len = em->len - (start - em->start);
  5120. goto map;
  5121. }
  5122. if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
  5123. ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
  5124. em->block_start != EXTENT_MAP_HOLE)) {
  5125. int type;
  5126. int ret;
  5127. u64 block_start;
  5128. if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
  5129. type = BTRFS_ORDERED_PREALLOC;
  5130. else
  5131. type = BTRFS_ORDERED_NOCOW;
  5132. len = min(len, em->len - (start - em->start));
  5133. block_start = em->block_start + (start - em->start);
  5134. /*
  5135. * we're not going to log anything, but we do need
  5136. * to make sure the current transaction stays open
  5137. * while we look for nocow cross refs
  5138. */
  5139. trans = btrfs_join_transaction(root);
  5140. if (IS_ERR(trans))
  5141. goto must_cow;
  5142. if (can_nocow_odirect(trans, inode, start, len) == 1) {
  5143. ret = btrfs_add_ordered_extent_dio(inode, start,
  5144. block_start, len, len, type);
  5145. btrfs_end_transaction(trans, root);
  5146. if (ret) {
  5147. free_extent_map(em);
  5148. return ret;
  5149. }
  5150. goto unlock;
  5151. }
  5152. btrfs_end_transaction(trans, root);
  5153. }
  5154. must_cow:
  5155. /*
  5156. * this will cow the extent, reset the len in case we changed
  5157. * it above
  5158. */
  5159. len = bh_result->b_size;
  5160. em = btrfs_new_extent_direct(inode, em, start, len);
  5161. if (IS_ERR(em))
  5162. return PTR_ERR(em);
  5163. len = min(len, em->len - (start - em->start));
  5164. unlock:
  5165. clear_extent_bit(&BTRFS_I(inode)->io_tree, start, start + len - 1,
  5166. EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DIRTY, 1,
  5167. 0, NULL, GFP_NOFS);
  5168. map:
  5169. bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
  5170. inode->i_blkbits;
  5171. bh_result->b_size = len;
  5172. bh_result->b_bdev = em->bdev;
  5173. set_buffer_mapped(bh_result);
  5174. if (create && !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
  5175. set_buffer_new(bh_result);
  5176. free_extent_map(em);
  5177. return 0;
  5178. }
  5179. struct btrfs_dio_private {
  5180. struct inode *inode;
  5181. u64 logical_offset;
  5182. u64 disk_bytenr;
  5183. u64 bytes;
  5184. u32 *csums;
  5185. void *private;
  5186. /* number of bios pending for this dio */
  5187. atomic_t pending_bios;
  5188. /* IO errors */
  5189. int errors;
  5190. struct bio *orig_bio;
  5191. };
  5192. static void btrfs_endio_direct_read(struct bio *bio, int err)
  5193. {
  5194. struct btrfs_dio_private *dip = bio->bi_private;
  5195. struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
  5196. struct bio_vec *bvec = bio->bi_io_vec;
  5197. struct inode *inode = dip->inode;
  5198. struct btrfs_root *root = BTRFS_I(inode)->root;
  5199. u64 start;
  5200. u32 *private = dip->csums;
  5201. start = dip->logical_offset;
  5202. do {
  5203. if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
  5204. struct page *page = bvec->bv_page;
  5205. char *kaddr;
  5206. u32 csum = ~(u32)0;
  5207. unsigned long flags;
  5208. local_irq_save(flags);
  5209. kaddr = kmap_atomic(page);
  5210. csum = btrfs_csum_data(root, kaddr + bvec->bv_offset,
  5211. csum, bvec->bv_len);
  5212. btrfs_csum_final(csum, (char *)&csum);
  5213. kunmap_atomic(kaddr);
  5214. local_irq_restore(flags);
  5215. flush_dcache_page(bvec->bv_page);
  5216. if (csum != *private) {
  5217. printk(KERN_ERR "btrfs csum failed ino %llu off"
  5218. " %llu csum %u private %u\n",
  5219. (unsigned long long)btrfs_ino(inode),
  5220. (unsigned long long)start,
  5221. csum, *private);
  5222. err = -EIO;
  5223. }
  5224. }
  5225. start += bvec->bv_len;
  5226. private++;
  5227. bvec++;
  5228. } while (bvec <= bvec_end);
  5229. unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
  5230. dip->logical_offset + dip->bytes - 1);
  5231. bio->bi_private = dip->private;
  5232. kfree(dip->csums);
  5233. kfree(dip);
  5234. /* If we had a csum failure make sure to clear the uptodate flag */
  5235. if (err)
  5236. clear_bit(BIO_UPTODATE, &bio->bi_flags);
  5237. dio_end_io(bio, err);
  5238. }
  5239. static void btrfs_endio_direct_write(struct bio *bio, int err)
  5240. {
  5241. struct btrfs_dio_private *dip = bio->bi_private;
  5242. struct inode *inode = dip->inode;
  5243. struct btrfs_root *root = BTRFS_I(inode)->root;
  5244. struct btrfs_ordered_extent *ordered = NULL;
  5245. u64 ordered_offset = dip->logical_offset;
  5246. u64 ordered_bytes = dip->bytes;
  5247. int ret;
  5248. if (err)
  5249. goto out_done;
  5250. again:
  5251. ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
  5252. &ordered_offset,
  5253. ordered_bytes, !err);
  5254. if (!ret)
  5255. goto out_test;
  5256. ordered->work.func = finish_ordered_fn;
  5257. ordered->work.flags = 0;
  5258. btrfs_queue_worker(&root->fs_info->endio_write_workers,
  5259. &ordered->work);
  5260. out_test:
  5261. /*
  5262. * our bio might span multiple ordered extents. If we haven't
  5263. * completed the accounting for the whole dio, go back and try again
  5264. */
  5265. if (ordered_offset < dip->logical_offset + dip->bytes) {
  5266. ordered_bytes = dip->logical_offset + dip->bytes -
  5267. ordered_offset;
  5268. ordered = NULL;
  5269. goto again;
  5270. }
  5271. out_done:
  5272. bio->bi_private = dip->private;
  5273. kfree(dip);
  5274. /* If we had an error make sure to clear the uptodate flag */
  5275. if (err)
  5276. clear_bit(BIO_UPTODATE, &bio->bi_flags);
  5277. dio_end_io(bio, err);
  5278. }
  5279. static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw,
  5280. struct bio *bio, int mirror_num,
  5281. unsigned long bio_flags, u64 offset)
  5282. {
  5283. int ret;
  5284. struct btrfs_root *root = BTRFS_I(inode)->root;
  5285. ret = btrfs_csum_one_bio(root, inode, bio, offset, 1);
  5286. BUG_ON(ret); /* -ENOMEM */
  5287. return 0;
  5288. }
  5289. static void btrfs_end_dio_bio(struct bio *bio, int err)
  5290. {
  5291. struct btrfs_dio_private *dip = bio->bi_private;
  5292. if (err) {
  5293. printk(KERN_ERR "btrfs direct IO failed ino %llu rw %lu "
  5294. "sector %#Lx len %u err no %d\n",
  5295. (unsigned long long)btrfs_ino(dip->inode), bio->bi_rw,
  5296. (unsigned long long)bio->bi_sector, bio->bi_size, err);
  5297. dip->errors = 1;
  5298. /*
  5299. * before atomic variable goto zero, we must make sure
  5300. * dip->errors is perceived to be set.
  5301. */
  5302. smp_mb__before_atomic_dec();
  5303. }
  5304. /* if there are more bios still pending for this dio, just exit */
  5305. if (!atomic_dec_and_test(&dip->pending_bios))
  5306. goto out;
  5307. if (dip->errors)
  5308. bio_io_error(dip->orig_bio);
  5309. else {
  5310. set_bit(BIO_UPTODATE, &dip->orig_bio->bi_flags);
  5311. bio_endio(dip->orig_bio, 0);
  5312. }
  5313. out:
  5314. bio_put(bio);
  5315. }
  5316. static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
  5317. u64 first_sector, gfp_t gfp_flags)
  5318. {
  5319. int nr_vecs = bio_get_nr_vecs(bdev);
  5320. return btrfs_bio_alloc(bdev, first_sector, nr_vecs, gfp_flags);
  5321. }
  5322. static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
  5323. int rw, u64 file_offset, int skip_sum,
  5324. u32 *csums, int async_submit)
  5325. {
  5326. int write = rw & REQ_WRITE;
  5327. struct btrfs_root *root = BTRFS_I(inode)->root;
  5328. int ret;
  5329. bio_get(bio);
  5330. if (!write) {
  5331. ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
  5332. if (ret)
  5333. goto err;
  5334. }
  5335. if (skip_sum)
  5336. goto map;
  5337. if (write && async_submit) {
  5338. ret = btrfs_wq_submit_bio(root->fs_info,
  5339. inode, rw, bio, 0, 0,
  5340. file_offset,
  5341. __btrfs_submit_bio_start_direct_io,
  5342. __btrfs_submit_bio_done);
  5343. goto err;
  5344. } else if (write) {
  5345. /*
  5346. * If we aren't doing async submit, calculate the csum of the
  5347. * bio now.
  5348. */
  5349. ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1);
  5350. if (ret)
  5351. goto err;
  5352. } else if (!skip_sum) {
  5353. ret = btrfs_lookup_bio_sums_dio(root, inode, bio,
  5354. file_offset, csums);
  5355. if (ret)
  5356. goto err;
  5357. }
  5358. map:
  5359. ret = btrfs_map_bio(root, rw, bio, 0, async_submit);
  5360. err:
  5361. bio_put(bio);
  5362. return ret;
  5363. }
  5364. static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip,
  5365. int skip_sum)
  5366. {
  5367. struct inode *inode = dip->inode;
  5368. struct btrfs_root *root = BTRFS_I(inode)->root;
  5369. struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
  5370. struct bio *bio;
  5371. struct bio *orig_bio = dip->orig_bio;
  5372. struct bio_vec *bvec = orig_bio->bi_io_vec;
  5373. u64 start_sector = orig_bio->bi_sector;
  5374. u64 file_offset = dip->logical_offset;
  5375. u64 submit_len = 0;
  5376. u64 map_length;
  5377. int nr_pages = 0;
  5378. u32 *csums = dip->csums;
  5379. int ret = 0;
  5380. int async_submit = 0;
  5381. int write = rw & REQ_WRITE;
  5382. map_length = orig_bio->bi_size;
  5383. ret = btrfs_map_block(map_tree, READ, start_sector << 9,
  5384. &map_length, NULL, 0);
  5385. if (ret) {
  5386. bio_put(orig_bio);
  5387. return -EIO;
  5388. }
  5389. if (map_length >= orig_bio->bi_size) {
  5390. bio = orig_bio;
  5391. goto submit;
  5392. }
  5393. async_submit = 1;
  5394. bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
  5395. if (!bio)
  5396. return -ENOMEM;
  5397. bio->bi_private = dip;
  5398. bio->bi_end_io = btrfs_end_dio_bio;
  5399. atomic_inc(&dip->pending_bios);
  5400. while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) {
  5401. if (unlikely(map_length < submit_len + bvec->bv_len ||
  5402. bio_add_page(bio, bvec->bv_page, bvec->bv_len,
  5403. bvec->bv_offset) < bvec->bv_len)) {
  5404. /*
  5405. * inc the count before we submit the bio so
  5406. * we know the end IO handler won't happen before
  5407. * we inc the count. Otherwise, the dip might get freed
  5408. * before we're done setting it up
  5409. */
  5410. atomic_inc(&dip->pending_bios);
  5411. ret = __btrfs_submit_dio_bio(bio, inode, rw,
  5412. file_offset, skip_sum,
  5413. csums, async_submit);
  5414. if (ret) {
  5415. bio_put(bio);
  5416. atomic_dec(&dip->pending_bios);
  5417. goto out_err;
  5418. }
  5419. /* Write's use the ordered csums */
  5420. if (!write && !skip_sum)
  5421. csums = csums + nr_pages;
  5422. start_sector += submit_len >> 9;
  5423. file_offset += submit_len;
  5424. submit_len = 0;
  5425. nr_pages = 0;
  5426. bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
  5427. start_sector, GFP_NOFS);
  5428. if (!bio)
  5429. goto out_err;
  5430. bio->bi_private = dip;
  5431. bio->bi_end_io = btrfs_end_dio_bio;
  5432. map_length = orig_bio->bi_size;
  5433. ret = btrfs_map_block(map_tree, READ, start_sector << 9,
  5434. &map_length, NULL, 0);
  5435. if (ret) {
  5436. bio_put(bio);
  5437. goto out_err;
  5438. }
  5439. } else {
  5440. submit_len += bvec->bv_len;
  5441. nr_pages ++;
  5442. bvec++;
  5443. }
  5444. }
  5445. submit:
  5446. ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum,
  5447. csums, async_submit);
  5448. if (!ret)
  5449. return 0;
  5450. bio_put(bio);
  5451. out_err:
  5452. dip->errors = 1;
  5453. /*
  5454. * before atomic variable goto zero, we must
  5455. * make sure dip->errors is perceived to be set.
  5456. */
  5457. smp_mb__before_atomic_dec();
  5458. if (atomic_dec_and_test(&dip->pending_bios))
  5459. bio_io_error(dip->orig_bio);
  5460. /* bio_end_io() will handle error, so we needn't return it */
  5461. return 0;
  5462. }
  5463. static void btrfs_submit_direct(int rw, struct bio *bio, struct inode *inode,
  5464. loff_t file_offset)
  5465. {
  5466. struct btrfs_root *root = BTRFS_I(inode)->root;
  5467. struct btrfs_dio_private *dip;
  5468. struct bio_vec *bvec = bio->bi_io_vec;
  5469. int skip_sum;
  5470. int write = rw & REQ_WRITE;
  5471. int ret = 0;
  5472. skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
  5473. dip = kmalloc(sizeof(*dip), GFP_NOFS);
  5474. if (!dip) {
  5475. ret = -ENOMEM;
  5476. goto free_ordered;
  5477. }
  5478. dip->csums = NULL;
  5479. /* Write's use the ordered csum stuff, so we don't need dip->csums */
  5480. if (!write && !skip_sum) {
  5481. dip->csums = kmalloc(sizeof(u32) * bio->bi_vcnt, GFP_NOFS);
  5482. if (!dip->csums) {
  5483. kfree(dip);
  5484. ret = -ENOMEM;
  5485. goto free_ordered;
  5486. }
  5487. }
  5488. dip->private = bio->bi_private;
  5489. dip->inode = inode;
  5490. dip->logical_offset = file_offset;
  5491. dip->bytes = 0;
  5492. do {
  5493. dip->bytes += bvec->bv_len;
  5494. bvec++;
  5495. } while (bvec <= (bio->bi_io_vec + bio->bi_vcnt - 1));
  5496. dip->disk_bytenr = (u64)bio->bi_sector << 9;
  5497. bio->bi_private = dip;
  5498. dip->errors = 0;
  5499. dip->orig_bio = bio;
  5500. atomic_set(&dip->pending_bios, 0);
  5501. if (write)
  5502. bio->bi_end_io = btrfs_endio_direct_write;
  5503. else
  5504. bio->bi_end_io = btrfs_endio_direct_read;
  5505. ret = btrfs_submit_direct_hook(rw, dip, skip_sum);
  5506. if (!ret)
  5507. return;
  5508. free_ordered:
  5509. /*
  5510. * If this is a write, we need to clean up the reserved space and kill
  5511. * the ordered extent.
  5512. */
  5513. if (write) {
  5514. struct btrfs_ordered_extent *ordered;
  5515. ordered = btrfs_lookup_ordered_extent(inode, file_offset);
  5516. if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) &&
  5517. !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags))
  5518. btrfs_free_reserved_extent(root, ordered->start,
  5519. ordered->disk_len);
  5520. btrfs_put_ordered_extent(ordered);
  5521. btrfs_put_ordered_extent(ordered);
  5522. }
  5523. bio_endio(bio, ret);
  5524. }
  5525. static ssize_t check_direct_IO(struct btrfs_root *root, int rw, struct kiocb *iocb,
  5526. const struct iovec *iov, loff_t offset,
  5527. unsigned long nr_segs)
  5528. {
  5529. int seg;
  5530. int i;
  5531. size_t size;
  5532. unsigned long addr;
  5533. unsigned blocksize_mask = root->sectorsize - 1;
  5534. ssize_t retval = -EINVAL;
  5535. loff_t end = offset;
  5536. if (offset & blocksize_mask)
  5537. goto out;
  5538. /* Check the memory alignment. Blocks cannot straddle pages */
  5539. for (seg = 0; seg < nr_segs; seg++) {
  5540. addr = (unsigned long)iov[seg].iov_base;
  5541. size = iov[seg].iov_len;
  5542. end += size;
  5543. if ((addr & blocksize_mask) || (size & blocksize_mask))
  5544. goto out;
  5545. /* If this is a write we don't need to check anymore */
  5546. if (rw & WRITE)
  5547. continue;
  5548. /*
  5549. * Check to make sure we don't have duplicate iov_base's in this
  5550. * iovec, if so return EINVAL, otherwise we'll get csum errors
  5551. * when reading back.
  5552. */
  5553. for (i = seg + 1; i < nr_segs; i++) {
  5554. if (iov[seg].iov_base == iov[i].iov_base)
  5555. goto out;
  5556. }
  5557. }
  5558. retval = 0;
  5559. out:
  5560. return retval;
  5561. }
  5562. static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
  5563. const struct iovec *iov, loff_t offset,
  5564. unsigned long nr_segs)
  5565. {
  5566. struct file *file = iocb->ki_filp;
  5567. struct inode *inode = file->f_mapping->host;
  5568. struct btrfs_ordered_extent *ordered;
  5569. struct extent_state *cached_state = NULL;
  5570. u64 lockstart, lockend;
  5571. ssize_t ret;
  5572. int writing = rw & WRITE;
  5573. int write_bits = 0;
  5574. size_t count = iov_length(iov, nr_segs);
  5575. if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iov,
  5576. offset, nr_segs)) {
  5577. return 0;
  5578. }
  5579. lockstart = offset;
  5580. lockend = offset + count - 1;
  5581. if (writing) {
  5582. ret = btrfs_delalloc_reserve_space(inode, count);
  5583. if (ret)
  5584. goto out;
  5585. }
  5586. while (1) {
  5587. lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
  5588. 0, &cached_state);
  5589. /*
  5590. * We're concerned with the entire range that we're going to be
  5591. * doing DIO to, so we need to make sure theres no ordered
  5592. * extents in this range.
  5593. */
  5594. ordered = btrfs_lookup_ordered_range(inode, lockstart,
  5595. lockend - lockstart + 1);
  5596. if (!ordered)
  5597. break;
  5598. unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
  5599. &cached_state, GFP_NOFS);
  5600. btrfs_start_ordered_extent(inode, ordered, 1);
  5601. btrfs_put_ordered_extent(ordered);
  5602. cond_resched();
  5603. }
  5604. /*
  5605. * we don't use btrfs_set_extent_delalloc because we don't want
  5606. * the dirty or uptodate bits
  5607. */
  5608. if (writing) {
  5609. write_bits = EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING;
  5610. ret = set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
  5611. EXTENT_DELALLOC, NULL, &cached_state,
  5612. GFP_NOFS);
  5613. if (ret) {
  5614. clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
  5615. lockend, EXTENT_LOCKED | write_bits,
  5616. 1, 0, &cached_state, GFP_NOFS);
  5617. goto out;
  5618. }
  5619. }
  5620. free_extent_state(cached_state);
  5621. cached_state = NULL;
  5622. ret = __blockdev_direct_IO(rw, iocb, inode,
  5623. BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev,
  5624. iov, offset, nr_segs, btrfs_get_blocks_direct, NULL,
  5625. btrfs_submit_direct, 0);
  5626. if (ret < 0 && ret != -EIOCBQUEUED) {
  5627. clear_extent_bit(&BTRFS_I(inode)->io_tree, offset,
  5628. offset + iov_length(iov, nr_segs) - 1,
  5629. EXTENT_LOCKED | write_bits, 1, 0,
  5630. &cached_state, GFP_NOFS);
  5631. } else if (ret >= 0 && ret < iov_length(iov, nr_segs)) {
  5632. /*
  5633. * We're falling back to buffered, unlock the section we didn't
  5634. * do IO on.
  5635. */
  5636. clear_extent_bit(&BTRFS_I(inode)->io_tree, offset + ret,
  5637. offset + iov_length(iov, nr_segs) - 1,
  5638. EXTENT_LOCKED | write_bits, 1, 0,
  5639. &cached_state, GFP_NOFS);
  5640. }
  5641. out:
  5642. free_extent_state(cached_state);
  5643. return ret;
  5644. }
  5645. static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
  5646. __u64 start, __u64 len)
  5647. {
  5648. return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
  5649. }
  5650. int btrfs_readpage(struct file *file, struct page *page)
  5651. {
  5652. struct extent_io_tree *tree;
  5653. tree = &BTRFS_I(page->mapping->host)->io_tree;
  5654. return extent_read_full_page(tree, page, btrfs_get_extent, 0);
  5655. }
  5656. static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
  5657. {
  5658. struct extent_io_tree *tree;
  5659. if (current->flags & PF_MEMALLOC) {
  5660. redirty_page_for_writepage(wbc, page);
  5661. unlock_page(page);
  5662. return 0;
  5663. }
  5664. tree = &BTRFS_I(page->mapping->host)->io_tree;
  5665. return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
  5666. }
  5667. int btrfs_writepages(struct address_space *mapping,
  5668. struct writeback_control *wbc)
  5669. {
  5670. struct extent_io_tree *tree;
  5671. tree = &BTRFS_I(mapping->host)->io_tree;
  5672. return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
  5673. }
  5674. static int
  5675. btrfs_readpages(struct file *file, struct address_space *mapping,
  5676. struct list_head *pages, unsigned nr_pages)
  5677. {
  5678. struct extent_io_tree *tree;
  5679. tree = &BTRFS_I(mapping->host)->io_tree;
  5680. return extent_readpages(tree, mapping, pages, nr_pages,
  5681. btrfs_get_extent);
  5682. }
  5683. static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
  5684. {
  5685. struct extent_io_tree *tree;
  5686. struct extent_map_tree *map;
  5687. int ret;
  5688. tree = &BTRFS_I(page->mapping->host)->io_tree;
  5689. map = &BTRFS_I(page->mapping->host)->extent_tree;
  5690. ret = try_release_extent_mapping(map, tree, page, gfp_flags);
  5691. if (ret == 1) {
  5692. ClearPagePrivate(page);
  5693. set_page_private(page, 0);
  5694. page_cache_release(page);
  5695. }
  5696. return ret;
  5697. }
  5698. static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
  5699. {
  5700. if (PageWriteback(page) || PageDirty(page))
  5701. return 0;
  5702. return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
  5703. }
  5704. static void btrfs_invalidatepage(struct page *page, unsigned long offset)
  5705. {
  5706. struct inode *inode = page->mapping->host;
  5707. struct extent_io_tree *tree;
  5708. struct btrfs_ordered_extent *ordered;
  5709. struct extent_state *cached_state = NULL;
  5710. u64 page_start = page_offset(page);
  5711. u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
  5712. /*
  5713. * we have the page locked, so new writeback can't start,
  5714. * and the dirty bit won't be cleared while we are here.
  5715. *
  5716. * Wait for IO on this page so that we can safely clear
  5717. * the PagePrivate2 bit and do ordered accounting
  5718. */
  5719. wait_on_page_writeback(page);
  5720. tree = &BTRFS_I(inode)->io_tree;
  5721. if (offset) {
  5722. btrfs_releasepage(page, GFP_NOFS);
  5723. return;
  5724. }
  5725. lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
  5726. ordered = btrfs_lookup_ordered_extent(inode,
  5727. page_offset(page));
  5728. if (ordered) {
  5729. /*
  5730. * IO on this page will never be started, so we need
  5731. * to account for any ordered extents now
  5732. */
  5733. clear_extent_bit(tree, page_start, page_end,
  5734. EXTENT_DIRTY | EXTENT_DELALLOC |
  5735. EXTENT_LOCKED | EXTENT_DO_ACCOUNTING, 1, 0,
  5736. &cached_state, GFP_NOFS);
  5737. /*
  5738. * whoever cleared the private bit is responsible
  5739. * for the finish_ordered_io
  5740. */
  5741. if (TestClearPagePrivate2(page) &&
  5742. btrfs_dec_test_ordered_pending(inode, &ordered, page_start,
  5743. PAGE_CACHE_SIZE, 1)) {
  5744. btrfs_finish_ordered_io(ordered);
  5745. }
  5746. btrfs_put_ordered_extent(ordered);
  5747. cached_state = NULL;
  5748. lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
  5749. }
  5750. clear_extent_bit(tree, page_start, page_end,
  5751. EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
  5752. EXTENT_DO_ACCOUNTING, 1, 1, &cached_state, GFP_NOFS);
  5753. __btrfs_releasepage(page, GFP_NOFS);
  5754. ClearPageChecked(page);
  5755. if (PagePrivate(page)) {
  5756. ClearPagePrivate(page);
  5757. set_page_private(page, 0);
  5758. page_cache_release(page);
  5759. }
  5760. }
  5761. /*
  5762. * btrfs_page_mkwrite() is not allowed to change the file size as it gets
  5763. * called from a page fault handler when a page is first dirtied. Hence we must
  5764. * be careful to check for EOF conditions here. We set the page up correctly
  5765. * for a written page which means we get ENOSPC checking when writing into
  5766. * holes and correct delalloc and unwritten extent mapping on filesystems that
  5767. * support these features.
  5768. *
  5769. * We are not allowed to take the i_mutex here so we have to play games to
  5770. * protect against truncate races as the page could now be beyond EOF. Because
  5771. * vmtruncate() writes the inode size before removing pages, once we have the
  5772. * page lock we can determine safely if the page is beyond EOF. If it is not
  5773. * beyond EOF, then the page is guaranteed safe against truncation until we
  5774. * unlock the page.
  5775. */
  5776. int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
  5777. {
  5778. struct page *page = vmf->page;
  5779. struct inode *inode = fdentry(vma->vm_file)->d_inode;
  5780. struct btrfs_root *root = BTRFS_I(inode)->root;
  5781. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  5782. struct btrfs_ordered_extent *ordered;
  5783. struct extent_state *cached_state = NULL;
  5784. char *kaddr;
  5785. unsigned long zero_start;
  5786. loff_t size;
  5787. int ret;
  5788. int reserved = 0;
  5789. u64 page_start;
  5790. u64 page_end;
  5791. ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
  5792. if (!ret) {
  5793. ret = btrfs_update_time(vma->vm_file);
  5794. reserved = 1;
  5795. }
  5796. if (ret) {
  5797. if (ret == -ENOMEM)
  5798. ret = VM_FAULT_OOM;
  5799. else /* -ENOSPC, -EIO, etc */
  5800. ret = VM_FAULT_SIGBUS;
  5801. if (reserved)
  5802. goto out;
  5803. goto out_noreserve;
  5804. }
  5805. ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
  5806. again:
  5807. lock_page(page);
  5808. size = i_size_read(inode);
  5809. page_start = page_offset(page);
  5810. page_end = page_start + PAGE_CACHE_SIZE - 1;
  5811. if ((page->mapping != inode->i_mapping) ||
  5812. (page_start >= size)) {
  5813. /* page got truncated out from underneath us */
  5814. goto out_unlock;
  5815. }
  5816. wait_on_page_writeback(page);
  5817. lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
  5818. set_page_extent_mapped(page);
  5819. /*
  5820. * we can't set the delalloc bits if there are pending ordered
  5821. * extents. Drop our locks and wait for them to finish
  5822. */
  5823. ordered = btrfs_lookup_ordered_extent(inode, page_start);
  5824. if (ordered) {
  5825. unlock_extent_cached(io_tree, page_start, page_end,
  5826. &cached_state, GFP_NOFS);
  5827. unlock_page(page);
  5828. btrfs_start_ordered_extent(inode, ordered, 1);
  5829. btrfs_put_ordered_extent(ordered);
  5830. goto again;
  5831. }
  5832. /*
  5833. * XXX - page_mkwrite gets called every time the page is dirtied, even
  5834. * if it was already dirty, so for space accounting reasons we need to
  5835. * clear any delalloc bits for the range we are fixing to save. There
  5836. * is probably a better way to do this, but for now keep consistent with
  5837. * prepare_pages in the normal write path.
  5838. */
  5839. clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
  5840. EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING,
  5841. 0, 0, &cached_state, GFP_NOFS);
  5842. ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
  5843. &cached_state);
  5844. if (ret) {
  5845. unlock_extent_cached(io_tree, page_start, page_end,
  5846. &cached_state, GFP_NOFS);
  5847. ret = VM_FAULT_SIGBUS;
  5848. goto out_unlock;
  5849. }
  5850. ret = 0;
  5851. /* page is wholly or partially inside EOF */
  5852. if (page_start + PAGE_CACHE_SIZE > size)
  5853. zero_start = size & ~PAGE_CACHE_MASK;
  5854. else
  5855. zero_start = PAGE_CACHE_SIZE;
  5856. if (zero_start != PAGE_CACHE_SIZE) {
  5857. kaddr = kmap(page);
  5858. memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
  5859. flush_dcache_page(page);
  5860. kunmap(page);
  5861. }
  5862. ClearPageChecked(page);
  5863. set_page_dirty(page);
  5864. SetPageUptodate(page);
  5865. BTRFS_I(inode)->last_trans = root->fs_info->generation;
  5866. BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
  5867. unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
  5868. out_unlock:
  5869. if (!ret)
  5870. return VM_FAULT_LOCKED;
  5871. unlock_page(page);
  5872. out:
  5873. btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
  5874. out_noreserve:
  5875. return ret;
  5876. }
  5877. static int btrfs_truncate(struct inode *inode)
  5878. {
  5879. struct btrfs_root *root = BTRFS_I(inode)->root;
  5880. struct btrfs_block_rsv *rsv;
  5881. int ret;
  5882. int err = 0;
  5883. struct btrfs_trans_handle *trans;
  5884. unsigned long nr;
  5885. u64 mask = root->sectorsize - 1;
  5886. u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
  5887. ret = btrfs_truncate_page(inode->i_mapping, inode->i_size);
  5888. if (ret)
  5889. return ret;
  5890. btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
  5891. btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
  5892. /*
  5893. * Yes ladies and gentelment, this is indeed ugly. The fact is we have
  5894. * 3 things going on here
  5895. *
  5896. * 1) We need to reserve space for our orphan item and the space to
  5897. * delete our orphan item. Lord knows we don't want to have a dangling
  5898. * orphan item because we didn't reserve space to remove it.
  5899. *
  5900. * 2) We need to reserve space to update our inode.
  5901. *
  5902. * 3) We need to have something to cache all the space that is going to
  5903. * be free'd up by the truncate operation, but also have some slack
  5904. * space reserved in case it uses space during the truncate (thank you
  5905. * very much snapshotting).
  5906. *
  5907. * And we need these to all be seperate. The fact is we can use alot of
  5908. * space doing the truncate, and we have no earthly idea how much space
  5909. * we will use, so we need the truncate reservation to be seperate so it
  5910. * doesn't end up using space reserved for updating the inode or
  5911. * removing the orphan item. We also need to be able to stop the
  5912. * transaction and start a new one, which means we need to be able to
  5913. * update the inode several times, and we have no idea of knowing how
  5914. * many times that will be, so we can't just reserve 1 item for the
  5915. * entirety of the opration, so that has to be done seperately as well.
  5916. * Then there is the orphan item, which does indeed need to be held on
  5917. * to for the whole operation, and we need nobody to touch this reserved
  5918. * space except the orphan code.
  5919. *
  5920. * So that leaves us with
  5921. *
  5922. * 1) root->orphan_block_rsv - for the orphan deletion.
  5923. * 2) rsv - for the truncate reservation, which we will steal from the
  5924. * transaction reservation.
  5925. * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
  5926. * updating the inode.
  5927. */
  5928. rsv = btrfs_alloc_block_rsv(root);
  5929. if (!rsv)
  5930. return -ENOMEM;
  5931. rsv->size = min_size;
  5932. /*
  5933. * 1 for the truncate slack space
  5934. * 1 for the orphan item we're going to add
  5935. * 1 for the orphan item deletion
  5936. * 1 for updating the inode.
  5937. */
  5938. trans = btrfs_start_transaction(root, 4);
  5939. if (IS_ERR(trans)) {
  5940. err = PTR_ERR(trans);
  5941. goto out;
  5942. }
  5943. /* Migrate the slack space for the truncate to our reserve */
  5944. ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv,
  5945. min_size);
  5946. BUG_ON(ret);
  5947. ret = btrfs_orphan_add(trans, inode);
  5948. if (ret) {
  5949. btrfs_end_transaction(trans, root);
  5950. goto out;
  5951. }
  5952. /*
  5953. * setattr is responsible for setting the ordered_data_close flag,
  5954. * but that is only tested during the last file release. That
  5955. * could happen well after the next commit, leaving a great big
  5956. * window where new writes may get lost if someone chooses to write
  5957. * to this file after truncating to zero
  5958. *
  5959. * The inode doesn't have any dirty data here, and so if we commit
  5960. * this is a noop. If someone immediately starts writing to the inode
  5961. * it is very likely we'll catch some of their writes in this
  5962. * transaction, and the commit will find this file on the ordered
  5963. * data list with good things to send down.
  5964. *
  5965. * This is a best effort solution, there is still a window where
  5966. * using truncate to replace the contents of the file will
  5967. * end up with a zero length file after a crash.
  5968. */
  5969. if (inode->i_size == 0 && BTRFS_I(inode)->ordered_data_close)
  5970. btrfs_add_ordered_operation(trans, root, inode);
  5971. while (1) {
  5972. ret = btrfs_block_rsv_refill(root, rsv, min_size);
  5973. if (ret) {
  5974. /*
  5975. * This can only happen with the original transaction we
  5976. * started above, every other time we shouldn't have a
  5977. * transaction started yet.
  5978. */
  5979. if (ret == -EAGAIN)
  5980. goto end_trans;
  5981. err = ret;
  5982. break;
  5983. }
  5984. if (!trans) {
  5985. /* Just need the 1 for updating the inode */
  5986. trans = btrfs_start_transaction(root, 1);
  5987. if (IS_ERR(trans)) {
  5988. ret = err = PTR_ERR(trans);
  5989. trans = NULL;
  5990. break;
  5991. }
  5992. }
  5993. trans->block_rsv = rsv;
  5994. ret = btrfs_truncate_inode_items(trans, root, inode,
  5995. inode->i_size,
  5996. BTRFS_EXTENT_DATA_KEY);
  5997. if (ret != -EAGAIN) {
  5998. err = ret;
  5999. break;
  6000. }
  6001. trans->block_rsv = &root->fs_info->trans_block_rsv;
  6002. ret = btrfs_update_inode(trans, root, inode);
  6003. if (ret) {
  6004. err = ret;
  6005. break;
  6006. }
  6007. end_trans:
  6008. nr = trans->blocks_used;
  6009. btrfs_end_transaction(trans, root);
  6010. trans = NULL;
  6011. btrfs_btree_balance_dirty(root, nr);
  6012. }
  6013. if (ret == 0 && inode->i_nlink > 0) {
  6014. trans->block_rsv = root->orphan_block_rsv;
  6015. ret = btrfs_orphan_del(trans, inode);
  6016. if (ret)
  6017. err = ret;
  6018. } else if (ret && inode->i_nlink > 0) {
  6019. /*
  6020. * Failed to do the truncate, remove us from the in memory
  6021. * orphan list.
  6022. */
  6023. ret = btrfs_orphan_del(NULL, inode);
  6024. }
  6025. if (trans) {
  6026. trans->block_rsv = &root->fs_info->trans_block_rsv;
  6027. ret = btrfs_update_inode(trans, root, inode);
  6028. if (ret && !err)
  6029. err = ret;
  6030. nr = trans->blocks_used;
  6031. ret = btrfs_end_transaction(trans, root);
  6032. btrfs_btree_balance_dirty(root, nr);
  6033. }
  6034. out:
  6035. btrfs_free_block_rsv(root, rsv);
  6036. if (ret && !err)
  6037. err = ret;
  6038. return err;
  6039. }
  6040. /*
  6041. * create a new subvolume directory/inode (helper for the ioctl).
  6042. */
  6043. int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
  6044. struct btrfs_root *new_root, u64 new_dirid)
  6045. {
  6046. struct inode *inode;
  6047. int err;
  6048. u64 index = 0;
  6049. inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
  6050. new_dirid, new_dirid,
  6051. S_IFDIR | (~current_umask() & S_IRWXUGO),
  6052. &index);
  6053. if (IS_ERR(inode))
  6054. return PTR_ERR(inode);
  6055. inode->i_op = &btrfs_dir_inode_operations;
  6056. inode->i_fop = &btrfs_dir_file_operations;
  6057. set_nlink(inode, 1);
  6058. btrfs_i_size_write(inode, 0);
  6059. err = btrfs_update_inode(trans, new_root, inode);
  6060. iput(inode);
  6061. return err;
  6062. }
  6063. struct inode *btrfs_alloc_inode(struct super_block *sb)
  6064. {
  6065. struct btrfs_inode *ei;
  6066. struct inode *inode;
  6067. ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
  6068. if (!ei)
  6069. return NULL;
  6070. ei->root = NULL;
  6071. ei->space_info = NULL;
  6072. ei->generation = 0;
  6073. ei->last_trans = 0;
  6074. ei->last_sub_trans = 0;
  6075. ei->logged_trans = 0;
  6076. ei->delalloc_bytes = 0;
  6077. ei->disk_i_size = 0;
  6078. ei->flags = 0;
  6079. ei->csum_bytes = 0;
  6080. ei->index_cnt = (u64)-1;
  6081. ei->last_unlink_trans = 0;
  6082. spin_lock_init(&ei->lock);
  6083. ei->outstanding_extents = 0;
  6084. ei->reserved_extents = 0;
  6085. ei->ordered_data_close = 0;
  6086. ei->orphan_meta_reserved = 0;
  6087. ei->dummy_inode = 0;
  6088. ei->in_defrag = 0;
  6089. ei->delalloc_meta_reserved = 0;
  6090. ei->force_compress = BTRFS_COMPRESS_NONE;
  6091. ei->delayed_node = NULL;
  6092. inode = &ei->vfs_inode;
  6093. extent_map_tree_init(&ei->extent_tree);
  6094. extent_io_tree_init(&ei->io_tree, &inode->i_data);
  6095. extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
  6096. ei->io_tree.track_uptodate = 1;
  6097. ei->io_failure_tree.track_uptodate = 1;
  6098. mutex_init(&ei->log_mutex);
  6099. mutex_init(&ei->delalloc_mutex);
  6100. btrfs_ordered_inode_tree_init(&ei->ordered_tree);
  6101. INIT_LIST_HEAD(&ei->i_orphan);
  6102. INIT_LIST_HEAD(&ei->delalloc_inodes);
  6103. INIT_LIST_HEAD(&ei->ordered_operations);
  6104. RB_CLEAR_NODE(&ei->rb_node);
  6105. return inode;
  6106. }
  6107. static void btrfs_i_callback(struct rcu_head *head)
  6108. {
  6109. struct inode *inode = container_of(head, struct inode, i_rcu);
  6110. kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
  6111. }
  6112. void btrfs_destroy_inode(struct inode *inode)
  6113. {
  6114. struct btrfs_ordered_extent *ordered;
  6115. struct btrfs_root *root = BTRFS_I(inode)->root;
  6116. WARN_ON(!list_empty(&inode->i_dentry));
  6117. WARN_ON(inode->i_data.nrpages);
  6118. WARN_ON(BTRFS_I(inode)->outstanding_extents);
  6119. WARN_ON(BTRFS_I(inode)->reserved_extents);
  6120. WARN_ON(BTRFS_I(inode)->delalloc_bytes);
  6121. WARN_ON(BTRFS_I(inode)->csum_bytes);
  6122. /*
  6123. * This can happen where we create an inode, but somebody else also
  6124. * created the same inode and we need to destroy the one we already
  6125. * created.
  6126. */
  6127. if (!root)
  6128. goto free;
  6129. /*
  6130. * Make sure we're properly removed from the ordered operation
  6131. * lists.
  6132. */
  6133. smp_mb();
  6134. if (!list_empty(&BTRFS_I(inode)->ordered_operations)) {
  6135. spin_lock(&root->fs_info->ordered_extent_lock);
  6136. list_del_init(&BTRFS_I(inode)->ordered_operations);
  6137. spin_unlock(&root->fs_info->ordered_extent_lock);
  6138. }
  6139. spin_lock(&root->orphan_lock);
  6140. if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
  6141. printk(KERN_INFO "BTRFS: inode %llu still on the orphan list\n",
  6142. (unsigned long long)btrfs_ino(inode));
  6143. list_del_init(&BTRFS_I(inode)->i_orphan);
  6144. }
  6145. spin_unlock(&root->orphan_lock);
  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. };