inode.c 231 KB

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