inode.c 283 KB

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