inode.c 216 KB

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