inode.c 241 KB

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