inode.c 196 KB

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