inode.c 231 KB

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