inode.c 231 KB

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