inode.c 233 KB

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