inode.c 283 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673
  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/compat.h>
  33. #include <linux/bit_spinlock.h>
  34. #include <linux/xattr.h>
  35. #include <linux/posix_acl.h>
  36. #include <linux/falloc.h>
  37. #include <linux/slab.h>
  38. #include <linux/ratelimit.h>
  39. #include <linux/mount.h>
  40. #include <linux/btrfs.h>
  41. #include <linux/blkdev.h>
  42. #include <linux/posix_acl_xattr.h>
  43. #include <linux/uio.h>
  44. #include "ctree.h"
  45. #include "disk-io.h"
  46. #include "transaction.h"
  47. #include "btrfs_inode.h"
  48. #include "print-tree.h"
  49. #include "ordered-data.h"
  50. #include "xattr.h"
  51. #include "tree-log.h"
  52. #include "volumes.h"
  53. #include "compression.h"
  54. #include "locking.h"
  55. #include "free-space-cache.h"
  56. #include "inode-map.h"
  57. #include "backref.h"
  58. #include "hash.h"
  59. #include "props.h"
  60. #include "qgroup.h"
  61. #include "dedupe.h"
  62. struct btrfs_iget_args {
  63. struct btrfs_key *location;
  64. struct btrfs_root *root;
  65. };
  66. struct btrfs_dio_data {
  67. u64 outstanding_extents;
  68. u64 reserve;
  69. u64 unsubmitted_oe_range_start;
  70. u64 unsubmitted_oe_range_end;
  71. };
  72. static const struct inode_operations btrfs_dir_inode_operations;
  73. static const struct inode_operations btrfs_symlink_inode_operations;
  74. static const struct inode_operations btrfs_dir_ro_inode_operations;
  75. static const struct inode_operations btrfs_special_inode_operations;
  76. static const struct inode_operations btrfs_file_inode_operations;
  77. static const struct address_space_operations btrfs_aops;
  78. static const struct address_space_operations btrfs_symlink_aops;
  79. static const struct file_operations btrfs_dir_file_operations;
  80. static const struct extent_io_ops btrfs_extent_io_ops;
  81. static struct kmem_cache *btrfs_inode_cachep;
  82. struct kmem_cache *btrfs_trans_handle_cachep;
  83. struct kmem_cache *btrfs_transaction_cachep;
  84. struct kmem_cache *btrfs_path_cachep;
  85. struct kmem_cache *btrfs_free_space_cachep;
  86. #define S_SHIFT 12
  87. static const unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
  88. [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
  89. [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
  90. [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
  91. [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
  92. [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
  93. [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
  94. [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
  95. };
  96. static int btrfs_setsize(struct inode *inode, struct iattr *attr);
  97. static int btrfs_truncate(struct inode *inode);
  98. static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent);
  99. static noinline int cow_file_range(struct inode *inode,
  100. struct page *locked_page,
  101. u64 start, u64 end, u64 delalloc_end,
  102. int *page_started, unsigned long *nr_written,
  103. int unlock, struct btrfs_dedupe_hash *hash);
  104. static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
  105. u64 len, u64 orig_start,
  106. u64 block_start, u64 block_len,
  107. u64 orig_block_len, u64 ram_bytes,
  108. int type);
  109. static int btrfs_dirty_inode(struct inode *inode);
  110. #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
  111. void btrfs_test_inode_set_ops(struct inode *inode)
  112. {
  113. BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
  114. }
  115. #endif
  116. static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
  117. struct inode *inode, struct inode *dir,
  118. const struct qstr *qstr)
  119. {
  120. int err;
  121. err = btrfs_init_acl(trans, inode, dir);
  122. if (!err)
  123. err = btrfs_xattr_security_init(trans, inode, dir, qstr);
  124. return err;
  125. }
  126. /*
  127. * this does all the hard work for inserting an inline extent into
  128. * the btree. The caller should have done a btrfs_drop_extents so that
  129. * no overlapping inline items exist in the btree
  130. */
  131. static int insert_inline_extent(struct btrfs_trans_handle *trans,
  132. struct btrfs_path *path, int extent_inserted,
  133. struct btrfs_root *root, struct inode *inode,
  134. u64 start, size_t size, size_t compressed_size,
  135. int compress_type,
  136. struct page **compressed_pages)
  137. {
  138. struct extent_buffer *leaf;
  139. struct page *page = NULL;
  140. char *kaddr;
  141. unsigned long ptr;
  142. struct btrfs_file_extent_item *ei;
  143. int err = 0;
  144. int ret;
  145. size_t cur_size = size;
  146. unsigned long offset;
  147. if (compressed_size && compressed_pages)
  148. cur_size = compressed_size;
  149. inode_add_bytes(inode, size);
  150. if (!extent_inserted) {
  151. struct btrfs_key key;
  152. size_t datasize;
  153. key.objectid = btrfs_ino(inode);
  154. key.offset = start;
  155. key.type = BTRFS_EXTENT_DATA_KEY;
  156. datasize = btrfs_file_extent_calc_inline_size(cur_size);
  157. path->leave_spinning = 1;
  158. ret = btrfs_insert_empty_item(trans, root, path, &key,
  159. datasize);
  160. if (ret) {
  161. err = ret;
  162. goto fail;
  163. }
  164. }
  165. leaf = path->nodes[0];
  166. ei = btrfs_item_ptr(leaf, path->slots[0],
  167. struct btrfs_file_extent_item);
  168. btrfs_set_file_extent_generation(leaf, ei, trans->transid);
  169. btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
  170. btrfs_set_file_extent_encryption(leaf, ei, 0);
  171. btrfs_set_file_extent_other_encoding(leaf, ei, 0);
  172. btrfs_set_file_extent_ram_bytes(leaf, ei, size);
  173. ptr = btrfs_file_extent_inline_start(ei);
  174. if (compress_type != BTRFS_COMPRESS_NONE) {
  175. struct page *cpage;
  176. int i = 0;
  177. while (compressed_size > 0) {
  178. cpage = compressed_pages[i];
  179. cur_size = min_t(unsigned long, compressed_size,
  180. PAGE_SIZE);
  181. kaddr = kmap_atomic(cpage);
  182. write_extent_buffer(leaf, kaddr, ptr, cur_size);
  183. kunmap_atomic(kaddr);
  184. i++;
  185. ptr += cur_size;
  186. compressed_size -= cur_size;
  187. }
  188. btrfs_set_file_extent_compression(leaf, ei,
  189. compress_type);
  190. } else {
  191. page = find_get_page(inode->i_mapping,
  192. start >> PAGE_SHIFT);
  193. btrfs_set_file_extent_compression(leaf, ei, 0);
  194. kaddr = kmap_atomic(page);
  195. offset = start & (PAGE_SIZE - 1);
  196. write_extent_buffer(leaf, kaddr + offset, ptr, size);
  197. kunmap_atomic(kaddr);
  198. put_page(page);
  199. }
  200. btrfs_mark_buffer_dirty(leaf);
  201. btrfs_release_path(path);
  202. /*
  203. * we're an inline extent, so nobody can
  204. * extend the file past i_size without locking
  205. * a page we already have locked.
  206. *
  207. * We must do any isize and inode updates
  208. * before we unlock the pages. Otherwise we
  209. * could end up racing with unlink.
  210. */
  211. BTRFS_I(inode)->disk_i_size = inode->i_size;
  212. ret = btrfs_update_inode(trans, root, inode);
  213. return ret;
  214. fail:
  215. return err;
  216. }
  217. /*
  218. * conditionally insert an inline extent into the file. This
  219. * does the checks required to make sure the data is small enough
  220. * to fit as an inline extent.
  221. */
  222. static noinline int cow_file_range_inline(struct btrfs_root *root,
  223. struct inode *inode, u64 start,
  224. u64 end, size_t compressed_size,
  225. int compress_type,
  226. struct page **compressed_pages)
  227. {
  228. struct btrfs_fs_info *fs_info = root->fs_info;
  229. struct btrfs_trans_handle *trans;
  230. u64 isize = i_size_read(inode);
  231. u64 actual_end = min(end + 1, isize);
  232. u64 inline_len = actual_end - start;
  233. u64 aligned_end = ALIGN(end, fs_info->sectorsize);
  234. u64 data_len = inline_len;
  235. int ret;
  236. struct btrfs_path *path;
  237. int extent_inserted = 0;
  238. u32 extent_item_size;
  239. if (compressed_size)
  240. data_len = compressed_size;
  241. if (start > 0 ||
  242. actual_end > fs_info->sectorsize ||
  243. data_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info) ||
  244. (!compressed_size &&
  245. (actual_end & (fs_info->sectorsize - 1)) == 0) ||
  246. end + 1 < isize ||
  247. data_len > fs_info->max_inline) {
  248. return 1;
  249. }
  250. path = btrfs_alloc_path();
  251. if (!path)
  252. return -ENOMEM;
  253. trans = btrfs_join_transaction(root);
  254. if (IS_ERR(trans)) {
  255. btrfs_free_path(path);
  256. return PTR_ERR(trans);
  257. }
  258. trans->block_rsv = &fs_info->delalloc_block_rsv;
  259. if (compressed_size && compressed_pages)
  260. extent_item_size = btrfs_file_extent_calc_inline_size(
  261. compressed_size);
  262. else
  263. extent_item_size = btrfs_file_extent_calc_inline_size(
  264. inline_len);
  265. ret = __btrfs_drop_extents(trans, root, inode, path,
  266. start, aligned_end, NULL,
  267. 1, 1, extent_item_size, &extent_inserted);
  268. if (ret) {
  269. btrfs_abort_transaction(trans, ret);
  270. goto out;
  271. }
  272. if (isize > actual_end)
  273. inline_len = min_t(u64, isize, actual_end);
  274. ret = insert_inline_extent(trans, path, extent_inserted,
  275. root, inode, start,
  276. inline_len, compressed_size,
  277. compress_type, compressed_pages);
  278. if (ret && ret != -ENOSPC) {
  279. btrfs_abort_transaction(trans, ret);
  280. goto out;
  281. } else if (ret == -ENOSPC) {
  282. ret = 1;
  283. goto out;
  284. }
  285. set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
  286. btrfs_delalloc_release_metadata(inode, end + 1 - start);
  287. btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0);
  288. out:
  289. /*
  290. * Don't forget to free the reserved space, as for inlined extent
  291. * it won't count as data extent, free them directly here.
  292. * And at reserve time, it's always aligned to page size, so
  293. * just free one page here.
  294. */
  295. btrfs_qgroup_free_data(inode, 0, PAGE_SIZE);
  296. btrfs_free_path(path);
  297. btrfs_end_transaction(trans);
  298. return ret;
  299. }
  300. struct async_extent {
  301. u64 start;
  302. u64 ram_size;
  303. u64 compressed_size;
  304. struct page **pages;
  305. unsigned long nr_pages;
  306. int compress_type;
  307. struct list_head list;
  308. };
  309. struct async_cow {
  310. struct inode *inode;
  311. struct btrfs_root *root;
  312. struct page *locked_page;
  313. u64 start;
  314. u64 end;
  315. struct list_head extents;
  316. struct btrfs_work work;
  317. };
  318. static noinline int add_async_extent(struct async_cow *cow,
  319. u64 start, u64 ram_size,
  320. u64 compressed_size,
  321. struct page **pages,
  322. unsigned long nr_pages,
  323. int compress_type)
  324. {
  325. struct async_extent *async_extent;
  326. async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
  327. BUG_ON(!async_extent); /* -ENOMEM */
  328. async_extent->start = start;
  329. async_extent->ram_size = ram_size;
  330. async_extent->compressed_size = compressed_size;
  331. async_extent->pages = pages;
  332. async_extent->nr_pages = nr_pages;
  333. async_extent->compress_type = compress_type;
  334. list_add_tail(&async_extent->list, &cow->extents);
  335. return 0;
  336. }
  337. static inline int inode_need_compress(struct inode *inode)
  338. {
  339. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  340. /* force compress */
  341. if (btrfs_test_opt(fs_info, FORCE_COMPRESS))
  342. return 1;
  343. /* bad compression ratios */
  344. if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
  345. return 0;
  346. if (btrfs_test_opt(fs_info, COMPRESS) ||
  347. BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS ||
  348. BTRFS_I(inode)->force_compress)
  349. return 1;
  350. return 0;
  351. }
  352. /*
  353. * we create compressed extents in two phases. The first
  354. * phase compresses a range of pages that have already been
  355. * locked (both pages and state bits are locked).
  356. *
  357. * This is done inside an ordered work queue, and the compression
  358. * is spread across many cpus. The actual IO submission is step
  359. * two, and the ordered work queue takes care of making sure that
  360. * happens in the same order things were put onto the queue by
  361. * writepages and friends.
  362. *
  363. * If this code finds it can't get good compression, it puts an
  364. * entry onto the work queue to write the uncompressed bytes. This
  365. * makes sure that both compressed inodes and uncompressed inodes
  366. * are written in the same order that the flusher thread sent them
  367. * down.
  368. */
  369. static noinline void compress_file_range(struct inode *inode,
  370. struct page *locked_page,
  371. u64 start, u64 end,
  372. struct async_cow *async_cow,
  373. int *num_added)
  374. {
  375. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  376. struct btrfs_root *root = BTRFS_I(inode)->root;
  377. u64 num_bytes;
  378. u64 blocksize = fs_info->sectorsize;
  379. u64 actual_end;
  380. u64 isize = i_size_read(inode);
  381. int ret = 0;
  382. struct page **pages = NULL;
  383. unsigned long nr_pages;
  384. unsigned long nr_pages_ret = 0;
  385. unsigned long total_compressed = 0;
  386. unsigned long total_in = 0;
  387. unsigned long max_compressed = SZ_128K;
  388. unsigned long max_uncompressed = SZ_128K;
  389. int i;
  390. int will_compress;
  391. int compress_type = fs_info->compress_type;
  392. int redirty = 0;
  393. /* if this is a small write inside eof, kick off a defrag */
  394. if ((end - start + 1) < SZ_16K &&
  395. (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
  396. btrfs_add_inode_defrag(NULL, inode);
  397. actual_end = min_t(u64, isize, end + 1);
  398. again:
  399. will_compress = 0;
  400. nr_pages = (end >> PAGE_SHIFT) - (start >> PAGE_SHIFT) + 1;
  401. nr_pages = min_t(unsigned long, nr_pages, SZ_128K / PAGE_SIZE);
  402. /*
  403. * we don't want to send crud past the end of i_size through
  404. * compression, that's just a waste of CPU time. So, if the
  405. * end of the file is before the start of our current
  406. * requested range of bytes, we bail out to the uncompressed
  407. * cleanup code that can deal with all of this.
  408. *
  409. * It isn't really the fastest way to fix things, but this is a
  410. * very uncommon corner.
  411. */
  412. if (actual_end <= start)
  413. goto cleanup_and_bail_uncompressed;
  414. total_compressed = actual_end - start;
  415. /*
  416. * skip compression for a small file range(<=blocksize) that
  417. * isn't an inline extent, since it doesn't save disk space at all.
  418. */
  419. if (total_compressed <= blocksize &&
  420. (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
  421. goto cleanup_and_bail_uncompressed;
  422. /* we want to make sure that amount of ram required to uncompress
  423. * an extent is reasonable, so we limit the total size in ram
  424. * of a compressed extent to 128k. This is a crucial number
  425. * because it also controls how easily we can spread reads across
  426. * cpus for decompression.
  427. *
  428. * We also want to make sure the amount of IO required to do
  429. * a random read is reasonably small, so we limit the size of
  430. * a compressed extent to 128k.
  431. */
  432. total_compressed = min(total_compressed, max_uncompressed);
  433. num_bytes = ALIGN(end - start + 1, blocksize);
  434. num_bytes = max(blocksize, num_bytes);
  435. total_in = 0;
  436. ret = 0;
  437. /*
  438. * we do compression for mount -o compress and when the
  439. * inode has not been flagged as nocompress. This flag can
  440. * change at any time if we discover bad compression ratios.
  441. */
  442. if (inode_need_compress(inode)) {
  443. WARN_ON(pages);
  444. pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS);
  445. if (!pages) {
  446. /* just bail out to the uncompressed code */
  447. goto cont;
  448. }
  449. if (BTRFS_I(inode)->force_compress)
  450. compress_type = BTRFS_I(inode)->force_compress;
  451. /*
  452. * we need to call clear_page_dirty_for_io on each
  453. * page in the range. Otherwise applications with the file
  454. * mmap'd can wander in and change the page contents while
  455. * we are compressing them.
  456. *
  457. * If the compression fails for any reason, we set the pages
  458. * dirty again later on.
  459. */
  460. extent_range_clear_dirty_for_io(inode, start, end);
  461. redirty = 1;
  462. ret = btrfs_compress_pages(compress_type,
  463. inode->i_mapping, start,
  464. total_compressed, pages,
  465. nr_pages, &nr_pages_ret,
  466. &total_in,
  467. &total_compressed,
  468. max_compressed);
  469. if (!ret) {
  470. unsigned long offset = total_compressed &
  471. (PAGE_SIZE - 1);
  472. struct page *page = pages[nr_pages_ret - 1];
  473. char *kaddr;
  474. /* zero the tail end of the last page, we might be
  475. * sending it down to disk
  476. */
  477. if (offset) {
  478. kaddr = kmap_atomic(page);
  479. memset(kaddr + offset, 0,
  480. PAGE_SIZE - offset);
  481. kunmap_atomic(kaddr);
  482. }
  483. will_compress = 1;
  484. }
  485. }
  486. cont:
  487. if (start == 0) {
  488. /* lets try to make an inline extent */
  489. if (ret || total_in < (actual_end - start)) {
  490. /* we didn't compress the entire range, try
  491. * to make an uncompressed inline extent.
  492. */
  493. ret = cow_file_range_inline(root, inode, start, end,
  494. 0, 0, NULL);
  495. } else {
  496. /* try making a compressed inline extent */
  497. ret = cow_file_range_inline(root, inode, start, end,
  498. total_compressed,
  499. compress_type, pages);
  500. }
  501. if (ret <= 0) {
  502. unsigned long clear_flags = EXTENT_DELALLOC |
  503. EXTENT_DEFRAG;
  504. unsigned long page_error_op;
  505. clear_flags |= (ret < 0) ? EXTENT_DO_ACCOUNTING : 0;
  506. page_error_op = ret < 0 ? PAGE_SET_ERROR : 0;
  507. /*
  508. * inline extent creation worked or returned error,
  509. * we don't need to create any more async work items.
  510. * Unlock and free up our temp pages.
  511. */
  512. extent_clear_unlock_delalloc(inode, start, end, end,
  513. NULL, clear_flags,
  514. PAGE_UNLOCK |
  515. PAGE_CLEAR_DIRTY |
  516. PAGE_SET_WRITEBACK |
  517. page_error_op |
  518. PAGE_END_WRITEBACK);
  519. btrfs_free_reserved_data_space_noquota(inode, start,
  520. end - start + 1);
  521. goto free_pages_out;
  522. }
  523. }
  524. if (will_compress) {
  525. /*
  526. * we aren't doing an inline extent round the compressed size
  527. * up to a block size boundary so the allocator does sane
  528. * things
  529. */
  530. total_compressed = ALIGN(total_compressed, blocksize);
  531. /*
  532. * one last check to make sure the compression is really a
  533. * win, compare the page count read with the blocks on disk
  534. */
  535. total_in = ALIGN(total_in, PAGE_SIZE);
  536. if (total_compressed >= total_in) {
  537. will_compress = 0;
  538. } else {
  539. num_bytes = total_in;
  540. *num_added += 1;
  541. /*
  542. * The async work queues will take care of doing actual
  543. * allocation on disk for these compressed pages, and
  544. * will submit them to the elevator.
  545. */
  546. add_async_extent(async_cow, start, num_bytes,
  547. total_compressed, pages, nr_pages_ret,
  548. compress_type);
  549. if (start + num_bytes < end) {
  550. start += num_bytes;
  551. pages = NULL;
  552. cond_resched();
  553. goto again;
  554. }
  555. return;
  556. }
  557. }
  558. if (pages) {
  559. /*
  560. * the compression code ran but failed to make things smaller,
  561. * free any pages it allocated and our page pointer array
  562. */
  563. for (i = 0; i < nr_pages_ret; i++) {
  564. WARN_ON(pages[i]->mapping);
  565. put_page(pages[i]);
  566. }
  567. kfree(pages);
  568. pages = NULL;
  569. total_compressed = 0;
  570. nr_pages_ret = 0;
  571. /* flag the file so we don't compress in the future */
  572. if (!btrfs_test_opt(fs_info, FORCE_COMPRESS) &&
  573. !(BTRFS_I(inode)->force_compress)) {
  574. BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
  575. }
  576. }
  577. cleanup_and_bail_uncompressed:
  578. /*
  579. * No compression, but we still need to write the pages in the file
  580. * we've been given so far. redirty the locked page if it corresponds
  581. * to our extent and set things up for the async work queue to run
  582. * cow_file_range to do the normal delalloc dance.
  583. */
  584. if (page_offset(locked_page) >= start &&
  585. page_offset(locked_page) <= end)
  586. __set_page_dirty_nobuffers(locked_page);
  587. /* unlocked later on in the async handlers */
  588. if (redirty)
  589. extent_range_redirty_for_io(inode, start, end);
  590. add_async_extent(async_cow, start, end - start + 1, 0, NULL, 0,
  591. BTRFS_COMPRESS_NONE);
  592. *num_added += 1;
  593. return;
  594. free_pages_out:
  595. for (i = 0; i < nr_pages_ret; i++) {
  596. WARN_ON(pages[i]->mapping);
  597. put_page(pages[i]);
  598. }
  599. kfree(pages);
  600. }
  601. static void free_async_extent_pages(struct async_extent *async_extent)
  602. {
  603. int i;
  604. if (!async_extent->pages)
  605. return;
  606. for (i = 0; i < async_extent->nr_pages; i++) {
  607. WARN_ON(async_extent->pages[i]->mapping);
  608. put_page(async_extent->pages[i]);
  609. }
  610. kfree(async_extent->pages);
  611. async_extent->nr_pages = 0;
  612. async_extent->pages = NULL;
  613. }
  614. /*
  615. * phase two of compressed writeback. This is the ordered portion
  616. * of the code, which only gets called in the order the work was
  617. * queued. We walk all the async extents created by compress_file_range
  618. * and send them down to the disk.
  619. */
  620. static noinline void submit_compressed_extents(struct inode *inode,
  621. struct async_cow *async_cow)
  622. {
  623. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  624. struct async_extent *async_extent;
  625. u64 alloc_hint = 0;
  626. struct btrfs_key ins;
  627. struct extent_map *em;
  628. struct btrfs_root *root = BTRFS_I(inode)->root;
  629. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  630. struct extent_io_tree *io_tree;
  631. int ret = 0;
  632. again:
  633. while (!list_empty(&async_cow->extents)) {
  634. async_extent = list_entry(async_cow->extents.next,
  635. struct async_extent, list);
  636. list_del(&async_extent->list);
  637. io_tree = &BTRFS_I(inode)->io_tree;
  638. retry:
  639. /* did the compression code fall back to uncompressed IO? */
  640. if (!async_extent->pages) {
  641. int page_started = 0;
  642. unsigned long nr_written = 0;
  643. lock_extent(io_tree, async_extent->start,
  644. async_extent->start +
  645. async_extent->ram_size - 1);
  646. /* allocate blocks */
  647. ret = cow_file_range(inode, async_cow->locked_page,
  648. async_extent->start,
  649. async_extent->start +
  650. async_extent->ram_size - 1,
  651. async_extent->start +
  652. async_extent->ram_size - 1,
  653. &page_started, &nr_written, 0,
  654. NULL);
  655. /* JDM XXX */
  656. /*
  657. * if page_started, cow_file_range inserted an
  658. * inline extent and took care of all the unlocking
  659. * and IO for us. Otherwise, we need to submit
  660. * all those pages down to the drive.
  661. */
  662. if (!page_started && !ret)
  663. extent_write_locked_range(io_tree,
  664. inode, async_extent->start,
  665. async_extent->start +
  666. async_extent->ram_size - 1,
  667. btrfs_get_extent,
  668. WB_SYNC_ALL);
  669. else if (ret)
  670. unlock_page(async_cow->locked_page);
  671. kfree(async_extent);
  672. cond_resched();
  673. continue;
  674. }
  675. lock_extent(io_tree, async_extent->start,
  676. async_extent->start + async_extent->ram_size - 1);
  677. ret = btrfs_reserve_extent(root, async_extent->ram_size,
  678. async_extent->compressed_size,
  679. async_extent->compressed_size,
  680. 0, alloc_hint, &ins, 1, 1);
  681. if (ret) {
  682. free_async_extent_pages(async_extent);
  683. if (ret == -ENOSPC) {
  684. unlock_extent(io_tree, async_extent->start,
  685. async_extent->start +
  686. async_extent->ram_size - 1);
  687. /*
  688. * we need to redirty the pages if we decide to
  689. * fallback to uncompressed IO, otherwise we
  690. * will not submit these pages down to lower
  691. * layers.
  692. */
  693. extent_range_redirty_for_io(inode,
  694. async_extent->start,
  695. async_extent->start +
  696. async_extent->ram_size - 1);
  697. goto retry;
  698. }
  699. goto out_free;
  700. }
  701. /*
  702. * here we're doing allocation and writeback of the
  703. * compressed pages
  704. */
  705. btrfs_drop_extent_cache(inode, async_extent->start,
  706. async_extent->start +
  707. async_extent->ram_size - 1, 0);
  708. em = alloc_extent_map();
  709. if (!em) {
  710. ret = -ENOMEM;
  711. goto out_free_reserve;
  712. }
  713. em->start = async_extent->start;
  714. em->len = async_extent->ram_size;
  715. em->orig_start = em->start;
  716. em->mod_start = em->start;
  717. em->mod_len = em->len;
  718. em->block_start = ins.objectid;
  719. em->block_len = ins.offset;
  720. em->orig_block_len = ins.offset;
  721. em->ram_bytes = async_extent->ram_size;
  722. em->bdev = fs_info->fs_devices->latest_bdev;
  723. em->compress_type = async_extent->compress_type;
  724. set_bit(EXTENT_FLAG_PINNED, &em->flags);
  725. set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
  726. em->generation = -1;
  727. while (1) {
  728. write_lock(&em_tree->lock);
  729. ret = add_extent_mapping(em_tree, em, 1);
  730. write_unlock(&em_tree->lock);
  731. if (ret != -EEXIST) {
  732. free_extent_map(em);
  733. break;
  734. }
  735. btrfs_drop_extent_cache(inode, async_extent->start,
  736. async_extent->start +
  737. async_extent->ram_size - 1, 0);
  738. }
  739. if (ret)
  740. goto out_free_reserve;
  741. ret = btrfs_add_ordered_extent_compress(inode,
  742. async_extent->start,
  743. ins.objectid,
  744. async_extent->ram_size,
  745. ins.offset,
  746. BTRFS_ORDERED_COMPRESSED,
  747. async_extent->compress_type);
  748. if (ret) {
  749. btrfs_drop_extent_cache(inode, async_extent->start,
  750. async_extent->start +
  751. async_extent->ram_size - 1, 0);
  752. goto out_free_reserve;
  753. }
  754. btrfs_dec_block_group_reservations(fs_info, ins.objectid);
  755. /*
  756. * clear dirty, set writeback and unlock the pages.
  757. */
  758. extent_clear_unlock_delalloc(inode, async_extent->start,
  759. async_extent->start +
  760. async_extent->ram_size - 1,
  761. async_extent->start +
  762. async_extent->ram_size - 1,
  763. NULL, EXTENT_LOCKED | EXTENT_DELALLOC,
  764. PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
  765. PAGE_SET_WRITEBACK);
  766. ret = btrfs_submit_compressed_write(inode,
  767. async_extent->start,
  768. async_extent->ram_size,
  769. ins.objectid,
  770. ins.offset, async_extent->pages,
  771. async_extent->nr_pages);
  772. if (ret) {
  773. struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
  774. struct page *p = async_extent->pages[0];
  775. const u64 start = async_extent->start;
  776. const u64 end = start + async_extent->ram_size - 1;
  777. p->mapping = inode->i_mapping;
  778. tree->ops->writepage_end_io_hook(p, start, end,
  779. NULL, 0);
  780. p->mapping = NULL;
  781. extent_clear_unlock_delalloc(inode, start, end, end,
  782. NULL, 0,
  783. PAGE_END_WRITEBACK |
  784. PAGE_SET_ERROR);
  785. free_async_extent_pages(async_extent);
  786. }
  787. alloc_hint = ins.objectid + ins.offset;
  788. kfree(async_extent);
  789. cond_resched();
  790. }
  791. return;
  792. out_free_reserve:
  793. btrfs_dec_block_group_reservations(fs_info, ins.objectid);
  794. btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
  795. out_free:
  796. extent_clear_unlock_delalloc(inode, async_extent->start,
  797. async_extent->start +
  798. async_extent->ram_size - 1,
  799. async_extent->start +
  800. async_extent->ram_size - 1,
  801. NULL, EXTENT_LOCKED | EXTENT_DELALLOC |
  802. EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING,
  803. PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
  804. PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK |
  805. PAGE_SET_ERROR);
  806. free_async_extent_pages(async_extent);
  807. kfree(async_extent);
  808. goto again;
  809. }
  810. static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
  811. u64 num_bytes)
  812. {
  813. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  814. struct extent_map *em;
  815. u64 alloc_hint = 0;
  816. read_lock(&em_tree->lock);
  817. em = search_extent_mapping(em_tree, start, num_bytes);
  818. if (em) {
  819. /*
  820. * if block start isn't an actual block number then find the
  821. * first block in this inode and use that as a hint. If that
  822. * block is also bogus then just don't worry about it.
  823. */
  824. if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
  825. free_extent_map(em);
  826. em = search_extent_mapping(em_tree, 0, 0);
  827. if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
  828. alloc_hint = em->block_start;
  829. if (em)
  830. free_extent_map(em);
  831. } else {
  832. alloc_hint = em->block_start;
  833. free_extent_map(em);
  834. }
  835. }
  836. read_unlock(&em_tree->lock);
  837. return alloc_hint;
  838. }
  839. /*
  840. * when extent_io.c finds a delayed allocation range in the file,
  841. * the call backs end up in this code. The basic idea is to
  842. * allocate extents on disk for the range, and create ordered data structs
  843. * in ram to track those extents.
  844. *
  845. * locked_page is the page that writepage had locked already. We use
  846. * it to make sure we don't do extra locks or unlocks.
  847. *
  848. * *page_started is set to one if we unlock locked_page and do everything
  849. * required to start IO on it. It may be clean and already done with
  850. * IO when we return.
  851. */
  852. static noinline int cow_file_range(struct inode *inode,
  853. struct page *locked_page,
  854. u64 start, u64 end, u64 delalloc_end,
  855. int *page_started, unsigned long *nr_written,
  856. int unlock, struct btrfs_dedupe_hash *hash)
  857. {
  858. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  859. struct btrfs_root *root = BTRFS_I(inode)->root;
  860. u64 alloc_hint = 0;
  861. u64 num_bytes;
  862. unsigned long ram_size;
  863. u64 disk_num_bytes;
  864. u64 cur_alloc_size;
  865. u64 blocksize = fs_info->sectorsize;
  866. struct btrfs_key ins;
  867. struct extent_map *em;
  868. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  869. int ret = 0;
  870. if (btrfs_is_free_space_inode(inode)) {
  871. WARN_ON_ONCE(1);
  872. ret = -EINVAL;
  873. goto out_unlock;
  874. }
  875. num_bytes = ALIGN(end - start + 1, blocksize);
  876. num_bytes = max(blocksize, num_bytes);
  877. disk_num_bytes = num_bytes;
  878. /* if this is a small write inside eof, kick off defrag */
  879. if (num_bytes < SZ_64K &&
  880. (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
  881. btrfs_add_inode_defrag(NULL, inode);
  882. if (start == 0) {
  883. /* lets try to make an inline extent */
  884. ret = cow_file_range_inline(root, inode, start, end, 0, 0,
  885. NULL);
  886. if (ret == 0) {
  887. extent_clear_unlock_delalloc(inode, start, end,
  888. delalloc_end, NULL,
  889. EXTENT_LOCKED | EXTENT_DELALLOC |
  890. EXTENT_DEFRAG, PAGE_UNLOCK |
  891. PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
  892. PAGE_END_WRITEBACK);
  893. btrfs_free_reserved_data_space_noquota(inode, start,
  894. end - start + 1);
  895. *nr_written = *nr_written +
  896. (end - start + PAGE_SIZE) / PAGE_SIZE;
  897. *page_started = 1;
  898. goto out;
  899. } else if (ret < 0) {
  900. goto out_unlock;
  901. }
  902. }
  903. BUG_ON(disk_num_bytes >
  904. btrfs_super_total_bytes(fs_info->super_copy));
  905. alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
  906. btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
  907. while (disk_num_bytes > 0) {
  908. unsigned long op;
  909. cur_alloc_size = disk_num_bytes;
  910. ret = btrfs_reserve_extent(root, cur_alloc_size, cur_alloc_size,
  911. fs_info->sectorsize, 0, alloc_hint,
  912. &ins, 1, 1);
  913. if (ret < 0)
  914. goto out_unlock;
  915. em = alloc_extent_map();
  916. if (!em) {
  917. ret = -ENOMEM;
  918. goto out_reserve;
  919. }
  920. em->start = start;
  921. em->orig_start = em->start;
  922. ram_size = ins.offset;
  923. em->len = ins.offset;
  924. em->mod_start = em->start;
  925. em->mod_len = em->len;
  926. em->block_start = ins.objectid;
  927. em->block_len = ins.offset;
  928. em->orig_block_len = ins.offset;
  929. em->ram_bytes = ram_size;
  930. em->bdev = fs_info->fs_devices->latest_bdev;
  931. set_bit(EXTENT_FLAG_PINNED, &em->flags);
  932. em->generation = -1;
  933. while (1) {
  934. write_lock(&em_tree->lock);
  935. ret = add_extent_mapping(em_tree, em, 1);
  936. write_unlock(&em_tree->lock);
  937. if (ret != -EEXIST) {
  938. free_extent_map(em);
  939. break;
  940. }
  941. btrfs_drop_extent_cache(inode, start,
  942. start + ram_size - 1, 0);
  943. }
  944. if (ret)
  945. goto out_reserve;
  946. cur_alloc_size = ins.offset;
  947. ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
  948. ram_size, cur_alloc_size, 0);
  949. if (ret)
  950. goto out_drop_extent_cache;
  951. if (root->root_key.objectid ==
  952. BTRFS_DATA_RELOC_TREE_OBJECTID) {
  953. ret = btrfs_reloc_clone_csums(inode, start,
  954. cur_alloc_size);
  955. if (ret)
  956. goto out_drop_extent_cache;
  957. }
  958. btrfs_dec_block_group_reservations(fs_info, ins.objectid);
  959. if (disk_num_bytes < cur_alloc_size)
  960. break;
  961. /* we're not doing compressed IO, don't unlock the first
  962. * page (which the caller expects to stay locked), don't
  963. * clear any dirty bits and don't set any writeback bits
  964. *
  965. * Do set the Private2 bit so we know this page was properly
  966. * setup for writepage
  967. */
  968. op = unlock ? PAGE_UNLOCK : 0;
  969. op |= PAGE_SET_PRIVATE2;
  970. extent_clear_unlock_delalloc(inode, start,
  971. start + ram_size - 1,
  972. delalloc_end, locked_page,
  973. EXTENT_LOCKED | EXTENT_DELALLOC,
  974. op);
  975. disk_num_bytes -= cur_alloc_size;
  976. num_bytes -= cur_alloc_size;
  977. alloc_hint = ins.objectid + ins.offset;
  978. start += cur_alloc_size;
  979. }
  980. out:
  981. return ret;
  982. out_drop_extent_cache:
  983. btrfs_drop_extent_cache(inode, start, start + ram_size - 1, 0);
  984. out_reserve:
  985. btrfs_dec_block_group_reservations(fs_info, ins.objectid);
  986. btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
  987. out_unlock:
  988. extent_clear_unlock_delalloc(inode, start, end, delalloc_end,
  989. locked_page,
  990. EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
  991. EXTENT_DELALLOC | EXTENT_DEFRAG,
  992. PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
  993. PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK);
  994. goto out;
  995. }
  996. /*
  997. * work queue call back to started compression on a file and pages
  998. */
  999. static noinline void async_cow_start(struct btrfs_work *work)
  1000. {
  1001. struct async_cow *async_cow;
  1002. int num_added = 0;
  1003. async_cow = container_of(work, struct async_cow, work);
  1004. compress_file_range(async_cow->inode, async_cow->locked_page,
  1005. async_cow->start, async_cow->end, async_cow,
  1006. &num_added);
  1007. if (num_added == 0) {
  1008. btrfs_add_delayed_iput(async_cow->inode);
  1009. async_cow->inode = NULL;
  1010. }
  1011. }
  1012. /*
  1013. * work queue call back to submit previously compressed pages
  1014. */
  1015. static noinline void async_cow_submit(struct btrfs_work *work)
  1016. {
  1017. struct btrfs_fs_info *fs_info;
  1018. struct async_cow *async_cow;
  1019. struct btrfs_root *root;
  1020. unsigned long nr_pages;
  1021. async_cow = container_of(work, struct async_cow, work);
  1022. root = async_cow->root;
  1023. fs_info = root->fs_info;
  1024. nr_pages = (async_cow->end - async_cow->start + PAGE_SIZE) >>
  1025. PAGE_SHIFT;
  1026. /*
  1027. * atomic_sub_return implies a barrier for waitqueue_active
  1028. */
  1029. if (atomic_sub_return(nr_pages, &fs_info->async_delalloc_pages) <
  1030. 5 * SZ_1M &&
  1031. waitqueue_active(&fs_info->async_submit_wait))
  1032. wake_up(&fs_info->async_submit_wait);
  1033. if (async_cow->inode)
  1034. submit_compressed_extents(async_cow->inode, async_cow);
  1035. }
  1036. static noinline void async_cow_free(struct btrfs_work *work)
  1037. {
  1038. struct async_cow *async_cow;
  1039. async_cow = container_of(work, struct async_cow, work);
  1040. if (async_cow->inode)
  1041. btrfs_add_delayed_iput(async_cow->inode);
  1042. kfree(async_cow);
  1043. }
  1044. static int cow_file_range_async(struct inode *inode, struct page *locked_page,
  1045. u64 start, u64 end, int *page_started,
  1046. unsigned long *nr_written)
  1047. {
  1048. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  1049. struct async_cow *async_cow;
  1050. struct btrfs_root *root = BTRFS_I(inode)->root;
  1051. unsigned long nr_pages;
  1052. u64 cur_end;
  1053. int limit = 10 * SZ_1M;
  1054. clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
  1055. 1, 0, NULL, GFP_NOFS);
  1056. while (start < end) {
  1057. async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
  1058. BUG_ON(!async_cow); /* -ENOMEM */
  1059. async_cow->inode = igrab(inode);
  1060. async_cow->root = root;
  1061. async_cow->locked_page = locked_page;
  1062. async_cow->start = start;
  1063. if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS &&
  1064. !btrfs_test_opt(fs_info, FORCE_COMPRESS))
  1065. cur_end = end;
  1066. else
  1067. cur_end = min(end, start + SZ_512K - 1);
  1068. async_cow->end = cur_end;
  1069. INIT_LIST_HEAD(&async_cow->extents);
  1070. btrfs_init_work(&async_cow->work,
  1071. btrfs_delalloc_helper,
  1072. async_cow_start, async_cow_submit,
  1073. async_cow_free);
  1074. nr_pages = (cur_end - start + PAGE_SIZE) >>
  1075. PAGE_SHIFT;
  1076. atomic_add(nr_pages, &fs_info->async_delalloc_pages);
  1077. btrfs_queue_work(fs_info->delalloc_workers, &async_cow->work);
  1078. if (atomic_read(&fs_info->async_delalloc_pages) > limit) {
  1079. wait_event(fs_info->async_submit_wait,
  1080. (atomic_read(&fs_info->async_delalloc_pages) <
  1081. limit));
  1082. }
  1083. while (atomic_read(&fs_info->async_submit_draining) &&
  1084. atomic_read(&fs_info->async_delalloc_pages)) {
  1085. wait_event(fs_info->async_submit_wait,
  1086. (atomic_read(&fs_info->async_delalloc_pages) ==
  1087. 0));
  1088. }
  1089. *nr_written += nr_pages;
  1090. start = cur_end + 1;
  1091. }
  1092. *page_started = 1;
  1093. return 0;
  1094. }
  1095. static noinline int csum_exist_in_range(struct btrfs_fs_info *fs_info,
  1096. u64 bytenr, u64 num_bytes)
  1097. {
  1098. int ret;
  1099. struct btrfs_ordered_sum *sums;
  1100. LIST_HEAD(list);
  1101. ret = btrfs_lookup_csums_range(fs_info->csum_root, bytenr,
  1102. bytenr + num_bytes - 1, &list, 0);
  1103. if (ret == 0 && list_empty(&list))
  1104. return 0;
  1105. while (!list_empty(&list)) {
  1106. sums = list_entry(list.next, struct btrfs_ordered_sum, list);
  1107. list_del(&sums->list);
  1108. kfree(sums);
  1109. }
  1110. return 1;
  1111. }
  1112. /*
  1113. * when nowcow writeback call back. This checks for snapshots or COW copies
  1114. * of the extents that exist in the file, and COWs the file as required.
  1115. *
  1116. * If no cow copies or snapshots exist, we write directly to the existing
  1117. * blocks on disk
  1118. */
  1119. static noinline int run_delalloc_nocow(struct inode *inode,
  1120. struct page *locked_page,
  1121. u64 start, u64 end, int *page_started, int force,
  1122. unsigned long *nr_written)
  1123. {
  1124. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  1125. struct btrfs_root *root = BTRFS_I(inode)->root;
  1126. struct btrfs_trans_handle *trans;
  1127. struct extent_buffer *leaf;
  1128. struct btrfs_path *path;
  1129. struct btrfs_file_extent_item *fi;
  1130. struct btrfs_key found_key;
  1131. u64 cow_start;
  1132. u64 cur_offset;
  1133. u64 extent_end;
  1134. u64 extent_offset;
  1135. u64 disk_bytenr;
  1136. u64 num_bytes;
  1137. u64 disk_num_bytes;
  1138. u64 ram_bytes;
  1139. int extent_type;
  1140. int ret, err;
  1141. int type;
  1142. int nocow;
  1143. int check_prev = 1;
  1144. bool nolock;
  1145. u64 ino = btrfs_ino(inode);
  1146. path = btrfs_alloc_path();
  1147. if (!path) {
  1148. extent_clear_unlock_delalloc(inode, start, end, end,
  1149. locked_page,
  1150. EXTENT_LOCKED | EXTENT_DELALLOC |
  1151. EXTENT_DO_ACCOUNTING |
  1152. EXTENT_DEFRAG, PAGE_UNLOCK |
  1153. PAGE_CLEAR_DIRTY |
  1154. PAGE_SET_WRITEBACK |
  1155. PAGE_END_WRITEBACK);
  1156. return -ENOMEM;
  1157. }
  1158. nolock = btrfs_is_free_space_inode(inode);
  1159. if (nolock)
  1160. trans = btrfs_join_transaction_nolock(root);
  1161. else
  1162. trans = btrfs_join_transaction(root);
  1163. if (IS_ERR(trans)) {
  1164. extent_clear_unlock_delalloc(inode, start, end, end,
  1165. locked_page,
  1166. EXTENT_LOCKED | EXTENT_DELALLOC |
  1167. EXTENT_DO_ACCOUNTING |
  1168. EXTENT_DEFRAG, PAGE_UNLOCK |
  1169. PAGE_CLEAR_DIRTY |
  1170. PAGE_SET_WRITEBACK |
  1171. PAGE_END_WRITEBACK);
  1172. btrfs_free_path(path);
  1173. return PTR_ERR(trans);
  1174. }
  1175. trans->block_rsv = &fs_info->delalloc_block_rsv;
  1176. cow_start = (u64)-1;
  1177. cur_offset = start;
  1178. while (1) {
  1179. ret = btrfs_lookup_file_extent(trans, root, path, ino,
  1180. cur_offset, 0);
  1181. if (ret < 0)
  1182. goto error;
  1183. if (ret > 0 && path->slots[0] > 0 && check_prev) {
  1184. leaf = path->nodes[0];
  1185. btrfs_item_key_to_cpu(leaf, &found_key,
  1186. path->slots[0] - 1);
  1187. if (found_key.objectid == ino &&
  1188. found_key.type == BTRFS_EXTENT_DATA_KEY)
  1189. path->slots[0]--;
  1190. }
  1191. check_prev = 0;
  1192. next_slot:
  1193. leaf = path->nodes[0];
  1194. if (path->slots[0] >= btrfs_header_nritems(leaf)) {
  1195. ret = btrfs_next_leaf(root, path);
  1196. if (ret < 0)
  1197. goto error;
  1198. if (ret > 0)
  1199. break;
  1200. leaf = path->nodes[0];
  1201. }
  1202. nocow = 0;
  1203. disk_bytenr = 0;
  1204. num_bytes = 0;
  1205. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  1206. if (found_key.objectid > ino)
  1207. break;
  1208. if (WARN_ON_ONCE(found_key.objectid < ino) ||
  1209. found_key.type < BTRFS_EXTENT_DATA_KEY) {
  1210. path->slots[0]++;
  1211. goto next_slot;
  1212. }
  1213. if (found_key.type > BTRFS_EXTENT_DATA_KEY ||
  1214. found_key.offset > end)
  1215. break;
  1216. if (found_key.offset > cur_offset) {
  1217. extent_end = found_key.offset;
  1218. extent_type = 0;
  1219. goto out_check;
  1220. }
  1221. fi = btrfs_item_ptr(leaf, path->slots[0],
  1222. struct btrfs_file_extent_item);
  1223. extent_type = btrfs_file_extent_type(leaf, fi);
  1224. ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
  1225. if (extent_type == BTRFS_FILE_EXTENT_REG ||
  1226. extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
  1227. disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
  1228. extent_offset = btrfs_file_extent_offset(leaf, fi);
  1229. extent_end = found_key.offset +
  1230. btrfs_file_extent_num_bytes(leaf, fi);
  1231. disk_num_bytes =
  1232. btrfs_file_extent_disk_num_bytes(leaf, fi);
  1233. if (extent_end <= start) {
  1234. path->slots[0]++;
  1235. goto next_slot;
  1236. }
  1237. if (disk_bytenr == 0)
  1238. goto out_check;
  1239. if (btrfs_file_extent_compression(leaf, fi) ||
  1240. btrfs_file_extent_encryption(leaf, fi) ||
  1241. btrfs_file_extent_other_encoding(leaf, fi))
  1242. goto out_check;
  1243. if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
  1244. goto out_check;
  1245. if (btrfs_extent_readonly(fs_info, disk_bytenr))
  1246. goto out_check;
  1247. if (btrfs_cross_ref_exist(trans, root, ino,
  1248. found_key.offset -
  1249. extent_offset, disk_bytenr))
  1250. goto out_check;
  1251. disk_bytenr += extent_offset;
  1252. disk_bytenr += cur_offset - found_key.offset;
  1253. num_bytes = min(end + 1, extent_end) - cur_offset;
  1254. /*
  1255. * if there are pending snapshots for this root,
  1256. * we fall into common COW way.
  1257. */
  1258. if (!nolock) {
  1259. err = btrfs_start_write_no_snapshoting(root);
  1260. if (!err)
  1261. goto out_check;
  1262. }
  1263. /*
  1264. * force cow if csum exists in the range.
  1265. * this ensure that csum for a given extent are
  1266. * either valid or do not exist.
  1267. */
  1268. if (csum_exist_in_range(fs_info, disk_bytenr,
  1269. num_bytes))
  1270. goto out_check;
  1271. if (!btrfs_inc_nocow_writers(fs_info, disk_bytenr))
  1272. goto out_check;
  1273. nocow = 1;
  1274. } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
  1275. extent_end = found_key.offset +
  1276. btrfs_file_extent_inline_len(leaf,
  1277. path->slots[0], fi);
  1278. extent_end = ALIGN(extent_end,
  1279. fs_info->sectorsize);
  1280. } else {
  1281. BUG_ON(1);
  1282. }
  1283. out_check:
  1284. if (extent_end <= start) {
  1285. path->slots[0]++;
  1286. if (!nolock && nocow)
  1287. btrfs_end_write_no_snapshoting(root);
  1288. if (nocow)
  1289. btrfs_dec_nocow_writers(fs_info, disk_bytenr);
  1290. goto next_slot;
  1291. }
  1292. if (!nocow) {
  1293. if (cow_start == (u64)-1)
  1294. cow_start = cur_offset;
  1295. cur_offset = extent_end;
  1296. if (cur_offset > end)
  1297. break;
  1298. path->slots[0]++;
  1299. goto next_slot;
  1300. }
  1301. btrfs_release_path(path);
  1302. if (cow_start != (u64)-1) {
  1303. ret = cow_file_range(inode, locked_page,
  1304. cow_start, found_key.offset - 1,
  1305. end, page_started, nr_written, 1,
  1306. NULL);
  1307. if (ret) {
  1308. if (!nolock && nocow)
  1309. btrfs_end_write_no_snapshoting(root);
  1310. if (nocow)
  1311. btrfs_dec_nocow_writers(fs_info,
  1312. disk_bytenr);
  1313. goto error;
  1314. }
  1315. cow_start = (u64)-1;
  1316. }
  1317. if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
  1318. struct extent_map *em;
  1319. struct extent_map_tree *em_tree;
  1320. em_tree = &BTRFS_I(inode)->extent_tree;
  1321. em = alloc_extent_map();
  1322. BUG_ON(!em); /* -ENOMEM */
  1323. em->start = cur_offset;
  1324. em->orig_start = found_key.offset - extent_offset;
  1325. em->len = num_bytes;
  1326. em->block_len = num_bytes;
  1327. em->block_start = disk_bytenr;
  1328. em->orig_block_len = disk_num_bytes;
  1329. em->ram_bytes = ram_bytes;
  1330. em->bdev = fs_info->fs_devices->latest_bdev;
  1331. em->mod_start = em->start;
  1332. em->mod_len = em->len;
  1333. set_bit(EXTENT_FLAG_PINNED, &em->flags);
  1334. set_bit(EXTENT_FLAG_FILLING, &em->flags);
  1335. em->generation = -1;
  1336. while (1) {
  1337. write_lock(&em_tree->lock);
  1338. ret = add_extent_mapping(em_tree, em, 1);
  1339. write_unlock(&em_tree->lock);
  1340. if (ret != -EEXIST) {
  1341. free_extent_map(em);
  1342. break;
  1343. }
  1344. btrfs_drop_extent_cache(inode, em->start,
  1345. em->start + em->len - 1, 0);
  1346. }
  1347. type = BTRFS_ORDERED_PREALLOC;
  1348. } else {
  1349. type = BTRFS_ORDERED_NOCOW;
  1350. }
  1351. ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
  1352. num_bytes, num_bytes, type);
  1353. if (nocow)
  1354. btrfs_dec_nocow_writers(fs_info, disk_bytenr);
  1355. BUG_ON(ret); /* -ENOMEM */
  1356. if (root->root_key.objectid ==
  1357. BTRFS_DATA_RELOC_TREE_OBJECTID) {
  1358. ret = btrfs_reloc_clone_csums(inode, cur_offset,
  1359. num_bytes);
  1360. if (ret) {
  1361. if (!nolock && nocow)
  1362. btrfs_end_write_no_snapshoting(root);
  1363. goto error;
  1364. }
  1365. }
  1366. extent_clear_unlock_delalloc(inode, cur_offset,
  1367. cur_offset + num_bytes - 1, end,
  1368. locked_page, EXTENT_LOCKED |
  1369. EXTENT_DELALLOC |
  1370. EXTENT_CLEAR_DATA_RESV,
  1371. PAGE_UNLOCK | PAGE_SET_PRIVATE2);
  1372. if (!nolock && nocow)
  1373. btrfs_end_write_no_snapshoting(root);
  1374. cur_offset = extent_end;
  1375. if (cur_offset > end)
  1376. break;
  1377. }
  1378. btrfs_release_path(path);
  1379. if (cur_offset <= end && cow_start == (u64)-1) {
  1380. cow_start = cur_offset;
  1381. cur_offset = end;
  1382. }
  1383. if (cow_start != (u64)-1) {
  1384. ret = cow_file_range(inode, locked_page, cow_start, end, end,
  1385. page_started, nr_written, 1, NULL);
  1386. if (ret)
  1387. goto error;
  1388. }
  1389. error:
  1390. err = btrfs_end_transaction(trans);
  1391. if (!ret)
  1392. ret = err;
  1393. if (ret && cur_offset < end)
  1394. extent_clear_unlock_delalloc(inode, cur_offset, end, end,
  1395. locked_page, EXTENT_LOCKED |
  1396. EXTENT_DELALLOC | EXTENT_DEFRAG |
  1397. EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
  1398. PAGE_CLEAR_DIRTY |
  1399. PAGE_SET_WRITEBACK |
  1400. PAGE_END_WRITEBACK);
  1401. btrfs_free_path(path);
  1402. return ret;
  1403. }
  1404. static inline int need_force_cow(struct inode *inode, u64 start, u64 end)
  1405. {
  1406. if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
  1407. !(BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC))
  1408. return 0;
  1409. /*
  1410. * @defrag_bytes is a hint value, no spinlock held here,
  1411. * if is not zero, it means the file is defragging.
  1412. * Force cow if given extent needs to be defragged.
  1413. */
  1414. if (BTRFS_I(inode)->defrag_bytes &&
  1415. test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
  1416. EXTENT_DEFRAG, 0, NULL))
  1417. return 1;
  1418. return 0;
  1419. }
  1420. /*
  1421. * extent_io.c call back to do delayed allocation processing
  1422. */
  1423. static int run_delalloc_range(struct inode *inode, struct page *locked_page,
  1424. u64 start, u64 end, int *page_started,
  1425. unsigned long *nr_written)
  1426. {
  1427. int ret;
  1428. int force_cow = need_force_cow(inode, start, end);
  1429. if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW && !force_cow) {
  1430. ret = run_delalloc_nocow(inode, locked_page, start, end,
  1431. page_started, 1, nr_written);
  1432. } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) {
  1433. ret = run_delalloc_nocow(inode, locked_page, start, end,
  1434. page_started, 0, nr_written);
  1435. } else if (!inode_need_compress(inode)) {
  1436. ret = cow_file_range(inode, locked_page, start, end, end,
  1437. page_started, nr_written, 1, NULL);
  1438. } else {
  1439. set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
  1440. &BTRFS_I(inode)->runtime_flags);
  1441. ret = cow_file_range_async(inode, locked_page, start, end,
  1442. page_started, nr_written);
  1443. }
  1444. return ret;
  1445. }
  1446. static void btrfs_split_extent_hook(struct inode *inode,
  1447. struct extent_state *orig, u64 split)
  1448. {
  1449. u64 size;
  1450. /* not delalloc, ignore it */
  1451. if (!(orig->state & EXTENT_DELALLOC))
  1452. return;
  1453. size = orig->end - orig->start + 1;
  1454. if (size > BTRFS_MAX_EXTENT_SIZE) {
  1455. u64 num_extents;
  1456. u64 new_size;
  1457. /*
  1458. * See the explanation in btrfs_merge_extent_hook, the same
  1459. * applies here, just in reverse.
  1460. */
  1461. new_size = orig->end - split + 1;
  1462. num_extents = div64_u64(new_size + BTRFS_MAX_EXTENT_SIZE - 1,
  1463. BTRFS_MAX_EXTENT_SIZE);
  1464. new_size = split - orig->start;
  1465. num_extents += div64_u64(new_size + BTRFS_MAX_EXTENT_SIZE - 1,
  1466. BTRFS_MAX_EXTENT_SIZE);
  1467. if (div64_u64(size + BTRFS_MAX_EXTENT_SIZE - 1,
  1468. BTRFS_MAX_EXTENT_SIZE) >= num_extents)
  1469. return;
  1470. }
  1471. spin_lock(&BTRFS_I(inode)->lock);
  1472. BTRFS_I(inode)->outstanding_extents++;
  1473. spin_unlock(&BTRFS_I(inode)->lock);
  1474. }
  1475. /*
  1476. * extent_io.c merge_extent_hook, used to track merged delayed allocation
  1477. * extents so we can keep track of new extents that are just merged onto old
  1478. * extents, such as when we are doing sequential writes, so we can properly
  1479. * account for the metadata space we'll need.
  1480. */
  1481. static void btrfs_merge_extent_hook(struct inode *inode,
  1482. struct extent_state *new,
  1483. struct extent_state *other)
  1484. {
  1485. u64 new_size, old_size;
  1486. u64 num_extents;
  1487. /* not delalloc, ignore it */
  1488. if (!(other->state & EXTENT_DELALLOC))
  1489. return;
  1490. if (new->start > other->start)
  1491. new_size = new->end - other->start + 1;
  1492. else
  1493. new_size = other->end - new->start + 1;
  1494. /* we're not bigger than the max, unreserve the space and go */
  1495. if (new_size <= BTRFS_MAX_EXTENT_SIZE) {
  1496. spin_lock(&BTRFS_I(inode)->lock);
  1497. BTRFS_I(inode)->outstanding_extents--;
  1498. spin_unlock(&BTRFS_I(inode)->lock);
  1499. return;
  1500. }
  1501. /*
  1502. * We have to add up either side to figure out how many extents were
  1503. * accounted for before we merged into one big extent. If the number of
  1504. * extents we accounted for is <= the amount we need for the new range
  1505. * then we can return, otherwise drop. Think of it like this
  1506. *
  1507. * [ 4k][MAX_SIZE]
  1508. *
  1509. * So we've grown the extent by a MAX_SIZE extent, this would mean we
  1510. * need 2 outstanding extents, on one side we have 1 and the other side
  1511. * we have 1 so they are == and we can return. But in this case
  1512. *
  1513. * [MAX_SIZE+4k][MAX_SIZE+4k]
  1514. *
  1515. * Each range on their own accounts for 2 extents, but merged together
  1516. * they are only 3 extents worth of accounting, so we need to drop in
  1517. * this case.
  1518. */
  1519. old_size = other->end - other->start + 1;
  1520. num_extents = div64_u64(old_size + BTRFS_MAX_EXTENT_SIZE - 1,
  1521. BTRFS_MAX_EXTENT_SIZE);
  1522. old_size = new->end - new->start + 1;
  1523. num_extents += div64_u64(old_size + BTRFS_MAX_EXTENT_SIZE - 1,
  1524. BTRFS_MAX_EXTENT_SIZE);
  1525. if (div64_u64(new_size + BTRFS_MAX_EXTENT_SIZE - 1,
  1526. BTRFS_MAX_EXTENT_SIZE) >= num_extents)
  1527. return;
  1528. spin_lock(&BTRFS_I(inode)->lock);
  1529. BTRFS_I(inode)->outstanding_extents--;
  1530. spin_unlock(&BTRFS_I(inode)->lock);
  1531. }
  1532. static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
  1533. struct inode *inode)
  1534. {
  1535. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  1536. spin_lock(&root->delalloc_lock);
  1537. if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
  1538. list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
  1539. &root->delalloc_inodes);
  1540. set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
  1541. &BTRFS_I(inode)->runtime_flags);
  1542. root->nr_delalloc_inodes++;
  1543. if (root->nr_delalloc_inodes == 1) {
  1544. spin_lock(&fs_info->delalloc_root_lock);
  1545. BUG_ON(!list_empty(&root->delalloc_root));
  1546. list_add_tail(&root->delalloc_root,
  1547. &fs_info->delalloc_roots);
  1548. spin_unlock(&fs_info->delalloc_root_lock);
  1549. }
  1550. }
  1551. spin_unlock(&root->delalloc_lock);
  1552. }
  1553. static void btrfs_del_delalloc_inode(struct btrfs_root *root,
  1554. struct inode *inode)
  1555. {
  1556. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  1557. spin_lock(&root->delalloc_lock);
  1558. if (!list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
  1559. list_del_init(&BTRFS_I(inode)->delalloc_inodes);
  1560. clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
  1561. &BTRFS_I(inode)->runtime_flags);
  1562. root->nr_delalloc_inodes--;
  1563. if (!root->nr_delalloc_inodes) {
  1564. spin_lock(&fs_info->delalloc_root_lock);
  1565. BUG_ON(list_empty(&root->delalloc_root));
  1566. list_del_init(&root->delalloc_root);
  1567. spin_unlock(&fs_info->delalloc_root_lock);
  1568. }
  1569. }
  1570. spin_unlock(&root->delalloc_lock);
  1571. }
  1572. /*
  1573. * extent_io.c set_bit_hook, used to track delayed allocation
  1574. * bytes in this file, and to maintain the list of inodes that
  1575. * have pending delalloc work to be done.
  1576. */
  1577. static void btrfs_set_bit_hook(struct inode *inode,
  1578. struct extent_state *state, unsigned *bits)
  1579. {
  1580. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  1581. if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC))
  1582. WARN_ON(1);
  1583. /*
  1584. * set_bit and clear bit hooks normally require _irqsave/restore
  1585. * but in this case, we are only testing for the DELALLOC
  1586. * bit, which is only set or cleared with irqs on
  1587. */
  1588. if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
  1589. struct btrfs_root *root = BTRFS_I(inode)->root;
  1590. u64 len = state->end + 1 - state->start;
  1591. bool do_list = !btrfs_is_free_space_inode(inode);
  1592. if (*bits & EXTENT_FIRST_DELALLOC) {
  1593. *bits &= ~EXTENT_FIRST_DELALLOC;
  1594. } else {
  1595. spin_lock(&BTRFS_I(inode)->lock);
  1596. BTRFS_I(inode)->outstanding_extents++;
  1597. spin_unlock(&BTRFS_I(inode)->lock);
  1598. }
  1599. /* For sanity tests */
  1600. if (btrfs_is_testing(fs_info))
  1601. return;
  1602. __percpu_counter_add(&fs_info->delalloc_bytes, len,
  1603. fs_info->delalloc_batch);
  1604. spin_lock(&BTRFS_I(inode)->lock);
  1605. BTRFS_I(inode)->delalloc_bytes += len;
  1606. if (*bits & EXTENT_DEFRAG)
  1607. BTRFS_I(inode)->defrag_bytes += len;
  1608. if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
  1609. &BTRFS_I(inode)->runtime_flags))
  1610. btrfs_add_delalloc_inodes(root, inode);
  1611. spin_unlock(&BTRFS_I(inode)->lock);
  1612. }
  1613. }
  1614. /*
  1615. * extent_io.c clear_bit_hook, see set_bit_hook for why
  1616. */
  1617. static void btrfs_clear_bit_hook(struct inode *inode,
  1618. struct extent_state *state,
  1619. unsigned *bits)
  1620. {
  1621. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  1622. u64 len = state->end + 1 - state->start;
  1623. u64 num_extents = div64_u64(len + BTRFS_MAX_EXTENT_SIZE -1,
  1624. BTRFS_MAX_EXTENT_SIZE);
  1625. spin_lock(&BTRFS_I(inode)->lock);
  1626. if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG))
  1627. BTRFS_I(inode)->defrag_bytes -= len;
  1628. spin_unlock(&BTRFS_I(inode)->lock);
  1629. /*
  1630. * set_bit and clear bit hooks normally require _irqsave/restore
  1631. * but in this case, we are only testing for the DELALLOC
  1632. * bit, which is only set or cleared with irqs on
  1633. */
  1634. if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
  1635. struct btrfs_root *root = BTRFS_I(inode)->root;
  1636. bool do_list = !btrfs_is_free_space_inode(inode);
  1637. if (*bits & EXTENT_FIRST_DELALLOC) {
  1638. *bits &= ~EXTENT_FIRST_DELALLOC;
  1639. } else if (!(*bits & EXTENT_DO_ACCOUNTING)) {
  1640. spin_lock(&BTRFS_I(inode)->lock);
  1641. BTRFS_I(inode)->outstanding_extents -= num_extents;
  1642. spin_unlock(&BTRFS_I(inode)->lock);
  1643. }
  1644. /*
  1645. * We don't reserve metadata space for space cache inodes so we
  1646. * don't need to call dellalloc_release_metadata if there is an
  1647. * error.
  1648. */
  1649. if (*bits & EXTENT_DO_ACCOUNTING &&
  1650. root != fs_info->tree_root)
  1651. btrfs_delalloc_release_metadata(inode, len);
  1652. /* For sanity tests. */
  1653. if (btrfs_is_testing(fs_info))
  1654. return;
  1655. if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
  1656. && do_list && !(state->state & EXTENT_NORESERVE)
  1657. && (*bits & (EXTENT_DO_ACCOUNTING |
  1658. EXTENT_CLEAR_DATA_RESV)))
  1659. btrfs_free_reserved_data_space_noquota(inode,
  1660. state->start, len);
  1661. __percpu_counter_add(&fs_info->delalloc_bytes, -len,
  1662. fs_info->delalloc_batch);
  1663. spin_lock(&BTRFS_I(inode)->lock);
  1664. BTRFS_I(inode)->delalloc_bytes -= len;
  1665. if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 &&
  1666. test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
  1667. &BTRFS_I(inode)->runtime_flags))
  1668. btrfs_del_delalloc_inode(root, inode);
  1669. spin_unlock(&BTRFS_I(inode)->lock);
  1670. }
  1671. }
  1672. /*
  1673. * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
  1674. * we don't create bios that span stripes or chunks
  1675. *
  1676. * return 1 if page cannot be merged to bio
  1677. * return 0 if page can be merged to bio
  1678. * return error otherwise
  1679. */
  1680. int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
  1681. size_t size, struct bio *bio,
  1682. unsigned long bio_flags)
  1683. {
  1684. struct inode *inode = page->mapping->host;
  1685. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  1686. u64 logical = (u64)bio->bi_iter.bi_sector << 9;
  1687. u64 length = 0;
  1688. u64 map_length;
  1689. int ret;
  1690. if (bio_flags & EXTENT_BIO_COMPRESSED)
  1691. return 0;
  1692. length = bio->bi_iter.bi_size;
  1693. map_length = length;
  1694. ret = btrfs_map_block(fs_info, btrfs_op(bio), logical, &map_length,
  1695. NULL, 0);
  1696. if (ret < 0)
  1697. return ret;
  1698. if (map_length < length + size)
  1699. return 1;
  1700. return 0;
  1701. }
  1702. /*
  1703. * in order to insert checksums into the metadata in large chunks,
  1704. * we wait until bio submission time. All the pages in the bio are
  1705. * checksummed and sums are attached onto the ordered extent record.
  1706. *
  1707. * At IO completion time the cums attached on the ordered extent record
  1708. * are inserted into the btree
  1709. */
  1710. static int __btrfs_submit_bio_start(struct inode *inode, struct bio *bio,
  1711. int mirror_num, unsigned long bio_flags,
  1712. u64 bio_offset)
  1713. {
  1714. int ret = 0;
  1715. ret = btrfs_csum_one_bio(inode, bio, 0, 0);
  1716. BUG_ON(ret); /* -ENOMEM */
  1717. return 0;
  1718. }
  1719. /*
  1720. * in order to insert checksums into the metadata in large chunks,
  1721. * we wait until bio submission time. All the pages in the bio are
  1722. * checksummed and sums are attached onto the ordered extent record.
  1723. *
  1724. * At IO completion time the cums attached on the ordered extent record
  1725. * are inserted into the btree
  1726. */
  1727. static int __btrfs_submit_bio_done(struct inode *inode, struct bio *bio,
  1728. int mirror_num, unsigned long bio_flags,
  1729. u64 bio_offset)
  1730. {
  1731. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  1732. int ret;
  1733. ret = btrfs_map_bio(fs_info, bio, mirror_num, 1);
  1734. if (ret) {
  1735. bio->bi_error = ret;
  1736. bio_endio(bio);
  1737. }
  1738. return ret;
  1739. }
  1740. /*
  1741. * extent_io.c submission hook. This does the right thing for csum calculation
  1742. * on write, or reading the csums from the tree before a read
  1743. */
  1744. static int btrfs_submit_bio_hook(struct inode *inode, struct bio *bio,
  1745. int mirror_num, unsigned long bio_flags,
  1746. u64 bio_offset)
  1747. {
  1748. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  1749. struct btrfs_root *root = BTRFS_I(inode)->root;
  1750. enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA;
  1751. int ret = 0;
  1752. int skip_sum;
  1753. int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
  1754. skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
  1755. if (btrfs_is_free_space_inode(inode))
  1756. metadata = BTRFS_WQ_ENDIO_FREE_SPACE;
  1757. if (bio_op(bio) != REQ_OP_WRITE) {
  1758. ret = btrfs_bio_wq_end_io(fs_info, bio, metadata);
  1759. if (ret)
  1760. goto out;
  1761. if (bio_flags & EXTENT_BIO_COMPRESSED) {
  1762. ret = btrfs_submit_compressed_read(inode, bio,
  1763. mirror_num,
  1764. bio_flags);
  1765. goto out;
  1766. } else if (!skip_sum) {
  1767. ret = btrfs_lookup_bio_sums(inode, bio, NULL);
  1768. if (ret)
  1769. goto out;
  1770. }
  1771. goto mapit;
  1772. } else if (async && !skip_sum) {
  1773. /* csum items have already been cloned */
  1774. if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
  1775. goto mapit;
  1776. /* we're doing a write, do the async checksumming */
  1777. ret = btrfs_wq_submit_bio(fs_info, inode, bio, mirror_num,
  1778. bio_flags, bio_offset,
  1779. __btrfs_submit_bio_start,
  1780. __btrfs_submit_bio_done);
  1781. goto out;
  1782. } else if (!skip_sum) {
  1783. ret = btrfs_csum_one_bio(inode, bio, 0, 0);
  1784. if (ret)
  1785. goto out;
  1786. }
  1787. mapit:
  1788. ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
  1789. out:
  1790. if (ret < 0) {
  1791. bio->bi_error = ret;
  1792. bio_endio(bio);
  1793. }
  1794. return ret;
  1795. }
  1796. /*
  1797. * given a list of ordered sums record them in the inode. This happens
  1798. * at IO completion time based on sums calculated at bio submission time.
  1799. */
  1800. static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
  1801. struct inode *inode, u64 file_offset,
  1802. struct list_head *list)
  1803. {
  1804. struct btrfs_ordered_sum *sum;
  1805. list_for_each_entry(sum, list, list) {
  1806. trans->adding_csums = 1;
  1807. btrfs_csum_file_blocks(trans,
  1808. BTRFS_I(inode)->root->fs_info->csum_root, sum);
  1809. trans->adding_csums = 0;
  1810. }
  1811. return 0;
  1812. }
  1813. int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
  1814. struct extent_state **cached_state, int dedupe)
  1815. {
  1816. WARN_ON((end & (PAGE_SIZE - 1)) == 0);
  1817. return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
  1818. cached_state);
  1819. }
  1820. /* see btrfs_writepage_start_hook for details on why this is required */
  1821. struct btrfs_writepage_fixup {
  1822. struct page *page;
  1823. struct btrfs_work work;
  1824. };
  1825. static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
  1826. {
  1827. struct btrfs_writepage_fixup *fixup;
  1828. struct btrfs_ordered_extent *ordered;
  1829. struct extent_state *cached_state = NULL;
  1830. struct page *page;
  1831. struct inode *inode;
  1832. u64 page_start;
  1833. u64 page_end;
  1834. int ret;
  1835. fixup = container_of(work, struct btrfs_writepage_fixup, work);
  1836. page = fixup->page;
  1837. again:
  1838. lock_page(page);
  1839. if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
  1840. ClearPageChecked(page);
  1841. goto out_page;
  1842. }
  1843. inode = page->mapping->host;
  1844. page_start = page_offset(page);
  1845. page_end = page_offset(page) + PAGE_SIZE - 1;
  1846. lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end,
  1847. &cached_state);
  1848. /* already ordered? We're done */
  1849. if (PagePrivate2(page))
  1850. goto out;
  1851. ordered = btrfs_lookup_ordered_range(inode, page_start,
  1852. PAGE_SIZE);
  1853. if (ordered) {
  1854. unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
  1855. page_end, &cached_state, GFP_NOFS);
  1856. unlock_page(page);
  1857. btrfs_start_ordered_extent(inode, ordered, 1);
  1858. btrfs_put_ordered_extent(ordered);
  1859. goto again;
  1860. }
  1861. ret = btrfs_delalloc_reserve_space(inode, page_start,
  1862. PAGE_SIZE);
  1863. if (ret) {
  1864. mapping_set_error(page->mapping, ret);
  1865. end_extent_writepage(page, ret, page_start, page_end);
  1866. ClearPageChecked(page);
  1867. goto out;
  1868. }
  1869. btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state,
  1870. 0);
  1871. ClearPageChecked(page);
  1872. set_page_dirty(page);
  1873. out:
  1874. unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
  1875. &cached_state, GFP_NOFS);
  1876. out_page:
  1877. unlock_page(page);
  1878. put_page(page);
  1879. kfree(fixup);
  1880. }
  1881. /*
  1882. * There are a few paths in the higher layers of the kernel that directly
  1883. * set the page dirty bit without asking the filesystem if it is a
  1884. * good idea. This causes problems because we want to make sure COW
  1885. * properly happens and the data=ordered rules are followed.
  1886. *
  1887. * In our case any range that doesn't have the ORDERED bit set
  1888. * hasn't been properly setup for IO. We kick off an async process
  1889. * to fix it up. The async helper will wait for ordered extents, set
  1890. * the delalloc bit and make it safe to write the page.
  1891. */
  1892. static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
  1893. {
  1894. struct inode *inode = page->mapping->host;
  1895. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  1896. struct btrfs_writepage_fixup *fixup;
  1897. /* this page is properly in the ordered list */
  1898. if (TestClearPagePrivate2(page))
  1899. return 0;
  1900. if (PageChecked(page))
  1901. return -EAGAIN;
  1902. fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
  1903. if (!fixup)
  1904. return -EAGAIN;
  1905. SetPageChecked(page);
  1906. get_page(page);
  1907. btrfs_init_work(&fixup->work, btrfs_fixup_helper,
  1908. btrfs_writepage_fixup_worker, NULL, NULL);
  1909. fixup->page = page;
  1910. btrfs_queue_work(fs_info->fixup_workers, &fixup->work);
  1911. return -EBUSY;
  1912. }
  1913. static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
  1914. struct inode *inode, u64 file_pos,
  1915. u64 disk_bytenr, u64 disk_num_bytes,
  1916. u64 num_bytes, u64 ram_bytes,
  1917. u8 compression, u8 encryption,
  1918. u16 other_encoding, int extent_type)
  1919. {
  1920. struct btrfs_root *root = BTRFS_I(inode)->root;
  1921. struct btrfs_file_extent_item *fi;
  1922. struct btrfs_path *path;
  1923. struct extent_buffer *leaf;
  1924. struct btrfs_key ins;
  1925. int extent_inserted = 0;
  1926. int ret;
  1927. path = btrfs_alloc_path();
  1928. if (!path)
  1929. return -ENOMEM;
  1930. /*
  1931. * we may be replacing one extent in the tree with another.
  1932. * The new extent is pinned in the extent map, and we don't want
  1933. * to drop it from the cache until it is completely in the btree.
  1934. *
  1935. * So, tell btrfs_drop_extents to leave this extent in the cache.
  1936. * the caller is expected to unpin it and allow it to be merged
  1937. * with the others.
  1938. */
  1939. ret = __btrfs_drop_extents(trans, root, inode, path, file_pos,
  1940. file_pos + num_bytes, NULL, 0,
  1941. 1, sizeof(*fi), &extent_inserted);
  1942. if (ret)
  1943. goto out;
  1944. if (!extent_inserted) {
  1945. ins.objectid = btrfs_ino(inode);
  1946. ins.offset = file_pos;
  1947. ins.type = BTRFS_EXTENT_DATA_KEY;
  1948. path->leave_spinning = 1;
  1949. ret = btrfs_insert_empty_item(trans, root, path, &ins,
  1950. sizeof(*fi));
  1951. if (ret)
  1952. goto out;
  1953. }
  1954. leaf = path->nodes[0];
  1955. fi = btrfs_item_ptr(leaf, path->slots[0],
  1956. struct btrfs_file_extent_item);
  1957. btrfs_set_file_extent_generation(leaf, fi, trans->transid);
  1958. btrfs_set_file_extent_type(leaf, fi, extent_type);
  1959. btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
  1960. btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
  1961. btrfs_set_file_extent_offset(leaf, fi, 0);
  1962. btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
  1963. btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
  1964. btrfs_set_file_extent_compression(leaf, fi, compression);
  1965. btrfs_set_file_extent_encryption(leaf, fi, encryption);
  1966. btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
  1967. btrfs_mark_buffer_dirty(leaf);
  1968. btrfs_release_path(path);
  1969. inode_add_bytes(inode, num_bytes);
  1970. ins.objectid = disk_bytenr;
  1971. ins.offset = disk_num_bytes;
  1972. ins.type = BTRFS_EXTENT_ITEM_KEY;
  1973. ret = btrfs_alloc_reserved_file_extent(trans, root->root_key.objectid,
  1974. btrfs_ino(inode), file_pos,
  1975. ram_bytes, &ins);
  1976. /*
  1977. * Release the reserved range from inode dirty range map, as it is
  1978. * already moved into delayed_ref_head
  1979. */
  1980. btrfs_qgroup_release_data(inode, file_pos, ram_bytes);
  1981. out:
  1982. btrfs_free_path(path);
  1983. return ret;
  1984. }
  1985. /* snapshot-aware defrag */
  1986. struct sa_defrag_extent_backref {
  1987. struct rb_node node;
  1988. struct old_sa_defrag_extent *old;
  1989. u64 root_id;
  1990. u64 inum;
  1991. u64 file_pos;
  1992. u64 extent_offset;
  1993. u64 num_bytes;
  1994. u64 generation;
  1995. };
  1996. struct old_sa_defrag_extent {
  1997. struct list_head list;
  1998. struct new_sa_defrag_extent *new;
  1999. u64 extent_offset;
  2000. u64 bytenr;
  2001. u64 offset;
  2002. u64 len;
  2003. int count;
  2004. };
  2005. struct new_sa_defrag_extent {
  2006. struct rb_root root;
  2007. struct list_head head;
  2008. struct btrfs_path *path;
  2009. struct inode *inode;
  2010. u64 file_pos;
  2011. u64 len;
  2012. u64 bytenr;
  2013. u64 disk_len;
  2014. u8 compress_type;
  2015. };
  2016. static int backref_comp(struct sa_defrag_extent_backref *b1,
  2017. struct sa_defrag_extent_backref *b2)
  2018. {
  2019. if (b1->root_id < b2->root_id)
  2020. return -1;
  2021. else if (b1->root_id > b2->root_id)
  2022. return 1;
  2023. if (b1->inum < b2->inum)
  2024. return -1;
  2025. else if (b1->inum > b2->inum)
  2026. return 1;
  2027. if (b1->file_pos < b2->file_pos)
  2028. return -1;
  2029. else if (b1->file_pos > b2->file_pos)
  2030. return 1;
  2031. /*
  2032. * [------------------------------] ===> (a range of space)
  2033. * |<--->| |<---->| =============> (fs/file tree A)
  2034. * |<---------------------------->| ===> (fs/file tree B)
  2035. *
  2036. * A range of space can refer to two file extents in one tree while
  2037. * refer to only one file extent in another tree.
  2038. *
  2039. * So we may process a disk offset more than one time(two extents in A)
  2040. * and locate at the same extent(one extent in B), then insert two same
  2041. * backrefs(both refer to the extent in B).
  2042. */
  2043. return 0;
  2044. }
  2045. static void backref_insert(struct rb_root *root,
  2046. struct sa_defrag_extent_backref *backref)
  2047. {
  2048. struct rb_node **p = &root->rb_node;
  2049. struct rb_node *parent = NULL;
  2050. struct sa_defrag_extent_backref *entry;
  2051. int ret;
  2052. while (*p) {
  2053. parent = *p;
  2054. entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
  2055. ret = backref_comp(backref, entry);
  2056. if (ret < 0)
  2057. p = &(*p)->rb_left;
  2058. else
  2059. p = &(*p)->rb_right;
  2060. }
  2061. rb_link_node(&backref->node, parent, p);
  2062. rb_insert_color(&backref->node, root);
  2063. }
  2064. /*
  2065. * Note the backref might has changed, and in this case we just return 0.
  2066. */
  2067. static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
  2068. void *ctx)
  2069. {
  2070. struct btrfs_file_extent_item *extent;
  2071. struct old_sa_defrag_extent *old = ctx;
  2072. struct new_sa_defrag_extent *new = old->new;
  2073. struct btrfs_path *path = new->path;
  2074. struct btrfs_key key;
  2075. struct btrfs_root *root;
  2076. struct sa_defrag_extent_backref *backref;
  2077. struct extent_buffer *leaf;
  2078. struct inode *inode = new->inode;
  2079. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  2080. int slot;
  2081. int ret;
  2082. u64 extent_offset;
  2083. u64 num_bytes;
  2084. if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
  2085. inum == btrfs_ino(inode))
  2086. return 0;
  2087. key.objectid = root_id;
  2088. key.type = BTRFS_ROOT_ITEM_KEY;
  2089. key.offset = (u64)-1;
  2090. root = btrfs_read_fs_root_no_name(fs_info, &key);
  2091. if (IS_ERR(root)) {
  2092. if (PTR_ERR(root) == -ENOENT)
  2093. return 0;
  2094. WARN_ON(1);
  2095. btrfs_debug(fs_info, "inum=%llu, offset=%llu, root_id=%llu",
  2096. inum, offset, root_id);
  2097. return PTR_ERR(root);
  2098. }
  2099. key.objectid = inum;
  2100. key.type = BTRFS_EXTENT_DATA_KEY;
  2101. if (offset > (u64)-1 << 32)
  2102. key.offset = 0;
  2103. else
  2104. key.offset = offset;
  2105. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  2106. if (WARN_ON(ret < 0))
  2107. return ret;
  2108. ret = 0;
  2109. while (1) {
  2110. cond_resched();
  2111. leaf = path->nodes[0];
  2112. slot = path->slots[0];
  2113. if (slot >= btrfs_header_nritems(leaf)) {
  2114. ret = btrfs_next_leaf(root, path);
  2115. if (ret < 0) {
  2116. goto out;
  2117. } else if (ret > 0) {
  2118. ret = 0;
  2119. goto out;
  2120. }
  2121. continue;
  2122. }
  2123. path->slots[0]++;
  2124. btrfs_item_key_to_cpu(leaf, &key, slot);
  2125. if (key.objectid > inum)
  2126. goto out;
  2127. if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
  2128. continue;
  2129. extent = btrfs_item_ptr(leaf, slot,
  2130. struct btrfs_file_extent_item);
  2131. if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
  2132. continue;
  2133. /*
  2134. * 'offset' refers to the exact key.offset,
  2135. * NOT the 'offset' field in btrfs_extent_data_ref, ie.
  2136. * (key.offset - extent_offset).
  2137. */
  2138. if (key.offset != offset)
  2139. continue;
  2140. extent_offset = btrfs_file_extent_offset(leaf, extent);
  2141. num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
  2142. if (extent_offset >= old->extent_offset + old->offset +
  2143. old->len || extent_offset + num_bytes <=
  2144. old->extent_offset + old->offset)
  2145. continue;
  2146. break;
  2147. }
  2148. backref = kmalloc(sizeof(*backref), GFP_NOFS);
  2149. if (!backref) {
  2150. ret = -ENOENT;
  2151. goto out;
  2152. }
  2153. backref->root_id = root_id;
  2154. backref->inum = inum;
  2155. backref->file_pos = offset;
  2156. backref->num_bytes = num_bytes;
  2157. backref->extent_offset = extent_offset;
  2158. backref->generation = btrfs_file_extent_generation(leaf, extent);
  2159. backref->old = old;
  2160. backref_insert(&new->root, backref);
  2161. old->count++;
  2162. out:
  2163. btrfs_release_path(path);
  2164. WARN_ON(ret);
  2165. return ret;
  2166. }
  2167. static noinline bool record_extent_backrefs(struct btrfs_path *path,
  2168. struct new_sa_defrag_extent *new)
  2169. {
  2170. struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
  2171. struct old_sa_defrag_extent *old, *tmp;
  2172. int ret;
  2173. new->path = path;
  2174. list_for_each_entry_safe(old, tmp, &new->head, list) {
  2175. ret = iterate_inodes_from_logical(old->bytenr +
  2176. old->extent_offset, fs_info,
  2177. path, record_one_backref,
  2178. old);
  2179. if (ret < 0 && ret != -ENOENT)
  2180. return false;
  2181. /* no backref to be processed for this extent */
  2182. if (!old->count) {
  2183. list_del(&old->list);
  2184. kfree(old);
  2185. }
  2186. }
  2187. if (list_empty(&new->head))
  2188. return false;
  2189. return true;
  2190. }
  2191. static int relink_is_mergable(struct extent_buffer *leaf,
  2192. struct btrfs_file_extent_item *fi,
  2193. struct new_sa_defrag_extent *new)
  2194. {
  2195. if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr)
  2196. return 0;
  2197. if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
  2198. return 0;
  2199. if (btrfs_file_extent_compression(leaf, fi) != new->compress_type)
  2200. return 0;
  2201. if (btrfs_file_extent_encryption(leaf, fi) ||
  2202. btrfs_file_extent_other_encoding(leaf, fi))
  2203. return 0;
  2204. return 1;
  2205. }
  2206. /*
  2207. * Note the backref might has changed, and in this case we just return 0.
  2208. */
  2209. static noinline int relink_extent_backref(struct btrfs_path *path,
  2210. struct sa_defrag_extent_backref *prev,
  2211. struct sa_defrag_extent_backref *backref)
  2212. {
  2213. struct btrfs_file_extent_item *extent;
  2214. struct btrfs_file_extent_item *item;
  2215. struct btrfs_ordered_extent *ordered;
  2216. struct btrfs_trans_handle *trans;
  2217. struct btrfs_root *root;
  2218. struct btrfs_key key;
  2219. struct extent_buffer *leaf;
  2220. struct old_sa_defrag_extent *old = backref->old;
  2221. struct new_sa_defrag_extent *new = old->new;
  2222. struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
  2223. struct inode *inode;
  2224. struct extent_state *cached = NULL;
  2225. int ret = 0;
  2226. u64 start;
  2227. u64 len;
  2228. u64 lock_start;
  2229. u64 lock_end;
  2230. bool merge = false;
  2231. int index;
  2232. if (prev && prev->root_id == backref->root_id &&
  2233. prev->inum == backref->inum &&
  2234. prev->file_pos + prev->num_bytes == backref->file_pos)
  2235. merge = true;
  2236. /* step 1: get root */
  2237. key.objectid = backref->root_id;
  2238. key.type = BTRFS_ROOT_ITEM_KEY;
  2239. key.offset = (u64)-1;
  2240. index = srcu_read_lock(&fs_info->subvol_srcu);
  2241. root = btrfs_read_fs_root_no_name(fs_info, &key);
  2242. if (IS_ERR(root)) {
  2243. srcu_read_unlock(&fs_info->subvol_srcu, index);
  2244. if (PTR_ERR(root) == -ENOENT)
  2245. return 0;
  2246. return PTR_ERR(root);
  2247. }
  2248. if (btrfs_root_readonly(root)) {
  2249. srcu_read_unlock(&fs_info->subvol_srcu, index);
  2250. return 0;
  2251. }
  2252. /* step 2: get inode */
  2253. key.objectid = backref->inum;
  2254. key.type = BTRFS_INODE_ITEM_KEY;
  2255. key.offset = 0;
  2256. inode = btrfs_iget(fs_info->sb, &key, root, NULL);
  2257. if (IS_ERR(inode)) {
  2258. srcu_read_unlock(&fs_info->subvol_srcu, index);
  2259. return 0;
  2260. }
  2261. srcu_read_unlock(&fs_info->subvol_srcu, index);
  2262. /* step 3: relink backref */
  2263. lock_start = backref->file_pos;
  2264. lock_end = backref->file_pos + backref->num_bytes - 1;
  2265. lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
  2266. &cached);
  2267. ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
  2268. if (ordered) {
  2269. btrfs_put_ordered_extent(ordered);
  2270. goto out_unlock;
  2271. }
  2272. trans = btrfs_join_transaction(root);
  2273. if (IS_ERR(trans)) {
  2274. ret = PTR_ERR(trans);
  2275. goto out_unlock;
  2276. }
  2277. key.objectid = backref->inum;
  2278. key.type = BTRFS_EXTENT_DATA_KEY;
  2279. key.offset = backref->file_pos;
  2280. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  2281. if (ret < 0) {
  2282. goto out_free_path;
  2283. } else if (ret > 0) {
  2284. ret = 0;
  2285. goto out_free_path;
  2286. }
  2287. extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
  2288. struct btrfs_file_extent_item);
  2289. if (btrfs_file_extent_generation(path->nodes[0], extent) !=
  2290. backref->generation)
  2291. goto out_free_path;
  2292. btrfs_release_path(path);
  2293. start = backref->file_pos;
  2294. if (backref->extent_offset < old->extent_offset + old->offset)
  2295. start += old->extent_offset + old->offset -
  2296. backref->extent_offset;
  2297. len = min(backref->extent_offset + backref->num_bytes,
  2298. old->extent_offset + old->offset + old->len);
  2299. len -= max(backref->extent_offset, old->extent_offset + old->offset);
  2300. ret = btrfs_drop_extents(trans, root, inode, start,
  2301. start + len, 1);
  2302. if (ret)
  2303. goto out_free_path;
  2304. again:
  2305. key.objectid = btrfs_ino(inode);
  2306. key.type = BTRFS_EXTENT_DATA_KEY;
  2307. key.offset = start;
  2308. path->leave_spinning = 1;
  2309. if (merge) {
  2310. struct btrfs_file_extent_item *fi;
  2311. u64 extent_len;
  2312. struct btrfs_key found_key;
  2313. ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
  2314. if (ret < 0)
  2315. goto out_free_path;
  2316. path->slots[0]--;
  2317. leaf = path->nodes[0];
  2318. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  2319. fi = btrfs_item_ptr(leaf, path->slots[0],
  2320. struct btrfs_file_extent_item);
  2321. extent_len = btrfs_file_extent_num_bytes(leaf, fi);
  2322. if (extent_len + found_key.offset == start &&
  2323. relink_is_mergable(leaf, fi, new)) {
  2324. btrfs_set_file_extent_num_bytes(leaf, fi,
  2325. extent_len + len);
  2326. btrfs_mark_buffer_dirty(leaf);
  2327. inode_add_bytes(inode, len);
  2328. ret = 1;
  2329. goto out_free_path;
  2330. } else {
  2331. merge = false;
  2332. btrfs_release_path(path);
  2333. goto again;
  2334. }
  2335. }
  2336. ret = btrfs_insert_empty_item(trans, root, path, &key,
  2337. sizeof(*extent));
  2338. if (ret) {
  2339. btrfs_abort_transaction(trans, ret);
  2340. goto out_free_path;
  2341. }
  2342. leaf = path->nodes[0];
  2343. item = btrfs_item_ptr(leaf, path->slots[0],
  2344. struct btrfs_file_extent_item);
  2345. btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
  2346. btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
  2347. btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
  2348. btrfs_set_file_extent_num_bytes(leaf, item, len);
  2349. btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
  2350. btrfs_set_file_extent_generation(leaf, item, trans->transid);
  2351. btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
  2352. btrfs_set_file_extent_compression(leaf, item, new->compress_type);
  2353. btrfs_set_file_extent_encryption(leaf, item, 0);
  2354. btrfs_set_file_extent_other_encoding(leaf, item, 0);
  2355. btrfs_mark_buffer_dirty(leaf);
  2356. inode_add_bytes(inode, len);
  2357. btrfs_release_path(path);
  2358. ret = btrfs_inc_extent_ref(trans, fs_info, new->bytenr,
  2359. new->disk_len, 0,
  2360. backref->root_id, backref->inum,
  2361. new->file_pos); /* start - extent_offset */
  2362. if (ret) {
  2363. btrfs_abort_transaction(trans, ret);
  2364. goto out_free_path;
  2365. }
  2366. ret = 1;
  2367. out_free_path:
  2368. btrfs_release_path(path);
  2369. path->leave_spinning = 0;
  2370. btrfs_end_transaction(trans);
  2371. out_unlock:
  2372. unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
  2373. &cached, GFP_NOFS);
  2374. iput(inode);
  2375. return ret;
  2376. }
  2377. static void free_sa_defrag_extent(struct new_sa_defrag_extent *new)
  2378. {
  2379. struct old_sa_defrag_extent *old, *tmp;
  2380. if (!new)
  2381. return;
  2382. list_for_each_entry_safe(old, tmp, &new->head, list) {
  2383. kfree(old);
  2384. }
  2385. kfree(new);
  2386. }
  2387. static void relink_file_extents(struct new_sa_defrag_extent *new)
  2388. {
  2389. struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
  2390. struct btrfs_path *path;
  2391. struct sa_defrag_extent_backref *backref;
  2392. struct sa_defrag_extent_backref *prev = NULL;
  2393. struct inode *inode;
  2394. struct btrfs_root *root;
  2395. struct rb_node *node;
  2396. int ret;
  2397. inode = new->inode;
  2398. root = BTRFS_I(inode)->root;
  2399. path = btrfs_alloc_path();
  2400. if (!path)
  2401. return;
  2402. if (!record_extent_backrefs(path, new)) {
  2403. btrfs_free_path(path);
  2404. goto out;
  2405. }
  2406. btrfs_release_path(path);
  2407. while (1) {
  2408. node = rb_first(&new->root);
  2409. if (!node)
  2410. break;
  2411. rb_erase(node, &new->root);
  2412. backref = rb_entry(node, struct sa_defrag_extent_backref, node);
  2413. ret = relink_extent_backref(path, prev, backref);
  2414. WARN_ON(ret < 0);
  2415. kfree(prev);
  2416. if (ret == 1)
  2417. prev = backref;
  2418. else
  2419. prev = NULL;
  2420. cond_resched();
  2421. }
  2422. kfree(prev);
  2423. btrfs_free_path(path);
  2424. out:
  2425. free_sa_defrag_extent(new);
  2426. atomic_dec(&fs_info->defrag_running);
  2427. wake_up(&fs_info->transaction_wait);
  2428. }
  2429. static struct new_sa_defrag_extent *
  2430. record_old_file_extents(struct inode *inode,
  2431. struct btrfs_ordered_extent *ordered)
  2432. {
  2433. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  2434. struct btrfs_root *root = BTRFS_I(inode)->root;
  2435. struct btrfs_path *path;
  2436. struct btrfs_key key;
  2437. struct old_sa_defrag_extent *old;
  2438. struct new_sa_defrag_extent *new;
  2439. int ret;
  2440. new = kmalloc(sizeof(*new), GFP_NOFS);
  2441. if (!new)
  2442. return NULL;
  2443. new->inode = inode;
  2444. new->file_pos = ordered->file_offset;
  2445. new->len = ordered->len;
  2446. new->bytenr = ordered->start;
  2447. new->disk_len = ordered->disk_len;
  2448. new->compress_type = ordered->compress_type;
  2449. new->root = RB_ROOT;
  2450. INIT_LIST_HEAD(&new->head);
  2451. path = btrfs_alloc_path();
  2452. if (!path)
  2453. goto out_kfree;
  2454. key.objectid = btrfs_ino(inode);
  2455. key.type = BTRFS_EXTENT_DATA_KEY;
  2456. key.offset = new->file_pos;
  2457. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  2458. if (ret < 0)
  2459. goto out_free_path;
  2460. if (ret > 0 && path->slots[0] > 0)
  2461. path->slots[0]--;
  2462. /* find out all the old extents for the file range */
  2463. while (1) {
  2464. struct btrfs_file_extent_item *extent;
  2465. struct extent_buffer *l;
  2466. int slot;
  2467. u64 num_bytes;
  2468. u64 offset;
  2469. u64 end;
  2470. u64 disk_bytenr;
  2471. u64 extent_offset;
  2472. l = path->nodes[0];
  2473. slot = path->slots[0];
  2474. if (slot >= btrfs_header_nritems(l)) {
  2475. ret = btrfs_next_leaf(root, path);
  2476. if (ret < 0)
  2477. goto out_free_path;
  2478. else if (ret > 0)
  2479. break;
  2480. continue;
  2481. }
  2482. btrfs_item_key_to_cpu(l, &key, slot);
  2483. if (key.objectid != btrfs_ino(inode))
  2484. break;
  2485. if (key.type != BTRFS_EXTENT_DATA_KEY)
  2486. break;
  2487. if (key.offset >= new->file_pos + new->len)
  2488. break;
  2489. extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
  2490. num_bytes = btrfs_file_extent_num_bytes(l, extent);
  2491. if (key.offset + num_bytes < new->file_pos)
  2492. goto next;
  2493. disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
  2494. if (!disk_bytenr)
  2495. goto next;
  2496. extent_offset = btrfs_file_extent_offset(l, extent);
  2497. old = kmalloc(sizeof(*old), GFP_NOFS);
  2498. if (!old)
  2499. goto out_free_path;
  2500. offset = max(new->file_pos, key.offset);
  2501. end = min(new->file_pos + new->len, key.offset + num_bytes);
  2502. old->bytenr = disk_bytenr;
  2503. old->extent_offset = extent_offset;
  2504. old->offset = offset - key.offset;
  2505. old->len = end - offset;
  2506. old->new = new;
  2507. old->count = 0;
  2508. list_add_tail(&old->list, &new->head);
  2509. next:
  2510. path->slots[0]++;
  2511. cond_resched();
  2512. }
  2513. btrfs_free_path(path);
  2514. atomic_inc(&fs_info->defrag_running);
  2515. return new;
  2516. out_free_path:
  2517. btrfs_free_path(path);
  2518. out_kfree:
  2519. free_sa_defrag_extent(new);
  2520. return NULL;
  2521. }
  2522. static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info,
  2523. u64 start, u64 len)
  2524. {
  2525. struct btrfs_block_group_cache *cache;
  2526. cache = btrfs_lookup_block_group(fs_info, start);
  2527. ASSERT(cache);
  2528. spin_lock(&cache->lock);
  2529. cache->delalloc_bytes -= len;
  2530. spin_unlock(&cache->lock);
  2531. btrfs_put_block_group(cache);
  2532. }
  2533. /* as ordered data IO finishes, this gets called so we can finish
  2534. * an ordered extent if the range of bytes in the file it covers are
  2535. * fully written.
  2536. */
  2537. static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
  2538. {
  2539. struct inode *inode = ordered_extent->inode;
  2540. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  2541. struct btrfs_root *root = BTRFS_I(inode)->root;
  2542. struct btrfs_trans_handle *trans = NULL;
  2543. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  2544. struct extent_state *cached_state = NULL;
  2545. struct new_sa_defrag_extent *new = NULL;
  2546. int compress_type = 0;
  2547. int ret = 0;
  2548. u64 logical_len = ordered_extent->len;
  2549. bool nolock;
  2550. bool truncated = false;
  2551. nolock = btrfs_is_free_space_inode(inode);
  2552. if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
  2553. ret = -EIO;
  2554. goto out;
  2555. }
  2556. btrfs_free_io_failure_record(inode, ordered_extent->file_offset,
  2557. ordered_extent->file_offset +
  2558. ordered_extent->len - 1);
  2559. if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
  2560. truncated = true;
  2561. logical_len = ordered_extent->truncated_len;
  2562. /* Truncated the entire extent, don't bother adding */
  2563. if (!logical_len)
  2564. goto out;
  2565. }
  2566. if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
  2567. BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
  2568. /*
  2569. * For mwrite(mmap + memset to write) case, we still reserve
  2570. * space for NOCOW range.
  2571. * As NOCOW won't cause a new delayed ref, just free the space
  2572. */
  2573. btrfs_qgroup_free_data(inode, ordered_extent->file_offset,
  2574. ordered_extent->len);
  2575. btrfs_ordered_update_i_size(inode, 0, ordered_extent);
  2576. if (nolock)
  2577. trans = btrfs_join_transaction_nolock(root);
  2578. else
  2579. trans = btrfs_join_transaction(root);
  2580. if (IS_ERR(trans)) {
  2581. ret = PTR_ERR(trans);
  2582. trans = NULL;
  2583. goto out;
  2584. }
  2585. trans->block_rsv = &fs_info->delalloc_block_rsv;
  2586. ret = btrfs_update_inode_fallback(trans, root, inode);
  2587. if (ret) /* -ENOMEM or corruption */
  2588. btrfs_abort_transaction(trans, ret);
  2589. goto out;
  2590. }
  2591. lock_extent_bits(io_tree, ordered_extent->file_offset,
  2592. ordered_extent->file_offset + ordered_extent->len - 1,
  2593. &cached_state);
  2594. ret = test_range_bit(io_tree, ordered_extent->file_offset,
  2595. ordered_extent->file_offset + ordered_extent->len - 1,
  2596. EXTENT_DEFRAG, 1, cached_state);
  2597. if (ret) {
  2598. u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
  2599. if (0 && last_snapshot >= BTRFS_I(inode)->generation)
  2600. /* the inode is shared */
  2601. new = record_old_file_extents(inode, ordered_extent);
  2602. clear_extent_bit(io_tree, ordered_extent->file_offset,
  2603. ordered_extent->file_offset + ordered_extent->len - 1,
  2604. EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS);
  2605. }
  2606. if (nolock)
  2607. trans = btrfs_join_transaction_nolock(root);
  2608. else
  2609. trans = btrfs_join_transaction(root);
  2610. if (IS_ERR(trans)) {
  2611. ret = PTR_ERR(trans);
  2612. trans = NULL;
  2613. goto out_unlock;
  2614. }
  2615. trans->block_rsv = &fs_info->delalloc_block_rsv;
  2616. if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
  2617. compress_type = ordered_extent->compress_type;
  2618. if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
  2619. BUG_ON(compress_type);
  2620. ret = btrfs_mark_extent_written(trans, inode,
  2621. ordered_extent->file_offset,
  2622. ordered_extent->file_offset +
  2623. logical_len);
  2624. } else {
  2625. BUG_ON(root == fs_info->tree_root);
  2626. ret = insert_reserved_file_extent(trans, inode,
  2627. ordered_extent->file_offset,
  2628. ordered_extent->start,
  2629. ordered_extent->disk_len,
  2630. logical_len, logical_len,
  2631. compress_type, 0, 0,
  2632. BTRFS_FILE_EXTENT_REG);
  2633. if (!ret)
  2634. btrfs_release_delalloc_bytes(fs_info,
  2635. ordered_extent->start,
  2636. ordered_extent->disk_len);
  2637. }
  2638. unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
  2639. ordered_extent->file_offset, ordered_extent->len,
  2640. trans->transid);
  2641. if (ret < 0) {
  2642. btrfs_abort_transaction(trans, ret);
  2643. goto out_unlock;
  2644. }
  2645. add_pending_csums(trans, inode, ordered_extent->file_offset,
  2646. &ordered_extent->list);
  2647. btrfs_ordered_update_i_size(inode, 0, ordered_extent);
  2648. ret = btrfs_update_inode_fallback(trans, root, inode);
  2649. if (ret) { /* -ENOMEM or corruption */
  2650. btrfs_abort_transaction(trans, ret);
  2651. goto out_unlock;
  2652. }
  2653. ret = 0;
  2654. out_unlock:
  2655. unlock_extent_cached(io_tree, ordered_extent->file_offset,
  2656. ordered_extent->file_offset +
  2657. ordered_extent->len - 1, &cached_state, GFP_NOFS);
  2658. out:
  2659. if (root != fs_info->tree_root)
  2660. btrfs_delalloc_release_metadata(inode, ordered_extent->len);
  2661. if (trans)
  2662. btrfs_end_transaction(trans);
  2663. if (ret || truncated) {
  2664. u64 start, end;
  2665. if (truncated)
  2666. start = ordered_extent->file_offset + logical_len;
  2667. else
  2668. start = ordered_extent->file_offset;
  2669. end = ordered_extent->file_offset + ordered_extent->len - 1;
  2670. clear_extent_uptodate(io_tree, start, end, NULL, GFP_NOFS);
  2671. /* Drop the cache for the part of the extent we didn't write. */
  2672. btrfs_drop_extent_cache(inode, start, end, 0);
  2673. /*
  2674. * If the ordered extent had an IOERR or something else went
  2675. * wrong we need to return the space for this ordered extent
  2676. * back to the allocator. We only free the extent in the
  2677. * truncated case if we didn't write out the extent at all.
  2678. */
  2679. if ((ret || !logical_len) &&
  2680. !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
  2681. !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
  2682. btrfs_free_reserved_extent(fs_info,
  2683. ordered_extent->start,
  2684. ordered_extent->disk_len, 1);
  2685. }
  2686. /*
  2687. * This needs to be done to make sure anybody waiting knows we are done
  2688. * updating everything for this ordered extent.
  2689. */
  2690. btrfs_remove_ordered_extent(inode, ordered_extent);
  2691. /* for snapshot-aware defrag */
  2692. if (new) {
  2693. if (ret) {
  2694. free_sa_defrag_extent(new);
  2695. atomic_dec(&fs_info->defrag_running);
  2696. } else {
  2697. relink_file_extents(new);
  2698. }
  2699. }
  2700. /* once for us */
  2701. btrfs_put_ordered_extent(ordered_extent);
  2702. /* once for the tree */
  2703. btrfs_put_ordered_extent(ordered_extent);
  2704. return ret;
  2705. }
  2706. static void finish_ordered_fn(struct btrfs_work *work)
  2707. {
  2708. struct btrfs_ordered_extent *ordered_extent;
  2709. ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
  2710. btrfs_finish_ordered_io(ordered_extent);
  2711. }
  2712. static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
  2713. struct extent_state *state, int uptodate)
  2714. {
  2715. struct inode *inode = page->mapping->host;
  2716. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  2717. struct btrfs_ordered_extent *ordered_extent = NULL;
  2718. struct btrfs_workqueue *wq;
  2719. btrfs_work_func_t func;
  2720. trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
  2721. ClearPagePrivate2(page);
  2722. if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
  2723. end - start + 1, uptodate))
  2724. return 0;
  2725. if (btrfs_is_free_space_inode(inode)) {
  2726. wq = fs_info->endio_freespace_worker;
  2727. func = btrfs_freespace_write_helper;
  2728. } else {
  2729. wq = fs_info->endio_write_workers;
  2730. func = btrfs_endio_write_helper;
  2731. }
  2732. btrfs_init_work(&ordered_extent->work, func, finish_ordered_fn, NULL,
  2733. NULL);
  2734. btrfs_queue_work(wq, &ordered_extent->work);
  2735. return 0;
  2736. }
  2737. static int __readpage_endio_check(struct inode *inode,
  2738. struct btrfs_io_bio *io_bio,
  2739. int icsum, struct page *page,
  2740. int pgoff, u64 start, size_t len)
  2741. {
  2742. char *kaddr;
  2743. u32 csum_expected;
  2744. u32 csum = ~(u32)0;
  2745. csum_expected = *(((u32 *)io_bio->csum) + icsum);
  2746. kaddr = kmap_atomic(page);
  2747. csum = btrfs_csum_data(kaddr + pgoff, csum, len);
  2748. btrfs_csum_final(csum, (u8 *)&csum);
  2749. if (csum != csum_expected)
  2750. goto zeroit;
  2751. kunmap_atomic(kaddr);
  2752. return 0;
  2753. zeroit:
  2754. btrfs_warn_rl(BTRFS_I(inode)->root->fs_info,
  2755. "csum failed ino %llu off %llu csum %u expected csum %u",
  2756. btrfs_ino(inode), start, csum, csum_expected);
  2757. memset(kaddr + pgoff, 1, len);
  2758. flush_dcache_page(page);
  2759. kunmap_atomic(kaddr);
  2760. if (csum_expected == 0)
  2761. return 0;
  2762. return -EIO;
  2763. }
  2764. /*
  2765. * when reads are done, we need to check csums to verify the data is correct
  2766. * if there's a match, we allow the bio to finish. If not, the code in
  2767. * extent_io.c will try to find good copies for us.
  2768. */
  2769. static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
  2770. u64 phy_offset, struct page *page,
  2771. u64 start, u64 end, int mirror)
  2772. {
  2773. size_t offset = start - page_offset(page);
  2774. struct inode *inode = page->mapping->host;
  2775. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  2776. struct btrfs_root *root = BTRFS_I(inode)->root;
  2777. if (PageChecked(page)) {
  2778. ClearPageChecked(page);
  2779. return 0;
  2780. }
  2781. if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
  2782. return 0;
  2783. if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
  2784. test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
  2785. clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM);
  2786. return 0;
  2787. }
  2788. phy_offset >>= inode->i_sb->s_blocksize_bits;
  2789. return __readpage_endio_check(inode, io_bio, phy_offset, page, offset,
  2790. start, (size_t)(end - start + 1));
  2791. }
  2792. void btrfs_add_delayed_iput(struct inode *inode)
  2793. {
  2794. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  2795. struct btrfs_inode *binode = BTRFS_I(inode);
  2796. if (atomic_add_unless(&inode->i_count, -1, 1))
  2797. return;
  2798. spin_lock(&fs_info->delayed_iput_lock);
  2799. if (binode->delayed_iput_count == 0) {
  2800. ASSERT(list_empty(&binode->delayed_iput));
  2801. list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs);
  2802. } else {
  2803. binode->delayed_iput_count++;
  2804. }
  2805. spin_unlock(&fs_info->delayed_iput_lock);
  2806. }
  2807. void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info)
  2808. {
  2809. spin_lock(&fs_info->delayed_iput_lock);
  2810. while (!list_empty(&fs_info->delayed_iputs)) {
  2811. struct btrfs_inode *inode;
  2812. inode = list_first_entry(&fs_info->delayed_iputs,
  2813. struct btrfs_inode, delayed_iput);
  2814. if (inode->delayed_iput_count) {
  2815. inode->delayed_iput_count--;
  2816. list_move_tail(&inode->delayed_iput,
  2817. &fs_info->delayed_iputs);
  2818. } else {
  2819. list_del_init(&inode->delayed_iput);
  2820. }
  2821. spin_unlock(&fs_info->delayed_iput_lock);
  2822. iput(&inode->vfs_inode);
  2823. spin_lock(&fs_info->delayed_iput_lock);
  2824. }
  2825. spin_unlock(&fs_info->delayed_iput_lock);
  2826. }
  2827. /*
  2828. * This is called in transaction commit time. If there are no orphan
  2829. * files in the subvolume, it removes orphan item and frees block_rsv
  2830. * structure.
  2831. */
  2832. void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
  2833. struct btrfs_root *root)
  2834. {
  2835. struct btrfs_fs_info *fs_info = root->fs_info;
  2836. struct btrfs_block_rsv *block_rsv;
  2837. int ret;
  2838. if (atomic_read(&root->orphan_inodes) ||
  2839. root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
  2840. return;
  2841. spin_lock(&root->orphan_lock);
  2842. if (atomic_read(&root->orphan_inodes)) {
  2843. spin_unlock(&root->orphan_lock);
  2844. return;
  2845. }
  2846. if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
  2847. spin_unlock(&root->orphan_lock);
  2848. return;
  2849. }
  2850. block_rsv = root->orphan_block_rsv;
  2851. root->orphan_block_rsv = NULL;
  2852. spin_unlock(&root->orphan_lock);
  2853. if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state) &&
  2854. btrfs_root_refs(&root->root_item) > 0) {
  2855. ret = btrfs_del_orphan_item(trans, fs_info->tree_root,
  2856. root->root_key.objectid);
  2857. if (ret)
  2858. btrfs_abort_transaction(trans, ret);
  2859. else
  2860. clear_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED,
  2861. &root->state);
  2862. }
  2863. if (block_rsv) {
  2864. WARN_ON(block_rsv->size > 0);
  2865. btrfs_free_block_rsv(fs_info, block_rsv);
  2866. }
  2867. }
  2868. /*
  2869. * This creates an orphan entry for the given inode in case something goes
  2870. * wrong in the middle of an unlink/truncate.
  2871. *
  2872. * NOTE: caller of this function should reserve 5 units of metadata for
  2873. * this function.
  2874. */
  2875. int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
  2876. {
  2877. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  2878. struct btrfs_root *root = BTRFS_I(inode)->root;
  2879. struct btrfs_block_rsv *block_rsv = NULL;
  2880. int reserve = 0;
  2881. int insert = 0;
  2882. int ret;
  2883. if (!root->orphan_block_rsv) {
  2884. block_rsv = btrfs_alloc_block_rsv(fs_info,
  2885. BTRFS_BLOCK_RSV_TEMP);
  2886. if (!block_rsv)
  2887. return -ENOMEM;
  2888. }
  2889. spin_lock(&root->orphan_lock);
  2890. if (!root->orphan_block_rsv) {
  2891. root->orphan_block_rsv = block_rsv;
  2892. } else if (block_rsv) {
  2893. btrfs_free_block_rsv(fs_info, block_rsv);
  2894. block_rsv = NULL;
  2895. }
  2896. if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
  2897. &BTRFS_I(inode)->runtime_flags)) {
  2898. #if 0
  2899. /*
  2900. * For proper ENOSPC handling, we should do orphan
  2901. * cleanup when mounting. But this introduces backward
  2902. * compatibility issue.
  2903. */
  2904. if (!xchg(&root->orphan_item_inserted, 1))
  2905. insert = 2;
  2906. else
  2907. insert = 1;
  2908. #endif
  2909. insert = 1;
  2910. atomic_inc(&root->orphan_inodes);
  2911. }
  2912. if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
  2913. &BTRFS_I(inode)->runtime_flags))
  2914. reserve = 1;
  2915. spin_unlock(&root->orphan_lock);
  2916. /* grab metadata reservation from transaction handle */
  2917. if (reserve) {
  2918. ret = btrfs_orphan_reserve_metadata(trans, inode);
  2919. ASSERT(!ret);
  2920. if (ret) {
  2921. atomic_dec(&root->orphan_inodes);
  2922. clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
  2923. &BTRFS_I(inode)->runtime_flags);
  2924. if (insert)
  2925. clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
  2926. &BTRFS_I(inode)->runtime_flags);
  2927. return ret;
  2928. }
  2929. }
  2930. /* insert an orphan item to track this unlinked/truncated file */
  2931. if (insert >= 1) {
  2932. ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
  2933. if (ret) {
  2934. atomic_dec(&root->orphan_inodes);
  2935. if (reserve) {
  2936. clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
  2937. &BTRFS_I(inode)->runtime_flags);
  2938. btrfs_orphan_release_metadata(inode);
  2939. }
  2940. if (ret != -EEXIST) {
  2941. clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
  2942. &BTRFS_I(inode)->runtime_flags);
  2943. btrfs_abort_transaction(trans, ret);
  2944. return ret;
  2945. }
  2946. }
  2947. ret = 0;
  2948. }
  2949. /* insert an orphan item to track subvolume contains orphan files */
  2950. if (insert >= 2) {
  2951. ret = btrfs_insert_orphan_item(trans, fs_info->tree_root,
  2952. root->root_key.objectid);
  2953. if (ret && ret != -EEXIST) {
  2954. btrfs_abort_transaction(trans, ret);
  2955. return ret;
  2956. }
  2957. }
  2958. return 0;
  2959. }
  2960. /*
  2961. * We have done the truncate/delete so we can go ahead and remove the orphan
  2962. * item for this particular inode.
  2963. */
  2964. static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
  2965. struct inode *inode)
  2966. {
  2967. struct btrfs_root *root = BTRFS_I(inode)->root;
  2968. int delete_item = 0;
  2969. int release_rsv = 0;
  2970. int ret = 0;
  2971. spin_lock(&root->orphan_lock);
  2972. if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
  2973. &BTRFS_I(inode)->runtime_flags))
  2974. delete_item = 1;
  2975. if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
  2976. &BTRFS_I(inode)->runtime_flags))
  2977. release_rsv = 1;
  2978. spin_unlock(&root->orphan_lock);
  2979. if (delete_item) {
  2980. atomic_dec(&root->orphan_inodes);
  2981. if (trans)
  2982. ret = btrfs_del_orphan_item(trans, root,
  2983. btrfs_ino(inode));
  2984. }
  2985. if (release_rsv)
  2986. btrfs_orphan_release_metadata(inode);
  2987. return ret;
  2988. }
  2989. /*
  2990. * this cleans up any orphans that may be left on the list from the last use
  2991. * of this root.
  2992. */
  2993. int btrfs_orphan_cleanup(struct btrfs_root *root)
  2994. {
  2995. struct btrfs_fs_info *fs_info = root->fs_info;
  2996. struct btrfs_path *path;
  2997. struct extent_buffer *leaf;
  2998. struct btrfs_key key, found_key;
  2999. struct btrfs_trans_handle *trans;
  3000. struct inode *inode;
  3001. u64 last_objectid = 0;
  3002. int ret = 0, nr_unlink = 0, nr_truncate = 0;
  3003. if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
  3004. return 0;
  3005. path = btrfs_alloc_path();
  3006. if (!path) {
  3007. ret = -ENOMEM;
  3008. goto out;
  3009. }
  3010. path->reada = READA_BACK;
  3011. key.objectid = BTRFS_ORPHAN_OBJECTID;
  3012. key.type = BTRFS_ORPHAN_ITEM_KEY;
  3013. key.offset = (u64)-1;
  3014. while (1) {
  3015. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  3016. if (ret < 0)
  3017. goto out;
  3018. /*
  3019. * if ret == 0 means we found what we were searching for, which
  3020. * is weird, but possible, so only screw with path if we didn't
  3021. * find the key and see if we have stuff that matches
  3022. */
  3023. if (ret > 0) {
  3024. ret = 0;
  3025. if (path->slots[0] == 0)
  3026. break;
  3027. path->slots[0]--;
  3028. }
  3029. /* pull out the item */
  3030. leaf = path->nodes[0];
  3031. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  3032. /* make sure the item matches what we want */
  3033. if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
  3034. break;
  3035. if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
  3036. break;
  3037. /* release the path since we're done with it */
  3038. btrfs_release_path(path);
  3039. /*
  3040. * this is where we are basically btrfs_lookup, without the
  3041. * crossing root thing. we store the inode number in the
  3042. * offset of the orphan item.
  3043. */
  3044. if (found_key.offset == last_objectid) {
  3045. btrfs_err(fs_info,
  3046. "Error removing orphan entry, stopping orphan cleanup");
  3047. ret = -EINVAL;
  3048. goto out;
  3049. }
  3050. last_objectid = found_key.offset;
  3051. found_key.objectid = found_key.offset;
  3052. found_key.type = BTRFS_INODE_ITEM_KEY;
  3053. found_key.offset = 0;
  3054. inode = btrfs_iget(fs_info->sb, &found_key, root, NULL);
  3055. ret = PTR_ERR_OR_ZERO(inode);
  3056. if (ret && ret != -ENOENT)
  3057. goto out;
  3058. if (ret == -ENOENT && root == fs_info->tree_root) {
  3059. struct btrfs_root *dead_root;
  3060. struct btrfs_fs_info *fs_info = root->fs_info;
  3061. int is_dead_root = 0;
  3062. /*
  3063. * this is an orphan in the tree root. Currently these
  3064. * could come from 2 sources:
  3065. * a) a snapshot deletion in progress
  3066. * b) a free space cache inode
  3067. * We need to distinguish those two, as the snapshot
  3068. * orphan must not get deleted.
  3069. * find_dead_roots already ran before us, so if this
  3070. * is a snapshot deletion, we should find the root
  3071. * in the dead_roots list
  3072. */
  3073. spin_lock(&fs_info->trans_lock);
  3074. list_for_each_entry(dead_root, &fs_info->dead_roots,
  3075. root_list) {
  3076. if (dead_root->root_key.objectid ==
  3077. found_key.objectid) {
  3078. is_dead_root = 1;
  3079. break;
  3080. }
  3081. }
  3082. spin_unlock(&fs_info->trans_lock);
  3083. if (is_dead_root) {
  3084. /* prevent this orphan from being found again */
  3085. key.offset = found_key.objectid - 1;
  3086. continue;
  3087. }
  3088. }
  3089. /*
  3090. * Inode is already gone but the orphan item is still there,
  3091. * kill the orphan item.
  3092. */
  3093. if (ret == -ENOENT) {
  3094. trans = btrfs_start_transaction(root, 1);
  3095. if (IS_ERR(trans)) {
  3096. ret = PTR_ERR(trans);
  3097. goto out;
  3098. }
  3099. btrfs_debug(fs_info, "auto deleting %Lu",
  3100. found_key.objectid);
  3101. ret = btrfs_del_orphan_item(trans, root,
  3102. found_key.objectid);
  3103. btrfs_end_transaction(trans);
  3104. if (ret)
  3105. goto out;
  3106. continue;
  3107. }
  3108. /*
  3109. * add this inode to the orphan list so btrfs_orphan_del does
  3110. * the proper thing when we hit it
  3111. */
  3112. set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
  3113. &BTRFS_I(inode)->runtime_flags);
  3114. atomic_inc(&root->orphan_inodes);
  3115. /* if we have links, this was a truncate, lets do that */
  3116. if (inode->i_nlink) {
  3117. if (WARN_ON(!S_ISREG(inode->i_mode))) {
  3118. iput(inode);
  3119. continue;
  3120. }
  3121. nr_truncate++;
  3122. /* 1 for the orphan item deletion. */
  3123. trans = btrfs_start_transaction(root, 1);
  3124. if (IS_ERR(trans)) {
  3125. iput(inode);
  3126. ret = PTR_ERR(trans);
  3127. goto out;
  3128. }
  3129. ret = btrfs_orphan_add(trans, inode);
  3130. btrfs_end_transaction(trans);
  3131. if (ret) {
  3132. iput(inode);
  3133. goto out;
  3134. }
  3135. ret = btrfs_truncate(inode);
  3136. if (ret)
  3137. btrfs_orphan_del(NULL, inode);
  3138. } else {
  3139. nr_unlink++;
  3140. }
  3141. /* this will do delete_inode and everything for us */
  3142. iput(inode);
  3143. if (ret)
  3144. goto out;
  3145. }
  3146. /* release the path since we're done with it */
  3147. btrfs_release_path(path);
  3148. root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
  3149. if (root->orphan_block_rsv)
  3150. btrfs_block_rsv_release(fs_info, root->orphan_block_rsv,
  3151. (u64)-1);
  3152. if (root->orphan_block_rsv ||
  3153. test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
  3154. trans = btrfs_join_transaction(root);
  3155. if (!IS_ERR(trans))
  3156. btrfs_end_transaction(trans);
  3157. }
  3158. if (nr_unlink)
  3159. btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink);
  3160. if (nr_truncate)
  3161. btrfs_debug(fs_info, "truncated %d orphans", nr_truncate);
  3162. out:
  3163. if (ret)
  3164. btrfs_err(fs_info, "could not do orphan cleanup %d", ret);
  3165. btrfs_free_path(path);
  3166. return ret;
  3167. }
  3168. /*
  3169. * very simple check to peek ahead in the leaf looking for xattrs. If we
  3170. * don't find any xattrs, we know there can't be any acls.
  3171. *
  3172. * slot is the slot the inode is in, objectid is the objectid of the inode
  3173. */
  3174. static noinline int acls_after_inode_item(struct extent_buffer *leaf,
  3175. int slot, u64 objectid,
  3176. int *first_xattr_slot)
  3177. {
  3178. u32 nritems = btrfs_header_nritems(leaf);
  3179. struct btrfs_key found_key;
  3180. static u64 xattr_access = 0;
  3181. static u64 xattr_default = 0;
  3182. int scanned = 0;
  3183. if (!xattr_access) {
  3184. xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS,
  3185. strlen(XATTR_NAME_POSIX_ACL_ACCESS));
  3186. xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT,
  3187. strlen(XATTR_NAME_POSIX_ACL_DEFAULT));
  3188. }
  3189. slot++;
  3190. *first_xattr_slot = -1;
  3191. while (slot < nritems) {
  3192. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  3193. /* we found a different objectid, there must not be acls */
  3194. if (found_key.objectid != objectid)
  3195. return 0;
  3196. /* we found an xattr, assume we've got an acl */
  3197. if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
  3198. if (*first_xattr_slot == -1)
  3199. *first_xattr_slot = slot;
  3200. if (found_key.offset == xattr_access ||
  3201. found_key.offset == xattr_default)
  3202. return 1;
  3203. }
  3204. /*
  3205. * we found a key greater than an xattr key, there can't
  3206. * be any acls later on
  3207. */
  3208. if (found_key.type > BTRFS_XATTR_ITEM_KEY)
  3209. return 0;
  3210. slot++;
  3211. scanned++;
  3212. /*
  3213. * it goes inode, inode backrefs, xattrs, extents,
  3214. * so if there are a ton of hard links to an inode there can
  3215. * be a lot of backrefs. Don't waste time searching too hard,
  3216. * this is just an optimization
  3217. */
  3218. if (scanned >= 8)
  3219. break;
  3220. }
  3221. /* we hit the end of the leaf before we found an xattr or
  3222. * something larger than an xattr. We have to assume the inode
  3223. * has acls
  3224. */
  3225. if (*first_xattr_slot == -1)
  3226. *first_xattr_slot = slot;
  3227. return 1;
  3228. }
  3229. /*
  3230. * read an inode from the btree into the in-memory inode
  3231. */
  3232. static int btrfs_read_locked_inode(struct inode *inode)
  3233. {
  3234. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  3235. struct btrfs_path *path;
  3236. struct extent_buffer *leaf;
  3237. struct btrfs_inode_item *inode_item;
  3238. struct btrfs_root *root = BTRFS_I(inode)->root;
  3239. struct btrfs_key location;
  3240. unsigned long ptr;
  3241. int maybe_acls;
  3242. u32 rdev;
  3243. int ret;
  3244. bool filled = false;
  3245. int first_xattr_slot;
  3246. ret = btrfs_fill_inode(inode, &rdev);
  3247. if (!ret)
  3248. filled = true;
  3249. path = btrfs_alloc_path();
  3250. if (!path) {
  3251. ret = -ENOMEM;
  3252. goto make_bad;
  3253. }
  3254. memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
  3255. ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
  3256. if (ret) {
  3257. if (ret > 0)
  3258. ret = -ENOENT;
  3259. goto make_bad;
  3260. }
  3261. leaf = path->nodes[0];
  3262. if (filled)
  3263. goto cache_index;
  3264. inode_item = btrfs_item_ptr(leaf, path->slots[0],
  3265. struct btrfs_inode_item);
  3266. inode->i_mode = btrfs_inode_mode(leaf, inode_item);
  3267. set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
  3268. i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
  3269. i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
  3270. btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
  3271. inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime);
  3272. inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime);
  3273. inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime);
  3274. inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime);
  3275. inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime);
  3276. inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime);
  3277. BTRFS_I(inode)->i_otime.tv_sec =
  3278. btrfs_timespec_sec(leaf, &inode_item->otime);
  3279. BTRFS_I(inode)->i_otime.tv_nsec =
  3280. btrfs_timespec_nsec(leaf, &inode_item->otime);
  3281. inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
  3282. BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
  3283. BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
  3284. inode->i_version = btrfs_inode_sequence(leaf, inode_item);
  3285. inode->i_generation = BTRFS_I(inode)->generation;
  3286. inode->i_rdev = 0;
  3287. rdev = btrfs_inode_rdev(leaf, inode_item);
  3288. BTRFS_I(inode)->index_cnt = (u64)-1;
  3289. BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
  3290. cache_index:
  3291. /*
  3292. * If we were modified in the current generation and evicted from memory
  3293. * and then re-read we need to do a full sync since we don't have any
  3294. * idea about which extents were modified before we were evicted from
  3295. * cache.
  3296. *
  3297. * This is required for both inode re-read from disk and delayed inode
  3298. * in delayed_nodes_tree.
  3299. */
  3300. if (BTRFS_I(inode)->last_trans == fs_info->generation)
  3301. set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
  3302. &BTRFS_I(inode)->runtime_flags);
  3303. /*
  3304. * We don't persist the id of the transaction where an unlink operation
  3305. * against the inode was last made. So here we assume the inode might
  3306. * have been evicted, and therefore the exact value of last_unlink_trans
  3307. * lost, and set it to last_trans to avoid metadata inconsistencies
  3308. * between the inode and its parent if the inode is fsync'ed and the log
  3309. * replayed. For example, in the scenario:
  3310. *
  3311. * touch mydir/foo
  3312. * ln mydir/foo mydir/bar
  3313. * sync
  3314. * unlink mydir/bar
  3315. * echo 2 > /proc/sys/vm/drop_caches # evicts inode
  3316. * xfs_io -c fsync mydir/foo
  3317. * <power failure>
  3318. * mount fs, triggers fsync log replay
  3319. *
  3320. * We must make sure that when we fsync our inode foo we also log its
  3321. * parent inode, otherwise after log replay the parent still has the
  3322. * dentry with the "bar" name but our inode foo has a link count of 1
  3323. * and doesn't have an inode ref with the name "bar" anymore.
  3324. *
  3325. * Setting last_unlink_trans to last_trans is a pessimistic approach,
  3326. * but it guarantees correctness at the expense of occasional full
  3327. * transaction commits on fsync if our inode is a directory, or if our
  3328. * inode is not a directory, logging its parent unnecessarily.
  3329. */
  3330. BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans;
  3331. path->slots[0]++;
  3332. if (inode->i_nlink != 1 ||
  3333. path->slots[0] >= btrfs_header_nritems(leaf))
  3334. goto cache_acl;
  3335. btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
  3336. if (location.objectid != btrfs_ino(inode))
  3337. goto cache_acl;
  3338. ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
  3339. if (location.type == BTRFS_INODE_REF_KEY) {
  3340. struct btrfs_inode_ref *ref;
  3341. ref = (struct btrfs_inode_ref *)ptr;
  3342. BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
  3343. } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
  3344. struct btrfs_inode_extref *extref;
  3345. extref = (struct btrfs_inode_extref *)ptr;
  3346. BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
  3347. extref);
  3348. }
  3349. cache_acl:
  3350. /*
  3351. * try to precache a NULL acl entry for files that don't have
  3352. * any xattrs or acls
  3353. */
  3354. maybe_acls = acls_after_inode_item(leaf, path->slots[0],
  3355. btrfs_ino(inode), &first_xattr_slot);
  3356. if (first_xattr_slot != -1) {
  3357. path->slots[0] = first_xattr_slot;
  3358. ret = btrfs_load_inode_props(inode, path);
  3359. if (ret)
  3360. btrfs_err(fs_info,
  3361. "error loading props for ino %llu (root %llu): %d",
  3362. btrfs_ino(inode),
  3363. root->root_key.objectid, ret);
  3364. }
  3365. btrfs_free_path(path);
  3366. if (!maybe_acls)
  3367. cache_no_acl(inode);
  3368. switch (inode->i_mode & S_IFMT) {
  3369. case S_IFREG:
  3370. inode->i_mapping->a_ops = &btrfs_aops;
  3371. BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
  3372. inode->i_fop = &btrfs_file_operations;
  3373. inode->i_op = &btrfs_file_inode_operations;
  3374. break;
  3375. case S_IFDIR:
  3376. inode->i_fop = &btrfs_dir_file_operations;
  3377. if (root == fs_info->tree_root)
  3378. inode->i_op = &btrfs_dir_ro_inode_operations;
  3379. else
  3380. inode->i_op = &btrfs_dir_inode_operations;
  3381. break;
  3382. case S_IFLNK:
  3383. inode->i_op = &btrfs_symlink_inode_operations;
  3384. inode_nohighmem(inode);
  3385. inode->i_mapping->a_ops = &btrfs_symlink_aops;
  3386. break;
  3387. default:
  3388. inode->i_op = &btrfs_special_inode_operations;
  3389. init_special_inode(inode, inode->i_mode, rdev);
  3390. break;
  3391. }
  3392. btrfs_update_iflags(inode);
  3393. return 0;
  3394. make_bad:
  3395. btrfs_free_path(path);
  3396. make_bad_inode(inode);
  3397. return ret;
  3398. }
  3399. /*
  3400. * given a leaf and an inode, copy the inode fields into the leaf
  3401. */
  3402. static void fill_inode_item(struct btrfs_trans_handle *trans,
  3403. struct extent_buffer *leaf,
  3404. struct btrfs_inode_item *item,
  3405. struct inode *inode)
  3406. {
  3407. struct btrfs_map_token token;
  3408. btrfs_init_map_token(&token);
  3409. btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
  3410. btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
  3411. btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
  3412. &token);
  3413. btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
  3414. btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
  3415. btrfs_set_token_timespec_sec(leaf, &item->atime,
  3416. inode->i_atime.tv_sec, &token);
  3417. btrfs_set_token_timespec_nsec(leaf, &item->atime,
  3418. inode->i_atime.tv_nsec, &token);
  3419. btrfs_set_token_timespec_sec(leaf, &item->mtime,
  3420. inode->i_mtime.tv_sec, &token);
  3421. btrfs_set_token_timespec_nsec(leaf, &item->mtime,
  3422. inode->i_mtime.tv_nsec, &token);
  3423. btrfs_set_token_timespec_sec(leaf, &item->ctime,
  3424. inode->i_ctime.tv_sec, &token);
  3425. btrfs_set_token_timespec_nsec(leaf, &item->ctime,
  3426. inode->i_ctime.tv_nsec, &token);
  3427. btrfs_set_token_timespec_sec(leaf, &item->otime,
  3428. BTRFS_I(inode)->i_otime.tv_sec, &token);
  3429. btrfs_set_token_timespec_nsec(leaf, &item->otime,
  3430. BTRFS_I(inode)->i_otime.tv_nsec, &token);
  3431. btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
  3432. &token);
  3433. btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
  3434. &token);
  3435. btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
  3436. btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
  3437. btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
  3438. btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
  3439. btrfs_set_token_inode_block_group(leaf, item, 0, &token);
  3440. }
  3441. /*
  3442. * copy everything in the in-memory inode into the btree.
  3443. */
  3444. static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
  3445. struct btrfs_root *root, struct inode *inode)
  3446. {
  3447. struct btrfs_inode_item *inode_item;
  3448. struct btrfs_path *path;
  3449. struct extent_buffer *leaf;
  3450. int ret;
  3451. path = btrfs_alloc_path();
  3452. if (!path)
  3453. return -ENOMEM;
  3454. path->leave_spinning = 1;
  3455. ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
  3456. 1);
  3457. if (ret) {
  3458. if (ret > 0)
  3459. ret = -ENOENT;
  3460. goto failed;
  3461. }
  3462. leaf = path->nodes[0];
  3463. inode_item = btrfs_item_ptr(leaf, path->slots[0],
  3464. struct btrfs_inode_item);
  3465. fill_inode_item(trans, leaf, inode_item, inode);
  3466. btrfs_mark_buffer_dirty(leaf);
  3467. btrfs_set_inode_last_trans(trans, inode);
  3468. ret = 0;
  3469. failed:
  3470. btrfs_free_path(path);
  3471. return ret;
  3472. }
  3473. /*
  3474. * copy everything in the in-memory inode into the btree.
  3475. */
  3476. noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
  3477. struct btrfs_root *root, struct inode *inode)
  3478. {
  3479. struct btrfs_fs_info *fs_info = root->fs_info;
  3480. int ret;
  3481. /*
  3482. * If the inode is a free space inode, we can deadlock during commit
  3483. * if we put it into the delayed code.
  3484. *
  3485. * The data relocation inode should also be directly updated
  3486. * without delay
  3487. */
  3488. if (!btrfs_is_free_space_inode(inode)
  3489. && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
  3490. && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
  3491. btrfs_update_root_times(trans, root);
  3492. ret = btrfs_delayed_update_inode(trans, root, inode);
  3493. if (!ret)
  3494. btrfs_set_inode_last_trans(trans, inode);
  3495. return ret;
  3496. }
  3497. return btrfs_update_inode_item(trans, root, inode);
  3498. }
  3499. noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
  3500. struct btrfs_root *root,
  3501. struct inode *inode)
  3502. {
  3503. int ret;
  3504. ret = btrfs_update_inode(trans, root, inode);
  3505. if (ret == -ENOSPC)
  3506. return btrfs_update_inode_item(trans, root, inode);
  3507. return ret;
  3508. }
  3509. /*
  3510. * unlink helper that gets used here in inode.c and in the tree logging
  3511. * recovery code. It remove a link in a directory with a given name, and
  3512. * also drops the back refs in the inode to the directory
  3513. */
  3514. static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
  3515. struct btrfs_root *root,
  3516. struct inode *dir, struct inode *inode,
  3517. const char *name, int name_len)
  3518. {
  3519. struct btrfs_fs_info *fs_info = root->fs_info;
  3520. struct btrfs_path *path;
  3521. int ret = 0;
  3522. struct extent_buffer *leaf;
  3523. struct btrfs_dir_item *di;
  3524. struct btrfs_key key;
  3525. u64 index;
  3526. u64 ino = btrfs_ino(inode);
  3527. u64 dir_ino = btrfs_ino(dir);
  3528. path = btrfs_alloc_path();
  3529. if (!path) {
  3530. ret = -ENOMEM;
  3531. goto out;
  3532. }
  3533. path->leave_spinning = 1;
  3534. di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
  3535. name, name_len, -1);
  3536. if (IS_ERR(di)) {
  3537. ret = PTR_ERR(di);
  3538. goto err;
  3539. }
  3540. if (!di) {
  3541. ret = -ENOENT;
  3542. goto err;
  3543. }
  3544. leaf = path->nodes[0];
  3545. btrfs_dir_item_key_to_cpu(leaf, di, &key);
  3546. ret = btrfs_delete_one_dir_name(trans, root, path, di);
  3547. if (ret)
  3548. goto err;
  3549. btrfs_release_path(path);
  3550. /*
  3551. * If we don't have dir index, we have to get it by looking up
  3552. * the inode ref, since we get the inode ref, remove it directly,
  3553. * it is unnecessary to do delayed deletion.
  3554. *
  3555. * But if we have dir index, needn't search inode ref to get it.
  3556. * Since the inode ref is close to the inode item, it is better
  3557. * that we delay to delete it, and just do this deletion when
  3558. * we update the inode item.
  3559. */
  3560. if (BTRFS_I(inode)->dir_index) {
  3561. ret = btrfs_delayed_delete_inode_ref(inode);
  3562. if (!ret) {
  3563. index = BTRFS_I(inode)->dir_index;
  3564. goto skip_backref;
  3565. }
  3566. }
  3567. ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
  3568. dir_ino, &index);
  3569. if (ret) {
  3570. btrfs_info(fs_info,
  3571. "failed to delete reference to %.*s, inode %llu parent %llu",
  3572. name_len, name, ino, dir_ino);
  3573. btrfs_abort_transaction(trans, ret);
  3574. goto err;
  3575. }
  3576. skip_backref:
  3577. ret = btrfs_delete_delayed_dir_index(trans, fs_info, dir, index);
  3578. if (ret) {
  3579. btrfs_abort_transaction(trans, ret);
  3580. goto err;
  3581. }
  3582. ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
  3583. inode, dir_ino);
  3584. if (ret != 0 && ret != -ENOENT) {
  3585. btrfs_abort_transaction(trans, ret);
  3586. goto err;
  3587. }
  3588. ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
  3589. dir, index);
  3590. if (ret == -ENOENT)
  3591. ret = 0;
  3592. else if (ret)
  3593. btrfs_abort_transaction(trans, ret);
  3594. err:
  3595. btrfs_free_path(path);
  3596. if (ret)
  3597. goto out;
  3598. btrfs_i_size_write(dir, dir->i_size - name_len * 2);
  3599. inode_inc_iversion(inode);
  3600. inode_inc_iversion(dir);
  3601. inode->i_ctime = dir->i_mtime =
  3602. dir->i_ctime = current_time(inode);
  3603. ret = btrfs_update_inode(trans, root, dir);
  3604. out:
  3605. return ret;
  3606. }
  3607. int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
  3608. struct btrfs_root *root,
  3609. struct inode *dir, struct inode *inode,
  3610. const char *name, int name_len)
  3611. {
  3612. int ret;
  3613. ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
  3614. if (!ret) {
  3615. drop_nlink(inode);
  3616. ret = btrfs_update_inode(trans, root, inode);
  3617. }
  3618. return ret;
  3619. }
  3620. /*
  3621. * helper to start transaction for unlink and rmdir.
  3622. *
  3623. * unlink and rmdir are special in btrfs, they do not always free space, so
  3624. * if we cannot make our reservations the normal way try and see if there is
  3625. * plenty of slack room in the global reserve to migrate, otherwise we cannot
  3626. * allow the unlink to occur.
  3627. */
  3628. static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
  3629. {
  3630. struct btrfs_root *root = BTRFS_I(dir)->root;
  3631. /*
  3632. * 1 for the possible orphan item
  3633. * 1 for the dir item
  3634. * 1 for the dir index
  3635. * 1 for the inode ref
  3636. * 1 for the inode
  3637. */
  3638. return btrfs_start_transaction_fallback_global_rsv(root, 5, 5);
  3639. }
  3640. static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
  3641. {
  3642. struct btrfs_root *root = BTRFS_I(dir)->root;
  3643. struct btrfs_trans_handle *trans;
  3644. struct inode *inode = d_inode(dentry);
  3645. int ret;
  3646. trans = __unlink_start_trans(dir);
  3647. if (IS_ERR(trans))
  3648. return PTR_ERR(trans);
  3649. btrfs_record_unlink_dir(trans, dir, d_inode(dentry), 0);
  3650. ret = btrfs_unlink_inode(trans, root, dir, d_inode(dentry),
  3651. dentry->d_name.name, dentry->d_name.len);
  3652. if (ret)
  3653. goto out;
  3654. if (inode->i_nlink == 0) {
  3655. ret = btrfs_orphan_add(trans, inode);
  3656. if (ret)
  3657. goto out;
  3658. }
  3659. out:
  3660. btrfs_end_transaction(trans);
  3661. btrfs_btree_balance_dirty(root->fs_info);
  3662. return ret;
  3663. }
  3664. int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
  3665. struct btrfs_root *root,
  3666. struct inode *dir, u64 objectid,
  3667. const char *name, int name_len)
  3668. {
  3669. struct btrfs_fs_info *fs_info = root->fs_info;
  3670. struct btrfs_path *path;
  3671. struct extent_buffer *leaf;
  3672. struct btrfs_dir_item *di;
  3673. struct btrfs_key key;
  3674. u64 index;
  3675. int ret;
  3676. u64 dir_ino = btrfs_ino(dir);
  3677. path = btrfs_alloc_path();
  3678. if (!path)
  3679. return -ENOMEM;
  3680. di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
  3681. name, name_len, -1);
  3682. if (IS_ERR_OR_NULL(di)) {
  3683. if (!di)
  3684. ret = -ENOENT;
  3685. else
  3686. ret = PTR_ERR(di);
  3687. goto out;
  3688. }
  3689. leaf = path->nodes[0];
  3690. btrfs_dir_item_key_to_cpu(leaf, di, &key);
  3691. WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
  3692. ret = btrfs_delete_one_dir_name(trans, root, path, di);
  3693. if (ret) {
  3694. btrfs_abort_transaction(trans, ret);
  3695. goto out;
  3696. }
  3697. btrfs_release_path(path);
  3698. ret = btrfs_del_root_ref(trans, fs_info, objectid,
  3699. root->root_key.objectid, dir_ino,
  3700. &index, name, name_len);
  3701. if (ret < 0) {
  3702. if (ret != -ENOENT) {
  3703. btrfs_abort_transaction(trans, ret);
  3704. goto out;
  3705. }
  3706. di = btrfs_search_dir_index_item(root, path, dir_ino,
  3707. name, name_len);
  3708. if (IS_ERR_OR_NULL(di)) {
  3709. if (!di)
  3710. ret = -ENOENT;
  3711. else
  3712. ret = PTR_ERR(di);
  3713. btrfs_abort_transaction(trans, ret);
  3714. goto out;
  3715. }
  3716. leaf = path->nodes[0];
  3717. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  3718. btrfs_release_path(path);
  3719. index = key.offset;
  3720. }
  3721. btrfs_release_path(path);
  3722. ret = btrfs_delete_delayed_dir_index(trans, fs_info, dir, index);
  3723. if (ret) {
  3724. btrfs_abort_transaction(trans, ret);
  3725. goto out;
  3726. }
  3727. btrfs_i_size_write(dir, dir->i_size - name_len * 2);
  3728. inode_inc_iversion(dir);
  3729. dir->i_mtime = dir->i_ctime = current_time(dir);
  3730. ret = btrfs_update_inode_fallback(trans, root, dir);
  3731. if (ret)
  3732. btrfs_abort_transaction(trans, ret);
  3733. out:
  3734. btrfs_free_path(path);
  3735. return ret;
  3736. }
  3737. static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
  3738. {
  3739. struct inode *inode = d_inode(dentry);
  3740. int err = 0;
  3741. struct btrfs_root *root = BTRFS_I(dir)->root;
  3742. struct btrfs_trans_handle *trans;
  3743. u64 last_unlink_trans;
  3744. if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
  3745. return -ENOTEMPTY;
  3746. if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID)
  3747. return -EPERM;
  3748. trans = __unlink_start_trans(dir);
  3749. if (IS_ERR(trans))
  3750. return PTR_ERR(trans);
  3751. if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
  3752. err = btrfs_unlink_subvol(trans, root, dir,
  3753. BTRFS_I(inode)->location.objectid,
  3754. dentry->d_name.name,
  3755. dentry->d_name.len);
  3756. goto out;
  3757. }
  3758. err = btrfs_orphan_add(trans, inode);
  3759. if (err)
  3760. goto out;
  3761. last_unlink_trans = BTRFS_I(inode)->last_unlink_trans;
  3762. /* now the directory is empty */
  3763. err = btrfs_unlink_inode(trans, root, dir, d_inode(dentry),
  3764. dentry->d_name.name, dentry->d_name.len);
  3765. if (!err) {
  3766. btrfs_i_size_write(inode, 0);
  3767. /*
  3768. * Propagate the last_unlink_trans value of the deleted dir to
  3769. * its parent directory. This is to prevent an unrecoverable
  3770. * log tree in the case we do something like this:
  3771. * 1) create dir foo
  3772. * 2) create snapshot under dir foo
  3773. * 3) delete the snapshot
  3774. * 4) rmdir foo
  3775. * 5) mkdir foo
  3776. * 6) fsync foo or some file inside foo
  3777. */
  3778. if (last_unlink_trans >= trans->transid)
  3779. BTRFS_I(dir)->last_unlink_trans = last_unlink_trans;
  3780. }
  3781. out:
  3782. btrfs_end_transaction(trans);
  3783. btrfs_btree_balance_dirty(root->fs_info);
  3784. return err;
  3785. }
  3786. static int truncate_space_check(struct btrfs_trans_handle *trans,
  3787. struct btrfs_root *root,
  3788. u64 bytes_deleted)
  3789. {
  3790. struct btrfs_fs_info *fs_info = root->fs_info;
  3791. int ret;
  3792. /*
  3793. * This is only used to apply pressure to the enospc system, we don't
  3794. * intend to use this reservation at all.
  3795. */
  3796. bytes_deleted = btrfs_csum_bytes_to_leaves(fs_info, bytes_deleted);
  3797. bytes_deleted *= fs_info->nodesize;
  3798. ret = btrfs_block_rsv_add(root, &fs_info->trans_block_rsv,
  3799. bytes_deleted, BTRFS_RESERVE_NO_FLUSH);
  3800. if (!ret) {
  3801. trace_btrfs_space_reservation(fs_info, "transaction",
  3802. trans->transid,
  3803. bytes_deleted, 1);
  3804. trans->bytes_reserved += bytes_deleted;
  3805. }
  3806. return ret;
  3807. }
  3808. static int truncate_inline_extent(struct inode *inode,
  3809. struct btrfs_path *path,
  3810. struct btrfs_key *found_key,
  3811. const u64 item_end,
  3812. const u64 new_size)
  3813. {
  3814. struct extent_buffer *leaf = path->nodes[0];
  3815. int slot = path->slots[0];
  3816. struct btrfs_file_extent_item *fi;
  3817. u32 size = (u32)(new_size - found_key->offset);
  3818. struct btrfs_root *root = BTRFS_I(inode)->root;
  3819. fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
  3820. if (btrfs_file_extent_compression(leaf, fi) != BTRFS_COMPRESS_NONE) {
  3821. loff_t offset = new_size;
  3822. loff_t page_end = ALIGN(offset, PAGE_SIZE);
  3823. /*
  3824. * Zero out the remaining of the last page of our inline extent,
  3825. * instead of directly truncating our inline extent here - that
  3826. * would be much more complex (decompressing all the data, then
  3827. * compressing the truncated data, which might be bigger than
  3828. * the size of the inline extent, resize the extent, etc).
  3829. * We release the path because to get the page we might need to
  3830. * read the extent item from disk (data not in the page cache).
  3831. */
  3832. btrfs_release_path(path);
  3833. return btrfs_truncate_block(inode, offset, page_end - offset,
  3834. 0);
  3835. }
  3836. btrfs_set_file_extent_ram_bytes(leaf, fi, size);
  3837. size = btrfs_file_extent_calc_inline_size(size);
  3838. btrfs_truncate_item(root->fs_info, path, size, 1);
  3839. if (test_bit(BTRFS_ROOT_REF_COWS, &root->state))
  3840. inode_sub_bytes(inode, item_end + 1 - new_size);
  3841. return 0;
  3842. }
  3843. /*
  3844. * this can truncate away extent items, csum items and directory items.
  3845. * It starts at a high offset and removes keys until it can't find
  3846. * any higher than new_size
  3847. *
  3848. * csum items that cross the new i_size are truncated to the new size
  3849. * as well.
  3850. *
  3851. * min_type is the minimum key type to truncate down to. If set to 0, this
  3852. * will kill all the items on this inode, including the INODE_ITEM_KEY.
  3853. */
  3854. int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
  3855. struct btrfs_root *root,
  3856. struct inode *inode,
  3857. u64 new_size, u32 min_type)
  3858. {
  3859. struct btrfs_fs_info *fs_info = root->fs_info;
  3860. struct btrfs_path *path;
  3861. struct extent_buffer *leaf;
  3862. struct btrfs_file_extent_item *fi;
  3863. struct btrfs_key key;
  3864. struct btrfs_key found_key;
  3865. u64 extent_start = 0;
  3866. u64 extent_num_bytes = 0;
  3867. u64 extent_offset = 0;
  3868. u64 item_end = 0;
  3869. u64 last_size = new_size;
  3870. u32 found_type = (u8)-1;
  3871. int found_extent;
  3872. int del_item;
  3873. int pending_del_nr = 0;
  3874. int pending_del_slot = 0;
  3875. int extent_type = -1;
  3876. int ret;
  3877. int err = 0;
  3878. u64 ino = btrfs_ino(inode);
  3879. u64 bytes_deleted = 0;
  3880. bool be_nice = 0;
  3881. bool should_throttle = 0;
  3882. bool should_end = 0;
  3883. BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
  3884. /*
  3885. * for non-free space inodes and ref cows, we want to back off from
  3886. * time to time
  3887. */
  3888. if (!btrfs_is_free_space_inode(inode) &&
  3889. test_bit(BTRFS_ROOT_REF_COWS, &root->state))
  3890. be_nice = 1;
  3891. path = btrfs_alloc_path();
  3892. if (!path)
  3893. return -ENOMEM;
  3894. path->reada = READA_BACK;
  3895. /*
  3896. * We want to drop from the next block forward in case this new size is
  3897. * not block aligned since we will be keeping the last block of the
  3898. * extent just the way it is.
  3899. */
  3900. if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
  3901. root == fs_info->tree_root)
  3902. btrfs_drop_extent_cache(inode, ALIGN(new_size,
  3903. fs_info->sectorsize),
  3904. (u64)-1, 0);
  3905. /*
  3906. * This function is also used to drop the items in the log tree before
  3907. * we relog the inode, so if root != BTRFS_I(inode)->root, it means
  3908. * it is used to drop the loged items. So we shouldn't kill the delayed
  3909. * items.
  3910. */
  3911. if (min_type == 0 && root == BTRFS_I(inode)->root)
  3912. btrfs_kill_delayed_inode_items(inode);
  3913. key.objectid = ino;
  3914. key.offset = (u64)-1;
  3915. key.type = (u8)-1;
  3916. search_again:
  3917. /*
  3918. * with a 16K leaf size and 128MB extents, you can actually queue
  3919. * up a huge file in a single leaf. Most of the time that
  3920. * bytes_deleted is > 0, it will be huge by the time we get here
  3921. */
  3922. if (be_nice && bytes_deleted > SZ_32M) {
  3923. if (btrfs_should_end_transaction(trans)) {
  3924. err = -EAGAIN;
  3925. goto error;
  3926. }
  3927. }
  3928. path->leave_spinning = 1;
  3929. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  3930. if (ret < 0) {
  3931. err = ret;
  3932. goto out;
  3933. }
  3934. if (ret > 0) {
  3935. /* there are no items in the tree for us to truncate, we're
  3936. * done
  3937. */
  3938. if (path->slots[0] == 0)
  3939. goto out;
  3940. path->slots[0]--;
  3941. }
  3942. while (1) {
  3943. fi = NULL;
  3944. leaf = path->nodes[0];
  3945. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  3946. found_type = found_key.type;
  3947. if (found_key.objectid != ino)
  3948. break;
  3949. if (found_type < min_type)
  3950. break;
  3951. item_end = found_key.offset;
  3952. if (found_type == BTRFS_EXTENT_DATA_KEY) {
  3953. fi = btrfs_item_ptr(leaf, path->slots[0],
  3954. struct btrfs_file_extent_item);
  3955. extent_type = btrfs_file_extent_type(leaf, fi);
  3956. if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
  3957. item_end +=
  3958. btrfs_file_extent_num_bytes(leaf, fi);
  3959. } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
  3960. item_end += btrfs_file_extent_inline_len(leaf,
  3961. path->slots[0], fi);
  3962. }
  3963. item_end--;
  3964. }
  3965. if (found_type > min_type) {
  3966. del_item = 1;
  3967. } else {
  3968. if (item_end < new_size)
  3969. break;
  3970. if (found_key.offset >= new_size)
  3971. del_item = 1;
  3972. else
  3973. del_item = 0;
  3974. }
  3975. found_extent = 0;
  3976. /* FIXME, shrink the extent if the ref count is only 1 */
  3977. if (found_type != BTRFS_EXTENT_DATA_KEY)
  3978. goto delete;
  3979. if (del_item)
  3980. last_size = found_key.offset;
  3981. else
  3982. last_size = new_size;
  3983. if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
  3984. u64 num_dec;
  3985. extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
  3986. if (!del_item) {
  3987. u64 orig_num_bytes =
  3988. btrfs_file_extent_num_bytes(leaf, fi);
  3989. extent_num_bytes = ALIGN(new_size -
  3990. found_key.offset,
  3991. fs_info->sectorsize);
  3992. btrfs_set_file_extent_num_bytes(leaf, fi,
  3993. extent_num_bytes);
  3994. num_dec = (orig_num_bytes -
  3995. extent_num_bytes);
  3996. if (test_bit(BTRFS_ROOT_REF_COWS,
  3997. &root->state) &&
  3998. extent_start != 0)
  3999. inode_sub_bytes(inode, num_dec);
  4000. btrfs_mark_buffer_dirty(leaf);
  4001. } else {
  4002. extent_num_bytes =
  4003. btrfs_file_extent_disk_num_bytes(leaf,
  4004. fi);
  4005. extent_offset = found_key.offset -
  4006. btrfs_file_extent_offset(leaf, fi);
  4007. /* FIXME blocksize != 4096 */
  4008. num_dec = btrfs_file_extent_num_bytes(leaf, fi);
  4009. if (extent_start != 0) {
  4010. found_extent = 1;
  4011. if (test_bit(BTRFS_ROOT_REF_COWS,
  4012. &root->state))
  4013. inode_sub_bytes(inode, num_dec);
  4014. }
  4015. }
  4016. } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
  4017. /*
  4018. * we can't truncate inline items that have had
  4019. * special encodings
  4020. */
  4021. if (!del_item &&
  4022. btrfs_file_extent_encryption(leaf, fi) == 0 &&
  4023. btrfs_file_extent_other_encoding(leaf, fi) == 0) {
  4024. /*
  4025. * Need to release path in order to truncate a
  4026. * compressed extent. So delete any accumulated
  4027. * extent items so far.
  4028. */
  4029. if (btrfs_file_extent_compression(leaf, fi) !=
  4030. BTRFS_COMPRESS_NONE && pending_del_nr) {
  4031. err = btrfs_del_items(trans, root, path,
  4032. pending_del_slot,
  4033. pending_del_nr);
  4034. if (err) {
  4035. btrfs_abort_transaction(trans,
  4036. err);
  4037. goto error;
  4038. }
  4039. pending_del_nr = 0;
  4040. }
  4041. err = truncate_inline_extent(inode, path,
  4042. &found_key,
  4043. item_end,
  4044. new_size);
  4045. if (err) {
  4046. btrfs_abort_transaction(trans, err);
  4047. goto error;
  4048. }
  4049. } else if (test_bit(BTRFS_ROOT_REF_COWS,
  4050. &root->state)) {
  4051. inode_sub_bytes(inode, item_end + 1 - new_size);
  4052. }
  4053. }
  4054. delete:
  4055. if (del_item) {
  4056. if (!pending_del_nr) {
  4057. /* no pending yet, add ourselves */
  4058. pending_del_slot = path->slots[0];
  4059. pending_del_nr = 1;
  4060. } else if (pending_del_nr &&
  4061. path->slots[0] + 1 == pending_del_slot) {
  4062. /* hop on the pending chunk */
  4063. pending_del_nr++;
  4064. pending_del_slot = path->slots[0];
  4065. } else {
  4066. BUG();
  4067. }
  4068. } else {
  4069. break;
  4070. }
  4071. should_throttle = 0;
  4072. if (found_extent &&
  4073. (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
  4074. root == fs_info->tree_root)) {
  4075. btrfs_set_path_blocking(path);
  4076. bytes_deleted += extent_num_bytes;
  4077. ret = btrfs_free_extent(trans, fs_info, extent_start,
  4078. extent_num_bytes, 0,
  4079. btrfs_header_owner(leaf),
  4080. ino, extent_offset);
  4081. BUG_ON(ret);
  4082. if (btrfs_should_throttle_delayed_refs(trans, fs_info))
  4083. btrfs_async_run_delayed_refs(fs_info,
  4084. trans->delayed_ref_updates * 2,
  4085. trans->transid, 0);
  4086. if (be_nice) {
  4087. if (truncate_space_check(trans, root,
  4088. extent_num_bytes)) {
  4089. should_end = 1;
  4090. }
  4091. if (btrfs_should_throttle_delayed_refs(trans,
  4092. fs_info))
  4093. should_throttle = 1;
  4094. }
  4095. }
  4096. if (found_type == BTRFS_INODE_ITEM_KEY)
  4097. break;
  4098. if (path->slots[0] == 0 ||
  4099. path->slots[0] != pending_del_slot ||
  4100. should_throttle || should_end) {
  4101. if (pending_del_nr) {
  4102. ret = btrfs_del_items(trans, root, path,
  4103. pending_del_slot,
  4104. pending_del_nr);
  4105. if (ret) {
  4106. btrfs_abort_transaction(trans, ret);
  4107. goto error;
  4108. }
  4109. pending_del_nr = 0;
  4110. }
  4111. btrfs_release_path(path);
  4112. if (should_throttle) {
  4113. unsigned long updates = trans->delayed_ref_updates;
  4114. if (updates) {
  4115. trans->delayed_ref_updates = 0;
  4116. ret = btrfs_run_delayed_refs(trans,
  4117. fs_info,
  4118. updates * 2);
  4119. if (ret && !err)
  4120. err = ret;
  4121. }
  4122. }
  4123. /*
  4124. * if we failed to refill our space rsv, bail out
  4125. * and let the transaction restart
  4126. */
  4127. if (should_end) {
  4128. err = -EAGAIN;
  4129. goto error;
  4130. }
  4131. goto search_again;
  4132. } else {
  4133. path->slots[0]--;
  4134. }
  4135. }
  4136. out:
  4137. if (pending_del_nr) {
  4138. ret = btrfs_del_items(trans, root, path, pending_del_slot,
  4139. pending_del_nr);
  4140. if (ret)
  4141. btrfs_abort_transaction(trans, ret);
  4142. }
  4143. error:
  4144. if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
  4145. btrfs_ordered_update_i_size(inode, last_size, NULL);
  4146. btrfs_free_path(path);
  4147. if (be_nice && bytes_deleted > SZ_32M) {
  4148. unsigned long updates = trans->delayed_ref_updates;
  4149. if (updates) {
  4150. trans->delayed_ref_updates = 0;
  4151. ret = btrfs_run_delayed_refs(trans, fs_info,
  4152. updates * 2);
  4153. if (ret && !err)
  4154. err = ret;
  4155. }
  4156. }
  4157. return err;
  4158. }
  4159. /*
  4160. * btrfs_truncate_block - read, zero a chunk and write a block
  4161. * @inode - inode that we're zeroing
  4162. * @from - the offset to start zeroing
  4163. * @len - the length to zero, 0 to zero the entire range respective to the
  4164. * offset
  4165. * @front - zero up to the offset instead of from the offset on
  4166. *
  4167. * This will find the block for the "from" offset and cow the block and zero the
  4168. * part we want to zero. This is used with truncate and hole punching.
  4169. */
  4170. int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len,
  4171. int front)
  4172. {
  4173. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  4174. struct address_space *mapping = inode->i_mapping;
  4175. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  4176. struct btrfs_ordered_extent *ordered;
  4177. struct extent_state *cached_state = NULL;
  4178. char *kaddr;
  4179. u32 blocksize = fs_info->sectorsize;
  4180. pgoff_t index = from >> PAGE_SHIFT;
  4181. unsigned offset = from & (blocksize - 1);
  4182. struct page *page;
  4183. gfp_t mask = btrfs_alloc_write_mask(mapping);
  4184. int ret = 0;
  4185. u64 block_start;
  4186. u64 block_end;
  4187. if ((offset & (blocksize - 1)) == 0 &&
  4188. (!len || ((len & (blocksize - 1)) == 0)))
  4189. goto out;
  4190. ret = btrfs_delalloc_reserve_space(inode,
  4191. round_down(from, blocksize), blocksize);
  4192. if (ret)
  4193. goto out;
  4194. again:
  4195. page = find_or_create_page(mapping, index, mask);
  4196. if (!page) {
  4197. btrfs_delalloc_release_space(inode,
  4198. round_down(from, blocksize),
  4199. blocksize);
  4200. ret = -ENOMEM;
  4201. goto out;
  4202. }
  4203. block_start = round_down(from, blocksize);
  4204. block_end = block_start + blocksize - 1;
  4205. if (!PageUptodate(page)) {
  4206. ret = btrfs_readpage(NULL, page);
  4207. lock_page(page);
  4208. if (page->mapping != mapping) {
  4209. unlock_page(page);
  4210. put_page(page);
  4211. goto again;
  4212. }
  4213. if (!PageUptodate(page)) {
  4214. ret = -EIO;
  4215. goto out_unlock;
  4216. }
  4217. }
  4218. wait_on_page_writeback(page);
  4219. lock_extent_bits(io_tree, block_start, block_end, &cached_state);
  4220. set_page_extent_mapped(page);
  4221. ordered = btrfs_lookup_ordered_extent(inode, block_start);
  4222. if (ordered) {
  4223. unlock_extent_cached(io_tree, block_start, block_end,
  4224. &cached_state, GFP_NOFS);
  4225. unlock_page(page);
  4226. put_page(page);
  4227. btrfs_start_ordered_extent(inode, ordered, 1);
  4228. btrfs_put_ordered_extent(ordered);
  4229. goto again;
  4230. }
  4231. clear_extent_bit(&BTRFS_I(inode)->io_tree, block_start, block_end,
  4232. EXTENT_DIRTY | EXTENT_DELALLOC |
  4233. EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
  4234. 0, 0, &cached_state, GFP_NOFS);
  4235. ret = btrfs_set_extent_delalloc(inode, block_start, block_end,
  4236. &cached_state, 0);
  4237. if (ret) {
  4238. unlock_extent_cached(io_tree, block_start, block_end,
  4239. &cached_state, GFP_NOFS);
  4240. goto out_unlock;
  4241. }
  4242. if (offset != blocksize) {
  4243. if (!len)
  4244. len = blocksize - offset;
  4245. kaddr = kmap(page);
  4246. if (front)
  4247. memset(kaddr + (block_start - page_offset(page)),
  4248. 0, offset);
  4249. else
  4250. memset(kaddr + (block_start - page_offset(page)) + offset,
  4251. 0, len);
  4252. flush_dcache_page(page);
  4253. kunmap(page);
  4254. }
  4255. ClearPageChecked(page);
  4256. set_page_dirty(page);
  4257. unlock_extent_cached(io_tree, block_start, block_end, &cached_state,
  4258. GFP_NOFS);
  4259. out_unlock:
  4260. if (ret)
  4261. btrfs_delalloc_release_space(inode, block_start,
  4262. blocksize);
  4263. unlock_page(page);
  4264. put_page(page);
  4265. out:
  4266. return ret;
  4267. }
  4268. static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
  4269. u64 offset, u64 len)
  4270. {
  4271. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  4272. struct btrfs_trans_handle *trans;
  4273. int ret;
  4274. /*
  4275. * Still need to make sure the inode looks like it's been updated so
  4276. * that any holes get logged if we fsync.
  4277. */
  4278. if (btrfs_fs_incompat(fs_info, NO_HOLES)) {
  4279. BTRFS_I(inode)->last_trans = fs_info->generation;
  4280. BTRFS_I(inode)->last_sub_trans = root->log_transid;
  4281. BTRFS_I(inode)->last_log_commit = root->last_log_commit;
  4282. return 0;
  4283. }
  4284. /*
  4285. * 1 - for the one we're dropping
  4286. * 1 - for the one we're adding
  4287. * 1 - for updating the inode.
  4288. */
  4289. trans = btrfs_start_transaction(root, 3);
  4290. if (IS_ERR(trans))
  4291. return PTR_ERR(trans);
  4292. ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
  4293. if (ret) {
  4294. btrfs_abort_transaction(trans, ret);
  4295. btrfs_end_transaction(trans);
  4296. return ret;
  4297. }
  4298. ret = btrfs_insert_file_extent(trans, root, btrfs_ino(inode), offset,
  4299. 0, 0, len, 0, len, 0, 0, 0);
  4300. if (ret)
  4301. btrfs_abort_transaction(trans, ret);
  4302. else
  4303. btrfs_update_inode(trans, root, inode);
  4304. btrfs_end_transaction(trans);
  4305. return ret;
  4306. }
  4307. /*
  4308. * This function puts in dummy file extents for the area we're creating a hole
  4309. * for. So if we are truncating this file to a larger size we need to insert
  4310. * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
  4311. * the range between oldsize and size
  4312. */
  4313. int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
  4314. {
  4315. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  4316. struct btrfs_root *root = BTRFS_I(inode)->root;
  4317. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  4318. struct extent_map *em = NULL;
  4319. struct extent_state *cached_state = NULL;
  4320. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  4321. u64 hole_start = ALIGN(oldsize, fs_info->sectorsize);
  4322. u64 block_end = ALIGN(size, fs_info->sectorsize);
  4323. u64 last_byte;
  4324. u64 cur_offset;
  4325. u64 hole_size;
  4326. int err = 0;
  4327. /*
  4328. * If our size started in the middle of a block we need to zero out the
  4329. * rest of the block before we expand the i_size, otherwise we could
  4330. * expose stale data.
  4331. */
  4332. err = btrfs_truncate_block(inode, oldsize, 0, 0);
  4333. if (err)
  4334. return err;
  4335. if (size <= hole_start)
  4336. return 0;
  4337. while (1) {
  4338. struct btrfs_ordered_extent *ordered;
  4339. lock_extent_bits(io_tree, hole_start, block_end - 1,
  4340. &cached_state);
  4341. ordered = btrfs_lookup_ordered_range(inode, hole_start,
  4342. block_end - hole_start);
  4343. if (!ordered)
  4344. break;
  4345. unlock_extent_cached(io_tree, hole_start, block_end - 1,
  4346. &cached_state, GFP_NOFS);
  4347. btrfs_start_ordered_extent(inode, ordered, 1);
  4348. btrfs_put_ordered_extent(ordered);
  4349. }
  4350. cur_offset = hole_start;
  4351. while (1) {
  4352. em = btrfs_get_extent(inode, NULL, 0, cur_offset,
  4353. block_end - cur_offset, 0);
  4354. if (IS_ERR(em)) {
  4355. err = PTR_ERR(em);
  4356. em = NULL;
  4357. break;
  4358. }
  4359. last_byte = min(extent_map_end(em), block_end);
  4360. last_byte = ALIGN(last_byte, fs_info->sectorsize);
  4361. if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
  4362. struct extent_map *hole_em;
  4363. hole_size = last_byte - cur_offset;
  4364. err = maybe_insert_hole(root, inode, cur_offset,
  4365. hole_size);
  4366. if (err)
  4367. break;
  4368. btrfs_drop_extent_cache(inode, cur_offset,
  4369. cur_offset + hole_size - 1, 0);
  4370. hole_em = alloc_extent_map();
  4371. if (!hole_em) {
  4372. set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
  4373. &BTRFS_I(inode)->runtime_flags);
  4374. goto next;
  4375. }
  4376. hole_em->start = cur_offset;
  4377. hole_em->len = hole_size;
  4378. hole_em->orig_start = cur_offset;
  4379. hole_em->block_start = EXTENT_MAP_HOLE;
  4380. hole_em->block_len = 0;
  4381. hole_em->orig_block_len = 0;
  4382. hole_em->ram_bytes = hole_size;
  4383. hole_em->bdev = fs_info->fs_devices->latest_bdev;
  4384. hole_em->compress_type = BTRFS_COMPRESS_NONE;
  4385. hole_em->generation = fs_info->generation;
  4386. while (1) {
  4387. write_lock(&em_tree->lock);
  4388. err = add_extent_mapping(em_tree, hole_em, 1);
  4389. write_unlock(&em_tree->lock);
  4390. if (err != -EEXIST)
  4391. break;
  4392. btrfs_drop_extent_cache(inode, cur_offset,
  4393. cur_offset +
  4394. hole_size - 1, 0);
  4395. }
  4396. free_extent_map(hole_em);
  4397. }
  4398. next:
  4399. free_extent_map(em);
  4400. em = NULL;
  4401. cur_offset = last_byte;
  4402. if (cur_offset >= block_end)
  4403. break;
  4404. }
  4405. free_extent_map(em);
  4406. unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
  4407. GFP_NOFS);
  4408. return err;
  4409. }
  4410. static int btrfs_setsize(struct inode *inode, struct iattr *attr)
  4411. {
  4412. struct btrfs_root *root = BTRFS_I(inode)->root;
  4413. struct btrfs_trans_handle *trans;
  4414. loff_t oldsize = i_size_read(inode);
  4415. loff_t newsize = attr->ia_size;
  4416. int mask = attr->ia_valid;
  4417. int ret;
  4418. /*
  4419. * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
  4420. * special case where we need to update the times despite not having
  4421. * these flags set. For all other operations the VFS set these flags
  4422. * explicitly if it wants a timestamp update.
  4423. */
  4424. if (newsize != oldsize) {
  4425. inode_inc_iversion(inode);
  4426. if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
  4427. inode->i_ctime = inode->i_mtime =
  4428. current_time(inode);
  4429. }
  4430. if (newsize > oldsize) {
  4431. /*
  4432. * Don't do an expanding truncate while snapshoting is ongoing.
  4433. * This is to ensure the snapshot captures a fully consistent
  4434. * state of this file - if the snapshot captures this expanding
  4435. * truncation, it must capture all writes that happened before
  4436. * this truncation.
  4437. */
  4438. btrfs_wait_for_snapshot_creation(root);
  4439. ret = btrfs_cont_expand(inode, oldsize, newsize);
  4440. if (ret) {
  4441. btrfs_end_write_no_snapshoting(root);
  4442. return ret;
  4443. }
  4444. trans = btrfs_start_transaction(root, 1);
  4445. if (IS_ERR(trans)) {
  4446. btrfs_end_write_no_snapshoting(root);
  4447. return PTR_ERR(trans);
  4448. }
  4449. i_size_write(inode, newsize);
  4450. btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
  4451. pagecache_isize_extended(inode, oldsize, newsize);
  4452. ret = btrfs_update_inode(trans, root, inode);
  4453. btrfs_end_write_no_snapshoting(root);
  4454. btrfs_end_transaction(trans);
  4455. } else {
  4456. /*
  4457. * We're truncating a file that used to have good data down to
  4458. * zero. Make sure it gets into the ordered flush list so that
  4459. * any new writes get down to disk quickly.
  4460. */
  4461. if (newsize == 0)
  4462. set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
  4463. &BTRFS_I(inode)->runtime_flags);
  4464. /*
  4465. * 1 for the orphan item we're going to add
  4466. * 1 for the orphan item deletion.
  4467. */
  4468. trans = btrfs_start_transaction(root, 2);
  4469. if (IS_ERR(trans))
  4470. return PTR_ERR(trans);
  4471. /*
  4472. * We need to do this in case we fail at _any_ point during the
  4473. * actual truncate. Once we do the truncate_setsize we could
  4474. * invalidate pages which forces any outstanding ordered io to
  4475. * be instantly completed which will give us extents that need
  4476. * to be truncated. If we fail to get an orphan inode down we
  4477. * could have left over extents that were never meant to live,
  4478. * so we need to guarantee from this point on that everything
  4479. * will be consistent.
  4480. */
  4481. ret = btrfs_orphan_add(trans, inode);
  4482. btrfs_end_transaction(trans);
  4483. if (ret)
  4484. return ret;
  4485. /* we don't support swapfiles, so vmtruncate shouldn't fail */
  4486. truncate_setsize(inode, newsize);
  4487. /* Disable nonlocked read DIO to avoid the end less truncate */
  4488. btrfs_inode_block_unlocked_dio(inode);
  4489. inode_dio_wait(inode);
  4490. btrfs_inode_resume_unlocked_dio(inode);
  4491. ret = btrfs_truncate(inode);
  4492. if (ret && inode->i_nlink) {
  4493. int err;
  4494. /*
  4495. * failed to truncate, disk_i_size is only adjusted down
  4496. * as we remove extents, so it should represent the true
  4497. * size of the inode, so reset the in memory size and
  4498. * delete our orphan entry.
  4499. */
  4500. trans = btrfs_join_transaction(root);
  4501. if (IS_ERR(trans)) {
  4502. btrfs_orphan_del(NULL, inode);
  4503. return ret;
  4504. }
  4505. i_size_write(inode, BTRFS_I(inode)->disk_i_size);
  4506. err = btrfs_orphan_del(trans, inode);
  4507. if (err)
  4508. btrfs_abort_transaction(trans, err);
  4509. btrfs_end_transaction(trans);
  4510. }
  4511. }
  4512. return ret;
  4513. }
  4514. static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
  4515. {
  4516. struct inode *inode = d_inode(dentry);
  4517. struct btrfs_root *root = BTRFS_I(inode)->root;
  4518. int err;
  4519. if (btrfs_root_readonly(root))
  4520. return -EROFS;
  4521. err = setattr_prepare(dentry, attr);
  4522. if (err)
  4523. return err;
  4524. if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
  4525. err = btrfs_setsize(inode, attr);
  4526. if (err)
  4527. return err;
  4528. }
  4529. if (attr->ia_valid) {
  4530. setattr_copy(inode, attr);
  4531. inode_inc_iversion(inode);
  4532. err = btrfs_dirty_inode(inode);
  4533. if (!err && attr->ia_valid & ATTR_MODE)
  4534. err = posix_acl_chmod(inode, inode->i_mode);
  4535. }
  4536. return err;
  4537. }
  4538. /*
  4539. * While truncating the inode pages during eviction, we get the VFS calling
  4540. * btrfs_invalidatepage() against each page of the inode. This is slow because
  4541. * the calls to btrfs_invalidatepage() result in a huge amount of calls to
  4542. * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
  4543. * extent_state structures over and over, wasting lots of time.
  4544. *
  4545. * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
  4546. * those expensive operations on a per page basis and do only the ordered io
  4547. * finishing, while we release here the extent_map and extent_state structures,
  4548. * without the excessive merging and splitting.
  4549. */
  4550. static void evict_inode_truncate_pages(struct inode *inode)
  4551. {
  4552. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  4553. struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
  4554. struct rb_node *node;
  4555. ASSERT(inode->i_state & I_FREEING);
  4556. truncate_inode_pages_final(&inode->i_data);
  4557. write_lock(&map_tree->lock);
  4558. while (!RB_EMPTY_ROOT(&map_tree->map)) {
  4559. struct extent_map *em;
  4560. node = rb_first(&map_tree->map);
  4561. em = rb_entry(node, struct extent_map, rb_node);
  4562. clear_bit(EXTENT_FLAG_PINNED, &em->flags);
  4563. clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
  4564. remove_extent_mapping(map_tree, em);
  4565. free_extent_map(em);
  4566. if (need_resched()) {
  4567. write_unlock(&map_tree->lock);
  4568. cond_resched();
  4569. write_lock(&map_tree->lock);
  4570. }
  4571. }
  4572. write_unlock(&map_tree->lock);
  4573. /*
  4574. * Keep looping until we have no more ranges in the io tree.
  4575. * We can have ongoing bios started by readpages (called from readahead)
  4576. * that have their endio callback (extent_io.c:end_bio_extent_readpage)
  4577. * still in progress (unlocked the pages in the bio but did not yet
  4578. * unlocked the ranges in the io tree). Therefore this means some
  4579. * ranges can still be locked and eviction started because before
  4580. * submitting those bios, which are executed by a separate task (work
  4581. * queue kthread), inode references (inode->i_count) were not taken
  4582. * (which would be dropped in the end io callback of each bio).
  4583. * Therefore here we effectively end up waiting for those bios and
  4584. * anyone else holding locked ranges without having bumped the inode's
  4585. * reference count - if we don't do it, when they access the inode's
  4586. * io_tree to unlock a range it may be too late, leading to an
  4587. * use-after-free issue.
  4588. */
  4589. spin_lock(&io_tree->lock);
  4590. while (!RB_EMPTY_ROOT(&io_tree->state)) {
  4591. struct extent_state *state;
  4592. struct extent_state *cached_state = NULL;
  4593. u64 start;
  4594. u64 end;
  4595. node = rb_first(&io_tree->state);
  4596. state = rb_entry(node, struct extent_state, rb_node);
  4597. start = state->start;
  4598. end = state->end;
  4599. spin_unlock(&io_tree->lock);
  4600. lock_extent_bits(io_tree, start, end, &cached_state);
  4601. /*
  4602. * If still has DELALLOC flag, the extent didn't reach disk,
  4603. * and its reserved space won't be freed by delayed_ref.
  4604. * So we need to free its reserved space here.
  4605. * (Refer to comment in btrfs_invalidatepage, case 2)
  4606. *
  4607. * Note, end is the bytenr of last byte, so we need + 1 here.
  4608. */
  4609. if (state->state & EXTENT_DELALLOC)
  4610. btrfs_qgroup_free_data(inode, start, end - start + 1);
  4611. clear_extent_bit(io_tree, start, end,
  4612. EXTENT_LOCKED | EXTENT_DIRTY |
  4613. EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
  4614. EXTENT_DEFRAG, 1, 1,
  4615. &cached_state, GFP_NOFS);
  4616. cond_resched();
  4617. spin_lock(&io_tree->lock);
  4618. }
  4619. spin_unlock(&io_tree->lock);
  4620. }
  4621. void btrfs_evict_inode(struct inode *inode)
  4622. {
  4623. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  4624. struct btrfs_trans_handle *trans;
  4625. struct btrfs_root *root = BTRFS_I(inode)->root;
  4626. struct btrfs_block_rsv *rsv, *global_rsv;
  4627. int steal_from_global = 0;
  4628. u64 min_size;
  4629. int ret;
  4630. trace_btrfs_inode_evict(inode);
  4631. if (!root) {
  4632. kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
  4633. return;
  4634. }
  4635. min_size = btrfs_calc_trunc_metadata_size(fs_info, 1);
  4636. evict_inode_truncate_pages(inode);
  4637. if (inode->i_nlink &&
  4638. ((btrfs_root_refs(&root->root_item) != 0 &&
  4639. root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
  4640. btrfs_is_free_space_inode(inode)))
  4641. goto no_delete;
  4642. if (is_bad_inode(inode)) {
  4643. btrfs_orphan_del(NULL, inode);
  4644. goto no_delete;
  4645. }
  4646. /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
  4647. if (!special_file(inode->i_mode))
  4648. btrfs_wait_ordered_range(inode, 0, (u64)-1);
  4649. btrfs_free_io_failure_record(inode, 0, (u64)-1);
  4650. if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
  4651. BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
  4652. &BTRFS_I(inode)->runtime_flags));
  4653. goto no_delete;
  4654. }
  4655. if (inode->i_nlink > 0) {
  4656. BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
  4657. root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
  4658. goto no_delete;
  4659. }
  4660. ret = btrfs_commit_inode_delayed_inode(inode);
  4661. if (ret) {
  4662. btrfs_orphan_del(NULL, inode);
  4663. goto no_delete;
  4664. }
  4665. rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
  4666. if (!rsv) {
  4667. btrfs_orphan_del(NULL, inode);
  4668. goto no_delete;
  4669. }
  4670. rsv->size = min_size;
  4671. rsv->failfast = 1;
  4672. global_rsv = &fs_info->global_block_rsv;
  4673. btrfs_i_size_write(inode, 0);
  4674. /*
  4675. * This is a bit simpler than btrfs_truncate since we've already
  4676. * reserved our space for our orphan item in the unlink, so we just
  4677. * need to reserve some slack space in case we add bytes and update
  4678. * inode item when doing the truncate.
  4679. */
  4680. while (1) {
  4681. ret = btrfs_block_rsv_refill(root, rsv, min_size,
  4682. BTRFS_RESERVE_FLUSH_LIMIT);
  4683. /*
  4684. * Try and steal from the global reserve since we will
  4685. * likely not use this space anyway, we want to try as
  4686. * hard as possible to get this to work.
  4687. */
  4688. if (ret)
  4689. steal_from_global++;
  4690. else
  4691. steal_from_global = 0;
  4692. ret = 0;
  4693. /*
  4694. * steal_from_global == 0: we reserved stuff, hooray!
  4695. * steal_from_global == 1: we didn't reserve stuff, boo!
  4696. * steal_from_global == 2: we've committed, still not a lot of
  4697. * room but maybe we'll have room in the global reserve this
  4698. * time.
  4699. * steal_from_global == 3: abandon all hope!
  4700. */
  4701. if (steal_from_global > 2) {
  4702. btrfs_warn(fs_info,
  4703. "Could not get space for a delete, will truncate on mount %d",
  4704. ret);
  4705. btrfs_orphan_del(NULL, inode);
  4706. btrfs_free_block_rsv(fs_info, rsv);
  4707. goto no_delete;
  4708. }
  4709. trans = btrfs_join_transaction(root);
  4710. if (IS_ERR(trans)) {
  4711. btrfs_orphan_del(NULL, inode);
  4712. btrfs_free_block_rsv(fs_info, rsv);
  4713. goto no_delete;
  4714. }
  4715. /*
  4716. * We can't just steal from the global reserve, we need to make
  4717. * sure there is room to do it, if not we need to commit and try
  4718. * again.
  4719. */
  4720. if (steal_from_global) {
  4721. if (!btrfs_check_space_for_delayed_refs(trans, fs_info))
  4722. ret = btrfs_block_rsv_migrate(global_rsv, rsv,
  4723. min_size, 0);
  4724. else
  4725. ret = -ENOSPC;
  4726. }
  4727. /*
  4728. * Couldn't steal from the global reserve, we have too much
  4729. * pending stuff built up, commit the transaction and try it
  4730. * again.
  4731. */
  4732. if (ret) {
  4733. ret = btrfs_commit_transaction(trans);
  4734. if (ret) {
  4735. btrfs_orphan_del(NULL, inode);
  4736. btrfs_free_block_rsv(fs_info, rsv);
  4737. goto no_delete;
  4738. }
  4739. continue;
  4740. } else {
  4741. steal_from_global = 0;
  4742. }
  4743. trans->block_rsv = rsv;
  4744. ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
  4745. if (ret != -ENOSPC && ret != -EAGAIN)
  4746. break;
  4747. trans->block_rsv = &fs_info->trans_block_rsv;
  4748. btrfs_end_transaction(trans);
  4749. trans = NULL;
  4750. btrfs_btree_balance_dirty(fs_info);
  4751. }
  4752. btrfs_free_block_rsv(fs_info, rsv);
  4753. /*
  4754. * Errors here aren't a big deal, it just means we leave orphan items
  4755. * in the tree. They will be cleaned up on the next mount.
  4756. */
  4757. if (ret == 0) {
  4758. trans->block_rsv = root->orphan_block_rsv;
  4759. btrfs_orphan_del(trans, inode);
  4760. } else {
  4761. btrfs_orphan_del(NULL, inode);
  4762. }
  4763. trans->block_rsv = &fs_info->trans_block_rsv;
  4764. if (!(root == fs_info->tree_root ||
  4765. root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
  4766. btrfs_return_ino(root, btrfs_ino(inode));
  4767. btrfs_end_transaction(trans);
  4768. btrfs_btree_balance_dirty(fs_info);
  4769. no_delete:
  4770. btrfs_remove_delayed_node(inode);
  4771. clear_inode(inode);
  4772. }
  4773. /*
  4774. * this returns the key found in the dir entry in the location pointer.
  4775. * If no dir entries were found, location->objectid is 0.
  4776. */
  4777. static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
  4778. struct btrfs_key *location)
  4779. {
  4780. const char *name = dentry->d_name.name;
  4781. int namelen = dentry->d_name.len;
  4782. struct btrfs_dir_item *di;
  4783. struct btrfs_path *path;
  4784. struct btrfs_root *root = BTRFS_I(dir)->root;
  4785. int ret = 0;
  4786. path = btrfs_alloc_path();
  4787. if (!path)
  4788. return -ENOMEM;
  4789. di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name,
  4790. namelen, 0);
  4791. if (IS_ERR(di))
  4792. ret = PTR_ERR(di);
  4793. if (IS_ERR_OR_NULL(di))
  4794. goto out_err;
  4795. btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
  4796. out:
  4797. btrfs_free_path(path);
  4798. return ret;
  4799. out_err:
  4800. location->objectid = 0;
  4801. goto out;
  4802. }
  4803. /*
  4804. * when we hit a tree root in a directory, the btrfs part of the inode
  4805. * needs to be changed to reflect the root directory of the tree root. This
  4806. * is kind of like crossing a mount point.
  4807. */
  4808. static int fixup_tree_root_location(struct btrfs_fs_info *fs_info,
  4809. struct inode *dir,
  4810. struct dentry *dentry,
  4811. struct btrfs_key *location,
  4812. struct btrfs_root **sub_root)
  4813. {
  4814. struct btrfs_path *path;
  4815. struct btrfs_root *new_root;
  4816. struct btrfs_root_ref *ref;
  4817. struct extent_buffer *leaf;
  4818. struct btrfs_key key;
  4819. int ret;
  4820. int err = 0;
  4821. path = btrfs_alloc_path();
  4822. if (!path) {
  4823. err = -ENOMEM;
  4824. goto out;
  4825. }
  4826. err = -ENOENT;
  4827. key.objectid = BTRFS_I(dir)->root->root_key.objectid;
  4828. key.type = BTRFS_ROOT_REF_KEY;
  4829. key.offset = location->objectid;
  4830. ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
  4831. if (ret) {
  4832. if (ret < 0)
  4833. err = ret;
  4834. goto out;
  4835. }
  4836. leaf = path->nodes[0];
  4837. ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
  4838. if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) ||
  4839. btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
  4840. goto out;
  4841. ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
  4842. (unsigned long)(ref + 1),
  4843. dentry->d_name.len);
  4844. if (ret)
  4845. goto out;
  4846. btrfs_release_path(path);
  4847. new_root = btrfs_read_fs_root_no_name(fs_info, location);
  4848. if (IS_ERR(new_root)) {
  4849. err = PTR_ERR(new_root);
  4850. goto out;
  4851. }
  4852. *sub_root = new_root;
  4853. location->objectid = btrfs_root_dirid(&new_root->root_item);
  4854. location->type = BTRFS_INODE_ITEM_KEY;
  4855. location->offset = 0;
  4856. err = 0;
  4857. out:
  4858. btrfs_free_path(path);
  4859. return err;
  4860. }
  4861. static void inode_tree_add(struct inode *inode)
  4862. {
  4863. struct btrfs_root *root = BTRFS_I(inode)->root;
  4864. struct btrfs_inode *entry;
  4865. struct rb_node **p;
  4866. struct rb_node *parent;
  4867. struct rb_node *new = &BTRFS_I(inode)->rb_node;
  4868. u64 ino = btrfs_ino(inode);
  4869. if (inode_unhashed(inode))
  4870. return;
  4871. parent = NULL;
  4872. spin_lock(&root->inode_lock);
  4873. p = &root->inode_tree.rb_node;
  4874. while (*p) {
  4875. parent = *p;
  4876. entry = rb_entry(parent, struct btrfs_inode, rb_node);
  4877. if (ino < btrfs_ino(&entry->vfs_inode))
  4878. p = &parent->rb_left;
  4879. else if (ino > btrfs_ino(&entry->vfs_inode))
  4880. p = &parent->rb_right;
  4881. else {
  4882. WARN_ON(!(entry->vfs_inode.i_state &
  4883. (I_WILL_FREE | I_FREEING)));
  4884. rb_replace_node(parent, new, &root->inode_tree);
  4885. RB_CLEAR_NODE(parent);
  4886. spin_unlock(&root->inode_lock);
  4887. return;
  4888. }
  4889. }
  4890. rb_link_node(new, parent, p);
  4891. rb_insert_color(new, &root->inode_tree);
  4892. spin_unlock(&root->inode_lock);
  4893. }
  4894. static void inode_tree_del(struct inode *inode)
  4895. {
  4896. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  4897. struct btrfs_root *root = BTRFS_I(inode)->root;
  4898. int empty = 0;
  4899. spin_lock(&root->inode_lock);
  4900. if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
  4901. rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
  4902. RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
  4903. empty = RB_EMPTY_ROOT(&root->inode_tree);
  4904. }
  4905. spin_unlock(&root->inode_lock);
  4906. if (empty && btrfs_root_refs(&root->root_item) == 0) {
  4907. synchronize_srcu(&fs_info->subvol_srcu);
  4908. spin_lock(&root->inode_lock);
  4909. empty = RB_EMPTY_ROOT(&root->inode_tree);
  4910. spin_unlock(&root->inode_lock);
  4911. if (empty)
  4912. btrfs_add_dead_root(root);
  4913. }
  4914. }
  4915. void btrfs_invalidate_inodes(struct btrfs_root *root)
  4916. {
  4917. struct btrfs_fs_info *fs_info = root->fs_info;
  4918. struct rb_node *node;
  4919. struct rb_node *prev;
  4920. struct btrfs_inode *entry;
  4921. struct inode *inode;
  4922. u64 objectid = 0;
  4923. if (!test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
  4924. WARN_ON(btrfs_root_refs(&root->root_item) != 0);
  4925. spin_lock(&root->inode_lock);
  4926. again:
  4927. node = root->inode_tree.rb_node;
  4928. prev = NULL;
  4929. while (node) {
  4930. prev = node;
  4931. entry = rb_entry(node, struct btrfs_inode, rb_node);
  4932. if (objectid < btrfs_ino(&entry->vfs_inode))
  4933. node = node->rb_left;
  4934. else if (objectid > btrfs_ino(&entry->vfs_inode))
  4935. node = node->rb_right;
  4936. else
  4937. break;
  4938. }
  4939. if (!node) {
  4940. while (prev) {
  4941. entry = rb_entry(prev, struct btrfs_inode, rb_node);
  4942. if (objectid <= btrfs_ino(&entry->vfs_inode)) {
  4943. node = prev;
  4944. break;
  4945. }
  4946. prev = rb_next(prev);
  4947. }
  4948. }
  4949. while (node) {
  4950. entry = rb_entry(node, struct btrfs_inode, rb_node);
  4951. objectid = btrfs_ino(&entry->vfs_inode) + 1;
  4952. inode = igrab(&entry->vfs_inode);
  4953. if (inode) {
  4954. spin_unlock(&root->inode_lock);
  4955. if (atomic_read(&inode->i_count) > 1)
  4956. d_prune_aliases(inode);
  4957. /*
  4958. * btrfs_drop_inode will have it removed from
  4959. * the inode cache when its usage count
  4960. * hits zero.
  4961. */
  4962. iput(inode);
  4963. cond_resched();
  4964. spin_lock(&root->inode_lock);
  4965. goto again;
  4966. }
  4967. if (cond_resched_lock(&root->inode_lock))
  4968. goto again;
  4969. node = rb_next(node);
  4970. }
  4971. spin_unlock(&root->inode_lock);
  4972. }
  4973. static int btrfs_init_locked_inode(struct inode *inode, void *p)
  4974. {
  4975. struct btrfs_iget_args *args = p;
  4976. inode->i_ino = args->location->objectid;
  4977. memcpy(&BTRFS_I(inode)->location, args->location,
  4978. sizeof(*args->location));
  4979. BTRFS_I(inode)->root = args->root;
  4980. return 0;
  4981. }
  4982. static int btrfs_find_actor(struct inode *inode, void *opaque)
  4983. {
  4984. struct btrfs_iget_args *args = opaque;
  4985. return args->location->objectid == BTRFS_I(inode)->location.objectid &&
  4986. args->root == BTRFS_I(inode)->root;
  4987. }
  4988. static struct inode *btrfs_iget_locked(struct super_block *s,
  4989. struct btrfs_key *location,
  4990. struct btrfs_root *root)
  4991. {
  4992. struct inode *inode;
  4993. struct btrfs_iget_args args;
  4994. unsigned long hashval = btrfs_inode_hash(location->objectid, root);
  4995. args.location = location;
  4996. args.root = root;
  4997. inode = iget5_locked(s, hashval, btrfs_find_actor,
  4998. btrfs_init_locked_inode,
  4999. (void *)&args);
  5000. return inode;
  5001. }
  5002. /* Get an inode object given its location and corresponding root.
  5003. * Returns in *is_new if the inode was read from disk
  5004. */
  5005. struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
  5006. struct btrfs_root *root, int *new)
  5007. {
  5008. struct inode *inode;
  5009. inode = btrfs_iget_locked(s, location, root);
  5010. if (!inode)
  5011. return ERR_PTR(-ENOMEM);
  5012. if (inode->i_state & I_NEW) {
  5013. int ret;
  5014. ret = btrfs_read_locked_inode(inode);
  5015. if (!is_bad_inode(inode)) {
  5016. inode_tree_add(inode);
  5017. unlock_new_inode(inode);
  5018. if (new)
  5019. *new = 1;
  5020. } else {
  5021. unlock_new_inode(inode);
  5022. iput(inode);
  5023. ASSERT(ret < 0);
  5024. inode = ERR_PTR(ret < 0 ? ret : -ESTALE);
  5025. }
  5026. }
  5027. return inode;
  5028. }
  5029. static struct inode *new_simple_dir(struct super_block *s,
  5030. struct btrfs_key *key,
  5031. struct btrfs_root *root)
  5032. {
  5033. struct inode *inode = new_inode(s);
  5034. if (!inode)
  5035. return ERR_PTR(-ENOMEM);
  5036. BTRFS_I(inode)->root = root;
  5037. memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
  5038. set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
  5039. inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
  5040. inode->i_op = &btrfs_dir_ro_inode_operations;
  5041. inode->i_fop = &simple_dir_operations;
  5042. inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
  5043. inode->i_mtime = current_time(inode);
  5044. inode->i_atime = inode->i_mtime;
  5045. inode->i_ctime = inode->i_mtime;
  5046. BTRFS_I(inode)->i_otime = inode->i_mtime;
  5047. return inode;
  5048. }
  5049. struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
  5050. {
  5051. struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
  5052. struct inode *inode;
  5053. struct btrfs_root *root = BTRFS_I(dir)->root;
  5054. struct btrfs_root *sub_root = root;
  5055. struct btrfs_key location;
  5056. int index;
  5057. int ret = 0;
  5058. if (dentry->d_name.len > BTRFS_NAME_LEN)
  5059. return ERR_PTR(-ENAMETOOLONG);
  5060. ret = btrfs_inode_by_name(dir, dentry, &location);
  5061. if (ret < 0)
  5062. return ERR_PTR(ret);
  5063. if (location.objectid == 0)
  5064. return ERR_PTR(-ENOENT);
  5065. if (location.type == BTRFS_INODE_ITEM_KEY) {
  5066. inode = btrfs_iget(dir->i_sb, &location, root, NULL);
  5067. return inode;
  5068. }
  5069. BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
  5070. index = srcu_read_lock(&fs_info->subvol_srcu);
  5071. ret = fixup_tree_root_location(fs_info, dir, dentry,
  5072. &location, &sub_root);
  5073. if (ret < 0) {
  5074. if (ret != -ENOENT)
  5075. inode = ERR_PTR(ret);
  5076. else
  5077. inode = new_simple_dir(dir->i_sb, &location, sub_root);
  5078. } else {
  5079. inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
  5080. }
  5081. srcu_read_unlock(&fs_info->subvol_srcu, index);
  5082. if (!IS_ERR(inode) && root != sub_root) {
  5083. down_read(&fs_info->cleanup_work_sem);
  5084. if (!(inode->i_sb->s_flags & MS_RDONLY))
  5085. ret = btrfs_orphan_cleanup(sub_root);
  5086. up_read(&fs_info->cleanup_work_sem);
  5087. if (ret) {
  5088. iput(inode);
  5089. inode = ERR_PTR(ret);
  5090. }
  5091. }
  5092. return inode;
  5093. }
  5094. static int btrfs_dentry_delete(const struct dentry *dentry)
  5095. {
  5096. struct btrfs_root *root;
  5097. struct inode *inode = d_inode(dentry);
  5098. if (!inode && !IS_ROOT(dentry))
  5099. inode = d_inode(dentry->d_parent);
  5100. if (inode) {
  5101. root = BTRFS_I(inode)->root;
  5102. if (btrfs_root_refs(&root->root_item) == 0)
  5103. return 1;
  5104. if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
  5105. return 1;
  5106. }
  5107. return 0;
  5108. }
  5109. static void btrfs_dentry_release(struct dentry *dentry)
  5110. {
  5111. kfree(dentry->d_fsdata);
  5112. }
  5113. static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
  5114. unsigned int flags)
  5115. {
  5116. struct inode *inode;
  5117. inode = btrfs_lookup_dentry(dir, dentry);
  5118. if (IS_ERR(inode)) {
  5119. if (PTR_ERR(inode) == -ENOENT)
  5120. inode = NULL;
  5121. else
  5122. return ERR_CAST(inode);
  5123. }
  5124. return d_splice_alias(inode, dentry);
  5125. }
  5126. unsigned char btrfs_filetype_table[] = {
  5127. DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
  5128. };
  5129. static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
  5130. {
  5131. struct inode *inode = file_inode(file);
  5132. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  5133. struct btrfs_root *root = BTRFS_I(inode)->root;
  5134. struct btrfs_item *item;
  5135. struct btrfs_dir_item *di;
  5136. struct btrfs_key key;
  5137. struct btrfs_key found_key;
  5138. struct btrfs_path *path;
  5139. struct list_head ins_list;
  5140. struct list_head del_list;
  5141. int ret;
  5142. struct extent_buffer *leaf;
  5143. int slot;
  5144. unsigned char d_type;
  5145. int over = 0;
  5146. char tmp_name[32];
  5147. char *name_ptr;
  5148. int name_len;
  5149. bool put = false;
  5150. struct btrfs_key location;
  5151. if (!dir_emit_dots(file, ctx))
  5152. return 0;
  5153. path = btrfs_alloc_path();
  5154. if (!path)
  5155. return -ENOMEM;
  5156. path->reada = READA_FORWARD;
  5157. INIT_LIST_HEAD(&ins_list);
  5158. INIT_LIST_HEAD(&del_list);
  5159. put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list);
  5160. key.type = BTRFS_DIR_INDEX_KEY;
  5161. key.offset = ctx->pos;
  5162. key.objectid = btrfs_ino(inode);
  5163. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  5164. if (ret < 0)
  5165. goto err;
  5166. while (1) {
  5167. leaf = path->nodes[0];
  5168. slot = path->slots[0];
  5169. if (slot >= btrfs_header_nritems(leaf)) {
  5170. ret = btrfs_next_leaf(root, path);
  5171. if (ret < 0)
  5172. goto err;
  5173. else if (ret > 0)
  5174. break;
  5175. continue;
  5176. }
  5177. item = btrfs_item_nr(slot);
  5178. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  5179. if (found_key.objectid != key.objectid)
  5180. break;
  5181. if (found_key.type != BTRFS_DIR_INDEX_KEY)
  5182. break;
  5183. if (found_key.offset < ctx->pos)
  5184. goto next;
  5185. if (btrfs_should_delete_dir_index(&del_list, found_key.offset))
  5186. goto next;
  5187. ctx->pos = found_key.offset;
  5188. di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
  5189. if (verify_dir_item(fs_info, leaf, di))
  5190. goto next;
  5191. name_len = btrfs_dir_name_len(leaf, di);
  5192. if (name_len <= sizeof(tmp_name)) {
  5193. name_ptr = tmp_name;
  5194. } else {
  5195. name_ptr = kmalloc(name_len, GFP_KERNEL);
  5196. if (!name_ptr) {
  5197. ret = -ENOMEM;
  5198. goto err;
  5199. }
  5200. }
  5201. read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1),
  5202. name_len);
  5203. d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
  5204. btrfs_dir_item_key_to_cpu(leaf, di, &location);
  5205. over = !dir_emit(ctx, name_ptr, name_len, location.objectid,
  5206. d_type);
  5207. if (name_ptr != tmp_name)
  5208. kfree(name_ptr);
  5209. if (over)
  5210. goto nopos;
  5211. ctx->pos++;
  5212. next:
  5213. path->slots[0]++;
  5214. }
  5215. ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
  5216. if (ret)
  5217. goto nopos;
  5218. /*
  5219. * Stop new entries from being returned after we return the last
  5220. * entry.
  5221. *
  5222. * New directory entries are assigned a strictly increasing
  5223. * offset. This means that new entries created during readdir
  5224. * are *guaranteed* to be seen in the future by that readdir.
  5225. * This has broken buggy programs which operate on names as
  5226. * they're returned by readdir. Until we re-use freed offsets
  5227. * we have this hack to stop new entries from being returned
  5228. * under the assumption that they'll never reach this huge
  5229. * offset.
  5230. *
  5231. * This is being careful not to overflow 32bit loff_t unless the
  5232. * last entry requires it because doing so has broken 32bit apps
  5233. * in the past.
  5234. */
  5235. if (ctx->pos >= INT_MAX)
  5236. ctx->pos = LLONG_MAX;
  5237. else
  5238. ctx->pos = INT_MAX;
  5239. nopos:
  5240. ret = 0;
  5241. err:
  5242. if (put)
  5243. btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list);
  5244. btrfs_free_path(path);
  5245. return ret;
  5246. }
  5247. int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
  5248. {
  5249. struct btrfs_root *root = BTRFS_I(inode)->root;
  5250. struct btrfs_trans_handle *trans;
  5251. int ret = 0;
  5252. bool nolock = false;
  5253. if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
  5254. return 0;
  5255. if (btrfs_fs_closing(root->fs_info) && btrfs_is_free_space_inode(inode))
  5256. nolock = true;
  5257. if (wbc->sync_mode == WB_SYNC_ALL) {
  5258. if (nolock)
  5259. trans = btrfs_join_transaction_nolock(root);
  5260. else
  5261. trans = btrfs_join_transaction(root);
  5262. if (IS_ERR(trans))
  5263. return PTR_ERR(trans);
  5264. ret = btrfs_commit_transaction(trans);
  5265. }
  5266. return ret;
  5267. }
  5268. /*
  5269. * This is somewhat expensive, updating the tree every time the
  5270. * inode changes. But, it is most likely to find the inode in cache.
  5271. * FIXME, needs more benchmarking...there are no reasons other than performance
  5272. * to keep or drop this code.
  5273. */
  5274. static int btrfs_dirty_inode(struct inode *inode)
  5275. {
  5276. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  5277. struct btrfs_root *root = BTRFS_I(inode)->root;
  5278. struct btrfs_trans_handle *trans;
  5279. int ret;
  5280. if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
  5281. return 0;
  5282. trans = btrfs_join_transaction(root);
  5283. if (IS_ERR(trans))
  5284. return PTR_ERR(trans);
  5285. ret = btrfs_update_inode(trans, root, inode);
  5286. if (ret && ret == -ENOSPC) {
  5287. /* whoops, lets try again with the full transaction */
  5288. btrfs_end_transaction(trans);
  5289. trans = btrfs_start_transaction(root, 1);
  5290. if (IS_ERR(trans))
  5291. return PTR_ERR(trans);
  5292. ret = btrfs_update_inode(trans, root, inode);
  5293. }
  5294. btrfs_end_transaction(trans);
  5295. if (BTRFS_I(inode)->delayed_node)
  5296. btrfs_balance_delayed_items(fs_info);
  5297. return ret;
  5298. }
  5299. /*
  5300. * This is a copy of file_update_time. We need this so we can return error on
  5301. * ENOSPC for updating the inode in the case of file write and mmap writes.
  5302. */
  5303. static int btrfs_update_time(struct inode *inode, struct timespec *now,
  5304. int flags)
  5305. {
  5306. struct btrfs_root *root = BTRFS_I(inode)->root;
  5307. if (btrfs_root_readonly(root))
  5308. return -EROFS;
  5309. if (flags & S_VERSION)
  5310. inode_inc_iversion(inode);
  5311. if (flags & S_CTIME)
  5312. inode->i_ctime = *now;
  5313. if (flags & S_MTIME)
  5314. inode->i_mtime = *now;
  5315. if (flags & S_ATIME)
  5316. inode->i_atime = *now;
  5317. return btrfs_dirty_inode(inode);
  5318. }
  5319. /*
  5320. * find the highest existing sequence number in a directory
  5321. * and then set the in-memory index_cnt variable to reflect
  5322. * free sequence numbers
  5323. */
  5324. static int btrfs_set_inode_index_count(struct inode *inode)
  5325. {
  5326. struct btrfs_root *root = BTRFS_I(inode)->root;
  5327. struct btrfs_key key, found_key;
  5328. struct btrfs_path *path;
  5329. struct extent_buffer *leaf;
  5330. int ret;
  5331. key.objectid = btrfs_ino(inode);
  5332. key.type = BTRFS_DIR_INDEX_KEY;
  5333. key.offset = (u64)-1;
  5334. path = btrfs_alloc_path();
  5335. if (!path)
  5336. return -ENOMEM;
  5337. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  5338. if (ret < 0)
  5339. goto out;
  5340. /* FIXME: we should be able to handle this */
  5341. if (ret == 0)
  5342. goto out;
  5343. ret = 0;
  5344. /*
  5345. * MAGIC NUMBER EXPLANATION:
  5346. * since we search a directory based on f_pos we have to start at 2
  5347. * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
  5348. * else has to start at 2
  5349. */
  5350. if (path->slots[0] == 0) {
  5351. BTRFS_I(inode)->index_cnt = 2;
  5352. goto out;
  5353. }
  5354. path->slots[0]--;
  5355. leaf = path->nodes[0];
  5356. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  5357. if (found_key.objectid != btrfs_ino(inode) ||
  5358. found_key.type != BTRFS_DIR_INDEX_KEY) {
  5359. BTRFS_I(inode)->index_cnt = 2;
  5360. goto out;
  5361. }
  5362. BTRFS_I(inode)->index_cnt = found_key.offset + 1;
  5363. out:
  5364. btrfs_free_path(path);
  5365. return ret;
  5366. }
  5367. /*
  5368. * helper to find a free sequence number in a given directory. This current
  5369. * code is very simple, later versions will do smarter things in the btree
  5370. */
  5371. int btrfs_set_inode_index(struct inode *dir, u64 *index)
  5372. {
  5373. int ret = 0;
  5374. if (BTRFS_I(dir)->index_cnt == (u64)-1) {
  5375. ret = btrfs_inode_delayed_dir_index_count(dir);
  5376. if (ret) {
  5377. ret = btrfs_set_inode_index_count(dir);
  5378. if (ret)
  5379. return ret;
  5380. }
  5381. }
  5382. *index = BTRFS_I(dir)->index_cnt;
  5383. BTRFS_I(dir)->index_cnt++;
  5384. return ret;
  5385. }
  5386. static int btrfs_insert_inode_locked(struct inode *inode)
  5387. {
  5388. struct btrfs_iget_args args;
  5389. args.location = &BTRFS_I(inode)->location;
  5390. args.root = BTRFS_I(inode)->root;
  5391. return insert_inode_locked4(inode,
  5392. btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
  5393. btrfs_find_actor, &args);
  5394. }
  5395. static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
  5396. struct btrfs_root *root,
  5397. struct inode *dir,
  5398. const char *name, int name_len,
  5399. u64 ref_objectid, u64 objectid,
  5400. umode_t mode, u64 *index)
  5401. {
  5402. struct btrfs_fs_info *fs_info = root->fs_info;
  5403. struct inode *inode;
  5404. struct btrfs_inode_item *inode_item;
  5405. struct btrfs_key *location;
  5406. struct btrfs_path *path;
  5407. struct btrfs_inode_ref *ref;
  5408. struct btrfs_key key[2];
  5409. u32 sizes[2];
  5410. int nitems = name ? 2 : 1;
  5411. unsigned long ptr;
  5412. int ret;
  5413. path = btrfs_alloc_path();
  5414. if (!path)
  5415. return ERR_PTR(-ENOMEM);
  5416. inode = new_inode(fs_info->sb);
  5417. if (!inode) {
  5418. btrfs_free_path(path);
  5419. return ERR_PTR(-ENOMEM);
  5420. }
  5421. /*
  5422. * O_TMPFILE, set link count to 0, so that after this point,
  5423. * we fill in an inode item with the correct link count.
  5424. */
  5425. if (!name)
  5426. set_nlink(inode, 0);
  5427. /*
  5428. * we have to initialize this early, so we can reclaim the inode
  5429. * number if we fail afterwards in this function.
  5430. */
  5431. inode->i_ino = objectid;
  5432. if (dir && name) {
  5433. trace_btrfs_inode_request(dir);
  5434. ret = btrfs_set_inode_index(dir, index);
  5435. if (ret) {
  5436. btrfs_free_path(path);
  5437. iput(inode);
  5438. return ERR_PTR(ret);
  5439. }
  5440. } else if (dir) {
  5441. *index = 0;
  5442. }
  5443. /*
  5444. * index_cnt is ignored for everything but a dir,
  5445. * btrfs_get_inode_index_count has an explanation for the magic
  5446. * number
  5447. */
  5448. BTRFS_I(inode)->index_cnt = 2;
  5449. BTRFS_I(inode)->dir_index = *index;
  5450. BTRFS_I(inode)->root = root;
  5451. BTRFS_I(inode)->generation = trans->transid;
  5452. inode->i_generation = BTRFS_I(inode)->generation;
  5453. /*
  5454. * We could have gotten an inode number from somebody who was fsynced
  5455. * and then removed in this same transaction, so let's just set full
  5456. * sync since it will be a full sync anyway and this will blow away the
  5457. * old info in the log.
  5458. */
  5459. set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
  5460. key[0].objectid = objectid;
  5461. key[0].type = BTRFS_INODE_ITEM_KEY;
  5462. key[0].offset = 0;
  5463. sizes[0] = sizeof(struct btrfs_inode_item);
  5464. if (name) {
  5465. /*
  5466. * Start new inodes with an inode_ref. This is slightly more
  5467. * efficient for small numbers of hard links since they will
  5468. * be packed into one item. Extended refs will kick in if we
  5469. * add more hard links than can fit in the ref item.
  5470. */
  5471. key[1].objectid = objectid;
  5472. key[1].type = BTRFS_INODE_REF_KEY;
  5473. key[1].offset = ref_objectid;
  5474. sizes[1] = name_len + sizeof(*ref);
  5475. }
  5476. location = &BTRFS_I(inode)->location;
  5477. location->objectid = objectid;
  5478. location->offset = 0;
  5479. location->type = BTRFS_INODE_ITEM_KEY;
  5480. ret = btrfs_insert_inode_locked(inode);
  5481. if (ret < 0)
  5482. goto fail;
  5483. path->leave_spinning = 1;
  5484. ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
  5485. if (ret != 0)
  5486. goto fail_unlock;
  5487. inode_init_owner(inode, dir, mode);
  5488. inode_set_bytes(inode, 0);
  5489. inode->i_mtime = current_time(inode);
  5490. inode->i_atime = inode->i_mtime;
  5491. inode->i_ctime = inode->i_mtime;
  5492. BTRFS_I(inode)->i_otime = inode->i_mtime;
  5493. inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  5494. struct btrfs_inode_item);
  5495. memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item,
  5496. sizeof(*inode_item));
  5497. fill_inode_item(trans, path->nodes[0], inode_item, inode);
  5498. if (name) {
  5499. ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
  5500. struct btrfs_inode_ref);
  5501. btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
  5502. btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
  5503. ptr = (unsigned long)(ref + 1);
  5504. write_extent_buffer(path->nodes[0], name, ptr, name_len);
  5505. }
  5506. btrfs_mark_buffer_dirty(path->nodes[0]);
  5507. btrfs_free_path(path);
  5508. btrfs_inherit_iflags(inode, dir);
  5509. if (S_ISREG(mode)) {
  5510. if (btrfs_test_opt(fs_info, NODATASUM))
  5511. BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
  5512. if (btrfs_test_opt(fs_info, NODATACOW))
  5513. BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
  5514. BTRFS_INODE_NODATASUM;
  5515. }
  5516. inode_tree_add(inode);
  5517. trace_btrfs_inode_new(inode);
  5518. btrfs_set_inode_last_trans(trans, inode);
  5519. btrfs_update_root_times(trans, root);
  5520. ret = btrfs_inode_inherit_props(trans, inode, dir);
  5521. if (ret)
  5522. btrfs_err(fs_info,
  5523. "error inheriting props for ino %llu (root %llu): %d",
  5524. btrfs_ino(inode), root->root_key.objectid, ret);
  5525. return inode;
  5526. fail_unlock:
  5527. unlock_new_inode(inode);
  5528. fail:
  5529. if (dir && name)
  5530. BTRFS_I(dir)->index_cnt--;
  5531. btrfs_free_path(path);
  5532. iput(inode);
  5533. return ERR_PTR(ret);
  5534. }
  5535. static inline u8 btrfs_inode_type(struct inode *inode)
  5536. {
  5537. return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
  5538. }
  5539. /*
  5540. * utility function to add 'inode' into 'parent_inode' with
  5541. * a give name and a given sequence number.
  5542. * if 'add_backref' is true, also insert a backref from the
  5543. * inode to the parent directory.
  5544. */
  5545. int btrfs_add_link(struct btrfs_trans_handle *trans,
  5546. struct inode *parent_inode, struct inode *inode,
  5547. const char *name, int name_len, int add_backref, u64 index)
  5548. {
  5549. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  5550. int ret = 0;
  5551. struct btrfs_key key;
  5552. struct btrfs_root *root = BTRFS_I(parent_inode)->root;
  5553. u64 ino = btrfs_ino(inode);
  5554. u64 parent_ino = btrfs_ino(parent_inode);
  5555. if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
  5556. memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
  5557. } else {
  5558. key.objectid = ino;
  5559. key.type = BTRFS_INODE_ITEM_KEY;
  5560. key.offset = 0;
  5561. }
  5562. if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
  5563. ret = btrfs_add_root_ref(trans, fs_info, key.objectid,
  5564. root->root_key.objectid, parent_ino,
  5565. index, name, name_len);
  5566. } else if (add_backref) {
  5567. ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
  5568. parent_ino, index);
  5569. }
  5570. /* Nothing to clean up yet */
  5571. if (ret)
  5572. return ret;
  5573. ret = btrfs_insert_dir_item(trans, root, name, name_len,
  5574. parent_inode, &key,
  5575. btrfs_inode_type(inode), index);
  5576. if (ret == -EEXIST || ret == -EOVERFLOW)
  5577. goto fail_dir_item;
  5578. else if (ret) {
  5579. btrfs_abort_transaction(trans, ret);
  5580. return ret;
  5581. }
  5582. btrfs_i_size_write(parent_inode, parent_inode->i_size +
  5583. name_len * 2);
  5584. inode_inc_iversion(parent_inode);
  5585. parent_inode->i_mtime = parent_inode->i_ctime =
  5586. current_time(parent_inode);
  5587. ret = btrfs_update_inode(trans, root, parent_inode);
  5588. if (ret)
  5589. btrfs_abort_transaction(trans, ret);
  5590. return ret;
  5591. fail_dir_item:
  5592. if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
  5593. u64 local_index;
  5594. int err;
  5595. err = btrfs_del_root_ref(trans, fs_info, key.objectid,
  5596. root->root_key.objectid, parent_ino,
  5597. &local_index, name, name_len);
  5598. } else if (add_backref) {
  5599. u64 local_index;
  5600. int err;
  5601. err = btrfs_del_inode_ref(trans, root, name, name_len,
  5602. ino, parent_ino, &local_index);
  5603. }
  5604. return ret;
  5605. }
  5606. static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
  5607. struct inode *dir, struct dentry *dentry,
  5608. struct inode *inode, int backref, u64 index)
  5609. {
  5610. int err = btrfs_add_link(trans, dir, inode,
  5611. dentry->d_name.name, dentry->d_name.len,
  5612. backref, index);
  5613. if (err > 0)
  5614. err = -EEXIST;
  5615. return err;
  5616. }
  5617. static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
  5618. umode_t mode, dev_t rdev)
  5619. {
  5620. struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
  5621. struct btrfs_trans_handle *trans;
  5622. struct btrfs_root *root = BTRFS_I(dir)->root;
  5623. struct inode *inode = NULL;
  5624. int err;
  5625. int drop_inode = 0;
  5626. u64 objectid;
  5627. u64 index = 0;
  5628. /*
  5629. * 2 for inode item and ref
  5630. * 2 for dir items
  5631. * 1 for xattr if selinux is on
  5632. */
  5633. trans = btrfs_start_transaction(root, 5);
  5634. if (IS_ERR(trans))
  5635. return PTR_ERR(trans);
  5636. err = btrfs_find_free_ino(root, &objectid);
  5637. if (err)
  5638. goto out_unlock;
  5639. inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
  5640. dentry->d_name.len, btrfs_ino(dir), objectid,
  5641. mode, &index);
  5642. if (IS_ERR(inode)) {
  5643. err = PTR_ERR(inode);
  5644. goto out_unlock;
  5645. }
  5646. /*
  5647. * If the active LSM wants to access the inode during
  5648. * d_instantiate it needs these. Smack checks to see
  5649. * if the filesystem supports xattrs by looking at the
  5650. * ops vector.
  5651. */
  5652. inode->i_op = &btrfs_special_inode_operations;
  5653. init_special_inode(inode, inode->i_mode, rdev);
  5654. err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
  5655. if (err)
  5656. goto out_unlock_inode;
  5657. err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
  5658. if (err) {
  5659. goto out_unlock_inode;
  5660. } else {
  5661. btrfs_update_inode(trans, root, inode);
  5662. unlock_new_inode(inode);
  5663. d_instantiate(dentry, inode);
  5664. }
  5665. out_unlock:
  5666. btrfs_end_transaction(trans);
  5667. btrfs_balance_delayed_items(fs_info);
  5668. btrfs_btree_balance_dirty(fs_info);
  5669. if (drop_inode) {
  5670. inode_dec_link_count(inode);
  5671. iput(inode);
  5672. }
  5673. return err;
  5674. out_unlock_inode:
  5675. drop_inode = 1;
  5676. unlock_new_inode(inode);
  5677. goto out_unlock;
  5678. }
  5679. static int btrfs_create(struct inode *dir, struct dentry *dentry,
  5680. umode_t mode, bool excl)
  5681. {
  5682. struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
  5683. struct btrfs_trans_handle *trans;
  5684. struct btrfs_root *root = BTRFS_I(dir)->root;
  5685. struct inode *inode = NULL;
  5686. int drop_inode_on_err = 0;
  5687. int err;
  5688. u64 objectid;
  5689. u64 index = 0;
  5690. /*
  5691. * 2 for inode item and ref
  5692. * 2 for dir items
  5693. * 1 for xattr if selinux is on
  5694. */
  5695. trans = btrfs_start_transaction(root, 5);
  5696. if (IS_ERR(trans))
  5697. return PTR_ERR(trans);
  5698. err = btrfs_find_free_ino(root, &objectid);
  5699. if (err)
  5700. goto out_unlock;
  5701. inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
  5702. dentry->d_name.len, btrfs_ino(dir), objectid,
  5703. mode, &index);
  5704. if (IS_ERR(inode)) {
  5705. err = PTR_ERR(inode);
  5706. goto out_unlock;
  5707. }
  5708. drop_inode_on_err = 1;
  5709. /*
  5710. * If the active LSM wants to access the inode during
  5711. * d_instantiate it needs these. Smack checks to see
  5712. * if the filesystem supports xattrs by looking at the
  5713. * ops vector.
  5714. */
  5715. inode->i_fop = &btrfs_file_operations;
  5716. inode->i_op = &btrfs_file_inode_operations;
  5717. inode->i_mapping->a_ops = &btrfs_aops;
  5718. err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
  5719. if (err)
  5720. goto out_unlock_inode;
  5721. err = btrfs_update_inode(trans, root, inode);
  5722. if (err)
  5723. goto out_unlock_inode;
  5724. err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
  5725. if (err)
  5726. goto out_unlock_inode;
  5727. BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
  5728. unlock_new_inode(inode);
  5729. d_instantiate(dentry, inode);
  5730. out_unlock:
  5731. btrfs_end_transaction(trans);
  5732. if (err && drop_inode_on_err) {
  5733. inode_dec_link_count(inode);
  5734. iput(inode);
  5735. }
  5736. btrfs_balance_delayed_items(fs_info);
  5737. btrfs_btree_balance_dirty(fs_info);
  5738. return err;
  5739. out_unlock_inode:
  5740. unlock_new_inode(inode);
  5741. goto out_unlock;
  5742. }
  5743. static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
  5744. struct dentry *dentry)
  5745. {
  5746. struct btrfs_trans_handle *trans = NULL;
  5747. struct btrfs_root *root = BTRFS_I(dir)->root;
  5748. struct inode *inode = d_inode(old_dentry);
  5749. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  5750. u64 index;
  5751. int err;
  5752. int drop_inode = 0;
  5753. /* do not allow sys_link's with other subvols of the same device */
  5754. if (root->objectid != BTRFS_I(inode)->root->objectid)
  5755. return -EXDEV;
  5756. if (inode->i_nlink >= BTRFS_LINK_MAX)
  5757. return -EMLINK;
  5758. err = btrfs_set_inode_index(dir, &index);
  5759. if (err)
  5760. goto fail;
  5761. /*
  5762. * 2 items for inode and inode ref
  5763. * 2 items for dir items
  5764. * 1 item for parent inode
  5765. */
  5766. trans = btrfs_start_transaction(root, 5);
  5767. if (IS_ERR(trans)) {
  5768. err = PTR_ERR(trans);
  5769. trans = NULL;
  5770. goto fail;
  5771. }
  5772. /* There are several dir indexes for this inode, clear the cache. */
  5773. BTRFS_I(inode)->dir_index = 0ULL;
  5774. inc_nlink(inode);
  5775. inode_inc_iversion(inode);
  5776. inode->i_ctime = current_time(inode);
  5777. ihold(inode);
  5778. set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
  5779. err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
  5780. if (err) {
  5781. drop_inode = 1;
  5782. } else {
  5783. struct dentry *parent = dentry->d_parent;
  5784. err = btrfs_update_inode(trans, root, inode);
  5785. if (err)
  5786. goto fail;
  5787. if (inode->i_nlink == 1) {
  5788. /*
  5789. * If new hard link count is 1, it's a file created
  5790. * with open(2) O_TMPFILE flag.
  5791. */
  5792. err = btrfs_orphan_del(trans, inode);
  5793. if (err)
  5794. goto fail;
  5795. }
  5796. d_instantiate(dentry, inode);
  5797. btrfs_log_new_name(trans, inode, NULL, parent);
  5798. }
  5799. btrfs_balance_delayed_items(fs_info);
  5800. fail:
  5801. if (trans)
  5802. btrfs_end_transaction(trans);
  5803. if (drop_inode) {
  5804. inode_dec_link_count(inode);
  5805. iput(inode);
  5806. }
  5807. btrfs_btree_balance_dirty(fs_info);
  5808. return err;
  5809. }
  5810. static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
  5811. {
  5812. struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
  5813. struct inode *inode = NULL;
  5814. struct btrfs_trans_handle *trans;
  5815. struct btrfs_root *root = BTRFS_I(dir)->root;
  5816. int err = 0;
  5817. int drop_on_err = 0;
  5818. u64 objectid = 0;
  5819. u64 index = 0;
  5820. /*
  5821. * 2 items for inode and ref
  5822. * 2 items for dir items
  5823. * 1 for xattr if selinux is on
  5824. */
  5825. trans = btrfs_start_transaction(root, 5);
  5826. if (IS_ERR(trans))
  5827. return PTR_ERR(trans);
  5828. err = btrfs_find_free_ino(root, &objectid);
  5829. if (err)
  5830. goto out_fail;
  5831. inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
  5832. dentry->d_name.len, btrfs_ino(dir), objectid,
  5833. S_IFDIR | mode, &index);
  5834. if (IS_ERR(inode)) {
  5835. err = PTR_ERR(inode);
  5836. goto out_fail;
  5837. }
  5838. drop_on_err = 1;
  5839. /* these must be set before we unlock the inode */
  5840. inode->i_op = &btrfs_dir_inode_operations;
  5841. inode->i_fop = &btrfs_dir_file_operations;
  5842. err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
  5843. if (err)
  5844. goto out_fail_inode;
  5845. btrfs_i_size_write(inode, 0);
  5846. err = btrfs_update_inode(trans, root, inode);
  5847. if (err)
  5848. goto out_fail_inode;
  5849. err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
  5850. dentry->d_name.len, 0, index);
  5851. if (err)
  5852. goto out_fail_inode;
  5853. d_instantiate(dentry, inode);
  5854. /*
  5855. * mkdir is special. We're unlocking after we call d_instantiate
  5856. * to avoid a race with nfsd calling d_instantiate.
  5857. */
  5858. unlock_new_inode(inode);
  5859. drop_on_err = 0;
  5860. out_fail:
  5861. btrfs_end_transaction(trans);
  5862. if (drop_on_err) {
  5863. inode_dec_link_count(inode);
  5864. iput(inode);
  5865. }
  5866. btrfs_balance_delayed_items(fs_info);
  5867. btrfs_btree_balance_dirty(fs_info);
  5868. return err;
  5869. out_fail_inode:
  5870. unlock_new_inode(inode);
  5871. goto out_fail;
  5872. }
  5873. /* Find next extent map of a given extent map, caller needs to ensure locks */
  5874. static struct extent_map *next_extent_map(struct extent_map *em)
  5875. {
  5876. struct rb_node *next;
  5877. next = rb_next(&em->rb_node);
  5878. if (!next)
  5879. return NULL;
  5880. return container_of(next, struct extent_map, rb_node);
  5881. }
  5882. static struct extent_map *prev_extent_map(struct extent_map *em)
  5883. {
  5884. struct rb_node *prev;
  5885. prev = rb_prev(&em->rb_node);
  5886. if (!prev)
  5887. return NULL;
  5888. return container_of(prev, struct extent_map, rb_node);
  5889. }
  5890. /* helper for btfs_get_extent. Given an existing extent in the tree,
  5891. * the existing extent is the nearest extent to map_start,
  5892. * and an extent that you want to insert, deal with overlap and insert
  5893. * the best fitted new extent into the tree.
  5894. */
  5895. static int merge_extent_mapping(struct extent_map_tree *em_tree,
  5896. struct extent_map *existing,
  5897. struct extent_map *em,
  5898. u64 map_start)
  5899. {
  5900. struct extent_map *prev;
  5901. struct extent_map *next;
  5902. u64 start;
  5903. u64 end;
  5904. u64 start_diff;
  5905. BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
  5906. if (existing->start > map_start) {
  5907. next = existing;
  5908. prev = prev_extent_map(next);
  5909. } else {
  5910. prev = existing;
  5911. next = next_extent_map(prev);
  5912. }
  5913. start = prev ? extent_map_end(prev) : em->start;
  5914. start = max_t(u64, start, em->start);
  5915. end = next ? next->start : extent_map_end(em);
  5916. end = min_t(u64, end, extent_map_end(em));
  5917. start_diff = start - em->start;
  5918. em->start = start;
  5919. em->len = end - start;
  5920. if (em->block_start < EXTENT_MAP_LAST_BYTE &&
  5921. !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
  5922. em->block_start += start_diff;
  5923. em->block_len -= start_diff;
  5924. }
  5925. return add_extent_mapping(em_tree, em, 0);
  5926. }
  5927. static noinline int uncompress_inline(struct btrfs_path *path,
  5928. struct page *page,
  5929. size_t pg_offset, u64 extent_offset,
  5930. struct btrfs_file_extent_item *item)
  5931. {
  5932. int ret;
  5933. struct extent_buffer *leaf = path->nodes[0];
  5934. char *tmp;
  5935. size_t max_size;
  5936. unsigned long inline_size;
  5937. unsigned long ptr;
  5938. int compress_type;
  5939. WARN_ON(pg_offset != 0);
  5940. compress_type = btrfs_file_extent_compression(leaf, item);
  5941. max_size = btrfs_file_extent_ram_bytes(leaf, item);
  5942. inline_size = btrfs_file_extent_inline_item_len(leaf,
  5943. btrfs_item_nr(path->slots[0]));
  5944. tmp = kmalloc(inline_size, GFP_NOFS);
  5945. if (!tmp)
  5946. return -ENOMEM;
  5947. ptr = btrfs_file_extent_inline_start(item);
  5948. read_extent_buffer(leaf, tmp, ptr, inline_size);
  5949. max_size = min_t(unsigned long, PAGE_SIZE, max_size);
  5950. ret = btrfs_decompress(compress_type, tmp, page,
  5951. extent_offset, inline_size, max_size);
  5952. kfree(tmp);
  5953. return ret;
  5954. }
  5955. /*
  5956. * a bit scary, this does extent mapping from logical file offset to the disk.
  5957. * the ugly parts come from merging extents from the disk with the in-ram
  5958. * representation. This gets more complex because of the data=ordered code,
  5959. * where the in-ram extents might be locked pending data=ordered completion.
  5960. *
  5961. * This also copies inline extents directly into the page.
  5962. */
  5963. struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
  5964. size_t pg_offset, u64 start, u64 len,
  5965. int create)
  5966. {
  5967. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  5968. int ret;
  5969. int err = 0;
  5970. u64 extent_start = 0;
  5971. u64 extent_end = 0;
  5972. u64 objectid = btrfs_ino(inode);
  5973. u32 found_type;
  5974. struct btrfs_path *path = NULL;
  5975. struct btrfs_root *root = BTRFS_I(inode)->root;
  5976. struct btrfs_file_extent_item *item;
  5977. struct extent_buffer *leaf;
  5978. struct btrfs_key found_key;
  5979. struct extent_map *em = NULL;
  5980. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  5981. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  5982. struct btrfs_trans_handle *trans = NULL;
  5983. const bool new_inline = !page || create;
  5984. again:
  5985. read_lock(&em_tree->lock);
  5986. em = lookup_extent_mapping(em_tree, start, len);
  5987. if (em)
  5988. em->bdev = fs_info->fs_devices->latest_bdev;
  5989. read_unlock(&em_tree->lock);
  5990. if (em) {
  5991. if (em->start > start || em->start + em->len <= start)
  5992. free_extent_map(em);
  5993. else if (em->block_start == EXTENT_MAP_INLINE && page)
  5994. free_extent_map(em);
  5995. else
  5996. goto out;
  5997. }
  5998. em = alloc_extent_map();
  5999. if (!em) {
  6000. err = -ENOMEM;
  6001. goto out;
  6002. }
  6003. em->bdev = fs_info->fs_devices->latest_bdev;
  6004. em->start = EXTENT_MAP_HOLE;
  6005. em->orig_start = EXTENT_MAP_HOLE;
  6006. em->len = (u64)-1;
  6007. em->block_len = (u64)-1;
  6008. if (!path) {
  6009. path = btrfs_alloc_path();
  6010. if (!path) {
  6011. err = -ENOMEM;
  6012. goto out;
  6013. }
  6014. /*
  6015. * Chances are we'll be called again, so go ahead and do
  6016. * readahead
  6017. */
  6018. path->reada = READA_FORWARD;
  6019. }
  6020. ret = btrfs_lookup_file_extent(trans, root, path,
  6021. objectid, start, trans != NULL);
  6022. if (ret < 0) {
  6023. err = ret;
  6024. goto out;
  6025. }
  6026. if (ret != 0) {
  6027. if (path->slots[0] == 0)
  6028. goto not_found;
  6029. path->slots[0]--;
  6030. }
  6031. leaf = path->nodes[0];
  6032. item = btrfs_item_ptr(leaf, path->slots[0],
  6033. struct btrfs_file_extent_item);
  6034. /* are we inside the extent that was found? */
  6035. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  6036. found_type = found_key.type;
  6037. if (found_key.objectid != objectid ||
  6038. found_type != BTRFS_EXTENT_DATA_KEY) {
  6039. /*
  6040. * If we backup past the first extent we want to move forward
  6041. * and see if there is an extent in front of us, otherwise we'll
  6042. * say there is a hole for our whole search range which can
  6043. * cause problems.
  6044. */
  6045. extent_end = start;
  6046. goto next;
  6047. }
  6048. found_type = btrfs_file_extent_type(leaf, item);
  6049. extent_start = found_key.offset;
  6050. if (found_type == BTRFS_FILE_EXTENT_REG ||
  6051. found_type == BTRFS_FILE_EXTENT_PREALLOC) {
  6052. extent_end = extent_start +
  6053. btrfs_file_extent_num_bytes(leaf, item);
  6054. } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
  6055. size_t size;
  6056. size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
  6057. extent_end = ALIGN(extent_start + size,
  6058. fs_info->sectorsize);
  6059. }
  6060. next:
  6061. if (start >= extent_end) {
  6062. path->slots[0]++;
  6063. if (path->slots[0] >= btrfs_header_nritems(leaf)) {
  6064. ret = btrfs_next_leaf(root, path);
  6065. if (ret < 0) {
  6066. err = ret;
  6067. goto out;
  6068. }
  6069. if (ret > 0)
  6070. goto not_found;
  6071. leaf = path->nodes[0];
  6072. }
  6073. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  6074. if (found_key.objectid != objectid ||
  6075. found_key.type != BTRFS_EXTENT_DATA_KEY)
  6076. goto not_found;
  6077. if (start + len <= found_key.offset)
  6078. goto not_found;
  6079. if (start > found_key.offset)
  6080. goto next;
  6081. em->start = start;
  6082. em->orig_start = start;
  6083. em->len = found_key.offset - start;
  6084. goto not_found_em;
  6085. }
  6086. btrfs_extent_item_to_extent_map(inode, path, item, new_inline, em);
  6087. if (found_type == BTRFS_FILE_EXTENT_REG ||
  6088. found_type == BTRFS_FILE_EXTENT_PREALLOC) {
  6089. goto insert;
  6090. } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
  6091. unsigned long ptr;
  6092. char *map;
  6093. size_t size;
  6094. size_t extent_offset;
  6095. size_t copy_size;
  6096. if (new_inline)
  6097. goto out;
  6098. size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
  6099. extent_offset = page_offset(page) + pg_offset - extent_start;
  6100. copy_size = min_t(u64, PAGE_SIZE - pg_offset,
  6101. size - extent_offset);
  6102. em->start = extent_start + extent_offset;
  6103. em->len = ALIGN(copy_size, fs_info->sectorsize);
  6104. em->orig_block_len = em->len;
  6105. em->orig_start = em->start;
  6106. ptr = btrfs_file_extent_inline_start(item) + extent_offset;
  6107. if (create == 0 && !PageUptodate(page)) {
  6108. if (btrfs_file_extent_compression(leaf, item) !=
  6109. BTRFS_COMPRESS_NONE) {
  6110. ret = uncompress_inline(path, page, pg_offset,
  6111. extent_offset, item);
  6112. if (ret) {
  6113. err = ret;
  6114. goto out;
  6115. }
  6116. } else {
  6117. map = kmap(page);
  6118. read_extent_buffer(leaf, map + pg_offset, ptr,
  6119. copy_size);
  6120. if (pg_offset + copy_size < PAGE_SIZE) {
  6121. memset(map + pg_offset + copy_size, 0,
  6122. PAGE_SIZE - pg_offset -
  6123. copy_size);
  6124. }
  6125. kunmap(page);
  6126. }
  6127. flush_dcache_page(page);
  6128. } else if (create && PageUptodate(page)) {
  6129. BUG();
  6130. if (!trans) {
  6131. kunmap(page);
  6132. free_extent_map(em);
  6133. em = NULL;
  6134. btrfs_release_path(path);
  6135. trans = btrfs_join_transaction(root);
  6136. if (IS_ERR(trans))
  6137. return ERR_CAST(trans);
  6138. goto again;
  6139. }
  6140. map = kmap(page);
  6141. write_extent_buffer(leaf, map + pg_offset, ptr,
  6142. copy_size);
  6143. kunmap(page);
  6144. btrfs_mark_buffer_dirty(leaf);
  6145. }
  6146. set_extent_uptodate(io_tree, em->start,
  6147. extent_map_end(em) - 1, NULL, GFP_NOFS);
  6148. goto insert;
  6149. }
  6150. not_found:
  6151. em->start = start;
  6152. em->orig_start = start;
  6153. em->len = len;
  6154. not_found_em:
  6155. em->block_start = EXTENT_MAP_HOLE;
  6156. set_bit(EXTENT_FLAG_VACANCY, &em->flags);
  6157. insert:
  6158. btrfs_release_path(path);
  6159. if (em->start > start || extent_map_end(em) <= start) {
  6160. btrfs_err(fs_info,
  6161. "bad extent! em: [%llu %llu] passed [%llu %llu]",
  6162. em->start, em->len, start, len);
  6163. err = -EIO;
  6164. goto out;
  6165. }
  6166. err = 0;
  6167. write_lock(&em_tree->lock);
  6168. ret = add_extent_mapping(em_tree, em, 0);
  6169. /* it is possible that someone inserted the extent into the tree
  6170. * while we had the lock dropped. It is also possible that
  6171. * an overlapping map exists in the tree
  6172. */
  6173. if (ret == -EEXIST) {
  6174. struct extent_map *existing;
  6175. ret = 0;
  6176. existing = search_extent_mapping(em_tree, start, len);
  6177. /*
  6178. * existing will always be non-NULL, since there must be
  6179. * extent causing the -EEXIST.
  6180. */
  6181. if (existing->start == em->start &&
  6182. extent_map_end(existing) >= extent_map_end(em) &&
  6183. em->block_start == existing->block_start) {
  6184. /*
  6185. * The existing extent map already encompasses the
  6186. * entire extent map we tried to add.
  6187. */
  6188. free_extent_map(em);
  6189. em = existing;
  6190. err = 0;
  6191. } else if (start >= extent_map_end(existing) ||
  6192. start <= existing->start) {
  6193. /*
  6194. * The existing extent map is the one nearest to
  6195. * the [start, start + len) range which overlaps
  6196. */
  6197. err = merge_extent_mapping(em_tree, existing,
  6198. em, start);
  6199. free_extent_map(existing);
  6200. if (err) {
  6201. free_extent_map(em);
  6202. em = NULL;
  6203. }
  6204. } else {
  6205. free_extent_map(em);
  6206. em = existing;
  6207. err = 0;
  6208. }
  6209. }
  6210. write_unlock(&em_tree->lock);
  6211. out:
  6212. trace_btrfs_get_extent(root, em);
  6213. btrfs_free_path(path);
  6214. if (trans) {
  6215. ret = btrfs_end_transaction(trans);
  6216. if (!err)
  6217. err = ret;
  6218. }
  6219. if (err) {
  6220. free_extent_map(em);
  6221. return ERR_PTR(err);
  6222. }
  6223. BUG_ON(!em); /* Error is always set */
  6224. return em;
  6225. }
  6226. struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
  6227. size_t pg_offset, u64 start, u64 len,
  6228. int create)
  6229. {
  6230. struct extent_map *em;
  6231. struct extent_map *hole_em = NULL;
  6232. u64 range_start = start;
  6233. u64 end;
  6234. u64 found;
  6235. u64 found_end;
  6236. int err = 0;
  6237. em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
  6238. if (IS_ERR(em))
  6239. return em;
  6240. if (em) {
  6241. /*
  6242. * if our em maps to
  6243. * - a hole or
  6244. * - a pre-alloc extent,
  6245. * there might actually be delalloc bytes behind it.
  6246. */
  6247. if (em->block_start != EXTENT_MAP_HOLE &&
  6248. !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
  6249. return em;
  6250. else
  6251. hole_em = em;
  6252. }
  6253. /* check to see if we've wrapped (len == -1 or similar) */
  6254. end = start + len;
  6255. if (end < start)
  6256. end = (u64)-1;
  6257. else
  6258. end -= 1;
  6259. em = NULL;
  6260. /* ok, we didn't find anything, lets look for delalloc */
  6261. found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
  6262. end, len, EXTENT_DELALLOC, 1);
  6263. found_end = range_start + found;
  6264. if (found_end < range_start)
  6265. found_end = (u64)-1;
  6266. /*
  6267. * we didn't find anything useful, return
  6268. * the original results from get_extent()
  6269. */
  6270. if (range_start > end || found_end <= start) {
  6271. em = hole_em;
  6272. hole_em = NULL;
  6273. goto out;
  6274. }
  6275. /* adjust the range_start to make sure it doesn't
  6276. * go backwards from the start they passed in
  6277. */
  6278. range_start = max(start, range_start);
  6279. found = found_end - range_start;
  6280. if (found > 0) {
  6281. u64 hole_start = start;
  6282. u64 hole_len = len;
  6283. em = alloc_extent_map();
  6284. if (!em) {
  6285. err = -ENOMEM;
  6286. goto out;
  6287. }
  6288. /*
  6289. * when btrfs_get_extent can't find anything it
  6290. * returns one huge hole
  6291. *
  6292. * make sure what it found really fits our range, and
  6293. * adjust to make sure it is based on the start from
  6294. * the caller
  6295. */
  6296. if (hole_em) {
  6297. u64 calc_end = extent_map_end(hole_em);
  6298. if (calc_end <= start || (hole_em->start > end)) {
  6299. free_extent_map(hole_em);
  6300. hole_em = NULL;
  6301. } else {
  6302. hole_start = max(hole_em->start, start);
  6303. hole_len = calc_end - hole_start;
  6304. }
  6305. }
  6306. em->bdev = NULL;
  6307. if (hole_em && range_start > hole_start) {
  6308. /* our hole starts before our delalloc, so we
  6309. * have to return just the parts of the hole
  6310. * that go until the delalloc starts
  6311. */
  6312. em->len = min(hole_len,
  6313. range_start - hole_start);
  6314. em->start = hole_start;
  6315. em->orig_start = hole_start;
  6316. /*
  6317. * don't adjust block start at all,
  6318. * it is fixed at EXTENT_MAP_HOLE
  6319. */
  6320. em->block_start = hole_em->block_start;
  6321. em->block_len = hole_len;
  6322. if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
  6323. set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
  6324. } else {
  6325. em->start = range_start;
  6326. em->len = found;
  6327. em->orig_start = range_start;
  6328. em->block_start = EXTENT_MAP_DELALLOC;
  6329. em->block_len = found;
  6330. }
  6331. } else if (hole_em) {
  6332. return hole_em;
  6333. }
  6334. out:
  6335. free_extent_map(hole_em);
  6336. if (err) {
  6337. free_extent_map(em);
  6338. return ERR_PTR(err);
  6339. }
  6340. return em;
  6341. }
  6342. static struct extent_map *btrfs_create_dio_extent(struct inode *inode,
  6343. const u64 start,
  6344. const u64 len,
  6345. const u64 orig_start,
  6346. const u64 block_start,
  6347. const u64 block_len,
  6348. const u64 orig_block_len,
  6349. const u64 ram_bytes,
  6350. const int type)
  6351. {
  6352. struct extent_map *em = NULL;
  6353. int ret;
  6354. down_read(&BTRFS_I(inode)->dio_sem);
  6355. if (type != BTRFS_ORDERED_NOCOW) {
  6356. em = create_pinned_em(inode, start, len, orig_start,
  6357. block_start, block_len, orig_block_len,
  6358. ram_bytes, type);
  6359. if (IS_ERR(em))
  6360. goto out;
  6361. }
  6362. ret = btrfs_add_ordered_extent_dio(inode, start, block_start,
  6363. len, block_len, type);
  6364. if (ret) {
  6365. if (em) {
  6366. free_extent_map(em);
  6367. btrfs_drop_extent_cache(inode, start,
  6368. start + len - 1, 0);
  6369. }
  6370. em = ERR_PTR(ret);
  6371. }
  6372. out:
  6373. up_read(&BTRFS_I(inode)->dio_sem);
  6374. return em;
  6375. }
  6376. static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
  6377. u64 start, u64 len)
  6378. {
  6379. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  6380. struct btrfs_root *root = BTRFS_I(inode)->root;
  6381. struct extent_map *em;
  6382. struct btrfs_key ins;
  6383. u64 alloc_hint;
  6384. int ret;
  6385. alloc_hint = get_extent_allocation_hint(inode, start, len);
  6386. ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize,
  6387. 0, alloc_hint, &ins, 1, 1);
  6388. if (ret)
  6389. return ERR_PTR(ret);
  6390. em = btrfs_create_dio_extent(inode, start, ins.offset, start,
  6391. ins.objectid, ins.offset, ins.offset,
  6392. ins.offset, 0);
  6393. btrfs_dec_block_group_reservations(fs_info, ins.objectid);
  6394. if (IS_ERR(em))
  6395. btrfs_free_reserved_extent(fs_info, ins.objectid,
  6396. ins.offset, 1);
  6397. return em;
  6398. }
  6399. /*
  6400. * returns 1 when the nocow is safe, < 1 on error, 0 if the
  6401. * block must be cow'd
  6402. */
  6403. noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
  6404. u64 *orig_start, u64 *orig_block_len,
  6405. u64 *ram_bytes)
  6406. {
  6407. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  6408. struct btrfs_trans_handle *trans;
  6409. struct btrfs_path *path;
  6410. int ret;
  6411. struct extent_buffer *leaf;
  6412. struct btrfs_root *root = BTRFS_I(inode)->root;
  6413. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  6414. struct btrfs_file_extent_item *fi;
  6415. struct btrfs_key key;
  6416. u64 disk_bytenr;
  6417. u64 backref_offset;
  6418. u64 extent_end;
  6419. u64 num_bytes;
  6420. int slot;
  6421. int found_type;
  6422. bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
  6423. path = btrfs_alloc_path();
  6424. if (!path)
  6425. return -ENOMEM;
  6426. ret = btrfs_lookup_file_extent(NULL, root, path, btrfs_ino(inode),
  6427. offset, 0);
  6428. if (ret < 0)
  6429. goto out;
  6430. slot = path->slots[0];
  6431. if (ret == 1) {
  6432. if (slot == 0) {
  6433. /* can't find the item, must cow */
  6434. ret = 0;
  6435. goto out;
  6436. }
  6437. slot--;
  6438. }
  6439. ret = 0;
  6440. leaf = path->nodes[0];
  6441. btrfs_item_key_to_cpu(leaf, &key, slot);
  6442. if (key.objectid != btrfs_ino(inode) ||
  6443. key.type != BTRFS_EXTENT_DATA_KEY) {
  6444. /* not our file or wrong item type, must cow */
  6445. goto out;
  6446. }
  6447. if (key.offset > offset) {
  6448. /* Wrong offset, must cow */
  6449. goto out;
  6450. }
  6451. fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
  6452. found_type = btrfs_file_extent_type(leaf, fi);
  6453. if (found_type != BTRFS_FILE_EXTENT_REG &&
  6454. found_type != BTRFS_FILE_EXTENT_PREALLOC) {
  6455. /* not a regular extent, must cow */
  6456. goto out;
  6457. }
  6458. if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
  6459. goto out;
  6460. extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
  6461. if (extent_end <= offset)
  6462. goto out;
  6463. disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
  6464. if (disk_bytenr == 0)
  6465. goto out;
  6466. if (btrfs_file_extent_compression(leaf, fi) ||
  6467. btrfs_file_extent_encryption(leaf, fi) ||
  6468. btrfs_file_extent_other_encoding(leaf, fi))
  6469. goto out;
  6470. backref_offset = btrfs_file_extent_offset(leaf, fi);
  6471. if (orig_start) {
  6472. *orig_start = key.offset - backref_offset;
  6473. *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
  6474. *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
  6475. }
  6476. if (btrfs_extent_readonly(fs_info, disk_bytenr))
  6477. goto out;
  6478. num_bytes = min(offset + *len, extent_end) - offset;
  6479. if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
  6480. u64 range_end;
  6481. range_end = round_up(offset + num_bytes,
  6482. root->fs_info->sectorsize) - 1;
  6483. ret = test_range_bit(io_tree, offset, range_end,
  6484. EXTENT_DELALLOC, 0, NULL);
  6485. if (ret) {
  6486. ret = -EAGAIN;
  6487. goto out;
  6488. }
  6489. }
  6490. btrfs_release_path(path);
  6491. /*
  6492. * look for other files referencing this extent, if we
  6493. * find any we must cow
  6494. */
  6495. trans = btrfs_join_transaction(root);
  6496. if (IS_ERR(trans)) {
  6497. ret = 0;
  6498. goto out;
  6499. }
  6500. ret = btrfs_cross_ref_exist(trans, root, btrfs_ino(inode),
  6501. key.offset - backref_offset, disk_bytenr);
  6502. btrfs_end_transaction(trans);
  6503. if (ret) {
  6504. ret = 0;
  6505. goto out;
  6506. }
  6507. /*
  6508. * adjust disk_bytenr and num_bytes to cover just the bytes
  6509. * in this extent we are about to write. If there
  6510. * are any csums in that range we have to cow in order
  6511. * to keep the csums correct
  6512. */
  6513. disk_bytenr += backref_offset;
  6514. disk_bytenr += offset - key.offset;
  6515. if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes))
  6516. goto out;
  6517. /*
  6518. * all of the above have passed, it is safe to overwrite this extent
  6519. * without cow
  6520. */
  6521. *len = num_bytes;
  6522. ret = 1;
  6523. out:
  6524. btrfs_free_path(path);
  6525. return ret;
  6526. }
  6527. bool btrfs_page_exists_in_range(struct inode *inode, loff_t start, loff_t end)
  6528. {
  6529. struct radix_tree_root *root = &inode->i_mapping->page_tree;
  6530. int found = false;
  6531. void **pagep = NULL;
  6532. struct page *page = NULL;
  6533. int start_idx;
  6534. int end_idx;
  6535. start_idx = start >> PAGE_SHIFT;
  6536. /*
  6537. * end is the last byte in the last page. end == start is legal
  6538. */
  6539. end_idx = end >> PAGE_SHIFT;
  6540. rcu_read_lock();
  6541. /* Most of the code in this while loop is lifted from
  6542. * find_get_page. It's been modified to begin searching from a
  6543. * page and return just the first page found in that range. If the
  6544. * found idx is less than or equal to the end idx then we know that
  6545. * a page exists. If no pages are found or if those pages are
  6546. * outside of the range then we're fine (yay!) */
  6547. while (page == NULL &&
  6548. radix_tree_gang_lookup_slot(root, &pagep, NULL, start_idx, 1)) {
  6549. page = radix_tree_deref_slot(pagep);
  6550. if (unlikely(!page))
  6551. break;
  6552. if (radix_tree_exception(page)) {
  6553. if (radix_tree_deref_retry(page)) {
  6554. page = NULL;
  6555. continue;
  6556. }
  6557. /*
  6558. * Otherwise, shmem/tmpfs must be storing a swap entry
  6559. * here as an exceptional entry: so return it without
  6560. * attempting to raise page count.
  6561. */
  6562. page = NULL;
  6563. break; /* TODO: Is this relevant for this use case? */
  6564. }
  6565. if (!page_cache_get_speculative(page)) {
  6566. page = NULL;
  6567. continue;
  6568. }
  6569. /*
  6570. * Has the page moved?
  6571. * This is part of the lockless pagecache protocol. See
  6572. * include/linux/pagemap.h for details.
  6573. */
  6574. if (unlikely(page != *pagep)) {
  6575. put_page(page);
  6576. page = NULL;
  6577. }
  6578. }
  6579. if (page) {
  6580. if (page->index <= end_idx)
  6581. found = true;
  6582. put_page(page);
  6583. }
  6584. rcu_read_unlock();
  6585. return found;
  6586. }
  6587. static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
  6588. struct extent_state **cached_state, int writing)
  6589. {
  6590. struct btrfs_ordered_extent *ordered;
  6591. int ret = 0;
  6592. while (1) {
  6593. lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
  6594. cached_state);
  6595. /*
  6596. * We're concerned with the entire range that we're going to be
  6597. * doing DIO to, so we need to make sure there's no ordered
  6598. * extents in this range.
  6599. */
  6600. ordered = btrfs_lookup_ordered_range(inode, lockstart,
  6601. lockend - lockstart + 1);
  6602. /*
  6603. * We need to make sure there are no buffered pages in this
  6604. * range either, we could have raced between the invalidate in
  6605. * generic_file_direct_write and locking the extent. The
  6606. * invalidate needs to happen so that reads after a write do not
  6607. * get stale data.
  6608. */
  6609. if (!ordered &&
  6610. (!writing ||
  6611. !btrfs_page_exists_in_range(inode, lockstart, lockend)))
  6612. break;
  6613. unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
  6614. cached_state, GFP_NOFS);
  6615. if (ordered) {
  6616. /*
  6617. * If we are doing a DIO read and the ordered extent we
  6618. * found is for a buffered write, we can not wait for it
  6619. * to complete and retry, because if we do so we can
  6620. * deadlock with concurrent buffered writes on page
  6621. * locks. This happens only if our DIO read covers more
  6622. * than one extent map, if at this point has already
  6623. * created an ordered extent for a previous extent map
  6624. * and locked its range in the inode's io tree, and a
  6625. * concurrent write against that previous extent map's
  6626. * range and this range started (we unlock the ranges
  6627. * in the io tree only when the bios complete and
  6628. * buffered writes always lock pages before attempting
  6629. * to lock range in the io tree).
  6630. */
  6631. if (writing ||
  6632. test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags))
  6633. btrfs_start_ordered_extent(inode, ordered, 1);
  6634. else
  6635. ret = -ENOTBLK;
  6636. btrfs_put_ordered_extent(ordered);
  6637. } else {
  6638. /*
  6639. * We could trigger writeback for this range (and wait
  6640. * for it to complete) and then invalidate the pages for
  6641. * this range (through invalidate_inode_pages2_range()),
  6642. * but that can lead us to a deadlock with a concurrent
  6643. * call to readpages() (a buffered read or a defrag call
  6644. * triggered a readahead) on a page lock due to an
  6645. * ordered dio extent we created before but did not have
  6646. * yet a corresponding bio submitted (whence it can not
  6647. * complete), which makes readpages() wait for that
  6648. * ordered extent to complete while holding a lock on
  6649. * that page.
  6650. */
  6651. ret = -ENOTBLK;
  6652. }
  6653. if (ret)
  6654. break;
  6655. cond_resched();
  6656. }
  6657. return ret;
  6658. }
  6659. static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
  6660. u64 len, u64 orig_start,
  6661. u64 block_start, u64 block_len,
  6662. u64 orig_block_len, u64 ram_bytes,
  6663. int type)
  6664. {
  6665. struct extent_map_tree *em_tree;
  6666. struct extent_map *em;
  6667. struct btrfs_root *root = BTRFS_I(inode)->root;
  6668. int ret;
  6669. em_tree = &BTRFS_I(inode)->extent_tree;
  6670. em = alloc_extent_map();
  6671. if (!em)
  6672. return ERR_PTR(-ENOMEM);
  6673. em->start = start;
  6674. em->orig_start = orig_start;
  6675. em->mod_start = start;
  6676. em->mod_len = len;
  6677. em->len = len;
  6678. em->block_len = block_len;
  6679. em->block_start = block_start;
  6680. em->bdev = root->fs_info->fs_devices->latest_bdev;
  6681. em->orig_block_len = orig_block_len;
  6682. em->ram_bytes = ram_bytes;
  6683. em->generation = -1;
  6684. set_bit(EXTENT_FLAG_PINNED, &em->flags);
  6685. if (type == BTRFS_ORDERED_PREALLOC)
  6686. set_bit(EXTENT_FLAG_FILLING, &em->flags);
  6687. do {
  6688. btrfs_drop_extent_cache(inode, em->start,
  6689. em->start + em->len - 1, 0);
  6690. write_lock(&em_tree->lock);
  6691. ret = add_extent_mapping(em_tree, em, 1);
  6692. write_unlock(&em_tree->lock);
  6693. } while (ret == -EEXIST);
  6694. if (ret) {
  6695. free_extent_map(em);
  6696. return ERR_PTR(ret);
  6697. }
  6698. return em;
  6699. }
  6700. static void adjust_dio_outstanding_extents(struct inode *inode,
  6701. struct btrfs_dio_data *dio_data,
  6702. const u64 len)
  6703. {
  6704. unsigned num_extents;
  6705. num_extents = (unsigned) div64_u64(len + BTRFS_MAX_EXTENT_SIZE - 1,
  6706. BTRFS_MAX_EXTENT_SIZE);
  6707. /*
  6708. * If we have an outstanding_extents count still set then we're
  6709. * within our reservation, otherwise we need to adjust our inode
  6710. * counter appropriately.
  6711. */
  6712. if (dio_data->outstanding_extents) {
  6713. dio_data->outstanding_extents -= num_extents;
  6714. } else {
  6715. spin_lock(&BTRFS_I(inode)->lock);
  6716. BTRFS_I(inode)->outstanding_extents += num_extents;
  6717. spin_unlock(&BTRFS_I(inode)->lock);
  6718. }
  6719. }
  6720. static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
  6721. struct buffer_head *bh_result, int create)
  6722. {
  6723. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  6724. struct extent_map *em;
  6725. struct extent_state *cached_state = NULL;
  6726. struct btrfs_dio_data *dio_data = NULL;
  6727. u64 start = iblock << inode->i_blkbits;
  6728. u64 lockstart, lockend;
  6729. u64 len = bh_result->b_size;
  6730. int unlock_bits = EXTENT_LOCKED;
  6731. int ret = 0;
  6732. if (create)
  6733. unlock_bits |= EXTENT_DIRTY;
  6734. else
  6735. len = min_t(u64, len, fs_info->sectorsize);
  6736. lockstart = start;
  6737. lockend = start + len - 1;
  6738. if (current->journal_info) {
  6739. /*
  6740. * Need to pull our outstanding extents and set journal_info to NULL so
  6741. * that anything that needs to check if there's a transaction doesn't get
  6742. * confused.
  6743. */
  6744. dio_data = current->journal_info;
  6745. current->journal_info = NULL;
  6746. }
  6747. /*
  6748. * If this errors out it's because we couldn't invalidate pagecache for
  6749. * this range and we need to fallback to buffered.
  6750. */
  6751. if (lock_extent_direct(inode, lockstart, lockend, &cached_state,
  6752. create)) {
  6753. ret = -ENOTBLK;
  6754. goto err;
  6755. }
  6756. em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
  6757. if (IS_ERR(em)) {
  6758. ret = PTR_ERR(em);
  6759. goto unlock_err;
  6760. }
  6761. /*
  6762. * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
  6763. * io. INLINE is special, and we could probably kludge it in here, but
  6764. * it's still buffered so for safety lets just fall back to the generic
  6765. * buffered path.
  6766. *
  6767. * For COMPRESSED we _have_ to read the entire extent in so we can
  6768. * decompress it, so there will be buffering required no matter what we
  6769. * do, so go ahead and fallback to buffered.
  6770. *
  6771. * We return -ENOTBLK because that's what makes DIO go ahead and go back
  6772. * to buffered IO. Don't blame me, this is the price we pay for using
  6773. * the generic code.
  6774. */
  6775. if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
  6776. em->block_start == EXTENT_MAP_INLINE) {
  6777. free_extent_map(em);
  6778. ret = -ENOTBLK;
  6779. goto unlock_err;
  6780. }
  6781. /* Just a good old fashioned hole, return */
  6782. if (!create && (em->block_start == EXTENT_MAP_HOLE ||
  6783. test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
  6784. free_extent_map(em);
  6785. goto unlock_err;
  6786. }
  6787. /*
  6788. * We don't allocate a new extent in the following cases
  6789. *
  6790. * 1) The inode is marked as NODATACOW. In this case we'll just use the
  6791. * existing extent.
  6792. * 2) The extent is marked as PREALLOC. We're good to go here and can
  6793. * just use the extent.
  6794. *
  6795. */
  6796. if (!create) {
  6797. len = min(len, em->len - (start - em->start));
  6798. lockstart = start + len;
  6799. goto unlock;
  6800. }
  6801. if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
  6802. ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
  6803. em->block_start != EXTENT_MAP_HOLE)) {
  6804. int type;
  6805. u64 block_start, orig_start, orig_block_len, ram_bytes;
  6806. if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
  6807. type = BTRFS_ORDERED_PREALLOC;
  6808. else
  6809. type = BTRFS_ORDERED_NOCOW;
  6810. len = min(len, em->len - (start - em->start));
  6811. block_start = em->block_start + (start - em->start);
  6812. if (can_nocow_extent(inode, start, &len, &orig_start,
  6813. &orig_block_len, &ram_bytes) == 1 &&
  6814. btrfs_inc_nocow_writers(fs_info, block_start)) {
  6815. struct extent_map *em2;
  6816. em2 = btrfs_create_dio_extent(inode, start, len,
  6817. orig_start, block_start,
  6818. len, orig_block_len,
  6819. ram_bytes, type);
  6820. btrfs_dec_nocow_writers(fs_info, block_start);
  6821. if (type == BTRFS_ORDERED_PREALLOC) {
  6822. free_extent_map(em);
  6823. em = em2;
  6824. }
  6825. if (em2 && IS_ERR(em2)) {
  6826. ret = PTR_ERR(em2);
  6827. goto unlock_err;
  6828. }
  6829. /*
  6830. * For inode marked NODATACOW or extent marked PREALLOC,
  6831. * use the existing or preallocated extent, so does not
  6832. * need to adjust btrfs_space_info's bytes_may_use.
  6833. */
  6834. btrfs_free_reserved_data_space_noquota(inode,
  6835. start, len);
  6836. goto unlock;
  6837. }
  6838. }
  6839. /*
  6840. * this will cow the extent, if em is within [start, len], then
  6841. * probably we've found a preallocated/existing extent, let's
  6842. * give it a chance to use preallocated space.
  6843. */
  6844. len = min_t(u64, bh_result->b_size, em->len - (start - em->start));
  6845. len = ALIGN(len, fs_info->sectorsize);
  6846. free_extent_map(em);
  6847. em = btrfs_new_extent_direct(inode, start, len);
  6848. if (IS_ERR(em)) {
  6849. ret = PTR_ERR(em);
  6850. goto unlock_err;
  6851. }
  6852. len = min(len, em->len - (start - em->start));
  6853. unlock:
  6854. bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
  6855. inode->i_blkbits;
  6856. bh_result->b_size = len;
  6857. bh_result->b_bdev = em->bdev;
  6858. set_buffer_mapped(bh_result);
  6859. if (create) {
  6860. if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
  6861. set_buffer_new(bh_result);
  6862. /*
  6863. * Need to update the i_size under the extent lock so buffered
  6864. * readers will get the updated i_size when we unlock.
  6865. */
  6866. if (start + len > i_size_read(inode))
  6867. i_size_write(inode, start + len);
  6868. adjust_dio_outstanding_extents(inode, dio_data, len);
  6869. WARN_ON(dio_data->reserve < len);
  6870. dio_data->reserve -= len;
  6871. dio_data->unsubmitted_oe_range_end = start + len;
  6872. current->journal_info = dio_data;
  6873. }
  6874. /*
  6875. * In the case of write we need to clear and unlock the entire range,
  6876. * in the case of read we need to unlock only the end area that we
  6877. * aren't using if there is any left over space.
  6878. */
  6879. if (lockstart < lockend) {
  6880. clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
  6881. lockend, unlock_bits, 1, 0,
  6882. &cached_state, GFP_NOFS);
  6883. } else {
  6884. free_extent_state(cached_state);
  6885. }
  6886. free_extent_map(em);
  6887. return 0;
  6888. unlock_err:
  6889. clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
  6890. unlock_bits, 1, 0, &cached_state, GFP_NOFS);
  6891. err:
  6892. if (dio_data)
  6893. current->journal_info = dio_data;
  6894. /*
  6895. * Compensate the delalloc release we do in btrfs_direct_IO() when we
  6896. * write less data then expected, so that we don't underflow our inode's
  6897. * outstanding extents counter.
  6898. */
  6899. if (create && dio_data)
  6900. adjust_dio_outstanding_extents(inode, dio_data, len);
  6901. return ret;
  6902. }
  6903. static inline int submit_dio_repair_bio(struct inode *inode, struct bio *bio,
  6904. int mirror_num)
  6905. {
  6906. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  6907. int ret;
  6908. BUG_ON(bio_op(bio) == REQ_OP_WRITE);
  6909. bio_get(bio);
  6910. ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DIO_REPAIR);
  6911. if (ret)
  6912. goto err;
  6913. ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
  6914. err:
  6915. bio_put(bio);
  6916. return ret;
  6917. }
  6918. static int btrfs_check_dio_repairable(struct inode *inode,
  6919. struct bio *failed_bio,
  6920. struct io_failure_record *failrec,
  6921. int failed_mirror)
  6922. {
  6923. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  6924. int num_copies;
  6925. num_copies = btrfs_num_copies(fs_info, failrec->logical, failrec->len);
  6926. if (num_copies == 1) {
  6927. /*
  6928. * we only have a single copy of the data, so don't bother with
  6929. * all the retry and error correction code that follows. no
  6930. * matter what the error is, it is very likely to persist.
  6931. */
  6932. btrfs_debug(fs_info,
  6933. "Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d",
  6934. num_copies, failrec->this_mirror, failed_mirror);
  6935. return 0;
  6936. }
  6937. failrec->failed_mirror = failed_mirror;
  6938. failrec->this_mirror++;
  6939. if (failrec->this_mirror == failed_mirror)
  6940. failrec->this_mirror++;
  6941. if (failrec->this_mirror > num_copies) {
  6942. btrfs_debug(fs_info,
  6943. "Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d",
  6944. num_copies, failrec->this_mirror, failed_mirror);
  6945. return 0;
  6946. }
  6947. return 1;
  6948. }
  6949. static int dio_read_error(struct inode *inode, struct bio *failed_bio,
  6950. struct page *page, unsigned int pgoff,
  6951. u64 start, u64 end, int failed_mirror,
  6952. bio_end_io_t *repair_endio, void *repair_arg)
  6953. {
  6954. struct io_failure_record *failrec;
  6955. struct bio *bio;
  6956. int isector;
  6957. int read_mode;
  6958. int ret;
  6959. BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE);
  6960. ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
  6961. if (ret)
  6962. return ret;
  6963. ret = btrfs_check_dio_repairable(inode, failed_bio, failrec,
  6964. failed_mirror);
  6965. if (!ret) {
  6966. free_io_failure(inode, failrec);
  6967. return -EIO;
  6968. }
  6969. if ((failed_bio->bi_vcnt > 1)
  6970. || (failed_bio->bi_io_vec->bv_len
  6971. > btrfs_inode_sectorsize(inode)))
  6972. read_mode = READ_SYNC | REQ_FAILFAST_DEV;
  6973. else
  6974. read_mode = READ_SYNC;
  6975. isector = start - btrfs_io_bio(failed_bio)->logical;
  6976. isector >>= inode->i_sb->s_blocksize_bits;
  6977. bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
  6978. pgoff, isector, repair_endio, repair_arg);
  6979. if (!bio) {
  6980. free_io_failure(inode, failrec);
  6981. return -EIO;
  6982. }
  6983. bio_set_op_attrs(bio, REQ_OP_READ, read_mode);
  6984. btrfs_debug(BTRFS_I(inode)->root->fs_info,
  6985. "Repair DIO Read Error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d\n",
  6986. read_mode, failrec->this_mirror, failrec->in_validation);
  6987. ret = submit_dio_repair_bio(inode, bio, failrec->this_mirror);
  6988. if (ret) {
  6989. free_io_failure(inode, failrec);
  6990. bio_put(bio);
  6991. }
  6992. return ret;
  6993. }
  6994. struct btrfs_retry_complete {
  6995. struct completion done;
  6996. struct inode *inode;
  6997. u64 start;
  6998. int uptodate;
  6999. };
  7000. static void btrfs_retry_endio_nocsum(struct bio *bio)
  7001. {
  7002. struct btrfs_retry_complete *done = bio->bi_private;
  7003. struct inode *inode;
  7004. struct bio_vec *bvec;
  7005. int i;
  7006. if (bio->bi_error)
  7007. goto end;
  7008. ASSERT(bio->bi_vcnt == 1);
  7009. inode = bio->bi_io_vec->bv_page->mapping->host;
  7010. ASSERT(bio->bi_io_vec->bv_len == btrfs_inode_sectorsize(inode));
  7011. done->uptodate = 1;
  7012. bio_for_each_segment_all(bvec, bio, i)
  7013. clean_io_failure(done->inode, done->start, bvec->bv_page, 0);
  7014. end:
  7015. complete(&done->done);
  7016. bio_put(bio);
  7017. }
  7018. static int __btrfs_correct_data_nocsum(struct inode *inode,
  7019. struct btrfs_io_bio *io_bio)
  7020. {
  7021. struct btrfs_fs_info *fs_info;
  7022. struct bio_vec *bvec;
  7023. struct btrfs_retry_complete done;
  7024. u64 start;
  7025. unsigned int pgoff;
  7026. u32 sectorsize;
  7027. int nr_sectors;
  7028. int i;
  7029. int ret;
  7030. fs_info = BTRFS_I(inode)->root->fs_info;
  7031. sectorsize = fs_info->sectorsize;
  7032. start = io_bio->logical;
  7033. done.inode = inode;
  7034. bio_for_each_segment_all(bvec, &io_bio->bio, i) {
  7035. nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec->bv_len);
  7036. pgoff = bvec->bv_offset;
  7037. next_block_or_try_again:
  7038. done.uptodate = 0;
  7039. done.start = start;
  7040. init_completion(&done.done);
  7041. ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page,
  7042. pgoff, start, start + sectorsize - 1,
  7043. io_bio->mirror_num,
  7044. btrfs_retry_endio_nocsum, &done);
  7045. if (ret)
  7046. return ret;
  7047. wait_for_completion(&done.done);
  7048. if (!done.uptodate) {
  7049. /* We might have another mirror, so try again */
  7050. goto next_block_or_try_again;
  7051. }
  7052. start += sectorsize;
  7053. if (nr_sectors--) {
  7054. pgoff += sectorsize;
  7055. goto next_block_or_try_again;
  7056. }
  7057. }
  7058. return 0;
  7059. }
  7060. static void btrfs_retry_endio(struct bio *bio)
  7061. {
  7062. struct btrfs_retry_complete *done = bio->bi_private;
  7063. struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
  7064. struct inode *inode;
  7065. struct bio_vec *bvec;
  7066. u64 start;
  7067. int uptodate;
  7068. int ret;
  7069. int i;
  7070. if (bio->bi_error)
  7071. goto end;
  7072. uptodate = 1;
  7073. start = done->start;
  7074. ASSERT(bio->bi_vcnt == 1);
  7075. inode = bio->bi_io_vec->bv_page->mapping->host;
  7076. ASSERT(bio->bi_io_vec->bv_len == btrfs_inode_sectorsize(inode));
  7077. bio_for_each_segment_all(bvec, bio, i) {
  7078. ret = __readpage_endio_check(done->inode, io_bio, i,
  7079. bvec->bv_page, bvec->bv_offset,
  7080. done->start, bvec->bv_len);
  7081. if (!ret)
  7082. clean_io_failure(done->inode, done->start,
  7083. bvec->bv_page, bvec->bv_offset);
  7084. else
  7085. uptodate = 0;
  7086. }
  7087. done->uptodate = uptodate;
  7088. end:
  7089. complete(&done->done);
  7090. bio_put(bio);
  7091. }
  7092. static int __btrfs_subio_endio_read(struct inode *inode,
  7093. struct btrfs_io_bio *io_bio, int err)
  7094. {
  7095. struct btrfs_fs_info *fs_info;
  7096. struct bio_vec *bvec;
  7097. struct btrfs_retry_complete done;
  7098. u64 start;
  7099. u64 offset = 0;
  7100. u32 sectorsize;
  7101. int nr_sectors;
  7102. unsigned int pgoff;
  7103. int csum_pos;
  7104. int i;
  7105. int ret;
  7106. fs_info = BTRFS_I(inode)->root->fs_info;
  7107. sectorsize = fs_info->sectorsize;
  7108. err = 0;
  7109. start = io_bio->logical;
  7110. done.inode = inode;
  7111. bio_for_each_segment_all(bvec, &io_bio->bio, i) {
  7112. nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec->bv_len);
  7113. pgoff = bvec->bv_offset;
  7114. next_block:
  7115. csum_pos = BTRFS_BYTES_TO_BLKS(fs_info, offset);
  7116. ret = __readpage_endio_check(inode, io_bio, csum_pos,
  7117. bvec->bv_page, pgoff, start,
  7118. sectorsize);
  7119. if (likely(!ret))
  7120. goto next;
  7121. try_again:
  7122. done.uptodate = 0;
  7123. done.start = start;
  7124. init_completion(&done.done);
  7125. ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page,
  7126. pgoff, start, start + sectorsize - 1,
  7127. io_bio->mirror_num,
  7128. btrfs_retry_endio, &done);
  7129. if (ret) {
  7130. err = ret;
  7131. goto next;
  7132. }
  7133. wait_for_completion(&done.done);
  7134. if (!done.uptodate) {
  7135. /* We might have another mirror, so try again */
  7136. goto try_again;
  7137. }
  7138. next:
  7139. offset += sectorsize;
  7140. start += sectorsize;
  7141. ASSERT(nr_sectors);
  7142. if (--nr_sectors) {
  7143. pgoff += sectorsize;
  7144. goto next_block;
  7145. }
  7146. }
  7147. return err;
  7148. }
  7149. static int btrfs_subio_endio_read(struct inode *inode,
  7150. struct btrfs_io_bio *io_bio, int err)
  7151. {
  7152. bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
  7153. if (skip_csum) {
  7154. if (unlikely(err))
  7155. return __btrfs_correct_data_nocsum(inode, io_bio);
  7156. else
  7157. return 0;
  7158. } else {
  7159. return __btrfs_subio_endio_read(inode, io_bio, err);
  7160. }
  7161. }
  7162. static void btrfs_endio_direct_read(struct bio *bio)
  7163. {
  7164. struct btrfs_dio_private *dip = bio->bi_private;
  7165. struct inode *inode = dip->inode;
  7166. struct bio *dio_bio;
  7167. struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
  7168. int err = bio->bi_error;
  7169. if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED)
  7170. err = btrfs_subio_endio_read(inode, io_bio, err);
  7171. unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
  7172. dip->logical_offset + dip->bytes - 1);
  7173. dio_bio = dip->dio_bio;
  7174. kfree(dip);
  7175. dio_bio->bi_error = bio->bi_error;
  7176. dio_end_io(dio_bio, bio->bi_error);
  7177. if (io_bio->end_io)
  7178. io_bio->end_io(io_bio, err);
  7179. bio_put(bio);
  7180. }
  7181. static void btrfs_endio_direct_write_update_ordered(struct inode *inode,
  7182. const u64 offset,
  7183. const u64 bytes,
  7184. const int uptodate)
  7185. {
  7186. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  7187. struct btrfs_ordered_extent *ordered = NULL;
  7188. u64 ordered_offset = offset;
  7189. u64 ordered_bytes = bytes;
  7190. int ret;
  7191. again:
  7192. ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
  7193. &ordered_offset,
  7194. ordered_bytes,
  7195. uptodate);
  7196. if (!ret)
  7197. goto out_test;
  7198. btrfs_init_work(&ordered->work, btrfs_endio_write_helper,
  7199. finish_ordered_fn, NULL, NULL);
  7200. btrfs_queue_work(fs_info->endio_write_workers, &ordered->work);
  7201. out_test:
  7202. /*
  7203. * our bio might span multiple ordered extents. If we haven't
  7204. * completed the accounting for the whole dio, go back and try again
  7205. */
  7206. if (ordered_offset < offset + bytes) {
  7207. ordered_bytes = offset + bytes - ordered_offset;
  7208. ordered = NULL;
  7209. goto again;
  7210. }
  7211. }
  7212. static void btrfs_endio_direct_write(struct bio *bio)
  7213. {
  7214. struct btrfs_dio_private *dip = bio->bi_private;
  7215. struct bio *dio_bio = dip->dio_bio;
  7216. btrfs_endio_direct_write_update_ordered(dip->inode,
  7217. dip->logical_offset,
  7218. dip->bytes,
  7219. !bio->bi_error);
  7220. kfree(dip);
  7221. dio_bio->bi_error = bio->bi_error;
  7222. dio_end_io(dio_bio, bio->bi_error);
  7223. bio_put(bio);
  7224. }
  7225. static int __btrfs_submit_bio_start_direct_io(struct inode *inode,
  7226. struct bio *bio, int mirror_num,
  7227. unsigned long bio_flags, u64 offset)
  7228. {
  7229. int ret;
  7230. ret = btrfs_csum_one_bio(inode, bio, offset, 1);
  7231. BUG_ON(ret); /* -ENOMEM */
  7232. return 0;
  7233. }
  7234. static void btrfs_end_dio_bio(struct bio *bio)
  7235. {
  7236. struct btrfs_dio_private *dip = bio->bi_private;
  7237. int err = bio->bi_error;
  7238. if (err)
  7239. btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
  7240. "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d",
  7241. btrfs_ino(dip->inode), bio_op(bio), bio->bi_opf,
  7242. (unsigned long long)bio->bi_iter.bi_sector,
  7243. bio->bi_iter.bi_size, err);
  7244. if (dip->subio_endio)
  7245. err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err);
  7246. if (err) {
  7247. dip->errors = 1;
  7248. /*
  7249. * before atomic variable goto zero, we must make sure
  7250. * dip->errors is perceived to be set.
  7251. */
  7252. smp_mb__before_atomic();
  7253. }
  7254. /* if there are more bios still pending for this dio, just exit */
  7255. if (!atomic_dec_and_test(&dip->pending_bios))
  7256. goto out;
  7257. if (dip->errors) {
  7258. bio_io_error(dip->orig_bio);
  7259. } else {
  7260. dip->dio_bio->bi_error = 0;
  7261. bio_endio(dip->orig_bio);
  7262. }
  7263. out:
  7264. bio_put(bio);
  7265. }
  7266. static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
  7267. u64 first_sector, gfp_t gfp_flags)
  7268. {
  7269. struct bio *bio;
  7270. bio = btrfs_bio_alloc(bdev, first_sector, BIO_MAX_PAGES, gfp_flags);
  7271. if (bio)
  7272. bio_associate_current(bio);
  7273. return bio;
  7274. }
  7275. static inline int btrfs_lookup_and_bind_dio_csum(struct inode *inode,
  7276. struct btrfs_dio_private *dip,
  7277. struct bio *bio,
  7278. u64 file_offset)
  7279. {
  7280. struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
  7281. struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio);
  7282. int ret;
  7283. /*
  7284. * We load all the csum data we need when we submit
  7285. * the first bio to reduce the csum tree search and
  7286. * contention.
  7287. */
  7288. if (dip->logical_offset == file_offset) {
  7289. ret = btrfs_lookup_bio_sums_dio(inode, dip->orig_bio,
  7290. file_offset);
  7291. if (ret)
  7292. return ret;
  7293. }
  7294. if (bio == dip->orig_bio)
  7295. return 0;
  7296. file_offset -= dip->logical_offset;
  7297. file_offset >>= inode->i_sb->s_blocksize_bits;
  7298. io_bio->csum = (u8 *)(((u32 *)orig_io_bio->csum) + file_offset);
  7299. return 0;
  7300. }
  7301. static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
  7302. u64 file_offset, int skip_sum,
  7303. int async_submit)
  7304. {
  7305. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  7306. struct btrfs_dio_private *dip = bio->bi_private;
  7307. bool write = bio_op(bio) == REQ_OP_WRITE;
  7308. int ret;
  7309. if (async_submit)
  7310. async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
  7311. bio_get(bio);
  7312. if (!write) {
  7313. ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
  7314. if (ret)
  7315. goto err;
  7316. }
  7317. if (skip_sum)
  7318. goto map;
  7319. if (write && async_submit) {
  7320. ret = btrfs_wq_submit_bio(fs_info, inode, bio, 0, 0,
  7321. file_offset,
  7322. __btrfs_submit_bio_start_direct_io,
  7323. __btrfs_submit_bio_done);
  7324. goto err;
  7325. } else if (write) {
  7326. /*
  7327. * If we aren't doing async submit, calculate the csum of the
  7328. * bio now.
  7329. */
  7330. ret = btrfs_csum_one_bio(inode, bio, file_offset, 1);
  7331. if (ret)
  7332. goto err;
  7333. } else {
  7334. ret = btrfs_lookup_and_bind_dio_csum(inode, dip, bio,
  7335. file_offset);
  7336. if (ret)
  7337. goto err;
  7338. }
  7339. map:
  7340. ret = btrfs_map_bio(fs_info, bio, 0, async_submit);
  7341. err:
  7342. bio_put(bio);
  7343. return ret;
  7344. }
  7345. static int btrfs_submit_direct_hook(struct btrfs_dio_private *dip,
  7346. int skip_sum)
  7347. {
  7348. struct inode *inode = dip->inode;
  7349. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  7350. struct btrfs_root *root = BTRFS_I(inode)->root;
  7351. struct bio *bio;
  7352. struct bio *orig_bio = dip->orig_bio;
  7353. struct bio_vec *bvec;
  7354. u64 start_sector = orig_bio->bi_iter.bi_sector;
  7355. u64 file_offset = dip->logical_offset;
  7356. u64 submit_len = 0;
  7357. u64 map_length;
  7358. u32 blocksize = fs_info->sectorsize;
  7359. int async_submit = 0;
  7360. int nr_sectors;
  7361. int ret;
  7362. int i, j;
  7363. map_length = orig_bio->bi_iter.bi_size;
  7364. ret = btrfs_map_block(fs_info, btrfs_op(orig_bio), start_sector << 9,
  7365. &map_length, NULL, 0);
  7366. if (ret)
  7367. return -EIO;
  7368. if (map_length >= orig_bio->bi_iter.bi_size) {
  7369. bio = orig_bio;
  7370. dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED;
  7371. goto submit;
  7372. }
  7373. /* async crcs make it difficult to collect full stripe writes. */
  7374. if (btrfs_get_alloc_profile(root, 1) & BTRFS_BLOCK_GROUP_RAID56_MASK)
  7375. async_submit = 0;
  7376. else
  7377. async_submit = 1;
  7378. bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
  7379. if (!bio)
  7380. return -ENOMEM;
  7381. bio_set_op_attrs(bio, bio_op(orig_bio), bio_flags(orig_bio));
  7382. bio->bi_private = dip;
  7383. bio->bi_end_io = btrfs_end_dio_bio;
  7384. btrfs_io_bio(bio)->logical = file_offset;
  7385. atomic_inc(&dip->pending_bios);
  7386. bio_for_each_segment_all(bvec, orig_bio, j) {
  7387. nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec->bv_len);
  7388. i = 0;
  7389. next_block:
  7390. if (unlikely(map_length < submit_len + blocksize ||
  7391. bio_add_page(bio, bvec->bv_page, blocksize,
  7392. bvec->bv_offset + (i * blocksize)) < blocksize)) {
  7393. /*
  7394. * inc the count before we submit the bio so
  7395. * we know the end IO handler won't happen before
  7396. * we inc the count. Otherwise, the dip might get freed
  7397. * before we're done setting it up
  7398. */
  7399. atomic_inc(&dip->pending_bios);
  7400. ret = __btrfs_submit_dio_bio(bio, inode,
  7401. file_offset, skip_sum,
  7402. async_submit);
  7403. if (ret) {
  7404. bio_put(bio);
  7405. atomic_dec(&dip->pending_bios);
  7406. goto out_err;
  7407. }
  7408. start_sector += submit_len >> 9;
  7409. file_offset += submit_len;
  7410. submit_len = 0;
  7411. bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
  7412. start_sector, GFP_NOFS);
  7413. if (!bio)
  7414. goto out_err;
  7415. bio_set_op_attrs(bio, bio_op(orig_bio),
  7416. bio_flags(orig_bio));
  7417. bio->bi_private = dip;
  7418. bio->bi_end_io = btrfs_end_dio_bio;
  7419. btrfs_io_bio(bio)->logical = file_offset;
  7420. map_length = orig_bio->bi_iter.bi_size;
  7421. ret = btrfs_map_block(fs_info, btrfs_op(orig_bio),
  7422. start_sector << 9,
  7423. &map_length, NULL, 0);
  7424. if (ret) {
  7425. bio_put(bio);
  7426. goto out_err;
  7427. }
  7428. goto next_block;
  7429. } else {
  7430. submit_len += blocksize;
  7431. if (--nr_sectors) {
  7432. i++;
  7433. goto next_block;
  7434. }
  7435. }
  7436. }
  7437. submit:
  7438. ret = __btrfs_submit_dio_bio(bio, inode, file_offset, skip_sum,
  7439. async_submit);
  7440. if (!ret)
  7441. return 0;
  7442. bio_put(bio);
  7443. out_err:
  7444. dip->errors = 1;
  7445. /*
  7446. * before atomic variable goto zero, we must
  7447. * make sure dip->errors is perceived to be set.
  7448. */
  7449. smp_mb__before_atomic();
  7450. if (atomic_dec_and_test(&dip->pending_bios))
  7451. bio_io_error(dip->orig_bio);
  7452. /* bio_end_io() will handle error, so we needn't return it */
  7453. return 0;
  7454. }
  7455. static void btrfs_submit_direct(struct bio *dio_bio, struct inode *inode,
  7456. loff_t file_offset)
  7457. {
  7458. struct btrfs_dio_private *dip = NULL;
  7459. struct bio *io_bio = NULL;
  7460. struct btrfs_io_bio *btrfs_bio;
  7461. int skip_sum;
  7462. bool write = (bio_op(dio_bio) == REQ_OP_WRITE);
  7463. int ret = 0;
  7464. skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
  7465. io_bio = btrfs_bio_clone(dio_bio, GFP_NOFS);
  7466. if (!io_bio) {
  7467. ret = -ENOMEM;
  7468. goto free_ordered;
  7469. }
  7470. dip = kzalloc(sizeof(*dip), GFP_NOFS);
  7471. if (!dip) {
  7472. ret = -ENOMEM;
  7473. goto free_ordered;
  7474. }
  7475. dip->private = dio_bio->bi_private;
  7476. dip->inode = inode;
  7477. dip->logical_offset = file_offset;
  7478. dip->bytes = dio_bio->bi_iter.bi_size;
  7479. dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
  7480. io_bio->bi_private = dip;
  7481. dip->orig_bio = io_bio;
  7482. dip->dio_bio = dio_bio;
  7483. atomic_set(&dip->pending_bios, 0);
  7484. btrfs_bio = btrfs_io_bio(io_bio);
  7485. btrfs_bio->logical = file_offset;
  7486. if (write) {
  7487. io_bio->bi_end_io = btrfs_endio_direct_write;
  7488. } else {
  7489. io_bio->bi_end_io = btrfs_endio_direct_read;
  7490. dip->subio_endio = btrfs_subio_endio_read;
  7491. }
  7492. /*
  7493. * Reset the range for unsubmitted ordered extents (to a 0 length range)
  7494. * even if we fail to submit a bio, because in such case we do the
  7495. * corresponding error handling below and it must not be done a second
  7496. * time by btrfs_direct_IO().
  7497. */
  7498. if (write) {
  7499. struct btrfs_dio_data *dio_data = current->journal_info;
  7500. dio_data->unsubmitted_oe_range_end = dip->logical_offset +
  7501. dip->bytes;
  7502. dio_data->unsubmitted_oe_range_start =
  7503. dio_data->unsubmitted_oe_range_end;
  7504. }
  7505. ret = btrfs_submit_direct_hook(dip, skip_sum);
  7506. if (!ret)
  7507. return;
  7508. if (btrfs_bio->end_io)
  7509. btrfs_bio->end_io(btrfs_bio, ret);
  7510. free_ordered:
  7511. /*
  7512. * If we arrived here it means either we failed to submit the dip
  7513. * or we either failed to clone the dio_bio or failed to allocate the
  7514. * dip. If we cloned the dio_bio and allocated the dip, we can just
  7515. * call bio_endio against our io_bio so that we get proper resource
  7516. * cleanup if we fail to submit the dip, otherwise, we must do the
  7517. * same as btrfs_endio_direct_[write|read] because we can't call these
  7518. * callbacks - they require an allocated dip and a clone of dio_bio.
  7519. */
  7520. if (io_bio && dip) {
  7521. io_bio->bi_error = -EIO;
  7522. bio_endio(io_bio);
  7523. /*
  7524. * The end io callbacks free our dip, do the final put on io_bio
  7525. * and all the cleanup and final put for dio_bio (through
  7526. * dio_end_io()).
  7527. */
  7528. dip = NULL;
  7529. io_bio = NULL;
  7530. } else {
  7531. if (write)
  7532. btrfs_endio_direct_write_update_ordered(inode,
  7533. file_offset,
  7534. dio_bio->bi_iter.bi_size,
  7535. 0);
  7536. else
  7537. unlock_extent(&BTRFS_I(inode)->io_tree, file_offset,
  7538. file_offset + dio_bio->bi_iter.bi_size - 1);
  7539. dio_bio->bi_error = -EIO;
  7540. /*
  7541. * Releases and cleans up our dio_bio, no need to bio_put()
  7542. * nor bio_endio()/bio_io_error() against dio_bio.
  7543. */
  7544. dio_end_io(dio_bio, ret);
  7545. }
  7546. if (io_bio)
  7547. bio_put(io_bio);
  7548. kfree(dip);
  7549. }
  7550. static ssize_t check_direct_IO(struct btrfs_fs_info *fs_info,
  7551. struct kiocb *iocb,
  7552. const struct iov_iter *iter, loff_t offset)
  7553. {
  7554. int seg;
  7555. int i;
  7556. unsigned int blocksize_mask = fs_info->sectorsize - 1;
  7557. ssize_t retval = -EINVAL;
  7558. if (offset & blocksize_mask)
  7559. goto out;
  7560. if (iov_iter_alignment(iter) & blocksize_mask)
  7561. goto out;
  7562. /* If this is a write we don't need to check anymore */
  7563. if (iov_iter_rw(iter) != READ || !iter_is_iovec(iter))
  7564. return 0;
  7565. /*
  7566. * Check to make sure we don't have duplicate iov_base's in this
  7567. * iovec, if so return EINVAL, otherwise we'll get csum errors
  7568. * when reading back.
  7569. */
  7570. for (seg = 0; seg < iter->nr_segs; seg++) {
  7571. for (i = seg + 1; i < iter->nr_segs; i++) {
  7572. if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
  7573. goto out;
  7574. }
  7575. }
  7576. retval = 0;
  7577. out:
  7578. return retval;
  7579. }
  7580. static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
  7581. {
  7582. struct file *file = iocb->ki_filp;
  7583. struct inode *inode = file->f_mapping->host;
  7584. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  7585. struct btrfs_dio_data dio_data = { 0 };
  7586. loff_t offset = iocb->ki_pos;
  7587. size_t count = 0;
  7588. int flags = 0;
  7589. bool wakeup = true;
  7590. bool relock = false;
  7591. ssize_t ret;
  7592. if (check_direct_IO(fs_info, iocb, iter, offset))
  7593. return 0;
  7594. inode_dio_begin(inode);
  7595. smp_mb__after_atomic();
  7596. /*
  7597. * The generic stuff only does filemap_write_and_wait_range, which
  7598. * isn't enough if we've written compressed pages to this area, so
  7599. * we need to flush the dirty pages again to make absolutely sure
  7600. * that any outstanding dirty pages are on disk.
  7601. */
  7602. count = iov_iter_count(iter);
  7603. if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
  7604. &BTRFS_I(inode)->runtime_flags))
  7605. filemap_fdatawrite_range(inode->i_mapping, offset,
  7606. offset + count - 1);
  7607. if (iov_iter_rw(iter) == WRITE) {
  7608. /*
  7609. * If the write DIO is beyond the EOF, we need update
  7610. * the isize, but it is protected by i_mutex. So we can
  7611. * not unlock the i_mutex at this case.
  7612. */
  7613. if (offset + count <= inode->i_size) {
  7614. inode_unlock(inode);
  7615. relock = true;
  7616. }
  7617. ret = btrfs_delalloc_reserve_space(inode, offset, count);
  7618. if (ret)
  7619. goto out;
  7620. dio_data.outstanding_extents = div64_u64(count +
  7621. BTRFS_MAX_EXTENT_SIZE - 1,
  7622. BTRFS_MAX_EXTENT_SIZE);
  7623. /*
  7624. * We need to know how many extents we reserved so that we can
  7625. * do the accounting properly if we go over the number we
  7626. * originally calculated. Abuse current->journal_info for this.
  7627. */
  7628. dio_data.reserve = round_up(count,
  7629. fs_info->sectorsize);
  7630. dio_data.unsubmitted_oe_range_start = (u64)offset;
  7631. dio_data.unsubmitted_oe_range_end = (u64)offset;
  7632. current->journal_info = &dio_data;
  7633. } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
  7634. &BTRFS_I(inode)->runtime_flags)) {
  7635. inode_dio_end(inode);
  7636. flags = DIO_LOCKING | DIO_SKIP_HOLES;
  7637. wakeup = false;
  7638. }
  7639. ret = __blockdev_direct_IO(iocb, inode,
  7640. fs_info->fs_devices->latest_bdev,
  7641. iter, btrfs_get_blocks_direct, NULL,
  7642. btrfs_submit_direct, flags);
  7643. if (iov_iter_rw(iter) == WRITE) {
  7644. current->journal_info = NULL;
  7645. if (ret < 0 && ret != -EIOCBQUEUED) {
  7646. if (dio_data.reserve)
  7647. btrfs_delalloc_release_space(inode, offset,
  7648. dio_data.reserve);
  7649. /*
  7650. * On error we might have left some ordered extents
  7651. * without submitting corresponding bios for them, so
  7652. * cleanup them up to avoid other tasks getting them
  7653. * and waiting for them to complete forever.
  7654. */
  7655. if (dio_data.unsubmitted_oe_range_start <
  7656. dio_data.unsubmitted_oe_range_end)
  7657. btrfs_endio_direct_write_update_ordered(inode,
  7658. dio_data.unsubmitted_oe_range_start,
  7659. dio_data.unsubmitted_oe_range_end -
  7660. dio_data.unsubmitted_oe_range_start,
  7661. 0);
  7662. } else if (ret >= 0 && (size_t)ret < count)
  7663. btrfs_delalloc_release_space(inode, offset,
  7664. count - (size_t)ret);
  7665. }
  7666. out:
  7667. if (wakeup)
  7668. inode_dio_end(inode);
  7669. if (relock)
  7670. inode_lock(inode);
  7671. return ret;
  7672. }
  7673. #define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
  7674. static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
  7675. __u64 start, __u64 len)
  7676. {
  7677. int ret;
  7678. ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
  7679. if (ret)
  7680. return ret;
  7681. return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
  7682. }
  7683. int btrfs_readpage(struct file *file, struct page *page)
  7684. {
  7685. struct extent_io_tree *tree;
  7686. tree = &BTRFS_I(page->mapping->host)->io_tree;
  7687. return extent_read_full_page(tree, page, btrfs_get_extent, 0);
  7688. }
  7689. static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
  7690. {
  7691. struct extent_io_tree *tree;
  7692. struct inode *inode = page->mapping->host;
  7693. int ret;
  7694. if (current->flags & PF_MEMALLOC) {
  7695. redirty_page_for_writepage(wbc, page);
  7696. unlock_page(page);
  7697. return 0;
  7698. }
  7699. /*
  7700. * If we are under memory pressure we will call this directly from the
  7701. * VM, we need to make sure we have the inode referenced for the ordered
  7702. * extent. If not just return like we didn't do anything.
  7703. */
  7704. if (!igrab(inode)) {
  7705. redirty_page_for_writepage(wbc, page);
  7706. return AOP_WRITEPAGE_ACTIVATE;
  7707. }
  7708. tree = &BTRFS_I(page->mapping->host)->io_tree;
  7709. ret = extent_write_full_page(tree, page, btrfs_get_extent, wbc);
  7710. btrfs_add_delayed_iput(inode);
  7711. return ret;
  7712. }
  7713. static int btrfs_writepages(struct address_space *mapping,
  7714. struct writeback_control *wbc)
  7715. {
  7716. struct extent_io_tree *tree;
  7717. tree = &BTRFS_I(mapping->host)->io_tree;
  7718. return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
  7719. }
  7720. static int
  7721. btrfs_readpages(struct file *file, struct address_space *mapping,
  7722. struct list_head *pages, unsigned nr_pages)
  7723. {
  7724. struct extent_io_tree *tree;
  7725. tree = &BTRFS_I(mapping->host)->io_tree;
  7726. return extent_readpages(tree, mapping, pages, nr_pages,
  7727. btrfs_get_extent);
  7728. }
  7729. static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
  7730. {
  7731. struct extent_io_tree *tree;
  7732. struct extent_map_tree *map;
  7733. int ret;
  7734. tree = &BTRFS_I(page->mapping->host)->io_tree;
  7735. map = &BTRFS_I(page->mapping->host)->extent_tree;
  7736. ret = try_release_extent_mapping(map, tree, page, gfp_flags);
  7737. if (ret == 1) {
  7738. ClearPagePrivate(page);
  7739. set_page_private(page, 0);
  7740. put_page(page);
  7741. }
  7742. return ret;
  7743. }
  7744. static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
  7745. {
  7746. if (PageWriteback(page) || PageDirty(page))
  7747. return 0;
  7748. return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
  7749. }
  7750. static void btrfs_invalidatepage(struct page *page, unsigned int offset,
  7751. unsigned int length)
  7752. {
  7753. struct inode *inode = page->mapping->host;
  7754. struct extent_io_tree *tree;
  7755. struct btrfs_ordered_extent *ordered;
  7756. struct extent_state *cached_state = NULL;
  7757. u64 page_start = page_offset(page);
  7758. u64 page_end = page_start + PAGE_SIZE - 1;
  7759. u64 start;
  7760. u64 end;
  7761. int inode_evicting = inode->i_state & I_FREEING;
  7762. /*
  7763. * we have the page locked, so new writeback can't start,
  7764. * and the dirty bit won't be cleared while we are here.
  7765. *
  7766. * Wait for IO on this page so that we can safely clear
  7767. * the PagePrivate2 bit and do ordered accounting
  7768. */
  7769. wait_on_page_writeback(page);
  7770. tree = &BTRFS_I(inode)->io_tree;
  7771. if (offset) {
  7772. btrfs_releasepage(page, GFP_NOFS);
  7773. return;
  7774. }
  7775. if (!inode_evicting)
  7776. lock_extent_bits(tree, page_start, page_end, &cached_state);
  7777. again:
  7778. start = page_start;
  7779. ordered = btrfs_lookup_ordered_range(inode, start,
  7780. page_end - start + 1);
  7781. if (ordered) {
  7782. end = min(page_end, ordered->file_offset + ordered->len - 1);
  7783. /*
  7784. * IO on this page will never be started, so we need
  7785. * to account for any ordered extents now
  7786. */
  7787. if (!inode_evicting)
  7788. clear_extent_bit(tree, start, end,
  7789. EXTENT_DIRTY | EXTENT_DELALLOC |
  7790. EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
  7791. EXTENT_DEFRAG, 1, 0, &cached_state,
  7792. GFP_NOFS);
  7793. /*
  7794. * whoever cleared the private bit is responsible
  7795. * for the finish_ordered_io
  7796. */
  7797. if (TestClearPagePrivate2(page)) {
  7798. struct btrfs_ordered_inode_tree *tree;
  7799. u64 new_len;
  7800. tree = &BTRFS_I(inode)->ordered_tree;
  7801. spin_lock_irq(&tree->lock);
  7802. set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
  7803. new_len = start - ordered->file_offset;
  7804. if (new_len < ordered->truncated_len)
  7805. ordered->truncated_len = new_len;
  7806. spin_unlock_irq(&tree->lock);
  7807. if (btrfs_dec_test_ordered_pending(inode, &ordered,
  7808. start,
  7809. end - start + 1, 1))
  7810. btrfs_finish_ordered_io(ordered);
  7811. }
  7812. btrfs_put_ordered_extent(ordered);
  7813. if (!inode_evicting) {
  7814. cached_state = NULL;
  7815. lock_extent_bits(tree, start, end,
  7816. &cached_state);
  7817. }
  7818. start = end + 1;
  7819. if (start < page_end)
  7820. goto again;
  7821. }
  7822. /*
  7823. * Qgroup reserved space handler
  7824. * Page here will be either
  7825. * 1) Already written to disk
  7826. * In this case, its reserved space is released from data rsv map
  7827. * and will be freed by delayed_ref handler finally.
  7828. * So even we call qgroup_free_data(), it won't decrease reserved
  7829. * space.
  7830. * 2) Not written to disk
  7831. * This means the reserved space should be freed here. However,
  7832. * if a truncate invalidates the page (by clearing PageDirty)
  7833. * and the page is accounted for while allocating extent
  7834. * in btrfs_check_data_free_space() we let delayed_ref to
  7835. * free the entire extent.
  7836. */
  7837. if (PageDirty(page))
  7838. btrfs_qgroup_free_data(inode, page_start, PAGE_SIZE);
  7839. if (!inode_evicting) {
  7840. clear_extent_bit(tree, page_start, page_end,
  7841. EXTENT_LOCKED | EXTENT_DIRTY |
  7842. EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
  7843. EXTENT_DEFRAG, 1, 1,
  7844. &cached_state, GFP_NOFS);
  7845. __btrfs_releasepage(page, GFP_NOFS);
  7846. }
  7847. ClearPageChecked(page);
  7848. if (PagePrivate(page)) {
  7849. ClearPagePrivate(page);
  7850. set_page_private(page, 0);
  7851. put_page(page);
  7852. }
  7853. }
  7854. /*
  7855. * btrfs_page_mkwrite() is not allowed to change the file size as it gets
  7856. * called from a page fault handler when a page is first dirtied. Hence we must
  7857. * be careful to check for EOF conditions here. We set the page up correctly
  7858. * for a written page which means we get ENOSPC checking when writing into
  7859. * holes and correct delalloc and unwritten extent mapping on filesystems that
  7860. * support these features.
  7861. *
  7862. * We are not allowed to take the i_mutex here so we have to play games to
  7863. * protect against truncate races as the page could now be beyond EOF. Because
  7864. * vmtruncate() writes the inode size before removing pages, once we have the
  7865. * page lock we can determine safely if the page is beyond EOF. If it is not
  7866. * beyond EOF, then the page is guaranteed safe against truncation until we
  7867. * unlock the page.
  7868. */
  7869. int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
  7870. {
  7871. struct page *page = vmf->page;
  7872. struct inode *inode = file_inode(vma->vm_file);
  7873. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  7874. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  7875. struct btrfs_ordered_extent *ordered;
  7876. struct extent_state *cached_state = NULL;
  7877. char *kaddr;
  7878. unsigned long zero_start;
  7879. loff_t size;
  7880. int ret;
  7881. int reserved = 0;
  7882. u64 reserved_space;
  7883. u64 page_start;
  7884. u64 page_end;
  7885. u64 end;
  7886. reserved_space = PAGE_SIZE;
  7887. sb_start_pagefault(inode->i_sb);
  7888. page_start = page_offset(page);
  7889. page_end = page_start + PAGE_SIZE - 1;
  7890. end = page_end;
  7891. /*
  7892. * Reserving delalloc space after obtaining the page lock can lead to
  7893. * deadlock. For example, if a dirty page is locked by this function
  7894. * and the call to btrfs_delalloc_reserve_space() ends up triggering
  7895. * dirty page write out, then the btrfs_writepage() function could
  7896. * end up waiting indefinitely to get a lock on the page currently
  7897. * being processed by btrfs_page_mkwrite() function.
  7898. */
  7899. ret = btrfs_delalloc_reserve_space(inode, page_start,
  7900. reserved_space);
  7901. if (!ret) {
  7902. ret = file_update_time(vma->vm_file);
  7903. reserved = 1;
  7904. }
  7905. if (ret) {
  7906. if (ret == -ENOMEM)
  7907. ret = VM_FAULT_OOM;
  7908. else /* -ENOSPC, -EIO, etc */
  7909. ret = VM_FAULT_SIGBUS;
  7910. if (reserved)
  7911. goto out;
  7912. goto out_noreserve;
  7913. }
  7914. ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
  7915. again:
  7916. lock_page(page);
  7917. size = i_size_read(inode);
  7918. if ((page->mapping != inode->i_mapping) ||
  7919. (page_start >= size)) {
  7920. /* page got truncated out from underneath us */
  7921. goto out_unlock;
  7922. }
  7923. wait_on_page_writeback(page);
  7924. lock_extent_bits(io_tree, page_start, page_end, &cached_state);
  7925. set_page_extent_mapped(page);
  7926. /*
  7927. * we can't set the delalloc bits if there are pending ordered
  7928. * extents. Drop our locks and wait for them to finish
  7929. */
  7930. ordered = btrfs_lookup_ordered_range(inode, page_start, page_end);
  7931. if (ordered) {
  7932. unlock_extent_cached(io_tree, page_start, page_end,
  7933. &cached_state, GFP_NOFS);
  7934. unlock_page(page);
  7935. btrfs_start_ordered_extent(inode, ordered, 1);
  7936. btrfs_put_ordered_extent(ordered);
  7937. goto again;
  7938. }
  7939. if (page->index == ((size - 1) >> PAGE_SHIFT)) {
  7940. reserved_space = round_up(size - page_start,
  7941. fs_info->sectorsize);
  7942. if (reserved_space < PAGE_SIZE) {
  7943. end = page_start + reserved_space - 1;
  7944. spin_lock(&BTRFS_I(inode)->lock);
  7945. BTRFS_I(inode)->outstanding_extents++;
  7946. spin_unlock(&BTRFS_I(inode)->lock);
  7947. btrfs_delalloc_release_space(inode, page_start,
  7948. PAGE_SIZE - reserved_space);
  7949. }
  7950. }
  7951. /*
  7952. * XXX - page_mkwrite gets called every time the page is dirtied, even
  7953. * if it was already dirty, so for space accounting reasons we need to
  7954. * clear any delalloc bits for the range we are fixing to save. There
  7955. * is probably a better way to do this, but for now keep consistent with
  7956. * prepare_pages in the normal write path.
  7957. */
  7958. clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end,
  7959. EXTENT_DIRTY | EXTENT_DELALLOC |
  7960. EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
  7961. 0, 0, &cached_state, GFP_NOFS);
  7962. ret = btrfs_set_extent_delalloc(inode, page_start, end,
  7963. &cached_state, 0);
  7964. if (ret) {
  7965. unlock_extent_cached(io_tree, page_start, page_end,
  7966. &cached_state, GFP_NOFS);
  7967. ret = VM_FAULT_SIGBUS;
  7968. goto out_unlock;
  7969. }
  7970. ret = 0;
  7971. /* page is wholly or partially inside EOF */
  7972. if (page_start + PAGE_SIZE > size)
  7973. zero_start = size & ~PAGE_MASK;
  7974. else
  7975. zero_start = PAGE_SIZE;
  7976. if (zero_start != PAGE_SIZE) {
  7977. kaddr = kmap(page);
  7978. memset(kaddr + zero_start, 0, PAGE_SIZE - zero_start);
  7979. flush_dcache_page(page);
  7980. kunmap(page);
  7981. }
  7982. ClearPageChecked(page);
  7983. set_page_dirty(page);
  7984. SetPageUptodate(page);
  7985. BTRFS_I(inode)->last_trans = fs_info->generation;
  7986. BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
  7987. BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
  7988. unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
  7989. out_unlock:
  7990. if (!ret) {
  7991. sb_end_pagefault(inode->i_sb);
  7992. return VM_FAULT_LOCKED;
  7993. }
  7994. unlock_page(page);
  7995. out:
  7996. btrfs_delalloc_release_space(inode, page_start, reserved_space);
  7997. out_noreserve:
  7998. sb_end_pagefault(inode->i_sb);
  7999. return ret;
  8000. }
  8001. static int btrfs_truncate(struct inode *inode)
  8002. {
  8003. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  8004. struct btrfs_root *root = BTRFS_I(inode)->root;
  8005. struct btrfs_block_rsv *rsv;
  8006. int ret = 0;
  8007. int err = 0;
  8008. struct btrfs_trans_handle *trans;
  8009. u64 mask = fs_info->sectorsize - 1;
  8010. u64 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1);
  8011. ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
  8012. (u64)-1);
  8013. if (ret)
  8014. return ret;
  8015. /*
  8016. * Yes ladies and gentlemen, this is indeed ugly. The fact is we have
  8017. * 3 things going on here
  8018. *
  8019. * 1) We need to reserve space for our orphan item and the space to
  8020. * delete our orphan item. Lord knows we don't want to have a dangling
  8021. * orphan item because we didn't reserve space to remove it.
  8022. *
  8023. * 2) We need to reserve space to update our inode.
  8024. *
  8025. * 3) We need to have something to cache all the space that is going to
  8026. * be free'd up by the truncate operation, but also have some slack
  8027. * space reserved in case it uses space during the truncate (thank you
  8028. * very much snapshotting).
  8029. *
  8030. * And we need these to all be separate. The fact is we can use a lot of
  8031. * space doing the truncate, and we have no earthly idea how much space
  8032. * we will use, so we need the truncate reservation to be separate so it
  8033. * doesn't end up using space reserved for updating the inode or
  8034. * removing the orphan item. We also need to be able to stop the
  8035. * transaction and start a new one, which means we need to be able to
  8036. * update the inode several times, and we have no idea of knowing how
  8037. * many times that will be, so we can't just reserve 1 item for the
  8038. * entirety of the operation, so that has to be done separately as well.
  8039. * Then there is the orphan item, which does indeed need to be held on
  8040. * to for the whole operation, and we need nobody to touch this reserved
  8041. * space except the orphan code.
  8042. *
  8043. * So that leaves us with
  8044. *
  8045. * 1) root->orphan_block_rsv - for the orphan deletion.
  8046. * 2) rsv - for the truncate reservation, which we will steal from the
  8047. * transaction reservation.
  8048. * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
  8049. * updating the inode.
  8050. */
  8051. rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
  8052. if (!rsv)
  8053. return -ENOMEM;
  8054. rsv->size = min_size;
  8055. rsv->failfast = 1;
  8056. /*
  8057. * 1 for the truncate slack space
  8058. * 1 for updating the inode.
  8059. */
  8060. trans = btrfs_start_transaction(root, 2);
  8061. if (IS_ERR(trans)) {
  8062. err = PTR_ERR(trans);
  8063. goto out;
  8064. }
  8065. /* Migrate the slack space for the truncate to our reserve */
  8066. ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv,
  8067. min_size, 0);
  8068. BUG_ON(ret);
  8069. /*
  8070. * So if we truncate and then write and fsync we normally would just
  8071. * write the extents that changed, which is a problem if we need to
  8072. * first truncate that entire inode. So set this flag so we write out
  8073. * all of the extents in the inode to the sync log so we're completely
  8074. * safe.
  8075. */
  8076. set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
  8077. trans->block_rsv = rsv;
  8078. while (1) {
  8079. ret = btrfs_truncate_inode_items(trans, root, inode,
  8080. inode->i_size,
  8081. BTRFS_EXTENT_DATA_KEY);
  8082. if (ret != -ENOSPC && ret != -EAGAIN) {
  8083. err = ret;
  8084. break;
  8085. }
  8086. trans->block_rsv = &fs_info->trans_block_rsv;
  8087. ret = btrfs_update_inode(trans, root, inode);
  8088. if (ret) {
  8089. err = ret;
  8090. break;
  8091. }
  8092. btrfs_end_transaction(trans);
  8093. btrfs_btree_balance_dirty(fs_info);
  8094. trans = btrfs_start_transaction(root, 2);
  8095. if (IS_ERR(trans)) {
  8096. ret = err = PTR_ERR(trans);
  8097. trans = NULL;
  8098. break;
  8099. }
  8100. ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv,
  8101. rsv, min_size, 0);
  8102. BUG_ON(ret); /* shouldn't happen */
  8103. trans->block_rsv = rsv;
  8104. }
  8105. if (ret == 0 && inode->i_nlink > 0) {
  8106. trans->block_rsv = root->orphan_block_rsv;
  8107. ret = btrfs_orphan_del(trans, inode);
  8108. if (ret)
  8109. err = ret;
  8110. }
  8111. if (trans) {
  8112. trans->block_rsv = &fs_info->trans_block_rsv;
  8113. ret = btrfs_update_inode(trans, root, inode);
  8114. if (ret && !err)
  8115. err = ret;
  8116. ret = btrfs_end_transaction(trans);
  8117. btrfs_btree_balance_dirty(fs_info);
  8118. }
  8119. out:
  8120. btrfs_free_block_rsv(fs_info, rsv);
  8121. if (ret && !err)
  8122. err = ret;
  8123. return err;
  8124. }
  8125. /*
  8126. * create a new subvolume directory/inode (helper for the ioctl).
  8127. */
  8128. int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
  8129. struct btrfs_root *new_root,
  8130. struct btrfs_root *parent_root,
  8131. u64 new_dirid)
  8132. {
  8133. struct inode *inode;
  8134. int err;
  8135. u64 index = 0;
  8136. inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
  8137. new_dirid, new_dirid,
  8138. S_IFDIR | (~current_umask() & S_IRWXUGO),
  8139. &index);
  8140. if (IS_ERR(inode))
  8141. return PTR_ERR(inode);
  8142. inode->i_op = &btrfs_dir_inode_operations;
  8143. inode->i_fop = &btrfs_dir_file_operations;
  8144. set_nlink(inode, 1);
  8145. btrfs_i_size_write(inode, 0);
  8146. unlock_new_inode(inode);
  8147. err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
  8148. if (err)
  8149. btrfs_err(new_root->fs_info,
  8150. "error inheriting subvolume %llu properties: %d",
  8151. new_root->root_key.objectid, err);
  8152. err = btrfs_update_inode(trans, new_root, inode);
  8153. iput(inode);
  8154. return err;
  8155. }
  8156. struct inode *btrfs_alloc_inode(struct super_block *sb)
  8157. {
  8158. struct btrfs_inode *ei;
  8159. struct inode *inode;
  8160. ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
  8161. if (!ei)
  8162. return NULL;
  8163. ei->root = NULL;
  8164. ei->generation = 0;
  8165. ei->last_trans = 0;
  8166. ei->last_sub_trans = 0;
  8167. ei->logged_trans = 0;
  8168. ei->delalloc_bytes = 0;
  8169. ei->defrag_bytes = 0;
  8170. ei->disk_i_size = 0;
  8171. ei->flags = 0;
  8172. ei->csum_bytes = 0;
  8173. ei->index_cnt = (u64)-1;
  8174. ei->dir_index = 0;
  8175. ei->last_unlink_trans = 0;
  8176. ei->last_log_commit = 0;
  8177. ei->delayed_iput_count = 0;
  8178. spin_lock_init(&ei->lock);
  8179. ei->outstanding_extents = 0;
  8180. ei->reserved_extents = 0;
  8181. ei->runtime_flags = 0;
  8182. ei->force_compress = BTRFS_COMPRESS_NONE;
  8183. ei->delayed_node = NULL;
  8184. ei->i_otime.tv_sec = 0;
  8185. ei->i_otime.tv_nsec = 0;
  8186. inode = &ei->vfs_inode;
  8187. extent_map_tree_init(&ei->extent_tree);
  8188. extent_io_tree_init(&ei->io_tree, &inode->i_data);
  8189. extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
  8190. ei->io_tree.track_uptodate = 1;
  8191. ei->io_failure_tree.track_uptodate = 1;
  8192. atomic_set(&ei->sync_writers, 0);
  8193. mutex_init(&ei->log_mutex);
  8194. mutex_init(&ei->delalloc_mutex);
  8195. btrfs_ordered_inode_tree_init(&ei->ordered_tree);
  8196. INIT_LIST_HEAD(&ei->delalloc_inodes);
  8197. INIT_LIST_HEAD(&ei->delayed_iput);
  8198. RB_CLEAR_NODE(&ei->rb_node);
  8199. init_rwsem(&ei->dio_sem);
  8200. return inode;
  8201. }
  8202. #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
  8203. void btrfs_test_destroy_inode(struct inode *inode)
  8204. {
  8205. btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
  8206. kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
  8207. }
  8208. #endif
  8209. static void btrfs_i_callback(struct rcu_head *head)
  8210. {
  8211. struct inode *inode = container_of(head, struct inode, i_rcu);
  8212. kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
  8213. }
  8214. void btrfs_destroy_inode(struct inode *inode)
  8215. {
  8216. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  8217. struct btrfs_ordered_extent *ordered;
  8218. struct btrfs_root *root = BTRFS_I(inode)->root;
  8219. WARN_ON(!hlist_empty(&inode->i_dentry));
  8220. WARN_ON(inode->i_data.nrpages);
  8221. WARN_ON(BTRFS_I(inode)->outstanding_extents);
  8222. WARN_ON(BTRFS_I(inode)->reserved_extents);
  8223. WARN_ON(BTRFS_I(inode)->delalloc_bytes);
  8224. WARN_ON(BTRFS_I(inode)->csum_bytes);
  8225. WARN_ON(BTRFS_I(inode)->defrag_bytes);
  8226. /*
  8227. * This can happen where we create an inode, but somebody else also
  8228. * created the same inode and we need to destroy the one we already
  8229. * created.
  8230. */
  8231. if (!root)
  8232. goto free;
  8233. if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
  8234. &BTRFS_I(inode)->runtime_flags)) {
  8235. btrfs_info(fs_info, "inode %llu still on the orphan list",
  8236. btrfs_ino(inode));
  8237. atomic_dec(&root->orphan_inodes);
  8238. }
  8239. while (1) {
  8240. ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
  8241. if (!ordered)
  8242. break;
  8243. else {
  8244. btrfs_err(fs_info,
  8245. "found ordered extent %llu %llu on inode cleanup",
  8246. ordered->file_offset, ordered->len);
  8247. btrfs_remove_ordered_extent(inode, ordered);
  8248. btrfs_put_ordered_extent(ordered);
  8249. btrfs_put_ordered_extent(ordered);
  8250. }
  8251. }
  8252. btrfs_qgroup_check_reserved_leak(inode);
  8253. inode_tree_del(inode);
  8254. btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
  8255. free:
  8256. call_rcu(&inode->i_rcu, btrfs_i_callback);
  8257. }
  8258. int btrfs_drop_inode(struct inode *inode)
  8259. {
  8260. struct btrfs_root *root = BTRFS_I(inode)->root;
  8261. if (root == NULL)
  8262. return 1;
  8263. /* the snap/subvol tree is on deleting */
  8264. if (btrfs_root_refs(&root->root_item) == 0)
  8265. return 1;
  8266. else
  8267. return generic_drop_inode(inode);
  8268. }
  8269. static void init_once(void *foo)
  8270. {
  8271. struct btrfs_inode *ei = (struct btrfs_inode *) foo;
  8272. inode_init_once(&ei->vfs_inode);
  8273. }
  8274. void btrfs_destroy_cachep(void)
  8275. {
  8276. /*
  8277. * Make sure all delayed rcu free inodes are flushed before we
  8278. * destroy cache.
  8279. */
  8280. rcu_barrier();
  8281. kmem_cache_destroy(btrfs_inode_cachep);
  8282. kmem_cache_destroy(btrfs_trans_handle_cachep);
  8283. kmem_cache_destroy(btrfs_transaction_cachep);
  8284. kmem_cache_destroy(btrfs_path_cachep);
  8285. kmem_cache_destroy(btrfs_free_space_cachep);
  8286. }
  8287. int btrfs_init_cachep(void)
  8288. {
  8289. btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
  8290. sizeof(struct btrfs_inode), 0,
  8291. SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT,
  8292. init_once);
  8293. if (!btrfs_inode_cachep)
  8294. goto fail;
  8295. btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
  8296. sizeof(struct btrfs_trans_handle), 0,
  8297. SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
  8298. if (!btrfs_trans_handle_cachep)
  8299. goto fail;
  8300. btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction",
  8301. sizeof(struct btrfs_transaction), 0,
  8302. SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
  8303. if (!btrfs_transaction_cachep)
  8304. goto fail;
  8305. btrfs_path_cachep = kmem_cache_create("btrfs_path",
  8306. sizeof(struct btrfs_path), 0,
  8307. SLAB_MEM_SPREAD, NULL);
  8308. if (!btrfs_path_cachep)
  8309. goto fail;
  8310. btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
  8311. sizeof(struct btrfs_free_space), 0,
  8312. SLAB_MEM_SPREAD, NULL);
  8313. if (!btrfs_free_space_cachep)
  8314. goto fail;
  8315. return 0;
  8316. fail:
  8317. btrfs_destroy_cachep();
  8318. return -ENOMEM;
  8319. }
  8320. static int btrfs_getattr(struct vfsmount *mnt,
  8321. struct dentry *dentry, struct kstat *stat)
  8322. {
  8323. u64 delalloc_bytes;
  8324. struct inode *inode = d_inode(dentry);
  8325. u32 blocksize = inode->i_sb->s_blocksize;
  8326. generic_fillattr(inode, stat);
  8327. stat->dev = BTRFS_I(inode)->root->anon_dev;
  8328. spin_lock(&BTRFS_I(inode)->lock);
  8329. delalloc_bytes = BTRFS_I(inode)->delalloc_bytes;
  8330. spin_unlock(&BTRFS_I(inode)->lock);
  8331. stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
  8332. ALIGN(delalloc_bytes, blocksize)) >> 9;
  8333. return 0;
  8334. }
  8335. static int btrfs_rename_exchange(struct inode *old_dir,
  8336. struct dentry *old_dentry,
  8337. struct inode *new_dir,
  8338. struct dentry *new_dentry)
  8339. {
  8340. struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
  8341. struct btrfs_trans_handle *trans;
  8342. struct btrfs_root *root = BTRFS_I(old_dir)->root;
  8343. struct btrfs_root *dest = BTRFS_I(new_dir)->root;
  8344. struct inode *new_inode = new_dentry->d_inode;
  8345. struct inode *old_inode = old_dentry->d_inode;
  8346. struct timespec ctime = current_time(old_inode);
  8347. struct dentry *parent;
  8348. u64 old_ino = btrfs_ino(old_inode);
  8349. u64 new_ino = btrfs_ino(new_inode);
  8350. u64 old_idx = 0;
  8351. u64 new_idx = 0;
  8352. u64 root_objectid;
  8353. int ret;
  8354. bool root_log_pinned = false;
  8355. bool dest_log_pinned = false;
  8356. /* we only allow rename subvolume link between subvolumes */
  8357. if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
  8358. return -EXDEV;
  8359. /* close the race window with snapshot create/destroy ioctl */
  8360. if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
  8361. down_read(&fs_info->subvol_sem);
  8362. if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
  8363. down_read(&fs_info->subvol_sem);
  8364. /*
  8365. * We want to reserve the absolute worst case amount of items. So if
  8366. * both inodes are subvols and we need to unlink them then that would
  8367. * require 4 item modifications, but if they are both normal inodes it
  8368. * would require 5 item modifications, so we'll assume their normal
  8369. * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items
  8370. * should cover the worst case number of items we'll modify.
  8371. */
  8372. trans = btrfs_start_transaction(root, 12);
  8373. if (IS_ERR(trans)) {
  8374. ret = PTR_ERR(trans);
  8375. goto out_notrans;
  8376. }
  8377. /*
  8378. * We need to find a free sequence number both in the source and
  8379. * in the destination directory for the exchange.
  8380. */
  8381. ret = btrfs_set_inode_index(new_dir, &old_idx);
  8382. if (ret)
  8383. goto out_fail;
  8384. ret = btrfs_set_inode_index(old_dir, &new_idx);
  8385. if (ret)
  8386. goto out_fail;
  8387. BTRFS_I(old_inode)->dir_index = 0ULL;
  8388. BTRFS_I(new_inode)->dir_index = 0ULL;
  8389. /* Reference for the source. */
  8390. if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
  8391. /* force full log commit if subvolume involved. */
  8392. btrfs_set_log_full_commit(fs_info, trans);
  8393. } else {
  8394. btrfs_pin_log_trans(root);
  8395. root_log_pinned = true;
  8396. ret = btrfs_insert_inode_ref(trans, dest,
  8397. new_dentry->d_name.name,
  8398. new_dentry->d_name.len,
  8399. old_ino,
  8400. btrfs_ino(new_dir), old_idx);
  8401. if (ret)
  8402. goto out_fail;
  8403. }
  8404. /* And now for the dest. */
  8405. if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
  8406. /* force full log commit if subvolume involved. */
  8407. btrfs_set_log_full_commit(fs_info, trans);
  8408. } else {
  8409. btrfs_pin_log_trans(dest);
  8410. dest_log_pinned = true;
  8411. ret = btrfs_insert_inode_ref(trans, root,
  8412. old_dentry->d_name.name,
  8413. old_dentry->d_name.len,
  8414. new_ino,
  8415. btrfs_ino(old_dir), new_idx);
  8416. if (ret)
  8417. goto out_fail;
  8418. }
  8419. /* Update inode version and ctime/mtime. */
  8420. inode_inc_iversion(old_dir);
  8421. inode_inc_iversion(new_dir);
  8422. inode_inc_iversion(old_inode);
  8423. inode_inc_iversion(new_inode);
  8424. old_dir->i_ctime = old_dir->i_mtime = ctime;
  8425. new_dir->i_ctime = new_dir->i_mtime = ctime;
  8426. old_inode->i_ctime = ctime;
  8427. new_inode->i_ctime = ctime;
  8428. if (old_dentry->d_parent != new_dentry->d_parent) {
  8429. btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
  8430. btrfs_record_unlink_dir(trans, new_dir, new_inode, 1);
  8431. }
  8432. /* src is a subvolume */
  8433. if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
  8434. root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
  8435. ret = btrfs_unlink_subvol(trans, root, old_dir,
  8436. root_objectid,
  8437. old_dentry->d_name.name,
  8438. old_dentry->d_name.len);
  8439. } else { /* src is an inode */
  8440. ret = __btrfs_unlink_inode(trans, root, old_dir,
  8441. old_dentry->d_inode,
  8442. old_dentry->d_name.name,
  8443. old_dentry->d_name.len);
  8444. if (!ret)
  8445. ret = btrfs_update_inode(trans, root, old_inode);
  8446. }
  8447. if (ret) {
  8448. btrfs_abort_transaction(trans, ret);
  8449. goto out_fail;
  8450. }
  8451. /* dest is a subvolume */
  8452. if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
  8453. root_objectid = BTRFS_I(new_inode)->root->root_key.objectid;
  8454. ret = btrfs_unlink_subvol(trans, dest, new_dir,
  8455. root_objectid,
  8456. new_dentry->d_name.name,
  8457. new_dentry->d_name.len);
  8458. } else { /* dest is an inode */
  8459. ret = __btrfs_unlink_inode(trans, dest, new_dir,
  8460. new_dentry->d_inode,
  8461. new_dentry->d_name.name,
  8462. new_dentry->d_name.len);
  8463. if (!ret)
  8464. ret = btrfs_update_inode(trans, dest, new_inode);
  8465. }
  8466. if (ret) {
  8467. btrfs_abort_transaction(trans, ret);
  8468. goto out_fail;
  8469. }
  8470. ret = btrfs_add_link(trans, new_dir, old_inode,
  8471. new_dentry->d_name.name,
  8472. new_dentry->d_name.len, 0, old_idx);
  8473. if (ret) {
  8474. btrfs_abort_transaction(trans, ret);
  8475. goto out_fail;
  8476. }
  8477. ret = btrfs_add_link(trans, old_dir, new_inode,
  8478. old_dentry->d_name.name,
  8479. old_dentry->d_name.len, 0, new_idx);
  8480. if (ret) {
  8481. btrfs_abort_transaction(trans, ret);
  8482. goto out_fail;
  8483. }
  8484. if (old_inode->i_nlink == 1)
  8485. BTRFS_I(old_inode)->dir_index = old_idx;
  8486. if (new_inode->i_nlink == 1)
  8487. BTRFS_I(new_inode)->dir_index = new_idx;
  8488. if (root_log_pinned) {
  8489. parent = new_dentry->d_parent;
  8490. btrfs_log_new_name(trans, old_inode, old_dir, parent);
  8491. btrfs_end_log_trans(root);
  8492. root_log_pinned = false;
  8493. }
  8494. if (dest_log_pinned) {
  8495. parent = old_dentry->d_parent;
  8496. btrfs_log_new_name(trans, new_inode, new_dir, parent);
  8497. btrfs_end_log_trans(dest);
  8498. dest_log_pinned = false;
  8499. }
  8500. out_fail:
  8501. /*
  8502. * If we have pinned a log and an error happened, we unpin tasks
  8503. * trying to sync the log and force them to fallback to a transaction
  8504. * commit if the log currently contains any of the inodes involved in
  8505. * this rename operation (to ensure we do not persist a log with an
  8506. * inconsistent state for any of these inodes or leading to any
  8507. * inconsistencies when replayed). If the transaction was aborted, the
  8508. * abortion reason is propagated to userspace when attempting to commit
  8509. * the transaction. If the log does not contain any of these inodes, we
  8510. * allow the tasks to sync it.
  8511. */
  8512. if (ret && (root_log_pinned || dest_log_pinned)) {
  8513. if (btrfs_inode_in_log(old_dir, fs_info->generation) ||
  8514. btrfs_inode_in_log(new_dir, fs_info->generation) ||
  8515. btrfs_inode_in_log(old_inode, fs_info->generation) ||
  8516. (new_inode &&
  8517. btrfs_inode_in_log(new_inode, fs_info->generation)))
  8518. btrfs_set_log_full_commit(fs_info, trans);
  8519. if (root_log_pinned) {
  8520. btrfs_end_log_trans(root);
  8521. root_log_pinned = false;
  8522. }
  8523. if (dest_log_pinned) {
  8524. btrfs_end_log_trans(dest);
  8525. dest_log_pinned = false;
  8526. }
  8527. }
  8528. ret = btrfs_end_transaction(trans);
  8529. out_notrans:
  8530. if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
  8531. up_read(&fs_info->subvol_sem);
  8532. if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
  8533. up_read(&fs_info->subvol_sem);
  8534. return ret;
  8535. }
  8536. static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans,
  8537. struct btrfs_root *root,
  8538. struct inode *dir,
  8539. struct dentry *dentry)
  8540. {
  8541. int ret;
  8542. struct inode *inode;
  8543. u64 objectid;
  8544. u64 index;
  8545. ret = btrfs_find_free_ino(root, &objectid);
  8546. if (ret)
  8547. return ret;
  8548. inode = btrfs_new_inode(trans, root, dir,
  8549. dentry->d_name.name,
  8550. dentry->d_name.len,
  8551. btrfs_ino(dir),
  8552. objectid,
  8553. S_IFCHR | WHITEOUT_MODE,
  8554. &index);
  8555. if (IS_ERR(inode)) {
  8556. ret = PTR_ERR(inode);
  8557. return ret;
  8558. }
  8559. inode->i_op = &btrfs_special_inode_operations;
  8560. init_special_inode(inode, inode->i_mode,
  8561. WHITEOUT_DEV);
  8562. ret = btrfs_init_inode_security(trans, inode, dir,
  8563. &dentry->d_name);
  8564. if (ret)
  8565. goto out;
  8566. ret = btrfs_add_nondir(trans, dir, dentry,
  8567. inode, 0, index);
  8568. if (ret)
  8569. goto out;
  8570. ret = btrfs_update_inode(trans, root, inode);
  8571. out:
  8572. unlock_new_inode(inode);
  8573. if (ret)
  8574. inode_dec_link_count(inode);
  8575. iput(inode);
  8576. return ret;
  8577. }
  8578. static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
  8579. struct inode *new_dir, struct dentry *new_dentry,
  8580. unsigned int flags)
  8581. {
  8582. struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
  8583. struct btrfs_trans_handle *trans;
  8584. unsigned int trans_num_items;
  8585. struct btrfs_root *root = BTRFS_I(old_dir)->root;
  8586. struct btrfs_root *dest = BTRFS_I(new_dir)->root;
  8587. struct inode *new_inode = d_inode(new_dentry);
  8588. struct inode *old_inode = d_inode(old_dentry);
  8589. u64 index = 0;
  8590. u64 root_objectid;
  8591. int ret;
  8592. u64 old_ino = btrfs_ino(old_inode);
  8593. bool log_pinned = false;
  8594. if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
  8595. return -EPERM;
  8596. /* we only allow rename subvolume link between subvolumes */
  8597. if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
  8598. return -EXDEV;
  8599. if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
  8600. (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID))
  8601. return -ENOTEMPTY;
  8602. if (S_ISDIR(old_inode->i_mode) && new_inode &&
  8603. new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
  8604. return -ENOTEMPTY;
  8605. /* check for collisions, even if the name isn't there */
  8606. ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
  8607. new_dentry->d_name.name,
  8608. new_dentry->d_name.len);
  8609. if (ret) {
  8610. if (ret == -EEXIST) {
  8611. /* we shouldn't get
  8612. * eexist without a new_inode */
  8613. if (WARN_ON(!new_inode)) {
  8614. return ret;
  8615. }
  8616. } else {
  8617. /* maybe -EOVERFLOW */
  8618. return ret;
  8619. }
  8620. }
  8621. ret = 0;
  8622. /*
  8623. * we're using rename to replace one file with another. Start IO on it
  8624. * now so we don't add too much work to the end of the transaction
  8625. */
  8626. if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
  8627. filemap_flush(old_inode->i_mapping);
  8628. /* close the racy window with snapshot create/destroy ioctl */
  8629. if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
  8630. down_read(&fs_info->subvol_sem);
  8631. /*
  8632. * We want to reserve the absolute worst case amount of items. So if
  8633. * both inodes are subvols and we need to unlink them then that would
  8634. * require 4 item modifications, but if they are both normal inodes it
  8635. * would require 5 item modifications, so we'll assume they are normal
  8636. * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
  8637. * should cover the worst case number of items we'll modify.
  8638. * If our rename has the whiteout flag, we need more 5 units for the
  8639. * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item
  8640. * when selinux is enabled).
  8641. */
  8642. trans_num_items = 11;
  8643. if (flags & RENAME_WHITEOUT)
  8644. trans_num_items += 5;
  8645. trans = btrfs_start_transaction(root, trans_num_items);
  8646. if (IS_ERR(trans)) {
  8647. ret = PTR_ERR(trans);
  8648. goto out_notrans;
  8649. }
  8650. if (dest != root)
  8651. btrfs_record_root_in_trans(trans, dest);
  8652. ret = btrfs_set_inode_index(new_dir, &index);
  8653. if (ret)
  8654. goto out_fail;
  8655. BTRFS_I(old_inode)->dir_index = 0ULL;
  8656. if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
  8657. /* force full log commit if subvolume involved. */
  8658. btrfs_set_log_full_commit(fs_info, trans);
  8659. } else {
  8660. btrfs_pin_log_trans(root);
  8661. log_pinned = true;
  8662. ret = btrfs_insert_inode_ref(trans, dest,
  8663. new_dentry->d_name.name,
  8664. new_dentry->d_name.len,
  8665. old_ino,
  8666. btrfs_ino(new_dir), index);
  8667. if (ret)
  8668. goto out_fail;
  8669. }
  8670. inode_inc_iversion(old_dir);
  8671. inode_inc_iversion(new_dir);
  8672. inode_inc_iversion(old_inode);
  8673. old_dir->i_ctime = old_dir->i_mtime =
  8674. new_dir->i_ctime = new_dir->i_mtime =
  8675. old_inode->i_ctime = current_time(old_dir);
  8676. if (old_dentry->d_parent != new_dentry->d_parent)
  8677. btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
  8678. if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
  8679. root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
  8680. ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
  8681. old_dentry->d_name.name,
  8682. old_dentry->d_name.len);
  8683. } else {
  8684. ret = __btrfs_unlink_inode(trans, root, old_dir,
  8685. d_inode(old_dentry),
  8686. old_dentry->d_name.name,
  8687. old_dentry->d_name.len);
  8688. if (!ret)
  8689. ret = btrfs_update_inode(trans, root, old_inode);
  8690. }
  8691. if (ret) {
  8692. btrfs_abort_transaction(trans, ret);
  8693. goto out_fail;
  8694. }
  8695. if (new_inode) {
  8696. inode_inc_iversion(new_inode);
  8697. new_inode->i_ctime = current_time(new_inode);
  8698. if (unlikely(btrfs_ino(new_inode) ==
  8699. BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
  8700. root_objectid = BTRFS_I(new_inode)->location.objectid;
  8701. ret = btrfs_unlink_subvol(trans, dest, new_dir,
  8702. root_objectid,
  8703. new_dentry->d_name.name,
  8704. new_dentry->d_name.len);
  8705. BUG_ON(new_inode->i_nlink == 0);
  8706. } else {
  8707. ret = btrfs_unlink_inode(trans, dest, new_dir,
  8708. d_inode(new_dentry),
  8709. new_dentry->d_name.name,
  8710. new_dentry->d_name.len);
  8711. }
  8712. if (!ret && new_inode->i_nlink == 0)
  8713. ret = btrfs_orphan_add(trans, d_inode(new_dentry));
  8714. if (ret) {
  8715. btrfs_abort_transaction(trans, ret);
  8716. goto out_fail;
  8717. }
  8718. }
  8719. ret = btrfs_add_link(trans, new_dir, old_inode,
  8720. new_dentry->d_name.name,
  8721. new_dentry->d_name.len, 0, index);
  8722. if (ret) {
  8723. btrfs_abort_transaction(trans, ret);
  8724. goto out_fail;
  8725. }
  8726. if (old_inode->i_nlink == 1)
  8727. BTRFS_I(old_inode)->dir_index = index;
  8728. if (log_pinned) {
  8729. struct dentry *parent = new_dentry->d_parent;
  8730. btrfs_log_new_name(trans, old_inode, old_dir, parent);
  8731. btrfs_end_log_trans(root);
  8732. log_pinned = false;
  8733. }
  8734. if (flags & RENAME_WHITEOUT) {
  8735. ret = btrfs_whiteout_for_rename(trans, root, old_dir,
  8736. old_dentry);
  8737. if (ret) {
  8738. btrfs_abort_transaction(trans, ret);
  8739. goto out_fail;
  8740. }
  8741. }
  8742. out_fail:
  8743. /*
  8744. * If we have pinned the log and an error happened, we unpin tasks
  8745. * trying to sync the log and force them to fallback to a transaction
  8746. * commit if the log currently contains any of the inodes involved in
  8747. * this rename operation (to ensure we do not persist a log with an
  8748. * inconsistent state for any of these inodes or leading to any
  8749. * inconsistencies when replayed). If the transaction was aborted, the
  8750. * abortion reason is propagated to userspace when attempting to commit
  8751. * the transaction. If the log does not contain any of these inodes, we
  8752. * allow the tasks to sync it.
  8753. */
  8754. if (ret && log_pinned) {
  8755. if (btrfs_inode_in_log(old_dir, fs_info->generation) ||
  8756. btrfs_inode_in_log(new_dir, fs_info->generation) ||
  8757. btrfs_inode_in_log(old_inode, fs_info->generation) ||
  8758. (new_inode &&
  8759. btrfs_inode_in_log(new_inode, fs_info->generation)))
  8760. btrfs_set_log_full_commit(fs_info, trans);
  8761. btrfs_end_log_trans(root);
  8762. log_pinned = false;
  8763. }
  8764. btrfs_end_transaction(trans);
  8765. out_notrans:
  8766. if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
  8767. up_read(&fs_info->subvol_sem);
  8768. return ret;
  8769. }
  8770. static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
  8771. struct inode *new_dir, struct dentry *new_dentry,
  8772. unsigned int flags)
  8773. {
  8774. if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
  8775. return -EINVAL;
  8776. if (flags & RENAME_EXCHANGE)
  8777. return btrfs_rename_exchange(old_dir, old_dentry, new_dir,
  8778. new_dentry);
  8779. return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
  8780. }
  8781. static void btrfs_run_delalloc_work(struct btrfs_work *work)
  8782. {
  8783. struct btrfs_delalloc_work *delalloc_work;
  8784. struct inode *inode;
  8785. delalloc_work = container_of(work, struct btrfs_delalloc_work,
  8786. work);
  8787. inode = delalloc_work->inode;
  8788. filemap_flush(inode->i_mapping);
  8789. if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
  8790. &BTRFS_I(inode)->runtime_flags))
  8791. filemap_flush(inode->i_mapping);
  8792. if (delalloc_work->delay_iput)
  8793. btrfs_add_delayed_iput(inode);
  8794. else
  8795. iput(inode);
  8796. complete(&delalloc_work->completion);
  8797. }
  8798. struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
  8799. int delay_iput)
  8800. {
  8801. struct btrfs_delalloc_work *work;
  8802. work = kmalloc(sizeof(*work), GFP_NOFS);
  8803. if (!work)
  8804. return NULL;
  8805. init_completion(&work->completion);
  8806. INIT_LIST_HEAD(&work->list);
  8807. work->inode = inode;
  8808. work->delay_iput = delay_iput;
  8809. WARN_ON_ONCE(!inode);
  8810. btrfs_init_work(&work->work, btrfs_flush_delalloc_helper,
  8811. btrfs_run_delalloc_work, NULL, NULL);
  8812. return work;
  8813. }
  8814. void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
  8815. {
  8816. wait_for_completion(&work->completion);
  8817. kfree(work);
  8818. }
  8819. /*
  8820. * some fairly slow code that needs optimization. This walks the list
  8821. * of all the inodes with pending delalloc and forces them to disk.
  8822. */
  8823. static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput,
  8824. int nr)
  8825. {
  8826. struct btrfs_inode *binode;
  8827. struct inode *inode;
  8828. struct btrfs_delalloc_work *work, *next;
  8829. struct list_head works;
  8830. struct list_head splice;
  8831. int ret = 0;
  8832. INIT_LIST_HEAD(&works);
  8833. INIT_LIST_HEAD(&splice);
  8834. mutex_lock(&root->delalloc_mutex);
  8835. spin_lock(&root->delalloc_lock);
  8836. list_splice_init(&root->delalloc_inodes, &splice);
  8837. while (!list_empty(&splice)) {
  8838. binode = list_entry(splice.next, struct btrfs_inode,
  8839. delalloc_inodes);
  8840. list_move_tail(&binode->delalloc_inodes,
  8841. &root->delalloc_inodes);
  8842. inode = igrab(&binode->vfs_inode);
  8843. if (!inode) {
  8844. cond_resched_lock(&root->delalloc_lock);
  8845. continue;
  8846. }
  8847. spin_unlock(&root->delalloc_lock);
  8848. work = btrfs_alloc_delalloc_work(inode, delay_iput);
  8849. if (!work) {
  8850. if (delay_iput)
  8851. btrfs_add_delayed_iput(inode);
  8852. else
  8853. iput(inode);
  8854. ret = -ENOMEM;
  8855. goto out;
  8856. }
  8857. list_add_tail(&work->list, &works);
  8858. btrfs_queue_work(root->fs_info->flush_workers,
  8859. &work->work);
  8860. ret++;
  8861. if (nr != -1 && ret >= nr)
  8862. goto out;
  8863. cond_resched();
  8864. spin_lock(&root->delalloc_lock);
  8865. }
  8866. spin_unlock(&root->delalloc_lock);
  8867. out:
  8868. list_for_each_entry_safe(work, next, &works, list) {
  8869. list_del_init(&work->list);
  8870. btrfs_wait_and_free_delalloc_work(work);
  8871. }
  8872. if (!list_empty_careful(&splice)) {
  8873. spin_lock(&root->delalloc_lock);
  8874. list_splice_tail(&splice, &root->delalloc_inodes);
  8875. spin_unlock(&root->delalloc_lock);
  8876. }
  8877. mutex_unlock(&root->delalloc_mutex);
  8878. return ret;
  8879. }
  8880. int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
  8881. {
  8882. struct btrfs_fs_info *fs_info = root->fs_info;
  8883. int ret;
  8884. if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
  8885. return -EROFS;
  8886. ret = __start_delalloc_inodes(root, delay_iput, -1);
  8887. if (ret > 0)
  8888. ret = 0;
  8889. /*
  8890. * the filemap_flush will queue IO into the worker threads, but
  8891. * we have to make sure the IO is actually started and that
  8892. * ordered extents get created before we return
  8893. */
  8894. atomic_inc(&fs_info->async_submit_draining);
  8895. while (atomic_read(&fs_info->nr_async_submits) ||
  8896. atomic_read(&fs_info->async_delalloc_pages)) {
  8897. wait_event(fs_info->async_submit_wait,
  8898. (atomic_read(&fs_info->nr_async_submits) == 0 &&
  8899. atomic_read(&fs_info->async_delalloc_pages) == 0));
  8900. }
  8901. atomic_dec(&fs_info->async_submit_draining);
  8902. return ret;
  8903. }
  8904. int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int delay_iput,
  8905. int nr)
  8906. {
  8907. struct btrfs_root *root;
  8908. struct list_head splice;
  8909. int ret;
  8910. if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
  8911. return -EROFS;
  8912. INIT_LIST_HEAD(&splice);
  8913. mutex_lock(&fs_info->delalloc_root_mutex);
  8914. spin_lock(&fs_info->delalloc_root_lock);
  8915. list_splice_init(&fs_info->delalloc_roots, &splice);
  8916. while (!list_empty(&splice) && nr) {
  8917. root = list_first_entry(&splice, struct btrfs_root,
  8918. delalloc_root);
  8919. root = btrfs_grab_fs_root(root);
  8920. BUG_ON(!root);
  8921. list_move_tail(&root->delalloc_root,
  8922. &fs_info->delalloc_roots);
  8923. spin_unlock(&fs_info->delalloc_root_lock);
  8924. ret = __start_delalloc_inodes(root, delay_iput, nr);
  8925. btrfs_put_fs_root(root);
  8926. if (ret < 0)
  8927. goto out;
  8928. if (nr != -1) {
  8929. nr -= ret;
  8930. WARN_ON(nr < 0);
  8931. }
  8932. spin_lock(&fs_info->delalloc_root_lock);
  8933. }
  8934. spin_unlock(&fs_info->delalloc_root_lock);
  8935. ret = 0;
  8936. atomic_inc(&fs_info->async_submit_draining);
  8937. while (atomic_read(&fs_info->nr_async_submits) ||
  8938. atomic_read(&fs_info->async_delalloc_pages)) {
  8939. wait_event(fs_info->async_submit_wait,
  8940. (atomic_read(&fs_info->nr_async_submits) == 0 &&
  8941. atomic_read(&fs_info->async_delalloc_pages) == 0));
  8942. }
  8943. atomic_dec(&fs_info->async_submit_draining);
  8944. out:
  8945. if (!list_empty_careful(&splice)) {
  8946. spin_lock(&fs_info->delalloc_root_lock);
  8947. list_splice_tail(&splice, &fs_info->delalloc_roots);
  8948. spin_unlock(&fs_info->delalloc_root_lock);
  8949. }
  8950. mutex_unlock(&fs_info->delalloc_root_mutex);
  8951. return ret;
  8952. }
  8953. static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
  8954. const char *symname)
  8955. {
  8956. struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
  8957. struct btrfs_trans_handle *trans;
  8958. struct btrfs_root *root = BTRFS_I(dir)->root;
  8959. struct btrfs_path *path;
  8960. struct btrfs_key key;
  8961. struct inode *inode = NULL;
  8962. int err;
  8963. int drop_inode = 0;
  8964. u64 objectid;
  8965. u64 index = 0;
  8966. int name_len;
  8967. int datasize;
  8968. unsigned long ptr;
  8969. struct btrfs_file_extent_item *ei;
  8970. struct extent_buffer *leaf;
  8971. name_len = strlen(symname);
  8972. if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info))
  8973. return -ENAMETOOLONG;
  8974. /*
  8975. * 2 items for inode item and ref
  8976. * 2 items for dir items
  8977. * 1 item for updating parent inode item
  8978. * 1 item for the inline extent item
  8979. * 1 item for xattr if selinux is on
  8980. */
  8981. trans = btrfs_start_transaction(root, 7);
  8982. if (IS_ERR(trans))
  8983. return PTR_ERR(trans);
  8984. err = btrfs_find_free_ino(root, &objectid);
  8985. if (err)
  8986. goto out_unlock;
  8987. inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
  8988. dentry->d_name.len, btrfs_ino(dir), objectid,
  8989. S_IFLNK|S_IRWXUGO, &index);
  8990. if (IS_ERR(inode)) {
  8991. err = PTR_ERR(inode);
  8992. goto out_unlock;
  8993. }
  8994. /*
  8995. * If the active LSM wants to access the inode during
  8996. * d_instantiate it needs these. Smack checks to see
  8997. * if the filesystem supports xattrs by looking at the
  8998. * ops vector.
  8999. */
  9000. inode->i_fop = &btrfs_file_operations;
  9001. inode->i_op = &btrfs_file_inode_operations;
  9002. inode->i_mapping->a_ops = &btrfs_aops;
  9003. BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
  9004. err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
  9005. if (err)
  9006. goto out_unlock_inode;
  9007. path = btrfs_alloc_path();
  9008. if (!path) {
  9009. err = -ENOMEM;
  9010. goto out_unlock_inode;
  9011. }
  9012. key.objectid = btrfs_ino(inode);
  9013. key.offset = 0;
  9014. key.type = BTRFS_EXTENT_DATA_KEY;
  9015. datasize = btrfs_file_extent_calc_inline_size(name_len);
  9016. err = btrfs_insert_empty_item(trans, root, path, &key,
  9017. datasize);
  9018. if (err) {
  9019. btrfs_free_path(path);
  9020. goto out_unlock_inode;
  9021. }
  9022. leaf = path->nodes[0];
  9023. ei = btrfs_item_ptr(leaf, path->slots[0],
  9024. struct btrfs_file_extent_item);
  9025. btrfs_set_file_extent_generation(leaf, ei, trans->transid);
  9026. btrfs_set_file_extent_type(leaf, ei,
  9027. BTRFS_FILE_EXTENT_INLINE);
  9028. btrfs_set_file_extent_encryption(leaf, ei, 0);
  9029. btrfs_set_file_extent_compression(leaf, ei, 0);
  9030. btrfs_set_file_extent_other_encoding(leaf, ei, 0);
  9031. btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
  9032. ptr = btrfs_file_extent_inline_start(ei);
  9033. write_extent_buffer(leaf, symname, ptr, name_len);
  9034. btrfs_mark_buffer_dirty(leaf);
  9035. btrfs_free_path(path);
  9036. inode->i_op = &btrfs_symlink_inode_operations;
  9037. inode_nohighmem(inode);
  9038. inode->i_mapping->a_ops = &btrfs_symlink_aops;
  9039. inode_set_bytes(inode, name_len);
  9040. btrfs_i_size_write(inode, name_len);
  9041. err = btrfs_update_inode(trans, root, inode);
  9042. /*
  9043. * Last step, add directory indexes for our symlink inode. This is the
  9044. * last step to avoid extra cleanup of these indexes if an error happens
  9045. * elsewhere above.
  9046. */
  9047. if (!err)
  9048. err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
  9049. if (err) {
  9050. drop_inode = 1;
  9051. goto out_unlock_inode;
  9052. }
  9053. unlock_new_inode(inode);
  9054. d_instantiate(dentry, inode);
  9055. out_unlock:
  9056. btrfs_end_transaction(trans);
  9057. if (drop_inode) {
  9058. inode_dec_link_count(inode);
  9059. iput(inode);
  9060. }
  9061. btrfs_btree_balance_dirty(fs_info);
  9062. return err;
  9063. out_unlock_inode:
  9064. drop_inode = 1;
  9065. unlock_new_inode(inode);
  9066. goto out_unlock;
  9067. }
  9068. static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
  9069. u64 start, u64 num_bytes, u64 min_size,
  9070. loff_t actual_len, u64 *alloc_hint,
  9071. struct btrfs_trans_handle *trans)
  9072. {
  9073. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  9074. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  9075. struct extent_map *em;
  9076. struct btrfs_root *root = BTRFS_I(inode)->root;
  9077. struct btrfs_key ins;
  9078. u64 cur_offset = start;
  9079. u64 i_size;
  9080. u64 cur_bytes;
  9081. u64 last_alloc = (u64)-1;
  9082. int ret = 0;
  9083. bool own_trans = true;
  9084. u64 end = start + num_bytes - 1;
  9085. if (trans)
  9086. own_trans = false;
  9087. while (num_bytes > 0) {
  9088. if (own_trans) {
  9089. trans = btrfs_start_transaction(root, 3);
  9090. if (IS_ERR(trans)) {
  9091. ret = PTR_ERR(trans);
  9092. break;
  9093. }
  9094. }
  9095. cur_bytes = min_t(u64, num_bytes, SZ_256M);
  9096. cur_bytes = max(cur_bytes, min_size);
  9097. /*
  9098. * If we are severely fragmented we could end up with really
  9099. * small allocations, so if the allocator is returning small
  9100. * chunks lets make its job easier by only searching for those
  9101. * sized chunks.
  9102. */
  9103. cur_bytes = min(cur_bytes, last_alloc);
  9104. ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes,
  9105. min_size, 0, *alloc_hint, &ins, 1, 0);
  9106. if (ret) {
  9107. if (own_trans)
  9108. btrfs_end_transaction(trans);
  9109. break;
  9110. }
  9111. btrfs_dec_block_group_reservations(fs_info, ins.objectid);
  9112. last_alloc = ins.offset;
  9113. ret = insert_reserved_file_extent(trans, inode,
  9114. cur_offset, ins.objectid,
  9115. ins.offset, ins.offset,
  9116. ins.offset, 0, 0, 0,
  9117. BTRFS_FILE_EXTENT_PREALLOC);
  9118. if (ret) {
  9119. btrfs_free_reserved_extent(fs_info, ins.objectid,
  9120. ins.offset, 0);
  9121. btrfs_abort_transaction(trans, ret);
  9122. if (own_trans)
  9123. btrfs_end_transaction(trans);
  9124. break;
  9125. }
  9126. btrfs_drop_extent_cache(inode, cur_offset,
  9127. cur_offset + ins.offset -1, 0);
  9128. em = alloc_extent_map();
  9129. if (!em) {
  9130. set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
  9131. &BTRFS_I(inode)->runtime_flags);
  9132. goto next;
  9133. }
  9134. em->start = cur_offset;
  9135. em->orig_start = cur_offset;
  9136. em->len = ins.offset;
  9137. em->block_start = ins.objectid;
  9138. em->block_len = ins.offset;
  9139. em->orig_block_len = ins.offset;
  9140. em->ram_bytes = ins.offset;
  9141. em->bdev = fs_info->fs_devices->latest_bdev;
  9142. set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
  9143. em->generation = trans->transid;
  9144. while (1) {
  9145. write_lock(&em_tree->lock);
  9146. ret = add_extent_mapping(em_tree, em, 1);
  9147. write_unlock(&em_tree->lock);
  9148. if (ret != -EEXIST)
  9149. break;
  9150. btrfs_drop_extent_cache(inode, cur_offset,
  9151. cur_offset + ins.offset - 1,
  9152. 0);
  9153. }
  9154. free_extent_map(em);
  9155. next:
  9156. num_bytes -= ins.offset;
  9157. cur_offset += ins.offset;
  9158. *alloc_hint = ins.objectid + ins.offset;
  9159. inode_inc_iversion(inode);
  9160. inode->i_ctime = current_time(inode);
  9161. BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
  9162. if (!(mode & FALLOC_FL_KEEP_SIZE) &&
  9163. (actual_len > inode->i_size) &&
  9164. (cur_offset > inode->i_size)) {
  9165. if (cur_offset > actual_len)
  9166. i_size = actual_len;
  9167. else
  9168. i_size = cur_offset;
  9169. i_size_write(inode, i_size);
  9170. btrfs_ordered_update_i_size(inode, i_size, NULL);
  9171. }
  9172. ret = btrfs_update_inode(trans, root, inode);
  9173. if (ret) {
  9174. btrfs_abort_transaction(trans, ret);
  9175. if (own_trans)
  9176. btrfs_end_transaction(trans);
  9177. break;
  9178. }
  9179. if (own_trans)
  9180. btrfs_end_transaction(trans);
  9181. }
  9182. if (cur_offset < end)
  9183. btrfs_free_reserved_data_space(inode, cur_offset,
  9184. end - cur_offset + 1);
  9185. return ret;
  9186. }
  9187. int btrfs_prealloc_file_range(struct inode *inode, int mode,
  9188. u64 start, u64 num_bytes, u64 min_size,
  9189. loff_t actual_len, u64 *alloc_hint)
  9190. {
  9191. return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
  9192. min_size, actual_len, alloc_hint,
  9193. NULL);
  9194. }
  9195. int btrfs_prealloc_file_range_trans(struct inode *inode,
  9196. struct btrfs_trans_handle *trans, int mode,
  9197. u64 start, u64 num_bytes, u64 min_size,
  9198. loff_t actual_len, u64 *alloc_hint)
  9199. {
  9200. return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
  9201. min_size, actual_len, alloc_hint, trans);
  9202. }
  9203. static int btrfs_set_page_dirty(struct page *page)
  9204. {
  9205. return __set_page_dirty_nobuffers(page);
  9206. }
  9207. static int btrfs_permission(struct inode *inode, int mask)
  9208. {
  9209. struct btrfs_root *root = BTRFS_I(inode)->root;
  9210. umode_t mode = inode->i_mode;
  9211. if (mask & MAY_WRITE &&
  9212. (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
  9213. if (btrfs_root_readonly(root))
  9214. return -EROFS;
  9215. if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
  9216. return -EACCES;
  9217. }
  9218. return generic_permission(inode, mask);
  9219. }
  9220. static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
  9221. {
  9222. struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
  9223. struct btrfs_trans_handle *trans;
  9224. struct btrfs_root *root = BTRFS_I(dir)->root;
  9225. struct inode *inode = NULL;
  9226. u64 objectid;
  9227. u64 index;
  9228. int ret = 0;
  9229. /*
  9230. * 5 units required for adding orphan entry
  9231. */
  9232. trans = btrfs_start_transaction(root, 5);
  9233. if (IS_ERR(trans))
  9234. return PTR_ERR(trans);
  9235. ret = btrfs_find_free_ino(root, &objectid);
  9236. if (ret)
  9237. goto out;
  9238. inode = btrfs_new_inode(trans, root, dir, NULL, 0,
  9239. btrfs_ino(dir), objectid, mode, &index);
  9240. if (IS_ERR(inode)) {
  9241. ret = PTR_ERR(inode);
  9242. inode = NULL;
  9243. goto out;
  9244. }
  9245. inode->i_fop = &btrfs_file_operations;
  9246. inode->i_op = &btrfs_file_inode_operations;
  9247. inode->i_mapping->a_ops = &btrfs_aops;
  9248. BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
  9249. ret = btrfs_init_inode_security(trans, inode, dir, NULL);
  9250. if (ret)
  9251. goto out_inode;
  9252. ret = btrfs_update_inode(trans, root, inode);
  9253. if (ret)
  9254. goto out_inode;
  9255. ret = btrfs_orphan_add(trans, inode);
  9256. if (ret)
  9257. goto out_inode;
  9258. /*
  9259. * We set number of links to 0 in btrfs_new_inode(), and here we set
  9260. * it to 1 because d_tmpfile() will issue a warning if the count is 0,
  9261. * through:
  9262. *
  9263. * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
  9264. */
  9265. set_nlink(inode, 1);
  9266. unlock_new_inode(inode);
  9267. d_tmpfile(dentry, inode);
  9268. mark_inode_dirty(inode);
  9269. out:
  9270. btrfs_end_transaction(trans);
  9271. if (ret)
  9272. iput(inode);
  9273. btrfs_balance_delayed_items(fs_info);
  9274. btrfs_btree_balance_dirty(fs_info);
  9275. return ret;
  9276. out_inode:
  9277. unlock_new_inode(inode);
  9278. goto out;
  9279. }
  9280. static const struct inode_operations btrfs_dir_inode_operations = {
  9281. .getattr = btrfs_getattr,
  9282. .lookup = btrfs_lookup,
  9283. .create = btrfs_create,
  9284. .unlink = btrfs_unlink,
  9285. .link = btrfs_link,
  9286. .mkdir = btrfs_mkdir,
  9287. .rmdir = btrfs_rmdir,
  9288. .rename = btrfs_rename2,
  9289. .symlink = btrfs_symlink,
  9290. .setattr = btrfs_setattr,
  9291. .mknod = btrfs_mknod,
  9292. .listxattr = btrfs_listxattr,
  9293. .permission = btrfs_permission,
  9294. .get_acl = btrfs_get_acl,
  9295. .set_acl = btrfs_set_acl,
  9296. .update_time = btrfs_update_time,
  9297. .tmpfile = btrfs_tmpfile,
  9298. };
  9299. static const struct inode_operations btrfs_dir_ro_inode_operations = {
  9300. .lookup = btrfs_lookup,
  9301. .permission = btrfs_permission,
  9302. .get_acl = btrfs_get_acl,
  9303. .set_acl = btrfs_set_acl,
  9304. .update_time = btrfs_update_time,
  9305. };
  9306. static const struct file_operations btrfs_dir_file_operations = {
  9307. .llseek = generic_file_llseek,
  9308. .read = generic_read_dir,
  9309. .iterate_shared = btrfs_real_readdir,
  9310. .unlocked_ioctl = btrfs_ioctl,
  9311. #ifdef CONFIG_COMPAT
  9312. .compat_ioctl = btrfs_compat_ioctl,
  9313. #endif
  9314. .release = btrfs_release_file,
  9315. .fsync = btrfs_sync_file,
  9316. };
  9317. static const struct extent_io_ops btrfs_extent_io_ops = {
  9318. .fill_delalloc = run_delalloc_range,
  9319. .submit_bio_hook = btrfs_submit_bio_hook,
  9320. .merge_bio_hook = btrfs_merge_bio_hook,
  9321. .readpage_end_io_hook = btrfs_readpage_end_io_hook,
  9322. .writepage_end_io_hook = btrfs_writepage_end_io_hook,
  9323. .writepage_start_hook = btrfs_writepage_start_hook,
  9324. .set_bit_hook = btrfs_set_bit_hook,
  9325. .clear_bit_hook = btrfs_clear_bit_hook,
  9326. .merge_extent_hook = btrfs_merge_extent_hook,
  9327. .split_extent_hook = btrfs_split_extent_hook,
  9328. };
  9329. /*
  9330. * btrfs doesn't support the bmap operation because swapfiles
  9331. * use bmap to make a mapping of extents in the file. They assume
  9332. * these extents won't change over the life of the file and they
  9333. * use the bmap result to do IO directly to the drive.
  9334. *
  9335. * the btrfs bmap call would return logical addresses that aren't
  9336. * suitable for IO and they also will change frequently as COW
  9337. * operations happen. So, swapfile + btrfs == corruption.
  9338. *
  9339. * For now we're avoiding this by dropping bmap.
  9340. */
  9341. static const struct address_space_operations btrfs_aops = {
  9342. .readpage = btrfs_readpage,
  9343. .writepage = btrfs_writepage,
  9344. .writepages = btrfs_writepages,
  9345. .readpages = btrfs_readpages,
  9346. .direct_IO = btrfs_direct_IO,
  9347. .invalidatepage = btrfs_invalidatepage,
  9348. .releasepage = btrfs_releasepage,
  9349. .set_page_dirty = btrfs_set_page_dirty,
  9350. .error_remove_page = generic_error_remove_page,
  9351. };
  9352. static const struct address_space_operations btrfs_symlink_aops = {
  9353. .readpage = btrfs_readpage,
  9354. .writepage = btrfs_writepage,
  9355. .invalidatepage = btrfs_invalidatepage,
  9356. .releasepage = btrfs_releasepage,
  9357. };
  9358. static const struct inode_operations btrfs_file_inode_operations = {
  9359. .getattr = btrfs_getattr,
  9360. .setattr = btrfs_setattr,
  9361. .listxattr = btrfs_listxattr,
  9362. .permission = btrfs_permission,
  9363. .fiemap = btrfs_fiemap,
  9364. .get_acl = btrfs_get_acl,
  9365. .set_acl = btrfs_set_acl,
  9366. .update_time = btrfs_update_time,
  9367. };
  9368. static const struct inode_operations btrfs_special_inode_operations = {
  9369. .getattr = btrfs_getattr,
  9370. .setattr = btrfs_setattr,
  9371. .permission = btrfs_permission,
  9372. .listxattr = btrfs_listxattr,
  9373. .get_acl = btrfs_get_acl,
  9374. .set_acl = btrfs_set_acl,
  9375. .update_time = btrfs_update_time,
  9376. };
  9377. static const struct inode_operations btrfs_symlink_inode_operations = {
  9378. .readlink = generic_readlink,
  9379. .get_link = page_get_link,
  9380. .getattr = btrfs_getattr,
  9381. .setattr = btrfs_setattr,
  9382. .permission = btrfs_permission,
  9383. .listxattr = btrfs_listxattr,
  9384. .update_time = btrfs_update_time,
  9385. };
  9386. const struct dentry_operations btrfs_dentry_operations = {
  9387. .d_delete = btrfs_dentry_delete,
  9388. .d_release = btrfs_dentry_release,
  9389. };