inode.c 210 KB

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