inode.c 282 KB

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