inode.c 237 KB

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