inode.c 267 KB

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