ioctl.c 148 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2007 Oracle. All rights reserved.
  4. */
  5. #include <linux/kernel.h>
  6. #include <linux/bio.h>
  7. #include <linux/file.h>
  8. #include <linux/fs.h>
  9. #include <linux/fsnotify.h>
  10. #include <linux/pagemap.h>
  11. #include <linux/highmem.h>
  12. #include <linux/time.h>
  13. #include <linux/string.h>
  14. #include <linux/backing-dev.h>
  15. #include <linux/mount.h>
  16. #include <linux/namei.h>
  17. #include <linux/writeback.h>
  18. #include <linux/compat.h>
  19. #include <linux/security.h>
  20. #include <linux/xattr.h>
  21. #include <linux/mm.h>
  22. #include <linux/slab.h>
  23. #include <linux/blkdev.h>
  24. #include <linux/uuid.h>
  25. #include <linux/btrfs.h>
  26. #include <linux/uaccess.h>
  27. #include <linux/iversion.h>
  28. #include "ctree.h"
  29. #include "disk-io.h"
  30. #include "transaction.h"
  31. #include "btrfs_inode.h"
  32. #include "print-tree.h"
  33. #include "volumes.h"
  34. #include "locking.h"
  35. #include "inode-map.h"
  36. #include "backref.h"
  37. #include "rcu-string.h"
  38. #include "send.h"
  39. #include "dev-replace.h"
  40. #include "props.h"
  41. #include "sysfs.h"
  42. #include "qgroup.h"
  43. #include "tree-log.h"
  44. #include "compression.h"
  45. #ifdef CONFIG_64BIT
  46. /* If we have a 32-bit userspace and 64-bit kernel, then the UAPI
  47. * structures are incorrect, as the timespec structure from userspace
  48. * is 4 bytes too small. We define these alternatives here to teach
  49. * the kernel about the 32-bit struct packing.
  50. */
  51. struct btrfs_ioctl_timespec_32 {
  52. __u64 sec;
  53. __u32 nsec;
  54. } __attribute__ ((__packed__));
  55. struct btrfs_ioctl_received_subvol_args_32 {
  56. char uuid[BTRFS_UUID_SIZE]; /* in */
  57. __u64 stransid; /* in */
  58. __u64 rtransid; /* out */
  59. struct btrfs_ioctl_timespec_32 stime; /* in */
  60. struct btrfs_ioctl_timespec_32 rtime; /* out */
  61. __u64 flags; /* in */
  62. __u64 reserved[16]; /* in */
  63. } __attribute__ ((__packed__));
  64. #define BTRFS_IOC_SET_RECEIVED_SUBVOL_32 _IOWR(BTRFS_IOCTL_MAGIC, 37, \
  65. struct btrfs_ioctl_received_subvol_args_32)
  66. #endif
  67. #if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
  68. struct btrfs_ioctl_send_args_32 {
  69. __s64 send_fd; /* in */
  70. __u64 clone_sources_count; /* in */
  71. compat_uptr_t clone_sources; /* in */
  72. __u64 parent_root; /* in */
  73. __u64 flags; /* in */
  74. __u64 reserved[4]; /* in */
  75. } __attribute__ ((__packed__));
  76. #define BTRFS_IOC_SEND_32 _IOW(BTRFS_IOCTL_MAGIC, 38, \
  77. struct btrfs_ioctl_send_args_32)
  78. #endif
  79. static int btrfs_clone(struct inode *src, struct inode *inode,
  80. u64 off, u64 olen, u64 olen_aligned, u64 destoff,
  81. int no_time_update);
  82. /* Mask out flags that are inappropriate for the given type of inode. */
  83. static unsigned int btrfs_mask_fsflags_for_type(struct inode *inode,
  84. unsigned int flags)
  85. {
  86. if (S_ISDIR(inode->i_mode))
  87. return flags;
  88. else if (S_ISREG(inode->i_mode))
  89. return flags & ~FS_DIRSYNC_FL;
  90. else
  91. return flags & (FS_NODUMP_FL | FS_NOATIME_FL);
  92. }
  93. /*
  94. * Export internal inode flags to the format expected by the FS_IOC_GETFLAGS
  95. * ioctl.
  96. */
  97. static unsigned int btrfs_inode_flags_to_fsflags(unsigned int flags)
  98. {
  99. unsigned int iflags = 0;
  100. if (flags & BTRFS_INODE_SYNC)
  101. iflags |= FS_SYNC_FL;
  102. if (flags & BTRFS_INODE_IMMUTABLE)
  103. iflags |= FS_IMMUTABLE_FL;
  104. if (flags & BTRFS_INODE_APPEND)
  105. iflags |= FS_APPEND_FL;
  106. if (flags & BTRFS_INODE_NODUMP)
  107. iflags |= FS_NODUMP_FL;
  108. if (flags & BTRFS_INODE_NOATIME)
  109. iflags |= FS_NOATIME_FL;
  110. if (flags & BTRFS_INODE_DIRSYNC)
  111. iflags |= FS_DIRSYNC_FL;
  112. if (flags & BTRFS_INODE_NODATACOW)
  113. iflags |= FS_NOCOW_FL;
  114. if (flags & BTRFS_INODE_NOCOMPRESS)
  115. iflags |= FS_NOCOMP_FL;
  116. else if (flags & BTRFS_INODE_COMPRESS)
  117. iflags |= FS_COMPR_FL;
  118. return iflags;
  119. }
  120. /*
  121. * Update inode->i_flags based on the btrfs internal flags.
  122. */
  123. void btrfs_sync_inode_flags_to_i_flags(struct inode *inode)
  124. {
  125. struct btrfs_inode *binode = BTRFS_I(inode);
  126. unsigned int new_fl = 0;
  127. if (binode->flags & BTRFS_INODE_SYNC)
  128. new_fl |= S_SYNC;
  129. if (binode->flags & BTRFS_INODE_IMMUTABLE)
  130. new_fl |= S_IMMUTABLE;
  131. if (binode->flags & BTRFS_INODE_APPEND)
  132. new_fl |= S_APPEND;
  133. if (binode->flags & BTRFS_INODE_NOATIME)
  134. new_fl |= S_NOATIME;
  135. if (binode->flags & BTRFS_INODE_DIRSYNC)
  136. new_fl |= S_DIRSYNC;
  137. set_mask_bits(&inode->i_flags,
  138. S_SYNC | S_APPEND | S_IMMUTABLE | S_NOATIME | S_DIRSYNC,
  139. new_fl);
  140. }
  141. static int btrfs_ioctl_getflags(struct file *file, void __user *arg)
  142. {
  143. struct btrfs_inode *binode = BTRFS_I(file_inode(file));
  144. unsigned int flags = btrfs_inode_flags_to_fsflags(binode->flags);
  145. if (copy_to_user(arg, &flags, sizeof(flags)))
  146. return -EFAULT;
  147. return 0;
  148. }
  149. /* Check if @flags are a supported and valid set of FS_*_FL flags */
  150. static int check_fsflags(unsigned int flags)
  151. {
  152. if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
  153. FS_NOATIME_FL | FS_NODUMP_FL | \
  154. FS_SYNC_FL | FS_DIRSYNC_FL | \
  155. FS_NOCOMP_FL | FS_COMPR_FL |
  156. FS_NOCOW_FL))
  157. return -EOPNOTSUPP;
  158. if ((flags & FS_NOCOMP_FL) && (flags & FS_COMPR_FL))
  159. return -EINVAL;
  160. return 0;
  161. }
  162. static int btrfs_ioctl_setflags(struct file *file, void __user *arg)
  163. {
  164. struct inode *inode = file_inode(file);
  165. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  166. struct btrfs_inode *binode = BTRFS_I(inode);
  167. struct btrfs_root *root = binode->root;
  168. struct btrfs_trans_handle *trans;
  169. unsigned int fsflags, old_fsflags;
  170. int ret;
  171. u64 old_flags;
  172. unsigned int old_i_flags;
  173. umode_t mode;
  174. if (!inode_owner_or_capable(inode))
  175. return -EPERM;
  176. if (btrfs_root_readonly(root))
  177. return -EROFS;
  178. if (copy_from_user(&fsflags, arg, sizeof(fsflags)))
  179. return -EFAULT;
  180. ret = check_fsflags(fsflags);
  181. if (ret)
  182. return ret;
  183. ret = mnt_want_write_file(file);
  184. if (ret)
  185. return ret;
  186. inode_lock(inode);
  187. old_flags = binode->flags;
  188. old_i_flags = inode->i_flags;
  189. mode = inode->i_mode;
  190. fsflags = btrfs_mask_fsflags_for_type(inode, fsflags);
  191. old_fsflags = btrfs_inode_flags_to_fsflags(binode->flags);
  192. if ((fsflags ^ old_fsflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
  193. if (!capable(CAP_LINUX_IMMUTABLE)) {
  194. ret = -EPERM;
  195. goto out_unlock;
  196. }
  197. }
  198. if (fsflags & FS_SYNC_FL)
  199. binode->flags |= BTRFS_INODE_SYNC;
  200. else
  201. binode->flags &= ~BTRFS_INODE_SYNC;
  202. if (fsflags & FS_IMMUTABLE_FL)
  203. binode->flags |= BTRFS_INODE_IMMUTABLE;
  204. else
  205. binode->flags &= ~BTRFS_INODE_IMMUTABLE;
  206. if (fsflags & FS_APPEND_FL)
  207. binode->flags |= BTRFS_INODE_APPEND;
  208. else
  209. binode->flags &= ~BTRFS_INODE_APPEND;
  210. if (fsflags & FS_NODUMP_FL)
  211. binode->flags |= BTRFS_INODE_NODUMP;
  212. else
  213. binode->flags &= ~BTRFS_INODE_NODUMP;
  214. if (fsflags & FS_NOATIME_FL)
  215. binode->flags |= BTRFS_INODE_NOATIME;
  216. else
  217. binode->flags &= ~BTRFS_INODE_NOATIME;
  218. if (fsflags & FS_DIRSYNC_FL)
  219. binode->flags |= BTRFS_INODE_DIRSYNC;
  220. else
  221. binode->flags &= ~BTRFS_INODE_DIRSYNC;
  222. if (fsflags & FS_NOCOW_FL) {
  223. if (S_ISREG(mode)) {
  224. /*
  225. * It's safe to turn csums off here, no extents exist.
  226. * Otherwise we want the flag to reflect the real COW
  227. * status of the file and will not set it.
  228. */
  229. if (inode->i_size == 0)
  230. binode->flags |= BTRFS_INODE_NODATACOW
  231. | BTRFS_INODE_NODATASUM;
  232. } else {
  233. binode->flags |= BTRFS_INODE_NODATACOW;
  234. }
  235. } else {
  236. /*
  237. * Revert back under same assumptions as above
  238. */
  239. if (S_ISREG(mode)) {
  240. if (inode->i_size == 0)
  241. binode->flags &= ~(BTRFS_INODE_NODATACOW
  242. | BTRFS_INODE_NODATASUM);
  243. } else {
  244. binode->flags &= ~BTRFS_INODE_NODATACOW;
  245. }
  246. }
  247. /*
  248. * The COMPRESS flag can only be changed by users, while the NOCOMPRESS
  249. * flag may be changed automatically if compression code won't make
  250. * things smaller.
  251. */
  252. if (fsflags & FS_NOCOMP_FL) {
  253. binode->flags &= ~BTRFS_INODE_COMPRESS;
  254. binode->flags |= BTRFS_INODE_NOCOMPRESS;
  255. ret = btrfs_set_prop(inode, "btrfs.compression", NULL, 0, 0);
  256. if (ret && ret != -ENODATA)
  257. goto out_drop;
  258. } else if (fsflags & FS_COMPR_FL) {
  259. const char *comp;
  260. binode->flags |= BTRFS_INODE_COMPRESS;
  261. binode->flags &= ~BTRFS_INODE_NOCOMPRESS;
  262. comp = btrfs_compress_type2str(fs_info->compress_type);
  263. if (!comp || comp[0] == 0)
  264. comp = btrfs_compress_type2str(BTRFS_COMPRESS_ZLIB);
  265. ret = btrfs_set_prop(inode, "btrfs.compression",
  266. comp, strlen(comp), 0);
  267. if (ret)
  268. goto out_drop;
  269. } else {
  270. ret = btrfs_set_prop(inode, "btrfs.compression", NULL, 0, 0);
  271. if (ret && ret != -ENODATA)
  272. goto out_drop;
  273. binode->flags &= ~(BTRFS_INODE_COMPRESS | BTRFS_INODE_NOCOMPRESS);
  274. }
  275. trans = btrfs_start_transaction(root, 1);
  276. if (IS_ERR(trans)) {
  277. ret = PTR_ERR(trans);
  278. goto out_drop;
  279. }
  280. btrfs_sync_inode_flags_to_i_flags(inode);
  281. inode_inc_iversion(inode);
  282. inode->i_ctime = current_time(inode);
  283. ret = btrfs_update_inode(trans, root, inode);
  284. btrfs_end_transaction(trans);
  285. out_drop:
  286. if (ret) {
  287. binode->flags = old_flags;
  288. inode->i_flags = old_i_flags;
  289. }
  290. out_unlock:
  291. inode_unlock(inode);
  292. mnt_drop_write_file(file);
  293. return ret;
  294. }
  295. /*
  296. * Translate btrfs internal inode flags to xflags as expected by the
  297. * FS_IOC_FSGETXATT ioctl. Filter only the supported ones, unknown flags are
  298. * silently dropped.
  299. */
  300. static unsigned int btrfs_inode_flags_to_xflags(unsigned int flags)
  301. {
  302. unsigned int xflags = 0;
  303. if (flags & BTRFS_INODE_APPEND)
  304. xflags |= FS_XFLAG_APPEND;
  305. if (flags & BTRFS_INODE_IMMUTABLE)
  306. xflags |= FS_XFLAG_IMMUTABLE;
  307. if (flags & BTRFS_INODE_NOATIME)
  308. xflags |= FS_XFLAG_NOATIME;
  309. if (flags & BTRFS_INODE_NODUMP)
  310. xflags |= FS_XFLAG_NODUMP;
  311. if (flags & BTRFS_INODE_SYNC)
  312. xflags |= FS_XFLAG_SYNC;
  313. return xflags;
  314. }
  315. /* Check if @flags are a supported and valid set of FS_XFLAGS_* flags */
  316. static int check_xflags(unsigned int flags)
  317. {
  318. if (flags & ~(FS_XFLAG_APPEND | FS_XFLAG_IMMUTABLE | FS_XFLAG_NOATIME |
  319. FS_XFLAG_NODUMP | FS_XFLAG_SYNC))
  320. return -EOPNOTSUPP;
  321. return 0;
  322. }
  323. /*
  324. * Set the xflags from the internal inode flags. The remaining items of fsxattr
  325. * are zeroed.
  326. */
  327. static int btrfs_ioctl_fsgetxattr(struct file *file, void __user *arg)
  328. {
  329. struct btrfs_inode *binode = BTRFS_I(file_inode(file));
  330. struct fsxattr fa;
  331. memset(&fa, 0, sizeof(fa));
  332. fa.fsx_xflags = btrfs_inode_flags_to_xflags(binode->flags);
  333. if (copy_to_user(arg, &fa, sizeof(fa)))
  334. return -EFAULT;
  335. return 0;
  336. }
  337. static int btrfs_ioctl_fssetxattr(struct file *file, void __user *arg)
  338. {
  339. struct inode *inode = file_inode(file);
  340. struct btrfs_inode *binode = BTRFS_I(inode);
  341. struct btrfs_root *root = binode->root;
  342. struct btrfs_trans_handle *trans;
  343. struct fsxattr fa;
  344. unsigned old_flags;
  345. unsigned old_i_flags;
  346. int ret = 0;
  347. if (!inode_owner_or_capable(inode))
  348. return -EPERM;
  349. if (btrfs_root_readonly(root))
  350. return -EROFS;
  351. memset(&fa, 0, sizeof(fa));
  352. if (copy_from_user(&fa, arg, sizeof(fa)))
  353. return -EFAULT;
  354. ret = check_xflags(fa.fsx_xflags);
  355. if (ret)
  356. return ret;
  357. if (fa.fsx_extsize != 0 || fa.fsx_projid != 0 || fa.fsx_cowextsize != 0)
  358. return -EOPNOTSUPP;
  359. ret = mnt_want_write_file(file);
  360. if (ret)
  361. return ret;
  362. inode_lock(inode);
  363. old_flags = binode->flags;
  364. old_i_flags = inode->i_flags;
  365. /* We need the capabilities to change append-only or immutable inode */
  366. if (((old_flags & (BTRFS_INODE_APPEND | BTRFS_INODE_IMMUTABLE)) ||
  367. (fa.fsx_xflags & (FS_XFLAG_APPEND | FS_XFLAG_IMMUTABLE))) &&
  368. !capable(CAP_LINUX_IMMUTABLE)) {
  369. ret = -EPERM;
  370. goto out_unlock;
  371. }
  372. if (fa.fsx_xflags & FS_XFLAG_SYNC)
  373. binode->flags |= BTRFS_INODE_SYNC;
  374. else
  375. binode->flags &= ~BTRFS_INODE_SYNC;
  376. if (fa.fsx_xflags & FS_XFLAG_IMMUTABLE)
  377. binode->flags |= BTRFS_INODE_IMMUTABLE;
  378. else
  379. binode->flags &= ~BTRFS_INODE_IMMUTABLE;
  380. if (fa.fsx_xflags & FS_XFLAG_APPEND)
  381. binode->flags |= BTRFS_INODE_APPEND;
  382. else
  383. binode->flags &= ~BTRFS_INODE_APPEND;
  384. if (fa.fsx_xflags & FS_XFLAG_NODUMP)
  385. binode->flags |= BTRFS_INODE_NODUMP;
  386. else
  387. binode->flags &= ~BTRFS_INODE_NODUMP;
  388. if (fa.fsx_xflags & FS_XFLAG_NOATIME)
  389. binode->flags |= BTRFS_INODE_NOATIME;
  390. else
  391. binode->flags &= ~BTRFS_INODE_NOATIME;
  392. /* 1 item for the inode */
  393. trans = btrfs_start_transaction(root, 1);
  394. if (IS_ERR(trans)) {
  395. ret = PTR_ERR(trans);
  396. goto out_unlock;
  397. }
  398. btrfs_sync_inode_flags_to_i_flags(inode);
  399. inode_inc_iversion(inode);
  400. inode->i_ctime = current_time(inode);
  401. ret = btrfs_update_inode(trans, root, inode);
  402. btrfs_end_transaction(trans);
  403. out_unlock:
  404. if (ret) {
  405. binode->flags = old_flags;
  406. inode->i_flags = old_i_flags;
  407. }
  408. inode_unlock(inode);
  409. mnt_drop_write_file(file);
  410. return ret;
  411. }
  412. static int btrfs_ioctl_getversion(struct file *file, int __user *arg)
  413. {
  414. struct inode *inode = file_inode(file);
  415. return put_user(inode->i_generation, arg);
  416. }
  417. static noinline int btrfs_ioctl_fitrim(struct file *file, void __user *arg)
  418. {
  419. struct inode *inode = file_inode(file);
  420. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  421. struct btrfs_device *device;
  422. struct request_queue *q;
  423. struct fstrim_range range;
  424. u64 minlen = ULLONG_MAX;
  425. u64 num_devices = 0;
  426. int ret;
  427. if (!capable(CAP_SYS_ADMIN))
  428. return -EPERM;
  429. /*
  430. * If the fs is mounted with nologreplay, which requires it to be
  431. * mounted in RO mode as well, we can not allow discard on free space
  432. * inside block groups, because log trees refer to extents that are not
  433. * pinned in a block group's free space cache (pinning the extents is
  434. * precisely the first phase of replaying a log tree).
  435. */
  436. if (btrfs_test_opt(fs_info, NOLOGREPLAY))
  437. return -EROFS;
  438. rcu_read_lock();
  439. list_for_each_entry_rcu(device, &fs_info->fs_devices->devices,
  440. dev_list) {
  441. if (!device->bdev)
  442. continue;
  443. q = bdev_get_queue(device->bdev);
  444. if (blk_queue_discard(q)) {
  445. num_devices++;
  446. minlen = min_t(u64, q->limits.discard_granularity,
  447. minlen);
  448. }
  449. }
  450. rcu_read_unlock();
  451. if (!num_devices)
  452. return -EOPNOTSUPP;
  453. if (copy_from_user(&range, arg, sizeof(range)))
  454. return -EFAULT;
  455. /*
  456. * NOTE: Don't truncate the range using super->total_bytes. Bytenr of
  457. * block group is in the logical address space, which can be any
  458. * sectorsize aligned bytenr in the range [0, U64_MAX].
  459. */
  460. if (range.len < fs_info->sb->s_blocksize)
  461. return -EINVAL;
  462. range.minlen = max(range.minlen, minlen);
  463. ret = btrfs_trim_fs(fs_info, &range);
  464. if (ret < 0)
  465. return ret;
  466. if (copy_to_user(arg, &range, sizeof(range)))
  467. return -EFAULT;
  468. return 0;
  469. }
  470. int btrfs_is_empty_uuid(u8 *uuid)
  471. {
  472. int i;
  473. for (i = 0; i < BTRFS_UUID_SIZE; i++) {
  474. if (uuid[i])
  475. return 0;
  476. }
  477. return 1;
  478. }
  479. static noinline int create_subvol(struct inode *dir,
  480. struct dentry *dentry,
  481. const char *name, int namelen,
  482. u64 *async_transid,
  483. struct btrfs_qgroup_inherit *inherit)
  484. {
  485. struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
  486. struct btrfs_trans_handle *trans;
  487. struct btrfs_key key;
  488. struct btrfs_root_item *root_item;
  489. struct btrfs_inode_item *inode_item;
  490. struct extent_buffer *leaf;
  491. struct btrfs_root *root = BTRFS_I(dir)->root;
  492. struct btrfs_root *new_root;
  493. struct btrfs_block_rsv block_rsv;
  494. struct timespec64 cur_time = current_time(dir);
  495. struct inode *inode;
  496. int ret;
  497. int err;
  498. u64 objectid;
  499. u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
  500. u64 index = 0;
  501. uuid_le new_uuid;
  502. root_item = kzalloc(sizeof(*root_item), GFP_KERNEL);
  503. if (!root_item)
  504. return -ENOMEM;
  505. ret = btrfs_find_free_objectid(fs_info->tree_root, &objectid);
  506. if (ret)
  507. goto fail_free;
  508. /*
  509. * Don't create subvolume whose level is not zero. Or qgroup will be
  510. * screwed up since it assumes subvolume qgroup's level to be 0.
  511. */
  512. if (btrfs_qgroup_level(objectid)) {
  513. ret = -ENOSPC;
  514. goto fail_free;
  515. }
  516. btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
  517. /*
  518. * The same as the snapshot creation, please see the comment
  519. * of create_snapshot().
  520. */
  521. ret = btrfs_subvolume_reserve_metadata(root, &block_rsv, 8, false);
  522. if (ret)
  523. goto fail_free;
  524. trans = btrfs_start_transaction(root, 0);
  525. if (IS_ERR(trans)) {
  526. ret = PTR_ERR(trans);
  527. btrfs_subvolume_release_metadata(fs_info, &block_rsv);
  528. goto fail_free;
  529. }
  530. trans->block_rsv = &block_rsv;
  531. trans->bytes_reserved = block_rsv.size;
  532. ret = btrfs_qgroup_inherit(trans, 0, objectid, inherit);
  533. if (ret)
  534. goto fail;
  535. leaf = btrfs_alloc_tree_block(trans, root, 0, objectid, NULL, 0, 0, 0);
  536. if (IS_ERR(leaf)) {
  537. ret = PTR_ERR(leaf);
  538. goto fail;
  539. }
  540. btrfs_mark_buffer_dirty(leaf);
  541. inode_item = &root_item->inode;
  542. btrfs_set_stack_inode_generation(inode_item, 1);
  543. btrfs_set_stack_inode_size(inode_item, 3);
  544. btrfs_set_stack_inode_nlink(inode_item, 1);
  545. btrfs_set_stack_inode_nbytes(inode_item,
  546. fs_info->nodesize);
  547. btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755);
  548. btrfs_set_root_flags(root_item, 0);
  549. btrfs_set_root_limit(root_item, 0);
  550. btrfs_set_stack_inode_flags(inode_item, BTRFS_INODE_ROOT_ITEM_INIT);
  551. btrfs_set_root_bytenr(root_item, leaf->start);
  552. btrfs_set_root_generation(root_item, trans->transid);
  553. btrfs_set_root_level(root_item, 0);
  554. btrfs_set_root_refs(root_item, 1);
  555. btrfs_set_root_used(root_item, leaf->len);
  556. btrfs_set_root_last_snapshot(root_item, 0);
  557. btrfs_set_root_generation_v2(root_item,
  558. btrfs_root_generation(root_item));
  559. uuid_le_gen(&new_uuid);
  560. memcpy(root_item->uuid, new_uuid.b, BTRFS_UUID_SIZE);
  561. btrfs_set_stack_timespec_sec(&root_item->otime, cur_time.tv_sec);
  562. btrfs_set_stack_timespec_nsec(&root_item->otime, cur_time.tv_nsec);
  563. root_item->ctime = root_item->otime;
  564. btrfs_set_root_ctransid(root_item, trans->transid);
  565. btrfs_set_root_otransid(root_item, trans->transid);
  566. btrfs_tree_unlock(leaf);
  567. btrfs_set_root_dirid(root_item, new_dirid);
  568. key.objectid = objectid;
  569. key.offset = 0;
  570. key.type = BTRFS_ROOT_ITEM_KEY;
  571. ret = btrfs_insert_root(trans, fs_info->tree_root, &key,
  572. root_item);
  573. if (ret) {
  574. /*
  575. * Since we don't abort the transaction in this case, free the
  576. * tree block so that we don't leak space and leave the
  577. * filesystem in an inconsistent state (an extent item in the
  578. * extent tree without backreferences). Also no need to have
  579. * the tree block locked since it is not in any tree at this
  580. * point, so no other task can find it and use it.
  581. */
  582. btrfs_free_tree_block(trans, root, leaf, 0, 1);
  583. free_extent_buffer(leaf);
  584. goto fail;
  585. }
  586. free_extent_buffer(leaf);
  587. leaf = NULL;
  588. key.offset = (u64)-1;
  589. new_root = btrfs_read_fs_root_no_name(fs_info, &key);
  590. if (IS_ERR(new_root)) {
  591. ret = PTR_ERR(new_root);
  592. btrfs_abort_transaction(trans, ret);
  593. goto fail;
  594. }
  595. btrfs_record_root_in_trans(trans, new_root);
  596. ret = btrfs_create_subvol_root(trans, new_root, root, new_dirid);
  597. if (ret) {
  598. /* We potentially lose an unused inode item here */
  599. btrfs_abort_transaction(trans, ret);
  600. goto fail;
  601. }
  602. mutex_lock(&new_root->objectid_mutex);
  603. new_root->highest_objectid = new_dirid;
  604. mutex_unlock(&new_root->objectid_mutex);
  605. /*
  606. * insert the directory item
  607. */
  608. ret = btrfs_set_inode_index(BTRFS_I(dir), &index);
  609. if (ret) {
  610. btrfs_abort_transaction(trans, ret);
  611. goto fail;
  612. }
  613. ret = btrfs_insert_dir_item(trans, root,
  614. name, namelen, BTRFS_I(dir), &key,
  615. BTRFS_FT_DIR, index);
  616. if (ret) {
  617. btrfs_abort_transaction(trans, ret);
  618. goto fail;
  619. }
  620. btrfs_i_size_write(BTRFS_I(dir), dir->i_size + namelen * 2);
  621. ret = btrfs_update_inode(trans, root, dir);
  622. if (ret) {
  623. btrfs_abort_transaction(trans, ret);
  624. goto fail;
  625. }
  626. ret = btrfs_add_root_ref(trans, objectid, root->root_key.objectid,
  627. btrfs_ino(BTRFS_I(dir)), index, name, namelen);
  628. if (ret) {
  629. btrfs_abort_transaction(trans, ret);
  630. goto fail;
  631. }
  632. ret = btrfs_uuid_tree_add(trans, root_item->uuid,
  633. BTRFS_UUID_KEY_SUBVOL, objectid);
  634. if (ret)
  635. btrfs_abort_transaction(trans, ret);
  636. fail:
  637. kfree(root_item);
  638. trans->block_rsv = NULL;
  639. trans->bytes_reserved = 0;
  640. btrfs_subvolume_release_metadata(fs_info, &block_rsv);
  641. if (async_transid) {
  642. *async_transid = trans->transid;
  643. err = btrfs_commit_transaction_async(trans, 1);
  644. if (err)
  645. err = btrfs_commit_transaction(trans);
  646. } else {
  647. err = btrfs_commit_transaction(trans);
  648. }
  649. if (err && !ret)
  650. ret = err;
  651. if (!ret) {
  652. inode = btrfs_lookup_dentry(dir, dentry);
  653. if (IS_ERR(inode))
  654. return PTR_ERR(inode);
  655. d_instantiate(dentry, inode);
  656. }
  657. return ret;
  658. fail_free:
  659. kfree(root_item);
  660. return ret;
  661. }
  662. static int create_snapshot(struct btrfs_root *root, struct inode *dir,
  663. struct dentry *dentry,
  664. u64 *async_transid, bool readonly,
  665. struct btrfs_qgroup_inherit *inherit)
  666. {
  667. struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
  668. struct inode *inode;
  669. struct btrfs_pending_snapshot *pending_snapshot;
  670. struct btrfs_trans_handle *trans;
  671. int ret;
  672. bool snapshot_force_cow = false;
  673. if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state))
  674. return -EINVAL;
  675. pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_KERNEL);
  676. if (!pending_snapshot)
  677. return -ENOMEM;
  678. pending_snapshot->root_item = kzalloc(sizeof(struct btrfs_root_item),
  679. GFP_KERNEL);
  680. pending_snapshot->path = btrfs_alloc_path();
  681. if (!pending_snapshot->root_item || !pending_snapshot->path) {
  682. ret = -ENOMEM;
  683. goto free_pending;
  684. }
  685. /*
  686. * Force new buffered writes to reserve space even when NOCOW is
  687. * possible. This is to avoid later writeback (running dealloc) to
  688. * fallback to COW mode and unexpectedly fail with ENOSPC.
  689. */
  690. atomic_inc(&root->will_be_snapshotted);
  691. smp_mb__after_atomic();
  692. /* wait for no snapshot writes */
  693. wait_event(root->subv_writers->wait,
  694. percpu_counter_sum(&root->subv_writers->counter) == 0);
  695. ret = btrfs_start_delalloc_snapshot(root);
  696. if (ret)
  697. goto dec_and_free;
  698. /*
  699. * All previous writes have started writeback in NOCOW mode, so now
  700. * we force future writes to fallback to COW mode during snapshot
  701. * creation.
  702. */
  703. atomic_inc(&root->snapshot_force_cow);
  704. snapshot_force_cow = true;
  705. btrfs_wait_ordered_extents(root, U64_MAX, 0, (u64)-1);
  706. btrfs_init_block_rsv(&pending_snapshot->block_rsv,
  707. BTRFS_BLOCK_RSV_TEMP);
  708. /*
  709. * 1 - parent dir inode
  710. * 2 - dir entries
  711. * 1 - root item
  712. * 2 - root ref/backref
  713. * 1 - root of snapshot
  714. * 1 - UUID item
  715. */
  716. ret = btrfs_subvolume_reserve_metadata(BTRFS_I(dir)->root,
  717. &pending_snapshot->block_rsv, 8,
  718. false);
  719. if (ret)
  720. goto dec_and_free;
  721. pending_snapshot->dentry = dentry;
  722. pending_snapshot->root = root;
  723. pending_snapshot->readonly = readonly;
  724. pending_snapshot->dir = dir;
  725. pending_snapshot->inherit = inherit;
  726. trans = btrfs_start_transaction(root, 0);
  727. if (IS_ERR(trans)) {
  728. ret = PTR_ERR(trans);
  729. goto fail;
  730. }
  731. spin_lock(&fs_info->trans_lock);
  732. list_add(&pending_snapshot->list,
  733. &trans->transaction->pending_snapshots);
  734. spin_unlock(&fs_info->trans_lock);
  735. if (async_transid) {
  736. *async_transid = trans->transid;
  737. ret = btrfs_commit_transaction_async(trans, 1);
  738. if (ret)
  739. ret = btrfs_commit_transaction(trans);
  740. } else {
  741. ret = btrfs_commit_transaction(trans);
  742. }
  743. if (ret)
  744. goto fail;
  745. ret = pending_snapshot->error;
  746. if (ret)
  747. goto fail;
  748. ret = btrfs_orphan_cleanup(pending_snapshot->snap);
  749. if (ret)
  750. goto fail;
  751. inode = btrfs_lookup_dentry(d_inode(dentry->d_parent), dentry);
  752. if (IS_ERR(inode)) {
  753. ret = PTR_ERR(inode);
  754. goto fail;
  755. }
  756. d_instantiate(dentry, inode);
  757. ret = 0;
  758. fail:
  759. btrfs_subvolume_release_metadata(fs_info, &pending_snapshot->block_rsv);
  760. dec_and_free:
  761. if (snapshot_force_cow)
  762. atomic_dec(&root->snapshot_force_cow);
  763. if (atomic_dec_and_test(&root->will_be_snapshotted))
  764. wake_up_var(&root->will_be_snapshotted);
  765. free_pending:
  766. kfree(pending_snapshot->root_item);
  767. btrfs_free_path(pending_snapshot->path);
  768. kfree(pending_snapshot);
  769. return ret;
  770. }
  771. /* copy of may_delete in fs/namei.c()
  772. * Check whether we can remove a link victim from directory dir, check
  773. * whether the type of victim is right.
  774. * 1. We can't do it if dir is read-only (done in permission())
  775. * 2. We should have write and exec permissions on dir
  776. * 3. We can't remove anything from append-only dir
  777. * 4. We can't do anything with immutable dir (done in permission())
  778. * 5. If the sticky bit on dir is set we should either
  779. * a. be owner of dir, or
  780. * b. be owner of victim, or
  781. * c. have CAP_FOWNER capability
  782. * 6. If the victim is append-only or immutable we can't do anything with
  783. * links pointing to it.
  784. * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
  785. * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
  786. * 9. We can't remove a root or mountpoint.
  787. * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
  788. * nfs_async_unlink().
  789. */
  790. static int btrfs_may_delete(struct inode *dir, struct dentry *victim, int isdir)
  791. {
  792. int error;
  793. if (d_really_is_negative(victim))
  794. return -ENOENT;
  795. BUG_ON(d_inode(victim->d_parent) != dir);
  796. audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
  797. error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
  798. if (error)
  799. return error;
  800. if (IS_APPEND(dir))
  801. return -EPERM;
  802. if (check_sticky(dir, d_inode(victim)) || IS_APPEND(d_inode(victim)) ||
  803. IS_IMMUTABLE(d_inode(victim)) || IS_SWAPFILE(d_inode(victim)))
  804. return -EPERM;
  805. if (isdir) {
  806. if (!d_is_dir(victim))
  807. return -ENOTDIR;
  808. if (IS_ROOT(victim))
  809. return -EBUSY;
  810. } else if (d_is_dir(victim))
  811. return -EISDIR;
  812. if (IS_DEADDIR(dir))
  813. return -ENOENT;
  814. if (victim->d_flags & DCACHE_NFSFS_RENAMED)
  815. return -EBUSY;
  816. return 0;
  817. }
  818. /* copy of may_create in fs/namei.c() */
  819. static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
  820. {
  821. if (d_really_is_positive(child))
  822. return -EEXIST;
  823. if (IS_DEADDIR(dir))
  824. return -ENOENT;
  825. return inode_permission(dir, MAY_WRITE | MAY_EXEC);
  826. }
  827. /*
  828. * Create a new subvolume below @parent. This is largely modeled after
  829. * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
  830. * inside this filesystem so it's quite a bit simpler.
  831. */
  832. static noinline int btrfs_mksubvol(const struct path *parent,
  833. const char *name, int namelen,
  834. struct btrfs_root *snap_src,
  835. u64 *async_transid, bool readonly,
  836. struct btrfs_qgroup_inherit *inherit)
  837. {
  838. struct inode *dir = d_inode(parent->dentry);
  839. struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
  840. struct dentry *dentry;
  841. int error;
  842. error = down_write_killable_nested(&dir->i_rwsem, I_MUTEX_PARENT);
  843. if (error == -EINTR)
  844. return error;
  845. dentry = lookup_one_len(name, parent->dentry, namelen);
  846. error = PTR_ERR(dentry);
  847. if (IS_ERR(dentry))
  848. goto out_unlock;
  849. error = btrfs_may_create(dir, dentry);
  850. if (error)
  851. goto out_dput;
  852. /*
  853. * even if this name doesn't exist, we may get hash collisions.
  854. * check for them now when we can safely fail
  855. */
  856. error = btrfs_check_dir_item_collision(BTRFS_I(dir)->root,
  857. dir->i_ino, name,
  858. namelen);
  859. if (error)
  860. goto out_dput;
  861. down_read(&fs_info->subvol_sem);
  862. if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
  863. goto out_up_read;
  864. if (snap_src) {
  865. error = create_snapshot(snap_src, dir, dentry,
  866. async_transid, readonly, inherit);
  867. } else {
  868. error = create_subvol(dir, dentry, name, namelen,
  869. async_transid, inherit);
  870. }
  871. if (!error)
  872. fsnotify_mkdir(dir, dentry);
  873. out_up_read:
  874. up_read(&fs_info->subvol_sem);
  875. out_dput:
  876. dput(dentry);
  877. out_unlock:
  878. inode_unlock(dir);
  879. return error;
  880. }
  881. /*
  882. * When we're defragging a range, we don't want to kick it off again
  883. * if it is really just waiting for delalloc to send it down.
  884. * If we find a nice big extent or delalloc range for the bytes in the
  885. * file you want to defrag, we return 0 to let you know to skip this
  886. * part of the file
  887. */
  888. static int check_defrag_in_cache(struct inode *inode, u64 offset, u32 thresh)
  889. {
  890. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  891. struct extent_map *em = NULL;
  892. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  893. u64 end;
  894. read_lock(&em_tree->lock);
  895. em = lookup_extent_mapping(em_tree, offset, PAGE_SIZE);
  896. read_unlock(&em_tree->lock);
  897. if (em) {
  898. end = extent_map_end(em);
  899. free_extent_map(em);
  900. if (end - offset > thresh)
  901. return 0;
  902. }
  903. /* if we already have a nice delalloc here, just stop */
  904. thresh /= 2;
  905. end = count_range_bits(io_tree, &offset, offset + thresh,
  906. thresh, EXTENT_DELALLOC, 1);
  907. if (end >= thresh)
  908. return 0;
  909. return 1;
  910. }
  911. /*
  912. * helper function to walk through a file and find extents
  913. * newer than a specific transid, and smaller than thresh.
  914. *
  915. * This is used by the defragging code to find new and small
  916. * extents
  917. */
  918. static int find_new_extents(struct btrfs_root *root,
  919. struct inode *inode, u64 newer_than,
  920. u64 *off, u32 thresh)
  921. {
  922. struct btrfs_path *path;
  923. struct btrfs_key min_key;
  924. struct extent_buffer *leaf;
  925. struct btrfs_file_extent_item *extent;
  926. int type;
  927. int ret;
  928. u64 ino = btrfs_ino(BTRFS_I(inode));
  929. path = btrfs_alloc_path();
  930. if (!path)
  931. return -ENOMEM;
  932. min_key.objectid = ino;
  933. min_key.type = BTRFS_EXTENT_DATA_KEY;
  934. min_key.offset = *off;
  935. while (1) {
  936. ret = btrfs_search_forward(root, &min_key, path, newer_than);
  937. if (ret != 0)
  938. goto none;
  939. process_slot:
  940. if (min_key.objectid != ino)
  941. goto none;
  942. if (min_key.type != BTRFS_EXTENT_DATA_KEY)
  943. goto none;
  944. leaf = path->nodes[0];
  945. extent = btrfs_item_ptr(leaf, path->slots[0],
  946. struct btrfs_file_extent_item);
  947. type = btrfs_file_extent_type(leaf, extent);
  948. if (type == BTRFS_FILE_EXTENT_REG &&
  949. btrfs_file_extent_num_bytes(leaf, extent) < thresh &&
  950. check_defrag_in_cache(inode, min_key.offset, thresh)) {
  951. *off = min_key.offset;
  952. btrfs_free_path(path);
  953. return 0;
  954. }
  955. path->slots[0]++;
  956. if (path->slots[0] < btrfs_header_nritems(leaf)) {
  957. btrfs_item_key_to_cpu(leaf, &min_key, path->slots[0]);
  958. goto process_slot;
  959. }
  960. if (min_key.offset == (u64)-1)
  961. goto none;
  962. min_key.offset++;
  963. btrfs_release_path(path);
  964. }
  965. none:
  966. btrfs_free_path(path);
  967. return -ENOENT;
  968. }
  969. static struct extent_map *defrag_lookup_extent(struct inode *inode, u64 start)
  970. {
  971. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  972. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  973. struct extent_map *em;
  974. u64 len = PAGE_SIZE;
  975. /*
  976. * hopefully we have this extent in the tree already, try without
  977. * the full extent lock
  978. */
  979. read_lock(&em_tree->lock);
  980. em = lookup_extent_mapping(em_tree, start, len);
  981. read_unlock(&em_tree->lock);
  982. if (!em) {
  983. struct extent_state *cached = NULL;
  984. u64 end = start + len - 1;
  985. /* get the big lock and read metadata off disk */
  986. lock_extent_bits(io_tree, start, end, &cached);
  987. em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len, 0);
  988. unlock_extent_cached(io_tree, start, end, &cached);
  989. if (IS_ERR(em))
  990. return NULL;
  991. }
  992. return em;
  993. }
  994. static bool defrag_check_next_extent(struct inode *inode, struct extent_map *em)
  995. {
  996. struct extent_map *next;
  997. bool ret = true;
  998. /* this is the last extent */
  999. if (em->start + em->len >= i_size_read(inode))
  1000. return false;
  1001. next = defrag_lookup_extent(inode, em->start + em->len);
  1002. if (!next || next->block_start >= EXTENT_MAP_LAST_BYTE)
  1003. ret = false;
  1004. else if ((em->block_start + em->block_len == next->block_start) &&
  1005. (em->block_len > SZ_128K && next->block_len > SZ_128K))
  1006. ret = false;
  1007. free_extent_map(next);
  1008. return ret;
  1009. }
  1010. static int should_defrag_range(struct inode *inode, u64 start, u32 thresh,
  1011. u64 *last_len, u64 *skip, u64 *defrag_end,
  1012. int compress)
  1013. {
  1014. struct extent_map *em;
  1015. int ret = 1;
  1016. bool next_mergeable = true;
  1017. bool prev_mergeable = true;
  1018. /*
  1019. * make sure that once we start defragging an extent, we keep on
  1020. * defragging it
  1021. */
  1022. if (start < *defrag_end)
  1023. return 1;
  1024. *skip = 0;
  1025. em = defrag_lookup_extent(inode, start);
  1026. if (!em)
  1027. return 0;
  1028. /* this will cover holes, and inline extents */
  1029. if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
  1030. ret = 0;
  1031. goto out;
  1032. }
  1033. if (!*defrag_end)
  1034. prev_mergeable = false;
  1035. next_mergeable = defrag_check_next_extent(inode, em);
  1036. /*
  1037. * we hit a real extent, if it is big or the next extent is not a
  1038. * real extent, don't bother defragging it
  1039. */
  1040. if (!compress && (*last_len == 0 || *last_len >= thresh) &&
  1041. (em->len >= thresh || (!next_mergeable && !prev_mergeable)))
  1042. ret = 0;
  1043. out:
  1044. /*
  1045. * last_len ends up being a counter of how many bytes we've defragged.
  1046. * every time we choose not to defrag an extent, we reset *last_len
  1047. * so that the next tiny extent will force a defrag.
  1048. *
  1049. * The end result of this is that tiny extents before a single big
  1050. * extent will force at least part of that big extent to be defragged.
  1051. */
  1052. if (ret) {
  1053. *defrag_end = extent_map_end(em);
  1054. } else {
  1055. *last_len = 0;
  1056. *skip = extent_map_end(em);
  1057. *defrag_end = 0;
  1058. }
  1059. free_extent_map(em);
  1060. return ret;
  1061. }
  1062. /*
  1063. * it doesn't do much good to defrag one or two pages
  1064. * at a time. This pulls in a nice chunk of pages
  1065. * to COW and defrag.
  1066. *
  1067. * It also makes sure the delalloc code has enough
  1068. * dirty data to avoid making new small extents as part
  1069. * of the defrag
  1070. *
  1071. * It's a good idea to start RA on this range
  1072. * before calling this.
  1073. */
  1074. static int cluster_pages_for_defrag(struct inode *inode,
  1075. struct page **pages,
  1076. unsigned long start_index,
  1077. unsigned long num_pages)
  1078. {
  1079. unsigned long file_end;
  1080. u64 isize = i_size_read(inode);
  1081. u64 page_start;
  1082. u64 page_end;
  1083. u64 page_cnt;
  1084. u64 start = (u64)start_index << PAGE_SHIFT;
  1085. int ret;
  1086. int i;
  1087. int i_done;
  1088. struct btrfs_ordered_extent *ordered;
  1089. struct extent_state *cached_state = NULL;
  1090. struct extent_io_tree *tree;
  1091. struct extent_changeset *data_reserved = NULL;
  1092. gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
  1093. file_end = (isize - 1) >> PAGE_SHIFT;
  1094. if (!isize || start_index > file_end)
  1095. return 0;
  1096. page_cnt = min_t(u64, (u64)num_pages, (u64)file_end - start_index + 1);
  1097. ret = btrfs_delalloc_reserve_space(inode, &data_reserved,
  1098. start, page_cnt << PAGE_SHIFT);
  1099. if (ret)
  1100. return ret;
  1101. i_done = 0;
  1102. tree = &BTRFS_I(inode)->io_tree;
  1103. /* step one, lock all the pages */
  1104. for (i = 0; i < page_cnt; i++) {
  1105. struct page *page;
  1106. again:
  1107. page = find_or_create_page(inode->i_mapping,
  1108. start_index + i, mask);
  1109. if (!page)
  1110. break;
  1111. page_start = page_offset(page);
  1112. page_end = page_start + PAGE_SIZE - 1;
  1113. while (1) {
  1114. lock_extent_bits(tree, page_start, page_end,
  1115. &cached_state);
  1116. ordered = btrfs_lookup_ordered_extent(inode,
  1117. page_start);
  1118. unlock_extent_cached(tree, page_start, page_end,
  1119. &cached_state);
  1120. if (!ordered)
  1121. break;
  1122. unlock_page(page);
  1123. btrfs_start_ordered_extent(inode, ordered, 1);
  1124. btrfs_put_ordered_extent(ordered);
  1125. lock_page(page);
  1126. /*
  1127. * we unlocked the page above, so we need check if
  1128. * it was released or not.
  1129. */
  1130. if (page->mapping != inode->i_mapping) {
  1131. unlock_page(page);
  1132. put_page(page);
  1133. goto again;
  1134. }
  1135. }
  1136. if (!PageUptodate(page)) {
  1137. btrfs_readpage(NULL, page);
  1138. lock_page(page);
  1139. if (!PageUptodate(page)) {
  1140. unlock_page(page);
  1141. put_page(page);
  1142. ret = -EIO;
  1143. break;
  1144. }
  1145. }
  1146. if (page->mapping != inode->i_mapping) {
  1147. unlock_page(page);
  1148. put_page(page);
  1149. goto again;
  1150. }
  1151. pages[i] = page;
  1152. i_done++;
  1153. }
  1154. if (!i_done || ret)
  1155. goto out;
  1156. if (!(inode->i_sb->s_flags & SB_ACTIVE))
  1157. goto out;
  1158. /*
  1159. * so now we have a nice long stream of locked
  1160. * and up to date pages, lets wait on them
  1161. */
  1162. for (i = 0; i < i_done; i++)
  1163. wait_on_page_writeback(pages[i]);
  1164. page_start = page_offset(pages[0]);
  1165. page_end = page_offset(pages[i_done - 1]) + PAGE_SIZE;
  1166. lock_extent_bits(&BTRFS_I(inode)->io_tree,
  1167. page_start, page_end - 1, &cached_state);
  1168. clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start,
  1169. page_end - 1, EXTENT_DIRTY | EXTENT_DELALLOC |
  1170. EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 0, 0,
  1171. &cached_state);
  1172. if (i_done != page_cnt) {
  1173. spin_lock(&BTRFS_I(inode)->lock);
  1174. btrfs_mod_outstanding_extents(BTRFS_I(inode), 1);
  1175. spin_unlock(&BTRFS_I(inode)->lock);
  1176. btrfs_delalloc_release_space(inode, data_reserved,
  1177. start, (page_cnt - i_done) << PAGE_SHIFT, true);
  1178. }
  1179. set_extent_defrag(&BTRFS_I(inode)->io_tree, page_start, page_end - 1,
  1180. &cached_state);
  1181. unlock_extent_cached(&BTRFS_I(inode)->io_tree,
  1182. page_start, page_end - 1, &cached_state);
  1183. for (i = 0; i < i_done; i++) {
  1184. clear_page_dirty_for_io(pages[i]);
  1185. ClearPageChecked(pages[i]);
  1186. set_page_extent_mapped(pages[i]);
  1187. set_page_dirty(pages[i]);
  1188. unlock_page(pages[i]);
  1189. put_page(pages[i]);
  1190. }
  1191. btrfs_delalloc_release_extents(BTRFS_I(inode), page_cnt << PAGE_SHIFT);
  1192. extent_changeset_free(data_reserved);
  1193. return i_done;
  1194. out:
  1195. for (i = 0; i < i_done; i++) {
  1196. unlock_page(pages[i]);
  1197. put_page(pages[i]);
  1198. }
  1199. btrfs_delalloc_release_space(inode, data_reserved,
  1200. start, page_cnt << PAGE_SHIFT, true);
  1201. btrfs_delalloc_release_extents(BTRFS_I(inode), page_cnt << PAGE_SHIFT);
  1202. extent_changeset_free(data_reserved);
  1203. return ret;
  1204. }
  1205. int btrfs_defrag_file(struct inode *inode, struct file *file,
  1206. struct btrfs_ioctl_defrag_range_args *range,
  1207. u64 newer_than, unsigned long max_to_defrag)
  1208. {
  1209. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  1210. struct btrfs_root *root = BTRFS_I(inode)->root;
  1211. struct file_ra_state *ra = NULL;
  1212. unsigned long last_index;
  1213. u64 isize = i_size_read(inode);
  1214. u64 last_len = 0;
  1215. u64 skip = 0;
  1216. u64 defrag_end = 0;
  1217. u64 newer_off = range->start;
  1218. unsigned long i;
  1219. unsigned long ra_index = 0;
  1220. int ret;
  1221. int defrag_count = 0;
  1222. int compress_type = BTRFS_COMPRESS_ZLIB;
  1223. u32 extent_thresh = range->extent_thresh;
  1224. unsigned long max_cluster = SZ_256K >> PAGE_SHIFT;
  1225. unsigned long cluster = max_cluster;
  1226. u64 new_align = ~((u64)SZ_128K - 1);
  1227. struct page **pages = NULL;
  1228. bool do_compress = range->flags & BTRFS_DEFRAG_RANGE_COMPRESS;
  1229. if (isize == 0)
  1230. return 0;
  1231. if (range->start >= isize)
  1232. return -EINVAL;
  1233. if (do_compress) {
  1234. if (range->compress_type > BTRFS_COMPRESS_TYPES)
  1235. return -EINVAL;
  1236. if (range->compress_type)
  1237. compress_type = range->compress_type;
  1238. }
  1239. if (extent_thresh == 0)
  1240. extent_thresh = SZ_256K;
  1241. /*
  1242. * If we were not given a file, allocate a readahead context. As
  1243. * readahead is just an optimization, defrag will work without it so
  1244. * we don't error out.
  1245. */
  1246. if (!file) {
  1247. ra = kzalloc(sizeof(*ra), GFP_KERNEL);
  1248. if (ra)
  1249. file_ra_state_init(ra, inode->i_mapping);
  1250. } else {
  1251. ra = &file->f_ra;
  1252. }
  1253. pages = kmalloc_array(max_cluster, sizeof(struct page *), GFP_KERNEL);
  1254. if (!pages) {
  1255. ret = -ENOMEM;
  1256. goto out_ra;
  1257. }
  1258. /* find the last page to defrag */
  1259. if (range->start + range->len > range->start) {
  1260. last_index = min_t(u64, isize - 1,
  1261. range->start + range->len - 1) >> PAGE_SHIFT;
  1262. } else {
  1263. last_index = (isize - 1) >> PAGE_SHIFT;
  1264. }
  1265. if (newer_than) {
  1266. ret = find_new_extents(root, inode, newer_than,
  1267. &newer_off, SZ_64K);
  1268. if (!ret) {
  1269. range->start = newer_off;
  1270. /*
  1271. * we always align our defrag to help keep
  1272. * the extents in the file evenly spaced
  1273. */
  1274. i = (newer_off & new_align) >> PAGE_SHIFT;
  1275. } else
  1276. goto out_ra;
  1277. } else {
  1278. i = range->start >> PAGE_SHIFT;
  1279. }
  1280. if (!max_to_defrag)
  1281. max_to_defrag = last_index - i + 1;
  1282. /*
  1283. * make writeback starts from i, so the defrag range can be
  1284. * written sequentially.
  1285. */
  1286. if (i < inode->i_mapping->writeback_index)
  1287. inode->i_mapping->writeback_index = i;
  1288. while (i <= last_index && defrag_count < max_to_defrag &&
  1289. (i < DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE))) {
  1290. /*
  1291. * make sure we stop running if someone unmounts
  1292. * the FS
  1293. */
  1294. if (!(inode->i_sb->s_flags & SB_ACTIVE))
  1295. break;
  1296. if (btrfs_defrag_cancelled(fs_info)) {
  1297. btrfs_debug(fs_info, "defrag_file cancelled");
  1298. ret = -EAGAIN;
  1299. break;
  1300. }
  1301. if (!should_defrag_range(inode, (u64)i << PAGE_SHIFT,
  1302. extent_thresh, &last_len, &skip,
  1303. &defrag_end, do_compress)){
  1304. unsigned long next;
  1305. /*
  1306. * the should_defrag function tells us how much to skip
  1307. * bump our counter by the suggested amount
  1308. */
  1309. next = DIV_ROUND_UP(skip, PAGE_SIZE);
  1310. i = max(i + 1, next);
  1311. continue;
  1312. }
  1313. if (!newer_than) {
  1314. cluster = (PAGE_ALIGN(defrag_end) >>
  1315. PAGE_SHIFT) - i;
  1316. cluster = min(cluster, max_cluster);
  1317. } else {
  1318. cluster = max_cluster;
  1319. }
  1320. if (i + cluster > ra_index) {
  1321. ra_index = max(i, ra_index);
  1322. if (ra)
  1323. page_cache_sync_readahead(inode->i_mapping, ra,
  1324. file, ra_index, cluster);
  1325. ra_index += cluster;
  1326. }
  1327. inode_lock(inode);
  1328. if (do_compress)
  1329. BTRFS_I(inode)->defrag_compress = compress_type;
  1330. ret = cluster_pages_for_defrag(inode, pages, i, cluster);
  1331. if (ret < 0) {
  1332. inode_unlock(inode);
  1333. goto out_ra;
  1334. }
  1335. defrag_count += ret;
  1336. balance_dirty_pages_ratelimited(inode->i_mapping);
  1337. inode_unlock(inode);
  1338. if (newer_than) {
  1339. if (newer_off == (u64)-1)
  1340. break;
  1341. if (ret > 0)
  1342. i += ret;
  1343. newer_off = max(newer_off + 1,
  1344. (u64)i << PAGE_SHIFT);
  1345. ret = find_new_extents(root, inode, newer_than,
  1346. &newer_off, SZ_64K);
  1347. if (!ret) {
  1348. range->start = newer_off;
  1349. i = (newer_off & new_align) >> PAGE_SHIFT;
  1350. } else {
  1351. break;
  1352. }
  1353. } else {
  1354. if (ret > 0) {
  1355. i += ret;
  1356. last_len += ret << PAGE_SHIFT;
  1357. } else {
  1358. i++;
  1359. last_len = 0;
  1360. }
  1361. }
  1362. }
  1363. if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO)) {
  1364. filemap_flush(inode->i_mapping);
  1365. if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
  1366. &BTRFS_I(inode)->runtime_flags))
  1367. filemap_flush(inode->i_mapping);
  1368. }
  1369. if (range->compress_type == BTRFS_COMPRESS_LZO) {
  1370. btrfs_set_fs_incompat(fs_info, COMPRESS_LZO);
  1371. } else if (range->compress_type == BTRFS_COMPRESS_ZSTD) {
  1372. btrfs_set_fs_incompat(fs_info, COMPRESS_ZSTD);
  1373. }
  1374. ret = defrag_count;
  1375. out_ra:
  1376. if (do_compress) {
  1377. inode_lock(inode);
  1378. BTRFS_I(inode)->defrag_compress = BTRFS_COMPRESS_NONE;
  1379. inode_unlock(inode);
  1380. }
  1381. if (!file)
  1382. kfree(ra);
  1383. kfree(pages);
  1384. return ret;
  1385. }
  1386. static noinline int btrfs_ioctl_resize(struct file *file,
  1387. void __user *arg)
  1388. {
  1389. struct inode *inode = file_inode(file);
  1390. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  1391. u64 new_size;
  1392. u64 old_size;
  1393. u64 devid = 1;
  1394. struct btrfs_root *root = BTRFS_I(inode)->root;
  1395. struct btrfs_ioctl_vol_args *vol_args;
  1396. struct btrfs_trans_handle *trans;
  1397. struct btrfs_device *device = NULL;
  1398. char *sizestr;
  1399. char *retptr;
  1400. char *devstr = NULL;
  1401. int ret = 0;
  1402. int mod = 0;
  1403. if (!capable(CAP_SYS_ADMIN))
  1404. return -EPERM;
  1405. ret = mnt_want_write_file(file);
  1406. if (ret)
  1407. return ret;
  1408. if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
  1409. mnt_drop_write_file(file);
  1410. return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
  1411. }
  1412. vol_args = memdup_user(arg, sizeof(*vol_args));
  1413. if (IS_ERR(vol_args)) {
  1414. ret = PTR_ERR(vol_args);
  1415. goto out;
  1416. }
  1417. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  1418. sizestr = vol_args->name;
  1419. devstr = strchr(sizestr, ':');
  1420. if (devstr) {
  1421. sizestr = devstr + 1;
  1422. *devstr = '\0';
  1423. devstr = vol_args->name;
  1424. ret = kstrtoull(devstr, 10, &devid);
  1425. if (ret)
  1426. goto out_free;
  1427. if (!devid) {
  1428. ret = -EINVAL;
  1429. goto out_free;
  1430. }
  1431. btrfs_info(fs_info, "resizing devid %llu", devid);
  1432. }
  1433. device = btrfs_find_device(fs_info->fs_devices, devid, NULL, NULL, true);
  1434. if (!device) {
  1435. btrfs_info(fs_info, "resizer unable to find device %llu",
  1436. devid);
  1437. ret = -ENODEV;
  1438. goto out_free;
  1439. }
  1440. if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
  1441. btrfs_info(fs_info,
  1442. "resizer unable to apply on readonly device %llu",
  1443. devid);
  1444. ret = -EPERM;
  1445. goto out_free;
  1446. }
  1447. if (!strcmp(sizestr, "max"))
  1448. new_size = device->bdev->bd_inode->i_size;
  1449. else {
  1450. if (sizestr[0] == '-') {
  1451. mod = -1;
  1452. sizestr++;
  1453. } else if (sizestr[0] == '+') {
  1454. mod = 1;
  1455. sizestr++;
  1456. }
  1457. new_size = memparse(sizestr, &retptr);
  1458. if (*retptr != '\0' || new_size == 0) {
  1459. ret = -EINVAL;
  1460. goto out_free;
  1461. }
  1462. }
  1463. if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
  1464. ret = -EPERM;
  1465. goto out_free;
  1466. }
  1467. old_size = btrfs_device_get_total_bytes(device);
  1468. if (mod < 0) {
  1469. if (new_size > old_size) {
  1470. ret = -EINVAL;
  1471. goto out_free;
  1472. }
  1473. new_size = old_size - new_size;
  1474. } else if (mod > 0) {
  1475. if (new_size > ULLONG_MAX - old_size) {
  1476. ret = -ERANGE;
  1477. goto out_free;
  1478. }
  1479. new_size = old_size + new_size;
  1480. }
  1481. if (new_size < SZ_256M) {
  1482. ret = -EINVAL;
  1483. goto out_free;
  1484. }
  1485. if (new_size > device->bdev->bd_inode->i_size) {
  1486. ret = -EFBIG;
  1487. goto out_free;
  1488. }
  1489. new_size = round_down(new_size, fs_info->sectorsize);
  1490. btrfs_info_in_rcu(fs_info, "new size for %s is %llu",
  1491. rcu_str_deref(device->name), new_size);
  1492. if (new_size > old_size) {
  1493. trans = btrfs_start_transaction(root, 0);
  1494. if (IS_ERR(trans)) {
  1495. ret = PTR_ERR(trans);
  1496. goto out_free;
  1497. }
  1498. ret = btrfs_grow_device(trans, device, new_size);
  1499. btrfs_commit_transaction(trans);
  1500. } else if (new_size < old_size) {
  1501. ret = btrfs_shrink_device(device, new_size);
  1502. } /* equal, nothing need to do */
  1503. out_free:
  1504. kfree(vol_args);
  1505. out:
  1506. clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
  1507. mnt_drop_write_file(file);
  1508. return ret;
  1509. }
  1510. static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
  1511. const char *name, unsigned long fd, int subvol,
  1512. u64 *transid, bool readonly,
  1513. struct btrfs_qgroup_inherit *inherit)
  1514. {
  1515. int namelen;
  1516. int ret = 0;
  1517. if (!S_ISDIR(file_inode(file)->i_mode))
  1518. return -ENOTDIR;
  1519. ret = mnt_want_write_file(file);
  1520. if (ret)
  1521. goto out;
  1522. namelen = strlen(name);
  1523. if (strchr(name, '/')) {
  1524. ret = -EINVAL;
  1525. goto out_drop_write;
  1526. }
  1527. if (name[0] == '.' &&
  1528. (namelen == 1 || (name[1] == '.' && namelen == 2))) {
  1529. ret = -EEXIST;
  1530. goto out_drop_write;
  1531. }
  1532. if (subvol) {
  1533. ret = btrfs_mksubvol(&file->f_path, name, namelen,
  1534. NULL, transid, readonly, inherit);
  1535. } else {
  1536. struct fd src = fdget(fd);
  1537. struct inode *src_inode;
  1538. if (!src.file) {
  1539. ret = -EINVAL;
  1540. goto out_drop_write;
  1541. }
  1542. src_inode = file_inode(src.file);
  1543. if (src_inode->i_sb != file_inode(file)->i_sb) {
  1544. btrfs_info(BTRFS_I(file_inode(file))->root->fs_info,
  1545. "Snapshot src from another FS");
  1546. ret = -EXDEV;
  1547. } else if (!inode_owner_or_capable(src_inode)) {
  1548. /*
  1549. * Subvolume creation is not restricted, but snapshots
  1550. * are limited to own subvolumes only
  1551. */
  1552. ret = -EPERM;
  1553. } else {
  1554. ret = btrfs_mksubvol(&file->f_path, name, namelen,
  1555. BTRFS_I(src_inode)->root,
  1556. transid, readonly, inherit);
  1557. }
  1558. fdput(src);
  1559. }
  1560. out_drop_write:
  1561. mnt_drop_write_file(file);
  1562. out:
  1563. return ret;
  1564. }
  1565. static noinline int btrfs_ioctl_snap_create(struct file *file,
  1566. void __user *arg, int subvol)
  1567. {
  1568. struct btrfs_ioctl_vol_args *vol_args;
  1569. int ret;
  1570. if (!S_ISDIR(file_inode(file)->i_mode))
  1571. return -ENOTDIR;
  1572. vol_args = memdup_user(arg, sizeof(*vol_args));
  1573. if (IS_ERR(vol_args))
  1574. return PTR_ERR(vol_args);
  1575. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  1576. ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
  1577. vol_args->fd, subvol,
  1578. NULL, false, NULL);
  1579. kfree(vol_args);
  1580. return ret;
  1581. }
  1582. static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
  1583. void __user *arg, int subvol)
  1584. {
  1585. struct btrfs_ioctl_vol_args_v2 *vol_args;
  1586. int ret;
  1587. u64 transid = 0;
  1588. u64 *ptr = NULL;
  1589. bool readonly = false;
  1590. struct btrfs_qgroup_inherit *inherit = NULL;
  1591. if (!S_ISDIR(file_inode(file)->i_mode))
  1592. return -ENOTDIR;
  1593. vol_args = memdup_user(arg, sizeof(*vol_args));
  1594. if (IS_ERR(vol_args))
  1595. return PTR_ERR(vol_args);
  1596. vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
  1597. if (vol_args->flags &
  1598. ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY |
  1599. BTRFS_SUBVOL_QGROUP_INHERIT)) {
  1600. ret = -EOPNOTSUPP;
  1601. goto free_args;
  1602. }
  1603. if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC)
  1604. ptr = &transid;
  1605. if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
  1606. readonly = true;
  1607. if (vol_args->flags & BTRFS_SUBVOL_QGROUP_INHERIT) {
  1608. u64 nums;
  1609. if (vol_args->size < sizeof(*inherit) ||
  1610. vol_args->size > PAGE_SIZE) {
  1611. ret = -EINVAL;
  1612. goto free_args;
  1613. }
  1614. inherit = memdup_user(vol_args->qgroup_inherit, vol_args->size);
  1615. if (IS_ERR(inherit)) {
  1616. ret = PTR_ERR(inherit);
  1617. goto free_args;
  1618. }
  1619. if (inherit->num_qgroups > PAGE_SIZE ||
  1620. inherit->num_ref_copies > PAGE_SIZE ||
  1621. inherit->num_excl_copies > PAGE_SIZE) {
  1622. ret = -EINVAL;
  1623. goto free_inherit;
  1624. }
  1625. nums = inherit->num_qgroups + 2 * inherit->num_ref_copies +
  1626. 2 * inherit->num_excl_copies;
  1627. if (vol_args->size != struct_size(inherit, qgroups, nums)) {
  1628. ret = -EINVAL;
  1629. goto free_inherit;
  1630. }
  1631. }
  1632. ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
  1633. vol_args->fd, subvol, ptr,
  1634. readonly, inherit);
  1635. if (ret)
  1636. goto free_inherit;
  1637. if (ptr && copy_to_user(arg +
  1638. offsetof(struct btrfs_ioctl_vol_args_v2,
  1639. transid),
  1640. ptr, sizeof(*ptr)))
  1641. ret = -EFAULT;
  1642. free_inherit:
  1643. kfree(inherit);
  1644. free_args:
  1645. kfree(vol_args);
  1646. return ret;
  1647. }
  1648. static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
  1649. void __user *arg)
  1650. {
  1651. struct inode *inode = file_inode(file);
  1652. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  1653. struct btrfs_root *root = BTRFS_I(inode)->root;
  1654. int ret = 0;
  1655. u64 flags = 0;
  1656. if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID)
  1657. return -EINVAL;
  1658. down_read(&fs_info->subvol_sem);
  1659. if (btrfs_root_readonly(root))
  1660. flags |= BTRFS_SUBVOL_RDONLY;
  1661. up_read(&fs_info->subvol_sem);
  1662. if (copy_to_user(arg, &flags, sizeof(flags)))
  1663. ret = -EFAULT;
  1664. return ret;
  1665. }
  1666. static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
  1667. void __user *arg)
  1668. {
  1669. struct inode *inode = file_inode(file);
  1670. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  1671. struct btrfs_root *root = BTRFS_I(inode)->root;
  1672. struct btrfs_trans_handle *trans;
  1673. u64 root_flags;
  1674. u64 flags;
  1675. int ret = 0;
  1676. if (!inode_owner_or_capable(inode))
  1677. return -EPERM;
  1678. ret = mnt_want_write_file(file);
  1679. if (ret)
  1680. goto out;
  1681. if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) {
  1682. ret = -EINVAL;
  1683. goto out_drop_write;
  1684. }
  1685. if (copy_from_user(&flags, arg, sizeof(flags))) {
  1686. ret = -EFAULT;
  1687. goto out_drop_write;
  1688. }
  1689. if (flags & BTRFS_SUBVOL_CREATE_ASYNC) {
  1690. ret = -EINVAL;
  1691. goto out_drop_write;
  1692. }
  1693. if (flags & ~BTRFS_SUBVOL_RDONLY) {
  1694. ret = -EOPNOTSUPP;
  1695. goto out_drop_write;
  1696. }
  1697. down_write(&fs_info->subvol_sem);
  1698. /* nothing to do */
  1699. if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
  1700. goto out_drop_sem;
  1701. root_flags = btrfs_root_flags(&root->root_item);
  1702. if (flags & BTRFS_SUBVOL_RDONLY) {
  1703. btrfs_set_root_flags(&root->root_item,
  1704. root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
  1705. } else {
  1706. /*
  1707. * Block RO -> RW transition if this subvolume is involved in
  1708. * send
  1709. */
  1710. spin_lock(&root->root_item_lock);
  1711. if (root->send_in_progress == 0) {
  1712. btrfs_set_root_flags(&root->root_item,
  1713. root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
  1714. spin_unlock(&root->root_item_lock);
  1715. } else {
  1716. spin_unlock(&root->root_item_lock);
  1717. btrfs_warn(fs_info,
  1718. "Attempt to set subvolume %llu read-write during send",
  1719. root->root_key.objectid);
  1720. ret = -EPERM;
  1721. goto out_drop_sem;
  1722. }
  1723. }
  1724. trans = btrfs_start_transaction(root, 1);
  1725. if (IS_ERR(trans)) {
  1726. ret = PTR_ERR(trans);
  1727. goto out_reset;
  1728. }
  1729. ret = btrfs_update_root(trans, fs_info->tree_root,
  1730. &root->root_key, &root->root_item);
  1731. if (ret < 0) {
  1732. btrfs_end_transaction(trans);
  1733. goto out_reset;
  1734. }
  1735. ret = btrfs_commit_transaction(trans);
  1736. out_reset:
  1737. if (ret)
  1738. btrfs_set_root_flags(&root->root_item, root_flags);
  1739. out_drop_sem:
  1740. up_write(&fs_info->subvol_sem);
  1741. out_drop_write:
  1742. mnt_drop_write_file(file);
  1743. out:
  1744. return ret;
  1745. }
  1746. static noinline int key_in_sk(struct btrfs_key *key,
  1747. struct btrfs_ioctl_search_key *sk)
  1748. {
  1749. struct btrfs_key test;
  1750. int ret;
  1751. test.objectid = sk->min_objectid;
  1752. test.type = sk->min_type;
  1753. test.offset = sk->min_offset;
  1754. ret = btrfs_comp_cpu_keys(key, &test);
  1755. if (ret < 0)
  1756. return 0;
  1757. test.objectid = sk->max_objectid;
  1758. test.type = sk->max_type;
  1759. test.offset = sk->max_offset;
  1760. ret = btrfs_comp_cpu_keys(key, &test);
  1761. if (ret > 0)
  1762. return 0;
  1763. return 1;
  1764. }
  1765. static noinline int copy_to_sk(struct btrfs_path *path,
  1766. struct btrfs_key *key,
  1767. struct btrfs_ioctl_search_key *sk,
  1768. size_t *buf_size,
  1769. char __user *ubuf,
  1770. unsigned long *sk_offset,
  1771. int *num_found)
  1772. {
  1773. u64 found_transid;
  1774. struct extent_buffer *leaf;
  1775. struct btrfs_ioctl_search_header sh;
  1776. struct btrfs_key test;
  1777. unsigned long item_off;
  1778. unsigned long item_len;
  1779. int nritems;
  1780. int i;
  1781. int slot;
  1782. int ret = 0;
  1783. leaf = path->nodes[0];
  1784. slot = path->slots[0];
  1785. nritems = btrfs_header_nritems(leaf);
  1786. if (btrfs_header_generation(leaf) > sk->max_transid) {
  1787. i = nritems;
  1788. goto advance_key;
  1789. }
  1790. found_transid = btrfs_header_generation(leaf);
  1791. for (i = slot; i < nritems; i++) {
  1792. item_off = btrfs_item_ptr_offset(leaf, i);
  1793. item_len = btrfs_item_size_nr(leaf, i);
  1794. btrfs_item_key_to_cpu(leaf, key, i);
  1795. if (!key_in_sk(key, sk))
  1796. continue;
  1797. if (sizeof(sh) + item_len > *buf_size) {
  1798. if (*num_found) {
  1799. ret = 1;
  1800. goto out;
  1801. }
  1802. /*
  1803. * return one empty item back for v1, which does not
  1804. * handle -EOVERFLOW
  1805. */
  1806. *buf_size = sizeof(sh) + item_len;
  1807. item_len = 0;
  1808. ret = -EOVERFLOW;
  1809. }
  1810. if (sizeof(sh) + item_len + *sk_offset > *buf_size) {
  1811. ret = 1;
  1812. goto out;
  1813. }
  1814. sh.objectid = key->objectid;
  1815. sh.offset = key->offset;
  1816. sh.type = key->type;
  1817. sh.len = item_len;
  1818. sh.transid = found_transid;
  1819. /*
  1820. * Copy search result header. If we fault then loop again so we
  1821. * can fault in the pages and -EFAULT there if there's a
  1822. * problem. Otherwise we'll fault and then copy the buffer in
  1823. * properly this next time through
  1824. */
  1825. if (probe_user_write(ubuf + *sk_offset, &sh, sizeof(sh))) {
  1826. ret = 0;
  1827. goto out;
  1828. }
  1829. *sk_offset += sizeof(sh);
  1830. if (item_len) {
  1831. char __user *up = ubuf + *sk_offset;
  1832. /*
  1833. * Copy the item, same behavior as above, but reset the
  1834. * * sk_offset so we copy the full thing again.
  1835. */
  1836. if (read_extent_buffer_to_user_nofault(leaf, up,
  1837. item_off, item_len)) {
  1838. ret = 0;
  1839. *sk_offset -= sizeof(sh);
  1840. goto out;
  1841. }
  1842. *sk_offset += item_len;
  1843. }
  1844. (*num_found)++;
  1845. if (ret) /* -EOVERFLOW from above */
  1846. goto out;
  1847. if (*num_found >= sk->nr_items) {
  1848. ret = 1;
  1849. goto out;
  1850. }
  1851. }
  1852. advance_key:
  1853. ret = 0;
  1854. test.objectid = sk->max_objectid;
  1855. test.type = sk->max_type;
  1856. test.offset = sk->max_offset;
  1857. if (btrfs_comp_cpu_keys(key, &test) >= 0)
  1858. ret = 1;
  1859. else if (key->offset < (u64)-1)
  1860. key->offset++;
  1861. else if (key->type < (u8)-1) {
  1862. key->offset = 0;
  1863. key->type++;
  1864. } else if (key->objectid < (u64)-1) {
  1865. key->offset = 0;
  1866. key->type = 0;
  1867. key->objectid++;
  1868. } else
  1869. ret = 1;
  1870. out:
  1871. /*
  1872. * 0: all items from this leaf copied, continue with next
  1873. * 1: * more items can be copied, but unused buffer is too small
  1874. * * all items were found
  1875. * Either way, it will stops the loop which iterates to the next
  1876. * leaf
  1877. * -EOVERFLOW: item was to large for buffer
  1878. * -EFAULT: could not copy extent buffer back to userspace
  1879. */
  1880. return ret;
  1881. }
  1882. static noinline int search_ioctl(struct inode *inode,
  1883. struct btrfs_ioctl_search_key *sk,
  1884. size_t *buf_size,
  1885. char __user *ubuf)
  1886. {
  1887. struct btrfs_fs_info *info = btrfs_sb(inode->i_sb);
  1888. struct btrfs_root *root;
  1889. struct btrfs_key key;
  1890. struct btrfs_path *path;
  1891. int ret;
  1892. int num_found = 0;
  1893. unsigned long sk_offset = 0;
  1894. if (*buf_size < sizeof(struct btrfs_ioctl_search_header)) {
  1895. *buf_size = sizeof(struct btrfs_ioctl_search_header);
  1896. return -EOVERFLOW;
  1897. }
  1898. path = btrfs_alloc_path();
  1899. if (!path)
  1900. return -ENOMEM;
  1901. if (sk->tree_id == 0) {
  1902. /* search the root of the inode that was passed */
  1903. root = BTRFS_I(inode)->root;
  1904. } else {
  1905. key.objectid = sk->tree_id;
  1906. key.type = BTRFS_ROOT_ITEM_KEY;
  1907. key.offset = (u64)-1;
  1908. root = btrfs_read_fs_root_no_name(info, &key);
  1909. if (IS_ERR(root)) {
  1910. btrfs_free_path(path);
  1911. return PTR_ERR(root);
  1912. }
  1913. }
  1914. key.objectid = sk->min_objectid;
  1915. key.type = sk->min_type;
  1916. key.offset = sk->min_offset;
  1917. while (1) {
  1918. ret = fault_in_pages_writeable(ubuf + sk_offset,
  1919. *buf_size - sk_offset);
  1920. if (ret)
  1921. break;
  1922. ret = btrfs_search_forward(root, &key, path, sk->min_transid);
  1923. if (ret != 0) {
  1924. if (ret > 0)
  1925. ret = 0;
  1926. goto err;
  1927. }
  1928. ret = copy_to_sk(path, &key, sk, buf_size, ubuf,
  1929. &sk_offset, &num_found);
  1930. btrfs_release_path(path);
  1931. if (ret)
  1932. break;
  1933. }
  1934. if (ret > 0)
  1935. ret = 0;
  1936. err:
  1937. sk->nr_items = num_found;
  1938. btrfs_free_path(path);
  1939. return ret;
  1940. }
  1941. static noinline int btrfs_ioctl_tree_search(struct file *file,
  1942. void __user *argp)
  1943. {
  1944. struct btrfs_ioctl_search_args __user *uargs;
  1945. struct btrfs_ioctl_search_key sk;
  1946. struct inode *inode;
  1947. int ret;
  1948. size_t buf_size;
  1949. if (!capable(CAP_SYS_ADMIN))
  1950. return -EPERM;
  1951. uargs = (struct btrfs_ioctl_search_args __user *)argp;
  1952. if (copy_from_user(&sk, &uargs->key, sizeof(sk)))
  1953. return -EFAULT;
  1954. buf_size = sizeof(uargs->buf);
  1955. inode = file_inode(file);
  1956. ret = search_ioctl(inode, &sk, &buf_size, uargs->buf);
  1957. /*
  1958. * In the origin implementation an overflow is handled by returning a
  1959. * search header with a len of zero, so reset ret.
  1960. */
  1961. if (ret == -EOVERFLOW)
  1962. ret = 0;
  1963. if (ret == 0 && copy_to_user(&uargs->key, &sk, sizeof(sk)))
  1964. ret = -EFAULT;
  1965. return ret;
  1966. }
  1967. static noinline int btrfs_ioctl_tree_search_v2(struct file *file,
  1968. void __user *argp)
  1969. {
  1970. struct btrfs_ioctl_search_args_v2 __user *uarg;
  1971. struct btrfs_ioctl_search_args_v2 args;
  1972. struct inode *inode;
  1973. int ret;
  1974. size_t buf_size;
  1975. const size_t buf_limit = SZ_16M;
  1976. if (!capable(CAP_SYS_ADMIN))
  1977. return -EPERM;
  1978. /* copy search header and buffer size */
  1979. uarg = (struct btrfs_ioctl_search_args_v2 __user *)argp;
  1980. if (copy_from_user(&args, uarg, sizeof(args)))
  1981. return -EFAULT;
  1982. buf_size = args.buf_size;
  1983. /* limit result size to 16MB */
  1984. if (buf_size > buf_limit)
  1985. buf_size = buf_limit;
  1986. inode = file_inode(file);
  1987. ret = search_ioctl(inode, &args.key, &buf_size,
  1988. (char __user *)(&uarg->buf[0]));
  1989. if (ret == 0 && copy_to_user(&uarg->key, &args.key, sizeof(args.key)))
  1990. ret = -EFAULT;
  1991. else if (ret == -EOVERFLOW &&
  1992. copy_to_user(&uarg->buf_size, &buf_size, sizeof(buf_size)))
  1993. ret = -EFAULT;
  1994. return ret;
  1995. }
  1996. /*
  1997. * Search INODE_REFs to identify path name of 'dirid' directory
  1998. * in a 'tree_id' tree. and sets path name to 'name'.
  1999. */
  2000. static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
  2001. u64 tree_id, u64 dirid, char *name)
  2002. {
  2003. struct btrfs_root *root;
  2004. struct btrfs_key key;
  2005. char *ptr;
  2006. int ret = -1;
  2007. int slot;
  2008. int len;
  2009. int total_len = 0;
  2010. struct btrfs_inode_ref *iref;
  2011. struct extent_buffer *l;
  2012. struct btrfs_path *path;
  2013. if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
  2014. name[0]='\0';
  2015. return 0;
  2016. }
  2017. path = btrfs_alloc_path();
  2018. if (!path)
  2019. return -ENOMEM;
  2020. ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX - 1];
  2021. key.objectid = tree_id;
  2022. key.type = BTRFS_ROOT_ITEM_KEY;
  2023. key.offset = (u64)-1;
  2024. root = btrfs_read_fs_root_no_name(info, &key);
  2025. if (IS_ERR(root)) {
  2026. ret = PTR_ERR(root);
  2027. goto out;
  2028. }
  2029. key.objectid = dirid;
  2030. key.type = BTRFS_INODE_REF_KEY;
  2031. key.offset = (u64)-1;
  2032. while (1) {
  2033. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  2034. if (ret < 0)
  2035. goto out;
  2036. else if (ret > 0) {
  2037. ret = btrfs_previous_item(root, path, dirid,
  2038. BTRFS_INODE_REF_KEY);
  2039. if (ret < 0)
  2040. goto out;
  2041. else if (ret > 0) {
  2042. ret = -ENOENT;
  2043. goto out;
  2044. }
  2045. }
  2046. l = path->nodes[0];
  2047. slot = path->slots[0];
  2048. btrfs_item_key_to_cpu(l, &key, slot);
  2049. iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
  2050. len = btrfs_inode_ref_name_len(l, iref);
  2051. ptr -= len + 1;
  2052. total_len += len + 1;
  2053. if (ptr < name) {
  2054. ret = -ENAMETOOLONG;
  2055. goto out;
  2056. }
  2057. *(ptr + len) = '/';
  2058. read_extent_buffer(l, ptr, (unsigned long)(iref + 1), len);
  2059. if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
  2060. break;
  2061. btrfs_release_path(path);
  2062. key.objectid = key.offset;
  2063. key.offset = (u64)-1;
  2064. dirid = key.objectid;
  2065. }
  2066. memmove(name, ptr, total_len);
  2067. name[total_len] = '\0';
  2068. ret = 0;
  2069. out:
  2070. btrfs_free_path(path);
  2071. return ret;
  2072. }
  2073. static int btrfs_search_path_in_tree_user(struct inode *inode,
  2074. struct btrfs_ioctl_ino_lookup_user_args *args)
  2075. {
  2076. struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
  2077. struct super_block *sb = inode->i_sb;
  2078. struct btrfs_key upper_limit = BTRFS_I(inode)->location;
  2079. u64 treeid = BTRFS_I(inode)->root->root_key.objectid;
  2080. u64 dirid = args->dirid;
  2081. unsigned long item_off;
  2082. unsigned long item_len;
  2083. struct btrfs_inode_ref *iref;
  2084. struct btrfs_root_ref *rref;
  2085. struct btrfs_root *root;
  2086. struct btrfs_path *path;
  2087. struct btrfs_key key, key2;
  2088. struct extent_buffer *leaf;
  2089. struct inode *temp_inode;
  2090. char *ptr;
  2091. int slot;
  2092. int len;
  2093. int total_len = 0;
  2094. int ret;
  2095. path = btrfs_alloc_path();
  2096. if (!path)
  2097. return -ENOMEM;
  2098. /*
  2099. * If the bottom subvolume does not exist directly under upper_limit,
  2100. * construct the path in from the bottom up.
  2101. */
  2102. if (dirid != upper_limit.objectid) {
  2103. ptr = &args->path[BTRFS_INO_LOOKUP_USER_PATH_MAX - 1];
  2104. key.objectid = treeid;
  2105. key.type = BTRFS_ROOT_ITEM_KEY;
  2106. key.offset = (u64)-1;
  2107. root = btrfs_read_fs_root_no_name(fs_info, &key);
  2108. if (IS_ERR(root)) {
  2109. ret = PTR_ERR(root);
  2110. goto out;
  2111. }
  2112. key.objectid = dirid;
  2113. key.type = BTRFS_INODE_REF_KEY;
  2114. key.offset = (u64)-1;
  2115. while (1) {
  2116. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  2117. if (ret < 0) {
  2118. goto out;
  2119. } else if (ret > 0) {
  2120. ret = btrfs_previous_item(root, path, dirid,
  2121. BTRFS_INODE_REF_KEY);
  2122. if (ret < 0) {
  2123. goto out;
  2124. } else if (ret > 0) {
  2125. ret = -ENOENT;
  2126. goto out;
  2127. }
  2128. }
  2129. leaf = path->nodes[0];
  2130. slot = path->slots[0];
  2131. btrfs_item_key_to_cpu(leaf, &key, slot);
  2132. iref = btrfs_item_ptr(leaf, slot, struct btrfs_inode_ref);
  2133. len = btrfs_inode_ref_name_len(leaf, iref);
  2134. ptr -= len + 1;
  2135. total_len += len + 1;
  2136. if (ptr < args->path) {
  2137. ret = -ENAMETOOLONG;
  2138. goto out;
  2139. }
  2140. *(ptr + len) = '/';
  2141. read_extent_buffer(leaf, ptr,
  2142. (unsigned long)(iref + 1), len);
  2143. /* Check the read+exec permission of this directory */
  2144. ret = btrfs_previous_item(root, path, dirid,
  2145. BTRFS_INODE_ITEM_KEY);
  2146. if (ret < 0) {
  2147. goto out;
  2148. } else if (ret > 0) {
  2149. ret = -ENOENT;
  2150. goto out;
  2151. }
  2152. leaf = path->nodes[0];
  2153. slot = path->slots[0];
  2154. btrfs_item_key_to_cpu(leaf, &key2, slot);
  2155. if (key2.objectid != dirid) {
  2156. ret = -ENOENT;
  2157. goto out;
  2158. }
  2159. temp_inode = btrfs_iget(sb, &key2, root, NULL);
  2160. if (IS_ERR(temp_inode)) {
  2161. ret = PTR_ERR(temp_inode);
  2162. goto out;
  2163. }
  2164. ret = inode_permission(temp_inode, MAY_READ | MAY_EXEC);
  2165. iput(temp_inode);
  2166. if (ret) {
  2167. ret = -EACCES;
  2168. goto out;
  2169. }
  2170. if (key.offset == upper_limit.objectid)
  2171. break;
  2172. if (key.objectid == BTRFS_FIRST_FREE_OBJECTID) {
  2173. ret = -EACCES;
  2174. goto out;
  2175. }
  2176. btrfs_release_path(path);
  2177. key.objectid = key.offset;
  2178. key.offset = (u64)-1;
  2179. dirid = key.objectid;
  2180. }
  2181. memmove(args->path, ptr, total_len);
  2182. args->path[total_len] = '\0';
  2183. btrfs_release_path(path);
  2184. }
  2185. /* Get the bottom subvolume's name from ROOT_REF */
  2186. root = fs_info->tree_root;
  2187. key.objectid = treeid;
  2188. key.type = BTRFS_ROOT_REF_KEY;
  2189. key.offset = args->treeid;
  2190. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  2191. if (ret < 0) {
  2192. goto out;
  2193. } else if (ret > 0) {
  2194. ret = -ENOENT;
  2195. goto out;
  2196. }
  2197. leaf = path->nodes[0];
  2198. slot = path->slots[0];
  2199. btrfs_item_key_to_cpu(leaf, &key, slot);
  2200. item_off = btrfs_item_ptr_offset(leaf, slot);
  2201. item_len = btrfs_item_size_nr(leaf, slot);
  2202. /* Check if dirid in ROOT_REF corresponds to passed dirid */
  2203. rref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref);
  2204. if (args->dirid != btrfs_root_ref_dirid(leaf, rref)) {
  2205. ret = -EINVAL;
  2206. goto out;
  2207. }
  2208. /* Copy subvolume's name */
  2209. item_off += sizeof(struct btrfs_root_ref);
  2210. item_len -= sizeof(struct btrfs_root_ref);
  2211. read_extent_buffer(leaf, args->name, item_off, item_len);
  2212. args->name[item_len] = 0;
  2213. out:
  2214. btrfs_free_path(path);
  2215. return ret;
  2216. }
  2217. static noinline int btrfs_ioctl_ino_lookup(struct file *file,
  2218. void __user *argp)
  2219. {
  2220. struct btrfs_ioctl_ino_lookup_args *args;
  2221. struct inode *inode;
  2222. int ret = 0;
  2223. args = memdup_user(argp, sizeof(*args));
  2224. if (IS_ERR(args))
  2225. return PTR_ERR(args);
  2226. inode = file_inode(file);
  2227. /*
  2228. * Unprivileged query to obtain the containing subvolume root id. The
  2229. * path is reset so it's consistent with btrfs_search_path_in_tree.
  2230. */
  2231. if (args->treeid == 0)
  2232. args->treeid = BTRFS_I(inode)->root->root_key.objectid;
  2233. if (args->objectid == BTRFS_FIRST_FREE_OBJECTID) {
  2234. args->name[0] = 0;
  2235. goto out;
  2236. }
  2237. if (!capable(CAP_SYS_ADMIN)) {
  2238. ret = -EPERM;
  2239. goto out;
  2240. }
  2241. ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
  2242. args->treeid, args->objectid,
  2243. args->name);
  2244. out:
  2245. if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
  2246. ret = -EFAULT;
  2247. kfree(args);
  2248. return ret;
  2249. }
  2250. /*
  2251. * Version of ino_lookup ioctl (unprivileged)
  2252. *
  2253. * The main differences from ino_lookup ioctl are:
  2254. *
  2255. * 1. Read + Exec permission will be checked using inode_permission() during
  2256. * path construction. -EACCES will be returned in case of failure.
  2257. * 2. Path construction will be stopped at the inode number which corresponds
  2258. * to the fd with which this ioctl is called. If constructed path does not
  2259. * exist under fd's inode, -EACCES will be returned.
  2260. * 3. The name of bottom subvolume is also searched and filled.
  2261. */
  2262. static int btrfs_ioctl_ino_lookup_user(struct file *file, void __user *argp)
  2263. {
  2264. struct btrfs_ioctl_ino_lookup_user_args *args;
  2265. struct inode *inode;
  2266. int ret;
  2267. args = memdup_user(argp, sizeof(*args));
  2268. if (IS_ERR(args))
  2269. return PTR_ERR(args);
  2270. inode = file_inode(file);
  2271. if (args->dirid == BTRFS_FIRST_FREE_OBJECTID &&
  2272. BTRFS_I(inode)->location.objectid != BTRFS_FIRST_FREE_OBJECTID) {
  2273. /*
  2274. * The subvolume does not exist under fd with which this is
  2275. * called
  2276. */
  2277. kfree(args);
  2278. return -EACCES;
  2279. }
  2280. ret = btrfs_search_path_in_tree_user(inode, args);
  2281. if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
  2282. ret = -EFAULT;
  2283. kfree(args);
  2284. return ret;
  2285. }
  2286. /* Get the subvolume information in BTRFS_ROOT_ITEM and BTRFS_ROOT_BACKREF */
  2287. static int btrfs_ioctl_get_subvol_info(struct file *file, void __user *argp)
  2288. {
  2289. struct btrfs_ioctl_get_subvol_info_args *subvol_info;
  2290. struct btrfs_fs_info *fs_info;
  2291. struct btrfs_root *root;
  2292. struct btrfs_path *path;
  2293. struct btrfs_key key;
  2294. struct btrfs_root_item *root_item;
  2295. struct btrfs_root_ref *rref;
  2296. struct extent_buffer *leaf;
  2297. unsigned long item_off;
  2298. unsigned long item_len;
  2299. struct inode *inode;
  2300. int slot;
  2301. int ret = 0;
  2302. path = btrfs_alloc_path();
  2303. if (!path)
  2304. return -ENOMEM;
  2305. subvol_info = kzalloc(sizeof(*subvol_info), GFP_KERNEL);
  2306. if (!subvol_info) {
  2307. btrfs_free_path(path);
  2308. return -ENOMEM;
  2309. }
  2310. inode = file_inode(file);
  2311. fs_info = BTRFS_I(inode)->root->fs_info;
  2312. /* Get root_item of inode's subvolume */
  2313. key.objectid = BTRFS_I(inode)->root->root_key.objectid;
  2314. key.type = BTRFS_ROOT_ITEM_KEY;
  2315. key.offset = (u64)-1;
  2316. root = btrfs_read_fs_root_no_name(fs_info, &key);
  2317. if (IS_ERR(root)) {
  2318. ret = PTR_ERR(root);
  2319. goto out;
  2320. }
  2321. root_item = &root->root_item;
  2322. subvol_info->treeid = key.objectid;
  2323. subvol_info->generation = btrfs_root_generation(root_item);
  2324. subvol_info->flags = btrfs_root_flags(root_item);
  2325. memcpy(subvol_info->uuid, root_item->uuid, BTRFS_UUID_SIZE);
  2326. memcpy(subvol_info->parent_uuid, root_item->parent_uuid,
  2327. BTRFS_UUID_SIZE);
  2328. memcpy(subvol_info->received_uuid, root_item->received_uuid,
  2329. BTRFS_UUID_SIZE);
  2330. subvol_info->ctransid = btrfs_root_ctransid(root_item);
  2331. subvol_info->ctime.sec = btrfs_stack_timespec_sec(&root_item->ctime);
  2332. subvol_info->ctime.nsec = btrfs_stack_timespec_nsec(&root_item->ctime);
  2333. subvol_info->otransid = btrfs_root_otransid(root_item);
  2334. subvol_info->otime.sec = btrfs_stack_timespec_sec(&root_item->otime);
  2335. subvol_info->otime.nsec = btrfs_stack_timespec_nsec(&root_item->otime);
  2336. subvol_info->stransid = btrfs_root_stransid(root_item);
  2337. subvol_info->stime.sec = btrfs_stack_timespec_sec(&root_item->stime);
  2338. subvol_info->stime.nsec = btrfs_stack_timespec_nsec(&root_item->stime);
  2339. subvol_info->rtransid = btrfs_root_rtransid(root_item);
  2340. subvol_info->rtime.sec = btrfs_stack_timespec_sec(&root_item->rtime);
  2341. subvol_info->rtime.nsec = btrfs_stack_timespec_nsec(&root_item->rtime);
  2342. if (key.objectid != BTRFS_FS_TREE_OBJECTID) {
  2343. /* Search root tree for ROOT_BACKREF of this subvolume */
  2344. root = fs_info->tree_root;
  2345. key.type = BTRFS_ROOT_BACKREF_KEY;
  2346. key.offset = 0;
  2347. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  2348. if (ret < 0) {
  2349. goto out;
  2350. } else if (path->slots[0] >=
  2351. btrfs_header_nritems(path->nodes[0])) {
  2352. ret = btrfs_next_leaf(root, path);
  2353. if (ret < 0) {
  2354. goto out;
  2355. } else if (ret > 0) {
  2356. ret = -EUCLEAN;
  2357. goto out;
  2358. }
  2359. }
  2360. leaf = path->nodes[0];
  2361. slot = path->slots[0];
  2362. btrfs_item_key_to_cpu(leaf, &key, slot);
  2363. if (key.objectid == subvol_info->treeid &&
  2364. key.type == BTRFS_ROOT_BACKREF_KEY) {
  2365. subvol_info->parent_id = key.offset;
  2366. rref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref);
  2367. subvol_info->dirid = btrfs_root_ref_dirid(leaf, rref);
  2368. item_off = btrfs_item_ptr_offset(leaf, slot)
  2369. + sizeof(struct btrfs_root_ref);
  2370. item_len = btrfs_item_size_nr(leaf, slot)
  2371. - sizeof(struct btrfs_root_ref);
  2372. read_extent_buffer(leaf, subvol_info->name,
  2373. item_off, item_len);
  2374. } else {
  2375. ret = -ENOENT;
  2376. goto out;
  2377. }
  2378. }
  2379. if (copy_to_user(argp, subvol_info, sizeof(*subvol_info)))
  2380. ret = -EFAULT;
  2381. out:
  2382. btrfs_free_path(path);
  2383. kzfree(subvol_info);
  2384. return ret;
  2385. }
  2386. /*
  2387. * Return ROOT_REF information of the subvolume containing this inode
  2388. * except the subvolume name.
  2389. */
  2390. static int btrfs_ioctl_get_subvol_rootref(struct file *file, void __user *argp)
  2391. {
  2392. struct btrfs_ioctl_get_subvol_rootref_args *rootrefs;
  2393. struct btrfs_root_ref *rref;
  2394. struct btrfs_root *root;
  2395. struct btrfs_path *path;
  2396. struct btrfs_key key;
  2397. struct extent_buffer *leaf;
  2398. struct inode *inode;
  2399. u64 objectid;
  2400. int slot;
  2401. int ret;
  2402. u8 found;
  2403. path = btrfs_alloc_path();
  2404. if (!path)
  2405. return -ENOMEM;
  2406. rootrefs = memdup_user(argp, sizeof(*rootrefs));
  2407. if (IS_ERR(rootrefs)) {
  2408. btrfs_free_path(path);
  2409. return PTR_ERR(rootrefs);
  2410. }
  2411. inode = file_inode(file);
  2412. root = BTRFS_I(inode)->root->fs_info->tree_root;
  2413. objectid = BTRFS_I(inode)->root->root_key.objectid;
  2414. key.objectid = objectid;
  2415. key.type = BTRFS_ROOT_REF_KEY;
  2416. key.offset = rootrefs->min_treeid;
  2417. found = 0;
  2418. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  2419. if (ret < 0) {
  2420. goto out;
  2421. } else if (path->slots[0] >=
  2422. btrfs_header_nritems(path->nodes[0])) {
  2423. ret = btrfs_next_leaf(root, path);
  2424. if (ret < 0) {
  2425. goto out;
  2426. } else if (ret > 0) {
  2427. ret = -EUCLEAN;
  2428. goto out;
  2429. }
  2430. }
  2431. while (1) {
  2432. leaf = path->nodes[0];
  2433. slot = path->slots[0];
  2434. btrfs_item_key_to_cpu(leaf, &key, slot);
  2435. if (key.objectid != objectid || key.type != BTRFS_ROOT_REF_KEY) {
  2436. ret = 0;
  2437. goto out;
  2438. }
  2439. if (found == BTRFS_MAX_ROOTREF_BUFFER_NUM) {
  2440. ret = -EOVERFLOW;
  2441. goto out;
  2442. }
  2443. rref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref);
  2444. rootrefs->rootref[found].treeid = key.offset;
  2445. rootrefs->rootref[found].dirid =
  2446. btrfs_root_ref_dirid(leaf, rref);
  2447. found++;
  2448. ret = btrfs_next_item(root, path);
  2449. if (ret < 0) {
  2450. goto out;
  2451. } else if (ret > 0) {
  2452. ret = -EUCLEAN;
  2453. goto out;
  2454. }
  2455. }
  2456. out:
  2457. if (!ret || ret == -EOVERFLOW) {
  2458. rootrefs->num_items = found;
  2459. /* update min_treeid for next search */
  2460. if (found)
  2461. rootrefs->min_treeid =
  2462. rootrefs->rootref[found - 1].treeid + 1;
  2463. if (copy_to_user(argp, rootrefs, sizeof(*rootrefs)))
  2464. ret = -EFAULT;
  2465. }
  2466. kfree(rootrefs);
  2467. btrfs_free_path(path);
  2468. return ret;
  2469. }
  2470. static noinline int btrfs_ioctl_snap_destroy(struct file *file,
  2471. void __user *arg)
  2472. {
  2473. struct dentry *parent = file->f_path.dentry;
  2474. struct btrfs_fs_info *fs_info = btrfs_sb(parent->d_sb);
  2475. struct dentry *dentry;
  2476. struct inode *dir = d_inode(parent);
  2477. struct inode *inode;
  2478. struct btrfs_root *root = BTRFS_I(dir)->root;
  2479. struct btrfs_root *dest = NULL;
  2480. struct btrfs_ioctl_vol_args *vol_args;
  2481. int namelen;
  2482. int err = 0;
  2483. if (!S_ISDIR(dir->i_mode))
  2484. return -ENOTDIR;
  2485. vol_args = memdup_user(arg, sizeof(*vol_args));
  2486. if (IS_ERR(vol_args))
  2487. return PTR_ERR(vol_args);
  2488. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  2489. namelen = strlen(vol_args->name);
  2490. if (strchr(vol_args->name, '/') ||
  2491. strncmp(vol_args->name, "..", namelen) == 0) {
  2492. err = -EINVAL;
  2493. goto out;
  2494. }
  2495. err = mnt_want_write_file(file);
  2496. if (err)
  2497. goto out;
  2498. err = down_write_killable_nested(&dir->i_rwsem, I_MUTEX_PARENT);
  2499. if (err == -EINTR)
  2500. goto out_drop_write;
  2501. dentry = lookup_one_len(vol_args->name, parent, namelen);
  2502. if (IS_ERR(dentry)) {
  2503. err = PTR_ERR(dentry);
  2504. goto out_unlock_dir;
  2505. }
  2506. if (d_really_is_negative(dentry)) {
  2507. err = -ENOENT;
  2508. goto out_dput;
  2509. }
  2510. inode = d_inode(dentry);
  2511. dest = BTRFS_I(inode)->root;
  2512. if (!capable(CAP_SYS_ADMIN)) {
  2513. /*
  2514. * Regular user. Only allow this with a special mount
  2515. * option, when the user has write+exec access to the
  2516. * subvol root, and when rmdir(2) would have been
  2517. * allowed.
  2518. *
  2519. * Note that this is _not_ check that the subvol is
  2520. * empty or doesn't contain data that we wouldn't
  2521. * otherwise be able to delete.
  2522. *
  2523. * Users who want to delete empty subvols should try
  2524. * rmdir(2).
  2525. */
  2526. err = -EPERM;
  2527. if (!btrfs_test_opt(fs_info, USER_SUBVOL_RM_ALLOWED))
  2528. goto out_dput;
  2529. /*
  2530. * Do not allow deletion if the parent dir is the same
  2531. * as the dir to be deleted. That means the ioctl
  2532. * must be called on the dentry referencing the root
  2533. * of the subvol, not a random directory contained
  2534. * within it.
  2535. */
  2536. err = -EINVAL;
  2537. if (root == dest)
  2538. goto out_dput;
  2539. err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
  2540. if (err)
  2541. goto out_dput;
  2542. }
  2543. /* check if subvolume may be deleted by a user */
  2544. err = btrfs_may_delete(dir, dentry, 1);
  2545. if (err)
  2546. goto out_dput;
  2547. if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) {
  2548. err = -EINVAL;
  2549. goto out_dput;
  2550. }
  2551. inode_lock(inode);
  2552. err = btrfs_delete_subvolume(dir, dentry);
  2553. inode_unlock(inode);
  2554. if (!err)
  2555. d_delete(dentry);
  2556. out_dput:
  2557. dput(dentry);
  2558. out_unlock_dir:
  2559. inode_unlock(dir);
  2560. out_drop_write:
  2561. mnt_drop_write_file(file);
  2562. out:
  2563. kfree(vol_args);
  2564. return err;
  2565. }
  2566. static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
  2567. {
  2568. struct inode *inode = file_inode(file);
  2569. struct btrfs_root *root = BTRFS_I(inode)->root;
  2570. struct btrfs_ioctl_defrag_range_args *range;
  2571. int ret;
  2572. ret = mnt_want_write_file(file);
  2573. if (ret)
  2574. return ret;
  2575. if (btrfs_root_readonly(root)) {
  2576. ret = -EROFS;
  2577. goto out;
  2578. }
  2579. switch (inode->i_mode & S_IFMT) {
  2580. case S_IFDIR:
  2581. if (!capable(CAP_SYS_ADMIN)) {
  2582. ret = -EPERM;
  2583. goto out;
  2584. }
  2585. ret = btrfs_defrag_root(root);
  2586. break;
  2587. case S_IFREG:
  2588. /*
  2589. * Note that this does not check the file descriptor for write
  2590. * access. This prevents defragmenting executables that are
  2591. * running and allows defrag on files open in read-only mode.
  2592. */
  2593. if (!capable(CAP_SYS_ADMIN) &&
  2594. inode_permission(inode, MAY_WRITE)) {
  2595. ret = -EPERM;
  2596. goto out;
  2597. }
  2598. range = kzalloc(sizeof(*range), GFP_KERNEL);
  2599. if (!range) {
  2600. ret = -ENOMEM;
  2601. goto out;
  2602. }
  2603. if (argp) {
  2604. if (copy_from_user(range, argp,
  2605. sizeof(*range))) {
  2606. ret = -EFAULT;
  2607. kfree(range);
  2608. goto out;
  2609. }
  2610. /* compression requires us to start the IO */
  2611. if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
  2612. range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
  2613. range->extent_thresh = (u32)-1;
  2614. }
  2615. } else {
  2616. /* the rest are all set to zero by kzalloc */
  2617. range->len = (u64)-1;
  2618. }
  2619. ret = btrfs_defrag_file(file_inode(file), file,
  2620. range, BTRFS_OLDEST_GENERATION, 0);
  2621. if (ret > 0)
  2622. ret = 0;
  2623. kfree(range);
  2624. break;
  2625. default:
  2626. ret = -EINVAL;
  2627. }
  2628. out:
  2629. mnt_drop_write_file(file);
  2630. return ret;
  2631. }
  2632. static long btrfs_ioctl_add_dev(struct btrfs_fs_info *fs_info, void __user *arg)
  2633. {
  2634. struct btrfs_ioctl_vol_args *vol_args;
  2635. int ret;
  2636. if (!capable(CAP_SYS_ADMIN))
  2637. return -EPERM;
  2638. if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags))
  2639. return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
  2640. vol_args = memdup_user(arg, sizeof(*vol_args));
  2641. if (IS_ERR(vol_args)) {
  2642. ret = PTR_ERR(vol_args);
  2643. goto out;
  2644. }
  2645. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  2646. ret = btrfs_init_new_device(fs_info, vol_args->name);
  2647. if (!ret)
  2648. btrfs_info(fs_info, "disk added %s", vol_args->name);
  2649. kfree(vol_args);
  2650. out:
  2651. clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
  2652. return ret;
  2653. }
  2654. static long btrfs_ioctl_rm_dev_v2(struct file *file, void __user *arg)
  2655. {
  2656. struct inode *inode = file_inode(file);
  2657. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  2658. struct btrfs_ioctl_vol_args_v2 *vol_args;
  2659. int ret;
  2660. if (!capable(CAP_SYS_ADMIN))
  2661. return -EPERM;
  2662. ret = mnt_want_write_file(file);
  2663. if (ret)
  2664. return ret;
  2665. vol_args = memdup_user(arg, sizeof(*vol_args));
  2666. if (IS_ERR(vol_args)) {
  2667. ret = PTR_ERR(vol_args);
  2668. goto err_drop;
  2669. }
  2670. /* Check for compatibility reject unknown flags */
  2671. if (vol_args->flags & ~BTRFS_VOL_ARG_V2_FLAGS_SUPPORTED) {
  2672. ret = -EOPNOTSUPP;
  2673. goto out;
  2674. }
  2675. if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
  2676. ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
  2677. goto out;
  2678. }
  2679. if (vol_args->flags & BTRFS_DEVICE_SPEC_BY_ID) {
  2680. ret = btrfs_rm_device(fs_info, NULL, vol_args->devid);
  2681. } else {
  2682. vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
  2683. ret = btrfs_rm_device(fs_info, vol_args->name, 0);
  2684. }
  2685. clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
  2686. if (!ret) {
  2687. if (vol_args->flags & BTRFS_DEVICE_SPEC_BY_ID)
  2688. btrfs_info(fs_info, "device deleted: id %llu",
  2689. vol_args->devid);
  2690. else
  2691. btrfs_info(fs_info, "device deleted: %s",
  2692. vol_args->name);
  2693. }
  2694. out:
  2695. kfree(vol_args);
  2696. err_drop:
  2697. mnt_drop_write_file(file);
  2698. return ret;
  2699. }
  2700. static long btrfs_ioctl_rm_dev(struct file *file, void __user *arg)
  2701. {
  2702. struct inode *inode = file_inode(file);
  2703. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  2704. struct btrfs_ioctl_vol_args *vol_args;
  2705. int ret;
  2706. if (!capable(CAP_SYS_ADMIN))
  2707. return -EPERM;
  2708. ret = mnt_want_write_file(file);
  2709. if (ret)
  2710. return ret;
  2711. if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
  2712. ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
  2713. goto out_drop_write;
  2714. }
  2715. vol_args = memdup_user(arg, sizeof(*vol_args));
  2716. if (IS_ERR(vol_args)) {
  2717. ret = PTR_ERR(vol_args);
  2718. goto out;
  2719. }
  2720. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  2721. ret = btrfs_rm_device(fs_info, vol_args->name, 0);
  2722. if (!ret)
  2723. btrfs_info(fs_info, "disk deleted %s", vol_args->name);
  2724. kfree(vol_args);
  2725. out:
  2726. clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
  2727. out_drop_write:
  2728. mnt_drop_write_file(file);
  2729. return ret;
  2730. }
  2731. static long btrfs_ioctl_fs_info(struct btrfs_fs_info *fs_info,
  2732. void __user *arg)
  2733. {
  2734. struct btrfs_ioctl_fs_info_args *fi_args;
  2735. struct btrfs_device *device;
  2736. struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
  2737. int ret = 0;
  2738. fi_args = kzalloc(sizeof(*fi_args), GFP_KERNEL);
  2739. if (!fi_args)
  2740. return -ENOMEM;
  2741. rcu_read_lock();
  2742. fi_args->num_devices = fs_devices->num_devices;
  2743. list_for_each_entry_rcu(device, &fs_devices->devices, dev_list) {
  2744. if (device->devid > fi_args->max_id)
  2745. fi_args->max_id = device->devid;
  2746. }
  2747. rcu_read_unlock();
  2748. memcpy(&fi_args->fsid, fs_info->fsid, sizeof(fi_args->fsid));
  2749. fi_args->nodesize = fs_info->nodesize;
  2750. fi_args->sectorsize = fs_info->sectorsize;
  2751. fi_args->clone_alignment = fs_info->sectorsize;
  2752. if (copy_to_user(arg, fi_args, sizeof(*fi_args)))
  2753. ret = -EFAULT;
  2754. kfree(fi_args);
  2755. return ret;
  2756. }
  2757. static long btrfs_ioctl_dev_info(struct btrfs_fs_info *fs_info,
  2758. void __user *arg)
  2759. {
  2760. struct btrfs_ioctl_dev_info_args *di_args;
  2761. struct btrfs_device *dev;
  2762. int ret = 0;
  2763. char *s_uuid = NULL;
  2764. di_args = memdup_user(arg, sizeof(*di_args));
  2765. if (IS_ERR(di_args))
  2766. return PTR_ERR(di_args);
  2767. if (!btrfs_is_empty_uuid(di_args->uuid))
  2768. s_uuid = di_args->uuid;
  2769. rcu_read_lock();
  2770. dev = btrfs_find_device(fs_info->fs_devices, di_args->devid, s_uuid,
  2771. NULL, true);
  2772. if (!dev) {
  2773. ret = -ENODEV;
  2774. goto out;
  2775. }
  2776. di_args->devid = dev->devid;
  2777. di_args->bytes_used = btrfs_device_get_bytes_used(dev);
  2778. di_args->total_bytes = btrfs_device_get_total_bytes(dev);
  2779. memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid));
  2780. if (dev->name) {
  2781. strncpy(di_args->path, rcu_str_deref(dev->name),
  2782. sizeof(di_args->path) - 1);
  2783. di_args->path[sizeof(di_args->path) - 1] = 0;
  2784. } else {
  2785. di_args->path[0] = '\0';
  2786. }
  2787. out:
  2788. rcu_read_unlock();
  2789. if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args)))
  2790. ret = -EFAULT;
  2791. kfree(di_args);
  2792. return ret;
  2793. }
  2794. static struct page *extent_same_get_page(struct inode *inode, pgoff_t index)
  2795. {
  2796. struct page *page;
  2797. page = grab_cache_page(inode->i_mapping, index);
  2798. if (!page)
  2799. return ERR_PTR(-ENOMEM);
  2800. if (!PageUptodate(page)) {
  2801. int ret;
  2802. ret = btrfs_readpage(NULL, page);
  2803. if (ret)
  2804. return ERR_PTR(ret);
  2805. lock_page(page);
  2806. if (!PageUptodate(page)) {
  2807. unlock_page(page);
  2808. put_page(page);
  2809. return ERR_PTR(-EIO);
  2810. }
  2811. if (page->mapping != inode->i_mapping) {
  2812. unlock_page(page);
  2813. put_page(page);
  2814. return ERR_PTR(-EAGAIN);
  2815. }
  2816. }
  2817. return page;
  2818. }
  2819. static int gather_extent_pages(struct inode *inode, struct page **pages,
  2820. int num_pages, u64 off)
  2821. {
  2822. int i;
  2823. pgoff_t index = off >> PAGE_SHIFT;
  2824. for (i = 0; i < num_pages; i++) {
  2825. again:
  2826. pages[i] = extent_same_get_page(inode, index + i);
  2827. if (IS_ERR(pages[i])) {
  2828. int err = PTR_ERR(pages[i]);
  2829. if (err == -EAGAIN)
  2830. goto again;
  2831. pages[i] = NULL;
  2832. return err;
  2833. }
  2834. }
  2835. return 0;
  2836. }
  2837. static int lock_extent_range(struct inode *inode, u64 off, u64 len,
  2838. bool retry_range_locking)
  2839. {
  2840. /*
  2841. * Do any pending delalloc/csum calculations on inode, one way or
  2842. * another, and lock file content.
  2843. * The locking order is:
  2844. *
  2845. * 1) pages
  2846. * 2) range in the inode's io tree
  2847. */
  2848. while (1) {
  2849. struct btrfs_ordered_extent *ordered;
  2850. lock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1);
  2851. ordered = btrfs_lookup_first_ordered_extent(inode,
  2852. off + len - 1);
  2853. if ((!ordered ||
  2854. ordered->file_offset + ordered->len <= off ||
  2855. ordered->file_offset >= off + len) &&
  2856. !test_range_bit(&BTRFS_I(inode)->io_tree, off,
  2857. off + len - 1, EXTENT_DELALLOC, 0, NULL)) {
  2858. if (ordered)
  2859. btrfs_put_ordered_extent(ordered);
  2860. break;
  2861. }
  2862. unlock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1);
  2863. if (ordered)
  2864. btrfs_put_ordered_extent(ordered);
  2865. if (!retry_range_locking)
  2866. return -EAGAIN;
  2867. btrfs_wait_ordered_range(inode, off, len);
  2868. }
  2869. return 0;
  2870. }
  2871. static void btrfs_double_inode_unlock(struct inode *inode1, struct inode *inode2)
  2872. {
  2873. inode_unlock(inode1);
  2874. inode_unlock(inode2);
  2875. }
  2876. static void btrfs_double_inode_lock(struct inode *inode1, struct inode *inode2)
  2877. {
  2878. if (inode1 < inode2)
  2879. swap(inode1, inode2);
  2880. inode_lock_nested(inode1, I_MUTEX_PARENT);
  2881. inode_lock_nested(inode2, I_MUTEX_CHILD);
  2882. }
  2883. static void btrfs_double_extent_unlock(struct inode *inode1, u64 loff1,
  2884. struct inode *inode2, u64 loff2, u64 len)
  2885. {
  2886. unlock_extent(&BTRFS_I(inode1)->io_tree, loff1, loff1 + len - 1);
  2887. unlock_extent(&BTRFS_I(inode2)->io_tree, loff2, loff2 + len - 1);
  2888. }
  2889. static int btrfs_double_extent_lock(struct inode *inode1, u64 loff1,
  2890. struct inode *inode2, u64 loff2, u64 len,
  2891. bool retry_range_locking)
  2892. {
  2893. int ret;
  2894. if (inode1 < inode2) {
  2895. swap(inode1, inode2);
  2896. swap(loff1, loff2);
  2897. }
  2898. ret = lock_extent_range(inode1, loff1, len, retry_range_locking);
  2899. if (ret)
  2900. return ret;
  2901. ret = lock_extent_range(inode2, loff2, len, retry_range_locking);
  2902. if (ret)
  2903. unlock_extent(&BTRFS_I(inode1)->io_tree, loff1,
  2904. loff1 + len - 1);
  2905. return ret;
  2906. }
  2907. struct cmp_pages {
  2908. int num_pages;
  2909. struct page **src_pages;
  2910. struct page **dst_pages;
  2911. };
  2912. static void btrfs_cmp_data_free(struct cmp_pages *cmp)
  2913. {
  2914. int i;
  2915. struct page *pg;
  2916. for (i = 0; i < cmp->num_pages; i++) {
  2917. pg = cmp->src_pages[i];
  2918. if (pg) {
  2919. unlock_page(pg);
  2920. put_page(pg);
  2921. cmp->src_pages[i] = NULL;
  2922. }
  2923. pg = cmp->dst_pages[i];
  2924. if (pg) {
  2925. unlock_page(pg);
  2926. put_page(pg);
  2927. cmp->dst_pages[i] = NULL;
  2928. }
  2929. }
  2930. }
  2931. static int btrfs_cmp_data_prepare(struct inode *src, u64 loff,
  2932. struct inode *dst, u64 dst_loff,
  2933. u64 len, struct cmp_pages *cmp)
  2934. {
  2935. int ret;
  2936. int num_pages = PAGE_ALIGN(len) >> PAGE_SHIFT;
  2937. cmp->num_pages = num_pages;
  2938. ret = gather_extent_pages(src, cmp->src_pages, num_pages, loff);
  2939. if (ret)
  2940. goto out;
  2941. ret = gather_extent_pages(dst, cmp->dst_pages, num_pages, dst_loff);
  2942. out:
  2943. if (ret)
  2944. btrfs_cmp_data_free(cmp);
  2945. return ret;
  2946. }
  2947. static int btrfs_cmp_data(u64 len, struct cmp_pages *cmp)
  2948. {
  2949. int ret = 0;
  2950. int i;
  2951. struct page *src_page, *dst_page;
  2952. unsigned int cmp_len = PAGE_SIZE;
  2953. void *addr, *dst_addr;
  2954. i = 0;
  2955. while (len) {
  2956. if (len < PAGE_SIZE)
  2957. cmp_len = len;
  2958. BUG_ON(i >= cmp->num_pages);
  2959. src_page = cmp->src_pages[i];
  2960. dst_page = cmp->dst_pages[i];
  2961. ASSERT(PageLocked(src_page));
  2962. ASSERT(PageLocked(dst_page));
  2963. addr = kmap_atomic(src_page);
  2964. dst_addr = kmap_atomic(dst_page);
  2965. flush_dcache_page(src_page);
  2966. flush_dcache_page(dst_page);
  2967. if (memcmp(addr, dst_addr, cmp_len))
  2968. ret = -EBADE;
  2969. kunmap_atomic(addr);
  2970. kunmap_atomic(dst_addr);
  2971. if (ret)
  2972. break;
  2973. len -= cmp_len;
  2974. i++;
  2975. }
  2976. return ret;
  2977. }
  2978. static int extent_same_check_offsets(struct inode *inode, u64 off, u64 *plen,
  2979. u64 olen)
  2980. {
  2981. u64 len = *plen;
  2982. u64 bs = BTRFS_I(inode)->root->fs_info->sb->s_blocksize;
  2983. if (off + olen > inode->i_size || off + olen < off)
  2984. return -EINVAL;
  2985. /* if we extend to eof, continue to block boundary */
  2986. if (off + len == inode->i_size)
  2987. *plen = len = ALIGN(inode->i_size, bs) - off;
  2988. /* Check that we are block aligned - btrfs_clone() requires this */
  2989. if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs))
  2990. return -EINVAL;
  2991. return 0;
  2992. }
  2993. static int btrfs_extent_same_range(struct inode *src, u64 loff, u64 olen,
  2994. struct inode *dst, u64 dst_loff,
  2995. struct cmp_pages *cmp)
  2996. {
  2997. int ret;
  2998. u64 len = olen;
  2999. bool same_inode = (src == dst);
  3000. u64 same_lock_start = 0;
  3001. u64 same_lock_len = 0;
  3002. ret = extent_same_check_offsets(src, loff, &len, olen);
  3003. if (ret)
  3004. return ret;
  3005. ret = extent_same_check_offsets(dst, dst_loff, &len, olen);
  3006. if (ret)
  3007. return ret;
  3008. if (same_inode) {
  3009. /*
  3010. * Single inode case wants the same checks, except we
  3011. * don't want our length pushed out past i_size as
  3012. * comparing that data range makes no sense.
  3013. *
  3014. * extent_same_check_offsets() will do this for an
  3015. * unaligned length at i_size, so catch it here and
  3016. * reject the request.
  3017. *
  3018. * This effectively means we require aligned extents
  3019. * for the single-inode case, whereas the other cases
  3020. * allow an unaligned length so long as it ends at
  3021. * i_size.
  3022. */
  3023. if (len != olen)
  3024. return -EINVAL;
  3025. /* Check for overlapping ranges */
  3026. if (dst_loff + len > loff && dst_loff < loff + len)
  3027. return -EINVAL;
  3028. same_lock_start = min_t(u64, loff, dst_loff);
  3029. same_lock_len = max_t(u64, loff, dst_loff) + len - same_lock_start;
  3030. } else {
  3031. /*
  3032. * If the source and destination inodes are different, the
  3033. * source's range end offset matches the source's i_size, that
  3034. * i_size is not a multiple of the sector size, and the
  3035. * destination range does not go past the destination's i_size,
  3036. * we must round down the length to the nearest sector size
  3037. * multiple. If we don't do this adjustment we end replacing
  3038. * with zeroes the bytes in the range that starts at the
  3039. * deduplication range's end offset and ends at the next sector
  3040. * size multiple.
  3041. */
  3042. if (loff + olen == i_size_read(src) &&
  3043. dst_loff + len < i_size_read(dst)) {
  3044. const u64 sz = BTRFS_I(src)->root->fs_info->sectorsize;
  3045. len = round_down(i_size_read(src), sz) - loff;
  3046. if (len == 0)
  3047. return 0;
  3048. olen = len;
  3049. }
  3050. }
  3051. again:
  3052. ret = btrfs_cmp_data_prepare(src, loff, dst, dst_loff, olen, cmp);
  3053. if (ret)
  3054. return ret;
  3055. if (same_inode)
  3056. ret = lock_extent_range(src, same_lock_start, same_lock_len,
  3057. false);
  3058. else
  3059. ret = btrfs_double_extent_lock(src, loff, dst, dst_loff, len,
  3060. false);
  3061. /*
  3062. * If one of the inodes has dirty pages in the respective range or
  3063. * ordered extents, we need to flush dellaloc and wait for all ordered
  3064. * extents in the range. We must unlock the pages and the ranges in the
  3065. * io trees to avoid deadlocks when flushing delalloc (requires locking
  3066. * pages) and when waiting for ordered extents to complete (they require
  3067. * range locking).
  3068. */
  3069. if (ret == -EAGAIN) {
  3070. /*
  3071. * Ranges in the io trees already unlocked. Now unlock all
  3072. * pages before waiting for all IO to complete.
  3073. */
  3074. btrfs_cmp_data_free(cmp);
  3075. if (same_inode) {
  3076. btrfs_wait_ordered_range(src, same_lock_start,
  3077. same_lock_len);
  3078. } else {
  3079. btrfs_wait_ordered_range(src, loff, len);
  3080. btrfs_wait_ordered_range(dst, dst_loff, len);
  3081. }
  3082. goto again;
  3083. }
  3084. ASSERT(ret == 0);
  3085. if (WARN_ON(ret)) {
  3086. /* ranges in the io trees already unlocked */
  3087. btrfs_cmp_data_free(cmp);
  3088. return ret;
  3089. }
  3090. /* pass original length for comparison so we stay within i_size */
  3091. ret = btrfs_cmp_data(olen, cmp);
  3092. if (ret == 0)
  3093. ret = btrfs_clone(src, dst, loff, olen, len, dst_loff, 1);
  3094. if (same_inode)
  3095. unlock_extent(&BTRFS_I(src)->io_tree, same_lock_start,
  3096. same_lock_start + same_lock_len - 1);
  3097. else
  3098. btrfs_double_extent_unlock(src, loff, dst, dst_loff, len);
  3099. btrfs_cmp_data_free(cmp);
  3100. return ret;
  3101. }
  3102. #define BTRFS_MAX_DEDUPE_LEN SZ_16M
  3103. static int btrfs_extent_same(struct inode *src, u64 loff, u64 olen,
  3104. struct inode *dst, u64 dst_loff)
  3105. {
  3106. int ret;
  3107. struct cmp_pages cmp;
  3108. int num_pages = PAGE_ALIGN(BTRFS_MAX_DEDUPE_LEN) >> PAGE_SHIFT;
  3109. bool same_inode = (src == dst);
  3110. u64 i, tail_len, chunk_count;
  3111. if (olen == 0)
  3112. return 0;
  3113. if (same_inode)
  3114. inode_lock(src);
  3115. else
  3116. btrfs_double_inode_lock(src, dst);
  3117. /* don't make the dst file partly checksummed */
  3118. if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
  3119. (BTRFS_I(dst)->flags & BTRFS_INODE_NODATASUM)) {
  3120. ret = -EINVAL;
  3121. goto out_unlock;
  3122. }
  3123. tail_len = olen % BTRFS_MAX_DEDUPE_LEN;
  3124. chunk_count = div_u64(olen, BTRFS_MAX_DEDUPE_LEN);
  3125. if (chunk_count == 0)
  3126. num_pages = PAGE_ALIGN(tail_len) >> PAGE_SHIFT;
  3127. /*
  3128. * If deduping ranges in the same inode, locking rules make it
  3129. * mandatory to always lock pages in ascending order to avoid deadlocks
  3130. * with concurrent tasks (such as starting writeback/delalloc).
  3131. */
  3132. if (same_inode && dst_loff < loff)
  3133. swap(loff, dst_loff);
  3134. /*
  3135. * We must gather up all the pages before we initiate our extent
  3136. * locking. We use an array for the page pointers. Size of the array is
  3137. * bounded by len, which is in turn bounded by BTRFS_MAX_DEDUPE_LEN.
  3138. */
  3139. cmp.src_pages = kvmalloc_array(num_pages, sizeof(struct page *),
  3140. GFP_KERNEL | __GFP_ZERO);
  3141. cmp.dst_pages = kvmalloc_array(num_pages, sizeof(struct page *),
  3142. GFP_KERNEL | __GFP_ZERO);
  3143. if (!cmp.src_pages || !cmp.dst_pages) {
  3144. ret = -ENOMEM;
  3145. goto out_free;
  3146. }
  3147. for (i = 0; i < chunk_count; i++) {
  3148. ret = btrfs_extent_same_range(src, loff, BTRFS_MAX_DEDUPE_LEN,
  3149. dst, dst_loff, &cmp);
  3150. if (ret)
  3151. goto out_free;
  3152. loff += BTRFS_MAX_DEDUPE_LEN;
  3153. dst_loff += BTRFS_MAX_DEDUPE_LEN;
  3154. }
  3155. if (tail_len > 0)
  3156. ret = btrfs_extent_same_range(src, loff, tail_len, dst,
  3157. dst_loff, &cmp);
  3158. out_free:
  3159. kvfree(cmp.src_pages);
  3160. kvfree(cmp.dst_pages);
  3161. out_unlock:
  3162. if (same_inode)
  3163. inode_unlock(src);
  3164. else
  3165. btrfs_double_inode_unlock(src, dst);
  3166. return ret;
  3167. }
  3168. int btrfs_dedupe_file_range(struct file *src_file, loff_t src_loff,
  3169. struct file *dst_file, loff_t dst_loff,
  3170. u64 olen)
  3171. {
  3172. struct inode *src = file_inode(src_file);
  3173. struct inode *dst = file_inode(dst_file);
  3174. u64 bs = BTRFS_I(src)->root->fs_info->sb->s_blocksize;
  3175. if (WARN_ON_ONCE(bs < PAGE_SIZE)) {
  3176. /*
  3177. * Btrfs does not support blocksize < page_size. As a
  3178. * result, btrfs_cmp_data() won't correctly handle
  3179. * this situation without an update.
  3180. */
  3181. return -EINVAL;
  3182. }
  3183. return btrfs_extent_same(src, src_loff, olen, dst, dst_loff);
  3184. }
  3185. static int clone_finish_inode_update(struct btrfs_trans_handle *trans,
  3186. struct inode *inode,
  3187. u64 endoff,
  3188. const u64 destoff,
  3189. const u64 olen,
  3190. int no_time_update)
  3191. {
  3192. struct btrfs_root *root = BTRFS_I(inode)->root;
  3193. int ret;
  3194. inode_inc_iversion(inode);
  3195. if (!no_time_update)
  3196. inode->i_mtime = inode->i_ctime = current_time(inode);
  3197. /*
  3198. * We round up to the block size at eof when determining which
  3199. * extents to clone above, but shouldn't round up the file size.
  3200. */
  3201. if (endoff > destoff + olen)
  3202. endoff = destoff + olen;
  3203. if (endoff > inode->i_size)
  3204. btrfs_i_size_write(BTRFS_I(inode), endoff);
  3205. ret = btrfs_update_inode(trans, root, inode);
  3206. if (ret) {
  3207. btrfs_abort_transaction(trans, ret);
  3208. btrfs_end_transaction(trans);
  3209. goto out;
  3210. }
  3211. ret = btrfs_end_transaction(trans);
  3212. out:
  3213. return ret;
  3214. }
  3215. static void clone_update_extent_map(struct btrfs_inode *inode,
  3216. const struct btrfs_trans_handle *trans,
  3217. const struct btrfs_path *path,
  3218. const u64 hole_offset,
  3219. const u64 hole_len)
  3220. {
  3221. struct extent_map_tree *em_tree = &inode->extent_tree;
  3222. struct extent_map *em;
  3223. int ret;
  3224. em = alloc_extent_map();
  3225. if (!em) {
  3226. set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags);
  3227. return;
  3228. }
  3229. if (path) {
  3230. struct btrfs_file_extent_item *fi;
  3231. fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
  3232. struct btrfs_file_extent_item);
  3233. btrfs_extent_item_to_extent_map(inode, path, fi, false, em);
  3234. em->generation = -1;
  3235. if (btrfs_file_extent_type(path->nodes[0], fi) ==
  3236. BTRFS_FILE_EXTENT_INLINE)
  3237. set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
  3238. &inode->runtime_flags);
  3239. } else {
  3240. em->start = hole_offset;
  3241. em->len = hole_len;
  3242. em->ram_bytes = em->len;
  3243. em->orig_start = hole_offset;
  3244. em->block_start = EXTENT_MAP_HOLE;
  3245. em->block_len = 0;
  3246. em->orig_block_len = 0;
  3247. em->compress_type = BTRFS_COMPRESS_NONE;
  3248. em->generation = trans->transid;
  3249. }
  3250. while (1) {
  3251. write_lock(&em_tree->lock);
  3252. ret = add_extent_mapping(em_tree, em, 1);
  3253. write_unlock(&em_tree->lock);
  3254. if (ret != -EEXIST) {
  3255. free_extent_map(em);
  3256. break;
  3257. }
  3258. btrfs_drop_extent_cache(inode, em->start,
  3259. em->start + em->len - 1, 0);
  3260. }
  3261. if (ret)
  3262. set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags);
  3263. }
  3264. /*
  3265. * Make sure we do not end up inserting an inline extent into a file that has
  3266. * already other (non-inline) extents. If a file has an inline extent it can
  3267. * not have any other extents and the (single) inline extent must start at the
  3268. * file offset 0. Failing to respect these rules will lead to file corruption,
  3269. * resulting in EIO errors on read/write operations, hitting BUG_ON's in mm, etc
  3270. *
  3271. * We can have extents that have been already written to disk or we can have
  3272. * dirty ranges still in delalloc, in which case the extent maps and items are
  3273. * created only when we run delalloc, and the delalloc ranges might fall outside
  3274. * the range we are currently locking in the inode's io tree. So we check the
  3275. * inode's i_size because of that (i_size updates are done while holding the
  3276. * i_mutex, which we are holding here).
  3277. * We also check to see if the inode has a size not greater than "datal" but has
  3278. * extents beyond it, due to an fallocate with FALLOC_FL_KEEP_SIZE (and we are
  3279. * protected against such concurrent fallocate calls by the i_mutex).
  3280. *
  3281. * If the file has no extents but a size greater than datal, do not allow the
  3282. * copy because we would need turn the inline extent into a non-inline one (even
  3283. * with NO_HOLES enabled). If we find our destination inode only has one inline
  3284. * extent, just overwrite it with the source inline extent if its size is less
  3285. * than the source extent's size, or we could copy the source inline extent's
  3286. * data into the destination inode's inline extent if the later is greater then
  3287. * the former.
  3288. */
  3289. static int clone_copy_inline_extent(struct inode *dst,
  3290. struct btrfs_trans_handle *trans,
  3291. struct btrfs_path *path,
  3292. struct btrfs_key *new_key,
  3293. const u64 drop_start,
  3294. const u64 datal,
  3295. const u64 skip,
  3296. const u64 size,
  3297. char *inline_data)
  3298. {
  3299. struct btrfs_fs_info *fs_info = btrfs_sb(dst->i_sb);
  3300. struct btrfs_root *root = BTRFS_I(dst)->root;
  3301. const u64 aligned_end = ALIGN(new_key->offset + datal,
  3302. fs_info->sectorsize);
  3303. int ret;
  3304. struct btrfs_key key;
  3305. if (new_key->offset > 0)
  3306. return -EOPNOTSUPP;
  3307. key.objectid = btrfs_ino(BTRFS_I(dst));
  3308. key.type = BTRFS_EXTENT_DATA_KEY;
  3309. key.offset = 0;
  3310. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  3311. if (ret < 0) {
  3312. return ret;
  3313. } else if (ret > 0) {
  3314. if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
  3315. ret = btrfs_next_leaf(root, path);
  3316. if (ret < 0)
  3317. return ret;
  3318. else if (ret > 0)
  3319. goto copy_inline_extent;
  3320. }
  3321. btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
  3322. if (key.objectid == btrfs_ino(BTRFS_I(dst)) &&
  3323. key.type == BTRFS_EXTENT_DATA_KEY) {
  3324. ASSERT(key.offset > 0);
  3325. return -EOPNOTSUPP;
  3326. }
  3327. } else if (i_size_read(dst) <= datal) {
  3328. struct btrfs_file_extent_item *ei;
  3329. u64 ext_len;
  3330. /*
  3331. * If the file size is <= datal, make sure there are no other
  3332. * extents following (can happen do to an fallocate call with
  3333. * the flag FALLOC_FL_KEEP_SIZE).
  3334. */
  3335. ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
  3336. struct btrfs_file_extent_item);
  3337. /*
  3338. * If it's an inline extent, it can not have other extents
  3339. * following it.
  3340. */
  3341. if (btrfs_file_extent_type(path->nodes[0], ei) ==
  3342. BTRFS_FILE_EXTENT_INLINE)
  3343. goto copy_inline_extent;
  3344. ext_len = btrfs_file_extent_num_bytes(path->nodes[0], ei);
  3345. if (ext_len > aligned_end)
  3346. return -EOPNOTSUPP;
  3347. ret = btrfs_next_item(root, path);
  3348. if (ret < 0) {
  3349. return ret;
  3350. } else if (ret == 0) {
  3351. btrfs_item_key_to_cpu(path->nodes[0], &key,
  3352. path->slots[0]);
  3353. if (key.objectid == btrfs_ino(BTRFS_I(dst)) &&
  3354. key.type == BTRFS_EXTENT_DATA_KEY)
  3355. return -EOPNOTSUPP;
  3356. }
  3357. }
  3358. copy_inline_extent:
  3359. /*
  3360. * We have no extent items, or we have an extent at offset 0 which may
  3361. * or may not be inlined. All these cases are dealt the same way.
  3362. */
  3363. if (i_size_read(dst) > datal) {
  3364. /*
  3365. * If the destination inode has an inline extent...
  3366. * This would require copying the data from the source inline
  3367. * extent into the beginning of the destination's inline extent.
  3368. * But this is really complex, both extents can be compressed
  3369. * or just one of them, which would require decompressing and
  3370. * re-compressing data (which could increase the new compressed
  3371. * size, not allowing the compressed data to fit anymore in an
  3372. * inline extent).
  3373. * So just don't support this case for now (it should be rare,
  3374. * we are not really saving space when cloning inline extents).
  3375. */
  3376. return -EOPNOTSUPP;
  3377. }
  3378. btrfs_release_path(path);
  3379. ret = btrfs_drop_extents(trans, root, dst, drop_start, aligned_end, 1);
  3380. if (ret)
  3381. return ret;
  3382. ret = btrfs_insert_empty_item(trans, root, path, new_key, size);
  3383. if (ret)
  3384. return ret;
  3385. if (skip) {
  3386. const u32 start = btrfs_file_extent_calc_inline_size(0);
  3387. memmove(inline_data + start, inline_data + start + skip, datal);
  3388. }
  3389. write_extent_buffer(path->nodes[0], inline_data,
  3390. btrfs_item_ptr_offset(path->nodes[0],
  3391. path->slots[0]),
  3392. size);
  3393. inode_add_bytes(dst, datal);
  3394. return 0;
  3395. }
  3396. /**
  3397. * btrfs_clone() - clone a range from inode file to another
  3398. *
  3399. * @src: Inode to clone from
  3400. * @inode: Inode to clone to
  3401. * @off: Offset within source to start clone from
  3402. * @olen: Original length, passed by user, of range to clone
  3403. * @olen_aligned: Block-aligned value of olen
  3404. * @destoff: Offset within @inode to start clone
  3405. * @no_time_update: Whether to update mtime/ctime on the target inode
  3406. */
  3407. static int btrfs_clone(struct inode *src, struct inode *inode,
  3408. const u64 off, const u64 olen, const u64 olen_aligned,
  3409. const u64 destoff, int no_time_update)
  3410. {
  3411. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  3412. struct btrfs_root *root = BTRFS_I(inode)->root;
  3413. struct btrfs_path *path = NULL;
  3414. struct extent_buffer *leaf;
  3415. struct btrfs_trans_handle *trans;
  3416. char *buf = NULL;
  3417. struct btrfs_key key;
  3418. u32 nritems;
  3419. int slot;
  3420. int ret;
  3421. const u64 len = olen_aligned;
  3422. u64 last_dest_end = destoff;
  3423. ret = -ENOMEM;
  3424. buf = kvmalloc(fs_info->nodesize, GFP_KERNEL);
  3425. if (!buf)
  3426. return ret;
  3427. path = btrfs_alloc_path();
  3428. if (!path) {
  3429. kvfree(buf);
  3430. return ret;
  3431. }
  3432. path->reada = READA_FORWARD;
  3433. /* clone data */
  3434. key.objectid = btrfs_ino(BTRFS_I(src));
  3435. key.type = BTRFS_EXTENT_DATA_KEY;
  3436. key.offset = off;
  3437. while (1) {
  3438. u64 next_key_min_offset = key.offset + 1;
  3439. /*
  3440. * note the key will change type as we walk through the
  3441. * tree.
  3442. */
  3443. path->leave_spinning = 1;
  3444. ret = btrfs_search_slot(NULL, BTRFS_I(src)->root, &key, path,
  3445. 0, 0);
  3446. if (ret < 0)
  3447. goto out;
  3448. /*
  3449. * First search, if no extent item that starts at offset off was
  3450. * found but the previous item is an extent item, it's possible
  3451. * it might overlap our target range, therefore process it.
  3452. */
  3453. if (key.offset == off && ret > 0 && path->slots[0] > 0) {
  3454. btrfs_item_key_to_cpu(path->nodes[0], &key,
  3455. path->slots[0] - 1);
  3456. if (key.type == BTRFS_EXTENT_DATA_KEY)
  3457. path->slots[0]--;
  3458. }
  3459. nritems = btrfs_header_nritems(path->nodes[0]);
  3460. process_slot:
  3461. if (path->slots[0] >= nritems) {
  3462. ret = btrfs_next_leaf(BTRFS_I(src)->root, path);
  3463. if (ret < 0)
  3464. goto out;
  3465. if (ret > 0)
  3466. break;
  3467. nritems = btrfs_header_nritems(path->nodes[0]);
  3468. }
  3469. leaf = path->nodes[0];
  3470. slot = path->slots[0];
  3471. btrfs_item_key_to_cpu(leaf, &key, slot);
  3472. if (key.type > BTRFS_EXTENT_DATA_KEY ||
  3473. key.objectid != btrfs_ino(BTRFS_I(src)))
  3474. break;
  3475. if (key.type == BTRFS_EXTENT_DATA_KEY) {
  3476. struct btrfs_file_extent_item *extent;
  3477. int type;
  3478. u32 size;
  3479. struct btrfs_key new_key;
  3480. u64 disko = 0, diskl = 0;
  3481. u64 datao = 0, datal = 0;
  3482. u8 comp;
  3483. u64 drop_start;
  3484. extent = btrfs_item_ptr(leaf, slot,
  3485. struct btrfs_file_extent_item);
  3486. comp = btrfs_file_extent_compression(leaf, extent);
  3487. type = btrfs_file_extent_type(leaf, extent);
  3488. if (type == BTRFS_FILE_EXTENT_REG ||
  3489. type == BTRFS_FILE_EXTENT_PREALLOC) {
  3490. disko = btrfs_file_extent_disk_bytenr(leaf,
  3491. extent);
  3492. diskl = btrfs_file_extent_disk_num_bytes(leaf,
  3493. extent);
  3494. datao = btrfs_file_extent_offset(leaf, extent);
  3495. datal = btrfs_file_extent_num_bytes(leaf,
  3496. extent);
  3497. } else if (type == BTRFS_FILE_EXTENT_INLINE) {
  3498. /* take upper bound, may be compressed */
  3499. datal = btrfs_file_extent_ram_bytes(leaf,
  3500. extent);
  3501. }
  3502. /*
  3503. * The first search might have left us at an extent
  3504. * item that ends before our target range's start, can
  3505. * happen if we have holes and NO_HOLES feature enabled.
  3506. */
  3507. if (key.offset + datal <= off) {
  3508. path->slots[0]++;
  3509. goto process_slot;
  3510. } else if (key.offset >= off + len) {
  3511. break;
  3512. }
  3513. next_key_min_offset = key.offset + datal;
  3514. size = btrfs_item_size_nr(leaf, slot);
  3515. read_extent_buffer(leaf, buf,
  3516. btrfs_item_ptr_offset(leaf, slot),
  3517. size);
  3518. btrfs_release_path(path);
  3519. path->leave_spinning = 0;
  3520. memcpy(&new_key, &key, sizeof(new_key));
  3521. new_key.objectid = btrfs_ino(BTRFS_I(inode));
  3522. if (off <= key.offset)
  3523. new_key.offset = key.offset + destoff - off;
  3524. else
  3525. new_key.offset = destoff;
  3526. /*
  3527. * Deal with a hole that doesn't have an extent item
  3528. * that represents it (NO_HOLES feature enabled).
  3529. * This hole is either in the middle of the cloning
  3530. * range or at the beginning (fully overlaps it or
  3531. * partially overlaps it).
  3532. */
  3533. if (new_key.offset != last_dest_end)
  3534. drop_start = last_dest_end;
  3535. else
  3536. drop_start = new_key.offset;
  3537. /*
  3538. * 1 - adjusting old extent (we may have to split it)
  3539. * 1 - add new extent
  3540. * 1 - inode update
  3541. */
  3542. trans = btrfs_start_transaction(root, 3);
  3543. if (IS_ERR(trans)) {
  3544. ret = PTR_ERR(trans);
  3545. goto out;
  3546. }
  3547. if (type == BTRFS_FILE_EXTENT_REG ||
  3548. type == BTRFS_FILE_EXTENT_PREALLOC) {
  3549. /*
  3550. * a | --- range to clone ---| b
  3551. * | ------------- extent ------------- |
  3552. */
  3553. /* subtract range b */
  3554. if (key.offset + datal > off + len)
  3555. datal = off + len - key.offset;
  3556. /* subtract range a */
  3557. if (off > key.offset) {
  3558. datao += off - key.offset;
  3559. datal -= off - key.offset;
  3560. }
  3561. ret = btrfs_drop_extents(trans, root, inode,
  3562. drop_start,
  3563. new_key.offset + datal,
  3564. 1);
  3565. if (ret) {
  3566. if (ret != -EOPNOTSUPP)
  3567. btrfs_abort_transaction(trans,
  3568. ret);
  3569. btrfs_end_transaction(trans);
  3570. goto out;
  3571. }
  3572. ret = btrfs_insert_empty_item(trans, root, path,
  3573. &new_key, size);
  3574. if (ret) {
  3575. btrfs_abort_transaction(trans, ret);
  3576. btrfs_end_transaction(trans);
  3577. goto out;
  3578. }
  3579. leaf = path->nodes[0];
  3580. slot = path->slots[0];
  3581. write_extent_buffer(leaf, buf,
  3582. btrfs_item_ptr_offset(leaf, slot),
  3583. size);
  3584. extent = btrfs_item_ptr(leaf, slot,
  3585. struct btrfs_file_extent_item);
  3586. /* disko == 0 means it's a hole */
  3587. if (!disko)
  3588. datao = 0;
  3589. btrfs_set_file_extent_offset(leaf, extent,
  3590. datao);
  3591. btrfs_set_file_extent_num_bytes(leaf, extent,
  3592. datal);
  3593. if (disko) {
  3594. inode_add_bytes(inode, datal);
  3595. ret = btrfs_inc_extent_ref(trans,
  3596. root,
  3597. disko, diskl, 0,
  3598. root->root_key.objectid,
  3599. btrfs_ino(BTRFS_I(inode)),
  3600. new_key.offset - datao);
  3601. if (ret) {
  3602. btrfs_abort_transaction(trans,
  3603. ret);
  3604. btrfs_end_transaction(trans);
  3605. goto out;
  3606. }
  3607. }
  3608. } else if (type == BTRFS_FILE_EXTENT_INLINE) {
  3609. u64 skip = 0;
  3610. u64 trim = 0;
  3611. if (off > key.offset) {
  3612. skip = off - key.offset;
  3613. new_key.offset += skip;
  3614. }
  3615. if (key.offset + datal > off + len)
  3616. trim = key.offset + datal - (off + len);
  3617. if (comp && (skip || trim)) {
  3618. ret = -EINVAL;
  3619. btrfs_end_transaction(trans);
  3620. goto out;
  3621. }
  3622. size -= skip + trim;
  3623. datal -= skip + trim;
  3624. ret = clone_copy_inline_extent(inode,
  3625. trans, path,
  3626. &new_key,
  3627. drop_start,
  3628. datal,
  3629. skip, size, buf);
  3630. if (ret) {
  3631. if (ret != -EOPNOTSUPP)
  3632. btrfs_abort_transaction(trans,
  3633. ret);
  3634. btrfs_end_transaction(trans);
  3635. goto out;
  3636. }
  3637. leaf = path->nodes[0];
  3638. slot = path->slots[0];
  3639. }
  3640. /* If we have an implicit hole (NO_HOLES feature). */
  3641. if (drop_start < new_key.offset)
  3642. clone_update_extent_map(BTRFS_I(inode), trans,
  3643. NULL, drop_start,
  3644. new_key.offset - drop_start);
  3645. clone_update_extent_map(BTRFS_I(inode), trans,
  3646. path, 0, 0);
  3647. btrfs_mark_buffer_dirty(leaf);
  3648. btrfs_release_path(path);
  3649. last_dest_end = ALIGN(new_key.offset + datal,
  3650. fs_info->sectorsize);
  3651. ret = clone_finish_inode_update(trans, inode,
  3652. last_dest_end,
  3653. destoff, olen,
  3654. no_time_update);
  3655. if (ret)
  3656. goto out;
  3657. if (new_key.offset + datal >= destoff + len)
  3658. break;
  3659. }
  3660. btrfs_release_path(path);
  3661. key.offset = next_key_min_offset;
  3662. if (fatal_signal_pending(current)) {
  3663. ret = -EINTR;
  3664. goto out;
  3665. }
  3666. cond_resched();
  3667. }
  3668. ret = 0;
  3669. if (last_dest_end < destoff + len) {
  3670. /*
  3671. * We have an implicit hole (NO_HOLES feature is enabled) that
  3672. * fully or partially overlaps our cloning range at its end.
  3673. */
  3674. btrfs_release_path(path);
  3675. /*
  3676. * 1 - remove extent(s)
  3677. * 1 - inode update
  3678. */
  3679. trans = btrfs_start_transaction(root, 2);
  3680. if (IS_ERR(trans)) {
  3681. ret = PTR_ERR(trans);
  3682. goto out;
  3683. }
  3684. ret = btrfs_drop_extents(trans, root, inode,
  3685. last_dest_end, destoff + len, 1);
  3686. if (ret) {
  3687. if (ret != -EOPNOTSUPP)
  3688. btrfs_abort_transaction(trans, ret);
  3689. btrfs_end_transaction(trans);
  3690. goto out;
  3691. }
  3692. clone_update_extent_map(BTRFS_I(inode), trans, NULL,
  3693. last_dest_end,
  3694. destoff + len - last_dest_end);
  3695. ret = clone_finish_inode_update(trans, inode, destoff + len,
  3696. destoff, olen, no_time_update);
  3697. }
  3698. out:
  3699. btrfs_free_path(path);
  3700. kvfree(buf);
  3701. return ret;
  3702. }
  3703. static noinline int btrfs_clone_files(struct file *file, struct file *file_src,
  3704. u64 off, u64 olen, u64 destoff)
  3705. {
  3706. struct inode *inode = file_inode(file);
  3707. struct inode *src = file_inode(file_src);
  3708. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  3709. struct btrfs_root *root = BTRFS_I(inode)->root;
  3710. int ret;
  3711. u64 len = olen;
  3712. u64 bs = fs_info->sb->s_blocksize;
  3713. int same_inode = src == inode;
  3714. /*
  3715. * TODO:
  3716. * - split compressed inline extents. annoying: we need to
  3717. * decompress into destination's address_space (the file offset
  3718. * may change, so source mapping won't do), then recompress (or
  3719. * otherwise reinsert) a subrange.
  3720. *
  3721. * - split destination inode's inline extents. The inline extents can
  3722. * be either compressed or non-compressed.
  3723. */
  3724. if (btrfs_root_readonly(root))
  3725. return -EROFS;
  3726. if (file_src->f_path.mnt != file->f_path.mnt ||
  3727. src->i_sb != inode->i_sb)
  3728. return -EXDEV;
  3729. if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
  3730. return -EISDIR;
  3731. if (!same_inode) {
  3732. btrfs_double_inode_lock(src, inode);
  3733. } else {
  3734. inode_lock(src);
  3735. }
  3736. /* don't make the dst file partly checksummed */
  3737. if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
  3738. (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
  3739. ret = -EINVAL;
  3740. goto out_unlock;
  3741. }
  3742. /* determine range to clone */
  3743. ret = -EINVAL;
  3744. if (off + len > src->i_size || off + len < off)
  3745. goto out_unlock;
  3746. if (len == 0)
  3747. olen = len = src->i_size - off;
  3748. /*
  3749. * If we extend to eof, continue to block boundary if and only if the
  3750. * destination end offset matches the destination file's size, otherwise
  3751. * we would be corrupting data by placing the eof block into the middle
  3752. * of a file.
  3753. */
  3754. if (off + len == src->i_size) {
  3755. if (!IS_ALIGNED(len, bs) && destoff + len < inode->i_size)
  3756. goto out_unlock;
  3757. len = ALIGN(src->i_size, bs) - off;
  3758. }
  3759. if (len == 0) {
  3760. ret = 0;
  3761. goto out_unlock;
  3762. }
  3763. /* verify the end result is block aligned */
  3764. if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) ||
  3765. !IS_ALIGNED(destoff, bs))
  3766. goto out_unlock;
  3767. /* verify if ranges are overlapped within the same file */
  3768. if (same_inode) {
  3769. if (destoff + len > off && destoff < off + len)
  3770. goto out_unlock;
  3771. }
  3772. if (destoff > inode->i_size) {
  3773. ret = btrfs_cont_expand(inode, inode->i_size, destoff);
  3774. if (ret)
  3775. goto out_unlock;
  3776. }
  3777. /*
  3778. * Lock the target range too. Right after we replace the file extent
  3779. * items in the fs tree (which now point to the cloned data), we might
  3780. * have a worker replace them with extent items relative to a write
  3781. * operation that was issued before this clone operation (i.e. confront
  3782. * with inode.c:btrfs_finish_ordered_io).
  3783. */
  3784. if (same_inode) {
  3785. u64 lock_start = min_t(u64, off, destoff);
  3786. u64 lock_len = max_t(u64, off, destoff) + len - lock_start;
  3787. ret = lock_extent_range(src, lock_start, lock_len, true);
  3788. } else {
  3789. ret = btrfs_double_extent_lock(src, off, inode, destoff, len,
  3790. true);
  3791. }
  3792. ASSERT(ret == 0);
  3793. if (WARN_ON(ret)) {
  3794. /* ranges in the io trees already unlocked */
  3795. goto out_unlock;
  3796. }
  3797. ret = btrfs_clone(src, inode, off, olen, len, destoff, 0);
  3798. if (same_inode) {
  3799. u64 lock_start = min_t(u64, off, destoff);
  3800. u64 lock_end = max_t(u64, off, destoff) + len - 1;
  3801. unlock_extent(&BTRFS_I(src)->io_tree, lock_start, lock_end);
  3802. } else {
  3803. btrfs_double_extent_unlock(src, off, inode, destoff, len);
  3804. }
  3805. /*
  3806. * Truncate page cache pages so that future reads will see the cloned
  3807. * data immediately and not the previous data.
  3808. */
  3809. truncate_inode_pages_range(&inode->i_data,
  3810. round_down(destoff, PAGE_SIZE),
  3811. round_up(destoff + len, PAGE_SIZE) - 1);
  3812. out_unlock:
  3813. if (!same_inode)
  3814. btrfs_double_inode_unlock(src, inode);
  3815. else
  3816. inode_unlock(src);
  3817. return ret;
  3818. }
  3819. int btrfs_clone_file_range(struct file *src_file, loff_t off,
  3820. struct file *dst_file, loff_t destoff, u64 len)
  3821. {
  3822. return btrfs_clone_files(dst_file, src_file, off, len, destoff);
  3823. }
  3824. static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
  3825. {
  3826. struct inode *inode = file_inode(file);
  3827. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  3828. struct btrfs_root *root = BTRFS_I(inode)->root;
  3829. struct btrfs_root *new_root;
  3830. struct btrfs_dir_item *di;
  3831. struct btrfs_trans_handle *trans;
  3832. struct btrfs_path *path;
  3833. struct btrfs_key location;
  3834. struct btrfs_disk_key disk_key;
  3835. u64 objectid = 0;
  3836. u64 dir_id;
  3837. int ret;
  3838. if (!capable(CAP_SYS_ADMIN))
  3839. return -EPERM;
  3840. ret = mnt_want_write_file(file);
  3841. if (ret)
  3842. return ret;
  3843. if (copy_from_user(&objectid, argp, sizeof(objectid))) {
  3844. ret = -EFAULT;
  3845. goto out;
  3846. }
  3847. if (!objectid)
  3848. objectid = BTRFS_FS_TREE_OBJECTID;
  3849. location.objectid = objectid;
  3850. location.type = BTRFS_ROOT_ITEM_KEY;
  3851. location.offset = (u64)-1;
  3852. new_root = btrfs_read_fs_root_no_name(fs_info, &location);
  3853. if (IS_ERR(new_root)) {
  3854. ret = PTR_ERR(new_root);
  3855. goto out;
  3856. }
  3857. if (!is_fstree(new_root->objectid)) {
  3858. ret = -ENOENT;
  3859. goto out;
  3860. }
  3861. path = btrfs_alloc_path();
  3862. if (!path) {
  3863. ret = -ENOMEM;
  3864. goto out;
  3865. }
  3866. path->leave_spinning = 1;
  3867. trans = btrfs_start_transaction(root, 1);
  3868. if (IS_ERR(trans)) {
  3869. btrfs_free_path(path);
  3870. ret = PTR_ERR(trans);
  3871. goto out;
  3872. }
  3873. dir_id = btrfs_super_root_dir(fs_info->super_copy);
  3874. di = btrfs_lookup_dir_item(trans, fs_info->tree_root, path,
  3875. dir_id, "default", 7, 1);
  3876. if (IS_ERR_OR_NULL(di)) {
  3877. btrfs_free_path(path);
  3878. btrfs_end_transaction(trans);
  3879. btrfs_err(fs_info,
  3880. "Umm, you don't have the default diritem, this isn't going to work");
  3881. ret = -ENOENT;
  3882. goto out;
  3883. }
  3884. btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
  3885. btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
  3886. btrfs_mark_buffer_dirty(path->nodes[0]);
  3887. btrfs_free_path(path);
  3888. btrfs_set_fs_incompat(fs_info, DEFAULT_SUBVOL);
  3889. btrfs_end_transaction(trans);
  3890. out:
  3891. mnt_drop_write_file(file);
  3892. return ret;
  3893. }
  3894. static void get_block_group_info(struct list_head *groups_list,
  3895. struct btrfs_ioctl_space_info *space)
  3896. {
  3897. struct btrfs_block_group_cache *block_group;
  3898. space->total_bytes = 0;
  3899. space->used_bytes = 0;
  3900. space->flags = 0;
  3901. list_for_each_entry(block_group, groups_list, list) {
  3902. space->flags = block_group->flags;
  3903. space->total_bytes += block_group->key.offset;
  3904. space->used_bytes +=
  3905. btrfs_block_group_used(&block_group->item);
  3906. }
  3907. }
  3908. static long btrfs_ioctl_space_info(struct btrfs_fs_info *fs_info,
  3909. void __user *arg)
  3910. {
  3911. struct btrfs_ioctl_space_args space_args;
  3912. struct btrfs_ioctl_space_info space;
  3913. struct btrfs_ioctl_space_info *dest;
  3914. struct btrfs_ioctl_space_info *dest_orig;
  3915. struct btrfs_ioctl_space_info __user *user_dest;
  3916. struct btrfs_space_info *info;
  3917. static const u64 types[] = {
  3918. BTRFS_BLOCK_GROUP_DATA,
  3919. BTRFS_BLOCK_GROUP_SYSTEM,
  3920. BTRFS_BLOCK_GROUP_METADATA,
  3921. BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA
  3922. };
  3923. int num_types = 4;
  3924. int alloc_size;
  3925. int ret = 0;
  3926. u64 slot_count = 0;
  3927. int i, c;
  3928. if (copy_from_user(&space_args,
  3929. (struct btrfs_ioctl_space_args __user *)arg,
  3930. sizeof(space_args)))
  3931. return -EFAULT;
  3932. for (i = 0; i < num_types; i++) {
  3933. struct btrfs_space_info *tmp;
  3934. info = NULL;
  3935. rcu_read_lock();
  3936. list_for_each_entry_rcu(tmp, &fs_info->space_info,
  3937. list) {
  3938. if (tmp->flags == types[i]) {
  3939. info = tmp;
  3940. break;
  3941. }
  3942. }
  3943. rcu_read_unlock();
  3944. if (!info)
  3945. continue;
  3946. down_read(&info->groups_sem);
  3947. for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
  3948. if (!list_empty(&info->block_groups[c]))
  3949. slot_count++;
  3950. }
  3951. up_read(&info->groups_sem);
  3952. }
  3953. /*
  3954. * Global block reserve, exported as a space_info
  3955. */
  3956. slot_count++;
  3957. /* space_slots == 0 means they are asking for a count */
  3958. if (space_args.space_slots == 0) {
  3959. space_args.total_spaces = slot_count;
  3960. goto out;
  3961. }
  3962. slot_count = min_t(u64, space_args.space_slots, slot_count);
  3963. alloc_size = sizeof(*dest) * slot_count;
  3964. /* we generally have at most 6 or so space infos, one for each raid
  3965. * level. So, a whole page should be more than enough for everyone
  3966. */
  3967. if (alloc_size > PAGE_SIZE)
  3968. return -ENOMEM;
  3969. space_args.total_spaces = 0;
  3970. dest = kmalloc(alloc_size, GFP_KERNEL);
  3971. if (!dest)
  3972. return -ENOMEM;
  3973. dest_orig = dest;
  3974. /* now we have a buffer to copy into */
  3975. for (i = 0; i < num_types; i++) {
  3976. struct btrfs_space_info *tmp;
  3977. if (!slot_count)
  3978. break;
  3979. info = NULL;
  3980. rcu_read_lock();
  3981. list_for_each_entry_rcu(tmp, &fs_info->space_info,
  3982. list) {
  3983. if (tmp->flags == types[i]) {
  3984. info = tmp;
  3985. break;
  3986. }
  3987. }
  3988. rcu_read_unlock();
  3989. if (!info)
  3990. continue;
  3991. down_read(&info->groups_sem);
  3992. for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
  3993. if (!list_empty(&info->block_groups[c])) {
  3994. get_block_group_info(&info->block_groups[c],
  3995. &space);
  3996. memcpy(dest, &space, sizeof(space));
  3997. dest++;
  3998. space_args.total_spaces++;
  3999. slot_count--;
  4000. }
  4001. if (!slot_count)
  4002. break;
  4003. }
  4004. up_read(&info->groups_sem);
  4005. }
  4006. /*
  4007. * Add global block reserve
  4008. */
  4009. if (slot_count) {
  4010. struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
  4011. spin_lock(&block_rsv->lock);
  4012. space.total_bytes = block_rsv->size;
  4013. space.used_bytes = block_rsv->size - block_rsv->reserved;
  4014. spin_unlock(&block_rsv->lock);
  4015. space.flags = BTRFS_SPACE_INFO_GLOBAL_RSV;
  4016. memcpy(dest, &space, sizeof(space));
  4017. space_args.total_spaces++;
  4018. }
  4019. user_dest = (struct btrfs_ioctl_space_info __user *)
  4020. (arg + sizeof(struct btrfs_ioctl_space_args));
  4021. if (copy_to_user(user_dest, dest_orig, alloc_size))
  4022. ret = -EFAULT;
  4023. kfree(dest_orig);
  4024. out:
  4025. if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
  4026. ret = -EFAULT;
  4027. return ret;
  4028. }
  4029. static noinline long btrfs_ioctl_start_sync(struct btrfs_root *root,
  4030. void __user *argp)
  4031. {
  4032. struct btrfs_trans_handle *trans;
  4033. u64 transid;
  4034. int ret;
  4035. trans = btrfs_attach_transaction_barrier(root);
  4036. if (IS_ERR(trans)) {
  4037. if (PTR_ERR(trans) != -ENOENT)
  4038. return PTR_ERR(trans);
  4039. /* No running transaction, don't bother */
  4040. transid = root->fs_info->last_trans_committed;
  4041. goto out;
  4042. }
  4043. transid = trans->transid;
  4044. ret = btrfs_commit_transaction_async(trans, 0);
  4045. if (ret) {
  4046. btrfs_end_transaction(trans);
  4047. return ret;
  4048. }
  4049. out:
  4050. if (argp)
  4051. if (copy_to_user(argp, &transid, sizeof(transid)))
  4052. return -EFAULT;
  4053. return 0;
  4054. }
  4055. static noinline long btrfs_ioctl_wait_sync(struct btrfs_fs_info *fs_info,
  4056. void __user *argp)
  4057. {
  4058. u64 transid;
  4059. if (argp) {
  4060. if (copy_from_user(&transid, argp, sizeof(transid)))
  4061. return -EFAULT;
  4062. } else {
  4063. transid = 0; /* current trans */
  4064. }
  4065. return btrfs_wait_for_commit(fs_info, transid);
  4066. }
  4067. static long btrfs_ioctl_scrub(struct file *file, void __user *arg)
  4068. {
  4069. struct btrfs_fs_info *fs_info = btrfs_sb(file_inode(file)->i_sb);
  4070. struct btrfs_ioctl_scrub_args *sa;
  4071. int ret;
  4072. if (!capable(CAP_SYS_ADMIN))
  4073. return -EPERM;
  4074. sa = memdup_user(arg, sizeof(*sa));
  4075. if (IS_ERR(sa))
  4076. return PTR_ERR(sa);
  4077. if (!(sa->flags & BTRFS_SCRUB_READONLY)) {
  4078. ret = mnt_want_write_file(file);
  4079. if (ret)
  4080. goto out;
  4081. }
  4082. ret = btrfs_scrub_dev(fs_info, sa->devid, sa->start, sa->end,
  4083. &sa->progress, sa->flags & BTRFS_SCRUB_READONLY,
  4084. 0);
  4085. if (copy_to_user(arg, sa, sizeof(*sa)))
  4086. ret = -EFAULT;
  4087. if (!(sa->flags & BTRFS_SCRUB_READONLY))
  4088. mnt_drop_write_file(file);
  4089. out:
  4090. kfree(sa);
  4091. return ret;
  4092. }
  4093. static long btrfs_ioctl_scrub_cancel(struct btrfs_fs_info *fs_info)
  4094. {
  4095. if (!capable(CAP_SYS_ADMIN))
  4096. return -EPERM;
  4097. return btrfs_scrub_cancel(fs_info);
  4098. }
  4099. static long btrfs_ioctl_scrub_progress(struct btrfs_fs_info *fs_info,
  4100. void __user *arg)
  4101. {
  4102. struct btrfs_ioctl_scrub_args *sa;
  4103. int ret;
  4104. if (!capable(CAP_SYS_ADMIN))
  4105. return -EPERM;
  4106. sa = memdup_user(arg, sizeof(*sa));
  4107. if (IS_ERR(sa))
  4108. return PTR_ERR(sa);
  4109. ret = btrfs_scrub_progress(fs_info, sa->devid, &sa->progress);
  4110. if (copy_to_user(arg, sa, sizeof(*sa)))
  4111. ret = -EFAULT;
  4112. kfree(sa);
  4113. return ret;
  4114. }
  4115. static long btrfs_ioctl_get_dev_stats(struct btrfs_fs_info *fs_info,
  4116. void __user *arg)
  4117. {
  4118. struct btrfs_ioctl_get_dev_stats *sa;
  4119. int ret;
  4120. sa = memdup_user(arg, sizeof(*sa));
  4121. if (IS_ERR(sa))
  4122. return PTR_ERR(sa);
  4123. if ((sa->flags & BTRFS_DEV_STATS_RESET) && !capable(CAP_SYS_ADMIN)) {
  4124. kfree(sa);
  4125. return -EPERM;
  4126. }
  4127. ret = btrfs_get_dev_stats(fs_info, sa);
  4128. if (copy_to_user(arg, sa, sizeof(*sa)))
  4129. ret = -EFAULT;
  4130. kfree(sa);
  4131. return ret;
  4132. }
  4133. static long btrfs_ioctl_dev_replace(struct btrfs_fs_info *fs_info,
  4134. void __user *arg)
  4135. {
  4136. struct btrfs_ioctl_dev_replace_args *p;
  4137. int ret;
  4138. if (!capable(CAP_SYS_ADMIN))
  4139. return -EPERM;
  4140. p = memdup_user(arg, sizeof(*p));
  4141. if (IS_ERR(p))
  4142. return PTR_ERR(p);
  4143. switch (p->cmd) {
  4144. case BTRFS_IOCTL_DEV_REPLACE_CMD_START:
  4145. if (sb_rdonly(fs_info->sb)) {
  4146. ret = -EROFS;
  4147. goto out;
  4148. }
  4149. if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
  4150. ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
  4151. } else {
  4152. ret = btrfs_dev_replace_by_ioctl(fs_info, p);
  4153. clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
  4154. }
  4155. break;
  4156. case BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS:
  4157. btrfs_dev_replace_status(fs_info, p);
  4158. ret = 0;
  4159. break;
  4160. case BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL:
  4161. p->result = btrfs_dev_replace_cancel(fs_info);
  4162. ret = 0;
  4163. break;
  4164. default:
  4165. ret = -EINVAL;
  4166. break;
  4167. }
  4168. if (copy_to_user(arg, p, sizeof(*p)))
  4169. ret = -EFAULT;
  4170. out:
  4171. kfree(p);
  4172. return ret;
  4173. }
  4174. static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg)
  4175. {
  4176. int ret = 0;
  4177. int i;
  4178. u64 rel_ptr;
  4179. int size;
  4180. struct btrfs_ioctl_ino_path_args *ipa = NULL;
  4181. struct inode_fs_paths *ipath = NULL;
  4182. struct btrfs_path *path;
  4183. if (!capable(CAP_DAC_READ_SEARCH))
  4184. return -EPERM;
  4185. path = btrfs_alloc_path();
  4186. if (!path) {
  4187. ret = -ENOMEM;
  4188. goto out;
  4189. }
  4190. ipa = memdup_user(arg, sizeof(*ipa));
  4191. if (IS_ERR(ipa)) {
  4192. ret = PTR_ERR(ipa);
  4193. ipa = NULL;
  4194. goto out;
  4195. }
  4196. size = min_t(u32, ipa->size, 4096);
  4197. ipath = init_ipath(size, root, path);
  4198. if (IS_ERR(ipath)) {
  4199. ret = PTR_ERR(ipath);
  4200. ipath = NULL;
  4201. goto out;
  4202. }
  4203. ret = paths_from_inode(ipa->inum, ipath);
  4204. if (ret < 0)
  4205. goto out;
  4206. for (i = 0; i < ipath->fspath->elem_cnt; ++i) {
  4207. rel_ptr = ipath->fspath->val[i] -
  4208. (u64)(unsigned long)ipath->fspath->val;
  4209. ipath->fspath->val[i] = rel_ptr;
  4210. }
  4211. ret = copy_to_user((void __user *)(unsigned long)ipa->fspath,
  4212. ipath->fspath, size);
  4213. if (ret) {
  4214. ret = -EFAULT;
  4215. goto out;
  4216. }
  4217. out:
  4218. btrfs_free_path(path);
  4219. free_ipath(ipath);
  4220. kfree(ipa);
  4221. return ret;
  4222. }
  4223. static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx)
  4224. {
  4225. struct btrfs_data_container *inodes = ctx;
  4226. const size_t c = 3 * sizeof(u64);
  4227. if (inodes->bytes_left >= c) {
  4228. inodes->bytes_left -= c;
  4229. inodes->val[inodes->elem_cnt] = inum;
  4230. inodes->val[inodes->elem_cnt + 1] = offset;
  4231. inodes->val[inodes->elem_cnt + 2] = root;
  4232. inodes->elem_cnt += 3;
  4233. } else {
  4234. inodes->bytes_missing += c - inodes->bytes_left;
  4235. inodes->bytes_left = 0;
  4236. inodes->elem_missed += 3;
  4237. }
  4238. return 0;
  4239. }
  4240. static long btrfs_ioctl_logical_to_ino(struct btrfs_fs_info *fs_info,
  4241. void __user *arg, int version)
  4242. {
  4243. int ret = 0;
  4244. int size;
  4245. struct btrfs_ioctl_logical_ino_args *loi;
  4246. struct btrfs_data_container *inodes = NULL;
  4247. struct btrfs_path *path = NULL;
  4248. bool ignore_offset;
  4249. if (!capable(CAP_SYS_ADMIN))
  4250. return -EPERM;
  4251. loi = memdup_user(arg, sizeof(*loi));
  4252. if (IS_ERR(loi))
  4253. return PTR_ERR(loi);
  4254. if (version == 1) {
  4255. ignore_offset = false;
  4256. size = min_t(u32, loi->size, SZ_64K);
  4257. } else {
  4258. /* All reserved bits must be 0 for now */
  4259. if (memchr_inv(loi->reserved, 0, sizeof(loi->reserved))) {
  4260. ret = -EINVAL;
  4261. goto out_loi;
  4262. }
  4263. /* Only accept flags we have defined so far */
  4264. if (loi->flags & ~(BTRFS_LOGICAL_INO_ARGS_IGNORE_OFFSET)) {
  4265. ret = -EINVAL;
  4266. goto out_loi;
  4267. }
  4268. ignore_offset = loi->flags & BTRFS_LOGICAL_INO_ARGS_IGNORE_OFFSET;
  4269. size = min_t(u32, loi->size, SZ_16M);
  4270. }
  4271. path = btrfs_alloc_path();
  4272. if (!path) {
  4273. ret = -ENOMEM;
  4274. goto out;
  4275. }
  4276. inodes = init_data_container(size);
  4277. if (IS_ERR(inodes)) {
  4278. ret = PTR_ERR(inodes);
  4279. inodes = NULL;
  4280. goto out;
  4281. }
  4282. ret = iterate_inodes_from_logical(loi->logical, fs_info, path,
  4283. build_ino_list, inodes, ignore_offset);
  4284. if (ret == -EINVAL)
  4285. ret = -ENOENT;
  4286. if (ret < 0)
  4287. goto out;
  4288. ret = copy_to_user((void __user *)(unsigned long)loi->inodes, inodes,
  4289. size);
  4290. if (ret)
  4291. ret = -EFAULT;
  4292. out:
  4293. btrfs_free_path(path);
  4294. kvfree(inodes);
  4295. out_loi:
  4296. kfree(loi);
  4297. return ret;
  4298. }
  4299. void btrfs_update_ioctl_balance_args(struct btrfs_fs_info *fs_info,
  4300. struct btrfs_ioctl_balance_args *bargs)
  4301. {
  4302. struct btrfs_balance_control *bctl = fs_info->balance_ctl;
  4303. bargs->flags = bctl->flags;
  4304. if (test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags))
  4305. bargs->state |= BTRFS_BALANCE_STATE_RUNNING;
  4306. if (atomic_read(&fs_info->balance_pause_req))
  4307. bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ;
  4308. if (atomic_read(&fs_info->balance_cancel_req))
  4309. bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ;
  4310. memcpy(&bargs->data, &bctl->data, sizeof(bargs->data));
  4311. memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta));
  4312. memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys));
  4313. spin_lock(&fs_info->balance_lock);
  4314. memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
  4315. spin_unlock(&fs_info->balance_lock);
  4316. }
  4317. static long btrfs_ioctl_balance(struct file *file, void __user *arg)
  4318. {
  4319. struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
  4320. struct btrfs_fs_info *fs_info = root->fs_info;
  4321. struct btrfs_ioctl_balance_args *bargs;
  4322. struct btrfs_balance_control *bctl;
  4323. bool need_unlock; /* for mut. excl. ops lock */
  4324. int ret;
  4325. if (!capable(CAP_SYS_ADMIN))
  4326. return -EPERM;
  4327. ret = mnt_want_write_file(file);
  4328. if (ret)
  4329. return ret;
  4330. again:
  4331. if (!test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
  4332. mutex_lock(&fs_info->balance_mutex);
  4333. need_unlock = true;
  4334. goto locked;
  4335. }
  4336. /*
  4337. * mut. excl. ops lock is locked. Three possibilities:
  4338. * (1) some other op is running
  4339. * (2) balance is running
  4340. * (3) balance is paused -- special case (think resume)
  4341. */
  4342. mutex_lock(&fs_info->balance_mutex);
  4343. if (fs_info->balance_ctl) {
  4344. /* this is either (2) or (3) */
  4345. if (!test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) {
  4346. mutex_unlock(&fs_info->balance_mutex);
  4347. /*
  4348. * Lock released to allow other waiters to continue,
  4349. * we'll reexamine the status again.
  4350. */
  4351. mutex_lock(&fs_info->balance_mutex);
  4352. if (fs_info->balance_ctl &&
  4353. !test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) {
  4354. /* this is (3) */
  4355. need_unlock = false;
  4356. goto locked;
  4357. }
  4358. mutex_unlock(&fs_info->balance_mutex);
  4359. goto again;
  4360. } else {
  4361. /* this is (2) */
  4362. mutex_unlock(&fs_info->balance_mutex);
  4363. ret = -EINPROGRESS;
  4364. goto out;
  4365. }
  4366. } else {
  4367. /* this is (1) */
  4368. mutex_unlock(&fs_info->balance_mutex);
  4369. ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
  4370. goto out;
  4371. }
  4372. locked:
  4373. BUG_ON(!test_bit(BTRFS_FS_EXCL_OP, &fs_info->flags));
  4374. if (arg) {
  4375. bargs = memdup_user(arg, sizeof(*bargs));
  4376. if (IS_ERR(bargs)) {
  4377. ret = PTR_ERR(bargs);
  4378. goto out_unlock;
  4379. }
  4380. if (bargs->flags & BTRFS_BALANCE_RESUME) {
  4381. if (!fs_info->balance_ctl) {
  4382. ret = -ENOTCONN;
  4383. goto out_bargs;
  4384. }
  4385. bctl = fs_info->balance_ctl;
  4386. spin_lock(&fs_info->balance_lock);
  4387. bctl->flags |= BTRFS_BALANCE_RESUME;
  4388. spin_unlock(&fs_info->balance_lock);
  4389. goto do_balance;
  4390. }
  4391. } else {
  4392. bargs = NULL;
  4393. }
  4394. if (fs_info->balance_ctl) {
  4395. ret = -EINPROGRESS;
  4396. goto out_bargs;
  4397. }
  4398. bctl = kzalloc(sizeof(*bctl), GFP_KERNEL);
  4399. if (!bctl) {
  4400. ret = -ENOMEM;
  4401. goto out_bargs;
  4402. }
  4403. if (arg) {
  4404. memcpy(&bctl->data, &bargs->data, sizeof(bctl->data));
  4405. memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta));
  4406. memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys));
  4407. bctl->flags = bargs->flags;
  4408. } else {
  4409. /* balance everything - no filters */
  4410. bctl->flags |= BTRFS_BALANCE_TYPE_MASK;
  4411. }
  4412. if (bctl->flags & ~(BTRFS_BALANCE_ARGS_MASK | BTRFS_BALANCE_TYPE_MASK)) {
  4413. ret = -EINVAL;
  4414. goto out_bctl;
  4415. }
  4416. do_balance:
  4417. /*
  4418. * Ownership of bctl and filesystem flag BTRFS_FS_EXCL_OP goes to
  4419. * btrfs_balance. bctl is freed in reset_balance_state, or, if
  4420. * restriper was paused all the way until unmount, in free_fs_info.
  4421. * The flag should be cleared after reset_balance_state.
  4422. */
  4423. need_unlock = false;
  4424. ret = btrfs_balance(fs_info, bctl, bargs);
  4425. bctl = NULL;
  4426. if (arg) {
  4427. if (copy_to_user(arg, bargs, sizeof(*bargs)))
  4428. ret = -EFAULT;
  4429. }
  4430. out_bctl:
  4431. kfree(bctl);
  4432. out_bargs:
  4433. kfree(bargs);
  4434. out_unlock:
  4435. mutex_unlock(&fs_info->balance_mutex);
  4436. if (need_unlock)
  4437. clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
  4438. out:
  4439. mnt_drop_write_file(file);
  4440. return ret;
  4441. }
  4442. static long btrfs_ioctl_balance_ctl(struct btrfs_fs_info *fs_info, int cmd)
  4443. {
  4444. if (!capable(CAP_SYS_ADMIN))
  4445. return -EPERM;
  4446. switch (cmd) {
  4447. case BTRFS_BALANCE_CTL_PAUSE:
  4448. return btrfs_pause_balance(fs_info);
  4449. case BTRFS_BALANCE_CTL_CANCEL:
  4450. return btrfs_cancel_balance(fs_info);
  4451. }
  4452. return -EINVAL;
  4453. }
  4454. static long btrfs_ioctl_balance_progress(struct btrfs_fs_info *fs_info,
  4455. void __user *arg)
  4456. {
  4457. struct btrfs_ioctl_balance_args *bargs;
  4458. int ret = 0;
  4459. if (!capable(CAP_SYS_ADMIN))
  4460. return -EPERM;
  4461. mutex_lock(&fs_info->balance_mutex);
  4462. if (!fs_info->balance_ctl) {
  4463. ret = -ENOTCONN;
  4464. goto out;
  4465. }
  4466. bargs = kzalloc(sizeof(*bargs), GFP_KERNEL);
  4467. if (!bargs) {
  4468. ret = -ENOMEM;
  4469. goto out;
  4470. }
  4471. btrfs_update_ioctl_balance_args(fs_info, bargs);
  4472. if (copy_to_user(arg, bargs, sizeof(*bargs)))
  4473. ret = -EFAULT;
  4474. kfree(bargs);
  4475. out:
  4476. mutex_unlock(&fs_info->balance_mutex);
  4477. return ret;
  4478. }
  4479. static long btrfs_ioctl_quota_ctl(struct file *file, void __user *arg)
  4480. {
  4481. struct inode *inode = file_inode(file);
  4482. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  4483. struct btrfs_ioctl_quota_ctl_args *sa;
  4484. int ret;
  4485. if (!capable(CAP_SYS_ADMIN))
  4486. return -EPERM;
  4487. ret = mnt_want_write_file(file);
  4488. if (ret)
  4489. return ret;
  4490. sa = memdup_user(arg, sizeof(*sa));
  4491. if (IS_ERR(sa)) {
  4492. ret = PTR_ERR(sa);
  4493. goto drop_write;
  4494. }
  4495. down_write(&fs_info->subvol_sem);
  4496. switch (sa->cmd) {
  4497. case BTRFS_QUOTA_CTL_ENABLE:
  4498. ret = btrfs_quota_enable(fs_info);
  4499. break;
  4500. case BTRFS_QUOTA_CTL_DISABLE:
  4501. ret = btrfs_quota_disable(fs_info);
  4502. break;
  4503. default:
  4504. ret = -EINVAL;
  4505. break;
  4506. }
  4507. kfree(sa);
  4508. up_write(&fs_info->subvol_sem);
  4509. drop_write:
  4510. mnt_drop_write_file(file);
  4511. return ret;
  4512. }
  4513. static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg)
  4514. {
  4515. struct inode *inode = file_inode(file);
  4516. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  4517. struct btrfs_root *root = BTRFS_I(inode)->root;
  4518. struct btrfs_ioctl_qgroup_assign_args *sa;
  4519. struct btrfs_trans_handle *trans;
  4520. int ret;
  4521. int err;
  4522. if (!capable(CAP_SYS_ADMIN))
  4523. return -EPERM;
  4524. ret = mnt_want_write_file(file);
  4525. if (ret)
  4526. return ret;
  4527. sa = memdup_user(arg, sizeof(*sa));
  4528. if (IS_ERR(sa)) {
  4529. ret = PTR_ERR(sa);
  4530. goto drop_write;
  4531. }
  4532. trans = btrfs_join_transaction(root);
  4533. if (IS_ERR(trans)) {
  4534. ret = PTR_ERR(trans);
  4535. goto out;
  4536. }
  4537. if (sa->assign) {
  4538. ret = btrfs_add_qgroup_relation(trans, sa->src, sa->dst);
  4539. } else {
  4540. ret = btrfs_del_qgroup_relation(trans, sa->src, sa->dst);
  4541. }
  4542. /* update qgroup status and info */
  4543. err = btrfs_run_qgroups(trans);
  4544. if (err < 0)
  4545. btrfs_handle_fs_error(fs_info, err,
  4546. "failed to update qgroup status and info");
  4547. err = btrfs_end_transaction(trans);
  4548. if (err && !ret)
  4549. ret = err;
  4550. out:
  4551. kfree(sa);
  4552. drop_write:
  4553. mnt_drop_write_file(file);
  4554. return ret;
  4555. }
  4556. static long btrfs_ioctl_qgroup_create(struct file *file, void __user *arg)
  4557. {
  4558. struct inode *inode = file_inode(file);
  4559. struct btrfs_root *root = BTRFS_I(inode)->root;
  4560. struct btrfs_ioctl_qgroup_create_args *sa;
  4561. struct btrfs_trans_handle *trans;
  4562. int ret;
  4563. int err;
  4564. if (!capable(CAP_SYS_ADMIN))
  4565. return -EPERM;
  4566. ret = mnt_want_write_file(file);
  4567. if (ret)
  4568. return ret;
  4569. sa = memdup_user(arg, sizeof(*sa));
  4570. if (IS_ERR(sa)) {
  4571. ret = PTR_ERR(sa);
  4572. goto drop_write;
  4573. }
  4574. if (!sa->qgroupid) {
  4575. ret = -EINVAL;
  4576. goto out;
  4577. }
  4578. trans = btrfs_join_transaction(root);
  4579. if (IS_ERR(trans)) {
  4580. ret = PTR_ERR(trans);
  4581. goto out;
  4582. }
  4583. if (sa->create) {
  4584. ret = btrfs_create_qgroup(trans, sa->qgroupid);
  4585. } else {
  4586. ret = btrfs_remove_qgroup(trans, sa->qgroupid);
  4587. }
  4588. err = btrfs_end_transaction(trans);
  4589. if (err && !ret)
  4590. ret = err;
  4591. out:
  4592. kfree(sa);
  4593. drop_write:
  4594. mnt_drop_write_file(file);
  4595. return ret;
  4596. }
  4597. static long btrfs_ioctl_qgroup_limit(struct file *file, void __user *arg)
  4598. {
  4599. struct inode *inode = file_inode(file);
  4600. struct btrfs_root *root = BTRFS_I(inode)->root;
  4601. struct btrfs_ioctl_qgroup_limit_args *sa;
  4602. struct btrfs_trans_handle *trans;
  4603. int ret;
  4604. int err;
  4605. u64 qgroupid;
  4606. if (!capable(CAP_SYS_ADMIN))
  4607. return -EPERM;
  4608. ret = mnt_want_write_file(file);
  4609. if (ret)
  4610. return ret;
  4611. sa = memdup_user(arg, sizeof(*sa));
  4612. if (IS_ERR(sa)) {
  4613. ret = PTR_ERR(sa);
  4614. goto drop_write;
  4615. }
  4616. trans = btrfs_join_transaction(root);
  4617. if (IS_ERR(trans)) {
  4618. ret = PTR_ERR(trans);
  4619. goto out;
  4620. }
  4621. qgroupid = sa->qgroupid;
  4622. if (!qgroupid) {
  4623. /* take the current subvol as qgroup */
  4624. qgroupid = root->root_key.objectid;
  4625. }
  4626. ret = btrfs_limit_qgroup(trans, qgroupid, &sa->lim);
  4627. err = btrfs_end_transaction(trans);
  4628. if (err && !ret)
  4629. ret = err;
  4630. out:
  4631. kfree(sa);
  4632. drop_write:
  4633. mnt_drop_write_file(file);
  4634. return ret;
  4635. }
  4636. static long btrfs_ioctl_quota_rescan(struct file *file, void __user *arg)
  4637. {
  4638. struct inode *inode = file_inode(file);
  4639. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  4640. struct btrfs_ioctl_quota_rescan_args *qsa;
  4641. int ret;
  4642. if (!capable(CAP_SYS_ADMIN))
  4643. return -EPERM;
  4644. ret = mnt_want_write_file(file);
  4645. if (ret)
  4646. return ret;
  4647. qsa = memdup_user(arg, sizeof(*qsa));
  4648. if (IS_ERR(qsa)) {
  4649. ret = PTR_ERR(qsa);
  4650. goto drop_write;
  4651. }
  4652. if (qsa->flags) {
  4653. ret = -EINVAL;
  4654. goto out;
  4655. }
  4656. ret = btrfs_qgroup_rescan(fs_info);
  4657. out:
  4658. kfree(qsa);
  4659. drop_write:
  4660. mnt_drop_write_file(file);
  4661. return ret;
  4662. }
  4663. static long btrfs_ioctl_quota_rescan_status(struct file *file, void __user *arg)
  4664. {
  4665. struct inode *inode = file_inode(file);
  4666. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  4667. struct btrfs_ioctl_quota_rescan_args *qsa;
  4668. int ret = 0;
  4669. if (!capable(CAP_SYS_ADMIN))
  4670. return -EPERM;
  4671. qsa = kzalloc(sizeof(*qsa), GFP_KERNEL);
  4672. if (!qsa)
  4673. return -ENOMEM;
  4674. if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
  4675. qsa->flags = 1;
  4676. qsa->progress = fs_info->qgroup_rescan_progress.objectid;
  4677. }
  4678. if (copy_to_user(arg, qsa, sizeof(*qsa)))
  4679. ret = -EFAULT;
  4680. kfree(qsa);
  4681. return ret;
  4682. }
  4683. static long btrfs_ioctl_quota_rescan_wait(struct file *file, void __user *arg)
  4684. {
  4685. struct inode *inode = file_inode(file);
  4686. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  4687. if (!capable(CAP_SYS_ADMIN))
  4688. return -EPERM;
  4689. return btrfs_qgroup_wait_for_completion(fs_info, true);
  4690. }
  4691. static long _btrfs_ioctl_set_received_subvol(struct file *file,
  4692. struct btrfs_ioctl_received_subvol_args *sa)
  4693. {
  4694. struct inode *inode = file_inode(file);
  4695. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  4696. struct btrfs_root *root = BTRFS_I(inode)->root;
  4697. struct btrfs_root_item *root_item = &root->root_item;
  4698. struct btrfs_trans_handle *trans;
  4699. struct timespec64 ct = current_time(inode);
  4700. int ret = 0;
  4701. int received_uuid_changed;
  4702. if (!inode_owner_or_capable(inode))
  4703. return -EPERM;
  4704. ret = mnt_want_write_file(file);
  4705. if (ret < 0)
  4706. return ret;
  4707. down_write(&fs_info->subvol_sem);
  4708. if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) {
  4709. ret = -EINVAL;
  4710. goto out;
  4711. }
  4712. if (btrfs_root_readonly(root)) {
  4713. ret = -EROFS;
  4714. goto out;
  4715. }
  4716. /*
  4717. * 1 - root item
  4718. * 2 - uuid items (received uuid + subvol uuid)
  4719. */
  4720. trans = btrfs_start_transaction(root, 3);
  4721. if (IS_ERR(trans)) {
  4722. ret = PTR_ERR(trans);
  4723. trans = NULL;
  4724. goto out;
  4725. }
  4726. sa->rtransid = trans->transid;
  4727. sa->rtime.sec = ct.tv_sec;
  4728. sa->rtime.nsec = ct.tv_nsec;
  4729. received_uuid_changed = memcmp(root_item->received_uuid, sa->uuid,
  4730. BTRFS_UUID_SIZE);
  4731. if (received_uuid_changed &&
  4732. !btrfs_is_empty_uuid(root_item->received_uuid)) {
  4733. ret = btrfs_uuid_tree_remove(trans, root_item->received_uuid,
  4734. BTRFS_UUID_KEY_RECEIVED_SUBVOL,
  4735. root->root_key.objectid);
  4736. if (ret && ret != -ENOENT) {
  4737. btrfs_abort_transaction(trans, ret);
  4738. btrfs_end_transaction(trans);
  4739. goto out;
  4740. }
  4741. }
  4742. memcpy(root_item->received_uuid, sa->uuid, BTRFS_UUID_SIZE);
  4743. btrfs_set_root_stransid(root_item, sa->stransid);
  4744. btrfs_set_root_rtransid(root_item, sa->rtransid);
  4745. btrfs_set_stack_timespec_sec(&root_item->stime, sa->stime.sec);
  4746. btrfs_set_stack_timespec_nsec(&root_item->stime, sa->stime.nsec);
  4747. btrfs_set_stack_timespec_sec(&root_item->rtime, sa->rtime.sec);
  4748. btrfs_set_stack_timespec_nsec(&root_item->rtime, sa->rtime.nsec);
  4749. ret = btrfs_update_root(trans, fs_info->tree_root,
  4750. &root->root_key, &root->root_item);
  4751. if (ret < 0) {
  4752. btrfs_end_transaction(trans);
  4753. goto out;
  4754. }
  4755. if (received_uuid_changed && !btrfs_is_empty_uuid(sa->uuid)) {
  4756. ret = btrfs_uuid_tree_add(trans, sa->uuid,
  4757. BTRFS_UUID_KEY_RECEIVED_SUBVOL,
  4758. root->root_key.objectid);
  4759. if (ret < 0 && ret != -EEXIST) {
  4760. btrfs_abort_transaction(trans, ret);
  4761. btrfs_end_transaction(trans);
  4762. goto out;
  4763. }
  4764. }
  4765. ret = btrfs_commit_transaction(trans);
  4766. out:
  4767. up_write(&fs_info->subvol_sem);
  4768. mnt_drop_write_file(file);
  4769. return ret;
  4770. }
  4771. #ifdef CONFIG_64BIT
  4772. static long btrfs_ioctl_set_received_subvol_32(struct file *file,
  4773. void __user *arg)
  4774. {
  4775. struct btrfs_ioctl_received_subvol_args_32 *args32 = NULL;
  4776. struct btrfs_ioctl_received_subvol_args *args64 = NULL;
  4777. int ret = 0;
  4778. args32 = memdup_user(arg, sizeof(*args32));
  4779. if (IS_ERR(args32))
  4780. return PTR_ERR(args32);
  4781. args64 = kmalloc(sizeof(*args64), GFP_KERNEL);
  4782. if (!args64) {
  4783. ret = -ENOMEM;
  4784. goto out;
  4785. }
  4786. memcpy(args64->uuid, args32->uuid, BTRFS_UUID_SIZE);
  4787. args64->stransid = args32->stransid;
  4788. args64->rtransid = args32->rtransid;
  4789. args64->stime.sec = args32->stime.sec;
  4790. args64->stime.nsec = args32->stime.nsec;
  4791. args64->rtime.sec = args32->rtime.sec;
  4792. args64->rtime.nsec = args32->rtime.nsec;
  4793. args64->flags = args32->flags;
  4794. ret = _btrfs_ioctl_set_received_subvol(file, args64);
  4795. if (ret)
  4796. goto out;
  4797. memcpy(args32->uuid, args64->uuid, BTRFS_UUID_SIZE);
  4798. args32->stransid = args64->stransid;
  4799. args32->rtransid = args64->rtransid;
  4800. args32->stime.sec = args64->stime.sec;
  4801. args32->stime.nsec = args64->stime.nsec;
  4802. args32->rtime.sec = args64->rtime.sec;
  4803. args32->rtime.nsec = args64->rtime.nsec;
  4804. args32->flags = args64->flags;
  4805. ret = copy_to_user(arg, args32, sizeof(*args32));
  4806. if (ret)
  4807. ret = -EFAULT;
  4808. out:
  4809. kfree(args32);
  4810. kfree(args64);
  4811. return ret;
  4812. }
  4813. #endif
  4814. static long btrfs_ioctl_set_received_subvol(struct file *file,
  4815. void __user *arg)
  4816. {
  4817. struct btrfs_ioctl_received_subvol_args *sa = NULL;
  4818. int ret = 0;
  4819. sa = memdup_user(arg, sizeof(*sa));
  4820. if (IS_ERR(sa))
  4821. return PTR_ERR(sa);
  4822. ret = _btrfs_ioctl_set_received_subvol(file, sa);
  4823. if (ret)
  4824. goto out;
  4825. ret = copy_to_user(arg, sa, sizeof(*sa));
  4826. if (ret)
  4827. ret = -EFAULT;
  4828. out:
  4829. kfree(sa);
  4830. return ret;
  4831. }
  4832. static int btrfs_ioctl_get_fslabel(struct file *file, void __user *arg)
  4833. {
  4834. struct inode *inode = file_inode(file);
  4835. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  4836. size_t len;
  4837. int ret;
  4838. char label[BTRFS_LABEL_SIZE];
  4839. spin_lock(&fs_info->super_lock);
  4840. memcpy(label, fs_info->super_copy->label, BTRFS_LABEL_SIZE);
  4841. spin_unlock(&fs_info->super_lock);
  4842. len = strnlen(label, BTRFS_LABEL_SIZE);
  4843. if (len == BTRFS_LABEL_SIZE) {
  4844. btrfs_warn(fs_info,
  4845. "label is too long, return the first %zu bytes",
  4846. --len);
  4847. }
  4848. ret = copy_to_user(arg, label, len);
  4849. return ret ? -EFAULT : 0;
  4850. }
  4851. static int btrfs_ioctl_set_fslabel(struct file *file, void __user *arg)
  4852. {
  4853. struct inode *inode = file_inode(file);
  4854. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  4855. struct btrfs_root *root = BTRFS_I(inode)->root;
  4856. struct btrfs_super_block *super_block = fs_info->super_copy;
  4857. struct btrfs_trans_handle *trans;
  4858. char label[BTRFS_LABEL_SIZE];
  4859. int ret;
  4860. if (!capable(CAP_SYS_ADMIN))
  4861. return -EPERM;
  4862. if (copy_from_user(label, arg, sizeof(label)))
  4863. return -EFAULT;
  4864. if (strnlen(label, BTRFS_LABEL_SIZE) == BTRFS_LABEL_SIZE) {
  4865. btrfs_err(fs_info,
  4866. "unable to set label with more than %d bytes",
  4867. BTRFS_LABEL_SIZE - 1);
  4868. return -EINVAL;
  4869. }
  4870. ret = mnt_want_write_file(file);
  4871. if (ret)
  4872. return ret;
  4873. trans = btrfs_start_transaction(root, 0);
  4874. if (IS_ERR(trans)) {
  4875. ret = PTR_ERR(trans);
  4876. goto out_unlock;
  4877. }
  4878. spin_lock(&fs_info->super_lock);
  4879. strcpy(super_block->label, label);
  4880. spin_unlock(&fs_info->super_lock);
  4881. ret = btrfs_commit_transaction(trans);
  4882. out_unlock:
  4883. mnt_drop_write_file(file);
  4884. return ret;
  4885. }
  4886. #define INIT_FEATURE_FLAGS(suffix) \
  4887. { .compat_flags = BTRFS_FEATURE_COMPAT_##suffix, \
  4888. .compat_ro_flags = BTRFS_FEATURE_COMPAT_RO_##suffix, \
  4889. .incompat_flags = BTRFS_FEATURE_INCOMPAT_##suffix }
  4890. int btrfs_ioctl_get_supported_features(void __user *arg)
  4891. {
  4892. static const struct btrfs_ioctl_feature_flags features[3] = {
  4893. INIT_FEATURE_FLAGS(SUPP),
  4894. INIT_FEATURE_FLAGS(SAFE_SET),
  4895. INIT_FEATURE_FLAGS(SAFE_CLEAR)
  4896. };
  4897. if (copy_to_user(arg, &features, sizeof(features)))
  4898. return -EFAULT;
  4899. return 0;
  4900. }
  4901. static int btrfs_ioctl_get_features(struct file *file, void __user *arg)
  4902. {
  4903. struct inode *inode = file_inode(file);
  4904. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  4905. struct btrfs_super_block *super_block = fs_info->super_copy;
  4906. struct btrfs_ioctl_feature_flags features;
  4907. features.compat_flags = btrfs_super_compat_flags(super_block);
  4908. features.compat_ro_flags = btrfs_super_compat_ro_flags(super_block);
  4909. features.incompat_flags = btrfs_super_incompat_flags(super_block);
  4910. if (copy_to_user(arg, &features, sizeof(features)))
  4911. return -EFAULT;
  4912. return 0;
  4913. }
  4914. static int check_feature_bits(struct btrfs_fs_info *fs_info,
  4915. enum btrfs_feature_set set,
  4916. u64 change_mask, u64 flags, u64 supported_flags,
  4917. u64 safe_set, u64 safe_clear)
  4918. {
  4919. const char *type = btrfs_feature_set_names[set];
  4920. char *names;
  4921. u64 disallowed, unsupported;
  4922. u64 set_mask = flags & change_mask;
  4923. u64 clear_mask = ~flags & change_mask;
  4924. unsupported = set_mask & ~supported_flags;
  4925. if (unsupported) {
  4926. names = btrfs_printable_features(set, unsupported);
  4927. if (names) {
  4928. btrfs_warn(fs_info,
  4929. "this kernel does not support the %s feature bit%s",
  4930. names, strchr(names, ',') ? "s" : "");
  4931. kfree(names);
  4932. } else
  4933. btrfs_warn(fs_info,
  4934. "this kernel does not support %s bits 0x%llx",
  4935. type, unsupported);
  4936. return -EOPNOTSUPP;
  4937. }
  4938. disallowed = set_mask & ~safe_set;
  4939. if (disallowed) {
  4940. names = btrfs_printable_features(set, disallowed);
  4941. if (names) {
  4942. btrfs_warn(fs_info,
  4943. "can't set the %s feature bit%s while mounted",
  4944. names, strchr(names, ',') ? "s" : "");
  4945. kfree(names);
  4946. } else
  4947. btrfs_warn(fs_info,
  4948. "can't set %s bits 0x%llx while mounted",
  4949. type, disallowed);
  4950. return -EPERM;
  4951. }
  4952. disallowed = clear_mask & ~safe_clear;
  4953. if (disallowed) {
  4954. names = btrfs_printable_features(set, disallowed);
  4955. if (names) {
  4956. btrfs_warn(fs_info,
  4957. "can't clear the %s feature bit%s while mounted",
  4958. names, strchr(names, ',') ? "s" : "");
  4959. kfree(names);
  4960. } else
  4961. btrfs_warn(fs_info,
  4962. "can't clear %s bits 0x%llx while mounted",
  4963. type, disallowed);
  4964. return -EPERM;
  4965. }
  4966. return 0;
  4967. }
  4968. #define check_feature(fs_info, change_mask, flags, mask_base) \
  4969. check_feature_bits(fs_info, FEAT_##mask_base, change_mask, flags, \
  4970. BTRFS_FEATURE_ ## mask_base ## _SUPP, \
  4971. BTRFS_FEATURE_ ## mask_base ## _SAFE_SET, \
  4972. BTRFS_FEATURE_ ## mask_base ## _SAFE_CLEAR)
  4973. static int btrfs_ioctl_set_features(struct file *file, void __user *arg)
  4974. {
  4975. struct inode *inode = file_inode(file);
  4976. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  4977. struct btrfs_root *root = BTRFS_I(inode)->root;
  4978. struct btrfs_super_block *super_block = fs_info->super_copy;
  4979. struct btrfs_ioctl_feature_flags flags[2];
  4980. struct btrfs_trans_handle *trans;
  4981. u64 newflags;
  4982. int ret;
  4983. if (!capable(CAP_SYS_ADMIN))
  4984. return -EPERM;
  4985. if (copy_from_user(flags, arg, sizeof(flags)))
  4986. return -EFAULT;
  4987. /* Nothing to do */
  4988. if (!flags[0].compat_flags && !flags[0].compat_ro_flags &&
  4989. !flags[0].incompat_flags)
  4990. return 0;
  4991. ret = check_feature(fs_info, flags[0].compat_flags,
  4992. flags[1].compat_flags, COMPAT);
  4993. if (ret)
  4994. return ret;
  4995. ret = check_feature(fs_info, flags[0].compat_ro_flags,
  4996. flags[1].compat_ro_flags, COMPAT_RO);
  4997. if (ret)
  4998. return ret;
  4999. ret = check_feature(fs_info, flags[0].incompat_flags,
  5000. flags[1].incompat_flags, INCOMPAT);
  5001. if (ret)
  5002. return ret;
  5003. ret = mnt_want_write_file(file);
  5004. if (ret)
  5005. return ret;
  5006. trans = btrfs_start_transaction(root, 0);
  5007. if (IS_ERR(trans)) {
  5008. ret = PTR_ERR(trans);
  5009. goto out_drop_write;
  5010. }
  5011. spin_lock(&fs_info->super_lock);
  5012. newflags = btrfs_super_compat_flags(super_block);
  5013. newflags |= flags[0].compat_flags & flags[1].compat_flags;
  5014. newflags &= ~(flags[0].compat_flags & ~flags[1].compat_flags);
  5015. btrfs_set_super_compat_flags(super_block, newflags);
  5016. newflags = btrfs_super_compat_ro_flags(super_block);
  5017. newflags |= flags[0].compat_ro_flags & flags[1].compat_ro_flags;
  5018. newflags &= ~(flags[0].compat_ro_flags & ~flags[1].compat_ro_flags);
  5019. btrfs_set_super_compat_ro_flags(super_block, newflags);
  5020. newflags = btrfs_super_incompat_flags(super_block);
  5021. newflags |= flags[0].incompat_flags & flags[1].incompat_flags;
  5022. newflags &= ~(flags[0].incompat_flags & ~flags[1].incompat_flags);
  5023. btrfs_set_super_incompat_flags(super_block, newflags);
  5024. spin_unlock(&fs_info->super_lock);
  5025. ret = btrfs_commit_transaction(trans);
  5026. out_drop_write:
  5027. mnt_drop_write_file(file);
  5028. return ret;
  5029. }
  5030. static int _btrfs_ioctl_send(struct file *file, void __user *argp, bool compat)
  5031. {
  5032. struct btrfs_ioctl_send_args *arg;
  5033. int ret;
  5034. if (compat) {
  5035. #if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
  5036. struct btrfs_ioctl_send_args_32 args32;
  5037. ret = copy_from_user(&args32, argp, sizeof(args32));
  5038. if (ret)
  5039. return -EFAULT;
  5040. arg = kzalloc(sizeof(*arg), GFP_KERNEL);
  5041. if (!arg)
  5042. return -ENOMEM;
  5043. arg->send_fd = args32.send_fd;
  5044. arg->clone_sources_count = args32.clone_sources_count;
  5045. arg->clone_sources = compat_ptr(args32.clone_sources);
  5046. arg->parent_root = args32.parent_root;
  5047. arg->flags = args32.flags;
  5048. memcpy(arg->reserved, args32.reserved,
  5049. sizeof(args32.reserved));
  5050. #else
  5051. return -ENOTTY;
  5052. #endif
  5053. } else {
  5054. arg = memdup_user(argp, sizeof(*arg));
  5055. if (IS_ERR(arg))
  5056. return PTR_ERR(arg);
  5057. }
  5058. ret = btrfs_ioctl_send(file, arg);
  5059. kfree(arg);
  5060. return ret;
  5061. }
  5062. long btrfs_ioctl(struct file *file, unsigned int
  5063. cmd, unsigned long arg)
  5064. {
  5065. struct inode *inode = file_inode(file);
  5066. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  5067. struct btrfs_root *root = BTRFS_I(inode)->root;
  5068. void __user *argp = (void __user *)arg;
  5069. switch (cmd) {
  5070. case FS_IOC_GETFLAGS:
  5071. return btrfs_ioctl_getflags(file, argp);
  5072. case FS_IOC_SETFLAGS:
  5073. return btrfs_ioctl_setflags(file, argp);
  5074. case FS_IOC_GETVERSION:
  5075. return btrfs_ioctl_getversion(file, argp);
  5076. case FITRIM:
  5077. return btrfs_ioctl_fitrim(file, argp);
  5078. case BTRFS_IOC_SNAP_CREATE:
  5079. return btrfs_ioctl_snap_create(file, argp, 0);
  5080. case BTRFS_IOC_SNAP_CREATE_V2:
  5081. return btrfs_ioctl_snap_create_v2(file, argp, 0);
  5082. case BTRFS_IOC_SUBVOL_CREATE:
  5083. return btrfs_ioctl_snap_create(file, argp, 1);
  5084. case BTRFS_IOC_SUBVOL_CREATE_V2:
  5085. return btrfs_ioctl_snap_create_v2(file, argp, 1);
  5086. case BTRFS_IOC_SNAP_DESTROY:
  5087. return btrfs_ioctl_snap_destroy(file, argp);
  5088. case BTRFS_IOC_SUBVOL_GETFLAGS:
  5089. return btrfs_ioctl_subvol_getflags(file, argp);
  5090. case BTRFS_IOC_SUBVOL_SETFLAGS:
  5091. return btrfs_ioctl_subvol_setflags(file, argp);
  5092. case BTRFS_IOC_DEFAULT_SUBVOL:
  5093. return btrfs_ioctl_default_subvol(file, argp);
  5094. case BTRFS_IOC_DEFRAG:
  5095. return btrfs_ioctl_defrag(file, NULL);
  5096. case BTRFS_IOC_DEFRAG_RANGE:
  5097. return btrfs_ioctl_defrag(file, argp);
  5098. case BTRFS_IOC_RESIZE:
  5099. return btrfs_ioctl_resize(file, argp);
  5100. case BTRFS_IOC_ADD_DEV:
  5101. return btrfs_ioctl_add_dev(fs_info, argp);
  5102. case BTRFS_IOC_RM_DEV:
  5103. return btrfs_ioctl_rm_dev(file, argp);
  5104. case BTRFS_IOC_RM_DEV_V2:
  5105. return btrfs_ioctl_rm_dev_v2(file, argp);
  5106. case BTRFS_IOC_FS_INFO:
  5107. return btrfs_ioctl_fs_info(fs_info, argp);
  5108. case BTRFS_IOC_DEV_INFO:
  5109. return btrfs_ioctl_dev_info(fs_info, argp);
  5110. case BTRFS_IOC_BALANCE:
  5111. return btrfs_ioctl_balance(file, NULL);
  5112. case BTRFS_IOC_TREE_SEARCH:
  5113. return btrfs_ioctl_tree_search(file, argp);
  5114. case BTRFS_IOC_TREE_SEARCH_V2:
  5115. return btrfs_ioctl_tree_search_v2(file, argp);
  5116. case BTRFS_IOC_INO_LOOKUP:
  5117. return btrfs_ioctl_ino_lookup(file, argp);
  5118. case BTRFS_IOC_INO_PATHS:
  5119. return btrfs_ioctl_ino_to_path(root, argp);
  5120. case BTRFS_IOC_LOGICAL_INO:
  5121. return btrfs_ioctl_logical_to_ino(fs_info, argp, 1);
  5122. case BTRFS_IOC_LOGICAL_INO_V2:
  5123. return btrfs_ioctl_logical_to_ino(fs_info, argp, 2);
  5124. case BTRFS_IOC_SPACE_INFO:
  5125. return btrfs_ioctl_space_info(fs_info, argp);
  5126. case BTRFS_IOC_SYNC: {
  5127. int ret;
  5128. ret = btrfs_start_delalloc_roots(fs_info, -1);
  5129. if (ret)
  5130. return ret;
  5131. ret = btrfs_sync_fs(inode->i_sb, 1);
  5132. /*
  5133. * The transaction thread may want to do more work,
  5134. * namely it pokes the cleaner kthread that will start
  5135. * processing uncleaned subvols.
  5136. */
  5137. wake_up_process(fs_info->transaction_kthread);
  5138. return ret;
  5139. }
  5140. case BTRFS_IOC_START_SYNC:
  5141. return btrfs_ioctl_start_sync(root, argp);
  5142. case BTRFS_IOC_WAIT_SYNC:
  5143. return btrfs_ioctl_wait_sync(fs_info, argp);
  5144. case BTRFS_IOC_SCRUB:
  5145. return btrfs_ioctl_scrub(file, argp);
  5146. case BTRFS_IOC_SCRUB_CANCEL:
  5147. return btrfs_ioctl_scrub_cancel(fs_info);
  5148. case BTRFS_IOC_SCRUB_PROGRESS:
  5149. return btrfs_ioctl_scrub_progress(fs_info, argp);
  5150. case BTRFS_IOC_BALANCE_V2:
  5151. return btrfs_ioctl_balance(file, argp);
  5152. case BTRFS_IOC_BALANCE_CTL:
  5153. return btrfs_ioctl_balance_ctl(fs_info, arg);
  5154. case BTRFS_IOC_BALANCE_PROGRESS:
  5155. return btrfs_ioctl_balance_progress(fs_info, argp);
  5156. case BTRFS_IOC_SET_RECEIVED_SUBVOL:
  5157. return btrfs_ioctl_set_received_subvol(file, argp);
  5158. #ifdef CONFIG_64BIT
  5159. case BTRFS_IOC_SET_RECEIVED_SUBVOL_32:
  5160. return btrfs_ioctl_set_received_subvol_32(file, argp);
  5161. #endif
  5162. case BTRFS_IOC_SEND:
  5163. return _btrfs_ioctl_send(file, argp, false);
  5164. #if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
  5165. case BTRFS_IOC_SEND_32:
  5166. return _btrfs_ioctl_send(file, argp, true);
  5167. #endif
  5168. case BTRFS_IOC_GET_DEV_STATS:
  5169. return btrfs_ioctl_get_dev_stats(fs_info, argp);
  5170. case BTRFS_IOC_QUOTA_CTL:
  5171. return btrfs_ioctl_quota_ctl(file, argp);
  5172. case BTRFS_IOC_QGROUP_ASSIGN:
  5173. return btrfs_ioctl_qgroup_assign(file, argp);
  5174. case BTRFS_IOC_QGROUP_CREATE:
  5175. return btrfs_ioctl_qgroup_create(file, argp);
  5176. case BTRFS_IOC_QGROUP_LIMIT:
  5177. return btrfs_ioctl_qgroup_limit(file, argp);
  5178. case BTRFS_IOC_QUOTA_RESCAN:
  5179. return btrfs_ioctl_quota_rescan(file, argp);
  5180. case BTRFS_IOC_QUOTA_RESCAN_STATUS:
  5181. return btrfs_ioctl_quota_rescan_status(file, argp);
  5182. case BTRFS_IOC_QUOTA_RESCAN_WAIT:
  5183. return btrfs_ioctl_quota_rescan_wait(file, argp);
  5184. case BTRFS_IOC_DEV_REPLACE:
  5185. return btrfs_ioctl_dev_replace(fs_info, argp);
  5186. case BTRFS_IOC_GET_FSLABEL:
  5187. return btrfs_ioctl_get_fslabel(file, argp);
  5188. case BTRFS_IOC_SET_FSLABEL:
  5189. return btrfs_ioctl_set_fslabel(file, argp);
  5190. case BTRFS_IOC_GET_SUPPORTED_FEATURES:
  5191. return btrfs_ioctl_get_supported_features(argp);
  5192. case BTRFS_IOC_GET_FEATURES:
  5193. return btrfs_ioctl_get_features(file, argp);
  5194. case BTRFS_IOC_SET_FEATURES:
  5195. return btrfs_ioctl_set_features(file, argp);
  5196. case FS_IOC_FSGETXATTR:
  5197. return btrfs_ioctl_fsgetxattr(file, argp);
  5198. case FS_IOC_FSSETXATTR:
  5199. return btrfs_ioctl_fssetxattr(file, argp);
  5200. case BTRFS_IOC_GET_SUBVOL_INFO:
  5201. return btrfs_ioctl_get_subvol_info(file, argp);
  5202. case BTRFS_IOC_GET_SUBVOL_ROOTREF:
  5203. return btrfs_ioctl_get_subvol_rootref(file, argp);
  5204. case BTRFS_IOC_INO_LOOKUP_USER:
  5205. return btrfs_ioctl_ino_lookup_user(file, argp);
  5206. }
  5207. return -ENOTTY;
  5208. }
  5209. #ifdef CONFIG_COMPAT
  5210. long btrfs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  5211. {
  5212. /*
  5213. * These all access 32-bit values anyway so no further
  5214. * handling is necessary.
  5215. */
  5216. switch (cmd) {
  5217. case FS_IOC32_GETFLAGS:
  5218. cmd = FS_IOC_GETFLAGS;
  5219. break;
  5220. case FS_IOC32_SETFLAGS:
  5221. cmd = FS_IOC_SETFLAGS;
  5222. break;
  5223. case FS_IOC32_GETVERSION:
  5224. cmd = FS_IOC_GETVERSION;
  5225. break;
  5226. }
  5227. return btrfs_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
  5228. }
  5229. #endif