inode.c 214 KB

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