inode.c 230 KB

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