inode.c 230 KB

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