inode.c 286 KB

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