inode.c 282 KB

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