inode.c 231 KB

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