inode.c 197 KB

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