inode.c 210 KB

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