inode.c 285 KB

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