inode.c 238 KB

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