ioctl.c 145 KB

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