inode.c 211 KB

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