inode.c 229 KB

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