inode.c 243 KB

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