inode.c 231 KB

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